Skip to content

Commit

Permalink
Version 3.0 release
Browse files Browse the repository at this point in the history
  • Loading branch information
metafloor committed Apr 28, 2021
1 parent cbf8301 commit 6dac9f9
Show file tree
Hide file tree
Showing 15 changed files with 34,380 additions and 33,975 deletions.
80 changes: 55 additions & 25 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,26 @@ All linear and two-dimensional barcodes in common use (and many uncommon
ones) are available. An exhaustive list of supported barcode types can be
found at the end of this document.

> Version 3.0 provides support for ES6 modules with the individual encoders as named exports.
> This will allow webpack and other bundlers to tree shake the large BWIPP cross-compiled
> code into something more managable.
>
> The ES6 module is only available on the browser. Support for nodejs is planned.
>
> There is minor API breakage versus version 2. The exported version strings have changed
> since there is no longer a `BWIPP` object - its contents were hoisted to module scope to
> enable dead code elimination.
>
> | Old Version String | 3.0 Version String |
> | :------------------------| :------------------------|
> | `bwipjs.VERSION` | `bwipjs.BWIPJS_VERSION` |
> | `bwipjs.BWIPP.VERSION` | `bwipjs.BWIPP_VERSION` |
>
> See the section [ES6 Browser Module Usage](#es6-browser-module-usage) for details of the new capabilities.
## Status

* Current bwip-js version is 2.1.3 (2021-04-08)
* Current bwip-js version is 3.0.0 (2021-04-28)
* Current BWIPP version is 2021-02-06
* Node.js compatibility: 0.12+
* Browser compatibility: Edge, Firefox, Chrome
Expand All @@ -24,17 +41,6 @@ found at the end of this document.
* [Electron](#electron-example)
* [Command Line](#command-line-interface)

## JavaScript Requirements

Emulating PostScript is non-trivial but most of the needed functionality is available
in relatively modern JavaScript. The basic features required are:

- Uint8Array
- Map (*for-of* iteration not used so older JS works)

If your JavaScript environment does not support those, there is no way to make this
code work.

## Links

* [Home Page](http://metafloor.github.io/bwip-js/)
Expand Down Expand Up @@ -123,8 +129,7 @@ Note that bwip-js normalizes the BWIPP `width` and `height` options to always be
The resulting images are rendered at 72 dpi. To convert to pixels, use a factor of 2.835 px/mm
(72 dpi / 25.4 mm/in). The bwip-js scale options multiply the `width`, `height`, and `padding`.

| :warning: An important note about the BWIPP `width` and `height` parameters. |
|---------------|
> An important note about the BWIPP `width` and `height` parameters.
Barcodes have the concept of module width (and height if a two-dimensional barcode). For
linear barcodes, the module width is the width of the narrowest bar, and all other bar widths are
Expand Down Expand Up @@ -166,13 +171,13 @@ To use within a browser, add the following to the head of your page:
<script type="text/javascript" src="file-or-url-path-to/bwip-js/dist/bwip-js-min.js"></script>
```

When developing your code, you may want to use `dist/bwip-js.js` to get better stack
While developing your project, you may want to use `dist/bwip-js.js` to get better stack
traces.

If you are using `RequireJS` or another module/packaging utility, the bwip-js script is
structured as a UMD and should work with your environment.
If you are using `RequireJS` or a common-js bundling utility, the bwip-js scripts are
structured as UMDs and should work with your environment.

The script adds a single `bwipjs` global object. To draw a barcode to a canvas:
The scripts adds a single `bwipjs` global object. To draw a barcode to a canvas:

```javascript
try {
Expand Down Expand Up @@ -208,15 +213,41 @@ to get a data URL. For example:
let canvas = document.createElement('canvas');
try {
bwipjs.toCanvas(canvas, options);
document.getElementById(myimg).src = canvas.toDataURL('image/png');
document.getElementById('my-img').src = canvas.toDataURL('image/png');
} catch (e) {
// `e` may be a string or Error object
}
```

## ES6 Browser Module Usage

The ESM provides the same API as the standard browser module using:

```javascript
import bwipjs from 'bwip-js';

// ... identical bwipjs.toCanvas() interface as above ...
```

The ESM also facilitates bundler tree shaking by providing the individual encoders as named exports.
Each exported encoder functions identically to `bwipjs.toCanvas()`.

The exported names are the same as the `bcid` names, with the caveat that dashes `-` are replaced with
underscores `_`. For example, if you want to import the `gs1-128` encoder, you would use:

```javascript
import { gs1_128 } from 'bwip-js';

try {
gs1_128('my-canvas', options);
} catch (e) {
// `e` may be a string or Error object
}
```

## React Usage

The following is a minimal example of using bwip-js in a React app.
The following is a minimal example of bwip-js in a React app.
It is based on the default `App.js` file generated by `create-react-app`.

```javascript
Expand Down Expand Up @@ -258,6 +289,8 @@ export default App;

See the Browser Usage section for details on the `toCanvas()` method.

See the ES6 Browser Module Usage section for details on importing encoders directly.

## Node.js Request Handler

The online barcode API is implemented as a Node.js application.
Expand Down Expand Up @@ -349,15 +382,12 @@ bwipjs.toBuffer({

There has been some changes to the Electron bundler, and it may pull in either the
nodejs or browser module, depending on your version of Electron. The example below
shows using the nodejs module.
assumes the nodejs module.

If you try this example and get the error `bwipjs.toBuffer is not a function`, the
Electron bundler grabbed the browser module. See the [Browser Usage](#browser-usage)
section above and draw to a canvas instead.

If you happen to know how the Electron bundler changed its behavior and how to
fix it in a project's `package.json`, please raise an issue at [github issues page](https://github.com/metafloor/bwip-js/issues).

This is an example `index.html` file for a basic, single window app:

```html
Expand Down Expand Up @@ -393,7 +423,7 @@ This is an example `index.html` file for a basic, single window app:

## Command Line Interface

bwip-js can be used as a command line tool.
bwip-js can be used as a command line tool when installed globally:

```
$ npm install -g bwip-js
Expand Down
3 changes: 2 additions & 1 deletion bin/bwip-js.js
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,7 @@ var opts = {};

if (process.argv.length < 5) {
usage();
process.exit(1);
}
if (!/bcid=/.test(process.argv[2])) {
process.argv[2] = 'bcid=' + process.argv[2];
Expand Down Expand Up @@ -255,7 +256,7 @@ if (process.argv.length == 2) {
} else if (opts.help) {
help();
} else if (opts.version && !opts.bcid) {
console.log('bwip-js: ' + bwipjs.VERSION + '\nBWIPP: ' + bwipjs.BWIPP.VERSION);
console.log('bwip-js: ' + bwipjs.BWIPJS_VERSION + '\nBWIPP: ' + bwipjs.BWIPP_VERSION);
} else if (opts.symbols && !opts.bcid) {
for (var sym in symdesc) {
console.log(' ' + sym + ' : ' + symdesc[sym]);
Expand Down
4 changes: 2 additions & 2 deletions demo.html
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@
});

document.getElementById('versions').textContent =
'bwip-js ' + bwipjs.VERSION + ' / BWIPP ' + bwipjs.BWIPP.VERSION;
'bwip-js ' + bwipjs.BWIPJS_VERSION + ' / BWIPP ' + bwipjs.BWIPP_VERSION;

// A reasonable match to OCR-B metrics.
bwipjs.loadFont("Inconsolata", 95, 105, Inconsolata);
Expand Down Expand Up @@ -211,7 +211,7 @@
</script>
</head><body>
<div id="header">
<div id="bwipjs">bwip-js // Barcode Writer in Pure JavaScript</div>
<div id="bwip-js">bwip-js // Barcode Writer in Pure JavaScript</div>
<div id="versions"></div>
</div>
<div id="params">
Expand Down
2 changes: 1 addition & 1 deletion dist/bwip-js-min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/bwip-js-node.js
Original file line number Diff line number Diff line change
Expand Up @@ -38769,7 +38769,7 @@ module.exports = {
request:Request, toBuffer:ToBuffer, render:Render, raw:ToRaw,
fixupOptions:FixupOptions,
loadFont:FontLib.loadFont,
BWIPJS_VERSION:'__BWIPJS_VERS__',
BWIPJS_VERSION:'3.0.0 (2021-04-28)',
BWIPP_VERSION:BWIPP_VERSION,
// Internals
BWIPJS:BWIPJS, STBTT:STBTT, FontLib:FontLib,
Expand Down
2 changes: 1 addition & 1 deletion dist/bwip-js.js
Original file line number Diff line number Diff line change
Expand Up @@ -38607,7 +38607,7 @@ FontLib.loadFont("OCR-B", 96, 100, "AAEAAAAPAIAAAwBwRkZUTXxHn14AADmUAAAAHEdERUYA
toCanvas:ToCanvas, render:Render, raw:ToRaw,
fixupOptions:FixupOptions,
loadFont:FontLib.loadFont,
BWIPJS_VERSION:'__BWIPJS_VERS__',
BWIPJS_VERSION:'3.0.0 (2021-04-28)',
BWIPP_VERSION:BWIPP_VERSION,
// Internals
BWIPJS:BWIPJS, STBTT:STBTT, FontLib:FontLib,
Expand Down
2 changes: 1 addition & 1 deletion examples/example.html
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@
});

document.getElementById('versions').textContent =
'bwip-js ' + bwipjs.VERSION + ' / BWIPP ' + bwipjs.BWIPP.VERSION;
'bwip-js ' + bwipjs.BWIPJS_VERSION + ' / BWIPP ' + bwipjs.BWIPP_VERSION;
});

function render() {
Expand Down
2 changes: 1 addition & 1 deletion examples/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ const bwipjs = (function() {
}
})();

console.log('bwip-js', bwipjs.VERSION, 'BWIPP', bwipjs.BWIPP.VERSION);
console.log('bwip-js ' + bwipjs.BWIPJS_VERSION + ' / BWIPP ' + bwipjs.BWIPP_VERSION);

// Optionally, load custom fonts. This shows how to load the Inconsolata font,
// supplied with the bwip-js distribution. The path to your fonts will be different.
Expand Down
2 changes: 1 addition & 1 deletion examples/svg.html
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@
});

document.getElementById('versions').textContent =
'bwip-js ' + bwipjs.VERSION + ' / BWIPP ' + bwipjs.BWIPP.VERSION;
'bwip-js ' + bwipjs.BWIPJS_VERSION + ' / BWIPP ' + bwipjs.BWIPP_VERSION;
});

function render() {
Expand Down
2 changes: 1 addition & 1 deletion lib/demo.css
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ body {
padding-bottom: 20px;
margin-bottom: 17px;
}
#header #bwipjs {
#header #bwip-js {
font-size: 25px;
font-family: "Lucida Grande",Calibri,Helvetica,Arial,sans-serif;
font-weight: bold;
Expand Down
15 changes: 13 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,9 +1,20 @@
{
"name": "bwip-js",
"version": "2.1.3",
"version": "3.0.0",
"description": "JavaScript barcode generator supporting over 100 types and standards.",
"main": "./dist/node-bwipjs.js",
"main": "./dist/bwip-js-node.js",
"browser": "./dist/bwip-js.js",
"exports": {
"browser": {
"import": "./dist/bwip-js.mjs",
"require": "./dist/bwip-js.js",
"script": "./dist/bwip-js-min.js"
},
"electron": "./dist/bwip-js-node.js",
"node": {
"require": "./dist/bwip-js-node.js"
}
},
"bin": {
"bwip-js": "./bin/bwip-js.js"
},
Expand Down
1 change: 0 additions & 1 deletion src/bwipjs.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
// Graphics-context interface to the BWIPP cross-compiled code

var BWIPJS = (function() {
"use strict";

// Math.floor(), etc. are notoriously slow. Caching seems to help.
var floor = Math.floor;
Expand Down
Loading

0 comments on commit 6dac9f9

Please sign in to comment.