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 CHANGED
@@ -76,68 +76,6 @@ function drawPath(options) {
76
76
  ctx.restore();
77
77
  }
78
78
 
79
- function filterEmpty(val) {
80
- if (!val)
81
- return val;
82
- const res = {};
83
- for (const key in val) {
84
- if (val[key] !== "" && val[key] !== void 0) {
85
- res[key] = val[key];
86
- }
87
- }
88
- return res;
89
- }
90
- function getRotationPoint(point, rotation) {
91
- const { x, y } = point;
92
- const sin = Math.sin(rotation);
93
- const cos = Math.cos(rotation);
94
- return {
95
- x: x * cos - y * sin,
96
- y: x * sin + y * cos
97
- };
98
- }
99
- function getSkewPoint(point, startPoint, skewX, skewY) {
100
- const dx = point.x - startPoint.x;
101
- const dy = point.y - startPoint.y;
102
- return {
103
- x: startPoint.x + (dx + Math.tan(skewX) * dy),
104
- y: startPoint.y + (dy + Math.tan(skewY) * dx)
105
- };
106
- }
107
- function getScalePoint(point, startPoint, scaleX, scaleY) {
108
- const x = scaleX < 0 ? startPoint.x - point.x + startPoint.x : point.x;
109
- const y = scaleY < 0 ? startPoint.y - point.y + startPoint.y : point.y;
110
- return {
111
- x: x * Math.abs(scaleX),
112
- y: y * Math.abs(scaleY)
113
- };
114
- }
115
- function getPointPosition(point, startPoint, rotation = 0, skewX = 0, skewY = 0, scaleX = 1, scaleY = 1) {
116
- let points = Array.isArray(point) ? point : [point];
117
- const _rotation = -rotation / 180 * Math.PI;
118
- const { x, y } = startPoint;
119
- if (scaleX !== 1 || scaleY !== 1) {
120
- points = points.map((point2) => {
121
- return getScalePoint(point2, startPoint, scaleX, scaleY);
122
- });
123
- }
124
- if (skewX || skewY) {
125
- points = points.map((point2) => {
126
- return getSkewPoint(point2, startPoint, skewX, skewY);
127
- });
128
- }
129
- points = points.map((d) => {
130
- const h = d.x - x;
131
- const f = -(d.y - y);
132
- d = getRotationPoint({ x: h, y: f }, _rotation);
133
- return {
134
- x: x + d.x,
135
- y: y - d.y
136
- };
137
- });
138
- return points[0];
139
- }
140
-
141
79
  var __defProp$4 = Object.defineProperty;
142
80
  var __defNormalProp$4 = (obj, key, value) => key in obj ? __defProp$4(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
143
81
  var __publicField$4 = (obj, key, value) => {
@@ -157,6 +95,19 @@ const set2 = /* @__PURE__ */ new Set([
157
95
  "\u2019",
158
96
  "\u02DC"
159
97
  ]);
98
+ const fontWeightMap = {
99
+ 100: -0.2,
100
+ 200: -0.1,
101
+ 300: 0,
102
+ 400: 0,
103
+ normal: 0,
104
+ 500: 0.1,
105
+ 600: 0.2,
106
+ 700: 0.3,
107
+ bold: 0.3,
108
+ 800: 0.4,
109
+ 900: 0.5
110
+ };
160
111
  class Character {
161
112
  constructor(content, index, parent) {
162
113
  this.content = content;
@@ -246,7 +197,7 @@ class Character {
246
197
  let x = left;
247
198
  let y = top + baseline;
248
199
  let glyphIndex;
249
- let commands;
200
+ const path = new modernPath2d.Path2D();
250
201
  if (isVertical) {
251
202
  x += (glyphHeight - glyphWidth) / 2;
252
203
  if (Math.abs(textWidth - textHeight) > 0.1) {
@@ -255,27 +206,31 @@ class Character {
255
206
  glyphIndex = void 0;
256
207
  }
257
208
  if (isVertical && !set1.has(content) && (content.codePointAt(0) <= 256 || set2.has(content))) {
258
- commands = font.getPathCommands(content, x, top + baseline - (glyphHeight - glyphWidth) / 2, fontSize) ?? [];
209
+ path.addCommands(
210
+ font.getPathCommands(content, x, top + baseline - (glyphHeight - glyphWidth) / 2, fontSize) ?? []
211
+ );
259
212
  const point = {
260
213
  y: top - (glyphHeight - glyphWidth) / 2 + glyphHeight / 2,
261
214
  x: x + glyphWidth / 2
262
215
  };
263
216
  if (fontStyle === "italic") {
264
- commands = this._italic(
265
- commands,
217
+ this._italic(
218
+ path,
266
219
  isVertical ? {
267
220
  x: point.x,
268
221
  y: top - (glyphHeight - glyphWidth) / 2 + baseline
269
222
  } : void 0
270
223
  );
271
224
  }
272
- commands = this._rotation90(commands, point);
225
+ path.rotate(90, point);
273
226
  } else {
274
227
  if (glyphIndex !== void 0) {
275
- commands = font.glyphs.get(glyphIndex).getPathCommands(x, y, fontSize);
228
+ path.addCommands(
229
+ font.glyphs.get(glyphIndex).getPathCommands(x, y, fontSize)
230
+ );
276
231
  if (fontStyle === "italic") {
277
- commands = this._italic(
278
- commands,
232
+ this._italic(
233
+ path,
279
234
  isVertical ? {
280
235
  x: x + glyphWidth / 2,
281
236
  y: top + typoAscender / (usWinAscent + Math.abs(usWinDescent)) * glyphHeight
@@ -283,17 +238,19 @@ class Character {
283
238
  );
284
239
  }
285
240
  } else {
286
- commands = font.getPathCommands(content, x, y, fontSize) ?? [];
241
+ path.addCommands(
242
+ font.getPathCommands(content, x, y, fontSize) ?? []
243
+ );
287
244
  if (fontStyle === "italic") {
288
- commands = this._italic(
289
- commands,
245
+ this._italic(
246
+ path,
290
247
  isVertical ? { x: x + glyphHeight / 2, y } : void 0
291
248
  );
292
249
  }
293
250
  }
294
251
  }
295
- commands.push(...this._decoration());
296
- const path = new modernPath2d.Path2D(commands);
252
+ path.addCommands(this._decoration());
253
+ path.bold(fontWeightMap[computedStyle.fontWeight ?? 400] * fontSize * 0.05);
297
254
  path.style = {
298
255
  fill: computedStyle.color,
299
256
  stroke: computedStyle.textStrokeWidth ? computedStyle.textStrokeColor : "none",
@@ -351,43 +308,10 @@ class Character {
351
308
  ];
352
309
  }
353
310
  }
354
- _italic(commands, startPoint) {
355
- const { baseline, glyphWidth } = this;
356
- const { left, top } = this.boundingBox;
357
- const _startPoint = startPoint || {
358
- y: top + baseline,
359
- x: left + glyphWidth / 2
360
- };
361
- return this._transform(commands, (x, y) => {
362
- const p = getSkewPoint({ x, y }, _startPoint, -0.24, 0);
363
- return [p.x, p.y];
364
- });
365
- }
366
- _rotation90(commands, point) {
367
- return this._transform(commands, (x, y) => {
368
- const p = getPointPosition({ x, y }, point, 90);
369
- return [p.x, p.y];
370
- });
371
- }
372
- _transform(commands, cb) {
373
- return commands.map((rawCmd) => {
374
- const cmd = { ...rawCmd };
375
- switch (cmd.type) {
376
- case "L":
377
- case "M":
378
- [cmd.x, cmd.y] = cb(cmd.x, cmd.y);
379
- break;
380
- case "Q":
381
- [cmd.x1, cmd.y1] = cb(cmd.x1, cmd.y1);
382
- [cmd.x, cmd.y] = cb(cmd.x, cmd.y);
383
- break;
384
- case "C":
385
- [cmd.x1, cmd.y1] = cb(cmd.x1, cmd.y1);
386
- [cmd.x2, cmd.y2] = cb(cmd.x2, cmd.y2);
387
- [cmd.x, cmd.y] = cb(cmd.x, cmd.y);
388
- break;
389
- }
390
- return cmd;
311
+ _italic(path, startPoint) {
312
+ path.skew(-0.24, 0, startPoint || {
313
+ y: this.boundingBox.top + this.baseline,
314
+ x: this.boundingBox.left + this.glyphWidth / 2
391
315
  });
392
316
  }
393
317
  getGlyphMinMax(min, max, withStyle) {
@@ -418,6 +342,18 @@ class Character {
418
342
  }
419
343
  }
420
344
 
345
+ function filterEmpty(val) {
346
+ if (!val)
347
+ return val;
348
+ const res = {};
349
+ for (const key in val) {
350
+ if (val[key] !== "" && val[key] !== void 0) {
351
+ res[key] = val[key];
352
+ }
353
+ }
354
+ return res;
355
+ }
356
+
421
357
  var __defProp$3 = Object.defineProperty;
422
358
  var __defNormalProp$3 = (obj, key, value) => key in obj ? __defProp$3(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
423
359
  var __publicField$3 = (obj, key, value) => {
@@ -665,7 +601,7 @@ const _Highlighter = class _Highlighter extends Feature {
665
601
  for (let i = 0; i < total; i++) {
666
602
  const _transform = transform.clone().translate(i * unitWidth, 0);
667
603
  paths.forEach((original) => {
668
- const path = original.clone().transform(_transform);
604
+ const path = original.clone().matrix(_transform);
669
605
  if (path.style.strokeWidth) {
670
606
  path.style.strokeWidth *= styleScale * strokeWidth;
671
607
  }
@@ -1176,10 +1112,6 @@ exports.defaultHighlightRefer = defaultHighlightRefer;
1176
1112
  exports.defaultTextStyles = defaultTextStyles;
1177
1113
  exports.drawPath = drawPath;
1178
1114
  exports.filterEmpty = filterEmpty;
1179
- exports.getPointPosition = getPointPosition;
1180
- exports.getRotationPoint = getRotationPoint;
1181
- exports.getScalePoint = getScalePoint;
1182
- exports.getSkewPoint = getSkewPoint;
1183
1115
  exports.parseColor = parseColor;
1184
1116
  exports.uploadColor = uploadColor;
1185
1117
  Object.keys(modernFont).forEach(function (k) {
package/dist/index.d.cts CHANGED
@@ -4,7 +4,7 @@ export * from 'modern-font';
4
4
 
5
5
  type WritingMode = 'horizontal-tb' | 'vertical-lr' | 'vertical-rl';
6
6
  type TextOrientation = 'mixed' | 'upright' | 'sideways-right' | 'sideways';
7
- type FontWeight = 'normal' | 'bold' | 'lighter' | 'bolder' | 100 | 200 | 300 | 400 | 500 | 600 | 700 | 800 | 900;
7
+ type FontWeight = 'normal' | 'bold' | 100 | 200 | 300 | 400 | 500 | 600 | 700 | 800 | 900;
8
8
  type FontStyle = 'normal' | 'italic' | 'oblique' | `oblique ${string}`;
9
9
  type FontKerning = 'auto' | 'none' | 'normal';
10
10
  type TextWrap = 'wrap' | 'nowrap';
@@ -124,9 +124,7 @@ declare class Character {
124
124
  updatePath(): this;
125
125
  update(): this;
126
126
  protected _decoration(): GlyphPathCommand[];
127
- protected _italic(commands: GlyphPathCommand[], startPoint?: VectorLike): GlyphPathCommand[];
128
- protected _rotation90(commands: GlyphPathCommand[], point: VectorLike): GlyphPathCommand[];
129
- protected _transform(commands: GlyphPathCommand[], cb: (x: number, y: number) => number[]): GlyphPathCommand[];
127
+ protected _italic(path: Path2D, startPoint?: VectorLike): void;
130
128
  getGlyphMinMax(min?: Vector2, max?: Vector2, withStyle?: boolean): {
131
129
  min: Vector2;
132
130
  max: Vector2;
@@ -333,27 +331,5 @@ declare class Renderer2D extends Feature {
333
331
  }
334
332
 
335
333
  declare function filterEmpty(val: Record<string, any> | undefined): Record<string, any> | undefined;
336
- declare function getRotationPoint(point: any, rotation: number): {
337
- x: number;
338
- y: number;
339
- };
340
- declare function getSkewPoint(point: any, startPoint: any, skewX: number, skewY: number): {
341
- x: number;
342
- y: number;
343
- };
344
- declare function getScalePoint(point: any, startPoint: any, scaleX: number, scaleY: number): {
345
- x: number;
346
- y: number;
347
- };
348
- declare function getPointPosition(point: {
349
- x: number;
350
- y: number;
351
- }, startPoint: {
352
- x: number;
353
- y: number;
354
- }, rotation?: number, skewX?: number, skewY?: number, scaleX?: number, scaleY?: number): {
355
- x: number;
356
- y: number;
357
- };
358
334
 
359
- 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, defaultHighlightRefer, defaultTextStyles, drawPath, filterEmpty, getPointPosition, getRotationPoint, getScalePoint, getSkewPoint, parseColor, uploadColor };
335
+ 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, defaultHighlightRefer, defaultTextStyles, drawPath, filterEmpty, parseColor, uploadColor };
package/dist/index.d.mts CHANGED
@@ -4,7 +4,7 @@ export * from 'modern-font';
4
4
 
5
5
  type WritingMode = 'horizontal-tb' | 'vertical-lr' | 'vertical-rl';
6
6
  type TextOrientation = 'mixed' | 'upright' | 'sideways-right' | 'sideways';
7
- type FontWeight = 'normal' | 'bold' | 'lighter' | 'bolder' | 100 | 200 | 300 | 400 | 500 | 600 | 700 | 800 | 900;
7
+ type FontWeight = 'normal' | 'bold' | 100 | 200 | 300 | 400 | 500 | 600 | 700 | 800 | 900;
8
8
  type FontStyle = 'normal' | 'italic' | 'oblique' | `oblique ${string}`;
9
9
  type FontKerning = 'auto' | 'none' | 'normal';
10
10
  type TextWrap = 'wrap' | 'nowrap';
@@ -124,9 +124,7 @@ declare class Character {
124
124
  updatePath(): this;
125
125
  update(): this;
126
126
  protected _decoration(): GlyphPathCommand[];
127
- protected _italic(commands: GlyphPathCommand[], startPoint?: VectorLike): GlyphPathCommand[];
128
- protected _rotation90(commands: GlyphPathCommand[], point: VectorLike): GlyphPathCommand[];
129
- protected _transform(commands: GlyphPathCommand[], cb: (x: number, y: number) => number[]): GlyphPathCommand[];
127
+ protected _italic(path: Path2D, startPoint?: VectorLike): void;
130
128
  getGlyphMinMax(min?: Vector2, max?: Vector2, withStyle?: boolean): {
131
129
  min: Vector2;
132
130
  max: Vector2;
@@ -333,27 +331,5 @@ declare class Renderer2D extends Feature {
333
331
  }
334
332
 
335
333
  declare function filterEmpty(val: Record<string, any> | undefined): Record<string, any> | undefined;
336
- declare function getRotationPoint(point: any, rotation: number): {
337
- x: number;
338
- y: number;
339
- };
340
- declare function getSkewPoint(point: any, startPoint: any, skewX: number, skewY: number): {
341
- x: number;
342
- y: number;
343
- };
344
- declare function getScalePoint(point: any, startPoint: any, scaleX: number, scaleY: number): {
345
- x: number;
346
- y: number;
347
- };
348
- declare function getPointPosition(point: {
349
- x: number;
350
- y: number;
351
- }, startPoint: {
352
- x: number;
353
- y: number;
354
- }, rotation?: number, skewX?: number, skewY?: number, scaleX?: number, scaleY?: number): {
355
- x: number;
356
- y: number;
357
- };
358
334
 
359
- 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, defaultHighlightRefer, defaultTextStyles, drawPath, filterEmpty, getPointPosition, getRotationPoint, getScalePoint, getSkewPoint, parseColor, uploadColor };
335
+ 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, defaultHighlightRefer, defaultTextStyles, drawPath, filterEmpty, parseColor, uploadColor };
package/dist/index.d.ts CHANGED
@@ -4,7 +4,7 @@ export * from 'modern-font';
4
4
 
5
5
  type WritingMode = 'horizontal-tb' | 'vertical-lr' | 'vertical-rl';
6
6
  type TextOrientation = 'mixed' | 'upright' | 'sideways-right' | 'sideways';
7
- type FontWeight = 'normal' | 'bold' | 'lighter' | 'bolder' | 100 | 200 | 300 | 400 | 500 | 600 | 700 | 800 | 900;
7
+ type FontWeight = 'normal' | 'bold' | 100 | 200 | 300 | 400 | 500 | 600 | 700 | 800 | 900;
8
8
  type FontStyle = 'normal' | 'italic' | 'oblique' | `oblique ${string}`;
9
9
  type FontKerning = 'auto' | 'none' | 'normal';
10
10
  type TextWrap = 'wrap' | 'nowrap';
@@ -124,9 +124,7 @@ declare class Character {
124
124
  updatePath(): this;
125
125
  update(): this;
126
126
  protected _decoration(): GlyphPathCommand[];
127
- protected _italic(commands: GlyphPathCommand[], startPoint?: VectorLike): GlyphPathCommand[];
128
- protected _rotation90(commands: GlyphPathCommand[], point: VectorLike): GlyphPathCommand[];
129
- protected _transform(commands: GlyphPathCommand[], cb: (x: number, y: number) => number[]): GlyphPathCommand[];
127
+ protected _italic(path: Path2D, startPoint?: VectorLike): void;
130
128
  getGlyphMinMax(min?: Vector2, max?: Vector2, withStyle?: boolean): {
131
129
  min: Vector2;
132
130
  max: Vector2;
@@ -333,27 +331,5 @@ declare class Renderer2D extends Feature {
333
331
  }
334
332
 
335
333
  declare function filterEmpty(val: Record<string, any> | undefined): Record<string, any> | undefined;
336
- declare function getRotationPoint(point: any, rotation: number): {
337
- x: number;
338
- y: number;
339
- };
340
- declare function getSkewPoint(point: any, startPoint: any, skewX: number, skewY: number): {
341
- x: number;
342
- y: number;
343
- };
344
- declare function getScalePoint(point: any, startPoint: any, scaleX: number, scaleY: number): {
345
- x: number;
346
- y: number;
347
- };
348
- declare function getPointPosition(point: {
349
- x: number;
350
- y: number;
351
- }, startPoint: {
352
- x: number;
353
- y: number;
354
- }, rotation?: number, skewX?: number, skewY?: number, scaleX?: number, scaleY?: number): {
355
- x: number;
356
- y: number;
357
- };
358
334
 
359
- 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, defaultHighlightRefer, defaultTextStyles, drawPath, filterEmpty, getPointPosition, getRotationPoint, getScalePoint, getSkewPoint, parseColor, uploadColor };
335
+ 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, defaultHighlightRefer, defaultTextStyles, drawPath, filterEmpty, parseColor, uploadColor };