modern-text 0.2.6 → 0.2.8

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
@@ -1,4 +1,4 @@
1
- import { BoundingBox, Point2D, Path2D, parseSvgToDom, parseSvg, Matrix3 } from 'modern-path2d';
1
+ import { BoundingBox, Path2D, Vector2, parseSvgToDom, parseSvg, Matrix3 } from 'modern-path2d';
2
2
  import { fonts, Woff, Ttf } from 'modern-font';
3
3
  export * from 'modern-font';
4
4
 
@@ -172,8 +172,7 @@ class Character {
172
172
  __publicField$4(this, "boundingBox", new BoundingBox());
173
173
  __publicField$4(this, "textWidth", 0);
174
174
  __publicField$4(this, "textHeight", 0);
175
- // glyph
176
- __publicField$4(this, "commands", []);
175
+ __publicField$4(this, "path", new Path2D());
177
176
  }
178
177
  get computedStyle() {
179
178
  return this.parent.computedStyle;
@@ -219,7 +218,7 @@ class Character {
219
218
  this.centerPoint = this.glyphBox.getCenterPoint();
220
219
  return this;
221
220
  }
222
- updateCommands() {
221
+ updatePath() {
223
222
  const font = this._font();
224
223
  if (!font) {
225
224
  return this;
@@ -291,11 +290,11 @@ class Character {
291
290
  }
292
291
  }
293
292
  commands.push(...this._decoration());
294
- this.commands = commands;
293
+ this.path = new Path2D(commands);
295
294
  return this;
296
295
  }
297
296
  update() {
298
- this.updateCommands();
297
+ this.updatePath();
299
298
  return this;
300
299
  }
301
300
  _decoration() {
@@ -375,71 +374,16 @@ class Character {
375
374
  return cmd;
376
375
  });
377
376
  }
378
- forEachCommand(cb) {
379
- const commands = this.commands;
380
- const last = { x: 0, y: 0 };
381
- const first = { x: 0, y: 0 };
382
- let isFirst = true;
383
- let doSetFirstPoint = false;
384
- for (let i = 0, len = commands.length; i < len; i++) {
385
- if (isFirst) {
386
- doSetFirstPoint = true;
387
- isFirst = false;
388
- }
389
- let command = commands[i];
390
- command = cb(command, i, { last, first }) ?? command;
391
- switch (command.type) {
392
- case "M":
393
- case "L":
394
- case "Q":
395
- last.x = command.x;
396
- last.y = command.y;
397
- if (doSetFirstPoint) {
398
- first.x = last.x;
399
- first.y = last.y;
400
- }
401
- break;
402
- case "Z":
403
- last.x = first.x;
404
- last.y = first.y;
405
- isFirst = true;
406
- break;
407
- }
408
- }
409
- return this;
377
+ getMinMax(min, max) {
378
+ return this.path.getMinMax(min, max);
410
379
  }
411
- getMinMax(min = Point2D.MAX, max = Point2D.MIN) {
412
- let last = { x: 0, y: 0 };
413
- this.commands.forEach((cmd) => {
414
- switch (cmd.type) {
415
- case "L":
416
- case "M":
417
- min.x = Math.min(min.x, cmd.x);
418
- min.y = Math.min(min.y, cmd.y);
419
- max.x = Math.max(max.x, cmd.x);
420
- max.y = Math.max(max.y, cmd.y);
421
- last = { x: cmd.x, y: cmd.y };
422
- break;
423
- case "Q": {
424
- const x1 = 0.5 * (last.x + cmd.x1);
425
- const y1 = 0.5 * (last.y + cmd.y1);
426
- const x2 = 0.5 * (last.x + cmd.x);
427
- const y2 = 0.5 * (last.y + cmd.y);
428
- min.x = Math.min(min.x, last.x, cmd.x, x1, x2);
429
- min.y = Math.min(min.y, last.y, cmd.y, y1, y2);
430
- max.x = Math.max(max.x, last.x, cmd.x, x1, x2);
431
- max.y = Math.max(max.y, last.y, cmd.y, y1, y2);
432
- last = { x: cmd.x, y: cmd.y };
433
- break;
434
- }
435
- }
436
- });
437
- return { min, max };
380
+ getBoundingBox() {
381
+ return this.path.getBoundingBox();
438
382
  }
439
383
  drawTo(ctx, config = {}) {
440
384
  drawPaths({
441
385
  ctx,
442
- paths: [new Path2D(this.commands)],
386
+ paths: [this.path],
443
387
  fontSize: this.computedStyle.fontSize,
444
388
  color: this.computedStyle.color,
445
389
  ...config
@@ -578,8 +522,8 @@ class Highlighter extends Feature {
578
522
  if (!this.paths.length) {
579
523
  return new BoundingBox();
580
524
  }
581
- const min = Point2D.MAX;
582
- const max = Point2D.MIN;
525
+ const min = Vector2.MAX;
526
+ const max = Vector2.MIN;
583
527
  this.paths.forEach((path) => path.getMinMax(min, max));
584
528
  return new BoundingBox(min.x, min.y, max.x - min.x, max.y - min.y);
585
529
  }
@@ -613,8 +557,8 @@ class Highlighter extends Feature {
613
557
  _parseSvg(url) {
614
558
  const svg = parseSvgToDom(url);
615
559
  const paths = parseSvg(svg);
616
- const min = Point2D.MAX;
617
- const max = Point2D.MIN;
560
+ const min = Vector2.MAX;
561
+ const max = Vector2.MIN;
618
562
  paths.forEach((path) => path.getMinMax(min, max));
619
563
  const { x, y, width, height } = new BoundingBox(min.x, min.y, max.x - min.x, max.y - min.y);
620
564
  const viewBox = svg.getAttribute("viewBox").split(" ").map(Number);
@@ -1042,12 +986,12 @@ class Text {
1042
986
  __publicField(this, "paragraphs", []);
1043
987
  __publicField(this, "boundingBox", new BoundingBox());
1044
988
  __publicField(this, "renderBoundingBox", new BoundingBox());
1045
- __publicField(this, "_parser", new Parser(this));
1046
- __publicField(this, "_measurer", new Measurer(this));
1047
- __publicField(this, "_deformer", new Deformer(this));
1048
- __publicField(this, "_effector", new Effector(this));
1049
- __publicField(this, "_highlighter", new Highlighter(this));
1050
- __publicField(this, "_renderer2D", new Renderer2D(this));
989
+ __publicField(this, "parser", new Parser(this));
990
+ __publicField(this, "measurer", new Measurer(this));
991
+ __publicField(this, "deformer", new Deformer(this));
992
+ __publicField(this, "effector", new Effector(this));
993
+ __publicField(this, "highlighter", new Highlighter(this));
994
+ __publicField(this, "renderer2D", new Renderer2D(this));
1051
995
  const { content = "", style = {}, effects, deformation, measureDom } = options;
1052
996
  this.content = content;
1053
997
  this.style = style;
@@ -1061,8 +1005,8 @@ class Text {
1061
1005
  measure(dom = this.measureDom) {
1062
1006
  this.computedStyle = { ...defaultTextStyles, ...this.style };
1063
1007
  const old = this.paragraphs;
1064
- this.paragraphs = this._parser.parse();
1065
- const result = this._measurer.measure(dom);
1008
+ this.paragraphs = this.parser.parse();
1009
+ const result = this.measurer.measure(dom);
1066
1010
  this.paragraphs = old;
1067
1011
  return result;
1068
1012
  }
@@ -1076,12 +1020,12 @@ class Text {
1076
1020
  this.boundingBox = boundingBox;
1077
1021
  const characters = this.characters;
1078
1022
  characters.forEach((c) => c.update());
1023
+ this.highlighter.highlight();
1079
1024
  if (this.deformation) {
1080
- this._deformer.deform();
1025
+ this.deformer.deform();
1081
1026
  }
1082
- this._highlighter.highlight();
1083
- const min = Point2D.MAX;
1084
- const max = Point2D.MIN;
1027
+ const min = Vector2.MAX;
1028
+ const max = Vector2.MIN;
1085
1029
  characters.forEach((c) => c.getMinMax(min, max));
1086
1030
  this.renderBoundingBox = new BoundingBox(min.x, min.y, max.x - min.x, max.y - min.y);
1087
1031
  return this;
@@ -1099,23 +1043,23 @@ class Text {
1099
1043
  this.renderBoundingBox = BoundingBox.from(
1100
1044
  this.boundingBox,
1101
1045
  this.renderBoundingBox,
1102
- this._effector.getBoundingBox(),
1103
- this._highlighter.getBoundingBox()
1046
+ this.effector.getBoundingBox(),
1047
+ this.highlighter.getBoundingBox()
1104
1048
  );
1105
1049
  } else {
1106
1050
  this.renderBoundingBox = BoundingBox.from(
1107
1051
  this.boundingBox,
1108
1052
  this.renderBoundingBox,
1109
- this._highlighter.getBoundingBox()
1053
+ this.highlighter.getBoundingBox()
1110
1054
  );
1111
1055
  }
1112
- this._renderer2D.setupView({ pixelRatio, ctx });
1113
- this._renderer2D.uploadColors({ ctx });
1114
- this._highlighter.draw({ ctx });
1056
+ this.renderer2D.setupView({ pixelRatio, ctx });
1057
+ this.renderer2D.uploadColors({ ctx });
1058
+ this.highlighter.draw({ ctx });
1115
1059
  if (this.effects?.length) {
1116
- this._effector.draw({ ctx });
1060
+ this.effector.draw({ ctx });
1117
1061
  } else {
1118
- this._renderer2D.draw({ ctx });
1062
+ this.renderer2D.draw({ ctx });
1119
1063
  }
1120
1064
  return this;
1121
1065
  }
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "modern-text",
3
3
  "type": "module",
4
- "version": "0.2.6",
4
+ "version": "0.2.8",
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",
@@ -58,7 +58,7 @@
58
58
  },
59
59
  "dependencies": {
60
60
  "modern-font": "^0.1.5",
61
- "modern-path2d": "^0.1.7"
61
+ "modern-path2d": "^0.1.8"
62
62
  },
63
63
  "devDependencies": {
64
64
  "@antfu/eslint-config": "^3.7.3",