/**
|
* 纸张尺寸
|
* <p>
|
* 与后端 QcReportEnums.PageSizeEnum 取值一一对应。
|
* 设计器换算画布像素、渲染引擎换算 @page 都用这一份,避免两处尺寸各写一遍后对不上。
|
*/
|
|
/** 纸张尺寸(毫米),未列出的尺寸回退到默认纸张 */
|
export const PAGE_SIZE_MM: Record<string, { height: number; width: number }> = {
|
A3: { width: 297, height: 420 },
|
A4: { width: 210, height: 297 },
|
A5: { width: 148, height: 210 },
|
Letter: { width: 215.9, height: 279.4 },
|
};
|
|
/** 默认纸张 */
|
export const DEFAULT_PAGE_SIZE = 'A4';
|
|
/** 默认页边距(毫米) */
|
export const DEFAULT_MARGIN_MM = 10;
|
|
/** 纸张配置,结构等价于 Schema.page,不依赖具体接口类型定义 */
|
export interface PageSetting {
|
size?: string;
|
orientation?: string;
|
margin?: { bottom?: number; left?: number; right?: number; top?: number };
|
}
|
|
export interface ResolvedPage {
|
heightMm: number;
|
marginMm: { bottom: number; left: number; right: number; top: number };
|
widthMm: number;
|
}
|
|
/** 解析纸张尺寸与页边距(毫米),横向时翻转宽高 */
|
export function resolvePageMm(page?: PageSetting): ResolvedPage {
|
const base = PAGE_SIZE_MM[page?.size ?? DEFAULT_PAGE_SIZE] ?? PAGE_SIZE_MM[DEFAULT_PAGE_SIZE]!;
|
const isLandscape = (page?.orientation ?? 'portrait') === 'landscape';
|
const margin = page?.margin;
|
return {
|
widthMm: isLandscape ? base.height : base.width,
|
heightMm: isLandscape ? base.width : base.height,
|
marginMm: {
|
top: margin?.top ?? DEFAULT_MARGIN_MM,
|
right: margin?.right ?? DEFAULT_MARGIN_MM,
|
bottom: margin?.bottom ?? DEFAULT_MARGIN_MM,
|
left: margin?.left ?? DEFAULT_MARGIN_MM,
|
},
|
};
|
}
|