modern-text 0.7.2 → 0.7.4

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,12 +103,6 @@ function uploadColors(ctx, text) {
103
103
  });
104
104
  }
105
105
 
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
- return value;
111
- };
112
106
  const set1 = /* @__PURE__ */ new Set(["\xA9", "\xAE", "\xF7"]);
113
107
  const set2 = /* @__PURE__ */ new Set([
114
108
  "\u2014",
@@ -148,29 +142,29 @@ class Character {
148
142
  this.content = content;
149
143
  this.index = index;
150
144
  this.parent = parent;
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
145
  }
146
+ path = new modernPath2d.Path2D();
147
+ lineBox = new modernPath2d.BoundingBox();
148
+ inlineBox = new modernPath2d.BoundingBox();
149
+ glyphBox;
150
+ advanceWidth = 0;
151
+ advanceHeight = 0;
152
+ underlinePosition = 0;
153
+ underlineThickness = 0;
154
+ strikeoutPosition = 0;
155
+ strikeoutSize = 0;
156
+ ascender = 0;
157
+ descender = 0;
158
+ typoAscender = 0;
159
+ typoDescender = 0;
160
+ typoLineGap = 0;
161
+ winAscent = 0;
162
+ winDescent = 0;
163
+ xHeight = 0;
164
+ capHeight = 0;
165
+ baseline = 0;
166
+ centerDiviation = 0;
167
+ fontStyle;
174
168
  get center() {
175
169
  return this.glyphBox?.center;
176
170
  }
@@ -460,20 +454,14 @@ function needsFetch(source) {
460
454
  return source.startsWith("http://") || source.startsWith("https://") || source.startsWith("blob://");
461
455
  }
462
456
 
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
- return value;
468
- };
469
457
  class Fragment {
470
458
  constructor(content, style = {}, parent) {
471
459
  this.content = content;
472
460
  this.style = style;
473
461
  this.parent = parent;
474
- __publicField$4(this, "inlineBox", new modernPath2d.BoundingBox());
475
462
  this.updateComputedStyle().initCharacters();
476
463
  }
464
+ inlineBox = new modernPath2d.BoundingBox();
477
465
  get computedContent() {
478
466
  const style = this.computedStyle;
479
467
  return style.textTransform === "uppercase" ? this.content.toUpperCase() : style.textTransform === "lowercase" ? this.content.toLowerCase() : this.content;
@@ -496,20 +484,14 @@ class Fragment {
496
484
  }
497
485
  }
498
486
 
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
- return value;
504
- };
505
487
  class Paragraph {
506
488
  constructor(style, parentStyle) {
507
489
  this.style = style;
508
490
  this.parentStyle = parentStyle;
509
- __publicField$3(this, "lineBox", new modernPath2d.BoundingBox());
510
- __publicField$3(this, "fragments", []);
511
491
  this.updateComputedStyle();
512
492
  }
493
+ lineBox = new modernPath2d.BoundingBox();
494
+ fragments = [];
513
495
  updateComputedStyle() {
514
496
  this.computedStyle = {
515
497
  ...filterEmpty(this.parentStyle),
@@ -528,17 +510,30 @@ function definePlugin(options) {
528
510
  return options;
529
511
  }
530
512
 
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 {
513
+ class Measurer {
514
+ static pxStyles = /* @__PURE__ */ new Set([
515
+ "width",
516
+ "height",
517
+ "fontSize",
518
+ "letterSpacing",
519
+ "textStrokeWidth",
520
+ "textIndent",
521
+ "shadowOffsetX",
522
+ "shadowOffsetY",
523
+ "shadowBlur",
524
+ "marginLeft",
525
+ "marginTop",
526
+ "marginRight",
527
+ "marginBottom",
528
+ "paddingLeft",
529
+ "paddingTop",
530
+ "paddingRight",
531
+ "paddingBottom"
532
+ ]);
538
533
  _styleToDomStyle(style) {
539
534
  const _style = { ...style };
540
535
  for (const key in style) {
541
- if (_Measurer.pxStyles.has(key)) {
536
+ if (Measurer.pxStyles.has(key)) {
542
537
  _style[key] = `${style[key]}px`;
543
538
  } else {
544
539
  _style[key] = style[key];
@@ -782,38 +777,10 @@ const _Measurer = class _Measurer {
782
777
  destory?.();
783
778
  return result;
784
779
  }
785
- };
786
- __publicField$2(_Measurer, "pxStyles", /* @__PURE__ */ new Set([
787
- "width",
788
- "height",
789
- "fontSize",
790
- "letterSpacing",
791
- "textStrokeWidth",
792
- "textIndent",
793
- "shadowOffsetX",
794
- "shadowOffsetY",
795
- "shadowBlur",
796
- "marginLeft",
797
- "marginTop",
798
- "marginRight",
799
- "marginBottom",
800
- "paddingLeft",
801
- "paddingTop",
802
- "paddingRight",
803
- "paddingBottom"
804
- ]));
805
- let Measurer = _Measurer;
780
+ }
806
781
 
807
- var __defProp$1 = Object.defineProperty;
808
- var __defNormalProp$1 = (obj, key, value) => key in obj ? __defProp$1(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
809
- var __publicField$1 = (obj, key, value) => {
810
- __defNormalProp$1(obj, typeof key !== "symbol" ? key + "" : key, value);
811
- return value;
812
- };
813
782
  class EventEmitter {
814
- constructor() {
815
- __publicField$1(this, "eventListeners", /* @__PURE__ */ new Map());
816
- }
783
+ eventListeners = /* @__PURE__ */ new Map();
817
784
  addEventListener(event, listener, options) {
818
785
  const object = { value: listener, options };
819
786
  const listeners = this.eventListeners.get(event);
@@ -898,6 +865,25 @@ class EventEmitter {
898
865
  }
899
866
  }
900
867
 
868
+ function getHighlightStyle(style) {
869
+ const {
870
+ highlight: highlight2,
871
+ highlightImage,
872
+ highlightReferImage,
873
+ highlightColormap,
874
+ highlightLine,
875
+ highlightSize,
876
+ highlightThickness
877
+ } = style;
878
+ return {
879
+ image: highlight2?.image ?? highlightImage ?? "none",
880
+ referImage: highlight2?.referImage ?? highlightReferImage ?? "none",
881
+ colormap: highlight2?.colormap ?? highlightColormap ?? "none",
882
+ line: highlight2?.line ?? highlightLine ?? "none",
883
+ size: highlight2?.size ?? highlightSize ?? "cover",
884
+ thickness: highlight2?.thickness ?? highlightThickness ?? "100%"
885
+ };
886
+ }
901
887
  function highlight() {
902
888
  const paths = [];
903
889
  const clipRects = [];
@@ -930,45 +916,46 @@ function highlight() {
930
916
  name: "highlight",
931
917
  paths,
932
918
  load: async (text) => {
933
- const promises = [];
919
+ const set = /* @__PURE__ */ new Set();
934
920
  text.forEachCharacter((character) => {
935
921
  const { computedStyle: style } = character;
936
- const { highlightImage, highlightReferImage } = style;
937
- if (needsFetch(highlightImage)) {
938
- promises.push(loadSvg(highlightImage));
922
+ const { image, referImage } = getHighlightStyle(style);
923
+ if (needsFetch(image)) {
924
+ set.add(image);
939
925
  }
940
- if (needsFetch(highlightReferImage)) {
941
- promises.push(loadSvg(highlightReferImage));
926
+ if (needsFetch(referImage)) {
927
+ set.add(referImage);
942
928
  }
943
929
  });
944
- await Promise.all(promises);
930
+ await Promise.all(Array.from(set).map((src) => loadSvg(src)));
945
931
  },
946
932
  update: (text) => {
947
933
  clipRects.length = 0;
948
934
  paths.length = 0;
949
935
  let groups = [];
950
936
  let group;
951
- let prevStyle;
937
+ let prevHighlight;
952
938
  text.forEachCharacter((character) => {
953
939
  const {
954
940
  computedStyle: style
955
941
  } = character;
942
+ const highlight2 = getHighlightStyle(style);
956
943
  const {
957
- highlightImage
958
- } = style;
959
- if (!isNone(highlightImage)) {
944
+ image,
945
+ colormap,
946
+ line,
947
+ size,
948
+ thickness
949
+ } = highlight2;
950
+ if (!isNone(image)) {
960
951
  const {
961
952
  inlineBox,
962
953
  isVertical
963
954
  } = character;
964
955
  const {
965
- fontSize,
966
- highlightColormap,
967
- highlightLine,
968
- highlightSize,
969
- highlightThickness
956
+ fontSize
970
957
  } = style;
971
- if ((!prevStyle || isEqualValue(prevStyle.highlightImage, highlightImage) && isEqualValue(prevStyle.highlightColormap, highlightColormap) && isEqualValue(prevStyle.highlightLine, highlightLine) && isEqualValue(prevStyle.highlightSize, highlightSize) && isEqualValue(prevStyle.highlightThickness, highlightThickness)) && group?.length && (isVertical ? group[0].inlineBox.left === inlineBox.left : group[0].inlineBox.top === inlineBox.top) && group[0].fontSize === fontSize) {
958
+ if ((!prevHighlight || isEqualValue(prevHighlight.image, image) && isEqualValue(prevHighlight.colormap, colormap) && isEqualValue(prevHighlight.line, line) && isEqualValue(prevHighlight.size, size) && isEqualValue(prevHighlight.thickness, thickness)) && group?.length && (isVertical ? group[0].inlineBox.left === inlineBox.left : group[0].inlineBox.top === inlineBox.top) && group[0].fontSize === fontSize) {
972
959
  group.push(character);
973
960
  } else {
974
961
  group = [];
@@ -981,7 +968,7 @@ function highlight() {
981
968
  groups.push(group);
982
969
  }
983
970
  }
984
- prevStyle = style;
971
+ prevHighlight = highlight2;
985
972
  });
986
973
  groups = groups.filter((characters) => characters.length);
987
974
  for (let i = 0; i < groups.length; i++) {
@@ -995,18 +982,20 @@ function highlight() {
995
982
  } = char;
996
983
  const {
997
984
  fontSize,
998
- writingMode,
999
- highlightImage,
1000
- highlightReferImage,
1001
- highlightColormap,
1002
- highlightLine,
1003
- highlightSize,
1004
- highlightThickness
985
+ writingMode
1005
986
  } = style;
987
+ const {
988
+ image,
989
+ referImage,
990
+ colormap,
991
+ line,
992
+ size,
993
+ thickness
994
+ } = getHighlightStyle(style);
1006
995
  const isVertical = writingMode.includes("vertical");
1007
- const thickness = parseValueNumber(highlightThickness, { fontSize, total: groupBox.width }) / groupBox.width;
1008
- const colormap = parseColormap(highlightColormap);
1009
- const { paths: svgPaths, dom: svgDom } = getPaths(highlightImage);
996
+ const _thickness = parseValueNumber(thickness, { fontSize, total: groupBox.width }) / groupBox.width;
997
+ const _colormap = parseColormap(colormap);
998
+ const { paths: svgPaths, dom: svgDom } = getPaths(image);
1010
999
  const aBox = modernPath2d.getPathsBoundingBox(svgPaths, true);
1011
1000
  const styleScale = fontSize / aBox.width * 2;
1012
1001
  const cBox = new modernPath2d.BoundingBox().copy(groupBox);
@@ -1017,37 +1006,37 @@ function highlight() {
1017
1006
  }
1018
1007
  const rawWidth = Math.floor(cBox.width);
1019
1008
  let userWidth = rawWidth;
1020
- if (highlightSize !== "cover") {
1021
- userWidth = parseValueNumber(highlightSize, { fontSize, total: groupBox.width });
1009
+ if (size !== "cover") {
1010
+ userWidth = parseValueNumber(size, { fontSize, total: groupBox.width }) || rawWidth;
1022
1011
  cBox.width = userWidth;
1023
1012
  }
1024
- if (!isNone(highlightReferImage) && isNone(highlightLine)) {
1025
- const bBox = modernPath2d.getPathsBoundingBox(getPaths(highlightReferImage).paths, true);
1013
+ if (!isNone(referImage) && isNone(line)) {
1014
+ const bBox = modernPath2d.getPathsBoundingBox(getPaths(referImage).paths, true);
1026
1015
  aBox.copy(bBox);
1027
1016
  } else {
1028
- let line;
1029
- if (isNone(highlightLine)) {
1017
+ let _line;
1018
+ if (isNone(line)) {
1030
1019
  if (aBox.width / aBox.height > 4) {
1031
- line = "underline";
1020
+ _line = "underline";
1032
1021
  const viewBox = svgDom.getAttribute("viewBox");
1033
1022
  if (viewBox) {
1034
1023
  const [_x, y, _w, h] = viewBox.split(" ").map((v) => Number(v));
1035
1024
  const viewCenter = y + h / 2;
1036
1025
  if (aBox.y < viewCenter && aBox.y + aBox.height > viewCenter) {
1037
- line = "line-through";
1026
+ _line = "line-through";
1038
1027
  } else if (aBox.y + aBox.height < viewCenter) {
1039
- line = "overline";
1028
+ _line = "overline";
1040
1029
  } else {
1041
- line = "underline";
1030
+ _line = "underline";
1042
1031
  }
1043
1032
  }
1044
1033
  } else {
1045
- line = "outline";
1034
+ _line = "outline";
1046
1035
  }
1047
1036
  } else {
1048
- line = highlightLine;
1037
+ _line = line;
1049
1038
  }
1050
- switch (line) {
1039
+ switch (_line) {
1051
1040
  case "outline": {
1052
1041
  const paddingX = cBox.width * 0.2;
1053
1042
  const paddingY = cBox.height * 0.2;
@@ -1104,7 +1093,7 @@ function highlight() {
1104
1093
  svgPaths.forEach((originalPath) => {
1105
1094
  const path = originalPath.clone().matrix(_transform);
1106
1095
  if (path.style.strokeWidth) {
1107
- path.style.strokeWidth *= styleScale * thickness;
1096
+ path.style.strokeWidth *= styleScale * _thickness;
1108
1097
  }
1109
1098
  if (path.style.strokeMiterlimit) {
1110
1099
  path.style.strokeMiterlimit *= styleScale;
@@ -1115,11 +1104,11 @@ function highlight() {
1115
1104
  if (path.style.strokeDasharray) {
1116
1105
  path.style.strokeDasharray = path.style.strokeDasharray.map((v) => v * styleScale);
1117
1106
  }
1118
- if (path.style.fill && path.style.fill in colormap) {
1119
- path.style.fill = colormap[path.style.fill];
1107
+ if (path.style.fill && path.style.fill in _colormap) {
1108
+ path.style.fill = _colormap[path.style.fill];
1120
1109
  }
1121
- if (path.style.stroke && path.style.stroke in colormap) {
1122
- path.style.stroke = colormap[path.style.stroke];
1110
+ if (path.style.stroke && path.style.stroke in _colormap) {
1111
+ path.style.stroke = _colormap[path.style.stroke];
1123
1112
  }
1124
1113
  paths.push(path);
1125
1114
  if (rawWidth !== userWidth) {
@@ -1492,12 +1481,6 @@ function textDecoration() {
1492
1481
  });
1493
1482
  }
1494
1483
 
1495
- var __defProp = Object.defineProperty;
1496
- var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
1497
- var __publicField = (obj, key, value) => {
1498
- __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
1499
- return value;
1500
- };
1501
1484
  const defaultTextStyles = {
1502
1485
  writingMode: "horizontal-tb",
1503
1486
  verticalAlign: "baseline",
@@ -1528,13 +1511,6 @@ const defaultTextStyles = {
1528
1511
  listStyleColormap: "none",
1529
1512
  listStyleSize: "cover",
1530
1513
  listStylePosition: "outside",
1531
- // highlight
1532
- highlightImage: "none",
1533
- highlightReferImage: "none",
1534
- highlightColormap: "none",
1535
- highlightLine: "none",
1536
- highlightSize: "cover",
1537
- highlightThickness: "100%",
1538
1514
  // shadow
1539
1515
  shadowColor: "rgba(0, 0, 0, 0)",
1540
1516
  shadowOffsetX: 0,
@@ -1546,24 +1522,33 @@ const defaultTextStyles = {
1546
1522
  skewY: 0
1547
1523
  };
1548
1524
  class Text extends EventEmitter {
1525
+ debug;
1526
+ content;
1527
+ style;
1528
+ effects;
1529
+ measureDom;
1530
+ needsUpdate = true;
1531
+ computedStyle = { ...defaultTextStyles };
1532
+ paragraphs = [];
1533
+ lineBox = new modernPath2d.BoundingBox();
1534
+ rawGlyphBox = new modernPath2d.BoundingBox();
1535
+ glyphBox = new modernPath2d.BoundingBox();
1536
+ pathBox = new modernPath2d.BoundingBox();
1537
+ boundingBox = new modernPath2d.BoundingBox();
1538
+ measurer = new Measurer();
1539
+ plugins = /* @__PURE__ */ new Map();
1540
+ fonts;
1541
+ get fontSize() {
1542
+ return this.computedStyle.fontSize;
1543
+ }
1544
+ get isVertical() {
1545
+ return this.computedStyle.writingMode.includes("vertical");
1546
+ }
1547
+ get characters() {
1548
+ return this.paragraphs.flatMap((p) => p.fragments.flatMap((f) => f.characters));
1549
+ }
1549
1550
  constructor(options = {}) {
1550
1551
  super();
1551
- __publicField(this, "debug");
1552
- __publicField(this, "content");
1553
- __publicField(this, "style");
1554
- __publicField(this, "effects");
1555
- __publicField(this, "measureDom");
1556
- __publicField(this, "needsUpdate", true);
1557
- __publicField(this, "computedStyle", { ...defaultTextStyles });
1558
- __publicField(this, "paragraphs", []);
1559
- __publicField(this, "lineBox", new modernPath2d.BoundingBox());
1560
- __publicField(this, "rawGlyphBox", new modernPath2d.BoundingBox());
1561
- __publicField(this, "glyphBox", new modernPath2d.BoundingBox());
1562
- __publicField(this, "pathBox", new modernPath2d.BoundingBox());
1563
- __publicField(this, "boundingBox", new modernPath2d.BoundingBox());
1564
- __publicField(this, "measurer", new Measurer());
1565
- __publicField(this, "plugins", /* @__PURE__ */ new Map());
1566
- __publicField(this, "fonts");
1567
1552
  this.debug = options.debug ?? false;
1568
1553
  this.content = options.content ?? "";
1569
1554
  this.style = options.style ?? {};
@@ -1571,15 +1556,7 @@ class Text extends EventEmitter {
1571
1556
  this.effects = options.effects;
1572
1557
  this.fonts = options.fonts;
1573
1558
  this.use(listStyle()).use(textDecoration()).use(highlight()).use(render());
1574
- }
1575
- get fontSize() {
1576
- return this.computedStyle.fontSize;
1577
- }
1578
- get isVertical() {
1579
- return this.computedStyle.writingMode.includes("vertical");
1580
- }
1581
- get characters() {
1582
- return this.paragraphs.flatMap((p) => p.fragments.flatMap((f) => f.characters));
1559
+ this.updateParagraphs();
1583
1560
  }
1584
1561
  use(plugin) {
1585
1562
  this.plugins.set(plugin.name, plugin);
@@ -1730,6 +1707,7 @@ class Text extends EventEmitter {
1730
1707
  this[key] = result[key];
1731
1708
  }
1732
1709
  this.emit("update", { text: this });
1710
+ this.needsUpdate = false;
1733
1711
  return this;
1734
1712
  }
1735
1713
  render(options) {
@@ -1762,11 +1740,23 @@ class Text extends EventEmitter {
1762
1740
  }
1763
1741
 
1764
1742
  function measureText(options) {
1765
- return new Text(options).measure();
1743
+ const text = new Text(options);
1744
+ if (options.load) {
1745
+ return text.load().then(() => {
1746
+ return text.measure();
1747
+ });
1748
+ }
1749
+ return text.measure();
1766
1750
  }
1767
1751
 
1768
1752
  function renderText(options) {
1769
- new Text(options).render(options);
1753
+ const text = new Text(options);
1754
+ if (options.load) {
1755
+ return text.load().then(() => {
1756
+ return text.render(options);
1757
+ });
1758
+ }
1759
+ return text.render(options);
1770
1760
  }
1771
1761
 
1772
1762
  exports.Character = Character;
@@ -1778,6 +1768,7 @@ exports.defaultTextStyles = defaultTextStyles;
1778
1768
  exports.definePlugin = definePlugin;
1779
1769
  exports.drawPath = drawPath;
1780
1770
  exports.filterEmpty = filterEmpty;
1771
+ exports.getHighlightStyle = getHighlightStyle;
1781
1772
  exports.getTransform2D = getTransform2D;
1782
1773
  exports.hexToRgb = hexToRgb;
1783
1774
  exports.highlight = highlight;
package/dist/index.d.cts CHANGED
@@ -252,7 +252,24 @@ interface TextLineStyle {
252
252
  listStyleSize: ListStyleSize;
253
253
  listStylePosition: ListStylePosition;
254
254
  }
255
- interface TextInlineStyle {
255
+ interface HighlightStyle {
256
+ image: HighlightImage;
257
+ referImage: HighlightReferImage;
258
+ colormap: HighlightColormap;
259
+ line: HighlightLine;
260
+ size: HighlightSize;
261
+ thickness: HighlightThickness;
262
+ }
263
+ interface TextHighlightStyle {
264
+ highlight?: Partial<HighlightStyle>;
265
+ highlightImage?: HighlightImage;
266
+ highlightReferImage?: HighlightReferImage;
267
+ highlightColormap?: HighlightColormap;
268
+ highlightLine?: HighlightLine;
269
+ highlightSize?: HighlightSize;
270
+ highlightThickness?: HighlightThickness;
271
+ }
272
+ interface TextInlineStyle extends TextHighlightStyle {
256
273
  verticalAlign: VerticalAlign;
257
274
  letterSpacing: number;
258
275
  fontSize: number;
@@ -263,12 +280,6 @@ interface TextInlineStyle {
263
280
  textTransform: TextTransform;
264
281
  textOrientation: TextOrientation;
265
282
  textDecoration: TextDecorationLine;
266
- highlightImage: HighlightImage;
267
- highlightReferImage: HighlightReferImage;
268
- highlightColormap: HighlightColormap;
269
- highlightLine: HighlightLine;
270
- highlightSize: HighlightSize;
271
- highlightThickness: HighlightThickness;
272
283
  }
273
284
  interface TextDrawStyle {
274
285
  color: string | CanvasGradient | CanvasPattern;
@@ -349,10 +360,22 @@ declare function uploadColors(ctx: CanvasRenderingContext2D, text: Text$1): void
349
360
 
350
361
  declare function definePlugin(options: TextPlugin): TextPlugin;
351
362
 
352
- declare function measureText(options: TextOptions): MeasureResult;
363
+ declare function measureText(options: TextOptions & {
364
+ load: true;
365
+ }): Promise<MeasureResult>;
366
+ declare function measureText(options: TextOptions & {
367
+ load?: false;
368
+ }): MeasureResult;
353
369
 
354
- declare function renderText(options: TextOptions & TextRenderOptions): void;
370
+ type RenderTextOptions = TextOptions & TextRenderOptions;
371
+ declare function renderText(options: RenderTextOptions & {
372
+ load: true;
373
+ }): Promise<void>;
374
+ declare function renderText(options: RenderTextOptions & {
375
+ load?: false;
376
+ }): void;
355
377
 
378
+ declare function getHighlightStyle(style: TextStyle): HighlightStyle;
356
379
  declare function highlight(): TextPlugin;
357
380
 
358
381
  declare function listStyle(): TextPlugin;
@@ -375,4 +398,4 @@ declare function hexToRgb(hex: string): string | null;
375
398
  declare function filterEmpty(val: Record<string, any> | undefined): Record<string, any> | undefined;
376
399
  declare function needsFetch(source: string): boolean;
377
400
 
378
- export { Character, type DrawShapePathsOptions, type FontKerning, type FontStyle, type FontWeight, Fragment, type FragmentContent, type HighlightColormap, type HighlightImage, type HighlightLine, type HighlightReferImage, type HighlightSize, type HighlightThickness, type LinearGradient, type ListStyleColormap, type ListStyleImage, type ListStylePosition, type ListStyleSize, type ListStyleType, type MeasureDomResult, type MeasureResult, type MeasuredCharacter, type MeasuredFragment, type MeasuredParagraph, Measurer, Paragraph, type ParagraphContent, type Sizeable, Text$1 as Text, type TextAlign, type TextContent, type TextDecorationLine, type TextDrawStyle, type TextEventMap, type TextInlineStyle, type TextLineStyle, type TextOptions, type TextOrientation, type TextPlugin, type TextRenderOptions, type TextStyle, type TextTransform, type TextWrap, type VerticalAlign, type WritingMode, defaultTextStyles, definePlugin, drawPath, filterEmpty, getTransform2D, hexToRgb, highlight, isEqualObject, isEqualValue, isNone, listStyle, measureText, needsFetch, parseColor, parseColormap, parseValueNumber, render, renderText, setupView, textDecoration, uploadColor, uploadColors };
401
+ export { Character, type DrawShapePathsOptions, type FontKerning, type FontStyle, type FontWeight, Fragment, type FragmentContent, type HighlightColormap, type HighlightImage, type HighlightLine, type HighlightReferImage, type HighlightSize, type HighlightStyle, type HighlightThickness, type LinearGradient, type ListStyleColormap, type ListStyleImage, type ListStylePosition, type ListStyleSize, type ListStyleType, type MeasureDomResult, type MeasureResult, type MeasuredCharacter, type MeasuredFragment, type MeasuredParagraph, Measurer, Paragraph, type ParagraphContent, type RenderTextOptions, type Sizeable, Text$1 as Text, type TextAlign, type TextContent, type TextDecorationLine, type TextDrawStyle, type TextEventMap, type TextHighlightStyle, type TextInlineStyle, type TextLineStyle, type TextOptions, type TextOrientation, type TextPlugin, type TextRenderOptions, type TextStyle, type TextTransform, type TextWrap, type VerticalAlign, type WritingMode, defaultTextStyles, definePlugin, drawPath, filterEmpty, getHighlightStyle, getTransform2D, hexToRgb, highlight, isEqualObject, isEqualValue, isNone, listStyle, measureText, needsFetch, parseColor, parseColormap, parseValueNumber, render, renderText, setupView, textDecoration, uploadColor, uploadColors };
package/dist/index.d.mts CHANGED
@@ -252,7 +252,24 @@ interface TextLineStyle {
252
252
  listStyleSize: ListStyleSize;
253
253
  listStylePosition: ListStylePosition;
254
254
  }
255
- interface TextInlineStyle {
255
+ interface HighlightStyle {
256
+ image: HighlightImage;
257
+ referImage: HighlightReferImage;
258
+ colormap: HighlightColormap;
259
+ line: HighlightLine;
260
+ size: HighlightSize;
261
+ thickness: HighlightThickness;
262
+ }
263
+ interface TextHighlightStyle {
264
+ highlight?: Partial<HighlightStyle>;
265
+ highlightImage?: HighlightImage;
266
+ highlightReferImage?: HighlightReferImage;
267
+ highlightColormap?: HighlightColormap;
268
+ highlightLine?: HighlightLine;
269
+ highlightSize?: HighlightSize;
270
+ highlightThickness?: HighlightThickness;
271
+ }
272
+ interface TextInlineStyle extends TextHighlightStyle {
256
273
  verticalAlign: VerticalAlign;
257
274
  letterSpacing: number;
258
275
  fontSize: number;
@@ -263,12 +280,6 @@ interface TextInlineStyle {
263
280
  textTransform: TextTransform;
264
281
  textOrientation: TextOrientation;
265
282
  textDecoration: TextDecorationLine;
266
- highlightImage: HighlightImage;
267
- highlightReferImage: HighlightReferImage;
268
- highlightColormap: HighlightColormap;
269
- highlightLine: HighlightLine;
270
- highlightSize: HighlightSize;
271
- highlightThickness: HighlightThickness;
272
283
  }
273
284
  interface TextDrawStyle {
274
285
  color: string | CanvasGradient | CanvasPattern;
@@ -349,10 +360,22 @@ declare function uploadColors(ctx: CanvasRenderingContext2D, text: Text$1): void
349
360
 
350
361
  declare function definePlugin(options: TextPlugin): TextPlugin;
351
362
 
352
- declare function measureText(options: TextOptions): MeasureResult;
363
+ declare function measureText(options: TextOptions & {
364
+ load: true;
365
+ }): Promise<MeasureResult>;
366
+ declare function measureText(options: TextOptions & {
367
+ load?: false;
368
+ }): MeasureResult;
353
369
 
354
- declare function renderText(options: TextOptions & TextRenderOptions): void;
370
+ type RenderTextOptions = TextOptions & TextRenderOptions;
371
+ declare function renderText(options: RenderTextOptions & {
372
+ load: true;
373
+ }): Promise<void>;
374
+ declare function renderText(options: RenderTextOptions & {
375
+ load?: false;
376
+ }): void;
355
377
 
378
+ declare function getHighlightStyle(style: TextStyle): HighlightStyle;
356
379
  declare function highlight(): TextPlugin;
357
380
 
358
381
  declare function listStyle(): TextPlugin;
@@ -375,4 +398,4 @@ declare function hexToRgb(hex: string): string | null;
375
398
  declare function filterEmpty(val: Record<string, any> | undefined): Record<string, any> | undefined;
376
399
  declare function needsFetch(source: string): boolean;
377
400
 
378
- export { Character, type DrawShapePathsOptions, type FontKerning, type FontStyle, type FontWeight, Fragment, type FragmentContent, type HighlightColormap, type HighlightImage, type HighlightLine, type HighlightReferImage, type HighlightSize, type HighlightThickness, type LinearGradient, type ListStyleColormap, type ListStyleImage, type ListStylePosition, type ListStyleSize, type ListStyleType, type MeasureDomResult, type MeasureResult, type MeasuredCharacter, type MeasuredFragment, type MeasuredParagraph, Measurer, Paragraph, type ParagraphContent, type Sizeable, Text$1 as Text, type TextAlign, type TextContent, type TextDecorationLine, type TextDrawStyle, type TextEventMap, type TextInlineStyle, type TextLineStyle, type TextOptions, type TextOrientation, type TextPlugin, type TextRenderOptions, type TextStyle, type TextTransform, type TextWrap, type VerticalAlign, type WritingMode, defaultTextStyles, definePlugin, drawPath, filterEmpty, getTransform2D, hexToRgb, highlight, isEqualObject, isEqualValue, isNone, listStyle, measureText, needsFetch, parseColor, parseColormap, parseValueNumber, render, renderText, setupView, textDecoration, uploadColor, uploadColors };
401
+ export { Character, type DrawShapePathsOptions, type FontKerning, type FontStyle, type FontWeight, Fragment, type FragmentContent, type HighlightColormap, type HighlightImage, type HighlightLine, type HighlightReferImage, type HighlightSize, type HighlightStyle, type HighlightThickness, type LinearGradient, type ListStyleColormap, type ListStyleImage, type ListStylePosition, type ListStyleSize, type ListStyleType, type MeasureDomResult, type MeasureResult, type MeasuredCharacter, type MeasuredFragment, type MeasuredParagraph, Measurer, Paragraph, type ParagraphContent, type RenderTextOptions, type Sizeable, Text$1 as Text, type TextAlign, type TextContent, type TextDecorationLine, type TextDrawStyle, type TextEventMap, type TextHighlightStyle, type TextInlineStyle, type TextLineStyle, type TextOptions, type TextOrientation, type TextPlugin, type TextRenderOptions, type TextStyle, type TextTransform, type TextWrap, type VerticalAlign, type WritingMode, defaultTextStyles, definePlugin, drawPath, filterEmpty, getHighlightStyle, getTransform2D, hexToRgb, highlight, isEqualObject, isEqualValue, isNone, listStyle, measureText, needsFetch, parseColor, parseColormap, parseValueNumber, render, renderText, setupView, textDecoration, uploadColor, uploadColors };