@beff/aula 0.1.0 → 0.2.0

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/README.md CHANGED
@@ -6,4 +6,4 @@ The `aula` command is in [`@beff/aula-cli`](https://www.npmjs.com/package/@beff/
6
6
 
7
7
  ## Licenses
8
8
 
9
- aula's own code is MIT; the engine includes eSpeak NG, GPL-3.0-or-later, so the package as a whole is MIT AND GPL-3.0-or-later. The engine component also has third-party code and data, with their own licenses, in `dist/licenses/`: eSpeak NG (GPL-3.0-or-later), misaki (Apache-2.0), spaCy (MIT) and the fonts Nunito Sans and JetBrains Mono (SIL Open Font License 1.1).
9
+ aula's own code is MIT; the engine includes eSpeak NG, GPL-3.0-or-later, so the package as a whole is MIT AND GPL-3.0-or-later. The engine component also has third-party code and data, with their own licenses, in `dist/licenses/`: eSpeak NG (GPL-3.0-or-later), misaki (Apache-2.0), spaCy (MIT), Skia's PathOps (BSD-3-Clause, ported to Rust by valo-pathops, MIT) and the fonts Nunito Sans and JetBrains Mono (SIL Open Font License 1.1).
@@ -139,7 +139,8 @@ export declare class FadeTransformAnimation extends TransformAnimation {
139
139
  * from `mobject`'s box (Manim `FadeTransform`). At the end the target takes its place. */
140
140
  export declare function FadeTransform(mobject: Mobject, targetMobject: Mobject, opts?: FadeTransformOptions): FadeTransformAnimation;
141
141
  /** `FadeTransform` part by part: each submobject of `mobject` goes to the submobject of
142
- * `targetMobject` at the same place in the list (Manim `FadeTransformPieces`). */
142
+ * `targetMobject` at the same place in the list (Manim `FadeTransformPieces`). It is one
143
+ * transform, as in Manim: the rate and the lag ratio apply to all the parts together. */
143
144
  export declare function FadeTransformPieces(mobject: Mobject, targetMobject: Mobject, opts?: FadeTransformOptions): FadeTransformAnimation;
144
145
  /** Draws the strokes of the mobject from their start (Manim `Create`; `lagRatio` 1: one part
145
146
  * after another). */
@@ -541,26 +541,11 @@ class FadeTransformPiecesAnimation extends FadeTransformAnimation {
541
541
  }
542
542
  }
543
543
  /** `FadeTransform` part by part: each submobject of `mobject` goes to the submobject of
544
- * `targetMobject` at the same place in the list (Manim `FadeTransformPieces`). */
544
+ * `targetMobject` at the same place in the list (Manim `FadeTransformPieces`). It is one
545
+ * transform, as in Manim: the rate and the lag ratio apply to all the parts together. */
545
546
  export function FadeTransformPieces(mobject, targetMobject, opts = {}) {
546
- const sources = mobject.submobjects;
547
- const targets = targetMobject.submobjects;
548
- if (sources.length === 0 || targets.length === 0)
549
- return FadeTransform(mobject, targetMobject, opts);
550
- const { runTime, rateFunc: rate, lagRatio, stretch = true, dimToMatch = 1, ...each } = opts;
551
- const n = Math.max(sources.length, targets.length);
552
- const pieces = Array.from({ length: n }, (_, i) => {
553
- const s = sources[Math.min(i, sources.length - 1)];
554
- const t = targets[Math.min(i, targets.length - 1)];
555
- const source = i < sources.length ? s : s?.copy();
556
- return source === undefined || t === undefined ? [] : [FadeTransform(source, t, { ...each, stretch, dimToMatch })];
557
- }).flat();
558
- const handle = engine.animationGroup(animations("FadeTransformPieces", pieces));
559
- const a = engineAnimation(FadeTransformPiecesAnimation, handle, mobject, {
560
- ...(runTime === undefined ? {} : { runTime }),
561
- ...(rate === undefined ? {} : { rateFunc: rate }),
562
- ...(lagRatio === undefined ? {} : { lagRatio }),
563
- }, { rate: linear });
547
+ const { stretch = true, dimToMatch = 1, ...rest } = opts;
548
+ const a = engineAnimation(FadeTransformPiecesAnimation, engine.fadeTransformPieces(raw(mobject), raw(targetMobject), stretch, dimToMatch), mobject, rest);
564
549
  a.targetMobject = targetMobject;
565
550
  a.stretch = stretch;
566
551
  a.dimToMatch = dimToMatch;
package/dist/engine.d.ts CHANGED
@@ -1,5 +1,6 @@
1
- import type { Animation as RawAnimation, Mobject as RawMobject, Rate as RawRate, Chapter as RawChapter, Narration, NarrationWait, Screen } from "./generated/interfaces/aula-engine-engine.js";
1
+ import type { Animation as RawAnimation, Mobject as RawMobject, Rate as RawRate, Chapter as RawChapter, Narration, NarrationWait, Screen, LexiconEntry, SayPart } from "./generated/interfaces/aula-engine-engine.js";
2
2
  import type { Color, Diagnostic, Location, Point, ChapterText as RawChapterText, ParagraphText as RawParagraphText } from "./generated/interfaces/aula-engine-types.js";
3
+ import type { Lexicon } from "./lexicon.ts";
3
4
  import type { Theme } from "./theme.ts";
4
5
  import { type CapStyleType, type Complex, type LineJointType } from "./utils.ts";
5
6
  export type { Instant, Screen, Shown } from "./generated/interfaces/aula-engine-engine.js";
@@ -1306,7 +1307,7 @@ type RowData = {
1306
1307
  } | {
1307
1308
  readonly kind: "say";
1308
1309
  readonly call: Call;
1309
- readonly text: string;
1310
+ readonly parts: readonly SayPart[];
1310
1311
  } | {
1311
1312
  readonly kind: "show";
1312
1313
  readonly call: Call;
@@ -1364,6 +1365,13 @@ export interface ProjectFields {
1364
1365
  card?: Card;
1365
1366
  /** The chapters, in order: the default exports of their chapter modules. */
1366
1367
  chapters: readonly ChapterDefinition[];
1368
+ /** The words that have one reading in all of the video: each key is a written word, and each
1369
+ * value is the reading that the narrator speaks at each use of the word in a `say`, in the
1370
+ * title and in a heading (the reading of `pronounce`). The match is the whole word, with marks around it and a
1371
+ * possessive `'s`, in the same case; a key that starts with a lowercase letter also matches
1372
+ * the word with a capital first letter. `pronounce` comes first, and aula's own lexicon and
1373
+ * misaki come after it. The default is no word. */
1374
+ lexicon?: Lexicon;
1367
1375
  }
1368
1376
  /** What `defineProject` returns. */
1369
1377
  export interface ProjectDefinition extends Required<Omit<ProjectFields, "card" | "theme">> {
@@ -1376,8 +1384,8 @@ export interface ProjectDefinition extends Required<Omit<ProjectFields, "card" |
1376
1384
  * `@beff/aula/run`). */
1377
1385
  readonly call: Error;
1378
1386
  }
1379
- /** `export default defineProject({ title, storyboard, theme, chapters })`: one project, in the
1380
- * `index.ts` of its folder. Makes no engine call. */
1387
+ /** `export default defineProject({ title, storyboard, theme, lexicon, chapters })`: one project,
1388
+ * in the `index.ts` of its folder. Makes no engine call. */
1381
1389
  export declare function defineProject(fields: ProjectFields): ProjectDefinition;
1382
1390
  /** True for a value that defineProject made. */
1383
1391
  export declare function isProjectDefinition(v: unknown): v is ProjectDefinition;
@@ -1385,11 +1393,35 @@ export declare function isProjectDefinition(v: unknown): v is ProjectDefinition;
1385
1393
  * under the paragraph. `check` is for kits: aula calls it one time, after the chapter is drawn,
1386
1394
  * and reports its findings. */
1387
1395
  export declare function paragraph(notes?: Notes, check?: Check): Row;
1388
- /** The text of a row tag (`name` is the tag): one template with no `${…}`, no invalid escape
1389
- * and no line break. Throws a AulaError at the row. A kit's own row tag calls it. */
1390
- export declare function tagText(name: string, strings: TemplateStringsArray): string;
1391
- /** ``say`text` ``: the next words of the narration. */
1392
- export declare function say(strings: TemplateStringsArray): Row;
1396
+ declare const PRONOUNCE: unique symbol;
1397
+ /** How the narrator reads the word of `pronounce`: `{ say: "…" }` is plain text, which goes
1398
+ * through the normal front end (aula's lexicon, then misaki), and `{ phonemes: "…" }` is
1399
+ * phonemes of Kokoro's token table (misaki's symbols, as `aula tokens` shows them). */
1400
+ export type Reading = {
1401
+ readonly say: string;
1402
+ } | {
1403
+ readonly phonemes: string;
1404
+ };
1405
+ /** What `pronounce` returns: one written word and its reading. Only a `say` takes it. */
1406
+ export interface Pronounce {
1407
+ readonly [PRONOUNCE]: true;
1408
+ readonly written: string;
1409
+ readonly reading: Reading;
1410
+ }
1411
+ /** ``say`The file is 4 ${pronounce("GB", { say: "gigabytes" })}.` ``: one word that the
1412
+ * screen, the word times and `aula script` show as `written`, and that the narrator reads as
1413
+ * `reading`. The written part is one word, and its word time covers all of the reading. Use it
1414
+ * only for a word whose reading changes with the context. `aula check` gives an error when the
1415
+ * written part is not one word, or when a phoneme is not in Kokoro's token table. */
1416
+ export declare function pronounce(written: string, reading: Reading): Pronounce;
1417
+ /** The text of a row tag (`name` is the tag), as written: one template with no invalid escape
1418
+ * and no line break. A `${…}` can only be the value of `pronounce`, and its written word is in
1419
+ * the text. Throws a AulaError at the row. A kit's own row tag calls it with the values of its
1420
+ * template. */
1421
+ export declare function tagText(name: string, strings: TemplateStringsArray, ...values: readonly unknown[]): string;
1422
+ /** ``say`text` ``: the next words of the narration. A `${…}` can only be
1423
+ * `pronounce(written, reading)`: ``say`The file is 4 ${pronounce("GB", { say: "gigabytes" })}.` ``. */
1424
+ export declare function say(strings: TemplateStringsArray, ...words: readonly Pronounce[]): Row;
1393
1425
  /** ``show`note``timing` ``: a move. The note says what changes on screen. The timing, a
1394
1426
  * second template right after the note, gives the length of the move: ``25%`` is a share of the
1395
1427
  * `say` above it (0 < N <= 100) and scales with the narration, and ``3s`` is seconds (N > 0).
@@ -1513,6 +1545,8 @@ export interface ChapterSlot {
1513
1545
  file: string;
1514
1546
  /** The frames of an error (`Host.frames`). */
1515
1547
  frames(error: Error): readonly StackFrame[];
1548
+ /** The project lexicon in the engine's form. */
1549
+ lexicon: readonly LexiconEntry[];
1516
1550
  }
1517
1551
  /** Reads a chapter: calls `rows` and no move. It gives the text and the text problems; the
1518
1552
  * paragraph checks run only in `drawRows`. A chapter error is thrown (a AulaError, a WIT
package/dist/engine.js CHANGED
@@ -2915,10 +2915,10 @@ export function defineChapter(fields) {
2915
2915
  return { ...fields, call: new Error("defineChapter") };
2916
2916
  }
2917
2917
  const PROJECT = Symbol("aula.project");
2918
- /** `export default defineProject({ title, storyboard, theme, chapters })`: one project, in the
2919
- * `index.ts` of its folder. Makes no engine call. */
2918
+ /** `export default defineProject({ title, storyboard, theme, lexicon, chapters })`: one project,
2919
+ * in the `index.ts` of its folder. Makes no engine call. */
2920
2920
  export function defineProject(fields) {
2921
- return { [PROJECT]: true, storyboard: "", ...fields, call: new Error("defineProject") };
2921
+ return { [PROJECT]: true, storyboard: "", lexicon: {}, ...fields, call: new Error("defineProject") };
2922
2922
  }
2923
2923
  /** True for a value that defineProject made. */
2924
2924
  export function isProjectDefinition(v) {
@@ -2938,25 +2938,72 @@ function row(name, data) {
2938
2938
  export function paragraph(notes = {}, check) {
2939
2939
  return row("paragraph", (call) => ({ kind: "paragraph", call, notes, check }));
2940
2940
  }
2941
- /** The text of a row tag (`name` is the tag): one template with no `${…}`, no invalid escape
2942
- * and no line break. Throws a AulaError at the row. A kit's own row tag calls it. */
2943
- export function tagText(name, strings) {
2944
- if (strings.length !== 1) {
2941
+ const PRONOUNCE = Symbol("aula.pronounce");
2942
+ /** ``say`The file is 4 ${pronounce("GB", { say: "gigabytes" })}.` ``: one word that the
2943
+ * screen, the word times and `aula script` show as `written`, and that the narrator reads as
2944
+ * `reading`. The written part is one word, and its word time covers all of the reading. Use it
2945
+ * only for a word whose reading changes with the context. `aula check` gives an error when the
2946
+ * written part is not one word, or when a phoneme is not in Kokoro's token table. */
2947
+ export function pronounce(written, reading) {
2948
+ const usage = 'pronounce("word", { say: "words" }) or pronounce("word", { phonemes: "fˈQnimz" })';
2949
+ if (typeof written !== "string")
2950
+ fault("pronounce: the written word must be a string", usage);
2951
+ const r = reading;
2952
+ const one = typeof r === "object" &&
2953
+ r !== null &&
2954
+ Object.keys(r).length === 1 &&
2955
+ (("say" in r && typeof r.say === "string") || ("phonemes" in r && typeof r.phonemes === "string"));
2956
+ if (!one)
2957
+ fault("pronounce: give the reading as { say: string } or { phonemes: string }", usage);
2958
+ return { [PRONOUNCE]: true, written, reading };
2959
+ }
2960
+ /** True for a value that `pronounce` made. */
2961
+ function isPronounce(v) {
2962
+ return typeof v === "object" && v !== null && PRONOUNCE in v;
2963
+ }
2964
+ /** The parts of a row tag's text: each piece of the template, and the written word and the
2965
+ * reading of each `pronounce` between them. Every other `${…}` is an error. */
2966
+ function tagParts(name, strings, values) {
2967
+ if (strings.length !== values.length + 1) {
2945
2968
  fault(`${name}: write the text as it is, with no \${…}`, "the text in the source is the text that is spoken");
2946
2969
  }
2947
- const text = strings[0];
2948
- if (text === undefined) {
2949
- fault(`${name}: the text has an invalid escape`, "escape only a backtick, a backslash and ${ with a backslash");
2950
- }
2951
- if (/[\r\n]/.test(text)) {
2952
- fault(`${name}: write the text on one line`, "a row's text stays on the line of its call");
2953
- }
2954
- return text;
2955
- }
2956
- /** ``say`text` ``: the next words of the narration. */
2957
- export function say(strings) {
2958
- const text = tagText("say", strings);
2959
- return row("say", (call) => ({ kind: "say", call, text }));
2970
+ const parts = [];
2971
+ strings.forEach((text, i) => {
2972
+ if (text === undefined) {
2973
+ fault(`${name}: the text has an invalid escape`, "escape only a backtick, a backslash and ${ with a backslash");
2974
+ }
2975
+ if (/[\r\n]/.test(text)) {
2976
+ fault(`${name}: write the text on one line`, "a row's text stays on the line of its call");
2977
+ }
2978
+ if (text !== "")
2979
+ parts.push({ text });
2980
+ if (i === values.length)
2981
+ return;
2982
+ const value = values[i];
2983
+ if (!isPronounce(value)) {
2984
+ fault(`${name}: write the text as it is; a \${…} can only be pronounce(written, reading)`, "the text in the source is the text that is spoken");
2985
+ }
2986
+ const reading = "say" in value.reading
2987
+ ? { tag: "words", val: value.reading.say }
2988
+ : { tag: "phonemes", val: value.reading.phonemes };
2989
+ parts.push({ text: value.written, reading });
2990
+ });
2991
+ return parts;
2992
+ }
2993
+ /** The text of a row tag (`name` is the tag), as written: one template with no invalid escape
2994
+ * and no line break. A `${…}` can only be the value of `pronounce`, and its written word is in
2995
+ * the text. Throws a AulaError at the row. A kit's own row tag calls it with the values of its
2996
+ * template. */
2997
+ export function tagText(name, strings, ...values) {
2998
+ return tagParts(name, strings, values)
2999
+ .map((p) => p.text)
3000
+ .join("");
3001
+ }
3002
+ /** ``say`text` ``: the next words of the narration. A `${…}` can only be
3003
+ * `pronounce(written, reading)`: ``say`The file is 4 ${pronounce("GB", { say: "gigabytes" })}.` ``. */
3004
+ export function say(strings, ...words) {
3005
+ const parts = tagParts("say", strings, words);
3006
+ return row("say", (call) => ({ kind: "say", call, parts }));
2960
3007
  }
2961
3008
  /** ``show`note``timing` ``: a move. The note says what changes on screen. The timing, a
2962
3009
  * second template right after the note, gives the length of the move: ``25%`` is a share of the
@@ -3176,24 +3223,18 @@ function rowsOf(chapter) {
3176
3223
  function noteList(notes) {
3177
3224
  return Object.entries(notes).map(([key, v]) => ({ key, values: typeof v === "string" ? [v] : [...v] }));
3178
3225
  }
3179
- /** Gives the rows to the engine in order; with a scene, it runs each move after its `show`.
3180
- * With a scene, it stops at the first paragraph that the narration does not cover (a partial
3181
- * render): the moves after it do not run. */
3226
+ /** Gives the rows to the engine in order; with a scene, it runs each move after its `show`. */
3182
3227
  function write(handle, chapter, rows, slot, scene, card) {
3183
- const narrated = scene === undefined ? Infinity : handle.narratedParagraphs();
3184
- let paragraphs = 0;
3228
+ handle.lexicon([...slot.lexicon]);
3185
3229
  handle.header(placeIn(chapter.call, slot), chapter.heading, chapter.metaphor);
3186
3230
  card?.();
3187
3231
  for (const r of rows) {
3188
3232
  const at = placeIn(r.call, slot);
3189
3233
  if (r.kind === "paragraph") {
3190
- paragraphs += 1;
3191
- if (paragraphs > narrated)
3192
- return;
3193
3234
  handle.paragraph(at, noteList(r.notes));
3194
3235
  }
3195
3236
  else if (r.kind === "say") {
3196
- handle.say(at, r.text);
3237
+ handle.say(at, [...r.parts]);
3197
3238
  }
3198
3239
  else if (r.move === undefined) {
3199
3240
  handle.open(at, r.note, r.timing);
@@ -3307,7 +3348,7 @@ export function checkRows(chapter, theme, card, slot, exists) {
3307
3348
  export function checkTitle(title, theme, card, count) {
3308
3349
  freshSeed();
3309
3350
  const handle = engine.Chapter.orderPass(0, count + 1);
3310
- const slot = { number: 0, count, source: "", file: "", frames: () => [] };
3351
+ const slot = { number: 0, count, source: "", file: "", frames: () => [], lexicon: [] };
3311
3352
  const scene = playCard(handle, slot, { file: "", line: 0, col: 0 }, card, { title, chapter: 0, count, theme });
3312
3353
  handle.finish();
3313
3354
  return { diagnostics: [...handle.report(), ...scene.warnings] };
@@ -3330,8 +3371,7 @@ function finished(handle) {
3330
3371
  * error). */
3331
3372
  export function drawRows(chapter, theme, card, slot, drawing) {
3332
3373
  const rows = rowsOf(chapter);
3333
- const { text } = textOf(chapter, rows, slot);
3334
- const handle = engine.Chapter.render(drawing.narration, theme.background, slot.number, drawing.count, drawing.name, drawing.lint, text);
3374
+ const handle = engine.Chapter.render(drawing.narration, theme.background, slot.number, drawing.count, drawing.name, drawing.lint);
3335
3375
  const scene = new Scene(handle, slot);
3336
3376
  const heading = { title: chapter.heading, chapter: slot.number, count: slot.count, theme };
3337
3377
  write(handle, chapter, rows, slot, scene, () => {
@@ -3342,9 +3382,9 @@ export function drawRows(chapter, theme, card, slot, drawing) {
3342
3382
  /** Draws chapter 0: the card of the project `title` while the title is spoken. */
3343
3383
  export function drawTitle(title, theme, card, drawing) {
3344
3384
  freshSeed();
3345
- const handle = engine.Chapter.render(drawing.narration, theme.background, 0, drawing.count, drawing.name, drawing.lint, undefined);
3385
+ const handle = engine.Chapter.render(drawing.narration, theme.background, 0, drawing.count, drawing.name, drawing.lint);
3346
3386
  const count = drawing.count - 1;
3347
- const slot = { number: 0, count, source: "", file: "", frames: () => [] };
3387
+ const slot = { number: 0, count, source: "", file: "", frames: () => [], lexicon: [] };
3348
3388
  playCard(handle, slot, { file: "", line: 0, col: 0 }, card, { title, chapter: 0, count, theme });
3349
3389
  return finished(handle);
3350
3390
  }
@@ -3352,7 +3392,7 @@ export function drawTitle(title, theme, card, drawing) {
3352
3392
  * second, on a black background. There is no project, no narration and no heading. */
3353
3393
  export function renderScene(draw) {
3354
3394
  const handle = engine.Chapter.scene();
3355
- const slot = { number: 0, count: 1, source: "scene", file: "scene", frames: () => [] };
3395
+ const slot = { number: 0, count: 1, source: "scene", file: "scene", frames: () => [], lexicon: [] };
3356
3396
  draw(new Scene(handle, slot));
3357
3397
  handle.finish();
3358
3398
  const trace = JSON.parse(handle.trace());
Binary file