modern-text 0.3.5 → 0.3.7

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
@@ -174,22 +174,23 @@ class Character {
174
174
  get fontHeight() {
175
175
  return this.fontSize * this.computedStyle.lineHeight;
176
176
  }
177
- _font() {
178
- const font = modernFont.fonts.get(this.computedStyle.fontFamily)?.font;
179
- if (font instanceof modernFont.Woff || font instanceof modernFont.Ttf) {
180
- return font.sfnt;
177
+ _getFontSfnt(fonts) {
178
+ const fontFamily = this.computedStyle.fontFamily;
179
+ const _font = fonts?.[fontFamily] ?? modernFont.fonts.get(this.computedStyle.fontFamily)?.font;
180
+ if (_font instanceof modernFont.Woff || _font instanceof modernFont.Ttf) {
181
+ return _font.sfnt;
181
182
  }
182
183
  return void 0;
183
184
  }
184
- updateGlyph(font = this._font()) {
185
- if (!font) {
185
+ updateGlyph(sfnt = this._getFontSfnt()) {
186
+ if (!sfnt) {
186
187
  return this;
187
188
  }
188
- const { unitsPerEm, ascender, descender, os2, post } = font;
189
+ const { unitsPerEm, ascender, descender, os2, post } = sfnt;
189
190
  const { content, computedStyle } = this;
190
191
  const { fontSize } = computedStyle;
191
192
  const rate = unitsPerEm / fontSize;
192
- const advanceWidth = font.getAdvanceWidth(content, fontSize);
193
+ const advanceWidth = sfnt.getAdvanceWidth(content, fontSize);
193
194
  const advanceHeight = (ascender + Math.abs(descender)) / rate;
194
195
  const baseline = ascender / rate;
195
196
  const yStrikeoutPosition = (ascender - os2.yStrikeoutPosition) / rate;
@@ -206,12 +207,12 @@ class Character {
206
207
  this.centerDiviation = advanceHeight / 2 - baseline;
207
208
  return this;
208
209
  }
209
- updatePath() {
210
- const font = this._font();
211
- if (!font) {
210
+ update(fonts) {
211
+ const sfnt = this._getFontSfnt(fonts);
212
+ if (!sfnt) {
212
213
  return this;
213
214
  }
214
- this.updateGlyph(font);
215
+ this.updateGlyph(sfnt);
215
216
  const {
216
217
  isVertical,
217
218
  content,
@@ -219,7 +220,7 @@ class Character {
219
220
  baseline,
220
221
  inlineBox
221
222
  } = this;
222
- const { os2, head, ascender, descender } = font;
223
+ const { os2, head, ascender, descender } = sfnt;
223
224
  const typoAscender = os2.sTypoAscender;
224
225
  const fontStyle = fsSelectionMap[os2.fsSelection] ?? macStyleMap[head.macStyle];
225
226
  const { left, top } = inlineBox;
@@ -237,7 +238,7 @@ class Character {
237
238
  }
238
239
  if (isVertical && !set1.has(content) && (content.codePointAt(0) <= 256 || set2.has(content))) {
239
240
  path.addCommands(
240
- font.getPathCommands(content, x, top + baseline - (inlineBox.height - inlineBox.width) / 2, style.fontSize) ?? []
241
+ sfnt.getPathCommands(content, x, top + baseline - (inlineBox.height - inlineBox.width) / 2, style.fontSize) ?? []
241
242
  );
242
243
  const point = {
243
244
  y: top - (inlineBox.height - inlineBox.width) / 2 + inlineBox.height / 2,
@@ -256,7 +257,7 @@ class Character {
256
257
  } else {
257
258
  if (glyphIndex !== void 0) {
258
259
  path.addCommands(
259
- font.glyphs.get(glyphIndex).getPathCommands(x, y, style.fontSize)
260
+ sfnt.glyphs.get(glyphIndex).getPathCommands(x, y, style.fontSize)
260
261
  );
261
262
  if (needsItalic) {
262
263
  this._italic(
@@ -269,7 +270,7 @@ class Character {
269
270
  }
270
271
  } else {
271
272
  path.addCommands(
272
- font.getPathCommands(content, x, y, style.fontSize) ?? []
273
+ sfnt.getPathCommands(content, x, y, style.fontSize) ?? []
273
274
  );
274
275
  if (needsItalic) {
275
276
  this._italic(
@@ -293,10 +294,6 @@ class Character {
293
294
  this.glyphBox = this.getGlyphBoundingBox();
294
295
  return this;
295
296
  }
296
- update() {
297
- this.updatePath();
298
- return this;
299
- }
300
297
  _decoration() {
301
298
  const { isVertical, underlinePosition, yStrikeoutPosition } = this;
302
299
  const { textDecoration, fontSize } = this.computedStyle;
@@ -1113,11 +1110,13 @@ class Text {
1113
1110
  __publicField(this, "parser", new Parser(this));
1114
1111
  __publicField(this, "measurer", new Measurer(this));
1115
1112
  __publicField(this, "plugins", /* @__PURE__ */ new Map());
1116
- const { content = "", style = {}, measureDom, effects } = options;
1113
+ __publicField(this, "fonts");
1114
+ const { content = "", style = {}, measureDom, effects, fonts } = options;
1117
1115
  this.content = content;
1118
1116
  this.style = style;
1119
1117
  this.measureDom = measureDom;
1120
1118
  this.effects = effects;
1119
+ this.fonts = fonts;
1121
1120
  this.use(render()).use(highlight()).use(listStyle());
1122
1121
  }
1123
1122
  get fontSize() {
@@ -1133,6 +1132,10 @@ class Text {
1133
1132
  this.plugins.set(plugin.name, plugin);
1134
1133
  return this;
1135
1134
  }
1135
+ updateParagraphs() {
1136
+ this.paragraphs = this.parser.parse();
1137
+ return this;
1138
+ }
1136
1139
  measure(dom = this.measureDom) {
1137
1140
  this.computedStyle = { ...defaultTextStyles, ...this.style };
1138
1141
  const old = {
@@ -1143,12 +1146,12 @@ class Text {
1143
1146
  pathBox: this.pathBox,
1144
1147
  boundingBox: this.boundingBox
1145
1148
  };
1146
- this.paragraphs = this.parser.parse();
1149
+ this.updateParagraphs();
1147
1150
  const result = this.measurer.measure(dom);
1148
1151
  this.paragraphs = result.paragraphs;
1149
1152
  this.lineBox = result.boundingBox;
1150
1153
  this.characters.forEach((c) => {
1151
- c.update();
1154
+ c.update(this.fonts);
1152
1155
  });
1153
1156
  this.rawGlyphBox = this.getGlyphBox();
1154
1157
  const plugins = [...this.plugins.values()];
package/dist/index.d.cts CHANGED
@@ -1,6 +1,6 @@
1
1
  import { BoundingBox, Path2D, Vector2, VectorLike, Matrix3 } from 'modern-path2d';
2
2
  export * from 'modern-path2d';
3
- import { Sfnt, GlyphPathCommand } from 'modern-font';
3
+ import { Font, Sfnt, GlyphPathCommand } from 'modern-font';
4
4
  export * from 'modern-font';
5
5
 
6
6
  type Sizeable = `${number}%` | `${number}rem` | number;
@@ -130,10 +130,9 @@ declare class Character {
130
130
  get fontSize(): number;
131
131
  get fontHeight(): number;
132
132
  constructor(content: string, index: number, parent: Fragment);
133
- protected _font(): Sfnt | undefined;
134
- updateGlyph(font?: Sfnt | undefined): this;
135
- updatePath(): this;
136
- update(): this;
133
+ protected _getFontSfnt(fonts?: Record<string, Font>): Sfnt | undefined;
134
+ updateGlyph(sfnt?: Sfnt | undefined): this;
135
+ update(fonts?: Record<string, Font>): this;
137
136
  protected _decoration(): GlyphPathCommand[];
138
137
  protected _italic(path: Path2D, startPoint?: VectorLike): void;
139
138
  getGlyphMinMax(min?: Vector2, max?: Vector2, withStyle?: boolean): {
@@ -241,6 +240,7 @@ interface TextOptions {
241
240
  style?: Partial<TextStyle>;
242
241
  measureDom?: HTMLElement;
243
242
  effects?: Partial<TextStyle>[];
243
+ fonts?: Record<string, Font>;
244
244
  }
245
245
  interface MeasureResult {
246
246
  paragraphs: Paragraph[];
@@ -267,11 +267,13 @@ declare class Text {
267
267
  parser: Parser;
268
268
  measurer: Measurer;
269
269
  plugins: Map<string, Plugin>;
270
+ fonts?: Record<string, Font>;
270
271
  get fontSize(): number;
271
272
  get isVertical(): boolean;
272
273
  get characters(): Character[];
273
274
  constructor(options?: TextOptions);
274
275
  use(plugin: Plugin): this;
276
+ updateParagraphs(): this;
275
277
  measure(dom?: HTMLElement | undefined): MeasureResult;
276
278
  getGlyphBox(): BoundingBox;
277
279
  updatePathBox(): this;
package/dist/index.d.mts CHANGED
@@ -1,6 +1,6 @@
1
1
  import { BoundingBox, Path2D, Vector2, VectorLike, Matrix3 } from 'modern-path2d';
2
2
  export * from 'modern-path2d';
3
- import { Sfnt, GlyphPathCommand } from 'modern-font';
3
+ import { Font, Sfnt, GlyphPathCommand } from 'modern-font';
4
4
  export * from 'modern-font';
5
5
 
6
6
  type Sizeable = `${number}%` | `${number}rem` | number;
@@ -130,10 +130,9 @@ declare class Character {
130
130
  get fontSize(): number;
131
131
  get fontHeight(): number;
132
132
  constructor(content: string, index: number, parent: Fragment);
133
- protected _font(): Sfnt | undefined;
134
- updateGlyph(font?: Sfnt | undefined): this;
135
- updatePath(): this;
136
- update(): this;
133
+ protected _getFontSfnt(fonts?: Record<string, Font>): Sfnt | undefined;
134
+ updateGlyph(sfnt?: Sfnt | undefined): this;
135
+ update(fonts?: Record<string, Font>): this;
137
136
  protected _decoration(): GlyphPathCommand[];
138
137
  protected _italic(path: Path2D, startPoint?: VectorLike): void;
139
138
  getGlyphMinMax(min?: Vector2, max?: Vector2, withStyle?: boolean): {
@@ -241,6 +240,7 @@ interface TextOptions {
241
240
  style?: Partial<TextStyle>;
242
241
  measureDom?: HTMLElement;
243
242
  effects?: Partial<TextStyle>[];
243
+ fonts?: Record<string, Font>;
244
244
  }
245
245
  interface MeasureResult {
246
246
  paragraphs: Paragraph[];
@@ -267,11 +267,13 @@ declare class Text {
267
267
  parser: Parser;
268
268
  measurer: Measurer;
269
269
  plugins: Map<string, Plugin>;
270
+ fonts?: Record<string, Font>;
270
271
  get fontSize(): number;
271
272
  get isVertical(): boolean;
272
273
  get characters(): Character[];
273
274
  constructor(options?: TextOptions);
274
275
  use(plugin: Plugin): this;
276
+ updateParagraphs(): this;
275
277
  measure(dom?: HTMLElement | undefined): MeasureResult;
276
278
  getGlyphBox(): BoundingBox;
277
279
  updatePathBox(): this;
package/dist/index.d.ts CHANGED
@@ -1,6 +1,6 @@
1
1
  import { BoundingBox, Path2D, Vector2, VectorLike, Matrix3 } from 'modern-path2d';
2
2
  export * from 'modern-path2d';
3
- import { Sfnt, GlyphPathCommand } from 'modern-font';
3
+ import { Font, Sfnt, GlyphPathCommand } from 'modern-font';
4
4
  export * from 'modern-font';
5
5
 
6
6
  type Sizeable = `${number}%` | `${number}rem` | number;
@@ -130,10 +130,9 @@ declare class Character {
130
130
  get fontSize(): number;
131
131
  get fontHeight(): number;
132
132
  constructor(content: string, index: number, parent: Fragment);
133
- protected _font(): Sfnt | undefined;
134
- updateGlyph(font?: Sfnt | undefined): this;
135
- updatePath(): this;
136
- update(): this;
133
+ protected _getFontSfnt(fonts?: Record<string, Font>): Sfnt | undefined;
134
+ updateGlyph(sfnt?: Sfnt | undefined): this;
135
+ update(fonts?: Record<string, Font>): this;
137
136
  protected _decoration(): GlyphPathCommand[];
138
137
  protected _italic(path: Path2D, startPoint?: VectorLike): void;
139
138
  getGlyphMinMax(min?: Vector2, max?: Vector2, withStyle?: boolean): {
@@ -241,6 +240,7 @@ interface TextOptions {
241
240
  style?: Partial<TextStyle>;
242
241
  measureDom?: HTMLElement;
243
242
  effects?: Partial<TextStyle>[];
243
+ fonts?: Record<string, Font>;
244
244
  }
245
245
  interface MeasureResult {
246
246
  paragraphs: Paragraph[];
@@ -267,11 +267,13 @@ declare class Text {
267
267
  parser: Parser;
268
268
  measurer: Measurer;
269
269
  plugins: Map<string, Plugin>;
270
+ fonts?: Record<string, Font>;
270
271
  get fontSize(): number;
271
272
  get isVertical(): boolean;
272
273
  get characters(): Character[];
273
274
  constructor(options?: TextOptions);
274
275
  use(plugin: Plugin): this;
276
+ updateParagraphs(): this;
275
277
  measure(dom?: HTMLElement | undefined): MeasureResult;
276
278
  getGlyphBox(): BoundingBox;
277
279
  updatePathBox(): this;