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.
@@ -1,212 +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;
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;