@yft-design/engine 0.0.0-stage → 0.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +39 -2
- package/assets/images/greyfloral.png +0 -0
- package/assets/images/honey_im_subtle.png +0 -0
- package/assets/images/nasty_fabric.png +0 -0
- package/assets/images/retina_wood.png +0 -0
- package/assets/images/rotate.png +0 -0
- package/dist/EngineCanvas.d.ts +500 -0
- package/dist/EngineCanvas.js +927 -0
- package/dist/core/canvasPerf.d.ts +23 -0
- package/dist/core/canvasPerf.js +170 -0
- package/dist/core/color.d.ts +52 -0
- package/dist/core/color.js +107 -0
- package/dist/core/distanceField.d.ts +15 -0
- package/dist/core/distanceField.js +92 -0
- package/dist/core/events.d.ts +34 -0
- package/dist/core/events.js +52 -0
- package/dist/core/gradient.d.ts +97 -0
- package/dist/core/gradient.js +175 -0
- package/dist/core/imageFilters.d.ts +150 -0
- package/dist/core/imageFilters.js +330 -0
- package/dist/core/mat2d.d.ts +151 -0
- package/dist/core/mat2d.js +241 -0
- package/dist/core/pathData.d.ts +53 -0
- package/dist/core/pathData.js +926 -0
- package/dist/core/pattern.d.ts +38 -0
- package/dist/core/pattern.js +40 -0
- package/dist/core/point.d.ts +59 -0
- package/dist/core/point.js +119 -0
- package/dist/core/psdEffects.d.ts +126 -0
- package/dist/core/psdEffects.js +158 -0
- package/dist/core/roundedRect.d.ts +62 -0
- package/dist/core/roundedRect.js +163 -0
- package/dist/core/shadow.d.ts +51 -0
- package/dist/core/shadow.js +47 -0
- package/dist/core/snap.d.ts +232 -0
- package/dist/core/snap.js +793 -0
- package/dist/core/stroke.d.ts +41 -0
- package/dist/core/stroke.js +44 -0
- package/dist/core/triangulate.d.ts +24 -0
- package/dist/core/triangulate.js +118 -0
- package/dist/debug/canvasInspection.d.ts +92 -0
- package/dist/debug/canvasInspection.js +113 -0
- package/dist/debug/pixelDiff.d.ts +22 -0
- package/dist/debug/pixelDiff.js +64 -0
- package/dist/debug/seed.d.ts +473 -0
- package/dist/debug/seed.js +3803 -0
- package/dist/decoders/gif2sprite.d.ts +9 -0
- package/dist/decoders/gif2sprite.js +84 -0
- package/dist/decoders/webp2sprite.d.ts +22 -0
- package/dist/decoders/webp2sprite.js +133 -0
- package/dist/engineFlag.d.ts +72 -0
- package/dist/engineFlag.js +86 -0
- package/dist/export/SceneExporter.d.ts +92 -0
- package/dist/export/SceneExporter.js +280 -0
- package/dist/export/glExport.d.ts +66 -0
- package/dist/export/glExport.js +79 -0
- package/dist/export/svgExport.d.ts +277 -0
- package/dist/export/svgExport.js +1790 -0
- package/dist/index.d.ts +52 -0
- package/dist/index.js +48 -0
- package/dist/interact/animationPlayback.d.ts +125 -0
- package/dist/interact/animationPlayback.js +344 -0
- package/dist/interact/canvasInteractions.d.ts +50 -0
- package/dist/interact/canvasInteractions.js +150 -0
- package/dist/interact/cropSession.d.ts +123 -0
- package/dist/interact/cropSession.js +526 -0
- package/dist/interact/dblClick.d.ts +51 -0
- package/dist/interact/dblClick.js +106 -0
- package/dist/interact/expandSession.d.ts +107 -0
- package/dist/interact/expandSession.js +281 -0
- package/dist/interact/hotkeyActions.d.ts +16 -0
- package/dist/interact/hotkeyActions.js +41 -0
- package/dist/interact/pathSession.d.ts +141 -0
- package/dist/interact/pathSession.js +480 -0
- package/dist/interact/pointer.d.ts +106 -0
- package/dist/interact/pointer.js +1594 -0
- package/dist/interact/pointerTablePuzzle.d.ts +100 -0
- package/dist/interact/pointerTablePuzzle.js +358 -0
- package/dist/interact/pointerTransform.d.ts +86 -0
- package/dist/interact/pointerTransform.js +440 -0
- package/dist/interact/pointerTypes.d.ts +577 -0
- package/dist/interact/pointerTypes.js +96 -0
- package/dist/interact/snapGuide.d.ts +127 -0
- package/dist/interact/snapGuide.js +376 -0
- package/dist/interact/tableCellEditing.d.ts +61 -0
- package/dist/interact/tableCellEditing.js +198 -0
- package/dist/interact/tableInteraction.d.ts +55 -0
- package/dist/interact/tableInteraction.js +212 -0
- package/dist/interact/textEditing.d.ts +77 -0
- package/dist/interact/textEditing.js +410 -0
- package/dist/interact/warpFacade.d.ts +36 -0
- package/dist/interact/warpFacade.js +51 -0
- package/dist/interact/warpSession.d.ts +191 -0
- package/dist/interact/warpSession.js +582 -0
- package/dist/interact/wheelScroll.d.ts +62 -0
- package/dist/interact/wheelScroll.js +72 -0
- package/dist/model/alphaHit.d.ts +59 -0
- package/dist/model/alphaHit.js +262 -0
- package/dist/model/animation.d.ts +228 -0
- package/dist/model/animation.js +510 -0
- package/dist/model/arcLayout.d.ts +54 -0
- package/dist/model/arcLayout.js +307 -0
- package/dist/model/arcTextTransform.d.ts +7 -0
- package/dist/model/arcTextTransform.js +87 -0
- package/dist/model/compatEvents.d.ts +135 -0
- package/dist/model/compatEvents.js +73 -0
- package/dist/model/compatObject.d.ts +716 -0
- package/dist/model/compatObject.js +1579 -0
- package/dist/model/cropEdit.d.ts +118 -0
- package/dist/model/cropEdit.js +497 -0
- package/dist/model/document.d.ts +294 -0
- package/dist/model/document.js +572 -0
- package/dist/model/expandEdit.d.ts +93 -0
- package/dist/model/expandEdit.js +161 -0
- package/dist/model/fabricPrimitives.d.ts +156 -0
- package/dist/model/fabricPrimitives.js +9 -0
- package/dist/model/filler.d.ts +53 -0
- package/dist/model/filler.js +1 -0
- package/dist/model/groupAssemble.d.ts +7 -0
- package/dist/model/groupAssemble.js +98 -0
- package/dist/model/groupChildMove.d.ts +46 -0
- package/dist/model/groupChildMove.js +145 -0
- package/dist/model/groupResize.d.ts +78 -0
- package/dist/model/groupResize.js +108 -0
- package/dist/model/handles.d.ts +40 -0
- package/dist/model/handles.js +29 -0
- package/dist/model/imageMaskBake.d.ts +75 -0
- package/dist/model/imageMaskBake.js +136 -0
- package/dist/model/lineConstruct.d.ts +13 -0
- package/dist/model/lineConstruct.js +79 -0
- package/dist/model/maskCropEdit.d.ts +64 -0
- package/dist/model/maskCropEdit.js +90 -0
- package/dist/model/pathBackground.d.ts +51 -0
- package/dist/model/pathBackground.js +196 -0
- package/dist/model/pathConstruct.d.ts +27 -0
- package/dist/model/pathConstruct.js +55 -0
- package/dist/model/pathEdit.d.ts +114 -0
- package/dist/model/pathEdit.js +378 -0
- package/dist/model/puzzleEdit.d.ts +136 -0
- package/dist/model/puzzleEdit.js +742 -0
- package/dist/model/puzzleLayout.d.ts +91 -0
- package/dist/model/puzzleLayout.js +143 -0
- package/dist/model/scene.d.ts +1004 -0
- package/dist/model/scene.js +2841 -0
- package/dist/model/schema.d.ts +290 -0
- package/dist/model/schema.js +13 -0
- package/dist/model/shapePath.d.ts +14 -0
- package/dist/model/shapePath.js +136 -0
- package/dist/model/tableEdit.d.ts +196 -0
- package/dist/model/tableEdit.js +751 -0
- package/dist/model/tableFacade.d.ts +6 -0
- package/dist/model/tableFacade.js +808 -0
- package/dist/model/tableLayout.d.ts +85 -0
- package/dist/model/tableLayout.js +193 -0
- package/dist/model/textLayout.d.ts +357 -0
- package/dist/model/textLayout.js +822 -0
- package/dist/render/GLBlend.d.ts +40 -0
- package/dist/render/GLBlend.js +270 -0
- package/dist/render/GLContext.d.ts +25 -0
- package/dist/render/GLContext.js +48 -0
- package/dist/render/GLIsolate.d.ts +46 -0
- package/dist/render/GLIsolate.js +188 -0
- package/dist/render/GLTextureCache.d.ts +86 -0
- package/dist/render/GLTextureCache.js +187 -0
- package/dist/render/GlyphAtlas.d.ts +151 -0
- package/dist/render/GlyphAtlas.js +404 -0
- package/dist/render/MeshBatch.d.ts +123 -0
- package/dist/render/MeshBatch.js +416 -0
- package/dist/render/QuadBatch.d.ts +138 -0
- package/dist/render/QuadBatch.js +490 -0
- package/dist/render/SceneRenderer.d.ts +186 -0
- package/dist/render/SceneRenderer.js +700 -0
- package/dist/render/SourceManager.d.ts +121 -0
- package/dist/render/SourceManager.js +505 -0
- package/dist/render/TextRenderer.d.ts +140 -0
- package/dist/render/TextRenderer.js +795 -0
- package/dist/render/arcText.d.ts +40 -0
- package/dist/render/arcText.js +187 -0
- package/dist/render/batchOrder.d.ts +105 -0
- package/dist/render/batchOrder.js +168 -0
- package/dist/render/blendMode.d.ts +35 -0
- package/dist/render/blendMode.js +142 -0
- package/dist/render/clipMaskPainter.d.ts +17 -0
- package/dist/render/clipMaskPainter.js +121 -0
- package/dist/render/fontInvalidation.d.ts +25 -0
- package/dist/render/fontInvalidation.js +77 -0
- package/dist/render/fontInvalidationController.d.ts +76 -0
- package/dist/render/fontInvalidationController.js +114 -0
- package/dist/render/frameOverlay.d.ts +62 -0
- package/dist/render/frameOverlay.js +124 -0
- package/dist/render/framePipeline.d.ts +99 -0
- package/dist/render/framePipeline.js +95 -0
- package/dist/render/gpuFilterPasses.d.ts +25 -0
- package/dist/render/gpuFilterPasses.js +883 -0
- package/dist/render/gpuFx.d.ts +136 -0
- package/dist/render/gpuFx.js +614 -0
- package/dist/render/gpuImageFilters.d.ts +59 -0
- package/dist/render/gpuImageFilters.js +289 -0
- package/dist/render/gpuShadow.d.ts +52 -0
- package/dist/render/gpuShadow.js +289 -0
- package/dist/render/gradientRamp.d.ts +45 -0
- package/dist/render/gradientRamp.js +74 -0
- package/dist/render/imageEffects.d.ts +123 -0
- package/dist/render/imageEffects.js +390 -0
- package/dist/render/maskRender.d.ts +92 -0
- package/dist/render/maskRender.js +224 -0
- package/dist/render/progressiveLoad.d.ts +45 -0
- package/dist/render/progressiveLoad.js +86 -0
- package/dist/render/psdEffectPainter.d.ts +34 -0
- package/dist/render/psdEffectPainter.js +309 -0
- package/dist/render/puzzleClamp.d.ts +28 -0
- package/dist/render/puzzleClamp.js +30 -0
- package/dist/render/retainedFrame.d.ts +85 -0
- package/dist/render/retainedFrame.js +193 -0
- package/dist/render/ruler.d.ts +90 -0
- package/dist/render/ruler.js +287 -0
- package/dist/render/scrollText.d.ts +65 -0
- package/dist/render/scrollText.js +261 -0
- package/dist/render/selectionOverlay.d.ts +318 -0
- package/dist/render/selectionOverlay.js +921 -0
- package/dist/render/shaders.d.ts +38 -0
- package/dist/render/shaders.js +314 -0
- package/dist/render/shadowBake.d.ts +43 -0
- package/dist/render/shadowBake.js +202 -0
- package/dist/render/shadowPainter.d.ts +13 -0
- package/dist/render/shadowPainter.js +80 -0
- package/dist/render/sharedGpuPainters.d.ts +25 -0
- package/dist/render/sharedGpuPainters.js +40 -0
- package/dist/render/sourceHydration.d.ts +12 -0
- package/dist/render/sourceHydration.js +48 -0
- package/dist/render/strokeBake.d.ts +85 -0
- package/dist/render/strokeBake.js +515 -0
- package/dist/render/strokePainter.d.ts +28 -0
- package/dist/render/strokePainter.js +119 -0
- package/dist/render/strokeSdf.d.ts +35 -0
- package/dist/render/strokeSdf.js +74 -0
- package/dist/render/svgRaster.d.ts +46 -0
- package/dist/render/svgRaster.js +71 -0
- package/dist/render/tableOverlay.d.ts +38 -0
- package/dist/render/tableOverlay.js +233 -0
- package/dist/render/textHighlight.d.ts +117 -0
- package/dist/render/textHighlight.js +380 -0
- package/dist/render/textureUpload.d.ts +18 -0
- package/dist/render/textureUpload.js +26 -0
- package/dist/render/verticalText.d.ts +38 -0
- package/dist/render/verticalText.js +63 -0
- package/dist/render/warpText.d.ts +58 -0
- package/dist/render/warpText.js +380 -0
- package/dist/render/workspaceBackdrop.d.ts +35 -0
- package/dist/render/workspaceBackdrop.js +71 -0
- package/dist/render/workspaceChecker.d.ts +54 -0
- package/dist/render/workspaceChecker.js +170 -0
- package/dist/render/workspaceGlow.d.ts +51 -0
- package/dist/render/workspaceGlow.js +171 -0
- package/package.json +44 -3
|
@@ -0,0 +1,822 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* fabric 文本排版算法的纯函数移植(main-webgl2 Phase 2a)。
|
|
3
|
+
*
|
|
4
|
+
* 与 fabric 6.9.0 的 Text/Textbox 逐公式对齐(源码精读结论):
|
|
5
|
+
* - 度量:共享 ctx 上以 CACHE_FONT_SIZE=400px 度量并按 fontSize/400 缩放,
|
|
6
|
+
* kernedWidth 用「前字+当前字 couple 度量 − 前字宽度」实现字偶字距(_measureChar);
|
|
7
|
+
* couple 只在前后字字体声明(含逐字 fontSize/fontFamily/weight/style)一致时生效
|
|
8
|
+
* (fabric stylesAreEqual = 实际字号下的 fontDeclaration 逐串相等)
|
|
9
|
+
* - charSpacing:fontSize×charSpacing/1000 同时加到 width 与 kernedWidth;
|
|
10
|
+
* 行宽额外减去末尾一次(measureLine)
|
|
11
|
+
* - 换行(Textbox):wordSplit 按 /[ \t\r]/ 分词 → 逐词 grapheme 度量(样式按
|
|
12
|
+
* 原始行/原始偏移解析)→ _wrapLine 的累加/回退状态机(含 infix 空格按其
|
|
13
|
+
* 原始偏移样式度量与 additionalSpace 的加减)
|
|
14
|
+
* - 行高:impl = 行内逐字 max(fontSize)×1.13(getHeightOfLineImpl,空行取行首
|
|
15
|
+
* 原始位样式);calcTextHeight 末行不乘 lineHeight,其余行乘(fabric 历史语义)
|
|
16
|
+
* - 字符盒 left:prev.left + prev.width + kernedWidth − width(_getGraphemeBox)
|
|
17
|
+
* - 行偏移:center=(W−w)/2、right=W−w、left/justify=0(ltr 子集)
|
|
18
|
+
* - 字体声明:`${fontStyle} ${fontWeight} ${size}px ${family}`,非 generic 字体加引号
|
|
19
|
+
*
|
|
20
|
+
* 逐字度量(P2-2):styles 里 fontSize/fontFamily/fontWeight/fontStyle 参与
|
|
21
|
+
* 度量(getCompleteStyleDeclaration 合并语义);fill/textBackgroundColor 等
|
|
22
|
+
* 视觉属性照旧透传。deltaY 仍属后续阶段。
|
|
23
|
+
* 描边(P2-3):stroke/strokeWidth 逐字覆写只影响渲染(TextStrokeDecl),
|
|
24
|
+
* 与 fabric 一致不进度量——盒宽/行高/换行结果与无描边时逐值相同。
|
|
25
|
+
* 装饰线(P2-4):underline/overline/linethrough 不进度量,渲染侧由
|
|
26
|
+
* lineDecorationRuns 按 fabric _renderTextDecoration 的 run 状态机产出线段 quad;
|
|
27
|
+
* 颜色逐字跟 fill(fabric ctx.fillStyle = getValueOfPropertyAt(FILL) 同语义)。
|
|
28
|
+
* Phase 2a 范围注记:ltr 子集(rtl 反向对齐不做);justify 全家已实现
|
|
29
|
+
* (enlargeSpaces 移植:空格均摊加宽 + 字符盒平移,纯 justify 含末行、
|
|
30
|
+
* justify-left/center/right 变体段末行豁免并按对应方式收尾),grapheme
|
|
31
|
+
* 切分用码点级(CJK/拉丁与 fabric 一致,emoji 簇差异已知,后续对齐)。
|
|
32
|
+
* splitByGrapheme(P4-4 表格单元格模式):LayoutOptions.splitByGrapheme ——
|
|
33
|
+
* 词=单字素、无 infix 空格、样式映射不吃空格(fabric 三处守卫逐条对齐)。
|
|
34
|
+
* 竖排(P4-5c):LayoutOptions.vertical —— 应用 Textbox vertical:true 的
|
|
35
|
+
* 内部坐标系移植(fabric 竖排 = 横排布局 + 对象 angle:90 + CJK 类字形逆旋转,
|
|
36
|
+
* 引擎只移植 advance/尺寸/装饰线口径,姿态在渲染侧);四套 advance 各站点
|
|
37
|
+
* 照抄互不通用:行宽 CJK 用盒高拉丁用 width、渲染 advance CJK 用盒高+spacing
|
|
38
|
+
* 拉丁用 width、装饰线 CJK 用盒高拉丁用 kernedWidth、calcTextWidth 双类都
|
|
39
|
+
* 加 space;换行判定轴不覆写(fabric 原样近似)。
|
|
40
|
+
* 布局级 parity 由 e2e 直接对比 fabric Textbox 的 _textLines/__lineWidths/height。
|
|
41
|
+
*/
|
|
42
|
+
/** fabric CACHE_FONT_SIZE:所有字体度量在固定字号下做缓存 */
|
|
43
|
+
export const CACHE_FONT_SIZE = 400;
|
|
44
|
+
/** fabric _fontSizeMult:行高实现值 = max(fontSize) × 1.13 */
|
|
45
|
+
export const FONT_SIZE_MULT = 1.13;
|
|
46
|
+
/** fabric _fontSizeFraction:基线上移比例——首基线 = impl×(1−0.222),实测自
|
|
47
|
+
* fillText 拦截(两行文本 y=−h/2+Σimpl×lineHeight+impl×0.778 逐点吻合) */
|
|
48
|
+
export const FONT_SIZE_FRACTION = 0.222;
|
|
49
|
+
/** fabric Textbox 默认 dynamicMinWidth(换行最大宽下限之一) */
|
|
50
|
+
export const DYNAMIC_MIN_WIDTH = 2;
|
|
51
|
+
/** fabric textDefaultValues.offsets:装饰线 y = 基线 + offsetY ×(run 起始字符 fontSize) */
|
|
52
|
+
export const DECORATION_OFFSETS = { underline: 0.1, linethrough: -0.28167, overline: -0.81333 };
|
|
53
|
+
/** fabric textDecorationThickness 类缺省(对象 fontSize 的千分比) */
|
|
54
|
+
export const DEFAULT_DECORATION_THICKNESS = 66.667;
|
|
55
|
+
/**
|
|
56
|
+
* 竖排字符类判定(P4-5c)。isLatin 是应用 TextCommon.ts:120 三 regex 的引擎
|
|
57
|
+
* 副本(空格/数字/半角标点都算拉丁——影响 advance 与姿态分支);符号集是
|
|
58
|
+
* 应用 configs/canvas.ts ChineseSymbols/ChineseBrackets 的副本。引擎不 import
|
|
59
|
+
* 应用层(先例:arcLayout.ts 复制 angularDiffSigned),上游改列表时须同步。
|
|
60
|
+
*/
|
|
61
|
+
const LATIN_LETTER = /[a-zA-Z.\s]+/;
|
|
62
|
+
const LATIN_NUM = /[0-9]/;
|
|
63
|
+
// 字符集与应用 TextCommon 的 SYMBOL_REGX 逐字符相同(仅去掉字符类内的冗余转义过 lint)
|
|
64
|
+
const LATIN_SYMBOL = /[()[\]{}></:;:,.?!]/;
|
|
65
|
+
export const isLatinChar = (ch) => LATIN_LETTER.test(ch) || LATIN_NUM.test(ch) || LATIN_SYMBOL.test(ch);
|
|
66
|
+
const CHINESE_SYMBOLS = new Set([
|
|
67
|
+
'ー',
|
|
68
|
+
'「',
|
|
69
|
+
'」',
|
|
70
|
+
'『',
|
|
71
|
+
'』',
|
|
72
|
+
'(',
|
|
73
|
+
')',
|
|
74
|
+
'〔',
|
|
75
|
+
'〕',
|
|
76
|
+
'[',
|
|
77
|
+
']',
|
|
78
|
+
'{',
|
|
79
|
+
'}',
|
|
80
|
+
'⦅',
|
|
81
|
+
'⦆',
|
|
82
|
+
'〈',
|
|
83
|
+
'〉',
|
|
84
|
+
'《',
|
|
85
|
+
'》',
|
|
86
|
+
'【',
|
|
87
|
+
'】',
|
|
88
|
+
'〖',
|
|
89
|
+
'〗',
|
|
90
|
+
'〘',
|
|
91
|
+
'〙',
|
|
92
|
+
'〚',
|
|
93
|
+
'〛',
|
|
94
|
+
'゛',
|
|
95
|
+
'゜',
|
|
96
|
+
'。',
|
|
97
|
+
'、',
|
|
98
|
+
'・',
|
|
99
|
+
'゠',
|
|
100
|
+
'=',
|
|
101
|
+
'〜',
|
|
102
|
+
'…',
|
|
103
|
+
'•',
|
|
104
|
+
'‥',
|
|
105
|
+
'◦',
|
|
106
|
+
'﹅',
|
|
107
|
+
'﹆'
|
|
108
|
+
]);
|
|
109
|
+
const CHINESE_BRACKETS = new Set(['「', '」', '『', '』', '〔', '〕', '[', ']', '{', '}', '⦅', '⦆', '〈', '〉', '《', '》', '【', '】', '〖', '〗', '〘', '〙', '〚', '〛', '(', ')']);
|
|
110
|
+
export const verticalCharClass = (ch) => {
|
|
111
|
+
if (isLatinChar(ch))
|
|
112
|
+
return 'latin';
|
|
113
|
+
if (CHINESE_BRACKETS.has(ch))
|
|
114
|
+
return 'bracket';
|
|
115
|
+
if (CHINESE_SYMBOLS.has(ch))
|
|
116
|
+
return 'symbol';
|
|
117
|
+
return 'cjk';
|
|
118
|
+
};
|
|
119
|
+
/** 竖排逐字渲染 advance(fabric _renderChars 2459-2478 per-char 口径):
|
|
120
|
+
* 拉丁 = charBox.width(已含 charSpacing),CJK 族 = charBox.height(=逐字
|
|
121
|
+
* fontSize) + verticalCharSpacing(对象 fontSize × charSpacing / 1000,
|
|
122
|
+
* fabric _getWidthOfCharSpacing——CJK 的 spacing 不在 kernedWidth 里须手动加) */
|
|
123
|
+
export const verticalRenderAdvance = (ch, box, verticalCharSpacing) => (isLatinChar(ch) ? box.width : box.height + verticalCharSpacing);
|
|
124
|
+
/** 竖排旋转类的 translate 锚点偏移(fabric _renderStr 2728/2734 经验魔数照抄):
|
|
125
|
+
* x 恒 +fontSize/1.1;y 普通CJK = +charBox.height/2 − fontSize/3.14,
|
|
126
|
+
* 标点 = −fontSize/3.14(句读推向字面上侧)。仅 symbol/cjk 类消费。 */
|
|
127
|
+
export const verticalAnchorShiftX = (fontSize) => fontSize / 1.1;
|
|
128
|
+
export const verticalAnchorShiftY = (klass, box, fontSize) => (klass === 'symbol' ? -fontSize / 3.14 : box.height / 2 - fontSize / 3.14);
|
|
129
|
+
/** K-line 垂直偏移的逐字抖动量(fabric Textbox._renderStr 2714-2757,应用
|
|
130
|
+
* 「上下上下」变形):偶字不动、奇字偏移 (textOffsetY/300)·fontSize/2——
|
|
131
|
+
* textOffsetY∈[-300,300](面板滑杆界),正下负上;fontSize 取该字有效字号
|
|
132
|
+
* (逐字覆写优先)。globalCharIndex 按视觉行字素数跨行累计(fabric 对
|
|
133
|
+
* _textLines 逐行求和口径,不含 \n 分隔符——勿用 styleMap.flatStart,它含
|
|
134
|
+
* 分隔符会错位奇偶)。竖排同式偏移落在锚点 y 上(fabric 对 charTop 加偏移
|
|
135
|
+
* 后再算旋转锚点,对象经 angle:90 后视觉呈左右交错)。 */
|
|
136
|
+
export const charJitterY = (textOffsetY, fontSize, globalCharIndex) => {
|
|
137
|
+
if (textOffsetY === 0)
|
|
138
|
+
return 0;
|
|
139
|
+
return globalCharIndex % 2 === 0 ? 0 : (textOffsetY / 300) * fontSize * 0.5;
|
|
140
|
+
};
|
|
141
|
+
/** 节点级抖动上界(虚线烘焙/特效剪影的画布余量用):|textOffsetY|/300·0.5·
|
|
142
|
+
* 全字最大有效字号——逐字覆写 fontSize 会抬高峰值,保守取 max。 */
|
|
143
|
+
export const maxCharJitterY = (textOffsetY, style, lines) => {
|
|
144
|
+
if (textOffsetY === 0)
|
|
145
|
+
return 0;
|
|
146
|
+
let max = style.fontSize;
|
|
147
|
+
for (const line of lines) {
|
|
148
|
+
for (const o of line.charStyles) {
|
|
149
|
+
const size = o?.fontSize;
|
|
150
|
+
if (typeof size === 'number' && size > max)
|
|
151
|
+
max = size;
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
return (Math.abs(textOffsetY) / 300) * max * 0.5;
|
|
155
|
+
};
|
|
156
|
+
let sharedInkCtx = null;
|
|
157
|
+
/** 缺省墨迹度量:共享 canvas;无 2d 上下文/缺实际边界度量时回退 em 比例
|
|
158
|
+
* (同 glyphAtlas 的 0.88/0.24 口径——right/center 对齐量略保守收入盒内) */
|
|
159
|
+
export const defaultInkMeasure = (decl, text, fontSize) => {
|
|
160
|
+
const fallback = { left: 0, right: 0, ascent: fontSize * 0.88, descent: fontSize * 0.24 };
|
|
161
|
+
if (typeof document === 'undefined')
|
|
162
|
+
return fallback;
|
|
163
|
+
if (!sharedInkCtx) {
|
|
164
|
+
const canvas = document.createElement('canvas');
|
|
165
|
+
canvas.width = 8;
|
|
166
|
+
canvas.height = 8;
|
|
167
|
+
sharedInkCtx = canvas.getContext('2d');
|
|
168
|
+
}
|
|
169
|
+
if (!sharedInkCtx)
|
|
170
|
+
return fallback;
|
|
171
|
+
sharedInkCtx.font = decl;
|
|
172
|
+
const m = sharedInkCtx.measureText(text);
|
|
173
|
+
if (m.actualBoundingBoxAscent === undefined || m.actualBoundingBoxDescent === undefined)
|
|
174
|
+
return fallback;
|
|
175
|
+
return {
|
|
176
|
+
left: -(m.actualBoundingBoxLeft ?? 0),
|
|
177
|
+
right: m.actualBoundingBoxRight ?? 0,
|
|
178
|
+
ascent: m.actualBoundingBoxAscent,
|
|
179
|
+
descent: m.actualBoundingBoxDescent
|
|
180
|
+
};
|
|
181
|
+
};
|
|
182
|
+
const inkMetricsByMeasure = new WeakMap();
|
|
183
|
+
/** 度量缓存注册表:WeakMap 键不可枚举,登记创建出的缓存表供迟到失效总清 */
|
|
184
|
+
const measureCacheRegistry = new Set();
|
|
185
|
+
const measureInk = (inkMeasure, style, char) => {
|
|
186
|
+
let byDecl = inkMetricsByMeasure.get(inkMeasure);
|
|
187
|
+
if (!byDecl) {
|
|
188
|
+
byDecl = new Map();
|
|
189
|
+
inkMetricsByMeasure.set(inkMeasure, byDecl);
|
|
190
|
+
measureCacheRegistry.add(byDecl);
|
|
191
|
+
}
|
|
192
|
+
const key = `${fontDeclaration(style, style.fontSize)}|${char}`;
|
|
193
|
+
let m = byDecl.get(key);
|
|
194
|
+
if (!m) {
|
|
195
|
+
m = inkMeasure(fontDeclaration(style, style.fontSize), char, style.fontSize);
|
|
196
|
+
byDecl.set(key, m);
|
|
197
|
+
}
|
|
198
|
+
return m;
|
|
199
|
+
};
|
|
200
|
+
/**
|
|
201
|
+
* 竖排行沿流向(内部坐标 +x,即对象 angle:90 后的屏幕 +y)的墨迹范围,相对
|
|
202
|
+
* 行首笔位。渲染链口径(与 renderGlyphPass / verticalGlyphOps 同一摆位公式):
|
|
203
|
+
* - 旋转类(symbol/cjk):锚点 = 笔位 + fontSize/1.1,rotate(−π/2) 后墨迹相对
|
|
204
|
+
* 锚点 = [−ascent, +descent](GL cell 摆位 relY 投影;40px「中」探针实测
|
|
205
|
+
* [3,41] vs 36.36−asc34/+desc6 逐像素吻合);
|
|
206
|
+
* - 平画类(latin/bracket):墨迹相对笔位 = [ink.left, ink.right],latin 画位
|
|
207
|
+
* 再前移 kerned−width(fabric signWidth)。
|
|
208
|
+
* 供竖排 center/right 对齐量消费(见 layoutTextbox 行偏移处的补丁注释);
|
|
209
|
+
* left 对齐不调用(offset 恒 0,与 fabric 逐像素一致的主路径不动)。
|
|
210
|
+
*/
|
|
211
|
+
export const verticalLineInkRun = (line, style, inkMeasure = defaultInkMeasure) => {
|
|
212
|
+
const vSpacing = style.charSpacing ? (style.fontSize * style.charSpacing) / 1000 : 0;
|
|
213
|
+
let penV = 0;
|
|
214
|
+
let start = 0;
|
|
215
|
+
let end = 0;
|
|
216
|
+
for (let i = 0; i < line.graphemes.length; i++) {
|
|
217
|
+
const g = line.graphemes[i];
|
|
218
|
+
const box = line.boxes[i];
|
|
219
|
+
const glyphStyle = resolveCharStyle(style, line.charStyles[i]);
|
|
220
|
+
const klass = verticalCharClass(g);
|
|
221
|
+
let lead;
|
|
222
|
+
let trail;
|
|
223
|
+
if (klass === 'latin' || klass === 'bracket') {
|
|
224
|
+
const shift = klass === 'latin' ? box.kernedWidth - box.width : 0;
|
|
225
|
+
const ink = measureInk(inkMeasure, glyphStyle, g);
|
|
226
|
+
lead = ink.left + shift;
|
|
227
|
+
trail = ink.right + shift;
|
|
228
|
+
}
|
|
229
|
+
else {
|
|
230
|
+
const ink = measureInk(inkMeasure, glyphStyle, g);
|
|
231
|
+
lead = verticalAnchorShiftX(glyphStyle.fontSize) - ink.ascent;
|
|
232
|
+
trail = verticalAnchorShiftX(glyphStyle.fontSize) + ink.descent;
|
|
233
|
+
}
|
|
234
|
+
if (i === 0)
|
|
235
|
+
start = lead;
|
|
236
|
+
end = penV + trail;
|
|
237
|
+
penV += verticalRenderAdvance(g, box, vSpacing);
|
|
238
|
+
}
|
|
239
|
+
return { start, end };
|
|
240
|
+
};
|
|
241
|
+
export const GENERIC_FONTS = new Set([
|
|
242
|
+
'serif',
|
|
243
|
+
'sans-serif',
|
|
244
|
+
'monospace',
|
|
245
|
+
'cursive',
|
|
246
|
+
'fantasy',
|
|
247
|
+
'system-ui',
|
|
248
|
+
'ui-serif',
|
|
249
|
+
'ui-sans-serif',
|
|
250
|
+
'ui-monospace',
|
|
251
|
+
'ui-rounded',
|
|
252
|
+
'math',
|
|
253
|
+
'emoji',
|
|
254
|
+
'fangsong',
|
|
255
|
+
'inherit',
|
|
256
|
+
'initial',
|
|
257
|
+
'unset'
|
|
258
|
+
]);
|
|
259
|
+
/** 与 fabric _getFontDeclaration 同构的字体声明(forMeasuring 时用 CACHE_FONT_SIZE) */
|
|
260
|
+
export const fontDeclaration = (style, size) => {
|
|
261
|
+
const family = style.fontFamily;
|
|
262
|
+
const parsed = family.includes("'") || family.includes('"') || family.includes(',') || GENERIC_FONTS.has(family.toLowerCase()) ? family : `"${family}"`;
|
|
263
|
+
return `${style.fontStyle ?? 'normal'} ${style.fontWeight ?? 'normal'} ${size}px ${parsed}`;
|
|
264
|
+
};
|
|
265
|
+
/** 前缀串(fabric Textbox._getParagraphPrefix:id=0 按视觉行号编号,含空行) */
|
|
266
|
+
export const paragraphPrefixOf = (paragraph, lineIndex) => {
|
|
267
|
+
if (!paragraph)
|
|
268
|
+
return undefined;
|
|
269
|
+
if (paragraph.id === 0)
|
|
270
|
+
return `${lineIndex + 1}`;
|
|
271
|
+
return paragraph.content || undefined;
|
|
272
|
+
};
|
|
273
|
+
/**
|
|
274
|
+
* 逐字描边解析(fabric getCompleteStyleDeclaration 语义:逐字覆写优先、对象级兜底)。
|
|
275
|
+
* 对象无描边但逐字给了 stroke+strokeWidth 时同样成声明——fabric 的
|
|
276
|
+
* `_renderTextStroke` 只因 isEmptyStyles() 才整体跳过。
|
|
277
|
+
*
|
|
278
|
+
* 已知缺口:strokeDashArray/strokeDashOffset 不在此列——fabric 按行(无逐字样式时)
|
|
279
|
+
* 或按 run 一次 strokeText,虚线相位在整行/整 run 内连续;引擎图集是逐字形 cell,
|
|
280
|
+
* 相位会逐字重来。虚线描边留待 P3-2(描边展开与对齐)按 run 级栅格化处理。
|
|
281
|
+
*/
|
|
282
|
+
export const resolveStrokeDecl = (base, o) => {
|
|
283
|
+
const color = o?.stroke ?? base?.color;
|
|
284
|
+
const width = o?.strokeWidth ?? base?.width;
|
|
285
|
+
if (!color || !width)
|
|
286
|
+
return null;
|
|
287
|
+
return {
|
|
288
|
+
color,
|
|
289
|
+
width,
|
|
290
|
+
lineJoin: base?.lineJoin,
|
|
291
|
+
miterLimit: base?.miterLimit,
|
|
292
|
+
lineCap: base?.lineCap
|
|
293
|
+
};
|
|
294
|
+
};
|
|
295
|
+
/**
|
|
296
|
+
* 行末 x(相对行首):fabric `_measureLine` 的**行末哨兵盒**口径 =
|
|
297
|
+
* 末字符盒 left + width(width 含行尾 charSpacing)。justify 拉伸行的空格
|
|
298
|
+
* 加宽/平移同样作用于末盒 → 端点落在拉伸末端(fabric enlargeSpaces 对哨兵盒
|
|
299
|
+
* 的 acc 平移同语义)。`line.width` 保持自然行宽(fabric `__lineWidths` 口径,
|
|
300
|
+
* 布局对拍用),行末光标/选区端点一律走本口径,两者不可混用。
|
|
301
|
+
*/
|
|
302
|
+
export const lineEndOffset = (line) => {
|
|
303
|
+
const last = line.boxes[line.boxes.length - 1];
|
|
304
|
+
return last ? last.left + last.width : 0;
|
|
305
|
+
};
|
|
306
|
+
/**
|
|
307
|
+
* 行首列表前缀的 canvas 绘制(fabric _renderChars 前缀块 2408-2444 的等价抽取):
|
|
308
|
+
* 在 (x, baselineY) 以行首字符的字体声明 fillText/strokeText 前缀串(一次出笔,
|
|
309
|
+
* 前缀内 kerning 保留——GL 图集管线的逐字 advance 是其近似)。竖排同样在此
|
|
310
|
+
* 出笔(fabric 前缀块无竖排分支:不旋转、画在列首行基线)。返回前缀宽
|
|
311
|
+
* (无前缀 = 0),供调用方把正文 run 起点 `left += prefixWidth`(fabric 同款)。
|
|
312
|
+
*/
|
|
313
|
+
export const drawParagraphPrefix = (ctx, line, x, baselineY, opts = {}) => {
|
|
314
|
+
const prefix = line.prefix;
|
|
315
|
+
if (!prefix)
|
|
316
|
+
return 0;
|
|
317
|
+
const prevFont = ctx.font;
|
|
318
|
+
ctx.font = fontDeclaration(prefix.style, prefix.style.fontSize);
|
|
319
|
+
if (opts.fill) {
|
|
320
|
+
if (opts.fill !== 'keep')
|
|
321
|
+
ctx.fillStyle = opts.fill;
|
|
322
|
+
ctx.fillText(prefix.text, x, baselineY);
|
|
323
|
+
}
|
|
324
|
+
if (opts.stroke && opts.stroke.width > 0) {
|
|
325
|
+
ctx.lineWidth = opts.stroke.width;
|
|
326
|
+
ctx.strokeStyle = opts.stroke.color;
|
|
327
|
+
ctx.strokeText(prefix.text, x, baselineY);
|
|
328
|
+
}
|
|
329
|
+
ctx.font = prevFont;
|
|
330
|
+
return prefix.width;
|
|
331
|
+
};
|
|
332
|
+
/**
|
|
333
|
+
* 单行装饰线 run(P2-4,fabric `_renderTextDecoration` 状态机的纯函数移植)。
|
|
334
|
+
*
|
|
335
|
+
* 语义要点(源码逐行核对):
|
|
336
|
+
* - run 分段键 = (开关, fill, 逐字 fontSize, thickness)——任一变化即断段;
|
|
337
|
+
* 断段后的新 run 首字符贡献**普通 width**(非 kernedWidth,fabric 原样行为),
|
|
338
|
+
* 续段字符累加 kernedWidth;
|
|
339
|
+
* - 线宽 = **对象** fontSize × thickness‰ / 1000(fabric finalTickness 用 this.fontSize,
|
|
340
|
+
* 逐字大字号不会加粗装饰线);y 偏移的 size 乘数则取 run 起始字符的逐字 fontSize;
|
|
341
|
+
* - 末次 flush 的宽度额外扣除一次对象级 charSpacing(fillRect(boxWidth − charSpacing));
|
|
342
|
+
* - 开关/色/线宽任一为假则该段不产出(fabric lastDecoration && lastFill && lastTickness)。
|
|
343
|
+
* 行内逐字全 false 时返回空(fabric 整行跳过 styleHas 判定的视觉等价)。
|
|
344
|
+
* deltaY 参与 fabric 的分段键与 y 公式,引擎暂不支持逐字 deltaY(恒 0),后续接。
|
|
345
|
+
*/
|
|
346
|
+
export const lineDecorationRuns = (line, type, defaults) => {
|
|
347
|
+
const runs = [];
|
|
348
|
+
const objOn = !!defaults[type];
|
|
349
|
+
const valueAt = (i) => !!(line.charStyles[i]?.[type] ?? objOn);
|
|
350
|
+
if (!line.graphemes.some((_, i) => valueAt(i)))
|
|
351
|
+
return runs;
|
|
352
|
+
const fillAt = (i) => line.charStyles[i]?.fill ?? defaults.fill ?? '';
|
|
353
|
+
const ticknessAt = (i) => line.charStyles[i]?.textDecorationThickness ?? defaults.textDecorationThickness ?? DEFAULT_DECORATION_THICKNESS;
|
|
354
|
+
const sizeAt = (i) => line.charStyles[i]?.fontSize ?? defaults.fontSize;
|
|
355
|
+
const offsetY = DECORATION_OFFSETS[type];
|
|
356
|
+
const aligner = type === 'linethrough' ? 0.5 : type === 'overline' ? 1 : 0;
|
|
357
|
+
const spacingWidth = defaults.charSpacing ? (defaults.fontSize * defaults.charSpacing) / 1000 : 0;
|
|
358
|
+
const push = (on, fill, ticknessPermille, size, x, width) => {
|
|
359
|
+
if (!on || !fill || !ticknessPermille || width <= 0)
|
|
360
|
+
return;
|
|
361
|
+
const thickness = (defaults.fontSize * ticknessPermille) / 1000;
|
|
362
|
+
runs.push({ x, width, y: offsetY * size - aligner * thickness, thickness, color: fill });
|
|
363
|
+
};
|
|
364
|
+
let boxStart = 0;
|
|
365
|
+
let boxWidth = 0;
|
|
366
|
+
let lastOn = valueAt(0);
|
|
367
|
+
let lastFill = fillAt(0);
|
|
368
|
+
let lastTickness = ticknessAt(0);
|
|
369
|
+
let lastSize = sizeAt(0);
|
|
370
|
+
for (let j = 0; j < line.graphemes.length; j++) {
|
|
371
|
+
const on = valueAt(j);
|
|
372
|
+
const fill = fillAt(j);
|
|
373
|
+
const tickness = ticknessAt(j);
|
|
374
|
+
const size = sizeAt(j);
|
|
375
|
+
const box = line.boxes[j];
|
|
376
|
+
// P4-5c 竖排 advance(应用 Textbox 1261/1269):CJK 用盒高、拉丁用 kernedWidth
|
|
377
|
+
// ——与 _renderChars 的渲染 advance(width / height+spacing)是两套口径,各自照抄
|
|
378
|
+
const advance = defaults.vertical ? (isLatinChar(line.graphemes[j]) ? box.kernedWidth : box.height) : box.kernedWidth;
|
|
379
|
+
if ((on !== lastOn || fill !== lastFill || tickness !== lastTickness || size !== lastSize) && boxWidth > 0) {
|
|
380
|
+
push(lastOn, lastFill, lastTickness, lastSize, boxStart, boxWidth);
|
|
381
|
+
boxStart = box.left;
|
|
382
|
+
boxWidth = defaults.vertical ? advance : box.width;
|
|
383
|
+
lastOn = on;
|
|
384
|
+
lastFill = fill;
|
|
385
|
+
lastTickness = tickness;
|
|
386
|
+
lastSize = size;
|
|
387
|
+
}
|
|
388
|
+
else {
|
|
389
|
+
boxWidth += defaults.vertical ? advance : box.kernedWidth;
|
|
390
|
+
}
|
|
391
|
+
}
|
|
392
|
+
push(lastOn, lastFill, lastTickness, lastSize, boxStart, boxWidth - spacingWidth);
|
|
393
|
+
return runs;
|
|
394
|
+
};
|
|
395
|
+
let sharedCtx = null;
|
|
396
|
+
export const defaultMeasure = (decl, text) => {
|
|
397
|
+
if (typeof document === 'undefined')
|
|
398
|
+
return 0;
|
|
399
|
+
if (!sharedCtx) {
|
|
400
|
+
const canvas = document.createElement('canvas');
|
|
401
|
+
canvas.width = 8;
|
|
402
|
+
canvas.height = 8;
|
|
403
|
+
// jsdom 无 2d 上下文实现:度量退化为 0(单测走注入度量,不依赖真实字体)
|
|
404
|
+
sharedCtx = canvas.getContext('2d');
|
|
405
|
+
if (!sharedCtx)
|
|
406
|
+
return 0;
|
|
407
|
+
}
|
|
408
|
+
sharedCtx.font = decl;
|
|
409
|
+
return sharedCtx.measureText(text).width;
|
|
410
|
+
};
|
|
411
|
+
/** grapheme 切分:码点级(Intl.Segmenter 的 grapheme 级差异属后续对齐项) */
|
|
412
|
+
export const graphemeSplit = (value) => Array.from(value);
|
|
413
|
+
const wordSplit = (value) => value.split(/[ \t\r]/);
|
|
414
|
+
/** per-font 度量缓存(fabric cache.getFontCache 同语义:400px 下的 char/couple 宽) */
|
|
415
|
+
class FontMeasure {
|
|
416
|
+
decl;
|
|
417
|
+
measure;
|
|
418
|
+
charWidths = new Map();
|
|
419
|
+
coupleWidths = new Map();
|
|
420
|
+
constructor(decl, measure) {
|
|
421
|
+
this.decl = decl;
|
|
422
|
+
this.measure = measure;
|
|
423
|
+
}
|
|
424
|
+
charWidth(char) {
|
|
425
|
+
let w = this.charWidths.get(char);
|
|
426
|
+
if (w === undefined) {
|
|
427
|
+
w = this.measure(this.decl, char);
|
|
428
|
+
this.charWidths.set(char, w);
|
|
429
|
+
}
|
|
430
|
+
return w;
|
|
431
|
+
}
|
|
432
|
+
coupleWidth(prev, char) {
|
|
433
|
+
const key = prev + '\u0000' + char;
|
|
434
|
+
let w = this.coupleWidths.get(key);
|
|
435
|
+
if (w === undefined) {
|
|
436
|
+
w = this.measure(this.decl, prev + char);
|
|
437
|
+
this.coupleWidths.set(key, w);
|
|
438
|
+
}
|
|
439
|
+
return w;
|
|
440
|
+
}
|
|
441
|
+
}
|
|
442
|
+
const fontMeasuresByMeasure = new WeakMap();
|
|
443
|
+
const getFontMeasure = (decl, measure) => {
|
|
444
|
+
// 缓存按度量函数分组:生产恒用 defaultMeasure(单例),测试注入各自的 mock 互不串扰
|
|
445
|
+
let byDecl = fontMeasuresByMeasure.get(measure);
|
|
446
|
+
if (!byDecl) {
|
|
447
|
+
byDecl = new Map();
|
|
448
|
+
fontMeasuresByMeasure.set(measure, byDecl);
|
|
449
|
+
measureCacheRegistry.add(byDecl);
|
|
450
|
+
}
|
|
451
|
+
let fm = byDecl.get(decl);
|
|
452
|
+
if (!fm) {
|
|
453
|
+
fm = new FontMeasure(decl, measure);
|
|
454
|
+
byDecl.set(decl, fm);
|
|
455
|
+
}
|
|
456
|
+
return fm;
|
|
457
|
+
};
|
|
458
|
+
/** 字体迟到失效的度量总清(EngineCanvas.handleFontsLoaded 消费):字符宽/字距宽/墨迹
|
|
459
|
+
* 三类按「字体声明串」键的缓存全部作废。声明串不含字体加载态——同族从回退变真实后
|
|
460
|
+
* 键不变,旧缓存命中会把 fallback 宽度带进重排(字形换了但折行/行宽仍错,且字距
|
|
461
|
+
* couple 键按声明串命中无法自愈)。调用时机稀有(字体到达),粗粒度全清安全。 */
|
|
462
|
+
export const clearAllMeasureCaches = () => {
|
|
463
|
+
for (const cache of measureCacheRegistry)
|
|
464
|
+
cache.clear();
|
|
465
|
+
};
|
|
466
|
+
/** fabric getCompleteStyleDeclaration 同构:对象样式为底、逐字覆写只合并字体四件套
|
|
467
|
+
* (fill/textBackgroundColor 等视觉属性不影响度量,由渲染侧直接读 charStyles) */
|
|
468
|
+
const resolveCharStyle = (base, o) => {
|
|
469
|
+
if (!o || (o.fontSize === undefined && o.fontFamily === undefined && o.fontWeight === undefined && o.fontStyle === undefined))
|
|
470
|
+
return base;
|
|
471
|
+
return {
|
|
472
|
+
...base,
|
|
473
|
+
fontSize: o.fontSize ?? base.fontSize,
|
|
474
|
+
fontFamily: o.fontFamily ?? base.fontFamily,
|
|
475
|
+
fontWeight: o.fontWeight ?? base.fontWeight,
|
|
476
|
+
fontStyle: o.fontStyle ?? base.fontStyle
|
|
477
|
+
};
|
|
478
|
+
};
|
|
479
|
+
/** fabric _measureChar 的纯函数版:400px 度量 ×(fontSize/400);kerning couple 只在
|
|
480
|
+
* 前后字「实际字号下的字体声明」一致时生效(fabric stylesAreEqual 同语义——
|
|
481
|
+
* _getFontDeclaration 不带 forMeasuring 时含实际 fontSize,声明不同的两字间
|
|
482
|
+
* 不做 couple 度量,kernedWidth 退化为 width) */
|
|
483
|
+
const measureChar = (measure, style, char, prev, prevStyle) => {
|
|
484
|
+
const fm = getFontMeasure(fontDeclaration(style, CACHE_FONT_SIZE), measure);
|
|
485
|
+
const multiplier = style.fontSize / CACHE_FONT_SIZE;
|
|
486
|
+
const width = fm.charWidth(char);
|
|
487
|
+
let kernedWidth = width;
|
|
488
|
+
if (prev && prevStyle && fontDeclaration(prevStyle, prevStyle.fontSize) === fontDeclaration(style, style.fontSize)) {
|
|
489
|
+
kernedWidth = fm.coupleWidth(prev, char) - fm.charWidth(prev);
|
|
490
|
+
}
|
|
491
|
+
return { width: width * multiplier, kernedWidth: kernedWidth * multiplier };
|
|
492
|
+
};
|
|
493
|
+
const getWidthOfCharSpacing = (style) => (style.charSpacing ? (style.fontSize * style.charSpacing) / 1000 : 0);
|
|
494
|
+
/** fabric Textbox._wrapLine 状态机的忠实移植,返回 grapheme 行数组。
|
|
495
|
+
* infix 空格按其在原始行内的偏移查样式度量(fabric _measureWord([infix],
|
|
496
|
+
* lineIndex, offset)),offset 游走与 fabric 逐行累加(词长+1)同构。
|
|
497
|
+
* splitByGrapheme 模式(P4-4):infix=''(永不压入行)、infixWidth 恒 0——
|
|
498
|
+
* fabric `!splitByGrapheme` 的两处守卫同构 */
|
|
499
|
+
const wrapLine = (lineIndex, desiredWidth, wordsData, largestWordWidth, style, styleAt, measure, splitByGrapheme = false) => {
|
|
500
|
+
const additionalSpace = getWidthOfCharSpacing(style);
|
|
501
|
+
const graphemeLines = [];
|
|
502
|
+
const infix = ' ';
|
|
503
|
+
let lineWidth = 0;
|
|
504
|
+
let line = [];
|
|
505
|
+
let infixWidth = 0;
|
|
506
|
+
let lineJustStarted = true;
|
|
507
|
+
let offset = 0;
|
|
508
|
+
const maxWidth = Math.max(desiredWidth, largestWordWidth, DYNAMIC_MIN_WIDTH);
|
|
509
|
+
const data = wordsData[lineIndex];
|
|
510
|
+
for (let i = 0; i < data.length; i++) {
|
|
511
|
+
const word = data[i].graphemes;
|
|
512
|
+
const wordWidth = data[i].width;
|
|
513
|
+
offset += word.length;
|
|
514
|
+
lineWidth += (splitByGrapheme ? 0 : infixWidth) + wordWidth - additionalSpace;
|
|
515
|
+
if (lineWidth > maxWidth && !lineJustStarted) {
|
|
516
|
+
graphemeLines.push(line);
|
|
517
|
+
line = [];
|
|
518
|
+
lineWidth = wordWidth;
|
|
519
|
+
lineJustStarted = true;
|
|
520
|
+
}
|
|
521
|
+
else {
|
|
522
|
+
lineWidth += additionalSpace;
|
|
523
|
+
}
|
|
524
|
+
if (!lineJustStarted && !splitByGrapheme) {
|
|
525
|
+
line.push(infix);
|
|
526
|
+
}
|
|
527
|
+
line = line.concat(word);
|
|
528
|
+
// fabric _measureWord([infix], lineIndex, offset):_getGraphemeBox 会把
|
|
529
|
+
// charSpacing 加进 kernedWidth,故 infix 宽 = 空格宽 + charSpacing
|
|
530
|
+
infixWidth = splitByGrapheme ? 0 : measureChar(measure, styleAt(lineIndex, offset), infix).kernedWidth + additionalSpace;
|
|
531
|
+
offset++;
|
|
532
|
+
lineJustStarted = false;
|
|
533
|
+
}
|
|
534
|
+
if (data.length)
|
|
535
|
+
graphemeLines.push(line);
|
|
536
|
+
return graphemeLines;
|
|
537
|
+
};
|
|
538
|
+
/**
|
|
539
|
+
* 完整排版:text + 样式 → 行切分 + 字符盒 + 尺寸。
|
|
540
|
+
* 字符盒 left/width/kernedWidth 与 fabric __charBounds 同单位(对象本地坐标,
|
|
541
|
+
* fontSize 像素、scale=1),渲染侧直接消费。
|
|
542
|
+
* 逐字 fontSize/fontFamily/fontWeight/fontStyle 按原始行/字索引参与度量
|
|
543
|
+
* (换行期词宽、最终字符盒、行高 impl 全部走逐字样式)。
|
|
544
|
+
*/
|
|
545
|
+
export const layoutTextbox = (text, style, options = {}) => {
|
|
546
|
+
const measure = options.measure ?? defaultMeasure;
|
|
547
|
+
const inkMeasure = options.inkMeasure ?? defaultInkMeasure;
|
|
548
|
+
const rawStyles = options.styles;
|
|
549
|
+
const styleAt = (line, char) => resolveCharStyle(style, rawStyles?.[line]?.[char]);
|
|
550
|
+
const spacing = getWidthOfCharSpacing(style);
|
|
551
|
+
const lineHeightMult = style.lineHeight ?? 1.16;
|
|
552
|
+
// 词宽度(fabric _measureWord:Σ kernedWidth,skipLeft=true;样式按原始行/偏移解析)
|
|
553
|
+
const measureWord = (graphemes, rawLine, charOffset) => {
|
|
554
|
+
let width = 0;
|
|
555
|
+
let prev;
|
|
556
|
+
for (let i = 0; i < graphemes.length; i++) {
|
|
557
|
+
const info = measureChar(measure, styleAt(rawLine, charOffset + i), graphemes[i], prev, i > 0 ? styleAt(rawLine, charOffset + i - 1) : undefined);
|
|
558
|
+
width += info.kernedWidth + spacing;
|
|
559
|
+
prev = graphemes[i];
|
|
560
|
+
}
|
|
561
|
+
return width;
|
|
562
|
+
};
|
|
563
|
+
const rawLines = (text ?? '').split('\n');
|
|
564
|
+
let graphemeLines;
|
|
565
|
+
// fabric Textbox dynamicMinWidth 语义:最长「词」放不下时按词宽撑盒(不重排),
|
|
566
|
+
// CJK 整句无空格时整句即一个词——不补这条,盒宽与 fabric 系统性不一致
|
|
567
|
+
let largestWordWidth = 0;
|
|
568
|
+
if (options.boxWidth == null) {
|
|
569
|
+
graphemeLines = rawLines.map((l) => graphemeSplit(l));
|
|
570
|
+
}
|
|
571
|
+
else if (options.splitByGrapheme) {
|
|
572
|
+
// fabric getGraphemeDataForRender 的 splitByGrapheme 分支:词 = 单字素、
|
|
573
|
+
// infix='',空行落 [{word:[],width:0}] 单条目(换行后仍产出一条空行)
|
|
574
|
+
const wordsData = [];
|
|
575
|
+
for (let lineIndex = 0; lineIndex < rawLines.length; lineIndex++) {
|
|
576
|
+
const graphemes = graphemeSplit(rawLines[lineIndex]);
|
|
577
|
+
if (graphemes.length === 0) {
|
|
578
|
+
wordsData.push([{ graphemes: [], width: 0, charOffset: 0 }]);
|
|
579
|
+
continue;
|
|
580
|
+
}
|
|
581
|
+
// fabric offset += 字素长 + infix.length(0) —— 每字素的 charOffset 即行内索引
|
|
582
|
+
const lineWords = graphemes.map((g, i) => {
|
|
583
|
+
const width = measureWord([g], lineIndex, i);
|
|
584
|
+
largestWordWidth = Math.max(width, largestWordWidth);
|
|
585
|
+
return { graphemes: [g], width, charOffset: i };
|
|
586
|
+
});
|
|
587
|
+
wordsData.push(lineWords);
|
|
588
|
+
}
|
|
589
|
+
graphemeLines = [];
|
|
590
|
+
for (let i = 0; i < wordsData.length; i++) {
|
|
591
|
+
graphemeLines.push(...wrapLine(i, options.boxWidth, wordsData, largestWordWidth, style, styleAt, measure, true));
|
|
592
|
+
}
|
|
593
|
+
}
|
|
594
|
+
else {
|
|
595
|
+
// fabric getGraphemeDataForRender + _wrapText(词内 charOffset 供逐字样式解析)
|
|
596
|
+
const wordsData = [];
|
|
597
|
+
for (let lineIndex = 0; lineIndex < rawLines.length; lineIndex++) {
|
|
598
|
+
const lineWords = [];
|
|
599
|
+
let offset = 0;
|
|
600
|
+
for (const word of wordSplit(rawLines[lineIndex])) {
|
|
601
|
+
const graphemes = graphemeSplit(word);
|
|
602
|
+
const width = measureWord(graphemes, lineIndex, offset);
|
|
603
|
+
largestWordWidth = Math.max(width, largestWordWidth);
|
|
604
|
+
lineWords.push({ graphemes, width, charOffset: offset });
|
|
605
|
+
offset += graphemes.length + 1; // +1 = 词后 infix 空格(与 fabric 同构)
|
|
606
|
+
}
|
|
607
|
+
wordsData.push(lineWords);
|
|
608
|
+
}
|
|
609
|
+
graphemeLines = [];
|
|
610
|
+
for (let i = 0; i < wordsData.length; i++) {
|
|
611
|
+
graphemeLines.push(...wrapLine(i, options.boxWidth, wordsData, largestWordWidth, style, styleAt, measure));
|
|
612
|
+
}
|
|
613
|
+
}
|
|
614
|
+
// fabric _generateStyleMap 移植:视觉行 → 原始行/偏移(含换行被吃掉的分隔符游走)。
|
|
615
|
+
// flat = Array.from(text)(含 \n);视觉行首在 flat 中的偏移同时记录(光标映射用)
|
|
616
|
+
const flatGraphemes = graphemeSplit(text ?? '');
|
|
617
|
+
const styleMapEntries = [];
|
|
618
|
+
{
|
|
619
|
+
let realLineCount = 0;
|
|
620
|
+
let realLineCharCount = 0;
|
|
621
|
+
let charCount = 0;
|
|
622
|
+
for (let i = 0; i < graphemeLines.length; i++) {
|
|
623
|
+
const separator = flatGraphemes[charCount];
|
|
624
|
+
if (separator === '\n' && i > 0) {
|
|
625
|
+
realLineCharCount = 0;
|
|
626
|
+
charCount++;
|
|
627
|
+
realLineCount++;
|
|
628
|
+
}
|
|
629
|
+
else if (i > 0 && !options.splitByGrapheme && /[ \t\r]/.test(separator ?? '')) {
|
|
630
|
+
// 自动换行吃掉的分隔空格(词级换行模式;splitByGrapheme 下空格是
|
|
631
|
+
// 实字素不落行——fabric _generateStyleMap 的 !splitByGrapheme 守卫)
|
|
632
|
+
realLineCharCount++;
|
|
633
|
+
charCount++;
|
|
634
|
+
}
|
|
635
|
+
styleMapEntries[i] = { line: realLineCount, offset: realLineCharCount, flatStart: charCount };
|
|
636
|
+
charCount += graphemeLines[i].length;
|
|
637
|
+
realLineCharCount += graphemeLines[i].length;
|
|
638
|
+
}
|
|
639
|
+
}
|
|
640
|
+
// 每行字符盒与行宽(fabric _measureLine:样式按视觉行→原始行映射解析)
|
|
641
|
+
const lines = graphemeLines.map((graphemes, lineIndex) => {
|
|
642
|
+
const entry = styleMapEntries[lineIndex];
|
|
643
|
+
const boxes = [];
|
|
644
|
+
let width = 0;
|
|
645
|
+
for (let i = 0; i < graphemes.length; i++) {
|
|
646
|
+
const info = measureChar(measure, styleAt(entry.line, entry.offset + i), graphemes[i], i > 0 ? graphemes[i - 1] : undefined, i > 0 ? styleAt(entry.line, entry.offset + i - 1) : undefined);
|
|
647
|
+
const box = {
|
|
648
|
+
left: 0,
|
|
649
|
+
width: info.width + spacing,
|
|
650
|
+
kernedWidth: info.kernedWidth + spacing,
|
|
651
|
+
// fabric _getGraphemeBox:height = 逐字 fontSize(竖排 CJK advance 基数)
|
|
652
|
+
height: styleAt(entry.line, entry.offset + i).fontSize
|
|
653
|
+
};
|
|
654
|
+
if (i > 0) {
|
|
655
|
+
box.left = boxes[i - 1].left + boxes[i - 1].width + info.kernedWidth - info.width;
|
|
656
|
+
}
|
|
657
|
+
boxes.push(box);
|
|
658
|
+
// P4-5c 竖排行宽(应用 Textbox._measureLine 1307-1311):CJK 用盒高、
|
|
659
|
+
// 拉丁用 width(注意不是 kernedWidth——与横排累加口径不同,各自照抄)
|
|
660
|
+
if (options.vertical && !isLatinChar(graphemes[i])) {
|
|
661
|
+
width += box.height;
|
|
662
|
+
}
|
|
663
|
+
else {
|
|
664
|
+
width += box.kernedWidth;
|
|
665
|
+
}
|
|
666
|
+
}
|
|
667
|
+
if (spacing !== 0)
|
|
668
|
+
width -= spacing;
|
|
669
|
+
if (width < 0)
|
|
670
|
+
width = 0;
|
|
671
|
+
const charStyles = graphemes.map((_, j) => rawStyles?.[entry.line]?.[entry.offset + j]);
|
|
672
|
+
// fabric getHeightOfLineImpl:行高 impl = 行内逐字 max(fontSize)×_fontSizeMult
|
|
673
|
+
// (getHeightOfChar = 样式覆写优先);空行取行首原始位样式(getValueOfPropertyAt(line,0))
|
|
674
|
+
let maxFont = 0;
|
|
675
|
+
if (graphemes.length > 0) {
|
|
676
|
+
for (let i = 0; i < graphemes.length; i++) {
|
|
677
|
+
maxFont = Math.max(maxFont, rawStyles?.[entry.line]?.[entry.offset + i]?.fontSize ?? style.fontSize);
|
|
678
|
+
}
|
|
679
|
+
}
|
|
680
|
+
else {
|
|
681
|
+
maxFont = rawStyles?.[entry.line]?.[entry.offset]?.fontSize ?? style.fontSize;
|
|
682
|
+
}
|
|
683
|
+
// 列表前缀(fabric _measureLine 1315-1337):前缀宽并入行宽 + 该行全部
|
|
684
|
+
// 字符盒 left 平移(含行末哨兵语义——引擎哨兵由末盒 left+width 推导,平移
|
|
685
|
+
// 自动生效);空行同样编号(前缀按视觉行号)。leftOffset/justify 在本
|
|
686
|
+
// map 之后跑,对齐把「前缀+文字」作为整体收边(fabric getLineWidth 同口径)
|
|
687
|
+
let prefix;
|
|
688
|
+
if (options.paragraph) {
|
|
689
|
+
const prefixText = paragraphPrefixOf(options.paragraph, lineIndex);
|
|
690
|
+
if (prefixText) {
|
|
691
|
+
const prefixStyle = styleAt(entry.line, entry.offset);
|
|
692
|
+
const prefixDecl = fontDeclaration(prefixStyle, prefixStyle.fontSize);
|
|
693
|
+
const prefixWidth = measure(prefixDecl, prefixText);
|
|
694
|
+
// 宽 0(jsdom 无 2d ctx 的度量退化)= 无前缀:不平移也不留给渲染层
|
|
695
|
+
//(零宽前缀画在未平移的首字上必重叠,浏览器实测非空串恒 > 0)
|
|
696
|
+
if (prefixWidth > 0) {
|
|
697
|
+
prefix = {
|
|
698
|
+
text: prefixText,
|
|
699
|
+
width: prefixWidth,
|
|
700
|
+
advances: graphemeSplit(prefixText).map((g) => measure(prefixDecl, g)),
|
|
701
|
+
style: prefixStyle
|
|
702
|
+
};
|
|
703
|
+
for (const box of boxes)
|
|
704
|
+
box.left += prefix.width;
|
|
705
|
+
width += prefix.width;
|
|
706
|
+
}
|
|
707
|
+
}
|
|
708
|
+
}
|
|
709
|
+
return { graphemes, boxes, width, leftOffset: 0, baseline: 0, top: 0, implHeight: maxFont * FONT_SIZE_MULT, styleMap: entry, charStyles, prefix };
|
|
710
|
+
});
|
|
711
|
+
// 高度与基线:fabric calcTextHeight(末行 impl 不乘 lineHeight);
|
|
712
|
+
// 基线 = Σ_{j<i}(impl_j×lineHeight) + impl_i×(1−_fontSizeFraction)
|
|
713
|
+
// ——注意首基线不是 impl(源码 _renderChars 里 top -= impl×0.222),
|
|
714
|
+
// 历史上只看 _renderTextCommon 会误推成 impl,已用 fillText 拦截实测校准
|
|
715
|
+
let height = 0;
|
|
716
|
+
let acc = 0;
|
|
717
|
+
for (let i = 0; i < lines.length; i++) {
|
|
718
|
+
const impl = lines[i].implHeight;
|
|
719
|
+
const isLast = i === lines.length - 1;
|
|
720
|
+
lines[i].top = acc;
|
|
721
|
+
lines[i].baseline = acc + impl * (1 - FONT_SIZE_FRACTION);
|
|
722
|
+
acc += impl * lineHeightMult;
|
|
723
|
+
height += isLast ? impl : impl * lineHeightMult;
|
|
724
|
+
}
|
|
725
|
+
// 行偏移(ltr 子集:rtl 的反向对齐不在此列);盒宽按 dynamicMinWidth 语义扩展后
|
|
726
|
+
// 参与对齐计算(fabric 用扩展后的 this.width)。justify 变体的段末行按
|
|
727
|
+
// left/center/right 收尾(fabric _getLineLeftOffset 的 JUSTIFY_* 分支),
|
|
728
|
+
// 其余(left/justify/justify-left 与将被拉伸的 justify-* 非段末行)恒 0
|
|
729
|
+
const expandedBoxWidth = options.boxWidth != null ? Math.max(options.boxWidth, largestWordWidth - spacing) : 0;
|
|
730
|
+
// P4-5c 竖排 text/itext 盒宽:fabric calcTextWidth 竖排分支(应用 Textbox
|
|
731
|
+
// 2165-2198)= 最长列 + cursorWidth(2);逐字累加 isLatin ? width+space :
|
|
732
|
+
// height+space——width 已含 spacing 仍再加一次 space,是 fabric 原样 quirk
|
|
733
|
+
const verticalCalcWidth = () => {
|
|
734
|
+
let longest = 0;
|
|
735
|
+
for (const line of lines) {
|
|
736
|
+
let len = 0;
|
|
737
|
+
for (let j = 0; j < line.graphemes.length; j++) {
|
|
738
|
+
len += isLatinChar(line.graphemes[j]) ? line.boxes[j].width + spacing : line.boxes[j].height + spacing;
|
|
739
|
+
}
|
|
740
|
+
longest = Math.max(longest, len);
|
|
741
|
+
}
|
|
742
|
+
return longest + 2;
|
|
743
|
+
};
|
|
744
|
+
const verticalWidth = options.vertical ? verticalCalcWidth() : undefined;
|
|
745
|
+
const boxW = options.boxWidth != null ? expandedBoxWidth : options.vertical ? verticalWidth : Math.max(0, ...lines.map((l) => l.width));
|
|
746
|
+
const align = style.textAlign ?? 'left';
|
|
747
|
+
// 段末行判定(fabric Textbox.isEndOfWrapping:下一视觉行已属新原始行即段末;
|
|
748
|
+
// text/itext 无自动换行,fabric Text.isEndOfWrapping 只认最后一行)
|
|
749
|
+
const isEndOfWrapping = (i) => {
|
|
750
|
+
if (options.boxWidth == null)
|
|
751
|
+
return i === lines.length - 1;
|
|
752
|
+
const next = lines[i + 1]?.styleMap;
|
|
753
|
+
return !next || next.line !== lines[i].styleMap.line;
|
|
754
|
+
};
|
|
755
|
+
for (let i = 0; i < lines.length; i++) {
|
|
756
|
+
const centerish = align === 'center' || (align === 'justify-center' && isEndOfWrapping(i));
|
|
757
|
+
const rightish = align === 'right' || (align === 'justify-right' && isEndOfWrapping(i));
|
|
758
|
+
// P4-5c 补丁(2026-09-20,仅 webgl2 引擎侧——登记为有意偏差):竖排
|
|
759
|
+
// center/right 的对齐量改按渲染链墨迹口径(verticalLineInkRun)。应用侧
|
|
760
|
+
// fabric 以度量列长收边(_measureLine 竖排 CJK=盒高、不含 charSpacing),
|
|
761
|
+
// 而渲染笔位每字多推进 spacing 且锚点前移 fontSize/1.1——right/center 时
|
|
762
|
+
// 墨迹越过盒远端(charSpacing=120×6 字实测超底 28.8px=n×spacing)。
|
|
763
|
+
// 引擎按墨迹收边:left 对齐零改动(主路径与 fabric 逐像素一致不变),
|
|
764
|
+
// center/right 与应用侧的偏差见 roadmap §1(应用侧修复须另立批次)。
|
|
765
|
+
if (options.vertical && (centerish || rightish) && lines[i].graphemes.length > 0) {
|
|
766
|
+
const ink = verticalLineInkRun(lines[i], style, inkMeasure);
|
|
767
|
+
lines[i].leftOffset = centerish ? (boxW - ink.start - ink.end) / 2 : boxW - ink.end;
|
|
768
|
+
continue;
|
|
769
|
+
}
|
|
770
|
+
const diff = boxW - lines[i].width;
|
|
771
|
+
if (centerish) {
|
|
772
|
+
lines[i].leftOffset = diff / 2;
|
|
773
|
+
}
|
|
774
|
+
else if (rightish) {
|
|
775
|
+
lines[i].leftOffset = diff;
|
|
776
|
+
}
|
|
777
|
+
}
|
|
778
|
+
// 两端对齐(fabric enlargeSpaces 移植):把 (盒宽 − 自然行宽) 均摊进行内每个
|
|
779
|
+
// 空格的 width/kernedWidth,并平移后续字符盒 left——fabric 在度量后直接改
|
|
780
|
+
// __charBounds,渲染/光标/选区/装饰线全部随盒生效,引擎同口径改 boxes。
|
|
781
|
+
// line.width 保持自然行宽(fabric __lineWidths 不被 enlargeSpaces 触碰,
|
|
782
|
+
// 布局对拍口径);行末端点(光标/选区)用 lineEndOffset 的哨兵盒口径,
|
|
783
|
+
// 末盒被同步平移/加宽后即落在拉伸末端。
|
|
784
|
+
// 纯 'justify' 拉伸所有行(含末行——fabric 的历史行为,不做排版学上的末行
|
|
785
|
+
// 豁免);'justify-left/center/right' 变体跳过段末行,由上面的行偏移收尾。
|
|
786
|
+
// 行内无空格(CJK 整句无词隙)或行宽已达盒宽时不拉伸。
|
|
787
|
+
if (align.includes('justify')) {
|
|
788
|
+
for (let i = 0; i < lines.length; i++) {
|
|
789
|
+
const line = lines[i];
|
|
790
|
+
if (align !== 'justify' && (i === lines.length - 1 || isEndOfWrapping(i)))
|
|
791
|
+
continue;
|
|
792
|
+
if (line.width >= boxW)
|
|
793
|
+
continue;
|
|
794
|
+
const numOfSpaces = line.graphemes.reduce((n, g) => (/[ \t\r]/.test(g) ? n + 1 : n), 0);
|
|
795
|
+
if (numOfSpaces === 0)
|
|
796
|
+
continue;
|
|
797
|
+
const diffSpace = (boxW - line.width) / numOfSpaces;
|
|
798
|
+
let accumulated = 0;
|
|
799
|
+
for (let j = 0; j < line.graphemes.length; j++) {
|
|
800
|
+
const box = line.boxes[j];
|
|
801
|
+
if (/[ \t\r]/.test(line.graphemes[j])) {
|
|
802
|
+
box.width += diffSpace;
|
|
803
|
+
box.kernedWidth += diffSpace;
|
|
804
|
+
box.left += accumulated;
|
|
805
|
+
accumulated += diffSpace;
|
|
806
|
+
}
|
|
807
|
+
else {
|
|
808
|
+
box.left += accumulated;
|
|
809
|
+
}
|
|
810
|
+
}
|
|
811
|
+
}
|
|
812
|
+
}
|
|
813
|
+
// flat grapheme → UTF-16 码元偏移(textarea selectionStart 是码元口径)
|
|
814
|
+
const graphemeCodeUnitOffsets = [];
|
|
815
|
+
let codeUnit = 0;
|
|
816
|
+
for (const g of flatGraphemes) {
|
|
817
|
+
graphemeCodeUnitOffsets.push(codeUnit);
|
|
818
|
+
codeUnit += g.length;
|
|
819
|
+
}
|
|
820
|
+
graphemeCodeUnitOffsets.push(codeUnit); // 末尾光标位
|
|
821
|
+
return { lines, width: boxW, height, graphemeCodeUnitOffsets, verticalWidth };
|
|
822
|
+
};
|