printOrientation.ts 894 Bytes
export type PrintOrientation = 'horizontal' | 'vertical'

/** 与 Web normalizePrintOrientation 一致:默认竖打 */
export function normalizePrintOrientation(raw: unknown): PrintOrientation {
  const v = String(raw ?? '').trim().toLowerCase()
  return v === 'horizontal' ? 'horizontal' : 'vertical'
}

/** 横打时内容相对纸张旋转 90°(纸张 W×H 不变) */
export function shouldRotatePrintContent(orientation: PrintOrientation): boolean {
  return orientation === 'horizontal'
}

/** 在 canvas 上应用模板级打印方向(与 Web printContentLayerStyle 一致) */
export function applyPrintOrientationCanvasTransform(
  ctx: UniApp.CanvasContext,
  cw: number,
  ch: number,
  orientation: PrintOrientation,
): void {
  if (!shouldRotatePrintContent(orientation)) return
  ctx.translate(cw / 2, ch / 2)
  ctx.rotate(Math.PI / 2)
  ctx.translate(-cw / 2, -ch / 2)
}