@operato/scene-label 8.0.0-beta.1 → 8.0.0-beta.2
Sign up to get free protection for your applications and to get access to all the features.
- package/package.json +2 -2
- package/CHANGELOG.md +0 -707
- package/schema.graphql +0 -3978
- package/src/barcode-scanner.ts +0 -127
- package/src/barcode.ts +0 -328
- package/src/bitmap.ts +0 -163
- package/src/index.ts +0 -5
- package/src/label-printer.ts +0 -141
- package/src/templates/barcode-scanner.ts +0 -17
- package/src/templates/barcode.ts +0 -18
- package/src/templates/index.ts +0 -5
- package/src/templates/label-printer.ts +0 -18
- package/tsconfig.json +0 -24
- package/tsconfig.tsbuildinfo +0 -1
package/src/barcode-scanner.ts
DELETED
@@ -1,127 +0,0 @@
|
|
1
|
-
/*
|
2
|
-
* Copyright © HatioLab Inc. All rights reserved.
|
3
|
-
*/
|
4
|
-
|
5
|
-
import { Component, ComponentNature, HTMLOverlayElement } from '@hatiolab/things-scene'
|
6
|
-
import { OxInputBarcode } from '@operato/input' // TODO make '@operato/input/ox-input-barcode.js' work.
|
7
|
-
|
8
|
-
const NATURE: ComponentNature = {
|
9
|
-
mutable: false,
|
10
|
-
resizable: true,
|
11
|
-
rotatable: true,
|
12
|
-
properties: [
|
13
|
-
{
|
14
|
-
type: 'checkbox',
|
15
|
-
label: 'without-enter',
|
16
|
-
name: 'withoutEnter'
|
17
|
-
},
|
18
|
-
{
|
19
|
-
type: 'checkbox',
|
20
|
-
label: 'english-only',
|
21
|
-
name: 'englishOnly'
|
22
|
-
},
|
23
|
-
{
|
24
|
-
type: 'checkbox',
|
25
|
-
label: 'select-after-change',
|
26
|
-
name: 'selectAfterChange'
|
27
|
-
},
|
28
|
-
{
|
29
|
-
type: 'checkbox',
|
30
|
-
label: 'autofocus',
|
31
|
-
name: 'autofocus'
|
32
|
-
},
|
33
|
-
{
|
34
|
-
type: 'checkbox',
|
35
|
-
label: 'hide-keyboard',
|
36
|
-
name: 'hideKeyboard'
|
37
|
-
}
|
38
|
-
],
|
39
|
-
help: 'scene/component/barcode-scanner'
|
40
|
-
}
|
41
|
-
|
42
|
-
export default class BarcodeScanner extends HTMLOverlayElement {
|
43
|
-
private _data: any
|
44
|
-
|
45
|
-
get nature() {
|
46
|
-
return NATURE
|
47
|
-
}
|
48
|
-
|
49
|
-
get data() {
|
50
|
-
return this._data
|
51
|
-
}
|
52
|
-
|
53
|
-
set data(data) {
|
54
|
-
this._data = data
|
55
|
-
this.executeMappings() // 이전 데이터와 비교하지 않고 매핑을 실행하기 위함
|
56
|
-
}
|
57
|
-
|
58
|
-
select() {
|
59
|
-
;(this.element as OxInputBarcode).input.select()
|
60
|
-
}
|
61
|
-
|
62
|
-
dispose() {
|
63
|
-
super.dispose()
|
64
|
-
}
|
65
|
-
|
66
|
-
ready() {
|
67
|
-
super.ready()
|
68
|
-
var scanInput = this.element as OxInputBarcode
|
69
|
-
|
70
|
-
scanInput.addEventListener('change', (e: Event) => {
|
71
|
-
this.data = scanInput.value
|
72
|
-
})
|
73
|
-
|
74
|
-
// 스캔 시 컴포넌트 데이터 세팅
|
75
|
-
var scan = scanInput.scan
|
76
|
-
scanInput.scan = (e: MouseEvent) =>
|
77
|
-
scan.call(scanInput, e).then(() => {
|
78
|
-
if (scanInput.input) this.data = scanInput.input.value
|
79
|
-
})
|
80
|
-
}
|
81
|
-
|
82
|
-
createElement() {
|
83
|
-
super.createElement()
|
84
|
-
|
85
|
-
if (this.app.isViewMode) {
|
86
|
-
const { hideKeyboard, autofocus } = this.state
|
87
|
-
|
88
|
-
if (hideKeyboard) {
|
89
|
-
requestAnimationFrame(() => {
|
90
|
-
const input = (this.element as OxInputBarcode).input
|
91
|
-
input.addEventListener('focusin', e => {
|
92
|
-
input.setAttribute('readonly', '')
|
93
|
-
this.select()
|
94
|
-
|
95
|
-
requestAnimationFrame(() => {
|
96
|
-
input.removeAttribute('readonly')
|
97
|
-
})
|
98
|
-
})
|
99
|
-
})
|
100
|
-
}
|
101
|
-
|
102
|
-
if (autofocus) {
|
103
|
-
requestAnimationFrame(() => {
|
104
|
-
this.select()
|
105
|
-
})
|
106
|
-
}
|
107
|
-
}
|
108
|
-
}
|
109
|
-
|
110
|
-
setElementProperties(input: OxInputBarcode) {
|
111
|
-
const { withoutEnter = false, englishOnly = false, selectAfterChange = false } = this.state
|
112
|
-
|
113
|
-
input.style.setProperty('--input-font', 'initial')
|
114
|
-
|
115
|
-
input.withoutEnter = withoutEnter
|
116
|
-
input.englishOnly = englishOnly
|
117
|
-
input.selectAfterChange = selectAfterChange
|
118
|
-
|
119
|
-
input.value = this._data = this.text
|
120
|
-
}
|
121
|
-
|
122
|
-
get tagName() {
|
123
|
-
return 'ox-input-barcode'
|
124
|
-
}
|
125
|
-
}
|
126
|
-
|
127
|
-
Component.register('barcode-scanner', BarcodeScanner)
|
package/src/barcode.ts
DELETED
@@ -1,328 +0,0 @@
|
|
1
|
-
/*
|
2
|
-
* Copyright © HatioLab Inc. All rights reserved.
|
3
|
-
*/
|
4
|
-
|
5
|
-
// 참고 웹페이지.
|
6
|
-
// http://www.neodynamic.com/Products/Help/BarcodeWinControl2.5/working_barcode_symbologies.htm
|
7
|
-
// const BARCODE_REGEXP = {
|
8
|
-
// 'code11': /^[0-9\-]*$/,
|
9
|
-
// 'codebar': /^[A-D][0-9\+$:\-/.]*[A-D]$/,
|
10
|
-
// 'code39': {
|
11
|
-
// 'normal': /^[0-9A-Z\-.$/\+%\*\s]*$/,
|
12
|
-
// 'extended': /^[\000-\177]*$/
|
13
|
-
// },
|
14
|
-
// 'code93': {
|
15
|
-
// 'normal': /^[0-9A-Z\-.$/\+%\*\s]*$/,
|
16
|
-
// 'extended': /^[\000-\177]*$/
|
17
|
-
// },
|
18
|
-
// 'code128': {
|
19
|
-
// 'auto': /^[\000-\177]*$/,
|
20
|
-
// 'A': /^[\000-\137]*$/,
|
21
|
-
// 'B': /^[\040-\177]*$/,
|
22
|
-
// 'C': /^(([0-9]{2})+?)*$/
|
23
|
-
// },
|
24
|
-
// 'datamatrix': /^[\x00-\xff]*$/, // 멀티바이트 캐릭터는 안됨 ?
|
25
|
-
// 'ean8': /^\d{1,}$/,
|
26
|
-
// 'ean13': /^\d{1,}$/,
|
27
|
-
// 'industrial2of5': /^\d{1,}$/,
|
28
|
-
// 'interleaved2of5': /^\d{1,}$/,
|
29
|
-
// 'isbn': /((978[\--– ])?[0-9][0-9\--– ]{10}[\--– ][0-9xX])|((978)?[0-9]{9}[0-9Xx])/,
|
30
|
-
// 'msi': /^\d{1,}$/,
|
31
|
-
// 'pdf417': {
|
32
|
-
// 'text-compaction': /^[\011\012\015\040-\177]*$/,
|
33
|
-
// 'binary-compaction': /^[\x00-\xff]*$/
|
34
|
-
// },
|
35
|
-
// 'planet': /^\d{1,}$/,
|
36
|
-
// 'postnet': /^\d{1,}$/,
|
37
|
-
// 'ean128': /^[\000-\177\xC8\xCA-\xCD]*$/,
|
38
|
-
// 'upca': /^\d{1,}$/,
|
39
|
-
// 'upce': /^\d{1,}$/
|
40
|
-
// };
|
41
|
-
|
42
|
-
/* opts 예제 - https://github.com/bwipp/postscriptbarcode/wiki/Options-Reference
|
43
|
-
* includecheck, includecheckintext, includetext, textfont, textsize, textgaps, textxalign, textyalign, textxoffset, textyoffset
|
44
|
-
* showborder, borderwidth, borderleft, borderright, bordertop, borderbottom, barcolor, backgroundcolor, bordercolor, textcolor
|
45
|
-
* parse, parsefnc, height, width, inkspread, inkspreadh, inkspreadv,
|
46
|
-
* addontextxoffset, addontextyoffset, addontextfont, addontextsize
|
47
|
-
* guardwhitespace, guardwidth, guardheight, guardleftpos, guardrightpos, guardleftypos, guardrightypos
|
48
|
-
*/
|
49
|
-
|
50
|
-
// symdesc["code39"].opts = "includetext textxalign=center textgaps=2";
|
51
|
-
// symdesc["interleaved2of5"].opts = "includetext textxalign=center textgaps=1.5";
|
52
|
-
// symdesc["code93"].opts = "includetext textxalign=center textgaps=2";
|
53
|
-
// symdesc["ean13"].opts = "includetext";
|
54
|
-
// symdesc["ean8"].opts = "includetext";
|
55
|
-
|
56
|
-
import bwipjs from 'bwip-js'
|
57
|
-
|
58
|
-
import { Component, ComponentNature, error, Properties, RectPath, Shape } from '@hatiolab/things-scene'
|
59
|
-
|
60
|
-
const NATURE: ComponentNature = {
|
61
|
-
mutable: false,
|
62
|
-
resizable: true,
|
63
|
-
rotatable: true,
|
64
|
-
properties: [
|
65
|
-
{
|
66
|
-
type: 'select',
|
67
|
-
label: 'symbol',
|
68
|
-
name: 'symbol',
|
69
|
-
property: {
|
70
|
-
options: [
|
71
|
-
{ value: 'auspost', display: 'AusPost 4 State Customer Code' },
|
72
|
-
{ value: 'azteccode', display: 'Aztec Code' },
|
73
|
-
{ value: 'azteccodecompact', display: 'Compact Aztec Code' },
|
74
|
-
{ value: 'aztecrune', display: 'Aztec Runes' },
|
75
|
-
{ value: 'bc412', display: 'BC412' },
|
76
|
-
{ value: 'channelcode', display: 'Channel Code' },
|
77
|
-
{ value: 'codablockf', display: 'Codablock F' },
|
78
|
-
{ value: 'code11', display: 'Code 11' },
|
79
|
-
{ value: 'code128', display: 'Code 128' },
|
80
|
-
{ value: 'code16k', display: 'Code 16K' },
|
81
|
-
{ value: 'code2of5', display: 'Code 25' },
|
82
|
-
{ value: 'code32', display: 'Italian Pharmacode' },
|
83
|
-
{ value: 'code39', display: 'Code 39' },
|
84
|
-
{ value: 'code39ext', display: 'Code 39 Extended' },
|
85
|
-
{ value: 'code49', display: 'Code 49' },
|
86
|
-
{ value: 'code93', display: 'Code 93' },
|
87
|
-
{ value: 'code93ext', display: 'Code 93 Extended' },
|
88
|
-
{ value: 'codeone', display: 'Code One' },
|
89
|
-
{ value: 'coop2of5', display: 'COOP 2 of 5' },
|
90
|
-
{ value: 'daft', display: 'Custom 4 state symbology' },
|
91
|
-
{ value: 'databarexpanded', display: 'GS1 DataBar Expanded' },
|
92
|
-
{
|
93
|
-
value: 'databarexpandedcomposite',
|
94
|
-
display: 'GS1 DataBar Expanded Composite'
|
95
|
-
},
|
96
|
-
{
|
97
|
-
value: 'databarexpandedstacked',
|
98
|
-
display: 'GS1 DataBar Expanded Stacked'
|
99
|
-
},
|
100
|
-
{
|
101
|
-
value: 'databarexpandedstackedcomposite',
|
102
|
-
display: 'GS1 DataBar Expanded Stacked Composite'
|
103
|
-
},
|
104
|
-
{ value: 'databarlimited', display: 'GS1 DataBar Limited' },
|
105
|
-
{
|
106
|
-
value: 'databarlimitedcomposite',
|
107
|
-
display: 'GS1 DataBar Limited Composite'
|
108
|
-
},
|
109
|
-
{ value: 'databaromni', display: 'GS1 DataBar Omnidirectional' },
|
110
|
-
{
|
111
|
-
value: 'databaromnicomposite',
|
112
|
-
display: 'GS1 DataBar Omnidirectional Composite'
|
113
|
-
},
|
114
|
-
{ value: 'databarstacked', display: 'GS1 DataBar Stacked' },
|
115
|
-
{
|
116
|
-
value: 'databarstackedcomposite',
|
117
|
-
display: 'GS1 DataBar Stacked Composite'
|
118
|
-
},
|
119
|
-
{
|
120
|
-
value: 'databarstackedomni',
|
121
|
-
display: 'GS1 DataBar Stacked Omnidirectional'
|
122
|
-
},
|
123
|
-
{
|
124
|
-
value: 'databarstackedomnicomposite',
|
125
|
-
display: 'GS1 DataBar Stacked Omnidirectional Composite'
|
126
|
-
},
|
127
|
-
{ value: 'databartruncated', display: 'GS1 DataBar Truncated' },
|
128
|
-
{
|
129
|
-
value: 'databartruncatedcomposite',
|
130
|
-
display: 'GS1 DataBar Truncated Composite'
|
131
|
-
},
|
132
|
-
{ value: 'datalogic2of5', display: 'Datalogic 2 of 5' },
|
133
|
-
{ value: 'datamatrix', display: 'Data Matrix' },
|
134
|
-
{
|
135
|
-
value: 'datamatrixrectangular',
|
136
|
-
display: 'Data Matrix Rectangular'
|
137
|
-
},
|
138
|
-
{ value: 'dotcode', display: 'DotCode' },
|
139
|
-
{ value: 'ean13', display: 'EAN-13' },
|
140
|
-
{ value: 'ean13composite', display: 'EAN-13 Composite' },
|
141
|
-
{ value: 'ean14', display: 'GS1-14' },
|
142
|
-
{ value: 'ean2', display: 'EAN-2 (2 digit addon)' },
|
143
|
-
{ value: 'ean5', display: 'EAN-5 (5 digit addon)' },
|
144
|
-
{ value: 'ean8', display: 'EAN-8' },
|
145
|
-
{ value: 'ean8composite', display: 'EAN-8 Composite' },
|
146
|
-
{ value: 'flattermarken', display: 'Flattermarken' },
|
147
|
-
{ value: 'gs1-128', display: 'GS1-128' },
|
148
|
-
{ value: 'gs1-128composite', display: 'GS1-128 Composite' },
|
149
|
-
{ value: 'gs1-cc', display: 'GS1 Composite 2D Component' },
|
150
|
-
{ value: 'gs1datamatrix', display: 'GS1 Data Matrix' },
|
151
|
-
{
|
152
|
-
value: 'gs1datamatrixrectangular',
|
153
|
-
display: 'GS1 Data Matrix Rectangular'
|
154
|
-
},
|
155
|
-
{
|
156
|
-
value: 'gs1northamericancoupon',
|
157
|
-
display: 'GS1 North American Coupon'
|
158
|
-
},
|
159
|
-
{ value: 'gs1qrcode', display: 'GS1 QR Code' },
|
160
|
-
{ value: 'hanxin', display: 'Han Xin Code' },
|
161
|
-
{ value: 'hibcazteccode', display: 'HIBC Aztec Code' },
|
162
|
-
{ value: 'hibccodablockf', display: 'HIBC Codablock F' },
|
163
|
-
{ value: 'hibccode128', display: 'HIBC Code 128' },
|
164
|
-
{ value: 'hibccode39', display: 'HIBC Code 39' },
|
165
|
-
{ value: 'hibcdatamatrix', display: 'HIBC Data Matrix' },
|
166
|
-
{
|
167
|
-
value: 'hibcdatamatrixrectangular',
|
168
|
-
display: 'HIBC Data Matrix Rectangular'
|
169
|
-
},
|
170
|
-
{ value: 'hibcmicropdf417', display: 'HIBC MicroPDF417' },
|
171
|
-
{ value: 'hibcpdf417', display: 'HIBC PDF417' },
|
172
|
-
{ value: 'hibcqrcode', display: 'HIBC QR Code' },
|
173
|
-
{ value: 'iata2of5', display: 'IATA 2 of 5' },
|
174
|
-
{ value: 'identcode', display: 'Deutsche Post Identcode' },
|
175
|
-
{ value: 'industrial2of5', display: 'Industrial 2 of 5' },
|
176
|
-
{ value: 'interleaved2of5', display: 'Interleaved 2 of 5 (ITF)' },
|
177
|
-
{ value: 'isbn', display: 'ISBN' },
|
178
|
-
{ value: 'ismn', display: 'ISMN' },
|
179
|
-
{ value: 'issn', display: 'ISSN' },
|
180
|
-
{ value: 'itf14', display: 'ITF-14' },
|
181
|
-
{ value: 'japanpost', display: 'Japan Post 4 State Customer Code' },
|
182
|
-
{ value: 'kix', display: 'Royal Dutch TPG Post KIX' },
|
183
|
-
{ value: 'leitcode', display: 'Deutsche Post Leitcode' },
|
184
|
-
{ value: 'matrix2of5', display: 'Matrix 2 of 5' },
|
185
|
-
{ value: 'maxicode', display: 'MaxiCode' },
|
186
|
-
{ value: 'micropdf417', display: 'MicroPDF417' },
|
187
|
-
{ value: 'microqrcode', display: 'Micro QR Code' },
|
188
|
-
{ value: 'msi', display: 'MSI Modified Plessey' },
|
189
|
-
{ value: 'onecode', display: 'USPS Intelligent Mail' },
|
190
|
-
{ value: 'pdf417', display: 'PDF417' },
|
191
|
-
{ value: 'pdf417compact', display: 'Compact PDF417' },
|
192
|
-
{ value: 'pharmacode', display: 'Pharmaceutical Binary Code' },
|
193
|
-
{ value: 'pharmacode2', display: 'Two-track Pharmacode' },
|
194
|
-
{ value: 'planet', display: 'USPS PLANET' },
|
195
|
-
{ value: 'plessey', display: 'Plessey UK' },
|
196
|
-
{ value: 'posicode', display: 'PosiCode' },
|
197
|
-
{ value: 'postnet', display: 'USPS POSTNET' },
|
198
|
-
{ value: 'pzn', display: 'Pharmazentralnummer (PZN)' },
|
199
|
-
{ value: 'qrcode', display: 'QR Code' },
|
200
|
-
{ value: 'rationalizedCodabar', display: 'Codabar' },
|
201
|
-
{ value: 'raw', display: 'Custom 1D symbology' },
|
202
|
-
{ value: 'royalmail', display: 'Royal Mail 4 State Customer Code' },
|
203
|
-
{ value: 'sscc18', display: 'SSCC-18' },
|
204
|
-
{ value: 'symbol', display: 'Miscellaneous symbols' },
|
205
|
-
{ value: 'telepen', display: 'Telepen' },
|
206
|
-
{ value: 'telepennumeric', display: 'Telepen Numeric' },
|
207
|
-
{ value: 'ultracode', display: 'Ultracode' },
|
208
|
-
{ value: 'upca', display: 'UPC-A' },
|
209
|
-
{ value: 'upcacomposite', display: 'UPC-A Composite' },
|
210
|
-
{ value: 'upce', display: 'UPC-E' },
|
211
|
-
{ value: 'upcecomposite', display: 'UPC-E Composite' }
|
212
|
-
]
|
213
|
-
}
|
214
|
-
},
|
215
|
-
{
|
216
|
-
type: 'number',
|
217
|
-
label: 'paddingwidth',
|
218
|
-
name: 'paddingwidth'
|
219
|
-
},
|
220
|
-
{
|
221
|
-
type: 'number',
|
222
|
-
label: 'paddingheight',
|
223
|
-
name: 'paddingheight'
|
224
|
-
},
|
225
|
-
{
|
226
|
-
type: 'checkbox',
|
227
|
-
label: 'monochrome',
|
228
|
-
name: 'monochrome'
|
229
|
-
},
|
230
|
-
{
|
231
|
-
type: 'checkbox',
|
232
|
-
label: 'show-text',
|
233
|
-
name: 'showText'
|
234
|
-
}
|
235
|
-
],
|
236
|
-
'value-property': 'text'
|
237
|
-
}
|
238
|
-
|
239
|
-
const REDRAW_PROPS = ['symbol', 'text', 'showText', 'height', 'width', 'paddingwidth', 'paddingheight']
|
240
|
-
|
241
|
-
export default class Barcode extends RectPath(Shape) {
|
242
|
-
private _canvas?: HTMLCanvasElement
|
243
|
-
private _image: any
|
244
|
-
|
245
|
-
get nature() {
|
246
|
-
return NATURE
|
247
|
-
}
|
248
|
-
|
249
|
-
get canvas() {
|
250
|
-
if (!this._canvas) {
|
251
|
-
this._canvas = document.createElement('canvas')
|
252
|
-
|
253
|
-
this._canvas.style.display = 'none'
|
254
|
-
this._canvas.height = 1
|
255
|
-
this._canvas.width = 1
|
256
|
-
}
|
257
|
-
|
258
|
-
return this._canvas
|
259
|
-
}
|
260
|
-
|
261
|
-
dispose() {
|
262
|
-
super.dispose()
|
263
|
-
}
|
264
|
-
|
265
|
-
ready() {
|
266
|
-
super.ready()
|
267
|
-
this.buildImage()
|
268
|
-
}
|
269
|
-
|
270
|
-
buildImage() {
|
271
|
-
var { symbol, text, width, height, includetext, textAlign, paddingwidth = 0, paddingheight = 0 } = this.state
|
272
|
-
|
273
|
-
if (!text) {
|
274
|
-
console.warn('barcode text is not specified.')
|
275
|
-
return
|
276
|
-
}
|
277
|
-
|
278
|
-
this.canvas.width = width
|
279
|
-
this.canvas.height = height
|
280
|
-
|
281
|
-
try {
|
282
|
-
//@ts-ignore
|
283
|
-
this._image = bwipjs.toCanvas(this.canvas, {
|
284
|
-
bcid: symbol, // Barcode type
|
285
|
-
text, // Text to encode
|
286
|
-
height, // Bar height, in millimeters
|
287
|
-
width,
|
288
|
-
includetext, // Show human-readable text
|
289
|
-
textxalign: textAlign || 'center', // Always good to set this
|
290
|
-
paddingwidth,
|
291
|
-
paddingheight
|
292
|
-
})
|
293
|
-
} catch (err) {
|
294
|
-
delete this._image
|
295
|
-
error(err)
|
296
|
-
} finally {
|
297
|
-
this.invalidate()
|
298
|
-
}
|
299
|
-
}
|
300
|
-
|
301
|
-
render(context: CanvasRenderingContext2D) {
|
302
|
-
var { left, top, width, height } = this.state
|
303
|
-
|
304
|
-
try {
|
305
|
-
if (this._image) {
|
306
|
-
// don't change to this.drawImage(context, ..) - this._image is canvas.
|
307
|
-
context.drawImage(this._image, left, top, width, height)
|
308
|
-
}
|
309
|
-
} catch (e) {
|
310
|
-
error(e)
|
311
|
-
}
|
312
|
-
}
|
313
|
-
|
314
|
-
drawText() {
|
315
|
-
// to nothing
|
316
|
-
}
|
317
|
-
|
318
|
-
onchange(props: Properties) {
|
319
|
-
for (let prop of REDRAW_PROPS) {
|
320
|
-
if (prop in props) {
|
321
|
-
this.buildImage()
|
322
|
-
return
|
323
|
-
}
|
324
|
-
}
|
325
|
-
}
|
326
|
-
}
|
327
|
-
|
328
|
-
Component.register('barcode', Barcode)
|
package/src/bitmap.ts
DELETED
@@ -1,163 +0,0 @@
|
|
1
|
-
/*
|
2
|
-
* Copyright © HatioLab Inc. All rights reserved.
|
3
|
-
*/
|
4
|
-
// file : bwip-js/lib/canvas.js
|
5
|
-
//
|
6
|
-
// bwip-js bitmap interface for an HTML canvas.
|
7
|
-
//
|
8
|
-
// Copyright (c) 2011-2016 Mark Warren
|
9
|
-
//
|
10
|
-
// Licensed MIT. See the LICENSE file in the bwip-js root directory
|
11
|
-
// for the extended copyright notice.
|
12
|
-
|
13
|
-
// bgcolor is optional, defaults to #fff.
|
14
|
-
export default class Bitmap {
|
15
|
-
bgcolor?: string
|
16
|
-
clr = [0, 0, 0]
|
17
|
-
pts = [] as any[]
|
18
|
-
minx = 0 // min-x
|
19
|
-
miny = 0 // min-y
|
20
|
-
maxx = 0 // max-x
|
21
|
-
maxy = 0 // max-y
|
22
|
-
padx = 0 // padding-x
|
23
|
-
pady = 0 // padding-y
|
24
|
-
|
25
|
-
constructor(bgcolor: string) {
|
26
|
-
this.bgcolor = bgcolor
|
27
|
-
}
|
28
|
-
|
29
|
-
pad(x: number, y: number) {
|
30
|
-
this.padx = x
|
31
|
-
this.pady = y
|
32
|
-
}
|
33
|
-
|
34
|
-
color(r: number, g: number, b: number) {
|
35
|
-
this.clr = [r, g, b]
|
36
|
-
}
|
37
|
-
|
38
|
-
// Sets the minimim size for the drawing surface (can grow larger).
|
39
|
-
// BWIPP has logic for borders (padding) that without this custom call
|
40
|
-
// gets lost. See custom/ren*.ps.
|
41
|
-
extent(llx: number, lly: number, urx: number, ury: number) {
|
42
|
-
llx = Math.floor(llx)
|
43
|
-
lly = Math.floor(lly)
|
44
|
-
urx = Math.floor(urx)
|
45
|
-
ury = Math.floor(ury)
|
46
|
-
if (this.minx > llx) this.minx = llx
|
47
|
-
if (this.miny > lly) this.miny = lly
|
48
|
-
if (this.maxx < urx) this.maxx = urx
|
49
|
-
if (this.maxy < ury) this.maxy = ury
|
50
|
-
}
|
51
|
-
|
52
|
-
// a is the alpha-level of the pixel [0 .. 255]
|
53
|
-
set(x: number, y: number, a: number) {
|
54
|
-
x = Math.floor(x)
|
55
|
-
y = Math.floor(y)
|
56
|
-
this.pts.push([x, y, this.clr, a])
|
57
|
-
if (this.minx > x) this.minx = x
|
58
|
-
if (this.miny > y) this.miny = y
|
59
|
-
if (this.maxx < x) this.maxx = x
|
60
|
-
if (this.maxy < y) this.maxy = y
|
61
|
-
}
|
62
|
-
|
63
|
-
error(cvs: HTMLCanvasElement) {
|
64
|
-
cvs.width = 64
|
65
|
-
cvs.height = 64
|
66
|
-
|
67
|
-
var ctx = cvs.getContext('2d') as CanvasRenderingContext2D
|
68
|
-
|
69
|
-
ctx.beginPath()
|
70
|
-
|
71
|
-
ctx.rect(0, 0, cvs.width, cvs.height)
|
72
|
-
ctx.moveTo(0, 0)
|
73
|
-
ctx.lineTo(cvs.width, cvs.height)
|
74
|
-
ctx.moveTo(cvs.width, 0)
|
75
|
-
ctx.lineTo(0, cvs.height)
|
76
|
-
|
77
|
-
ctx.lineWidth = 5
|
78
|
-
ctx.strokeStyle = '#ff0000'
|
79
|
-
ctx.stroke()
|
80
|
-
|
81
|
-
return cvs.toDataURL()
|
82
|
-
}
|
83
|
-
|
84
|
-
show(cvsid: string | HTMLCanvasElement, rot: 'R' | 'L' | 'N' | 'I') {
|
85
|
-
var cvs = cvsid instanceof HTMLCanvasElement ? cvsid : (document.getElementById(cvsid) as HTMLCanvasElement)
|
86
|
-
|
87
|
-
if (this.pts.length == 0) {
|
88
|
-
cvs.width = 32
|
89
|
-
cvs.height = 32
|
90
|
-
cvs.getContext('2d')?.clearRect(0, 0, cvs.width, cvs.height)
|
91
|
-
cvs.style.visibility = 'visible'
|
92
|
-
return
|
93
|
-
}
|
94
|
-
|
95
|
-
if (rot == 'R' || rot == 'L') {
|
96
|
-
var h = this.maxx - this.minx + 1
|
97
|
-
var w = this.maxy - this.miny + 1
|
98
|
-
} else {
|
99
|
-
var w = this.maxx - this.minx + 1
|
100
|
-
var h = this.maxy - this.miny + 1
|
101
|
-
}
|
102
|
-
|
103
|
-
cvs.width = w + 2 * this.padx
|
104
|
-
cvs.height = h + 2 * this.pady
|
105
|
-
|
106
|
-
// Convert from cmyk?
|
107
|
-
if (this.bgcolor && this.bgcolor.length == 8) {
|
108
|
-
let c = parseInt(this.bgcolor.substr(0, 2), 16) / 255
|
109
|
-
let m = parseInt(this.bgcolor.substr(2, 2), 16) / 255
|
110
|
-
let y = parseInt(this.bgcolor.substr(4, 2), 16) / 255
|
111
|
-
let k = parseInt(this.bgcolor.substr(6, 2), 16) / 255
|
112
|
-
let r = Math.floor((1 - c) * (1 - k) * 255)
|
113
|
-
let g = Math.floor((1 - m) * (1 - k) * 255)
|
114
|
-
let b = Math.floor((1 - y) * (1 - k) * 255)
|
115
|
-
this.bgcolor = 'rgb(' + r + ',' + g + ',' + b + ')'
|
116
|
-
} else if (this.bgcolor) {
|
117
|
-
this.bgcolor = '#' + this.bgcolor
|
118
|
-
}
|
119
|
-
|
120
|
-
var ctx = cvs.getContext('2d') as CanvasRenderingContext2D
|
121
|
-
ctx.fillStyle = this.bgcolor || '#fff'
|
122
|
-
ctx.fillRect(0, 0, cvs.width, cvs.height)
|
123
|
-
ctx.fillStyle = '#000'
|
124
|
-
|
125
|
-
var id = ctx.getImageData(0, 0, cvs.width, cvs.height)
|
126
|
-
var dat = id.data
|
127
|
-
|
128
|
-
for (let i = 0; i < this.pts.length; i++) {
|
129
|
-
// PostScript builds bottom-up, we build top-down.
|
130
|
-
let x = this.pts[i][0] - this.minx
|
131
|
-
let y = this.pts[i][1] - this.miny
|
132
|
-
let c = this.pts[i][2] as number[]
|
133
|
-
let a = this.pts[i][3] / 255
|
134
|
-
|
135
|
-
if (rot == 'N') {
|
136
|
-
y = h - y - 1 // Invert y
|
137
|
-
} else if (rot == 'I') {
|
138
|
-
x = w - x - 1 // Invert x
|
139
|
-
} else {
|
140
|
-
y = w - y // Invert y
|
141
|
-
if (rot == 'L') {
|
142
|
-
var t = y
|
143
|
-
y = h - x - 1
|
144
|
-
x = t - 1
|
145
|
-
} else {
|
146
|
-
var t = x
|
147
|
-
x = w - y
|
148
|
-
y = t
|
149
|
-
}
|
150
|
-
}
|
151
|
-
|
152
|
-
var idx = (y * id.width + x) * 4
|
153
|
-
dat[idx + 0] = (dat[idx + 0] * (1 - a) + c[0] * a) | 0
|
154
|
-
dat[idx + 1] = (dat[idx + 1] * (1 - a) + c[1] * a) | 0
|
155
|
-
dat[idx + 2] = (dat[idx + 2] * (1 - a) + c[2] * a) | 0
|
156
|
-
dat[idx + 3] = 255
|
157
|
-
}
|
158
|
-
|
159
|
-
ctx.putImageData(id, this.padx, this.pady)
|
160
|
-
|
161
|
-
return cvs.toDataURL()
|
162
|
-
}
|
163
|
-
}
|