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 +55 -47
- package/dist/index.d.cts +12 -7
- package/dist/index.d.mts +12 -7
- package/dist/index.d.ts +12 -7
- package/dist/index.js +2 -2
- package/dist/index.mjs +55 -47
- package/package.json +3 -3
package/dist/index.mjs
CHANGED
|
@@ -51,28 +51,31 @@ function parseCssLinearGradient(css, x, y, width, height) {
|
|
|
51
51
|
};
|
|
52
52
|
}
|
|
53
53
|
|
|
54
|
-
function
|
|
55
|
-
const { ctx,
|
|
56
|
-
|
|
57
|
-
|
|
54
|
+
function drawPath(options) {
|
|
55
|
+
const { ctx, path, fontSize, clipRect } = options;
|
|
56
|
+
ctx.save();
|
|
57
|
+
ctx.beginPath();
|
|
58
|
+
const style = path.style;
|
|
59
|
+
path.style = {
|
|
60
|
+
...style,
|
|
61
|
+
fill: options.color ?? style.fill,
|
|
62
|
+
stroke: options.textStrokeColor ?? style.stroke,
|
|
63
|
+
strokeWidth: options.textStrokeWidth ? options.textStrokeWidth * fontSize : style.strokeWidth,
|
|
64
|
+
shadowOffsetX: (options.shadowOffsetX ?? 0) * fontSize,
|
|
65
|
+
shadowOffsetY: (options.shadowOffsetY ?? 0) * fontSize,
|
|
66
|
+
shadowBlur: (options.shadowBlur ?? 0) * fontSize,
|
|
67
|
+
shadowColor: options.shadowColor
|
|
68
|
+
};
|
|
69
|
+
const offsetX = (options.offsetX ?? 0) * fontSize;
|
|
70
|
+
const offsetY = (options.offsetY ?? 0) * fontSize;
|
|
71
|
+
ctx.translate(offsetX, offsetY);
|
|
72
|
+
if (clipRect) {
|
|
73
|
+
ctx.rect(clipRect.x, clipRect.y, clipRect.width, clipRect.height);
|
|
74
|
+
ctx.clip();
|
|
58
75
|
ctx.beginPath();
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
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
|
-
path.drawTo(ctx);
|
|
74
|
-
ctx.restore();
|
|
75
|
-
});
|
|
76
|
+
}
|
|
77
|
+
path.drawTo(ctx);
|
|
78
|
+
ctx.restore();
|
|
76
79
|
}
|
|
77
80
|
|
|
78
81
|
function filterEmpty(val) {
|
|
@@ -208,7 +211,6 @@ class Character {
|
|
|
208
211
|
this.centerDiviation = 0.5 * height - baseline;
|
|
209
212
|
this.glyphBox = isVertical ? new BoundingBox(left, top, glyphHeight, glyphWidth) : new BoundingBox(left, top, glyphWidth, glyphHeight);
|
|
210
213
|
this.centerPoint = this.glyphBox.getCenterPoint();
|
|
211
|
-
this.fontMinGlyphWidth = font.getAdvanceWidth("i", fontSize);
|
|
212
214
|
return this;
|
|
213
215
|
}
|
|
214
216
|
updatePath() {
|
|
@@ -262,7 +264,7 @@ class Character {
|
|
|
262
264
|
commands = this._rotation90(commands, point);
|
|
263
265
|
} else {
|
|
264
266
|
if (glyphIndex !== void 0) {
|
|
265
|
-
commands = font.
|
|
267
|
+
commands = font.glyphs.get(glyphIndex).getPathCommands(x, y, fontSize);
|
|
266
268
|
if (fontStyle === "italic") {
|
|
267
269
|
commands = this._italic(
|
|
268
270
|
commands,
|
|
@@ -369,6 +371,11 @@ class Character {
|
|
|
369
371
|
[cmd.x1, cmd.y1] = cb(cmd.x1, cmd.y1);
|
|
370
372
|
[cmd.x, cmd.y] = cb(cmd.x, cmd.y);
|
|
371
373
|
break;
|
|
374
|
+
case "C":
|
|
375
|
+
[cmd.x1, cmd.y1] = cb(cmd.x1, cmd.y1);
|
|
376
|
+
[cmd.x2, cmd.y2] = cb(cmd.x2, cmd.y2);
|
|
377
|
+
[cmd.x, cmd.y] = cb(cmd.x, cmd.y);
|
|
378
|
+
break;
|
|
372
379
|
}
|
|
373
380
|
return cmd;
|
|
374
381
|
});
|
|
@@ -380,9 +387,9 @@ class Character {
|
|
|
380
387
|
return this.path.getBoundingBox();
|
|
381
388
|
}
|
|
382
389
|
drawTo(ctx, config = {}) {
|
|
383
|
-
|
|
390
|
+
drawPath({
|
|
384
391
|
ctx,
|
|
385
|
-
|
|
392
|
+
path: this.path,
|
|
386
393
|
fontSize: this.computedStyle.fontSize,
|
|
387
394
|
color: this.computedStyle.color,
|
|
388
395
|
...config
|
|
@@ -523,7 +530,7 @@ class Highlighter extends Feature {
|
|
|
523
530
|
}
|
|
524
531
|
const min = Vector2.MAX;
|
|
525
532
|
const max = Vector2.MIN;
|
|
526
|
-
this.paths.forEach((
|
|
533
|
+
this.paths.forEach((v) => v.path.getMinMax(min, max));
|
|
527
534
|
return new BoundingBox(min.x, min.y, max.x - min.x, max.y - min.y);
|
|
528
535
|
}
|
|
529
536
|
highlight() {
|
|
@@ -549,7 +556,6 @@ class Highlighter extends Feature {
|
|
|
549
556
|
url: characters2[0].parent.highlight.url,
|
|
550
557
|
box: BoundingBox.from(...characters2.map((c) => c.boundingBox)),
|
|
551
558
|
baseline: Math.max(...characters2.map((c) => c.baseline)),
|
|
552
|
-
fontMinGlyphWidth: characters2[0].fontMinGlyphWidth,
|
|
553
559
|
fontSize: characters2[0].fontSize
|
|
554
560
|
};
|
|
555
561
|
}).map((group2) => this._parseGroup(group2)).flat();
|
|
@@ -567,23 +573,23 @@ class Highlighter extends Feature {
|
|
|
567
573
|
};
|
|
568
574
|
}
|
|
569
575
|
_parseGroup(group) {
|
|
570
|
-
const { url, box: groupBox, baseline, fontSize
|
|
576
|
+
const { url, box: groupBox, baseline, fontSize } = group;
|
|
571
577
|
const { box, viewBox, paths } = this._parseSvg(url);
|
|
578
|
+
const centerY = viewBox.top + viewBox.height / 2;
|
|
572
579
|
const result = [];
|
|
573
|
-
const type =
|
|
574
|
-
function transformPathStyle(path) {
|
|
575
|
-
const rate = fontSize * 0.03;
|
|
580
|
+
const type = centerY > box.top ? 0 : 1;
|
|
581
|
+
function transformPathStyle(path, scale) {
|
|
576
582
|
if (path.style.strokeWidth) {
|
|
577
|
-
path.style.strokeWidth *=
|
|
583
|
+
path.style.strokeWidth *= scale;
|
|
578
584
|
}
|
|
579
585
|
if (path.style.strokeMiterlimit) {
|
|
580
|
-
path.style.strokeMiterlimit *=
|
|
586
|
+
path.style.strokeMiterlimit *= scale;
|
|
581
587
|
}
|
|
582
588
|
if (path.style.strokeDashoffset) {
|
|
583
|
-
path.style.strokeDashoffset *=
|
|
589
|
+
path.style.strokeDashoffset *= scale;
|
|
584
590
|
}
|
|
585
591
|
if (path.style.strokeDasharray) {
|
|
586
|
-
path.style.strokeDasharray = path.style.strokeDasharray.map((v) => v *
|
|
592
|
+
path.style.strokeDasharray = path.style.strokeDasharray.map((v) => v * scale);
|
|
587
593
|
}
|
|
588
594
|
}
|
|
589
595
|
if (type === 0) {
|
|
@@ -596,16 +602,15 @@ class Highlighter extends Feature {
|
|
|
596
602
|
const m = new Matrix3().translate(-box.x, -box.y).scale(scaleX, scaleY).translate(offset.x, offset.y);
|
|
597
603
|
paths.forEach((original) => {
|
|
598
604
|
const path = original.clone().transform(m);
|
|
599
|
-
transformPathStyle(path);
|
|
600
|
-
result.push(path);
|
|
605
|
+
transformPathStyle(path, scaleX);
|
|
606
|
+
result.push({ path });
|
|
601
607
|
});
|
|
602
608
|
} else if (type === 1) {
|
|
603
|
-
const scale =
|
|
609
|
+
const scale = fontSize / box.width;
|
|
604
610
|
const width = box.width * scale;
|
|
605
611
|
const length = Math.ceil(groupBox.width / width);
|
|
606
|
-
const totalWidth = width * length;
|
|
607
612
|
const offset = {
|
|
608
|
-
x: groupBox.left
|
|
613
|
+
x: groupBox.left,
|
|
609
614
|
y: groupBox.top + baseline + fontSize * 0.1
|
|
610
615
|
};
|
|
611
616
|
const m = new Matrix3().translate(-box.x, -box.y).scale(scale, scale).translate(offset.x, offset.y);
|
|
@@ -613,18 +618,21 @@ class Highlighter extends Feature {
|
|
|
613
618
|
const _m = m.clone().translate(i * width, 0);
|
|
614
619
|
paths.forEach((original) => {
|
|
615
620
|
const path = original.clone().transform(_m);
|
|
616
|
-
transformPathStyle(path);
|
|
617
|
-
result.push(path);
|
|
621
|
+
transformPathStyle(path, scale);
|
|
622
|
+
result.push({ clipRect: groupBox, path });
|
|
618
623
|
});
|
|
619
624
|
}
|
|
620
625
|
}
|
|
621
626
|
return result;
|
|
622
627
|
}
|
|
623
628
|
draw({ ctx }) {
|
|
624
|
-
|
|
625
|
-
|
|
626
|
-
|
|
627
|
-
|
|
629
|
+
this.paths.forEach((v) => {
|
|
630
|
+
drawPath({
|
|
631
|
+
ctx,
|
|
632
|
+
path: v.path,
|
|
633
|
+
clipRect: v.clipRect,
|
|
634
|
+
fontSize: this._text.computedStyle.fontSize
|
|
635
|
+
});
|
|
628
636
|
});
|
|
629
637
|
return this;
|
|
630
638
|
}
|
|
@@ -1079,4 +1087,4 @@ class Text {
|
|
|
1079
1087
|
}
|
|
1080
1088
|
}
|
|
1081
1089
|
|
|
1082
|
-
export { Character, Deformer, Effector, Fragment, Highlighter, Measurer, Paragraph, Parser, Reflector, Renderer2D, Text, defaultTextStyles,
|
|
1090
|
+
export { Character, Deformer, Effector, Fragment, Highlighter, Measurer, Paragraph, Parser, Reflector, Renderer2D, Text, defaultTextStyles, drawPath, filterEmpty, getPointPosition, getRotationPoint, getScalePoint, getSkewPoint, parseColor, uploadColor };
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "modern-text",
|
|
3
3
|
"type": "module",
|
|
4
|
-
"version": "0.2.
|
|
4
|
+
"version": "0.2.15",
|
|
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,8 +57,8 @@
|
|
|
57
57
|
"prepare": "simple-git-hooks"
|
|
58
58
|
},
|
|
59
59
|
"dependencies": {
|
|
60
|
-
"modern-font": "^0.
|
|
61
|
-
"modern-path2d": "^0.1.
|
|
60
|
+
"modern-font": "^0.2.0",
|
|
61
|
+
"modern-path2d": "^0.1.16"
|
|
62
62
|
},
|
|
63
63
|
"devDependencies": {
|
|
64
64
|
"@antfu/eslint-config": "^3.7.3",
|