/** 文本控件在元素框内的垂直对齐(config.verticalAlign) */ export type TextVerticalAlign = 'top' | 'center' | 'bottom'; export function readVerticalAlign( config: Record | undefined | null, ): TextVerticalAlign { const raw = config?.verticalAlign ?? config?.VerticalAlign; const v = String(raw ?? 'top').trim().toLowerCase(); if (v === 'center' || v === 'middle') return 'center'; if (v === 'bottom') return 'bottom'; return 'top'; } export function verticalAlignToFlexJustify( align: TextVerticalAlign, ): 'flex-start' | 'center' | 'flex-end' { if (align === 'center') return 'center'; if (align === 'bottom') return 'flex-end'; return 'flex-start'; } export function textAlignToFlexAlign( align: string | undefined, ): 'flex-start' | 'center' | 'flex-end' { const v = String(align ?? 'left').toLowerCase(); if (v === 'center') return 'center'; if (v === 'right') return 'flex-end'; return 'flex-start'; } /** 画布/位图:文本块在元素框内的顶部 Y 偏移(不含 baseline) */ export function computeVerticalTextBlockOffset( innerHeight: number, blockHeight: number, verticalAlign: TextVerticalAlign, ): number { const extra = Math.max(0, innerHeight - blockHeight); if (verticalAlign === 'center') return Math.floor(extra / 2); if (verticalAlign === 'bottom') return extra; return 0; } export function readFontWeight( config: Record | undefined | null, ): 'normal' | 'bold' { const raw = config?.fontWeight ?? config?.FontWeight; const v = String(raw ?? 'normal').trim().toLowerCase(); return v === 'bold' || v === '700' || v === 'bolder' ? 'bold' : 'normal'; } export function readFontStyle( config: Record | undefined | null, ): 'normal' | 'italic' { const raw = config?.fontStyle ?? config?.FontStyle; const v = String(raw ?? 'normal').trim().toLowerCase(); return v === 'italic' || v === 'oblique' ? 'italic' : 'normal'; } export function readTextDecoration( config: Record | undefined | null, ): 'none' | 'underline' { const raw = config?.textDecoration ?? config?.TextDecoration; const v = String(raw ?? 'none').trim().toLowerCase(); return v.includes('underline') ? 'underline' : 'none'; } /** 元素级 border(兼容 Border / BorderType) */ export function readElementBorder( el: { border?: string | null; Border?: string | null; BorderType?: string | null; borderType?: string | null }, ): string { const raw = el.border ?? el.Border ?? el.BorderType ?? el.borderType ?? 'none'; return String(raw).trim().toLowerCase() || 'none'; } /** 读取元素 rotation(兼容大小写 / 旧数据) */ export function readElementRotation( el: { rotation?: string | null }, ): 'horizontal' | 'vertical' { const v = String(el.rotation ?? 'horizontal').trim().toLowerCase(); return v === 'vertical' ? 'vertical' : 'horizontal'; } /** 切换 horizontal / vertical 时交换宽高并保持中心点不变(与参考图 2 一致) */ export function patchElementRotationWithLayout( el: { x: number; y: number; width: number; height: number; rotation?: string | null }, nextRotation: 'horizontal' | 'vertical', ): { rotation: 'horizontal' | 'vertical'; x: number; y: number; width: number; height: number; } { const prev = readElementRotation(el); if (prev === nextRotation) { // vertical 但宽>高:选框与文字方向不一致,强制纠正 if (nextRotation === 'vertical' && el.width > el.height) { const w = Math.max(1, el.width); const h = Math.max(1, el.height); const cx = el.x + w / 2; const cy = el.y + h / 2; const newW = h; const newH = w; return { rotation: nextRotation, width: newW, height: newH, x: Math.round(cx - newW / 2), y: Math.round(cy - newH / 2), }; } return { rotation: nextRotation, x: el.x, y: el.y, width: el.width, height: el.height, }; } const w = Math.max(1, el.width); const h = Math.max(1, el.height); const cx = el.x + w / 2; const cy = el.y + h / 2; const newW = h; const newH = w; return { rotation: nextRotation, width: newW, height: newH, x: Math.round(cx - newW / 2), y: Math.round(cy - newH / 2), }; } /** 竖排且宽>高时纠正选框(落库 / 渲染用) */ export function canonicalElementGeometry< T extends { rotation?: string | null; width: number; height: number; x: number; y: number }, >(el: T): T { return normalizeElementRotationBox(el); } /** 旧模板 vertical 但宽>高时自动纠正选框 */ export function normalizeElementRotationBox< T extends { rotation?: string | null; width: number; height: number; x: number; y: number }, >(el: T): T { if (readElementRotation(el) !== 'vertical') return el; if (el.width <= el.height) return el; return { ...el, rotation: 'vertical', ...patchElementRotationWithLayout({ ...el, rotation: 'horizontal' }, 'vertical'), }; }