canvas-recorder-lib 0.8.0 → 0.8.3

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.
Files changed (100) hide show
  1. package/BREAKING-CHANGES.md +8 -8
  2. package/dev/canvas-recorder.css +6 -6
  3. package/dev/canvas-recorder.ts +62 -10
  4. package/dev/character-panel.ts +201 -0
  5. package/dev/delay-files.ts +3 -1
  6. package/dev/format-editor.ts +36 -0
  7. package/dev/multi-text-dialog.ts +31 -2
  8. package/dev/multi-text-panel.ts +6 -0
  9. package/dev/player-body.ts +3 -3
  10. package/dev/transcribe-core.ts +7 -4
  11. package/dev/transcript-align.ts +20 -17
  12. package/dist/dev/canvas-recorder.css +6 -6
  13. package/dist/dev/canvas-recorder.js +57 -10
  14. package/dist/dev/canvas-recorder.js.map +1 -1
  15. package/dist/dev/character-panel.d.ts +43 -0
  16. package/dist/dev/character-panel.d.ts.map +1 -0
  17. package/dist/dev/character-panel.js +133 -0
  18. package/dist/dev/character-panel.js.map +1 -0
  19. package/dist/dev/delay-files.d.ts.map +1 -1
  20. package/dist/dev/delay-files.js +3 -1
  21. package/dist/dev/delay-files.js.map +1 -1
  22. package/dist/dev/format-editor.d.ts +16 -0
  23. package/dist/dev/format-editor.d.ts.map +1 -1
  24. package/dist/dev/format-editor.js +31 -0
  25. package/dist/dev/format-editor.js.map +1 -1
  26. package/dist/dev/multi-text-dialog.d.ts +2 -0
  27. package/dist/dev/multi-text-dialog.d.ts.map +1 -1
  28. package/dist/dev/multi-text-dialog.js +26 -2
  29. package/dist/dev/multi-text-dialog.js.map +1 -1
  30. package/dist/dev/multi-text-panel.d.ts.map +1 -1
  31. package/dist/dev/multi-text-panel.js +6 -0
  32. package/dist/dev/multi-text-panel.js.map +1 -1
  33. package/dist/dev/player-body.d.ts +1 -1
  34. package/dist/dev/player-body.d.ts.map +1 -1
  35. package/dist/dev/player-body.js +3 -3
  36. package/dist/dev/transcribe-core.d.ts.map +1 -1
  37. package/dist/dev/transcribe-core.js +7 -4
  38. package/dist/dev/transcribe-core.js.map +1 -1
  39. package/dist/dev/transcript-align.d.ts +3 -3
  40. package/dist/dev/transcript-align.d.ts.map +1 -1
  41. package/dist/dev/transcript-align.js +16 -13
  42. package/dist/dev/transcript-align.js.map +1 -1
  43. package/dist/src/glib/font-choices.d.ts +61 -0
  44. package/dist/src/glib/font-choices.d.ts.map +1 -0
  45. package/dist/src/glib/font-choices.js +101 -0
  46. package/dist/src/glib/font-choices.js.map +1 -0
  47. package/dist/src/glib/letters-base.d.ts +97 -0
  48. package/dist/src/glib/letters-base.d.ts.map +1 -1
  49. package/dist/src/glib/letters-base.js +120 -18
  50. package/dist/src/glib/letters-base.js.map +1 -1
  51. package/dist/src/glib/line-font.d.ts +23 -3
  52. package/dist/src/glib/line-font.d.ts.map +1 -1
  53. package/dist/src/glib/line-font.js +40 -1
  54. package/dist/src/glib/line-font.js.map +1 -1
  55. package/dist/src/glib/path-shape.d.ts +7 -0
  56. package/dist/src/glib/path-shape.d.ts.map +1 -1
  57. package/dist/src/glib/path-shape.js +43 -221
  58. package/dist/src/glib/path-shape.js.map +1 -1
  59. package/dist/src/glib/svg-arc.d.ts +41 -0
  60. package/dist/src/glib/svg-arc.d.ts.map +1 -0
  61. package/dist/src/glib/svg-arc.js +114 -0
  62. package/dist/src/glib/svg-arc.js.map +1 -0
  63. package/dist/src/glib/svg-path-parser.d.ts +60 -0
  64. package/dist/src/glib/svg-path-parser.d.ts.map +1 -0
  65. package/dist/src/glib/svg-path-parser.js +258 -0
  66. package/dist/src/glib/svg-path-parser.js.map +1 -0
  67. package/dist/src/slide-components/fourier.d.ts.map +1 -1
  68. package/dist/src/slide-components/fourier.js +14 -1
  69. package/dist/src/slide-components/fourier.js.map +1 -1
  70. package/dist/src/slide-components/halftone-shadow.d.ts.map +1 -1
  71. package/dist/src/slide-components/halftone-shadow.js +12 -9
  72. package/dist/src/slide-components/halftone-shadow.js.map +1 -1
  73. package/dist/src/slide-components/multi-text.d.ts +24 -0
  74. package/dist/src/slide-components/multi-text.d.ts.map +1 -1
  75. package/dist/src/slide-components/multi-text.js +75 -5
  76. package/dist/src/slide-components/multi-text.js.map +1 -1
  77. package/dist/src/slide-components/registry.d.ts.map +1 -1
  78. package/dist/src/slide-components/registry.js +3 -2
  79. package/dist/src/slide-components/registry.js.map +1 -1
  80. package/dist/src/slide-components/show-error.d.ts.map +1 -1
  81. package/dist/src/slide-components/show-error.js +9 -2
  82. package/dist/src/slide-components/show-error.js.map +1 -1
  83. package/dist/src/slide-components/simple-text.d.ts +20 -0
  84. package/dist/src/slide-components/simple-text.d.ts.map +1 -1
  85. package/dist/src/slide-components/simple-text.js +58 -5
  86. package/dist/src/slide-components/simple-text.js.map +1 -1
  87. package/internals.md +402 -406
  88. package/package.json +1 -1
  89. package/src/glib/font-choices.ts +132 -0
  90. package/src/glib/letters-base.ts +212 -18
  91. package/src/glib/line-font.ts +56 -3
  92. package/src/glib/path-shape.ts +66 -245
  93. package/src/glib/svg-arc.ts +141 -0
  94. package/src/glib/svg-path-parser.ts +308 -0
  95. package/src/slide-components/fourier.ts +15 -3
  96. package/src/slide-components/halftone-shadow.ts +12 -9
  97. package/src/slide-components/multi-text.ts +100 -4
  98. package/src/slide-components/registry.ts +3 -2
  99. package/src/slide-components/show-error.ts +9 -2
  100. package/src/slide-components/simple-text.ts +76 -4
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "canvas-recorder-lib",
3
- "version": "0.8.0",
3
+ "version": "0.8.3",
4
4
  "description": "Preview, edit, annotate and record HTML canvas animations. The tooling from canvas-recorder, as a library.",
5
5
  "type": "module",
6
6
  "exports": {
@@ -0,0 +1,132 @@
1
+ import { Font, FontOptions } from "./letters-base.ts";
2
+ import { makeLineFont } from "./line-font.ts";
3
+
4
+ /**
5
+ * The fonts the text components offer, by the names the Visual Editor shows.
6
+ *
7
+ * The Text Format and Text components have a Font property with these choices.
8
+ * Line Font is the default.
9
+ * @module
10
+ */
11
+
12
+ /** Every font a text component can use, by the name the Visual Editor shows. */
13
+ export const FONT_NAMES = ["Line Font", "Futura L", "Cursive"] as const;
14
+
15
+ /** One of {@link FONT_NAMES}. */
16
+ export type FontName = (typeof FONT_NAMES)[number];
17
+
18
+ /**
19
+ * Make a font by its name.
20
+ *
21
+ * An unknown name, say from an old save file, gets the Line Font.
22
+ */
23
+ export function makeFontByName(name: FontName, options: FontOptions): Font {
24
+ switch (name) {
25
+ case "Futura L": {
26
+ return Font.futuraL(options);
27
+ }
28
+ case "Cursive": {
29
+ return Font.cursive(options);
30
+ }
31
+ default: {
32
+ return makeLineFont(options);
33
+ }
34
+ }
35
+ }
36
+
37
+ const characterSets = new Map<FontName, ReadonlySet<string>>();
38
+
39
+ /**
40
+ * Every character `name` can draw, except the space, which the layout handles
41
+ * itself.
42
+ */
43
+ export function fontCharacters(name: FontName): ReadonlySet<string> {
44
+ return characterSets.getOrInsertComputed(name, () => {
45
+ const result = new Set(makeFontByName(name, { size: 1 }).getAllLetters());
46
+ result.delete(" ");
47
+ return result;
48
+ });
49
+ }
50
+
51
+ let allCharacters: readonly string[] | undefined;
52
+
53
+ /**
54
+ * Every character any of {@link FONT_NAMES} can draw, in Unicode order.
55
+ * Except the space, which the layout handles itself.
56
+ */
57
+ export function allFontCharacters(): readonly string[] {
58
+ allCharacters ??= [
59
+ ...new Set(FONT_NAMES.flatMap((name) => [...fontCharacters(name)])),
60
+ ].sort((a, b) => a.codePointAt(0)! - b.codePointAt(0)!);
61
+ return allCharacters;
62
+ }
63
+
64
+ /**
65
+ * The characters in `text` that `name` can't draw, each once, in the order they first
66
+ * appear. The layout skips them, so they don't appear on the canvas.
67
+ *
68
+ * The space and "\n" aren't letters, so they're never missing.
69
+ */
70
+ export function missingCharacters(text: string, name: FontName): string[] {
71
+ const available = fontCharacters(name);
72
+ const result = new Set<string>();
73
+ for (const char of text) {
74
+ if (char != " " && char != "\n" && !available.has(char)) {
75
+ result.add(char);
76
+ }
77
+ }
78
+ return [...result];
79
+ }
80
+
81
+ /**
82
+ * Characters some text needs and its font doesn't have.
83
+ * See `MultiTextComponent.missingCharacters()`.
84
+ */
85
+ export type MissingCharacters = {
86
+ /** The Text Format's name, or undefined for a component with one font, like Text. */
87
+ readonly formatName?: string;
88
+ readonly fontName: FontName;
89
+ /** Each once, in the order they first appear. */
90
+ readonly characters: readonly string[];
91
+ };
92
+
93
+ /**
94
+ * Check `text` against `font` at every moment either could change.
95
+ *
96
+ * Both are step functions: each keyframe holds until the next. So checking at each
97
+ * keyframe's time covers the whole timeline.
98
+ * @param keyframeTimes Every keyframe time of every schedule `pairsAt` reads.
99
+ * @param pairsAt The text and its font, at one time. Several, for several formats.
100
+ * @returns One entry per font and format with missing characters.
101
+ */
102
+ export function checkCharacters(
103
+ keyframeTimes: Iterable<number>,
104
+ pairsAt: (
105
+ timeInMs: number,
106
+ ) => Iterable<{ text: string; fontName: FontName; formatName?: string }>,
107
+ ): MissingCharacters[] {
108
+ const found = new Map<
109
+ string,
110
+ { fontName: FontName; formatName?: string; characters: Set<string> }
111
+ >();
112
+ for (const timeInMs of new Set(keyframeTimes)) {
113
+ for (const { text, fontName, formatName } of pairsAt(timeInMs)) {
114
+ const missing = missingCharacters(text, fontName);
115
+ if (missing.length == 0) {
116
+ continue;
117
+ }
118
+ const key = JSON.stringify([formatName, fontName]);
119
+ const entry = found.getOrInsertComputed(key, () => ({
120
+ fontName,
121
+ formatName,
122
+ characters: new Set<string>(),
123
+ }));
124
+ missing.forEach((char) => entry.characters.add(char));
125
+ }
126
+ }
127
+ return [...found.values()].map(({ fontName, formatName, characters }) => ({
128
+ fontName,
129
+ formatName,
130
+ characters: [...characters],
131
+ }));
132
+ }
@@ -43,6 +43,50 @@ export type FontMetrics = {
43
43
  // baseLine is now frozen at 0!!!!!
44
44
  };
45
45
 
46
+ /**
47
+ * Everything about a font, all at once: its size, width, slant and stroke.
48
+ * For {@link Font.cursive}, {@link Font.futuraL} and `makeLineFont()`.
49
+ * Only `size` is required.
50
+ *
51
+ * Give `boldness` or `strokeWidth`, not both.
52
+ */
53
+ export type FontOptions = {
54
+ /** The height of a capital M, the font's `mHeight`. Width doesn't change it. */
55
+ readonly size: number;
56
+ /**
57
+ * How wide the letters are, compared to normal. 1, the default, is
58
+ * normal. 2 is twice as wide, 0.5 half as wide. Must be positive.
59
+ * The height stays `size`. See {@link Font.stretch}.
60
+ */
61
+ readonly width?: number;
62
+ /**
63
+ * The slant in degrees, like {@link Font.oblique}. 0, the default, is
64
+ * upright. It's the angle you see, whatever the width: the letters are
65
+ * stretched first, then slanted.
66
+ */
67
+ readonly obliqueness?: number;
68
+ } & (
69
+ | {
70
+ /**
71
+ * The stroke width, compared to the font's own recommendation
72
+ * ({@link Font.strokeWidth}). 1, the default, is normal; 2 is twice
73
+ * as thick. Each font recommends its own: the line font a tenth of
74
+ * its size, the Hershey fonts about half that.
75
+ *
76
+ * The recommendation follows the geometric mean of the font's height
77
+ * and width, `size × √width`. So 1 is a little bolder in a wide font
78
+ * and a little lighter in a narrow one. See {@link Font.stretch}.
79
+ */
80
+ readonly boldness?: number;
81
+ readonly strokeWidth?: never;
82
+ }
83
+ | {
84
+ /** The stroke width, in the same units as `size`. */
85
+ readonly strokeWidth: number;
86
+ readonly boldness?: never;
87
+ }
88
+ );
89
+
46
90
  export type DescriptionOfLetter = {
47
91
  readonly shape: PathShape;
48
92
  readonly advance: number;
@@ -180,6 +224,8 @@ export class Font {
180
224
  * This says how much to *change* the font, not the final value.
181
225
  * font.oblique(5) != font.oblique(5).oblique(5).oblique(5).
182
226
  * This differs from resize() which takes the desired resulting value, where this takes the change as an input.
227
+ *
228
+ * Call {@link stretch} first, if you use both. It would stretch the slant too.
183
229
  * @returns A new font. Or the same font if you request a 0° change.
184
230
  */
185
231
  oblique(degreesRight = 15): Font {
@@ -196,13 +242,7 @@ export class Font {
196
242
  [],
197
243
  );
198
244
  const matrix = new DOMMatrixReadOnly().skewX(-degreesRight);
199
- this.#letters.forEach((originalLetter, key) => {
200
- const newLetter: DescriptionOfLetter = {
201
- advance: originalLetter.advance,
202
- shape: originalLetter.shape.transform(matrix),
203
- };
204
- result.#letters.set(key, newLetter);
205
- });
245
+ this.#copyLettersTo(result, matrix, 1);
206
246
  return result;
207
247
  }
208
248
  resize(newSize: number): Font {
@@ -221,23 +261,177 @@ export class Font {
221
261
  );
222
262
  const matrix = new DOMMatrix();
223
263
  matrix.scaleSelf(ratio);
264
+ this.#copyLettersTo(result, matrix, ratio);
265
+ return result;
266
+ }
267
+ /**
268
+ * Make the letters wider or narrower, keeping their height.
269
+ *
270
+ * Only the shapes change, not the pen: stroke them with one line width, and
271
+ * the vertical lines are as thick as the horizontal ones.
272
+ *
273
+ * Everything horizontal scales with the letters: each letter's advance, the
274
+ * kerning and the space. So a line of text is exactly `widthRatio` times as
275
+ * long, and cursive letters still meet. `top`, `bottom` and `mHeight` don't
276
+ * change.
277
+ *
278
+ * The recommended {@link strokeWidth} changes by √`widthRatio`, the geometric
279
+ * mean of the change in width and the change in height (none). That's the
280
+ * rule resize() follows, where both change by the same amount. So a wide
281
+ * font is a little bolder and a narrow one a little lighter, as in a real type
282
+ * family, and narrow letters don't fill in.
283
+ *
284
+ * **Stretch first, then oblique.** Stretching a slanted font stretches its
285
+ * slant too: `font.oblique(15).stretch(2)` leans about 28°, and
286
+ * `font.stretch(2).oblique(15)` leans 15°.
287
+ *
288
+ * For the line font, `makeLineFont({ size, width })` does a little better
289
+ * than `makeLineFont(size).stretch(width)`. Some of its letters depend on the
290
+ * stroke width, like the K, whose lines meet exactly, and it builds those
291
+ * for the stretched size.
292
+ * @param widthRatio 2 for twice as wide, 0.5 for half as wide. Must be
293
+ * positive. Like oblique(), this is a change, not the final value:
294
+ * `font.stretch(2).stretch(2)` is 4 times as wide as `font`.
295
+ * @returns A new font. Or the same font if `widthRatio` is 1.
296
+ */
297
+ stretch(widthRatio: number): Font {
298
+ if (widthRatio == 1) {
299
+ return this;
300
+ }
301
+ if (!(widthRatio > 0 && isFinite(widthRatio))) {
302
+ throw new Error(`widthRatio must be positive, not ${widthRatio}.`);
303
+ }
304
+ const result = new Font(
305
+ this.top,
306
+ this.bottom,
307
+ widthRatio * this.spaceWidth,
308
+ Math.sqrt(widthRatio) * this.strokeWidth,
309
+ widthRatio * this.kerning,
310
+ this.mHeight,
311
+ [],
312
+ );
313
+ const matrix = new DOMMatrixReadOnly().scale(widthRatio, 1);
314
+ this.#copyLettersTo(result, matrix, widthRatio);
315
+ return result;
316
+ }
317
+ /**
318
+ * Copy every letter into a new font, transformed. For resize(), oblique()
319
+ * and stretch().
320
+ *
321
+ * Each shape is transformed the first time something asks for it, not all
322
+ * at once. The line font has hundreds of letters, and a text component
323
+ * builds a new font every frame while its size, width or slant is animated,
324
+ * but only draws a few of them.
325
+ * @param advanceRatio Each letter's advance is multiplied by this.
326
+ */
327
+ #copyLettersTo(
328
+ result: Font,
329
+ matrix: DOMMatrixReadOnly,
330
+ advanceRatio: number,
331
+ ): void {
224
332
  this.#letters.forEach((originalLetter, key) => {
333
+ let shape: PathShape | undefined;
225
334
  const newLetter: DescriptionOfLetter = {
226
- advance: ratio * originalLetter.advance,
227
- shape: originalLetter.shape.transform(matrix),
335
+ advance: advanceRatio * originalLetter.advance,
336
+ get shape() {
337
+ shape ??= originalLetter.shape.transform(matrix);
338
+ return shape;
339
+ },
228
340
  };
229
341
  result.#letters.set(key, newLetter);
230
342
  });
231
- return result;
232
343
  }
233
- static cursive(size: number, degreesRight = 0): Font {
234
- const base = this.fromJSON(cursiveBase);
235
- const result = base.resize(size).oblique(degreesRight);
236
- return result;
344
+ /**
345
+ * The same font, recommending a different {@link strokeWidth}.
346
+ * The letters are shared, not copied.
347
+ */
348
+ #withStrokeWidth(strokeWidth: number): Font {
349
+ if (strokeWidth == this.strokeWidth) {
350
+ return this;
351
+ }
352
+ return new Font(
353
+ this.top,
354
+ this.bottom,
355
+ this.spaceWidth,
356
+ strokeWidth,
357
+ this.kerning,
358
+ this.mHeight,
359
+ this.#letters,
360
+ );
237
361
  }
238
- static futuraL(size: number, degreesRight = 0): Font {
239
- const base = this.fromJSON(futuraLBase);
240
- const result = base.resize(size).oblique(degreesRight);
241
- return result;
362
+ /**
363
+ * One of the Hershey fonts, from its JSON. Parsed once, the first time it's
364
+ * needed. A font never changes, so everyone can share it.
365
+ *
366
+ * These fonts came from 7-bit ASCII, which put a curly quote where `'` and
367
+ * `` ` `` are. So `'` and `` ` `` draw as `’` and `‘`. Without that, "don't"
368
+ * would lose its apostrophe.
369
+ */
370
+ static #hershey(json: unknown): Font {
371
+ const font = Font.fromJSON(json);
372
+ for (const [ascii, curly] of [
373
+ ["'", "’"],
374
+ ["`", "‘"],
375
+ ]) {
376
+ const letter = font.#letters.get(curly);
377
+ if (letter && !font.#letters.has(ascii)) {
378
+ font.#letters.set(ascii, letter);
379
+ }
380
+ }
381
+ return font;
382
+ }
383
+ static #cursiveBase: Font | undefined;
384
+ static #futuraLBase: Font | undefined;
385
+ /**
386
+ * Size, width, slant and stroke, all at once, in the right order.
387
+ * See {@link FontOptions}.
388
+ */
389
+ static #fromOptions(base: Font, options: FontOptions): Font {
390
+ const { size, width = 1, obliqueness = 0 } = options;
391
+ if (!(width > 0 && isFinite(width))) {
392
+ throw new Error(`width must be positive, not ${width}.`);
393
+ }
394
+ const shaped = base.resize(size).stretch(width).oblique(obliqueness);
395
+ return shaped.#withStrokeWidth(
396
+ options.strokeWidth ?? shaped.strokeWidth * (options.boldness ?? 1),
397
+ );
398
+ }
399
+ /**
400
+ * A handwritten script, from the Hershey fonts. Its letters join up.
401
+ *
402
+ * It has the 96 printable ASCII characters and nothing else.
403
+ * @param size The height of a capital M.
404
+ * @param degreesRight Slant it this much more. See {@link oblique}.
405
+ */
406
+ static cursive(size: number, degreesRight?: number): Font;
407
+ /**
408
+ * A handwritten script, from the Hershey fonts, with its size, width,
409
+ * slant and stroke set all at once. See {@link FontOptions}.
410
+ */
411
+ static cursive(options: FontOptions): Font;
412
+ static cursive(sizeOrOptions: number | FontOptions, degreesRight = 0): Font {
413
+ const base = (Font.#cursiveBase ??= Font.#hershey(cursiveBase));
414
+ return typeof sizeOrOptions === "number"
415
+ ? base.resize(sizeOrOptions).oblique(degreesRight)
416
+ : Font.#fromOptions(base, sizeOrOptions);
417
+ }
418
+ /**
419
+ * A simple sans-serif, from the Hershey fonts.
420
+ *
421
+ * It has the 96 printable ASCII characters and nothing else.
422
+ * @param size The height of a capital M.
423
+ * @param degreesRight Slant it this much. See {@link oblique}.
424
+ */
425
+ static futuraL(size: number, degreesRight?: number): Font;
426
+ /**
427
+ * A simple sans-serif, from the Hershey fonts, with its size, width, slant
428
+ * and stroke set all at once. See {@link FontOptions}.
429
+ */
430
+ static futuraL(options: FontOptions): Font;
431
+ static futuraL(sizeOrOptions: number | FontOptions, degreesRight = 0): Font {
432
+ const base = (Font.#futuraLBase ??= Font.#hershey(futuraLBase));
433
+ return typeof sizeOrOptions === "number"
434
+ ? base.resize(sizeOrOptions).oblique(degreesRight)
435
+ : Font.#fromOptions(base, sizeOrOptions);
242
436
  }
243
437
  }
@@ -6,7 +6,7 @@ import {
6
6
  pickAny,
7
7
  polarToRectangular,
8
8
  } from "phil-lib/misc";
9
- import { Font, FontMetrics } from "./letters-base.ts";
9
+ import { Font, FontMetrics, FontOptions } from "./letters-base.ts";
10
10
  import {
11
11
  PathShape,
12
12
  PathBuilder,
@@ -103,13 +103,30 @@ class DescriptionOfLetter {
103
103
  export type FontMap = Map<string, DescriptionOfLetter>;
104
104
 
105
105
  /**
106
- *
106
+ * Everything about a line font, all at once, for {@link makeLineFont}.
107
+ * The same options the other fonts take. See {@link FontOptions}.
108
+ */
109
+ export type LineFontOptions = FontOptions;
110
+
111
+ /**
112
+ * Make a line font.
107
113
  * @param fontMetrics How big to make the font.
108
114
  * There are additional things you can configure with a LineFontMetrics object.
109
115
  * In particular, some letters get a little wider if you use a thicker line width.
116
+ *
117
+ * Or a {@link LineFontOptions}, to set the size, boldness, width and slant all
118
+ * at once.
110
119
  * @returns
111
120
  */
112
- export function makeLineFont(fontMetrics: number | LineFontMetrics): Font {
121
+ export function makeLineFont(
122
+ fontMetrics: number | LineFontMetrics | LineFontOptions,
123
+ ): Font {
124
+ if (
125
+ typeof fontMetrics === "object" &&
126
+ !(fontMetrics instanceof LineFontMetrics)
127
+ ) {
128
+ return makeLineFontFromOptions(fontMetrics);
129
+ }
113
130
  const fontMap = makeLineFontMap(fontMetrics);
114
131
  const { top, bottom, spaceWidth, strokeWidth, defaultKerning, mHeight } =
115
132
  (fontMap.get("0") ?? pickAny(fontMap))!.fontMetrics;
@@ -125,6 +142,42 @@ export function makeLineFont(fontMetrics: number | LineFontMetrics): Font {
125
142
  return result;
126
143
  }
127
144
 
145
+ /**
146
+ * The narrowest `width` ({@link LineFontOptions}) the text components draw. A
147
+ * Font Width of 0 or less in the Visual Editor is drawn at this width, as
148
+ * thin as it goes, instead of throwing.
149
+ */
150
+ export const MIN_FONT_WIDTH = 0.01;
151
+
152
+ /**
153
+ * The smallest `size` the text components draw. A Font Size of 0 or less in
154
+ * the Visual Editor is drawn at this size, too small to see, instead of
155
+ * throwing. Typing 0.5 passes through 0, and an animation can start at 0.
156
+ */
157
+ export const MIN_FONT_SIZE = 1e-6;
158
+
159
+ /** {@link makeLineFont} with a {@link LineFontOptions}. */
160
+ function makeLineFontFromOptions(options: LineFontOptions): Font {
161
+ const { size, width = 1, obliqueness = 0 } = options;
162
+ if (!(width > 0 && isFinite(width))) {
163
+ throw new Error(`width must be positive, not ${width}.`);
164
+ }
165
+ // Some letters are drawn for a particular stroke width at a particular size:
166
+ // the K's three lines meet exactly, for example. A stretched font has two
167
+ // sizes, so build it at their geometric mean, then scale it to the height and
168
+ // width asked for. With a width of 1, that's exactly makeLineFontRatio().
169
+ const buildSize = size * Math.sqrt(width);
170
+ const strokeWidth =
171
+ options.strokeWidth ??
172
+ LineFontMetrics.defaultStrokeWidth(buildSize) * (options.boldness ?? 1);
173
+ // resize() scales the stroke width by size / buildSize, and stretch() by
174
+ // √width, so it comes out as strokeWidth.
175
+ return makeLineFont(new LineFontMetrics(buildSize, strokeWidth))
176
+ .resize(size)
177
+ .stretch(width)
178
+ .oblique(obliqueness);
179
+ }
180
+
128
181
  /**
129
182
  * Create a font whose full height (top margin through descender) equals
130
183
  * `totalHeight`. The mHeight (cap height) is `totalHeight * 2/3` because