modern-text 0.2.13 → 0.2.15

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
@@ -52,28 +52,31 @@ function parseCssLinearGradient(css, x, y, width, height) {
52
52
  };
53
53
  }
54
54
 
55
- function drawPaths(options) {
56
- const { ctx, paths, fontSize } = options;
57
- paths.forEach((path) => {
58
- ctx.save();
55
+ function drawPath(options) {
56
+ const { ctx, path, fontSize, clipRect } = options;
57
+ ctx.save();
58
+ ctx.beginPath();
59
+ const style = path.style;
60
+ path.style = {
61
+ ...style,
62
+ fill: options.color ?? style.fill,
63
+ stroke: options.textStrokeColor ?? style.stroke,
64
+ strokeWidth: options.textStrokeWidth ? options.textStrokeWidth * fontSize : style.strokeWidth,
65
+ shadowOffsetX: (options.shadowOffsetX ?? 0) * fontSize,
66
+ shadowOffsetY: (options.shadowOffsetY ?? 0) * fontSize,
67
+ shadowBlur: (options.shadowBlur ?? 0) * fontSize,
68
+ shadowColor: options.shadowColor
69
+ };
70
+ const offsetX = (options.offsetX ?? 0) * fontSize;
71
+ const offsetY = (options.offsetY ?? 0) * fontSize;
72
+ ctx.translate(offsetX, offsetY);
73
+ if (clipRect) {
74
+ ctx.rect(clipRect.x, clipRect.y, clipRect.width, clipRect.height);
75
+ ctx.clip();
59
76
  ctx.beginPath();
60
- const style = path.style;
61
- path.style = {
62
- ...style,
63
- fill: options.color ?? style.fill,
64
- stroke: options.textStrokeColor ?? style.stroke,
65
- strokeWidth: options.textStrokeWidth ? options.textStrokeWidth * fontSize : style.strokeWidth,
66
- shadowOffsetX: (options.shadowOffsetX ?? 0) * fontSize,
67
- shadowOffsetY: (options.shadowOffsetY ?? 0) * fontSize,
68
- shadowBlur: (options.shadowBlur ?? 0) * fontSize,
69
- shadowColor: options.shadowColor
70
- };
71
- const offsetX = (options.offsetX ?? 0) * fontSize;
72
- const offsetY = (options.offsetY ?? 0) * fontSize;
73
- ctx.translate(offsetX, offsetY);
74
- path.drawTo(ctx);
75
- ctx.restore();
76
- });
77
+ }
78
+ path.drawTo(ctx);
79
+ ctx.restore();
77
80
  }
78
81
 
79
82
  function filterEmpty(val) {
@@ -209,7 +212,6 @@ class Character {
209
212
  this.centerDiviation = 0.5 * height - baseline;
210
213
  this.glyphBox = isVertical ? new modernPath2d.BoundingBox(left, top, glyphHeight, glyphWidth) : new modernPath2d.BoundingBox(left, top, glyphWidth, glyphHeight);
211
214
  this.centerPoint = this.glyphBox.getCenterPoint();
212
- this.fontMinGlyphWidth = font.getAdvanceWidth("i", fontSize);
213
215
  return this;
214
216
  }
215
217
  updatePath() {
@@ -263,7 +265,7 @@ class Character {
263
265
  commands = this._rotation90(commands, point);
264
266
  } else {
265
267
  if (glyphIndex !== void 0) {
266
- commands = font.glyf.glyphs.get(glyphIndex).getPathCommands(x, y, fontSize);
268
+ commands = font.glyphs.get(glyphIndex).getPathCommands(x, y, fontSize);
267
269
  if (fontStyle === "italic") {
268
270
  commands = this._italic(
269
271
  commands,
@@ -370,6 +372,11 @@ class Character {
370
372
  [cmd.x1, cmd.y1] = cb(cmd.x1, cmd.y1);
371
373
  [cmd.x, cmd.y] = cb(cmd.x, cmd.y);
372
374
  break;
375
+ case "C":
376
+ [cmd.x1, cmd.y1] = cb(cmd.x1, cmd.y1);
377
+ [cmd.x2, cmd.y2] = cb(cmd.x2, cmd.y2);
378
+ [cmd.x, cmd.y] = cb(cmd.x, cmd.y);
379
+ break;
373
380
  }
374
381
  return cmd;
375
382
  });
@@ -381,9 +388,9 @@ class Character {
381
388
  return this.path.getBoundingBox();
382
389
  }
383
390
  drawTo(ctx, config = {}) {
384
- drawPaths({
391
+ drawPath({
385
392
  ctx,
386
- paths: [this.path],
393
+ path: this.path,
387
394
  fontSize: this.computedStyle.fontSize,
388
395
  color: this.computedStyle.color,
389
396
  ...config
@@ -524,7 +531,7 @@ class Highlighter extends Feature {
524
531
  }
525
532
  const min = modernPath2d.Vector2.MAX;
526
533
  const max = modernPath2d.Vector2.MIN;
527
- this.paths.forEach((path) => path.getMinMax(min, max));
534
+ this.paths.forEach((v) => v.path.getMinMax(min, max));
528
535
  return new modernPath2d.BoundingBox(min.x, min.y, max.x - min.x, max.y - min.y);
529
536
  }
530
537
  highlight() {
@@ -550,7 +557,6 @@ class Highlighter extends Feature {
550
557
  url: characters2[0].parent.highlight.url,
551
558
  box: modernPath2d.BoundingBox.from(...characters2.map((c) => c.boundingBox)),
552
559
  baseline: Math.max(...characters2.map((c) => c.baseline)),
553
- fontMinGlyphWidth: characters2[0].fontMinGlyphWidth,
554
560
  fontSize: characters2[0].fontSize
555
561
  };
556
562
  }).map((group2) => this._parseGroup(group2)).flat();
@@ -568,23 +574,23 @@ class Highlighter extends Feature {
568
574
  };
569
575
  }
570
576
  _parseGroup(group) {
571
- const { url, box: groupBox, baseline, fontSize, fontMinGlyphWidth } = group;
577
+ const { url, box: groupBox, baseline, fontSize } = group;
572
578
  const { box, viewBox, paths } = this._parseSvg(url);
579
+ const centerY = viewBox.top + viewBox.height / 2;
573
580
  const result = [];
574
- const type = box.height / viewBox.height > 0.3 ? 0 : 1;
575
- function transformPathStyle(path) {
576
- const rate = fontSize * 0.03;
581
+ const type = centerY > box.top ? 0 : 1;
582
+ function transformPathStyle(path, scale) {
577
583
  if (path.style.strokeWidth) {
578
- path.style.strokeWidth *= rate;
584
+ path.style.strokeWidth *= scale;
579
585
  }
580
586
  if (path.style.strokeMiterlimit) {
581
- path.style.strokeMiterlimit *= rate;
587
+ path.style.strokeMiterlimit *= scale;
582
588
  }
583
589
  if (path.style.strokeDashoffset) {
584
- path.style.strokeDashoffset *= rate;
590
+ path.style.strokeDashoffset *= scale;
585
591
  }
586
592
  if (path.style.strokeDasharray) {
587
- path.style.strokeDasharray = path.style.strokeDasharray.map((v) => v * rate);
593
+ path.style.strokeDasharray = path.style.strokeDasharray.map((v) => v * scale);
588
594
  }
589
595
  }
590
596
  if (type === 0) {
@@ -597,16 +603,15 @@ class Highlighter extends Feature {
597
603
  const m = new modernPath2d.Matrix3().translate(-box.x, -box.y).scale(scaleX, scaleY).translate(offset.x, offset.y);
598
604
  paths.forEach((original) => {
599
605
  const path = original.clone().transform(m);
600
- transformPathStyle(path);
601
- result.push(path);
606
+ transformPathStyle(path, scaleX);
607
+ result.push({ path });
602
608
  });
603
609
  } else if (type === 1) {
604
- const scale = fontMinGlyphWidth / box.width;
610
+ const scale = fontSize / box.width;
605
611
  const width = box.width * scale;
606
612
  const length = Math.ceil(groupBox.width / width);
607
- const totalWidth = width * length;
608
613
  const offset = {
609
- x: groupBox.left + (groupBox.width - totalWidth) / 2,
614
+ x: groupBox.left,
610
615
  y: groupBox.top + baseline + fontSize * 0.1
611
616
  };
612
617
  const m = new modernPath2d.Matrix3().translate(-box.x, -box.y).scale(scale, scale).translate(offset.x, offset.y);
@@ -614,18 +619,21 @@ class Highlighter extends Feature {
614
619
  const _m = m.clone().translate(i * width, 0);
615
620
  paths.forEach((original) => {
616
621
  const path = original.clone().transform(_m);
617
- transformPathStyle(path);
618
- result.push(path);
622
+ transformPathStyle(path, scale);
623
+ result.push({ clipRect: groupBox, path });
619
624
  });
620
625
  }
621
626
  }
622
627
  return result;
623
628
  }
624
629
  draw({ ctx }) {
625
- drawPaths({
626
- ctx,
627
- paths: this.paths,
628
- fontSize: this._text.computedStyle.fontSize
630
+ this.paths.forEach((v) => {
631
+ drawPath({
632
+ ctx,
633
+ path: v.path,
634
+ clipRect: v.clipRect,
635
+ fontSize: this._text.computedStyle.fontSize
636
+ });
629
637
  });
630
638
  return this;
631
639
  }
@@ -1092,7 +1100,7 @@ exports.Reflector = Reflector;
1092
1100
  exports.Renderer2D = Renderer2D;
1093
1101
  exports.Text = Text;
1094
1102
  exports.defaultTextStyles = defaultTextStyles;
1095
- exports.drawPaths = drawPaths;
1103
+ exports.drawPath = drawPath;
1096
1104
  exports.filterEmpty = filterEmpty;
1097
1105
  exports.getPointPosition = getPointPosition;
1098
1106
  exports.getRotationPoint = getRotationPoint;
package/dist/index.d.cts CHANGED
@@ -72,10 +72,11 @@ interface LinearGradient {
72
72
 
73
73
  interface DrawShapePathsOptions extends Partial<TextEffect> {
74
74
  ctx: CanvasRenderingContext2D;
75
- paths: Path2D[];
75
+ path: Path2D;
76
76
  fontSize: number;
77
+ clipRect?: BoundingBox;
77
78
  }
78
- declare function drawPaths(options: DrawShapePathsOptions): void;
79
+ declare function drawPath(options: DrawShapePathsOptions): void;
79
80
 
80
81
  declare class Fragment {
81
82
  content: string;
@@ -109,7 +110,6 @@ declare class Character {
109
110
  centerDiviation: number;
110
111
  glyphBox: BoundingBox;
111
112
  centerPoint: VectorLike;
112
- fontMinGlyphWidth: number;
113
113
  get computedStyle(): TextStyle;
114
114
  get isVertical(): boolean;
115
115
  get fontSize(): number;
@@ -223,10 +223,12 @@ interface HighlightGroup {
223
223
  box: BoundingBox;
224
224
  baseline: number;
225
225
  fontSize: number;
226
- fontMinGlyphWidth: number;
227
226
  }
228
227
  declare class Highlighter extends Feature {
229
- paths: Path2D[];
228
+ paths: {
229
+ clipRect?: BoundingBox;
230
+ path: Path2D;
231
+ }[];
230
232
  getBoundingBox(): BoundingBox;
231
233
  highlight(): void;
232
234
  protected _parseSvg(url: string): {
@@ -234,7 +236,10 @@ declare class Highlighter extends Feature {
234
236
  box: BoundingBox;
235
237
  viewBox: BoundingBox;
236
238
  };
237
- protected _parseGroup(group: HighlightGroup): Path2D[];
239
+ protected _parseGroup(group: HighlightGroup): {
240
+ clipRect?: BoundingBox;
241
+ path: Path2D;
242
+ }[];
238
243
  draw({ ctx }: {
239
244
  ctx: CanvasRenderingContext2D;
240
245
  }): this;
@@ -339,4 +344,4 @@ declare function getPointPosition(point: {
339
344
  y: number;
340
345
  };
341
346
 
342
- export { Character, Deformer, type DrawShapePathsOptions, type EffectOptions, Effector, type FontKerning, type FontStyle, type FontWeight, Fragment, type FragmentContent, type FragmentHighlight, Highlighter, type LinearGradient, type MeasuredCharacter, type MeasuredFragment, type MeasuredParagraph, type MeasuredResult, Measurer, Paragraph, type ParagraphContent, Parser, Reflector, type Render2dOptions, Renderer2D, Text, type TextAlign, type TextContent, type TextDecoration, type TextDeformation, type TextDrawStyle, type TextEffect, type TextLayoutStyle, type TextOptions, type TextOrientation, type TextRenderOptions, type TextStyle, type TextTransform, type TextWrap, type VerticalAlign, type WritingMode, defaultTextStyles, drawPaths, filterEmpty, getPointPosition, getRotationPoint, getScalePoint, getSkewPoint, parseColor, uploadColor };
347
+ export { Character, Deformer, type DrawShapePathsOptions, type EffectOptions, Effector, type FontKerning, type FontStyle, type FontWeight, Fragment, type FragmentContent, type FragmentHighlight, Highlighter, type LinearGradient, type MeasuredCharacter, type MeasuredFragment, type MeasuredParagraph, type MeasuredResult, Measurer, Paragraph, type ParagraphContent, Parser, Reflector, type Render2dOptions, Renderer2D, Text, type TextAlign, type TextContent, type TextDecoration, type TextDeformation, type TextDrawStyle, type TextEffect, type TextLayoutStyle, type TextOptions, type TextOrientation, type TextRenderOptions, type TextStyle, type TextTransform, type TextWrap, type VerticalAlign, type WritingMode, defaultTextStyles, drawPath, filterEmpty, getPointPosition, getRotationPoint, getScalePoint, getSkewPoint, parseColor, uploadColor };
package/dist/index.d.mts CHANGED
@@ -72,10 +72,11 @@ interface LinearGradient {
72
72
 
73
73
  interface DrawShapePathsOptions extends Partial<TextEffect> {
74
74
  ctx: CanvasRenderingContext2D;
75
- paths: Path2D[];
75
+ path: Path2D;
76
76
  fontSize: number;
77
+ clipRect?: BoundingBox;
77
78
  }
78
- declare function drawPaths(options: DrawShapePathsOptions): void;
79
+ declare function drawPath(options: DrawShapePathsOptions): void;
79
80
 
80
81
  declare class Fragment {
81
82
  content: string;
@@ -109,7 +110,6 @@ declare class Character {
109
110
  centerDiviation: number;
110
111
  glyphBox: BoundingBox;
111
112
  centerPoint: VectorLike;
112
- fontMinGlyphWidth: number;
113
113
  get computedStyle(): TextStyle;
114
114
  get isVertical(): boolean;
115
115
  get fontSize(): number;
@@ -223,10 +223,12 @@ interface HighlightGroup {
223
223
  box: BoundingBox;
224
224
  baseline: number;
225
225
  fontSize: number;
226
- fontMinGlyphWidth: number;
227
226
  }
228
227
  declare class Highlighter extends Feature {
229
- paths: Path2D[];
228
+ paths: {
229
+ clipRect?: BoundingBox;
230
+ path: Path2D;
231
+ }[];
230
232
  getBoundingBox(): BoundingBox;
231
233
  highlight(): void;
232
234
  protected _parseSvg(url: string): {
@@ -234,7 +236,10 @@ declare class Highlighter extends Feature {
234
236
  box: BoundingBox;
235
237
  viewBox: BoundingBox;
236
238
  };
237
- protected _parseGroup(group: HighlightGroup): Path2D[];
239
+ protected _parseGroup(group: HighlightGroup): {
240
+ clipRect?: BoundingBox;
241
+ path: Path2D;
242
+ }[];
238
243
  draw({ ctx }: {
239
244
  ctx: CanvasRenderingContext2D;
240
245
  }): this;
@@ -339,4 +344,4 @@ declare function getPointPosition(point: {
339
344
  y: number;
340
345
  };
341
346
 
342
- export { Character, Deformer, type DrawShapePathsOptions, type EffectOptions, Effector, type FontKerning, type FontStyle, type FontWeight, Fragment, type FragmentContent, type FragmentHighlight, Highlighter, type LinearGradient, type MeasuredCharacter, type MeasuredFragment, type MeasuredParagraph, type MeasuredResult, Measurer, Paragraph, type ParagraphContent, Parser, Reflector, type Render2dOptions, Renderer2D, Text, type TextAlign, type TextContent, type TextDecoration, type TextDeformation, type TextDrawStyle, type TextEffect, type TextLayoutStyle, type TextOptions, type TextOrientation, type TextRenderOptions, type TextStyle, type TextTransform, type TextWrap, type VerticalAlign, type WritingMode, defaultTextStyles, drawPaths, filterEmpty, getPointPosition, getRotationPoint, getScalePoint, getSkewPoint, parseColor, uploadColor };
347
+ export { Character, Deformer, type DrawShapePathsOptions, type EffectOptions, Effector, type FontKerning, type FontStyle, type FontWeight, Fragment, type FragmentContent, type FragmentHighlight, Highlighter, type LinearGradient, type MeasuredCharacter, type MeasuredFragment, type MeasuredParagraph, type MeasuredResult, Measurer, Paragraph, type ParagraphContent, Parser, Reflector, type Render2dOptions, Renderer2D, Text, type TextAlign, type TextContent, type TextDecoration, type TextDeformation, type TextDrawStyle, type TextEffect, type TextLayoutStyle, type TextOptions, type TextOrientation, type TextRenderOptions, type TextStyle, type TextTransform, type TextWrap, type VerticalAlign, type WritingMode, defaultTextStyles, drawPath, filterEmpty, getPointPosition, getRotationPoint, getScalePoint, getSkewPoint, parseColor, uploadColor };
package/dist/index.d.ts CHANGED
@@ -72,10 +72,11 @@ interface LinearGradient {
72
72
 
73
73
  interface DrawShapePathsOptions extends Partial<TextEffect> {
74
74
  ctx: CanvasRenderingContext2D;
75
- paths: Path2D[];
75
+ path: Path2D;
76
76
  fontSize: number;
77
+ clipRect?: BoundingBox;
77
78
  }
78
- declare function drawPaths(options: DrawShapePathsOptions): void;
79
+ declare function drawPath(options: DrawShapePathsOptions): void;
79
80
 
80
81
  declare class Fragment {
81
82
  content: string;
@@ -109,7 +110,6 @@ declare class Character {
109
110
  centerDiviation: number;
110
111
  glyphBox: BoundingBox;
111
112
  centerPoint: VectorLike;
112
- fontMinGlyphWidth: number;
113
113
  get computedStyle(): TextStyle;
114
114
  get isVertical(): boolean;
115
115
  get fontSize(): number;
@@ -223,10 +223,12 @@ interface HighlightGroup {
223
223
  box: BoundingBox;
224
224
  baseline: number;
225
225
  fontSize: number;
226
- fontMinGlyphWidth: number;
227
226
  }
228
227
  declare class Highlighter extends Feature {
229
- paths: Path2D[];
228
+ paths: {
229
+ clipRect?: BoundingBox;
230
+ path: Path2D;
231
+ }[];
230
232
  getBoundingBox(): BoundingBox;
231
233
  highlight(): void;
232
234
  protected _parseSvg(url: string): {
@@ -234,7 +236,10 @@ declare class Highlighter extends Feature {
234
236
  box: BoundingBox;
235
237
  viewBox: BoundingBox;
236
238
  };
237
- protected _parseGroup(group: HighlightGroup): Path2D[];
239
+ protected _parseGroup(group: HighlightGroup): {
240
+ clipRect?: BoundingBox;
241
+ path: Path2D;
242
+ }[];
238
243
  draw({ ctx }: {
239
244
  ctx: CanvasRenderingContext2D;
240
245
  }): this;
@@ -339,4 +344,4 @@ declare function getPointPosition(point: {
339
344
  y: number;
340
345
  };
341
346
 
342
- export { Character, Deformer, type DrawShapePathsOptions, type EffectOptions, Effector, type FontKerning, type FontStyle, type FontWeight, Fragment, type FragmentContent, type FragmentHighlight, Highlighter, type LinearGradient, type MeasuredCharacter, type MeasuredFragment, type MeasuredParagraph, type MeasuredResult, Measurer, Paragraph, type ParagraphContent, Parser, Reflector, type Render2dOptions, Renderer2D, Text, type TextAlign, type TextContent, type TextDecoration, type TextDeformation, type TextDrawStyle, type TextEffect, type TextLayoutStyle, type TextOptions, type TextOrientation, type TextRenderOptions, type TextStyle, type TextTransform, type TextWrap, type VerticalAlign, type WritingMode, defaultTextStyles, drawPaths, filterEmpty, getPointPosition, getRotationPoint, getScalePoint, getSkewPoint, parseColor, uploadColor };
347
+ export { Character, Deformer, type DrawShapePathsOptions, type EffectOptions, Effector, type FontKerning, type FontStyle, type FontWeight, Fragment, type FragmentContent, type FragmentHighlight, Highlighter, type LinearGradient, type MeasuredCharacter, type MeasuredFragment, type MeasuredParagraph, type MeasuredResult, Measurer, Paragraph, type ParagraphContent, Parser, Reflector, type Render2dOptions, Renderer2D, Text, type TextAlign, type TextContent, type TextDecoration, type TextDeformation, type TextDrawStyle, type TextEffect, type TextLayoutStyle, type TextOptions, type TextOrientation, type TextRenderOptions, type TextStyle, type TextTransform, type TextWrap, type VerticalAlign, type WritingMode, defaultTextStyles, drawPath, filterEmpty, getPointPosition, getRotationPoint, getScalePoint, getSkewPoint, parseColor, uploadColor };