modern-text 0.6.2 → 0.7.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
@@ -101,10 +101,10 @@ function uploadColors(ctx, text) {
101
101
  });
102
102
  }
103
103
 
104
- var __defProp$4 = Object.defineProperty;
105
- var __defNormalProp$4 = (obj, key, value) => key in obj ? __defProp$4(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
106
- var __publicField$4 = (obj, key, value) => {
107
- __defNormalProp$4(obj, typeof key !== "symbol" ? key + "" : key, value);
104
+ var __defProp$5 = Object.defineProperty;
105
+ var __defNormalProp$5 = (obj, key, value) => key in obj ? __defProp$5(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
106
+ var __publicField$5 = (obj, key, value) => {
107
+ __defNormalProp$5(obj, typeof key !== "symbol" ? key + "" : key, value);
108
108
  return value;
109
109
  };
110
110
  const set1 = /* @__PURE__ */ new Set(["\xA9", "\xAE", "\xF7"]);
@@ -146,28 +146,28 @@ class Character {
146
146
  this.content = content;
147
147
  this.index = index;
148
148
  this.parent = parent;
149
- __publicField$4(this, "path", new Path2D());
150
- __publicField$4(this, "lineBox", new BoundingBox());
151
- __publicField$4(this, "inlineBox", new BoundingBox());
152
- __publicField$4(this, "glyphBox");
153
- __publicField$4(this, "advanceWidth", 0);
154
- __publicField$4(this, "advanceHeight", 0);
155
- __publicField$4(this, "underlinePosition", 0);
156
- __publicField$4(this, "underlineThickness", 0);
157
- __publicField$4(this, "strikeoutPosition", 0);
158
- __publicField$4(this, "strikeoutSize", 0);
159
- __publicField$4(this, "ascender", 0);
160
- __publicField$4(this, "descender", 0);
161
- __publicField$4(this, "typoAscender", 0);
162
- __publicField$4(this, "typoDescender", 0);
163
- __publicField$4(this, "typoLineGap", 0);
164
- __publicField$4(this, "winAscent", 0);
165
- __publicField$4(this, "winDescent", 0);
166
- __publicField$4(this, "xHeight", 0);
167
- __publicField$4(this, "capHeight", 0);
168
- __publicField$4(this, "baseline", 0);
169
- __publicField$4(this, "centerDiviation", 0);
170
- __publicField$4(this, "fontStyle");
149
+ __publicField$5(this, "path", new Path2D());
150
+ __publicField$5(this, "lineBox", new BoundingBox());
151
+ __publicField$5(this, "inlineBox", new BoundingBox());
152
+ __publicField$5(this, "glyphBox");
153
+ __publicField$5(this, "advanceWidth", 0);
154
+ __publicField$5(this, "advanceHeight", 0);
155
+ __publicField$5(this, "underlinePosition", 0);
156
+ __publicField$5(this, "underlineThickness", 0);
157
+ __publicField$5(this, "strikeoutPosition", 0);
158
+ __publicField$5(this, "strikeoutSize", 0);
159
+ __publicField$5(this, "ascender", 0);
160
+ __publicField$5(this, "descender", 0);
161
+ __publicField$5(this, "typoAscender", 0);
162
+ __publicField$5(this, "typoDescender", 0);
163
+ __publicField$5(this, "typoLineGap", 0);
164
+ __publicField$5(this, "winAscent", 0);
165
+ __publicField$5(this, "winDescent", 0);
166
+ __publicField$5(this, "xHeight", 0);
167
+ __publicField$5(this, "capHeight", 0);
168
+ __publicField$5(this, "baseline", 0);
169
+ __publicField$5(this, "centerDiviation", 0);
170
+ __publicField$5(this, "fontStyle");
171
171
  }
172
172
  get center() {
173
173
  return this.glyphBox?.center;
@@ -458,10 +458,10 @@ function needsFetch(source) {
458
458
  return source.startsWith("http://") || source.startsWith("https://") || source.startsWith("blob://");
459
459
  }
460
460
 
461
- var __defProp$3 = Object.defineProperty;
462
- var __defNormalProp$3 = (obj, key, value) => key in obj ? __defProp$3(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
463
- var __publicField$3 = (obj, key, value) => {
464
- __defNormalProp$3(obj, typeof key !== "symbol" ? key + "" : key, value);
461
+ var __defProp$4 = Object.defineProperty;
462
+ var __defNormalProp$4 = (obj, key, value) => key in obj ? __defProp$4(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
463
+ var __publicField$4 = (obj, key, value) => {
464
+ __defNormalProp$4(obj, typeof key !== "symbol" ? key + "" : key, value);
465
465
  return value;
466
466
  };
467
467
  class Fragment {
@@ -469,7 +469,7 @@ class Fragment {
469
469
  this.content = content;
470
470
  this.style = style;
471
471
  this.parent = parent;
472
- __publicField$3(this, "inlineBox", new BoundingBox());
472
+ __publicField$4(this, "inlineBox", new BoundingBox());
473
473
  this.updateComputedStyle().initCharacters();
474
474
  }
475
475
  get computedContent() {
@@ -494,18 +494,18 @@ class Fragment {
494
494
  }
495
495
  }
496
496
 
497
- var __defProp$2 = Object.defineProperty;
498
- var __defNormalProp$2 = (obj, key, value) => key in obj ? __defProp$2(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
499
- var __publicField$2 = (obj, key, value) => {
500
- __defNormalProp$2(obj, typeof key !== "symbol" ? key + "" : key, value);
497
+ var __defProp$3 = Object.defineProperty;
498
+ var __defNormalProp$3 = (obj, key, value) => key in obj ? __defProp$3(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
499
+ var __publicField$3 = (obj, key, value) => {
500
+ __defNormalProp$3(obj, typeof key !== "symbol" ? key + "" : key, value);
501
501
  return value;
502
502
  };
503
503
  class Paragraph {
504
504
  constructor(style, parentStyle) {
505
505
  this.style = style;
506
506
  this.parentStyle = parentStyle;
507
- __publicField$2(this, "lineBox", new BoundingBox());
508
- __publicField$2(this, "fragments", []);
507
+ __publicField$3(this, "lineBox", new BoundingBox());
508
+ __publicField$3(this, "fragments", []);
509
509
  this.updateComputedStyle();
510
510
  }
511
511
  updateComputedStyle() {
@@ -526,21 +526,17 @@ function definePlugin(options) {
526
526
  return options;
527
527
  }
528
528
 
529
- class Measurer {
529
+ var __defProp$2 = Object.defineProperty;
530
+ var __defNormalProp$2 = (obj, key, value) => key in obj ? __defProp$2(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
531
+ var __publicField$2 = (obj, key, value) => {
532
+ __defNormalProp$2(obj, typeof key !== "symbol" ? key + "" : key, value);
533
+ return value;
534
+ };
535
+ const _Measurer = class _Measurer {
530
536
  _styleToDomStyle(style) {
531
537
  const _style = { ...style };
532
538
  for (const key in style) {
533
- if ([
534
- "width",
535
- "height",
536
- "fontSize",
537
- "letterSpacing",
538
- "textStrokeWidth",
539
- "textIndent",
540
- "shadowOffsetX",
541
- "shadowOffsetY",
542
- "shadowBlur"
543
- ].includes(key)) {
539
+ if (_Measurer.pxStyles.has(key)) {
544
540
  _style[key] = `${style[key]}px`;
545
541
  } else {
546
542
  _style[key] = style[key];
@@ -561,27 +557,64 @@ class Measurer {
561
557
  createParagraphDom(paragraphs, rootStyle) {
562
558
  const documentFragment = document.createDocumentFragment();
563
559
  const dom = document.createElement("section");
560
+ const style = { ...rootStyle };
561
+ const isHorizontal = rootStyle.writingMode.includes("horizontal");
562
+ switch (rootStyle.textAlign) {
563
+ case "start":
564
+ case "left":
565
+ style.justifyContent = "start";
566
+ break;
567
+ case "center":
568
+ style.justifyContent = "center";
569
+ break;
570
+ case "end":
571
+ case "right":
572
+ style.justifyContent = "end";
573
+ break;
574
+ }
575
+ switch (rootStyle.verticalAlign) {
576
+ case "top":
577
+ style.alignItems = "top";
578
+ break;
579
+ case "middle":
580
+ style.alignItems = "center";
581
+ break;
582
+ case "bottom":
583
+ style.alignItems = "end";
584
+ break;
585
+ }
586
+ const isFlex = Boolean(style.justifyContent || style.alignItems);
564
587
  Object.assign(dom.style, {
588
+ display: isFlex ? "inline-flex" : void 0,
565
589
  width: "max-content",
566
590
  height: "max-content",
567
591
  whiteSpace: "pre-wrap",
568
592
  wordBreak: "break-all",
569
- ...this._styleToDomStyle(rootStyle),
593
+ ...this._styleToDomStyle(style),
570
594
  position: "fixed",
571
595
  visibility: "hidden"
572
596
  });
573
597
  const ul = document.createElement("ul");
574
598
  Object.assign(ul.style, {
599
+ verticalAlign: "inherit",
575
600
  listStyleType: "inherit",
576
601
  padding: "0",
577
- margin: "0"
602
+ margin: "0",
603
+ width: isFlex && isHorizontal ? "100%" : void 0,
604
+ height: isFlex && !isHorizontal ? "100%" : void 0
578
605
  });
579
606
  paragraphs.forEach((paragraph) => {
580
607
  const li = document.createElement("li");
581
- Object.assign(li.style, this._styleToDomStyle(paragraph.style));
608
+ Object.assign(li.style, {
609
+ verticalAlign: "inherit",
610
+ ...this._styleToDomStyle(paragraph.style)
611
+ });
582
612
  paragraph.fragments.forEach((fragment) => {
583
613
  const span = document.createElement("span");
584
- Object.assign(span.style, this._styleToDomStyle(fragment.style));
614
+ Object.assign(span.style, {
615
+ verticalAlign: "inherit",
616
+ ...this._styleToDomStyle(fragment.style)
617
+ });
585
618
  span.appendChild(document.createTextNode(fragment.content));
586
619
  li.appendChild(span);
587
620
  });
@@ -746,7 +779,27 @@ class Measurer {
746
779
  destory?.();
747
780
  return result;
748
781
  }
749
- }
782
+ };
783
+ __publicField$2(_Measurer, "pxStyles", /* @__PURE__ */ new Set([
784
+ "width",
785
+ "height",
786
+ "fontSize",
787
+ "letterSpacing",
788
+ "textStrokeWidth",
789
+ "textIndent",
790
+ "shadowOffsetX",
791
+ "shadowOffsetY",
792
+ "shadowBlur",
793
+ "marginLeft",
794
+ "marginTop",
795
+ "marginRight",
796
+ "marginBottom",
797
+ "paddingLeft",
798
+ "paddingTop",
799
+ "paddingRight",
800
+ "paddingBottom"
801
+ ]));
802
+ let Measurer = _Measurer;
750
803
 
751
804
  var __defProp$1 = Object.defineProperty;
752
805
  var __defNormalProp$1 = (obj, key, value) => key in obj ? __defProp$1(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
@@ -1691,16 +1744,12 @@ class Text extends EventEmitter {
1691
1744
  }
1692
1745
  }
1693
1746
 
1694
- async function measureText(options) {
1695
- const text = new Text(options);
1696
- await text.load();
1697
- return text.measure();
1747
+ function measureText(options) {
1748
+ return new Text(options).measure();
1698
1749
  }
1699
1750
 
1700
- async function renderText(options) {
1701
- const text = new Text(options);
1702
- await text.load();
1703
- text.render(options);
1751
+ function renderText(options) {
1752
+ new Text(options).render(options);
1704
1753
  }
1705
1754
 
1706
1755
  export { Character, Fragment, Measurer, Paragraph, Text, defaultTextStyles, definePlugin, drawPath, filterEmpty, getTransform2D, hexToRgb, highlight, isEqualObject, isEqualValue, isNone, listStyle, measureText, needsFetch, parseColor, parseColormap, parseValueNumber, render, renderText, setupView, textDecoration, uploadColor, uploadColors };
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "modern-text",
3
3
  "type": "module",
4
- "version": "0.6.2",
4
+ "version": "0.7.0",
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",