modern-text 0.3.7 → 0.3.9

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) {
@@ -176,11 +176,19 @@ class Character {
176
176
  }
177
177
  _getFontSfnt(fonts$1) {
178
178
  const fontFamily = this.computedStyle.fontFamily;
179
- const _font = fonts$1?.[fontFamily] ?? fonts.get(this.computedStyle.fontFamily)?.font;
180
- if (_font instanceof Woff || _font instanceof Ttf) {
181
- return _font.sfnt;
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
+ }
182
190
  }
183
- return void 0;
191
+ return sfnt;
184
192
  }
185
193
  updateGlyph(sfnt = this._getFontSfnt()) {
186
194
  if (!sfnt) {
@@ -448,6 +456,10 @@ class Paragraph {
448
456
  }
449
457
  }
450
458
 
459
+ function definePlugin(options) {
460
+ return options;
461
+ }
462
+
451
463
  class Measurer {
452
464
  constructor(_text) {
453
465
  this._text = _text;
@@ -643,65 +655,6 @@ class Measurer {
643
655
  }
644
656
  }
645
657
 
646
- class Parser {
647
- constructor(_text) {
648
- this._text = _text;
649
- }
650
- parse() {
651
- let { content, computedStyle: style } = this._text;
652
- const paragraphs = [];
653
- if (typeof content === "string") {
654
- const paragraph = new Paragraph({}, style);
655
- paragraph.addFragment(content);
656
- paragraphs.push(paragraph);
657
- } else {
658
- content = Array.isArray(content) ? content : [content];
659
- for (const p of content) {
660
- if (typeof p === "string") {
661
- const paragraph = new Paragraph({}, style);
662
- paragraph.addFragment(p);
663
- paragraphs.push(paragraph);
664
- } else if (Array.isArray(p)) {
665
- const paragraph = new Paragraph({}, style);
666
- p.forEach((f) => {
667
- if (typeof f === "string") {
668
- paragraph.addFragment(f);
669
- } else {
670
- const { content: content2, ...fStyle } = f;
671
- if (content2 !== void 0) {
672
- paragraph.addFragment(content2, fStyle);
673
- }
674
- }
675
- });
676
- paragraphs.push(paragraph);
677
- } else if ("fragments" in p) {
678
- const { fragments, ...pStyle } = p;
679
- const paragraph = new Paragraph(pStyle, style);
680
- fragments.forEach((f) => {
681
- const { content: content2, ...fStyle } = f;
682
- if (content2 !== void 0) {
683
- paragraph.addFragment(content2, fStyle);
684
- }
685
- });
686
- paragraphs.push(paragraph);
687
- } else if ("content" in p) {
688
- const { content: pData, ...pStyle } = p;
689
- if (pData !== void 0) {
690
- const paragraph = new Paragraph(pStyle, style);
691
- paragraph.addFragment(pData);
692
- paragraphs.push(paragraph);
693
- }
694
- }
695
- }
696
- }
697
- return paragraphs;
698
- }
699
- }
700
-
701
- function definePlugin(options) {
702
- return options;
703
- }
704
-
705
658
  const defaultReferImage = "data:image/svg+xml;base64,PHN2ZyB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIHdpZHRoPSI3MiIgaGVpZ2h0PSI3MiIgdmlld0JveD0iMCAwIDcyIDcyIiBmaWxsPSJub25lIj48cGF0aCBmaWxsLXJ1bGU9ImV2ZW5vZGQiIGNsaXAtcnVsZT0iZXZlbm9kZCIgZD0iTTMyLjQwMjkgMjhIMzUuMTU5NFYzMy4xNzcxQzM1Ljk4MjEgMzIuMzExNSAzNi45NzEgMzEuODczNyAzOC4wOTQ4IDMxLjg3MzdDMzkuNjY3NiAzMS44NzM3IDQwLjkxNjYgMzIuNDI5NSA0MS44MzkgMzMuNTQzN0w0MS44NDAzIDMzLjU0NTND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SIjMjIyNTI5Ii8+PHBhdGggZmlsbC1ydWxlPSJldmVub2RkIiBjbGlwLXJ1bGU9ImV2ZW5vZGQiIGQ9Ik0yNS43NTYxIDI4LjI3NTNIMjIuNzQ0TDE3IDQyLjcyNDdIMjAuMDE0MUwyMS4zNDI5IDM5LjIwNDlIMjcuMTU3MkwyOC40ODYgNDIuNzI0N0gzMS41MDAxTDI1Ljc1NjEgMjguMjc1M1pNMjIuMjEyNSAzNi45MDc2TDI0LjI1OTYgMzEuNDUzOUwyNi4yODg1IDM2LjkwNzZIMjIuMjEyNVoiIGZpbGw9IiMyMjI1MjkiLz48L3N2Zz4=";
706
659
  function parseCharsPerRepeat(size, fontSize, total) {
707
660
  if (size === "cover") {
@@ -1056,7 +1009,7 @@ const defaultTextStyles = {
1056
1009
  // font
1057
1010
  fontSize: 14,
1058
1011
  fontWeight: "normal",
1059
- fontFamily: "_fallback",
1012
+ fontFamily: "",
1060
1013
  fontStyle: "normal",
1061
1014
  fontKerning: "normal",
1062
1015
  // text
@@ -1107,7 +1060,6 @@ class Text {
1107
1060
  __publicField(this, "glyphBox", new BoundingBox());
1108
1061
  __publicField(this, "pathBox", new BoundingBox());
1109
1062
  __publicField(this, "boundingBox", new BoundingBox());
1110
- __publicField(this, "parser", new Parser(this));
1111
1063
  __publicField(this, "measurer", new Measurer(this));
1112
1064
  __publicField(this, "plugins", /* @__PURE__ */ new Map());
1113
1065
  __publicField(this, "fonts");
@@ -1133,11 +1085,57 @@ class Text {
1133
1085
  return this;
1134
1086
  }
1135
1087
  updateParagraphs() {
1136
- this.paragraphs = this.parser.parse();
1088
+ this.computedStyle = { ...defaultTextStyles, ...this.style };
1089
+ let { content, computedStyle: style } = this;
1090
+ const paragraphs = [];
1091
+ if (typeof content === "string") {
1092
+ const paragraph = new Paragraph({}, style);
1093
+ paragraph.addFragment(content);
1094
+ paragraphs.push(paragraph);
1095
+ } else {
1096
+ content = Array.isArray(content) ? content : [content];
1097
+ for (const p of content) {
1098
+ if (typeof p === "string") {
1099
+ const paragraph = new Paragraph({}, style);
1100
+ paragraph.addFragment(p);
1101
+ paragraphs.push(paragraph);
1102
+ } else if (Array.isArray(p)) {
1103
+ const paragraph = new Paragraph({}, style);
1104
+ p.forEach((f) => {
1105
+ if (typeof f === "string") {
1106
+ paragraph.addFragment(f);
1107
+ } else {
1108
+ const { content: content2, ...fStyle } = f;
1109
+ if (content2 !== void 0) {
1110
+ paragraph.addFragment(content2, fStyle);
1111
+ }
1112
+ }
1113
+ });
1114
+ paragraphs.push(paragraph);
1115
+ } else if ("fragments" in p) {
1116
+ const { fragments, ...pStyle } = p;
1117
+ const paragraph = new Paragraph(pStyle, style);
1118
+ fragments.forEach((f) => {
1119
+ const { content: content2, ...fStyle } = f;
1120
+ if (content2 !== void 0) {
1121
+ paragraph.addFragment(content2, fStyle);
1122
+ }
1123
+ });
1124
+ paragraphs.push(paragraph);
1125
+ } else if ("content" in p) {
1126
+ const { content: pData, ...pStyle } = p;
1127
+ if (pData !== void 0) {
1128
+ const paragraph = new Paragraph(pStyle, style);
1129
+ paragraph.addFragment(pData);
1130
+ paragraphs.push(paragraph);
1131
+ }
1132
+ }
1133
+ }
1134
+ }
1135
+ this.paragraphs = paragraphs;
1137
1136
  return this;
1138
1137
  }
1139
1138
  measure(dom = this.measureDom) {
1140
- this.computedStyle = { ...defaultTextStyles, ...this.style };
1141
1139
  const old = {
1142
1140
  paragraphs: this.paragraphs,
1143
1141
  lineBox: this.lineBox,
@@ -1257,4 +1255,4 @@ function renderText(options) {
1257
1255
  return new Text(options).render(options);
1258
1256
  }
1259
1257
 
1260
- 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.7",
4
+ "version": "0.3.9",
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": {