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.mjs 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 = fonts.get(this.computedStyle.fontFamily)?.font;
179
- if (font instanceof Woff || font instanceof Ttf) {
180
- return font.sfnt;
177
+ _getFontSfnt(font) {
178
+ const _font = font ?? fonts.get(this.computedStyle.fontFamily)?.font;
179
+ if (_font instanceof Woff || _font instanceof 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 BoundingBox(
1202
1199
  left,
1203
1200
  top,
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "modern-text",
3
3
  "type": "module",
4
- "version": "0.3.4",
4
+ "version": "0.3.6",
5
5
  "packageManager": "pnpm@9.9.0",
6
6
  "description": "Measure and render text in a way that describes the DOM.",
7
7
  "author": "wxm",
@@ -57,7 +57,7 @@
57
57
  "prepare": "simple-git-hooks"
58
58
  },
59
59
  "dependencies": {
60
- "modern-font": "^0.2.1",
60
+ "modern-font": "^0.3.0",
61
61
  "modern-path2d": "^0.2.5"
62
62
  },
63
63
  "devDependencies": {