modern-text 0.3.4 → 0.3.6

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
@@ -81,8 +81,8 @@ function setupView(ctx, pixelRatio, boundingBox) {
81
81
  const view = ctx.canvas;
82
82
  view.dataset.viewBox = String(`${left} ${top} ${width} ${height}`);
83
83
  view.dataset.pixelRatio = String(pixelRatio);
84
- const canvasWidth = width + Math.abs(left);
85
- const canvasHeight = height + Math.abs(top);
84
+ const canvasWidth = width;
85
+ const canvasHeight = height;
86
86
  view.width = Math.max(1, Math.ceil(canvasWidth * pixelRatio));
87
87
  view.height = Math.max(1, Math.ceil(canvasHeight * pixelRatio));
88
88
  view.style.width = `${canvasWidth}px`;
@@ -174,22 +174,22 @@ 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(font) {
178
+ const _font = font ?? modernFont.fonts.get(this.computedStyle.fontFamily)?.font;
179
+ if (_font instanceof modernFont.Woff || _font instanceof modernFont.Ttf) {
180
+ return _font.sfnt;
181
181
  }
182
182
  return void 0;
183
183
  }
184
- updateGlyph(font = this._font()) {
185
- if (!font) {
184
+ updateGlyph(sfnt = this._getFontSfnt()) {
185
+ if (!sfnt) {
186
186
  return this;
187
187
  }
188
- const { unitsPerEm, ascender, descender, os2, post } = font;
188
+ const { unitsPerEm, ascender, descender, os2, post } = sfnt;
189
189
  const { content, computedStyle } = this;
190
190
  const { fontSize } = computedStyle;
191
191
  const rate = unitsPerEm / fontSize;
192
- const advanceWidth = font.getAdvanceWidth(content, fontSize);
192
+ const advanceWidth = sfnt.getAdvanceWidth(content, fontSize);
193
193
  const advanceHeight = (ascender + Math.abs(descender)) / rate;
194
194
  const baseline = ascender / rate;
195
195
  const yStrikeoutPosition = (ascender - os2.yStrikeoutPosition) / rate;
@@ -206,12 +206,12 @@ class Character {
206
206
  this.centerDiviation = advanceHeight / 2 - baseline;
207
207
  return this;
208
208
  }
209
- updatePath() {
210
- const font = this._font();
211
- if (!font) {
209
+ update(font) {
210
+ const sfnt = this._getFontSfnt(font);
211
+ if (!sfnt) {
212
212
  return this;
213
213
  }
214
- this.updateGlyph(font);
214
+ this.updateGlyph(sfnt);
215
215
  const {
216
216
  isVertical,
217
217
  content,
@@ -219,7 +219,7 @@ class Character {
219
219
  baseline,
220
220
  inlineBox
221
221
  } = this;
222
- const { os2, head, ascender, descender } = font;
222
+ const { os2, head, ascender, descender } = sfnt;
223
223
  const typoAscender = os2.sTypoAscender;
224
224
  const fontStyle = fsSelectionMap[os2.fsSelection] ?? macStyleMap[head.macStyle];
225
225
  const { left, top } = inlineBox;
@@ -237,7 +237,7 @@ class Character {
237
237
  }
238
238
  if (isVertical && !set1.has(content) && (content.codePointAt(0) <= 256 || set2.has(content))) {
239
239
  path.addCommands(
240
- font.getPathCommands(content, x, top + baseline - (inlineBox.height - inlineBox.width) / 2, style.fontSize) ?? []
240
+ sfnt.getPathCommands(content, x, top + baseline - (inlineBox.height - inlineBox.width) / 2, style.fontSize) ?? []
241
241
  );
242
242
  const point = {
243
243
  y: top - (inlineBox.height - inlineBox.width) / 2 + inlineBox.height / 2,
@@ -256,7 +256,7 @@ class Character {
256
256
  } else {
257
257
  if (glyphIndex !== void 0) {
258
258
  path.addCommands(
259
- font.glyphs.get(glyphIndex).getPathCommands(x, y, style.fontSize)
259
+ sfnt.glyphs.get(glyphIndex).getPathCommands(x, y, style.fontSize)
260
260
  );
261
261
  if (needsItalic) {
262
262
  this._italic(
@@ -269,7 +269,7 @@ class Character {
269
269
  }
270
270
  } else {
271
271
  path.addCommands(
272
- font.getPathCommands(content, x, y, style.fontSize) ?? []
272
+ sfnt.getPathCommands(content, x, y, style.fontSize) ?? []
273
273
  );
274
274
  if (needsItalic) {
275
275
  this._italic(
@@ -293,10 +293,6 @@ class Character {
293
293
  this.glyphBox = this.getGlyphBoundingBox();
294
294
  return this;
295
295
  }
296
- update() {
297
- this.updatePath();
298
- return this;
299
- }
300
296
  _decoration() {
301
297
  const { isVertical, underlinePosition, yStrikeoutPosition } = this;
302
298
  const { textDecoration, fontSize } = this.computedStyle;
@@ -1113,6 +1109,7 @@ class Text {
1113
1109
  __publicField(this, "parser", new Parser(this));
1114
1110
  __publicField(this, "measurer", new Measurer(this));
1115
1111
  __publicField(this, "plugins", /* @__PURE__ */ new Map());
1112
+ __publicField(this, "font");
1116
1113
  const { content = "", style = {}, measureDom, effects } = options;
1117
1114
  this.content = content;
1118
1115
  this.style = style;
@@ -1148,7 +1145,7 @@ class Text {
1148
1145
  this.paragraphs = result.paragraphs;
1149
1146
  this.lineBox = result.boundingBox;
1150
1147
  this.characters.forEach((c) => {
1151
- c.update();
1148
+ c.update(this.font);
1152
1149
  });
1153
1150
  this.rawGlyphBox = this.getGlyphBox();
1154
1151
  const plugins = [...this.plugins.values()];
@@ -1196,8 +1193,8 @@ class Text {
1196
1193
  const { lineBox, rawGlyphBox, pathBox } = this;
1197
1194
  const left = pathBox.left + lineBox.left - rawGlyphBox.left;
1198
1195
  const top = pathBox.top + lineBox.top - rawGlyphBox.top;
1199
- const right = pathBox.right + Math.max(0, lineBox.right - rawGlyphBox.right);
1200
- const bottom = pathBox.bottom + Math.max(0, lineBox.bottom - rawGlyphBox.bottom);
1196
+ const right = pathBox.right + lineBox.right - rawGlyphBox.right;
1197
+ const bottom = pathBox.bottom + lineBox.bottom - rawGlyphBox.bottom;
1201
1198
  this.boundingBox = new modernPath2d.BoundingBox(
1202
1199
  left,
1203
1200
  top,
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(font?: Font): Sfnt | undefined;
134
+ updateGlyph(sfnt?: Sfnt | undefined): this;
135
+ update(font?: 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): {
@@ -267,6 +266,7 @@ declare class Text {
267
266
  parser: Parser;
268
267
  measurer: Measurer;
269
268
  plugins: Map<string, Plugin>;
269
+ font?: Font;
270
270
  get fontSize(): number;
271
271
  get isVertical(): boolean;
272
272
  get characters(): Character[];
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(font?: Font): Sfnt | undefined;
134
+ updateGlyph(sfnt?: Sfnt | undefined): this;
135
+ update(font?: 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): {
@@ -267,6 +266,7 @@ declare class Text {
267
266
  parser: Parser;
268
267
  measurer: Measurer;
269
268
  plugins: Map<string, Plugin>;
269
+ font?: Font;
270
270
  get fontSize(): number;
271
271
  get isVertical(): boolean;
272
272
  get characters(): Character[];
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(font?: Font): Sfnt | undefined;
134
+ updateGlyph(sfnt?: Sfnt | undefined): this;
135
+ update(font?: 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): {
@@ -267,6 +266,7 @@ declare class Text {
267
266
  parser: Parser;
268
267
  measurer: Measurer;
269
268
  plugins: Map<string, Plugin>;
269
+ font?: Font;
270
270
  get fontSize(): number;
271
271
  get isVertical(): boolean;
272
272
  get characters(): Character[];