modern-text 0.2.29 → 0.2.30
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 +51 -131
- package/dist/index.d.cts +3 -28
- package/dist/index.d.mts +3 -28
- package/dist/index.d.ts +3 -28
- package/dist/index.js +2 -2
- package/dist/index.mjs +52 -128
- package/package.json +2 -2
package/dist/index.mjs
CHANGED
|
@@ -52,7 +52,7 @@ function parseCssLinearGradient(css, x, y, width, height) {
|
|
|
52
52
|
}
|
|
53
53
|
|
|
54
54
|
function drawPath(options) {
|
|
55
|
-
const { ctx, path, fontSize,
|
|
55
|
+
const { ctx, path, fontSize, clipRect } = options;
|
|
56
56
|
ctx.save();
|
|
57
57
|
ctx.beginPath();
|
|
58
58
|
const pathStyle = path.style;
|
|
@@ -71,83 +71,10 @@ function drawPath(options) {
|
|
|
71
71
|
ctx.clip();
|
|
72
72
|
ctx.beginPath();
|
|
73
73
|
}
|
|
74
|
-
|
|
75
|
-
for (let offsetX = -boldness; offsetX <= boldness; offsetX += boldness / 2) {
|
|
76
|
-
for (let offsetY = -boldness; offsetY <= boldness; offsetY += boldness / 2) {
|
|
77
|
-
ctx.save();
|
|
78
|
-
ctx.translate(Math.round(offsetX), Math.round(offsetY));
|
|
79
|
-
path.drawTo(ctx, style);
|
|
80
|
-
ctx.restore();
|
|
81
|
-
}
|
|
82
|
-
}
|
|
83
|
-
} else {
|
|
84
|
-
path.drawTo(ctx, style);
|
|
85
|
-
}
|
|
74
|
+
path.drawTo(ctx, style);
|
|
86
75
|
ctx.restore();
|
|
87
76
|
}
|
|
88
77
|
|
|
89
|
-
function filterEmpty(val) {
|
|
90
|
-
if (!val)
|
|
91
|
-
return val;
|
|
92
|
-
const res = {};
|
|
93
|
-
for (const key in val) {
|
|
94
|
-
if (val[key] !== "" && val[key] !== void 0) {
|
|
95
|
-
res[key] = val[key];
|
|
96
|
-
}
|
|
97
|
-
}
|
|
98
|
-
return res;
|
|
99
|
-
}
|
|
100
|
-
function getRotationPoint(point, rotation) {
|
|
101
|
-
const { x, y } = point;
|
|
102
|
-
const sin = Math.sin(rotation);
|
|
103
|
-
const cos = Math.cos(rotation);
|
|
104
|
-
return {
|
|
105
|
-
x: x * cos - y * sin,
|
|
106
|
-
y: x * sin + y * cos
|
|
107
|
-
};
|
|
108
|
-
}
|
|
109
|
-
function getSkewPoint(point, startPoint, skewX, skewY) {
|
|
110
|
-
const dx = point.x - startPoint.x;
|
|
111
|
-
const dy = point.y - startPoint.y;
|
|
112
|
-
return {
|
|
113
|
-
x: startPoint.x + (dx + Math.tan(skewX) * dy),
|
|
114
|
-
y: startPoint.y + (dy + Math.tan(skewY) * dx)
|
|
115
|
-
};
|
|
116
|
-
}
|
|
117
|
-
function getScalePoint(point, startPoint, scaleX, scaleY) {
|
|
118
|
-
const x = scaleX < 0 ? startPoint.x - point.x + startPoint.x : point.x;
|
|
119
|
-
const y = scaleY < 0 ? startPoint.y - point.y + startPoint.y : point.y;
|
|
120
|
-
return {
|
|
121
|
-
x: x * Math.abs(scaleX),
|
|
122
|
-
y: y * Math.abs(scaleY)
|
|
123
|
-
};
|
|
124
|
-
}
|
|
125
|
-
function getPointPosition(point, startPoint, rotation = 0, skewX = 0, skewY = 0, scaleX = 1, scaleY = 1) {
|
|
126
|
-
let points = Array.isArray(point) ? point : [point];
|
|
127
|
-
const _rotation = -rotation / 180 * Math.PI;
|
|
128
|
-
const { x, y } = startPoint;
|
|
129
|
-
if (scaleX !== 1 || scaleY !== 1) {
|
|
130
|
-
points = points.map((point2) => {
|
|
131
|
-
return getScalePoint(point2, startPoint, scaleX, scaleY);
|
|
132
|
-
});
|
|
133
|
-
}
|
|
134
|
-
if (skewX || skewY) {
|
|
135
|
-
points = points.map((point2) => {
|
|
136
|
-
return getSkewPoint(point2, startPoint, skewX, skewY);
|
|
137
|
-
});
|
|
138
|
-
}
|
|
139
|
-
points = points.map((d) => {
|
|
140
|
-
const h = d.x - x;
|
|
141
|
-
const f = -(d.y - y);
|
|
142
|
-
d = getRotationPoint({ x: h, y: f }, _rotation);
|
|
143
|
-
return {
|
|
144
|
-
x: x + d.x,
|
|
145
|
-
y: y - d.y
|
|
146
|
-
};
|
|
147
|
-
});
|
|
148
|
-
return points[0];
|
|
149
|
-
}
|
|
150
|
-
|
|
151
78
|
var __defProp$4 = Object.defineProperty;
|
|
152
79
|
var __defNormalProp$4 = (obj, key, value) => key in obj ? __defProp$4(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
153
80
|
var __publicField$4 = (obj, key, value) => {
|
|
@@ -167,6 +94,19 @@ const set2 = /* @__PURE__ */ new Set([
|
|
|
167
94
|
"\u2019",
|
|
168
95
|
"\u02DC"
|
|
169
96
|
]);
|
|
97
|
+
const fontWeightMap = {
|
|
98
|
+
100: -0.2,
|
|
99
|
+
200: -0.1,
|
|
100
|
+
300: 0,
|
|
101
|
+
400: 0,
|
|
102
|
+
normal: 0,
|
|
103
|
+
500: 0.1,
|
|
104
|
+
600: 0.2,
|
|
105
|
+
700: 0.3,
|
|
106
|
+
bold: 0.3,
|
|
107
|
+
800: 0.4,
|
|
108
|
+
900: 0.5
|
|
109
|
+
};
|
|
170
110
|
class Character {
|
|
171
111
|
constructor(content, index, parent) {
|
|
172
112
|
this.content = content;
|
|
@@ -256,7 +196,7 @@ class Character {
|
|
|
256
196
|
let x = left;
|
|
257
197
|
let y = top + baseline;
|
|
258
198
|
let glyphIndex;
|
|
259
|
-
|
|
199
|
+
const path = new Path2D();
|
|
260
200
|
if (isVertical) {
|
|
261
201
|
x += (glyphHeight - glyphWidth) / 2;
|
|
262
202
|
if (Math.abs(textWidth - textHeight) > 0.1) {
|
|
@@ -265,27 +205,31 @@ class Character {
|
|
|
265
205
|
glyphIndex = void 0;
|
|
266
206
|
}
|
|
267
207
|
if (isVertical && !set1.has(content) && (content.codePointAt(0) <= 256 || set2.has(content))) {
|
|
268
|
-
|
|
208
|
+
path.addCommands(
|
|
209
|
+
font.getPathCommands(content, x, top + baseline - (glyphHeight - glyphWidth) / 2, fontSize) ?? []
|
|
210
|
+
);
|
|
269
211
|
const point = {
|
|
270
212
|
y: top - (glyphHeight - glyphWidth) / 2 + glyphHeight / 2,
|
|
271
213
|
x: x + glyphWidth / 2
|
|
272
214
|
};
|
|
273
215
|
if (fontStyle === "italic") {
|
|
274
|
-
|
|
275
|
-
|
|
216
|
+
this._italic(
|
|
217
|
+
path,
|
|
276
218
|
isVertical ? {
|
|
277
219
|
x: point.x,
|
|
278
220
|
y: top - (glyphHeight - glyphWidth) / 2 + baseline
|
|
279
221
|
} : void 0
|
|
280
222
|
);
|
|
281
223
|
}
|
|
282
|
-
|
|
224
|
+
path.rotate(90, point);
|
|
283
225
|
} else {
|
|
284
226
|
if (glyphIndex !== void 0) {
|
|
285
|
-
|
|
227
|
+
path.addCommands(
|
|
228
|
+
font.glyphs.get(glyphIndex).getPathCommands(x, y, fontSize)
|
|
229
|
+
);
|
|
286
230
|
if (fontStyle === "italic") {
|
|
287
|
-
|
|
288
|
-
|
|
231
|
+
this._italic(
|
|
232
|
+
path,
|
|
289
233
|
isVertical ? {
|
|
290
234
|
x: x + glyphWidth / 2,
|
|
291
235
|
y: top + typoAscender / (usWinAscent + Math.abs(usWinDescent)) * glyphHeight
|
|
@@ -293,17 +237,19 @@ class Character {
|
|
|
293
237
|
);
|
|
294
238
|
}
|
|
295
239
|
} else {
|
|
296
|
-
|
|
240
|
+
path.addCommands(
|
|
241
|
+
font.getPathCommands(content, x, y, fontSize) ?? []
|
|
242
|
+
);
|
|
297
243
|
if (fontStyle === "italic") {
|
|
298
|
-
|
|
299
|
-
|
|
244
|
+
this._italic(
|
|
245
|
+
path,
|
|
300
246
|
isVertical ? { x: x + glyphHeight / 2, y } : void 0
|
|
301
247
|
);
|
|
302
248
|
}
|
|
303
249
|
}
|
|
304
250
|
}
|
|
305
|
-
|
|
306
|
-
|
|
251
|
+
path.addCommands(this._decoration());
|
|
252
|
+
path.bold(fontWeightMap[computedStyle.fontWeight ?? 400] * fontSize * 0.05);
|
|
307
253
|
path.style = {
|
|
308
254
|
fill: computedStyle.color,
|
|
309
255
|
stroke: computedStyle.textStrokeWidth ? computedStyle.textStrokeColor : "none",
|
|
@@ -361,43 +307,10 @@ class Character {
|
|
|
361
307
|
];
|
|
362
308
|
}
|
|
363
309
|
}
|
|
364
|
-
_italic(
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
y: top + baseline,
|
|
369
|
-
x: left + glyphWidth / 2
|
|
370
|
-
};
|
|
371
|
-
return this._transform(commands, (x, y) => {
|
|
372
|
-
const p = getSkewPoint({ x, y }, _startPoint, -0.24, 0);
|
|
373
|
-
return [p.x, p.y];
|
|
374
|
-
});
|
|
375
|
-
}
|
|
376
|
-
_rotation90(commands, point) {
|
|
377
|
-
return this._transform(commands, (x, y) => {
|
|
378
|
-
const p = getPointPosition({ x, y }, point, 90);
|
|
379
|
-
return [p.x, p.y];
|
|
380
|
-
});
|
|
381
|
-
}
|
|
382
|
-
_transform(commands, cb) {
|
|
383
|
-
return commands.map((rawCmd) => {
|
|
384
|
-
const cmd = { ...rawCmd };
|
|
385
|
-
switch (cmd.type) {
|
|
386
|
-
case "L":
|
|
387
|
-
case "M":
|
|
388
|
-
[cmd.x, cmd.y] = cb(cmd.x, cmd.y);
|
|
389
|
-
break;
|
|
390
|
-
case "Q":
|
|
391
|
-
[cmd.x1, cmd.y1] = cb(cmd.x1, cmd.y1);
|
|
392
|
-
[cmd.x, cmd.y] = cb(cmd.x, cmd.y);
|
|
393
|
-
break;
|
|
394
|
-
case "C":
|
|
395
|
-
[cmd.x1, cmd.y1] = cb(cmd.x1, cmd.y1);
|
|
396
|
-
[cmd.x2, cmd.y2] = cb(cmd.x2, cmd.y2);
|
|
397
|
-
[cmd.x, cmd.y] = cb(cmd.x, cmd.y);
|
|
398
|
-
break;
|
|
399
|
-
}
|
|
400
|
-
return cmd;
|
|
310
|
+
_italic(path, startPoint) {
|
|
311
|
+
path.skew(-0.24, 0, startPoint || {
|
|
312
|
+
y: this.boundingBox.top + this.baseline,
|
|
313
|
+
x: this.boundingBox.left + this.glyphWidth / 2
|
|
401
314
|
});
|
|
402
315
|
}
|
|
403
316
|
getGlyphMinMax(min, max, withStyle) {
|
|
@@ -423,12 +336,23 @@ class Character {
|
|
|
423
336
|
path: this.path,
|
|
424
337
|
fontSize: this.computedStyle.fontSize,
|
|
425
338
|
color: this.computedStyle.color,
|
|
426
|
-
boldness: this.computedStyle.fontWeight === "bold" ? 2 : 0,
|
|
427
339
|
...config
|
|
428
340
|
});
|
|
429
341
|
}
|
|
430
342
|
}
|
|
431
343
|
|
|
344
|
+
function filterEmpty(val) {
|
|
345
|
+
if (!val)
|
|
346
|
+
return val;
|
|
347
|
+
const res = {};
|
|
348
|
+
for (const key in val) {
|
|
349
|
+
if (val[key] !== "" && val[key] !== void 0) {
|
|
350
|
+
res[key] = val[key];
|
|
351
|
+
}
|
|
352
|
+
}
|
|
353
|
+
return res;
|
|
354
|
+
}
|
|
355
|
+
|
|
432
356
|
var __defProp$3 = Object.defineProperty;
|
|
433
357
|
var __defNormalProp$3 = (obj, key, value) => key in obj ? __defProp$3(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
434
358
|
var __publicField$3 = (obj, key, value) => {
|
|
@@ -676,7 +600,7 @@ const _Highlighter = class _Highlighter extends Feature {
|
|
|
676
600
|
for (let i = 0; i < total; i++) {
|
|
677
601
|
const _transform = transform.clone().translate(i * unitWidth, 0);
|
|
678
602
|
paths.forEach((original) => {
|
|
679
|
-
const path = original.clone().
|
|
603
|
+
const path = original.clone().matrix(_transform);
|
|
680
604
|
if (path.style.strokeWidth) {
|
|
681
605
|
path.style.strokeWidth *= styleScale * strokeWidth;
|
|
682
606
|
}
|
|
@@ -1172,4 +1096,4 @@ class Text {
|
|
|
1172
1096
|
}
|
|
1173
1097
|
}
|
|
1174
1098
|
|
|
1175
|
-
export { Character, Deformer, Effector, Fragment, Highlighter, Measurer, Paragraph, Parser, Reflector, Renderer2D, Text, defaultHighlightRefer, defaultTextStyles, drawPath, filterEmpty,
|
|
1099
|
+
export { Character, Deformer, Effector, Fragment, Highlighter, Measurer, Paragraph, Parser, Reflector, Renderer2D, Text, defaultHighlightRefer, defaultTextStyles, drawPath, filterEmpty, 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.30",
|
|
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.2.1",
|
|
61
|
-
"modern-path2d": "^0.
|
|
61
|
+
"modern-path2d": "^0.2.0"
|
|
62
62
|
},
|
|
63
63
|
"devDependencies": {
|
|
64
64
|
"@antfu/eslint-config": "^3.7.3",
|