nativeBitmapPatch.ts
10.4 KB
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
import type {
MonochromeImageData,
SystemTemplateElementBase,
SystemTemplateTextAlign,
} from './types/printer'
declare const plus: any
const DESIGN_DPI = 96
const DEFAULT_THRESHOLD = 180
const TEXT_PADDING_DOTS = 6
function normalizePrinterLikeText (str: string): string {
return String(str || '')
.normalize('NFKC')
.replace(/[\u2018\u2019]/g, '\'')
.replace(/[\u201C\u201D]/g, '"')
.replace(/[\u2013\u2014]/g, '-')
}
type BitmapPatchItem = {
type: 'bitmap'
x: number
y: number
image: MonochromeImageData
}
function clamp (value: number, min: number, max: number): number {
return Math.max(min, Math.min(max, Math.round(value)))
}
function ensureMultipleOf8 (value: number): number {
const safe = Math.max(8, Math.round(value || 0))
return safe % 8 === 0 ? safe : safe + (8 - (safe % 8))
}
function pxToDots (value: number, dpi: number): number {
return Math.max(0, Math.round((Number(value) || 0) * dpi / DESIGN_DPI))
}
function normalizeBase64Payload (source: string): string {
const value = String(source || '').trim()
if (!value) return ''
if (value.startsWith('data:image/')) {
const index = value.indexOf(',')
return index >= 0 ? value.slice(index + 1) : ''
}
if (/^[A-Za-z0-9+/=\r\n]+$/.test(value) && value.length > 128) {
return value.replace(/\s+/g, '')
}
return ''
}
function resolveLocalImagePath (source: string): string {
let path = String(source || '').trim()
if (!path) return ''
if (path.startsWith('file://')) {
path = path.replace(/^file:\/\//, '')
}
// #ifdef APP-PLUS
try {
const converted = plus.io.convertLocalFileSystemURL(path)
if (converted) path = converted
} catch (_) {}
// #endif
try {
path = decodeURIComponent(path)
} catch (_) {}
return path
}
function getAndroidGraphics () {
// #ifdef APP-PLUS
try {
if (typeof plus === 'undefined' || String(plus.os?.name || '').toLowerCase() !== 'android') return null
return {
Bitmap: plus.android.importClass('android.graphics.Bitmap'),
BitmapFactory: plus.android.importClass('android.graphics.BitmapFactory'),
BitmapConfig: plus.android.importClass('android.graphics.Bitmap$Config'),
Canvas: plus.android.importClass('android.graphics.Canvas'),
Paint: plus.android.importClass('android.graphics.Paint'),
Color: plus.android.importClass('android.graphics.Color'),
Typeface: plus.android.importClass('android.graphics.Typeface'),
Base64: plus.android.importClass('android.util.Base64'),
}
} catch (error) {
console.error('getAndroidGraphics failed', error)
return null
}
// #endif
// #ifndef APP-PLUS
return null
// #endif
}
function bitmapToMonochromeImage (
bitmap: any,
threshold = DEFAULT_THRESHOLD
): MonochromeImageData {
const bitmapWidth = Number(bitmap.getWidth ? bitmap.getWidth() : 0)
const bitmapHeight = Number(bitmap.getHeight ? bitmap.getHeight() : 0)
const width = ensureMultipleOf8(bitmapWidth)
const height = Math.max(1, bitmapHeight)
const pixels: number[] = new Array(width * height).fill(0)
for (let y = 0; y < height; y++) {
for (let x = 0; x < width; x++) {
if (x >= bitmapWidth) {
pixels[y * width + x] = 0
continue
}
const color = Number(bitmap.getPixel(x, y))
const alpha = (color >>> 24) & 0xff
const red = (color >>> 16) & 0xff
const green = (color >>> 8) & 0xff
const blue = color & 0xff
const gray = red * 0.299 + green * 0.587 + blue * 0.114
pixels[y * width + x] = alpha <= 10 || gray > threshold ? 0 : 1
}
}
return { width, height, pixels }
}
function splitTextLines (text: string, paint: any, maxWidth: number): string[] {
const lines: string[] = []
const rawLines = String(text || '').replace(/\r/g, '').split('\n')
rawLines.forEach((segment) => {
if (!segment) {
lines.push('')
return
}
let current = ''
for (let i = 0; i < segment.length; i++) {
const char = segment.charAt(i)
const candidate = current + char
const measure = Number(paint.measureText(candidate))
if (current && measure > maxWidth) {
lines.push(current)
current = char
} else {
current = candidate
}
}
if (current || lines.length === 0) lines.push(current)
})
return lines.length > 0 ? lines : ['']
}
export function shouldRasterizeTextElement (text: string, type: string): boolean {
const normalizedType = String(type || '').toUpperCase()
const normalizedText = normalizePrinterLikeText(text)
if (!normalizedText) return false
if (normalizedType === 'TEXT_PRICE' && /[€£¥¥]/.test(normalizedText)) return true
if (/[€£¥¥éÉáàâäãåæçèêëìíîïñòóôöõøùúûüýÿœšž]/.test(normalizedText)) return true
return /[^\x20-\x7E]/.test(normalizedText)
}
export function createTextBitmapPatch (params: {
element: SystemTemplateElementBase
text: string
dpi: number
align: SystemTemplateTextAlign
}): BitmapPatchItem | null {
const graphics = getAndroidGraphics()
if (!graphics) return null
const { element, text, dpi, align } = params
const config = element.config || {}
const Bitmap = graphics.Bitmap
const BitmapConfig = graphics.BitmapConfig
const Canvas = graphics.Canvas
const Paint = graphics.Paint
const Color = graphics.Color
const Typeface = graphics.Typeface
const contentWidth = Math.max(8, pxToDots(element.width, dpi))
const width = ensureMultipleOf8(contentWidth + TEXT_PADDING_DOTS * 2)
const height = Math.max(16, pxToDots(element.height, dpi) + TEXT_PADDING_DOTS * 2)
const bitmap = Bitmap.createBitmap(width, height, BitmapConfig.ARGB_8888)
const canvas = new Canvas(bitmap)
canvas.drawColor(Color.WHITE)
const paint = new Paint()
paint.setAntiAlias(true)
paint.setDither(true)
paint.setColor(Color.BLACK)
paint.setSubpixelText(true)
const fontSizeDots = Math.max(14, pxToDots(Number(config.fontSize || 14), dpi))
paint.setTextSize(fontSizeDots)
const isBold = String(config.fontWeight || '').toLowerCase() === 'bold' || String(element.type || '').toUpperCase() === 'TEXT_PRICE'
paint.setFakeBoldText(isBold)
paint.setTypeface(isBold ? Typeface.DEFAULT_BOLD : Typeface.DEFAULT)
const maxTextWidth = Math.max(8, contentWidth)
const lines = splitTextLines(text, paint, maxTextWidth)
const fontMetrics = paint.getFontMetrics()
const lineHeight = Math.max(
fontSizeDots + 2,
Math.ceil(Math.abs(Number(fontMetrics.top)) + Math.abs(Number(fontMetrics.bottom)) + 2)
)
const totalHeight = lines.length * lineHeight
const isCenteredVertically = String(element.type || '').toUpperCase() === 'TEXT_PRICE'
const topOffset = isCenteredVertically
? Math.max(TEXT_PADDING_DOTS, Math.floor((height - totalHeight) / 2))
: TEXT_PADDING_DOTS
for (let i = 0; i < lines.length; i++) {
const line = lines[i]
const lineWidth = Number(paint.measureText(line))
let drawX = TEXT_PADDING_DOTS
if (align === 'center') {
drawX = TEXT_PADDING_DOTS + Math.max(0, Math.round((maxTextWidth - lineWidth) / 2))
} else if (align === 'right') {
drawX = TEXT_PADDING_DOTS + Math.max(0, Math.round(maxTextWidth - lineWidth))
}
const baseline = topOffset + i * lineHeight - Number(fontMetrics.top)
canvas.drawText(line, drawX, baseline, paint)
}
const image = bitmapToMonochromeImage(bitmap)
try {
bitmap.recycle && bitmap.recycle()
} catch (_) {}
return {
type: 'bitmap',
x: Math.max(0, pxToDots(element.x, dpi) - TEXT_PADDING_DOTS),
y: Math.max(0, pxToDots(element.y, dpi) - TEXT_PADDING_DOTS),
image,
}
}
function decodeSourceBitmap (source: string, graphics: ReturnType<typeof getAndroidGraphics>) {
if (!graphics) return null
const rawSource = String(source || '').trim()
if (!rawSource) return null
const base64Payload = normalizeBase64Payload(rawSource)
if (base64Payload) {
const bytes = graphics.Base64.decode(base64Payload, 0)
return graphics.BitmapFactory.decodeByteArray(bytes, 0, bytes.length)
}
const localPath = resolveLocalImagePath(rawSource)
if (!localPath) return null
return graphics.BitmapFactory.decodeFile(localPath)
}
export function createImageBitmapPatch (params: {
element: SystemTemplateElementBase
dpi: number
}): BitmapPatchItem | null {
const graphics = getAndroidGraphics()
if (!graphics) return null
const { element, dpi } = params
const config = element.config || {}
const sourceBitmap = decodeSourceBitmap(String(config.src || config.data || config.url || ''), graphics)
if (!sourceBitmap) return null
const Bitmap = graphics.Bitmap
const BitmapConfig = graphics.BitmapConfig
const Canvas = graphics.Canvas
const Paint = graphics.Paint
const Color = graphics.Color
const width = ensureMultipleOf8(Math.max(8, pxToDots(element.width, dpi)))
const height = Math.max(8, pxToDots(element.height, dpi))
const outputBitmap = Bitmap.createBitmap(width, height, BitmapConfig.ARGB_8888)
const canvas = new Canvas(outputBitmap)
canvas.drawColor(Color.WHITE)
const sourceWidth = Number(sourceBitmap.getWidth ? sourceBitmap.getWidth() : 0)
const sourceHeight = Number(sourceBitmap.getHeight ? sourceBitmap.getHeight() : 0)
const scaleMode = String(config.scaleMode || 'contain').toLowerCase()
let targetWidth = width
let targetHeight = height
let targetLeft = 0
let targetTop = 0
if (sourceWidth > 0 && sourceHeight > 0 && scaleMode !== 'fill') {
const ratio = scaleMode === 'cover'
? Math.max(width / sourceWidth, height / sourceHeight)
: Math.min(width / sourceWidth, height / sourceHeight)
targetWidth = Math.max(1, Math.round(sourceWidth * ratio))
targetHeight = Math.max(1, Math.round(sourceHeight * ratio))
targetLeft = Math.round((width - targetWidth) / 2)
targetTop = Math.round((height - targetHeight) / 2)
}
const scaledBitmap = Bitmap.createScaledBitmap(sourceBitmap, targetWidth, targetHeight, true)
const paint = new Paint()
paint.setAntiAlias(true)
paint.setFilterBitmap(true)
canvas.drawBitmap(scaledBitmap, targetLeft, targetTop, paint)
const image = bitmapToMonochromeImage(outputBitmap, Number(config.threshold || DEFAULT_THRESHOLD))
try {
scaledBitmap?.recycle && scaledBitmap.recycle()
} catch (_) {}
try {
sourceBitmap?.recycle && sourceBitmap.recycle()
} catch (_) {}
try {
outputBitmap?.recycle && outputBitmap.recycle()
} catch (_) {}
return {
type: 'bitmap',
x: pxToDots(element.x, dpi),
y: pxToDots(element.y, dpi),
image,
}
}