guidex 0.3.3 → 0.4.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +105 -34
- package/dist/lib/app/editorState.d.ts +22 -23
- package/dist/lib/app/shareLink.d.ts +2 -3
- package/dist/lib/{chantLayout-CVVuJUJl.js → chantLayout-g_xuQ9nD.js} +683 -1112
- package/dist/lib/chantLayout.d.ts +30 -2
- package/dist/lib/chantMarkup.d.ts +63 -20
- package/dist/lib/editorStorage.d.ts +9 -4
- package/dist/lib/embed/core.d.ts +16 -3
- package/dist/lib/embed/index.js +1 -1
- package/dist/lib/embed/manual.d.ts +6 -3
- package/dist/lib/embed/manual.js +16 -49
- package/dist/lib/guidoRender.d.ts +11 -3
- 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 +12 -5
- package/dist/lib/react/index.d.ts +2 -0
- package/dist/lib/react/index.js +10 -2
- package/dist/lib/scoreImage-Dz3c55Zi.js +1985 -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-B_9vyTt6.js} +38 -13
- package/dist/lib/wordExport.d.ts +17 -4
- package/dist-embed/guidex-embed.min.js +1 -1
- package/package.json +1 -1
- 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;
|
|
705
|
-
}
|
|
706
|
-
return out;
|
|
707
|
-
}
|
|
708
|
-
/** The sung text of the script: the clef stripped, syllables joined
|
|
709
|
-
* into words, and the words joined with single spaces. */
|
|
710
|
-
function chantText(script) {
|
|
711
|
-
return new GuidoScript(script).words.map((word) => word.text).join(" ");
|
|
712
|
-
}
|
|
713
|
-
/** Folds backslash escapes into the characters they stand for. Shared
|
|
714
|
-
* with the transforms module (guidoScriptTransforms.ts). */
|
|
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
|
-
});
|
|
1005
|
+
all.push(holder);
|
|
814
1006
|
}
|
|
815
|
-
const
|
|
816
|
-
|
|
817
|
-
|
|
818
|
-
|
|
819
|
-
|
|
820
|
-
|
|
821
|
-
|
|
822
|
-
|
|
823
|
-
|
|
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];
|
|
1172
|
-
return {
|
|
1173
|
-
position: STAFF.indexOf("2") + 2 * (entry.line - 1),
|
|
1174
|
-
step
|
|
1175
|
-
};
|
|
1176
|
-
}
|
|
1177
|
-
function stepAt(position, clef) {
|
|
1178
|
-
const anchor = clefAnchor(clef);
|
|
1179
|
-
return anchor.step + position - anchor.position;
|
|
1180
|
-
}
|
|
1181
|
-
function positionOf(step, clef) {
|
|
1182
|
-
const anchor = clefAnchor(clef);
|
|
1183
|
-
return anchor.position + step - anchor.step;
|
|
1184
|
-
}
|
|
1185
|
-
/** The staff position and variant role of a pitched glyph (`'base'` for
|
|
1186
|
-
* a base glyph), or undefined for a glyph without pitch. */
|
|
1187
|
-
function glyphOf(ch) {
|
|
1188
|
-
if (STAFF.includes(ch)) return {
|
|
1189
|
-
position: STAFF.indexOf(ch),
|
|
1190
|
-
role: "base"
|
|
1191
|
-
};
|
|
1192
|
-
const entry = baseOf(ch);
|
|
1193
|
-
return entry ? {
|
|
1194
|
-
position: STAFF.indexOf(entry.base),
|
|
1195
|
-
role: entry.role
|
|
1196
|
-
} : void 0;
|
|
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;
|
|
1035
|
+
/** A syllable's text fields, from its resolved text. */
|
|
1036
|
+
function textFields(piece) {
|
|
1037
|
+
const { text, source, runs, hyphen } = piece;
|
|
1286
1038
|
return {
|
|
1287
|
-
|
|
1288
|
-
|
|
1039
|
+
text,
|
|
1040
|
+
source,
|
|
1041
|
+
runs,
|
|
1042
|
+
...hyphen ? { hyphen } : {}
|
|
1289
1043
|
};
|
|
1290
1044
|
}
|
|
1291
|
-
|
|
1292
|
-
function
|
|
1293
|
-
return
|
|
1294
|
-
}
|
|
1295
|
-
|
|
1296
|
-
|
|
1297
|
-
|
|
1298
|
-
|
|
1299
|
-
|
|
1300
|
-
|
|
1301
|
-
|
|
1302
|
-
|
|
1303
|
-
|
|
1304
|
-
|
|
1305
|
-
|
|
1306
|
-
|
|
1307
|
-
if (
|
|
1308
|
-
|
|
1309
|
-
|
|
1310
|
-
|
|
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);
|
|
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;
|
|
1353
1072
|
}
|
|
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
1073
|
}
|
|
1406
|
-
/**
|
|
1407
|
-
|
|
1408
|
-
|
|
1409
|
-
|
|
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)}`;
|
|
1525
|
-
}
|
|
1526
|
-
return source;
|
|
1527
|
-
}
|
|
1528
|
-
/**
|
|
1529
|
-
* The built-in transformations, keyed by name; pass the ones a feature
|
|
1530
|
-
* needs to the GuidoScript constructor.
|
|
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;
|
|
1870
1291
|
}
|
|
1871
|
-
|
|
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;
|
|
1307
|
+
}
|
|
1308
|
+
case "marker": return styleSet.markerGlyphs?.chars.has(text[0]) ? {
|
|
1309
|
+
...style,
|
|
1310
|
+
fontFamily: styleSet.markerGlyphs.fontFamily
|
|
1311
|
+
} : style;
|
|
1872
1312
|
}
|
|
1873
|
-
|
|
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
|
+
});
|
|
1349
|
+
}
|
|
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`;
|
|
@@ -2043,6 +1567,12 @@ function ink(text, font) {
|
|
|
2043
1567
|
right: m.actualBoundingBoxRight
|
|
2044
1568
|
};
|
|
2045
1569
|
}
|
|
1570
|
+
var STAFF_FILLS = [
|
|
1571
|
+
"all-but-last",
|
|
1572
|
+
"none",
|
|
1573
|
+
"all",
|
|
1574
|
+
"longest"
|
|
1575
|
+
];
|
|
2046
1576
|
/** Each underlay's tokens, checked to share one melody. */
|
|
2047
1577
|
function underlayTokens(source) {
|
|
2048
1578
|
const list = typeof source === "string" || source instanceof GuidoScript ? [source] : source;
|
|
@@ -2106,14 +1636,16 @@ async function layoutScore(source, options = {}) {
|
|
|
2106
1636
|
* position in the script) that open a fresh line; `splitRuns` reports the
|
|
2107
1637
|
* ones that crossed a line break. */
|
|
2108
1638
|
async function layoutPass(source, options, breakBefore) {
|
|
2109
|
-
const { style: styleName = "BNS", bold = false, textPx = 18, notesPx = textPx * 2, maxWidthPx = Infinity, junctionDashes = 3 } = options;
|
|
1639
|
+
const { style: styleName = "BNS", marks, bold = false, textPx = 18, notesPx = textPx * 2, maxWidthPx = Infinity, junctionDashes = 3, staffFill = "all-but-last", staffFillSnapPx = 0 } = options;
|
|
1640
|
+
if (!STAFF_FILLS.includes(staffFill)) throw new Error(`Unknown staffFill: "${staffFill}"`);
|
|
1641
|
+
if (!(staffFillSnapPx >= 0 && Number.isFinite(staffFillSnapPx))) throw new Error(`staffFillSnapPx must be zero or more: ${staffFillSnapPx}`);
|
|
2110
1642
|
const junctionThirds = Math.round(junctionDashes * 3);
|
|
2111
1643
|
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
1644
|
const fullJunction = junctionSpacer(junctionThirds);
|
|
2113
1645
|
const reducedJunction = junctionSpacer(Math.max(3, junctionThirds - 3));
|
|
2114
1646
|
const underlays = underlayTokens(source);
|
|
2115
1647
|
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 =
|
|
1648
|
+
const styleSet = styleSetOf(styleName);
|
|
2117
1649
|
if (!styleSet) throw new Error(`Unknown style set: "${styleName}"`);
|
|
2118
1650
|
for (const font of fontStringsToVerify(styleSet, textPx)) {
|
|
2119
1651
|
try {
|
|
@@ -2124,10 +1656,23 @@ async function layoutPass(source, options, breakBefore) {
|
|
|
2124
1656
|
const notesFont = `400 ${notesPx}px ${styleSet.notesFontStack}`;
|
|
2125
1657
|
const textStack = styleSet.textFontStack;
|
|
2126
1658
|
const spaceFont = measurementFont({ weight: bold ? "700" : "400" }, textPx, textStack);
|
|
2127
|
-
const resolveParts = (
|
|
2128
|
-
const measureParts = (parts) => parts.reduce((w, part) => w + measure(part
|
|
1659
|
+
const resolveParts = (runs) => resolveTextRuns(runs, styleSet, bold, marks);
|
|
1660
|
+
const measureParts = (parts) => parts.reduce((w, part) => w + measure(drawnText(part), measurementFont(part.style, textPx, textStack)), 0);
|
|
2129
1661
|
const dashNotesWidth = measure("-", notesFont);
|
|
2130
1662
|
const dashTextWidth = measure("-", spaceFont);
|
|
1663
|
+
/** The hyphen after a syllable: plain, unless a `{…}` span runs on
|
|
1664
|
+
* across it, when it takes the span's look and that look's width. */
|
|
1665
|
+
const hyphenOf = (hyphenMarks) => {
|
|
1666
|
+
if (!hyphenMarks) return { width: dashTextWidth };
|
|
1667
|
+
const parts = resolveParts([{
|
|
1668
|
+
text: "-",
|
|
1669
|
+
marks: hyphenMarks
|
|
1670
|
+
}]);
|
|
1671
|
+
return {
|
|
1672
|
+
parts,
|
|
1673
|
+
width: measureParts(parts)
|
|
1674
|
+
};
|
|
1675
|
+
};
|
|
2131
1676
|
let clefGlyph = script.clef?.[0];
|
|
2132
1677
|
let keyGlyphs = [...(script.clef ?? "").slice(1)].filter(isModifierGlyph).join("");
|
|
2133
1678
|
let clefGroup = script.clef;
|
|
@@ -2141,7 +1686,8 @@ async function layoutPass(source, options, breakBefore) {
|
|
|
2141
1686
|
lastTextSyl: syls.reduce((last, s, i) => silent(s) ? last : i, -1),
|
|
2142
1687
|
cursor: 0,
|
|
2143
1688
|
pending: [],
|
|
2144
|
-
prevWordEnd: void 0
|
|
1689
|
+
prevWordEnd: void 0,
|
|
1690
|
+
hyphen: { width: dashTextWidth }
|
|
2145
1691
|
};
|
|
2146
1692
|
});
|
|
2147
1693
|
const lines = [];
|
|
@@ -2169,6 +1715,7 @@ async function layoutPass(source, options, breakBefore) {
|
|
|
2169
1715
|
segments: texts[0].segments,
|
|
2170
1716
|
textCursor: 0,
|
|
2171
1717
|
notesEnd: 0,
|
|
1718
|
+
staffEnd: 0,
|
|
2172
1719
|
lastWordEnd: null,
|
|
2173
1720
|
texts
|
|
2174
1721
|
};
|
|
@@ -2261,7 +1808,7 @@ async function layoutPass(source, options, breakBefore) {
|
|
|
2261
1808
|
tracks.forEach((t, u) => {
|
|
2262
1809
|
while (t.pending.length > 0) {
|
|
2263
1810
|
const p = t.pending[0];
|
|
2264
|
-
if (t.cursor > 0 && t.cursor + p.textWidth + (p.wordEnd ? 0 :
|
|
1811
|
+
if (t.cursor > 0 && t.cursor + p.textWidth + (p.wordEnd ? 0 : p.hyphen.width) > maxWidthPx) {
|
|
2265
1812
|
startLine();
|
|
2266
1813
|
continue;
|
|
2267
1814
|
}
|
|
@@ -2288,6 +1835,8 @@ async function layoutPass(source, options, breakBefore) {
|
|
|
2288
1835
|
t.cursor += p.spaceWidth;
|
|
2289
1836
|
}
|
|
2290
1837
|
text.lastWordEnd = p.wordEnd;
|
|
1838
|
+
text.hyphen = p.hyphen;
|
|
1839
|
+
t.hyphen = p.hyphen;
|
|
2291
1840
|
text.textCursor = t.cursor;
|
|
2292
1841
|
}
|
|
2293
1842
|
});
|
|
@@ -2311,7 +1860,7 @@ async function layoutPass(source, options, breakBefore) {
|
|
|
2311
1860
|
idx++;
|
|
2312
1861
|
continue;
|
|
2313
1862
|
}
|
|
2314
|
-
if (item.kind === "tight") {
|
|
1863
|
+
if (item.kind === "tight" || item.kind === "aid") {
|
|
2315
1864
|
idx++;
|
|
2316
1865
|
continue;
|
|
2317
1866
|
}
|
|
@@ -2374,7 +1923,7 @@ async function layoutPass(source, options, breakBefore) {
|
|
|
2374
1923
|
const sung = tracks.map((t) => {
|
|
2375
1924
|
const own = t.syls[i];
|
|
2376
1925
|
const last = item.lastSeg && i >= t.lastTextSyl;
|
|
2377
|
-
const sylParts = resolveParts(item.first ? own.
|
|
1926
|
+
const sylParts = resolveParts(item.first ? own.runs : []);
|
|
2378
1927
|
return {
|
|
2379
1928
|
t,
|
|
2380
1929
|
own,
|
|
@@ -2396,13 +1945,14 @@ async function layoutPass(source, options, breakBefore) {
|
|
|
2396
1945
|
sylParts: s.sylParts,
|
|
2397
1946
|
textWidth: s.textWidth,
|
|
2398
1947
|
spaceWidth: s.spaceWidth,
|
|
2399
|
-
wordEnd: s.own.wordEnd
|
|
1948
|
+
wordEnd: s.own.wordEnd,
|
|
1949
|
+
hyphen: hyphenOf(s.own.hyphen)
|
|
2400
1950
|
});
|
|
2401
1951
|
}
|
|
2402
1952
|
idx++;
|
|
2403
1953
|
continue;
|
|
2404
1954
|
}
|
|
2405
|
-
if (item.first && item.lastSeg && !syl.empty && syl.
|
|
1955
|
+
if (item.first && item.lastSeg && !syl.empty && syl.runs.length === 0 && splitNoteMarkers(item.notes).core === "") {
|
|
2406
1956
|
const last = notesSoFar[notesSoFar.length - 1];
|
|
2407
1957
|
const sep = last && last !== "-" && !lastGroupTrailingMarker && !syl.notes.startsWith("-") ? "-" : "";
|
|
2408
1958
|
line.notesParts.push(sep + syl.notes);
|
|
@@ -2456,7 +2006,7 @@ async function layoutPass(source, options, breakBefore) {
|
|
|
2456
2006
|
}
|
|
2457
2007
|
const voiced = item.first ? sung.filter((s) => !silent(s.own)) : sung;
|
|
2458
2008
|
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 :
|
|
2009
|
+
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
2010
|
if (special && !runStart && line.notesParts.length > 0 && !specialFits) startLine();
|
|
2461
2011
|
if (!item.first) {
|
|
2462
2012
|
for (;;) {
|
|
@@ -2503,7 +2053,7 @@ async function layoutPass(source, options, breakBefore) {
|
|
|
2503
2053
|
let placed = place();
|
|
2504
2054
|
for (;;) {
|
|
2505
2055
|
const notesOver = measure(notesSoFar + junctionPrefix + item.notes, notesFont) > maxWidthPx;
|
|
2506
|
-
const textOver = voiced.some((s) => placed.textStartOf(s.textWidth) + s.textWidth + (s.own.wordEnd ? 0 :
|
|
2056
|
+
const textOver = voiced.some((s) => placed.textStartOf(s.textWidth) + s.textWidth + (s.own.wordEnd ? 0 : hyphenOf(s.own.hyphen).width) > maxWidthPx);
|
|
2507
2057
|
if (!notesOver && !textOver) break;
|
|
2508
2058
|
if (notesOver) {
|
|
2509
2059
|
const cut = findBreakCut(item.notes, maxWidthPx, (prefix) => measure(notesSoFar + junctionPrefix + prefix, notesFont));
|
|
@@ -2537,8 +2087,8 @@ async function layoutPass(source, options, breakBefore) {
|
|
|
2537
2087
|
const { t, textWidth } = s;
|
|
2538
2088
|
const { contentWidth } = placed;
|
|
2539
2089
|
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 ?
|
|
2090
|
+
const minStart = t.cursor + t.hyphen.width;
|
|
2091
|
+
const minTextStart = t.cursor + s.pendingTotal + (prevRecited && connectorDash ? t.hyphen.width : 0);
|
|
2542
2092
|
let textStart = placed.textStartOf(textWidth);
|
|
2543
2093
|
let contentStart = placed.contentStart;
|
|
2544
2094
|
let pads = 0;
|
|
@@ -2622,7 +2172,7 @@ async function layoutPass(source, options, breakBefore) {
|
|
|
2622
2172
|
};
|
|
2623
2173
|
settle();
|
|
2624
2174
|
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 :
|
|
2175
|
+
return Math.max(placements[v].textStart, s.t.cursor + s.pendingTotal) + s.textWidth + (s.own.wordEnd ? 0 : hyphenOf(s.own.hyphen).width) > maxWidthPx;
|
|
2626
2176
|
}))) {
|
|
2627
2177
|
startLine();
|
|
2628
2178
|
junctionPrefix = "";
|
|
@@ -2655,12 +2205,13 @@ async function layoutPass(source, options, breakBefore) {
|
|
|
2655
2205
|
textStartPx: textStart
|
|
2656
2206
|
});
|
|
2657
2207
|
else if (pendingTotal === 0 && p.tight) {} else if (pendingTotal === 0) {
|
|
2658
|
-
const pad = Math.max(0, (spacerWidth -
|
|
2208
|
+
const pad = Math.max(0, (spacerWidth - t.hyphen.width) / 2);
|
|
2659
2209
|
text.segments.push({
|
|
2660
2210
|
kind: "connector",
|
|
2661
2211
|
pad,
|
|
2662
2212
|
gapWidth: spacerWidth,
|
|
2663
|
-
dashWidth:
|
|
2213
|
+
dashWidth: t.hyphen.width,
|
|
2214
|
+
...t.hyphen.parts ? { parts: t.hyphen.parts } : {},
|
|
2664
2215
|
dashStartPx: t.cursor + pad,
|
|
2665
2216
|
nextAnchorPx: textStart
|
|
2666
2217
|
});
|
|
@@ -2706,6 +2257,8 @@ async function layoutPass(source, options, breakBefore) {
|
|
|
2706
2257
|
t.cursor = textStart + textWidth + spaceWidth;
|
|
2707
2258
|
text.lastWordEnd = s.own.wordEnd;
|
|
2708
2259
|
t.prevWordEnd = s.own.wordEnd;
|
|
2260
|
+
t.hyphen = hyphenOf(s.own.hyphen);
|
|
2261
|
+
text.hyphen = t.hyphen;
|
|
2709
2262
|
});
|
|
2710
2263
|
for (const s of sung) if (!voiced.includes(s)) keepWordState(s, fresh);
|
|
2711
2264
|
notesSoFar += notesStr;
|
|
@@ -2728,11 +2281,13 @@ async function layoutPass(source, options, breakBefore) {
|
|
|
2728
2281
|
if (li === lines.length - 1) return;
|
|
2729
2282
|
for (const text of l.texts) {
|
|
2730
2283
|
if (text.lastWordEnd !== false) continue;
|
|
2731
|
-
const
|
|
2284
|
+
const hyphen = text.hyphen ?? { width: dashTextWidth };
|
|
2285
|
+
const pad = l.notesEnd > text.textCursor ? Math.max(0, (l.notesEnd - text.textCursor - hyphen.width) / 2) : 0;
|
|
2732
2286
|
text.segments.push({
|
|
2733
2287
|
kind: "line-end-dash",
|
|
2734
2288
|
pad,
|
|
2735
|
-
dashWidth:
|
|
2289
|
+
dashWidth: hyphen.width,
|
|
2290
|
+
...hyphen.parts ? { parts: hyphen.parts } : {},
|
|
2736
2291
|
dashStartPx: text.textCursor + pad,
|
|
2737
2292
|
trailEndPx: l.notesEnd,
|
|
2738
2293
|
textEndPx: text.textCursor
|
|
@@ -2743,20 +2298,36 @@ async function layoutPass(source, options, breakBefore) {
|
|
|
2743
2298
|
l.textCursor = l.texts[0].textCursor;
|
|
2744
2299
|
l.lastWordEnd = l.texts[0].lastWordEnd;
|
|
2745
2300
|
}
|
|
2746
|
-
const
|
|
2301
|
+
const padding = styleSet.padding ?? 0;
|
|
2302
|
+
const starTarget = maxWidthPx + padding;
|
|
2747
2303
|
const starWidth = measure("*", notesFont);
|
|
2304
|
+
const fill = staffFill === "all" && maxWidthPx === Infinity ? "longest" : staffFill;
|
|
2305
|
+
const fillsLast = fill === "all" || fill === "longest";
|
|
2306
|
+
const longest = Math.max(...lines.map((l) => Math.max(l.notesEnd, ...l.texts.map((t) => t.textCursor))));
|
|
2307
|
+
const fillTarget = fill === "none" || maxWidthPx === Infinity && !fillsLast ? null : fill === "longest" && maxWidthPx - longest > staffFillSnapPx ? longest : maxWidthPx;
|
|
2748
2308
|
lines.forEach((l, li) => {
|
|
2749
|
-
|
|
2750
|
-
|
|
2751
|
-
|
|
2309
|
+
const last = li === lines.length - 1;
|
|
2310
|
+
l.staffEnd = l.notesEnd;
|
|
2311
|
+
if (last && (!fillsLast || l.notesParts.length === 0)) return;
|
|
2312
|
+
if (fillTarget === null) {
|
|
2313
|
+
l.breakMarkerLeftPx = padding;
|
|
2752
2314
|
return;
|
|
2753
2315
|
}
|
|
2316
|
+
const staffEdge = fillTarget + padding;
|
|
2754
2317
|
let lineEnd = measure(l.clefPrefix + l.notesParts.join(""), notesFont);
|
|
2755
|
-
while (lineEnd + dashNotesWidth <=
|
|
2318
|
+
while (last ? lineEnd + dashNotesWidth <= staffEdge : lineEnd + dashNotesWidth <= staffEdge - starWidth && lineEnd + dashNotesWidth < fillTarget) {
|
|
2756
2319
|
l.notesParts.push("-");
|
|
2757
2320
|
lineEnd += dashNotesWidth;
|
|
2758
2321
|
}
|
|
2759
|
-
|
|
2322
|
+
if (last) {
|
|
2323
|
+
const third = measure("¨", notesFont);
|
|
2324
|
+
while (lineEnd + third <= staffEdge) {
|
|
2325
|
+
l.notesParts.push("¨");
|
|
2326
|
+
lineEnd += third;
|
|
2327
|
+
}
|
|
2328
|
+
}
|
|
2329
|
+
l.staffEnd = lineEnd;
|
|
2330
|
+
if (!last) l.breakMarkerLeftPx = staffEdge - lineEnd - starWidth;
|
|
2760
2331
|
});
|
|
2761
2332
|
return {
|
|
2762
2333
|
result: {
|
|
@@ -2775,4 +2346,4 @@ async function layoutPass(source, options, breakBefore) {
|
|
|
2775
2346
|
};
|
|
2776
2347
|
}
|
|
2777
2348
|
//#endregion
|
|
2778
|
-
export {
|
|
2349
|
+
export { CLEF_GLYPHS as A, toConnectDown as B, scriptSyllables as C, resolveSpans as D, unescape as E, isModifierGlyph as F, variantOf as G, toSmallHead as H, modifyDown as I, 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, toSmallPoint as U, toLonga as V, toVirga 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, toCephalicus as z };
|