/* text.js - 文字的字体串、度量与绘制(v1.9) * * 为什么单独一个模块:文字要在两处画 —— 落层(layers.js#drawVectorToCanvas)与 * 预览/度量(objects.js)。两边各写一遍必然出现「量出来的框和画出来的字不一样」。 * 这里做成零依赖叶子模块(不 import 任何编辑器模块),objects.js 与 layers.js * 都只往下依赖它,不会形成循环 import(esbuild 打成 IIFE 时循环 import 会拿到 undefined)。 * * 坐标语义(与 v1.8 之前保持一致,勿随意改动): * t.x / t.y 是「文字块」左上角;底色 padding 与描边从这一点向内/外扩展。 * 单行 + 无底色 + 无描边时,textBox() 与老版 textBounds() 完全相等(w=文字宽,h=fontSize×1.25)。 */ 'use strict' /** 默认行高倍数:必须等于老版的 fontSize×1.25,否则单行文字的选中框会变高 */ export const TEXT_LINE_HEIGHT = 1.25 /** * 文字默认样式:**唯一的真相源**。 * 以前 '#ff4444' / '#ffffff' / '#000000' / 0.68 散落在 state.js、text.js、 * events.js 三处,改一处另两处不跟,界面上就会出现「同一个概念三种颜色」—— * 用户实测反馈「字体颜色有 3 个位置,那个黑色应该换成默认的参数」正是这么来的。 * 底色默认也不再是纯黑 #000000:纯黑在深色/花色底图上像抠了个洞, * 而且和「文字色」看起来像是同一件事。改成中性近黑 #101014 + 68% 透明。 */ export const TEXT_DEFAULTS = { color: '#ff4444', strokeWidth: 0, strokeColor: '#ffffff', bgFill: false, bgColor: '#101014', bgAlpha: 0.68 } /** 底色块相对字号的 padding 系数 */ const BG_PAD_RATIO = 0.3 let _measureCtx = null /** 度量用的离屏 ctx:不依赖编辑器 DOM 是否已建好,避免 textBounds 在早期调用时抛错 */ function measureCtx() { if (!_measureCtx) { const c = document.createElement('canvas') c.width = 8 c.height = 8 _measureCtx = c.getContext('2d') } return _measureCtx } /** 字重:'bold' | 'normal' | '900' …(默认粗体,与老版一致) */ export function weightOf(t) { const w = t && t.fontWeight return w == null || w === '' ? 'bold' : String(w) } /** * 字体预设(v1.14):key → { label, stack }。 * * 为什么只做「系统字体族」而不是内置字体文件: * 1. 宿主 CSP 是 `default-src 'self'`,font-src 回落它,第三方字体 CDN 会被浏览器直接拦; * 2. 中文字体动辄 2–64MB(本机宋体 2.3MB / 楷体 2.2MB / 苹方全字重 64MB), * 插件产物现在才 389KB,内联等于体积涨 5–150 倍; * 3. 系统字体同步可用,没有「字体没加载完就 measureText」导致框和字错位的时序问题。 * 每个 stack 都是跨平台候选队列:macOS 先命中苹方/宋体,Windows 命中雅黑/宋体, * 全不命中时退到通用的 sans-serif / serif —— 永远不会渲染失败。 * * 注意:中文是全角等宽,换字体不改字宽(实测 10 字 48px 在 6 种字体下都是 480px), * 所以切换不会引起版面跳动;只有拉丁字母和数字会伸缩。 */ export const TEXT_FONTS = { sans: { label: '黑体', stack: '"PingFang SC", "Microsoft YaHei", "Heiti SC", "Hiragino Sans GB", "Source Han Sans SC", "Noto Sans SC", sans-serif' }, serif: { label: '宋体', stack: '"Songti SC", "SimSun", "STSong", "Source Han Serif SC", "Noto Serif SC", serif' }, kai: { label: '楷体', stack: '"Kaiti SC", "KaiTi", "STKaiti", "AR PL UKai CN", "Noto Serif SC", serif' }, round: { label: '圆体', stack: '"Yuanti SC", "YouYuan", "STYuanti", "PingFang SC", sans-serif' } } export const TEXT_FONT_KEYS = ['sans', 'serif', 'kai', 'round'] export const TEXT_FONT_DEFAULT = 'sans' /** 把任意输入收敛成已知的字体 key:老对象没有 fontFamily、或存了脏值,都回退到默认 */ export function fontKeyOf(t) { const k = t && t.fontFamily return TEXT_FONTS[k] ? k : TEXT_FONT_DEFAULT } /** 字体栈串(不含字号字重),选项栏下拉与预览都用它 */ export function fontStackOf(t) { return TEXT_FONTS[fontKeyOf(t)].stack } export function fontOf(t) { const size = Math.max(4, Number(t && t.fontSize) || 32) return weightOf(t) + ' ' + size + 'px ' + fontStackOf(t) } /** 文字的排布度量:分行、每行宽度、行高、底色 padding、描边宽度 */ export function textMetrics(t) { const ctx = measureCtx() const font = fontOf(t) ctx.font = font const raw = (t && t.text) == null ? '' : String(t.text) const lines = raw.split('\n') const widths = lines.map(line => ctx.measureText(line).width) const size = Math.max(4, Number(t && t.fontSize) || 32) const lh = size * (Number(t && t.lineHeight) || TEXT_LINE_HEIGHT) const w = lines.length ? Math.max.apply(null, widths) : 0 const pad = t && t.bgFill ? Math.round(size * BG_PAD_RATIO) : 0 const sw = Math.max(0, Number(t && t.strokeWidth) || 0) return { lines, widths, w, h: lh * lines.length, lh, pad, sw, font, size } } /** 包围盒(图片坐标):含底色 padding 与描边向外扩出的一半 */ export function textBox(t) { const m = textMetrics(t) const half = m.sw / 2 return { x: t.x - half, y: t.y - half, w: Math.max(m.w, 2) + m.pad * 2 + m.sw, h: Math.max(m.h, 1) + m.pad * 2 + m.sw } } function alignOf(t) { const a = t && t.align return a === 'center' || a === 'right' ? a : 'left' } /** * 画一段文字(含底色块、描边、多行、旋转)。 * 底色块与描边都按「块」来画:底色覆盖整块,描边沿字形外沿。 */ export function drawTextObject(ctx, t) { const m = textMetrics(t) ctx.save() ctx.textBaseline = 'top' ctx.globalAlpha = t.opacity != null ? t.opacity : 1 const b = textBox(t) const rot = t.rotation || 0 if (rot) { const cx = b.x + b.w / 2 const cy = b.y + b.h / 2 ctx.translate(cx, cy) ctx.rotate(rot * Math.PI / 180) ctx.translate(-cx, -cy) } ctx.font = m.font const align = alignOf(t) const boxW = m.w + m.pad * 2 // 底色块:先于文字画,圆角按字号比例给一点(纯直角贴小字会很硬) if (t.bgFill) { const r = Math.min(m.pad + 2, Math.min(boxW, m.h + m.pad * 2) / 2) // 0.55 在浅色/花色底图上偏透,用户容易以为「底色没生效」;0.68 既压得住底图, // 也还看得见一点背景,是「垫一块板让字立起来」的常用手感。 ctx.globalAlpha = (t.opacity != null ? t.opacity : 1) * (t.bgAlpha == null ? TEXT_DEFAULTS.bgAlpha : t.bgAlpha) ctx.fillStyle = t.bgColor || TEXT_DEFAULTS.bgColor const bx = t.x, by = t.y const bw = boxW, bh = m.h + m.pad * 2 ctx.beginPath() if (typeof ctx.roundRect === 'function') { ctx.roundRect(bx, by, bw, bh, r) } else { ctx.moveTo(bx + r, by) ctx.arcTo(bx + bw, by, bx + bw, by + bh, r) ctx.arcTo(bx + bw, by + bh, bx, by + bh, r) ctx.arcTo(bx, by + bh, bx, by, r) ctx.arcTo(bx, by, bx + bw, by, r) ctx.closePath() } ctx.fill() ctx.globalAlpha = (t.opacity != null ? t.opacity : 1) } // 对齐以「块内可用宽度(不含 padding)」为基准:多行时各行按这个宽度对齐 ctx.textAlign = align const left = t.x + m.pad const ox = align === 'center' ? left + m.w / 2 : align === 'right' ? left + m.w : left ctx.lineJoin = 'round' ctx.miterLimit = 2 for (let i = 0; i < m.lines.length; i++) { const y = t.y + m.pad + i * m.lh if (m.sw > 0) { ctx.lineWidth = m.sw ctx.strokeStyle = t.strokeColor || TEXT_DEFAULTS.strokeColor ctx.strokeText(m.lines[i], ox, y) } ctx.fillStyle = t.color || TEXT_DEFAULTS.color ctx.fillText(m.lines[i], ox, y) } ctx.restore() } /** 从默认样式造一段文字的字段(新建文字 / 老对象补默认值共用) */ export function newTextStyle(src) { const s = src || {} return { fontWeight: weightOf(s), // 字体 key 过一遍收敛:外部传进来不认识的字符串不会进 ctx.font(脏值会让整个 // 字体串失效、静默退回浏览器默认,文件本身看不出问题,极难排查) fontFamily: fontKeyOf(s), align: alignOf(s), lineHeight: Number(s.lineHeight) || TEXT_LINE_HEIGHT, strokeWidth: Math.max(0, Number(s.strokeWidth) || 0), strokeColor: s.strokeColor || TEXT_DEFAULTS.strokeColor, bgFill: !!s.bgFill, bgColor: s.bgColor || TEXT_DEFAULTS.bgColor, bgAlpha: s.bgAlpha == null ? TEXT_DEFAULTS.bgAlpha : Number(s.bgAlpha) } }