modern-text 1.12.0 → 2.0.0

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/dist/index.mjs CHANGED
@@ -1,5 +1,5 @@
1
- import { T as Text } from './shared/modern-text.CrFHIJtB.mjs';
2
- export { C as Canvas2DRenderer, a as Character, F as Fragment, M as Measurer, P as Paragraph, b as backgroundPlugin, c as createSvgLoader, d as createSvgParser, g as getEffectTransform2D, e as getHighlightStyle, h as highlightPlugin, i as isEqualObject, f as isEqualValue, l as listStylePlugin, o as outlinePlugin, p as parseColormap, j as parseTransformOrigin, k as parseValueNumber, r as renderPlugin, t as textDecorationPlugin, m as textDefaultStyle } from './shared/modern-text.CrFHIJtB.mjs';
1
+ import { T as Text } from './shared/modern-text.ChzjFjsk.mjs';
2
+ export { C as Canvas2DRenderer, a as Character, D as DomMeasurer, F as FontMeasurer, b as Fragment, P as Paragraph, c as backgroundPlugin, d as createSvgLoader, e as createSvgParser, g as getEffectTransform2D, f as getHighlightStyle, h as highlightPlugin, i as isEqualObject, j as isEqualValue, l as listStylePlugin, o as outlinePlugin, p as parseColormap, k as parseTransformOrigin, m as parseValueNumber, r as renderPlugin, t as textDecorationPlugin, n as textDefaultStyle } from './shared/modern-text.ChzjFjsk.mjs';
3
3
  export { d as defineDeformation, a as definePlugin, b as deformationPlugin, g as getDeformationNames, r as removeDeformation } from './shared/modern-text.JF1ny7A-.mjs';
4
4
  export { C as CircleCurve, E as EllipseCurve, H as HeartCurve, P as PolygonCurve, R as RectangularCurve } from './shared/modern-text.fT17R5HY.mjs';
5
5
  import 'modern-idoc';
@@ -1,5 +1,5 @@
1
1
  import { Vector2Like, BoundingBox, Vector2 } from 'modern-path2d';
2
- import { a as Character } from './modern-text.C8g4NZgY.cjs';
2
+ import { a as Character } from './modern-text.D4WopQCu.cjs';
3
3
 
4
4
  /** 逐字沿形状排布所需的曲线最小接口(实现了这三个方法即可当作变形曲线) */
5
5
  interface DeformationCurve {
@@ -1,5 +1,5 @@
1
1
  import { Vector2Like, BoundingBox, Vector2 } from 'modern-path2d';
2
- import { a as Character } from './modern-text.C8g4NZgY.js';
2
+ import { a as Character } from './modern-text.D4WopQCu.js';
3
3
 
4
4
  /** 逐字沿形状排布所需的曲线最小接口(实现了这三个方法即可当作变形曲线) */
5
5
  interface DeformationCurve {
@@ -2,8 +2,8 @@
2
2
 
3
3
  const modernIdoc = require('modern-idoc');
4
4
  const modernPath2d = require('modern-path2d');
5
- const deformation = require('./modern-text.B2xfrqDc.cjs');
6
5
  const modernFont = require('modern-font');
6
+ const deformation = require('./modern-text.B2xfrqDc.cjs');
7
7
 
8
8
  function createSvgLoader() {
9
9
  const loaded = /* @__PURE__ */ new Map();
@@ -531,6 +531,18 @@ class Character {
531
531
  this.fontStyle = fsSelectionMap[os2.fsSelection] ?? macStyleMap[head.macStyle];
532
532
  return this;
533
533
  }
534
+ /**
535
+ * Populate glyph metrics only (advance width/height, ascender/descender,
536
+ * baseline, …) without building the glyph `path` or touching boxes.
537
+ *
538
+ * The DOM {@link DomMeasurer} never needs this — it reads positions back from the
539
+ * browser. A pure-JS measurer (e.g. `FontMeasurer`) must know advances *before*
540
+ * it can place characters, so it calls this ahead of layout. `update()` later
541
+ * recomputes the same metrics while building the path, so this is idempotent.
542
+ */
543
+ measureGlyph(fonts) {
544
+ return this.updateGlyph(this._getFontSFNT(fonts));
545
+ }
534
546
  update(fonts) {
535
547
  const sfnt = this._getFontSFNT(fonts);
536
548
  if (!sfnt) {
@@ -723,7 +735,7 @@ function getSharedContainer() {
723
735
  sharedContainer = container;
724
736
  return container;
725
737
  }
726
- class Measurer {
738
+ class DomMeasurer {
727
739
  static notZeroStyles = /* @__PURE__ */ new Set([
728
740
  "width",
729
741
  "height"
@@ -758,7 +770,7 @@ class Measurer {
758
770
  return cached;
759
771
  }
760
772
  const domStyle = {};
761
- const { notZeroStyles, pxStyles } = Measurer;
773
+ const { notZeroStyles, pxStyles } = DomMeasurer;
762
774
  for (const key in style) {
763
775
  const value = style[key];
764
776
  if (notZeroStyles.has(key) && value === 0) {
@@ -1067,6 +1079,253 @@ class Measurer {
1067
1079
  }
1068
1080
  }
1069
1081
 
1082
+ function side(style, name, edge) {
1083
+ return style[`${name}${edge}`] ?? style[name] ?? 0;
1084
+ }
1085
+ class FontMeasurer {
1086
+ measure(paragraphs, rootStyle, _dom, fonts) {
1087
+ const _fonts = fonts ?? modernFont.fonts;
1088
+ for (const paragraph of paragraphs) {
1089
+ for (const fragment of paragraph.fragments) {
1090
+ for (const character of fragment.characters) {
1091
+ character.measureGlyph(_fonts);
1092
+ }
1093
+ }
1094
+ }
1095
+ return rootStyle.writingMode.includes("vertical") ? this._measureVertical(paragraphs, rootStyle) : this._measureHorizontal(paragraphs, rootStyle);
1096
+ }
1097
+ _rootPadding(rootStyle) {
1098
+ return {
1099
+ top: side(rootStyle, "padding", "Top"),
1100
+ right: side(rootStyle, "padding", "Right"),
1101
+ bottom: side(rootStyle, "padding", "Bottom"),
1102
+ left: side(rootStyle, "padding", "Left")
1103
+ };
1104
+ }
1105
+ _measureHorizontal(paragraphs, rootStyle) {
1106
+ const rootPad = this._rootPadding(rootStyle);
1107
+ const hasWidth = typeof rootStyle.width === "number";
1108
+ const availWidth = hasWidth ? rootStyle.width - rootPad.left - rootPad.right : Infinity;
1109
+ let y = rootPad.top;
1110
+ let maxRight = rootPad.left;
1111
+ for (const paragraph of paragraphs) {
1112
+ const pStyle = paragraph.computedStyle;
1113
+ const pBox = paragraph.style;
1114
+ const mTop = side(pBox, "margin", "Top");
1115
+ const mBottom = side(pBox, "margin", "Bottom");
1116
+ const mLeft = side(pBox, "margin", "Left");
1117
+ const mRight = side(pBox, "margin", "Right");
1118
+ const pTop = side(pBox, "padding", "Top");
1119
+ const pBottom = side(pBox, "padding", "Bottom");
1120
+ const pLeft = side(pBox, "padding", "Left");
1121
+ const pRight = side(pBox, "padding", "Right");
1122
+ const liLeft = rootPad.left + mLeft + pLeft;
1123
+ const liAvail = availWidth === Infinity ? Infinity : availWidth - mLeft - mRight - pLeft - pRight;
1124
+ y += mTop + pTop;
1125
+ const paraTop = y;
1126
+ let paraRight = liLeft;
1127
+ const lines = this._breakLines(paragraph, liAvail);
1128
+ const align = pStyle.textAlign;
1129
+ const indent = pStyle.textIndent ?? 0;
1130
+ for (let i = 0; i < lines.length; i++) {
1131
+ const line = lines[i];
1132
+ const lineIndent = i === 0 ? indent : 0;
1133
+ let lineHeight = pStyle.fontSize * pStyle.lineHeight;
1134
+ let contentWidth = 0;
1135
+ for (const c of line) {
1136
+ if (c.fontHeight > lineHeight) {
1137
+ lineHeight = c.fontHeight;
1138
+ }
1139
+ contentWidth += this._advance(c);
1140
+ }
1141
+ let x = liLeft + lineIndent;
1142
+ if (liAvail !== Infinity) {
1143
+ const slack = liAvail - lineIndent - contentWidth;
1144
+ if (align === "center") {
1145
+ x += slack / 2;
1146
+ } else if (align === "end" || align === "right") {
1147
+ x += slack;
1148
+ }
1149
+ }
1150
+ for (const c of line) {
1151
+ const adv = c.advanceWidth;
1152
+ const contentHeight = c.advanceHeight;
1153
+ const fontHeight = c.fontHeight;
1154
+ c.inlineBox.left = x;
1155
+ c.inlineBox.top = y + (lineHeight - contentHeight) / 2;
1156
+ c.inlineBox.width = adv;
1157
+ c.inlineBox.height = contentHeight;
1158
+ c.lineBox.left = x;
1159
+ c.lineBox.top = c.inlineBox.top + (contentHeight - fontHeight) / 2;
1160
+ c.lineBox.width = adv;
1161
+ c.lineBox.height = fontHeight;
1162
+ x += this._advance(c);
1163
+ }
1164
+ if (x > paraRight) {
1165
+ paraRight = x;
1166
+ }
1167
+ y += lineHeight;
1168
+ }
1169
+ if (paraRight > maxRight) {
1170
+ maxRight = paraRight;
1171
+ }
1172
+ for (const fragment of paragraph.fragments) {
1173
+ this._unionInto(fragment.inlineBox, fragment.characters.map((c) => c.inlineBox));
1174
+ }
1175
+ paragraph.lineBox.left = liLeft;
1176
+ paragraph.lineBox.top = paraTop;
1177
+ paragraph.lineBox.width = liAvail === Infinity ? paraRight - liLeft : liAvail;
1178
+ paragraph.lineBox.height = y - paraTop;
1179
+ y += pBottom + mBottom;
1180
+ }
1181
+ const contentBottom = y + rootPad.bottom;
1182
+ const totalWidth = hasWidth ? rootStyle.width : maxRight + rootPad.right;
1183
+ const totalHeight = typeof rootStyle.height === "number" ? rootStyle.height : contentBottom;
1184
+ if (typeof rootStyle.height === "number") {
1185
+ const slack = totalHeight - contentBottom;
1186
+ const va = rootStyle.verticalAlign;
1187
+ const dy = va === "middle" ? slack / 2 : va === "bottom" ? slack : 0;
1188
+ if (dy) {
1189
+ this._shiftAll(paragraphs, dy);
1190
+ }
1191
+ }
1192
+ return {
1193
+ paragraphs,
1194
+ boundingBox: new modernPath2d.BoundingBox(0, 0, totalWidth, totalHeight)
1195
+ };
1196
+ }
1197
+ /**
1198
+ * Vertical writing-mode (`vertical-rl`): columns stack right-to-left, glyphs
1199
+ * flow top→bottom. It is the horizontal layout with the inline and block axes
1200
+ * swapped — the inline (down-column) advance is `advanceWidth` (CJK upright = em;
1201
+ * Latin is rotated, so its advance ≈ its width), the cross-axis content box is
1202
+ * `advanceHeight`, and the column thickness is `fontHeight`. The lineBox is
1203
+ * derived exactly as DomMeasurer.measureParagraphDom's vertical branch, so it
1204
+ * stays accurate even though inlineBox carries the content-box rounding residual.
1205
+ *
1206
+ * v1: `vertical-rl` only; no per-paragraph margin/padding or block alignment.
1207
+ */
1208
+ _measureVertical(paragraphs, rootStyle) {
1209
+ const rootPad = this._rootPadding(rootStyle);
1210
+ const hasHeight = typeof rootStyle.height === "number";
1211
+ const availHeight = hasHeight ? rootStyle.height - rootPad.top - rootPad.bottom : Infinity;
1212
+ const columns = [];
1213
+ for (const paragraph of paragraphs) {
1214
+ const strut = paragraph.computedStyle.fontSize * paragraph.computedStyle.lineHeight;
1215
+ for (const line of this._breakLines(paragraph, availHeight)) {
1216
+ let thickness = strut;
1217
+ for (const c of line) {
1218
+ if (c.fontHeight > thickness) {
1219
+ thickness = c.fontHeight;
1220
+ }
1221
+ }
1222
+ columns.push({ chars: line, thickness });
1223
+ }
1224
+ }
1225
+ const totalThickness = columns.reduce((sum, c) => sum + c.thickness, 0);
1226
+ const hasWidth = typeof rootStyle.width === "number";
1227
+ const blockWidth = hasWidth ? Math.max(rootStyle.width - rootPad.left - rootPad.right, totalThickness) : totalThickness;
1228
+ let xRight = rootPad.left + blockWidth;
1229
+ let maxBottom = rootPad.top;
1230
+ for (const column of columns) {
1231
+ xRight -= column.thickness;
1232
+ const colLeft = xRight;
1233
+ let y = rootPad.top;
1234
+ for (const c of column.chars) {
1235
+ const advance = c.advanceWidth;
1236
+ const contentWidth = c.advanceHeight;
1237
+ const fontHeight = c.fontHeight;
1238
+ c.inlineBox.top = y;
1239
+ c.inlineBox.height = advance;
1240
+ c.inlineBox.width = contentWidth;
1241
+ c.inlineBox.left = colLeft + (column.thickness - contentWidth) / 2;
1242
+ c.lineBox.left = c.inlineBox.left + (contentWidth - fontHeight) / 2;
1243
+ c.lineBox.top = y;
1244
+ c.lineBox.width = fontHeight;
1245
+ c.lineBox.height = advance;
1246
+ y += this._advance(c);
1247
+ }
1248
+ if (y > maxBottom) {
1249
+ maxBottom = y;
1250
+ }
1251
+ }
1252
+ for (const paragraph of paragraphs) {
1253
+ for (const fragment of paragraph.fragments) {
1254
+ this._unionInto(fragment.inlineBox, fragment.characters.map((c) => c.inlineBox));
1255
+ }
1256
+ this._unionInto(
1257
+ paragraph.lineBox,
1258
+ paragraph.fragments.flatMap((f) => f.characters.map((c) => c.inlineBox))
1259
+ );
1260
+ }
1261
+ const totalWidth = hasWidth ? rootStyle.width : blockWidth + rootPad.left + rootPad.right;
1262
+ const totalHeight = hasHeight ? rootStyle.height : maxBottom + rootPad.bottom;
1263
+ return {
1264
+ paragraphs,
1265
+ boundingBox: new modernPath2d.BoundingBox(0, 0, totalWidth, totalHeight)
1266
+ };
1267
+ }
1268
+ /** Advance step including CSS letter-spacing (px). */
1269
+ _advance(character) {
1270
+ return character.advanceWidth + (character.computedStyle.letterSpacing ?? 0);
1271
+ }
1272
+ /**
1273
+ * Break a paragraph's characters into visual lines.
1274
+ * v1: `word-break: break-all` (break before any character that would overflow)
1275
+ * plus explicit `\n`/`\r` hard breaks. The newline itself occupies no line box.
1276
+ */
1277
+ _breakLines(paragraph, avail) {
1278
+ const lines = [];
1279
+ let current = [];
1280
+ let width = 0;
1281
+ const flush = () => {
1282
+ lines.push(current);
1283
+ current = [];
1284
+ width = 0;
1285
+ };
1286
+ for (const fragment of paragraph.fragments) {
1287
+ for (const c of fragment.characters) {
1288
+ if (c.content === "\n" || c.content === "\r") {
1289
+ flush();
1290
+ continue;
1291
+ }
1292
+ if (avail !== Infinity && current.length > 0 && width + c.advanceWidth > avail) {
1293
+ flush();
1294
+ }
1295
+ current.push(c);
1296
+ width += this._advance(c);
1297
+ }
1298
+ }
1299
+ flush();
1300
+ return lines;
1301
+ }
1302
+ _unionInto(target, boxes) {
1303
+ const used = boxes.filter((b) => b.width !== 0 || b.height !== 0);
1304
+ if (!used.length) {
1305
+ return;
1306
+ }
1307
+ const u = modernPath2d.BoundingBox.from(...used);
1308
+ target.left = u.left;
1309
+ target.top = u.top;
1310
+ target.width = u.width;
1311
+ target.height = u.height;
1312
+ }
1313
+ _shiftAll(paragraphs, dy) {
1314
+ for (const paragraph of paragraphs) {
1315
+ paragraph.lineBox.top += dy;
1316
+ for (const fragment of paragraph.fragments) {
1317
+ fragment.inlineBox.top += dy;
1318
+ for (const character of fragment.characters) {
1319
+ character.inlineBox.top += dy;
1320
+ character.lineBox.top += dy;
1321
+ }
1322
+ }
1323
+ }
1324
+ }
1325
+ dispose() {
1326
+ }
1327
+ }
1328
+
1070
1329
  function backgroundPlugin() {
1071
1330
  const pathSet = new modernPath2d.Path2DSet();
1072
1331
  const loader = createSvgLoader();
@@ -1825,7 +2084,7 @@ class Text extends modernIdoc.Reactivable {
1825
2084
  glyphBox = new modernPath2d.BoundingBox();
1826
2085
  pathBox = new modernPath2d.BoundingBox();
1827
2086
  boundingBox = new modernPath2d.BoundingBox();
1828
- measurer = new Measurer();
2087
+ measurer = new DomMeasurer();
1829
2088
  plugins = /* @__PURE__ */ new Map();
1830
2089
  pathSets = [];
1831
2090
  _paragraphs = [];
@@ -1883,6 +2142,12 @@ class Text extends modernIdoc.Reactivable {
1883
2142
  outline,
1884
2143
  deformation: deformation$1
1885
2144
  } = modernIdoc.normalizeText(options);
2145
+ if (options.measurer && typeof options.measurer !== "string") {
2146
+ this.measurer = options.measurer;
2147
+ } else {
2148
+ const kind = options.measurer ?? (fonts ? "font" : "dom");
2149
+ this.measurer = kind === "font" ? new FontMeasurer() : new DomMeasurer();
2150
+ }
1886
2151
  this.debug = options.debug ?? false;
1887
2152
  this.content = content;
1888
2153
  this.effects = effects;
@@ -1957,6 +2222,9 @@ class Text extends modernIdoc.Reactivable {
1957
2222
  }
1958
2223
  createDom() {
1959
2224
  this._update();
2225
+ if (!this.measurer.createDom) {
2226
+ throw new Error("current measurer does not support createDom()");
2227
+ }
1960
2228
  return this.measurer.createDom(this.paragraphs, this.computedStyle);
1961
2229
  }
1962
2230
  measure(dom = this.measureDom) {
@@ -1970,7 +2238,7 @@ class Text extends modernIdoc.Reactivable {
1970
2238
  boundingBox: this.boundingBox
1971
2239
  };
1972
2240
  this._update();
1973
- const result = this.measurer.measure(this.paragraphs, this.computedStyle, dom);
2241
+ const result = this.measurer.measure(this.paragraphs, this.computedStyle, dom, this.fonts);
1974
2242
  this.paragraphs = result.paragraphs;
1975
2243
  this.lineBox = result.boundingBox;
1976
2244
  const characters = this.characters;
@@ -2102,7 +2370,7 @@ class Text extends modernIdoc.Reactivable {
2102
2370
  options.onContext?.(ctx);
2103
2371
  }
2104
2372
  dispose() {
2105
- this.measurer.dispose();
2373
+ this.measurer.dispose?.();
2106
2374
  this._renderer = void 0;
2107
2375
  this._rendererCtx = void 0;
2108
2376
  }
@@ -2140,8 +2408,9 @@ __decorateClass([
2140
2408
 
2141
2409
  exports.Canvas2DRenderer = Canvas2DRenderer;
2142
2410
  exports.Character = Character;
2411
+ exports.DomMeasurer = DomMeasurer;
2412
+ exports.FontMeasurer = FontMeasurer;
2143
2413
  exports.Fragment = Fragment;
2144
- exports.Measurer = Measurer;
2145
2414
  exports.Paragraph = Paragraph;
2146
2415
  exports.Text = Text;
2147
2416
  exports.backgroundPlugin = backgroundPlugin;
@@ -1,5 +1,5 @@
1
1
  import { Vector2Like, BoundingBox, Vector2 } from 'modern-path2d';
2
- import { a as Character } from './modern-text.C8g4NZgY.mjs';
2
+ import { a as Character } from './modern-text.D4WopQCu.mjs';
3
3
 
4
4
  /** 逐字沿形状排布所需的曲线最小接口(实现了这三个方法即可当作变形曲线) */
5
5
  interface DeformationCurve {