guidex 0.3.3 → 0.5.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/CHANGELOG.md +68 -0
- package/README.md +266 -37
- package/dist/lib/app/StaffSettings.d.ts +1 -0
- package/dist/lib/app/editorState.d.ts +32 -23
- package/dist/lib/app/shareLink.d.ts +2 -3
- package/dist/lib/{chantLayout-CVVuJUJl.js → chantLayout-BrCQUOhp.js} +980 -1142
- package/dist/lib/chantLayout.d.ts +32 -3
- package/dist/lib/chantMarkup.d.ts +63 -20
- package/dist/lib/drawnStaff.d.ts +130 -0
- package/dist/lib/editorStorage.d.ts +44 -4
- package/dist/lib/embed/core.d.ts +26 -4
- package/dist/lib/embed/index.js +1 -1
- package/dist/lib/embed/manual.d.ts +9 -3
- package/dist/lib/embed/manual.js +16 -49
- package/dist/lib/fonts/guidohu-staffless.woff2 +0 -0
- package/dist/lib/guidex.css +27 -0
- package/dist/lib/guidoRender.d.ts +22 -4
- package/dist/lib/guidoScript.d.ts +60 -23
- package/dist/lib/guidoScriptTransforms.d.ts +0 -18
- package/dist/lib/guidoStyles.d.ts +85 -74
- package/dist/lib/react/GuidexScore.d.ts +18 -6
- package/dist/lib/react/index.d.ts +2 -0
- package/dist/lib/react/index.js +47 -4
- package/dist/lib/recitationSegments.d.ts +93 -0
- package/dist/lib/scoreImage-B2SGdROL.js +8391 -0
- package/dist/lib/staffGlyphs.d.ts +12 -0
- package/dist/lib/stanzas.d.ts +6 -3
- package/dist/lib/textMarkup.d.ts +67 -0
- package/dist/lib/word/index.js +1 -1
- package/dist/lib/{wordChecker-DG09l_ol.js → wordChecker-CjYl2yof.js} +40 -13
- package/dist/lib/wordExport.d.ts +23 -4
- package/dist-embed/fonts/guidohu-staffless.woff2 +0 -0
- package/dist-embed/guidex-embed.min.css +1 -1
- package/dist-embed/guidex-embed.min.js +1 -1
- package/package.json +3 -2
- package/dist/lib/scoreImage-lvOrTbG-.js +0 -1009
|
@@ -346,7 +346,7 @@ function isClefGlyph(ch) {
|
|
|
346
346
|
* (spelled out: `Object.keys` would list the digit keys first). The
|
|
347
347
|
* index is the staff position the exact transposition counts with. */
|
|
348
348
|
var STAFF = [..."ðñ0123456789öüó"];
|
|
349
|
-
var LETTERS
|
|
349
|
+
var LETTERS = "abcdefg";
|
|
350
350
|
/** The pitch letter (`a`…`g`, octave dropped) of a pitch glyph — a base
|
|
351
351
|
* glyph or any of its variants — under `clef` (a clef glyph; default the
|
|
352
352
|
* standard g-clef). Undefined when the glyph has no pitch or the clef
|
|
@@ -356,8 +356,287 @@ function pitchLetterOf(ch, clef = "M") {
|
|
|
356
356
|
const entry = CLEF_GLYPHS[clef];
|
|
357
357
|
if (position < 0 || !entry) return void 0;
|
|
358
358
|
const linePosition = STAFF.indexOf("2") + 2 * (entry.line - 1);
|
|
359
|
-
const shift = LETTERS
|
|
360
|
-
return LETTERS
|
|
359
|
+
const shift = LETTERS.indexOf(entry.clef) - LETTERS.indexOf(gClefPitchOf(STAFF[linePosition])[0]);
|
|
360
|
+
return LETTERS[((LETTERS.indexOf(gClefPitchOf(STAFF[position])[0]) + shift) % 7 + 7) % 7];
|
|
361
|
+
}
|
|
362
|
+
//#endregion
|
|
363
|
+
//#region src/noteGroups.ts
|
|
364
|
+
/**
|
|
365
|
+
* Note-group anatomy shared by the layout pass and the exact
|
|
366
|
+
* transposition: the score markers and the split of a group into its
|
|
367
|
+
* leading markers, notes and trailing markers. Kept free of imports, so
|
|
368
|
+
* the transforms can use it without pulling in the style sets.
|
|
369
|
+
*/
|
|
370
|
+
/**
|
|
371
|
+
* Score marker characters: they carry no pitch, and when they sit at the
|
|
372
|
+
* start or end of a note group (with the adjacent dash) they act as
|
|
373
|
+
* whitespace rather than note width.
|
|
374
|
+
*/
|
|
375
|
+
var SCORE_MARKERS = ",.:?'\"+!%/=()ÖÜÓ;";
|
|
376
|
+
var MARKER_CHARS$1 = new Set(SCORE_MARKERS.split(""));
|
|
377
|
+
function splitNoteMarkers(notes) {
|
|
378
|
+
let start = 0;
|
|
379
|
+
while (start < notes.length && MARKER_CHARS$1.has(notes[start])) start++;
|
|
380
|
+
if (start > 0 && start < notes.length) start = notes[start] === "-" ? start + 1 : 0;
|
|
381
|
+
let end = notes.length;
|
|
382
|
+
while (end > start && MARKER_CHARS$1.has(notes[end - 1])) end--;
|
|
383
|
+
if (end < notes.length && end > start) end = notes[end - 1] === "-" ? end - 1 : notes.length;
|
|
384
|
+
return {
|
|
385
|
+
leading: notes.slice(0, start),
|
|
386
|
+
core: notes.slice(start, end),
|
|
387
|
+
trailing: notes.slice(end)
|
|
388
|
+
};
|
|
389
|
+
}
|
|
390
|
+
//#endregion
|
|
391
|
+
//#region src/textMarkup.ts
|
|
392
|
+
/** The pause-marker characters: they split off into runs of their own. */
|
|
393
|
+
var MARKER_CHARS = /* @__PURE__ */ new Set(["*", "†"]);
|
|
394
|
+
/** The marks that take an argument, written `size:0.8`. */
|
|
395
|
+
var MARK_ARGUMENTS = /* @__PURE__ */ new Set([
|
|
396
|
+
"color",
|
|
397
|
+
"size",
|
|
398
|
+
"raise",
|
|
399
|
+
"font"
|
|
400
|
+
]);
|
|
401
|
+
/** Stand-ins for escaped markup characters, from the private use area:
|
|
402
|
+
* the matching below never mistakes them for markup. Only ever held by a
|
|
403
|
+
* masked string, inside the functions that work on one. */
|
|
404
|
+
var MASKED = {
|
|
405
|
+
_: "",
|
|
406
|
+
"*": "",
|
|
407
|
+
"†": "",
|
|
408
|
+
"∗": ""
|
|
409
|
+
};
|
|
410
|
+
var UNMASKED = new Map(Object.entries(MASKED).map(([ch, mask]) => [mask, ch]));
|
|
411
|
+
function maskEscapes(written) {
|
|
412
|
+
return written.replace(/\\(.)/gsu, (_, ch) => MASKED[ch] ?? ch);
|
|
413
|
+
}
|
|
414
|
+
function unmask(masked) {
|
|
415
|
+
return masked.replace(/[-]/g, (mask) => UNMASKED.get(mask));
|
|
416
|
+
}
|
|
417
|
+
/** The whole-syllable wrapper of masked text, the longer `__` first so
|
|
418
|
+
* `__V.__` never matches the `_` rubric rule: the same characters on both
|
|
419
|
+
* ends, content between. */
|
|
420
|
+
function wrapperOf(masked) {
|
|
421
|
+
return ["__", "_"].find((wrapper) => masked.length >= wrapper.length * 2 + 1 && masked.startsWith(wrapper) && masked.endsWith(wrapper));
|
|
422
|
+
}
|
|
423
|
+
/** The `*…*` initial pairs of masked text, as segments: only non-empty
|
|
424
|
+
* pairs count, so an unmatched (or doubled) star stays a character. */
|
|
425
|
+
function initialSegments(masked) {
|
|
426
|
+
const segments = [];
|
|
427
|
+
let rest = masked;
|
|
428
|
+
let plain = "";
|
|
429
|
+
for (let m = /\*([^*]+)\*/.exec(rest); m; m = /\*([^*]+)\*/.exec(rest)) {
|
|
430
|
+
plain += rest.slice(0, m.index);
|
|
431
|
+
if (plain) {
|
|
432
|
+
segments.push({
|
|
433
|
+
text: plain,
|
|
434
|
+
initial: false
|
|
435
|
+
});
|
|
436
|
+
plain = "";
|
|
437
|
+
}
|
|
438
|
+
segments.push({
|
|
439
|
+
text: m[1],
|
|
440
|
+
initial: true
|
|
441
|
+
});
|
|
442
|
+
rest = rest.slice(m.index + m[0].length);
|
|
443
|
+
}
|
|
444
|
+
plain += rest;
|
|
445
|
+
if (plain) segments.push({
|
|
446
|
+
text: plain,
|
|
447
|
+
initial: false
|
|
448
|
+
});
|
|
449
|
+
return segments;
|
|
450
|
+
}
|
|
451
|
+
/** Splits one segment's characters into its plain, marker and plus runs. */
|
|
452
|
+
function splitGlyphRuns(text, marks, liturgy) {
|
|
453
|
+
const glyphOf = (ch) => MARKER_CHARS.has(ch) ? "marker" : liturgy && ch === "+" ? "plus" : void 0;
|
|
454
|
+
const runs = [];
|
|
455
|
+
for (const ch of text) {
|
|
456
|
+
const glyph = glyphOf(ch);
|
|
457
|
+
const last = runs[runs.length - 1];
|
|
458
|
+
if (last && last.glyph === glyph) last.text += ch;
|
|
459
|
+
else runs.push({
|
|
460
|
+
text: ch,
|
|
461
|
+
glyph
|
|
462
|
+
});
|
|
463
|
+
}
|
|
464
|
+
return runs.map((run) => ({
|
|
465
|
+
text: run.text,
|
|
466
|
+
marks: run.glyph ? [...marks, run.glyph] : [...marks]
|
|
467
|
+
}));
|
|
468
|
+
}
|
|
469
|
+
/**
|
|
470
|
+
* Parses text with no `{…}` span in it into the runs it draws: the
|
|
471
|
+
* whole-syllable `**` star (drawn as ∗), a `__…__` or `_…_` wrapper around
|
|
472
|
+
* all of it, paired `*` markers around any part of it (`*sam*ple` → "sam"
|
|
473
|
+
* and "ple"), and the marker characters and a Liturgy run's `+` split off.
|
|
474
|
+
* The wrapper characters are dropped; escaped characters are literal.
|
|
475
|
+
*/
|
|
476
|
+
function parseTextRuns(written) {
|
|
477
|
+
if (written.length === 0) return [];
|
|
478
|
+
const masked = maskEscapes(written);
|
|
479
|
+
if (masked === "**") return [{
|
|
480
|
+
text: "∗",
|
|
481
|
+
marks: ["star"]
|
|
482
|
+
}];
|
|
483
|
+
const wrapper = wrapperOf(masked);
|
|
484
|
+
const content = wrapper ? masked.slice(wrapper.length, -wrapper.length) : masked;
|
|
485
|
+
const base = wrapper ? [wrapper === "__" ? "liturgy" : "rubric"] : [];
|
|
486
|
+
return initialSegments(content).flatMap(({ text, initial }) => splitGlyphRuns(unmask(text), initial ? [...base, "initial"] : base, wrapper === "__"));
|
|
487
|
+
}
|
|
488
|
+
/** The text to read from text with no span in it: every unescaped markup
|
|
489
|
+
* character and sign gone, escapes folded. Empty for a sign alone. */
|
|
490
|
+
function plainText(written) {
|
|
491
|
+
return unmask(maskEscapes(written).replace(/[_*†∗]/g, ""));
|
|
492
|
+
}
|
|
493
|
+
/** The marks of a span's opening, read just past its `{`: comma-separated
|
|
494
|
+
* names, those of `MARK_ARGUMENTS` with a `:argument`, closed by a colon.
|
|
495
|
+
* Returns undefined when the text does not start with such a list. */
|
|
496
|
+
function spanHead(text) {
|
|
497
|
+
const marks = [];
|
|
498
|
+
let at = 0;
|
|
499
|
+
for (;;) {
|
|
500
|
+
const name = /^[A-Za-z][\w-]*/.exec(text.slice(at))?.[0];
|
|
501
|
+
if (!name) return void 0;
|
|
502
|
+
at += name.length;
|
|
503
|
+
if (text[at] === ":" && MARK_ARGUMENTS.has(name)) {
|
|
504
|
+
const argument = /^[^\s,:{}]+/.exec(text.slice(at + 1))?.[0];
|
|
505
|
+
if (!argument) return void 0;
|
|
506
|
+
marks.push(`${name}:${argument}`);
|
|
507
|
+
at += 1 + argument.length;
|
|
508
|
+
} else marks.push(name);
|
|
509
|
+
if (text[at] === ":") return {
|
|
510
|
+
marks,
|
|
511
|
+
length: at + 1
|
|
512
|
+
};
|
|
513
|
+
if (text[at] !== ",") return void 0;
|
|
514
|
+
at += 1;
|
|
515
|
+
}
|
|
516
|
+
}
|
|
517
|
+
/** Splits text into what stands between its braces, and its braces. */
|
|
518
|
+
function lexSpans(text) {
|
|
519
|
+
const lexemes = [];
|
|
520
|
+
let written = "";
|
|
521
|
+
const flush = () => {
|
|
522
|
+
if (written) lexemes.push({
|
|
523
|
+
type: "text",
|
|
524
|
+
written
|
|
525
|
+
});
|
|
526
|
+
written = "";
|
|
527
|
+
};
|
|
528
|
+
for (let i = 0; i < text.length; i++) {
|
|
529
|
+
const ch = text[i];
|
|
530
|
+
if (ch === "\\" && i + 1 < text.length) written += ch + text[++i];
|
|
531
|
+
else if (ch === "{") {
|
|
532
|
+
flush();
|
|
533
|
+
const head = spanHead(text.slice(i + 1));
|
|
534
|
+
lexemes.push({
|
|
535
|
+
type: "open",
|
|
536
|
+
marks: head?.marks
|
|
537
|
+
});
|
|
538
|
+
if (head) i += head.length;
|
|
539
|
+
} else if (ch === "}") {
|
|
540
|
+
flush();
|
|
541
|
+
lexemes.push({ type: "close" });
|
|
542
|
+
} else written += ch;
|
|
543
|
+
}
|
|
544
|
+
flush();
|
|
545
|
+
return lexemes;
|
|
546
|
+
}
|
|
547
|
+
/**
|
|
548
|
+
* Resolves the `{…}` spans of consecutive pieces of text, which may open in
|
|
549
|
+
* one piece and close in a later one. A malformed span, a span never closed
|
|
550
|
+
* and a stray `}` are reported in `problems`; their text stays, plain, and
|
|
551
|
+
* their braces go, so no punctuation prints.
|
|
552
|
+
*/
|
|
553
|
+
function resolveSpans(pieces) {
|
|
554
|
+
const problems = [];
|
|
555
|
+
const lexed = pieces.map(lexSpans);
|
|
556
|
+
const opens = /* @__PURE__ */ new Map();
|
|
557
|
+
const stack = [];
|
|
558
|
+
const strays = /* @__PURE__ */ new Set();
|
|
559
|
+
for (const [p, lexemes] of lexed.entries()) for (const lexeme of lexemes) if (lexeme.type === "open") {
|
|
560
|
+
if (!lexeme.marks) problems.push({
|
|
561
|
+
kind: "span",
|
|
562
|
+
text: pieces[p].trim(),
|
|
563
|
+
message: `A "{" needs its marks and a colon, as in "{red:text}" (in "${pieces[p].trim()}")`
|
|
564
|
+
});
|
|
565
|
+
const span = { marks: lexeme.marks ?? [] };
|
|
566
|
+
opens.set(lexeme, span);
|
|
567
|
+
stack.push(span);
|
|
568
|
+
} else if (lexeme.type === "close" && !stack.pop()) {
|
|
569
|
+
strays.add(lexeme);
|
|
570
|
+
problems.push({
|
|
571
|
+
kind: "span",
|
|
572
|
+
text: pieces[p].trim(),
|
|
573
|
+
message: `A "}" closes nothing (in "${pieces[p].trim()}")`
|
|
574
|
+
});
|
|
575
|
+
}
|
|
576
|
+
for (const span of stack) {
|
|
577
|
+
if (span.marks.length > 0) problems.push({
|
|
578
|
+
kind: "span",
|
|
579
|
+
text: span.marks.join(","),
|
|
580
|
+
message: `The span "{${span.marks.join(",")}:" is never closed`
|
|
581
|
+
});
|
|
582
|
+
span.marks = [];
|
|
583
|
+
}
|
|
584
|
+
const open = [];
|
|
585
|
+
const resolved = [];
|
|
586
|
+
let lastVisible;
|
|
587
|
+
for (const [p, lexemes] of lexed.entries()) {
|
|
588
|
+
const crossesIn = open.length > 0;
|
|
589
|
+
const segments = [];
|
|
590
|
+
const piece = {
|
|
591
|
+
bare: "",
|
|
592
|
+
source: "",
|
|
593
|
+
runs: [],
|
|
594
|
+
text: ""
|
|
595
|
+
};
|
|
596
|
+
for (const lexeme of lexemes) if (lexeme.type === "open") open.push(opens.get(lexeme));
|
|
597
|
+
else if (lexeme.type === "close") {
|
|
598
|
+
if (!strays.has(lexeme)) open.pop();
|
|
599
|
+
} else segments.push({
|
|
600
|
+
marks: open.flatMap((span) => span.marks),
|
|
601
|
+
written: lexeme.written
|
|
602
|
+
});
|
|
603
|
+
piece.bare = segments.map((s) => s.written).join("");
|
|
604
|
+
const body = trimSegments(segments);
|
|
605
|
+
piece.runs = body.flatMap(({ marks, written }) => parseTextRuns(written).map((run) => ({
|
|
606
|
+
text: run.text,
|
|
607
|
+
marks: [...marks, ...run.marks]
|
|
608
|
+
})));
|
|
609
|
+
piece.text = body.map(({ written }) => plainText(written)).join("");
|
|
610
|
+
const dropped = lexemes.some((l) => strays.has(l) || l.type === "open" && opens.get(l).marks.length === 0);
|
|
611
|
+
const live = lexemes.some((l) => l.type === "open" || l.type === "close");
|
|
612
|
+
piece.source = crossesIn || open.length > 0 || dropped || live && piece.bare.trim() === "" ? body.map(({ marks, written }) => marks.length > 0 ? `{${marks.join(",")}:${written}}` : written).join("") : pieces[p].trim();
|
|
613
|
+
const visible = body.filter((s) => s.written.trim() !== "");
|
|
614
|
+
if (visible.length > 0) {
|
|
615
|
+
if (lastVisible) {
|
|
616
|
+
const next = new Set(visible[0].marks);
|
|
617
|
+
const shared = lastVisible.marks.filter((mark) => next.has(mark));
|
|
618
|
+
if (shared.length > 0) lastVisible.piece.hyphen = shared;
|
|
619
|
+
}
|
|
620
|
+
lastVisible = {
|
|
621
|
+
piece,
|
|
622
|
+
marks: visible[visible.length - 1].marks
|
|
623
|
+
};
|
|
624
|
+
}
|
|
625
|
+
resolved.push(piece);
|
|
626
|
+
}
|
|
627
|
+
return {
|
|
628
|
+
pieces: resolved,
|
|
629
|
+
problems
|
|
630
|
+
};
|
|
631
|
+
}
|
|
632
|
+
/** The segments with the piece's outer whitespace gone. */
|
|
633
|
+
function trimSegments(segments) {
|
|
634
|
+
const body = segments.map((s) => ({ ...s }));
|
|
635
|
+
const first = body[0];
|
|
636
|
+
const last = body[body.length - 1];
|
|
637
|
+
if (first) first.written = first.written.trimStart();
|
|
638
|
+
if (last) last.written = last.written.trimEnd();
|
|
639
|
+
return body.filter((s) => s.written !== "");
|
|
361
640
|
}
|
|
362
641
|
//#endregion
|
|
363
642
|
//#region src/guidoScript.ts
|
|
@@ -462,6 +741,7 @@ function parseStatement(content) {
|
|
|
462
741
|
case "tight":
|
|
463
742
|
case "keep":
|
|
464
743
|
case "unit":
|
|
744
|
+
case "aid":
|
|
465
745
|
if (args !== "" && args !== "off") throw new Error(`"@${keyword}" takes only "off": "${statement}"`);
|
|
466
746
|
return {
|
|
467
747
|
kind: keyword,
|
|
@@ -478,7 +758,8 @@ function statementSource(tok) {
|
|
|
478
758
|
case "key": return tok.key === "" ? "(@key)" : `(@key ${tok.key})`;
|
|
479
759
|
case "tight":
|
|
480
760
|
case "keep":
|
|
481
|
-
case "unit":
|
|
761
|
+
case "unit":
|
|
762
|
+
case "aid": return tok.on ? `(@${tok.kind})` : `(@${tok.kind} off)`;
|
|
482
763
|
}
|
|
483
764
|
}
|
|
484
765
|
/** The stanza text of a `(@stanza …)` statement's trimmed content, or
|
|
@@ -519,21 +800,29 @@ var GuidoScript = class {
|
|
|
519
800
|
get tokenSpans() {
|
|
520
801
|
return this.parse().spans;
|
|
521
802
|
}
|
|
803
|
+
/** What is wrong with the script's `{…}` spans: such a span renders
|
|
804
|
+
* plainly instead of failing the score. */
|
|
805
|
+
get spanProblems() {
|
|
806
|
+
return this.parse().problems;
|
|
807
|
+
}
|
|
522
808
|
parse() {
|
|
523
809
|
const script = this.normalized;
|
|
524
810
|
const head = script.match(/^\s*\(([^)]*)\)/);
|
|
525
811
|
const skipped = head && isOpeningClef(head[1]) ? head[0].length : 0;
|
|
526
812
|
const matches = [...script.slice(skipped).matchAll(/((?:[^()\\]|\\.)*)\(((?:[^)\\]|\\.)*)\)/g)];
|
|
813
|
+
const resolved = resolveSpans(matches.map((match) => match[1]));
|
|
527
814
|
let prev;
|
|
528
815
|
const stanzas = [];
|
|
529
816
|
const tokens = [];
|
|
530
817
|
const spans = [];
|
|
531
|
-
for (const match of matches) {
|
|
532
|
-
const [,
|
|
818
|
+
for (const [m, match] of matches.entries()) {
|
|
819
|
+
const [, written, notes] = match;
|
|
820
|
+
const piece = resolved.pieces[m];
|
|
821
|
+
const text = piece.bare;
|
|
533
822
|
const at = skipped + match.index;
|
|
534
|
-
const notesStart = at +
|
|
823
|
+
const notesStart = at + written.length + 1;
|
|
535
824
|
const span = {
|
|
536
|
-
start: at + (
|
|
825
|
+
start: at + (written.length - written.trimStart().length),
|
|
537
826
|
notesStart,
|
|
538
827
|
notesEnd: notesStart + notes.length,
|
|
539
828
|
end: notesStart + notes.length + 1
|
|
@@ -559,9 +848,11 @@ var GuidoScript = class {
|
|
|
559
848
|
continue;
|
|
560
849
|
}
|
|
561
850
|
const raw = text.trim();
|
|
851
|
+
if (raw === "" && notes === "" && written.trim() !== "") continue;
|
|
562
852
|
const tok = {
|
|
563
853
|
kind: "syllable",
|
|
564
|
-
|
|
854
|
+
...textFields(raw === "~" ? NO_TEXT : piece),
|
|
855
|
+
role: "sung",
|
|
565
856
|
notes: unescape(notes),
|
|
566
857
|
spaceBefore: /^\s/.test(text)
|
|
567
858
|
};
|
|
@@ -593,10 +884,12 @@ var GuidoScript = class {
|
|
|
593
884
|
}
|
|
594
885
|
holder = t;
|
|
595
886
|
}
|
|
887
|
+
assignRoles(tokens);
|
|
596
888
|
return {
|
|
597
889
|
tokens,
|
|
598
890
|
stanzas,
|
|
599
|
-
spans
|
|
891
|
+
spans,
|
|
892
|
+
problems: resolved.problems
|
|
600
893
|
};
|
|
601
894
|
}
|
|
602
895
|
/** The script broken into syllables, each with its sung text and
|
|
@@ -630,15 +923,19 @@ var GuidoScript = class {
|
|
|
630
923
|
get words() {
|
|
631
924
|
const words = [];
|
|
632
925
|
let word;
|
|
633
|
-
for (const syl of this.
|
|
634
|
-
if (syl.held || syl.empty) continue;
|
|
926
|
+
for (const syl of this.tokens) {
|
|
927
|
+
if (syl.kind !== "syllable" || syl.held || syl.empty) continue;
|
|
635
928
|
word ??= {
|
|
636
929
|
text: "",
|
|
637
930
|
syllables: []
|
|
638
931
|
};
|
|
932
|
+
const { text, source, runs, role, notes } = syl;
|
|
639
933
|
word.syllables.push({
|
|
640
|
-
text
|
|
641
|
-
|
|
934
|
+
text,
|
|
935
|
+
source,
|
|
936
|
+
runs,
|
|
937
|
+
role,
|
|
938
|
+
notes
|
|
642
939
|
});
|
|
643
940
|
word.text = word.syllables.map((s) => s.text).join("");
|
|
644
941
|
if (syl.wordEnd) {
|
|
@@ -653,26 +950,29 @@ var GuidoScript = class {
|
|
|
653
950
|
toString() {
|
|
654
951
|
const parts = this.tokens.map((tok, i) => {
|
|
655
952
|
const prefix = i > 0 && tok.spaceBefore ? " " : "";
|
|
656
|
-
return tok.kind === "syllable" ? `${prefix}${tok.empty ? "~" : tok.
|
|
953
|
+
return tok.kind === "syllable" ? `${prefix}${tok.empty ? "~" : tok.source}(${escape(tok.notes)})` : `${prefix}${statementSource(tok)}`;
|
|
657
954
|
});
|
|
658
955
|
const stanzas = this.stanzas.map((text) => `(@stanza ${text})`);
|
|
659
956
|
return `${this.clef !== void 0 ? `(${this.clef})` : ""}${parts.join("")}${stanzas.join("")}`;
|
|
660
957
|
}
|
|
661
958
|
};
|
|
959
|
+
/** The roles `scriptSyllables` lists by default: what is sung. */
|
|
960
|
+
var SUNG_ROLES = ["sung", "aid"];
|
|
662
961
|
/**
|
|
663
|
-
* The syllables of a script
|
|
664
|
-
*
|
|
665
|
-
* (
|
|
666
|
-
*
|
|
667
|
-
*
|
|
668
|
-
* `composeScript`'s
|
|
962
|
+
* The syllables of a script, in order: by default the sung ones, labels and
|
|
963
|
+
* signs left out (`roles` chooses others). Held groups ride on their
|
|
964
|
+
* syllable (`held`); `~` empty slots, statements and `(@stanza …)` are
|
|
965
|
+
* skipped. Word starts and ends are the listed syllables' own, so a word
|
|
966
|
+
* end on a left-out syllable lands on the one before it. Offsets index the
|
|
967
|
+
* script's source; index i is the "syllable i" of `composeScript`'s
|
|
968
|
+
* `from`/`to`.
|
|
669
969
|
*/
|
|
670
|
-
function scriptSyllables(script) {
|
|
970
|
+
function scriptSyllables(script, options = {}) {
|
|
671
971
|
const parsed = typeof script === "string" ? new GuidoScript(script) : script;
|
|
972
|
+
const roles = options.roles ?? SUNG_ROLES;
|
|
672
973
|
const tokens = parsed.tokens;
|
|
673
974
|
const spans = parsed.tokenSpans;
|
|
674
|
-
const
|
|
675
|
-
let prevWordEnd = true;
|
|
975
|
+
const all = [];
|
|
676
976
|
let holder;
|
|
677
977
|
for (const [i, tok] of tokens.entries()) {
|
|
678
978
|
if (tok.kind !== "syllable") continue;
|
|
@@ -690,9 +990,11 @@ function scriptSyllables(script) {
|
|
|
690
990
|
const span = spans[i];
|
|
691
991
|
holder = {
|
|
692
992
|
text: tok.text,
|
|
993
|
+
source: tok.source,
|
|
994
|
+
role: tok.role,
|
|
693
995
|
notes: tok.notes,
|
|
694
996
|
held: [],
|
|
695
|
-
wordStart:
|
|
997
|
+
wordStart: false,
|
|
696
998
|
wordEnd: tok.wordEnd,
|
|
697
999
|
start: span.start,
|
|
698
1000
|
notesStart: span.notesStart,
|
|
@@ -700,919 +1002,83 @@ function scriptSyllables(script) {
|
|
|
700
1002
|
end: span.end,
|
|
701
1003
|
token: i
|
|
702
1004
|
};
|
|
703
|
-
|
|
704
|
-
prevWordEnd = tok.wordEnd;
|
|
1005
|
+
all.push(holder);
|
|
705
1006
|
}
|
|
706
|
-
|
|
707
|
-
|
|
708
|
-
|
|
709
|
-
|
|
710
|
-
|
|
711
|
-
|
|
712
|
-
}
|
|
713
|
-
|
|
714
|
-
|
|
715
|
-
function unescape(text) {
|
|
716
|
-
return text.replace(/\\(.)/g, "$1");
|
|
717
|
-
}
|
|
718
|
-
/** Turns literal parentheses and backslashes into their escape sequences. */
|
|
719
|
-
function escape(text) {
|
|
720
|
-
return text.replace(/\\/g, "\\\\").replace(/[()]/g, (ch) => `\\${ch}`);
|
|
721
|
-
}
|
|
722
|
-
//#endregion
|
|
723
|
-
//#region src/noteGroups.ts
|
|
724
|
-
/**
|
|
725
|
-
* Note-group anatomy shared by the layout pass and the exact
|
|
726
|
-
* transposition: the score markers and the split of a group into its
|
|
727
|
-
* leading markers, notes and trailing markers. Kept free of imports, so
|
|
728
|
-
* the transforms can use it without pulling in the style sets.
|
|
729
|
-
*/
|
|
730
|
-
/**
|
|
731
|
-
* Score marker characters: they carry no pitch, and when they sit at the
|
|
732
|
-
* start or end of a note group (with the adjacent dash) they act as
|
|
733
|
-
* whitespace rather than note width.
|
|
734
|
-
*/
|
|
735
|
-
var SCORE_MARKERS = ",.:?'\"+!%/=()ÖÜÓ;";
|
|
736
|
-
var MARKER_CHARS$1 = new Set(SCORE_MARKERS.split(""));
|
|
737
|
-
function splitNoteMarkers(notes) {
|
|
738
|
-
let start = 0;
|
|
739
|
-
while (start < notes.length && MARKER_CHARS$1.has(notes[start])) start++;
|
|
740
|
-
if (start > 0 && start < notes.length) start = notes[start] === "-" ? start + 1 : 0;
|
|
741
|
-
let end = notes.length;
|
|
742
|
-
while (end > start && MARKER_CHARS$1.has(notes[end - 1])) end--;
|
|
743
|
-
if (end < notes.length && end > start) end = notes[end - 1] === "-" ? end - 1 : notes.length;
|
|
744
|
-
return {
|
|
745
|
-
leading: notes.slice(0, start),
|
|
746
|
-
core: notes.slice(start, end),
|
|
747
|
-
trailing: notes.slice(end)
|
|
748
|
-
};
|
|
749
|
-
}
|
|
750
|
-
//#endregion
|
|
751
|
-
//#region src/stanzas.ts
|
|
752
|
-
/**
|
|
753
|
-
* Stanzas on one melody (issue #69): a hymn stores its melody once, as
|
|
754
|
-
* stanza 1's GuidoScript, and every further stanza as syllabified text
|
|
755
|
-
* whose syllables map one-to-one onto the melody's note groups — the
|
|
756
|
-
* slots.
|
|
757
|
-
*
|
|
758
|
-
* A slot is one note group: held groups and `~` empty slots included
|
|
759
|
-
* (issue #83), syllables without notes and marker-only groups not. The
|
|
760
|
-
* further stanzas ride the script as `(@stanza …)` statements at its end
|
|
761
|
-
* (`GuidoScript.stanzas`), or come from a `stanzas` option that replaces
|
|
762
|
-
* them. Stanza numbers and slot numbers are 1-based: stanza 1 is the
|
|
763
|
-
* script itself, and they are what the warnings report.
|
|
764
|
-
*
|
|
765
|
-
* Stanza text follows Aretino's lyric-line rules (see `parseStanza`).
|
|
766
|
-
* Rendering stanza k is a script rewrite (`selectStanza`, `withStanza`):
|
|
767
|
-
* its syllables are swapped onto the slots, the notes never change, so
|
|
768
|
-
* every emitter (DOM, image, Word) works on the result unchanged.
|
|
769
|
-
*/
|
|
770
|
-
/** The syllable text that marks a slot a stanza is missing a syllable
|
|
771
|
-
* for: a rubric ellipsis, red in BNS, so the mismatch shows in the
|
|
772
|
-
* render (and in the Word export). */
|
|
773
|
-
var STANZA_PLACEHOLDER = "_…_";
|
|
774
|
-
/** The unescaped characters that end a syllable within a word: the
|
|
775
|
-
* hyphen, the mandatory hyphen and the soft hyphen. */
|
|
776
|
-
var SYLLABLE_BREAKS = "-=";
|
|
777
|
-
/**
|
|
778
|
-
* Parses stanza text into its slot items, by Aretino's lyric-line rules:
|
|
779
|
-
*
|
|
780
|
-
* - whitespace separates words, `-` separates the syllables of a word
|
|
781
|
-
* (a soft hyphen U+00AD counts as `-`, and so does the mandatory
|
|
782
|
-
* hyphen `=` — the renderer has no hyphen it may not drop yet);
|
|
783
|
-
* - a lone `-` (or an empty syllable, `Al--le`) holds the slot for the
|
|
784
|
-
* syllable before it; that syllable's word end is its own (`Al- - le`
|
|
785
|
-
* is mid-word, `bunt - gen` ends the word);
|
|
786
|
-
* - a lone `~` is an empty slot; `~` inside a syllable is a
|
|
787
|
-
* non-breaking space;
|
|
788
|
-
* - a backslash makes the next character literal (`\-`, `\~`, `\=`,
|
|
789
|
-
* `\(`, `\\`).
|
|
790
|
-
*
|
|
791
|
-
* There are no `_` extenders: `_…_`, `__…__` and `*…*` stay the
|
|
792
|
-
* syllable formatting of the script notation.
|
|
793
|
-
*/
|
|
794
|
-
function parseStanza(text) {
|
|
795
|
-
const words = [];
|
|
796
|
-
let word = null;
|
|
797
|
-
const chars = Array.from(text);
|
|
798
|
-
for (let i = 0; i < chars.length; i++) {
|
|
799
|
-
let ch = chars[i];
|
|
800
|
-
const escaped = ch === "\\" && i + 1 < chars.length;
|
|
801
|
-
if (escaped) ch = chars[++i];
|
|
802
|
-
if (!escaped && /\s/.test(ch)) {
|
|
803
|
-
word = null;
|
|
804
|
-
continue;
|
|
805
|
-
}
|
|
806
|
-
if (word === null) {
|
|
807
|
-
word = [];
|
|
808
|
-
words.push(word);
|
|
809
|
-
}
|
|
810
|
-
word.push({
|
|
811
|
-
ch,
|
|
812
|
-
escaped
|
|
813
|
-
});
|
|
814
|
-
}
|
|
815
|
-
const out = [];
|
|
816
|
-
for (const w of words) {
|
|
817
|
-
if (w.length === 1 && !w[0].escaped && w[0].ch === "~") {
|
|
818
|
-
out.push({ kind: "empty" });
|
|
819
|
-
continue;
|
|
820
|
-
}
|
|
821
|
-
const pieces = [""];
|
|
822
|
-
for (const { ch, escaped } of w) if (!escaped && SYLLABLE_BREAKS.includes(ch)) pieces.push("");
|
|
823
|
-
else pieces[pieces.length - 1] += !escaped && ch === "~" ? "\xA0" : ch;
|
|
824
|
-
const last = pieces.length - 1;
|
|
825
|
-
for (const [p, piece] of pieces.entries()) {
|
|
826
|
-
if (piece === "") {
|
|
827
|
-
if (p < last) out.push({ kind: "hold" });
|
|
828
|
-
continue;
|
|
829
|
-
}
|
|
830
|
-
out.push({
|
|
831
|
-
kind: "syllable",
|
|
832
|
-
text: piece,
|
|
833
|
-
wordEnd: p === last
|
|
834
|
-
});
|
|
835
|
-
}
|
|
836
|
-
}
|
|
837
|
-
return out;
|
|
838
|
-
}
|
|
839
|
-
/** Whether a token opens a slot: a syllable with notes that are not all
|
|
840
|
-
* markers. */
|
|
841
|
-
function isSlot(tok) {
|
|
842
|
-
return tok.kind === "syllable" && tok.notes !== "" && splitNoteMarkers(tok.notes).core !== "";
|
|
843
|
-
}
|
|
844
|
-
function parsed(script) {
|
|
845
|
-
return typeof script === "string" ? new GuidoScript(script) : script;
|
|
846
|
-
}
|
|
847
|
-
/** The number of slots (note groups that take a stanza syllable). */
|
|
848
|
-
function slotCount(script) {
|
|
849
|
-
return parsed(script).tokens.filter(isSlot).length;
|
|
850
|
-
}
|
|
851
|
-
/** The stanza texts of stanzas 2, 3, …: the `stanzas` option when
|
|
852
|
-
* given, the script's `(@stanza …)` statements otherwise. */
|
|
853
|
-
function stanzaTexts(script, options = {}) {
|
|
854
|
-
return options.stanzas ?? parsed(script).stanzas;
|
|
855
|
-
}
|
|
856
|
-
/** How many stanzas the script has, stanza 1 (the script) included. */
|
|
857
|
-
function stanzaCount(script, options = {}) {
|
|
858
|
-
return stanzaTexts(script, options).length + 1;
|
|
859
|
-
}
|
|
860
|
-
function sings(fill) {
|
|
861
|
-
return fill.kind === "syllable" || fill.kind === "hold";
|
|
862
|
-
}
|
|
863
|
-
/** Stanza 1's fills: the slot tokens themselves. */
|
|
864
|
-
function scriptFills(slots) {
|
|
865
|
-
return slots.map((tok) => tok.empty ? { kind: "empty" } : tok.held ? { kind: "hold" } : {
|
|
866
|
-
kind: "syllable",
|
|
867
|
-
text: tok.text,
|
|
868
|
-
wordEnd: tok.wordEnd
|
|
869
|
-
});
|
|
870
|
-
}
|
|
871
|
-
/** A stanza's syllables laid on `count` slots, with its mismatches. */
|
|
872
|
-
function alignStanza(text, count, stanza) {
|
|
873
|
-
const syllables = parseStanza(text);
|
|
874
|
-
const fills = [];
|
|
875
|
-
const warnings = [];
|
|
876
|
-
for (let i = 0; i < count; i++) {
|
|
877
|
-
const syl = syllables[i];
|
|
878
|
-
fills.push(syl ?? { kind: "missing" });
|
|
879
|
-
if (syl === void 0) warnings.push({
|
|
880
|
-
stanza,
|
|
881
|
-
slot: i + 1,
|
|
882
|
-
kind: "missing",
|
|
883
|
-
message: `Stanza ${stanza} has no syllable for slot ${i + 1} of ${count}`
|
|
884
|
-
});
|
|
885
|
-
}
|
|
886
|
-
for (const [i, syl] of syllables.slice(count).entries()) {
|
|
887
|
-
const shown = syl.kind === "syllable" ? syl.text : syl.kind === "hold" ? "-" : "~";
|
|
888
|
-
warnings.push({
|
|
889
|
-
stanza,
|
|
890
|
-
slot: count + i + 1,
|
|
891
|
-
kind: "extra",
|
|
892
|
-
text: shown,
|
|
893
|
-
message: `Stanza ${stanza} has an extra syllable "${shown}" past the last slot (${count})`
|
|
894
|
-
});
|
|
895
|
-
}
|
|
896
|
-
return {
|
|
897
|
-
fills,
|
|
898
|
-
warnings
|
|
899
|
-
};
|
|
900
|
-
}
|
|
901
|
-
/** Throws when `stanza` is not one of the script's stanzas. */
|
|
902
|
-
function checkStanzaNumber(stanza, count) {
|
|
903
|
-
if (!Number.isInteger(stanza) || stanza < 1 || stanza > count) throw new RangeError(`Stanza ${stanza} does not exist: the script has ${count} stanza${count === 1 ? "" : "s"}`);
|
|
904
|
-
}
|
|
905
|
-
/** The count mismatches of one stanza (`options.stanza`), or of every
|
|
906
|
-
* stanza when none is named. Stanza 1 is the script and never warns;
|
|
907
|
-
* an intentional `~` is a syllable like any other. */
|
|
908
|
-
function stanzaWarnings(script, options = {}) {
|
|
909
|
-
const texts = stanzaTexts(script, options);
|
|
910
|
-
const count = slotCount(script);
|
|
911
|
-
if (options.stanza !== void 0) {
|
|
912
|
-
checkStanzaNumber(options.stanza, texts.length + 1);
|
|
913
|
-
if (options.stanza === 1) return [];
|
|
914
|
-
return alignStanza(texts[options.stanza - 2], count, options.stanza).warnings;
|
|
915
|
-
}
|
|
916
|
-
return texts.flatMap((text, i) => alignStanza(text, count, i + 2).warnings);
|
|
917
|
-
}
|
|
918
|
-
/** Pads stanza text with `~` empty slots up to the script's slot count
|
|
919
|
-
* (unchanged when it already has enough syllables). */
|
|
920
|
-
function padStanza(script, text) {
|
|
921
|
-
const missing = slotCount(script) - parseStanza(text).length;
|
|
922
|
-
return missing > 0 ? `${text.trimEnd()}${" ~".repeat(missing)}` : text;
|
|
923
|
-
}
|
|
924
|
-
/** A note group with its plain heads, virgas and longas swapped for small
|
|
925
|
-
* heads (connectors, markers and signs stay). */
|
|
926
|
-
function smallHeads(notes) {
|
|
927
|
-
return Array.from(notes, (ch) => {
|
|
928
|
-
const small = toSmallHead(ch);
|
|
929
|
-
if (small !== void 0) return small;
|
|
930
|
-
const variant = baseOf(ch);
|
|
931
|
-
if (variant && (variant.role === "virga" || variant.role === "longa")) return toSmallHead(variant.base) ?? ch;
|
|
932
|
-
return ch;
|
|
933
|
-
}).join("");
|
|
934
|
-
}
|
|
935
|
-
/** A syllable's text written back into the script notation. */
|
|
936
|
-
function syllableSource(text) {
|
|
937
|
-
return text === "~" ? "\\~" : escape(text);
|
|
938
|
-
}
|
|
939
|
-
/** Rewrites a script's source to sing stanza `stanza`. */
|
|
940
|
-
function stanzaSource(source, stanza, options) {
|
|
941
|
-
const script = new GuidoScript(source);
|
|
942
|
-
const texts = stanzaTexts(script, options);
|
|
943
|
-
checkStanzaNumber(stanza, texts.length + 1);
|
|
944
|
-
const rendered = options.rendered ?? [stanza];
|
|
945
|
-
for (const k of rendered) checkStanzaNumber(k, texts.length + 1);
|
|
946
|
-
const mode = options.emptySlots ?? "normal";
|
|
947
|
-
if (stanza === 1 && mode === "normal") return source;
|
|
948
|
-
const tokens = script.tokens;
|
|
949
|
-
const slotTokens = tokens.filter((t) => isSlot(t));
|
|
950
|
-
const allFills = [scriptFills(slotTokens), ...texts.map((text, i) => alignStanza(text, slotTokens.length, i + 2).fills)];
|
|
951
|
-
const fills = allFills[stanza - 1];
|
|
952
|
-
const cue = (slot) => mode === "partial" ? allFills.some((f) => sings(f[slot])) && !allFills.every((f) => sings(f[slot])) : mode === "small" && rendered.every((k) => !sings(allFills[k - 1][slot])) && allFills.some((f, j) => !rendered.includes(j + 1) && sings(f[slot]));
|
|
953
|
-
let out = script.clef !== void 0 ? `(${script.clef})` : "";
|
|
954
|
-
let first = true;
|
|
955
|
-
let pendingSpace = false;
|
|
956
|
-
let canHold = false;
|
|
957
|
-
const emit = (piece, space) => {
|
|
958
|
-
out += (space && !first ? " " : "") + piece;
|
|
959
|
-
first = false;
|
|
960
|
-
};
|
|
961
|
-
let slot = 0;
|
|
962
|
-
for (const tok of tokens) {
|
|
963
|
-
if (tok.kind !== "syllable") {
|
|
964
|
-
emit(statementSource(tok), pendingSpace);
|
|
965
|
-
pendingSpace = false;
|
|
966
|
-
continue;
|
|
967
|
-
}
|
|
968
|
-
if (!isSlot(tok)) {
|
|
969
|
-
if (tok.held) {
|
|
970
|
-
emit(`(${escape(tok.notes)})`, pendingSpace);
|
|
971
|
-
pendingSpace = false;
|
|
972
|
-
} else {
|
|
973
|
-
emit(`${syllableSource(tok.text)}(${escape(tok.notes)})`, pendingSpace || tok.spaceBefore);
|
|
974
|
-
pendingSpace = tok.wordEnd;
|
|
975
|
-
canHold = tok.notes !== "";
|
|
976
|
-
}
|
|
977
|
-
continue;
|
|
978
|
-
}
|
|
979
|
-
const fill = fills[slot];
|
|
980
|
-
const notes = escape(cue(slot) ? smallHeads(tok.notes) : tok.notes);
|
|
981
|
-
slot++;
|
|
982
|
-
switch (fill.kind) {
|
|
983
|
-
case "syllable":
|
|
984
|
-
emit(`${syllableSource(fill.text)}(${notes})`, pendingSpace);
|
|
985
|
-
pendingSpace = fill.wordEnd;
|
|
986
|
-
break;
|
|
987
|
-
case "hold":
|
|
988
|
-
emit(`${canHold ? "" : "~"}(${notes})`, false);
|
|
989
|
-
break;
|
|
990
|
-
case "empty":
|
|
991
|
-
emit(`~(${notes})`, false);
|
|
992
|
-
break;
|
|
993
|
-
case "missing":
|
|
994
|
-
emit(`${STANZA_PLACEHOLDER}(${notes})`, true);
|
|
995
|
-
pendingSpace = true;
|
|
996
|
-
}
|
|
997
|
-
canHold = true;
|
|
998
|
-
}
|
|
999
|
-
return out;
|
|
1000
|
-
}
|
|
1001
|
-
/** The `selectStanza` transform factory: rewrites the script to sing
|
|
1002
|
-
* stanza `stanza` (1-based; 1 is the script itself). Its syllables go on
|
|
1003
|
-
* the slots in order — missing ones become `STANZA_PLACEHOLDER`
|
|
1004
|
-
* syllables, extra ones are dropped (`stanzaWarnings` reports both) — and
|
|
1005
|
-
* the `(@stanza …)` statements are dropped. Everything that is not a
|
|
1006
|
-
* slot (statements, syllables without notes, marker-only groups) stays as
|
|
1007
|
-
* written. With `emptySlots: 'small'`, a slot the stanza leaves empty
|
|
1008
|
-
* while another stanza sings it gets small heads (`rendered` lists the
|
|
1009
|
-
* stanzas stacked with it: then a slot gets small heads when none of
|
|
1010
|
-
* them sings it while another stanza does). With `emptySlots:
|
|
1011
|
-
* 'partial'`, every slot some stanza sings and another does not gets
|
|
1012
|
-
* small heads, whatever is rendered. Stanza 1 with normal
|
|
1013
|
-
* empty slots returns the source unchanged. Throws a RangeError for a
|
|
1014
|
-
* stanza the script does not have. Register it first, so the other
|
|
1015
|
-
* transforms (the initial, the transpositions) see the stanza's text. */
|
|
1016
|
-
function selectStanza(stanza, options = {}) {
|
|
1017
|
-
return {
|
|
1018
|
-
name: "selectStanza",
|
|
1019
|
-
apply: (source) => stanzaSource(source, stanza, options)
|
|
1020
|
-
};
|
|
1021
|
-
}
|
|
1022
|
-
/** The script singing stanza `stanza` (see `selectStanza`); `transforms`
|
|
1023
|
-
* run after the stanza swap. A GuidoScript argument is taken by its
|
|
1024
|
-
* (already transformed) source. */
|
|
1025
|
-
function withStanza(script, stanza, options = {}) {
|
|
1026
|
-
return new GuidoScript(typeof script === "string" ? script : script.source, { transforms: [selectStanza(stanza, options), ...options.transforms ?? []] });
|
|
1027
|
-
}
|
|
1028
|
-
/** The stanza numbers a stack names: every stanza for `'all'`, the
|
|
1029
|
-
* listed ones otherwise. Throws a RangeError for an empty list or a
|
|
1030
|
-
* stanza the script does not have. */
|
|
1031
|
-
function stackedStanzaNumbers(script, stack, options = {}) {
|
|
1032
|
-
const count = stanzaCount(script, options);
|
|
1033
|
-
if (stack === "all") return Array.from({ length: count }, (_, k) => k + 1);
|
|
1034
|
-
if (stack.length === 0) throw new RangeError("No stanza to stack: the list is empty");
|
|
1035
|
-
for (const k of stack) checkStanzaNumber(k, count);
|
|
1036
|
-
return [...stack];
|
|
1037
|
-
}
|
|
1038
|
-
/** The scripts of stacked stanzas (issue #84): one per stanza of the
|
|
1039
|
-
* stack, in its order, each singing its stanza on the shared notes (see
|
|
1040
|
-
* `withStanza`; `transforms` run after the swap). Hand them to the layout
|
|
1041
|
-
* together (`layoutScore`, `GuidoRender`, the Word export) for one text
|
|
1042
|
-
* line per stanza under one staff. With `emptySlots: 'small'`, a slot
|
|
1043
|
-
* gets small heads when no stacked stanza sings it while a stanza left
|
|
1044
|
-
* out of the stack does, so every script carries the same notes. */
|
|
1045
|
-
function stackStanzas(script, stack, options = {}) {
|
|
1046
|
-
const rendered = stackedStanzaNumbers(script, stack, options);
|
|
1047
|
-
return rendered.map((k) => withStanza(script, k, {
|
|
1048
|
-
...options,
|
|
1049
|
-
rendered
|
|
1050
|
-
}));
|
|
1051
|
-
}
|
|
1052
|
-
//#endregion
|
|
1053
|
-
//#region src/exactTranspose.ts
|
|
1054
|
-
/**
|
|
1055
|
-
* Exact transposition (issue #70): moves the sounding pitches of a
|
|
1056
|
-
* script and re-notates them under a target clef and key, so the result
|
|
1057
|
-
* reads like a chant notated at the new pitch. The staff-degree shift
|
|
1058
|
-
* (`transposeBy`) stays separate and ignores pitch meaning.
|
|
1059
|
-
*
|
|
1060
|
-
* A note's sounding pitch is its staff position + the clef in effect +
|
|
1061
|
-
* the key in effect + any sign written in its group. A sign affects its
|
|
1062
|
-
* whole group: every note of that pitch letter, in every octave. The
|
|
1063
|
-
* transposition is two steps:
|
|
1064
|
-
*
|
|
1065
|
-
* 1. Move every pitch by a spelled interval (letter steps + semitones,
|
|
1066
|
-
* so a major third up turns b♭ into d, not e♭♭).
|
|
1067
|
-
* 2. Re-notate under the target clef and key: each note's staff position
|
|
1068
|
-
* and glyph, plus the signs the target key doesn't cover. Added signs
|
|
1069
|
-
* go before the note group, after its leading markers (the #49 rule).
|
|
1070
|
-
*
|
|
1071
|
-
* Keys are written with the classical key-signature layout
|
|
1072
|
-
* (`KEY_SIGNATURES`). Results past the glyph range and missing glyph
|
|
1073
|
-
* variants are errors, never silent clamps.
|
|
1074
|
-
*/
|
|
1075
|
-
/** A transposed note past the end of the glyph range. Thrown as its own
|
|
1076
|
-
* class so the octave search for a `final` can tell it apart. */
|
|
1077
|
-
var TranspositionRangeError = class extends Error {
|
|
1078
|
-
constructor(message) {
|
|
1079
|
-
super(message);
|
|
1080
|
-
this.name = "TranspositionRangeError";
|
|
1007
|
+
const listed = [];
|
|
1008
|
+
let atWordStart = true;
|
|
1009
|
+
for (const syl of all) if (roles.includes(syl.role)) {
|
|
1010
|
+
syl.wordStart = atWordStart;
|
|
1011
|
+
listed.push(syl);
|
|
1012
|
+
atWordStart = syl.wordEnd;
|
|
1013
|
+
} else if (syl.wordEnd) {
|
|
1014
|
+
if (listed.length > 0) listed[listed.length - 1].wordEnd = true;
|
|
1015
|
+
atWordStart = true;
|
|
1081
1016
|
}
|
|
1017
|
+
return listed;
|
|
1018
|
+
}
|
|
1019
|
+
/** The text of the script to read: the sung words joined with single
|
|
1020
|
+
* spaces, with no markup, labels, signs or aids in them. Characters the
|
|
1021
|
+
* author escaped stay. */
|
|
1022
|
+
function chantText(script, options = {}) {
|
|
1023
|
+
const roles = ["sung"];
|
|
1024
|
+
if (options.labels) roles.push("label");
|
|
1025
|
+
if (options.aids) roles.push("aid");
|
|
1026
|
+
return scriptSyllables(script, { roles }).map((syl) => (syl.wordStart ? " " : "") + syl.text).join("").replace(/ +/g, " ").trim();
|
|
1027
|
+
}
|
|
1028
|
+
/** A text with nothing in it. */
|
|
1029
|
+
var NO_TEXT = {
|
|
1030
|
+
bare: "",
|
|
1031
|
+
source: "",
|
|
1032
|
+
runs: [],
|
|
1033
|
+
text: ""
|
|
1082
1034
|
};
|
|
1083
|
-
/**
|
|
1084
|
-
|
|
1085
|
-
|
|
1086
|
-
* is the first n glyphs of its row. */
|
|
1087
|
-
var KEY_SIGNATURES = {
|
|
1088
|
-
M: {
|
|
1089
|
-
flats: "XBYVÍCS",
|
|
1090
|
-
sharps: "ôþùßÑòÒ"
|
|
1091
|
-
},
|
|
1092
|
-
"<": {
|
|
1093
|
-
flats: "XBYVÍCS",
|
|
1094
|
-
sharps: "ôþùßÑòÒ"
|
|
1095
|
-
},
|
|
1096
|
-
">": {
|
|
1097
|
-
flats: "V$CNXBY",
|
|
1098
|
-
sharps: "ÑòÒôþùß"
|
|
1099
|
-
},
|
|
1100
|
-
"#": {
|
|
1101
|
-
flats: "NXBYVÍC",
|
|
1102
|
-
sharps: "þÐßÑÎÒÏ"
|
|
1103
|
-
},
|
|
1104
|
-
"&": {
|
|
1105
|
-
flats: "YVÍCSXA",
|
|
1106
|
-
sharps: "òÒôþÐßÑ"
|
|
1107
|
-
},
|
|
1108
|
-
"@": {
|
|
1109
|
-
flats: "CNXBYVÍ",
|
|
1110
|
-
sharps: "ÐßÑòÒôþ"
|
|
1111
|
-
},
|
|
1112
|
-
"{": {
|
|
1113
|
-
flats: "BYVÍCSX",
|
|
1114
|
-
sharps: "ÒÏþÐÌÑÎ"
|
|
1115
|
-
},
|
|
1116
|
-
"}": {
|
|
1117
|
-
flats: "ÍCSXAYa",
|
|
1118
|
-
sharps: "ßÑòÒÏþÐ"
|
|
1119
|
-
}
|
|
1120
|
-
};
|
|
1121
|
-
var FLAT_ORDER = "beadgcf";
|
|
1122
|
-
var SHARP_ORDER = "fcgdaeb";
|
|
1123
|
-
/** Pitch letters from c, so a diatonic step's letter is `step mod 7`. */
|
|
1124
|
-
var LETTERS = "cdefgab";
|
|
1125
|
-
var LETTER_SEMITONES = [
|
|
1126
|
-
0,
|
|
1127
|
-
2,
|
|
1128
|
-
4,
|
|
1129
|
-
5,
|
|
1130
|
-
7,
|
|
1131
|
-
9,
|
|
1132
|
-
11
|
|
1133
|
-
];
|
|
1134
|
-
var mod7 = (n) => (n % 7 + 7) % 7;
|
|
1135
|
-
function semitoneOf({ step, alter }) {
|
|
1136
|
-
return Math.floor(step / 7) * 12 + LETTER_SEMITONES[mod7(step)] + alter;
|
|
1137
|
-
}
|
|
1138
|
-
function letterName(step) {
|
|
1139
|
-
return LETTERS[mod7(step)];
|
|
1140
|
-
}
|
|
1141
|
-
/** A pitch moved by a spelled interval: the letter moves by the steps,
|
|
1142
|
-
* the alteration absorbs whatever the semitones leave over. */
|
|
1143
|
-
function movePitch(pitch, interval) {
|
|
1144
|
-
const step = pitch.step + interval.steps;
|
|
1145
|
-
const alter = semitoneOf(pitch) + interval.semitones - semitoneOf({
|
|
1146
|
-
step,
|
|
1147
|
-
alter: 0
|
|
1148
|
-
});
|
|
1149
|
-
if (Math.abs(alter) > 1) throw new Error(`Moving ${letterName(pitch.step)}${alterName(pitch.alter)} lands on a double accidental (${letterName(step)}, ${alter > 0 ? "+" : ""}${alter})`);
|
|
1150
|
-
return {
|
|
1151
|
-
step,
|
|
1152
|
-
alter
|
|
1153
|
-
};
|
|
1154
|
-
}
|
|
1155
|
-
function alterName(alter) {
|
|
1156
|
-
return alter < 0 ? "♭" : alter > 0 ? "♯" : "";
|
|
1157
|
-
}
|
|
1158
|
-
function roleOfAlter(alter) {
|
|
1159
|
-
return alter < 0 ? "modifyDown" : alter > 0 ? "modifyUp" : "modifyNatural";
|
|
1160
|
-
}
|
|
1161
|
-
function alterOfRole(role) {
|
|
1162
|
-
return role === "modifyDown" ? -1 : role === "modifyUp" ? 1 : 0;
|
|
1163
|
-
}
|
|
1164
|
-
/** The staff position of a clef's own line and the step of its pitch. */
|
|
1165
|
-
function clefAnchor(clef) {
|
|
1166
|
-
const entry = CLEF_GLYPHS[clef];
|
|
1167
|
-
const step = {
|
|
1168
|
-
c: 28,
|
|
1169
|
-
f: 24,
|
|
1170
|
-
g: 32
|
|
1171
|
-
}[entry.clef];
|
|
1035
|
+
/** A syllable's text fields, from its resolved text. */
|
|
1036
|
+
function textFields(piece) {
|
|
1037
|
+
const { text, source, runs, hyphen } = piece;
|
|
1172
1038
|
return {
|
|
1173
|
-
|
|
1174
|
-
|
|
1039
|
+
text,
|
|
1040
|
+
source,
|
|
1041
|
+
runs,
|
|
1042
|
+
...hyphen ? { hyphen } : {}
|
|
1175
1043
|
};
|
|
1176
1044
|
}
|
|
1177
|
-
|
|
1178
|
-
|
|
1179
|
-
return
|
|
1180
|
-
}
|
|
1181
|
-
|
|
1182
|
-
|
|
1183
|
-
|
|
1184
|
-
|
|
1185
|
-
|
|
1186
|
-
|
|
1187
|
-
|
|
1188
|
-
|
|
1189
|
-
|
|
1190
|
-
|
|
1191
|
-
|
|
1192
|
-
const
|
|
1193
|
-
|
|
1194
|
-
|
|
1195
|
-
|
|
1196
|
-
|
|
1197
|
-
}
|
|
1198
|
-
/** Whether `ch` is in a pitch row. Read at call time: this module and
|
|
1199
|
-
* the transforms module import each other. */
|
|
1200
|
-
function isPitchChar(ch) {
|
|
1201
|
-
return PITCH_ROWS.some((row) => row.includes(ch));
|
|
1202
|
-
}
|
|
1203
|
-
/** The glyph of `role` at a staff position. Throws past the glyph range
|
|
1204
|
-
* and when the variant does not exist. */
|
|
1205
|
-
function glyphAt(position, role, what) {
|
|
1206
|
-
const base = STAFF[position];
|
|
1207
|
-
if (base === void 0) throw new TranspositionRangeError(`${what} runs past the glyph range`);
|
|
1208
|
-
const glyph = role === "base" ? base : variantOf(base, role);
|
|
1209
|
-
if (glyph === void 0) throw new Error(`${what}: the glyph "${base}" has no ${role} variant`);
|
|
1210
|
-
return glyph;
|
|
1211
|
-
}
|
|
1212
|
-
/** A key read from modifier glyphs under `clef`. Naturals carry no
|
|
1213
|
-
* alteration, so they drop out. */
|
|
1214
|
-
function readKey(glyphs, clef) {
|
|
1215
|
-
const key = /* @__PURE__ */ new Map();
|
|
1216
|
-
for (const ch of glyphs) {
|
|
1217
|
-
if (!isModifierGlyph(ch)) continue;
|
|
1218
|
-
const { base, role } = baseOf(ch);
|
|
1219
|
-
const letter = mod7(stepAt(STAFF.indexOf(base), clef));
|
|
1220
|
-
const alter = alterOfRole(role);
|
|
1221
|
-
if (alter === 0) key.delete(letter);
|
|
1222
|
-
else key.set(letter, alter);
|
|
1223
|
-
}
|
|
1224
|
-
return key;
|
|
1225
|
-
}
|
|
1226
|
-
/** A signed key count: −n is the first n flats, n the first n sharps. */
|
|
1227
|
-
function countedKey(count) {
|
|
1228
|
-
if (!Number.isInteger(count) || Math.abs(count) > 7) throw new Error(`A key has −7 to 7 signs, not ${count}`);
|
|
1229
|
-
const order = count < 0 ? FLAT_ORDER : SHARP_ORDER;
|
|
1230
|
-
const key = /* @__PURE__ */ new Map();
|
|
1231
|
-
for (const name of order.slice(0, Math.abs(count))) key.set(LETTERS.indexOf(name), Math.sign(count));
|
|
1232
|
-
return key;
|
|
1233
|
-
}
|
|
1234
|
-
/** A key moved by an interval. The key is a scale, not a set of
|
|
1235
|
-
* pitches: every letter moves (d minor's natural e becomes g minor's
|
|
1236
|
-
* e♭ a fourth up), and the moved key is whatever the scale alters. */
|
|
1237
|
-
function moveKey(key, interval) {
|
|
1238
|
-
const moved = /* @__PURE__ */ new Map();
|
|
1239
|
-
for (let letter = 0; letter < 7; letter++) {
|
|
1240
|
-
const pitch = movePitch({
|
|
1241
|
-
step: letter,
|
|
1242
|
-
alter: key.get(letter) ?? 0
|
|
1243
|
-
}, interval);
|
|
1244
|
-
if (pitch.alter !== 0) moved.set(mod7(pitch.step), pitch.alter);
|
|
1245
|
-
}
|
|
1246
|
-
return moved;
|
|
1247
|
-
}
|
|
1248
|
-
function sameKey(a, b) {
|
|
1249
|
-
return a.size === b.size && [...a].every(([letter, alter]) => b.get(letter) === alter);
|
|
1250
|
-
}
|
|
1251
|
-
/** Whether two keys written as modifier glyphs under `clef` alter the
|
|
1252
|
-
* same pitch letters the same way (glyph order and octave aside). */
|
|
1253
|
-
function sameKeyGlyphs(a, b, clef) {
|
|
1254
|
-
return sameKey(readKey(a, clef), readKey(b, clef));
|
|
1255
|
-
}
|
|
1256
|
-
/** A key written with the key-signature layout of `clef`: flats first,
|
|
1257
|
-
* then sharps, each in the table's order. */
|
|
1258
|
-
function writeKey(key, clef) {
|
|
1259
|
-
const row = KEY_SIGNATURES[clef];
|
|
1260
|
-
const glyphs = (order, alter, signs) => [...order].map((name, i) => key.get(LETTERS.indexOf(name)) === alter ? signs[i] : "").join("");
|
|
1261
|
-
return glyphs(FLAT_ORDER, -1, row.flats) + glyphs(SHARP_ORDER, 1, row.sharps);
|
|
1262
|
-
}
|
|
1263
|
-
/** Parses an interval name: an optional sign, a quality (`P`, `M`, `m`,
|
|
1264
|
-
* `A`, `d`) and a number (`P1`, `-P5`, `M3`, `A4`, `P8`, `M9`). */
|
|
1265
|
-
function parseInterval(name) {
|
|
1266
|
-
const match = name.trim().match(/^([+-]?)([PMmAd])(\d+)$/);
|
|
1267
|
-
const number = match ? Number(match[3]) : 0;
|
|
1268
|
-
if (!match || number < 1) throw new Error(`Unknown interval "${name}"`);
|
|
1269
|
-
const [, sign, quality] = match;
|
|
1270
|
-
const steps = number - 1;
|
|
1271
|
-
const perfect = [
|
|
1272
|
-
0,
|
|
1273
|
-
3,
|
|
1274
|
-
4
|
|
1275
|
-
].includes(steps % 7);
|
|
1276
|
-
if (!(perfect ? "PAd" : "MmAd").includes(quality)) throw new Error(`Unknown interval "${name}"`);
|
|
1277
|
-
const base = Math.floor(steps / 7) * 12 + LETTER_SEMITONES[steps % 7];
|
|
1278
|
-
const adjust = {
|
|
1279
|
-
P: 0,
|
|
1280
|
-
M: 0,
|
|
1281
|
-
A: 1,
|
|
1282
|
-
m: -1,
|
|
1283
|
-
d: perfect ? -1 : -2
|
|
1284
|
-
}[quality];
|
|
1285
|
-
const direction = sign === "-" ? -1 : 1;
|
|
1286
|
-
return {
|
|
1287
|
-
steps: direction * steps,
|
|
1288
|
-
semitones: direction * (base + adjust)
|
|
1289
|
-
};
|
|
1290
|
-
}
|
|
1291
|
-
var GROUP = /((?:[^()\\]|\\.)*)\(((?:[^)\\]|\\.)*)\)/g;
|
|
1292
|
-
function escapeNotes(notes) {
|
|
1293
|
-
return notes.replace(/\\/g, "\\\\").replace(/[()]/g, (ch) => `\\${ch}`);
|
|
1294
|
-
}
|
|
1295
|
-
/** Re-notates one note group. `src*` are the clef and key in effect in
|
|
1296
|
-
* the source, `tgt*` the ones in the result. Records the group's last
|
|
1297
|
-
* note pitch (the candidate final) through `onNote`. */
|
|
1298
|
-
function renotateGroup(notes, interval, srcClef, srcKey, tgtClef, tgtKey, onNote) {
|
|
1299
|
-
const { leading, core, trailing } = splitNoteMarkers(notes);
|
|
1300
|
-
const chars = [...core];
|
|
1301
|
-
const groupSigns = /* @__PURE__ */ new Map();
|
|
1302
|
-
for (const ch of chars) {
|
|
1303
|
-
if (!isModifierGlyph(ch)) continue;
|
|
1304
|
-
const glyph = glyphOf(ch);
|
|
1305
|
-
const letter = mod7(stepAt(glyph.position, srcClef));
|
|
1306
|
-
const alter = alterOfRole(glyph.role);
|
|
1307
|
-
if (groupSigns.has(letter) && groupSigns.get(letter) !== alter) throw new Error(`The note group "${notes}" carries two different signs for ${LETTERS[letter]}`);
|
|
1308
|
-
groupSigns.set(letter, alter);
|
|
1309
|
-
}
|
|
1310
|
-
const coveredLetters = /* @__PURE__ */ new Set();
|
|
1311
|
-
const out = [];
|
|
1312
|
-
const added = [];
|
|
1313
|
-
const pending = [];
|
|
1314
|
-
for (const ch of chars) {
|
|
1315
|
-
const glyph = glyphOf(ch);
|
|
1316
|
-
if (!glyph) {
|
|
1317
|
-
if (isPitchChar(ch)) throw new Error(`The glyph "${ch}" has no glyph-family entry, so it cannot be transposed exactly`);
|
|
1318
|
-
out.push(ch);
|
|
1319
|
-
continue;
|
|
1320
|
-
}
|
|
1321
|
-
const step = stepAt(glyph.position, srcClef);
|
|
1322
|
-
const letter = mod7(step);
|
|
1323
|
-
const keyAlter = srcKey.get(letter) ?? 0;
|
|
1324
|
-
if (isModifierGlyph(ch)) {
|
|
1325
|
-
const alter = alterOfRole(glyph.role);
|
|
1326
|
-
const moved = movePitch({
|
|
1327
|
-
step,
|
|
1328
|
-
alter
|
|
1329
|
-
}, interval);
|
|
1330
|
-
const tgtAlter = tgtKey.get(mod7(moved.step)) ?? 0;
|
|
1331
|
-
if (moved.alter !== tgtAlter || alter === keyAlter) {
|
|
1332
|
-
const position = positionOf(moved.step, tgtClef);
|
|
1333
|
-
out.push(glyphAt(position, roleOfAlter(moved.alter), `The sign "${ch}"`));
|
|
1334
|
-
coveredLetters.add(mod7(moved.step));
|
|
1335
|
-
}
|
|
1045
|
+
/** A syllable's text fields, from text written on its own. */
|
|
1046
|
+
function syllableText(written) {
|
|
1047
|
+
return textFields(resolveSpans([written]).pieces[0]);
|
|
1048
|
+
}
|
|
1049
|
+
var LABEL_MARKS = ["liturgy", "rubric"];
|
|
1050
|
+
/** Whether a group carries pitch: anything but markers and spacing. */
|
|
1051
|
+
function hasPitch(notes) {
|
|
1052
|
+
return /[^-¨]/.test(splitNoteMarkers(notes).core);
|
|
1053
|
+
}
|
|
1054
|
+
/** What each syllable token is for. A held group takes its holder's role;
|
|
1055
|
+
* an empty slot stays `sung`, unless it stands in an `(@aid)` run. */
|
|
1056
|
+
function assignRoles(tokens) {
|
|
1057
|
+
let aid = 0;
|
|
1058
|
+
let holder = "sung";
|
|
1059
|
+
const markedBy = (tok, names) => tok.runs.every((run) => run.marks.some((mark) => names.includes(mark)));
|
|
1060
|
+
for (const tok of tokens) {
|
|
1061
|
+
if (tok.kind === "aid") aid = Math.max(0, aid + (tok.on ? 1 : -1));
|
|
1062
|
+
if (tok.kind !== "syllable") continue;
|
|
1063
|
+
if (tok.held) {
|
|
1064
|
+
tok.role = holder;
|
|
1336
1065
|
continue;
|
|
1337
1066
|
}
|
|
1338
|
-
|
|
1339
|
-
|
|
1340
|
-
|
|
1341
|
-
|
|
1342
|
-
|
|
1343
|
-
out.push(glyphAt(positionOf(moved.step, tgtClef), glyph.role, `The note "${ch}"`));
|
|
1344
|
-
if (glyph.role === "smallPoint") continue;
|
|
1345
|
-
onNote(pitch);
|
|
1346
|
-
pending.push(moved);
|
|
1347
|
-
}
|
|
1348
|
-
for (const moved of pending) {
|
|
1349
|
-
const letter = mod7(moved.step);
|
|
1350
|
-
if (coveredLetters.has(letter) || moved.alter === (tgtKey.get(letter) ?? 0)) continue;
|
|
1351
|
-
const sign = glyphAt(positionOf(moved.step, tgtClef), roleOfAlter(moved.alter), `The sign for ${letterName(moved.step)}${alterName(moved.alter)}`);
|
|
1352
|
-
if (!added.includes(sign)) added.push(sign);
|
|
1353
|
-
}
|
|
1354
|
-
return leading + added.join("") + out.join("") + trailing;
|
|
1355
|
-
}
|
|
1356
|
-
/** One re-notation pass over the source. Returns the result and the
|
|
1357
|
-
* source's final (the pitch of its last note). */
|
|
1358
|
-
function renotate(source, plan) {
|
|
1359
|
-
new GuidoScript(source).tokens;
|
|
1360
|
-
const head = source.match(/^\s*\(([^)]*)\)/);
|
|
1361
|
-
const opening = head && (!isStatementGroup(head[1]) || isC4ClefGroup(head[1])) ? head : null;
|
|
1362
|
-
const openingClef = opening && isClefGlyph(opening[1].trimStart()[0] ?? "") ? opening[1].trimStart()[0] : void 0;
|
|
1363
|
-
if (opening && openingClef === void 0 && opening[1].trim() !== "") throw new Error(`The opening group "(${opening[1]})" does not start with a clef glyph`);
|
|
1364
|
-
if (plan.clef !== void 0 && !isClefGlyph(plan.clef)) throw new Error(`"${plan.clef}" is not a clef glyph`);
|
|
1365
|
-
let srcClef = openingClef ?? "M";
|
|
1366
|
-
let srcKey = readKey(opening && openingClef ? opening[1].trimStart().slice(1) : "", srcClef);
|
|
1367
|
-
let tgtClef = plan.clef ?? srcClef;
|
|
1368
|
-
let tgtKey = plan.key === void 0 ? moveKey(srcKey, plan.interval) : typeof plan.key === "number" ? countedKey(plan.key) : readKey(plan.key, tgtClef);
|
|
1369
|
-
const openingKey = writeKey(tgtKey, tgtClef);
|
|
1370
|
-
const openingGroup = opening || plan.clef !== void 0 || openingKey !== "" ? `(${tgtClef}${openingKey})` : "";
|
|
1371
|
-
let final;
|
|
1372
|
-
const offset = opening ? opening[0].length : 0;
|
|
1373
|
-
return {
|
|
1374
|
-
result: openingGroup + source.slice(offset).replace(GROUP, (_, text, rawNotes) => {
|
|
1375
|
-
if (isStatementGroup(rawNotes)) {
|
|
1376
|
-
const [, keyword, args = ""] = rawNotes.trim().match(/^@(\S*)(?: (.*))?$/) ?? [];
|
|
1377
|
-
if (keyword === "clef") {
|
|
1378
|
-
srcClef = args[0];
|
|
1379
|
-
srcKey = readKey(args.slice(1), srcClef);
|
|
1380
|
-
const moved = moveKey(srcKey, plan.interval);
|
|
1381
|
-
if (plan.clef === void 0) {
|
|
1382
|
-
tgtClef = srcClef;
|
|
1383
|
-
tgtKey = moved;
|
|
1384
|
-
return `${text}(@clef ${tgtClef}${writeKey(tgtKey, tgtClef)})`;
|
|
1385
|
-
}
|
|
1386
|
-
if (sameKey(moved, tgtKey)) return text;
|
|
1387
|
-
tgtKey = moved;
|
|
1388
|
-
const key = writeKey(tgtKey, tgtClef);
|
|
1389
|
-
return `${text}(@key${key === "" ? "" : ` ${key}`})`;
|
|
1390
|
-
}
|
|
1391
|
-
if (keyword === "key") {
|
|
1392
|
-
srcKey = readKey(args, srcClef);
|
|
1393
|
-
tgtKey = moveKey(srcKey, plan.interval);
|
|
1394
|
-
const key = writeKey(tgtKey, tgtClef);
|
|
1395
|
-
return `${text}(@key${key === "" ? "" : ` ${key}`})`;
|
|
1396
|
-
}
|
|
1397
|
-
return `${text}(${rawNotes})`;
|
|
1398
|
-
}
|
|
1399
|
-
return `${text}(${escapeNotes(renotateGroup(unescape(rawNotes), plan.interval, srcClef, srcKey, tgtClef, tgtKey, (pitch) => {
|
|
1400
|
-
final = pitch;
|
|
1401
|
-
}))})`;
|
|
1402
|
-
}),
|
|
1403
|
-
final
|
|
1404
|
-
};
|
|
1405
|
-
}
|
|
1406
|
-
/** The finals' pitch letter and alteration (`g`, `bb`, `b♭`, `f#`). */
|
|
1407
|
-
function parseFinal(final) {
|
|
1408
|
-
const match = final.trim().match(/^([a-gA-G])(b|♭|#|♯)?$/);
|
|
1409
|
-
if (!match) throw new Error(`Unknown final "${final}"`);
|
|
1410
|
-
const alter = match[2] === void 0 ? 0 : "b♭".includes(match[2]) ? -1 : 1;
|
|
1411
|
-
return {
|
|
1412
|
-
letter: LETTERS.indexOf(match[1].toLowerCase()),
|
|
1413
|
-
alter
|
|
1414
|
-
};
|
|
1415
|
-
}
|
|
1416
|
-
/**
|
|
1417
|
-
* Transposes a script exactly: moves its sounding pitches and
|
|
1418
|
-
* re-notates them under the target clef and key (see
|
|
1419
|
-
* `TranspositionTarget`). Without a key, the key moves with the
|
|
1420
|
-
* interval; without a clef, the clefs stay as written (mid-script
|
|
1421
|
-
* `(@clef …)` included); `(@key …)` statements always move with the
|
|
1422
|
-
* interval. A target clef re-notates the whole script in that clef and
|
|
1423
|
-
* removes mid-script `(@clef …)` statements (one that changed the key
|
|
1424
|
-
* leaves a `(@key …)` behind). With a `final`, the octave whose result
|
|
1425
|
-
* fits the glyph range wins — the smaller move when several fit.
|
|
1426
|
-
* Throws past the glyph range and on a missing glyph variant.
|
|
1427
|
-
*/
|
|
1428
|
-
function transposeScript(source, target) {
|
|
1429
|
-
if (target.interval !== void 0 && target.final !== void 0) throw new Error("A transposition target takes an interval or a final, not both");
|
|
1430
|
-
const plan = (interval) => ({
|
|
1431
|
-
interval,
|
|
1432
|
-
clef: target.clef,
|
|
1433
|
-
key: target.key
|
|
1434
|
-
});
|
|
1435
|
-
if (target.final === void 0) return renotate(source, plan(typeof target.interval === "string" ? parseInterval(target.interval) : target.interval ?? {
|
|
1436
|
-
steps: 0,
|
|
1437
|
-
semitones: 0
|
|
1438
|
-
})).result;
|
|
1439
|
-
const sourceFinal = renotate(source, { interval: {
|
|
1440
|
-
steps: 0,
|
|
1441
|
-
semitones: 0
|
|
1442
|
-
} }).final;
|
|
1443
|
-
if (sourceFinal === void 0) throw new Error("The script has no notes, so it has no final");
|
|
1444
|
-
const wanted = parseFinal(target.final);
|
|
1445
|
-
const up = mod7(wanted.letter - sourceFinal.step);
|
|
1446
|
-
const candidates = [
|
|
1447
|
-
-14,
|
|
1448
|
-
-7,
|
|
1449
|
-
0,
|
|
1450
|
-
7,
|
|
1451
|
-
14
|
|
1452
|
-
].map((octave) => {
|
|
1453
|
-
const semitones = semitoneOf({
|
|
1454
|
-
step: sourceFinal.step + up + octave,
|
|
1455
|
-
alter: wanted.alter
|
|
1456
|
-
}) - semitoneOf(sourceFinal);
|
|
1457
|
-
return {
|
|
1458
|
-
steps: up + octave,
|
|
1459
|
-
semitones
|
|
1460
|
-
};
|
|
1461
|
-
}).sort((a, b) => Math.abs(a.semitones) - Math.abs(b.semitones) || b.semitones - a.semitones);
|
|
1462
|
-
let rangeError;
|
|
1463
|
-
for (const interval of candidates) try {
|
|
1464
|
-
return renotate(source, plan(interval)).result;
|
|
1465
|
-
} catch (error) {
|
|
1466
|
-
if (!(error instanceof TranspositionRangeError)) throw error;
|
|
1467
|
-
rangeError ??= error;
|
|
1468
|
-
}
|
|
1469
|
-
throw rangeError;
|
|
1470
|
-
}
|
|
1471
|
-
/** The exact transposition as a script transform (see `transposeScript`). */
|
|
1472
|
-
function transposeExact(target) {
|
|
1473
|
-
return {
|
|
1474
|
-
name: "transposeExact",
|
|
1475
|
-
apply: (source) => transposeScript(source, target)
|
|
1476
|
-
};
|
|
1477
|
-
}
|
|
1478
|
-
//#endregion
|
|
1479
|
-
//#region src/guidoScriptTransforms.ts
|
|
1480
|
-
/**
|
|
1481
|
-
* Source-level transformations for GuidoScript: named rewrites applied
|
|
1482
|
-
* to a script's raw source before parsing (see the `transforms` option
|
|
1483
|
-
* on the GuidoScript constructor). Each one receives the source and
|
|
1484
|
-
* returns the rewritten source — or the source unchanged when the
|
|
1485
|
-
* feature does not apply. They compose in registration order, and the
|
|
1486
|
-
* parsed script never sees which ones ran. New script-level features
|
|
1487
|
-
* are added here, and only here: as entries of SCRIPT_TRANSFORMS, or —
|
|
1488
|
-
* when the feature takes parameters — as factories returning a
|
|
1489
|
-
* ScriptTransform (see `transposeBy`).
|
|
1490
|
-
*/
|
|
1491
|
-
/** The first character unit of a text run: a backslash escape pair or a
|
|
1492
|
-
* single character, skipping leading whitespace. Returns null when the
|
|
1493
|
-
* run has no characters to mark. */
|
|
1494
|
-
function firstCharUnit(text) {
|
|
1495
|
-
const chars = [...text];
|
|
1496
|
-
for (let i = 0; i < chars.length; i++) {
|
|
1497
|
-
const ch = chars[i];
|
|
1498
|
-
if (/\s/.test(ch)) continue;
|
|
1499
|
-
return {
|
|
1500
|
-
unit: ch === "\\" && chars[i + 1] !== void 0 ? ch + chars[i + 1] : ch,
|
|
1501
|
-
index: i
|
|
1502
|
-
};
|
|
1503
|
-
}
|
|
1504
|
-
return null;
|
|
1505
|
-
}
|
|
1506
|
-
/** The `startWithInitial` transform: finds the first syllable group with
|
|
1507
|
-
* actual notes and text (the clef, no-notes groups, control statements,
|
|
1508
|
-
* text-less groups and `~` empty slots don't count) and wraps the
|
|
1509
|
-
* syllable's first character in the `*…*` initial marker. Already-marked
|
|
1510
|
-
* text (leading `*`) is left alone. */
|
|
1511
|
-
function startWithInitial(source) {
|
|
1512
|
-
const clefMatch = source.match(/^\s*\(([^)]*)\)/);
|
|
1513
|
-
const offset = clefMatch ? clefMatch[0].length : 0;
|
|
1514
|
-
const rest = source.slice(offset);
|
|
1515
|
-
for (const match of rest.matchAll(/((?:[^()\\]|\\.)*)\(((?:[^)\\]|\\.)*)\)/g)) {
|
|
1516
|
-
const [, text, notes] = match;
|
|
1517
|
-
if (unescape(notes).trim().length === 0) continue;
|
|
1518
|
-
if (isStatementGroup(notes)) continue;
|
|
1519
|
-
const unit = firstCharUnit(text);
|
|
1520
|
-
if (!unit || text.trim() === "~") continue;
|
|
1521
|
-
if (text.trimStart().startsWith("*")) return source;
|
|
1522
|
-
const unitStart = offset + match.index + unit.index;
|
|
1523
|
-
const unitEnd = unitStart + unit.unit.length;
|
|
1524
|
-
return `${source.slice(0, unitStart)}*${source.slice(unitStart, unitEnd)}*${source.slice(unitEnd)}`;
|
|
1067
|
+
if (tok.runs.length === 0) tok.role = !tok.empty && tok.notes === "" ? "sign" : aid > 0 ? "aid" : "sung";
|
|
1068
|
+
else if (tok.text === "" || markedBy(tok, ["plus"])) tok.role = "sign";
|
|
1069
|
+
else if (markedBy(tok, LABEL_MARKS) && !hasPitch(tok.notes)) tok.role = "label";
|
|
1070
|
+
else tok.role = aid > 0 ? "aid" : "sung";
|
|
1071
|
+
holder = tok.role;
|
|
1525
1072
|
}
|
|
1526
|
-
return source;
|
|
1527
1073
|
}
|
|
1528
|
-
/**
|
|
1529
|
-
*
|
|
1530
|
-
|
|
1531
|
-
|
|
1532
|
-
var SCRIPT_TRANSFORMS = {
|
|
1533
|
-
/** Wraps the first letter of the first syllable that has notes in
|
|
1534
|
-
* `*…*`, the initial marker of the style marking system. */
|
|
1535
|
-
startWithInitial: {
|
|
1536
|
-
name: "startWithInitial",
|
|
1537
|
-
apply: startWithInitial
|
|
1538
|
-
} };
|
|
1539
|
-
/** The pitch rows of the chant font: every character carrying pitch
|
|
1540
|
-
* information, grouped into rows of neighbouring pitches from low to
|
|
1541
|
-
* high. A transpose shift moves a character this many positions along
|
|
1542
|
-
* its row; characters outside every row carry no pitch and stay put.
|
|
1543
|
-
* Rows are searched in order, so a character found in several rows
|
|
1544
|
-
* transposes within the first one. */
|
|
1545
|
-
var PITCH_ROWS = [
|
|
1546
|
-
"ðñ0123456789öüó",
|
|
1547
|
-
"^]qwertzuiopõú",
|
|
1548
|
-
"ÝÞQWERTZUIOPÕÚ",
|
|
1549
|
-
"×sdfghjkléáû",
|
|
1550
|
-
"ØÙDFGHJKLÉÁÛ",
|
|
1551
|
-
"÷øaASÍYXCVBN$m",
|
|
1552
|
-
"ÈÊËÌÎÏÐÑÒþßòôùý",
|
|
1553
|
-
"“”•–—˜™š›œº»",
|
|
1554
|
-
"®−§ÿ\"+!%/=()ÖÜÓ",
|
|
1555
|
-
"Ÿ¡¢£[¥¦©ª«¬",
|
|
1556
|
-
"~¯‚ƒ„…†‡ˆ‰Š‹Œ‘’",
|
|
1557
|
-
"àâãäåæçèêëìîï",
|
|
1558
|
-
"½°±²³´μ·¾¸¹"
|
|
1559
|
-
];
|
|
1560
|
-
/** Maps one character to its pitch-shifted neighbour, or undefined when
|
|
1561
|
-
* it is not a pitch character (and so stays unchanged). */
|
|
1562
|
-
function shiftPitch(ch, shift, overflow) {
|
|
1563
|
-
for (const row of PITCH_ROWS) {
|
|
1564
|
-
const index = row.indexOf(ch);
|
|
1565
|
-
if (index === -1) continue;
|
|
1566
|
-
const target = index + shift;
|
|
1567
|
-
if (target < 0 || target >= row.length) {
|
|
1568
|
-
if (overflow === "null") return "\0";
|
|
1569
|
-
if (overflow === "error") throw new Error(`Transposing "${ch}" by ${shift} runs past the glyph range`);
|
|
1570
|
-
return row[Math.max(0, Math.min(row.length - 1, target))];
|
|
1571
|
-
}
|
|
1572
|
-
return row[target];
|
|
1573
|
-
}
|
|
1074
|
+
/** Folds backslash escapes into the characters they stand for. Shared
|
|
1075
|
+
* with the transforms module (guidoScriptTransforms.ts). */
|
|
1076
|
+
function unescape(text) {
|
|
1077
|
+
return text.replace(/\\(.)/g, "$1");
|
|
1574
1078
|
}
|
|
1575
|
-
/**
|
|
1576
|
-
|
|
1577
|
-
|
|
1578
|
-
* underlying character (an escaped paren is a pitch glyph of the
|
|
1579
|
-
* `®−§ÿ…` row), and a shifted result that is a control character of the
|
|
1580
|
-
* group syntax is written escaped. A statement group's content (`@…`) is returned
|
|
1581
|
-
* unchanged: its keyword letters are pitch glyphs, but no notes. */
|
|
1582
|
-
function transposeNotes(notes, shift, options = {}) {
|
|
1583
|
-
if (shift === 0 || isStatementGroup(notes)) return notes;
|
|
1584
|
-
const overflow = options.overflow ?? "clamp";
|
|
1585
|
-
let out = "";
|
|
1586
|
-
for (let i = 0; i < notes.length; i++) {
|
|
1587
|
-
const ch = notes[i];
|
|
1588
|
-
const escaped = ch === "\\" && notes[i + 1] !== void 0;
|
|
1589
|
-
const shifted = shiftPitch(escaped ? notes[i + 1] : ch, shift, overflow);
|
|
1590
|
-
if (shifted === void 0) {
|
|
1591
|
-
out += escaped ? ch + notes[i + 1] : ch;
|
|
1592
|
-
if (escaped) i++;
|
|
1593
|
-
continue;
|
|
1594
|
-
}
|
|
1595
|
-
out += "\\()".includes(shifted) ? `\\${shifted}` : shifted;
|
|
1596
|
-
if (escaped) i++;
|
|
1597
|
-
}
|
|
1598
|
-
return out;
|
|
1599
|
-
}
|
|
1600
|
-
/** The `transposeBy` transform factory: shifts every pitch character of
|
|
1601
|
-
* every notes group `shift` positions along its pitch row. The clef
|
|
1602
|
-
* group and the syllable text are left alone — clef handling is a
|
|
1603
|
-
* separate, future tool. */
|
|
1604
|
-
function transposeBy(shift, options = {}) {
|
|
1605
|
-
const overflow = options.overflow ?? "clamp";
|
|
1606
|
-
const apply = (source) => {
|
|
1607
|
-
if (shift === 0) return source;
|
|
1608
|
-
const clefMatch = source.match(/^\s*\(([^)]*)\)/);
|
|
1609
|
-
const offset = clefMatch ? clefMatch[0].length : 0;
|
|
1610
|
-
return source.slice(0, offset) + source.slice(offset).replace(/((?:[^()\\]|\\.)*)\(((?:[^)\\]|\\.)*)\)/g, (_, text, notes) => `${text}(${transposeNotes(notes, shift, { overflow })})`);
|
|
1611
|
-
};
|
|
1612
|
-
return {
|
|
1613
|
-
name: "transpose",
|
|
1614
|
-
apply
|
|
1615
|
-
};
|
|
1079
|
+
/** Turns literal parentheses and backslashes into their escape sequences. */
|
|
1080
|
+
function escape(text) {
|
|
1081
|
+
return text.replace(/\\/g, "\\\\").replace(/[()]/g, (ch) => `\\${ch}`);
|
|
1616
1082
|
}
|
|
1617
1083
|
//#endregion
|
|
1618
1084
|
//#region src/guidoStyles.ts
|
|
@@ -1621,12 +1087,6 @@ function transposeBy(shift, options = {}) {
|
|
|
1621
1087
|
* the special syllable decorations) kept in one constant registry, so a
|
|
1622
1088
|
* new style is a data entry here rather than a renderer fork.
|
|
1623
1089
|
*/
|
|
1624
|
-
/** The notation's pause-marker characters: they split off into their own
|
|
1625
|
-
* runs in every style set — carrying the style set's dedicated
|
|
1626
|
-
* `markerGlyphs` font when one is configured, the text font otherwise
|
|
1627
|
-
* (Vesperale's text is Times New Roman already) — and always the
|
|
1628
|
-
* notation-fixed `marker` kind. */
|
|
1629
|
-
var MARKER_CHARS = /* @__PURE__ */ new Set(["*", "†"]);
|
|
1630
1090
|
/** Text characters (* and †) rendered in Times New Roman. */
|
|
1631
1091
|
var TIMES_MARKER_GLYPHS = {
|
|
1632
1092
|
chars: MARKER_CHARS,
|
|
@@ -1664,33 +1124,33 @@ var STYLE_SETS = {
|
|
|
1664
1124
|
weight: "700"
|
|
1665
1125
|
}
|
|
1666
1126
|
],
|
|
1667
|
-
|
|
1668
|
-
|
|
1127
|
+
marks: {
|
|
1128
|
+
liturgy: {
|
|
1669
1129
|
fontFamily: "'Liturgy', serif",
|
|
1670
1130
|
weight: "700",
|
|
1671
1131
|
color: "var(--red)"
|
|
1672
1132
|
},
|
|
1673
|
-
|
|
1133
|
+
rubric: {
|
|
1674
1134
|
uppercase: true,
|
|
1675
1135
|
scale: .7,
|
|
1676
1136
|
color: "var(--red)"
|
|
1677
1137
|
},
|
|
1678
|
-
|
|
1138
|
+
initial: {
|
|
1679
1139
|
weight: "700",
|
|
1680
1140
|
color: "var(--red)",
|
|
1681
1141
|
initial: true
|
|
1682
|
-
}
|
|
1683
|
-
|
|
1684
|
-
|
|
1685
|
-
|
|
1686
|
-
|
|
1687
|
-
|
|
1688
|
-
|
|
1689
|
-
|
|
1690
|
-
|
|
1142
|
+
},
|
|
1143
|
+
star: {
|
|
1144
|
+
fontFamily: TIMES_MARKER_GLYPHS.fontFamily,
|
|
1145
|
+
weight: "700",
|
|
1146
|
+
scale: 1,
|
|
1147
|
+
measureScale: .95,
|
|
1148
|
+
raise: .1,
|
|
1149
|
+
color: "var(--red)"
|
|
1150
|
+
},
|
|
1151
|
+
plus: LITURGY_PLUS_STYLE
|
|
1691
1152
|
},
|
|
1692
|
-
padding: 12
|
|
1693
|
-
scriptTransforms: [SCRIPT_TRANSFORMS.startWithInitial]
|
|
1153
|
+
padding: 12
|
|
1694
1154
|
},
|
|
1695
1155
|
/** Times New Roman text, strictly black and white. */
|
|
1696
1156
|
Vesperale: {
|
|
@@ -1714,163 +1174,226 @@ var STYLE_SETS = {
|
|
|
1714
1174
|
weight: "700"
|
|
1715
1175
|
}
|
|
1716
1176
|
],
|
|
1717
|
-
|
|
1718
|
-
|
|
1177
|
+
marks: {
|
|
1178
|
+
liturgy: {
|
|
1719
1179
|
fontFamily: "'Liturgy', serif",
|
|
1720
1180
|
weight: "700"
|
|
1721
1181
|
},
|
|
1722
|
-
|
|
1182
|
+
rubric: {
|
|
1723
1183
|
scale: .8,
|
|
1724
1184
|
italic: true
|
|
1725
1185
|
},
|
|
1726
|
-
|
|
1186
|
+
initial: {
|
|
1727
1187
|
weight: "700",
|
|
1728
1188
|
initial: true
|
|
1729
|
-
}
|
|
1730
|
-
|
|
1731
|
-
|
|
1732
|
-
|
|
1733
|
-
|
|
1734
|
-
|
|
1735
|
-
|
|
1736
|
-
|
|
1189
|
+
},
|
|
1190
|
+
star: {
|
|
1191
|
+
fontFamily: TIMES_MARKER_GLYPHS.fontFamily,
|
|
1192
|
+
weight: "700",
|
|
1193
|
+
scale: 1,
|
|
1194
|
+
measureScale: .95,
|
|
1195
|
+
raise: .2
|
|
1196
|
+
},
|
|
1197
|
+
plus: LITURGY_PLUS_STYLE
|
|
1737
1198
|
},
|
|
1738
|
-
padding: 12
|
|
1739
|
-
scriptTransforms: []
|
|
1199
|
+
padding: 12
|
|
1740
1200
|
}
|
|
1741
1201
|
};
|
|
1742
1202
|
var DEFAULT_STYLE_SET = "BNS";
|
|
1743
|
-
/**
|
|
1744
|
-
|
|
1745
|
-
|
|
1746
|
-
|
|
1747
|
-
|
|
1748
|
-
|
|
1749
|
-
|
|
1750
|
-
*
|
|
1751
|
-
|
|
1752
|
-
|
|
1753
|
-
|
|
1754
|
-
|
|
1755
|
-
|
|
1756
|
-
|
|
1757
|
-
|
|
1758
|
-
|
|
1759
|
-
|
|
1760
|
-
|
|
1761
|
-
|
|
1762
|
-
|
|
1763
|
-
|
|
1764
|
-
|
|
1765
|
-
|
|
1766
|
-
|
|
1767
|
-
|
|
1768
|
-
|
|
1769
|
-
kind: "star"
|
|
1770
|
-
}];
|
|
1771
|
-
let content = text;
|
|
1772
|
-
let base = {};
|
|
1773
|
-
let baseKind;
|
|
1774
|
-
for (const wrapper of ["__", "_"]) {
|
|
1775
|
-
const rule = styleSet.decorations[wrapper];
|
|
1776
|
-
if (rule && text.length >= wrapper.length * 2 + 1 && text.startsWith(wrapper) && text.endsWith(wrapper)) {
|
|
1777
|
-
content = text.slice(wrapper.length, -wrapper.length);
|
|
1778
|
-
base = { ...rule };
|
|
1779
|
-
baseKind = wrapper === "__" ? "liturgy" : "rubric";
|
|
1780
|
-
break;
|
|
1781
|
-
}
|
|
1782
|
-
}
|
|
1783
|
-
const initialRule = styleSet.decorations["*"];
|
|
1784
|
-
const segmentSpecs = [];
|
|
1785
|
-
let rest = content;
|
|
1786
|
-
let plain = "";
|
|
1787
|
-
for (let m = /\*([^*]+)\*/.exec(rest); m; m = /\*([^*]+)\*/.exec(rest)) {
|
|
1788
|
-
plain += rest.slice(0, m.index);
|
|
1789
|
-
if (plain) {
|
|
1790
|
-
segmentSpecs.push({
|
|
1791
|
-
text: plain,
|
|
1792
|
-
marked: false
|
|
1793
|
-
});
|
|
1794
|
-
plain = "";
|
|
1203
|
+
/** The style sets `defineStyleSet` added. */
|
|
1204
|
+
var definedStyleSets = /* @__PURE__ */ new Map();
|
|
1205
|
+
/** The style set of that name, built-in or defined. */
|
|
1206
|
+
function styleSetOf(name) {
|
|
1207
|
+
return definedStyleSets.get(name) ?? STYLE_SETS[name];
|
|
1208
|
+
}
|
|
1209
|
+
/** Adds a style set under a name `style` can then give, without patching
|
|
1210
|
+
* the package. Returns it. */
|
|
1211
|
+
function defineStyleSet(name, definition) {
|
|
1212
|
+
const { extends: base = "BNS", ...own } = definition;
|
|
1213
|
+
const parent = styleSetOf(base);
|
|
1214
|
+
if (!parent) throw new Error(`Unknown style set: "${base}"`);
|
|
1215
|
+
const styleSet = {
|
|
1216
|
+
...parent,
|
|
1217
|
+
...own,
|
|
1218
|
+
marks: {
|
|
1219
|
+
...parent.marks,
|
|
1220
|
+
...own.marks
|
|
1221
|
+
},
|
|
1222
|
+
colors: {
|
|
1223
|
+
...parent.colors,
|
|
1224
|
+
...own.colors
|
|
1225
|
+
},
|
|
1226
|
+
fonts: {
|
|
1227
|
+
...parent.fonts,
|
|
1228
|
+
...own.fonts
|
|
1795
1229
|
}
|
|
1796
|
-
|
|
1797
|
-
|
|
1798
|
-
|
|
1799
|
-
|
|
1800
|
-
|
|
1801
|
-
|
|
1802
|
-
|
|
1803
|
-
|
|
1804
|
-
|
|
1805
|
-
|
|
1806
|
-
|
|
1807
|
-
|
|
1808
|
-
|
|
1809
|
-
|
|
1810
|
-
|
|
1811
|
-
|
|
1812
|
-
|
|
1813
|
-
|
|
1814
|
-
|
|
1815
|
-
|
|
1816
|
-
|
|
1817
|
-
|
|
1818
|
-
|
|
1819
|
-
|
|
1820
|
-
|
|
1821
|
-
|
|
1230
|
+
};
|
|
1231
|
+
definedStyleSets.set(name, styleSet);
|
|
1232
|
+
return styleSet;
|
|
1233
|
+
}
|
|
1234
|
+
/** The built-in colors `color:<name>` can name. */
|
|
1235
|
+
var PALETTE = { red: "var(--red)" };
|
|
1236
|
+
/** The built-in fonts `font:<name>` can name. */
|
|
1237
|
+
var FONTS = {
|
|
1238
|
+
liturgy: "'Liturgy', serif",
|
|
1239
|
+
times: "'Times New Roman', Times, serif"
|
|
1240
|
+
};
|
|
1241
|
+
/** How much smaller small capitals draw than the full capitals. */
|
|
1242
|
+
var SMALL_CAP_SCALE = .75;
|
|
1243
|
+
var KINDS = /* @__PURE__ */ new Set([
|
|
1244
|
+
"liturgy",
|
|
1245
|
+
"rubric",
|
|
1246
|
+
"star",
|
|
1247
|
+
"marker",
|
|
1248
|
+
"plus"
|
|
1249
|
+
]);
|
|
1250
|
+
/** The run kind of a run's marks: the last of the notation's own. */
|
|
1251
|
+
function kindOf(marks) {
|
|
1252
|
+
return marks.filter((mark) => KINDS.has(mark)).pop();
|
|
1253
|
+
}
|
|
1254
|
+
/** The style `mark` leaves on `style`, or undefined when no such mark
|
|
1255
|
+
* exists. The primitives come first, so they mean the same in every style
|
|
1256
|
+
* set; a registered mark's `color` may name a palette color. */
|
|
1257
|
+
function withMark(style, mark, text, styleSet, extra) {
|
|
1258
|
+
const colorOf = (name) => styleSet.colors?.[name] ?? PALETTE[name];
|
|
1259
|
+
const at = mark.indexOf(":");
|
|
1260
|
+
const name = at < 0 ? mark : mark.slice(0, at);
|
|
1261
|
+
const argument = at < 0 ? "" : mark.slice(at + 1);
|
|
1262
|
+
const amount = Number(argument);
|
|
1263
|
+
switch (name) {
|
|
1264
|
+
case "plain": return {};
|
|
1265
|
+
case "red": return {
|
|
1266
|
+
...style,
|
|
1267
|
+
color: PALETTE.red
|
|
1268
|
+
};
|
|
1269
|
+
case "b": return {
|
|
1270
|
+
...style,
|
|
1822
1271
|
weight: "700"
|
|
1823
1272
|
};
|
|
1824
|
-
|
|
1825
|
-
|
|
1826
|
-
|
|
1827
|
-
if (MARKER_CHARS.has(ch) || glyphs && glyphs.chars.has(ch)) return "marker";
|
|
1828
|
-
if (inLiturgy && ch === "+") return "plus";
|
|
1829
|
-
return "text";
|
|
1273
|
+
case "i": return {
|
|
1274
|
+
...style,
|
|
1275
|
+
italic: true
|
|
1830
1276
|
};
|
|
1831
|
-
|
|
1832
|
-
|
|
1833
|
-
|
|
1834
|
-
|
|
1835
|
-
|
|
1836
|
-
|
|
1837
|
-
|
|
1838
|
-
}
|
|
1839
|
-
let charRun = "";
|
|
1840
|
-
let currentKind = null;
|
|
1841
|
-
const pushRun = () => {
|
|
1842
|
-
if (!charRun) return;
|
|
1843
|
-
let runStyle = { ...segment.style };
|
|
1844
|
-
let partKind = segment.kind;
|
|
1845
|
-
if (currentKind === "marker") {
|
|
1846
|
-
partKind = "marker";
|
|
1847
|
-
if (glyphs && glyphs.chars.has(charRun[0])) runStyle = {
|
|
1848
|
-
...runStyle,
|
|
1849
|
-
fontFamily: glyphs.fontFamily
|
|
1850
|
-
};
|
|
1851
|
-
} else if (currentKind === "plus") {
|
|
1852
|
-
runStyle = {
|
|
1853
|
-
...runStyle,
|
|
1854
|
-
...LITURGY_PLUS_STYLE
|
|
1855
|
-
};
|
|
1856
|
-
partKind = "plus";
|
|
1857
|
-
}
|
|
1858
|
-
parts.push({
|
|
1859
|
-
text: charRun,
|
|
1860
|
-
style: runStyle,
|
|
1861
|
-
kind: partKind
|
|
1862
|
-
});
|
|
1863
|
-
charRun = "";
|
|
1277
|
+
case "sc": return {
|
|
1278
|
+
...style,
|
|
1279
|
+
smallCaps: true
|
|
1280
|
+
};
|
|
1281
|
+
case "up": return {
|
|
1282
|
+
...style,
|
|
1283
|
+
uppercase: true
|
|
1864
1284
|
};
|
|
1865
|
-
|
|
1866
|
-
const
|
|
1867
|
-
|
|
1868
|
-
|
|
1869
|
-
|
|
1285
|
+
case "color": {
|
|
1286
|
+
const color = colorOf(argument);
|
|
1287
|
+
return color ? {
|
|
1288
|
+
...style,
|
|
1289
|
+
color
|
|
1290
|
+
} : void 0;
|
|
1291
|
+
}
|
|
1292
|
+
case "size": return amount > 0 ? {
|
|
1293
|
+
...style,
|
|
1294
|
+
scale: amount,
|
|
1295
|
+
measureScale: void 0
|
|
1296
|
+
} : void 0;
|
|
1297
|
+
case "raise": return Number.isFinite(amount) && argument !== "" ? {
|
|
1298
|
+
...style,
|
|
1299
|
+
raise: amount
|
|
1300
|
+
} : void 0;
|
|
1301
|
+
case "font": {
|
|
1302
|
+
const fontFamily = styleSet.fonts?.[argument] ?? FONTS[argument];
|
|
1303
|
+
return fontFamily ? {
|
|
1304
|
+
...style,
|
|
1305
|
+
fontFamily
|
|
1306
|
+
} : void 0;
|
|
1870
1307
|
}
|
|
1871
|
-
|
|
1308
|
+
case "marker": return styleSet.markerGlyphs?.chars.has(text[0]) ? {
|
|
1309
|
+
...style,
|
|
1310
|
+
fontFamily: styleSet.markerGlyphs.fontFamily
|
|
1311
|
+
} : style;
|
|
1312
|
+
}
|
|
1313
|
+
const rule = extra?.[mark] ?? styleSet.marks[mark];
|
|
1314
|
+
if (!rule) return void 0;
|
|
1315
|
+
const color = rule.color && colorOf(rule.color);
|
|
1316
|
+
return {
|
|
1317
|
+
...style,
|
|
1318
|
+
...rule,
|
|
1319
|
+
...color ? { color } : {}
|
|
1320
|
+
};
|
|
1321
|
+
}
|
|
1322
|
+
/** The look of a list of marks, later ones winning; a mark that does not
|
|
1323
|
+
* exist changes nothing (`unknownMarks` reports it). */
|
|
1324
|
+
function styleOf(text, marks, styleSet, extra) {
|
|
1325
|
+
return marks.reduce((style, mark) => withMark(style, mark, text, styleSet, extra) ?? style, {});
|
|
1326
|
+
}
|
|
1327
|
+
/** The look of comma-separated marks (`red,sc`), for a host styling prose
|
|
1328
|
+
* to match the score. */
|
|
1329
|
+
function resolveMarks(marks, styleSet, extra) {
|
|
1330
|
+
return styleOf("", marks.split(",").map((mark) => mark.trim()), styleSet, extra);
|
|
1331
|
+
}
|
|
1332
|
+
/** The marks of `marks` that neither the primitives, the style set nor
|
|
1333
|
+
* `extra` define. */
|
|
1334
|
+
function unknownMarks(marks, styleSet, extra) {
|
|
1335
|
+
return marks.filter((mark) => withMark({}, mark, "", styleSet, extra) === void 0);
|
|
1336
|
+
}
|
|
1337
|
+
/** A run's small-caps split: lowercase letters as small capitals, in runs
|
|
1338
|
+
* of their own, everything else at full size. */
|
|
1339
|
+
function splitSmallCaps(text) {
|
|
1340
|
+
const pieces = [];
|
|
1341
|
+
for (const ch of text) {
|
|
1342
|
+
const small = ch.toUpperCase() !== ch;
|
|
1343
|
+
const last = pieces[pieces.length - 1];
|
|
1344
|
+
if (last && last.small === small) last.text += ch;
|
|
1345
|
+
else pieces.push({
|
|
1346
|
+
text: ch,
|
|
1347
|
+
small
|
|
1348
|
+
});
|
|
1872
1349
|
}
|
|
1873
|
-
return
|
|
1350
|
+
return pieces;
|
|
1351
|
+
}
|
|
1352
|
+
/** Resolves parsed text runs (`SyllableToken.runs`) into how a style set
|
|
1353
|
+
* draws them: each run takes the style of its marks, the `bold` option
|
|
1354
|
+
* forces weight 700 onto every run, and `kind` names its notation-fixed
|
|
1355
|
+
* semantic (`liturgy`, `rubric`, `star`, `marker`, `plus`), the vocabulary
|
|
1356
|
+
* the marked-up text export and the renderer's run classes share. A
|
|
1357
|
+
* small-caps run splits into full and small parts. Text stays as written:
|
|
1358
|
+
* capitals are the part's display (`drawnText`). */
|
|
1359
|
+
function resolveTextRuns(runs, styleSet, bold, extra) {
|
|
1360
|
+
return runs.flatMap(({ text, marks }) => {
|
|
1361
|
+
const resolved = styleOf(text, marks, styleSet, extra);
|
|
1362
|
+
const style = bold ? {
|
|
1363
|
+
...resolved,
|
|
1364
|
+
weight: "700"
|
|
1365
|
+
} : resolved;
|
|
1366
|
+
const kind = kindOf(marks);
|
|
1367
|
+
if (!style.smallCaps) return [{
|
|
1368
|
+
text,
|
|
1369
|
+
style,
|
|
1370
|
+
kind
|
|
1371
|
+
}];
|
|
1372
|
+
const full = {
|
|
1373
|
+
...style,
|
|
1374
|
+
smallCaps: void 0
|
|
1375
|
+
};
|
|
1376
|
+
return splitSmallCaps(text).map((piece) => ({
|
|
1377
|
+
text: piece.text,
|
|
1378
|
+
kind,
|
|
1379
|
+
style: piece.small ? {
|
|
1380
|
+
...full,
|
|
1381
|
+
smallCaps: true,
|
|
1382
|
+
uppercase: true,
|
|
1383
|
+
scale: (full.scale ?? 1) * SMALL_CAP_SCALE,
|
|
1384
|
+
measureScale: void 0
|
|
1385
|
+
} : full
|
|
1386
|
+
}));
|
|
1387
|
+
});
|
|
1388
|
+
}
|
|
1389
|
+
/** The characters a part draws, which measuring must see. */
|
|
1390
|
+
function drawnText(part) {
|
|
1391
|
+
return part.style.uppercase ? part.text.toUpperCase() : part.text;
|
|
1392
|
+
}
|
|
1393
|
+
/** Parses a syllable as written (`SyllableToken.source`) and resolves its
|
|
1394
|
+
* runs in one step (see `resolveSpans`, `resolveTextRuns`). */
|
|
1395
|
+
function resolveSyllableParts(written, styleSet, bold, extra) {
|
|
1396
|
+
return resolveTextRuns(resolveSpans([written]).pieces[0].runs, styleSet, bold, extra);
|
|
1874
1397
|
}
|
|
1875
1398
|
/** The canvas font shorthand for measuring a run at the text size. */
|
|
1876
1399
|
function measurementFont(style, textPx, fallbackStack) {
|
|
@@ -1891,6 +1414,7 @@ function inlineStylesFor(style, textPx) {
|
|
|
1891
1414
|
if (style.italic) styles["font-style"] = "italic";
|
|
1892
1415
|
if (style.scale !== void 0) styles["font-size"] = `${textPx * style.scale}px`;
|
|
1893
1416
|
if (style.color) styles["color"] = style.color;
|
|
1417
|
+
if (style.uppercase) styles["text-transform"] = "uppercase";
|
|
1894
1418
|
if (style.raise) {
|
|
1895
1419
|
styles["position"] = "relative";
|
|
1896
1420
|
styles["top"] = `${-textPx * style.raise}px`;
|
|
@@ -1902,6 +1426,198 @@ function fontStringsToVerify(styleSet, textPx) {
|
|
|
1902
1426
|
return styleSet.verifiedFonts.map((f) => `${f.weight} ${textPx}px ${f.family}`);
|
|
1903
1427
|
}
|
|
1904
1428
|
//#endregion
|
|
1429
|
+
//#region src/recitationSegments.ts
|
|
1430
|
+
/** The reciting pitch of a note group: one reciting glyph, or one plain
|
|
1431
|
+
* note that has one, optionally after one accidental sign; null for any
|
|
1432
|
+
* other group. */
|
|
1433
|
+
function recitationPitch(notes) {
|
|
1434
|
+
const sign = notes.length === 2 && isModifierGlyph(notes[0]) ? notes[0] : "";
|
|
1435
|
+
const ch = notes.slice(sign.length);
|
|
1436
|
+
if (ch.length !== 1) return null;
|
|
1437
|
+
const family = baseOf(ch);
|
|
1438
|
+
if (family?.role === "longa") return {
|
|
1439
|
+
sign,
|
|
1440
|
+
plain: family.base,
|
|
1441
|
+
glyph: ch,
|
|
1442
|
+
written: "glyph"
|
|
1443
|
+
};
|
|
1444
|
+
const glyph = toLonga(ch);
|
|
1445
|
+
return glyph ? {
|
|
1446
|
+
sign,
|
|
1447
|
+
plain: ch,
|
|
1448
|
+
glyph,
|
|
1449
|
+
written: "plain"
|
|
1450
|
+
} : null;
|
|
1451
|
+
}
|
|
1452
|
+
/** The notes a group renders with in a mode. */
|
|
1453
|
+
function notesInMode(notes, mode) {
|
|
1454
|
+
const pitch = recitationPitch(notes);
|
|
1455
|
+
if (!pitch || mode === "written") return notes;
|
|
1456
|
+
return pitch.sign + (mode === "recite" ? pitch.glyph : pitch.plain);
|
|
1457
|
+
}
|
|
1458
|
+
/** What a mode renders, in written terms: 'recite' or 'plain'. */
|
|
1459
|
+
function shownAs(notes, mode) {
|
|
1460
|
+
if (mode !== "written") return mode;
|
|
1461
|
+
return recitationPitch(notes)?.written === "glyph" ? "recite" : "plain";
|
|
1462
|
+
}
|
|
1463
|
+
/** The decisions' view of a script: its syllables, and where a clef or key
|
|
1464
|
+
* statement stands (a run never continues across one). */
|
|
1465
|
+
function runSyllables(underlays) {
|
|
1466
|
+
const syllables = [];
|
|
1467
|
+
const pitchBreaks = /* @__PURE__ */ new Set();
|
|
1468
|
+
(underlays[0] ?? []).forEach((tok, k) => {
|
|
1469
|
+
if (tok.kind === "clef" || tok.kind === "key") pitchBreaks.add(syllables.length);
|
|
1470
|
+
if (tok.kind !== "syllable") return;
|
|
1471
|
+
const counted = underlays.some((u) => {
|
|
1472
|
+
const own = u[k];
|
|
1473
|
+
return own?.kind === "syllable" && !own.held && !own.empty;
|
|
1474
|
+
});
|
|
1475
|
+
syllables.push({
|
|
1476
|
+
notes: tok.notes,
|
|
1477
|
+
counted
|
|
1478
|
+
});
|
|
1479
|
+
});
|
|
1480
|
+
return {
|
|
1481
|
+
syllables,
|
|
1482
|
+
pitchBreaks
|
|
1483
|
+
};
|
|
1484
|
+
}
|
|
1485
|
+
/** Whether a syllable can join a recitation run under the options. */
|
|
1486
|
+
function isMember(syl, options) {
|
|
1487
|
+
const pitch = recitationPitch(syl.notes);
|
|
1488
|
+
if (!pitch) return false;
|
|
1489
|
+
return pitch.written === "glyph" || options.plainRunsToRecitation;
|
|
1490
|
+
}
|
|
1491
|
+
/** The run key of a member: its pitch, glyph and plain note alike. */
|
|
1492
|
+
function runKey(notes) {
|
|
1493
|
+
const pitch = recitationPitch(notes);
|
|
1494
|
+
return pitch.sign + pitch.plain;
|
|
1495
|
+
}
|
|
1496
|
+
/**
|
|
1497
|
+
* The mode each member is first laid out in: a recitation when its run in
|
|
1498
|
+
* the script counts 3 or more syllables (so the line segments are counted
|
|
1499
|
+
* with the recitation's widths), else as written (a run of 2 or fewer can
|
|
1500
|
+
* only ever need its pair converted).
|
|
1501
|
+
*/
|
|
1502
|
+
function trialModes(syllables, pitchBreaks, options) {
|
|
1503
|
+
const modes = /* @__PURE__ */ new Map();
|
|
1504
|
+
let k = 0;
|
|
1505
|
+
while (k < syllables.length) {
|
|
1506
|
+
if (!isMember(syllables[k], options)) {
|
|
1507
|
+
k++;
|
|
1508
|
+
continue;
|
|
1509
|
+
}
|
|
1510
|
+
const key = runKey(syllables[k].notes);
|
|
1511
|
+
let end = k;
|
|
1512
|
+
let count = 0;
|
|
1513
|
+
while (end < syllables.length && isMember(syllables[end], options) && runKey(syllables[end].notes) === key && (end === k || !pitchBreaks.has(end))) {
|
|
1514
|
+
if (syllables[end].counted) count++;
|
|
1515
|
+
end++;
|
|
1516
|
+
}
|
|
1517
|
+
for (let j = k; j < end; j++) modes.set(j, count >= 3 ? "recite" : "written");
|
|
1518
|
+
k = end;
|
|
1519
|
+
}
|
|
1520
|
+
return modes;
|
|
1521
|
+
}
|
|
1522
|
+
/** Whether a script has anything for the decisions to do. */
|
|
1523
|
+
function hasCandidates(syllables, options) {
|
|
1524
|
+
if (!options.twoSyllableRecitationsToPlain && !options.plainRunsToRecitation) return false;
|
|
1525
|
+
return syllables.some((s) => isMember(s, options));
|
|
1526
|
+
}
|
|
1527
|
+
/** The line segments of a pass: consecutive members on the same line with
|
|
1528
|
+
* the same placed pitch. */
|
|
1529
|
+
function segmentsOf(syllables, placedNotes, sylLines, options) {
|
|
1530
|
+
const segments = [];
|
|
1531
|
+
let current;
|
|
1532
|
+
let currentKey = "";
|
|
1533
|
+
let currentLine = -1;
|
|
1534
|
+
syllables.forEach((syl, k) => {
|
|
1535
|
+
const placed = placedNotes[k];
|
|
1536
|
+
const line = sylLines[k];
|
|
1537
|
+
if (!(isMember(syl, options) && placed !== void 0 && line !== void 0 && recitationPitch(placed) !== null)) {
|
|
1538
|
+
current = void 0;
|
|
1539
|
+
return;
|
|
1540
|
+
}
|
|
1541
|
+
const key = runKey(placed);
|
|
1542
|
+
if (!current || key !== currentKey || line !== currentLine) {
|
|
1543
|
+
current = {
|
|
1544
|
+
members: [],
|
|
1545
|
+
count: 0
|
|
1546
|
+
};
|
|
1547
|
+
segments.push(current);
|
|
1548
|
+
currentKey = key;
|
|
1549
|
+
currentLine = line;
|
|
1550
|
+
}
|
|
1551
|
+
current.members.push(k);
|
|
1552
|
+
if (syl.counted) current.count++;
|
|
1553
|
+
});
|
|
1554
|
+
return segments;
|
|
1555
|
+
}
|
|
1556
|
+
function initialState() {
|
|
1557
|
+
return {
|
|
1558
|
+
decided: /* @__PURE__ */ new Map(),
|
|
1559
|
+
breakBefore: /* @__PURE__ */ new Set(),
|
|
1560
|
+
pending: null
|
|
1561
|
+
};
|
|
1562
|
+
}
|
|
1563
|
+
/** The mode each member renders with in the next pass. */
|
|
1564
|
+
function passModes(trial, state) {
|
|
1565
|
+
const modes = new Map(trial);
|
|
1566
|
+
for (const [k, mode] of state.decided) modes.set(k, mode);
|
|
1567
|
+
return modes;
|
|
1568
|
+
}
|
|
1569
|
+
/**
|
|
1570
|
+
* One step of the decisions over a laid-out pass, from the left: checks the
|
|
1571
|
+
* segment changed in the previous step, then decides segments in order while
|
|
1572
|
+
* the pass already rendered them as decided, and stops at the first one
|
|
1573
|
+
* whose decision changes what the pass rendered. Returns whether another
|
|
1574
|
+
* pass is needed.
|
|
1575
|
+
*
|
|
1576
|
+
* Every pass lays the score out left to right, so a decision only moves
|
|
1577
|
+
* what comes after it: the segments decided before it stay where they
|
|
1578
|
+
* were. A segment is counted on the line the pass put it on, and a change
|
|
1579
|
+
* keeps it there: one that opened its line still opens it (as plain notes
|
|
1580
|
+
* it might fit the line before), and a member that no longer fits its line
|
|
1581
|
+
* in the new form (a pair turned plain is wider than its recitation) opens
|
|
1582
|
+
* the next one and is decided again there, the members before it keeping
|
|
1583
|
+
* their written form.
|
|
1584
|
+
*/
|
|
1585
|
+
function decideStep(syllables, placedNotes, sylLines, modes, state, options) {
|
|
1586
|
+
if (state.pending) {
|
|
1587
|
+
const { members, line } = state.pending;
|
|
1588
|
+
state.pending = null;
|
|
1589
|
+
const moved = members.findIndex((k) => sylLines[k] !== line);
|
|
1590
|
+
if (moved >= 0) {
|
|
1591
|
+
members.forEach((k, j) => {
|
|
1592
|
+
if (j < moved) state.decided.set(k, "written");
|
|
1593
|
+
else state.decided.delete(k);
|
|
1594
|
+
});
|
|
1595
|
+
state.breakBefore.add(members[moved]);
|
|
1596
|
+
return true;
|
|
1597
|
+
}
|
|
1598
|
+
}
|
|
1599
|
+
for (const segment of segmentsOf(syllables, placedNotes, sylLines, options)) {
|
|
1600
|
+
if (segment.members.every((k) => state.decided.has(k))) continue;
|
|
1601
|
+
const target = segment.count >= 3 ? "recite" : segment.count === 2 && options.twoSyllableRecitationsToPlain ? "plain" : "written";
|
|
1602
|
+
const changes = segment.members.some((k) => {
|
|
1603
|
+
const notes = syllables[k].notes;
|
|
1604
|
+
return shownAs(notes, modes.get(k) ?? "written") !== shownAs(notes, target);
|
|
1605
|
+
});
|
|
1606
|
+
for (const k of segment.members) state.decided.set(k, target);
|
|
1607
|
+
if (changes) {
|
|
1608
|
+
const first = segment.members[0];
|
|
1609
|
+
const line = sylLines[first];
|
|
1610
|
+
if (first > 0 && (sylLines[first - 1] ?? -1) < line) state.breakBefore.add(first);
|
|
1611
|
+
state.pending = {
|
|
1612
|
+
members: segment.members,
|
|
1613
|
+
line
|
|
1614
|
+
};
|
|
1615
|
+
return true;
|
|
1616
|
+
}
|
|
1617
|
+
}
|
|
1618
|
+
return false;
|
|
1619
|
+
}
|
|
1620
|
+
//#endregion
|
|
1905
1621
|
//#region src/chantLayout.ts
|
|
1906
1622
|
/**
|
|
1907
1623
|
* Special reciting notes: when such a note stands alone in a note group it
|
|
@@ -2043,6 +1759,12 @@ function ink(text, font) {
|
|
|
2043
1759
|
right: m.actualBoundingBoxRight
|
|
2044
1760
|
};
|
|
2045
1761
|
}
|
|
1762
|
+
var STAFF_FILLS = [
|
|
1763
|
+
"all-but-last",
|
|
1764
|
+
"none",
|
|
1765
|
+
"all",
|
|
1766
|
+
"longest"
|
|
1767
|
+
];
|
|
2046
1768
|
/** Each underlay's tokens, checked to share one melody. */
|
|
2047
1769
|
function underlayTokens(source) {
|
|
2048
1770
|
const list = typeof source === "string" || source instanceof GuidoScript ? [source] : source;
|
|
@@ -2088,12 +1810,51 @@ function silent(tok) {
|
|
|
2088
1810
|
* fonts cannot be loaded.
|
|
2089
1811
|
*/
|
|
2090
1812
|
async function layoutScore(source, options = {}) {
|
|
1813
|
+
const recitation = {
|
|
1814
|
+
twoSyllableRecitationsToPlain: options.twoSyllableRecitationsToPlain ?? true,
|
|
1815
|
+
plainRunsToRecitation: options.plainRunsToRecitation ?? false
|
|
1816
|
+
};
|
|
1817
|
+
const { syllables, pitchBreaks } = runSyllables(underlayTokens(source));
|
|
1818
|
+
if (!hasCandidates(syllables, recitation)) return (await layoutKept(source, options, NO_RECITATION)).result;
|
|
1819
|
+
const trial = trialModes(syllables, pitchBreaks, recitation);
|
|
1820
|
+
const state = initialState();
|
|
1821
|
+
for (let step = 0;; step++) {
|
|
1822
|
+
const modes = passModes(trial, state);
|
|
1823
|
+
const pass = await layoutKept(source, options, {
|
|
1824
|
+
notes: notesOverrides(syllables.map((s) => s.notes), modes),
|
|
1825
|
+
breakBefore: state.breakBefore
|
|
1826
|
+
});
|
|
1827
|
+
if (step > 4 * syllables.length + 8 || !decideStep(syllables, pass.placedNotes, pass.sylLines, modes, state, recitation)) return pass.result;
|
|
1828
|
+
}
|
|
1829
|
+
}
|
|
1830
|
+
/** The notes each syllable renders with under the recitation modes, where
|
|
1831
|
+
* they differ from the written ones. */
|
|
1832
|
+
function notesOverrides(written, modes) {
|
|
1833
|
+
const notes = /* @__PURE__ */ new Map();
|
|
1834
|
+
for (const [k, mode] of modes) {
|
|
1835
|
+
const shown = notesInMode(written[k], mode);
|
|
1836
|
+
if (shown !== written[k]) notes.set(k, shown);
|
|
1837
|
+
}
|
|
1838
|
+
return notes;
|
|
1839
|
+
}
|
|
1840
|
+
var NO_RECITATION = {
|
|
1841
|
+
notes: /* @__PURE__ */ new Map(),
|
|
1842
|
+
breakBefore: /* @__PURE__ */ new Set()
|
|
1843
|
+
};
|
|
1844
|
+
/** One layout with its `(@keep)` runs settled: a run is only known to fit
|
|
1845
|
+
* once it is placed, pads and group splits included, so a run that ends up
|
|
1846
|
+
* split is retried with a break before it. One wider than a fresh line
|
|
1847
|
+
* stops counting: a flat run wraps where it stood, and a run with inner
|
|
1848
|
+
* runs dissolves into them but keeps its break, so the boundary between
|
|
1849
|
+
* pairs still wins over the one between verses. Runs number in opening
|
|
1850
|
+
* order, so the lowest split is the leftmost and outermost. */
|
|
1851
|
+
async function layoutKept(source, options, recitation) {
|
|
2091
1852
|
const breakBefore = /* @__PURE__ */ new Set();
|
|
2092
1853
|
const wraps = /* @__PURE__ */ new Set();
|
|
2093
1854
|
for (;;) {
|
|
2094
|
-
const
|
|
2095
|
-
const open = splitRuns.filter((r) => !wraps.has(r.run));
|
|
2096
|
-
if (open.length === 0) return
|
|
1855
|
+
const pass = await layoutPass(source, options, breakBefore, recitation);
|
|
1856
|
+
const open = pass.splitRuns.filter((r) => !wraps.has(r.run));
|
|
1857
|
+
if (open.length === 0) return pass;
|
|
2097
1858
|
const split = open.reduce((a, b) => b.run < a.run ? b : a);
|
|
2098
1859
|
if (breakBefore.has(split.run) || split.atLineStart) {
|
|
2099
1860
|
wraps.add(split.run);
|
|
@@ -2104,16 +1865,19 @@ async function layoutScore(source, options = {}) {
|
|
|
2104
1865
|
}
|
|
2105
1866
|
/** One pass of `layoutScore`: `breakBefore` lists the `(@keep)` runs (by
|
|
2106
1867
|
* position in the script) that open a fresh line; `splitRuns` reports the
|
|
2107
|
-
* ones that crossed a line break.
|
|
2108
|
-
|
|
2109
|
-
|
|
1868
|
+
* ones that crossed a line break. `recitation` carries the recitation
|
|
1869
|
+
* segments' forms and breaks (issue #21). */
|
|
1870
|
+
async function layoutPass(source, options, breakBefore, recitation = NO_RECITATION) {
|
|
1871
|
+
const { style: styleName = "BNS", marks, bold = false, textPx = 18, notesPx = textPx * 2, maxWidthPx = Infinity, junctionDashes = 3, staffFill = "all-but-last", staffFillSnapPx = 0 } = options;
|
|
1872
|
+
if (!STAFF_FILLS.includes(staffFill)) throw new Error(`Unknown staffFill: "${staffFill}"`);
|
|
1873
|
+
if (!(staffFillSnapPx >= 0 && Number.isFinite(staffFillSnapPx))) throw new Error(`staffFillSnapPx must be zero or more: ${staffFillSnapPx}`);
|
|
2110
1874
|
const junctionThirds = Math.round(junctionDashes * 3);
|
|
2111
1875
|
if (!(junctionDashes >= 1 && Math.abs(junctionDashes - junctionThirds / 3) <= .01)) throw new Error(`junctionDashes must be at least 1, in steps of a third: ${junctionDashes}`);
|
|
2112
1876
|
const fullJunction = junctionSpacer(junctionThirds);
|
|
2113
1877
|
const reducedJunction = junctionSpacer(Math.max(3, junctionThirds - 3));
|
|
2114
1878
|
const underlays = underlayTokens(source);
|
|
2115
1879
|
const script = typeof source === "string" || source instanceof GuidoScript ? typeof source === "string" ? new GuidoScript(source) : source : typeof source[0] === "string" ? new GuidoScript(source[0]) : source[0];
|
|
2116
|
-
const styleSet =
|
|
1880
|
+
const styleSet = styleSetOf(styleName);
|
|
2117
1881
|
if (!styleSet) throw new Error(`Unknown style set: "${styleName}"`);
|
|
2118
1882
|
for (const font of fontStringsToVerify(styleSet, textPx)) {
|
|
2119
1883
|
try {
|
|
@@ -2124,10 +1888,23 @@ async function layoutPass(source, options, breakBefore) {
|
|
|
2124
1888
|
const notesFont = `400 ${notesPx}px ${styleSet.notesFontStack}`;
|
|
2125
1889
|
const textStack = styleSet.textFontStack;
|
|
2126
1890
|
const spaceFont = measurementFont({ weight: bold ? "700" : "400" }, textPx, textStack);
|
|
2127
|
-
const resolveParts = (
|
|
2128
|
-
const measureParts = (parts) => parts.reduce((w, part) => w + measure(part
|
|
1891
|
+
const resolveParts = (runs) => resolveTextRuns(runs, styleSet, bold, marks);
|
|
1892
|
+
const measureParts = (parts) => parts.reduce((w, part) => w + measure(drawnText(part), measurementFont(part.style, textPx, textStack)), 0);
|
|
2129
1893
|
const dashNotesWidth = measure("-", notesFont);
|
|
2130
1894
|
const dashTextWidth = measure("-", spaceFont);
|
|
1895
|
+
/** The hyphen after a syllable: plain, unless a `{…}` span runs on
|
|
1896
|
+
* across it, when it takes the span's look and that look's width. */
|
|
1897
|
+
const hyphenOf = (hyphenMarks) => {
|
|
1898
|
+
if (!hyphenMarks) return { width: dashTextWidth };
|
|
1899
|
+
const parts = resolveParts([{
|
|
1900
|
+
text: "-",
|
|
1901
|
+
marks: hyphenMarks
|
|
1902
|
+
}]);
|
|
1903
|
+
return {
|
|
1904
|
+
parts,
|
|
1905
|
+
width: measureParts(parts)
|
|
1906
|
+
};
|
|
1907
|
+
};
|
|
2131
1908
|
let clefGlyph = script.clef?.[0];
|
|
2132
1909
|
let keyGlyphs = [...(script.clef ?? "").slice(1)].filter(isModifierGlyph).join("");
|
|
2133
1910
|
let clefGroup = script.clef;
|
|
@@ -2141,7 +1918,8 @@ async function layoutPass(source, options, breakBefore) {
|
|
|
2141
1918
|
lastTextSyl: syls.reduce((last, s, i) => silent(s) ? last : i, -1),
|
|
2142
1919
|
cursor: 0,
|
|
2143
1920
|
pending: [],
|
|
2144
|
-
prevWordEnd: void 0
|
|
1921
|
+
prevWordEnd: void 0,
|
|
1922
|
+
hyphen: { width: dashTextWidth }
|
|
2145
1923
|
};
|
|
2146
1924
|
});
|
|
2147
1925
|
const lines = [];
|
|
@@ -2169,6 +1947,7 @@ async function layoutPass(source, options, breakBefore) {
|
|
|
2169
1947
|
segments: texts[0].segments,
|
|
2170
1948
|
textCursor: 0,
|
|
2171
1949
|
notesEnd: 0,
|
|
1950
|
+
staffEnd: 0,
|
|
2172
1951
|
lastWordEnd: null,
|
|
2173
1952
|
texts
|
|
2174
1953
|
};
|
|
@@ -2201,11 +1980,12 @@ async function layoutPass(source, options, breakBefore) {
|
|
|
2201
1980
|
if (tightDepth === 0) tightGroupPlaced = false;
|
|
2202
1981
|
}
|
|
2203
1982
|
} else {
|
|
1983
|
+
const i = sylIndex++;
|
|
2204
1984
|
const item = {
|
|
2205
1985
|
kind: "syl",
|
|
2206
1986
|
syl: tok,
|
|
2207
|
-
i
|
|
2208
|
-
notes: tok.notes,
|
|
1987
|
+
i,
|
|
1988
|
+
notes: recitation.notes.get(i) ?? tok.notes,
|
|
2209
1989
|
inserted: [],
|
|
2210
1990
|
first: true,
|
|
2211
1991
|
lastSeg: true,
|
|
@@ -2261,7 +2041,7 @@ async function layoutPass(source, options, breakBefore) {
|
|
|
2261
2041
|
tracks.forEach((t, u) => {
|
|
2262
2042
|
while (t.pending.length > 0) {
|
|
2263
2043
|
const p = t.pending[0];
|
|
2264
|
-
if (t.cursor > 0 && t.cursor + p.textWidth + (p.wordEnd ? 0 :
|
|
2044
|
+
if (t.cursor > 0 && t.cursor + p.textWidth + (p.wordEnd ? 0 : p.hyphen.width) > maxWidthPx) {
|
|
2265
2045
|
startLine();
|
|
2266
2046
|
continue;
|
|
2267
2047
|
}
|
|
@@ -2288,6 +2068,8 @@ async function layoutPass(source, options, breakBefore) {
|
|
|
2288
2068
|
t.cursor += p.spaceWidth;
|
|
2289
2069
|
}
|
|
2290
2070
|
text.lastWordEnd = p.wordEnd;
|
|
2071
|
+
text.hyphen = p.hyphen;
|
|
2072
|
+
t.hyphen = p.hyphen;
|
|
2291
2073
|
text.textCursor = t.cursor;
|
|
2292
2074
|
}
|
|
2293
2075
|
});
|
|
@@ -2300,8 +2082,15 @@ async function layoutPass(source, options, breakBefore) {
|
|
|
2300
2082
|
const run = keepStack.pop();
|
|
2301
2083
|
if (run && lines.length - 1 > run.line) splitRuns.push(run);
|
|
2302
2084
|
};
|
|
2085
|
+
const sylLines = [];
|
|
2086
|
+
let placedSyl = -1;
|
|
2087
|
+
const notePlacedLine = () => {
|
|
2088
|
+
if (placedSyl >= 0) sylLines[placedSyl] = lines.length - 1;
|
|
2089
|
+
placedSyl = -1;
|
|
2090
|
+
};
|
|
2303
2091
|
let idx = 0;
|
|
2304
2092
|
while (idx < items.length) {
|
|
2093
|
+
notePlacedLine();
|
|
2305
2094
|
const item = items[idx];
|
|
2306
2095
|
if (item.kind === "break") {
|
|
2307
2096
|
if (line.texts.some((x) => x.segments.length > 0) || tracks.some((t) => t.pending.length > 0)) {
|
|
@@ -2311,7 +2100,7 @@ async function layoutPass(source, options, breakBefore) {
|
|
|
2311
2100
|
idx++;
|
|
2312
2101
|
continue;
|
|
2313
2102
|
}
|
|
2314
|
-
if (item.kind === "tight") {
|
|
2103
|
+
if (item.kind === "tight" || item.kind === "aid") {
|
|
2315
2104
|
idx++;
|
|
2316
2105
|
continue;
|
|
2317
2106
|
}
|
|
@@ -2360,6 +2149,11 @@ async function layoutPass(source, options, breakBefore) {
|
|
|
2360
2149
|
}
|
|
2361
2150
|
const syl = item.syl;
|
|
2362
2151
|
const i = item.i;
|
|
2152
|
+
if (item.first) placedSyl = i;
|
|
2153
|
+
if (item.first && recitation.breakBefore.has(i) && (line.texts.some((x) => x.segments.length > 0) || tracks.some((t) => t.pending.length > 0))) {
|
|
2154
|
+
flushPending(false);
|
|
2155
|
+
forceBreak = true;
|
|
2156
|
+
}
|
|
2363
2157
|
if (forceBreak) {
|
|
2364
2158
|
startLine();
|
|
2365
2159
|
forceBreak = false;
|
|
@@ -2374,7 +2168,7 @@ async function layoutPass(source, options, breakBefore) {
|
|
|
2374
2168
|
const sung = tracks.map((t) => {
|
|
2375
2169
|
const own = t.syls[i];
|
|
2376
2170
|
const last = item.lastSeg && i >= t.lastTextSyl;
|
|
2377
|
-
const sylParts = resolveParts(item.first ? own.
|
|
2171
|
+
const sylParts = resolveParts(item.first ? own.runs : []);
|
|
2378
2172
|
return {
|
|
2379
2173
|
t,
|
|
2380
2174
|
own,
|
|
@@ -2396,13 +2190,14 @@ async function layoutPass(source, options, breakBefore) {
|
|
|
2396
2190
|
sylParts: s.sylParts,
|
|
2397
2191
|
textWidth: s.textWidth,
|
|
2398
2192
|
spaceWidth: s.spaceWidth,
|
|
2399
|
-
wordEnd: s.own.wordEnd
|
|
2193
|
+
wordEnd: s.own.wordEnd,
|
|
2194
|
+
hyphen: hyphenOf(s.own.hyphen)
|
|
2400
2195
|
});
|
|
2401
2196
|
}
|
|
2402
2197
|
idx++;
|
|
2403
2198
|
continue;
|
|
2404
2199
|
}
|
|
2405
|
-
if (item.first && item.lastSeg && !syl.empty && syl.
|
|
2200
|
+
if (item.first && item.lastSeg && !syl.empty && syl.runs.length === 0 && splitNoteMarkers(item.notes).core === "") {
|
|
2406
2201
|
const last = notesSoFar[notesSoFar.length - 1];
|
|
2407
2202
|
const sep = last && last !== "-" && !lastGroupTrailingMarker && !syl.notes.startsWith("-") ? "-" : "";
|
|
2408
2203
|
line.notesParts.push(sep + syl.notes);
|
|
@@ -2456,7 +2251,7 @@ async function layoutPass(source, options, breakBefore) {
|
|
|
2456
2251
|
}
|
|
2457
2252
|
const voiced = item.first ? sung.filter((s) => !silent(s.own)) : sung;
|
|
2458
2253
|
const runStart = placedNotes[i - 1] !== item.notes;
|
|
2459
|
-
const specialFits = voiced.every((s) => s.t.cursor === 0 || s.t.cursor + s.pendingTotal + s.textWidth + (s.own.wordEnd ? 0 :
|
|
2254
|
+
const specialFits = voiced.every((s) => s.t.cursor === 0 || s.t.cursor + s.pendingTotal + s.textWidth + (s.own.wordEnd ? 0 : hyphenOf(s.own.hyphen).width) <= maxWidthPx);
|
|
2460
2255
|
if (special && !runStart && line.notesParts.length > 0 && !specialFits) startLine();
|
|
2461
2256
|
if (!item.first) {
|
|
2462
2257
|
for (;;) {
|
|
@@ -2501,44 +2296,54 @@ async function layoutPass(source, options, breakBefore) {
|
|
|
2501
2296
|
};
|
|
2502
2297
|
};
|
|
2503
2298
|
let placed = place();
|
|
2504
|
-
|
|
2505
|
-
|
|
2506
|
-
|
|
2507
|
-
|
|
2508
|
-
|
|
2509
|
-
|
|
2510
|
-
|
|
2511
|
-
|
|
2512
|
-
|
|
2513
|
-
|
|
2514
|
-
|
|
2515
|
-
|
|
2516
|
-
}
|
|
2517
|
-
}
|
|
2518
|
-
if (line.notesParts.length === 0 && tracks.every((t) => t.cursor === 0)) {
|
|
2299
|
+
const uncut = {
|
|
2300
|
+
notes: item.notes,
|
|
2301
|
+
inserted: item.inserted,
|
|
2302
|
+
lastSeg: item.lastSeg,
|
|
2303
|
+
forceBreak,
|
|
2304
|
+
items: items.length
|
|
2305
|
+
};
|
|
2306
|
+
const fit = () => {
|
|
2307
|
+
for (;;) {
|
|
2308
|
+
const notesOver = measure(notesSoFar + junctionPrefix + item.notes, notesFont) > maxWidthPx;
|
|
2309
|
+
const textOver = voiced.some((s) => placed.textStartOf(s.textWidth) + s.textWidth + (s.own.wordEnd ? 0 : hyphenOf(s.own.hyphen).width) > maxWidthPx);
|
|
2310
|
+
if (!notesOver && !textOver) break;
|
|
2519
2311
|
if (notesOver) {
|
|
2520
|
-
const cut = findBreakCut(item.notes, maxWidthPx, (prefix) => measure(notesSoFar + prefix, notesFont));
|
|
2312
|
+
const cut = findBreakCut(item.notes, maxWidthPx, (prefix) => measure(notesSoFar + junctionPrefix + prefix, notesFont));
|
|
2521
2313
|
if (cut >= 0 && cut + 1 < item.notes.length) {
|
|
2522
2314
|
const rest = applyBreakCut(item, cut);
|
|
2523
|
-
item.notes = item.notes.slice(0, cut);
|
|
2524
2315
|
queueContinuation(item, idx, rest);
|
|
2525
2316
|
parts = splitNoteMarkers(item.notes);
|
|
2526
2317
|
placed = place();
|
|
2318
|
+
break;
|
|
2319
|
+
}
|
|
2320
|
+
}
|
|
2321
|
+
if (line.notesParts.length === 0 && tracks.every((t) => t.cursor === 0)) {
|
|
2322
|
+
if (notesOver) {
|
|
2323
|
+
const cut = findBreakCut(item.notes, maxWidthPx, (prefix) => measure(notesSoFar + prefix, notesFont));
|
|
2324
|
+
if (cut >= 0 && cut + 1 < item.notes.length) {
|
|
2325
|
+
const rest = applyBreakCut(item, cut);
|
|
2326
|
+
item.notes = item.notes.slice(0, cut);
|
|
2327
|
+
queueContinuation(item, idx, rest);
|
|
2328
|
+
parts = splitNoteMarkers(item.notes);
|
|
2329
|
+
placed = place();
|
|
2330
|
+
}
|
|
2527
2331
|
}
|
|
2332
|
+
break;
|
|
2528
2333
|
}
|
|
2529
|
-
|
|
2334
|
+
startLine();
|
|
2335
|
+
junctionPrefix = "";
|
|
2336
|
+
parts = splitNoteMarkers(item.notes);
|
|
2337
|
+
placed = place();
|
|
2530
2338
|
}
|
|
2531
|
-
|
|
2532
|
-
|
|
2533
|
-
parts = splitNoteMarkers(item.notes);
|
|
2534
|
-
placed = place();
|
|
2535
|
-
}
|
|
2339
|
+
};
|
|
2340
|
+
fit();
|
|
2536
2341
|
const decide = (s) => {
|
|
2537
2342
|
const { t, textWidth } = s;
|
|
2538
2343
|
const { contentWidth } = placed;
|
|
2539
2344
|
const connectorDash = line.notesParts.length > 0 && !t.prevWordEnd && s.pendingTotal === 0;
|
|
2540
|
-
const minStart = t.cursor +
|
|
2541
|
-
const minTextStart = t.cursor + s.pendingTotal + (prevRecited && connectorDash ?
|
|
2345
|
+
const minStart = t.cursor + t.hyphen.width;
|
|
2346
|
+
const minTextStart = t.cursor + s.pendingTotal + (prevRecited && connectorDash ? t.hyphen.width : 0);
|
|
2542
2347
|
let textStart = placed.textStartOf(textWidth);
|
|
2543
2348
|
let contentStart = placed.contentStart;
|
|
2544
2349
|
let pads = 0;
|
|
@@ -2622,12 +2427,21 @@ async function layoutPass(source, options, breakBefore) {
|
|
|
2622
2427
|
};
|
|
2623
2428
|
settle();
|
|
2624
2429
|
if (line.notesParts.length > 0 && (measure(notesSoFar + junctionPrefix + "-".repeat(padDashes) + item.notes, notesFont) > maxWidthPx || voiced.some((s, v) => {
|
|
2625
|
-
return Math.max(placements[v].textStart, s.t.cursor + s.pendingTotal) + s.textWidth + (s.own.wordEnd ? 0 :
|
|
2430
|
+
return Math.max(placements[v].textStart, s.t.cursor + s.pendingTotal) + s.textWidth + (s.own.wordEnd ? 0 : hyphenOf(s.own.hyphen).width) > maxWidthPx;
|
|
2626
2431
|
}))) {
|
|
2432
|
+
const recut = items.length > uncut.items;
|
|
2433
|
+
if (recut) {
|
|
2434
|
+
items.splice(idx + 1, items.length - uncut.items);
|
|
2435
|
+
item.notes = uncut.notes;
|
|
2436
|
+
item.inserted = uncut.inserted;
|
|
2437
|
+
item.lastSeg = uncut.lastSeg;
|
|
2438
|
+
forceBreak = uncut.forceBreak;
|
|
2439
|
+
}
|
|
2627
2440
|
startLine();
|
|
2628
2441
|
junctionPrefix = "";
|
|
2629
2442
|
parts = splitNoteMarkers(item.notes);
|
|
2630
2443
|
placed = place();
|
|
2444
|
+
if (recut) fit();
|
|
2631
2445
|
settle();
|
|
2632
2446
|
}
|
|
2633
2447
|
}
|
|
@@ -2655,12 +2469,13 @@ async function layoutPass(source, options, breakBefore) {
|
|
|
2655
2469
|
textStartPx: textStart
|
|
2656
2470
|
});
|
|
2657
2471
|
else if (pendingTotal === 0 && p.tight) {} else if (pendingTotal === 0) {
|
|
2658
|
-
const pad = Math.max(0, (spacerWidth -
|
|
2472
|
+
const pad = Math.max(0, (spacerWidth - t.hyphen.width) / 2);
|
|
2659
2473
|
text.segments.push({
|
|
2660
2474
|
kind: "connector",
|
|
2661
2475
|
pad,
|
|
2662
2476
|
gapWidth: spacerWidth,
|
|
2663
|
-
dashWidth:
|
|
2477
|
+
dashWidth: t.hyphen.width,
|
|
2478
|
+
...t.hyphen.parts ? { parts: t.hyphen.parts } : {},
|
|
2664
2479
|
dashStartPx: t.cursor + pad,
|
|
2665
2480
|
nextAnchorPx: textStart
|
|
2666
2481
|
});
|
|
@@ -2706,6 +2521,8 @@ async function layoutPass(source, options, breakBefore) {
|
|
|
2706
2521
|
t.cursor = textStart + textWidth + spaceWidth;
|
|
2707
2522
|
text.lastWordEnd = s.own.wordEnd;
|
|
2708
2523
|
t.prevWordEnd = s.own.wordEnd;
|
|
2524
|
+
t.hyphen = hyphenOf(s.own.hyphen);
|
|
2525
|
+
text.hyphen = t.hyphen;
|
|
2709
2526
|
});
|
|
2710
2527
|
for (const s of sung) if (!voiced.includes(s)) keepWordState(s, fresh);
|
|
2711
2528
|
notesSoFar += notesStr;
|
|
@@ -2718,6 +2535,7 @@ async function layoutPass(source, options, breakBefore) {
|
|
|
2718
2535
|
if (!item.lastSeg) forceBreak = true;
|
|
2719
2536
|
idx++;
|
|
2720
2537
|
}
|
|
2538
|
+
notePlacedLine();
|
|
2721
2539
|
flushPending(true);
|
|
2722
2540
|
while (keepStack.length > 0) closeKeepRun();
|
|
2723
2541
|
tracks.forEach((t, u) => {
|
|
@@ -2728,11 +2546,13 @@ async function layoutPass(source, options, breakBefore) {
|
|
|
2728
2546
|
if (li === lines.length - 1) return;
|
|
2729
2547
|
for (const text of l.texts) {
|
|
2730
2548
|
if (text.lastWordEnd !== false) continue;
|
|
2731
|
-
const
|
|
2549
|
+
const hyphen = text.hyphen ?? { width: dashTextWidth };
|
|
2550
|
+
const pad = l.notesEnd > text.textCursor ? Math.max(0, (l.notesEnd - text.textCursor - hyphen.width) / 2) : 0;
|
|
2732
2551
|
text.segments.push({
|
|
2733
2552
|
kind: "line-end-dash",
|
|
2734
2553
|
pad,
|
|
2735
|
-
dashWidth:
|
|
2554
|
+
dashWidth: hyphen.width,
|
|
2555
|
+
...hyphen.parts ? { parts: hyphen.parts } : {},
|
|
2736
2556
|
dashStartPx: text.textCursor + pad,
|
|
2737
2557
|
trailEndPx: l.notesEnd,
|
|
2738
2558
|
textEndPx: text.textCursor
|
|
@@ -2743,20 +2563,36 @@ async function layoutPass(source, options, breakBefore) {
|
|
|
2743
2563
|
l.textCursor = l.texts[0].textCursor;
|
|
2744
2564
|
l.lastWordEnd = l.texts[0].lastWordEnd;
|
|
2745
2565
|
}
|
|
2746
|
-
const
|
|
2566
|
+
const padding = styleSet.padding ?? 0;
|
|
2567
|
+
const starTarget = maxWidthPx + padding;
|
|
2747
2568
|
const starWidth = measure("*", notesFont);
|
|
2569
|
+
const fill = staffFill === "all" && maxWidthPx === Infinity ? "longest" : staffFill;
|
|
2570
|
+
const fillsLast = fill === "all" || fill === "longest";
|
|
2571
|
+
const longest = Math.max(...lines.map((l) => Math.max(l.notesEnd, ...l.texts.map((t) => t.textCursor))));
|
|
2572
|
+
const fillTarget = fill === "none" || maxWidthPx === Infinity && !fillsLast ? null : fill === "longest" && maxWidthPx - longest > staffFillSnapPx ? longest : maxWidthPx;
|
|
2748
2573
|
lines.forEach((l, li) => {
|
|
2749
|
-
|
|
2750
|
-
|
|
2751
|
-
|
|
2574
|
+
const last = li === lines.length - 1;
|
|
2575
|
+
l.staffEnd = l.notesEnd;
|
|
2576
|
+
if (last && (!fillsLast || l.notesParts.length === 0)) return;
|
|
2577
|
+
if (fillTarget === null) {
|
|
2578
|
+
l.breakMarkerLeftPx = padding;
|
|
2752
2579
|
return;
|
|
2753
2580
|
}
|
|
2581
|
+
const staffEdge = fillTarget + padding;
|
|
2754
2582
|
let lineEnd = measure(l.clefPrefix + l.notesParts.join(""), notesFont);
|
|
2755
|
-
while (lineEnd + dashNotesWidth <=
|
|
2583
|
+
while (last ? lineEnd + dashNotesWidth <= staffEdge : lineEnd + dashNotesWidth <= staffEdge - starWidth && lineEnd + dashNotesWidth < fillTarget) {
|
|
2756
2584
|
l.notesParts.push("-");
|
|
2757
2585
|
lineEnd += dashNotesWidth;
|
|
2758
2586
|
}
|
|
2759
|
-
|
|
2587
|
+
if (last) {
|
|
2588
|
+
const third = measure("¨", notesFont);
|
|
2589
|
+
while (lineEnd + third <= staffEdge) {
|
|
2590
|
+
l.notesParts.push("¨");
|
|
2591
|
+
lineEnd += third;
|
|
2592
|
+
}
|
|
2593
|
+
}
|
|
2594
|
+
l.staffEnd = lineEnd;
|
|
2595
|
+
if (!last) l.breakMarkerLeftPx = staffEdge - lineEnd - starWidth;
|
|
2760
2596
|
});
|
|
2761
2597
|
return {
|
|
2762
2598
|
result: {
|
|
@@ -2771,8 +2607,10 @@ async function layoutPass(source, options, breakBefore) {
|
|
|
2771
2607
|
starTarget,
|
|
2772
2608
|
starWidth
|
|
2773
2609
|
},
|
|
2774
|
-
splitRuns
|
|
2610
|
+
splitRuns,
|
|
2611
|
+
placedNotes,
|
|
2612
|
+
sylLines
|
|
2775
2613
|
};
|
|
2776
2614
|
}
|
|
2777
2615
|
//#endregion
|
|
2778
|
-
export {
|
|
2616
|
+
export { CLEF_GLYPHS as A, toCephalicus as B, scriptSyllables as C, resolveSpans as D, unescape as E, isModifierGlyph as F, toVirga as G, toLonga as H, modifyDown as I, variantOf as K, modifyNatural as L, baseOf as M, gClefPitchOf as N, SCORE_MARKERS as O, isClefGlyph as P, modifyUp as R, runProperties as S, syllableText as T, toSmallHead as U, toConnectDown as V, toSmallPoint as W, GuidoScript as _, DEFAULT_STYLE_SET as a, isC4ClefGroup as b, defineStyleSet as c, measurementFont as d, resolveMarks as f, unknownMarks as g, styleSetOf as h, measure as i, STAFF as j, splitNoteMarkers as k, inlineStylesFor as l, resolveTextRuns as m, SPECIAL_NOTES as n, SMALL_CAP_SCALE as o, resolveSyllableParts as p, layoutScore as r, STYLE_SETS as s, NOTE_MODIFIERS as t, kindOf as u, chantText as v, statementSource as w, isStatementGroup as x, escape as y, pitchLetterOf as z };
|