isahaq-barcode 1.0.0 → 1.2.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/.eslintrc.js +29 -29
- package/.prettierignore +11 -0
- package/.prettierrc +11 -0
- package/README.md +596 -549
- package/bin/generate.js +274 -274
- package/eslint.config.js +66 -68
- package/examples/basic-usage.js +58 -58
- package/examples/batch-example.json +56 -56
- package/examples/express-server.js +163 -159
- package/index.js +134 -134
- package/jest.config.js +13 -13
- package/package.json +27 -15
- package/src/builders/QrCodeBuilder.js +401 -401
- package/src/renderers/HTMLRenderer.js +212 -212
- package/src/renderers/PDFRenderer.js +184 -184
- package/src/renderers/PNGRenderer.js +123 -123
- package/src/renderers/RenderFormats.js +87 -87
- package/src/renderers/SVGRenderer.js +221 -221
- package/src/services/BarcodeService.js +175 -175
- package/src/types/BarcodeTypes.js +195 -195
- package/src/validators/Validator.js +352 -352
- package/tests/BarcodeGenerator.test.js +187 -187
- package/tests/BarcodeService.test.js +76 -76
- package/tests/QrCodeBuilder.test.js +130 -130
- package/tests/setup.js +59 -59
|
@@ -1,87 +1,87 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Render Formats - Supported output formats
|
|
3
|
-
*/
|
|
4
|
-
|
|
5
|
-
class RenderFormats {
|
|
6
|
-
static FORMATS = {
|
|
7
|
-
PNG: 'png',
|
|
8
|
-
SVG: 'svg',
|
|
9
|
-
HTML: 'html',
|
|
10
|
-
PDF: 'pdf',
|
|
11
|
-
JPG: 'jpg',
|
|
12
|
-
JPEG: 'jpeg',
|
|
13
|
-
};
|
|
14
|
-
|
|
15
|
-
static MIME_TYPES = {
|
|
16
|
-
png: 'image/png',
|
|
17
|
-
svg: 'image/svg+xml',
|
|
18
|
-
html: 'text/html',
|
|
19
|
-
pdf: 'application/pdf',
|
|
20
|
-
jpg: 'image/jpeg',
|
|
21
|
-
jpeg: 'image/jpeg',
|
|
22
|
-
};
|
|
23
|
-
|
|
24
|
-
static EXTENSIONS = {
|
|
25
|
-
png: '.png',
|
|
26
|
-
svg: '.svg',
|
|
27
|
-
html: '.html',
|
|
28
|
-
pdf: '.pdf',
|
|
29
|
-
jpg: '.jpg',
|
|
30
|
-
jpeg: '.jpeg',
|
|
31
|
-
};
|
|
32
|
-
|
|
33
|
-
/**
|
|
34
|
-
* Get all supported formats
|
|
35
|
-
* @returns {Array} Array of all formats
|
|
36
|
-
*/
|
|
37
|
-
static getAll() {
|
|
38
|
-
return Object.values(this.FORMATS);
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
/**
|
|
42
|
-
* Check if a format is valid
|
|
43
|
-
* @param {string} format - The format to check
|
|
44
|
-
* @returns {boolean} True if valid
|
|
45
|
-
*/
|
|
46
|
-
static isValid(format) {
|
|
47
|
-
return Object.values(this.FORMATS).includes(format);
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
/**
|
|
51
|
-
* Get MIME type for a format
|
|
52
|
-
* @param {string} format - The format
|
|
53
|
-
* @returns {string} MIME type
|
|
54
|
-
*/
|
|
55
|
-
static getMimeType(format) {
|
|
56
|
-
return this.MIME_TYPES[format] || 'application/octet-stream';
|
|
57
|
-
}
|
|
58
|
-
|
|
59
|
-
/**
|
|
60
|
-
* Get file extension for a format
|
|
61
|
-
* @param {string} format - The format
|
|
62
|
-
* @returns {string} File extension
|
|
63
|
-
*/
|
|
64
|
-
static getExtension(format) {
|
|
65
|
-
return this.EXTENSIONS[format] || '';
|
|
66
|
-
}
|
|
67
|
-
|
|
68
|
-
/**
|
|
69
|
-
* Get format description
|
|
70
|
-
* @param {string} format - The format
|
|
71
|
-
* @returns {string} Description
|
|
72
|
-
*/
|
|
73
|
-
static getDescription(format) {
|
|
74
|
-
const descriptions = {
|
|
75
|
-
png: 'Portable Network Graphics - Raster image format',
|
|
76
|
-
svg: 'Scalable Vector Graphics - Vector image format',
|
|
77
|
-
html: 'HyperText Markup Language - Web page format',
|
|
78
|
-
pdf: 'Portable Document Format - Document format',
|
|
79
|
-
jpg: 'JPEG - Compressed raster image format',
|
|
80
|
-
jpeg: 'JPEG - Compressed raster image format',
|
|
81
|
-
};
|
|
82
|
-
|
|
83
|
-
return descriptions[format] || 'Unknown format';
|
|
84
|
-
}
|
|
85
|
-
}
|
|
86
|
-
|
|
87
|
-
module.exports = { RenderFormats };
|
|
1
|
+
/**
|
|
2
|
+
* Render Formats - Supported output formats
|
|
3
|
+
*/
|
|
4
|
+
|
|
5
|
+
class RenderFormats {
|
|
6
|
+
static FORMATS = {
|
|
7
|
+
PNG: 'png',
|
|
8
|
+
SVG: 'svg',
|
|
9
|
+
HTML: 'html',
|
|
10
|
+
PDF: 'pdf',
|
|
11
|
+
JPG: 'jpg',
|
|
12
|
+
JPEG: 'jpeg',
|
|
13
|
+
};
|
|
14
|
+
|
|
15
|
+
static MIME_TYPES = {
|
|
16
|
+
png: 'image/png',
|
|
17
|
+
svg: 'image/svg+xml',
|
|
18
|
+
html: 'text/html',
|
|
19
|
+
pdf: 'application/pdf',
|
|
20
|
+
jpg: 'image/jpeg',
|
|
21
|
+
jpeg: 'image/jpeg',
|
|
22
|
+
};
|
|
23
|
+
|
|
24
|
+
static EXTENSIONS = {
|
|
25
|
+
png: '.png',
|
|
26
|
+
svg: '.svg',
|
|
27
|
+
html: '.html',
|
|
28
|
+
pdf: '.pdf',
|
|
29
|
+
jpg: '.jpg',
|
|
30
|
+
jpeg: '.jpeg',
|
|
31
|
+
};
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* Get all supported formats
|
|
35
|
+
* @returns {Array} Array of all formats
|
|
36
|
+
*/
|
|
37
|
+
static getAll() {
|
|
38
|
+
return Object.values(this.FORMATS);
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
/**
|
|
42
|
+
* Check if a format is valid
|
|
43
|
+
* @param {string} format - The format to check
|
|
44
|
+
* @returns {boolean} True if valid
|
|
45
|
+
*/
|
|
46
|
+
static isValid(format) {
|
|
47
|
+
return Object.values(this.FORMATS).includes(format);
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
/**
|
|
51
|
+
* Get MIME type for a format
|
|
52
|
+
* @param {string} format - The format
|
|
53
|
+
* @returns {string} MIME type
|
|
54
|
+
*/
|
|
55
|
+
static getMimeType(format) {
|
|
56
|
+
return this.MIME_TYPES[format] || 'application/octet-stream';
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
/**
|
|
60
|
+
* Get file extension for a format
|
|
61
|
+
* @param {string} format - The format
|
|
62
|
+
* @returns {string} File extension
|
|
63
|
+
*/
|
|
64
|
+
static getExtension(format) {
|
|
65
|
+
return this.EXTENSIONS[format] || '';
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
/**
|
|
69
|
+
* Get format description
|
|
70
|
+
* @param {string} format - The format
|
|
71
|
+
* @returns {string} Description
|
|
72
|
+
*/
|
|
73
|
+
static getDescription(format) {
|
|
74
|
+
const descriptions = {
|
|
75
|
+
png: 'Portable Network Graphics - Raster image format',
|
|
76
|
+
svg: 'Scalable Vector Graphics - Vector image format',
|
|
77
|
+
html: 'HyperText Markup Language - Web page format',
|
|
78
|
+
pdf: 'Portable Document Format - Document format',
|
|
79
|
+
jpg: 'JPEG - Compressed raster image format',
|
|
80
|
+
jpeg: 'JPEG - Compressed raster image format',
|
|
81
|
+
};
|
|
82
|
+
|
|
83
|
+
return descriptions[format] || 'Unknown format';
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
module.exports = { RenderFormats };
|
|
@@ -1,221 +1,221 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* SVG Renderer - Renders barcodes as SVG
|
|
3
|
-
*/
|
|
4
|
-
|
|
5
|
-
const JsBarcode = require('jsbarcode');
|
|
6
|
-
|
|
7
|
-
class SVGRenderer {
|
|
8
|
-
constructor() {
|
|
9
|
-
this.defaultOptions = {
|
|
10
|
-
width: 2,
|
|
11
|
-
height: 100,
|
|
12
|
-
displayValue: true,
|
|
13
|
-
fontSize: 20,
|
|
14
|
-
textAlign: 'center',
|
|
15
|
-
textPosition: 'bottom',
|
|
16
|
-
textMargin: 2,
|
|
17
|
-
background: '#ffffff',
|
|
18
|
-
lineColor: '#000000',
|
|
19
|
-
margin: 10,
|
|
20
|
-
marginTop: 10,
|
|
21
|
-
marginBottom: 10,
|
|
22
|
-
marginLeft: 10,
|
|
23
|
-
marginRight: 10,
|
|
24
|
-
};
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
/**
|
|
28
|
-
* Render a barcode as SVG
|
|
29
|
-
* @param {string} data - The data to encode
|
|
30
|
-
* @param {string} type - The barcode type
|
|
31
|
-
* @param {Object} options - Render options
|
|
32
|
-
* @returns {string} SVG string
|
|
33
|
-
*/
|
|
34
|
-
render(data, type, options = {}) {
|
|
35
|
-
try {
|
|
36
|
-
// Merge options with defaults
|
|
37
|
-
const renderOptions = { ...this.defaultOptions, ...options };
|
|
38
|
-
|
|
39
|
-
// Create a temporary canvas element for JsBarcode
|
|
40
|
-
const canvas = document.createElement('canvas');
|
|
41
|
-
|
|
42
|
-
// Generate barcode using JsBarcode
|
|
43
|
-
JsBarcode(canvas, data, {
|
|
44
|
-
format: this.mapBarcodeType(type),
|
|
45
|
-
width: renderOptions.width,
|
|
46
|
-
height: renderOptions.height,
|
|
47
|
-
displayValue: renderOptions.displayValue,
|
|
48
|
-
fontSize: renderOptions.fontSize,
|
|
49
|
-
textAlign: renderOptions.textAlign,
|
|
50
|
-
textPosition: renderOptions.textPosition,
|
|
51
|
-
textMargin: renderOptions.textMargin,
|
|
52
|
-
background: renderOptions.background,
|
|
53
|
-
lineColor: renderOptions.lineColor,
|
|
54
|
-
margin: renderOptions.margin,
|
|
55
|
-
marginTop: renderOptions.marginTop,
|
|
56
|
-
marginBottom: renderOptions.marginBottom,
|
|
57
|
-
marginLeft: renderOptions.marginLeft,
|
|
58
|
-
marginRight: renderOptions.marginRight,
|
|
59
|
-
xmlDocument: this.createXMLDocument(),
|
|
60
|
-
});
|
|
61
|
-
|
|
62
|
-
// Get SVG string from canvas
|
|
63
|
-
const svgString = canvas.toSVG();
|
|
64
|
-
return svgString;
|
|
65
|
-
} catch {
|
|
66
|
-
// Fallback: create SVG manually
|
|
67
|
-
return this.createManualSVG(data, type, options);
|
|
68
|
-
}
|
|
69
|
-
}
|
|
70
|
-
|
|
71
|
-
/**
|
|
72
|
-
* Create SVG manually as fallback
|
|
73
|
-
* @param {string} data - The data to encode
|
|
74
|
-
* @param {string} type - The barcode type
|
|
75
|
-
* @param {Object} options - Render options
|
|
76
|
-
* @returns {string} SVG string
|
|
77
|
-
*/
|
|
78
|
-
createManualSVG(data, type, options = {}) {
|
|
79
|
-
const renderOptions = { ...this.defaultOptions, ...options };
|
|
80
|
-
|
|
81
|
-
// Simple SVG structure for basic barcodes
|
|
82
|
-
const width = 300;
|
|
83
|
-
const height = renderOptions.height + (renderOptions.displayValue ? 30 : 0);
|
|
84
|
-
|
|
85
|
-
let svg = `<svg width="${width}" height="${height}" xmlns="http://www.w3.org/2000/svg">`;
|
|
86
|
-
svg += `<rect width="${width}" height="${height}" fill="${renderOptions.background}"/>`;
|
|
87
|
-
|
|
88
|
-
// Add barcode representation (simplified)
|
|
89
|
-
if (type === 'qrcode') {
|
|
90
|
-
svg += this.createQRCodeSVG(data, width, height, renderOptions);
|
|
91
|
-
} else {
|
|
92
|
-
svg += this.createLinearBarcodeSVG(data, width, height, renderOptions);
|
|
93
|
-
}
|
|
94
|
-
|
|
95
|
-
// Add text if displayValue is true
|
|
96
|
-
if (renderOptions.displayValue) {
|
|
97
|
-
svg += `<text x="${width / 2}" y="${
|
|
98
|
-
height - 5
|
|
99
|
-
}" text-anchor="middle" font-family="Arial" font-size="${
|
|
100
|
-
renderOptions.fontSize
|
|
101
|
-
}" fill="${renderOptions.lineColor}">${data}</text>`;
|
|
102
|
-
}
|
|
103
|
-
|
|
104
|
-
svg += '</svg>';
|
|
105
|
-
return svg;
|
|
106
|
-
}
|
|
107
|
-
|
|
108
|
-
/**
|
|
109
|
-
* Create QR Code SVG representation
|
|
110
|
-
* @param {string} data - The data to encode
|
|
111
|
-
* @param {number} width - SVG width
|
|
112
|
-
* @param {number} height - SVG height
|
|
113
|
-
* @param {Object} options - Render options
|
|
114
|
-
* @returns {string} SVG content
|
|
115
|
-
*/
|
|
116
|
-
createQRCodeSVG(data, width, height, options) {
|
|
117
|
-
// This is a simplified representation
|
|
118
|
-
// In a real implementation, you would use a QR code library
|
|
119
|
-
const size = Math.min(width, height) - 40;
|
|
120
|
-
const x = (width - size) / 2;
|
|
121
|
-
const y = (height - size) / 2;
|
|
122
|
-
|
|
123
|
-
return `<rect x="${x}" y="${y}" width="${size}" height="${size}" fill="${options.lineColor}" stroke="${options.lineColor}" stroke-width="1"/>`;
|
|
124
|
-
}
|
|
125
|
-
|
|
126
|
-
/**
|
|
127
|
-
* Create linear barcode SVG representation
|
|
128
|
-
* @param {string} data - The data to encode
|
|
129
|
-
* @param {number} width - SVG width
|
|
130
|
-
* @param {number} height - SVG height
|
|
131
|
-
* @param {Object} options - Render options
|
|
132
|
-
* @returns {string} SVG content
|
|
133
|
-
*/
|
|
134
|
-
createLinearBarcodeSVG(data, width, height, options) {
|
|
135
|
-
// This is a simplified representation
|
|
136
|
-
// In a real implementation, you would use a barcode library
|
|
137
|
-
const barWidth = 2;
|
|
138
|
-
const barHeight = height - (options.displayValue ? 30 : 0);
|
|
139
|
-
const startX = 20;
|
|
140
|
-
let currentX = startX;
|
|
141
|
-
|
|
142
|
-
let svg = '';
|
|
143
|
-
for (let i = 0; i < data.length; i++) {
|
|
144
|
-
const char = data.charCodeAt(i);
|
|
145
|
-
const barCount = (char % 5) + 1;
|
|
146
|
-
|
|
147
|
-
for (let j = 0; j < barCount; j++) {
|
|
148
|
-
svg += `<rect x="${currentX}" y="10" width="${barWidth}" height="${barHeight}" fill="${options.lineColor}"/>`;
|
|
149
|
-
currentX += barWidth * 2;
|
|
150
|
-
}
|
|
151
|
-
}
|
|
152
|
-
|
|
153
|
-
return svg;
|
|
154
|
-
}
|
|
155
|
-
|
|
156
|
-
/**
|
|
157
|
-
* Create XML document for JsBarcode
|
|
158
|
-
* @returns {Object} XML document
|
|
159
|
-
*/
|
|
160
|
-
createXMLDocument() {
|
|
161
|
-
// Create a minimal XML document for JsBarcode
|
|
162
|
-
return {
|
|
163
|
-
createElement:
|
|
164
|
-
tagName,
|
|
165
|
-
setAttribute: () => {},
|
|
166
|
-
appendChild: () => {},
|
|
167
|
-
toSVG: () => '<svg></svg>',
|
|
168
|
-
}),
|
|
169
|
-
};
|
|
170
|
-
}
|
|
171
|
-
|
|
172
|
-
/**
|
|
173
|
-
* Map barcode type to JsBarcode format
|
|
174
|
-
* @param {string} type - The barcode type
|
|
175
|
-
* @returns {string} JsBarcode format
|
|
176
|
-
*/
|
|
177
|
-
mapBarcodeType(type) {
|
|
178
|
-
const typeMap = {
|
|
179
|
-
code128: 'CODE128',
|
|
180
|
-
code128a: 'CODE128A',
|
|
181
|
-
code128b: 'CODE128B',
|
|
182
|
-
code128c: 'CODE128C',
|
|
183
|
-
code128auto: 'CODE128',
|
|
184
|
-
code39: 'CODE39',
|
|
185
|
-
code39extended: 'CODE39',
|
|
186
|
-
code39checksum: 'CODE39',
|
|
187
|
-
code39auto: 'CODE39',
|
|
188
|
-
code93: 'CODE93',
|
|
189
|
-
ean13: 'EAN13',
|
|
190
|
-
ean8: 'EAN8',
|
|
191
|
-
upca: 'UPC',
|
|
192
|
-
upce: 'UPC',
|
|
193
|
-
codabar: 'codabar',
|
|
194
|
-
code11: 'CODE11',
|
|
195
|
-
msi: 'MSI',
|
|
196
|
-
pharmazentral: 'pharmazentral',
|
|
197
|
-
interleaved25: 'ITF',
|
|
198
|
-
standard25: 'STD25',
|
|
199
|
-
};
|
|
200
|
-
|
|
201
|
-
return typeMap[type] || 'CODE128';
|
|
202
|
-
}
|
|
203
|
-
|
|
204
|
-
/**
|
|
205
|
-
* Get default options
|
|
206
|
-
* @returns {Object} Default options
|
|
207
|
-
*/
|
|
208
|
-
getDefaultOptions() {
|
|
209
|
-
return { ...this.defaultOptions };
|
|
210
|
-
}
|
|
211
|
-
|
|
212
|
-
/**
|
|
213
|
-
* Set default options
|
|
214
|
-
* @param {Object} options - New default options
|
|
215
|
-
*/
|
|
216
|
-
setDefaultOptions(options) {
|
|
217
|
-
this.defaultOptions = { ...this.defaultOptions, ...options };
|
|
218
|
-
}
|
|
219
|
-
}
|
|
220
|
-
|
|
221
|
-
module.exports = SVGRenderer;
|
|
1
|
+
/**
|
|
2
|
+
* SVG Renderer - Renders barcodes as SVG
|
|
3
|
+
*/
|
|
4
|
+
|
|
5
|
+
const JsBarcode = require('jsbarcode');
|
|
6
|
+
|
|
7
|
+
class SVGRenderer {
|
|
8
|
+
constructor() {
|
|
9
|
+
this.defaultOptions = {
|
|
10
|
+
width: 2,
|
|
11
|
+
height: 100,
|
|
12
|
+
displayValue: true,
|
|
13
|
+
fontSize: 20,
|
|
14
|
+
textAlign: 'center',
|
|
15
|
+
textPosition: 'bottom',
|
|
16
|
+
textMargin: 2,
|
|
17
|
+
background: '#ffffff',
|
|
18
|
+
lineColor: '#000000',
|
|
19
|
+
margin: 10,
|
|
20
|
+
marginTop: 10,
|
|
21
|
+
marginBottom: 10,
|
|
22
|
+
marginLeft: 10,
|
|
23
|
+
marginRight: 10,
|
|
24
|
+
};
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* Render a barcode as SVG
|
|
29
|
+
* @param {string} data - The data to encode
|
|
30
|
+
* @param {string} type - The barcode type
|
|
31
|
+
* @param {Object} options - Render options
|
|
32
|
+
* @returns {string} SVG string
|
|
33
|
+
*/
|
|
34
|
+
render(data, type, options = {}) {
|
|
35
|
+
try {
|
|
36
|
+
// Merge options with defaults
|
|
37
|
+
const renderOptions = { ...this.defaultOptions, ...options };
|
|
38
|
+
|
|
39
|
+
// Create a temporary canvas element for JsBarcode
|
|
40
|
+
const canvas = document.createElement('canvas');
|
|
41
|
+
|
|
42
|
+
// Generate barcode using JsBarcode
|
|
43
|
+
JsBarcode(canvas, data, {
|
|
44
|
+
format: this.mapBarcodeType(type),
|
|
45
|
+
width: renderOptions.width,
|
|
46
|
+
height: renderOptions.height,
|
|
47
|
+
displayValue: renderOptions.displayValue,
|
|
48
|
+
fontSize: renderOptions.fontSize,
|
|
49
|
+
textAlign: renderOptions.textAlign,
|
|
50
|
+
textPosition: renderOptions.textPosition,
|
|
51
|
+
textMargin: renderOptions.textMargin,
|
|
52
|
+
background: renderOptions.background,
|
|
53
|
+
lineColor: renderOptions.lineColor,
|
|
54
|
+
margin: renderOptions.margin,
|
|
55
|
+
marginTop: renderOptions.marginTop,
|
|
56
|
+
marginBottom: renderOptions.marginBottom,
|
|
57
|
+
marginLeft: renderOptions.marginLeft,
|
|
58
|
+
marginRight: renderOptions.marginRight,
|
|
59
|
+
xmlDocument: this.createXMLDocument(),
|
|
60
|
+
});
|
|
61
|
+
|
|
62
|
+
// Get SVG string from canvas
|
|
63
|
+
const svgString = canvas.toSVG();
|
|
64
|
+
return svgString;
|
|
65
|
+
} catch {
|
|
66
|
+
// Fallback: create SVG manually
|
|
67
|
+
return this.createManualSVG(data, type, options);
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
/**
|
|
72
|
+
* Create SVG manually as fallback
|
|
73
|
+
* @param {string} data - The data to encode
|
|
74
|
+
* @param {string} type - The barcode type
|
|
75
|
+
* @param {Object} options - Render options
|
|
76
|
+
* @returns {string} SVG string
|
|
77
|
+
*/
|
|
78
|
+
createManualSVG(data, type, options = {}) {
|
|
79
|
+
const renderOptions = { ...this.defaultOptions, ...options };
|
|
80
|
+
|
|
81
|
+
// Simple SVG structure for basic barcodes
|
|
82
|
+
const width = 300;
|
|
83
|
+
const height = renderOptions.height + (renderOptions.displayValue ? 30 : 0);
|
|
84
|
+
|
|
85
|
+
let svg = `<svg width="${width}" height="${height}" xmlns="http://www.w3.org/2000/svg">`;
|
|
86
|
+
svg += `<rect width="${width}" height="${height}" fill="${renderOptions.background}"/>`;
|
|
87
|
+
|
|
88
|
+
// Add barcode representation (simplified)
|
|
89
|
+
if (type === 'qrcode') {
|
|
90
|
+
svg += this.createQRCodeSVG(data, width, height, renderOptions);
|
|
91
|
+
} else {
|
|
92
|
+
svg += this.createLinearBarcodeSVG(data, width, height, renderOptions);
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
// Add text if displayValue is true
|
|
96
|
+
if (renderOptions.displayValue) {
|
|
97
|
+
svg += `<text x="${width / 2}" y="${
|
|
98
|
+
height - 5
|
|
99
|
+
}" text-anchor="middle" font-family="Arial" font-size="${
|
|
100
|
+
renderOptions.fontSize
|
|
101
|
+
}" fill="${renderOptions.lineColor}">${data}</text>`;
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
svg += '</svg>';
|
|
105
|
+
return svg;
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
/**
|
|
109
|
+
* Create QR Code SVG representation
|
|
110
|
+
* @param {string} data - The data to encode
|
|
111
|
+
* @param {number} width - SVG width
|
|
112
|
+
* @param {number} height - SVG height
|
|
113
|
+
* @param {Object} options - Render options
|
|
114
|
+
* @returns {string} SVG content
|
|
115
|
+
*/
|
|
116
|
+
createQRCodeSVG(data, width, height, options) {
|
|
117
|
+
// This is a simplified representation
|
|
118
|
+
// In a real implementation, you would use a QR code library
|
|
119
|
+
const size = Math.min(width, height) - 40;
|
|
120
|
+
const x = (width - size) / 2;
|
|
121
|
+
const y = (height - size) / 2;
|
|
122
|
+
|
|
123
|
+
return `<rect x="${x}" y="${y}" width="${size}" height="${size}" fill="${options.lineColor}" stroke="${options.lineColor}" stroke-width="1"/>`;
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
/**
|
|
127
|
+
* Create linear barcode SVG representation
|
|
128
|
+
* @param {string} data - The data to encode
|
|
129
|
+
* @param {number} width - SVG width
|
|
130
|
+
* @param {number} height - SVG height
|
|
131
|
+
* @param {Object} options - Render options
|
|
132
|
+
* @returns {string} SVG content
|
|
133
|
+
*/
|
|
134
|
+
createLinearBarcodeSVG(data, width, height, options) {
|
|
135
|
+
// This is a simplified representation
|
|
136
|
+
// In a real implementation, you would use a barcode library
|
|
137
|
+
const barWidth = 2;
|
|
138
|
+
const barHeight = height - (options.displayValue ? 30 : 0);
|
|
139
|
+
const startX = 20;
|
|
140
|
+
let currentX = startX;
|
|
141
|
+
|
|
142
|
+
let svg = '';
|
|
143
|
+
for (let i = 0; i < data.length; i++) {
|
|
144
|
+
const char = data.charCodeAt(i);
|
|
145
|
+
const barCount = (char % 5) + 1;
|
|
146
|
+
|
|
147
|
+
for (let j = 0; j < barCount; j++) {
|
|
148
|
+
svg += `<rect x="${currentX}" y="10" width="${barWidth}" height="${barHeight}" fill="${options.lineColor}"/>`;
|
|
149
|
+
currentX += barWidth * 2;
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
return svg;
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
/**
|
|
157
|
+
* Create XML document for JsBarcode
|
|
158
|
+
* @returns {Object} XML document
|
|
159
|
+
*/
|
|
160
|
+
createXMLDocument() {
|
|
161
|
+
// Create a minimal XML document for JsBarcode
|
|
162
|
+
return {
|
|
163
|
+
createElement: tagName => ({
|
|
164
|
+
tagName,
|
|
165
|
+
setAttribute: () => {},
|
|
166
|
+
appendChild: () => {},
|
|
167
|
+
toSVG: () => '<svg></svg>',
|
|
168
|
+
}),
|
|
169
|
+
};
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
/**
|
|
173
|
+
* Map barcode type to JsBarcode format
|
|
174
|
+
* @param {string} type - The barcode type
|
|
175
|
+
* @returns {string} JsBarcode format
|
|
176
|
+
*/
|
|
177
|
+
mapBarcodeType(type) {
|
|
178
|
+
const typeMap = {
|
|
179
|
+
code128: 'CODE128',
|
|
180
|
+
code128a: 'CODE128A',
|
|
181
|
+
code128b: 'CODE128B',
|
|
182
|
+
code128c: 'CODE128C',
|
|
183
|
+
code128auto: 'CODE128',
|
|
184
|
+
code39: 'CODE39',
|
|
185
|
+
code39extended: 'CODE39',
|
|
186
|
+
code39checksum: 'CODE39',
|
|
187
|
+
code39auto: 'CODE39',
|
|
188
|
+
code93: 'CODE93',
|
|
189
|
+
ean13: 'EAN13',
|
|
190
|
+
ean8: 'EAN8',
|
|
191
|
+
upca: 'UPC',
|
|
192
|
+
upce: 'UPC',
|
|
193
|
+
codabar: 'codabar',
|
|
194
|
+
code11: 'CODE11',
|
|
195
|
+
msi: 'MSI',
|
|
196
|
+
pharmazentral: 'pharmazentral',
|
|
197
|
+
interleaved25: 'ITF',
|
|
198
|
+
standard25: 'STD25',
|
|
199
|
+
};
|
|
200
|
+
|
|
201
|
+
return typeMap[type] || 'CODE128';
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
/**
|
|
205
|
+
* Get default options
|
|
206
|
+
* @returns {Object} Default options
|
|
207
|
+
*/
|
|
208
|
+
getDefaultOptions() {
|
|
209
|
+
return { ...this.defaultOptions };
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
/**
|
|
213
|
+
* Set default options
|
|
214
|
+
* @param {Object} options - New default options
|
|
215
|
+
*/
|
|
216
|
+
setDefaultOptions(options) {
|
|
217
|
+
this.defaultOptions = { ...this.defaultOptions, ...options };
|
|
218
|
+
}
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
module.exports = SVGRenderer;
|