@sythos/js_barcode_universal 0.1.0 → 1.0.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/README.md +446 -414
- package/bundle/sythos-barcode.esm.js +2887 -2139
- package/bundle/sythos-barcode.js +2883 -2139
- package/examples/create.html +730 -729
- package/examples/read.html +341 -341
- package/licenses/README.md +1 -0
- package/licenses/data-matrix.license +82 -0
- package/package.json +90 -87
- package/src/core/reed-solomon.js +1 -1
- package/src/datamatrix/decoder.js +262 -0
- package/src/datamatrix/detector.js +225 -0
- package/src/datamatrix/encoder.js +191 -0
- package/src/datamatrix/index.js +42 -0
- package/src/datamatrix/tables.js +123 -0
- package/src/index.js +34 -2
package/licenses/README.md
CHANGED
|
@@ -11,6 +11,7 @@ appendix carries the consolidated inventory these files expand on.
|
|
|
11
11
|
| Format | File | `[TO VERIFY]` items |
|
|
12
12
|
|---|---|---:|
|
|
13
13
|
| QR Code | [`qr-code.license`](qr-code.license) | 5 |
|
|
14
|
+
| Data Matrix ECC 200 | [`data-matrix.license`](data-matrix.license) | 9 |
|
|
14
15
|
| EAN-13 | [`ean-13.license`](ean-13.license) | 5 |
|
|
15
16
|
| EAN-8 | [`ean-8.license`](ean-8.license) | 5 |
|
|
16
17
|
| UPC-A | [`upc-a.license`](upc-a.license) | 6 |
|
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
Data Matrix ECC 200 — format provenance and usability
|
|
2
|
+
==========================================================================
|
|
3
|
+
|
|
4
|
+
This file records where the Data Matrix ECC 200 symbology comes from, what governs
|
|
5
|
+
it, and the basis on which this project implements and redistributes it.
|
|
6
|
+
|
|
7
|
+
It is an ENGINEERING INVENTORY, NOT LEGAL ADVICE. Items marked [TO VERIFY]
|
|
8
|
+
have not been independently confirmed by the author.
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
ORIGIN
|
|
12
|
+
--------------------------------------------------------------------------
|
|
13
|
+
|
|
14
|
+
Data Matrix emerged from industrial automatic-identification work
|
|
15
|
+
associated with International Data Matrix and later RVSI/CI Matrix in the
|
|
16
|
+
late 1980s and early 1990s. The exact corporate chronology and attribution
|
|
17
|
+
have not been independently confirmed by the author. [TO VERIFY]
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
SPECIFICATION
|
|
21
|
+
--------------------------------------------------------------------------
|
|
22
|
+
|
|
23
|
+
ISO/IEC 16022:2024 — Data Matrix bar code symbology specification.
|
|
24
|
+
Published by ISO/IEC. The specification TEXT is copyrighted by its
|
|
25
|
+
publisher and is paywalled; it is not reproduced here.
|
|
26
|
+
GS1 General Specifications and the GS1 DataMatrix Guideline govern use
|
|
27
|
+
of ECC 200 with a leading FNC1 and GS1 Application Identifiers.
|
|
28
|
+
|
|
29
|
+
Specification TEXT is copyrighted by its publisher. The SYMBOLOGY it
|
|
30
|
+
describes is not — an encoding scheme is a system, not a work of
|
|
31
|
+
authorship. This project implements the symbology; it does not reproduce,
|
|
32
|
+
redistribute or excerpt any specification document.
|
|
33
|
+
|
|
34
|
+
|
|
35
|
+
PATENT STATUS
|
|
36
|
+
--------------------------------------------------------------------------
|
|
37
|
+
|
|
38
|
+
Patent rights associated with ECC 200 have been reported as released for
|
|
39
|
+
public use by an earlier rights holder, but the original patents, chain of
|
|
40
|
+
title and release terms have not been independently confirmed by the
|
|
41
|
+
author. [TO VERIFY]
|
|
42
|
+
|
|
43
|
+
|
|
44
|
+
TRADEMARK
|
|
45
|
+
--------------------------------------------------------------------------
|
|
46
|
+
|
|
47
|
+
The current registration status and ownership of the words "Data Matrix"
|
|
48
|
+
have not been independently established. [TO VERIFY] "GS1" and "GS1
|
|
49
|
+
DataMatrix" are GS1 designations; their use is descriptive here and does
|
|
50
|
+
not imply endorsement. [TO VERIFY]
|
|
51
|
+
|
|
52
|
+
A trademark does not restrict implementing a symbology. It restricts
|
|
53
|
+
branding — how a product names and presents itself.
|
|
54
|
+
|
|
55
|
+
|
|
56
|
+
IMPLEMENTATION BASIS IN THIS PROJECT
|
|
57
|
+
--------------------------------------------------------------------------
|
|
58
|
+
|
|
59
|
+
Data Matrix ECC 200 is implemented here from published descriptions of the format,
|
|
60
|
+
which are systems and facts rather than works of authorship. No source
|
|
61
|
+
code and no constant table from any other barcode implementation was used,
|
|
62
|
+
consulted or copied, under any licence.
|
|
63
|
+
|
|
64
|
+
|
|
65
|
+
CONCLUSION
|
|
66
|
+
--------------------------------------------------------------------------
|
|
67
|
+
|
|
68
|
+
This project's original implementation is distributed under the MIT
|
|
69
|
+
License. The ISO/IEC and GS1 references establish the technical standards;
|
|
70
|
+
this engineering inventory does not claim to have resolved the
|
|
71
|
+
patent-release or trademark entries that remain marked [TO VERIFY].
|
|
72
|
+
|
|
73
|
+
References relied upon:
|
|
74
|
+
- ISO/IEC 16022:2024, ISO/IEC
|
|
75
|
+
- GS1 General Specifications, GS1 AISBL
|
|
76
|
+
- GS1 DataMatrix Guideline, GS1 AISBL
|
|
77
|
+
- Reported ECC 200 patent-release position [TO VERIFY]
|
|
78
|
+
- Data Matrix trademark status and ownership [TO VERIFY]
|
|
79
|
+
|
|
80
|
+
This is an engineering assessment, not legal advice. Items marked
|
|
81
|
+
[TO VERIFY] have not been independently confirmed by the author. If your
|
|
82
|
+
use is commercially sensitive, obtain your own legal review.
|
package/package.json
CHANGED
|
@@ -1,89 +1,92 @@
|
|
|
1
|
-
{
|
|
2
|
-
"name": "@sythos/js_barcode_universal",
|
|
3
|
-
"version": "
|
|
4
|
-
"description": "Read and write barcodes in JavaScript. 100% original code, zero runtime dependencies. QR Code, EAN/UPC, ISBN, Code 128, Code 39, Code 93, ITF, Codabar, MSI, Code 11 and Pharmacode.",
|
|
5
|
-
"author": "Sythos",
|
|
6
|
-
"license": "MIT",
|
|
7
|
-
"type": "module",
|
|
8
|
-
"main": "./src/index.js",
|
|
9
|
-
"module": "./src/index.js",
|
|
10
|
-
"browser": "./bundle/sythos-barcode.js",
|
|
11
|
-
"unpkg": "./bundle/sythos-barcode.js",
|
|
12
|
-
"jsdelivr": "./bundle/sythos-barcode.js",
|
|
13
|
-
"sideEffects": false,
|
|
14
|
-
"engines": {
|
|
15
|
-
"node": ">=18"
|
|
16
|
-
},
|
|
17
|
-
"publishConfig": {
|
|
18
|
-
"access": "public"
|
|
19
|
-
},
|
|
20
|
-
"exports": {
|
|
21
|
-
".": "./src/index.js",
|
|
22
|
-
"./core": "./src/core/index.js",
|
|
23
|
-
"./image": "./src/image/index.js",
|
|
24
|
-
"./oned": "./src/oned/index.js",
|
|
1
|
+
{
|
|
2
|
+
"name": "@sythos/js_barcode_universal",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "Read and write barcodes in JavaScript. 100% original code, zero runtime dependencies. Data Matrix ECC 200, QR Code, EAN/UPC, ISBN, Code 128, Code 39, Code 93, ITF, Codabar, MSI, Code 11 and Pharmacode.",
|
|
5
|
+
"author": "Sythos",
|
|
6
|
+
"license": "MIT",
|
|
7
|
+
"type": "module",
|
|
8
|
+
"main": "./src/index.js",
|
|
9
|
+
"module": "./src/index.js",
|
|
10
|
+
"browser": "./bundle/sythos-barcode.js",
|
|
11
|
+
"unpkg": "./bundle/sythos-barcode.js",
|
|
12
|
+
"jsdelivr": "./bundle/sythos-barcode.js",
|
|
13
|
+
"sideEffects": false,
|
|
14
|
+
"engines": {
|
|
15
|
+
"node": ">=18"
|
|
16
|
+
},
|
|
17
|
+
"publishConfig": {
|
|
18
|
+
"access": "public"
|
|
19
|
+
},
|
|
20
|
+
"exports": {
|
|
21
|
+
".": "./src/index.js",
|
|
22
|
+
"./core": "./src/core/index.js",
|
|
23
|
+
"./image": "./src/image/index.js",
|
|
24
|
+
"./oned": "./src/oned/index.js",
|
|
25
25
|
"./qr": "./src/qr/index.js",
|
|
26
|
-
"./
|
|
27
|
-
"./render
|
|
28
|
-
"./render/
|
|
29
|
-
"./render/
|
|
30
|
-
"./
|
|
31
|
-
"./bundle
|
|
32
|
-
"./
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
"
|
|
37
|
-
"
|
|
38
|
-
"
|
|
39
|
-
"
|
|
40
|
-
"
|
|
41
|
-
"
|
|
42
|
-
|
|
43
|
-
|
|
26
|
+
"./datamatrix": "./src/datamatrix/index.js",
|
|
27
|
+
"./render": "./src/render/index.js",
|
|
28
|
+
"./render/svg": "./src/render/svg.js",
|
|
29
|
+
"./render/png": "./src/render/png.js",
|
|
30
|
+
"./render/image-data": "./src/render/image-data.js",
|
|
31
|
+
"./bundle": "./bundle/sythos-barcode.esm.js",
|
|
32
|
+
"./bundle/iife": "./bundle/sythos-barcode.js",
|
|
33
|
+
"./package.json": "./package.json"
|
|
34
|
+
},
|
|
35
|
+
"files": [
|
|
36
|
+
"src",
|
|
37
|
+
"bundle",
|
|
38
|
+
"examples",
|
|
39
|
+
"licenses",
|
|
40
|
+
"LICENSE",
|
|
41
|
+
"NOTICE.md",
|
|
42
|
+
"README.md"
|
|
43
|
+
],
|
|
44
|
+
"keywords": [
|
|
44
45
|
"barcode",
|
|
45
|
-
"
|
|
46
|
-
"
|
|
47
|
-
"
|
|
48
|
-
"
|
|
49
|
-
"
|
|
50
|
-
"
|
|
51
|
-
"
|
|
52
|
-
"
|
|
53
|
-
"
|
|
54
|
-
"
|
|
55
|
-
"
|
|
56
|
-
"
|
|
57
|
-
"
|
|
58
|
-
"
|
|
59
|
-
"
|
|
60
|
-
"
|
|
61
|
-
"
|
|
62
|
-
"
|
|
63
|
-
"
|
|
64
|
-
"
|
|
65
|
-
"
|
|
66
|
-
"
|
|
67
|
-
"
|
|
68
|
-
"
|
|
69
|
-
"
|
|
70
|
-
"
|
|
71
|
-
"
|
|
72
|
-
"
|
|
73
|
-
"
|
|
74
|
-
"
|
|
75
|
-
"
|
|
76
|
-
"
|
|
77
|
-
"
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
"
|
|
89
|
-
}
|
|
46
|
+
"datamatrix",
|
|
47
|
+
"ecc200",
|
|
48
|
+
"barcode-generator",
|
|
49
|
+
"barcode-scanner",
|
|
50
|
+
"qrcode",
|
|
51
|
+
"qr",
|
|
52
|
+
"qr-code",
|
|
53
|
+
"ean",
|
|
54
|
+
"ean13",
|
|
55
|
+
"ean8",
|
|
56
|
+
"upc",
|
|
57
|
+
"upca",
|
|
58
|
+
"upce",
|
|
59
|
+
"isbn",
|
|
60
|
+
"code128",
|
|
61
|
+
"gs1-128",
|
|
62
|
+
"code39",
|
|
63
|
+
"code93",
|
|
64
|
+
"itf",
|
|
65
|
+
"itf14",
|
|
66
|
+
"codabar",
|
|
67
|
+
"msi",
|
|
68
|
+
"code11",
|
|
69
|
+
"pharmacode",
|
|
70
|
+
"scanner",
|
|
71
|
+
"decoder",
|
|
72
|
+
"encoder",
|
|
73
|
+
"generator",
|
|
74
|
+
"reader",
|
|
75
|
+
"writer",
|
|
76
|
+
"zero-dependencies",
|
|
77
|
+
"no-dependencies",
|
|
78
|
+
"browser",
|
|
79
|
+
"nodejs",
|
|
80
|
+
"esm"
|
|
81
|
+
],
|
|
82
|
+
"repository": {
|
|
83
|
+
"type": "git",
|
|
84
|
+
"url": "git+https://github.com/Sythos/JS_Barcode_Universal.git"
|
|
85
|
+
},
|
|
86
|
+
"bugs": {
|
|
87
|
+
"url": "https://github.com/Sythos/JS_Barcode_Universal/issues"
|
|
88
|
+
},
|
|
89
|
+
"homepage": "https://github.com/Sythos/JS_Barcode_Universal#readme",
|
|
90
|
+
"dependencies": {},
|
|
91
|
+
"devDependencies": {}
|
|
92
|
+
}
|
package/src/core/reed-solomon.js
CHANGED
|
@@ -56,7 +56,7 @@ import { ChecksumError } from './errors.js';
|
|
|
56
56
|
*
|
|
57
57
|
* g(x) = product over i of (x - a^(base + i)), i = 0 .. eccLen-1
|
|
58
58
|
*
|
|
59
|
-
* `base` is 0 for QR
|
|
59
|
+
* `base` is 0 for QR and Aztec; 1 for Data Matrix and PDF417.
|
|
60
60
|
*
|
|
61
61
|
* @param {number} eccLen
|
|
62
62
|
* @param {import('./galois-field.js').GaloisField} field
|
|
@@ -0,0 +1,262 @@
|
|
|
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
|
+
* Data Matrix ECC 200 decoder for an already sampled symbol.
|
|
33
|
+
*
|
|
34
|
+
* The detector owns locating, perspective correction and orientation. This
|
|
35
|
+
* module starts with the complete, upright symbol including its finder borders.
|
|
36
|
+
* The table entry is deliberately read through a small normalizer so table data
|
|
37
|
+
* remains declarative: it needs total rows/columns, one data-region's rows and
|
|
38
|
+
* columns, data/ECC codeword counts, and either a block count or data block
|
|
39
|
+
* lengths. The standard 144x144 uneven data blocks are supported.
|
|
40
|
+
*
|
|
41
|
+
* @module datamatrix/decoder
|
|
42
|
+
*/
|
|
43
|
+
|
|
44
|
+
import { ChecksumError, FormatError } from '../core/errors.js';
|
|
45
|
+
import { GF256_DM } from '../core/galois-field.js';
|
|
46
|
+
import { rsDecode } from '../core/reed-solomon.js';
|
|
47
|
+
import { SYMBOLS } from './tables.js';
|
|
48
|
+
|
|
49
|
+
const CW_PAD = 129;
|
|
50
|
+
const CW_BASE256 = 231;
|
|
51
|
+
|
|
52
|
+
/** @param {object} entry @param {...string} names @returns {number | undefined} */
|
|
53
|
+
function numberField(entry, ...names) {
|
|
54
|
+
for (const name of names) if (Number.isInteger(entry[name])) return entry[name];
|
|
55
|
+
return undefined;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
/** Normalize the public table entry into the decoder's geometry contract. */
|
|
59
|
+
function layoutFor(width, height) {
|
|
60
|
+
const entry = SYMBOLS.find((s) =>
|
|
61
|
+
numberField(s, 'columns', 'cols', 'matrixColumns', 'width') === width &&
|
|
62
|
+
numberField(s, 'rows', 'matrixRows', 'height') === height);
|
|
63
|
+
if (!entry) throw new FormatError(`Data Matrix: ${width}x${height} is not an ECC 200 symbol size`);
|
|
64
|
+
|
|
65
|
+
const regionRows = numberField(entry, 'dataRegionRows', 'regionRows') ??
|
|
66
|
+
(numberField(entry, 'regionHeight') ? numberField(entry, 'regionHeight') - 2 : undefined);
|
|
67
|
+
const regionCols = numberField(entry, 'dataRegionColumns', 'dataRegionCols', 'regionColumns') ??
|
|
68
|
+
(numberField(entry, 'regionWidth') ? numberField(entry, 'regionWidth') - 2 : undefined);
|
|
69
|
+
const dataCount = numberField(entry, 'dataCodewords', 'dataCapacity');
|
|
70
|
+
const eccCount = numberField(entry, 'errorCodewords', 'eccCodewords');
|
|
71
|
+
const blockCount = numberField(entry, 'interleavedBlocks', 'interleavedBlockCount', 'blockCount', 'rsBlocks') || 1;
|
|
72
|
+
if (!regionRows || !regionCols || dataCount === undefined || eccCount === undefined ||
|
|
73
|
+
height % (regionRows + 2) || width % (regionCols + 2) || eccCount % blockCount) {
|
|
74
|
+
throw new FormatError(`Data Matrix: invalid table layout for ${width}x${height}`);
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
const rows = height / (regionRows + 2);
|
|
78
|
+
const cols = width / (regionCols + 2);
|
|
79
|
+
const blockData = Array.isArray(entry.blockDataCodewords) ? entry.blockDataCodewords.slice() :
|
|
80
|
+
Array.isArray(entry.dataCodewordsPerBlock) ? entry.dataCodewordsPerBlock.slice() : null;
|
|
81
|
+
let dataLengths;
|
|
82
|
+
if (blockData) {
|
|
83
|
+
dataLengths = blockData;
|
|
84
|
+
} else {
|
|
85
|
+
// The sole uneven ECC 200 distribution is 144x144: its first eight of ten
|
|
86
|
+
// blocks contain one extra data codeword. This derives it instead of hiding
|
|
87
|
+
// a magic size check in the deinterleaver.
|
|
88
|
+
const short = Math.floor(dataCount / blockCount);
|
|
89
|
+
dataLengths = new Array(blockCount).fill(short);
|
|
90
|
+
for (let i = 0; i < dataCount % blockCount; i++) dataLengths[i]++;
|
|
91
|
+
}
|
|
92
|
+
if (dataLengths.length !== blockCount || dataLengths.reduce((a, b) => a + b, 0) !== dataCount) {
|
|
93
|
+
throw new FormatError(`Data Matrix: inconsistent block layout for ${width}x${height}`);
|
|
94
|
+
}
|
|
95
|
+
return { entry, regionRows, regionCols, regionRowCount: rows, regionColCount: cols,
|
|
96
|
+
dataRows: rows * regionRows, dataCols: cols * regionCols, dataCount, eccCount,
|
|
97
|
+
blockCount, eccPerBlock: eccCount / blockCount, dataLengths };
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
/** Remove the L/finders from every data region, retaining only placement modules. */
|
|
101
|
+
function extractDataModules(matrix, layout) {
|
|
102
|
+
const data = new Uint8Array(layout.dataRows * layout.dataCols);
|
|
103
|
+
for (let regionY = 0; regionY < layout.regionRowCount; regionY++) {
|
|
104
|
+
for (let regionX = 0; regionX < layout.regionColCount; regionX++) {
|
|
105
|
+
const sourceX = regionX * (layout.regionCols + 2) + 1;
|
|
106
|
+
const sourceY = regionY * (layout.regionRows + 2) + 1;
|
|
107
|
+
for (let y = 0; y < layout.regionRows; y++) {
|
|
108
|
+
const targetY = regionY * layout.regionRows + y;
|
|
109
|
+
for (let x = 0; x < layout.regionCols; x++) {
|
|
110
|
+
data[targetY * layout.dataCols + regionX * layout.regionCols + x] =
|
|
111
|
+
matrix.get(sourceX + x, sourceY + y) ? 1 : 0;
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
return data;
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
/** Read placement codewords using the ECC 200 Utah sweep (the inverse writer path). */
|
|
120
|
+
function readPlacement(modules, rows, cols, count) {
|
|
121
|
+
const seen = new Uint8Array(rows * cols);
|
|
122
|
+
const out = new Uint8Array(count);
|
|
123
|
+
const get = (row, col) => modules[row * cols + col] !== 0;
|
|
124
|
+
const module = (row, col) => {
|
|
125
|
+
if (row < 0) { row += rows; col += 4 - ((rows + 4) % 8); }
|
|
126
|
+
if (col < 0) { col += cols; row += 4 - ((cols + 4) % 8); }
|
|
127
|
+
if (row < 0 || row >= rows || col < 0 || col >= cols) {
|
|
128
|
+
throw new FormatError('Data Matrix: placement coordinate escaped data region');
|
|
129
|
+
}
|
|
130
|
+
seen[row * cols + col] = 1;
|
|
131
|
+
return get(row, col) ? 1 : 0;
|
|
132
|
+
};
|
|
133
|
+
const bits = (coords) => coords.reduce((value, p) => (value << 1) | module(p[0], p[1]), 0);
|
|
134
|
+
const utah = (row, col) => bits([[row - 2, col - 2], [row - 2, col - 1], [row - 1, col - 2], [row - 1, col - 1],
|
|
135
|
+
[row - 1, col], [row, col - 2], [row, col - 1], [row, col]]);
|
|
136
|
+
const corner1 = () => bits([[rows - 1, 0], [rows - 1, 1], [rows - 1, 2], [0, cols - 2], [0, cols - 1], [1, cols - 1], [2, cols - 1], [3, cols - 1]]);
|
|
137
|
+
const corner2 = () => bits([[rows - 3, 0], [rows - 2, 0], [rows - 1, 0], [0, cols - 4], [0, cols - 3], [0, cols - 2], [0, cols - 1], [1, cols - 1]]);
|
|
138
|
+
const corner3 = () => bits([[rows - 3, 0], [rows - 2, 0], [rows - 1, 0], [0, cols - 2], [0, cols - 1], [1, cols - 1], [2, cols - 1], [3, cols - 1]]);
|
|
139
|
+
const corner4 = () => bits([[rows - 1, 0], [rows - 1, cols - 1], [0, cols - 3], [0, cols - 2], [0, cols - 1], [1, cols - 3], [1, cols - 2], [1, cols - 1]]);
|
|
140
|
+
|
|
141
|
+
let row = 4, col = 0, n = 0;
|
|
142
|
+
const put = (value) => { if (n < count) out[n++] = value; };
|
|
143
|
+
do {
|
|
144
|
+
if (row === rows && col === 0) put(corner1());
|
|
145
|
+
if (row === rows - 2 && col === 0 && cols % 4 !== 0) put(corner2());
|
|
146
|
+
if (row === rows - 2 && col === 0 && cols % 8 === 4) put(corner3());
|
|
147
|
+
if (row === rows + 4 && col === 2 && cols % 8 === 0) put(corner4());
|
|
148
|
+
do { if (row < rows && col >= 0 && !seen[row * cols + col]) put(utah(row, col)); row -= 2; col += 2; } while (row >= 0 && col < cols);
|
|
149
|
+
row += 1; col += 3;
|
|
150
|
+
do { if (row >= 0 && col < cols && !seen[row * cols + col]) put(utah(row, col)); row += 2; col -= 2; } while (row < rows && col >= 0);
|
|
151
|
+
row += 3; col += 1;
|
|
152
|
+
} while (row < rows || col < cols);
|
|
153
|
+
if (n !== count) throw new FormatError(`Data Matrix: placement yielded ${n}, expected ${count} codewords`);
|
|
154
|
+
return out;
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
/** Restore RS blocks, correct them, then concatenate their data portions. */
|
|
158
|
+
function deinterleaveAndCorrect(codewords, layout) {
|
|
159
|
+
if (codewords.length !== layout.dataCount + layout.eccCount) throw new FormatError('Data Matrix: codeword count mismatch');
|
|
160
|
+
const blocks = layout.dataLengths.map((len) => new Uint8Array(len + layout.eccPerBlock));
|
|
161
|
+
// Data codewords arrive in their original stream order. ECC 200 deals that
|
|
162
|
+
// stream round-robin across the RS blocks, so the inverse is determined by
|
|
163
|
+
// the wire index rather than by splitting it into consecutive block-sized
|
|
164
|
+
// chunks. For 144x144, indices 1550..1557 naturally land in the eight long
|
|
165
|
+
// blocks while the two short blocks remain at 155 data codewords.
|
|
166
|
+
for (let i = 0; i < layout.dataCount; i++) {
|
|
167
|
+
blocks[i % layout.blockCount][Math.floor(i / layout.blockCount)] = codewords[i];
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
// Parity normally begins with block zero. The uneven 144x144 layout rotates
|
|
171
|
+
// the parity wire order to begin with its first short block; derive the same
|
|
172
|
+
// mapping from the declarative lengths instead of keying it to dimensions.
|
|
173
|
+
const longest = Math.max(...layout.dataLengths);
|
|
174
|
+
const firstShort = layout.dataLengths.findIndex((length) => length < longest);
|
|
175
|
+
const rotation = firstShort < 0 ? 0 : firstShort;
|
|
176
|
+
let at = layout.dataCount;
|
|
177
|
+
for (let i = 0; i < layout.eccPerBlock; i++) {
|
|
178
|
+
for (let slot = 0; slot < layout.blockCount; slot++) {
|
|
179
|
+
const block = (slot + rotation) % layout.blockCount;
|
|
180
|
+
blocks[block][layout.dataLengths[block] + i] = codewords[at++];
|
|
181
|
+
}
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
let corrections = 0;
|
|
185
|
+
const data = new Uint8Array(layout.dataCount);
|
|
186
|
+
for (let b = 0; b < blocks.length; b++) {
|
|
187
|
+
corrections += rsDecode(blocks[b], layout.eccPerBlock, GF256_DM, 1);
|
|
188
|
+
}
|
|
189
|
+
// Rebuild the original high-level codeword stream after correction. Keeping
|
|
190
|
+
// this in wire-index order is essential: concatenating block data passes
|
|
191
|
+
// single-block round trips but scrambles every multi-block payload.
|
|
192
|
+
for (let i = 0; i < layout.dataCount; i++) {
|
|
193
|
+
data[i] = blocks[i % layout.blockCount][Math.floor(i / layout.blockCount)];
|
|
194
|
+
}
|
|
195
|
+
return { data, corrections };
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
function unrandomize(value, position) {
|
|
199
|
+
const pseudo = ((149 * position) % 255) + 1;
|
|
200
|
+
return value - pseudo >= 0 ? value - pseudo : value - pseudo + 256;
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
/** Decode ASCII plus Base 256, preserving semantic bytes alongside text. */
|
|
204
|
+
function parseData(data) {
|
|
205
|
+
let text = '';
|
|
206
|
+
const bytes = [];
|
|
207
|
+
let upperShift = false;
|
|
208
|
+
let gs1 = false;
|
|
209
|
+
for (let i = 0; i < data.length;) {
|
|
210
|
+
const cw = data[i++];
|
|
211
|
+
if (cw === CW_PAD) break;
|
|
212
|
+
if (cw <= 128) {
|
|
213
|
+
const value = cw - 1 + (upperShift ? 128 : 0);
|
|
214
|
+
upperShift = false;
|
|
215
|
+
text += String.fromCharCode(value); bytes.push(value); continue;
|
|
216
|
+
}
|
|
217
|
+
if (cw <= 229) {
|
|
218
|
+
const pair = cw - 130;
|
|
219
|
+
const digits = String(pair).padStart(2, '0'); text += digits; bytes.push(digits.charCodeAt(0), digits.charCodeAt(1)); continue;
|
|
220
|
+
}
|
|
221
|
+
if (cw === 232) {
|
|
222
|
+
if (i === 1) gs1 = true;
|
|
223
|
+
else { text += '\x1d'; bytes.push(29); }
|
|
224
|
+
continue;
|
|
225
|
+
}
|
|
226
|
+
if (cw === 235) { upperShift = true; continue; }
|
|
227
|
+
if (cw === CW_BASE256) {
|
|
228
|
+
if (i >= data.length) throw new FormatError('Data Matrix: Base 256 length is missing');
|
|
229
|
+
let length = unrandomize(data[i], i + 1); i++;
|
|
230
|
+
if (length === 0) length = data.length - i;
|
|
231
|
+
else if (length >= 250) {
|
|
232
|
+
if (i >= data.length) throw new FormatError('Data Matrix: Base 256 extended length is missing');
|
|
233
|
+
length = 250 * (length - 249) + unrandomize(data[i], i + 1); i++;
|
|
234
|
+
}
|
|
235
|
+
if (i + length > data.length) throw new FormatError('Data Matrix: Base 256 segment exceeds data capacity');
|
|
236
|
+
const segment = new Uint8Array(length);
|
|
237
|
+
for (let n = 0; n < length; n++, i++) segment[n] = unrandomize(data[i], i + 1);
|
|
238
|
+
bytes.push(...segment);
|
|
239
|
+
for (let n = 0; n < segment.length; n++) text += String.fromCharCode(segment[n]);
|
|
240
|
+
continue;
|
|
241
|
+
}
|
|
242
|
+
throw new FormatError(`Data Matrix: unsupported encoding codeword ${cw}`);
|
|
243
|
+
}
|
|
244
|
+
return { text, bytes: Uint8Array.from(bytes), gs1 };
|
|
245
|
+
}
|
|
246
|
+
|
|
247
|
+
/**
|
|
248
|
+
* Decode an upright, sampled Data Matrix ECC 200 symbol.
|
|
249
|
+
*
|
|
250
|
+
* @param {import('../core/bit-matrix.js').BitMatrix} matrix Full symbol, no quiet zone.
|
|
251
|
+
* @returns {{text: string, bytes: Uint8Array, correctedErrors: number, symbol: object}}
|
|
252
|
+
*/
|
|
253
|
+
export function decodeDataMatrix(matrix) {
|
|
254
|
+
if (!matrix || !Number.isInteger(matrix.width) || !Number.isInteger(matrix.height)) throw new FormatError('Data Matrix: no matrix supplied');
|
|
255
|
+
const layout = layoutFor(matrix.width, matrix.height);
|
|
256
|
+
const placement = readPlacement(extractDataModules(matrix, layout), layout.dataRows, layout.dataCols, layout.dataCount + layout.eccCount);
|
|
257
|
+
const { data, corrections } = deinterleaveAndCorrect(placement, layout);
|
|
258
|
+
const result = parseData(data);
|
|
259
|
+
return { ...result, corrections, correctedErrors: corrections, symbol: layout.entry };
|
|
260
|
+
}
|
|
261
|
+
|
|
262
|
+
export { ChecksumError };
|