isahaq-barcode 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.
@@ -0,0 +1,212 @@
1
+ /**
2
+ * HTML Renderer - Renders barcodes as HTML
3
+ */
4
+
5
+ class HTMLRenderer {
6
+ constructor() {
7
+ this.defaultOptions = {
8
+ width: 2,
9
+ height: 100,
10
+ displayValue: true,
11
+ fontSize: 20,
12
+ textAlign: 'center',
13
+ textPosition: 'bottom',
14
+ textMargin: 2,
15
+ background: '#ffffff',
16
+ lineColor: '#000000',
17
+ margin: 10,
18
+ marginTop: 10,
19
+ marginBottom: 10,
20
+ marginLeft: 10,
21
+ marginRight: 10,
22
+ className: 'barcode',
23
+ id: null,
24
+ };
25
+ }
26
+
27
+ /**
28
+ * Render a barcode as HTML
29
+ * @param {string} data - The data to encode
30
+ * @param {string} type - The barcode type
31
+ * @param {Object} options - Render options
32
+ * @returns {string} HTML string
33
+ */
34
+ render(data, type, options = {}) {
35
+ try {
36
+ // Merge options with defaults
37
+ const renderOptions = { ...this.defaultOptions, ...options };
38
+
39
+ // Generate HTML structure
40
+ const html = this.createHTMLStructure(data, type, renderOptions);
41
+ return html;
42
+ } catch (error) {
43
+ throw new Error(`HTML rendering failed: ${error.message}`);
44
+ }
45
+ }
46
+
47
+ /**
48
+ * Create HTML structure for barcode
49
+ * @param {string} data - The data to encode
50
+ * @param {string} type - The barcode type
51
+ * @param {Object} options - Render options
52
+ * @returns {string} HTML string
53
+ */
54
+ createHTMLStructure(data, type, options) {
55
+ const containerId =
56
+ options.id ||
57
+ `barcode-${Date.now()}-${Math.random().toString(36).substr(2, 9)}`;
58
+ const containerClass = options.className || 'barcode';
59
+
60
+ let html = `<div id="${containerId}" class="${containerClass}" style="${this.getContainerStyles(
61
+ options
62
+ )}">`;
63
+
64
+ // Add barcode representation
65
+ if (type === 'qrcode') {
66
+ html += this.createQRCodeHTML(data, options);
67
+ } else {
68
+ html += this.createLinearBarcodeHTML(data, type, options);
69
+ }
70
+
71
+ // Add text if displayValue is true
72
+ if (options.displayValue) {
73
+ html += `<div class="barcode-text" style="${this.getTextStyles(
74
+ options
75
+ )}">${data}</div>`;
76
+ }
77
+
78
+ html += '</div>';
79
+
80
+ // Add CSS styles
81
+ html += this.getCSSStyles(containerId, options);
82
+
83
+ return html;
84
+ }
85
+
86
+ /**
87
+ * Create QR Code HTML representation
88
+ * @param {string} data - The data to encode
89
+ * @param {Object} options - Render options
90
+ * @returns {string} HTML content
91
+ */
92
+ createQRCodeHTML(data, options) {
93
+ // This is a simplified representation
94
+ // In a real implementation, you would use a QR code library
95
+ const size = 200;
96
+ const cellSize = 10;
97
+ const cells = size / cellSize;
98
+
99
+ let html = `<div class="qr-code" style="width: ${size}px; height: ${size}px; display: grid; grid-template-columns: repeat(${cells}, 1fr); gap: 1px; background: ${options.background}; padding: 10px;">`;
100
+
101
+ // Generate a simple pattern based on data
102
+ for (let i = 0; i < cells * cells; i++) {
103
+ const shouldFill = (data.charCodeAt(i % data.length) + i) % 2 === 0;
104
+ const color = shouldFill ? options.lineColor : options.background;
105
+ html += `<div style="background-color: ${color}; width: 100%; height: 100%;"></div>`;
106
+ }
107
+
108
+ html += '</div>';
109
+ return html;
110
+ }
111
+
112
+ /**
113
+ * Create linear barcode HTML representation
114
+ * @param {string} data - The data to encode
115
+ * @param {string} type - The barcode type
116
+ * @param {Object} options - Render options
117
+ * @returns {string} HTML content
118
+ */
119
+ createLinearBarcodeHTML(data, type, options) {
120
+ const barWidth = options.width || 2;
121
+ const barHeight = options.height || 100;
122
+
123
+ let html = `<div class="linear-barcode" style="display: flex; align-items: flex-start; background: ${options.background}; padding: 10px;">`;
124
+
125
+ // Generate bars based on data
126
+ for (let i = 0; i < data.length; i++) {
127
+ const char = data.charCodeAt(i);
128
+ const barCount = (char % 5) + 1;
129
+
130
+ for (let j = 0; j < barCount; j++) {
131
+ html += `<div style="width: ${barWidth}px; height: ${barHeight}px; background-color: ${options.lineColor}; margin-right: ${barWidth}px;"></div>`;
132
+ }
133
+ }
134
+
135
+ html += '</div>';
136
+ return html;
137
+ }
138
+
139
+ /**
140
+ * Get container styles
141
+ * @param {Object} options - Render options
142
+ * @returns {string} CSS styles
143
+ */
144
+ getContainerStyles(options) {
145
+ return `
146
+ display: inline-block;
147
+ background: ${options.background};
148
+ padding: ${options.margin}px;
149
+ margin: ${options.marginTop}px ${options.marginRight}px ${options.marginBottom}px ${options.marginLeft}px;
150
+ border: 1px solid #ccc;
151
+ text-align: center;
152
+ `;
153
+ }
154
+
155
+ /**
156
+ * Get text styles
157
+ * @param {Object} options - Render options
158
+ * @returns {string} CSS styles
159
+ */
160
+ getTextStyles(options) {
161
+ return `
162
+ font-family: Arial, sans-serif;
163
+ font-size: ${options.fontSize}px;
164
+ color: ${options.lineColor};
165
+ text-align: ${options.textAlign};
166
+ margin-top: ${options.textMargin}px;
167
+ `;
168
+ }
169
+
170
+ /**
171
+ * Get CSS styles for the barcode
172
+ * @param {string} containerId - Container ID
173
+ * @param {Object} options - Render options
174
+ * @returns {string} CSS styles
175
+ */
176
+ getCSSStyles(containerId) {
177
+ return `
178
+ <style>
179
+ #${containerId} {
180
+ font-family: Arial, sans-serif;
181
+ }
182
+ #${containerId} .barcode-text {
183
+ font-weight: bold;
184
+ }
185
+ #${containerId} .qr-code {
186
+ margin: 0 auto;
187
+ }
188
+ #${containerId} .linear-barcode {
189
+ justify-content: center;
190
+ }
191
+ </style>
192
+ `;
193
+ }
194
+
195
+ /**
196
+ * Get default options
197
+ * @returns {Object} Default options
198
+ */
199
+ getDefaultOptions() {
200
+ return { ...this.defaultOptions };
201
+ }
202
+
203
+ /**
204
+ * Set default options
205
+ * @param {Object} options - New default options
206
+ */
207
+ setDefaultOptions(options) {
208
+ this.defaultOptions = { ...this.defaultOptions, ...options };
209
+ }
210
+ }
211
+
212
+ module.exports = HTMLRenderer;
@@ -0,0 +1,184 @@
1
+ /**
2
+ * PDF Renderer - Renders barcodes as PDF
3
+ */
4
+
5
+ const PDFDocument = require('pdfkit');
6
+
7
+ class PDFRenderer {
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
+ pageWidth: 612,
25
+ pageHeight: 792,
26
+ };
27
+ }
28
+
29
+ /**
30
+ * Render a barcode as PDF
31
+ * @param {string} data - The data to encode
32
+ * @param {string} type - The barcode type
33
+ * @param {Object} options - Render options
34
+ * @returns {Buffer} PDF buffer
35
+ */
36
+ render(data, type, options = {}) {
37
+ try {
38
+ // Merge options with defaults
39
+ const renderOptions = { ...this.defaultOptions, ...options };
40
+
41
+ // Create PDF document
42
+ const doc = new PDFDocument({
43
+ size: [renderOptions.pageWidth, renderOptions.pageHeight],
44
+ margins: {
45
+ top: renderOptions.marginTop,
46
+ bottom: renderOptions.marginBottom,
47
+ left: renderOptions.marginLeft,
48
+ right: renderOptions.marginRight,
49
+ },
50
+ });
51
+
52
+ // Collect PDF data
53
+ const chunks = [];
54
+ doc.on('data', (chunk) => chunks.push(chunk));
55
+
56
+ return new Promise((resolve, reject) => {
57
+ doc.on('end', () => {
58
+ const pdfBuffer = Buffer.concat(chunks);
59
+ resolve(pdfBuffer);
60
+ });
61
+
62
+ doc.on('error', reject);
63
+
64
+ // Add barcode to PDF
65
+ this.addBarcodeToPDF(doc, data, type, renderOptions);
66
+
67
+ // Finalize PDF
68
+ doc.end();
69
+ });
70
+ } catch (error) {
71
+ throw new Error(`PDF rendering failed: ${error.message}`);
72
+ }
73
+ }
74
+
75
+ /**
76
+ * Add barcode to PDF document
77
+ * @param {PDFDocument} doc - PDF document
78
+ * @param {string} data - The data to encode
79
+ * @param {string} type - The barcode type
80
+ * @param {Object} options - Render options
81
+ */
82
+ addBarcodeToPDF(doc, data, type, options) {
83
+ // Set background color
84
+ doc.rect(0, 0, options.pageWidth, options.pageHeight);
85
+ doc.fillColor(options.background);
86
+ doc.fill();
87
+
88
+ // Calculate position
89
+ const x = options.marginLeft;
90
+ const y = options.marginTop;
91
+
92
+ if (type === 'qrcode') {
93
+ this.addQRCodeToPDF(doc, data, x, y, options);
94
+ } else {
95
+ this.addLinearBarcodeToPDF(doc, data, type, x, y, options);
96
+ }
97
+
98
+ // Add text if displayValue is true
99
+ if (options.displayValue) {
100
+ doc.fontSize(options.fontSize);
101
+ doc.fillColor(options.lineColor);
102
+ doc.text(data, x, y + options.height + options.textMargin, {
103
+ align: options.textAlign,
104
+ });
105
+ }
106
+ }
107
+
108
+ /**
109
+ * Add QR Code to PDF
110
+ * @param {PDFDocument} doc - PDF document
111
+ * @param {string} data - The data to encode
112
+ * @param {number} x - X position
113
+ * @param {number} y - Y position
114
+ * @param {Object} options - Render options
115
+ */
116
+ addQRCodeToPDF(doc, data, x, y, options) {
117
+ // This is a simplified representation
118
+ // In a real implementation, you would use a QR code library
119
+ const size = 200;
120
+ const cellSize = 10;
121
+ const cells = size / cellSize;
122
+
123
+ // Draw QR code pattern
124
+ for (let row = 0; row < cells; row++) {
125
+ for (let col = 0; col < cells; col++) {
126
+ const shouldFill =
127
+ (data.charCodeAt((row * cells + col) % data.length) + row + col) %
128
+ 2 ===
129
+ 0;
130
+
131
+ if (shouldFill) {
132
+ doc.rect(x + col * cellSize, y + row * cellSize, cellSize, cellSize);
133
+ doc.fillColor(options.lineColor);
134
+ doc.fill();
135
+ }
136
+ }
137
+ }
138
+ }
139
+
140
+ /**
141
+ * Add linear barcode to PDF
142
+ * @param {PDFDocument} doc - PDF document
143
+ * @param {string} data - The data to encode
144
+ * @param {string} type - The barcode type
145
+ * @param {number} x - X position
146
+ * @param {number} y - Y position
147
+ * @param {Object} options - Render options
148
+ */
149
+ addLinearBarcodeToPDF(doc, data, type, x, y, options) {
150
+ const barWidth = options.width || 2;
151
+ let currentX = x;
152
+
153
+ // Draw bars based on data
154
+ for (let i = 0; i < data.length; i++) {
155
+ const char = data.charCodeAt(i);
156
+ const barCount = (char % 5) + 1;
157
+
158
+ for (let j = 0; j < barCount; j++) {
159
+ doc.rect(currentX, y, barWidth, options.height);
160
+ doc.fillColor(options.lineColor);
161
+ doc.fill();
162
+ currentX += barWidth * 2;
163
+ }
164
+ }
165
+ }
166
+
167
+ /**
168
+ * Get default options
169
+ * @returns {Object} Default options
170
+ */
171
+ getDefaultOptions() {
172
+ return { ...this.defaultOptions };
173
+ }
174
+
175
+ /**
176
+ * Set default options
177
+ * @param {Object} options - New default options
178
+ */
179
+ setDefaultOptions(options) {
180
+ this.defaultOptions = { ...this.defaultOptions, ...options };
181
+ }
182
+ }
183
+
184
+ module.exports = PDFRenderer;
@@ -0,0 +1,123 @@
1
+ /**
2
+ * PNG Renderer - Renders barcodes as PNG images
3
+ */
4
+
5
+ const { createCanvas } = require('canvas');
6
+ const JsBarcode = require('jsbarcode');
7
+
8
+ class PNGRenderer {
9
+ constructor() {
10
+ this.defaultOptions = {
11
+ width: 2,
12
+ height: 100,
13
+ displayValue: true,
14
+ fontSize: 20,
15
+ textAlign: 'center',
16
+ textPosition: 'bottom',
17
+ textMargin: 2,
18
+ background: '#ffffff',
19
+ lineColor: '#000000',
20
+ margin: 10,
21
+ marginTop: 10,
22
+ marginBottom: 10,
23
+ marginLeft: 10,
24
+ marginRight: 10,
25
+ };
26
+ }
27
+
28
+ /**
29
+ * Render a barcode as PNG
30
+ * @param {string} data - The data to encode
31
+ * @param {string} type - The barcode type
32
+ * @param {Object} options - Render options
33
+ * @returns {Buffer} PNG buffer
34
+ */
35
+ render(data, type, options = {}) {
36
+ try {
37
+ // Merge options with defaults
38
+ const renderOptions = { ...this.defaultOptions, ...options };
39
+
40
+ // Create canvas
41
+ const canvas = createCanvas(400, 200);
42
+ const ctx = canvas.getContext('2d');
43
+
44
+ // Set background
45
+ ctx.fillStyle = renderOptions.background;
46
+ ctx.fillRect(0, 0, canvas.width, canvas.height);
47
+
48
+ // Generate barcode using JsBarcode
49
+ JsBarcode(canvas, data, {
50
+ format: this.mapBarcodeType(type),
51
+ width: renderOptions.width,
52
+ height: renderOptions.height,
53
+ displayValue: renderOptions.displayValue,
54
+ fontSize: renderOptions.fontSize,
55
+ textAlign: renderOptions.textAlign,
56
+ textPosition: renderOptions.textPosition,
57
+ textMargin: renderOptions.textMargin,
58
+ background: renderOptions.background,
59
+ lineColor: renderOptions.lineColor,
60
+ margin: renderOptions.margin,
61
+ marginTop: renderOptions.marginTop,
62
+ marginBottom: renderOptions.marginBottom,
63
+ marginLeft: renderOptions.marginLeft,
64
+ marginRight: renderOptions.marginRight,
65
+ });
66
+
67
+ // Convert to PNG buffer
68
+ return canvas.toBuffer('image/png');
69
+ } catch (error) {
70
+ throw new Error(`PNG rendering failed: ${error.message}`);
71
+ }
72
+ }
73
+
74
+ /**
75
+ * Map barcode type to JsBarcode format
76
+ * @param {string} type - The barcode type
77
+ * @returns {string} JsBarcode format
78
+ */
79
+ mapBarcodeType(type) {
80
+ const typeMap = {
81
+ code128: 'CODE128',
82
+ code128a: 'CODE128A',
83
+ code128b: 'CODE128B',
84
+ code128c: 'CODE128C',
85
+ code128auto: 'CODE128',
86
+ code39: 'CODE39',
87
+ code39extended: 'CODE39',
88
+ code39checksum: 'CODE39',
89
+ code39auto: 'CODE39',
90
+ code93: 'CODE93',
91
+ ean13: 'EAN13',
92
+ ean8: 'EAN8',
93
+ upca: 'UPC',
94
+ upce: 'UPC',
95
+ codabar: 'codabar',
96
+ code11: 'CODE11',
97
+ msi: 'MSI',
98
+ pharmazentral: 'pharmazentral',
99
+ interleaved25: 'ITF',
100
+ standard25: 'STD25',
101
+ };
102
+
103
+ return typeMap[type] || 'CODE128';
104
+ }
105
+
106
+ /**
107
+ * Get default options
108
+ * @returns {Object} Default options
109
+ */
110
+ getDefaultOptions() {
111
+ return { ...this.defaultOptions };
112
+ }
113
+
114
+ /**
115
+ * Set default options
116
+ * @param {Object} options - New default options
117
+ */
118
+ setDefaultOptions(options) {
119
+ this.defaultOptions = { ...this.defaultOptions, ...options };
120
+ }
121
+ }
122
+
123
+ module.exports = PNGRenderer;
@@ -0,0 +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 };