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.cjs CHANGED
@@ -103,10 +103,10 @@ function uploadColors(ctx, text) {
103
103
  });
104
104
  }
105
105
 
106
- var __defProp$4 = Object.defineProperty;
107
- var __defNormalProp$4 = (obj, key, value) => key in obj ? __defProp$4(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
108
- var __publicField$4 = (obj, key, value) => {
109
- __defNormalProp$4(obj, typeof key !== "symbol" ? key + "" : key, value);
106
+ var __defProp$5 = Object.defineProperty;
107
+ var __defNormalProp$5 = (obj, key, value) => key in obj ? __defProp$5(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
108
+ var __publicField$5 = (obj, key, value) => {
109
+ __defNormalProp$5(obj, typeof key !== "symbol" ? key + "" : key, value);
110
110
  return value;
111
111
  };
112
112
  const set1 = /* @__PURE__ */ new Set(["\xA9", "\xAE", "\xF7"]);
@@ -148,28 +148,28 @@ class Character {
148
148
  this.content = content;
149
149
  this.index = index;
150
150
  this.parent = parent;
151
- __publicField$4(this, "path", new modernPath2d.Path2D());
152
- __publicField$4(this, "lineBox", new modernPath2d.BoundingBox());
153
- __publicField$4(this, "inlineBox", new modernPath2d.BoundingBox());
154
- __publicField$4(this, "glyphBox");
155
- __publicField$4(this, "advanceWidth", 0);
156
- __publicField$4(this, "advanceHeight", 0);
157
- __publicField$4(this, "underlinePosition", 0);
158
- __publicField$4(this, "underlineThickness", 0);
159
- __publicField$4(this, "strikeoutPosition", 0);
160
- __publicField$4(this, "strikeoutSize", 0);
161
- __publicField$4(this, "ascender", 0);
162
- __publicField$4(this, "descender", 0);
163
- __publicField$4(this, "typoAscender", 0);
164
- __publicField$4(this, "typoDescender", 0);
165
- __publicField$4(this, "typoLineGap", 0);
166
- __publicField$4(this, "winAscent", 0);
167
- __publicField$4(this, "winDescent", 0);
168
- __publicField$4(this, "xHeight", 0);
169
- __publicField$4(this, "capHeight", 0);
170
- __publicField$4(this, "baseline", 0);
171
- __publicField$4(this, "centerDiviation", 0);
172
- __publicField$4(this, "fontStyle");
151
+ __publicField$5(this, "path", new modernPath2d.Path2D());
152
+ __publicField$5(this, "lineBox", new modernPath2d.BoundingBox());
153
+ __publicField$5(this, "inlineBox", new modernPath2d.BoundingBox());
154
+ __publicField$5(this, "glyphBox");
155
+ __publicField$5(this, "advanceWidth", 0);
156
+ __publicField$5(this, "advanceHeight", 0);
157
+ __publicField$5(this, "underlinePosition", 0);
158
+ __publicField$5(this, "underlineThickness", 0);
159
+ __publicField$5(this, "strikeoutPosition", 0);
160
+ __publicField$5(this, "strikeoutSize", 0);
161
+ __publicField$5(this, "ascender", 0);
162
+ __publicField$5(this, "descender", 0);
163
+ __publicField$5(this, "typoAscender", 0);
164
+ __publicField$5(this, "typoDescender", 0);
165
+ __publicField$5(this, "typoLineGap", 0);
166
+ __publicField$5(this, "winAscent", 0);
167
+ __publicField$5(this, "winDescent", 0);
168
+ __publicField$5(this, "xHeight", 0);
169
+ __publicField$5(this, "capHeight", 0);
170
+ __publicField$5(this, "baseline", 0);
171
+ __publicField$5(this, "centerDiviation", 0);
172
+ __publicField$5(this, "fontStyle");
173
173
  }
174
174
  get center() {
175
175
  return this.glyphBox?.center;
@@ -460,10 +460,10 @@ function needsFetch(source) {
460
460
  return source.startsWith("http://") || source.startsWith("https://") || source.startsWith("blob://");
461
461
  }
462
462
 
463
- var __defProp$3 = Object.defineProperty;
464
- var __defNormalProp$3 = (obj, key, value) => key in obj ? __defProp$3(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
465
- var __publicField$3 = (obj, key, value) => {
466
- __defNormalProp$3(obj, typeof key !== "symbol" ? key + "" : key, value);
463
+ var __defProp$4 = Object.defineProperty;
464
+ var __defNormalProp$4 = (obj, key, value) => key in obj ? __defProp$4(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
465
+ var __publicField$4 = (obj, key, value) => {
466
+ __defNormalProp$4(obj, typeof key !== "symbol" ? key + "" : key, value);
467
467
  return value;
468
468
  };
469
469
  class Fragment {
@@ -471,7 +471,7 @@ class Fragment {
471
471
  this.content = content;
472
472
  this.style = style;
473
473
  this.parent = parent;
474
- __publicField$3(this, "inlineBox", new modernPath2d.BoundingBox());
474
+ __publicField$4(this, "inlineBox", new modernPath2d.BoundingBox());
475
475
  this.updateComputedStyle().initCharacters();
476
476
  }
477
477
  get computedContent() {
@@ -496,18 +496,18 @@ class Fragment {
496
496
  }
497
497
  }
498
498
 
499
- var __defProp$2 = Object.defineProperty;
500
- var __defNormalProp$2 = (obj, key, value) => key in obj ? __defProp$2(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
501
- var __publicField$2 = (obj, key, value) => {
502
- __defNormalProp$2(obj, typeof key !== "symbol" ? key + "" : key, value);
499
+ var __defProp$3 = Object.defineProperty;
500
+ var __defNormalProp$3 = (obj, key, value) => key in obj ? __defProp$3(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
501
+ var __publicField$3 = (obj, key, value) => {
502
+ __defNormalProp$3(obj, typeof key !== "symbol" ? key + "" : key, value);
503
503
  return value;
504
504
  };
505
505
  class Paragraph {
506
506
  constructor(style, parentStyle) {
507
507
  this.style = style;
508
508
  this.parentStyle = parentStyle;
509
- __publicField$2(this, "lineBox", new modernPath2d.BoundingBox());
510
- __publicField$2(this, "fragments", []);
509
+ __publicField$3(this, "lineBox", new modernPath2d.BoundingBox());
510
+ __publicField$3(this, "fragments", []);
511
511
  this.updateComputedStyle();
512
512
  }
513
513
  updateComputedStyle() {
@@ -528,21 +528,17 @@ function definePlugin(options) {
528
528
  return options;
529
529
  }
530
530
 
531
- class Measurer {
531
+ var __defProp$2 = Object.defineProperty;
532
+ var __defNormalProp$2 = (obj, key, value) => key in obj ? __defProp$2(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
533
+ var __publicField$2 = (obj, key, value) => {
534
+ __defNormalProp$2(obj, typeof key !== "symbol" ? key + "" : key, value);
535
+ return value;
536
+ };
537
+ const _Measurer = class _Measurer {
532
538
  _styleToDomStyle(style) {
533
539
  const _style = { ...style };
534
540
  for (const key in style) {
535
- if ([
536
- "width",
537
- "height",
538
- "fontSize",
539
- "letterSpacing",
540
- "textStrokeWidth",
541
- "textIndent",
542
- "shadowOffsetX",
543
- "shadowOffsetY",
544
- "shadowBlur"
545
- ].includes(key)) {
541
+ if (_Measurer.pxStyles.has(key)) {
546
542
  _style[key] = `${style[key]}px`;
547
543
  } else {
548
544
  _style[key] = style[key];
@@ -563,27 +559,64 @@ class Measurer {
563
559
  createParagraphDom(paragraphs, rootStyle) {
564
560
  const documentFragment = document.createDocumentFragment();
565
561
  const dom = document.createElement("section");
562
+ const style = { ...rootStyle };
563
+ const isHorizontal = rootStyle.writingMode.includes("horizontal");
564
+ switch (rootStyle.textAlign) {
565
+ case "start":
566
+ case "left":
567
+ style.justifyContent = "start";
568
+ break;
569
+ case "center":
570
+ style.justifyContent = "center";
571
+ break;
572
+ case "end":
573
+ case "right":
574
+ style.justifyContent = "end";
575
+ break;
576
+ }
577
+ switch (rootStyle.verticalAlign) {
578
+ case "top":
579
+ style.alignItems = "top";
580
+ break;
581
+ case "middle":
582
+ style.alignItems = "center";
583
+ break;
584
+ case "bottom":
585
+ style.alignItems = "end";
586
+ break;
587
+ }
588
+ const isFlex = Boolean(style.justifyContent || style.alignItems);
566
589
  Object.assign(dom.style, {
590
+ display: isFlex ? "inline-flex" : void 0,
567
591
  width: "max-content",
568
592
  height: "max-content",
569
593
  whiteSpace: "pre-wrap",
570
594
  wordBreak: "break-all",
571
- ...this._styleToDomStyle(rootStyle),
595
+ ...this._styleToDomStyle(style),
572
596
  position: "fixed",
573
597
  visibility: "hidden"
574
598
  });
575
599
  const ul = document.createElement("ul");
576
600
  Object.assign(ul.style, {
601
+ verticalAlign: "inherit",
577
602
  listStyleType: "inherit",
578
603
  padding: "0",
579
- margin: "0"
604
+ margin: "0",
605
+ width: isFlex && isHorizontal ? "100%" : void 0,
606
+ height: isFlex && !isHorizontal ? "100%" : void 0
580
607
  });
581
608
  paragraphs.forEach((paragraph) => {
582
609
  const li = document.createElement("li");
583
- Object.assign(li.style, this._styleToDomStyle(paragraph.style));
610
+ Object.assign(li.style, {
611
+ verticalAlign: "inherit",
612
+ ...this._styleToDomStyle(paragraph.style)
613
+ });
584
614
  paragraph.fragments.forEach((fragment) => {
585
615
  const span = document.createElement("span");
586
- Object.assign(span.style, this._styleToDomStyle(fragment.style));
616
+ Object.assign(span.style, {
617
+ verticalAlign: "inherit",
618
+ ...this._styleToDomStyle(fragment.style)
619
+ });
587
620
  span.appendChild(document.createTextNode(fragment.content));
588
621
  li.appendChild(span);
589
622
  });
@@ -748,7 +781,27 @@ class Measurer {
748
781
  destory?.();
749
782
  return result;
750
783
  }
751
- }
784
+ };
785
+ __publicField$2(_Measurer, "pxStyles", /* @__PURE__ */ new Set([
786
+ "width",
787
+ "height",
788
+ "fontSize",
789
+ "letterSpacing",
790
+ "textStrokeWidth",
791
+ "textIndent",
792
+ "shadowOffsetX",
793
+ "shadowOffsetY",
794
+ "shadowBlur",
795
+ "marginLeft",
796
+ "marginTop",
797
+ "marginRight",
798
+ "marginBottom",
799
+ "paddingLeft",
800
+ "paddingTop",
801
+ "paddingRight",
802
+ "paddingBottom"
803
+ ]));
804
+ let Measurer = _Measurer;
752
805
 
753
806
  var __defProp$1 = Object.defineProperty;
754
807
  var __defNormalProp$1 = (obj, key, value) => key in obj ? __defProp$1(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
@@ -1693,16 +1746,12 @@ class Text extends EventEmitter {
1693
1746
  }
1694
1747
  }
1695
1748
 
1696
- async function measureText(options) {
1697
- const text = new Text(options);
1698
- await text.load();
1699
- return text.measure();
1749
+ function measureText(options) {
1750
+ return new Text(options).measure();
1700
1751
  }
1701
1752
 
1702
- async function renderText(options) {
1703
- const text = new Text(options);
1704
- await text.load();
1705
- text.render(options);
1753
+ function renderText(options) {
1754
+ new Text(options).render(options);
1706
1755
  }
1707
1756
 
1708
1757
  exports.Character = Character;
package/dist/index.d.cts CHANGED
@@ -121,7 +121,8 @@ interface MeasureDomResult {
121
121
  boundingBox: BoundingBox;
122
122
  }
123
123
  declare class Measurer {
124
- protected _styleToDomStyle(style: Partial<TextStyle>): Record<string, any>;
124
+ static pxStyles: Set<string>;
125
+ protected _styleToDomStyle(style: Record<string, any>): Record<string, any>;
125
126
  /**
126
127
  * <section style="...">
127
128
  * <ul>
@@ -284,6 +285,16 @@ interface TextDrawStyle {
284
285
  skewY: number;
285
286
  }
286
287
  interface TextStyle extends TextLineStyle, TextInlineStyle, TextDrawStyle {
288
+ marginLeft?: number;
289
+ marginTop?: number;
290
+ marginRight?: number;
291
+ marginBottom?: number;
292
+ paddingLeft?: number;
293
+ paddingTop?: number;
294
+ paddingRight?: number;
295
+ paddingBottom?: number;
296
+ width?: number;
297
+ height?: number;
287
298
  }
288
299
  interface FragmentContent extends Partial<TextStyle> {
289
300
  content: string;
@@ -338,9 +349,9 @@ declare function uploadColors(ctx: CanvasRenderingContext2D, text: Text$1): void
338
349
 
339
350
  declare function definePlugin(options: TextPlugin): TextPlugin;
340
351
 
341
- declare function measureText(options: TextOptions): Promise<MeasureResult>;
352
+ declare function measureText(options: TextOptions): MeasureResult;
342
353
 
343
- declare function renderText(options: TextOptions & TextRenderOptions): Promise<void>;
354
+ declare function renderText(options: TextOptions & TextRenderOptions): void;
344
355
 
345
356
  declare function highlight(): TextPlugin;
346
357
 
package/dist/index.d.mts CHANGED
@@ -121,7 +121,8 @@ interface MeasureDomResult {
121
121
  boundingBox: BoundingBox;
122
122
  }
123
123
  declare class Measurer {
124
- protected _styleToDomStyle(style: Partial<TextStyle>): Record<string, any>;
124
+ static pxStyles: Set<string>;
125
+ protected _styleToDomStyle(style: Record<string, any>): Record<string, any>;
125
126
  /**
126
127
  * <section style="...">
127
128
  * <ul>
@@ -284,6 +285,16 @@ interface TextDrawStyle {
284
285
  skewY: number;
285
286
  }
286
287
  interface TextStyle extends TextLineStyle, TextInlineStyle, TextDrawStyle {
288
+ marginLeft?: number;
289
+ marginTop?: number;
290
+ marginRight?: number;
291
+ marginBottom?: number;
292
+ paddingLeft?: number;
293
+ paddingTop?: number;
294
+ paddingRight?: number;
295
+ paddingBottom?: number;
296
+ width?: number;
297
+ height?: number;
287
298
  }
288
299
  interface FragmentContent extends Partial<TextStyle> {
289
300
  content: string;
@@ -338,9 +349,9 @@ declare function uploadColors(ctx: CanvasRenderingContext2D, text: Text$1): void
338
349
 
339
350
  declare function definePlugin(options: TextPlugin): TextPlugin;
340
351
 
341
- declare function measureText(options: TextOptions): Promise<MeasureResult>;
352
+ declare function measureText(options: TextOptions): MeasureResult;
342
353
 
343
- declare function renderText(options: TextOptions & TextRenderOptions): Promise<void>;
354
+ declare function renderText(options: TextOptions & TextRenderOptions): void;
344
355
 
345
356
  declare function highlight(): TextPlugin;
346
357
 
package/dist/index.d.ts CHANGED
@@ -121,7 +121,8 @@ interface MeasureDomResult {
121
121
  boundingBox: BoundingBox;
122
122
  }
123
123
  declare class Measurer {
124
- protected _styleToDomStyle(style: Partial<TextStyle>): Record<string, any>;
124
+ static pxStyles: Set<string>;
125
+ protected _styleToDomStyle(style: Record<string, any>): Record<string, any>;
125
126
  /**
126
127
  * <section style="...">
127
128
  * <ul>
@@ -284,6 +285,16 @@ interface TextDrawStyle {
284
285
  skewY: number;
285
286
  }
286
287
  interface TextStyle extends TextLineStyle, TextInlineStyle, TextDrawStyle {
288
+ marginLeft?: number;
289
+ marginTop?: number;
290
+ marginRight?: number;
291
+ marginBottom?: number;
292
+ paddingLeft?: number;
293
+ paddingTop?: number;
294
+ paddingRight?: number;
295
+ paddingBottom?: number;
296
+ width?: number;
297
+ height?: number;
287
298
  }
288
299
  interface FragmentContent extends Partial<TextStyle> {
289
300
  content: string;
@@ -338,9 +349,9 @@ declare function uploadColors(ctx: CanvasRenderingContext2D, text: Text$1): void
338
349
 
339
350
  declare function definePlugin(options: TextPlugin): TextPlugin;
340
351
 
341
- declare function measureText(options: TextOptions): Promise<MeasureResult>;
352
+ declare function measureText(options: TextOptions): MeasureResult;
342
353
 
343
- declare function renderText(options: TextOptions & TextRenderOptions): Promise<void>;
354
+ declare function renderText(options: TextOptions & TextRenderOptions): void;
344
355
 
345
356
  declare function highlight(): TextPlugin;
346
357