@mlightcad/mtext-renderer 0.3.3 → 0.3.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.
@@ -1,4 +1,4 @@
1
- import { FontInfo, FontLoader } from './fontLoader';
1
+ import { FontInfo, FontLoader, FontLoadStatus } from './fontLoader';
2
2
 
3
3
  /**
4
4
  * Default implementation of the FontLoader interface.
@@ -25,10 +25,11 @@ export declare class DefaultFontLoader implements FontLoader {
25
25
  */
26
26
  getAvaiableFonts(): Promise<FontInfo[]>;
27
27
  /**
28
- * Loads the specified fonts into the system.
29
- * If no font names are provided, loads all available fonts.
28
+ * Loads the specified fonts into the system. If one font is already loaded,
29
+ * the font will not be loaded again. If no font names are provided, just loads
30
+ * all available fonts information (not fonts).
30
31
  * @param fontNames - Array of font names to load
31
32
  * @returns Promise that resolves to an array of FontLoadStatus objects
32
33
  */
33
- load(fontNames: string[]): Promise<import('./fontLoader').FontLoadStatus[]>;
34
+ load(fontNames: string[]): Promise<FontLoadStatus[]>;
34
35
  }
@@ -102,6 +102,12 @@ export declare class FontManager {
102
102
  * @returns The shape for the not found indicator, or undefined if not available
103
103
  */
104
104
  getNotFoundTextShape(size: number): BaseTextShape | undefined;
105
+ /**
106
+ * Checks if a font is already loaded in the system
107
+ * @param fontName - The name of the font to check
108
+ * @returns True if the font is loaded, false otherwise
109
+ */
110
+ isFontLoaded(fontName: string): boolean;
105
111
  /**
106
112
  * Records a font that was requested but not found
107
113
  * @param fontName - The name of the font that was not found
package/dist/index.js CHANGED
@@ -79,7 +79,7 @@ function Sa(t) {
79
79
  const a = () => {
80
80
  t.removeEventListener("success", s), t.removeEventListener("error", i);
81
81
  }, s = () => {
82
- r(Ue(t.result)), a();
82
+ r(Le(t.result)), a();
83
83
  }, i = () => {
84
84
  n(t.error), a();
85
85
  };
@@ -110,7 +110,7 @@ let _t = {
110
110
  if (e === "store")
111
111
  return r.objectStoreNames[1] ? void 0 : r.objectStore(r.objectStoreNames[0]);
112
112
  }
113
- return Ue(t[e]);
113
+ return Le(t[e]);
114
114
  },
115
115
  set(t, e, r) {
116
116
  return t[e] = r, !0;
@@ -124,15 +124,15 @@ function mn(t) {
124
124
  }
125
125
  function Ta(t) {
126
126
  return ba().includes(t) ? function(...e) {
127
- return t.apply(Ht(this), e), Ue(this.request);
127
+ return t.apply(Ht(this), e), Le(this.request);
128
128
  } : function(...e) {
129
- return Ue(t.apply(Ht(this), e));
129
+ return Le(t.apply(Ht(this), e));
130
130
  };
131
131
  }
132
132
  function Fa(t) {
133
133
  return typeof t == "function" ? Ta(t) : (t instanceof IDBTransaction && ka(t), Gt(t, xa()) ? new Proxy(t, _t) : t);
134
134
  }
135
- function Ue(t) {
135
+ function Le(t) {
136
136
  if (t instanceof IDBRequest)
137
137
  return Sa(t);
138
138
  if (St.has(t))
@@ -142,9 +142,9 @@ function Ue(t) {
142
142
  }
143
143
  const Ht = (t) => vt.get(t);
144
144
  function Ca(t, e, { blocked: r, upgrade: n, blocking: a, terminated: s } = {}) {
145
- const i = indexedDB.open(t, e), u = Ue(i);
145
+ const i = indexedDB.open(t, e), u = Le(i);
146
146
  return n && i.addEventListener("upgradeneeded", (o) => {
147
- n(Ue(i.result), o.oldVersion, o.newVersion, Ue(i.transaction), o);
147
+ n(Le(i.result), o.oldVersion, o.newVersion, Le(i.transaction), o);
148
148
  }), r && i.addEventListener("blocked", (o) => r(
149
149
  // Casting due to https://github.com/microsoft/TypeScript-DOM-lib-generator/pull/1405
150
150
  o.oldVersion,
@@ -213,14 +213,14 @@ mn((t) => ({
213
213
  return mr(e, r) || t.has(e, r);
214
214
  }
215
215
  }));
216
- const Ae = {
216
+ const De = {
217
217
  fonts: "fonts"
218
218
  }, Tt = [
219
219
  {
220
220
  version: 1,
221
221
  stores: [
222
222
  {
223
- name: Ae.fonts,
223
+ name: De.fonts,
224
224
  keyPath: "name"
225
225
  }
226
226
  ]
@@ -243,7 +243,7 @@ const Ae = {
243
243
  * @param fontData The font data to store
244
244
  */
245
245
  async set(e, r) {
246
- await (await this.getDatabase()).put(Ae.fonts, { ...r, name: e });
246
+ await (await this.getDatabase()).put(De.fonts, { ...r, name: e });
247
247
  }
248
248
  /**
249
249
  * Gets a font from the cache
@@ -251,27 +251,27 @@ const Ae = {
251
251
  * @returns The font data if found, undefined otherwise
252
252
  */
253
253
  async get(e) {
254
- return await (await this.getDatabase()).get(Ae.fonts, e);
254
+ return await (await this.getDatabase()).get(De.fonts, e);
255
255
  }
256
256
  /**
257
257
  * Deletes a font from the cache
258
258
  * @param fileName The font file name (key)
259
259
  */
260
260
  async delete(e) {
261
- await (await this.getDatabase()).delete(Ae.fonts, e);
261
+ await (await this.getDatabase()).delete(De.fonts, e);
262
262
  }
263
263
  /**
264
264
  * Gets all fonts from the cache
265
265
  * @returns An array of all font data in the cache
266
266
  */
267
267
  async getAll() {
268
- return await (await this.getDatabase()).getAll(Ae.fonts);
268
+ return await (await this.getDatabase()).getAll(De.fonts);
269
269
  }
270
270
  /**
271
271
  * Clears all fonts from the cache
272
272
  */
273
273
  async clear() {
274
- await (await this.getDatabase()).clear(Ae.fonts);
274
+ await (await this.getDatabase()).clear(De.fonts);
275
275
  }
276
276
  /**
277
277
  * Checks if a font exists in the cache
@@ -903,7 +903,7 @@ wn(Fn, Cn, 2, 1);
903
903
  $t[28] = 0;
904
904
  er[28] = 258;
905
905
  var qa = Va;
906
- function Re(t, e, r, n, a) {
906
+ function Ae(t, e, r, n, a) {
907
907
  return Math.pow(1 - a, 3) * t + 3 * Math.pow(1 - a, 2) * a * e + 3 * (1 - a) * Math.pow(a, 2) * r + Math.pow(a, 3) * n;
908
908
  }
909
909
  function Fe() {
@@ -930,15 +930,15 @@ Fe.prototype.addBezier = function(t, e, r, n, a, s, i, u) {
930
930
  if (p === 0)
931
931
  continue;
932
932
  var y = -g / p;
933
- 0 < y && y < 1 && (h === 0 && this.addX(Re(o[h], l[h], c[h], f[h], y)), h === 1 && this.addY(Re(o[h], l[h], c[h], f[h], y)));
933
+ 0 < y && y < 1 && (h === 0 && this.addX(Ae(o[h], l[h], c[h], f[h], y)), h === 1 && this.addY(Ae(o[h], l[h], c[h], f[h], y)));
934
934
  continue;
935
935
  }
936
936
  var m = Math.pow(p, 2) - 4 * g * d;
937
937
  if (!(m < 0)) {
938
938
  var S = (-p + Math.sqrt(m)) / (2 * d);
939
- 0 < S && S < 1 && (h === 0 && this.addX(Re(o[h], l[h], c[h], f[h], S)), h === 1 && this.addY(Re(o[h], l[h], c[h], f[h], S)));
939
+ 0 < S && S < 1 && (h === 0 && this.addX(Ae(o[h], l[h], c[h], f[h], S)), h === 1 && this.addY(Ae(o[h], l[h], c[h], f[h], S)));
940
940
  var x = (-p - Math.sqrt(m)) / (2 * d);
941
- 0 < x && x < 1 && (h === 0 && this.addX(Re(o[h], l[h], c[h], f[h], x)), h === 1 && this.addY(Re(o[h], l[h], c[h], f[h], x)));
941
+ 0 < x && x < 1 && (h === 0 && this.addX(Ae(o[h], l[h], c[h], f[h], x)), h === 1 && this.addY(Ae(o[h], l[h], c[h], f[h], x)));
942
942
  }
943
943
  }
944
944
  };
@@ -6296,7 +6296,7 @@ function bi(t, e, r, n) {
6296
6296
  function Si(t, e, r, n, a) {
6297
6297
  return a.lowMemory ? bi(t, e, r, n) : xi(t, e, r, n);
6298
6298
  }
6299
- var aa = { getPath: ra, parse: Si }, sa, Le, ia, Qt;
6299
+ var aa = { getPath: ra, parse: Si }, sa, Re, ia, Qt;
6300
6300
  function oa(t) {
6301
6301
  this.font = t, this.getCommands = function(e) {
6302
6302
  return aa.getPath(e).commands;
@@ -6522,7 +6522,7 @@ oa.prototype.exec = function(t, e) {
6522
6522
  if (!a) {
6523
6523
  be.prototype = Ei, a = this._fpgmState = new be("fpgm", r.tables.fpgm), a.funcs = [], a.font = r, exports.DEBUG && (console.log("---EXEC FPGM---"), a.step = -1);
6524
6524
  try {
6525
- Le(a);
6525
+ Re(a);
6526
6526
  } catch (l) {
6527
6527
  console.log("Hinting error in FPGM:" + l), this._errorState = 3;
6528
6528
  return;
@@ -6537,7 +6537,7 @@ oa.prototype.exec = function(t, e) {
6537
6537
  n.cvt = [];
6538
6538
  exports.DEBUG && (console.log("---EXEC PREP---"), n.step = -1);
6539
6539
  try {
6540
- Le(n);
6540
+ Re(n);
6541
6541
  } catch (l) {
6542
6542
  this._errorState < 2 && console.log("Hinting error in PREP:" + l), this._errorState = 2;
6543
6543
  }
@@ -6573,7 +6573,7 @@ ia = function(t, e) {
6573
6573
  t.instructions && !u.inhibitGridFit && (u = new be("glyf", t.instructions), u.gZone = u.z0 = u.z1 = u.z2 = i, u.contours = s, i.push(
6574
6574
  new ge(0, 0),
6575
6575
  new ge(Math.round(t.advanceWidth * r), 0)
6576
- ), exports.DEBUG && (console.log("---EXEC COMPOSITE---"), u.step = -1), Le(u), i.length -= 2);
6576
+ ), exports.DEBUG && (console.log("---EXEC COMPOSITE---"), u.step = -1), Re(u), i.length -= 2);
6577
6577
  }
6578
6578
  return i;
6579
6579
  };
@@ -6596,14 +6596,14 @@ Qt = function(t, e, r, n) {
6596
6596
  if (i.push(
6597
6597
  new ge(0, 0),
6598
6598
  new ge(Math.round(t.advanceWidth * r), 0)
6599
- ), Le(e), i.length -= 2, exports.DEBUG) {
6599
+ ), Re(e), i.length -= 2, exports.DEBUG) {
6600
6600
  console.log("FINISHED GLYPH", e.stack);
6601
6601
  for (var d = 0; d < s; d++)
6602
6602
  console.log(d, i[d].x, i[d].y);
6603
6603
  }
6604
6604
  }
6605
6605
  };
6606
- Le = function(t) {
6606
+ Re = function(t) {
6607
6607
  var e = t.prog;
6608
6608
  if (e) {
6609
6609
  var r = e.length, n;
@@ -6795,7 +6795,7 @@ function Ji(t) {
6795
6795
  var a = t.ip, s = t.prog;
6796
6796
  t.prog = t.funcs[r];
6797
6797
  for (var i = 0; i < n; i++)
6798
- Le(t), exports.DEBUG && console.log(
6798
+ Re(t), exports.DEBUG && console.log(
6799
6799
  ++t.step,
6800
6800
  i + 1 < n ? "next loopcall" : "done loopcall",
6801
6801
  i
@@ -6806,7 +6806,7 @@ function $i(t) {
6806
6806
  var e = t.stack.pop();
6807
6807
  exports.DEBUG && console.log(t.step, "CALL[]", e);
6808
6808
  var r = t.ip, n = t.prog;
6809
- t.prog = t.funcs[e], Le(t), t.ip = r, t.prog = n, exports.DEBUG && console.log(++t.step, "returning from", e);
6809
+ t.prog = t.funcs[e], Re(t), t.ip = r, t.prog = n, exports.DEBUG && console.log(++t.step, "returning from", e);
6810
6810
  }
6811
6811
  function eo(t) {
6812
6812
  var e = t.stack, r = e.pop();
@@ -10584,7 +10584,7 @@ class Se {
10584
10584
  throw new Error(`Unsupported font file type: ${n}`);
10585
10585
  }
10586
10586
  }
10587
- class Me {
10587
+ class Ue {
10588
10588
  constructor() {
10589
10589
  this.fontMapping = {}, this.fontMap = /* @__PURE__ */ new Map(), this.unsupportedChars = {}, this.missedFonts = {}, this.enableFontCache = !0, this.defaultFont = "simsun", this.events = {
10590
10590
  /** Event triggered when a font cannot be found */
@@ -10598,7 +10598,7 @@ class Me {
10598
10598
  * @returns The FontManager instance
10599
10599
  */
10600
10600
  static get instance() {
10601
- return Me._instance || (Me._instance = new Me()), Me._instance;
10601
+ return Ue._instance || (Ue._instance = new Ue()), Ue._instance;
10602
10602
  }
10603
10603
  /**
10604
10604
  * Sets the font mapping configuration
@@ -10696,6 +10696,14 @@ class Me {
10696
10696
  if (n) return n;
10697
10697
  }
10698
10698
  }
10699
+ /**
10700
+ * Checks if a font is already loaded in the system
10701
+ * @param fontName - The name of the font to check
10702
+ * @returns True if the font is loaded, false otherwise
10703
+ */
10704
+ isFontLoaded(e) {
10705
+ return this.fontMap.has(e.toLowerCase());
10706
+ }
10699
10707
  /**
10700
10708
  * Records a font that was requested but not found
10701
10709
  * @param fontName - The name of the font that was not found
@@ -10797,21 +10805,44 @@ class Sl {
10797
10805
  return this._avaiableFonts;
10798
10806
  }
10799
10807
  /**
10800
- * Loads the specified fonts into the system.
10801
- * If no font names are provided, loads all available fonts.
10808
+ * Loads the specified fonts into the system. If one font is already loaded,
10809
+ * the font will not be loaded again. If no font names are provided, just loads
10810
+ * all available fonts information (not fonts).
10802
10811
  * @param fontNames - Array of font names to load
10803
10812
  * @returns Promise that resolves to an array of FontLoadStatus objects
10804
10813
  */
10805
10814
  async load(e) {
10806
- e.length == 0 && await this.getAvaiableFonts();
10807
- const r = [];
10808
- return e.forEach((n) => {
10809
- const a = n.toLowerCase(), s = this._avaiableFonts.find((i) => i.name.findIndex((u) => u.toLowerCase() === a) >= 0);
10810
- s && r.push(s.url);
10811
- }), await Me.instance.loadFonts(r);
10815
+ if (e.length == 0)
10816
+ return await this.getAvaiableFonts(), [];
10817
+ const r = [], n = [], a = {};
10818
+ this._avaiableFonts.forEach((u) => {
10819
+ u.name.forEach((o) => {
10820
+ a[o.toLowerCase()] = u.url;
10821
+ });
10822
+ }), e.forEach((u) => {
10823
+ const o = u.toLowerCase(), l = a[o];
10824
+ l && (Ue.instance.isFontLoaded(o) ? n.push({
10825
+ fontName: o,
10826
+ url: l,
10827
+ status: !0
10828
+ }) : r.push(l));
10829
+ });
10830
+ let s = [];
10831
+ r.length > 0 && (s = await Ue.instance.loadFonts(r));
10832
+ const i = {};
10833
+ return [...n, ...s].forEach((u) => {
10834
+ i[u.fontName] = u;
10835
+ }), e.map((u) => {
10836
+ const o = u.toLowerCase();
10837
+ return i[o] || {
10838
+ fontName: o,
10839
+ url: a[o] || "",
10840
+ status: !1
10841
+ };
10842
+ });
10812
10843
  }
10813
10844
  }
10814
- var je = /* @__PURE__ */ ((t) => (t[t.LEFT_TO_RIGHT = 1] = "LEFT_TO_RIGHT", t[t.RIGHT_TO_LEFT = 2] = "RIGHT_TO_LEFT", t[t.TOP_TO_BOTTOM = 3] = "TOP_TO_BOTTOM", t[t.BOTTOM_TO_TOP = 4] = "BOTTOM_TO_TOP", t[t.BY_STYLE = 5] = "BY_STYLE", t))(je || {}), cl = /* @__PURE__ */ ((t) => (t[t.TopLeft = 1] = "TopLeft", t[t.TopCenter = 2] = "TopCenter", t[t.TopRight = 3] = "TopRight", t[t.MiddleLeft = 4] = "MiddleLeft", t[t.MiddleCenter = 5] = "MiddleCenter", t[t.MiddleRight = 6] = "MiddleRight", t[t.BottomLeft = 7] = "BottomLeft", t[t.BottomCenter = 8] = "BottomCenter", t[t.BottomRight = 9] = "BottomRight", t))(cl || {}), De = /* @__PURE__ */ ((t) => (t[t.NONE = 0] = "NONE", t[t.WORD = 1] = "WORD", t[t.STACK = 2] = "STACK", t[t.SPACE = 3] = "SPACE", t[t.NBSP = 4] = "NBSP", t[t.TABULATOR = 5] = "TABULATOR", t[t.NEW_PARAGRAPH = 6] = "NEW_PARAGRAPH", t[t.NEW_COLUMN = 7] = "NEW_COLUMN", t[t.WRAP_AT_DIMLINE = 8] = "WRAP_AT_DIMLINE", t[t.PROPERTIES_CHANGED = 9] = "PROPERTIES_CHANGED", t))(De || {}), _e = /* @__PURE__ */ ((t) => (t[t.BOTTOM = 0] = "BOTTOM", t[t.MIDDLE = 1] = "MIDDLE", t[t.TOP = 2] = "TOP", t))(_e || {}), se = /* @__PURE__ */ ((t) => (t[t.DEFAULT = 0] = "DEFAULT", t[t.LEFT = 1] = "LEFT", t[t.RIGHT = 2] = "RIGHT", t[t.CENTER = 3] = "CENTER", t[t.JUSTIFIED = 4] = "JUSTIFIED", t[t.DISTRIBUTED = 5] = "DISTRIBUTED", t))(se || {});
10845
+ var je = /* @__PURE__ */ ((t) => (t[t.LEFT_TO_RIGHT = 1] = "LEFT_TO_RIGHT", t[t.RIGHT_TO_LEFT = 2] = "RIGHT_TO_LEFT", t[t.TOP_TO_BOTTOM = 3] = "TOP_TO_BOTTOM", t[t.BOTTOM_TO_TOP = 4] = "BOTTOM_TO_TOP", t[t.BY_STYLE = 5] = "BY_STYLE", t))(je || {}), cl = /* @__PURE__ */ ((t) => (t[t.TopLeft = 1] = "TopLeft", t[t.TopCenter = 2] = "TopCenter", t[t.TopRight = 3] = "TopRight", t[t.MiddleLeft = 4] = "MiddleLeft", t[t.MiddleCenter = 5] = "MiddleCenter", t[t.MiddleRight = 6] = "MiddleRight", t[t.BottomLeft = 7] = "BottomLeft", t[t.BottomCenter = 8] = "BottomCenter", t[t.BottomRight = 9] = "BottomRight", t))(cl || {}), Me = /* @__PURE__ */ ((t) => (t[t.NONE = 0] = "NONE", t[t.WORD = 1] = "WORD", t[t.STACK = 2] = "STACK", t[t.SPACE = 3] = "SPACE", t[t.NBSP = 4] = "NBSP", t[t.TABULATOR = 5] = "TABULATOR", t[t.NEW_PARAGRAPH = 6] = "NEW_PARAGRAPH", t[t.NEW_COLUMN = 7] = "NEW_COLUMN", t[t.WRAP_AT_DIMLINE = 8] = "WRAP_AT_DIMLINE", t[t.PROPERTIES_CHANGED = 9] = "PROPERTIES_CHANGED", t))(Me || {}), _e = /* @__PURE__ */ ((t) => (t[t.BOTTOM = 0] = "BOTTOM", t[t.MIDDLE = 1] = "MIDDLE", t[t.TOP = 2] = "TOP", t))(_e || {}), se = /* @__PURE__ */ ((t) => (t[t.DEFAULT = 0] = "DEFAULT", t[t.LEFT = 1] = "LEFT", t[t.RIGHT = 2] = "RIGHT", t[t.CENTER = 3] = "CENTER", t[t.JUSTIFIED = 4] = "JUSTIFIED", t[t.DISTRIBUTED = 5] = "DISTRIBUTED", t))(se || {});
10815
10846
  const hl = {
10816
10847
  c: "Ø",
10817
10848
  d: "°",
@@ -12063,17 +12094,17 @@ class yl {
12063
12094
  processText(e) {
12064
12095
  const r = [], n = [], a = new R.Group();
12065
12096
  for (const s of e)
12066
- if (s.type === De.NEW_PARAGRAPH)
12097
+ if (s.type === Me.NEW_PARAGRAPH)
12067
12098
  this.startNewLine(), this.processGeometries(r, n, a);
12068
- else if (s.type === De.WORD) {
12099
+ else if (s.type === Me.WORD) {
12069
12100
  const i = s.data;
12070
12101
  Array.isArray(i) ? i.forEach((u) => this.processWord(u, r, n)) : typeof i == "string" && i.length > 0 && this.processWord(i, r, n), this.processGeometries(r, n, a);
12071
- } else if (s.type === De.SPACE)
12102
+ } else if (s.type === Me.SPACE)
12072
12103
  this.processBlank();
12073
- else if (s.type === De.PROPERTIES_CHANGED) {
12104
+ else if (s.type === Me.PROPERTIES_CHANGED) {
12074
12105
  const i = s.data;
12075
12106
  i.command === void 0 ? this._contextStack.length > 0 && (this._currentContext = this._contextStack.pop()) : (this._contextStack.length === 0 && this._contextStack.push(this._currentContext.clone()), this.processFormat(i)), this.processGeometries(r, n, a);
12076
- } else s.type === De.STACK && (this.processStack(s.data, r, n), this.processGeometries(r, n, a));
12107
+ } else s.type === Me.STACK && (this.processStack(s.data, r, n), this.processGeometries(r, n, a));
12077
12108
  return (r.length > 0 || n.length > 0) && this.processGeometries(r, n, a), a;
12078
12109
  }
12079
12110
  processGeometries(e, r, n) {
@@ -12538,7 +12569,7 @@ export {
12538
12569
  Sl as DefaultFontLoader,
12539
12570
  pr as EventManager,
12540
12571
  Se as FontFactory,
12541
- Me as FontManager,
12572
+ Ue as FontManager,
12542
12573
  kl as MText,
12543
12574
  cl as MTextAttachmentPoint,
12544
12575
  je as MTextFlowDirection,