guidex 0.3.1 → 0.3.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +81 -4
- package/dist/lib/app/EditorContext.d.ts +6 -1
- package/dist/lib/app/ExactTransposeControls.d.ts +1 -0
- package/dist/lib/app/StanzaControls.d.ts +1 -0
- package/dist/lib/app/TransposeSettings.d.ts +1 -0
- package/dist/lib/app/editorState.d.ts +81 -6
- package/dist/lib/app/shareLink.d.ts +22 -0
- package/dist/lib/chantLayout-CVVuJUJl.js +2778 -0
- package/dist/lib/chantLayout.d.ts +48 -8
- package/dist/lib/composeScript.d.ts +71 -0
- package/dist/lib/editorStorage.d.ts +11 -0
- package/dist/lib/embed/core.d.ts +34 -3
- package/dist/lib/embed/index.js +1 -1
- package/dist/lib/embed/manual.d.ts +12 -3
- package/dist/lib/embed/manual.js +348 -276
- package/dist/lib/exactTranspose.d.ts +54 -0
- package/dist/lib/glyphFamilies.d.ts +29 -0
- package/dist/lib/guidex.css +29 -2
- package/dist/lib/guidoRender.d.ts +7 -3
- package/dist/lib/guidoScript.d.ts +109 -10
- package/dist/lib/guidoScriptTransforms.d.ts +19 -7
- package/dist/lib/noteGroups.d.ts +22 -0
- package/dist/lib/react/GuidexScore.d.ts +21 -1
- package/dist/lib/react/index.d.ts +2 -1
- package/dist/lib/react/index.js +25 -3
- package/dist/lib/{scoreImage-oVBasBX5.js → scoreImage-lvOrTbG-.js} +111 -53
- package/dist/lib/stanzas.d.ts +135 -0
- package/dist/lib/word/index.js +1 -1
- package/dist/lib/{wordChecker-C3nJrk_j.js → wordChecker-DG09l_ol.js} +99 -46
- package/dist/lib/wordClipboard.d.ts +3 -3
- package/dist/lib/wordExport.d.ts +11 -5
- package/dist-embed/guidex-embed.min.css +1 -1
- package/dist-embed/guidex-embed.min.js +1 -1
- package/package.json +1 -1
- package/dist/lib/chantLayout-GAfg2iKS.js +0 -1042
|
@@ -0,0 +1,2778 @@
|
|
|
1
|
+
//#region src/glyphFamilies.ts
|
|
2
|
+
/** Base pitch glyph → its variants. Keys are the note glyphs of the
|
|
3
|
+
* first column of `PITCH_ROWS`' notation table. */
|
|
4
|
+
var FAMILIES = {
|
|
5
|
+
ð: {
|
|
6
|
+
smallHead: "~",
|
|
7
|
+
modifyUp: "È",
|
|
8
|
+
cephalicus: "®"
|
|
9
|
+
},
|
|
10
|
+
ñ: {
|
|
11
|
+
virga: "^",
|
|
12
|
+
smallHead: "¯",
|
|
13
|
+
modifyUp: "Ê",
|
|
14
|
+
modifyDown: "÷",
|
|
15
|
+
cephalicus: "−",
|
|
16
|
+
connectOneDown: "Ý"
|
|
17
|
+
},
|
|
18
|
+
"0": {
|
|
19
|
+
virga: "]",
|
|
20
|
+
smallHead: "‚",
|
|
21
|
+
modifyUp: "Ë",
|
|
22
|
+
modifyDown: "ø",
|
|
23
|
+
modifyNatural: "“",
|
|
24
|
+
cephalicus: "§",
|
|
25
|
+
connectOneDown: "Þ"
|
|
26
|
+
},
|
|
27
|
+
"1": {
|
|
28
|
+
virga: "q",
|
|
29
|
+
longa: "Ÿ",
|
|
30
|
+
smallHead: "ƒ",
|
|
31
|
+
modifyUp: "Ì",
|
|
32
|
+
modifyDown: "a",
|
|
33
|
+
modifyNatural: "”",
|
|
34
|
+
cephalicus: "ÿ",
|
|
35
|
+
connectOneDown: "Q",
|
|
36
|
+
connectTwoDown: "×",
|
|
37
|
+
connectThreeDown: "Ø",
|
|
38
|
+
smallPoint: "í"
|
|
39
|
+
},
|
|
40
|
+
"2": {
|
|
41
|
+
virga: "w",
|
|
42
|
+
longa: "¡",
|
|
43
|
+
smallHead: "„",
|
|
44
|
+
modifyUp: "Î",
|
|
45
|
+
modifyDown: "A",
|
|
46
|
+
modifyNatural: "•",
|
|
47
|
+
cephalicus: "\"",
|
|
48
|
+
connectOneDown: "W",
|
|
49
|
+
connectTwoDown: "s",
|
|
50
|
+
connectThreeDown: "Ù",
|
|
51
|
+
connectFourDown: "½"
|
|
52
|
+
},
|
|
53
|
+
"3": {
|
|
54
|
+
virga: "e",
|
|
55
|
+
longa: "¢",
|
|
56
|
+
smallHead: "…",
|
|
57
|
+
modifyUp: "Ï",
|
|
58
|
+
modifyDown: "S",
|
|
59
|
+
modifyNatural: "–",
|
|
60
|
+
cephalicus: "+",
|
|
61
|
+
connectOneDown: "E",
|
|
62
|
+
connectTwoDown: "d",
|
|
63
|
+
connectThreeDown: "D",
|
|
64
|
+
connectFourDown: "°",
|
|
65
|
+
smallPoint: "y"
|
|
66
|
+
},
|
|
67
|
+
"4": {
|
|
68
|
+
virga: "r",
|
|
69
|
+
longa: "£",
|
|
70
|
+
smallHead: "†",
|
|
71
|
+
modifyUp: "Ð",
|
|
72
|
+
modifyDown: "Í",
|
|
73
|
+
modifyNatural: "—",
|
|
74
|
+
cephalicus: "!",
|
|
75
|
+
connectOneDown: "R",
|
|
76
|
+
connectTwoDown: "f",
|
|
77
|
+
connectThreeDown: "F",
|
|
78
|
+
connectFourDown: "±"
|
|
79
|
+
},
|
|
80
|
+
"5": {
|
|
81
|
+
virga: "t",
|
|
82
|
+
longa: "[",
|
|
83
|
+
smallHead: "‡",
|
|
84
|
+
modifyUp: "Ñ",
|
|
85
|
+
modifyDown: "Y",
|
|
86
|
+
modifyNatural: "˜",
|
|
87
|
+
cephalicus: "%",
|
|
88
|
+
connectOneDown: "T",
|
|
89
|
+
connectTwoDown: "g",
|
|
90
|
+
connectThreeDown: "G",
|
|
91
|
+
connectFourDown: "²",
|
|
92
|
+
smallPoint: "x"
|
|
93
|
+
},
|
|
94
|
+
"6": {
|
|
95
|
+
virga: "z",
|
|
96
|
+
longa: "¥",
|
|
97
|
+
smallHead: "ˆ",
|
|
98
|
+
modifyUp: "Ò",
|
|
99
|
+
modifyDown: "X",
|
|
100
|
+
modifyNatural: "™",
|
|
101
|
+
cephalicus: "/",
|
|
102
|
+
connectOneDown: "Z",
|
|
103
|
+
connectTwoDown: "h",
|
|
104
|
+
connectThreeDown: "H",
|
|
105
|
+
connectFourDown: "³"
|
|
106
|
+
},
|
|
107
|
+
"7": {
|
|
108
|
+
virga: "u",
|
|
109
|
+
longa: "¦",
|
|
110
|
+
smallHead: "‰",
|
|
111
|
+
modifyUp: "þ",
|
|
112
|
+
modifyDown: "C",
|
|
113
|
+
modifyNatural: "š",
|
|
114
|
+
cephalicus: "=",
|
|
115
|
+
connectOneDown: "U",
|
|
116
|
+
connectTwoDown: "j",
|
|
117
|
+
connectThreeDown: "J",
|
|
118
|
+
connectFourDown: "´",
|
|
119
|
+
smallPoint: "c"
|
|
120
|
+
},
|
|
121
|
+
"8": {
|
|
122
|
+
virga: "i",
|
|
123
|
+
longa: "©",
|
|
124
|
+
smallHead: "Š",
|
|
125
|
+
modifyUp: "ß",
|
|
126
|
+
modifyDown: "V",
|
|
127
|
+
modifyNatural: "›",
|
|
128
|
+
cephalicus: "(",
|
|
129
|
+
connectOneDown: "I",
|
|
130
|
+
connectTwoDown: "k",
|
|
131
|
+
connectThreeDown: "K",
|
|
132
|
+
connectFourDown: "μ"
|
|
133
|
+
},
|
|
134
|
+
"9": {
|
|
135
|
+
virga: "o",
|
|
136
|
+
longa: "ª",
|
|
137
|
+
smallHead: "‹",
|
|
138
|
+
modifyUp: "ò",
|
|
139
|
+
modifyDown: "B",
|
|
140
|
+
modifyNatural: "œ",
|
|
141
|
+
cephalicus: ")",
|
|
142
|
+
connectOneDown: "O",
|
|
143
|
+
connectTwoDown: "l",
|
|
144
|
+
connectThreeDown: "L",
|
|
145
|
+
connectFourDown: "·",
|
|
146
|
+
smallPoint: "v"
|
|
147
|
+
},
|
|
148
|
+
ö: {
|
|
149
|
+
virga: "p",
|
|
150
|
+
longa: "«",
|
|
151
|
+
smallHead: "Œ",
|
|
152
|
+
modifyUp: "ô",
|
|
153
|
+
modifyDown: "N",
|
|
154
|
+
modifyNatural: "º",
|
|
155
|
+
cephalicus: "Ö",
|
|
156
|
+
connectOneDown: "P",
|
|
157
|
+
connectTwoDown: "é",
|
|
158
|
+
connectThreeDown: "É",
|
|
159
|
+
connectFourDown: "¾"
|
|
160
|
+
},
|
|
161
|
+
ü: {
|
|
162
|
+
virga: "õ",
|
|
163
|
+
longa: "¬",
|
|
164
|
+
smallHead: "‘",
|
|
165
|
+
modifyUp: "ù",
|
|
166
|
+
modifyDown: "$",
|
|
167
|
+
modifyNatural: "»",
|
|
168
|
+
cephalicus: "Ü",
|
|
169
|
+
connectOneDown: "Õ",
|
|
170
|
+
connectTwoDown: "á",
|
|
171
|
+
connectThreeDown: "Á",
|
|
172
|
+
connectFourDown: "¸",
|
|
173
|
+
smallPoint: "b"
|
|
174
|
+
},
|
|
175
|
+
ó: {
|
|
176
|
+
virga: "ú",
|
|
177
|
+
smallHead: "’",
|
|
178
|
+
modifyUp: "ý",
|
|
179
|
+
modifyDown: "m",
|
|
180
|
+
cephalicus: "Ó",
|
|
181
|
+
connectOneDown: "Ú",
|
|
182
|
+
connectTwoDown: "û",
|
|
183
|
+
connectThreeDown: "Û",
|
|
184
|
+
connectFourDown: "¹"
|
|
185
|
+
}
|
|
186
|
+
};
|
|
187
|
+
/** Base pitch glyph → its pitch name in the standard g-clef (the
|
|
188
|
+
* "pitch in standard g-clef" column of the notation table). The last
|
|
189
|
+
* row of that table has no base glyph, so it has no entry here. */
|
|
190
|
+
var G_CLEF_PITCHES = {
|
|
191
|
+
ð: "a",
|
|
192
|
+
ñ: "b",
|
|
193
|
+
"0": "c'",
|
|
194
|
+
"1": "d'",
|
|
195
|
+
"2": "e'",
|
|
196
|
+
"3": "f'",
|
|
197
|
+
"4": "g'",
|
|
198
|
+
"5": "a'",
|
|
199
|
+
"6": "b'",
|
|
200
|
+
"7": "c''",
|
|
201
|
+
"8": "d''",
|
|
202
|
+
"9": "e''",
|
|
203
|
+
ö: "f''",
|
|
204
|
+
ü: "g''",
|
|
205
|
+
ó: "a''"
|
|
206
|
+
};
|
|
207
|
+
/** The standard g-clef pitch name of a base glyph, or of the base of a
|
|
208
|
+
* variant glyph; undefined when the glyph has no pitch. */
|
|
209
|
+
function gClefPitchOf(ch) {
|
|
210
|
+
return G_CLEF_PITCHES[ch] ?? G_CLEF_PITCHES[baseOf(ch)?.base ?? ""];
|
|
211
|
+
}
|
|
212
|
+
/** Reverse lookup: variant glyph → its base glyph and role. Built once;
|
|
213
|
+
* the table has no glyph in two places, so each variant maps to one base. */
|
|
214
|
+
var BASES = /* @__PURE__ */ new Map();
|
|
215
|
+
for (const [base, family] of Object.entries(FAMILIES)) for (const role of Object.keys(family)) BASES.set(family[role], {
|
|
216
|
+
base,
|
|
217
|
+
role
|
|
218
|
+
});
|
|
219
|
+
/** The variant of `ch` in `role`, or undefined when `ch` has none. */
|
|
220
|
+
function variantOf(ch, role) {
|
|
221
|
+
return FAMILIES[ch]?.[role];
|
|
222
|
+
}
|
|
223
|
+
/** The virga (the stemmed form) of a pitch glyph. */
|
|
224
|
+
function toVirga(ch) {
|
|
225
|
+
return variantOf(ch, "virga");
|
|
226
|
+
}
|
|
227
|
+
/** The longa (the double-length form) of a pitch glyph. */
|
|
228
|
+
function toLonga(ch) {
|
|
229
|
+
return variantOf(ch, "longa");
|
|
230
|
+
}
|
|
231
|
+
/** The small head of a pitch glyph. */
|
|
232
|
+
function toSmallHead(ch) {
|
|
233
|
+
return variantOf(ch, "smallHead");
|
|
234
|
+
}
|
|
235
|
+
/** The cephalicus of a pitch glyph. */
|
|
236
|
+
function toCephalicus(ch) {
|
|
237
|
+
return variantOf(ch, "cephalicus");
|
|
238
|
+
}
|
|
239
|
+
/** The connecting form that joins a pitch to the note `steps` rows below
|
|
240
|
+
* it (1 to 4). */
|
|
241
|
+
function toConnectDown(ch, steps) {
|
|
242
|
+
const role = [
|
|
243
|
+
"connectOneDown",
|
|
244
|
+
"connectTwoDown",
|
|
245
|
+
"connectThreeDown",
|
|
246
|
+
"connectFourDown"
|
|
247
|
+
][steps - 1];
|
|
248
|
+
return variantOf(ch, role);
|
|
249
|
+
}
|
|
250
|
+
/** The small point (the dot that lengthens a note by half) of a pitch glyph. */
|
|
251
|
+
function toSmallPoint(ch) {
|
|
252
|
+
return variantOf(ch, "smallPoint");
|
|
253
|
+
}
|
|
254
|
+
/** The sharp (raised) form of a pitch glyph. */
|
|
255
|
+
function modifyUp(ch) {
|
|
256
|
+
return variantOf(ch, "modifyUp");
|
|
257
|
+
}
|
|
258
|
+
/** The flat (lowered) form of a pitch glyph. */
|
|
259
|
+
function modifyDown(ch) {
|
|
260
|
+
return variantOf(ch, "modifyDown");
|
|
261
|
+
}
|
|
262
|
+
/** The natural sign for a pitch glyph. */
|
|
263
|
+
function modifyNatural(ch) {
|
|
264
|
+
return variantOf(ch, "modifyNatural");
|
|
265
|
+
}
|
|
266
|
+
/** Reverse lookup: the base glyph and role of a variant glyph, or
|
|
267
|
+
* undefined when `ch` is not a variant. */
|
|
268
|
+
function baseOf(ch) {
|
|
269
|
+
const entry = BASES.get(ch);
|
|
270
|
+
return entry ? { ...entry } : void 0;
|
|
271
|
+
}
|
|
272
|
+
/** The modifier roles: the accidental signs a key is written with. */
|
|
273
|
+
var MODIFIER_ROLES = [
|
|
274
|
+
"modifyUp",
|
|
275
|
+
"modifyDown",
|
|
276
|
+
"modifyNatural"
|
|
277
|
+
];
|
|
278
|
+
/** Whether `ch` is a modifier glyph (a sharp, flat or natural sign). */
|
|
279
|
+
function isModifierGlyph(ch) {
|
|
280
|
+
const role = BASES.get(ch)?.role;
|
|
281
|
+
return role !== void 0 && MODIFIER_ROLES.includes(role);
|
|
282
|
+
}
|
|
283
|
+
/** The bracket pairs that set a cautionary accidental off (issue #95). */
|
|
284
|
+
var ACCIDENTAL_BRACKETS = /* @__PURE__ */ new Map([
|
|
285
|
+
["Â", "Ã"],
|
|
286
|
+
["Ä", "Å"],
|
|
287
|
+
["Æ", "Ç"]
|
|
288
|
+
]);
|
|
289
|
+
/**
|
|
290
|
+
* The length of the bracketed accidental that opens `notes` at `from`:
|
|
291
|
+
* an opening bracket, one or more accidental signs, the matching closing
|
|
292
|
+
* bracket, and the `¨`/`-` spacing right after it (`ÄôŨ¨` in `ÄôŨ¨õ`).
|
|
293
|
+
* 0 when no such unit stands there.
|
|
294
|
+
*/
|
|
295
|
+
function bracketedAccidentalLength(notes, from = 0) {
|
|
296
|
+
const close = ACCIDENTAL_BRACKETS.get(notes[from] ?? "");
|
|
297
|
+
if (close === void 0) return 0;
|
|
298
|
+
let at = from + 1;
|
|
299
|
+
while (at < notes.length && isModifierGlyph(notes[at])) at++;
|
|
300
|
+
if (at === from + 1 || notes[at] !== close) return 0;
|
|
301
|
+
at++;
|
|
302
|
+
while (notes[at] === "¨" || notes[at] === "-") at++;
|
|
303
|
+
return at - from;
|
|
304
|
+
}
|
|
305
|
+
/** The clef glyphs (issue #49) and the staff line each one sits on,
|
|
306
|
+
* counted from the bottom. The g-clef has two keybindings. */
|
|
307
|
+
var CLEF_GLYPHS = {
|
|
308
|
+
">": {
|
|
309
|
+
clef: "c",
|
|
310
|
+
line: 1
|
|
311
|
+
},
|
|
312
|
+
"#": {
|
|
313
|
+
clef: "c",
|
|
314
|
+
line: 2
|
|
315
|
+
},
|
|
316
|
+
"&": {
|
|
317
|
+
clef: "c",
|
|
318
|
+
line: 3
|
|
319
|
+
},
|
|
320
|
+
"@": {
|
|
321
|
+
clef: "c",
|
|
322
|
+
line: 4
|
|
323
|
+
},
|
|
324
|
+
"{": {
|
|
325
|
+
clef: "f",
|
|
326
|
+
line: 3
|
|
327
|
+
},
|
|
328
|
+
"}": {
|
|
329
|
+
clef: "f",
|
|
330
|
+
line: 4
|
|
331
|
+
},
|
|
332
|
+
M: {
|
|
333
|
+
clef: "g",
|
|
334
|
+
line: 2
|
|
335
|
+
},
|
|
336
|
+
"<": {
|
|
337
|
+
clef: "g",
|
|
338
|
+
line: 2
|
|
339
|
+
}
|
|
340
|
+
};
|
|
341
|
+
/** Whether `ch` is a clef glyph. */
|
|
342
|
+
function isClefGlyph(ch) {
|
|
343
|
+
return Object.hasOwn(CLEF_GLYPHS, ch);
|
|
344
|
+
}
|
|
345
|
+
/** The base pitch glyphs from low to high, one per staff position
|
|
346
|
+
* (spelled out: `Object.keys` would list the digit keys first). The
|
|
347
|
+
* index is the staff position the exact transposition counts with. */
|
|
348
|
+
var STAFF = [..."ðñ0123456789öüó"];
|
|
349
|
+
var LETTERS$1 = "abcdefg";
|
|
350
|
+
/** The pitch letter (`a`…`g`, octave dropped) of a pitch glyph — a base
|
|
351
|
+
* glyph or any of its variants — under `clef` (a clef glyph; default the
|
|
352
|
+
* standard g-clef). Undefined when the glyph has no pitch or the clef
|
|
353
|
+
* glyph is unknown. */
|
|
354
|
+
function pitchLetterOf(ch, clef = "M") {
|
|
355
|
+
const position = STAFF.indexOf(STAFF.includes(ch) ? ch : baseOf(ch)?.base ?? "");
|
|
356
|
+
const entry = CLEF_GLYPHS[clef];
|
|
357
|
+
if (position < 0 || !entry) return void 0;
|
|
358
|
+
const linePosition = STAFF.indexOf("2") + 2 * (entry.line - 1);
|
|
359
|
+
const shift = LETTERS$1.indexOf(entry.clef) - LETTERS$1.indexOf(gClefPitchOf(STAFF[linePosition])[0]);
|
|
360
|
+
return LETTERS$1[((LETTERS$1.indexOf(gClefPitchOf(STAFF[position])[0]) + shift) % 7 + 7) % 7];
|
|
361
|
+
}
|
|
362
|
+
//#endregion
|
|
363
|
+
//#region src/guidoScript.ts
|
|
364
|
+
/**
|
|
365
|
+
* A Guido script: the compact chant notation where pitches, neume shapes,
|
|
366
|
+
* and rhythm are packed into short character sequences, e.g.
|
|
367
|
+
*
|
|
368
|
+
* (MX)Ki(3)rály(3)ként(5) tró(7i)nol(7-:) *() az(7i) Úr(5-/) mind(5)ö(4t)rök(3)ké.(3-.)
|
|
369
|
+
*
|
|
370
|
+
* The first parenthesized group, when present, is the starting clef and
|
|
371
|
+
* note modifiers. After that the source alternates syllable-first:
|
|
372
|
+
*
|
|
373
|
+
* (clef) syllable(notes) syllable(notes) ...
|
|
374
|
+
*
|
|
375
|
+
* Word boundaries are carried by whitespace in the source: a syllable
|
|
376
|
+
* after which the source has a space ends a word.
|
|
377
|
+
*
|
|
378
|
+
* A note group with no text in front of it is held by the syllable
|
|
379
|
+
* before it (issue #83): `mé(3)(tT4R3E2)` sings `mé` on both groups. The
|
|
380
|
+
* held group stays a token of its own (`text: ''`, `held: true`), so
|
|
381
|
+
* every group is one slot and formatting still resolves on each
|
|
382
|
+
* syllable's own text. A syllable whose whole text is `~` is an empty
|
|
383
|
+
* slot (`empty: true`): notes, but no syllable — and so is a text-less
|
|
384
|
+
* group at the very start of the script.
|
|
385
|
+
*
|
|
386
|
+
* The parentheses and backslash are control characters, so literal ones
|
|
387
|
+
* in the syllable text are typed as backslash escapes: `\(`, `\)` and
|
|
388
|
+
* `\\` stand for `(`, `)` and `\`.
|
|
389
|
+
*
|
|
390
|
+
* Control statements are groups whose content is an `@`-keyword with
|
|
391
|
+
* optional arguments after one space: `(@break)` ends the current line
|
|
392
|
+
* pair at its position, `(@clef MX)` changes the clef (and key) inline,
|
|
393
|
+
* `(@key X)` replaces the key and `(@key)` clears it. `(@tight)` tightens
|
|
394
|
+
* the junctions up to `(@tight off)`, `(@keep)` keeps its run on one line
|
|
395
|
+
* up to `(@keep off)`, and `(@unit)` / `(@unit off)` do both. Statements are
|
|
396
|
+
* positional — the text run before one must be whitespace-only — and
|
|
397
|
+
* they are invisible to the sung-text accessors.
|
|
398
|
+
*
|
|
399
|
+
* `(@stanza …)` statements (issue #69) stand at the very end of the
|
|
400
|
+
* script, one per further stanza (2, 3, …) in order. Their text is
|
|
401
|
+
* syllabified stanza text (see `parseStanza` in stanzas.ts), kept raw —
|
|
402
|
+
* escapes included — and listed by `stanzas`; they are not tokens, so
|
|
403
|
+
* nothing that walks the tokens (the layout, the sung-text accessors)
|
|
404
|
+
* ever sees them.
|
|
405
|
+
*
|
|
406
|
+
* `@` is also the c4 clef glyph: an opening group of `@` and nothing but
|
|
407
|
+
* modifier glyphs (`(@)`, `(@X)`) is the opening clef, not a statement.
|
|
408
|
+
*/
|
|
409
|
+
/** The run properties a run statement sets. */
|
|
410
|
+
function runProperties(kind) {
|
|
411
|
+
return kind === "unit" ? ["tight", "keep"] : [kind];
|
|
412
|
+
}
|
|
413
|
+
/** Whether a group's content is a control statement (`@`-keyword,
|
|
414
|
+
* trimmed). Shared with the transforms, which must leave statement
|
|
415
|
+
* groups alone — their keyword characters are pitch glyphs otherwise.
|
|
416
|
+
* A c4 clef group (`@` + modifier glyphs) counts too: the opening one is
|
|
417
|
+
* told apart by `isC4ClefGroup`, and mid-script it is an error. */
|
|
418
|
+
function isStatementGroup(content) {
|
|
419
|
+
return content.trim().startsWith("@");
|
|
420
|
+
}
|
|
421
|
+
/** Whether a group's content has the shape of a c4 clef group: `@`
|
|
422
|
+
* followed by nothing but modifier glyphs. Statement keywords never
|
|
423
|
+
* consist solely of modifier glyphs, so the two cannot collide. */
|
|
424
|
+
function isC4ClefGroup(content) {
|
|
425
|
+
const [at, ...key] = content;
|
|
426
|
+
return at === "@" && key.every(isModifierGlyph);
|
|
427
|
+
}
|
|
428
|
+
/** Whether the leading group is the opening clef: anything but a
|
|
429
|
+
* statement, and the c4 clef despite its `@`. */
|
|
430
|
+
function isOpeningClef(content) {
|
|
431
|
+
return !isStatementGroup(content) || isC4ClefGroup(content);
|
|
432
|
+
}
|
|
433
|
+
/** Parses a key argument: modifier glyphs only. */
|
|
434
|
+
function parseKey(glyphs, statement) {
|
|
435
|
+
for (const ch of glyphs) if (!isModifierGlyph(ch)) throw new Error(`"${ch}" is not a key modifier glyph in "${statement}"`);
|
|
436
|
+
return glyphs;
|
|
437
|
+
}
|
|
438
|
+
/** Parses one statement group's content into its token (without
|
|
439
|
+
* `spaceBefore`). Throws on an unknown keyword or a bad argument. */
|
|
440
|
+
function parseStatement(content) {
|
|
441
|
+
const statement = content.trim();
|
|
442
|
+
if (isC4ClefGroup(statement)) throw new Error(`a clef group is only allowed at the start; write "(@clef ${statement})" to change the clef`);
|
|
443
|
+
const [, keyword, args = ""] = statement.match(/^@(\S*)(?: (.*))?$/) ?? [];
|
|
444
|
+
switch (keyword) {
|
|
445
|
+
case "break":
|
|
446
|
+
if (args !== "") throw new Error(`"@break" takes no argument: "${statement}"`);
|
|
447
|
+
return { kind: "break" };
|
|
448
|
+
case "clef": {
|
|
449
|
+
const [clef, ...key] = args;
|
|
450
|
+
if (clef === void 0) throw new Error("\"@clef\" needs a clef group argument, e.g. \"(@clef MX)\"");
|
|
451
|
+
if (!isClefGlyph(clef)) throw new Error(`"${clef}" is not a clef glyph in "${statement}"`);
|
|
452
|
+
return {
|
|
453
|
+
kind: "clef",
|
|
454
|
+
clef,
|
|
455
|
+
key: parseKey(key.join(""), statement)
|
|
456
|
+
};
|
|
457
|
+
}
|
|
458
|
+
case "key": return {
|
|
459
|
+
kind: "key",
|
|
460
|
+
key: parseKey(args, statement)
|
|
461
|
+
};
|
|
462
|
+
case "tight":
|
|
463
|
+
case "keep":
|
|
464
|
+
case "unit":
|
|
465
|
+
if (args !== "" && args !== "off") throw new Error(`"@${keyword}" takes only "off": "${statement}"`);
|
|
466
|
+
return {
|
|
467
|
+
kind: keyword,
|
|
468
|
+
on: args === ""
|
|
469
|
+
};
|
|
470
|
+
default: throw new Error(`Unknown statement "${statement}"`);
|
|
471
|
+
}
|
|
472
|
+
}
|
|
473
|
+
/** A statement token written back as its group. */
|
|
474
|
+
function statementSource(tok) {
|
|
475
|
+
switch (tok.kind) {
|
|
476
|
+
case "break": return "(@break)";
|
|
477
|
+
case "clef": return `(@clef ${tok.clef}${tok.key})`;
|
|
478
|
+
case "key": return tok.key === "" ? "(@key)" : `(@key ${tok.key})`;
|
|
479
|
+
case "tight":
|
|
480
|
+
case "keep":
|
|
481
|
+
case "unit": return tok.on ? `(@${tok.kind})` : `(@${tok.kind} off)`;
|
|
482
|
+
}
|
|
483
|
+
}
|
|
484
|
+
/** The stanza text of a `(@stanza …)` statement's trimmed content, or
|
|
485
|
+
* undefined when the statement is something else. */
|
|
486
|
+
function stanzaStatementText(statement) {
|
|
487
|
+
const match = statement.match(/^@stanza(?:\s+([\s\S]*))?$/);
|
|
488
|
+
return match ? match[1] ?? "" : void 0;
|
|
489
|
+
}
|
|
490
|
+
var GuidoScript = class {
|
|
491
|
+
source;
|
|
492
|
+
constructor(source, options = {}) {
|
|
493
|
+
this.source = (options.transforms ?? []).reduce((script, transform) => transform.apply(script), source);
|
|
494
|
+
}
|
|
495
|
+
/** The starting clef and note modifiers, or undefined when absent.
|
|
496
|
+
* A leading statement group is not a clef. */
|
|
497
|
+
get clef() {
|
|
498
|
+
const match = this.normalized.match(/^\s*\(([^)]*)\)/);
|
|
499
|
+
if (!match || !isOpeningClef(match[1])) return void 0;
|
|
500
|
+
return match[1];
|
|
501
|
+
}
|
|
502
|
+
/** The script's tokens in source order: syllables interleaved with
|
|
503
|
+
* control statements. Throws on a malformed statement (syllable text
|
|
504
|
+
* attached to one, or an unknown `@` keyword). */
|
|
505
|
+
get tokens() {
|
|
506
|
+
return this.parse().tokens;
|
|
507
|
+
}
|
|
508
|
+
/** The raw text of the `(@stanza …)` statements at the end of the
|
|
509
|
+
* script — stanzas 2, 3, … in order, escapes kept (the stanza text
|
|
510
|
+
* grammar folds them). Throws like `tokens` on a malformed script,
|
|
511
|
+
* and when anything but another stanza follows a stanza statement. */
|
|
512
|
+
get stanzas() {
|
|
513
|
+
return this.parse().stanzas;
|
|
514
|
+
}
|
|
515
|
+
/** Where each token stands in the source, parallel to `tokens`:
|
|
516
|
+
* offsets of its text (past leading whitespace), of its group's
|
|
517
|
+
* content and just past its group's `)`. The walker
|
|
518
|
+
* (`scriptSyllables`) reads them. */
|
|
519
|
+
get tokenSpans() {
|
|
520
|
+
return this.parse().spans;
|
|
521
|
+
}
|
|
522
|
+
parse() {
|
|
523
|
+
const script = this.normalized;
|
|
524
|
+
const head = script.match(/^\s*\(([^)]*)\)/);
|
|
525
|
+
const skipped = head && isOpeningClef(head[1]) ? head[0].length : 0;
|
|
526
|
+
const matches = [...script.slice(skipped).matchAll(/((?:[^()\\]|\\.)*)\(((?:[^)\\]|\\.)*)\)/g)];
|
|
527
|
+
let prev;
|
|
528
|
+
const stanzas = [];
|
|
529
|
+
const tokens = [];
|
|
530
|
+
const spans = [];
|
|
531
|
+
for (const match of matches) {
|
|
532
|
+
const [, text, notes] = match;
|
|
533
|
+
const at = skipped + match.index;
|
|
534
|
+
const notesStart = at + text.length + 1;
|
|
535
|
+
const span = {
|
|
536
|
+
start: at + (text.length - text.trimStart().length),
|
|
537
|
+
notesStart,
|
|
538
|
+
notesEnd: notesStart + notes.length,
|
|
539
|
+
end: notesStart + notes.length + 1
|
|
540
|
+
};
|
|
541
|
+
const stanza = isStatementGroup(notes) ? stanzaStatementText(notes.trim()) : void 0;
|
|
542
|
+
if (stanza !== void 0) {
|
|
543
|
+
if (text.trim() !== "") throw new Error(`statement must not carry syllable text: "${unescape(text.trim())}"`);
|
|
544
|
+
stanzas.push(stanza);
|
|
545
|
+
continue;
|
|
546
|
+
}
|
|
547
|
+
if (stanzas.length > 0) {
|
|
548
|
+
if (text.trim() === "" && notes === "") continue;
|
|
549
|
+
throw new Error("\"(@stanza …)\" statements must stand at the end of the script");
|
|
550
|
+
}
|
|
551
|
+
if (isStatementGroup(notes)) {
|
|
552
|
+
const statement = parseStatement(notes);
|
|
553
|
+
if (text.trim() !== "") throw new Error(`statement must not carry syllable text: "${unescape(text.trim())}"`);
|
|
554
|
+
tokens.push({
|
|
555
|
+
...statement,
|
|
556
|
+
spaceBefore: /^\s/.test(text)
|
|
557
|
+
});
|
|
558
|
+
spans.push(span);
|
|
559
|
+
continue;
|
|
560
|
+
}
|
|
561
|
+
const raw = text.trim();
|
|
562
|
+
const tok = {
|
|
563
|
+
kind: "syllable",
|
|
564
|
+
text: raw === "~" ? "" : unescape(raw),
|
|
565
|
+
notes: unescape(notes),
|
|
566
|
+
spaceBefore: /^\s/.test(text)
|
|
567
|
+
};
|
|
568
|
+
if (raw === "~" || raw === "" && prev === void 0 && tok.notes !== "") tok.empty = true;
|
|
569
|
+
else if (raw === "" && tok.notes !== "") {
|
|
570
|
+
if (prev.notes === "") throw new Error(`the group "(${tok.notes})" has no syllable to hold it: "${prev.text}" has no notes; write "~(${tok.notes})" for an empty slot`);
|
|
571
|
+
tok.held = true;
|
|
572
|
+
}
|
|
573
|
+
prev = tok;
|
|
574
|
+
tokens.push(tok);
|
|
575
|
+
spans.push(span);
|
|
576
|
+
}
|
|
577
|
+
let holder;
|
|
578
|
+
for (const [i, t] of tokens.entries()) {
|
|
579
|
+
if (t.kind !== "syllable") continue;
|
|
580
|
+
if (t.held) {
|
|
581
|
+
t.wordEnd = holder.wordEnd;
|
|
582
|
+
continue;
|
|
583
|
+
}
|
|
584
|
+
t.wordEnd = true;
|
|
585
|
+
let space = false;
|
|
586
|
+
for (let j = i + 1; j < tokens.length; j++) {
|
|
587
|
+
const tok = tokens[j];
|
|
588
|
+
if (tok.spaceBefore) space = true;
|
|
589
|
+
if (tok.kind === "syllable" && !tok.held && !tok.empty) {
|
|
590
|
+
t.wordEnd = space;
|
|
591
|
+
break;
|
|
592
|
+
}
|
|
593
|
+
}
|
|
594
|
+
holder = t;
|
|
595
|
+
}
|
|
596
|
+
return {
|
|
597
|
+
tokens,
|
|
598
|
+
stanzas,
|
|
599
|
+
spans
|
|
600
|
+
};
|
|
601
|
+
}
|
|
602
|
+
/** The script broken into syllables, each with its sung text and
|
|
603
|
+
* notes — the syllable tokens, statements filtered out. */
|
|
604
|
+
get syllables() {
|
|
605
|
+
return this.tokens.filter((t) => t.kind === "syllable").map(({ text, notes, wordEnd, held, empty }) => ({
|
|
606
|
+
text,
|
|
607
|
+
notes,
|
|
608
|
+
wordEnd,
|
|
609
|
+
...held ? { held } : {},
|
|
610
|
+
...empty ? { empty } : {}
|
|
611
|
+
}));
|
|
612
|
+
}
|
|
613
|
+
/**
|
|
614
|
+
* The source with the typing conveniences folded in, so the parser
|
|
615
|
+
* itself only ever sees complete scripts:
|
|
616
|
+
*
|
|
617
|
+
* - an unclosed parenthesis is closed at the end, so the notes typed
|
|
618
|
+
* so far already preview on the score;
|
|
619
|
+
* - a trailing syllable with no note group gets an empty one, so it
|
|
620
|
+
* renders like a `*()` syllable (text without notes width).
|
|
621
|
+
*/
|
|
622
|
+
get normalized() {
|
|
623
|
+
let script = this.source;
|
|
624
|
+
if ((script.match(/(^|[^\\])\(/g) ?? []).length > (script.match(/(^|[^\\])\)/g) ?? []).length) script += ")";
|
|
625
|
+
if (!/(^|[^\\])\)$/.test(script)) script += "()";
|
|
626
|
+
return script;
|
|
627
|
+
}
|
|
628
|
+
/** The syllables grouped into the words of the text. Held groups and
|
|
629
|
+
* empty slots sing no syllable of their own, so they are left out. */
|
|
630
|
+
get words() {
|
|
631
|
+
const words = [];
|
|
632
|
+
let word;
|
|
633
|
+
for (const syl of this.syllables) {
|
|
634
|
+
if (syl.held || syl.empty) continue;
|
|
635
|
+
word ??= {
|
|
636
|
+
text: "",
|
|
637
|
+
syllables: []
|
|
638
|
+
};
|
|
639
|
+
word.syllables.push({
|
|
640
|
+
text: syl.text,
|
|
641
|
+
notes: syl.notes
|
|
642
|
+
});
|
|
643
|
+
word.text = word.syllables.map((s) => s.text).join("");
|
|
644
|
+
if (syl.wordEnd) {
|
|
645
|
+
words.push(word);
|
|
646
|
+
word = void 0;
|
|
647
|
+
}
|
|
648
|
+
}
|
|
649
|
+
return words;
|
|
650
|
+
}
|
|
651
|
+
/** The script rebuilt from the parsed pieces — a round-trip of the
|
|
652
|
+
* source, statements and stanzas included. */
|
|
653
|
+
toString() {
|
|
654
|
+
const parts = this.tokens.map((tok, i) => {
|
|
655
|
+
const prefix = i > 0 && tok.spaceBefore ? " " : "";
|
|
656
|
+
return tok.kind === "syllable" ? `${prefix}${tok.empty ? "~" : tok.text === "~" ? "\\~" : escape(tok.text)}(${escape(tok.notes)})` : `${prefix}${statementSource(tok)}`;
|
|
657
|
+
});
|
|
658
|
+
const stanzas = this.stanzas.map((text) => `(@stanza ${text})`);
|
|
659
|
+
return `${this.clef !== void 0 ? `(${this.clef})` : ""}${parts.join("")}${stanzas.join("")}`;
|
|
660
|
+
}
|
|
661
|
+
};
|
|
662
|
+
/**
|
|
663
|
+
* The syllables of a script that carry text, in order — the ones the
|
|
664
|
+
* sung text is made of, so their count and texts match `chantText`
|
|
665
|
+
* (and `GuidoScript.words`). Held groups ride on their syllable
|
|
666
|
+
* (`held`), `~` empty slots, statements and `(@stanza …)` are skipped.
|
|
667
|
+
* Offsets index the script's source; index i is the "syllable i" of
|
|
668
|
+
* `composeScript`'s `from`/`to`.
|
|
669
|
+
*/
|
|
670
|
+
function scriptSyllables(script) {
|
|
671
|
+
const parsed = typeof script === "string" ? new GuidoScript(script) : script;
|
|
672
|
+
const tokens = parsed.tokens;
|
|
673
|
+
const spans = parsed.tokenSpans;
|
|
674
|
+
const out = [];
|
|
675
|
+
let prevWordEnd = true;
|
|
676
|
+
let holder;
|
|
677
|
+
for (const [i, tok] of tokens.entries()) {
|
|
678
|
+
if (tok.kind !== "syllable") continue;
|
|
679
|
+
if (tok.empty) {
|
|
680
|
+
holder = void 0;
|
|
681
|
+
continue;
|
|
682
|
+
}
|
|
683
|
+
if (tok.held) {
|
|
684
|
+
if (holder) {
|
|
685
|
+
holder.held.push(tok.notes);
|
|
686
|
+
holder.end = spans[i].end;
|
|
687
|
+
}
|
|
688
|
+
continue;
|
|
689
|
+
}
|
|
690
|
+
const span = spans[i];
|
|
691
|
+
holder = {
|
|
692
|
+
text: tok.text,
|
|
693
|
+
notes: tok.notes,
|
|
694
|
+
held: [],
|
|
695
|
+
wordStart: prevWordEnd,
|
|
696
|
+
wordEnd: tok.wordEnd,
|
|
697
|
+
start: span.start,
|
|
698
|
+
notesStart: span.notesStart,
|
|
699
|
+
notesEnd: span.notesEnd,
|
|
700
|
+
end: span.end,
|
|
701
|
+
token: i
|
|
702
|
+
};
|
|
703
|
+
out.push(holder);
|
|
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
|
+
});
|
|
814
|
+
}
|
|
815
|
+
const out = [];
|
|
816
|
+
for (const w of words) {
|
|
817
|
+
if (w.length === 1 && !w[0].escaped && w[0].ch === "~") {
|
|
818
|
+
out.push({ kind: "empty" });
|
|
819
|
+
continue;
|
|
820
|
+
}
|
|
821
|
+
const pieces = [""];
|
|
822
|
+
for (const { ch, escaped } of w) if (!escaped && SYLLABLE_BREAKS.includes(ch)) pieces.push("");
|
|
823
|
+
else pieces[pieces.length - 1] += !escaped && ch === "~" ? "\xA0" : ch;
|
|
824
|
+
const last = pieces.length - 1;
|
|
825
|
+
for (const [p, piece] of pieces.entries()) {
|
|
826
|
+
if (piece === "") {
|
|
827
|
+
if (p < last) out.push({ kind: "hold" });
|
|
828
|
+
continue;
|
|
829
|
+
}
|
|
830
|
+
out.push({
|
|
831
|
+
kind: "syllable",
|
|
832
|
+
text: piece,
|
|
833
|
+
wordEnd: p === last
|
|
834
|
+
});
|
|
835
|
+
}
|
|
836
|
+
}
|
|
837
|
+
return out;
|
|
838
|
+
}
|
|
839
|
+
/** Whether a token opens a slot: a syllable with notes that are not all
|
|
840
|
+
* markers. */
|
|
841
|
+
function isSlot(tok) {
|
|
842
|
+
return tok.kind === "syllable" && tok.notes !== "" && splitNoteMarkers(tok.notes).core !== "";
|
|
843
|
+
}
|
|
844
|
+
function parsed(script) {
|
|
845
|
+
return typeof script === "string" ? new GuidoScript(script) : script;
|
|
846
|
+
}
|
|
847
|
+
/** The number of slots (note groups that take a stanza syllable). */
|
|
848
|
+
function slotCount(script) {
|
|
849
|
+
return parsed(script).tokens.filter(isSlot).length;
|
|
850
|
+
}
|
|
851
|
+
/** The stanza texts of stanzas 2, 3, …: the `stanzas` option when
|
|
852
|
+
* given, the script's `(@stanza …)` statements otherwise. */
|
|
853
|
+
function stanzaTexts(script, options = {}) {
|
|
854
|
+
return options.stanzas ?? parsed(script).stanzas;
|
|
855
|
+
}
|
|
856
|
+
/** How many stanzas the script has, stanza 1 (the script) included. */
|
|
857
|
+
function stanzaCount(script, options = {}) {
|
|
858
|
+
return stanzaTexts(script, options).length + 1;
|
|
859
|
+
}
|
|
860
|
+
function sings(fill) {
|
|
861
|
+
return fill.kind === "syllable" || fill.kind === "hold";
|
|
862
|
+
}
|
|
863
|
+
/** Stanza 1's fills: the slot tokens themselves. */
|
|
864
|
+
function scriptFills(slots) {
|
|
865
|
+
return slots.map((tok) => tok.empty ? { kind: "empty" } : tok.held ? { kind: "hold" } : {
|
|
866
|
+
kind: "syllable",
|
|
867
|
+
text: tok.text,
|
|
868
|
+
wordEnd: tok.wordEnd
|
|
869
|
+
});
|
|
870
|
+
}
|
|
871
|
+
/** A stanza's syllables laid on `count` slots, with its mismatches. */
|
|
872
|
+
function alignStanza(text, count, stanza) {
|
|
873
|
+
const syllables = parseStanza(text);
|
|
874
|
+
const fills = [];
|
|
875
|
+
const warnings = [];
|
|
876
|
+
for (let i = 0; i < count; i++) {
|
|
877
|
+
const syl = syllables[i];
|
|
878
|
+
fills.push(syl ?? { kind: "missing" });
|
|
879
|
+
if (syl === void 0) warnings.push({
|
|
880
|
+
stanza,
|
|
881
|
+
slot: i + 1,
|
|
882
|
+
kind: "missing",
|
|
883
|
+
message: `Stanza ${stanza} has no syllable for slot ${i + 1} of ${count}`
|
|
884
|
+
});
|
|
885
|
+
}
|
|
886
|
+
for (const [i, syl] of syllables.slice(count).entries()) {
|
|
887
|
+
const shown = syl.kind === "syllable" ? syl.text : syl.kind === "hold" ? "-" : "~";
|
|
888
|
+
warnings.push({
|
|
889
|
+
stanza,
|
|
890
|
+
slot: count + i + 1,
|
|
891
|
+
kind: "extra",
|
|
892
|
+
text: shown,
|
|
893
|
+
message: `Stanza ${stanza} has an extra syllable "${shown}" past the last slot (${count})`
|
|
894
|
+
});
|
|
895
|
+
}
|
|
896
|
+
return {
|
|
897
|
+
fills,
|
|
898
|
+
warnings
|
|
899
|
+
};
|
|
900
|
+
}
|
|
901
|
+
/** Throws when `stanza` is not one of the script's stanzas. */
|
|
902
|
+
function checkStanzaNumber(stanza, count) {
|
|
903
|
+
if (!Number.isInteger(stanza) || stanza < 1 || stanza > count) throw new RangeError(`Stanza ${stanza} does not exist: the script has ${count} stanza${count === 1 ? "" : "s"}`);
|
|
904
|
+
}
|
|
905
|
+
/** The count mismatches of one stanza (`options.stanza`), or of every
|
|
906
|
+
* stanza when none is named. Stanza 1 is the script and never warns;
|
|
907
|
+
* an intentional `~` is a syllable like any other. */
|
|
908
|
+
function stanzaWarnings(script, options = {}) {
|
|
909
|
+
const texts = stanzaTexts(script, options);
|
|
910
|
+
const count = slotCount(script);
|
|
911
|
+
if (options.stanza !== void 0) {
|
|
912
|
+
checkStanzaNumber(options.stanza, texts.length + 1);
|
|
913
|
+
if (options.stanza === 1) return [];
|
|
914
|
+
return alignStanza(texts[options.stanza - 2], count, options.stanza).warnings;
|
|
915
|
+
}
|
|
916
|
+
return texts.flatMap((text, i) => alignStanza(text, count, i + 2).warnings);
|
|
917
|
+
}
|
|
918
|
+
/** Pads stanza text with `~` empty slots up to the script's slot count
|
|
919
|
+
* (unchanged when it already has enough syllables). */
|
|
920
|
+
function padStanza(script, text) {
|
|
921
|
+
const missing = slotCount(script) - parseStanza(text).length;
|
|
922
|
+
return missing > 0 ? `${text.trimEnd()}${" ~".repeat(missing)}` : text;
|
|
923
|
+
}
|
|
924
|
+
/** A note group with its plain heads, virgas and longas swapped for small
|
|
925
|
+
* heads (connectors, markers and signs stay). */
|
|
926
|
+
function smallHeads(notes) {
|
|
927
|
+
return Array.from(notes, (ch) => {
|
|
928
|
+
const small = toSmallHead(ch);
|
|
929
|
+
if (small !== void 0) return small;
|
|
930
|
+
const variant = baseOf(ch);
|
|
931
|
+
if (variant && (variant.role === "virga" || variant.role === "longa")) return toSmallHead(variant.base) ?? ch;
|
|
932
|
+
return ch;
|
|
933
|
+
}).join("");
|
|
934
|
+
}
|
|
935
|
+
/** A syllable's text written back into the script notation. */
|
|
936
|
+
function syllableSource(text) {
|
|
937
|
+
return text === "~" ? "\\~" : escape(text);
|
|
938
|
+
}
|
|
939
|
+
/** Rewrites a script's source to sing stanza `stanza`. */
|
|
940
|
+
function stanzaSource(source, stanza, options) {
|
|
941
|
+
const script = new GuidoScript(source);
|
|
942
|
+
const texts = stanzaTexts(script, options);
|
|
943
|
+
checkStanzaNumber(stanza, texts.length + 1);
|
|
944
|
+
const rendered = options.rendered ?? [stanza];
|
|
945
|
+
for (const k of rendered) checkStanzaNumber(k, texts.length + 1);
|
|
946
|
+
const mode = options.emptySlots ?? "normal";
|
|
947
|
+
if (stanza === 1 && mode === "normal") return source;
|
|
948
|
+
const tokens = script.tokens;
|
|
949
|
+
const slotTokens = tokens.filter((t) => isSlot(t));
|
|
950
|
+
const allFills = [scriptFills(slotTokens), ...texts.map((text, i) => alignStanza(text, slotTokens.length, i + 2).fills)];
|
|
951
|
+
const fills = allFills[stanza - 1];
|
|
952
|
+
const cue = (slot) => mode === "partial" ? allFills.some((f) => sings(f[slot])) && !allFills.every((f) => sings(f[slot])) : mode === "small" && rendered.every((k) => !sings(allFills[k - 1][slot])) && allFills.some((f, j) => !rendered.includes(j + 1) && sings(f[slot]));
|
|
953
|
+
let out = script.clef !== void 0 ? `(${script.clef})` : "";
|
|
954
|
+
let first = true;
|
|
955
|
+
let pendingSpace = false;
|
|
956
|
+
let canHold = false;
|
|
957
|
+
const emit = (piece, space) => {
|
|
958
|
+
out += (space && !first ? " " : "") + piece;
|
|
959
|
+
first = false;
|
|
960
|
+
};
|
|
961
|
+
let slot = 0;
|
|
962
|
+
for (const tok of tokens) {
|
|
963
|
+
if (tok.kind !== "syllable") {
|
|
964
|
+
emit(statementSource(tok), pendingSpace);
|
|
965
|
+
pendingSpace = false;
|
|
966
|
+
continue;
|
|
967
|
+
}
|
|
968
|
+
if (!isSlot(tok)) {
|
|
969
|
+
if (tok.held) {
|
|
970
|
+
emit(`(${escape(tok.notes)})`, pendingSpace);
|
|
971
|
+
pendingSpace = false;
|
|
972
|
+
} else {
|
|
973
|
+
emit(`${syllableSource(tok.text)}(${escape(tok.notes)})`, pendingSpace || tok.spaceBefore);
|
|
974
|
+
pendingSpace = tok.wordEnd;
|
|
975
|
+
canHold = tok.notes !== "";
|
|
976
|
+
}
|
|
977
|
+
continue;
|
|
978
|
+
}
|
|
979
|
+
const fill = fills[slot];
|
|
980
|
+
const notes = escape(cue(slot) ? smallHeads(tok.notes) : tok.notes);
|
|
981
|
+
slot++;
|
|
982
|
+
switch (fill.kind) {
|
|
983
|
+
case "syllable":
|
|
984
|
+
emit(`${syllableSource(fill.text)}(${notes})`, pendingSpace);
|
|
985
|
+
pendingSpace = fill.wordEnd;
|
|
986
|
+
break;
|
|
987
|
+
case "hold":
|
|
988
|
+
emit(`${canHold ? "" : "~"}(${notes})`, false);
|
|
989
|
+
break;
|
|
990
|
+
case "empty":
|
|
991
|
+
emit(`~(${notes})`, false);
|
|
992
|
+
break;
|
|
993
|
+
case "missing":
|
|
994
|
+
emit(`${STANZA_PLACEHOLDER}(${notes})`, true);
|
|
995
|
+
pendingSpace = true;
|
|
996
|
+
}
|
|
997
|
+
canHold = true;
|
|
998
|
+
}
|
|
999
|
+
return out;
|
|
1000
|
+
}
|
|
1001
|
+
/** The `selectStanza` transform factory: rewrites the script to sing
|
|
1002
|
+
* stanza `stanza` (1-based; 1 is the script itself). Its syllables go on
|
|
1003
|
+
* the slots in order — missing ones become `STANZA_PLACEHOLDER`
|
|
1004
|
+
* syllables, extra ones are dropped (`stanzaWarnings` reports both) — and
|
|
1005
|
+
* the `(@stanza …)` statements are dropped. Everything that is not a
|
|
1006
|
+
* slot (statements, syllables without notes, marker-only groups) stays as
|
|
1007
|
+
* written. With `emptySlots: 'small'`, a slot the stanza leaves empty
|
|
1008
|
+
* while another stanza sings it gets small heads (`rendered` lists the
|
|
1009
|
+
* stanzas stacked with it: then a slot gets small heads when none of
|
|
1010
|
+
* them sings it while another stanza does). With `emptySlots:
|
|
1011
|
+
* 'partial'`, every slot some stanza sings and another does not gets
|
|
1012
|
+
* small heads, whatever is rendered. Stanza 1 with normal
|
|
1013
|
+
* empty slots returns the source unchanged. Throws a RangeError for a
|
|
1014
|
+
* stanza the script does not have. Register it first, so the other
|
|
1015
|
+
* transforms (the initial, the transpositions) see the stanza's text. */
|
|
1016
|
+
function selectStanza(stanza, options = {}) {
|
|
1017
|
+
return {
|
|
1018
|
+
name: "selectStanza",
|
|
1019
|
+
apply: (source) => stanzaSource(source, stanza, options)
|
|
1020
|
+
};
|
|
1021
|
+
}
|
|
1022
|
+
/** The script singing stanza `stanza` (see `selectStanza`); `transforms`
|
|
1023
|
+
* run after the stanza swap. A GuidoScript argument is taken by its
|
|
1024
|
+
* (already transformed) source. */
|
|
1025
|
+
function withStanza(script, stanza, options = {}) {
|
|
1026
|
+
return new GuidoScript(typeof script === "string" ? script : script.source, { transforms: [selectStanza(stanza, options), ...options.transforms ?? []] });
|
|
1027
|
+
}
|
|
1028
|
+
/** The stanza numbers a stack names: every stanza for `'all'`, the
|
|
1029
|
+
* listed ones otherwise. Throws a RangeError for an empty list or a
|
|
1030
|
+
* stanza the script does not have. */
|
|
1031
|
+
function stackedStanzaNumbers(script, stack, options = {}) {
|
|
1032
|
+
const count = stanzaCount(script, options);
|
|
1033
|
+
if (stack === "all") return Array.from({ length: count }, (_, k) => k + 1);
|
|
1034
|
+
if (stack.length === 0) throw new RangeError("No stanza to stack: the list is empty");
|
|
1035
|
+
for (const k of stack) checkStanzaNumber(k, count);
|
|
1036
|
+
return [...stack];
|
|
1037
|
+
}
|
|
1038
|
+
/** The scripts of stacked stanzas (issue #84): one per stanza of the
|
|
1039
|
+
* stack, in its order, each singing its stanza on the shared notes (see
|
|
1040
|
+
* `withStanza`; `transforms` run after the swap). Hand them to the layout
|
|
1041
|
+
* together (`layoutScore`, `GuidoRender`, the Word export) for one text
|
|
1042
|
+
* line per stanza under one staff. With `emptySlots: 'small'`, a slot
|
|
1043
|
+
* gets small heads when no stacked stanza sings it while a stanza left
|
|
1044
|
+
* out of the stack does, so every script carries the same notes. */
|
|
1045
|
+
function stackStanzas(script, stack, options = {}) {
|
|
1046
|
+
const rendered = stackedStanzaNumbers(script, stack, options);
|
|
1047
|
+
return rendered.map((k) => withStanza(script, k, {
|
|
1048
|
+
...options,
|
|
1049
|
+
rendered
|
|
1050
|
+
}));
|
|
1051
|
+
}
|
|
1052
|
+
//#endregion
|
|
1053
|
+
//#region src/exactTranspose.ts
|
|
1054
|
+
/**
|
|
1055
|
+
* Exact transposition (issue #70): moves the sounding pitches of a
|
|
1056
|
+
* script and re-notates them under a target clef and key, so the result
|
|
1057
|
+
* reads like a chant notated at the new pitch. The staff-degree shift
|
|
1058
|
+
* (`transposeBy`) stays separate and ignores pitch meaning.
|
|
1059
|
+
*
|
|
1060
|
+
* A note's sounding pitch is its staff position + the clef in effect +
|
|
1061
|
+
* the key in effect + any sign written in its group. A sign affects its
|
|
1062
|
+
* whole group: every note of that pitch letter, in every octave. The
|
|
1063
|
+
* transposition is two steps:
|
|
1064
|
+
*
|
|
1065
|
+
* 1. Move every pitch by a spelled interval (letter steps + semitones,
|
|
1066
|
+
* so a major third up turns b♭ into d, not e♭♭).
|
|
1067
|
+
* 2. Re-notate under the target clef and key: each note's staff position
|
|
1068
|
+
* and glyph, plus the signs the target key doesn't cover. Added signs
|
|
1069
|
+
* go before the note group, after its leading markers (the #49 rule).
|
|
1070
|
+
*
|
|
1071
|
+
* Keys are written with the classical key-signature layout
|
|
1072
|
+
* (`KEY_SIGNATURES`). Results past the glyph range and missing glyph
|
|
1073
|
+
* variants are errors, never silent clamps.
|
|
1074
|
+
*/
|
|
1075
|
+
/** A transposed note past the end of the glyph range. Thrown as its own
|
|
1076
|
+
* class so the octave search for a `final` can tell it apart. */
|
|
1077
|
+
var TranspositionRangeError = class extends Error {
|
|
1078
|
+
constructor(message) {
|
|
1079
|
+
super(message);
|
|
1080
|
+
this.name = "TranspositionRangeError";
|
|
1081
|
+
}
|
|
1082
|
+
};
|
|
1083
|
+
/** The key-signature glyphs per clef glyph (issue #70): flats in the
|
|
1084
|
+
* order b e a d g c f, sharps in the order f c g d a e b, each at the
|
|
1085
|
+
* highest octave of the shape that stays on the staff. A key of n signs
|
|
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;
|
|
1286
|
+
return {
|
|
1287
|
+
steps: direction * steps,
|
|
1288
|
+
semitones: direction * (base + adjust)
|
|
1289
|
+
};
|
|
1290
|
+
}
|
|
1291
|
+
var GROUP = /((?:[^()\\]|\\.)*)\(((?:[^)\\]|\\.)*)\)/g;
|
|
1292
|
+
function escapeNotes(notes) {
|
|
1293
|
+
return notes.replace(/\\/g, "\\\\").replace(/[()]/g, (ch) => `\\${ch}`);
|
|
1294
|
+
}
|
|
1295
|
+
/** Re-notates one note group. `src*` are the clef and key in effect in
|
|
1296
|
+
* the source, `tgt*` the ones in the result. Records the group's last
|
|
1297
|
+
* note pitch (the candidate final) through `onNote`. */
|
|
1298
|
+
function renotateGroup(notes, interval, srcClef, srcKey, tgtClef, tgtKey, onNote) {
|
|
1299
|
+
const { leading, core, trailing } = splitNoteMarkers(notes);
|
|
1300
|
+
const chars = [...core];
|
|
1301
|
+
const groupSigns = /* @__PURE__ */ new Map();
|
|
1302
|
+
for (const ch of chars) {
|
|
1303
|
+
if (!isModifierGlyph(ch)) continue;
|
|
1304
|
+
const glyph = glyphOf(ch);
|
|
1305
|
+
const letter = mod7(stepAt(glyph.position, srcClef));
|
|
1306
|
+
const alter = alterOfRole(glyph.role);
|
|
1307
|
+
if (groupSigns.has(letter) && groupSigns.get(letter) !== alter) throw new Error(`The note group "${notes}" carries two different signs for ${LETTERS[letter]}`);
|
|
1308
|
+
groupSigns.set(letter, alter);
|
|
1309
|
+
}
|
|
1310
|
+
const coveredLetters = /* @__PURE__ */ new Set();
|
|
1311
|
+
const out = [];
|
|
1312
|
+
const added = [];
|
|
1313
|
+
const pending = [];
|
|
1314
|
+
for (const ch of chars) {
|
|
1315
|
+
const glyph = glyphOf(ch);
|
|
1316
|
+
if (!glyph) {
|
|
1317
|
+
if (isPitchChar(ch)) throw new Error(`The glyph "${ch}" has no glyph-family entry, so it cannot be transposed exactly`);
|
|
1318
|
+
out.push(ch);
|
|
1319
|
+
continue;
|
|
1320
|
+
}
|
|
1321
|
+
const step = stepAt(glyph.position, srcClef);
|
|
1322
|
+
const letter = mod7(step);
|
|
1323
|
+
const keyAlter = srcKey.get(letter) ?? 0;
|
|
1324
|
+
if (isModifierGlyph(ch)) {
|
|
1325
|
+
const alter = alterOfRole(glyph.role);
|
|
1326
|
+
const moved = movePitch({
|
|
1327
|
+
step,
|
|
1328
|
+
alter
|
|
1329
|
+
}, interval);
|
|
1330
|
+
const tgtAlter = tgtKey.get(mod7(moved.step)) ?? 0;
|
|
1331
|
+
if (moved.alter !== tgtAlter || alter === keyAlter) {
|
|
1332
|
+
const position = positionOf(moved.step, tgtClef);
|
|
1333
|
+
out.push(glyphAt(position, roleOfAlter(moved.alter), `The sign "${ch}"`));
|
|
1334
|
+
coveredLetters.add(mod7(moved.step));
|
|
1335
|
+
}
|
|
1336
|
+
continue;
|
|
1337
|
+
}
|
|
1338
|
+
const pitch = {
|
|
1339
|
+
step,
|
|
1340
|
+
alter: groupSigns.get(letter) ?? keyAlter
|
|
1341
|
+
};
|
|
1342
|
+
const moved = movePitch(pitch, interval);
|
|
1343
|
+
out.push(glyphAt(positionOf(moved.step, tgtClef), glyph.role, `The note "${ch}"`));
|
|
1344
|
+
if (glyph.role === "smallPoint") continue;
|
|
1345
|
+
onNote(pitch);
|
|
1346
|
+
pending.push(moved);
|
|
1347
|
+
}
|
|
1348
|
+
for (const moved of pending) {
|
|
1349
|
+
const letter = mod7(moved.step);
|
|
1350
|
+
if (coveredLetters.has(letter) || moved.alter === (tgtKey.get(letter) ?? 0)) continue;
|
|
1351
|
+
const sign = glyphAt(positionOf(moved.step, tgtClef), roleOfAlter(moved.alter), `The sign for ${letterName(moved.step)}${alterName(moved.alter)}`);
|
|
1352
|
+
if (!added.includes(sign)) added.push(sign);
|
|
1353
|
+
}
|
|
1354
|
+
return leading + added.join("") + out.join("") + trailing;
|
|
1355
|
+
}
|
|
1356
|
+
/** One re-notation pass over the source. Returns the result and the
|
|
1357
|
+
* source's final (the pitch of its last note). */
|
|
1358
|
+
function renotate(source, plan) {
|
|
1359
|
+
new GuidoScript(source).tokens;
|
|
1360
|
+
const head = source.match(/^\s*\(([^)]*)\)/);
|
|
1361
|
+
const opening = head && (!isStatementGroup(head[1]) || isC4ClefGroup(head[1])) ? head : null;
|
|
1362
|
+
const openingClef = opening && isClefGlyph(opening[1].trimStart()[0] ?? "") ? opening[1].trimStart()[0] : void 0;
|
|
1363
|
+
if (opening && openingClef === void 0 && opening[1].trim() !== "") throw new Error(`The opening group "(${opening[1]})" does not start with a clef glyph`);
|
|
1364
|
+
if (plan.clef !== void 0 && !isClefGlyph(plan.clef)) throw new Error(`"${plan.clef}" is not a clef glyph`);
|
|
1365
|
+
let srcClef = openingClef ?? "M";
|
|
1366
|
+
let srcKey = readKey(opening && openingClef ? opening[1].trimStart().slice(1) : "", srcClef);
|
|
1367
|
+
let tgtClef = plan.clef ?? srcClef;
|
|
1368
|
+
let tgtKey = plan.key === void 0 ? moveKey(srcKey, plan.interval) : typeof plan.key === "number" ? countedKey(plan.key) : readKey(plan.key, tgtClef);
|
|
1369
|
+
const openingKey = writeKey(tgtKey, tgtClef);
|
|
1370
|
+
const openingGroup = opening || plan.clef !== void 0 || openingKey !== "" ? `(${tgtClef}${openingKey})` : "";
|
|
1371
|
+
let final;
|
|
1372
|
+
const offset = opening ? opening[0].length : 0;
|
|
1373
|
+
return {
|
|
1374
|
+
result: openingGroup + source.slice(offset).replace(GROUP, (_, text, rawNotes) => {
|
|
1375
|
+
if (isStatementGroup(rawNotes)) {
|
|
1376
|
+
const [, keyword, args = ""] = rawNotes.trim().match(/^@(\S*)(?: (.*))?$/) ?? [];
|
|
1377
|
+
if (keyword === "clef") {
|
|
1378
|
+
srcClef = args[0];
|
|
1379
|
+
srcKey = readKey(args.slice(1), srcClef);
|
|
1380
|
+
const moved = moveKey(srcKey, plan.interval);
|
|
1381
|
+
if (plan.clef === void 0) {
|
|
1382
|
+
tgtClef = srcClef;
|
|
1383
|
+
tgtKey = moved;
|
|
1384
|
+
return `${text}(@clef ${tgtClef}${writeKey(tgtKey, tgtClef)})`;
|
|
1385
|
+
}
|
|
1386
|
+
if (sameKey(moved, tgtKey)) return text;
|
|
1387
|
+
tgtKey = moved;
|
|
1388
|
+
const key = writeKey(tgtKey, tgtClef);
|
|
1389
|
+
return `${text}(@key${key === "" ? "" : ` ${key}`})`;
|
|
1390
|
+
}
|
|
1391
|
+
if (keyword === "key") {
|
|
1392
|
+
srcKey = readKey(args, srcClef);
|
|
1393
|
+
tgtKey = moveKey(srcKey, plan.interval);
|
|
1394
|
+
const key = writeKey(tgtKey, tgtClef);
|
|
1395
|
+
return `${text}(@key${key === "" ? "" : ` ${key}`})`;
|
|
1396
|
+
}
|
|
1397
|
+
return `${text}(${rawNotes})`;
|
|
1398
|
+
}
|
|
1399
|
+
return `${text}(${escapeNotes(renotateGroup(unescape(rawNotes), plan.interval, srcClef, srcKey, tgtClef, tgtKey, (pitch) => {
|
|
1400
|
+
final = pitch;
|
|
1401
|
+
}))})`;
|
|
1402
|
+
}),
|
|
1403
|
+
final
|
|
1404
|
+
};
|
|
1405
|
+
}
|
|
1406
|
+
/** The finals' pitch letter and alteration (`g`, `bb`, `b♭`, `f#`). */
|
|
1407
|
+
function parseFinal(final) {
|
|
1408
|
+
const match = final.trim().match(/^([a-gA-G])(b|♭|#|♯)?$/);
|
|
1409
|
+
if (!match) throw new Error(`Unknown final "${final}"`);
|
|
1410
|
+
const alter = match[2] === void 0 ? 0 : "b♭".includes(match[2]) ? -1 : 1;
|
|
1411
|
+
return {
|
|
1412
|
+
letter: LETTERS.indexOf(match[1].toLowerCase()),
|
|
1413
|
+
alter
|
|
1414
|
+
};
|
|
1415
|
+
}
|
|
1416
|
+
/**
|
|
1417
|
+
* Transposes a script exactly: moves its sounding pitches and
|
|
1418
|
+
* re-notates them under the target clef and key (see
|
|
1419
|
+
* `TranspositionTarget`). Without a key, the key moves with the
|
|
1420
|
+
* interval; without a clef, the clefs stay as written (mid-script
|
|
1421
|
+
* `(@clef …)` included); `(@key …)` statements always move with the
|
|
1422
|
+
* interval. A target clef re-notates the whole script in that clef and
|
|
1423
|
+
* removes mid-script `(@clef …)` statements (one that changed the key
|
|
1424
|
+
* leaves a `(@key …)` behind). With a `final`, the octave whose result
|
|
1425
|
+
* fits the glyph range wins — the smaller move when several fit.
|
|
1426
|
+
* Throws past the glyph range and on a missing glyph variant.
|
|
1427
|
+
*/
|
|
1428
|
+
function transposeScript(source, target) {
|
|
1429
|
+
if (target.interval !== void 0 && target.final !== void 0) throw new Error("A transposition target takes an interval or a final, not both");
|
|
1430
|
+
const plan = (interval) => ({
|
|
1431
|
+
interval,
|
|
1432
|
+
clef: target.clef,
|
|
1433
|
+
key: target.key
|
|
1434
|
+
});
|
|
1435
|
+
if (target.final === void 0) return renotate(source, plan(typeof target.interval === "string" ? parseInterval(target.interval) : target.interval ?? {
|
|
1436
|
+
steps: 0,
|
|
1437
|
+
semitones: 0
|
|
1438
|
+
})).result;
|
|
1439
|
+
const sourceFinal = renotate(source, { interval: {
|
|
1440
|
+
steps: 0,
|
|
1441
|
+
semitones: 0
|
|
1442
|
+
} }).final;
|
|
1443
|
+
if (sourceFinal === void 0) throw new Error("The script has no notes, so it has no final");
|
|
1444
|
+
const wanted = parseFinal(target.final);
|
|
1445
|
+
const up = mod7(wanted.letter - sourceFinal.step);
|
|
1446
|
+
const candidates = [
|
|
1447
|
+
-14,
|
|
1448
|
+
-7,
|
|
1449
|
+
0,
|
|
1450
|
+
7,
|
|
1451
|
+
14
|
|
1452
|
+
].map((octave) => {
|
|
1453
|
+
const semitones = semitoneOf({
|
|
1454
|
+
step: sourceFinal.step + up + octave,
|
|
1455
|
+
alter: wanted.alter
|
|
1456
|
+
}) - semitoneOf(sourceFinal);
|
|
1457
|
+
return {
|
|
1458
|
+
steps: up + octave,
|
|
1459
|
+
semitones
|
|
1460
|
+
};
|
|
1461
|
+
}).sort((a, b) => Math.abs(a.semitones) - Math.abs(b.semitones) || b.semitones - a.semitones);
|
|
1462
|
+
let rangeError;
|
|
1463
|
+
for (const interval of candidates) try {
|
|
1464
|
+
return renotate(source, plan(interval)).result;
|
|
1465
|
+
} catch (error) {
|
|
1466
|
+
if (!(error instanceof TranspositionRangeError)) throw error;
|
|
1467
|
+
rangeError ??= error;
|
|
1468
|
+
}
|
|
1469
|
+
throw rangeError;
|
|
1470
|
+
}
|
|
1471
|
+
/** The exact transposition as a script transform (see `transposeScript`). */
|
|
1472
|
+
function transposeExact(target) {
|
|
1473
|
+
return {
|
|
1474
|
+
name: "transposeExact",
|
|
1475
|
+
apply: (source) => transposeScript(source, target)
|
|
1476
|
+
};
|
|
1477
|
+
}
|
|
1478
|
+
//#endregion
|
|
1479
|
+
//#region src/guidoScriptTransforms.ts
|
|
1480
|
+
/**
|
|
1481
|
+
* Source-level transformations for GuidoScript: named rewrites applied
|
|
1482
|
+
* to a script's raw source before parsing (see the `transforms` option
|
|
1483
|
+
* on the GuidoScript constructor). Each one receives the source and
|
|
1484
|
+
* returns the rewritten source — or the source unchanged when the
|
|
1485
|
+
* feature does not apply. They compose in registration order, and the
|
|
1486
|
+
* parsed script never sees which ones ran. New script-level features
|
|
1487
|
+
* are added here, and only here: as entries of SCRIPT_TRANSFORMS, or —
|
|
1488
|
+
* when the feature takes parameters — as factories returning a
|
|
1489
|
+
* ScriptTransform (see `transposeBy`).
|
|
1490
|
+
*/
|
|
1491
|
+
/** The first character unit of a text run: a backslash escape pair or a
|
|
1492
|
+
* single character, skipping leading whitespace. Returns null when the
|
|
1493
|
+
* run has no characters to mark. */
|
|
1494
|
+
function firstCharUnit(text) {
|
|
1495
|
+
const chars = [...text];
|
|
1496
|
+
for (let i = 0; i < chars.length; i++) {
|
|
1497
|
+
const ch = chars[i];
|
|
1498
|
+
if (/\s/.test(ch)) continue;
|
|
1499
|
+
return {
|
|
1500
|
+
unit: ch === "\\" && chars[i + 1] !== void 0 ? ch + chars[i + 1] : ch,
|
|
1501
|
+
index: i
|
|
1502
|
+
};
|
|
1503
|
+
}
|
|
1504
|
+
return null;
|
|
1505
|
+
}
|
|
1506
|
+
/** The `startWithInitial` transform: finds the first syllable group with
|
|
1507
|
+
* actual notes and text (the clef, no-notes groups, control statements,
|
|
1508
|
+
* text-less groups and `~` empty slots don't count) and wraps the
|
|
1509
|
+
* syllable's first character in the `*…*` initial marker. Already-marked
|
|
1510
|
+
* text (leading `*`) is left alone. */
|
|
1511
|
+
function startWithInitial(source) {
|
|
1512
|
+
const clefMatch = source.match(/^\s*\(([^)]*)\)/);
|
|
1513
|
+
const offset = clefMatch ? clefMatch[0].length : 0;
|
|
1514
|
+
const rest = source.slice(offset);
|
|
1515
|
+
for (const match of rest.matchAll(/((?:[^()\\]|\\.)*)\(((?:[^)\\]|\\.)*)\)/g)) {
|
|
1516
|
+
const [, text, notes] = match;
|
|
1517
|
+
if (unescape(notes).trim().length === 0) continue;
|
|
1518
|
+
if (isStatementGroup(notes)) continue;
|
|
1519
|
+
const unit = firstCharUnit(text);
|
|
1520
|
+
if (!unit || text.trim() === "~") continue;
|
|
1521
|
+
if (text.trimStart().startsWith("*")) return source;
|
|
1522
|
+
const unitStart = offset + match.index + unit.index;
|
|
1523
|
+
const unitEnd = unitStart + unit.unit.length;
|
|
1524
|
+
return `${source.slice(0, unitStart)}*${source.slice(unitStart, unitEnd)}*${source.slice(unitEnd)}`;
|
|
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
|
+
}
|
|
1574
|
+
}
|
|
1575
|
+
/** Transposes the pitch characters of one note group's content as
|
|
1576
|
+
* written in the source (escapes included) — the staff-degree shift
|
|
1577
|
+
* `transposeBy` applies to every group. Escape pairs stand for their
|
|
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
|
+
};
|
|
1616
|
+
}
|
|
1617
|
+
//#endregion
|
|
1618
|
+
//#region src/guidoStyles.ts
|
|
1619
|
+
/**
|
|
1620
|
+
* Style sets for the renderer: named appearance rules (fonts, colors, and
|
|
1621
|
+
* the special syllable decorations) kept in one constant registry, so a
|
|
1622
|
+
* new style is a data entry here rather than a renderer fork.
|
|
1623
|
+
*/
|
|
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
|
+
/** Text characters (* and †) rendered in Times New Roman. */
|
|
1631
|
+
var TIMES_MARKER_GLYPHS = {
|
|
1632
|
+
chars: MARKER_CHARS,
|
|
1633
|
+
fontFamily: "'Times New Roman', Times, serif"
|
|
1634
|
+
};
|
|
1635
|
+
/** How a `+` inside a Liturgy-font run is drawn: normal weight (the font
|
|
1636
|
+
* is bold-looking by design), sized to 70% and raised 20% of the text
|
|
1637
|
+
* size so it sits like a connecting sign between syllable parts. */
|
|
1638
|
+
var LITURGY_PLUS_STYLE = {
|
|
1639
|
+
weight: "400",
|
|
1640
|
+
scale: .7,
|
|
1641
|
+
raise: .2
|
|
1642
|
+
};
|
|
1643
|
+
var STYLE_SETS = {
|
|
1644
|
+
/** Book Antiqua text with red accents. */
|
|
1645
|
+
BNS: {
|
|
1646
|
+
textFontStack: "'Book Antiqua', Georgia, serif",
|
|
1647
|
+
notesFontStack: "'Guidohu'",
|
|
1648
|
+
markerGlyphs: TIMES_MARKER_GLYPHS,
|
|
1649
|
+
verifiedFonts: [
|
|
1650
|
+
{
|
|
1651
|
+
family: "'Book Antiqua'",
|
|
1652
|
+
weight: "400"
|
|
1653
|
+
},
|
|
1654
|
+
{
|
|
1655
|
+
family: "'Book Antiqua'",
|
|
1656
|
+
weight: "700"
|
|
1657
|
+
},
|
|
1658
|
+
{
|
|
1659
|
+
family: "'Guidohu'",
|
|
1660
|
+
weight: "400"
|
|
1661
|
+
},
|
|
1662
|
+
{
|
|
1663
|
+
family: "'Liturgy'",
|
|
1664
|
+
weight: "700"
|
|
1665
|
+
}
|
|
1666
|
+
],
|
|
1667
|
+
decorations: {
|
|
1668
|
+
__: {
|
|
1669
|
+
fontFamily: "'Liturgy', serif",
|
|
1670
|
+
weight: "700",
|
|
1671
|
+
color: "var(--red)"
|
|
1672
|
+
},
|
|
1673
|
+
_: {
|
|
1674
|
+
uppercase: true,
|
|
1675
|
+
scale: .7,
|
|
1676
|
+
color: "var(--red)"
|
|
1677
|
+
},
|
|
1678
|
+
"*": {
|
|
1679
|
+
weight: "700",
|
|
1680
|
+
color: "var(--red)",
|
|
1681
|
+
initial: true
|
|
1682
|
+
}
|
|
1683
|
+
},
|
|
1684
|
+
specialStarStyle: {
|
|
1685
|
+
fontFamily: TIMES_MARKER_GLYPHS.fontFamily,
|
|
1686
|
+
weight: "700",
|
|
1687
|
+
scale: 1,
|
|
1688
|
+
measureScale: .95,
|
|
1689
|
+
raise: .1,
|
|
1690
|
+
color: "var(--red)"
|
|
1691
|
+
},
|
|
1692
|
+
padding: 12,
|
|
1693
|
+
scriptTransforms: [SCRIPT_TRANSFORMS.startWithInitial]
|
|
1694
|
+
},
|
|
1695
|
+
/** Times New Roman text, strictly black and white. */
|
|
1696
|
+
Vesperale: {
|
|
1697
|
+
textFontStack: "'Times New Roman', Times, serif",
|
|
1698
|
+
notesFontStack: "'Guidohu'",
|
|
1699
|
+
verifiedFonts: [
|
|
1700
|
+
{
|
|
1701
|
+
family: "'Times New Roman', Times, serif",
|
|
1702
|
+
weight: "400"
|
|
1703
|
+
},
|
|
1704
|
+
{
|
|
1705
|
+
family: "'Times New Roman', Times, serif",
|
|
1706
|
+
weight: "700"
|
|
1707
|
+
},
|
|
1708
|
+
{
|
|
1709
|
+
family: "'Guidohu'",
|
|
1710
|
+
weight: "400"
|
|
1711
|
+
},
|
|
1712
|
+
{
|
|
1713
|
+
family: "'Liturgy'",
|
|
1714
|
+
weight: "700"
|
|
1715
|
+
}
|
|
1716
|
+
],
|
|
1717
|
+
decorations: {
|
|
1718
|
+
__: {
|
|
1719
|
+
fontFamily: "'Liturgy', serif",
|
|
1720
|
+
weight: "700"
|
|
1721
|
+
},
|
|
1722
|
+
_: {
|
|
1723
|
+
scale: .8,
|
|
1724
|
+
italic: true
|
|
1725
|
+
},
|
|
1726
|
+
"*": {
|
|
1727
|
+
weight: "700",
|
|
1728
|
+
initial: true
|
|
1729
|
+
}
|
|
1730
|
+
},
|
|
1731
|
+
specialStarStyle: {
|
|
1732
|
+
fontFamily: TIMES_MARKER_GLYPHS.fontFamily,
|
|
1733
|
+
weight: "700",
|
|
1734
|
+
scale: 1,
|
|
1735
|
+
measureScale: .95,
|
|
1736
|
+
raise: .2
|
|
1737
|
+
},
|
|
1738
|
+
padding: 12,
|
|
1739
|
+
scriptTransforms: []
|
|
1740
|
+
}
|
|
1741
|
+
};
|
|
1742
|
+
var DEFAULT_STYLE_SET = "BNS";
|
|
1743
|
+
/**
|
|
1744
|
+
* Resolves a syllable's raw text into the runs it renders as: the whole-
|
|
1745
|
+
* syllable `**` star renders as the bold ∗ operator, a `__…__` or `_…_`
|
|
1746
|
+
* wrapper (the same characters on both ends, content between) picks up
|
|
1747
|
+
* its decoration rule from the style set, and paired `*` markers wrap any
|
|
1748
|
+
* part of the syllable — even the whole of it — with the initial rule
|
|
1749
|
+
* (`*sam*ple` → "sam" in the initial style, "ple" normal; an unmatched
|
|
1750
|
+
* `*` renders literally), the `bold` option forces weight 700 onto every
|
|
1751
|
+
* run, and the marker characters (`*`, `†`) split off into runs of their
|
|
1752
|
+
* own — with the style set's `markerGlyphs` font when one is configured,
|
|
1753
|
+
* the text font otherwise (always the `marker` kind).
|
|
1754
|
+
* Inside a Liturgy-font run, `+` gets its own weight-400 runs — the font
|
|
1755
|
+
* is bold-looking by design, so the weight stays normal. The wrapper
|
|
1756
|
+
* characters themselves are dropped. Each run also carries `kind`, the
|
|
1757
|
+
* notation-fixed semantic of its markup (`liturgy`, `rubric`, `star`,
|
|
1758
|
+
* `marker`, `plus`) — the vocabulary the marked-up text export and the
|
|
1759
|
+
* renderer's run classes share, identical across style sets. `rawCase`
|
|
1760
|
+
* skips the BNS rubric uppercasing: uppercasing is presentation, so the
|
|
1761
|
+
* marked-up text export keeps the raw letter case (a consumer CSS applies
|
|
1762
|
+
* text-transform instead).
|
|
1763
|
+
*/
|
|
1764
|
+
function resolveSyllableParts(text, styleSet, bold, options = {}) {
|
|
1765
|
+
if (text.length === 0) return [];
|
|
1766
|
+
if (text === "**") return [{
|
|
1767
|
+
text: "∗",
|
|
1768
|
+
style: { ...styleSet.specialStarStyle },
|
|
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 = "";
|
|
1795
|
+
}
|
|
1796
|
+
segmentSpecs.push({
|
|
1797
|
+
text: m[1],
|
|
1798
|
+
marked: true
|
|
1799
|
+
});
|
|
1800
|
+
rest = rest.slice(m.index + m[0].length);
|
|
1801
|
+
}
|
|
1802
|
+
plain += rest;
|
|
1803
|
+
if (plain) segmentSpecs.push({
|
|
1804
|
+
text: plain,
|
|
1805
|
+
marked: false
|
|
1806
|
+
});
|
|
1807
|
+
const segments = segmentSpecs.map(({ text: segText, marked }) => ({
|
|
1808
|
+
text: segText,
|
|
1809
|
+
style: marked ? {
|
|
1810
|
+
...base,
|
|
1811
|
+
...initialRule,
|
|
1812
|
+
initial: true
|
|
1813
|
+
} : { ...base },
|
|
1814
|
+
kind: baseKind
|
|
1815
|
+
}));
|
|
1816
|
+
const glyphs = styleSet.markerGlyphs;
|
|
1817
|
+
const parts = [];
|
|
1818
|
+
for (const segment of segments) {
|
|
1819
|
+
if (segment.style.uppercase && !options.rawCase) segment.text = segment.text.toUpperCase();
|
|
1820
|
+
if (bold) segment.style = {
|
|
1821
|
+
...segment.style,
|
|
1822
|
+
weight: "700"
|
|
1823
|
+
};
|
|
1824
|
+
const chars = [...segment.text];
|
|
1825
|
+
const inLiturgy = segment.style.fontFamily?.includes("Liturgy") ?? false;
|
|
1826
|
+
const runKind = (ch) => {
|
|
1827
|
+
if (MARKER_CHARS.has(ch) || glyphs && glyphs.chars.has(ch)) return "marker";
|
|
1828
|
+
if (inLiturgy && ch === "+") return "plus";
|
|
1829
|
+
return "text";
|
|
1830
|
+
};
|
|
1831
|
+
if (chars.every((ch) => runKind(ch) === "text")) {
|
|
1832
|
+
parts.push({
|
|
1833
|
+
text: segment.text,
|
|
1834
|
+
style: { ...segment.style },
|
|
1835
|
+
kind: segment.kind
|
|
1836
|
+
});
|
|
1837
|
+
continue;
|
|
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 = "";
|
|
1864
|
+
};
|
|
1865
|
+
for (const ch of chars) {
|
|
1866
|
+
const kind = runKind(ch);
|
|
1867
|
+
if (kind !== currentKind) pushRun();
|
|
1868
|
+
currentKind = kind;
|
|
1869
|
+
charRun += ch;
|
|
1870
|
+
}
|
|
1871
|
+
pushRun();
|
|
1872
|
+
}
|
|
1873
|
+
return parts;
|
|
1874
|
+
}
|
|
1875
|
+
/** The canvas font shorthand for measuring a run at the text size. */
|
|
1876
|
+
function measurementFont(style, textPx, fallbackStack) {
|
|
1877
|
+
return [
|
|
1878
|
+
style.italic ? "italic" : "normal",
|
|
1879
|
+
"normal",
|
|
1880
|
+
style.weight ?? "400",
|
|
1881
|
+
`${textPx * (style.measureScale ?? style.scale ?? 1)}px`,
|
|
1882
|
+
style.fontFamily ?? fallbackStack
|
|
1883
|
+
].join(" ");
|
|
1884
|
+
}
|
|
1885
|
+
/** The inline CSS properties that draw a run, as dash-cased names for
|
|
1886
|
+
* CSSStyleDeclaration.setProperty. */
|
|
1887
|
+
function inlineStylesFor(style, textPx) {
|
|
1888
|
+
const styles = {};
|
|
1889
|
+
if (style.fontFamily) styles["font-family"] = style.fontFamily;
|
|
1890
|
+
if (style.weight) styles["font-weight"] = style.weight;
|
|
1891
|
+
if (style.italic) styles["font-style"] = "italic";
|
|
1892
|
+
if (style.scale !== void 0) styles["font-size"] = `${textPx * style.scale}px`;
|
|
1893
|
+
if (style.color) styles["color"] = style.color;
|
|
1894
|
+
if (style.raise) {
|
|
1895
|
+
styles["position"] = "relative";
|
|
1896
|
+
styles["top"] = `${-textPx * style.raise}px`;
|
|
1897
|
+
}
|
|
1898
|
+
return styles;
|
|
1899
|
+
}
|
|
1900
|
+
/** The font strings render() must load and verify for a style set. */
|
|
1901
|
+
function fontStringsToVerify(styleSet, textPx) {
|
|
1902
|
+
return styleSet.verifiedFonts.map((f) => `${f.weight} ${textPx}px ${f.family}`);
|
|
1903
|
+
}
|
|
1904
|
+
//#endregion
|
|
1905
|
+
//#region src/chantLayout.ts
|
|
1906
|
+
/**
|
|
1907
|
+
* Special reciting notes: when such a note stands alone in a note group it
|
|
1908
|
+
* marks recited text. The glyph shows at a run's first syllable (the
|
|
1909
|
+
* previous syllable has different notes) and at every line start; every
|
|
1910
|
+
* other recited syllable contributes only the staff-line segments filling
|
|
1911
|
+
* the space above its text, which flows tight after the previous text.
|
|
1912
|
+
*/
|
|
1913
|
+
var SPECIAL_NOTES = "Ÿ¡¢£[¥¦©ª«¬";
|
|
1914
|
+
var SPECIAL_NOTE_CHARS = new Set(SPECIAL_NOTES.split(""));
|
|
1915
|
+
/** Whether a note group is a recitation group: one special reciting note,
|
|
1916
|
+
* optionally after one accidental sign (`¦`, `C¦`). */
|
|
1917
|
+
function isRecitationGroup(notes) {
|
|
1918
|
+
return notes.length === 1 && SPECIAL_NOTE_CHARS.has(notes) || notes.length === 2 && MODIFIER_CHARS.has(notes[0]) && SPECIAL_NOTE_CHARS.has(notes[1]);
|
|
1919
|
+
}
|
|
1920
|
+
/**
|
|
1921
|
+
* Modifier glyphs: flats, sharps, and naturals. They carry pitch (and
|
|
1922
|
+
* transpose with the melody) but modify the note that follows them, so
|
|
1923
|
+
* they hang in the junction whitespace: the syllable text anchors on the
|
|
1924
|
+
* note characters only, and a group starting with a modifier needs only
|
|
1925
|
+
* two junction dashes — the glyph itself provides the third dash's
|
|
1926
|
+
* separation.
|
|
1927
|
+
*/
|
|
1928
|
+
var NOTE_MODIFIERS = "÷øaASÍYXCVBN$mÈÊËÌÎÏÐÑÒþßòôùý“”•–—˜™š›œº»";
|
|
1929
|
+
var MODIFIER_CHARS = new Set(NOTE_MODIFIERS.split(""));
|
|
1930
|
+
/**
|
|
1931
|
+
* The length of what hangs at the start of a group's core: its leading
|
|
1932
|
+
* modifiers and bracketed accidentals (issue #95), which stand in the
|
|
1933
|
+
* junction whitespace before the group's first note.
|
|
1934
|
+
*/
|
|
1935
|
+
function hangingLength(core) {
|
|
1936
|
+
let at = 0;
|
|
1937
|
+
for (;;) {
|
|
1938
|
+
const unit = MODIFIER_CHARS.has(core[at] ?? "") ? 1 : bracketedAccidentalLength(core, at);
|
|
1939
|
+
if (unit === 0) return at;
|
|
1940
|
+
at += unit;
|
|
1941
|
+
}
|
|
1942
|
+
}
|
|
1943
|
+
/** The index in `notes` right after its hanging run (leading markers
|
|
1944
|
+
* counted), or 0 when nothing hangs. */
|
|
1945
|
+
function hangEnd(notes) {
|
|
1946
|
+
const { leading, core } = splitNoteMarkers(notes);
|
|
1947
|
+
const hang = hangingLength(core);
|
|
1948
|
+
return hang === 0 ? 0 : leading.length + hang;
|
|
1949
|
+
}
|
|
1950
|
+
var NOTE_GROUP_SPLITTERS = ["-", "¨"];
|
|
1951
|
+
/** A junction `thirds` thirds of a dash wide: whole dashes, then one `¨`
|
|
1952
|
+
* per leftover third (`--¨¨` is 2⅔). */
|
|
1953
|
+
function junctionSpacer(thirds) {
|
|
1954
|
+
return "-".repeat(Math.floor(thirds / 3)) + "¨".repeat(thirds % 3);
|
|
1955
|
+
}
|
|
1956
|
+
/** The key written by a run of modifier glyphs, under `clef` (a clef
|
|
1957
|
+
* glyph; the standard g-clef when unknown — matching pitches by staff
|
|
1958
|
+
* position does not depend on it). A statement's key throws when two
|
|
1959
|
+
* glyphs alter the same pitch; the opening group's stays lenient (the
|
|
1960
|
+
* later glyph wins), as it was never validated. */
|
|
1961
|
+
function keyMapOf(key, clef, strict = true) {
|
|
1962
|
+
const map = /* @__PURE__ */ new Map();
|
|
1963
|
+
for (const ch of key) {
|
|
1964
|
+
if (!isModifierGlyph(ch)) continue;
|
|
1965
|
+
const letter = pitchLetterOf(ch, clefOrDefault(clef));
|
|
1966
|
+
if (strict && map.has(letter)) throw new Error(`Key "${key}" alters the pitch ${letter} twice`);
|
|
1967
|
+
map.set(letter, baseOf(ch).role);
|
|
1968
|
+
}
|
|
1969
|
+
return map;
|
|
1970
|
+
}
|
|
1971
|
+
function clefOrDefault(clef) {
|
|
1972
|
+
return clef !== void 0 && isClefGlyph(clef) ? clef : "M";
|
|
1973
|
+
}
|
|
1974
|
+
/** The note glyphs a key alters: the base pitch glyphs and their shape
|
|
1975
|
+
* variants. Accidentals and small points are not notes, and the score
|
|
1976
|
+
* markers carry no pitch (several double as cephalicus glyphs). */
|
|
1977
|
+
function noteBaseOf(ch) {
|
|
1978
|
+
if (MARKER_CHARS$1.has(ch)) return void 0;
|
|
1979
|
+
if (variantOf(ch, "smallHead") !== void 0) return ch;
|
|
1980
|
+
const entry = baseOf(ch);
|
|
1981
|
+
if (!entry || entry.role === "smallPoint" || isModifierGlyph(ch)) return void 0;
|
|
1982
|
+
return entry.base;
|
|
1983
|
+
}
|
|
1984
|
+
/**
|
|
1985
|
+
* Writes a mid-line key change into a note group (issue #49): every note
|
|
1986
|
+
* whose pitch carries a different accidental under `active` than under
|
|
1987
|
+
* `staff` (the key the line's staff shows) calls for the accidental
|
|
1988
|
+
* sign — or the natural, when the pitch dropped out of the key. The
|
|
1989
|
+
* signs stand before the note group, not before their notes: they go at
|
|
1990
|
+
* the start of the group's notes (after its leading markers), one per
|
|
1991
|
+
* affected pitch glyph, in order of first appearance. Notes the source
|
|
1992
|
+
* already marks with a modifier are left alone (a bracketed accidental
|
|
1993
|
+
* opening the group, issue #95, marks the note after it). The groups a
|
|
1994
|
+
* syllable holds (`held`, issue #83) are scanned too: a held run writes its signs
|
|
1995
|
+
* once, before its first group. Returns the rewritten group and the
|
|
1996
|
+
* indices of the inserted signs. Throws when a needed sign does not
|
|
1997
|
+
* exist.
|
|
1998
|
+
*/
|
|
1999
|
+
function applyKeyChange(notes, staff, active, clef, held = []) {
|
|
2000
|
+
let signs = "";
|
|
2001
|
+
for (const group of [notes, ...held]) {
|
|
2002
|
+
const hang = hangEnd(group);
|
|
2003
|
+
for (let i = 0; i < group.length; i++) {
|
|
2004
|
+
const ch = group[i];
|
|
2005
|
+
const base = noteBaseOf(ch);
|
|
2006
|
+
const letter = base !== void 0 ? pitchLetterOf(base, clefOrDefault(clef)) : void 0;
|
|
2007
|
+
const want = letter !== void 0 ? active.get(letter) : void 0;
|
|
2008
|
+
const marked = i > 0 && (isModifierGlyph(group[i - 1]) || i === hang);
|
|
2009
|
+
if (letter !== void 0 && want !== staff.get(letter) && !marked) {
|
|
2010
|
+
const role = want ?? "modifyNatural";
|
|
2011
|
+
const sign = want ? variantOf(base, want) : modifyNatural(base);
|
|
2012
|
+
if (sign === void 0) throw new Error(`The note "${ch}" has no ${role} glyph for the key change`);
|
|
2013
|
+
if (!signs.includes(sign)) signs += sign;
|
|
2014
|
+
}
|
|
2015
|
+
}
|
|
2016
|
+
}
|
|
2017
|
+
const at = splitNoteMarkers(notes).leading.length;
|
|
2018
|
+
return {
|
|
2019
|
+
notes: notes.slice(0, at) + signs + notes.slice(at),
|
|
2020
|
+
inserted: [...signs].map((_, k) => at + k)
|
|
2021
|
+
};
|
|
2022
|
+
}
|
|
2023
|
+
/** A group with the given inserted signs removed again. */
|
|
2024
|
+
function stripInserted(notes, inserted, from = 0) {
|
|
2025
|
+
return [...notes].filter((_, i) => !inserted.includes(i + from)).join("");
|
|
2026
|
+
}
|
|
2027
|
+
var ctx = null;
|
|
2028
|
+
/** Canvas text measurement, shared with the emitters (the Word export
|
|
2029
|
+
* measures the spacer character's natural advance for the unit width). */
|
|
2030
|
+
function measure(text, font) {
|
|
2031
|
+
if (!ctx) ctx = document.createElement("canvas").getContext("2d");
|
|
2032
|
+
ctx.font = font;
|
|
2033
|
+
return ctx.measureText(text).width;
|
|
2034
|
+
}
|
|
2035
|
+
/** Ink extents of the text relative to the glyph origin (negative left
|
|
2036
|
+
* means the ink starts left of the origin). */
|
|
2037
|
+
function ink(text, font) {
|
|
2038
|
+
if (!ctx) ctx = document.createElement("canvas").getContext("2d");
|
|
2039
|
+
ctx.font = font;
|
|
2040
|
+
const m = ctx.measureText(text);
|
|
2041
|
+
return {
|
|
2042
|
+
left: m.actualBoundingBoxLeft,
|
|
2043
|
+
right: m.actualBoundingBoxRight
|
|
2044
|
+
};
|
|
2045
|
+
}
|
|
2046
|
+
/** Each underlay's tokens, checked to share one melody. */
|
|
2047
|
+
function underlayTokens(source) {
|
|
2048
|
+
const list = typeof source === "string" || source instanceof GuidoScript ? [source] : source;
|
|
2049
|
+
if (list.length === 0) throw new Error("Nothing to lay out: no script given");
|
|
2050
|
+
const all = list.map((s) => (typeof s === "string" ? new GuidoScript(s) : s).tokens);
|
|
2051
|
+
const [first] = all;
|
|
2052
|
+
for (const [u, tokens] of all.entries()) if (!(tokens.length === first.length && tokens.every((tok, k) => {
|
|
2053
|
+
const ref = first[k];
|
|
2054
|
+
return tok.kind === ref.kind && (tok.kind !== "syllable" || tok.notes === ref.notes);
|
|
2055
|
+
}))) throw new Error(`Stacked underlay ${u + 1} does not share the first one's notes`);
|
|
2056
|
+
return all;
|
|
2057
|
+
}
|
|
2058
|
+
/** Whether an underlay's token sings no text of its own on its group: a
|
|
2059
|
+
* held group or an empty slot. */
|
|
2060
|
+
function silent(tok) {
|
|
2061
|
+
return !!(tok.held || tok.empty);
|
|
2062
|
+
}
|
|
2063
|
+
/**
|
|
2064
|
+
* Lays a GuidoScript score out: a notes line in the chant font, and a text
|
|
2065
|
+
* line where each syllable is centered below its notes. The dashes of the
|
|
2066
|
+
* chant font are pure whitespace: centering ignores them and happens under
|
|
2067
|
+
* the non-whitespace note characters only. When a syllable's text would
|
|
2068
|
+
* overlap the previous one, it starts right after it and the notes group is
|
|
2069
|
+
* padded with extra dashes until it covers the text.
|
|
2070
|
+
*
|
|
2071
|
+
* Stacked stanzas (issue #84): given several underlays of one melody, every
|
|
2072
|
+
* line pair gets one text line per underlay. Each text line keeps its own
|
|
2073
|
+
* cursor and runs the placement rules on its own (tight pairs, connectors,
|
|
2074
|
+
* hyphens, recitation flow); a group takes the most pad dashes any of them
|
|
2075
|
+
* asks for, so the notes spacing serves every text line, and a line breaks
|
|
2076
|
+
* wherever any of them has to. A group that every underlay holds joins its
|
|
2077
|
+
* run with the held join; one that some underlay sings takes the normal
|
|
2078
|
+
* junction.
|
|
2079
|
+
*
|
|
2080
|
+
* This is the shared layout pass behind every emitter: the DOM renderer
|
|
2081
|
+
* turns the returned lines into (notes div, text paragraph) pairs, and the
|
|
2082
|
+
* Word export turns them into fake-spaced Word HTML. Both consume the same
|
|
2083
|
+
* line breaks, notes parts, and syllable anchor positions.
|
|
2084
|
+
*
|
|
2085
|
+
* Appearance comes from a named style set (see guidoStyles.ts): the layout
|
|
2086
|
+
* options reference it by name, together with the bold option and the
|
|
2087
|
+
* text/notes sizes. Throws when the style set is unknown or one of its
|
|
2088
|
+
* fonts cannot be loaded.
|
|
2089
|
+
*/
|
|
2090
|
+
async function layoutScore(source, options = {}) {
|
|
2091
|
+
const breakBefore = /* @__PURE__ */ new Set();
|
|
2092
|
+
const wraps = /* @__PURE__ */ new Set();
|
|
2093
|
+
for (;;) {
|
|
2094
|
+
const { result, splitRuns } = await layoutPass(source, options, breakBefore);
|
|
2095
|
+
const open = splitRuns.filter((r) => !wraps.has(r.run));
|
|
2096
|
+
if (open.length === 0) return result;
|
|
2097
|
+
const split = open.reduce((a, b) => b.run < a.run ? b : a);
|
|
2098
|
+
if (breakBefore.has(split.run) || split.atLineStart) {
|
|
2099
|
+
wraps.add(split.run);
|
|
2100
|
+
if (split.nested) breakBefore.add(split.run);
|
|
2101
|
+
else breakBefore.delete(split.run);
|
|
2102
|
+
} else breakBefore.add(split.run);
|
|
2103
|
+
}
|
|
2104
|
+
}
|
|
2105
|
+
/** One pass of `layoutScore`: `breakBefore` lists the `(@keep)` runs (by
|
|
2106
|
+
* position in the script) that open a fresh line; `splitRuns` reports the
|
|
2107
|
+
* ones that crossed a line break. */
|
|
2108
|
+
async function layoutPass(source, options, breakBefore) {
|
|
2109
|
+
const { style: styleName = "BNS", bold = false, textPx = 18, notesPx = textPx * 2, maxWidthPx = Infinity, junctionDashes = 3 } = options;
|
|
2110
|
+
const junctionThirds = Math.round(junctionDashes * 3);
|
|
2111
|
+
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
|
+
const fullJunction = junctionSpacer(junctionThirds);
|
|
2113
|
+
const reducedJunction = junctionSpacer(Math.max(3, junctionThirds - 3));
|
|
2114
|
+
const underlays = underlayTokens(source);
|
|
2115
|
+
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 = STYLE_SETS[styleName];
|
|
2117
|
+
if (!styleSet) throw new Error(`Unknown style set: "${styleName}"`);
|
|
2118
|
+
for (const font of fontStringsToVerify(styleSet, textPx)) {
|
|
2119
|
+
try {
|
|
2120
|
+
await document.fonts.load(font);
|
|
2121
|
+
} catch {}
|
|
2122
|
+
if (!document.fonts.check(font)) throw new Error(`Font could not be loaded: ${font} (style "${styleName}")`);
|
|
2123
|
+
}
|
|
2124
|
+
const notesFont = `400 ${notesPx}px ${styleSet.notesFontStack}`;
|
|
2125
|
+
const textStack = styleSet.textFontStack;
|
|
2126
|
+
const spaceFont = measurementFont({ weight: bold ? "700" : "400" }, textPx, textStack);
|
|
2127
|
+
const resolveParts = (spanText) => resolveSyllableParts(spanText, styleSet, bold);
|
|
2128
|
+
const measureParts = (parts) => parts.reduce((w, part) => w + measure(part.text, measurementFont(part.style, textPx, textStack)), 0);
|
|
2129
|
+
const dashNotesWidth = measure("-", notesFont);
|
|
2130
|
+
const dashTextWidth = measure("-", spaceFont);
|
|
2131
|
+
let clefGlyph = script.clef?.[0];
|
|
2132
|
+
let keyGlyphs = [...(script.clef ?? "").slice(1)].filter(isModifierGlyph).join("");
|
|
2133
|
+
let clefGroup = script.clef;
|
|
2134
|
+
const prefixOf = (group) => group ? `${group}-` : "";
|
|
2135
|
+
let activeKey = keyMapOf(keyGlyphs, clefGlyph, false);
|
|
2136
|
+
let staffKey = activeKey;
|
|
2137
|
+
const tracks = underlays.map((tokens) => {
|
|
2138
|
+
const syls = tokens.filter((t) => t.kind === "syllable");
|
|
2139
|
+
return {
|
|
2140
|
+
syls,
|
|
2141
|
+
lastTextSyl: syls.reduce((last, s, i) => silent(s) ? last : i, -1),
|
|
2142
|
+
cursor: 0,
|
|
2143
|
+
pending: [],
|
|
2144
|
+
prevWordEnd: void 0
|
|
2145
|
+
};
|
|
2146
|
+
});
|
|
2147
|
+
const lines = [];
|
|
2148
|
+
let line;
|
|
2149
|
+
let notesSoFar;
|
|
2150
|
+
let lastGroupTrailingMarker;
|
|
2151
|
+
let afterInlineClef = false;
|
|
2152
|
+
let current;
|
|
2153
|
+
let prevRecited = false;
|
|
2154
|
+
const startLine = () => {
|
|
2155
|
+
if (line) {
|
|
2156
|
+
tracks.forEach((t, u) => {
|
|
2157
|
+
line.texts[u].textCursor = t.cursor;
|
|
2158
|
+
});
|
|
2159
|
+
line.notesEnd = line.notesParts.length ? measure(line.clefPrefix + line.notesParts.join(""), notesFont) : 0;
|
|
2160
|
+
}
|
|
2161
|
+
const texts = tracks.map(() => ({
|
|
2162
|
+
segments: [],
|
|
2163
|
+
textCursor: 0,
|
|
2164
|
+
lastWordEnd: null
|
|
2165
|
+
}));
|
|
2166
|
+
line = {
|
|
2167
|
+
clefPrefix: prefixOf(clefGroup),
|
|
2168
|
+
notesParts: [],
|
|
2169
|
+
segments: texts[0].segments,
|
|
2170
|
+
textCursor: 0,
|
|
2171
|
+
notesEnd: 0,
|
|
2172
|
+
lastWordEnd: null,
|
|
2173
|
+
texts
|
|
2174
|
+
};
|
|
2175
|
+
lines.push(line);
|
|
2176
|
+
notesSoFar = line.clefPrefix;
|
|
2177
|
+
for (const t of tracks) t.cursor = 0;
|
|
2178
|
+
lastGroupTrailingMarker = false;
|
|
2179
|
+
prevRecited = false;
|
|
2180
|
+
afterInlineClef = false;
|
|
2181
|
+
staffKey = activeKey;
|
|
2182
|
+
if (current && current.inserted.length > 0) {
|
|
2183
|
+
current.notes = stripInserted(current.notes, current.inserted);
|
|
2184
|
+
current.inserted = [];
|
|
2185
|
+
if (current.first) placedNotes[current.i] = current.notes;
|
|
2186
|
+
}
|
|
2187
|
+
};
|
|
2188
|
+
startLine();
|
|
2189
|
+
const tokens = underlays[0];
|
|
2190
|
+
const allHeld = (i) => tracks.every((t) => t.syls[i].held);
|
|
2191
|
+
const items = [];
|
|
2192
|
+
let sylIndex = 0;
|
|
2193
|
+
let runHead;
|
|
2194
|
+
let tightDepth = 0;
|
|
2195
|
+
let tightGroupPlaced = false;
|
|
2196
|
+
for (const tok of tokens) if (tok.kind !== "syllable") {
|
|
2197
|
+
items.push(tok);
|
|
2198
|
+
runHead = void 0;
|
|
2199
|
+
if (tok.kind === "tight" || tok.kind === "unit") {
|
|
2200
|
+
tightDepth = Math.max(0, tightDepth + (tok.on ? 1 : -1));
|
|
2201
|
+
if (tightDepth === 0) tightGroupPlaced = false;
|
|
2202
|
+
}
|
|
2203
|
+
} else {
|
|
2204
|
+
const item = {
|
|
2205
|
+
kind: "syl",
|
|
2206
|
+
syl: tok,
|
|
2207
|
+
i: sylIndex++,
|
|
2208
|
+
notes: tok.notes,
|
|
2209
|
+
inserted: [],
|
|
2210
|
+
first: true,
|
|
2211
|
+
lastSeg: true,
|
|
2212
|
+
heldRun: [],
|
|
2213
|
+
signedByRun: false,
|
|
2214
|
+
tightJoin: tightGroupPlaced
|
|
2215
|
+
};
|
|
2216
|
+
if (tok.notes !== "") tightGroupPlaced = tightDepth > 0;
|
|
2217
|
+
if (allHeld(item.i) && runHead) {
|
|
2218
|
+
runHead.heldRun.push(tok.notes);
|
|
2219
|
+
item.signedByRun = true;
|
|
2220
|
+
} else runHead = item;
|
|
2221
|
+
items.push(item);
|
|
2222
|
+
}
|
|
2223
|
+
const placedNotes = [];
|
|
2224
|
+
const findBreakCut = (notes, room, measureFromLineStart) => {
|
|
2225
|
+
const first = hangEnd(notes);
|
|
2226
|
+
for (let c = notes.length - 1; c >= first && c > 0; c--) {
|
|
2227
|
+
if (!NOTE_GROUP_SPLITTERS.includes(notes[c])) continue;
|
|
2228
|
+
let end = c + 1;
|
|
2229
|
+
while (end < notes.length && MARKER_CHARS$1.has(notes[end])) end++;
|
|
2230
|
+
if (end >= notes.length) continue;
|
|
2231
|
+
if (measureFromLineStart(notes.slice(0, end)) <= room) return c;
|
|
2232
|
+
}
|
|
2233
|
+
return -1;
|
|
2234
|
+
};
|
|
2235
|
+
const applyBreakCut = (item, cut) => {
|
|
2236
|
+
const notes = item.notes;
|
|
2237
|
+
let restStart = cut + 1;
|
|
2238
|
+
while (restStart < notes.length && MARKER_CHARS$1.has(notes[restStart])) restStart++;
|
|
2239
|
+
item.notes = notes.slice(0, cut) + notes.slice(cut + 1, restStart);
|
|
2240
|
+
const rest = stripInserted(notes.slice(restStart), item.inserted, restStart);
|
|
2241
|
+
item.inserted = item.inserted.filter((at) => at < cut);
|
|
2242
|
+
return rest;
|
|
2243
|
+
};
|
|
2244
|
+
const queueContinuation = (item, idx, rest) => {
|
|
2245
|
+
item.lastSeg = false;
|
|
2246
|
+
items.splice(idx + 1, 0, {
|
|
2247
|
+
kind: "syl",
|
|
2248
|
+
syl: item.syl,
|
|
2249
|
+
i: item.i,
|
|
2250
|
+
notes: rest,
|
|
2251
|
+
inserted: [],
|
|
2252
|
+
first: false,
|
|
2253
|
+
lastSeg: true,
|
|
2254
|
+
heldRun: [],
|
|
2255
|
+
signedByRun: false,
|
|
2256
|
+
tightJoin: false
|
|
2257
|
+
});
|
|
2258
|
+
forceBreak = true;
|
|
2259
|
+
};
|
|
2260
|
+
const flushPending = (trailing) => {
|
|
2261
|
+
tracks.forEach((t, u) => {
|
|
2262
|
+
while (t.pending.length > 0) {
|
|
2263
|
+
const p = t.pending[0];
|
|
2264
|
+
if (t.cursor > 0 && t.cursor + p.textWidth + (p.wordEnd ? 0 : dashTextWidth) > maxWidthPx) {
|
|
2265
|
+
startLine();
|
|
2266
|
+
continue;
|
|
2267
|
+
}
|
|
2268
|
+
t.pending.shift();
|
|
2269
|
+
const text = line.texts[u];
|
|
2270
|
+
text.segments.push({
|
|
2271
|
+
kind: "syl",
|
|
2272
|
+
parts: p.sylParts,
|
|
2273
|
+
textStart: t.cursor,
|
|
2274
|
+
textWidth: p.textWidth,
|
|
2275
|
+
contentWidth: 0,
|
|
2276
|
+
contentStart: 0,
|
|
2277
|
+
extraDashes: 0,
|
|
2278
|
+
tight: false,
|
|
2279
|
+
noNotes: true,
|
|
2280
|
+
...trailing && t.pending.length === 0 ? { trailing: true } : {}
|
|
2281
|
+
});
|
|
2282
|
+
t.cursor += p.textWidth;
|
|
2283
|
+
if (p.spaceWidth > 0) {
|
|
2284
|
+
text.segments.push({
|
|
2285
|
+
kind: "word-space",
|
|
2286
|
+
width: p.spaceWidth
|
|
2287
|
+
});
|
|
2288
|
+
t.cursor += p.spaceWidth;
|
|
2289
|
+
}
|
|
2290
|
+
text.lastWordEnd = p.wordEnd;
|
|
2291
|
+
text.textCursor = t.cursor;
|
|
2292
|
+
}
|
|
2293
|
+
});
|
|
2294
|
+
};
|
|
2295
|
+
let forceBreak = false;
|
|
2296
|
+
let keepRuns = 0;
|
|
2297
|
+
const keepStack = [];
|
|
2298
|
+
const splitRuns = [];
|
|
2299
|
+
const closeKeepRun = () => {
|
|
2300
|
+
const run = keepStack.pop();
|
|
2301
|
+
if (run && lines.length - 1 > run.line) splitRuns.push(run);
|
|
2302
|
+
};
|
|
2303
|
+
let idx = 0;
|
|
2304
|
+
while (idx < items.length) {
|
|
2305
|
+
const item = items[idx];
|
|
2306
|
+
if (item.kind === "break") {
|
|
2307
|
+
if (line.texts.some((x) => x.segments.length > 0) || tracks.some((t) => t.pending.length > 0)) {
|
|
2308
|
+
flushPending(false);
|
|
2309
|
+
forceBreak = true;
|
|
2310
|
+
}
|
|
2311
|
+
idx++;
|
|
2312
|
+
continue;
|
|
2313
|
+
}
|
|
2314
|
+
if (item.kind === "tight") {
|
|
2315
|
+
idx++;
|
|
2316
|
+
continue;
|
|
2317
|
+
}
|
|
2318
|
+
if (item.kind === "keep" || item.kind === "unit") {
|
|
2319
|
+
if (!item.on) closeKeepRun();
|
|
2320
|
+
else {
|
|
2321
|
+
const hasContent = line.texts.some((x) => x.segments.length > 0) || tracks.some((t) => t.pending.length > 0);
|
|
2322
|
+
if (breakBefore.has(keepRuns) && hasContent) {
|
|
2323
|
+
flushPending(false);
|
|
2324
|
+
forceBreak = true;
|
|
2325
|
+
}
|
|
2326
|
+
if (keepStack.length > 0) keepStack[keepStack.length - 1].nested = true;
|
|
2327
|
+
keepStack.push({
|
|
2328
|
+
run: keepRuns++,
|
|
2329
|
+
line: forceBreak ? lines.length : lines.length - 1,
|
|
2330
|
+
atLineStart: !hasContent || forceBreak,
|
|
2331
|
+
nested: false
|
|
2332
|
+
});
|
|
2333
|
+
}
|
|
2334
|
+
idx++;
|
|
2335
|
+
continue;
|
|
2336
|
+
}
|
|
2337
|
+
if (item.kind === "clef" || item.kind === "key") {
|
|
2338
|
+
if (item.kind === "clef") clefGlyph = item.clef;
|
|
2339
|
+
keyGlyphs = item.key;
|
|
2340
|
+
clefGroup = (clefGlyph ?? "") + keyGlyphs;
|
|
2341
|
+
activeKey = keyMapOf(keyGlyphs, clefGlyph);
|
|
2342
|
+
if (forceBreak) {} else if (line.notesParts.length === 0) {
|
|
2343
|
+
line.clefPrefix = prefixOf(clefGroup);
|
|
2344
|
+
notesSoFar = line.clefPrefix;
|
|
2345
|
+
staffKey = activeKey;
|
|
2346
|
+
} else if (item.kind === "clef") {
|
|
2347
|
+
const part = (lastGroupTrailingMarker ? reducedJunction : fullJunction) + clefGroup + "-";
|
|
2348
|
+
if (measure(notesSoFar + part, notesFont) > maxWidthPx) forceBreak = true;
|
|
2349
|
+
else {
|
|
2350
|
+
line.notesParts.push(part);
|
|
2351
|
+
notesSoFar += part;
|
|
2352
|
+
staffKey = activeKey;
|
|
2353
|
+
afterInlineClef = true;
|
|
2354
|
+
lastGroupTrailingMarker = false;
|
|
2355
|
+
prevRecited = false;
|
|
2356
|
+
}
|
|
2357
|
+
}
|
|
2358
|
+
idx++;
|
|
2359
|
+
continue;
|
|
2360
|
+
}
|
|
2361
|
+
const syl = item.syl;
|
|
2362
|
+
const i = item.i;
|
|
2363
|
+
if (forceBreak) {
|
|
2364
|
+
startLine();
|
|
2365
|
+
forceBreak = false;
|
|
2366
|
+
}
|
|
2367
|
+
current = item;
|
|
2368
|
+
if (staffKey !== activeKey && !item.signedByRun) {
|
|
2369
|
+
const changed = applyKeyChange(item.notes, staffKey, activeKey, clefGlyph, item.first ? item.heldRun : []);
|
|
2370
|
+
item.notes = changed.notes;
|
|
2371
|
+
item.inserted = changed.inserted;
|
|
2372
|
+
}
|
|
2373
|
+
if (item.first) placedNotes[i] = item.signedByRun && staffKey !== activeKey ? applyKeyChange(item.notes, staffKey, activeKey, clefGlyph).notes : item.notes;
|
|
2374
|
+
const sung = tracks.map((t) => {
|
|
2375
|
+
const own = t.syls[i];
|
|
2376
|
+
const last = item.lastSeg && i >= t.lastTextSyl;
|
|
2377
|
+
const sylParts = resolveParts(item.first ? own.text : "");
|
|
2378
|
+
return {
|
|
2379
|
+
t,
|
|
2380
|
+
own,
|
|
2381
|
+
sylParts,
|
|
2382
|
+
textWidth: measureParts(sylParts),
|
|
2383
|
+
spaceWidth: !last && own.wordEnd ? measure(" ", spaceFont) : 0,
|
|
2384
|
+
pendingTotal: t.pending.reduce((s, p) => s + p.textWidth + p.spaceWidth, 0)
|
|
2385
|
+
};
|
|
2386
|
+
});
|
|
2387
|
+
const special = isRecitationGroup(item.notes);
|
|
2388
|
+
if (syl.empty && item.notes.length === 0) {
|
|
2389
|
+
idx++;
|
|
2390
|
+
continue;
|
|
2391
|
+
}
|
|
2392
|
+
if (item.notes.length === 0) {
|
|
2393
|
+
for (const s of sung) {
|
|
2394
|
+
if (s.own.empty) continue;
|
|
2395
|
+
s.t.pending.push({
|
|
2396
|
+
sylParts: s.sylParts,
|
|
2397
|
+
textWidth: s.textWidth,
|
|
2398
|
+
spaceWidth: s.spaceWidth,
|
|
2399
|
+
wordEnd: s.own.wordEnd
|
|
2400
|
+
});
|
|
2401
|
+
}
|
|
2402
|
+
idx++;
|
|
2403
|
+
continue;
|
|
2404
|
+
}
|
|
2405
|
+
if (item.first && item.lastSeg && !syl.empty && syl.text.trim() === "" && splitNoteMarkers(item.notes).core === "") {
|
|
2406
|
+
const last = notesSoFar[notesSoFar.length - 1];
|
|
2407
|
+
const sep = last && last !== "-" && !lastGroupTrailingMarker && !syl.notes.startsWith("-") ? "-" : "";
|
|
2408
|
+
line.notesParts.push(sep + syl.notes);
|
|
2409
|
+
notesSoFar += sep + syl.notes;
|
|
2410
|
+
lastGroupTrailingMarker = true;
|
|
2411
|
+
prevRecited = false;
|
|
2412
|
+
for (const s of sung) s.t.prevWordEnd = s.own.wordEnd;
|
|
2413
|
+
idx++;
|
|
2414
|
+
continue;
|
|
2415
|
+
}
|
|
2416
|
+
const keepWordState = (s, fresh) => {
|
|
2417
|
+
if (s.own.held) {
|
|
2418
|
+
if (fresh) line.texts[tracks.indexOf(s.t)].lastWordEnd = true;
|
|
2419
|
+
s.t.prevWordEnd = s.own.wordEnd;
|
|
2420
|
+
} else if (s.t.prevWordEnd === void 0) s.t.prevWordEnd = true;
|
|
2421
|
+
};
|
|
2422
|
+
if (item.first && sung.every((s) => silent(s.own))) {
|
|
2423
|
+
const held = allHeld(i);
|
|
2424
|
+
const recitation = isRecitationGroup(item.notes);
|
|
2425
|
+
if (held && recitation && placedNotes[i - 1] === placedNotes[i] && line.notesParts.length > 0 && !afterInlineClef) {
|
|
2426
|
+
prevRecited = true;
|
|
2427
|
+
idx++;
|
|
2428
|
+
continue;
|
|
2429
|
+
}
|
|
2430
|
+
let parts = splitNoteMarkers(item.notes);
|
|
2431
|
+
const joinOf = () => line.notesParts.length === 0 || afterInlineClef ? "" : held ? lastGroupTrailingMarker ? "--" : "-" : lastGroupTrailingMarker || item.tightJoin || hangingLength(parts.core) > 0 ? reducedJunction : fullJunction;
|
|
2432
|
+
let join = joinOf();
|
|
2433
|
+
if (line.notesParts.length > 0 && measure(notesSoFar + join + item.notes, notesFont) > maxWidthPx) {
|
|
2434
|
+
startLine();
|
|
2435
|
+
join = joinOf();
|
|
2436
|
+
}
|
|
2437
|
+
const fresh = line.notesParts.length === 0;
|
|
2438
|
+
if (fresh && measure(notesSoFar + item.notes, notesFont) > maxWidthPx) {
|
|
2439
|
+
const cut = findBreakCut(item.notes, maxWidthPx, (prefix) => measure(notesSoFar + prefix, notesFont));
|
|
2440
|
+
if (cut >= 0 && cut + 1 < item.notes.length) {
|
|
2441
|
+
const rest = applyBreakCut(item, cut);
|
|
2442
|
+
queueContinuation(item, idx, rest);
|
|
2443
|
+
parts = splitNoteMarkers(item.notes);
|
|
2444
|
+
}
|
|
2445
|
+
}
|
|
2446
|
+
line.notesParts.push(join + item.notes);
|
|
2447
|
+
notesSoFar += join + item.notes;
|
|
2448
|
+
for (const s of sung) keepWordState(s, fresh);
|
|
2449
|
+
afterInlineClef = false;
|
|
2450
|
+
if (item.lastSeg) {
|
|
2451
|
+
lastGroupTrailingMarker = parts.trailing.length > 0;
|
|
2452
|
+
prevRecited = recitation;
|
|
2453
|
+
}
|
|
2454
|
+
idx++;
|
|
2455
|
+
continue;
|
|
2456
|
+
}
|
|
2457
|
+
const voiced = item.first ? sung.filter((s) => !silent(s.own)) : sung;
|
|
2458
|
+
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 : dashTextWidth) <= maxWidthPx);
|
|
2460
|
+
if (special && !runStart && line.notesParts.length > 0 && !specialFits) startLine();
|
|
2461
|
+
if (!item.first) {
|
|
2462
|
+
for (;;) {
|
|
2463
|
+
if (measure(notesSoFar + item.notes, notesFont) <= maxWidthPx) break;
|
|
2464
|
+
const cut = findBreakCut(item.notes, maxWidthPx, (prefix) => measure(notesSoFar + prefix, notesFont));
|
|
2465
|
+
if (cut < 0 || cut + 1 >= item.notes.length) break;
|
|
2466
|
+
const rest = applyBreakCut(item, cut);
|
|
2467
|
+
queueContinuation(item, idx, rest);
|
|
2468
|
+
}
|
|
2469
|
+
const parts = splitNoteMarkers(item.notes);
|
|
2470
|
+
line.notesParts.push(item.notes);
|
|
2471
|
+
notesSoFar += item.notes;
|
|
2472
|
+
if (item.lastSeg) lastGroupTrailingMarker = parts.trailing.length > 0;
|
|
2473
|
+
for (const [u, s] of sung.entries()) {
|
|
2474
|
+
line.texts[u].lastWordEnd = true;
|
|
2475
|
+
s.t.prevWordEnd = s.own.wordEnd;
|
|
2476
|
+
}
|
|
2477
|
+
if (!item.lastSeg) forceBreak = true;
|
|
2478
|
+
idx++;
|
|
2479
|
+
continue;
|
|
2480
|
+
}
|
|
2481
|
+
let parts = splitNoteMarkers(item.notes);
|
|
2482
|
+
let junctionPrefix = line.notesParts.length === 0 || afterInlineClef ? "" : lastGroupTrailingMarker || item.tightJoin || hangingLength(parts.core) > 0 ? reducedJunction : fullJunction;
|
|
2483
|
+
if (prevRecited) {
|
|
2484
|
+
const closing = /-*$/.exec(notesSoFar)[0].length;
|
|
2485
|
+
junctionPrefix = special && !runStart || closing > /^-*/.exec(junctionPrefix)[0].length ? "" : junctionPrefix.slice(closing);
|
|
2486
|
+
}
|
|
2487
|
+
const place = (padDashes = 0) => {
|
|
2488
|
+
let mods = hangingLength(parts.core);
|
|
2489
|
+
if (mods === parts.core.length) mods = 0;
|
|
2490
|
+
const anchorText = parts.core.slice(mods);
|
|
2491
|
+
const contentWidth = measure(anchorText, notesFont);
|
|
2492
|
+
const contentStart = measure(notesSoFar + junctionPrefix + "-".repeat(padDashes) + parts.leading + parts.core.slice(0, mods), notesFont);
|
|
2493
|
+
const inkRaw = ink(anchorText, notesFont);
|
|
2494
|
+
const inkStartOffset = -inkRaw.left;
|
|
2495
|
+
const inkCenterOffset = (inkRaw.right - inkRaw.left) / 2;
|
|
2496
|
+
const textStartOf = (textWidth) => contentWidth >= textWidth || special ? contentStart + inkStartOffset : contentStart + inkCenterOffset - textWidth / 2;
|
|
2497
|
+
return {
|
|
2498
|
+
contentWidth,
|
|
2499
|
+
contentStart,
|
|
2500
|
+
textStartOf
|
|
2501
|
+
};
|
|
2502
|
+
};
|
|
2503
|
+
let placed = place();
|
|
2504
|
+
for (;;) {
|
|
2505
|
+
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 : dashTextWidth) > maxWidthPx);
|
|
2507
|
+
if (!notesOver && !textOver) break;
|
|
2508
|
+
if (notesOver) {
|
|
2509
|
+
const cut = findBreakCut(item.notes, maxWidthPx, (prefix) => measure(notesSoFar + junctionPrefix + prefix, notesFont));
|
|
2510
|
+
if (cut >= 0 && cut + 1 < item.notes.length) {
|
|
2511
|
+
const rest = applyBreakCut(item, cut);
|
|
2512
|
+
queueContinuation(item, idx, rest);
|
|
2513
|
+
parts = splitNoteMarkers(item.notes);
|
|
2514
|
+
placed = place();
|
|
2515
|
+
break;
|
|
2516
|
+
}
|
|
2517
|
+
}
|
|
2518
|
+
if (line.notesParts.length === 0 && tracks.every((t) => t.cursor === 0)) {
|
|
2519
|
+
if (notesOver) {
|
|
2520
|
+
const cut = findBreakCut(item.notes, maxWidthPx, (prefix) => measure(notesSoFar + prefix, notesFont));
|
|
2521
|
+
if (cut >= 0 && cut + 1 < item.notes.length) {
|
|
2522
|
+
const rest = applyBreakCut(item, cut);
|
|
2523
|
+
item.notes = item.notes.slice(0, cut);
|
|
2524
|
+
queueContinuation(item, idx, rest);
|
|
2525
|
+
parts = splitNoteMarkers(item.notes);
|
|
2526
|
+
placed = place();
|
|
2527
|
+
}
|
|
2528
|
+
}
|
|
2529
|
+
break;
|
|
2530
|
+
}
|
|
2531
|
+
startLine();
|
|
2532
|
+
junctionPrefix = "";
|
|
2533
|
+
parts = splitNoteMarkers(item.notes);
|
|
2534
|
+
placed = place();
|
|
2535
|
+
}
|
|
2536
|
+
const decide = (s) => {
|
|
2537
|
+
const { t, textWidth } = s;
|
|
2538
|
+
const { contentWidth } = placed;
|
|
2539
|
+
const connectorDash = line.notesParts.length > 0 && !t.prevWordEnd && s.pendingTotal === 0;
|
|
2540
|
+
const minStart = t.cursor + dashTextWidth;
|
|
2541
|
+
const minTextStart = t.cursor + s.pendingTotal + (prevRecited && connectorDash ? dashTextWidth : 0);
|
|
2542
|
+
let textStart = placed.textStartOf(textWidth);
|
|
2543
|
+
let contentStart = placed.contentStart;
|
|
2544
|
+
let pads = 0;
|
|
2545
|
+
let tight = false;
|
|
2546
|
+
if (!prevRecited && connectorDash && textStart < minStart && (textWidth > contentWidth || textStart < t.cursor)) {
|
|
2547
|
+
tight = true;
|
|
2548
|
+
textStart = t.cursor;
|
|
2549
|
+
const idealContentStart = contentWidth >= textWidth ? textStart : textStart + (textWidth - contentWidth) / 2;
|
|
2550
|
+
if (idealContentStart > contentStart) pads = Math.round((idealContentStart - contentStart) / dashNotesWidth);
|
|
2551
|
+
return {
|
|
2552
|
+
textStart,
|
|
2553
|
+
contentStart,
|
|
2554
|
+
pads,
|
|
2555
|
+
tight,
|
|
2556
|
+
floor: t.cursor,
|
|
2557
|
+
minStart
|
|
2558
|
+
};
|
|
2559
|
+
}
|
|
2560
|
+
if (textStart < minTextStart) {
|
|
2561
|
+
const idealContentStart = contentWidth >= textWidth ? minTextStart : minTextStart + (textWidth - contentWidth) / 2;
|
|
2562
|
+
if (idealContentStart > contentStart) {
|
|
2563
|
+
pads = Math.max(0, Math.floor((idealContentStart - contentStart) / dashNotesWidth));
|
|
2564
|
+
let padded = place(pads);
|
|
2565
|
+
while (padded.textStartOf(textWidth) < minTextStart) {
|
|
2566
|
+
pads++;
|
|
2567
|
+
padded = place(pads);
|
|
2568
|
+
}
|
|
2569
|
+
textStart = padded.textStartOf(textWidth);
|
|
2570
|
+
contentStart = padded.contentStart;
|
|
2571
|
+
}
|
|
2572
|
+
}
|
|
2573
|
+
return {
|
|
2574
|
+
textStart,
|
|
2575
|
+
contentStart,
|
|
2576
|
+
pads,
|
|
2577
|
+
tight,
|
|
2578
|
+
floor: minTextStart,
|
|
2579
|
+
minStart
|
|
2580
|
+
};
|
|
2581
|
+
};
|
|
2582
|
+
const hiddenSpecial = special && !runStart && line.notesParts.length > 0 && !afterInlineClef;
|
|
2583
|
+
let placements;
|
|
2584
|
+
let padDashes = 0;
|
|
2585
|
+
if (hiddenSpecial) {
|
|
2586
|
+
junctionPrefix = "";
|
|
2587
|
+
placements = voiced.map((s) => {
|
|
2588
|
+
const textStart = s.t.cursor + s.pendingTotal;
|
|
2589
|
+
return {
|
|
2590
|
+
textStart,
|
|
2591
|
+
contentStart: placed.contentStart,
|
|
2592
|
+
pads: 0,
|
|
2593
|
+
tight: line.notesParts.length > 0 && !s.t.prevWordEnd && s.pendingTotal === 0,
|
|
2594
|
+
floor: textStart,
|
|
2595
|
+
minStart: textStart
|
|
2596
|
+
};
|
|
2597
|
+
});
|
|
2598
|
+
const deficit = Math.max(...voiced.map((s, v) => placements[v].textStart + s.textWidth + s.spaceWidth)) - measure(notesSoFar, notesFont);
|
|
2599
|
+
const room = maxWidthPx - measure(notesSoFar + junctionPrefix, notesFont);
|
|
2600
|
+
const fill = deficit > 0 ? Math.min(Math.ceil(deficit / dashNotesWidth), Math.max(0, Math.floor(room / dashNotesWidth))) : 0;
|
|
2601
|
+
item.notes = "-".repeat(fill);
|
|
2602
|
+
item.inserted = [];
|
|
2603
|
+
parts = splitNoteMarkers(item.notes);
|
|
2604
|
+
} else {
|
|
2605
|
+
const settle = () => {
|
|
2606
|
+
placements = voiced.map(decide);
|
|
2607
|
+
padDashes = Math.max(0, ...placements.map((p) => p.pads));
|
|
2608
|
+
if (placements.some((p) => p.pads < padDashes)) {
|
|
2609
|
+
const padded = place(padDashes);
|
|
2610
|
+
placements = placements.map((p, v) => {
|
|
2611
|
+
if (p.pads === padDashes) return p;
|
|
2612
|
+
const textStart = Math.max(padded.textStartOf(voiced[v].textWidth), p.floor);
|
|
2613
|
+
return {
|
|
2614
|
+
...p,
|
|
2615
|
+
textStart,
|
|
2616
|
+
contentStart: padded.contentStart,
|
|
2617
|
+
pads: padDashes,
|
|
2618
|
+
tight: p.tight && textStart < p.minStart
|
|
2619
|
+
};
|
|
2620
|
+
});
|
|
2621
|
+
}
|
|
2622
|
+
};
|
|
2623
|
+
settle();
|
|
2624
|
+
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 : dashTextWidth) > maxWidthPx;
|
|
2626
|
+
}))) {
|
|
2627
|
+
startLine();
|
|
2628
|
+
junctionPrefix = "";
|
|
2629
|
+
parts = splitNoteMarkers(item.notes);
|
|
2630
|
+
placed = place();
|
|
2631
|
+
settle();
|
|
2632
|
+
}
|
|
2633
|
+
}
|
|
2634
|
+
const notesStr = junctionPrefix + "-".repeat(padDashes) + item.notes;
|
|
2635
|
+
const fresh = line.notesParts.length === 0;
|
|
2636
|
+
voiced.forEach((s, v) => {
|
|
2637
|
+
const { t, sylParts, textWidth, spaceWidth, pendingTotal } = s;
|
|
2638
|
+
const text = line.texts[tracks.indexOf(t)];
|
|
2639
|
+
const p = placements[v];
|
|
2640
|
+
let textStart = p.textStart;
|
|
2641
|
+
if (pendingTotal > 0 && textStart < t.cursor + pendingTotal) textStart = t.cursor + pendingTotal;
|
|
2642
|
+
const spacerWidth = Math.max(0, textStart - pendingTotal - t.cursor);
|
|
2643
|
+
if (fresh) text.segments.push({
|
|
2644
|
+
kind: "gap",
|
|
2645
|
+
role: "lead-in",
|
|
2646
|
+
width: spacerWidth,
|
|
2647
|
+
endPx: textStart - pendingTotal,
|
|
2648
|
+
textStartPx: textStart
|
|
2649
|
+
});
|
|
2650
|
+
else if (t.prevWordEnd) text.segments.push({
|
|
2651
|
+
kind: "gap",
|
|
2652
|
+
role: "word-gap",
|
|
2653
|
+
width: spacerWidth,
|
|
2654
|
+
endPx: textStart - pendingTotal,
|
|
2655
|
+
textStartPx: textStart
|
|
2656
|
+
});
|
|
2657
|
+
else if (pendingTotal === 0 && p.tight) {} else if (pendingTotal === 0) {
|
|
2658
|
+
const pad = Math.max(0, (spacerWidth - dashTextWidth) / 2);
|
|
2659
|
+
text.segments.push({
|
|
2660
|
+
kind: "connector",
|
|
2661
|
+
pad,
|
|
2662
|
+
gapWidth: spacerWidth,
|
|
2663
|
+
dashWidth: dashTextWidth,
|
|
2664
|
+
dashStartPx: t.cursor + pad,
|
|
2665
|
+
nextAnchorPx: textStart
|
|
2666
|
+
});
|
|
2667
|
+
}
|
|
2668
|
+
let pendingStart = textStart - pendingTotal;
|
|
2669
|
+
for (const pend of t.pending) {
|
|
2670
|
+
text.segments.push({
|
|
2671
|
+
kind: "syl",
|
|
2672
|
+
parts: pend.sylParts,
|
|
2673
|
+
textStart: pendingStart,
|
|
2674
|
+
textWidth: pend.textWidth,
|
|
2675
|
+
contentWidth: 0,
|
|
2676
|
+
contentStart: 0,
|
|
2677
|
+
extraDashes: 0,
|
|
2678
|
+
tight: false,
|
|
2679
|
+
noNotes: true
|
|
2680
|
+
});
|
|
2681
|
+
pendingStart += pend.textWidth;
|
|
2682
|
+
if (pend.spaceWidth > 0) {
|
|
2683
|
+
text.segments.push({
|
|
2684
|
+
kind: "word-space",
|
|
2685
|
+
width: pend.spaceWidth
|
|
2686
|
+
});
|
|
2687
|
+
pendingStart += pend.spaceWidth;
|
|
2688
|
+
}
|
|
2689
|
+
}
|
|
2690
|
+
t.pending = [];
|
|
2691
|
+
text.segments.push({
|
|
2692
|
+
kind: "syl",
|
|
2693
|
+
parts: sylParts,
|
|
2694
|
+
textStart,
|
|
2695
|
+
textWidth,
|
|
2696
|
+
contentWidth: placed.contentWidth,
|
|
2697
|
+
contentStart: p.contentStart,
|
|
2698
|
+
extraDashes: padDashes,
|
|
2699
|
+
tight: p.tight,
|
|
2700
|
+
noNotes: false
|
|
2701
|
+
});
|
|
2702
|
+
if (spaceWidth > 0) text.segments.push({
|
|
2703
|
+
kind: "word-space",
|
|
2704
|
+
width: spaceWidth
|
|
2705
|
+
});
|
|
2706
|
+
t.cursor = textStart + textWidth + spaceWidth;
|
|
2707
|
+
text.lastWordEnd = s.own.wordEnd;
|
|
2708
|
+
t.prevWordEnd = s.own.wordEnd;
|
|
2709
|
+
});
|
|
2710
|
+
for (const s of sung) if (!voiced.includes(s)) keepWordState(s, fresh);
|
|
2711
|
+
notesSoFar += notesStr;
|
|
2712
|
+
line.notesParts.push(notesStr);
|
|
2713
|
+
afterInlineClef = false;
|
|
2714
|
+
if (item.lastSeg) {
|
|
2715
|
+
lastGroupTrailingMarker = parts.trailing.length > 0;
|
|
2716
|
+
prevRecited = special;
|
|
2717
|
+
}
|
|
2718
|
+
if (!item.lastSeg) forceBreak = true;
|
|
2719
|
+
idx++;
|
|
2720
|
+
}
|
|
2721
|
+
flushPending(true);
|
|
2722
|
+
while (keepStack.length > 0) closeKeepRun();
|
|
2723
|
+
tracks.forEach((t, u) => {
|
|
2724
|
+
line.texts[u].textCursor = t.cursor;
|
|
2725
|
+
});
|
|
2726
|
+
line.notesEnd = line.notesParts.length ? measure(line.clefPrefix + line.notesParts.join(""), notesFont) : 0;
|
|
2727
|
+
lines.forEach((l, li) => {
|
|
2728
|
+
if (li === lines.length - 1) return;
|
|
2729
|
+
for (const text of l.texts) {
|
|
2730
|
+
if (text.lastWordEnd !== false) continue;
|
|
2731
|
+
const pad = l.notesEnd > text.textCursor ? Math.max(0, (l.notesEnd - text.textCursor - dashTextWidth) / 2) : 0;
|
|
2732
|
+
text.segments.push({
|
|
2733
|
+
kind: "line-end-dash",
|
|
2734
|
+
pad,
|
|
2735
|
+
dashWidth: dashTextWidth,
|
|
2736
|
+
dashStartPx: text.textCursor + pad,
|
|
2737
|
+
trailEndPx: l.notesEnd,
|
|
2738
|
+
textEndPx: text.textCursor
|
|
2739
|
+
});
|
|
2740
|
+
}
|
|
2741
|
+
});
|
|
2742
|
+
for (const l of lines) {
|
|
2743
|
+
l.textCursor = l.texts[0].textCursor;
|
|
2744
|
+
l.lastWordEnd = l.texts[0].lastWordEnd;
|
|
2745
|
+
}
|
|
2746
|
+
const starTarget = maxWidthPx + (styleSet.padding ?? 0);
|
|
2747
|
+
const starWidth = measure("*", notesFont);
|
|
2748
|
+
lines.forEach((l, li) => {
|
|
2749
|
+
if (li === lines.length - 1) return;
|
|
2750
|
+
if (maxWidthPx === Infinity) {
|
|
2751
|
+
l.breakMarkerLeftPx = styleSet.padding ?? 0;
|
|
2752
|
+
return;
|
|
2753
|
+
}
|
|
2754
|
+
let lineEnd = measure(l.clefPrefix + l.notesParts.join(""), notesFont);
|
|
2755
|
+
while (lineEnd + dashNotesWidth <= starTarget - starWidth && lineEnd + dashNotesWidth < maxWidthPx) {
|
|
2756
|
+
l.notesParts.push("-");
|
|
2757
|
+
lineEnd += dashNotesWidth;
|
|
2758
|
+
}
|
|
2759
|
+
l.breakMarkerLeftPx = starTarget - lineEnd - starWidth;
|
|
2760
|
+
});
|
|
2761
|
+
return {
|
|
2762
|
+
result: {
|
|
2763
|
+
lines,
|
|
2764
|
+
clefPrefix: lines[0].clefPrefix,
|
|
2765
|
+
styleSet,
|
|
2766
|
+
styleName,
|
|
2767
|
+
textPx,
|
|
2768
|
+
notesPx,
|
|
2769
|
+
bold,
|
|
2770
|
+
maxWidthPx,
|
|
2771
|
+
starTarget,
|
|
2772
|
+
starWidth
|
|
2773
|
+
},
|
|
2774
|
+
splitRuns
|
|
2775
|
+
};
|
|
2776
|
+
}
|
|
2777
|
+
//#endregion
|
|
2778
|
+
export { toVirga as $, stanzaWarnings as A, unescape as B, selectStanza as C, stackedStanzaNumbers as D, stackStanzas as E, chantText as F, modifyDown as G, baseOf as H, escape as I, toCephalicus as J, modifyNatural as K, runProperties as L, SCORE_MARKERS as M, splitNoteMarkers as N, stanzaCount as O, GuidoScript as P, toSmallPoint as Q, scriptSyllables as R, parseStanza as S, smallHeads as T, gClefPitchOf as U, CLEF_GLYPHS as V, isClefGlyph as W, toLonga as X, toConnectDown as Y, toSmallHead as Z, transposeExact as _, DEFAULT_STYLE_SET as a, isSlot as b, measurementFont as c, transposeBy as d, variantOf as et, transposeNotes as f, sameKeyGlyphs as g, parseInterval as h, measure as i, withStanza as j, stanzaTexts as k, resolveSyllableParts as l, TranspositionRangeError as m, SPECIAL_NOTES as n, STYLE_SETS as o, KEY_SIGNATURES as p, modifyUp as q, layoutScore as r, inlineStylesFor as s, NOTE_MODIFIERS as t, PITCH_ROWS as u, transposeScript as v, slotCount as w, padStanza as x, STANZA_PLACEHOLDER as y, statementSource as z };
|