king
2022-12-09 cb9ade2afd2a367ad767bc605ab7086c695dd010
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
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
138
139
140
141
142
143
144
145
146
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
219
220
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
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
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
export default class Printctrl {
  /**
   * @description 绘制模板至缓存
   * @param  {Object}    configs      配置信息
   * @param  {Boolean}   selectId     编辑元素
   */
  static sketch (configs, selectId, debug = false) {
    if (!configs.height || !configs.width) return
 
    if (configs.height / configs.width > 10 || configs.width / configs.height > 10) return
 
    let ratio = parseInt(document.getElementById('darea').style.width) / configs.width * (window.devicePixelRatio || 1) * 1.5
    let sizeradio = 210 / configs.width * (window.devicePixelRatio || 1) * 1.5
    let canvas = document.createElement('canvas')
 
    canvas.height = configs.height * ratio
    canvas.width = configs.width * ratio
    let context = canvas.getContext('2d')
 
    if (configs.elements.length > 0) {
      let elements = JSON.parse(JSON.stringify(configs.elements))
      elements.forEach(element => {
        element.left = element.left * ratio
        element.top = element.top * ratio
        element.oriwidth = element.width
        element.oriheight = element.height
        element.width = element.width * ratio
        element.height = element.height * ratio
 
        if (element.type === 'text') {
          element.padding = element.padding * ratio
          element.fontSize = element.fontSize * sizeradio
        } else if (element.type === 'barcode') {
          element.barcodeWidth = element.barcodeWidth * ratio
          element.barcodeHeight = element.barcodeHeight * ratio
          element.fontSize = element.fontSize * sizeradio
        } else if (element.type === 'qrcode') {
          element.qrcodeWidth = element.qrcodeWidth * ratio
 
          if (element.qrcodeWidth > element.height) {
            element.qrcodeWidth = element.height
          }
          if (element.qrcodeWidth > element.width) {
            element.qrcodeWidth = element.width
          }
        } else if (element.type === 'image') {
          element.imgWidth = element.imgWidth * ratio
          element.imgHeight = element.imgHeight * ratio
        }
      })
 
      return new Promise(resolve => {
        this.sketchothers(context, elements, selectId, debug, ratio, resolve)
      })
    } else {
      return new Promise(resolve => {
        this.cachesketch(context, resolve)
      })
    }
  }
 
  /**
   * @description 绘制图片及文字
   * @param  {Object}    context    画布对象
   * @param  {Object}    elements   图片文字信息
   */
  static sketchothers (context, elements, selectId, debug, ratio, resolve) {
    let element = elements.splice(0, 1)[0] // 逐个绘制图片文字
    let textLineSpace = 5 // 绘制时行间距,防止文字重叠
    context.save()
 
    // if (element.rotate) { // 元素旋转时,设置画布旋转角度
    //   let _cx = element.left + element.width / 2
    //   let _cy = element.top + element.height / 2
    //   context.translate(_cx, _cy) // 移动原点
    //   context.rotate(element.rotate * Math.PI / 180)
    //   context.translate(-_cx, -_cy) // 恢复原点
    // }
 
    if (selectId === element.uuid) { // 选中元素,设置外部阴影
      context.shadowBlur = 5
      context.shadowColor = '#1890ff'
      context.fillStyle = 'white'
      context.fillRect(element.left, element.top, element.width || 1, element.height || 1)
      context.shadowBlur = 0
    } else if (debug) {
      context.shadowBlur = 3
      context.shadowColor = 'orange'
      context.fillStyle = 'white'
      context.fillRect(element.left, element.top, element.width || 1, element.height || 1)
      context.shadowBlur = 0
    }
 
    // 绘制边框
    // context.rect(element.left + element.borderSize / 2, element.top + element.borderSize / 2, element.width - element.borderSize, element.height - element.borderSize)
    if (element.borderSize >= 1) {
      context.beginPath()
      context.strokeStyle = element.borderColor
      context.lineWidth = element.borderSize
      context.rect(element.left, element.top, element.width, element.height)
      context.stroke()
    }
 
    // 设置背景色
    if (!element.borderSize && (element.oriwidth === 1 || element.oriheight === 1)) { // 线
      context.strokeStyle = element.background
      context.beginPath()
      if (element.oriwidth === 1) {
        context.moveTo(element.left, element.top)
        context.lineTo(element.left, element.top + element.height)
      } else {
        context.moveTo(element.left, element.top + element.height)
        context.lineTo(element.left + element.width, element.top + element.height)
      }
      context.stroke()
    } else if (element.background && element.background !== 'white') {
      context.fillStyle = element.background
      context.fillRect(element.left, element.top, element.width, element.height)
    }
 
    if (selectId === element.uuid && element.width > 3 * ratio && element.height > 3 * ratio) { // 选中元素,设置外部阴影
      context.strokeStyle = '#1890ff'
      context.beginPath()
      context.moveTo(element.left + element.width - 7, element.top + element.height - 2)
      context.lineTo(element.left + element.width - 2, element.top + element.height - 7)
      context.moveTo(element.left + element.width - 14, element.top + element.height - 2)
      context.lineTo(element.left + element.width - 2, element.top + element.height - 14)
      context.stroke()
    }
 
    if (!element.width || !element.height) {
      context.restore() // 重置画布
      if (elements.length > 0) {
        this.sketchothers(context, elements, selectId, debug, ratio, resolve)
      } else {
        this.cachesketch(context, resolve)
      }
    } else if (element.type === 'text') {
      // 绘制文字信息
      if (element.fontSize < 12) { // 浏览器最小字体
        textLineSpace += 12 - element.fontSize
      }
      // italic 斜体 small-caps(英文小写字母变成小的大写)
      context.font = 'normal normal ' + element.fontWeight + ' ' + element.fontSize + 'px ' + element.fontFamily
      context.fillStyle = element.fontColor
 
      let lines = element.value.split('\n')
 
      if (!element.value && element.field) {
        if (element.field === 'other_field') {
          lines = [element.cusfield || '']
        } else {
          lines = [element.field]
        }
      }
      let _y = element.top + element.padding + element.fontSize + element.borderSize
      let _left = element.left + element.borderSize + element.padding
      let _right = element.left + element.width - element.padding - element.borderSize
      let _width = _right - _left
      if (element.vertialAlign === 'top') {
        lines.forEach(text => {
          if (!text) {
            _y += element.fontSize + textLineSpace
          }
          let _textArr = []
          let _text = ''
          text.split('').forEach(word => {
            if (context.measureText(_text + word).width <= _width) {
              _text = _text + word
            } else {
              _textArr.push(_text)
              _text = word
            }
          })
          _textArr.forEach(word => {
            let _l = _left
            if (element.align === 'center') {
              _l = _left + (_width - context.measureText(word).width) / 2
            } else if (element.align === 'right') {
              _l = _right - context.measureText(word).width
            } else if (element.align === 'justify') {
              _l = _left + (_width - context.measureText(word).width) / 2
            }
            context.fillText(word, _l, _y)
            _y += element.fontSize + textLineSpace
          })
 
          if (!_text) {
            return
          }
 
          if (element.align === 'left') {
            context.fillText(_text, _left, _y)
          } else if (element.align === 'center') {
            context.fillText(_text, _left + (_width - context.measureText(_text).width) / 2, _y)
          } else if (element.align === 'right') {
            context.fillText(_text, _right - context.measureText(_text).width, _y)
          } else if (element.align === 'justify') {
            if (_text.length === 1) {
              context.fillText(_text, _left + (_width - context.measureText(_text).width) / 2, _y)
            } else {
              let _dleft = _left
              _text.split('').forEach(_word => {
                context.fillText(_word, _dleft, _y)
                _dleft += (_width - context.measureText(_text).width) / (_text.length - 1) + context.measureText(_word).width
              })
            }
          }
 
          _y += element.fontSize + textLineSpace
        })
      } else if (element.vertialAlign === 'middle' || element.vertialAlign === 'bottom') {
        let lineheight = 0
        lines.forEach(text => {
          if (!text) {
            lineheight += element.fontSize + textLineSpace
            return
          }
          lineheight += Math.ceil(context.measureText(text).width / _width) * (element.fontSize + textLineSpace)
        })
 
        if (element.vertialAlign === 'middle') {
          _y += (element.height - element.padding * 2 - element.borderSize * 2 - lineheight) / 2
        } else {
          _y += element.height - element.padding * 2 - element.borderSize * 2 - lineheight
        }
 
        lines.forEach(text => {
          if (!text) {
            _y += element.fontSize + textLineSpace
          }
          let _textArr = []
          let _text = ''
          text.split('').forEach(word => {
            if (context.measureText(_text + word).width <= _width) {
              _text = _text + word
            } else {
              _textArr.push(_text)
              _text = word
            }
          })
          _textArr.forEach(word => {
            let _l = _left
            if (element.align === 'center') {
              _l = _left + (_width - context.measureText(word).width) / 2
            } else if (element.align === 'right') {
              _l = _right - context.measureText(word).width
            } else if (element.align === 'justify') {
              _l = _left + (_width - context.measureText(word).width) / 2
            }
            context.fillText(word, _l, _y)
            _y += element.fontSize + textLineSpace
          })
 
          if (!_text) {
            return
          }
 
          if (element.align === 'left') {
            context.fillText(_text, _left, _y)
          } else if (element.align === 'center') {
            context.fillText(_text, _left + (_width - context.measureText(_text).width) / 2, _y)
          } else if (element.align === 'right') {
            context.fillText(_text, _right - context.measureText(_text).width, _y)
          } else if (element.align === 'justify') {
            if (_text.length === 1) {
              context.fillText(_text, _left + (_width - context.measureText(_text).width) / 2, _y)
            } else {
              let _dleft = _left
              _text.split('').forEach(_word => {
                context.fillText(_word, _dleft, _y)
                _dleft += (_width - context.measureText(_text).width) / (_text.length - 1) + context.measureText(_word).width
              })
            }
          }
 
          _y += element.fontSize + textLineSpace
        })
      }
 
      context.restore() // 重置画布
      if (elements.length > 0) {
        this.sketchothers(context, elements, selectId, debug, ratio, resolve)
      } else {
        this.cachesketch(context, resolve)
      }
    } else if (element.type === 'barcode') {
      let _space = 5
      let _left = element.left
      let _top = element.top
      if (element.vertialAlign === 'middle') {
        if (element.barcodeLabel === 'true') {
          _top = element.top + (element.height - element.barcodeHeight - element.fontSize - _space) / 2
        } else {
          _top = element.top + (element.height - element.barcodeHeight) / 2
        }
      } else if (element.vertialAlign === 'bottom') {
        if (element.barcodeLabel === 'true') {
          _top = element.top + (element.height - element.barcodeHeight - element.fontSize - _space)
        } else {
          _top = element.top + element.height - element.barcodeHeight
        }
      }
 
      if (element.align === 'center') {
        _left = element.left + (element.width - element.barcodeWidth) / 2
      } else if (element.align === 'right') {
        _left = element.left + element.width - element.barcodeWidth
      }
      context.font = 'normal normal normal ' + element.fontSize + 'px Microsoft YaHei'
      context.fillStyle = 'black'
      let text = '1234567890123'
      let _tleft = _left + (element.barcodeWidth - context.measureText(text).width) / 2
      let _ttop = _top + element.barcodeHeight + _space + element.fontSize
 
      let image = new Image()
      image.src = element.url
      if (image.complete) {
        context.drawImage(image, _left, _top, element.barcodeWidth, element.barcodeHeight)
        if (element.barcodeLabel === 'true') {
          context.fillText(text, _tleft, _ttop)
        }
 
        context.restore() // 重置画布
        if (elements.length > 0) {
          this.sketchothers(context, elements, selectId, debug, ratio, resolve)
        } else {
          this.cachesketch(context, resolve)
        }
      } else {
        image.onload = () => {
          context.drawImage(image, _left, _top, element.barcodeWidth, element.barcodeHeight)
          if (element.barcodeLabel === 'true') {
            context.fillText(text, _tleft, _ttop)
          }
  
          context.restore() // 重置画布
          if (elements.length > 0) {
            this.sketchothers(context, elements, selectId, debug, ratio, resolve)
          } else {
            this.cachesketch(context, resolve)
          }
        }
      }
    } else if (element.type === 'qrcode') {
      let _left = element.left
      let _top = element.top
      if (element.vertialAlign === 'middle') {
        _top = element.top + (element.height - element.qrcodeWidth) / 2
      } else if (element.vertialAlign === 'bottom') {
        _top = element.top + element.height - element.qrcodeWidth
      }
 
      if (element.align === 'center') {
        _left = element.left + (element.width - element.qrcodeWidth) / 2
      } else if (element.align === 'right') {
        _left = element.left + element.width - element.qrcodeWidth
      }
 
      let image = new Image()
      image.src = element.url
 
      if (image.complete) {
        context.drawImage(image, _left, _top, element.qrcodeWidth, element.qrcodeWidth)
 
        context.restore() // 重置画布
        if (elements.length > 0) {
          this.sketchothers(context, elements, selectId, debug, ratio, resolve)
        } else {
          this.cachesketch(context, resolve)
        }
      } else {
        image.onload = () => {
          context.drawImage(image, _left, _top, element.qrcodeWidth, element.qrcodeWidth)
  
          context.restore() // 重置画布
          if (elements.length > 0) {
            this.sketchothers(context, elements, selectId, debug, ratio, resolve)
          } else {
            this.cachesketch(context, resolve)
          }
        }
      }
    } else if (element.type === 'image') {
      let _left = element.left
      let _top = element.top
      if (element.vertialAlign === 'middle') {
        _top = element.top + (element.height - element.imgHeight) / 2
      } else if (element.vertialAlign === 'bottom') {
        _top = element.top + element.height - element.imgHeight
      }
 
      if (element.align === 'center') {
        _left = element.left + (element.width - element.imgWidth) / 2
      } else if (element.align === 'right') {
        _left = element.left + element.width - element.imgWidth
      }
 
      let image = new Image()
      image.src = element.url
      if (element.value && element.value.indexOf(window.location.hostname) > -1) {
        image.src = element.value
      }
 
      if (image.complete) {
        context.drawImage(image, _left, _top, element.imgWidth, element.imgHeight)
        
        context.restore() // 重置画布
        if (elements.length > 0) {
          this.sketchothers(context, elements, selectId, debug, ratio, resolve)
        } else {
          this.cachesketch(context, resolve)
        }
      } else {
        image.onload = () => {
          context.drawImage(image, _left, _top, element.imgWidth, element.imgHeight)
  
          context.restore() // 重置画布
          if (elements.length > 0) {
            this.sketchothers(context, elements, selectId, debug, ratio, resolve)
          } else {
            this.cachesketch(context, resolve)
          }
        }
      }
      image.onerror = () => {
        context.restore() // 重置画布
        if (elements.length > 0) {
          this.sketchothers(context, elements, selectId, debug, ratio, resolve)
        } else {
          this.cachesketch(context, resolve)
        }
      }
    }
  }
 
  /**
   * @description 绘制模板值页面
   * @param  {Object}    configs      配置信息
   */
  static cachesketch (context, resolve) {
    let cacheCanvas = context.canvas
    let canvas = document.getElementById('darea')
 
    canvas.width = cacheCanvas.width
    canvas.height = cacheCanvas.height
    let ctx = canvas.getContext('2d')
    ctx.clearRect(0, 0, canvas.width + 1, canvas.height + 1)
    ctx.beginPath()
    ctx.fillStyle = 'white'
    ctx.fillRect(0, 0, canvas.width + 1, canvas.height + 1)
    ctx.drawImage(cacheCanvas, 0, 0, canvas.width, canvas.height)
    resolve(canvas.toDataURL('image/png'))
  }
}