templatePhysicalMm.ts
1.41 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
/**
* 与 NativeTemplateCommandBuilder.toMillimeter 一致(px 按 96dpi 转 mm),
* 用于判断模板是否适合走 native-fast-printer 的 TSC 模板指令(常见标签幅宽约 4 英寸级)。
*/
import type { SystemLabelTemplate } from './types/printer'
const DESIGN_DPI = 96
/** 常见 4″ 标签机安全上限(mm),略放宽 */
const NATIVE_FAST_MAX_WIDTH_MM = 112
const NATIVE_FAST_MAX_HEIGHT_MM = 320
export function templateSizeToMillimeters (
unit: string | undefined,
width: number,
height: number
): { widthMm: number; heightMm: number } {
const u = String(unit || 'inch').toLowerCase()
let widthMm = 0
let heightMm = 0
if (u === 'mm') {
widthMm = width
heightMm = height
} else if (u === 'cm') {
widthMm = width * 10
heightMm = height * 10
} else if (u === 'px') {
widthMm = (width / DESIGN_DPI) * 25.4
heightMm = (height / DESIGN_DPI) * 25.4
} else {
widthMm = width * 25.4
heightMm = height * 25.4
}
return { widthMm, heightMm }
}
export function isTemplateWithinNativeFastPrintBounds (
template: Pick<SystemLabelTemplate, 'unit' | 'width' | 'height'>
): boolean {
const w = Number(template.width) || 0
const h = Number(template.height) || 0
if (w <= 0 || h <= 0) return false
const { widthMm, heightMm } = templateSizeToMillimeters(template.unit, w, h)
return widthMm <= NATIVE_FAST_MAX_WIDTH_MM && heightMm <= NATIVE_FAST_MAX_HEIGHT_MM
}