guidex 0.5.1 → 0.6.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +200 -0
- package/README.md +62 -16
- package/dist/lib/app/App.d.ts +1 -0
- package/dist/lib/app/AretinoControls.d.ts +35 -0
- package/dist/lib/app/CaretBand.d.ts +28 -0
- package/dist/lib/app/Developers.d.ts +13 -0
- package/dist/lib/app/EditorContext.d.ts +15 -0
- package/dist/lib/app/EditorTextarea.d.ts +18 -1
- package/dist/lib/app/EditorToolbar.d.ts +13 -0
- package/dist/lib/app/ExportSettings.d.ts +20 -1
- package/dist/lib/app/Footer.d.ts +1 -0
- package/dist/lib/app/GlyphChooser.d.ts +31 -0
- package/dist/lib/app/Inspector.d.ts +26 -0
- package/dist/lib/app/LiveScore.d.ts +31 -0
- package/dist/lib/app/Opening.d.ts +3 -0
- package/dist/lib/app/Reference.d.ts +28 -0
- package/dist/lib/app/RenderSettings.d.ts +2 -1
- package/dist/lib/app/ScriptViews.d.ts +8 -0
- package/dist/lib/app/SectionSize.d.ts +33 -0
- package/dist/lib/app/StaffSettings.d.ts +4 -0
- package/dist/lib/app/StaffedGlyph.d.ts +8 -0
- package/dist/lib/app/Tabs.d.ts +15 -0
- package/dist/lib/app/TakeItOut.d.ts +1 -0
- package/dist/lib/app/ThemeSwitch.d.ts +1 -0
- package/dist/lib/app/TopBar.d.ts +6 -0
- package/dist/lib/app/WidthControls.d.ts +21 -0
- package/dist/lib/app/aretinoCaret.d.ts +47 -0
- package/dist/lib/app/aretinoSource.d.ts +46 -0
- package/dist/lib/app/caretSync.d.ts +48 -0
- package/dist/lib/app/chantLibrary.d.ts +8 -4
- package/dist/lib/app/editorSnapshot.d.ts +11 -0
- package/dist/lib/app/editorState.d.ts +38 -5
- package/dist/lib/app/expandLink.d.ts +19 -0
- package/dist/lib/app/glyphData.d.ts +37 -0
- package/dist/lib/app/inCode.d.ts +4 -0
- package/dist/lib/app/insertAtCaret.d.ts +12 -0
- package/dist/lib/app/learn/Learn.d.ts +1 -0
- package/dist/lib/app/learn/LessonCard.d.ts +12 -0
- package/dist/lib/app/learn/lessons.d.ts +64 -0
- package/dist/lib/app/learn/narrowing.d.ts +24 -0
- package/dist/lib/app/learn/progress.d.ts +14 -0
- package/dist/lib/app/pageEvents.d.ts +14 -0
- package/dist/lib/app/previewHighlight.d.ts +44 -0
- package/dist/lib/app/samples.d.ts +9 -1
- package/dist/lib/app/scriptParts.d.ts +31 -0
- package/dist/lib/app/shareLink.d.ts +12 -2
- package/dist/lib/app/showcases/Showcases.d.ts +17 -0
- package/dist/lib/app/wordCopy.d.ts +4 -0
- package/dist/lib/aretino/fromAretino.d.ts +99 -0
- package/dist/lib/aretino/index.d.ts +38 -0
- package/dist/lib/aretino/index.js +1870 -0
- package/dist/lib/aretino/toAretino.d.ts +27 -0
- package/dist/lib/chantLayout-BRQ6FYND.js +1345 -0
- package/dist/lib/chantLayout.d.ts +101 -6
- package/dist/lib/chantMarkup-DX4K86Rv.js +993 -0
- package/dist/lib/drawnStaff.d.ts +39 -5
- package/dist/lib/editorStorage.d.ts +48 -6
- package/dist/lib/embed/core.d.ts +15 -1
- package/dist/lib/embed/index.js +1 -1
- package/dist/lib/embed/manual.d.ts +5 -4
- package/dist/lib/embed/manual.js +6 -4
- package/dist/lib/fonts/guidohu-staffless.woff2 +0 -0
- package/dist/lib/fonts/guidohu.ttf +0 -0
- package/dist/lib/glyphFamilies.d.ts +5 -1
- package/dist/lib/guidoRender.d.ts +30 -1
- package/dist/lib/guidoScriptTransforms.d.ts +2 -2
- package/dist/lib/guidoStyles-CvnACCsk.js +1466 -0
- package/dist/lib/guidoStyles.d.ts +23 -2
- package/dist/lib/react/GuidexScore.d.ts +10 -2
- package/dist/lib/react/index.js +14 -4
- package/dist/lib/{scoreImage-DUxsPIm0.js → scoreImage-CKsQxHg5.js} +282 -1026
- package/dist/lib/theme.d.ts +13 -0
- package/dist/lib/word/index.d.ts +1 -1
- package/dist/lib/word/index.js +1 -1
- package/dist/lib/{wordChecker-DmjGFR7e.js → wordChecker-BN_9dKAx.js} +133 -20
- package/dist/lib/wordChecker.d.ts +18 -1
- package/dist/lib/wordExport.d.ts +51 -3
- package/dist-embed/demo.html +2 -2
- package/dist-embed/fonts/guidohu-staffless.woff2 +0 -0
- package/dist-embed/fonts/guidohu.ttf +0 -0
- package/dist-embed/guidex-embed.min.js +1 -1
- package/package.json +10 -1
- package/dist/lib/app/ControlsRow.d.ts +0 -1
- package/dist/lib/app/SettingsMenu.d.ts +0 -1
- package/dist/lib/app/SettingsPanel.d.ts +0 -1
- package/dist/lib/chantLayout-DGWAhg8Q.js +0 -2617
|
@@ -0,0 +1,1870 @@
|
|
|
1
|
+
import { A as splitNoteMarkers, B as modifyUp, G as toSmallHead, H as toCephalicus, K as toSmallPoint, M as STAFF, N as baseOf, R as modifyDown, U as toConnectDown, W as toLonga, _ as GuidoScript, q as toVirga, z as modifyNatural } from "../guidoStyles-CvnACCsk.js";
|
|
2
|
+
import { C as stanzaCount, E as withStanza, T as stanzaWarnings, _ as parseStanza, c as KEY_SIGNATURES, m as STANZA_PLACEHOLDER, p as transposeScript, r as chantTextStyle } from "../chantMarkup-DX4K86Rv.js";
|
|
3
|
+
import { parseAretino } from "@aretino-chant/core";
|
|
4
|
+
//#region src/aretino/fromAretino.ts
|
|
5
|
+
/**
|
|
6
|
+
* Aretino source → GuidoScript (issue #79). Every point where the two
|
|
7
|
+
* notations differ is a decision (`Decisions`); `DEFAULTS` holds the decided
|
|
8
|
+
* choices, and `aretinoToGuido` (./index.ts) always uses them. The prototype
|
|
9
|
+
* pages (prototypes/aretino) pass other candidates to compare them.
|
|
10
|
+
*
|
|
11
|
+
* Pure: no DOM. Uses Aretino's own parser unmodified.
|
|
12
|
+
*/
|
|
13
|
+
/** Every decision with its default: the decided choice (`DECIDED` in
|
|
14
|
+
* catalog.ts), else the prototype's recommendation. */
|
|
15
|
+
var DEFAULTS = {
|
|
16
|
+
indent: "drop",
|
|
17
|
+
transpose: "regions",
|
|
18
|
+
verse: "drop",
|
|
19
|
+
range: "error",
|
|
20
|
+
virga: "auto",
|
|
21
|
+
upLeap: "third",
|
|
22
|
+
tenor: "longa",
|
|
23
|
+
glyphless: "warn",
|
|
24
|
+
episema: "glyph",
|
|
25
|
+
moraOnLine: "above",
|
|
26
|
+
moraInNeume: "cut",
|
|
27
|
+
plica: "append",
|
|
28
|
+
connectors: "on",
|
|
29
|
+
connectBeforePlica: "connect",
|
|
30
|
+
connectFromPlica: "skip",
|
|
31
|
+
connectFromSmall: "connect",
|
|
32
|
+
longLeap: "chain4",
|
|
33
|
+
cut: "¨",
|
|
34
|
+
multiCut: "dash",
|
|
35
|
+
noteParens: "brackets",
|
|
36
|
+
neumeLabel: "drop",
|
|
37
|
+
bar_comma: ":",
|
|
38
|
+
bar_comma2: ":",
|
|
39
|
+
bar_semi: ";",
|
|
40
|
+
bar_pipe: ",",
|
|
41
|
+
bar_empty: "",
|
|
42
|
+
bar_dots: "",
|
|
43
|
+
bar_double: ",,",
|
|
44
|
+
bar_triple: ".",
|
|
45
|
+
bar_repStart: "",
|
|
46
|
+
bar_repEnd: "",
|
|
47
|
+
bar_repBoth: "",
|
|
48
|
+
breath: "above",
|
|
49
|
+
plicaBar: "above",
|
|
50
|
+
barLabel: "own",
|
|
51
|
+
badClef: "error",
|
|
52
|
+
keyMeaning: "positions",
|
|
53
|
+
accScope: "once",
|
|
54
|
+
breaks: "keep",
|
|
55
|
+
extenderTail: "keep",
|
|
56
|
+
tildes2: "before",
|
|
57
|
+
star: "escaped",
|
|
58
|
+
dagger: "escaped",
|
|
59
|
+
dagger2: "‡",
|
|
60
|
+
rv: "liturgy",
|
|
61
|
+
red: "span",
|
|
62
|
+
color: "span",
|
|
63
|
+
underline: "u",
|
|
64
|
+
textAccidentals: "unicode",
|
|
65
|
+
missingStanza: "empty",
|
|
66
|
+
extraSyllables: "truncate",
|
|
67
|
+
tenorText: "split",
|
|
68
|
+
sectionBreak: "break"
|
|
69
|
+
};
|
|
70
|
+
var LETTERS$1 = "ABcdefgabCDEFG";
|
|
71
|
+
var QUILISMAS$1 = "àâãäåæçèêëìîï";
|
|
72
|
+
var CLEFS = {
|
|
73
|
+
c1: ">",
|
|
74
|
+
c2: "#",
|
|
75
|
+
c3: "&",
|
|
76
|
+
c4: "@",
|
|
77
|
+
f3: "{",
|
|
78
|
+
f4: "}",
|
|
79
|
+
g2: "M"
|
|
80
|
+
};
|
|
81
|
+
var BAR_DECISION = {
|
|
82
|
+
",": "bar_comma",
|
|
83
|
+
",2": "bar_comma2",
|
|
84
|
+
";": "bar_semi",
|
|
85
|
+
"|": "bar_pipe",
|
|
86
|
+
"|0": "bar_empty",
|
|
87
|
+
"|?": "bar_dots",
|
|
88
|
+
"||": "bar_double",
|
|
89
|
+
"|||": "bar_triple",
|
|
90
|
+
"|:": "bar_repStart",
|
|
91
|
+
":|": "bar_repEnd",
|
|
92
|
+
":|:": "bar_repBoth"
|
|
93
|
+
};
|
|
94
|
+
var FLAT_ORDER = "beadgcf";
|
|
95
|
+
var SHARP_ORDER = "fcgdaeb";
|
|
96
|
+
var TranslationError = class extends Error {};
|
|
97
|
+
var position = (pitch, shift = 0) => LETTERS$1.indexOf(pitch) + 7 * shift;
|
|
98
|
+
var escapeNotes = (notes) => notes.replace(/[()\\]/g, "\\$&");
|
|
99
|
+
function translate(rawSource, d = DEFAULTS) {
|
|
100
|
+
const source = rawSource.replace(/\r\n/g, "\n");
|
|
101
|
+
const warnings = [];
|
|
102
|
+
const lost = {};
|
|
103
|
+
const loss = (what) => {
|
|
104
|
+
lost[what] = (lost[what] ?? 0) + 1;
|
|
105
|
+
};
|
|
106
|
+
const warn = (msg) => {
|
|
107
|
+
if (!warnings.includes(msg)) warnings.push(msg);
|
|
108
|
+
};
|
|
109
|
+
const fail = (msg) => {
|
|
110
|
+
throw new TranslationError(msg);
|
|
111
|
+
};
|
|
112
|
+
const metadata = {};
|
|
113
|
+
const verse = [];
|
|
114
|
+
try {
|
|
115
|
+
const ast = parseAretino(source);
|
|
116
|
+
let indentLabel = "";
|
|
117
|
+
for (const [key, value] of Object.entries(ast.header)) {
|
|
118
|
+
const k = key.toLowerCase();
|
|
119
|
+
if (k === "indent" && d.indent === "label") indentLabel = value;
|
|
120
|
+
else if (k === "indent" && d.indent === "drop") loss("%indent");
|
|
121
|
+
else if (k === "transpose") {
|
|
122
|
+
if (d.transpose === "error") fail(`%transpose: ${value} is not supported`);
|
|
123
|
+
if (d.transpose === "warn") warn(`%transpose: ${value} was not applied`);
|
|
124
|
+
metadata[key] = value;
|
|
125
|
+
} else if (k === "option" || k === "tempo" || k === "language") loss(`%${k} (Aretino renderer or playback only)`);
|
|
126
|
+
else metadata[key] = value;
|
|
127
|
+
}
|
|
128
|
+
let semitones = 0;
|
|
129
|
+
if ((d.transpose === "exact" || d.transpose === "regions") && metadata.transpose) {
|
|
130
|
+
const n = Number.parseInt(metadata.transpose, 10);
|
|
131
|
+
if (Number.isFinite(n)) semitones = n;
|
|
132
|
+
else warn(`%transpose: ${metadata.transpose} is not a number, not applied`);
|
|
133
|
+
}
|
|
134
|
+
const keyChanges = [];
|
|
135
|
+
const items = [];
|
|
136
|
+
const sections = [];
|
|
137
|
+
let section = null;
|
|
138
|
+
let openingClef = null;
|
|
139
|
+
let clef = "M";
|
|
140
|
+
let keyAccs = [];
|
|
141
|
+
let openingKey = null;
|
|
142
|
+
let pendingAccidental;
|
|
143
|
+
let lastClef = null;
|
|
144
|
+
let lastWasLyrics = true;
|
|
145
|
+
/** An open [ … ]: the first group inside it and every note position, for the bracket height */
|
|
146
|
+
let paren = null;
|
|
147
|
+
/** Inline accidentals in force until the next bar: position → glyph */
|
|
148
|
+
let active = /* @__PURE__ */ new Map();
|
|
149
|
+
const accidentalGlyph = (acc, at = position(acc.pitch)) => {
|
|
150
|
+
const base = STAFF[at];
|
|
151
|
+
const glyph = base && (acc.symbol === "x" ? modifyDown(base) : acc.symbol === "#" ? modifyUp(base) : modifyNatural(base));
|
|
152
|
+
if (!glyph) warn(`No ${acc.symbol === "x" ? "flat" : acc.symbol === "#" ? "sharp" : "natural"} glyph at "${acc.pitch}": left out`);
|
|
153
|
+
return glyph ?? "";
|
|
154
|
+
};
|
|
155
|
+
const keyGlyphs = (accs, inClef) => accs.map((acc) => {
|
|
156
|
+
if (d.keyMeaning === "letters" && acc.symbol !== "y") {
|
|
157
|
+
const row = KEY_SIGNATURES[inClef];
|
|
158
|
+
const letter = acc.pitch.toLowerCase();
|
|
159
|
+
const glyph = acc.symbol === "x" ? row.flats[FLAT_ORDER.indexOf(letter)] : row.sharps[SHARP_ORDER.indexOf(letter)];
|
|
160
|
+
if (glyph) return glyph;
|
|
161
|
+
}
|
|
162
|
+
return accidentalGlyph(acc);
|
|
163
|
+
}).join("");
|
|
164
|
+
const autoVirga = (notes) => {
|
|
165
|
+
const pos = notes.map((n) => position(n.pitch, n.octaveShift));
|
|
166
|
+
if (notes.length < 2 || Math.max(...pos) <= Math.min(...pos)) return notes.map(() => false);
|
|
167
|
+
return notes.map((n, i) => (i === 0 || pos[i] >= pos[i - 1]) && (i === notes.length - 1 || pos[i] > pos[i + 1]) && !n.noVirga);
|
|
168
|
+
};
|
|
169
|
+
/** Auto-virga as Aretino draws it: an inner mora ends a part of the
|
|
170
|
+
* neume (splitGroupsAtInternalMora: not between the last two notes
|
|
171
|
+
* when both carry one), and every part has its own peaks */
|
|
172
|
+
const autoVirgaSplit = (notes) => {
|
|
173
|
+
const out = [];
|
|
174
|
+
let start = 0;
|
|
175
|
+
for (let i = 0; i < notes.length; i++) {
|
|
176
|
+
const mora = notes[i].modifiers.includes("mora");
|
|
177
|
+
const lastPairBothMora = i === notes.length - 2 && notes[i + 1].modifiers.includes("mora");
|
|
178
|
+
if (i === notes.length - 1 || mora && !lastPairBothMora) {
|
|
179
|
+
out.push(...autoVirga(notes.slice(start, i + 1)));
|
|
180
|
+
start = i + 1;
|
|
181
|
+
}
|
|
182
|
+
}
|
|
183
|
+
return out;
|
|
184
|
+
};
|
|
185
|
+
/** Guido HU has no connector up: a note a leap up from the note before it in its
|
|
186
|
+
* part is joined by its virga stem (upLeap). Parts as autoVirgaSplit cuts them. */
|
|
187
|
+
const leapVirga = (notes) => {
|
|
188
|
+
const min = d.upLeap === "third" ? 2 : d.upLeap === "fourth" ? 3 : Infinity;
|
|
189
|
+
return notes.map((n, i) => {
|
|
190
|
+
const prev = notes[i - 1];
|
|
191
|
+
if (!prev || n.noVirga) return false;
|
|
192
|
+
return !(prev.modifiers.includes("mora") && !(i === notes.length - 1 && n.modifiers.includes("mora"))) && position(n.pitch, n.octaveShift) - position(prev.pitch, prev.octaveShift) >= min;
|
|
193
|
+
});
|
|
194
|
+
};
|
|
195
|
+
const clampedPos = (note) => {
|
|
196
|
+
const pos = position(note.pitch, note.octaveShift);
|
|
197
|
+
if (STAFF[pos]) return pos;
|
|
198
|
+
const name = `${note.octaveShift && note.octaveShift > 0 ? "^".repeat(note.octaveShift) : "v".repeat(-(note.octaveShift ?? 0))}${note.pitch}`;
|
|
199
|
+
if (d.range === "error") fail(`Note "${name}" is outside the Guido range (A … ^a)`);
|
|
200
|
+
if (d.range === "drop") {
|
|
201
|
+
warn(`Note "${name}" is outside the Guido range: left out`);
|
|
202
|
+
return null;
|
|
203
|
+
}
|
|
204
|
+
warn(`Note "${name}" is outside the Guido range: moved to the edge`);
|
|
205
|
+
return Math.max(0, Math.min(STAFF.length - 1, pos));
|
|
206
|
+
};
|
|
207
|
+
const signed = /* @__PURE__ */ new Set();
|
|
208
|
+
const noteGlyphs = (note, virga, next) => {
|
|
209
|
+
const pos = clampedPos(note);
|
|
210
|
+
if (pos === null) return "";
|
|
211
|
+
const base = STAFF[pos];
|
|
212
|
+
const mods = new Set(note.modifiers);
|
|
213
|
+
let head;
|
|
214
|
+
let headKind = "plain";
|
|
215
|
+
if (note.shape === "tenor") {
|
|
216
|
+
if (d.tenor === "longa") {
|
|
217
|
+
head = toLonga(base);
|
|
218
|
+
headKind = "tenor";
|
|
219
|
+
if (!head) warn(`No reciting (tenor) glyph at "${note.pitch}": drawn as a plain note`);
|
|
220
|
+
} else warn("Tenor notes drawn as plain notes");
|
|
221
|
+
} else if (note.shape === "quilisma") {
|
|
222
|
+
head = QUILISMAS$1[pos - 1];
|
|
223
|
+
headKind = "quilisma";
|
|
224
|
+
if (!head) warn(`No quilisma glyph at "${note.pitch}": drawn as a plain note`);
|
|
225
|
+
} else if (mods.has("small")) {
|
|
226
|
+
head = toSmallHead(base);
|
|
227
|
+
headKind = "small";
|
|
228
|
+
if (!head) warn(`No small head at "${note.pitch}": drawn full size`);
|
|
229
|
+
} else if (virga) {
|
|
230
|
+
head = toVirga(base);
|
|
231
|
+
if (!head) warn(`No virga glyph at "${note.pitch}": drawn as a plain note`);
|
|
232
|
+
}
|
|
233
|
+
if (mods.has("plica") && d.plica === "replace") {
|
|
234
|
+
const ceph = cephalicusOf(base);
|
|
235
|
+
if (ceph) {
|
|
236
|
+
head = ceph;
|
|
237
|
+
headKind = "plica";
|
|
238
|
+
} else warn(`No cephalicus at "${note.pitch}"`);
|
|
239
|
+
}
|
|
240
|
+
head ??= base;
|
|
241
|
+
if (mods.has("small") && note.shape !== "punctum") warn("A small tenor or quilisma has no glyph: drawn full size");
|
|
242
|
+
const episema = mods.has("episema") && d.episema === "glyph" ? "\\" : "";
|
|
243
|
+
for (const m of [
|
|
244
|
+
"episema",
|
|
245
|
+
"ictus",
|
|
246
|
+
"open"
|
|
247
|
+
]) {
|
|
248
|
+
if (!mods.has(m) || m === "episema" && episema) continue;
|
|
249
|
+
if (d.glyphless === "error") fail(`The ${m} has no Guido glyph`);
|
|
250
|
+
if (d.glyphless === "warn") loss(`${m} (no glyph)`);
|
|
251
|
+
}
|
|
252
|
+
if (note.label) {
|
|
253
|
+
if (d.neumeLabel === "drop") loss("label above a note");
|
|
254
|
+
else warn(`A label above a single note ("${note.label}") can only go before the whole neume`);
|
|
255
|
+
}
|
|
256
|
+
let sign = "";
|
|
257
|
+
if (note.accidental) {
|
|
258
|
+
sign = accidentalGlyph(note.accidental);
|
|
259
|
+
active.set(position(note.accidental.pitch), sign);
|
|
260
|
+
if (note.accidental.brackets) sign = note.accidental.brackets[0] + sign + note.accidental.brackets[1];
|
|
261
|
+
signed.add(position(note.accidental.pitch));
|
|
262
|
+
} else if (d.accScope === "repeat" && active.has(pos) && !signed.has(pos)) {
|
|
263
|
+
sign = active.get(pos);
|
|
264
|
+
signed.add(pos);
|
|
265
|
+
}
|
|
266
|
+
let out = sign + head + episema;
|
|
267
|
+
const nextPos = next ? position(next.pitch, next.octaveShift) : pos;
|
|
268
|
+
const moraHere = mods.has("mora");
|
|
269
|
+
const skipForShape = d.connectFromSmall === "skip" && headKind !== "plain";
|
|
270
|
+
const skipForPlica = d.connectBeforePlica === "skip" && next?.modifiers.includes("plica");
|
|
271
|
+
const skipFromPlica = d.connectFromPlica === "skip" && mods.has("plica");
|
|
272
|
+
if (d.connectors === "on" && next && nextPos < pos && !moraHere && !next.accidental && !skipForShape && !skipForPlica && !skipFromPlica) {
|
|
273
|
+
let from = pos;
|
|
274
|
+
let left = pos - nextPos;
|
|
275
|
+
if (left > 4 && d.longLeap === "none") {
|
|
276
|
+
warn(`A step down of ${left} has no single connector glyph: left unconnected`);
|
|
277
|
+
left = 0;
|
|
278
|
+
}
|
|
279
|
+
const maxStep = d.longLeap === "chain3" ? 3 : 4;
|
|
280
|
+
while (left > 0) {
|
|
281
|
+
const step = Math.min(maxStep, left);
|
|
282
|
+
const glyph = toConnectDown(STAFF[from], step);
|
|
283
|
+
if (!glyph) {
|
|
284
|
+
warn(`No connector glyph ${step} down from "${LETTERS$1[from] ?? from}"`);
|
|
285
|
+
break;
|
|
286
|
+
}
|
|
287
|
+
out += glyph === "μ" ? "µ" : glyph;
|
|
288
|
+
from -= step;
|
|
289
|
+
left -= step;
|
|
290
|
+
}
|
|
291
|
+
}
|
|
292
|
+
if (mods.has("plica") && d.plica === "append") {
|
|
293
|
+
const ceph = cephalicusOf(base);
|
|
294
|
+
if (ceph) out += ceph;
|
|
295
|
+
else warn(`No cephalicus at "${note.pitch}"`);
|
|
296
|
+
}
|
|
297
|
+
if (moraHere) {
|
|
298
|
+
const dot = toSmallPoint(base) ?? (d.moraOnLine === "above" ? toSmallPoint(STAFF[pos + 1] ?? "") : void 0) ?? (d.moraOnLine === "below" ? toSmallPoint(STAFF[pos - 1] ?? "") : void 0);
|
|
299
|
+
if (dot) out += dot;
|
|
300
|
+
else loss("mora on a line note (no dot glyph)");
|
|
301
|
+
if (next && d.moraInNeume === "cut") out += d.cut === "none" ? "" : d.cut;
|
|
302
|
+
}
|
|
303
|
+
return out;
|
|
304
|
+
};
|
|
305
|
+
const lastGroup = () => items.findLast((it) => it.kind === "group");
|
|
306
|
+
const directive = (value) => {
|
|
307
|
+
const v = value.trim();
|
|
308
|
+
const clefGlyph = CLEFS[v.toLowerCase()];
|
|
309
|
+
if (clefGlyph) {
|
|
310
|
+
const changed = clefGlyph !== clef;
|
|
311
|
+
clef = clefGlyph;
|
|
312
|
+
if (openingClef === null && items.length === 0) openingClef = clefGlyph;
|
|
313
|
+
else if (changed) {
|
|
314
|
+
const stmt = {
|
|
315
|
+
kind: "stmt",
|
|
316
|
+
text: `(@clef ${clefGlyph}${keyGlyphs(keyAccs, clefGlyph)})`
|
|
317
|
+
};
|
|
318
|
+
items.push(stmt);
|
|
319
|
+
lastClef = stmt;
|
|
320
|
+
}
|
|
321
|
+
return;
|
|
322
|
+
}
|
|
323
|
+
if (/^[cfg]\d$/i.test(v)) {
|
|
324
|
+
if (d.badClef === "error") fail(`Clef (${v}) has no Guido glyph`);
|
|
325
|
+
warn(`Clef (${v}) has no Guido glyph: ignored, the notes keep their staff positions`);
|
|
326
|
+
return;
|
|
327
|
+
}
|
|
328
|
+
const short = v.match(/^K(b+|#+)?$/);
|
|
329
|
+
const long = v.match(/^K:\s*(.*)$/);
|
|
330
|
+
if (short || long) {
|
|
331
|
+
let accs = [];
|
|
332
|
+
if (short) {
|
|
333
|
+
const chars = short[1] ?? "";
|
|
334
|
+
const order = chars[0] === "b" ? [
|
|
335
|
+
"b",
|
|
336
|
+
"E",
|
|
337
|
+
"a",
|
|
338
|
+
"D",
|
|
339
|
+
"g",
|
|
340
|
+
"C",
|
|
341
|
+
"F"
|
|
342
|
+
] : [
|
|
343
|
+
"F",
|
|
344
|
+
"C",
|
|
345
|
+
"G",
|
|
346
|
+
"D",
|
|
347
|
+
"a",
|
|
348
|
+
"E",
|
|
349
|
+
"b"
|
|
350
|
+
];
|
|
351
|
+
accs = [...chars].map((c, i) => ({
|
|
352
|
+
pitch: order[i],
|
|
353
|
+
symbol: c === "b" ? "x" : "#"
|
|
354
|
+
}));
|
|
355
|
+
} else for (const part of long[1].trim().split(/\s+/).filter(Boolean)) {
|
|
356
|
+
const m = part.match(/^([a-gA-G]?)([bn#])$/);
|
|
357
|
+
if (m) accs.push({
|
|
358
|
+
pitch: m[1] || "b",
|
|
359
|
+
symbol: m[2] === "b" ? "x" : m[2] === "n" ? "y" : "#"
|
|
360
|
+
});
|
|
361
|
+
}
|
|
362
|
+
keyAccs = accs;
|
|
363
|
+
const glyphs = keyGlyphs(accs, clef);
|
|
364
|
+
const change = (withClef) => {
|
|
365
|
+
const kc = {
|
|
366
|
+
label: `(${v})`,
|
|
367
|
+
clef,
|
|
368
|
+
glyphs,
|
|
369
|
+
fifths: fifthsOf(accs),
|
|
370
|
+
withClef
|
|
371
|
+
};
|
|
372
|
+
keyChanges.push(kc);
|
|
373
|
+
return kc;
|
|
374
|
+
};
|
|
375
|
+
if (items.length === 0) openingKey = accs;
|
|
376
|
+
else if (lastClef && items.at(-1) === lastClef) {
|
|
377
|
+
lastClef.text = `(@clef ${clef}${glyphs})`;
|
|
378
|
+
lastClef.keyChange = change(true);
|
|
379
|
+
} else items.push({
|
|
380
|
+
kind: "stmt",
|
|
381
|
+
text: `(@key${glyphs ? " " + glyphs : ""})`,
|
|
382
|
+
keyChange: change(false)
|
|
383
|
+
});
|
|
384
|
+
return;
|
|
385
|
+
}
|
|
386
|
+
const acc = v.match(/^([a-gA-G]?)([bn#])$/);
|
|
387
|
+
if (acc) {
|
|
388
|
+
if (pendingAccidental) loss("an accidental standing alone");
|
|
389
|
+
pendingAccidental = {
|
|
390
|
+
pitch: acc[1] || "b",
|
|
391
|
+
symbol: acc[2] === "b" ? "x" : acc[2] === "n" ? "y" : "#"
|
|
392
|
+
};
|
|
393
|
+
if (paren && !paren.first) paren.accs.push(position(pendingAccidental.pitch));
|
|
394
|
+
return;
|
|
395
|
+
}
|
|
396
|
+
if (v === "z" || v === "Z") {
|
|
397
|
+
if (d.breaks === "keep") items.push({
|
|
398
|
+
kind: "stmt",
|
|
399
|
+
text: "(@break)"
|
|
400
|
+
});
|
|
401
|
+
else loss(`(${v}) line break`);
|
|
402
|
+
return;
|
|
403
|
+
}
|
|
404
|
+
loss(`directive (${v})`);
|
|
405
|
+
};
|
|
406
|
+
const barline = (kind) => {
|
|
407
|
+
active = /* @__PURE__ */ new Map();
|
|
408
|
+
if (pendingAccidental) {
|
|
409
|
+
loss("an accidental standing alone");
|
|
410
|
+
pendingAccidental = void 0;
|
|
411
|
+
}
|
|
412
|
+
const prev = lastGroup();
|
|
413
|
+
let glyph;
|
|
414
|
+
if (kind === "'" || kind === "~") {
|
|
415
|
+
const mode = kind === "'" ? d.breath : d.plicaBar;
|
|
416
|
+
if (mode === "drop") return loss(kind === "'" ? "breath mark" : "plica divider ~");
|
|
417
|
+
const at = !prev ? -1 : mode === "above" ? prev.lastPos + 1 : prev.lastPos;
|
|
418
|
+
glyph = mode === "fixed" ? "/" : cephalicusOf(STAFF[at] ?? "");
|
|
419
|
+
if (!glyph) return warn("Breath mark: no cephalicus glyph at that height");
|
|
420
|
+
} else {
|
|
421
|
+
const id = BAR_DECISION[kind];
|
|
422
|
+
glyph = id ? d[id] : void 0;
|
|
423
|
+
if (!glyph) return loss(`bar "${kind}"`);
|
|
424
|
+
}
|
|
425
|
+
const bar = {
|
|
426
|
+
kind: "bar",
|
|
427
|
+
glyph,
|
|
428
|
+
...section ? { groupsBefore: section.groups.length } : {}
|
|
429
|
+
};
|
|
430
|
+
items.push(bar);
|
|
431
|
+
section?.bars.push(bar);
|
|
432
|
+
};
|
|
433
|
+
const isMusicContinuation = (tokens) => {
|
|
434
|
+
const at = tokens[0]?.srcStart;
|
|
435
|
+
if (at === void 0) return false;
|
|
436
|
+
const lineStart = source.lastIndexOf("\n", at - 1) + 1;
|
|
437
|
+
return /^\s*n:/.test(source.slice(lineStart, at));
|
|
438
|
+
};
|
|
439
|
+
for (const line of ast.lines) {
|
|
440
|
+
if (line.type === "lyrics") {
|
|
441
|
+
section?.lyrics.push(line.text);
|
|
442
|
+
lastWasLyrics = true;
|
|
443
|
+
continue;
|
|
444
|
+
}
|
|
445
|
+
if (line.type === "verse") {
|
|
446
|
+
const text = (line.lines ?? []).join("\n");
|
|
447
|
+
if (d.verse === "meta") verse.push(text);
|
|
448
|
+
else loss("W: text block");
|
|
449
|
+
continue;
|
|
450
|
+
}
|
|
451
|
+
if (line.type === "preprocessor") {
|
|
452
|
+
loss("%[ … %] directive");
|
|
453
|
+
continue;
|
|
454
|
+
}
|
|
455
|
+
if (line.type !== "music") continue;
|
|
456
|
+
const tokens = line.tokens ?? [];
|
|
457
|
+
if (!section || lastWasLyrics && !isMusicContinuation(tokens)) {
|
|
458
|
+
if (section && d.sectionBreak === "break" && items.length) items.push({
|
|
459
|
+
kind: "stmt",
|
|
460
|
+
text: "(@break)"
|
|
461
|
+
});
|
|
462
|
+
section = {
|
|
463
|
+
groups: [],
|
|
464
|
+
bars: [],
|
|
465
|
+
lyrics: []
|
|
466
|
+
};
|
|
467
|
+
sections.push(section);
|
|
468
|
+
}
|
|
469
|
+
lastWasLyrics = false;
|
|
470
|
+
for (const tok of tokens) if (tok.type === "directive") directive(tok.value);
|
|
471
|
+
else if (tok.type === "barline") barline(tok.kind);
|
|
472
|
+
else if (tok.type === "ligature") {
|
|
473
|
+
const groups = tok.groups;
|
|
474
|
+
const gaps = tok.gaps ?? [];
|
|
475
|
+
if (tok.label) {
|
|
476
|
+
if (d.neumeLabel === "label") items.push({
|
|
477
|
+
kind: "stmt",
|
|
478
|
+
text: `_${escapeNotes(tok.label)}_()`
|
|
479
|
+
});
|
|
480
|
+
else loss("label above a neume");
|
|
481
|
+
}
|
|
482
|
+
if (pendingAccidental && groups[0]?.[0] && !groups[0][0].accidental) groups[0][0] = {
|
|
483
|
+
...groups[0][0],
|
|
484
|
+
accidental: pendingAccidental
|
|
485
|
+
};
|
|
486
|
+
pendingAccidental = void 0;
|
|
487
|
+
signed.clear();
|
|
488
|
+
const parts = groups.map((notes) => {
|
|
489
|
+
const peaks = d.virga === "auto" ? autoVirgaSplit(notes) : notes.map(() => false);
|
|
490
|
+
const leaps = leapVirga(notes);
|
|
491
|
+
const auto = peaks.map((p, i) => p || leaps[i]);
|
|
492
|
+
return notes.map((n, i) => noteGlyphs(n, n.virga || n.shape === "virga" || auto[i], notes[i + 1])).join("");
|
|
493
|
+
});
|
|
494
|
+
let notes = parts[0] ?? "";
|
|
495
|
+
for (let g = 1; g < parts.length; g++) {
|
|
496
|
+
const cut = d.cut === "none" ? "" : d.cut;
|
|
497
|
+
const n = gaps[g - 1] ?? 1;
|
|
498
|
+
const wide = d.multiCut === "dash" && cut === "¨" ? "-".repeat(Math.floor(n / 3)) + cut.repeat(n % 3) : cut.repeat(n);
|
|
499
|
+
notes += (d.multiCut === "single" ? cut : wide) + parts[g];
|
|
500
|
+
}
|
|
501
|
+
if (!notes) {
|
|
502
|
+
loss("neume with no notes left");
|
|
503
|
+
continue;
|
|
504
|
+
}
|
|
505
|
+
const last = groups.at(-1).at(-1);
|
|
506
|
+
const all = groups.flat();
|
|
507
|
+
const group = {
|
|
508
|
+
kind: "group",
|
|
509
|
+
notes,
|
|
510
|
+
lastPos: position(last.pitch, last.octaveShift),
|
|
511
|
+
tenorOnly: all.length === 1 && all[0].shape === "tenor"
|
|
512
|
+
};
|
|
513
|
+
items.push(group);
|
|
514
|
+
section.groups.push(group);
|
|
515
|
+
if (paren) {
|
|
516
|
+
paren.first ??= group;
|
|
517
|
+
paren.positions.push(...all.map((n) => position(n.pitch, n.octaveShift)));
|
|
518
|
+
}
|
|
519
|
+
} else if (tok.type === "paren-open") {
|
|
520
|
+
if (d.noteParens === "drop") loss("[ ] note parentheses");
|
|
521
|
+
else paren = {
|
|
522
|
+
first: null,
|
|
523
|
+
positions: [],
|
|
524
|
+
accs: []
|
|
525
|
+
};
|
|
526
|
+
} else if (tok.type === "paren-close") {
|
|
527
|
+
const prev = lastGroup();
|
|
528
|
+
if (paren && !paren.first && pendingAccidental && paren.accs.length) pendingAccidental = {
|
|
529
|
+
...pendingAccidental,
|
|
530
|
+
brackets: roundBrackets(paren.accs)
|
|
531
|
+
};
|
|
532
|
+
else if (paren?.first && prev) {
|
|
533
|
+
const [open, close] = d.noteParens === "staff" ? ["¿-", "-À"] : roundBrackets(paren.positions);
|
|
534
|
+
paren.first.notes = open + paren.first.notes;
|
|
535
|
+
prev.notes += close;
|
|
536
|
+
}
|
|
537
|
+
paren = null;
|
|
538
|
+
} else if (tok.type === "spacer") loss("spacer = / (sp)");
|
|
539
|
+
else if (tok.type === "expander") loss("expander *");
|
|
540
|
+
else if (tok.type === "brace-open") loss(`${tok.kind} span`);
|
|
541
|
+
else if (tok.type === "brace-close" && tok.label) loss("span label");
|
|
542
|
+
else if (tok.type === "inline-directive") loss("%[ … %] directive");
|
|
543
|
+
}
|
|
544
|
+
if (openingClef === null) {
|
|
545
|
+
openingClef = "M";
|
|
546
|
+
if (sections.length) warn("No opening clef: (g2) assumed");
|
|
547
|
+
}
|
|
548
|
+
const opening = `(${openingClef}${keyGlyphs(openingKey ?? [], openingClef)})`;
|
|
549
|
+
const stanzaCount = Math.max(0, ...sections.map((s) => s.lyrics.length));
|
|
550
|
+
const perSection = sections.map((sec, s) => {
|
|
551
|
+
const stanzas = [];
|
|
552
|
+
/** Display parts (1.~~Ky): stanza → slot → text in script notation */
|
|
553
|
+
const prefixes = /* @__PURE__ */ new Map();
|
|
554
|
+
for (let k = 0; k < stanzaCount; k++) {
|
|
555
|
+
const raw = sec.lyrics[k];
|
|
556
|
+
const fill = (n) => Array.from({ length: n }, () => d.missingStanza === "placeholder" ? {
|
|
557
|
+
kind: "syllable",
|
|
558
|
+
text: "…",
|
|
559
|
+
wordEnd: true,
|
|
560
|
+
source: STANZA_PLACEHOLDER
|
|
561
|
+
} : { kind: "empty" });
|
|
562
|
+
if (raw === void 0) {
|
|
563
|
+
stanzas.push(fill(sec.groups.length));
|
|
564
|
+
continue;
|
|
565
|
+
}
|
|
566
|
+
const { text, labels } = lyricsToStanza(raw, d, warn, loss);
|
|
567
|
+
if (k === 0) {
|
|
568
|
+
if (labels.length && d.barLabel === "drop") labels.forEach(() => loss("bar label (…) in lyrics"));
|
|
569
|
+
else for (const [i, label] of labels.entries()) {
|
|
570
|
+
const bar = sec.bars.find((b) => b.label === void 0 && b.groupsBefore === label.at) ?? sec.bars.filter((b) => b.label === void 0)[0] ?? sec.bars[i];
|
|
571
|
+
if (bar) bar.label = label.text;
|
|
572
|
+
else warn(`Bar label "(${label.text})" has no bar to go under: left out`);
|
|
573
|
+
}
|
|
574
|
+
} else if (labels.length) warn(`Bar labels in stanza ${k + 1} are left out (only stanza 1 can carry them)`);
|
|
575
|
+
let syl = parseStanza(text);
|
|
576
|
+
const slots = sec.groups.length;
|
|
577
|
+
for (const [j, item] of syl.entries()) {
|
|
578
|
+
if (item.kind !== "syllable") continue;
|
|
579
|
+
const found = takePrefix(item);
|
|
580
|
+
if (!found) continue;
|
|
581
|
+
if (!prefixes.has(k)) prefixes.set(k, /* @__PURE__ */ new Map());
|
|
582
|
+
prefixes.get(k).set(j, found);
|
|
583
|
+
}
|
|
584
|
+
if (syl.length > slots) {
|
|
585
|
+
warn(`Stanza ${k + 1}, section ${s + 1}: ${syl.length} syllables for ${slots} neumes — ${d.extraSyllables === "truncate" ? "the extra ones are left out" : "the extra ones run on into the next section"}`);
|
|
586
|
+
if (d.extraSyllables === "truncate") syl = syl.slice(0, slots);
|
|
587
|
+
} else if (syl.length < slots && sec.lyrics.length) {
|
|
588
|
+
warn(`Stanza ${k + 1}, section ${s + 1}: ${syl.length} syllables for ${slots} neumes — padded`);
|
|
589
|
+
syl = [...syl, ...fill(slots - syl.length)];
|
|
590
|
+
}
|
|
591
|
+
stanzas.push(syl);
|
|
592
|
+
}
|
|
593
|
+
const slotsWith = new Set([...prefixes.values()].flatMap((m) => [...m.keys()]));
|
|
594
|
+
for (const j of slotsWith) {
|
|
595
|
+
const owners = [...prefixes.entries()].filter(([, m]) => m.has(j)).map(([k]) => k);
|
|
596
|
+
if (owners.length === 1 && owners[0] === 0 && sec.groups[j]) {
|
|
597
|
+
sec.groups[j].prefix = prefixes.get(0).get(j);
|
|
598
|
+
continue;
|
|
599
|
+
}
|
|
600
|
+
for (const k of owners) {
|
|
601
|
+
const found = prefixes.get(k).get(j);
|
|
602
|
+
const item = stanzas[k]?.[j];
|
|
603
|
+
if (item?.kind !== "syllable") continue;
|
|
604
|
+
const span = found === "**" ? "∗" : found.replace(/^__(.+)__$/, "{liturgy:$1}").replace(/^_(.+)_$/, "{rubric:$1}");
|
|
605
|
+
stanzas[k][j] = {
|
|
606
|
+
...item,
|
|
607
|
+
text: `${unescapeScript(found)}\u00a0${item.text}`,
|
|
608
|
+
source: `${span}\u00a0${item.source ?? item.text.replace(/[\\()]/g, "\\$&")}`
|
|
609
|
+
};
|
|
610
|
+
warn(`Stanza ${k + 1}: “${unescapeScript(found)}” stays joined to its syllable (several stanzas have a display part there)`);
|
|
611
|
+
}
|
|
612
|
+
}
|
|
613
|
+
return stanzas;
|
|
614
|
+
});
|
|
615
|
+
if (d.tenorText === "split") for (const [s, sec] of sections.entries()) {
|
|
616
|
+
const stanzas = perSection[s];
|
|
617
|
+
const first = stanzas[0];
|
|
618
|
+
if (!first) continue;
|
|
619
|
+
for (let j = sec.groups.length - 1; j >= 0; j--) {
|
|
620
|
+
const group = sec.groups[j];
|
|
621
|
+
const syl = first[j];
|
|
622
|
+
if (!group.tenorOnly || syl?.kind !== "syllable" || !/[ \u00a0\ue003]/.test(syl.text)) continue;
|
|
623
|
+
const pieces = [];
|
|
624
|
+
for (const m of syl.text.matchAll(/([^ \u00a0\ue003]+)([ \u00a0]+|\ue003)?/g)) pieces.push({
|
|
625
|
+
kind: "syllable",
|
|
626
|
+
text: m[1],
|
|
627
|
+
wordEnd: m[2] === void 0 ? syl.wordEnd : m[2] !== ""
|
|
628
|
+
});
|
|
629
|
+
first.splice(j, 1, ...pieces);
|
|
630
|
+
for (const other of stanzas.slice(1)) other.splice(j + 1, 0, ...pieces.slice(1).map(() => ({ kind: "hold" })));
|
|
631
|
+
const at = items.indexOf(group);
|
|
632
|
+
const copies = pieces.slice(1).map(() => {
|
|
633
|
+
const copy = { ...group };
|
|
634
|
+
delete copy.prefix;
|
|
635
|
+
return copy;
|
|
636
|
+
});
|
|
637
|
+
items.splice(at + 1, 0, ...copies);
|
|
638
|
+
sec.groups.splice(j + 1, 0, ...copies);
|
|
639
|
+
warn(/\ue003/.test(syl.text) ? "A recited phrase was split into one reciting note per syllable (at its spaces and soft hyphens)" : "A recited phrase was split into one reciting note per word (Aretino cannot say where the syllables are)");
|
|
640
|
+
}
|
|
641
|
+
}
|
|
642
|
+
for (const stanzas of perSection) for (const st of stanzas) for (const it of st) {
|
|
643
|
+
if (it.kind !== "syllable") continue;
|
|
644
|
+
it.text = it.text.replace(/\ue003/g, "");
|
|
645
|
+
if (it.source !== void 0) it.source = it.source.replace(/\ue003/g, "");
|
|
646
|
+
}
|
|
647
|
+
const stanzaTexts = [];
|
|
648
|
+
for (let k = 0; k < stanzaCount; k++) stanzaTexts.push(perSection.map((st) => rebuildStanza(st[k] ?? [])).filter(Boolean).join(" "));
|
|
649
|
+
for (const [i, it] of items.entries()) {
|
|
650
|
+
if (it.kind !== "bar") continue;
|
|
651
|
+
const prev = items[i - 1];
|
|
652
|
+
if (it.label !== void 0 && d.barLabel === "own" && !isStarLabel(it.label)) continue;
|
|
653
|
+
if (prev?.kind === "group") {
|
|
654
|
+
prev.notes += (prev.markerEnd ? "" : "-") + it.glyph;
|
|
655
|
+
prev.markerEnd = true;
|
|
656
|
+
it.attached = true;
|
|
657
|
+
it.host = prev;
|
|
658
|
+
} else if (prev?.kind === "bar") {
|
|
659
|
+
if (prev.host) {
|
|
660
|
+
prev.host.notes += it.glyph;
|
|
661
|
+
it.host = prev.host;
|
|
662
|
+
} else prev.glyph += it.glyph;
|
|
663
|
+
it.attached = true;
|
|
664
|
+
}
|
|
665
|
+
}
|
|
666
|
+
const body = [];
|
|
667
|
+
if (indentLabel) body.push(`_${escapeNotes(indentLabel)}_()`);
|
|
668
|
+
for (const it of items) if (it.kind === "group") {
|
|
669
|
+
if (it.prefix) body.push(d.tildes2 === "label" ? `_${it.prefix}_()` : `${rubricSugar(it.prefix)}()`);
|
|
670
|
+
body.push(`~(${escapeNotes(it.notes)})`);
|
|
671
|
+
} else if (it.kind === "stmt") body.push(it.text);
|
|
672
|
+
else {
|
|
673
|
+
if (it.label !== void 0 && d.barLabel !== "drop" && isStarLabel(it.label)) {
|
|
674
|
+
body.push(it.attached ? "**()" : `**(${escapeNotes(it.glyph)})`);
|
|
675
|
+
continue;
|
|
676
|
+
}
|
|
677
|
+
const label = it.label === void 0 || d.barLabel === "drop" ? "" : /[{}]/.test(it.label) ? `{rubric:${it.label}}` : `_${it.label}_`;
|
|
678
|
+
if (!it.attached) body.push(`${d.barLabel === "own" ? label : ""}(${escapeNotes(it.glyph)})`);
|
|
679
|
+
if (label && d.barLabel === "before") body.push(`${label}()`);
|
|
680
|
+
}
|
|
681
|
+
let script = opening + body.join(" ");
|
|
682
|
+
const groupsExist = items.some((it) => it.kind === "group");
|
|
683
|
+
if (stanzaTexts.length > 0 && groupsExist) {
|
|
684
|
+
script = withStanza(`${script} (@stanza ${stanzaTexts[0]})`, 2).source;
|
|
685
|
+
script += stanzaTexts.slice(1).map((s) => ` (@stanza ${s})`).join("");
|
|
686
|
+
}
|
|
687
|
+
script = script.replace(/(\((?:@break|@key[^)]*|@clef [^)]*)\))(?=[^\s(])/g, "$1 ");
|
|
688
|
+
script = joinBreathHolds(script);
|
|
689
|
+
script = script.replace(/((?:^|\s)\*\*\([^()]*\))~\(/g, "$1 ~(");
|
|
690
|
+
if (semitones !== 0) script = transposeBySemitones(script, semitones, fifthsOf(openingKey ?? []), keyChanges, d.transpose === "regions", warn);
|
|
691
|
+
const sw = safeStanzaWarnings(script);
|
|
692
|
+
return {
|
|
693
|
+
script,
|
|
694
|
+
stanzas: stanzaTexts.slice(1),
|
|
695
|
+
warnings,
|
|
696
|
+
lost,
|
|
697
|
+
metadata,
|
|
698
|
+
verse,
|
|
699
|
+
stanzaWarnings: sw
|
|
700
|
+
};
|
|
701
|
+
} catch (e) {
|
|
702
|
+
return {
|
|
703
|
+
script: "",
|
|
704
|
+
stanzas: [],
|
|
705
|
+
warnings,
|
|
706
|
+
lost,
|
|
707
|
+
metadata,
|
|
708
|
+
verse,
|
|
709
|
+
stanzaWarnings: [],
|
|
710
|
+
error: e instanceof Error ? e.message : String(e)
|
|
711
|
+
};
|
|
712
|
+
}
|
|
713
|
+
}
|
|
714
|
+
function safeStanzaWarnings(script) {
|
|
715
|
+
try {
|
|
716
|
+
return stanzaWarnings(script).map((w) => w.message);
|
|
717
|
+
} catch (e) {
|
|
718
|
+
return [String(e)];
|
|
719
|
+
}
|
|
720
|
+
}
|
|
721
|
+
/** The cephalicus glyph of a pitch glyph (on d it is ', as typists write it, in guidex's table too) */
|
|
722
|
+
var cephalicusOf = (base) => toCephalicus(base);
|
|
723
|
+
/** Joins a held group to the group before it when that group ends in a breath (a cephalicus after a dash):
|
|
724
|
+
* (…qQ0-')(1) → (…qQ0-'-1). Not with stanzas, where the held group may be another stanza's slot */
|
|
725
|
+
function joinBreathHolds(script) {
|
|
726
|
+
if (/\(@stanza /.test(script)) return script;
|
|
727
|
+
const gs = new GuidoScript(script);
|
|
728
|
+
const tokens = gs.tokens;
|
|
729
|
+
const spans = gs.tokenSpans;
|
|
730
|
+
let out = script;
|
|
731
|
+
for (let i = tokens.length - 1; i > 0; i--) {
|
|
732
|
+
const tok = tokens[i];
|
|
733
|
+
const prev = tokens[i - 1];
|
|
734
|
+
if (tok.kind !== "syllable" || !tok.held || prev.kind !== "syllable") continue;
|
|
735
|
+
const chars = [...prev.notes];
|
|
736
|
+
const last = chars.at(-1) ?? "";
|
|
737
|
+
if (chars.at(-2) !== "-" || last !== "'" && baseOf(last)?.role !== "cephalicus") continue;
|
|
738
|
+
out = out.slice(0, spans[i - 1].notesEnd) + "-" + out.slice(spans[i].notesStart);
|
|
739
|
+
}
|
|
740
|
+
return out;
|
|
741
|
+
}
|
|
742
|
+
/** A bar label that is the flex star: (*) under a bar */
|
|
743
|
+
var isStarLabel = (label) => /^(\\?\*|∗)$/.test(label);
|
|
744
|
+
/** A key on the line of fifths: +1 per sharp, −1 per flat (naturals 0), whatever the clef */
|
|
745
|
+
function fifthsOf(accs) {
|
|
746
|
+
return accs.reduce((f, acc) => f + (acc.symbol === "#" ? 1 : acc.symbol === "x" ? -1 : 0), 0);
|
|
747
|
+
}
|
|
748
|
+
/** Aretino's transposeTarget: the target key with the fewest accidentals
|
|
749
|
+
* (at most 6 flats or 5 sharps, ties to flats) and the staff steps `d` */
|
|
750
|
+
function transposeTarget(f0, semitones) {
|
|
751
|
+
const r = ((f0 + 7 * semitones) % 12 + 12) % 12;
|
|
752
|
+
const f1 = r <= 5 ? r : r - 12;
|
|
753
|
+
return {
|
|
754
|
+
f1,
|
|
755
|
+
d: Math.round((7 * semitones - (f1 - f0)) / 12)
|
|
756
|
+
};
|
|
757
|
+
}
|
|
758
|
+
var STEP_SEMITONES = [
|
|
759
|
+
0,
|
|
760
|
+
2,
|
|
761
|
+
4,
|
|
762
|
+
5,
|
|
763
|
+
7,
|
|
764
|
+
9,
|
|
765
|
+
11
|
|
766
|
+
];
|
|
767
|
+
/** A spelled interval's name: { steps: 1, semitones: 1 } → m2 */
|
|
768
|
+
function intervalLabel({ steps, semitones }) {
|
|
769
|
+
const down = steps < 0 || steps === 0 && semitones < 0;
|
|
770
|
+
const s = Math.abs(steps);
|
|
771
|
+
const diff = (down ? -semitones : semitones) - (Math.floor(s / 7) * 12 + STEP_SEMITONES[s % 7]);
|
|
772
|
+
const quality = ([
|
|
773
|
+
0,
|
|
774
|
+
3,
|
|
775
|
+
4
|
|
776
|
+
].includes(s % 7) ? {
|
|
777
|
+
"-1": "d",
|
|
778
|
+
0: "P",
|
|
779
|
+
1: "A"
|
|
780
|
+
} : {
|
|
781
|
+
"-2": "d",
|
|
782
|
+
"-1": "m",
|
|
783
|
+
0: "M",
|
|
784
|
+
1: "A"
|
|
785
|
+
})[diff];
|
|
786
|
+
return `${down ? "-" : ""}${quality ?? `(${diff > 0 ? "+" : ""}${diff})`}${s + 1}`;
|
|
787
|
+
}
|
|
788
|
+
var keyLabel = (f) => f > 0 ? `${f}♯` : f < 0 ? `${-f}♭` : "no accidentals";
|
|
789
|
+
/** Transposes the finished script by N semitones. One interval, from the
|
|
790
|
+
* opening key (`regions` false), or, as Aretino does, re-spelled from each
|
|
791
|
+
* key change on: the script is cut at its key-change statements, each part
|
|
792
|
+
* transposed with its own interval under the clef and key in effect there,
|
|
793
|
+
* and the parts' new keys written back as the statements. */
|
|
794
|
+
function transposeBySemitones(script, semitones, openingFifths, keyChanges, regions, warn) {
|
|
795
|
+
const first = transposeTarget(openingFifths, semitones);
|
|
796
|
+
const interval = (d) => ({
|
|
797
|
+
steps: d,
|
|
798
|
+
semitones
|
|
799
|
+
});
|
|
800
|
+
warn(`%transpose: ${semitones} read as d=${first.d} (${intervalLabel(interval(first.d))}), target key ${keyLabel(first.f1)}`);
|
|
801
|
+
const respelled = keyChanges.map((kc) => ({
|
|
802
|
+
kc,
|
|
803
|
+
...transposeTarget(kc.fifths, semitones)
|
|
804
|
+
}));
|
|
805
|
+
let previous = first.d;
|
|
806
|
+
for (const r of respelled) {
|
|
807
|
+
if (r.d !== previous) warn(regions ? `Re-spelled after ${r.kc.label}: d=${r.d} (${intervalLabel(interval(r.d))}), target key ${keyLabel(r.f1)}, as Aretino does` : `Aretino re-spells the transposition after ${r.kc.label} (d=${r.d}, ${intervalLabel(interval(r.d))}); guidex uses one interval (d=${first.d})`);
|
|
808
|
+
previous = r.d;
|
|
809
|
+
}
|
|
810
|
+
if (!regions || respelled.every((r) => r.d === first.d)) return transposeScript(script, { interval: interval(first.d) });
|
|
811
|
+
const parts = [];
|
|
812
|
+
let cursor = 0;
|
|
813
|
+
let start = 0;
|
|
814
|
+
let current = { d: first.d };
|
|
815
|
+
for (const r of respelled) {
|
|
816
|
+
const text = r.kc.withClef ? `(@clef ${r.kc.clef}${r.kc.glyphs})` : `(@key${r.kc.glyphs ? " " + r.kc.glyphs : ""})`;
|
|
817
|
+
const at = script.indexOf(text, cursor);
|
|
818
|
+
if (at < 0) {
|
|
819
|
+
warn(`Could not find ${r.kc.label} in the script: one interval used`);
|
|
820
|
+
return transposeScript(script, { interval: interval(first.d) });
|
|
821
|
+
}
|
|
822
|
+
parts.push({
|
|
823
|
+
body: script.slice(start, at),
|
|
824
|
+
...current
|
|
825
|
+
});
|
|
826
|
+
current = {
|
|
827
|
+
d: r.d,
|
|
828
|
+
kc: r.kc
|
|
829
|
+
};
|
|
830
|
+
start = at + text.length;
|
|
831
|
+
cursor = start;
|
|
832
|
+
}
|
|
833
|
+
parts.push({
|
|
834
|
+
body: script.slice(start),
|
|
835
|
+
...current
|
|
836
|
+
});
|
|
837
|
+
return parts.map((part) => {
|
|
838
|
+
if (!part.kc) return transposeScript(part.body, { interval: interval(part.d) });
|
|
839
|
+
const moved = transposeScript(`(${part.kc.clef}${part.kc.glyphs})${part.body}`, { interval: interval(part.d) });
|
|
840
|
+
const head = moved.match(/^\(([^)]*)\)/);
|
|
841
|
+
const [clef, keys] = [head[1].slice(0, 1), head[1].slice(1)];
|
|
842
|
+
return (part.kc.withClef ? `(@clef ${clef}${keys})` : `(@key${keys ? " " + keys : ""})`) + moved.slice(head[0].length);
|
|
843
|
+
}).join("");
|
|
844
|
+
}
|
|
845
|
+
/** The round bracket pair whose height suits the notes (Guido HU draws
|
|
846
|
+
* them low around e′, in the middle around a′, high around e″) */
|
|
847
|
+
function roundBrackets(positions) {
|
|
848
|
+
const mean = positions.reduce((a, b) => a + b, 0) / Math.max(1, positions.length);
|
|
849
|
+
return mean <= 5.5 ? ["Æ", "Ç"] : mean <= 9 ? ["Â", "Ã"] : ["Ä", "Å"];
|
|
850
|
+
}
|
|
851
|
+
var PREFIX_OPEN = "";
|
|
852
|
+
var PREFIX_CLOSE = "";
|
|
853
|
+
var PREFIX = /\ue001(.*?)\ue002/;
|
|
854
|
+
/** Takes the marked display part (1.~~Ky) off a stanza syllable, in place;
|
|
855
|
+
* returns it in script notation */
|
|
856
|
+
function takePrefix(item) {
|
|
857
|
+
const inSource = item.source?.match(PREFIX);
|
|
858
|
+
const inText = item.text.match(PREFIX);
|
|
859
|
+
if (!inSource && !inText) return void 0;
|
|
860
|
+
item.text = item.text.replace(PREFIX, "");
|
|
861
|
+
if (item.source !== void 0) item.source = item.source.replace(PREFIX, "");
|
|
862
|
+
return inSource ? inSource[1] : inText[1].replace(/[\\()]/g, "\\$&");
|
|
863
|
+
}
|
|
864
|
+
var unescapeScript = (text) => text.replace(/\\(.)/g, "$1");
|
|
865
|
+
/** A rubric span that is a whole syllable, or the whole of the span around it, is written
|
|
866
|
+
* with the rubric wrapper (the wrapper only matches a whole segment) */
|
|
867
|
+
var rubricSugar = (text) => text.replace(/(^|:)\{rubric:([^{}]*)\}(?=\}|$)/g, "$1_$2_");
|
|
868
|
+
/** Stanza items back to `(@stanza …)` text. */
|
|
869
|
+
function rebuildStanza(items) {
|
|
870
|
+
return items.map((it) => {
|
|
871
|
+
if (it.kind === "hold") return "-";
|
|
872
|
+
if (it.kind === "empty") return "~";
|
|
873
|
+
return (it.source !== void 0 ? rubricSugar(it.source) : it.text.replace(/[\\()]/g, "\\$&")).replace(/[-=~]/g, (c) => `\\${c}`).replace(/[ \u00a0]/g, "~") + (it.wordEnd ? "" : "-");
|
|
874
|
+
}).join(" ");
|
|
875
|
+
}
|
|
876
|
+
/** One Aretino `w:` line → guidex stanza text, plus its bar labels. */
|
|
877
|
+
function lyricsToStanza(raw, d, warn, loss) {
|
|
878
|
+
const words = lyricWords(raw);
|
|
879
|
+
const out = [];
|
|
880
|
+
const labels = [];
|
|
881
|
+
const closers = [];
|
|
882
|
+
for (const word of words) {
|
|
883
|
+
if (word.startsWith("(") && word.endsWith(")")) {
|
|
884
|
+
const at = out.length ? parseStanza(out.join(" ").replace(/\ue001.*?\ue002/g, "")).length : 0;
|
|
885
|
+
labels.push({
|
|
886
|
+
text: convertText(word.slice(1, -1), d, warn, loss, []).replace(/[()]/g, "\\$&"),
|
|
887
|
+
at
|
|
888
|
+
});
|
|
889
|
+
continue;
|
|
890
|
+
}
|
|
891
|
+
if (word === "*") {
|
|
892
|
+
out.push(d.star === "star" ? "**" : d.star === "escaped" ? "\\*" : "*");
|
|
893
|
+
continue;
|
|
894
|
+
}
|
|
895
|
+
out.push(convertText(word, d, warn, loss, closers));
|
|
896
|
+
}
|
|
897
|
+
if (closers.length) warn("An unclosed formatting span was closed at the end of the line");
|
|
898
|
+
let text = out.join(" ");
|
|
899
|
+
while (closers.length) text += closers.pop();
|
|
900
|
+
return {
|
|
901
|
+
text,
|
|
902
|
+
labels
|
|
903
|
+
};
|
|
904
|
+
}
|
|
905
|
+
/** A lyric line's words, with bar labels as words of their own: as
|
|
906
|
+
* Aretino reads them, a label runs from any unescaped ( to the next ),
|
|
907
|
+
* spaces and all ((\\red{{bold red}})), even in the middle of a word;
|
|
908
|
+
* spaces next to a hyphen do not end a word. */
|
|
909
|
+
function lyricWords(raw) {
|
|
910
|
+
const out = [];
|
|
911
|
+
let word = "";
|
|
912
|
+
const flush = () => {
|
|
913
|
+
if (word !== "") out.push(word);
|
|
914
|
+
word = "";
|
|
915
|
+
};
|
|
916
|
+
for (let i = 0; i < raw.length; i++) {
|
|
917
|
+
const ch = raw[i];
|
|
918
|
+
if (ch === "\\") {
|
|
919
|
+
word += ch + (raw[i + 1] ?? "");
|
|
920
|
+
i++;
|
|
921
|
+
continue;
|
|
922
|
+
}
|
|
923
|
+
if (ch === "(") {
|
|
924
|
+
flush();
|
|
925
|
+
const end = raw.indexOf(")", i);
|
|
926
|
+
out.push(end < 0 ? raw.slice(i) + ")" : raw.slice(i, end + 1));
|
|
927
|
+
i = end < 0 ? raw.length : end;
|
|
928
|
+
continue;
|
|
929
|
+
}
|
|
930
|
+
if (/\s/.test(ch)) {
|
|
931
|
+
let next = i;
|
|
932
|
+
while (next < raw.length && /\s/.test(raw[next])) next++;
|
|
933
|
+
if (word !== "" && (/[-=]$/.test(word) || /[-=]/.test(raw[next] ?? ""))) {
|
|
934
|
+
i = next - 1;
|
|
935
|
+
continue;
|
|
936
|
+
}
|
|
937
|
+
flush();
|
|
938
|
+
continue;
|
|
939
|
+
}
|
|
940
|
+
word += ch;
|
|
941
|
+
}
|
|
942
|
+
flush();
|
|
943
|
+
return out;
|
|
944
|
+
}
|
|
945
|
+
function convertText(word, d, _warn, loss, closers) {
|
|
946
|
+
let out = "";
|
|
947
|
+
for (let i = 0; i < word.length; i++) {
|
|
948
|
+
const ch = word[i];
|
|
949
|
+
const rest = word.slice(i);
|
|
950
|
+
if (ch === "\\") {
|
|
951
|
+
const initial = rest.match(/^\\color:initial\{/) ?? rest.match(/^\\red\{\\large\{/);
|
|
952
|
+
if (initial) {
|
|
953
|
+
i += initial[0].length - 1;
|
|
954
|
+
out += "*";
|
|
955
|
+
closers.push("*");
|
|
956
|
+
if (initial[0].startsWith("\\red")) closers.push("");
|
|
957
|
+
continue;
|
|
958
|
+
}
|
|
959
|
+
const rubric = rest.match(/^\\color:rubric\{/) ?? rest.match(/^\\red\{\\sc\{/);
|
|
960
|
+
if (rubric) {
|
|
961
|
+
i += rubric[0].length - 1;
|
|
962
|
+
out += "{rubric:";
|
|
963
|
+
closers.push("}");
|
|
964
|
+
if (rubric[0].startsWith("\\red")) closers.push("");
|
|
965
|
+
continue;
|
|
966
|
+
}
|
|
967
|
+
const cmd = rest.match(/^\\(red|sc|small|large)\{/) ?? rest.match(/^\\color:([^{]*)\{/);
|
|
968
|
+
if (cmd) {
|
|
969
|
+
i += cmd[0].length - 1;
|
|
970
|
+
if (cmd[0].startsWith("\\color:")) {
|
|
971
|
+
if (d.color === "span" && chantTextStyle(`color:${cmd[1]}`).color) {
|
|
972
|
+
out += `{color:${cmd[1]}:`;
|
|
973
|
+
closers.push("}");
|
|
974
|
+
} else {
|
|
975
|
+
loss(d.color === "span" ? `\\color:${cmd[1]}{…} (no CSS color)` : "\\color:NAME{…}");
|
|
976
|
+
closers.push("");
|
|
977
|
+
}
|
|
978
|
+
} else if (cmd[1] === "red") {
|
|
979
|
+
if (d.red === "span") {
|
|
980
|
+
out += "{red:";
|
|
981
|
+
closers.push("}");
|
|
982
|
+
} else {
|
|
983
|
+
out += "_";
|
|
984
|
+
closers.push("_");
|
|
985
|
+
}
|
|
986
|
+
} else if (cmd[1] === "sc") {
|
|
987
|
+
out += "{sc:";
|
|
988
|
+
closers.push("}");
|
|
989
|
+
} else if (cmd[1] === "small") {
|
|
990
|
+
out += "{size:0.75:";
|
|
991
|
+
closers.push("}");
|
|
992
|
+
} else {
|
|
993
|
+
out += "{size:1.33:";
|
|
994
|
+
closers.push("}");
|
|
995
|
+
}
|
|
996
|
+
continue;
|
|
997
|
+
}
|
|
998
|
+
const next = word[i + 1];
|
|
999
|
+
i++;
|
|
1000
|
+
if (next === void 0) continue;
|
|
1001
|
+
if (next === "R" || next === "V") {
|
|
1002
|
+
const whole = out === "" && i === word.length - 1;
|
|
1003
|
+
out += d.rv === "unicode" ? next === "R" ? "℟" : "℣" : d.rv === "liturgy" ? whole ? `__${next}.__` : `{liturgy:${next}.}` : `${next}.`;
|
|
1004
|
+
continue;
|
|
1005
|
+
}
|
|
1006
|
+
if ("bn#'".includes(next)) {
|
|
1007
|
+
const sign = {
|
|
1008
|
+
b: "♭",
|
|
1009
|
+
n: "♮",
|
|
1010
|
+
"#": "♯",
|
|
1011
|
+
"'": "́"
|
|
1012
|
+
}[next];
|
|
1013
|
+
if (d.textAccidentals === "unicode") out += sign;
|
|
1014
|
+
else loss(`\\${next} sign in the text`);
|
|
1015
|
+
continue;
|
|
1016
|
+
}
|
|
1017
|
+
out += "(){}_*-~=\\†".includes(next) ? `\\${next}` : next;
|
|
1018
|
+
continue;
|
|
1019
|
+
}
|
|
1020
|
+
if (ch === "{") {
|
|
1021
|
+
out += "{b:";
|
|
1022
|
+
closers.push("}");
|
|
1023
|
+
continue;
|
|
1024
|
+
}
|
|
1025
|
+
if (ch === "<") {
|
|
1026
|
+
out += "{i:";
|
|
1027
|
+
closers.push("}");
|
|
1028
|
+
continue;
|
|
1029
|
+
}
|
|
1030
|
+
if (ch === "[") {
|
|
1031
|
+
if (d.underline === "u" || d.underline === "italic") {
|
|
1032
|
+
out += d.underline === "u" ? "{u:" : "{i:";
|
|
1033
|
+
closers.push("}");
|
|
1034
|
+
} else {
|
|
1035
|
+
loss("[underline]");
|
|
1036
|
+
closers.push("");
|
|
1037
|
+
}
|
|
1038
|
+
continue;
|
|
1039
|
+
}
|
|
1040
|
+
if (ch === "}" || ch === ">" || ch === "]") {
|
|
1041
|
+
out += closers.pop() ?? "";
|
|
1042
|
+
continue;
|
|
1043
|
+
}
|
|
1044
|
+
if (ch === "(" || ch === ")") {
|
|
1045
|
+
out += `\\${ch}`;
|
|
1046
|
+
continue;
|
|
1047
|
+
}
|
|
1048
|
+
if (ch === "*") {
|
|
1049
|
+
out += "\\*";
|
|
1050
|
+
continue;
|
|
1051
|
+
}
|
|
1052
|
+
if (ch === "+") {
|
|
1053
|
+
const double = word[i + 1] === "+";
|
|
1054
|
+
if (double) i++;
|
|
1055
|
+
const one = d.dagger === "sign" ? "†" : "\\†";
|
|
1056
|
+
out += double ? d.dagger2 === "‡" ? "‡" : d.dagger2 === "††" ? one + one : "" : one;
|
|
1057
|
+
if (double && d.dagger2 === "drop") loss("++ double dagger");
|
|
1058
|
+
continue;
|
|
1059
|
+
}
|
|
1060
|
+
if (ch === "~" && word[i + 1] === "~") {
|
|
1061
|
+
i++;
|
|
1062
|
+
if (d.tildes2 === "join") out += "~";
|
|
1063
|
+
else if (d.tildes2 === "before" || d.tildes2 === "label") out = `${PREFIX_OPEN}${out === "\\*" ? "**" : out.replace(/^\{liturgy:(.+)\}$/, "__$1__")}${PREFIX_CLOSE}`;
|
|
1064
|
+
else out = "";
|
|
1065
|
+
continue;
|
|
1066
|
+
}
|
|
1067
|
+
if (ch === "_") {
|
|
1068
|
+
let n = 1;
|
|
1069
|
+
while (word[i + n] === "_") n++;
|
|
1070
|
+
i += n - 1;
|
|
1071
|
+
let tail = "";
|
|
1072
|
+
while (i + 1 < word.length && !"-=_".includes(word[i + 1])) tail += word[++i];
|
|
1073
|
+
if (d.extenderTail === "keep") out += tail;
|
|
1074
|
+
else if (tail) loss("punctuation after an extender");
|
|
1075
|
+
if (word[i + 1] === "-" || word[i + 1] === "=") {
|
|
1076
|
+
out += "-";
|
|
1077
|
+
i++;
|
|
1078
|
+
}
|
|
1079
|
+
out += " -".repeat(n - 1);
|
|
1080
|
+
if (n > 1 && i + 1 < word.length) out += " ";
|
|
1081
|
+
continue;
|
|
1082
|
+
}
|
|
1083
|
+
out += ch === "" ? "" : ch;
|
|
1084
|
+
}
|
|
1085
|
+
return out;
|
|
1086
|
+
}
|
|
1087
|
+
//#endregion
|
|
1088
|
+
//#region src/aretino/toAretino.ts
|
|
1089
|
+
/**
|
|
1090
|
+
* GuidoScript → Aretino, the way back (issue #79), written from the forward
|
|
1091
|
+
* converter's decisions (fromAretino.ts), not from upstream's guido2aretino.
|
|
1092
|
+
* Every point of difference is a decision (`ReverseDecisions`); the defaults
|
|
1093
|
+
* are the decided choices, and `guidoToAretino` (./index.ts) always uses
|
|
1094
|
+
* them. prototypes/aretino/reverse.html shows the candidates side by side.
|
|
1095
|
+
*
|
|
1096
|
+
* The principle (prototypes/aretino/TWO-WAY.md): what the forward converter derives
|
|
1097
|
+
* (connectors, auto virgas, the cut after an inner mora) is not written;
|
|
1098
|
+
* the round trip may correct a script towards that ideal form.
|
|
1099
|
+
*
|
|
1100
|
+
* Pure: no DOM.
|
|
1101
|
+
*/
|
|
1102
|
+
var REVERSE_DEFAULTS = {
|
|
1103
|
+
virga: "cut",
|
|
1104
|
+
dashInGroup: "///",
|
|
1105
|
+
held: "hold",
|
|
1106
|
+
tenorRun: "each",
|
|
1107
|
+
quarterBar: ",",
|
|
1108
|
+
keyForm: "short",
|
|
1109
|
+
rubric: "redsc",
|
|
1110
|
+
initial: "redlarge",
|
|
1111
|
+
noteless: "prefix",
|
|
1112
|
+
breaks: "Z",
|
|
1113
|
+
loneCeph: "plica",
|
|
1114
|
+
cephMark: "~",
|
|
1115
|
+
extenderPunct: "after"
|
|
1116
|
+
};
|
|
1117
|
+
var LETTERS = "ABcdefgabCDEFG";
|
|
1118
|
+
var QUILISMAS = "àâãäåæçèêëìîï";
|
|
1119
|
+
var CLEF_NAMES = {
|
|
1120
|
+
">": "c1",
|
|
1121
|
+
"#": "c2",
|
|
1122
|
+
"&": "c3",
|
|
1123
|
+
"@": "c4",
|
|
1124
|
+
"{": "f3",
|
|
1125
|
+
"}": "f4",
|
|
1126
|
+
M: "g2",
|
|
1127
|
+
"<": "g2"
|
|
1128
|
+
};
|
|
1129
|
+
var OPEN_BRACKETS = /* @__PURE__ */ new Set([
|
|
1130
|
+
"Æ",
|
|
1131
|
+
"Â",
|
|
1132
|
+
"Ä",
|
|
1133
|
+
"¿"
|
|
1134
|
+
]);
|
|
1135
|
+
var CLOSE_BRACKETS = /* @__PURE__ */ new Set([
|
|
1136
|
+
"Ç",
|
|
1137
|
+
"Ã",
|
|
1138
|
+
"Å",
|
|
1139
|
+
"À"
|
|
1140
|
+
]);
|
|
1141
|
+
var FLAT_KEY = [
|
|
1142
|
+
"b",
|
|
1143
|
+
"E",
|
|
1144
|
+
"a",
|
|
1145
|
+
"D",
|
|
1146
|
+
"g",
|
|
1147
|
+
"C",
|
|
1148
|
+
"F"
|
|
1149
|
+
];
|
|
1150
|
+
var SHARP_KEY = [
|
|
1151
|
+
"F",
|
|
1152
|
+
"C",
|
|
1153
|
+
"G",
|
|
1154
|
+
"D",
|
|
1155
|
+
"a",
|
|
1156
|
+
"E",
|
|
1157
|
+
"b"
|
|
1158
|
+
];
|
|
1159
|
+
var letterAt = (pos) => pos < 14 ? LETTERS[pos] : `^${LETTERS[pos - 7]}`;
|
|
1160
|
+
var posOf = (ch) => STAFF.indexOf(ch);
|
|
1161
|
+
var accOf = (ch) => {
|
|
1162
|
+
const b = baseOf(ch);
|
|
1163
|
+
if (!b || ![
|
|
1164
|
+
"modifyUp",
|
|
1165
|
+
"modifyDown",
|
|
1166
|
+
"modifyNatural"
|
|
1167
|
+
].includes(b.role)) return void 0;
|
|
1168
|
+
return {
|
|
1169
|
+
letter: letterAt(posOf(b.base)),
|
|
1170
|
+
sym: b.role === "modifyDown" ? "b" : b.role === "modifyUp" ? "#" : "n"
|
|
1171
|
+
};
|
|
1172
|
+
};
|
|
1173
|
+
/** `forward`: the forward decisions the result must read back under (virgas, leaps) */
|
|
1174
|
+
function reverse(script, d = REVERSE_DEFAULTS, forward = DEFAULTS) {
|
|
1175
|
+
const warnings = [];
|
|
1176
|
+
const lost = {};
|
|
1177
|
+
const loss = (what) => {
|
|
1178
|
+
lost[what] = (lost[what] ?? 0) + 1;
|
|
1179
|
+
};
|
|
1180
|
+
const warn = (msg) => {
|
|
1181
|
+
if (!warnings.includes(msg)) warnings.push(msg);
|
|
1182
|
+
};
|
|
1183
|
+
try {
|
|
1184
|
+
const gs = new GuidoScript(script);
|
|
1185
|
+
const tokens = gs.tokens;
|
|
1186
|
+
const opening = gs.clef ?? "M";
|
|
1187
|
+
const clefName = (glyph) => {
|
|
1188
|
+
const name = CLEF_NAMES[glyph];
|
|
1189
|
+
if (!name) {
|
|
1190
|
+
warn(`Clef "${glyph}": no Aretino clef, (g2) written`);
|
|
1191
|
+
return "g2";
|
|
1192
|
+
}
|
|
1193
|
+
return name;
|
|
1194
|
+
};
|
|
1195
|
+
const keyDirective = (glyphs) => {
|
|
1196
|
+
const accs = [...glyphs].map(accOf).filter((a) => !!a);
|
|
1197
|
+
if (accs.length === 0) return "";
|
|
1198
|
+
if (d.keyForm === "short") {
|
|
1199
|
+
const flats = accs.every((a, i) => a.sym === "b" && FLAT_KEY[i] === a.letter);
|
|
1200
|
+
const sharps = accs.every((a, i) => a.sym === "#" && SHARP_KEY[i] === a.letter);
|
|
1201
|
+
if (flats || sharps) return `(K${(flats ? "b" : "#").repeat(accs.length)})`;
|
|
1202
|
+
}
|
|
1203
|
+
return `(K: ${accs.map((a) => a.letter + a.sym).join(" ")})`;
|
|
1204
|
+
};
|
|
1205
|
+
const parseGroup = (raw) => {
|
|
1206
|
+
const notes = raw.replace(/\u00b5/g, "μ");
|
|
1207
|
+
const split = splitNoteMarkers(notes);
|
|
1208
|
+
const { leading } = split;
|
|
1209
|
+
let { core, trailing } = split;
|
|
1210
|
+
const end = core.match(/[-¨]+([^-¨]+)$/);
|
|
1211
|
+
if (end && core.length > end[0].length && (/^[,.:;']+$/.test(end[1]) || baseOf(end[1])?.role === "cephalicus")) {
|
|
1212
|
+
trailing = end[1] + trailing;
|
|
1213
|
+
core = core.slice(0, -end[0].length);
|
|
1214
|
+
}
|
|
1215
|
+
const els = [];
|
|
1216
|
+
let pendingAcc;
|
|
1217
|
+
let last;
|
|
1218
|
+
/** A connector glyph since the last note */
|
|
1219
|
+
let connected = false;
|
|
1220
|
+
const add = (note) => {
|
|
1221
|
+
if (pendingAcc) {
|
|
1222
|
+
note.acc = pendingAcc;
|
|
1223
|
+
pendingAcc = void 0;
|
|
1224
|
+
}
|
|
1225
|
+
if (last && els.at(-1)?.kind === "note" && note.pos < last.pos && !connected && !last.mora && !last.plica && !note.acc) els.push({
|
|
1226
|
+
kind: "gap",
|
|
1227
|
+
n: 1,
|
|
1228
|
+
dash: false,
|
|
1229
|
+
units: 1
|
|
1230
|
+
});
|
|
1231
|
+
els.push({
|
|
1232
|
+
kind: "note",
|
|
1233
|
+
note
|
|
1234
|
+
});
|
|
1235
|
+
last = note;
|
|
1236
|
+
connected = false;
|
|
1237
|
+
};
|
|
1238
|
+
for (let i = 0; i < core.length; i++) {
|
|
1239
|
+
const ch = core[i];
|
|
1240
|
+
if (ch === "-" || ch === "¨") {
|
|
1241
|
+
let n = 0;
|
|
1242
|
+
let units = 0;
|
|
1243
|
+
let dash = false;
|
|
1244
|
+
while (core[i] === "-" || core[i] === "¨") {
|
|
1245
|
+
if (core[i] === "-") {
|
|
1246
|
+
dash = true;
|
|
1247
|
+
units += 3;
|
|
1248
|
+
} else units++;
|
|
1249
|
+
n++;
|
|
1250
|
+
i++;
|
|
1251
|
+
}
|
|
1252
|
+
i--;
|
|
1253
|
+
if (!dash && last?.mora && els.at(-1)?.kind === "note") continue;
|
|
1254
|
+
els.push({
|
|
1255
|
+
kind: "gap",
|
|
1256
|
+
n: dash ? 1 : n,
|
|
1257
|
+
dash,
|
|
1258
|
+
units
|
|
1259
|
+
});
|
|
1260
|
+
continue;
|
|
1261
|
+
}
|
|
1262
|
+
if (OPEN_BRACKETS.has(ch) || CLOSE_BRACKETS.has(ch)) {
|
|
1263
|
+
if (OPEN_BRACKETS.has(ch) && accOf(core[i + 1] ?? "")) {
|
|
1264
|
+
let j = i + 1;
|
|
1265
|
+
while (accOf(core[j] ?? "")) j++;
|
|
1266
|
+
if (CLOSE_BRACKETS.has(core[j] ?? "")) {
|
|
1267
|
+
const acc = accOf(core[j - 1]);
|
|
1268
|
+
if (acc) pendingAcc = {
|
|
1269
|
+
...acc,
|
|
1270
|
+
cautionary: true
|
|
1271
|
+
};
|
|
1272
|
+
if (els.some((e) => e.kind === "note")) els.push({ kind: "split" });
|
|
1273
|
+
i = j;
|
|
1274
|
+
continue;
|
|
1275
|
+
}
|
|
1276
|
+
}
|
|
1277
|
+
els.push({ kind: OPEN_BRACKETS.has(ch) ? "open" : "close" });
|
|
1278
|
+
continue;
|
|
1279
|
+
}
|
|
1280
|
+
if (ch === "\\") {
|
|
1281
|
+
if (last) last.episema = true;
|
|
1282
|
+
continue;
|
|
1283
|
+
}
|
|
1284
|
+
if ((ch === "'" || baseOf(ch)?.role === "cephalicus") && els.at(-1)?.kind === "gap" && /[-¨]/.test(core[i + 1] ?? "")) {
|
|
1285
|
+
els.push({
|
|
1286
|
+
kind: "bar",
|
|
1287
|
+
markers: ch
|
|
1288
|
+
});
|
|
1289
|
+
continue;
|
|
1290
|
+
}
|
|
1291
|
+
if (ch === "'" && last && els.at(-1)?.kind === "note") {
|
|
1292
|
+
last.plica = true;
|
|
1293
|
+
continue;
|
|
1294
|
+
}
|
|
1295
|
+
if (ch === "?") {
|
|
1296
|
+
loss("the ? mark (written as a full bar)");
|
|
1297
|
+
els.push({
|
|
1298
|
+
kind: "bar",
|
|
1299
|
+
markers: ","
|
|
1300
|
+
});
|
|
1301
|
+
continue;
|
|
1302
|
+
}
|
|
1303
|
+
if (",.:;".includes(ch)) {
|
|
1304
|
+
let run = "";
|
|
1305
|
+
while (i < core.length && ",.:;".includes(core[i])) run += core[i++];
|
|
1306
|
+
i--;
|
|
1307
|
+
els.push({
|
|
1308
|
+
kind: "bar",
|
|
1309
|
+
markers: run
|
|
1310
|
+
});
|
|
1311
|
+
continue;
|
|
1312
|
+
}
|
|
1313
|
+
const staff = posOf(ch);
|
|
1314
|
+
if (staff >= 0) {
|
|
1315
|
+
add({
|
|
1316
|
+
pos: staff,
|
|
1317
|
+
shape: "punctum"
|
|
1318
|
+
});
|
|
1319
|
+
continue;
|
|
1320
|
+
}
|
|
1321
|
+
const q = QUILISMAS.indexOf(ch);
|
|
1322
|
+
if (q >= 0) {
|
|
1323
|
+
add({
|
|
1324
|
+
pos: q + 1,
|
|
1325
|
+
shape: "quilisma"
|
|
1326
|
+
});
|
|
1327
|
+
continue;
|
|
1328
|
+
}
|
|
1329
|
+
const b = baseOf(ch);
|
|
1330
|
+
if (b) {
|
|
1331
|
+
const pos = posOf(b.base);
|
|
1332
|
+
switch (b.role) {
|
|
1333
|
+
case "virga":
|
|
1334
|
+
add({
|
|
1335
|
+
pos,
|
|
1336
|
+
shape: "virga"
|
|
1337
|
+
});
|
|
1338
|
+
continue;
|
|
1339
|
+
case "longa":
|
|
1340
|
+
add({
|
|
1341
|
+
pos,
|
|
1342
|
+
shape: "tenor"
|
|
1343
|
+
});
|
|
1344
|
+
continue;
|
|
1345
|
+
case "smallHead":
|
|
1346
|
+
add({
|
|
1347
|
+
pos,
|
|
1348
|
+
shape: "punctum",
|
|
1349
|
+
small: true
|
|
1350
|
+
});
|
|
1351
|
+
continue;
|
|
1352
|
+
case "smallPoint":
|
|
1353
|
+
if (last) last.mora = true;
|
|
1354
|
+
else loss("a dot with no note before it");
|
|
1355
|
+
continue;
|
|
1356
|
+
case "cephalicus":
|
|
1357
|
+
if (last && els.at(-1)?.kind === "note") {
|
|
1358
|
+
if (last.pos !== pos) warn("A cephalicus at another pitch than its note: written as that note’s plica");
|
|
1359
|
+
last.plica = true;
|
|
1360
|
+
} else if (d.loneCeph === "plica") {
|
|
1361
|
+
add({
|
|
1362
|
+
pos,
|
|
1363
|
+
shape: "punctum",
|
|
1364
|
+
plica: true
|
|
1365
|
+
});
|
|
1366
|
+
warn("A cephalicus on its own: written as a note with a plica (Aretino draws the head too)");
|
|
1367
|
+
} else loss("a cephalicus on its own");
|
|
1368
|
+
continue;
|
|
1369
|
+
case "modifyUp":
|
|
1370
|
+
case "modifyDown":
|
|
1371
|
+
case "modifyNatural":
|
|
1372
|
+
pendingAcc = accOf(ch);
|
|
1373
|
+
continue;
|
|
1374
|
+
default:
|
|
1375
|
+
connected = true;
|
|
1376
|
+
continue;
|
|
1377
|
+
}
|
|
1378
|
+
}
|
|
1379
|
+
loss(`"${ch}" inside a note group`);
|
|
1380
|
+
}
|
|
1381
|
+
if (pendingAcc) loss("an accidental with no note after it");
|
|
1382
|
+
return {
|
|
1383
|
+
els,
|
|
1384
|
+
lead: leading.replace(/-/g, ""),
|
|
1385
|
+
trail: trailing.replace(/-/g, "")
|
|
1386
|
+
};
|
|
1387
|
+
};
|
|
1388
|
+
/** The virgas the forward converter adds by itself, per part: Aretino's peaks
|
|
1389
|
+
* (translator.ts autoVirgaSplit) and the notes a leap up joins with their stem (upLeap) */
|
|
1390
|
+
const leapMin = forward.upLeap === "third" ? 2 : forward.upLeap === "fourth" ? 3 : Infinity;
|
|
1391
|
+
const autoVirga = (notes) => {
|
|
1392
|
+
const out = [];
|
|
1393
|
+
const peaks = (part) => {
|
|
1394
|
+
const pos = part.map((n) => n.pos);
|
|
1395
|
+
return (part.length < 2 || Math.max(...pos) <= Math.min(...pos) || forward.virga !== "auto" ? part.map(() => false) : part.map((_, i) => (i === 0 || pos[i] >= pos[i - 1]) && (i === part.length - 1 || pos[i] > pos[i + 1]))).map((p, i) => p || i > 0 && pos[i] - pos[i - 1] >= leapMin);
|
|
1396
|
+
};
|
|
1397
|
+
let start = 0;
|
|
1398
|
+
for (let i = 0; i < notes.length; i++) {
|
|
1399
|
+
const lastPairBothMora = i === notes.length - 2 && notes[i + 1].mora;
|
|
1400
|
+
if (i === notes.length - 1 || notes[i].mora && !lastPairBothMora) {
|
|
1401
|
+
out.push(...peaks(notes.slice(start, i + 1)));
|
|
1402
|
+
start = i + 1;
|
|
1403
|
+
}
|
|
1404
|
+
}
|
|
1405
|
+
return out;
|
|
1406
|
+
};
|
|
1407
|
+
const noteText = (n, auto) => {
|
|
1408
|
+
let s = n.acc ? n.acc.cautionary ? `[(${n.acc.letter}${n.acc.sym})] ` : `(${n.acc.letter}${n.acc.sym})` : "";
|
|
1409
|
+
s += letterAt(n.pos);
|
|
1410
|
+
if (n.shape === "tenor") s += "t";
|
|
1411
|
+
else if (n.shape === "quilisma") s += "w";
|
|
1412
|
+
if (d.virga !== "normalize" && n.shape !== "tenor" && n.shape !== "quilisma" && !n.small) {
|
|
1413
|
+
if (n.shape === "virga" && !auto) s += "'";
|
|
1414
|
+
if (n.shape !== "virga" && auto) s += "`";
|
|
1415
|
+
}
|
|
1416
|
+
if (n.small) s += "s";
|
|
1417
|
+
if (n.mora) s += ".";
|
|
1418
|
+
if (n.episema) s += "_";
|
|
1419
|
+
if (n.plica) s += "~";
|
|
1420
|
+
return s;
|
|
1421
|
+
};
|
|
1422
|
+
/** Elements → neume tokens (a dash splits into its own neume under `dashInGroup: hold`) */
|
|
1423
|
+
const neumesOf = (els) => {
|
|
1424
|
+
const pieces = [[]];
|
|
1425
|
+
const barsAfter = [""];
|
|
1426
|
+
for (const el of els) if (el.kind === "bar") {
|
|
1427
|
+
barsAfter[barsAfter.length - 1] += el.markers;
|
|
1428
|
+
pieces.push([]);
|
|
1429
|
+
barsAfter.push("");
|
|
1430
|
+
} else if (el.kind === "gap" && el.dash && d.dashInGroup === "hold" || el.kind === "split") {
|
|
1431
|
+
pieces.push([]);
|
|
1432
|
+
barsAfter.push("");
|
|
1433
|
+
} else pieces.at(-1).push(el);
|
|
1434
|
+
return pieces.map((p, i) => ({
|
|
1435
|
+
p,
|
|
1436
|
+
bars: barsAfter[i]
|
|
1437
|
+
})).filter(({ p }) => p.some((e) => e.kind === "note")).map(({ p: all, bars }) => {
|
|
1438
|
+
const first = all.findIndex((e) => e.kind !== "gap");
|
|
1439
|
+
let lastAt = all.length - 1;
|
|
1440
|
+
while (all[lastAt]?.kind === "gap") lastAt--;
|
|
1441
|
+
const piece = all.slice(first, lastAt + 1);
|
|
1442
|
+
for (let guard = 0; d.virga === "cut" && guard < 32; guard++) {
|
|
1443
|
+
const notesNow = piece.filter((e) => e.kind === "note");
|
|
1444
|
+
const partsNow = [[]];
|
|
1445
|
+
for (const el of piece) if (el.kind === "gap") partsNow.push([]);
|
|
1446
|
+
else if (el.kind === "note") partsNow.at(-1).push(el.note);
|
|
1447
|
+
const autosNow = partsNow.flatMap(autoVirga);
|
|
1448
|
+
const at = notesNow.findIndex(({ note }, i) => {
|
|
1449
|
+
if (note.shape !== "virga" || autosNow[i]) return false;
|
|
1450
|
+
const next = piece[piece.indexOf(notesNow[i]) + 1];
|
|
1451
|
+
if (!next || next.kind === "gap" || note.mora) return false;
|
|
1452
|
+
const prev = piece[piece.indexOf(notesNow[i]) - 1];
|
|
1453
|
+
return !prev || prev.kind !== "note" || prev.note.pos <= note.pos;
|
|
1454
|
+
});
|
|
1455
|
+
if (at < 0) break;
|
|
1456
|
+
piece.splice(piece.indexOf(notesNow[at]) + 1, 0, {
|
|
1457
|
+
kind: "gap",
|
|
1458
|
+
n: 1,
|
|
1459
|
+
dash: false,
|
|
1460
|
+
units: 1
|
|
1461
|
+
});
|
|
1462
|
+
}
|
|
1463
|
+
const parts = [[]];
|
|
1464
|
+
for (const el of piece) if (el.kind === "gap") parts.push([]);
|
|
1465
|
+
else if (el.kind === "note") parts.at(-1).push(el.note);
|
|
1466
|
+
const autos = parts.flatMap(autoVirga);
|
|
1467
|
+
let k = 0;
|
|
1468
|
+
let text = "";
|
|
1469
|
+
let pre = "";
|
|
1470
|
+
let post = "";
|
|
1471
|
+
for (const el of piece) if (el.kind === "note") text += noteText(el.note, autos[k++]);
|
|
1472
|
+
else if (el.kind === "gap") text += "/".repeat(d.dashInGroup === "///" ? el.units : el.dash ? d.dashInGroup === "//" ? 2 : 1 : el.n);
|
|
1473
|
+
else if (el.kind === "open") {
|
|
1474
|
+
if (text) loss("a bracket inside a neume");
|
|
1475
|
+
else pre = "[";
|
|
1476
|
+
} else post = "]";
|
|
1477
|
+
const notes = piece.filter((e) => e.kind === "note");
|
|
1478
|
+
const lone = notes.length === 1 && notes[0].note.shape === "tenor" ? notes[0].note : void 0;
|
|
1479
|
+
const barText = barsOf(bars).join(" ");
|
|
1480
|
+
return {
|
|
1481
|
+
text: `${pre}${pre ? " " : ""}${text}${post ? " " : ""}${post}${barText ? " " + barText : ""}`,
|
|
1482
|
+
slots: 1,
|
|
1483
|
+
...lone ? {
|
|
1484
|
+
tenorPos: lone.pos,
|
|
1485
|
+
tenorAcc: lone.acc ? lone.acc.letter + lone.acc.sym : ""
|
|
1486
|
+
} : {}
|
|
1487
|
+
};
|
|
1488
|
+
});
|
|
1489
|
+
};
|
|
1490
|
+
const barsOf = (markers) => {
|
|
1491
|
+
const out = [];
|
|
1492
|
+
for (let i = 0; i < markers.length; i++) {
|
|
1493
|
+
const ch = markers[i];
|
|
1494
|
+
if (ch === "," && markers[i + 1] === ",") {
|
|
1495
|
+
out.push("||");
|
|
1496
|
+
i++;
|
|
1497
|
+
continue;
|
|
1498
|
+
}
|
|
1499
|
+
if (ch === ",") {
|
|
1500
|
+
out.push("|");
|
|
1501
|
+
continue;
|
|
1502
|
+
}
|
|
1503
|
+
if (ch === ".") {
|
|
1504
|
+
out.push("|||");
|
|
1505
|
+
continue;
|
|
1506
|
+
}
|
|
1507
|
+
if (ch === ":") {
|
|
1508
|
+
out.push(d.quarterBar);
|
|
1509
|
+
continue;
|
|
1510
|
+
}
|
|
1511
|
+
if (ch === ";") {
|
|
1512
|
+
out.push(";");
|
|
1513
|
+
continue;
|
|
1514
|
+
}
|
|
1515
|
+
if (ch === "?") {
|
|
1516
|
+
loss("the ? mark (written as a full bar)");
|
|
1517
|
+
out.push("|");
|
|
1518
|
+
continue;
|
|
1519
|
+
}
|
|
1520
|
+
if (ch === "'" || ch === "/" || baseOf(ch)?.role === "cephalicus") {
|
|
1521
|
+
if (ch !== "/") loss("the breath mark’s height");
|
|
1522
|
+
out.push(d.cephMark === "'" ? "'" : "~");
|
|
1523
|
+
continue;
|
|
1524
|
+
}
|
|
1525
|
+
loss(`marker "${ch}"`);
|
|
1526
|
+
}
|
|
1527
|
+
return out;
|
|
1528
|
+
};
|
|
1529
|
+
const sections = [{
|
|
1530
|
+
items: [],
|
|
1531
|
+
from: 0,
|
|
1532
|
+
to: tokens.length
|
|
1533
|
+
}];
|
|
1534
|
+
/** Per token: its neume count (slots), and whether it merged into the run before */
|
|
1535
|
+
const slotsOf = /* @__PURE__ */ new Map();
|
|
1536
|
+
const mergedInto = /* @__PURE__ */ new Map();
|
|
1537
|
+
/** Bars with a label: token → label text (stanza 1) */
|
|
1538
|
+
const barLabels = /* @__PURE__ */ new Map();
|
|
1539
|
+
/** Syllables over marks with no Aretino bar: written as noteless syllables */
|
|
1540
|
+
const notelessAt = /* @__PURE__ */ new Set();
|
|
1541
|
+
let key = opening.slice(1);
|
|
1542
|
+
const head = [`(${clefName(opening[0] ?? "M")})`];
|
|
1543
|
+
const openingKey = keyDirective(key);
|
|
1544
|
+
if (openingKey) head.push(openingKey);
|
|
1545
|
+
const items = () => sections.at(-1).items;
|
|
1546
|
+
let lastTenor = null;
|
|
1547
|
+
for (const [t, tok] of tokens.entries()) {
|
|
1548
|
+
if (tok.kind !== "syllable") {
|
|
1549
|
+
lastTenor = null;
|
|
1550
|
+
if (tok.kind === "break") {
|
|
1551
|
+
if (d.breaks === "line") {
|
|
1552
|
+
sections.at(-1).to = t;
|
|
1553
|
+
sections.push({
|
|
1554
|
+
items: [],
|
|
1555
|
+
from: t + 1,
|
|
1556
|
+
to: tokens.length
|
|
1557
|
+
});
|
|
1558
|
+
} else items().push({
|
|
1559
|
+
kind: "raw",
|
|
1560
|
+
text: d.breaks === "Z" ? "(Z)" : "(z)"
|
|
1561
|
+
});
|
|
1562
|
+
} else if (tok.kind === "clef") {
|
|
1563
|
+
items().push({
|
|
1564
|
+
kind: "raw",
|
|
1565
|
+
text: `(${clefName(tok.clef)})`
|
|
1566
|
+
});
|
|
1567
|
+
if (tok.key !== key) items().push({
|
|
1568
|
+
kind: "raw",
|
|
1569
|
+
text: keyDirective(tok.key) || "(K)"
|
|
1570
|
+
});
|
|
1571
|
+
key = tok.key;
|
|
1572
|
+
} else if (tok.kind === "key") {
|
|
1573
|
+
items().push({
|
|
1574
|
+
kind: "raw",
|
|
1575
|
+
text: keyDirective(tok.key) || "(K)"
|
|
1576
|
+
});
|
|
1577
|
+
key = tok.key;
|
|
1578
|
+
} else loss(`(@${tok.kind}) run`);
|
|
1579
|
+
continue;
|
|
1580
|
+
}
|
|
1581
|
+
const notes = tok.notes.trim();
|
|
1582
|
+
if (!notes) {
|
|
1583
|
+
const before = items().at(-1);
|
|
1584
|
+
const barBefore = before?.kind === "raw" ? before.text : before?.kind === "neume" ? before.neume.text.split(" ").at(-1) : void 0;
|
|
1585
|
+
if (tok.source.trim() === "**" && barBefore !== void 0 && [
|
|
1586
|
+
",",
|
|
1587
|
+
",2",
|
|
1588
|
+
";",
|
|
1589
|
+
"|",
|
|
1590
|
+
"||",
|
|
1591
|
+
"|||",
|
|
1592
|
+
"'"
|
|
1593
|
+
].includes(barBefore)) barLabels.set(t, tok.source);
|
|
1594
|
+
lastTenor = null;
|
|
1595
|
+
continue;
|
|
1596
|
+
}
|
|
1597
|
+
const { els, lead, trail } = parseGroup(notes);
|
|
1598
|
+
const neumes = neumesOf(els);
|
|
1599
|
+
if (neumes.length === 0) {
|
|
1600
|
+
const bars = barsOf(lead + trail);
|
|
1601
|
+
for (const bar of bars) items().push({
|
|
1602
|
+
kind: "raw",
|
|
1603
|
+
text: bar
|
|
1604
|
+
});
|
|
1605
|
+
if ((tok.text || tok.runs.length) && bars.length) barLabels.set(t, tok.source);
|
|
1606
|
+
else if (tok.runs.length) notelessAt.add(t);
|
|
1607
|
+
lastTenor = null;
|
|
1608
|
+
continue;
|
|
1609
|
+
}
|
|
1610
|
+
for (const bar of barsOf(lead)) items().push({
|
|
1611
|
+
kind: "raw",
|
|
1612
|
+
text: bar
|
|
1613
|
+
});
|
|
1614
|
+
if (tok.held && d.held === "merge") {
|
|
1615
|
+
const prev = items().findLast((it) => it.kind === "neume");
|
|
1616
|
+
if (prev) {
|
|
1617
|
+
prev.neume.text += "/" + neumes.map((n) => n.text).join("/");
|
|
1618
|
+
mergedInto.set(t, prev.token);
|
|
1619
|
+
slotsOf.set(t, 0);
|
|
1620
|
+
for (const bar of barsOf(trail)) items().push({
|
|
1621
|
+
kind: "raw",
|
|
1622
|
+
text: bar
|
|
1623
|
+
});
|
|
1624
|
+
continue;
|
|
1625
|
+
}
|
|
1626
|
+
}
|
|
1627
|
+
const lone = neumes.length === 1 ? neumes[0] : void 0;
|
|
1628
|
+
if (d.tenorRun !== "each" && lone?.tenorPos !== void 0 && lastTenor && lastTenor.pos === lone.tenorPos && lastTenor.acc === lone.tenorAcc && !tok.held && !tok.empty) {
|
|
1629
|
+
mergedInto.set(t, lastTenor.token);
|
|
1630
|
+
slotsOf.set(t, 0);
|
|
1631
|
+
for (const bar of barsOf(trail)) {
|
|
1632
|
+
items().push({
|
|
1633
|
+
kind: "raw",
|
|
1634
|
+
text: bar
|
|
1635
|
+
});
|
|
1636
|
+
lastTenor = null;
|
|
1637
|
+
}
|
|
1638
|
+
continue;
|
|
1639
|
+
}
|
|
1640
|
+
for (const n of neumes) items().push({
|
|
1641
|
+
kind: "neume",
|
|
1642
|
+
neume: n,
|
|
1643
|
+
token: t
|
|
1644
|
+
});
|
|
1645
|
+
slotsOf.set(t, neumes.length);
|
|
1646
|
+
lastTenor = lone?.tenorPos !== void 0 ? {
|
|
1647
|
+
pos: lone.tenorPos,
|
|
1648
|
+
acc: lone.tenorAcc ?? "",
|
|
1649
|
+
token: t
|
|
1650
|
+
} : null;
|
|
1651
|
+
const bars = barsOf(trail);
|
|
1652
|
+
for (const bar of bars) items().push({
|
|
1653
|
+
kind: "raw",
|
|
1654
|
+
text: bar
|
|
1655
|
+
});
|
|
1656
|
+
if (bars.length) lastTenor = null;
|
|
1657
|
+
}
|
|
1658
|
+
const stanzas = stanzaCount(gs);
|
|
1659
|
+
const stanzaTokens = [tokens];
|
|
1660
|
+
for (let k = 2; k <= stanzas; k++) stanzaTokens.push(withStanza(script, k).tokens);
|
|
1661
|
+
if (stanzas > 1 && tokens.some((t) => t.kind === "syllable" && !t.notes.trim() && t.runs.length)) warn("A noteless syllable is written in the first stanza’s line only");
|
|
1662
|
+
const lyricLine = (toks, from, to, first) => {
|
|
1663
|
+
const units = [];
|
|
1664
|
+
let prefix = "";
|
|
1665
|
+
let current = null;
|
|
1666
|
+
for (let t = from; t < to; t++) {
|
|
1667
|
+
const tok = toks[t];
|
|
1668
|
+
if (!tok || tok.kind !== "syllable") continue;
|
|
1669
|
+
if (barLabels.has(t)) {
|
|
1670
|
+
if (first) units.push({ label: `(${lyricText(tok, d, loss, true)})` });
|
|
1671
|
+
continue;
|
|
1672
|
+
}
|
|
1673
|
+
if (!tok.notes.trim() || notelessAt.has(t)) {
|
|
1674
|
+
if (tok.runs.length && first) {
|
|
1675
|
+
if (d.noteless === "prefix") prefix += (prefix ? "~" : "") + lyricText(tok, d, loss);
|
|
1676
|
+
else loss("a noteless syllable");
|
|
1677
|
+
}
|
|
1678
|
+
continue;
|
|
1679
|
+
}
|
|
1680
|
+
const slots = slotsOf.get(t) ?? 0;
|
|
1681
|
+
if (mergedInto.get(t) !== void 0) {
|
|
1682
|
+
if (current && !tok.held && !tok.empty && tok.text) {
|
|
1683
|
+
current.text += (current.wordEnd ? "~" : d.tenorRun === "shy" ? "" : "") + lyricText(tok, d, loss);
|
|
1684
|
+
current.wordEnd = tok.wordEnd;
|
|
1685
|
+
}
|
|
1686
|
+
continue;
|
|
1687
|
+
}
|
|
1688
|
+
if (slots === 0) continue;
|
|
1689
|
+
if (tok.held) {
|
|
1690
|
+
if (current) current.holds += slots;
|
|
1691
|
+
else units.push(current = {
|
|
1692
|
+
text: "~",
|
|
1693
|
+
wordEnd: true,
|
|
1694
|
+
holds: slots - 1,
|
|
1695
|
+
prefix: ""
|
|
1696
|
+
});
|
|
1697
|
+
continue;
|
|
1698
|
+
}
|
|
1699
|
+
if (tok.empty || !tok.text) {
|
|
1700
|
+
for (let s = 0; s < slots; s++) units.push({
|
|
1701
|
+
text: "~",
|
|
1702
|
+
wordEnd: true,
|
|
1703
|
+
holds: 0,
|
|
1704
|
+
prefix: ""
|
|
1705
|
+
});
|
|
1706
|
+
current = null;
|
|
1707
|
+
continue;
|
|
1708
|
+
}
|
|
1709
|
+
current = {
|
|
1710
|
+
text: lyricText(tok, d, loss),
|
|
1711
|
+
wordEnd: tok.wordEnd,
|
|
1712
|
+
holds: slots - 1,
|
|
1713
|
+
prefix: prefix ? `${prefix}~~` : ""
|
|
1714
|
+
};
|
|
1715
|
+
prefix = "";
|
|
1716
|
+
units.push(current);
|
|
1717
|
+
}
|
|
1718
|
+
if (prefix) loss("a noteless syllable at the end of a line");
|
|
1719
|
+
let out = "";
|
|
1720
|
+
for (const u of units) {
|
|
1721
|
+
if ("label" in u) {
|
|
1722
|
+
out += (out ? " " : "") + u.label;
|
|
1723
|
+
continue;
|
|
1724
|
+
}
|
|
1725
|
+
let piece = u.prefix + u.text;
|
|
1726
|
+
if (u.holds > 0 && u.wordEnd) {
|
|
1727
|
+
const m = d.extenderPunct === "after" ? piece.match(/^(.*?)([.,;:!?]+)$/) : null;
|
|
1728
|
+
piece = m ? `${m[1]}${"_".repeat(u.holds + 1)}${m[2]}` : `${piece}${"_".repeat(u.holds + 1)}`;
|
|
1729
|
+
} else if (u.holds > 0) piece += "-" + " -".repeat(u.holds);
|
|
1730
|
+
else if (!u.wordEnd) piece += "-";
|
|
1731
|
+
const joinTight = out.endsWith("-") && !out.endsWith(" -");
|
|
1732
|
+
out += out && !joinTight ? " " + piece : piece;
|
|
1733
|
+
}
|
|
1734
|
+
return out;
|
|
1735
|
+
};
|
|
1736
|
+
const lines = [];
|
|
1737
|
+
for (const [s, sec] of sections.entries()) {
|
|
1738
|
+
const music = sec.items.map((it) => it.kind === "neume" ? it.neume.text : it.text).filter(Boolean);
|
|
1739
|
+
if (s === 0) music.unshift(...head);
|
|
1740
|
+
if (!music.length) continue;
|
|
1741
|
+
if (lines.length) lines.push("");
|
|
1742
|
+
lines.push(music.join(" "));
|
|
1743
|
+
for (const [k, toks] of stanzaTokens.entries()) {
|
|
1744
|
+
const text = lyricLine(toks, sec.from, sec.to, k === 0);
|
|
1745
|
+
if (text || k === 0) lines.push(`w: ${text}`.trimEnd());
|
|
1746
|
+
}
|
|
1747
|
+
}
|
|
1748
|
+
return {
|
|
1749
|
+
aretino: lines.join("\n"),
|
|
1750
|
+
warnings,
|
|
1751
|
+
lost
|
|
1752
|
+
};
|
|
1753
|
+
} catch (e) {
|
|
1754
|
+
return {
|
|
1755
|
+
aretino: "",
|
|
1756
|
+
warnings,
|
|
1757
|
+
lost,
|
|
1758
|
+
error: e instanceof Error ? e.message : String(e)
|
|
1759
|
+
};
|
|
1760
|
+
}
|
|
1761
|
+
}
|
|
1762
|
+
var CHAR_MAP = {
|
|
1763
|
+
"†": "+",
|
|
1764
|
+
"‡": "++",
|
|
1765
|
+
"∗": "*",
|
|
1766
|
+
"♭": "\\b",
|
|
1767
|
+
"♮": "\\n",
|
|
1768
|
+
"♯": "\\#",
|
|
1769
|
+
"́": "\\'",
|
|
1770
|
+
"℟": "\\R",
|
|
1771
|
+
"℣": "\\V"
|
|
1772
|
+
};
|
|
1773
|
+
function escapeLyric(text) {
|
|
1774
|
+
let out = "";
|
|
1775
|
+
for (const ch of text) if (CHAR_MAP[ch]) out += CHAR_MAP[ch];
|
|
1776
|
+
else if ("\\-_{}<>[]()=+~".includes(ch)) out += `\\${ch}`;
|
|
1777
|
+
else if (ch === " " || ch === "\xA0") out += "~";
|
|
1778
|
+
else out += ch;
|
|
1779
|
+
return out;
|
|
1780
|
+
}
|
|
1781
|
+
/** A syllable's runs → Aretino lyric text with its formatting */
|
|
1782
|
+
function lyricText(tok, d, loss, isLabel = false) {
|
|
1783
|
+
let out = "";
|
|
1784
|
+
for (const run of tok.runs) {
|
|
1785
|
+
let open = "";
|
|
1786
|
+
let close = "";
|
|
1787
|
+
let text = escapeLyric(run.text);
|
|
1788
|
+
for (const mark of run.marks) {
|
|
1789
|
+
const [name, arg] = mark.includes(":") ? [mark.slice(0, mark.indexOf(":")), mark.slice(mark.indexOf(":") + 1)] : [mark, ""];
|
|
1790
|
+
let pair = null;
|
|
1791
|
+
if (name === "red") pair = ["\\red{", "}"];
|
|
1792
|
+
else if (name === "b") pair = ["{", "}"];
|
|
1793
|
+
else if (name === "i") pair = ["<", ">"];
|
|
1794
|
+
else if (name === "u") pair = ["[", "]"];
|
|
1795
|
+
else if (name === "sc") pair = ["\\sc{", "}"];
|
|
1796
|
+
else if (name === "color") pair = [`\\color:${arg}{`, "}"];
|
|
1797
|
+
else if (name === "size" && Number(arg) < 1) pair = ["\\small{", "}"];
|
|
1798
|
+
else if (name === "size" && Number(arg) > 1) pair = ["\\large{", "}"];
|
|
1799
|
+
else if (name === "rubric" && isLabel) {} else if (name === "rubric") pair = d.rubric === "red" ? ["\\red{", "}"] : d.rubric === "redsc" ? ["\\red{\\sc{", "}}"] : d.rubric === "color" ? ["\\color:rubric{", "}"] : null;
|
|
1800
|
+
else if (name === "liturgy") {
|
|
1801
|
+
if (/^[RV]\.?$/.test(run.text.trim())) text = `\\${run.text.trim()[0]}`;
|
|
1802
|
+
else loss("Liturgy font text");
|
|
1803
|
+
} else if (name === "initial") {
|
|
1804
|
+
pair = d.initial === "redlarge" ? ["\\red{\\large{", "}}"] : d.initial === "color" ? ["\\color:initial{", "}"] : null;
|
|
1805
|
+
if (!pair) loss("the initial (*…*)");
|
|
1806
|
+
} else if ([
|
|
1807
|
+
"marker",
|
|
1808
|
+
"plus",
|
|
1809
|
+
"star",
|
|
1810
|
+
"plain"
|
|
1811
|
+
].includes(name)) {} else loss(`the mark "${name}"`);
|
|
1812
|
+
if (pair) {
|
|
1813
|
+
open += pair[0];
|
|
1814
|
+
close = pair[1] + close;
|
|
1815
|
+
}
|
|
1816
|
+
}
|
|
1817
|
+
out += open + text + close;
|
|
1818
|
+
}
|
|
1819
|
+
return out;
|
|
1820
|
+
}
|
|
1821
|
+
//#endregion
|
|
1822
|
+
//#region src/aretino/index.ts
|
|
1823
|
+
/**
|
|
1824
|
+
* The `guidex/aretino` entry (issue #79): conversion between Aretino chant
|
|
1825
|
+
* notation and GuidoScript, both ways, with the decided choices.
|
|
1826
|
+
*
|
|
1827
|
+
* - `aretinoToGuido(source)` reads an Aretino source into a GuidoScript.
|
|
1828
|
+
* - `guidoToAretino(script)` writes a GuidoScript as Aretino.
|
|
1829
|
+
*
|
|
1830
|
+
* Aretino spells neumes the way guidex calls ideal, so a GuidoScript that
|
|
1831
|
+
* goes to Aretino and back comes back in that ideal form (connectors and
|
|
1832
|
+
* virgas re-derived, the source's spacing normalised); converting it again
|
|
1833
|
+
* changes nothing. What the other side cannot say is reported in `lost`,
|
|
1834
|
+
* what it approximates in `warnings`.
|
|
1835
|
+
*
|
|
1836
|
+
* Needs the optional peer dependency `@aretino-chant/core` (Aretino's own
|
|
1837
|
+
* parser) for `aretinoToGuido`.
|
|
1838
|
+
*/
|
|
1839
|
+
/**
|
|
1840
|
+
* The Aretino reader's own version: bumped by hand whenever aretinoToGuido
|
|
1841
|
+
* gives different GuidoScript for some source (a decision, a fix, a new
|
|
1842
|
+
* rule), and only then. A chant saved as Aretino is converted again on every
|
|
1843
|
+
* load, so the editor stores this with it and says when today's reader is
|
|
1844
|
+
* another. tests/aretino.test.ts fails when the output changes without a bump.
|
|
1845
|
+
*/
|
|
1846
|
+
var ARETINO_CONVERTER_VERSION = 3;
|
|
1847
|
+
/** Reads an Aretino source into a GuidoScript. */
|
|
1848
|
+
function aretinoToGuido(source) {
|
|
1849
|
+
const t = translate(source, DEFAULTS);
|
|
1850
|
+
return {
|
|
1851
|
+
script: t.script,
|
|
1852
|
+
stanzas: t.stanzas,
|
|
1853
|
+
warnings: [...t.warnings, ...t.stanzaWarnings],
|
|
1854
|
+
lost: t.lost,
|
|
1855
|
+
metadata: t.metadata,
|
|
1856
|
+
...t.error ? { error: t.error } : {}
|
|
1857
|
+
};
|
|
1858
|
+
}
|
|
1859
|
+
/** Writes a GuidoScript as Aretino. */
|
|
1860
|
+
function guidoToAretino(script) {
|
|
1861
|
+
const r = reverse(script, REVERSE_DEFAULTS, DEFAULTS);
|
|
1862
|
+
return {
|
|
1863
|
+
aretino: r.aretino,
|
|
1864
|
+
warnings: r.warnings,
|
|
1865
|
+
lost: r.lost,
|
|
1866
|
+
...r.error ? { error: r.error } : {}
|
|
1867
|
+
};
|
|
1868
|
+
}
|
|
1869
|
+
//#endregion
|
|
1870
|
+
export { ARETINO_CONVERTER_VERSION, aretinoToGuido, guidoToAretino };
|