@sythos/js_barcode_universal 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (53) hide show
  1. package/LICENSE +215 -0
  2. package/NOTICE.md +106 -0
  3. package/README.md +433 -0
  4. package/bundle/sythos-barcode.esm.js +7998 -0
  5. package/bundle/sythos-barcode.js +7948 -0
  6. package/examples/create.html +731 -0
  7. package/examples/read.html +341 -0
  8. package/licenses/README.md +42 -0
  9. package/licenses/codabar.license +74 -0
  10. package/licenses/code-11.license +69 -0
  11. package/licenses/code-128.license +69 -0
  12. package/licenses/code-39.license +70 -0
  13. package/licenses/code-93.license +71 -0
  14. package/licenses/ean-13.license +70 -0
  15. package/licenses/ean-8.license +70 -0
  16. package/licenses/gs1-128.license +71 -0
  17. package/licenses/isbn.license +76 -0
  18. package/licenses/itf-14.license +69 -0
  19. package/licenses/itf.license +70 -0
  20. package/licenses/msi-plessey.license +72 -0
  21. package/licenses/pharmacode.license +71 -0
  22. package/licenses/qr-code.license +75 -0
  23. package/licenses/upc-a.license +72 -0
  24. package/licenses/upc-e.license +69 -0
  25. package/package.json +89 -0
  26. package/src/core/bit-buffer.js +174 -0
  27. package/src/core/bit-matrix.js +241 -0
  28. package/src/core/errors.js +61 -0
  29. package/src/core/galois-field.js +204 -0
  30. package/src/core/index.js +56 -0
  31. package/src/core/reed-solomon.js +313 -0
  32. package/src/image/binarizer.js +270 -0
  33. package/src/image/grid-sampler.js +164 -0
  34. package/src/image/index.js +40 -0
  35. package/src/image/luminance.js +196 -0
  36. package/src/image/perspective.js +195 -0
  37. package/src/index.js +240 -0
  38. package/src/oned/index.js +89 -0
  39. package/src/oned/patterns.js +384 -0
  40. package/src/oned/reader.js +918 -0
  41. package/src/oned/writers.js +741 -0
  42. package/src/qr/decoder.js +575 -0
  43. package/src/qr/detector.js +630 -0
  44. package/src/qr/encoder.js +958 -0
  45. package/src/qr/index.js +44 -0
  46. package/src/qr/tables.js +737 -0
  47. package/src/render/image-data.js +125 -0
  48. package/src/render/index.js +130 -0
  49. package/src/render/options.js +160 -0
  50. package/src/render/png.js +295 -0
  51. package/src/render/svg.js +120 -0
  52. package/src/render/webgl.js +206 -0
  53. package/src/render/webgpu.js +369 -0
@@ -0,0 +1,125 @@
1
+ /*!
2
+ * Sythos Barcode Suite
3
+ *
4
+ * MIT License
5
+ *
6
+ * Copyright (c) 2026 Sythos
7
+ *
8
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
9
+ * of this software and associated documentation files (the "Software"), to deal
10
+ * in the Software without restriction, including without limitation the rights
11
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12
+ * copies of the Software, and to permit persons to whom the Software is
13
+ * furnished to do so, subject to the following conditions:
14
+ *
15
+ * The above copyright notice and this permission notice shall be included in all
16
+ * copies or substantial portions of the Software.
17
+ *
18
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
24
+ * SOFTWARE.
25
+ *
26
+ * SPDX-License-Identifier: MIT
27
+ *
28
+ * Original work. No code from any other barcode implementation.
29
+ */
30
+
31
+ /**
32
+ * Raster output as ImageData, and 2D-canvas drawing.
33
+ *
34
+ * @module render/image-data
35
+ */
36
+
37
+ import { normalizeOptions, parseColor } from './options.js';
38
+
39
+ /**
40
+ * Render to an `ImageData`-shaped object.
41
+ *
42
+ * A plain object rather than a real `ImageData`, so this works in Node and in
43
+ * workers without a DOM. It is accepted directly by `ctx.putImageData` in the
44
+ * browser, and by this library's own reader.
45
+ *
46
+ * @param {import('../core/bit-matrix.js').BitMatrix} matrix
47
+ * @param {import('./options.js').RenderOptions} [options]
48
+ * @returns {{data: Uint8ClampedArray, width: number, height: number}}
49
+ */
50
+ export function toImageData(matrix, options = {}) {
51
+ const opts = normalizeOptions(matrix, options);
52
+ const { scale, source, pixelWidth, pixelHeight } = opts;
53
+
54
+ const dark = parseColor(opts.dark);
55
+ const light = parseColor(opts.light);
56
+ const data = new Uint8ClampedArray(pixelWidth * pixelHeight * 4);
57
+
58
+ // Fill one scanline per module row, then copy it `scale` times. Barcodes are
59
+ // wide and repetitive, so this is markedly faster than a per-pixel loop.
60
+ const line = new Uint8ClampedArray(pixelWidth * 4);
61
+
62
+ for (let my = 0; my < source.height; my++) {
63
+ for (let mx = 0; mx < source.width; mx++) {
64
+ const colour = source.get(mx, my) ? dark : light;
65
+ const start = mx * scale * 4;
66
+ for (let px = 0; px < scale; px++) {
67
+ const p = start + px * 4;
68
+ line[p] = colour[0];
69
+ line[p + 1] = colour[1];
70
+ line[p + 2] = colour[2];
71
+ line[p + 3] = colour[3];
72
+ }
73
+ }
74
+ for (let py = 0; py < scale; py++) {
75
+ data.set(line, ((my * scale + py) * pixelWidth) * 4);
76
+ }
77
+ }
78
+
79
+ return { data, width: pixelWidth, height: pixelHeight };
80
+ }
81
+
82
+ /**
83
+ * Draw into a canvas using its 2D context.
84
+ *
85
+ * This is the universal fallback: every browser that runs JavaScript at all
86
+ * has a 2D context, including every iOS Safari version.
87
+ *
88
+ * @param {import('../core/bit-matrix.js').BitMatrix} matrix
89
+ * @param {HTMLCanvasElement | OffscreenCanvas} canvas
90
+ * @param {import('./options.js').RenderOptions} [options]
91
+ * @returns {boolean} True when it drew.
92
+ */
93
+ export function toCanvas(matrix, canvas, options = {}) {
94
+ const opts = normalizeOptions(matrix, options);
95
+ const ctx = canvas.getContext('2d');
96
+ if (!ctx) return false;
97
+
98
+ canvas.width = opts.pixelWidth;
99
+ canvas.height = opts.pixelHeight;
100
+
101
+ // Draw with fillRect rather than putImageData: it respects the canvas's
102
+ // alpha compositing, so a transparent `light` leaves the page showing
103
+ // through instead of punching a hole.
104
+ const [, , , lightAlpha] = parseColor(opts.light);
105
+ if (lightAlpha > 0) {
106
+ ctx.fillStyle = opts.light;
107
+ ctx.fillRect(0, 0, opts.pixelWidth, opts.pixelHeight);
108
+ } else {
109
+ ctx.clearRect(0, 0, opts.pixelWidth, opts.pixelHeight);
110
+ }
111
+
112
+ ctx.fillStyle = opts.dark;
113
+ const { source, scale } = opts;
114
+ for (let y = 0; y < source.height; y++) {
115
+ let x = 0;
116
+ while (x < source.width) {
117
+ if (!source.get(x, y)) { x++; continue; }
118
+ let run = 1;
119
+ while (x + run < source.width && source.get(x + run, y)) run++;
120
+ ctx.fillRect(x * scale, y * scale, run * scale, scale);
121
+ x += run;
122
+ }
123
+ }
124
+ return true;
125
+ }
@@ -0,0 +1,130 @@
1
+ /*!
2
+ * Sythos Barcode Suite
3
+ *
4
+ * MIT License
5
+ *
6
+ * Copyright (c) 2026 Sythos
7
+ *
8
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
9
+ * of this software and associated documentation files (the "Software"), to deal
10
+ * in the Software without restriction, including without limitation the rights
11
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12
+ * copies of the Software, and to permit persons to whom the Software is
13
+ * furnished to do so, subject to the following conditions:
14
+ *
15
+ * The above copyright notice and this permission notice shall be included in all
16
+ * copies or substantial portions of the Software.
17
+ *
18
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
24
+ * SOFTWARE.
25
+ *
26
+ * SPDX-License-Identifier: MIT
27
+ *
28
+ * Original work. No code from any other barcode implementation.
29
+ */
30
+
31
+ /**
32
+ * Output backends.
33
+ *
34
+ * ## On GPU acceleration — read this before assuming what it does
35
+ *
36
+ * The WebGL2 and WebGPU backends accelerate **drawing** a barcode, not
37
+ * **computing** one. That distinction is worth stating plainly, because "GPU
38
+ * barcode generation" naturally sounds like the latter.
39
+ *
40
+ * Encoding is sequential integer work: Reed-Solomon polynomial division, mask
41
+ * penalty scoring, bit placement along a zig-zag path. Each step depends on the
42
+ * one before it, which is precisely the shape a GPU cannot exploit. A complete
43
+ * QR encode takes well under a millisecond on the CPU — less time than it takes
44
+ * to dispatch a compute shader and read the result back. Moving it to the GPU
45
+ * would make it slower, not faster, and no amount of engineering changes that.
46
+ *
47
+ * What the GPU genuinely helps with:
48
+ *
49
+ * - **Drawing** large symbols, or many symbols per frame, straight into a
50
+ * canvas without a CPU-side pixel buffer.
51
+ * - **Reading**, where per-frame greyscale conversion and block statistics
52
+ * over a 1080p or 4K camera image are the real bottleneck and are
53
+ * embarrassingly parallel.
54
+ *
55
+ * So: encoding stays on the CPU because that is the correct engineering answer,
56
+ * not because of a missing feature.
57
+ *
58
+ * @module render
59
+ */
60
+
61
+ export { toSVG, toSVGDataURI } from './svg.js';
62
+ export { toImageData, toCanvas } from './image-data.js';
63
+ export { toPNG, toPNGDataURI, deflateStored } from './png.js';
64
+ export { isWebGL2Available, renderToCanvasWebGL } from './webgl.js';
65
+ export { isWebGPUAvailable, renderToCanvasWebGPU } from './webgpu.js';
66
+ export { normalizeOptions, parseColor } from './options.js';
67
+
68
+ import { toCanvas } from './image-data.js';
69
+ import { isWebGL2Available, renderToCanvasWebGL } from './webgl.js';
70
+ import { isWebGPUAvailable, renderToCanvasWebGPU } from './webgpu.js';
71
+
72
+ /**
73
+ * Draw into a canvas using the best backend available.
74
+ *
75
+ * Tries WebGL2, then the 2D context. The 2D path is always available, so this
76
+ * never fails on a browser that can run the library at all — including every
77
+ * version of Safari on iOS.
78
+ *
79
+ * WebGPU is not reachable from here, and cannot be: obtaining an adapter is
80
+ * asynchronous, so a synchronous function can never wait for one. Use
81
+ * `renderToCanvasAutoAsync` to include it. This one stays synchronous because
82
+ * it is the documented signature and callers rely on the returned backend name
83
+ * being available immediately.
84
+ *
85
+ * @param {import('../core/bit-matrix.js').BitMatrix} matrix
86
+ * @param {HTMLCanvasElement | OffscreenCanvas} canvas
87
+ * @param {import('./options.js').RenderOptions & {backend?: 'auto'|'webgl2'|'2d'}} [options]
88
+ * @returns {{backend: 'webgl2' | '2d' | 'none'}}
89
+ */
90
+ export function renderToCanvasAuto(matrix, canvas, options = {}) {
91
+ const preferred = options.backend ?? 'auto';
92
+
93
+ if ((preferred === 'auto' || preferred === 'webgl2') && isWebGL2Available()) {
94
+ if (renderToCanvasWebGL(matrix, canvas, options)) return { backend: 'webgl2' };
95
+ }
96
+ if (toCanvas(matrix, canvas, options)) return { backend: '2d' };
97
+ return { backend: 'none' };
98
+ }
99
+
100
+ /**
101
+ * Draw into a canvas using the best backend available, including WebGPU.
102
+ *
103
+ * Tries WebGPU, then WebGL2, then the 2D context, and returns the name of the
104
+ * one that drew.
105
+ *
106
+ * Each backend is *probed* before the canvas is handed to it. That ordering is
107
+ * deliberate: a canvas can only ever have one kind of context, so committing it
108
+ * to WebGPU and failing afterwards would leave it unable to fall back to
109
+ * WebGL2 or 2D. The probes use throwaway objects of their own, so the caller's
110
+ * canvas is only touched by a backend that is already known to work.
111
+ *
112
+ * @param {import('../core/bit-matrix.js').BitMatrix} matrix
113
+ * @param {HTMLCanvasElement | OffscreenCanvas} canvas
114
+ * @param {import('./options.js').RenderOptions & {backend?: 'auto'|'webgpu'|'webgl2'|'2d'}} [options]
115
+ * @returns {Promise<{backend: 'webgpu' | 'webgl2' | '2d' | 'none'}>}
116
+ */
117
+ export async function renderToCanvasAutoAsync(matrix, canvas, options = {}) {
118
+ const preferred = options.backend ?? 'auto';
119
+
120
+ if (preferred === 'auto' || preferred === 'webgpu') {
121
+ if (await isWebGPUAvailable()) {
122
+ if (await renderToCanvasWebGPU(matrix, canvas, options)) return { backend: 'webgpu' };
123
+ }
124
+ }
125
+ if ((preferred === 'auto' || preferred === 'webgl2') && isWebGL2Available()) {
126
+ if (renderToCanvasWebGL(matrix, canvas, options)) return { backend: 'webgl2' };
127
+ }
128
+ if (toCanvas(matrix, canvas, options)) return { backend: '2d' };
129
+ return { backend: 'none' };
130
+ }
@@ -0,0 +1,160 @@
1
+ /*!
2
+ * Sythos Barcode Suite
3
+ *
4
+ * MIT License
5
+ *
6
+ * Copyright (c) 2026 Sythos
7
+ *
8
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
9
+ * of this software and associated documentation files (the "Software"), to deal
10
+ * in the Software without restriction, including without limitation the rights
11
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12
+ * copies of the Software, and to permit persons to whom the Software is
13
+ * furnished to do so, subject to the following conditions:
14
+ *
15
+ * The above copyright notice and this permission notice shall be included in all
16
+ * copies or substantial portions of the Software.
17
+ *
18
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
24
+ * SOFTWARE.
25
+ *
26
+ * SPDX-License-Identifier: MIT
27
+ *
28
+ * Original work. No code from any other barcode implementation.
29
+ */
30
+
31
+ /**
32
+ * Shared render options, normalised once so every backend agrees.
33
+ *
34
+ * @module render/options
35
+ */
36
+
37
+ import { BitMatrix } from '../core/bit-matrix.js';
38
+
39
+ /**
40
+ * @typedef {object} RenderOptions
41
+ * @property {number} [scale] Pixels per module. Default 8.
42
+ * @property {number} [margin] Quiet-zone modules on every side. Default 4.
43
+ * @property {string} [dark] Colour of set modules. Default '#000000'.
44
+ * @property {string} [light] Colour of clear modules, or 'none' for transparent.
45
+ * @property {number} [barHeight] For 1D symbols: total bar height in pixels.
46
+ */
47
+
48
+ /**
49
+ * Expand and pad the matrix, and resolve every dimension.
50
+ *
51
+ * Linear symbols arrive one module tall. They are stretched to `barHeight`
52
+ * *before* the quiet zone is applied, so the margin ends up uniform on all
53
+ * four sides — padding first would leave a quiet zone one module tall against
54
+ * bars a hundred pixels tall, which no scanner would accept.
55
+ *
56
+ * @param {BitMatrix} matrix
57
+ * @param {RenderOptions} options
58
+ */
59
+ export function normalizeOptions(matrix, options = {}) {
60
+ const scale = Math.max(1, Math.floor(options.scale ?? 8));
61
+ const margin = Math.max(0, Math.floor(options.margin ?? 4));
62
+ const dark = options.dark ?? '#000000';
63
+ const light = options.light ?? '#ffffff';
64
+ const barHeight = options.barHeight ?? null;
65
+
66
+ let base = matrix;
67
+ const is1D = matrix.height === 1;
68
+
69
+ if (is1D) {
70
+ // Default to a bar height that stays scannable: tall enough that a laser
71
+ // crossing at a slight angle still passes through the whole symbol.
72
+ const targetPixels = barHeight ?? Math.max(40, Math.round(matrix.width * scale * 0.15));
73
+ const rows = Math.max(1, Math.round(targetPixels / scale));
74
+ base = new BitMatrix(matrix.width, rows);
75
+ for (let x = 0; x < matrix.width; x++) {
76
+ if (!matrix.get(x, 0)) continue;
77
+ for (let y = 0; y < rows; y++) base.set(x, y);
78
+ }
79
+ }
80
+
81
+ const source = margin > 0 ? base.withMargin(margin) : base;
82
+
83
+ return {
84
+ scale,
85
+ margin,
86
+ dark,
87
+ light,
88
+ is1D,
89
+ source,
90
+ rowHeight: scale,
91
+ pixelWidth: source.width * scale,
92
+ pixelHeight: source.height * scale,
93
+ };
94
+ }
95
+
96
+ /**
97
+ * Parse a CSS colour into RGBA bytes.
98
+ *
99
+ * Supports the forms a barcode actually needs: #rgb, #rgba, #rrggbb,
100
+ * #rrggbbaa, rgb(), rgba(), plus 'none' and 'transparent'.
101
+ *
102
+ * @param {string} colour
103
+ * @returns {[number, number, number, number]}
104
+ */
105
+ export function parseColor(colour) {
106
+ const value = String(colour).trim().toLowerCase();
107
+
108
+ if (value === 'none' || value === 'transparent') return [0, 0, 0, 0];
109
+ if (value === 'white') return [255, 255, 255, 255];
110
+ if (value === 'black') return [0, 0, 0, 255];
111
+
112
+ if (value[0] === '#') {
113
+ const hex = value.slice(1);
114
+ const expand = (c) => parseInt(c + c, 16);
115
+ if (hex.length === 3) {
116
+ return [expand(hex[0]), expand(hex[1]), expand(hex[2]), 255];
117
+ }
118
+ if (hex.length === 4) {
119
+ return [expand(hex[0]), expand(hex[1]), expand(hex[2]), expand(hex[3])];
120
+ }
121
+ if (hex.length === 6) {
122
+ return [
123
+ parseInt(hex.slice(0, 2), 16),
124
+ parseInt(hex.slice(2, 4), 16),
125
+ parseInt(hex.slice(4, 6), 16),
126
+ 255,
127
+ ];
128
+ }
129
+ if (hex.length === 8) {
130
+ return [
131
+ parseInt(hex.slice(0, 2), 16),
132
+ parseInt(hex.slice(2, 4), 16),
133
+ parseInt(hex.slice(4, 6), 16),
134
+ parseInt(hex.slice(6, 8), 16),
135
+ ];
136
+ }
137
+ }
138
+
139
+ const fn = value.match(/^rgba?\(([^)]+)\)$/);
140
+ if (fn) {
141
+ const parts = fn[1].split(/[,/\s]+/).filter(Boolean);
142
+ const channel = (s) => (s.endsWith('%')
143
+ ? Math.round((parseFloat(s) / 100) * 255)
144
+ : Math.round(parseFloat(s)));
145
+ const r = channel(parts[0]);
146
+ const g = channel(parts[1]);
147
+ const b = channel(parts[2]);
148
+ let a = 255;
149
+ if (parts.length > 3) {
150
+ a = parts[3].endsWith('%')
151
+ ? Math.round((parseFloat(parts[3]) / 100) * 255)
152
+ : Math.round(parseFloat(parts[3]) * 255);
153
+ }
154
+ return [r, g, b, a];
155
+ }
156
+
157
+ // Unrecognised: fall back to opaque black rather than throwing, so an
158
+ // unusual colour never costs someone a barcode.
159
+ return [0, 0, 0, 255];
160
+ }