@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.
- package/LICENSE +215 -0
- package/NOTICE.md +106 -0
- package/README.md +433 -0
- package/bundle/sythos-barcode.esm.js +7998 -0
- package/bundle/sythos-barcode.js +7948 -0
- package/examples/create.html +731 -0
- package/examples/read.html +341 -0
- package/licenses/README.md +42 -0
- package/licenses/codabar.license +74 -0
- package/licenses/code-11.license +69 -0
- package/licenses/code-128.license +69 -0
- package/licenses/code-39.license +70 -0
- package/licenses/code-93.license +71 -0
- package/licenses/ean-13.license +70 -0
- package/licenses/ean-8.license +70 -0
- package/licenses/gs1-128.license +71 -0
- package/licenses/isbn.license +76 -0
- package/licenses/itf-14.license +69 -0
- package/licenses/itf.license +70 -0
- package/licenses/msi-plessey.license +72 -0
- package/licenses/pharmacode.license +71 -0
- package/licenses/qr-code.license +75 -0
- package/licenses/upc-a.license +72 -0
- package/licenses/upc-e.license +69 -0
- package/package.json +89 -0
- package/src/core/bit-buffer.js +174 -0
- package/src/core/bit-matrix.js +241 -0
- package/src/core/errors.js +61 -0
- package/src/core/galois-field.js +204 -0
- package/src/core/index.js +56 -0
- package/src/core/reed-solomon.js +313 -0
- package/src/image/binarizer.js +270 -0
- package/src/image/grid-sampler.js +164 -0
- package/src/image/index.js +40 -0
- package/src/image/luminance.js +196 -0
- package/src/image/perspective.js +195 -0
- package/src/index.js +240 -0
- package/src/oned/index.js +89 -0
- package/src/oned/patterns.js +384 -0
- package/src/oned/reader.js +918 -0
- package/src/oned/writers.js +741 -0
- package/src/qr/decoder.js +575 -0
- package/src/qr/detector.js +630 -0
- package/src/qr/encoder.js +958 -0
- package/src/qr/index.js +44 -0
- package/src/qr/tables.js +737 -0
- package/src/render/image-data.js +125 -0
- package/src/render/index.js +130 -0
- package/src/render/options.js +160 -0
- package/src/render/png.js +295 -0
- package/src/render/svg.js +120 -0
- package/src/render/webgl.js +206 -0
- package/src/render/webgpu.js +369 -0
|
@@ -0,0 +1,295 @@
|
|
|
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
|
+
* PNG output, with no dependencies and no compression library of our own.
|
|
33
|
+
*
|
|
34
|
+
* A barcode is a two-colour image, so this writes a 1-bit palette PNG: eight
|
|
35
|
+
* modules per byte, and a two-entry palette. That is both the smallest and the
|
|
36
|
+
* simplest correct encoding.
|
|
37
|
+
*
|
|
38
|
+
* Compression strategy, in order of preference:
|
|
39
|
+
*
|
|
40
|
+
* 1. `node:zlib` in Node.
|
|
41
|
+
* 2. `CompressionStream('deflate')` in browsers that have it.
|
|
42
|
+
* 3. Stored (uncompressed) deflate blocks, written here.
|
|
43
|
+
*
|
|
44
|
+
* Writing a Huffman coder would be a week of work to save a few kilobytes on
|
|
45
|
+
* an image that is mostly already tiny. The stored-block fallback is about
|
|
46
|
+
* eighty lines and produces a completely valid PNG; the only cost is size, on
|
|
47
|
+
* the minority of platforms that reach it.
|
|
48
|
+
*
|
|
49
|
+
* @module render/png
|
|
50
|
+
*/
|
|
51
|
+
|
|
52
|
+
import { normalizeOptions, parseColor } from './options.js';
|
|
53
|
+
|
|
54
|
+
/* ------------------------------------------------------------------ *
|
|
55
|
+
* Checksums
|
|
56
|
+
* ------------------------------------------------------------------ */
|
|
57
|
+
|
|
58
|
+
const CRC_TABLE = (() => {
|
|
59
|
+
const table = new Uint32Array(256);
|
|
60
|
+
for (let n = 0; n < 256; n++) {
|
|
61
|
+
let c = n;
|
|
62
|
+
for (let k = 0; k < 8; k++) {
|
|
63
|
+
c = c & 1 ? 0xedb88320 ^ (c >>> 1) : c >>> 1;
|
|
64
|
+
}
|
|
65
|
+
table[n] = c >>> 0;
|
|
66
|
+
}
|
|
67
|
+
return table;
|
|
68
|
+
})();
|
|
69
|
+
|
|
70
|
+
/**
|
|
71
|
+
* @param {Uint8Array} bytes
|
|
72
|
+
* @returns {number}
|
|
73
|
+
*/
|
|
74
|
+
function crc32(bytes) {
|
|
75
|
+
let c = 0xffffffff;
|
|
76
|
+
for (let i = 0; i < bytes.length; i++) {
|
|
77
|
+
c = CRC_TABLE[(c ^ bytes[i]) & 0xff] ^ (c >>> 8);
|
|
78
|
+
}
|
|
79
|
+
return (c ^ 0xffffffff) >>> 0;
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
/**
|
|
83
|
+
* @param {Uint8Array} bytes
|
|
84
|
+
* @returns {number}
|
|
85
|
+
*/
|
|
86
|
+
function adler32(bytes) {
|
|
87
|
+
let a = 1;
|
|
88
|
+
let b = 0;
|
|
89
|
+
// 5552 is the largest run that cannot overflow the 32-bit accumulator.
|
|
90
|
+
for (let i = 0; i < bytes.length;) {
|
|
91
|
+
const end = Math.min(i + 5552, bytes.length);
|
|
92
|
+
for (; i < end; i++) {
|
|
93
|
+
a += bytes[i];
|
|
94
|
+
b += a;
|
|
95
|
+
}
|
|
96
|
+
a %= 65521;
|
|
97
|
+
b %= 65521;
|
|
98
|
+
}
|
|
99
|
+
return ((b << 16) | a) >>> 0;
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
/* ------------------------------------------------------------------ *
|
|
103
|
+
* Deflate
|
|
104
|
+
* ------------------------------------------------------------------ */
|
|
105
|
+
|
|
106
|
+
/**
|
|
107
|
+
* Wrap data in stored (type 00) deflate blocks with a zlib header.
|
|
108
|
+
*
|
|
109
|
+
* A stored block's length field is sixteen bits, so each block caps at 65535
|
|
110
|
+
* bytes and longer data must be split. BFINAL is set on the last block only —
|
|
111
|
+
* getting that wrong yields a stream that decodes correctly for any small
|
|
112
|
+
* image and truncates on the first large one.
|
|
113
|
+
*
|
|
114
|
+
* @param {Uint8Array} data
|
|
115
|
+
* @returns {Uint8Array}
|
|
116
|
+
*/
|
|
117
|
+
export function deflateStored(data) {
|
|
118
|
+
const MAX = 0xffff;
|
|
119
|
+
const blockCount = Math.max(1, Math.ceil(data.length / MAX));
|
|
120
|
+
const out = new Uint8Array(2 + blockCount * 5 + data.length + 4);
|
|
121
|
+
let p = 0;
|
|
122
|
+
|
|
123
|
+
// zlib header: deflate, 32K window, no preset dictionary. 0x78 0x01 is a
|
|
124
|
+
// valid FCHECK pair (0x7801 % 31 === 0).
|
|
125
|
+
out[p++] = 0x78;
|
|
126
|
+
out[p++] = 0x01;
|
|
127
|
+
|
|
128
|
+
for (let i = 0; i < blockCount; i++) {
|
|
129
|
+
const start = i * MAX;
|
|
130
|
+
const len = Math.min(MAX, data.length - start);
|
|
131
|
+
const isLast = i === blockCount - 1;
|
|
132
|
+
|
|
133
|
+
out[p++] = isLast ? 1 : 0; // BFINAL, BTYPE = 00
|
|
134
|
+
out[p++] = len & 0xff; // LEN, little endian
|
|
135
|
+
out[p++] = (len >>> 8) & 0xff;
|
|
136
|
+
out[p++] = ~len & 0xff; // NLEN, one's complement
|
|
137
|
+
out[p++] = (~len >>> 8) & 0xff;
|
|
138
|
+
|
|
139
|
+
out.set(data.subarray(start, start + len), p);
|
|
140
|
+
p += len;
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
const sum = adler32(data);
|
|
144
|
+
out[p++] = (sum >>> 24) & 0xff; // adler32, big endian
|
|
145
|
+
out[p++] = (sum >>> 16) & 0xff;
|
|
146
|
+
out[p++] = (sum >>> 8) & 0xff;
|
|
147
|
+
out[p++] = sum & 0xff;
|
|
148
|
+
|
|
149
|
+
return out.subarray(0, p);
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
/**
|
|
153
|
+
* Compress with whatever the platform provides, falling back to stored blocks.
|
|
154
|
+
*
|
|
155
|
+
* @param {Uint8Array} data
|
|
156
|
+
* @returns {Promise<Uint8Array>}
|
|
157
|
+
*/
|
|
158
|
+
async function deflate(data) {
|
|
159
|
+
// Node: zlib is built in, so this is not a dependency.
|
|
160
|
+
if (typeof process !== 'undefined' && process.versions && process.versions.node) {
|
|
161
|
+
try {
|
|
162
|
+
const zlib = await import('node:zlib');
|
|
163
|
+
return new Uint8Array(zlib.deflateSync(data));
|
|
164
|
+
} catch {
|
|
165
|
+
/* fall through */
|
|
166
|
+
}
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
// Browsers: CompressionStream('deflate') emits the zlib format PNG wants.
|
|
170
|
+
if (typeof CompressionStream === 'function') {
|
|
171
|
+
try {
|
|
172
|
+
const stream = new Blob([data]).stream().pipeThrough(new CompressionStream('deflate'));
|
|
173
|
+
const buffer = await new Response(stream).arrayBuffer();
|
|
174
|
+
return new Uint8Array(buffer);
|
|
175
|
+
} catch {
|
|
176
|
+
/* fall through */
|
|
177
|
+
}
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
return deflateStored(data);
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
/* ------------------------------------------------------------------ *
|
|
184
|
+
* PNG assembly
|
|
185
|
+
* ------------------------------------------------------------------ */
|
|
186
|
+
|
|
187
|
+
const SIGNATURE = new Uint8Array([0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a]);
|
|
188
|
+
|
|
189
|
+
/**
|
|
190
|
+
* @param {string} type Four ASCII characters.
|
|
191
|
+
* @param {Uint8Array} payload
|
|
192
|
+
* @returns {Uint8Array}
|
|
193
|
+
*/
|
|
194
|
+
function chunk(type, payload) {
|
|
195
|
+
const out = new Uint8Array(12 + payload.length);
|
|
196
|
+
const view = new DataView(out.buffer);
|
|
197
|
+
view.setUint32(0, payload.length);
|
|
198
|
+
for (let i = 0; i < 4; i++) out[4 + i] = type.charCodeAt(i);
|
|
199
|
+
out.set(payload, 8);
|
|
200
|
+
// The CRC covers the type and the payload, but not the length.
|
|
201
|
+
view.setUint32(8 + payload.length, crc32(out.subarray(4, 8 + payload.length)));
|
|
202
|
+
return out;
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
/**
|
|
206
|
+
* Render to a PNG file.
|
|
207
|
+
*
|
|
208
|
+
* @param {import('../core/bit-matrix.js').BitMatrix} matrix
|
|
209
|
+
* @param {import('./options.js').RenderOptions} [options]
|
|
210
|
+
* @returns {Promise<Uint8Array>}
|
|
211
|
+
*/
|
|
212
|
+
export async function toPNG(matrix, options = {}) {
|
|
213
|
+
const opts = normalizeOptions(matrix, options);
|
|
214
|
+
const { scale, source, pixelWidth, pixelHeight } = opts;
|
|
215
|
+
|
|
216
|
+
const dark = parseColor(opts.dark);
|
|
217
|
+
const light = parseColor(opts.light);
|
|
218
|
+
|
|
219
|
+
// --- Scanlines: filter byte 0, then one bit per pixel, MSB leftmost.
|
|
220
|
+
const bytesPerRow = (pixelWidth + 7) >> 3;
|
|
221
|
+
const raw = new Uint8Array((bytesPerRow + 1) * pixelHeight);
|
|
222
|
+
|
|
223
|
+
const rowBits = new Uint8Array(bytesPerRow);
|
|
224
|
+
for (let my = 0; my < source.height; my++) {
|
|
225
|
+
rowBits.fill(0);
|
|
226
|
+
for (let mx = 0; mx < source.width; mx++) {
|
|
227
|
+
if (!source.get(mx, my)) continue;
|
|
228
|
+
const from = mx * scale;
|
|
229
|
+
for (let px = from; px < from + scale; px++) {
|
|
230
|
+
rowBits[px >> 3] |= 0x80 >> (px & 7);
|
|
231
|
+
}
|
|
232
|
+
}
|
|
233
|
+
for (let py = 0; py < scale; py++) {
|
|
234
|
+
const offset = (my * scale + py) * (bytesPerRow + 1);
|
|
235
|
+
raw[offset] = 0; // filter type 0 (None)
|
|
236
|
+
raw.set(rowBits, offset + 1);
|
|
237
|
+
}
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
// --- Chunks.
|
|
241
|
+
const ihdr = new Uint8Array(13);
|
|
242
|
+
const ihdrView = new DataView(ihdr.buffer);
|
|
243
|
+
ihdrView.setUint32(0, pixelWidth);
|
|
244
|
+
ihdrView.setUint32(4, pixelHeight);
|
|
245
|
+
ihdr[8] = 1; // bit depth
|
|
246
|
+
ihdr[9] = 3; // colour type 3: palette
|
|
247
|
+
ihdr[10] = 0; // compression: deflate
|
|
248
|
+
ihdr[11] = 0; // filter method
|
|
249
|
+
ihdr[12] = 0; // no interlace
|
|
250
|
+
|
|
251
|
+
// Palette index 0 is light (a clear module), index 1 is dark.
|
|
252
|
+
const plte = new Uint8Array([
|
|
253
|
+
light[0], light[1], light[2],
|
|
254
|
+
dark[0], dark[1], dark[2],
|
|
255
|
+
]);
|
|
256
|
+
|
|
257
|
+
const parts = [SIGNATURE, chunk('IHDR', ihdr), chunk('PLTE', plte)];
|
|
258
|
+
|
|
259
|
+
// tRNS is only emitted when something is actually translucent, so the common
|
|
260
|
+
// opaque case produces a file every decoder handles identically.
|
|
261
|
+
if (light[3] < 255 || dark[3] < 255) {
|
|
262
|
+
parts.push(chunk('tRNS', new Uint8Array([light[3], dark[3]])));
|
|
263
|
+
}
|
|
264
|
+
|
|
265
|
+
parts.push(chunk('IDAT', await deflate(raw)));
|
|
266
|
+
parts.push(chunk('IEND', new Uint8Array(0)));
|
|
267
|
+
|
|
268
|
+
let total = 0;
|
|
269
|
+
for (const part of parts) total += part.length;
|
|
270
|
+
const file = new Uint8Array(total);
|
|
271
|
+
let offset = 0;
|
|
272
|
+
for (const part of parts) {
|
|
273
|
+
file.set(part, offset);
|
|
274
|
+
offset += part.length;
|
|
275
|
+
}
|
|
276
|
+
return file;
|
|
277
|
+
}
|
|
278
|
+
|
|
279
|
+
/**
|
|
280
|
+
* Render to a data URI usable as an `<img>` src or a download href.
|
|
281
|
+
*
|
|
282
|
+
* @param {import('../core/bit-matrix.js').BitMatrix} matrix
|
|
283
|
+
* @param {import('./options.js').RenderOptions} [options]
|
|
284
|
+
* @returns {Promise<string>}
|
|
285
|
+
*/
|
|
286
|
+
export async function toPNGDataURI(matrix, options = {}) {
|
|
287
|
+
const bytes = await toPNG(matrix, options);
|
|
288
|
+
let binary = '';
|
|
289
|
+
for (let i = 0; i < bytes.length; i++) binary += String.fromCharCode(bytes[i]);
|
|
290
|
+
const base64 = typeof btoa === 'function'
|
|
291
|
+
? btoa(binary)
|
|
292
|
+
/* eslint-disable-next-line no-undef */
|
|
293
|
+
: Buffer.from(bytes).toString('base64');
|
|
294
|
+
return 'data:image/png;base64,' + base64;
|
|
295
|
+
}
|
|
@@ -0,0 +1,120 @@
|
|
|
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
|
+
* SVG output.
|
|
33
|
+
*
|
|
34
|
+
* Dark modules are emitted as a single `<path>` with horizontal runs merged,
|
|
35
|
+
* not as one `<rect>` per module. A version 40 QR symbol has 31329 modules; the
|
|
36
|
+
* naive rendering is a megabyte of XML that browsers choke on, while the merged
|
|
37
|
+
* path is a few kilobytes and draws identically.
|
|
38
|
+
*
|
|
39
|
+
* @module render/svg
|
|
40
|
+
*/
|
|
41
|
+
|
|
42
|
+
import { normalizeOptions } from './options.js';
|
|
43
|
+
|
|
44
|
+
/**
|
|
45
|
+
* @param {string} value
|
|
46
|
+
* @returns {string}
|
|
47
|
+
*/
|
|
48
|
+
function escapeAttr(value) {
|
|
49
|
+
return String(value)
|
|
50
|
+
.replace(/&/g, '&')
|
|
51
|
+
.replace(/</g, '<')
|
|
52
|
+
.replace(/>/g, '>')
|
|
53
|
+
.replace(/"/g, '"');
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
/**
|
|
57
|
+
* Render to an SVG document.
|
|
58
|
+
*
|
|
59
|
+
* @param {import('../core/bit-matrix.js').BitMatrix} matrix
|
|
60
|
+
* @param {import('./options.js').RenderOptions} [options]
|
|
61
|
+
* @returns {string}
|
|
62
|
+
*/
|
|
63
|
+
export function toSVG(matrix, options = {}) {
|
|
64
|
+
const opts = normalizeOptions(matrix, options);
|
|
65
|
+
const { scale, source, pixelWidth, pixelHeight, rowHeight } = opts;
|
|
66
|
+
|
|
67
|
+
let path = '';
|
|
68
|
+
for (let y = 0; y < source.height; y++) {
|
|
69
|
+
let x = 0;
|
|
70
|
+
while (x < source.width) {
|
|
71
|
+
if (!source.get(x, y)) { x++; continue; }
|
|
72
|
+
let run = 1;
|
|
73
|
+
while (x + run < source.width && source.get(x + run, y)) run++;
|
|
74
|
+
// Relative horizontal-vertical path commands: shorter than rects and
|
|
75
|
+
// free of the seams that appear between adjacent rects at some zooms.
|
|
76
|
+
path += `M${x * scale} ${y * rowHeight}h${run * scale}v${rowHeight}h${-run * scale}z`;
|
|
77
|
+
x += run;
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
const bg = opts.light === 'none'
|
|
82
|
+
? ''
|
|
83
|
+
: `<rect width="${pixelWidth}" height="${pixelHeight}" fill="${escapeAttr(opts.light)}"/>`;
|
|
84
|
+
|
|
85
|
+
return `<svg xmlns="http://www.w3.org/2000/svg" width="${pixelWidth}" height="${pixelHeight}" ` +
|
|
86
|
+
`viewBox="0 0 ${pixelWidth} ${pixelHeight}" shape-rendering="crispEdges">` +
|
|
87
|
+
bg +
|
|
88
|
+
`<path d="${path}" fill="${escapeAttr(opts.dark)}"/>` +
|
|
89
|
+
'</svg>';
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
/**
|
|
93
|
+
* Base64 that works identically in Node and the browser.
|
|
94
|
+
*
|
|
95
|
+
* `btoa` is byte-oriented, so the UTF-8 encoding has to happen first — passing
|
|
96
|
+
* it a string with any character above U+00FF throws.
|
|
97
|
+
*
|
|
98
|
+
* @param {string} text
|
|
99
|
+
* @returns {string}
|
|
100
|
+
*/
|
|
101
|
+
function toBase64(text) {
|
|
102
|
+
const bytes = new TextEncoder().encode(text);
|
|
103
|
+
let binary = '';
|
|
104
|
+
for (let i = 0; i < bytes.length; i++) binary += String.fromCharCode(bytes[i]);
|
|
105
|
+
if (typeof btoa === 'function') return btoa(binary);
|
|
106
|
+
// Node before btoa was global, and non-browser embedders.
|
|
107
|
+
/* eslint-disable-next-line no-undef */
|
|
108
|
+
return Buffer.from(bytes).toString('base64');
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
/**
|
|
112
|
+
* Render to a data URI usable directly as an `<img>` src.
|
|
113
|
+
*
|
|
114
|
+
* @param {import('../core/bit-matrix.js').BitMatrix} matrix
|
|
115
|
+
* @param {import('./options.js').RenderOptions} [options]
|
|
116
|
+
* @returns {string}
|
|
117
|
+
*/
|
|
118
|
+
export function toSVGDataURI(matrix, options = {}) {
|
|
119
|
+
return 'data:image/svg+xml;base64,' + toBase64(toSVG(matrix, options));
|
|
120
|
+
}
|
|
@@ -0,0 +1,206 @@
|
|
|
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
|
+
* WebGL2 drawing.
|
|
33
|
+
*
|
|
34
|
+
* The matrix is uploaded as a one-byte-per-module R8 texture and drawn with a
|
|
35
|
+
* fragment shader sampling it at NEAREST. That keeps module edges perfectly
|
|
36
|
+
* sharp at any zoom, which is the property that matters — a barcode resampled
|
|
37
|
+
* with interpolation stops being a barcode.
|
|
38
|
+
*
|
|
39
|
+
* Every entry point is failure-tolerant: no context, a lost context, a driver
|
|
40
|
+
* that rejects the shader — all return false so the caller falls back to the
|
|
41
|
+
* 2D path rather than showing the user nothing.
|
|
42
|
+
*
|
|
43
|
+
* @module render/webgl
|
|
44
|
+
*/
|
|
45
|
+
|
|
46
|
+
import { normalizeOptions, parseColor } from './options.js';
|
|
47
|
+
|
|
48
|
+
const VERTEX_SHADER = `#version 300 es
|
|
49
|
+
// A single oversized triangle covers the viewport with no vertex buffer:
|
|
50
|
+
// three gl_VertexID lookups, no attribute state to set up or leak.
|
|
51
|
+
out vec2 vUV;
|
|
52
|
+
void main() {
|
|
53
|
+
vec2 pos = vec2((gl_VertexID << 1) & 2, gl_VertexID & 2);
|
|
54
|
+
vUV = pos;
|
|
55
|
+
gl_Position = vec4(pos * 2.0 - 1.0, 0.0, 1.0);
|
|
56
|
+
}`;
|
|
57
|
+
|
|
58
|
+
const FRAGMENT_SHADER = `#version 300 es
|
|
59
|
+
precision highp float;
|
|
60
|
+
in vec2 vUV;
|
|
61
|
+
out vec4 fragColor;
|
|
62
|
+
uniform sampler2D uMatrix;
|
|
63
|
+
uniform vec4 uDark;
|
|
64
|
+
uniform vec4 uLight;
|
|
65
|
+
uniform vec2 uSize;
|
|
66
|
+
void main() {
|
|
67
|
+
// Flip Y: texture row 0 is the top of the symbol, but GL's origin is bottom.
|
|
68
|
+
vec2 uv = vec2(vUV.x, 1.0 - vUV.y);
|
|
69
|
+
// Sample at the centre of the module, never on a boundary, so rounding
|
|
70
|
+
// cannot pull a neighbouring module's value in at fractional scales.
|
|
71
|
+
vec2 texel = (floor(uv * uSize) + 0.5) / uSize;
|
|
72
|
+
float v = texture(uMatrix, texel).r;
|
|
73
|
+
fragColor = v > 0.5 ? uDark : uLight;
|
|
74
|
+
}`;
|
|
75
|
+
|
|
76
|
+
/**
|
|
77
|
+
* Is WebGL2 usable here?
|
|
78
|
+
*
|
|
79
|
+
* @returns {boolean}
|
|
80
|
+
*/
|
|
81
|
+
export function isWebGL2Available() {
|
|
82
|
+
try {
|
|
83
|
+
if (typeof document === 'undefined') return false;
|
|
84
|
+
if (typeof WebGL2RenderingContext === 'undefined') return false;
|
|
85
|
+
const probe = document.createElement('canvas');
|
|
86
|
+
const gl = probe.getContext('webgl2');
|
|
87
|
+
if (!gl) return false;
|
|
88
|
+
const lose = gl.getExtension('WEBGL_lose_context');
|
|
89
|
+
if (lose) lose.loseContext();
|
|
90
|
+
return true;
|
|
91
|
+
} catch {
|
|
92
|
+
return false;
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
/**
|
|
97
|
+
* @param {WebGL2RenderingContext} gl
|
|
98
|
+
* @param {number} type
|
|
99
|
+
* @param {string} source
|
|
100
|
+
* @returns {WebGLShader | null}
|
|
101
|
+
*/
|
|
102
|
+
function compile(gl, type, source) {
|
|
103
|
+
const shader = gl.createShader(type);
|
|
104
|
+
if (!shader) return null;
|
|
105
|
+
gl.shaderSource(shader, source);
|
|
106
|
+
gl.compileShader(shader);
|
|
107
|
+
if (!gl.getShaderParameter(shader, gl.COMPILE_STATUS)) {
|
|
108
|
+
gl.deleteShader(shader);
|
|
109
|
+
return null;
|
|
110
|
+
}
|
|
111
|
+
return shader;
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
/**
|
|
115
|
+
* Draw a matrix into a canvas with WebGL2.
|
|
116
|
+
*
|
|
117
|
+
* @param {import('../core/bit-matrix.js').BitMatrix} matrix
|
|
118
|
+
* @param {HTMLCanvasElement | OffscreenCanvas} canvas
|
|
119
|
+
* @param {import('./options.js').RenderOptions} [options]
|
|
120
|
+
* @returns {boolean} True if it drew; false means the caller should fall back.
|
|
121
|
+
*/
|
|
122
|
+
export function renderToCanvasWebGL(matrix, canvas, options = {}) {
|
|
123
|
+
let gl = null;
|
|
124
|
+
let program = null;
|
|
125
|
+
let vs = null;
|
|
126
|
+
let fs = null;
|
|
127
|
+
let texture = null;
|
|
128
|
+
let vao = null;
|
|
129
|
+
|
|
130
|
+
try {
|
|
131
|
+
const opts = normalizeOptions(matrix, options);
|
|
132
|
+
const { source, pixelWidth, pixelHeight } = opts;
|
|
133
|
+
|
|
134
|
+
gl = canvas.getContext('webgl2', { antialias: false, premultipliedAlpha: false });
|
|
135
|
+
if (!gl) return false;
|
|
136
|
+
|
|
137
|
+
canvas.width = pixelWidth;
|
|
138
|
+
canvas.height = pixelHeight;
|
|
139
|
+
|
|
140
|
+
vs = compile(gl, gl.VERTEX_SHADER, VERTEX_SHADER);
|
|
141
|
+
fs = compile(gl, gl.FRAGMENT_SHADER, FRAGMENT_SHADER);
|
|
142
|
+
if (!vs || !fs) return false;
|
|
143
|
+
|
|
144
|
+
program = gl.createProgram();
|
|
145
|
+
gl.attachShader(program, vs);
|
|
146
|
+
gl.attachShader(program, fs);
|
|
147
|
+
gl.linkProgram(program);
|
|
148
|
+
if (!gl.getProgramParameter(program, gl.LINK_STATUS)) return false;
|
|
149
|
+
gl.useProgram(program);
|
|
150
|
+
|
|
151
|
+
// One byte per module. R8 is the narrowest single-channel format WebGL2
|
|
152
|
+
// guarantees as colour-renderable and filterable.
|
|
153
|
+
const pixels = new Uint8Array(source.width * source.height);
|
|
154
|
+
for (let y = 0; y < source.height; y++) {
|
|
155
|
+
for (let x = 0; x < source.width; x++) {
|
|
156
|
+
pixels[y * source.width + x] = source.get(x, y) ? 255 : 0;
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
texture = gl.createTexture();
|
|
161
|
+
gl.bindTexture(gl.TEXTURE_2D, texture);
|
|
162
|
+
gl.pixelStorei(gl.UNPACK_ALIGNMENT, 1);
|
|
163
|
+
gl.texImage2D(
|
|
164
|
+
gl.TEXTURE_2D, 0, gl.R8,
|
|
165
|
+
source.width, source.height, 0,
|
|
166
|
+
gl.RED, gl.UNSIGNED_BYTE, pixels
|
|
167
|
+
);
|
|
168
|
+
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.NEAREST);
|
|
169
|
+
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.NEAREST);
|
|
170
|
+
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.CLAMP_TO_EDGE);
|
|
171
|
+
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.CLAMP_TO_EDGE);
|
|
172
|
+
|
|
173
|
+
const dark = parseColor(opts.dark).map((c) => c / 255);
|
|
174
|
+
const light = parseColor(opts.light).map((c) => c / 255);
|
|
175
|
+
|
|
176
|
+
gl.uniform1i(gl.getUniformLocation(program, 'uMatrix'), 0);
|
|
177
|
+
gl.uniform4f(gl.getUniformLocation(program, 'uDark'), dark[0], dark[1], dark[2], dark[3]);
|
|
178
|
+
gl.uniform4f(gl.getUniformLocation(program, 'uLight'), light[0], light[1], light[2], light[3]);
|
|
179
|
+
gl.uniform2f(gl.getUniformLocation(program, 'uSize'), source.width, source.height);
|
|
180
|
+
|
|
181
|
+
// WebGL2 still requires a bound VAO even when drawing without attributes.
|
|
182
|
+
vao = gl.createVertexArray();
|
|
183
|
+
gl.bindVertexArray(vao);
|
|
184
|
+
|
|
185
|
+
gl.viewport(0, 0, pixelWidth, pixelHeight);
|
|
186
|
+
gl.drawArrays(gl.TRIANGLES, 0, 3);
|
|
187
|
+
|
|
188
|
+
return true;
|
|
189
|
+
} catch {
|
|
190
|
+
return false;
|
|
191
|
+
} finally {
|
|
192
|
+
// Release eagerly: a page generating many barcodes would otherwise hold
|
|
193
|
+
// every texture until GC caught up, and GL memory is not GC's priority.
|
|
194
|
+
if (gl) {
|
|
195
|
+
try {
|
|
196
|
+
if (vao) gl.deleteVertexArray(vao);
|
|
197
|
+
if (texture) gl.deleteTexture(texture);
|
|
198
|
+
if (program) gl.deleteProgram(program);
|
|
199
|
+
if (vs) gl.deleteShader(vs);
|
|
200
|
+
if (fs) gl.deleteShader(fs);
|
|
201
|
+
} catch {
|
|
202
|
+
/* context already gone */
|
|
203
|
+
}
|
|
204
|
+
}
|
|
205
|
+
}
|
|
206
|
+
}
|