modern-text 0.3.6 → 0.3.8

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,6 +1,6 @@
1
1
  import { BoundingBox, Path2D, getPathsBoundingBox, Matrix3, parseSvg, Vector2 } from 'modern-path2d';
2
2
  export * from 'modern-path2d';
3
- import { fonts, Woff, Ttf } from 'modern-font';
3
+ import { Ttf, Woff, fonts } from 'modern-font';
4
4
  export * from 'modern-font';
5
5
 
6
6
  function parseColor(ctx, source, box) {
@@ -174,12 +174,21 @@ class Character {
174
174
  get fontHeight() {
175
175
  return this.fontSize * this.computedStyle.lineHeight;
176
176
  }
177
- _getFontSfnt(font) {
178
- const _font = font ?? fonts.get(this.computedStyle.fontFamily)?.font;
179
- if (_font instanceof Woff || _font instanceof Ttf) {
180
- return _font.sfnt;
177
+ _getFontSfnt(fonts$1) {
178
+ const fontFamily = this.computedStyle.fontFamily;
179
+ let sfnt;
180
+ if (fontFamily) {
181
+ if (!sfnt) {
182
+ const font = fonts$1?.[fontFamily];
183
+ if (font instanceof Ttf || font instanceof Woff) {
184
+ sfnt = font.sfnt;
185
+ }
186
+ }
187
+ if (!sfnt) {
188
+ sfnt = fonts.get(fontFamily)?.getSfnt();
189
+ }
181
190
  }
182
- return void 0;
191
+ return sfnt ?? fonts.fallbackFont?.getSfnt();
183
192
  }
184
193
  updateGlyph(sfnt = this._getFontSfnt()) {
185
194
  if (!sfnt) {
@@ -206,8 +215,8 @@ class Character {
206
215
  this.centerDiviation = advanceHeight / 2 - baseline;
207
216
  return this;
208
217
  }
209
- update(font) {
210
- const sfnt = this._getFontSfnt(font);
218
+ update(fonts) {
219
+ const sfnt = this._getFontSfnt(fonts);
211
220
  if (!sfnt) {
212
221
  return this;
213
222
  }
@@ -447,6 +456,10 @@ class Paragraph {
447
456
  }
448
457
  }
449
458
 
459
+ function definePlugin(options) {
460
+ return options;
461
+ }
462
+
450
463
  class Measurer {
451
464
  constructor(_text) {
452
465
  this._text = _text;
@@ -642,65 +655,6 @@ class Measurer {
642
655
  }
643
656
  }
644
657
 
645
- class Parser {
646
- constructor(_text) {
647
- this._text = _text;
648
- }
649
- parse() {
650
- let { content, computedStyle: style } = this._text;
651
- const paragraphs = [];
652
- if (typeof content === "string") {
653
- const paragraph = new Paragraph({}, style);
654
- paragraph.addFragment(content);
655
- paragraphs.push(paragraph);
656
- } else {
657
- content = Array.isArray(content) ? content : [content];
658
- for (const p of content) {
659
- if (typeof p === "string") {
660
- const paragraph = new Paragraph({}, style);
661
- paragraph.addFragment(p);
662
- paragraphs.push(paragraph);
663
- } else if (Array.isArray(p)) {
664
- const paragraph = new Paragraph({}, style);
665
- p.forEach((f) => {
666
- if (typeof f === "string") {
667
- paragraph.addFragment(f);
668
- } else {
669
- const { content: content2, ...fStyle } = f;
670
- if (content2 !== void 0) {
671
- paragraph.addFragment(content2, fStyle);
672
- }
673
- }
674
- });
675
- paragraphs.push(paragraph);
676
- } else if ("fragments" in p) {
677
- const { fragments, ...pStyle } = p;
678
- const paragraph = new Paragraph(pStyle, style);
679
- fragments.forEach((f) => {
680
- const { content: content2, ...fStyle } = f;
681
- if (content2 !== void 0) {
682
- paragraph.addFragment(content2, fStyle);
683
- }
684
- });
685
- paragraphs.push(paragraph);
686
- } else if ("content" in p) {
687
- const { content: pData, ...pStyle } = p;
688
- if (pData !== void 0) {
689
- const paragraph = new Paragraph(pStyle, style);
690
- paragraph.addFragment(pData);
691
- paragraphs.push(paragraph);
692
- }
693
- }
694
- }
695
- }
696
- return paragraphs;
697
- }
698
- }
699
-
700
- function definePlugin(options) {
701
- return options;
702
- }
703
-
704
658
  const defaultReferImage = "data:image/svg+xml;base64,PHN2ZyB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIHdpZHRoPSI3MiIgaGVpZ2h0PSI3MiIgdmlld0JveD0iMCAwIDcyIDcyIiBmaWxsPSJub25lIj48cGF0aCBmaWxsLXJ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NyA1Mi4xODgxIDM1Ljg1NzFMNTIuMjIzOSAzNi4wMTk0SDU0Ljk1NDhMNTQuOTE3IDM1Ljc4MzVDNTQuNzA2MyAzNC40NjYgNTQuMTUzNiAzMy40NzAxIDUzLjI2MzQgMzIuODAxOUw1My4yNjAyIDMyLjc5OTVDNTIuMzk1MSAzMi4xNzU1IDUxLjI2MjEgMzEuODczNyA0OS44NjE0IDMxLjg3MzdaIiBmaWxsPSIjMjIyNTI5Ii8+PHBhdGggZmlsbC1ydWxlPSJldmVub2RkIiBjbGlwLXJ1bGU9ImV2ZW5vZGQiIGQ9Ik0yNS43NTYxIDI4LjI3NTNIMjIuNzQ0TDE3IDQyLjcyNDdIMjAuMDE0MUwyMS4zNDI5IDM5LjIwNDlIMjcuMTU3MkwyOC40ODYgNDIuNzI0N0gzMS41MDAxTDI1Ljc1NjEgMjguMjc1M1pNMjIuMjEyNSAzNi45MDc2TDI0LjI1OTYgMzEuNDUzOUwyNi4yODg1IDM2LjkwNzZIMjIuMjEyNVoiIGZpbGw9IiMyMjI1MjkiLz48L3N2Zz4=";
705
659
  function parseCharsPerRepeat(size, fontSize, total) {
706
660
  if (size === "cover") {
@@ -1055,7 +1009,7 @@ const defaultTextStyles = {
1055
1009
  // font
1056
1010
  fontSize: 14,
1057
1011
  fontWeight: "normal",
1058
- fontFamily: "_fallback",
1012
+ fontFamily: "",
1059
1013
  fontStyle: "normal",
1060
1014
  fontKerning: "normal",
1061
1015
  // text
@@ -1106,15 +1060,15 @@ class Text {
1106
1060
  __publicField(this, "glyphBox", new BoundingBox());
1107
1061
  __publicField(this, "pathBox", new BoundingBox());
1108
1062
  __publicField(this, "boundingBox", new BoundingBox());
1109
- __publicField(this, "parser", new Parser(this));
1110
1063
  __publicField(this, "measurer", new Measurer(this));
1111
1064
  __publicField(this, "plugins", /* @__PURE__ */ new Map());
1112
- __publicField(this, "font");
1113
- const { content = "", style = {}, measureDom, effects } = options;
1065
+ __publicField(this, "fonts");
1066
+ const { content = "", style = {}, measureDom, effects, fonts } = options;
1114
1067
  this.content = content;
1115
1068
  this.style = style;
1116
1069
  this.measureDom = measureDom;
1117
1070
  this.effects = effects;
1071
+ this.fonts = fonts;
1118
1072
  this.use(render()).use(highlight()).use(listStyle());
1119
1073
  }
1120
1074
  get fontSize() {
@@ -1130,6 +1084,56 @@ class Text {
1130
1084
  this.plugins.set(plugin.name, plugin);
1131
1085
  return this;
1132
1086
  }
1087
+ updateParagraphs() {
1088
+ let { content, computedStyle: style } = this;
1089
+ const paragraphs = [];
1090
+ if (typeof content === "string") {
1091
+ const paragraph = new Paragraph({}, style);
1092
+ paragraph.addFragment(content);
1093
+ paragraphs.push(paragraph);
1094
+ } else {
1095
+ content = Array.isArray(content) ? content : [content];
1096
+ for (const p of content) {
1097
+ if (typeof p === "string") {
1098
+ const paragraph = new Paragraph({}, style);
1099
+ paragraph.addFragment(p);
1100
+ paragraphs.push(paragraph);
1101
+ } else if (Array.isArray(p)) {
1102
+ const paragraph = new Paragraph({}, style);
1103
+ p.forEach((f) => {
1104
+ if (typeof f === "string") {
1105
+ paragraph.addFragment(f);
1106
+ } else {
1107
+ const { content: content2, ...fStyle } = f;
1108
+ if (content2 !== void 0) {
1109
+ paragraph.addFragment(content2, fStyle);
1110
+ }
1111
+ }
1112
+ });
1113
+ paragraphs.push(paragraph);
1114
+ } else if ("fragments" in p) {
1115
+ const { fragments, ...pStyle } = p;
1116
+ const paragraph = new Paragraph(pStyle, style);
1117
+ fragments.forEach((f) => {
1118
+ const { content: content2, ...fStyle } = f;
1119
+ if (content2 !== void 0) {
1120
+ paragraph.addFragment(content2, fStyle);
1121
+ }
1122
+ });
1123
+ paragraphs.push(paragraph);
1124
+ } else if ("content" in p) {
1125
+ const { content: pData, ...pStyle } = p;
1126
+ if (pData !== void 0) {
1127
+ const paragraph = new Paragraph(pStyle, style);
1128
+ paragraph.addFragment(pData);
1129
+ paragraphs.push(paragraph);
1130
+ }
1131
+ }
1132
+ }
1133
+ }
1134
+ this.paragraphs = paragraphs;
1135
+ return this;
1136
+ }
1133
1137
  measure(dom = this.measureDom) {
1134
1138
  this.computedStyle = { ...defaultTextStyles, ...this.style };
1135
1139
  const old = {
@@ -1140,12 +1144,12 @@ class Text {
1140
1144
  pathBox: this.pathBox,
1141
1145
  boundingBox: this.boundingBox
1142
1146
  };
1143
- this.paragraphs = this.parser.parse();
1147
+ this.updateParagraphs();
1144
1148
  const result = this.measurer.measure(dom);
1145
1149
  this.paragraphs = result.paragraphs;
1146
1150
  this.lineBox = result.boundingBox;
1147
1151
  this.characters.forEach((c) => {
1148
- c.update(this.font);
1152
+ c.update(this.fonts);
1149
1153
  });
1150
1154
  this.rawGlyphBox = this.getGlyphBox();
1151
1155
  const plugins = [...this.plugins.values()];
@@ -1251,4 +1255,4 @@ function renderText(options) {
1251
1255
  return new Text(options).render(options);
1252
1256
  }
1253
1257
 
1254
- export { Character, Fragment, Measurer, Paragraph, Parser, Text, defaultTextStyles, definePlugin, drawPath, filterEmpty, getTransform2D, highlight, isNone, listStyle, measureText, parseColor, render, renderText, setupView, uploadColor, uploadColors };
1258
+ export { Character, Fragment, Measurer, Paragraph, Text, defaultTextStyles, definePlugin, drawPath, filterEmpty, getTransform2D, highlight, isNone, listStyle, measureText, parseColor, render, renderText, setupView, uploadColor, uploadColors };
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "modern-text",
3
3
  "type": "module",
4
- "version": "0.3.6",
4
+ "version": "0.3.8",
5
5
  "packageManager": "pnpm@9.9.0",
6
6
  "description": "Measure and render text in a way that describes the DOM.",
7
7
  "author": "wxm",
@@ -57,7 +57,7 @@
57
57
  "prepare": "simple-git-hooks"
58
58
  },
59
59
  "dependencies": {
60
- "modern-font": "^0.3.0",
60
+ "modern-font": "^0.3.1",
61
61
  "modern-path2d": "^0.2.5"
62
62
  },
63
63
  "devDependencies": {