Blame view

美国版/Food Labeling Management App UniApp/src/utils/print/protocols/escPosBuilder.ts 7.79 KB
9927b97e   “wangming”   Improve GP_R3 pri...
1
2
3
4
5
6
7
8
9
  import type {
    LabelPrintPayload,
    LabelTemplateData,
    MonochromeImageData,
    PrintImageOptions,
    StructuredLabelTemplate,
  } from '../types/printer'
  import { resolveEscTemplate } from '../templateRenderer'
  import { createTestPrintTemplate } from '../templates/testPrintTemplate'
961eecae   “wangming”   对打印机进行开发
10
  
a6f5c1af   “wangming”   开发了安卓基座
11
12
13
14
15
16
17
18
  function normalizePrinterText (str: string): string {
    return String(str || '')
      .normalize('NFKC')
      .replace(/[\u2018\u2019]/g, '\'')
      .replace(/[\u201C\u201D]/g, '"')
      .replace(/[\u2013\u2014]/g, '-')
  }
  
961eecae   “wangming”   对打印机进行开发
19
  function stringToBytes (str: string): number[] {
a6f5c1af   “wangming”   开发了安卓基座
20
    const normalized = normalizePrinterText(str)
961eecae   “wangming”   对打印机进行开发
21
    const out: number[] = []
a6f5c1af   “wangming”   开发了安卓基座
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
    const cp1252Map: Record<number, number> = {
      0x20ac: 0x80,
      0x201a: 0x82,
      0x0192: 0x83,
      0x201e: 0x84,
      0x2026: 0x85,
      0x2020: 0x86,
      0x2021: 0x87,
      0x02c6: 0x88,
      0x2030: 0x89,
      0x0160: 0x8a,
      0x2039: 0x8b,
      0x0152: 0x8c,
      0x017d: 0x8e,
      0x2018: 0x91,
      0x2019: 0x92,
      0x201c: 0x93,
      0x201d: 0x94,
      0x2022: 0x95,
      0x2013: 0x96,
      0x2014: 0x97,
      0x02dc: 0x98,
      0x2122: 0x99,
      0x0161: 0x9a,
      0x203a: 0x9b,
      0x0153: 0x9c,
      0x017e: 0x9e,
      0x0178: 0x9f,
    }
    for (let i = 0; i < normalized.length; i++) {
      const code = normalized.charCodeAt(i)
      if (code < 0x80) {
        out.push(code)
        continue
      }
      if (code >= 0xa0 && code <= 0xff) {
        out.push(code)
        continue
      }
      if (cp1252Map[code] != null) {
        out.push(cp1252Map[code])
        continue
961eecae   “wangming”   对打印机进行开发
64
      }
a6f5c1af   “wangming”   开发了安卓基座
65
      out.push(0x3f)
961eecae   “wangming”   对打印机进行开发
66
67
68
69
70
71
72
73
74
75
76
77
78
79
    }
    return out
  }
  
  function appendText (out: number[], text: string) {
    const bytes = stringToBytes(text)
    for (let i = 0; i < bytes.length; i++) out.push(bytes[i])
  }
  
  function appendLine (out: number[], text = '') {
    appendText(out, text)
    out.push(0x0a)
  }
  
9927b97e   “wangming”   Improve GP_R3 pri...
80
81
82
83
  function appendBytes (out: number[], bytes: number[]) {
    for (let i = 0; i < bytes.length; i++) out.push(bytes[i])
  }
  
961eecae   “wangming”   对打印机进行开发
84
85
86
87
88
89
90
91
92
93
94
95
96
  function appendAlign (out: number[], align: 0 | 1 | 2) {
    out.push(0x1b, 0x61, align)
  }
  
  function appendBold (out: number[], bold: boolean) {
    out.push(0x1b, 0x45, bold ? 1 : 0)
  }
  
  function appendSize (out: number[], width = 0, height = 0) {
    const value = ((width & 0x07) << 4) | (height & 0x07)
    out.push(0x1d, 0x21, value)
  }
  
9927b97e   “wangming”   Improve GP_R3 pri...
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
  function clamp (value: number, min: number, max: number): number {
    return Math.max(min, Math.min(max, Math.round(value)))
  }
  
  function normalizeEscBarcodeType (value?: string): number {
    const key = String(value || 'CODE128').trim().toUpperCase()
    const map: Record<string, number> = {
      UPCA: 65,
      UPCE: 66,
      EAN13: 67,
      EAN8: 68,
      CODE39: 69,
      ITF: 70,
      CODABAR: 71,
      CODE93: 72,
      CODE128: 73,
    }
    return map[key] || 73
  }
  
  function normalizeEscQrLevel (value?: string): number {
    const key = String(value || 'M').trim().toUpperCase()
    const map: Record<string, number> = {
      L: 48,
      M: 49,
      Q: 50,
      H: 51,
    }
    return map[key] || 49
  }
  
  function appendHorizontalRule (out: number[], width = 32) {
    appendLine(out, '+' + '-'.repeat(Math.max(width - 2, 0)) + '+')
  }
  
  function appendBoxLine (out: number[], text = '', width = 32) {
    const innerWidth = Math.max(width - 4, 0)
    const value = text.length > innerWidth ? text.slice(0, innerWidth) : text.padEnd(innerWidth, ' ')
    appendLine(out, `| ${value} |`)
  }
  
961eecae   “wangming”   对打印机进行开发
138
139
140
  function createEscDocument (builder: (out: number[]) => void): number[] {
    const out: number[] = []
    out.push(0x1b, 0x40)
a6f5c1af   “wangming”   开发了安卓基座
141
    out.push(0x1b, 0x74, 16)
961eecae   “wangming”   对打印机进行开发
142
143
144
145
146
    builder(out)
    out.push(0x1b, 0x64, 0x04)
    return out
  }
  
9927b97e   “wangming”   Improve GP_R3 pri...
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
  function appendRasterImage (out: number[], image: MonochromeImageData) {
    const bytesPerRow = image.width / 8
    const xL = bytesPerRow & 0xff
    const xH = (bytesPerRow >> 8) & 0xff
    const yL = image.height & 0xff
    const yH = (image.height >> 8) & 0xff
    out.push(0x1d, 0x76, 0x30, 0x00, xL, xH, yL, yH)
    for (let y = 0; y < image.height; y++) {
      for (let byteIndex = 0; byteIndex < bytesPerRow; byteIndex++) {
        let value = 0
        for (let bit = 0; bit < 8; bit++) {
          const x = byteIndex * 8 + bit
          const pixel = image.pixels[y * image.width + x]
          if (pixel) value |= 1 << (7 - bit)
        }
        out.push(value & 0xff)
      }
    }
  }
  
  function appendBarcode (out: number[], item: {
    value: string
    align?: 0 | 1 | 2
    symbology?: string
    height?: number
    width?: number
    showText?: boolean
  }) {
    const value = String(item.value || '')
    if (!value) return
    appendAlign(out, item.align ?? 1)
    out.push(0x1d, 0x48, item.showText === false ? 0 : 2)
    out.push(0x1d, 0x68, clamp(item.height || 96, 1, 255))
    out.push(0x1d, 0x77, clamp(item.width || 3, 2, 6))
  
    const type = normalizeEscBarcodeType(item.symbology)
    const bytes = stringToBytes(value)
  
    if (type >= 73) {
      out.push(0x1d, 0x6b, type, clamp(bytes.length, 0, 255))
      appendBytes(out, bytes)
    } else {
      out.push(0x1d, 0x6b, type)
      appendBytes(out, bytes)
      out.push(0x00)
    }
    out.push(0x0a)
  }
  
  function appendQrCode (out: number[], item: {
    value: string
    align?: 0 | 1 | 2
    size?: number
    level?: 'L' | 'M' | 'Q' | 'H'
  }) {
    const value = String(item.value || '')
    if (!value) return
    const bytes = stringToBytes(value)
    const storeLength = bytes.length + 3
    const pL = storeLength & 0xff
    const pH = (storeLength >> 8) & 0xff
  
    appendAlign(out, item.align ?? 1)
    out.push(0x1d, 0x28, 0x6b, 0x04, 0x00, 0x31, 0x41, 0x32, 0x00)
    out.push(0x1d, 0x28, 0x6b, 0x03, 0x00, 0x31, 0x43, clamp(item.size || 5, 1, 16))
    out.push(0x1d, 0x28, 0x6b, 0x03, 0x00, 0x31, 0x45, normalizeEscQrLevel(item.level))
    out.push(0x1d, 0x28, 0x6b, pL, pH, 0x31, 0x50, 0x30)
    appendBytes(out, bytes)
    out.push(0x1d, 0x28, 0x6b, 0x03, 0x00, 0x31, 0x51, 0x30)
    out.push(0x0a)
  }
  
961eecae   “wangming”   对打印机进行开发
219
  export function buildEscPosTestPrintData (): number[] {
9927b97e   “wangming”   Improve GP_R3 pri...
220
    return buildEscPosTemplateData(createTestPrintTemplate())
961eecae   “wangming”   对打印机进行开发
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
  }
  
  export function buildEscPosLabelData (payload: LabelPrintPayload): number[] {
    const {
      productName,
      labelId,
      printQty = 1,
      category = '',
      extraLine = '',
    } = payload
  
    return createEscDocument((out) => {
      appendAlign(out, 1)
      appendBold(out, true)
      appendSize(out, 1, 1)
      appendLine(out, 'FOOD LABEL')
      appendBold(out, false)
      appendSize(out, 0, 0)
      appendLine(out, '-----------------------------')
      appendLine(out, 'Product: ' + productName)
      if (category) appendLine(out, 'Category: ' + category)
      appendLine(out, 'Label ID: ' + labelId)
      if (extraLine) appendLine(out, extraLine)
      appendLine(out, 'Qty: ' + String(printQty))
      appendLine(out, '-----------------------------')
    })
  }
9927b97e   “wangming”   Improve GP_R3 pri...
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
  
  export function buildEscPosImageData (
    image: MonochromeImageData,
    options: PrintImageOptions = {}
  ): number[] {
    const printQty = Math.max(1, Math.round(options.printQty || 1))
    return createEscDocument((out) => {
      for (let i = 0; i < printQty; i++) {
        appendAlign(out, 1)
        appendRasterImage(out, image)
        appendLine(out)
        appendLine(out)
      }
    })
  }
  
  export function buildEscPosTemplateData (
    template: StructuredLabelTemplate,
    data: LabelTemplateData = {}
  ): number[] {
    const resolved = resolveEscTemplate(template, data)
    const printQty = Math.max(1, Math.round(resolved.printQty || 1))
    const feedLines = Math.max(1, Math.round(resolved.feedLines || 4))
  
    return createEscDocument((out) => {
      for (let i = 0; i < printQty; i++) {
        resolved.items.forEach((item) => {
          if (item.type === 'rule') {
            appendHorizontalRule(out, item.width || 32)
            return
          }
          if (item.type === 'qrcode') {
            appendQrCode(out, item)
            return
          }
          if (item.type === 'barcode') {
            appendBarcode(out, item)
            return
          }
          appendAlign(out, item.align ?? 0)
          appendBold(out, !!item.bold)
          appendSize(out, item.widthScale || 0, item.heightScale || 0)
          appendLine(out, item.text)
          appendBold(out, false)
          appendSize(out, 0, 0)
        })
        out.push(0x1b, 0x64, feedLines)
      }
    })
  }