modern-text 0.2.44 → 0.3.1

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,20 +81,19 @@ 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
- view.width = Math.max(1, Math.ceil(width * pixelRatio));
85
- view.height = Math.max(1, Math.ceil(height * pixelRatio));
86
- view.style.marginTop = `${top}px`;
87
- view.style.marginLeft = `${left}px`;
88
- view.style.width = `${width}px`;
89
- view.style.height = `${height}px`;
84
+ const canvasWidth = width + left;
85
+ const canvasHeight = height + top;
86
+ view.width = Math.max(1, Math.ceil(canvasWidth * pixelRatio));
87
+ view.height = Math.max(1, Math.ceil(canvasHeight * pixelRatio));
88
+ view.style.width = `${canvasWidth}px`;
89
+ view.style.height = `${canvasHeight}px`;
90
90
  ctx.clearRect(0, 0, ctx.canvas.width, ctx.canvas.height);
91
91
  ctx.scale(pixelRatio, pixelRatio);
92
- ctx.translate(-left, -top);
93
92
  }
94
93
 
95
94
  function uploadColors(ctx, text) {
96
- const { paragraphs, computedStyle: style, renderBoundingBox } = text;
97
- uploadColor(style, renderBoundingBox, ctx);
95
+ const { paragraphs, computedStyle: style, glyphBox } = text;
96
+ uploadColor(style, glyphBox, ctx);
98
97
  paragraphs.forEach((paragraph) => {
99
98
  uploadColor(paragraph.computedStyle, paragraph.lineBox, ctx);
100
99
  paragraph.fragments.forEach((fragment) => {
@@ -980,7 +979,7 @@ function render() {
980
979
  return boxes.length ? modernPath2d.BoundingBox.from(...boxes) : void 0;
981
980
  },
982
981
  render: (ctx, text) => {
983
- const { characters, paragraphs, renderBoundingBox, effects, style } = text;
982
+ const { characters, paragraphs, glyphBox, effects, style } = text;
984
983
  function fillBackground(color, box) {
985
984
  ctx.fillStyle = color;
986
985
  ctx.fillRect(box.left, box.top, box.width, box.height);
@@ -995,7 +994,7 @@ function render() {
995
994
  });
996
995
  if (effects) {
997
996
  effects.forEach((style2) => {
998
- uploadColor(style2, renderBoundingBox, ctx);
997
+ uploadColor(style2, glyphBox, ctx);
999
998
  ctx.save();
1000
999
  const [a, c, e, b, d, f] = getTransform2D(text, style2).transpose().elements;
1001
1000
  ctx.transform(a, b, c, d, e, f);
@@ -1023,13 +1022,13 @@ function render() {
1023
1022
  });
1024
1023
  }
1025
1024
  function getTransform2D(text, style) {
1026
- const { fontSize, renderBoundingBox } = text;
1025
+ const { fontSize, glyphBox } = text;
1027
1026
  const translateX = (style.translateX ?? 0) * fontSize;
1028
1027
  const translateY = (style.translateY ?? 0) * fontSize;
1029
1028
  const PI_2 = Math.PI * 2;
1030
1029
  const skewX = (style.skewX ?? 0) / 360 * PI_2;
1031
1030
  const skewY = (style.skewY ?? 0) / 360 * PI_2;
1032
- const { left, top, width, height } = renderBoundingBox;
1031
+ const { left, top, width, height } = glyphBox;
1033
1032
  const centerX = left + width / 2;
1034
1033
  const centerY = top + height / 2;
1035
1034
  tempM1.identity();
@@ -1105,7 +1104,7 @@ class Text {
1105
1104
  __publicField(this, "computedStyle", { ...defaultTextStyles });
1106
1105
  __publicField(this, "paragraphs", []);
1107
1106
  __publicField(this, "boundingBox", new modernPath2d.BoundingBox());
1108
- __publicField(this, "renderBoundingBox", new modernPath2d.BoundingBox());
1107
+ __publicField(this, "glyphBox", new modernPath2d.BoundingBox());
1109
1108
  __publicField(this, "parser", new Parser(this));
1110
1109
  __publicField(this, "measurer", new Measurer(this));
1111
1110
  __publicField(this, "plugins", /* @__PURE__ */ new Map());
@@ -1131,10 +1130,15 @@ class Text {
1131
1130
  }
1132
1131
  measure(dom = this.measureDom) {
1133
1132
  this.computedStyle = { ...defaultTextStyles, ...this.style };
1134
- const oldParagraphs = this.paragraphs;
1135
- const oldRenderBoundingBox = this.renderBoundingBox;
1133
+ const old = {
1134
+ paragraphs: this.paragraphs,
1135
+ boundingBox: this.boundingBox,
1136
+ glyphBox: this.glyphBox
1137
+ };
1136
1138
  this.paragraphs = this.parser.parse();
1137
1139
  const result = this.measurer.measure(dom);
1140
+ this.paragraphs = result.paragraphs;
1141
+ this.boundingBox = result.boundingBox;
1138
1142
  const characters = this.characters;
1139
1143
  characters.forEach((c) => c.update());
1140
1144
  const plugins = [...this.plugins.values()];
@@ -1152,9 +1156,13 @@ class Text {
1152
1156
  max.max(a, b);
1153
1157
  }
1154
1158
  });
1155
- this.renderBoundingBox = new modernPath2d.BoundingBox(min.x, min.y, max.x - min.x, max.y - min.y);
1156
- this.renderBoundingBox = modernPath2d.BoundingBox.from(
1157
- this.renderBoundingBox,
1159
+ this.glyphBox = new modernPath2d.BoundingBox(min.x, min.y, max.x - min.x, max.y - min.y);
1160
+ const dLeft = this.glyphBox.left - result.boundingBox.left;
1161
+ const dRight = result.boundingBox.right - this.glyphBox.right;
1162
+ const dTop = this.glyphBox.top - result.boundingBox.top;
1163
+ const dBottom = result.boundingBox.bottom - this.glyphBox.bottom;
1164
+ this.glyphBox = modernPath2d.BoundingBox.from(
1165
+ this.glyphBox,
1158
1166
  ...plugins.map((plugin) => {
1159
1167
  if (plugin.getBoundingBox) {
1160
1168
  return plugin.getBoundingBox(this);
@@ -1162,9 +1170,12 @@ class Text {
1162
1170
  return modernPath2d.getPathsBoundingBox(plugin.paths ?? []);
1163
1171
  }).filter(Boolean)
1164
1172
  );
1165
- result.renderBoundingBox = this.renderBoundingBox;
1166
- this.paragraphs = oldParagraphs;
1167
- this.renderBoundingBox = oldRenderBoundingBox;
1173
+ result.glyphBox = this.glyphBox;
1174
+ result.boundingBox.width = this.glyphBox.width + dLeft + dRight;
1175
+ result.boundingBox.height = this.glyphBox.height + dTop + dBottom;
1176
+ this.paragraphs = old.paragraphs;
1177
+ this.boundingBox = old.boundingBox;
1178
+ this.glyphBox = old.glyphBox;
1168
1179
  return result;
1169
1180
  }
1170
1181
  requestUpdate() {
@@ -1172,10 +1183,10 @@ class Text {
1172
1183
  return this;
1173
1184
  }
1174
1185
  update() {
1175
- const { paragraphs, boundingBox, renderBoundingBox } = this.measure();
1186
+ const { paragraphs, boundingBox, glyphBox } = this.measure();
1176
1187
  this.paragraphs = paragraphs;
1177
1188
  this.boundingBox = boundingBox;
1178
- this.renderBoundingBox = renderBoundingBox;
1189
+ this.glyphBox = glyphBox;
1179
1190
  return this;
1180
1191
  }
1181
1192
  render(options) {
@@ -1187,7 +1198,7 @@ class Text {
1187
1198
  if (this.needsUpdate) {
1188
1199
  this.update();
1189
1200
  }
1190
- setupView(ctx, pixelRatio, this.renderBoundingBox);
1201
+ setupView(ctx, pixelRatio, this.boundingBox);
1191
1202
  uploadColors(ctx, this);
1192
1203
  const plugins = [...this.plugins.values()];
1193
1204
  plugins.sort((a, b) => (a.renderOrder ?? 0) - (b.renderOrder ?? 0)).forEach((plugin) => {
package/dist/index.d.cts CHANGED
@@ -243,7 +243,7 @@ interface TextOptions {
243
243
  effects?: Partial<TextStyle>[];
244
244
  }
245
245
  type MeasureResult = MeasureDomResult & {
246
- renderBoundingBox: BoundingBox;
246
+ glyphBox: BoundingBox;
247
247
  };
248
248
  declare const defaultTextStyles: TextStyle;
249
249
  declare class Text {
@@ -255,7 +255,7 @@ declare class Text {
255
255
  computedStyle: TextStyle;
256
256
  paragraphs: Paragraph[];
257
257
  boundingBox: BoundingBox;
258
- renderBoundingBox: BoundingBox;
258
+ glyphBox: BoundingBox;
259
259
  parser: Parser;
260
260
  measurer: Measurer;
261
261
  plugins: Map<string, Plugin>;
package/dist/index.d.mts CHANGED
@@ -243,7 +243,7 @@ interface TextOptions {
243
243
  effects?: Partial<TextStyle>[];
244
244
  }
245
245
  type MeasureResult = MeasureDomResult & {
246
- renderBoundingBox: BoundingBox;
246
+ glyphBox: BoundingBox;
247
247
  };
248
248
  declare const defaultTextStyles: TextStyle;
249
249
  declare class Text {
@@ -255,7 +255,7 @@ declare class Text {
255
255
  computedStyle: TextStyle;
256
256
  paragraphs: Paragraph[];
257
257
  boundingBox: BoundingBox;
258
- renderBoundingBox: BoundingBox;
258
+ glyphBox: BoundingBox;
259
259
  parser: Parser;
260
260
  measurer: Measurer;
261
261
  plugins: Map<string, Plugin>;
package/dist/index.d.ts CHANGED
@@ -243,7 +243,7 @@ interface TextOptions {
243
243
  effects?: Partial<TextStyle>[];
244
244
  }
245
245
  type MeasureResult = MeasureDomResult & {
246
- renderBoundingBox: BoundingBox;
246
+ glyphBox: BoundingBox;
247
247
  };
248
248
  declare const defaultTextStyles: TextStyle;
249
249
  declare class Text {
@@ -255,7 +255,7 @@ declare class Text {
255
255
  computedStyle: TextStyle;
256
256
  paragraphs: Paragraph[];
257
257
  boundingBox: BoundingBox;
258
- renderBoundingBox: BoundingBox;
258
+ glyphBox: BoundingBox;
259
259
  parser: Parser;
260
260
  measurer: Measurer;
261
261
  plugins: Map<string, Plugin>;