modern-text 0.2.28 → 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 +49 -117
- package/dist/index.d.cts +3 -27
- package/dist/index.d.mts +3 -27
- package/dist/index.d.ts +3 -27
- package/dist/index.js +2 -2
- package/dist/index.mjs +50 -114
- package/package.json +2 -2
package/dist/index.mjs
CHANGED
|
@@ -75,68 +75,6 @@ function drawPath(options) {
|
|
|
75
75
|
ctx.restore();
|
|
76
76
|
}
|
|
77
77
|
|
|
78
|
-
function filterEmpty(val) {
|
|
79
|
-
if (!val)
|
|
80
|
-
return val;
|
|
81
|
-
const res = {};
|
|
82
|
-
for (const key in val) {
|
|
83
|
-
if (val[key] !== "" && val[key] !== void 0) {
|
|
84
|
-
res[key] = val[key];
|
|
85
|
-
}
|
|
86
|
-
}
|
|
87
|
-
return res;
|
|
88
|
-
}
|
|
89
|
-
function getRotationPoint(point, rotation) {
|
|
90
|
-
const { x, y } = point;
|
|
91
|
-
const sin = Math.sin(rotation);
|
|
92
|
-
const cos = Math.cos(rotation);
|
|
93
|
-
return {
|
|
94
|
-
x: x * cos - y * sin,
|
|
95
|
-
y: x * sin + y * cos
|
|
96
|
-
};
|
|
97
|
-
}
|
|
98
|
-
function getSkewPoint(point, startPoint, skewX, skewY) {
|
|
99
|
-
const dx = point.x - startPoint.x;
|
|
100
|
-
const dy = point.y - startPoint.y;
|
|
101
|
-
return {
|
|
102
|
-
x: startPoint.x + (dx + Math.tan(skewX) * dy),
|
|
103
|
-
y: startPoint.y + (dy + Math.tan(skewY) * dx)
|
|
104
|
-
};
|
|
105
|
-
}
|
|
106
|
-
function getScalePoint(point, startPoint, scaleX, scaleY) {
|
|
107
|
-
const x = scaleX < 0 ? startPoint.x - point.x + startPoint.x : point.x;
|
|
108
|
-
const y = scaleY < 0 ? startPoint.y - point.y + startPoint.y : point.y;
|
|
109
|
-
return {
|
|
110
|
-
x: x * Math.abs(scaleX),
|
|
111
|
-
y: y * Math.abs(scaleY)
|
|
112
|
-
};
|
|
113
|
-
}
|
|
114
|
-
function getPointPosition(point, startPoint, rotation = 0, skewX = 0, skewY = 0, scaleX = 1, scaleY = 1) {
|
|
115
|
-
let points = Array.isArray(point) ? point : [point];
|
|
116
|
-
const _rotation = -rotation / 180 * Math.PI;
|
|
117
|
-
const { x, y } = startPoint;
|
|
118
|
-
if (scaleX !== 1 || scaleY !== 1) {
|
|
119
|
-
points = points.map((point2) => {
|
|
120
|
-
return getScalePoint(point2, startPoint, scaleX, scaleY);
|
|
121
|
-
});
|
|
122
|
-
}
|
|
123
|
-
if (skewX || skewY) {
|
|
124
|
-
points = points.map((point2) => {
|
|
125
|
-
return getSkewPoint(point2, startPoint, skewX, skewY);
|
|
126
|
-
});
|
|
127
|
-
}
|
|
128
|
-
points = points.map((d) => {
|
|
129
|
-
const h = d.x - x;
|
|
130
|
-
const f = -(d.y - y);
|
|
131
|
-
d = getRotationPoint({ x: h, y: f }, _rotation);
|
|
132
|
-
return {
|
|
133
|
-
x: x + d.x,
|
|
134
|
-
y: y - d.y
|
|
135
|
-
};
|
|
136
|
-
});
|
|
137
|
-
return points[0];
|
|
138
|
-
}
|
|
139
|
-
|
|
140
78
|
var __defProp$4 = Object.defineProperty;
|
|
141
79
|
var __defNormalProp$4 = (obj, key, value) => key in obj ? __defProp$4(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
142
80
|
var __publicField$4 = (obj, key, value) => {
|
|
@@ -156,6 +94,19 @@ const set2 = /* @__PURE__ */ new Set([
|
|
|
156
94
|
"\u2019",
|
|
157
95
|
"\u02DC"
|
|
158
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
|
+
};
|
|
159
110
|
class Character {
|
|
160
111
|
constructor(content, index, parent) {
|
|
161
112
|
this.content = content;
|
|
@@ -245,7 +196,7 @@ class Character {
|
|
|
245
196
|
let x = left;
|
|
246
197
|
let y = top + baseline;
|
|
247
198
|
let glyphIndex;
|
|
248
|
-
|
|
199
|
+
const path = new Path2D();
|
|
249
200
|
if (isVertical) {
|
|
250
201
|
x += (glyphHeight - glyphWidth) / 2;
|
|
251
202
|
if (Math.abs(textWidth - textHeight) > 0.1) {
|
|
@@ -254,27 +205,31 @@ class Character {
|
|
|
254
205
|
glyphIndex = void 0;
|
|
255
206
|
}
|
|
256
207
|
if (isVertical && !set1.has(content) && (content.codePointAt(0) <= 256 || set2.has(content))) {
|
|
257
|
-
|
|
208
|
+
path.addCommands(
|
|
209
|
+
font.getPathCommands(content, x, top + baseline - (glyphHeight - glyphWidth) / 2, fontSize) ?? []
|
|
210
|
+
);
|
|
258
211
|
const point = {
|
|
259
212
|
y: top - (glyphHeight - glyphWidth) / 2 + glyphHeight / 2,
|
|
260
213
|
x: x + glyphWidth / 2
|
|
261
214
|
};
|
|
262
215
|
if (fontStyle === "italic") {
|
|
263
|
-
|
|
264
|
-
|
|
216
|
+
this._italic(
|
|
217
|
+
path,
|
|
265
218
|
isVertical ? {
|
|
266
219
|
x: point.x,
|
|
267
220
|
y: top - (glyphHeight - glyphWidth) / 2 + baseline
|
|
268
221
|
} : void 0
|
|
269
222
|
);
|
|
270
223
|
}
|
|
271
|
-
|
|
224
|
+
path.rotate(90, point);
|
|
272
225
|
} else {
|
|
273
226
|
if (glyphIndex !== void 0) {
|
|
274
|
-
|
|
227
|
+
path.addCommands(
|
|
228
|
+
font.glyphs.get(glyphIndex).getPathCommands(x, y, fontSize)
|
|
229
|
+
);
|
|
275
230
|
if (fontStyle === "italic") {
|
|
276
|
-
|
|
277
|
-
|
|
231
|
+
this._italic(
|
|
232
|
+
path,
|
|
278
233
|
isVertical ? {
|
|
279
234
|
x: x + glyphWidth / 2,
|
|
280
235
|
y: top + typoAscender / (usWinAscent + Math.abs(usWinDescent)) * glyphHeight
|
|
@@ -282,17 +237,19 @@ class Character {
|
|
|
282
237
|
);
|
|
283
238
|
}
|
|
284
239
|
} else {
|
|
285
|
-
|
|
240
|
+
path.addCommands(
|
|
241
|
+
font.getPathCommands(content, x, y, fontSize) ?? []
|
|
242
|
+
);
|
|
286
243
|
if (fontStyle === "italic") {
|
|
287
|
-
|
|
288
|
-
|
|
244
|
+
this._italic(
|
|
245
|
+
path,
|
|
289
246
|
isVertical ? { x: x + glyphHeight / 2, y } : void 0
|
|
290
247
|
);
|
|
291
248
|
}
|
|
292
249
|
}
|
|
293
250
|
}
|
|
294
|
-
|
|
295
|
-
|
|
251
|
+
path.addCommands(this._decoration());
|
|
252
|
+
path.bold(fontWeightMap[computedStyle.fontWeight ?? 400] * fontSize * 0.05);
|
|
296
253
|
path.style = {
|
|
297
254
|
fill: computedStyle.color,
|
|
298
255
|
stroke: computedStyle.textStrokeWidth ? computedStyle.textStrokeColor : "none",
|
|
@@ -350,43 +307,10 @@ class Character {
|
|
|
350
307
|
];
|
|
351
308
|
}
|
|
352
309
|
}
|
|
353
|
-
_italic(
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
y: top + baseline,
|
|
358
|
-
x: left + glyphWidth / 2
|
|
359
|
-
};
|
|
360
|
-
return this._transform(commands, (x, y) => {
|
|
361
|
-
const p = getSkewPoint({ x, y }, _startPoint, -0.24, 0);
|
|
362
|
-
return [p.x, p.y];
|
|
363
|
-
});
|
|
364
|
-
}
|
|
365
|
-
_rotation90(commands, point) {
|
|
366
|
-
return this._transform(commands, (x, y) => {
|
|
367
|
-
const p = getPointPosition({ x, y }, point, 90);
|
|
368
|
-
return [p.x, p.y];
|
|
369
|
-
});
|
|
370
|
-
}
|
|
371
|
-
_transform(commands, cb) {
|
|
372
|
-
return commands.map((rawCmd) => {
|
|
373
|
-
const cmd = { ...rawCmd };
|
|
374
|
-
switch (cmd.type) {
|
|
375
|
-
case "L":
|
|
376
|
-
case "M":
|
|
377
|
-
[cmd.x, cmd.y] = cb(cmd.x, cmd.y);
|
|
378
|
-
break;
|
|
379
|
-
case "Q":
|
|
380
|
-
[cmd.x1, cmd.y1] = cb(cmd.x1, cmd.y1);
|
|
381
|
-
[cmd.x, cmd.y] = cb(cmd.x, cmd.y);
|
|
382
|
-
break;
|
|
383
|
-
case "C":
|
|
384
|
-
[cmd.x1, cmd.y1] = cb(cmd.x1, cmd.y1);
|
|
385
|
-
[cmd.x2, cmd.y2] = cb(cmd.x2, cmd.y2);
|
|
386
|
-
[cmd.x, cmd.y] = cb(cmd.x, cmd.y);
|
|
387
|
-
break;
|
|
388
|
-
}
|
|
389
|
-
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
|
|
390
314
|
});
|
|
391
315
|
}
|
|
392
316
|
getGlyphMinMax(min, max, withStyle) {
|
|
@@ -417,6 +341,18 @@ class Character {
|
|
|
417
341
|
}
|
|
418
342
|
}
|
|
419
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
|
+
|
|
420
356
|
var __defProp$3 = Object.defineProperty;
|
|
421
357
|
var __defNormalProp$3 = (obj, key, value) => key in obj ? __defProp$3(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
422
358
|
var __publicField$3 = (obj, key, value) => {
|
|
@@ -664,7 +600,7 @@ const _Highlighter = class _Highlighter extends Feature {
|
|
|
664
600
|
for (let i = 0; i < total; i++) {
|
|
665
601
|
const _transform = transform.clone().translate(i * unitWidth, 0);
|
|
666
602
|
paths.forEach((original) => {
|
|
667
|
-
const path = original.clone().
|
|
603
|
+
const path = original.clone().matrix(_transform);
|
|
668
604
|
if (path.style.strokeWidth) {
|
|
669
605
|
path.style.strokeWidth *= styleScale * strokeWidth;
|
|
670
606
|
}
|
|
@@ -1160,4 +1096,4 @@ class Text {
|
|
|
1160
1096
|
}
|
|
1161
1097
|
}
|
|
1162
1098
|
|
|
1163
|
-
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",
|