Files
iwb-plugins-desktop/image-annotate/plugin/editor/text.js
T
qiqi e699c22fa0 feat(image-annotate): v1.9.0 文字样式包、吸附参考线、圆角与最近色、复制到剪贴板
文字从「字号 + 颜色」扩到完整样式:字重(常规/粗/特粗)、对齐(左/中/右)、
行距、描边、底色块、多行(textarea + Shift+Enter)。新增叶子模块 text.js,
fontOf / textMetrics / textBox / drawTextObject 成为唯一事实来源,
objects.js(预览/命中/包围盒)与 layers.js(栅格化)共用,避免量一套画一套;
描边与底色撑大包围盒,手柄、吸附、缩略图随之变化。单行无描边无底色的老文字
包围盒与旧实现逐像素一致,老作业打开不变形。

吸附参考线:snap.js 只做纯计算。目标线为画布 0/中/满 + 其他可见元素的左中右、
上中下;阈值 6 屏幕像素按 viewScale() 折回图片像素,磁力不随缩放变大;
命中画洋红参考线(区别于选中框的橙),松手清空;Alt 全程禁用。
位图/文字/形状三条拖动路径都接,文字需补 b.x - t.x 原点偏移。

矩形圆角:shapeRadius 进 drawShape,半径夹到 min(w,h)/2,路径手写 arcTo
(不用 ctx.roundRect,老内核没有)。最近使用颜色最多 8 个,存 ctx.settings
跨会话保留,只有落定的改色才记录;色板改用事件委托以覆盖动态插入的色块。

导出「复制」:结果编成 PNG 写 ClipboardItem,可直接粘进聊天窗口/后台上传框
且不关闭编辑器。优先 createImageBitmap 绕开宿主 CSP 的 img-src 限制;
非安全上下文无 navigator.clipboard 时明确提示改用「导出」而非静默失败。

顺带修:选项栏中文双字标签加 white-space:nowrap(否则 36px 一行内竖排成
「常/规」);.iwb-editor-optionbar-tip 补基础样式(原来只有 display:none,
裸 span 参与 flex 收缩被挤成两行贴边);文字工具的 hint 移到按钮之后,
放不下时让说明滚出视野而不是把操作按钮顶没。

harness 140 → 158 条,全部通过;validate 9/9。
2026-11-08 20:27:32 +08:00

156 lines
5.6 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
/* 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
/** 底色块相对字号的 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)
}
export function fontOf(t) {
const size = Math.max(4, Number(t && t.fontSize) || 32)
return weightOf(t) + ' ' + size + 'px sans-serif'
}
/** 文字的排布度量:分行、每行宽度、行高、底色 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)
ctx.globalAlpha = (t.opacity != null ? t.opacity : 1) * (t.bgAlpha == null ? 0.55 : t.bgAlpha)
ctx.fillStyle = t.bgColor || '#000000'
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 || '#ffffff'
ctx.strokeText(m.lines[i], ox, y)
}
ctx.fillStyle = t.color || '#ff4444'
ctx.fillText(m.lines[i], ox, y)
}
ctx.restore()
}
/** 从默认样式造一段文字的字段(新建文字 / 老对象补默认值共用) */
export function newTextStyle(src) {
const s = src || {}
return {
fontWeight: weightOf(s),
align: alignOf(s),
lineHeight: Number(s.lineHeight) || TEXT_LINE_HEIGHT,
strokeWidth: Math.max(0, Number(s.strokeWidth) || 0),
strokeColor: s.strokeColor || '#ffffff',
bgFill: !!s.bgFill,
bgColor: s.bgColor || '#000000',
bgAlpha: s.bgAlpha == null ? 0.55 : Number(s.bgAlpha)
}
}