vexflow-native 2.4.0 → 2.5.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (160) hide show
  1. package/README.md +7 -1
  2. package/lib/module/base/FontManager.js +41 -27
  3. package/lib/module/base/FontManager.js.map +1 -1
  4. package/lib/module/base/VexflowCanvas.js +8 -6
  5. package/lib/module/base/VexflowCanvas.js.map +1 -1
  6. package/lib/module/base/VexflowRecordingClone.js +83 -0
  7. package/lib/module/base/VexflowRecordingClone.js.map +1 -0
  8. package/lib/module/base/VexflowRecordingContext.js +1 -80
  9. package/lib/module/base/VexflowRecordingContext.js.map +1 -1
  10. package/lib/module/base/builtInTextFont.js +21 -0
  11. package/lib/module/base/builtInTextFont.js.map +1 -0
  12. package/lib/module/base/cssFontShorthand.js +34 -0
  13. package/lib/module/base/cssFontShorthand.js.map +1 -0
  14. package/lib/module/base/fonts/OFL.txt +93 -0
  15. package/lib/module/base/fonts/academico.js +12 -0
  16. package/lib/module/base/fonts/academico.js.map +1 -0
  17. package/lib/module/base/pictureRetirement.js +70 -0
  18. package/lib/module/base/pictureRetirement.js.map +1 -0
  19. package/lib/module/base/setupVexflowReactNative.js +1 -36
  20. package/lib/module/base/setupVexflowReactNative.js.map +1 -1
  21. package/lib/module/musicxml/MusicXmlParser/MeasureParser.js +22 -119
  22. package/lib/module/musicxml/MusicXmlParser/MeasureParser.js.map +1 -1
  23. package/lib/module/musicxml/MusicXmlParser/MusicXmlNotes.js +129 -0
  24. package/lib/module/musicxml/MusicXmlParser/MusicXmlNotes.js.map +1 -0
  25. package/lib/module/musicxml/MusicXmlParser/PartParser.js +8 -1
  26. package/lib/module/musicxml/MusicXmlParser/PartParser.js.map +1 -1
  27. package/lib/module/musicxml/MusicXmlParser/Spanners.js +8 -5
  28. package/lib/module/musicxml/MusicXmlParser/Spanners.js.map +1 -1
  29. package/lib/module/musicxml/MusicXmlParser/mappers.js +83 -3
  30. package/lib/module/musicxml/MusicXmlParser/mappers.js.map +1 -1
  31. package/lib/module/renderer/Layout/DocumentLayout.js +13 -4
  32. package/lib/module/renderer/Layout/DocumentLayout.js.map +1 -1
  33. package/lib/module/renderer/Layout/VerticalSpacing.js +37 -7
  34. package/lib/module/renderer/Layout/VerticalSpacing.js.map +1 -1
  35. package/lib/module/renderer/ScoreRenderer/ScoreOverlayPicture.js +80 -46
  36. package/lib/module/renderer/ScoreRenderer/ScoreOverlayPicture.js.map +1 -1
  37. package/lib/module/renderer/ScoreRenderer/ScoreRenderer.js +10 -22
  38. package/lib/module/renderer/ScoreRenderer/ScoreRenderer.js.map +1 -1
  39. package/lib/module/renderer/ScoreRenderer/ScoreScrollbar.js +18 -8
  40. package/lib/module/renderer/ScoreRenderer/ScoreScrollbar.js.map +1 -1
  41. package/lib/module/renderer/ScoreRenderer/utils.js +42 -88
  42. package/lib/module/renderer/ScoreRenderer/utils.js.map +1 -1
  43. package/lib/module/renderer/constants.js +1 -1
  44. package/lib/module/renderer/layout.js +7 -3
  45. package/lib/module/renderer/layout.js.map +1 -1
  46. package/lib/module/renderer/lyrics.js +127 -0
  47. package/lib/module/renderer/lyrics.js.map +1 -0
  48. package/lib/module/renderer/measure.js +11 -9
  49. package/lib/module/renderer/measure.js.map +1 -1
  50. package/lib/module/renderer/measureBounds.js +5 -0
  51. package/lib/module/renderer/measureBounds.js.map +1 -1
  52. package/lib/module/renderer/measureModifiers.js +24 -6
  53. package/lib/module/renderer/measureModifiers.js.map +1 -1
  54. package/lib/module/renderer/noteModifiers.js +5 -13
  55. package/lib/module/renderer/noteModifiers.js.map +1 -1
  56. package/lib/module/renderer/{fixedNoteSpacing.js → noteSpacing.js} +46 -10
  57. package/lib/module/renderer/noteSpacing.js.map +1 -0
  58. package/lib/module/renderer/render.js +37 -20
  59. package/lib/module/renderer/render.js.map +1 -1
  60. package/lib/typescript/src/base/FontManager.d.ts +12 -7
  61. package/lib/typescript/src/base/FontManager.d.ts.map +1 -1
  62. package/lib/typescript/src/base/VexflowCanvas.d.ts.map +1 -1
  63. package/lib/typescript/src/base/VexflowRecordingClone.d.ts +8 -0
  64. package/lib/typescript/src/base/VexflowRecordingClone.d.ts.map +1 -0
  65. package/lib/typescript/src/base/VexflowRecordingContext.d.ts.map +1 -1
  66. package/lib/typescript/src/base/builtInTextFont.d.ts +6 -0
  67. package/lib/typescript/src/base/builtInTextFont.d.ts.map +1 -0
  68. package/lib/typescript/src/base/cssFontShorthand.d.ts +3 -0
  69. package/lib/typescript/src/base/cssFontShorthand.d.ts.map +1 -0
  70. package/lib/typescript/src/base/fonts/academico.d.ts +5 -0
  71. package/lib/typescript/src/base/fonts/academico.d.ts.map +1 -0
  72. package/lib/typescript/src/base/pictureRetirement.d.ts +26 -0
  73. package/lib/typescript/src/base/pictureRetirement.d.ts.map +1 -0
  74. package/lib/typescript/src/base/setupVexflowReactNative.d.ts +0 -8
  75. package/lib/typescript/src/base/setupVexflowReactNative.d.ts.map +1 -1
  76. package/lib/typescript/src/base/types.d.ts.map +1 -1
  77. package/lib/typescript/src/musicxml/MusicXmlParser/InternalTypes.d.ts +4 -1
  78. package/lib/typescript/src/musicxml/MusicXmlParser/InternalTypes.d.ts.map +1 -1
  79. package/lib/typescript/src/musicxml/MusicXmlParser/MeasureParser.d.ts.map +1 -1
  80. package/lib/typescript/src/musicxml/MusicXmlParser/MusicXmlNotes.d.ts +13 -0
  81. package/lib/typescript/src/musicxml/MusicXmlParser/MusicXmlNotes.d.ts.map +1 -0
  82. package/lib/typescript/src/musicxml/MusicXmlParser/PartParser.d.ts.map +1 -1
  83. package/lib/typescript/src/musicxml/MusicXmlParser/Spanners.d.ts +6 -0
  84. package/lib/typescript/src/musicxml/MusicXmlParser/Spanners.d.ts.map +1 -1
  85. package/lib/typescript/src/musicxml/MusicXmlParser/mappers.d.ts +7 -1
  86. package/lib/typescript/src/musicxml/MusicXmlParser/mappers.d.ts.map +1 -1
  87. package/lib/typescript/src/renderer/Layout/DocumentLayout.d.ts.map +1 -1
  88. package/lib/typescript/src/renderer/Layout/VerticalSpacing.d.ts +6 -0
  89. package/lib/typescript/src/renderer/Layout/VerticalSpacing.d.ts.map +1 -1
  90. package/lib/typescript/src/renderer/ScoreRenderer/ScoreOverlayPicture.d.ts +1 -1
  91. package/lib/typescript/src/renderer/ScoreRenderer/ScoreOverlayPicture.d.ts.map +1 -1
  92. package/lib/typescript/src/renderer/ScoreRenderer/ScoreRenderer.d.ts.map +1 -1
  93. package/lib/typescript/src/renderer/ScoreRenderer/ScoreScrollbar.d.ts +1 -0
  94. package/lib/typescript/src/renderer/ScoreRenderer/ScoreScrollbar.d.ts.map +1 -1
  95. package/lib/typescript/src/renderer/ScoreRenderer/types.d.ts +14 -11
  96. package/lib/typescript/src/renderer/ScoreRenderer/types.d.ts.map +1 -1
  97. package/lib/typescript/src/renderer/ScoreRenderer/utils.d.ts +8 -27
  98. package/lib/typescript/src/renderer/ScoreRenderer/utils.d.ts.map +1 -1
  99. package/lib/typescript/src/renderer/layout.d.ts +4 -1
  100. package/lib/typescript/src/renderer/layout.d.ts.map +1 -1
  101. package/lib/typescript/src/renderer/lyrics.d.ts +42 -0
  102. package/lib/typescript/src/renderer/lyrics.d.ts.map +1 -0
  103. package/lib/typescript/src/renderer/measure.d.ts +2 -2
  104. package/lib/typescript/src/renderer/measure.d.ts.map +1 -1
  105. package/lib/typescript/src/renderer/measureBounds.d.ts +3 -0
  106. package/lib/typescript/src/renderer/measureBounds.d.ts.map +1 -1
  107. package/lib/typescript/src/renderer/measureModifiers.d.ts +14 -4
  108. package/lib/typescript/src/renderer/measureModifiers.d.ts.map +1 -1
  109. package/lib/typescript/src/renderer/noteModifiers.d.ts +2 -1
  110. package/lib/typescript/src/renderer/noteModifiers.d.ts.map +1 -1
  111. package/lib/typescript/src/renderer/{fixedNoteSpacing.d.ts → noteSpacing.d.ts} +9 -1
  112. package/lib/typescript/src/renderer/noteSpacing.d.ts.map +1 -0
  113. package/lib/typescript/src/renderer/render.d.ts.map +1 -1
  114. package/lib/typescript/src/renderer/types.d.ts +17 -7
  115. package/lib/typescript/src/renderer/types.d.ts.map +1 -1
  116. package/lib/typescript/src/state/types.d.ts +8 -2
  117. package/lib/typescript/src/state/types.d.ts.map +1 -1
  118. package/package.json +34 -26
  119. package/src/base/FontManager.ts +50 -31
  120. package/src/base/VexflowCanvas.tsx +10 -10
  121. package/src/base/VexflowRecordingClone.ts +89 -0
  122. package/src/base/VexflowRecordingContext.ts +8 -81
  123. package/src/base/builtInTextFont.ts +25 -0
  124. package/src/base/cssFontShorthand.ts +45 -0
  125. package/src/base/fonts/OFL.txt +93 -0
  126. package/src/base/fonts/academico.ts +10 -0
  127. package/src/base/pictureRetirement.ts +74 -0
  128. package/src/base/setupVexflowReactNative.ts +1 -49
  129. package/src/base/types.ts +0 -3
  130. package/src/musicxml/MusicXmlParser/InternalTypes.ts +5 -0
  131. package/src/musicxml/MusicXmlParser/MeasureParser.ts +31 -182
  132. package/src/musicxml/MusicXmlParser/MusicXmlNotes.ts +198 -0
  133. package/src/musicxml/MusicXmlParser/PartParser.ts +10 -3
  134. package/src/musicxml/MusicXmlParser/Spanners.ts +11 -8
  135. package/src/musicxml/MusicXmlParser/mappers.ts +55 -6
  136. package/src/renderer/Layout/DocumentLayout.ts +12 -3
  137. package/src/renderer/Layout/VerticalSpacing.ts +46 -13
  138. package/src/renderer/ScoreRenderer/ScoreOverlayPicture.tsx +127 -66
  139. package/src/renderer/ScoreRenderer/ScoreRenderer.tsx +8 -23
  140. package/src/renderer/ScoreRenderer/ScoreScrollbar.tsx +16 -2
  141. package/src/renderer/ScoreRenderer/types.ts +14 -12
  142. package/src/renderer/ScoreRenderer/utils.ts +54 -125
  143. package/src/renderer/constants.ts +1 -1
  144. package/src/renderer/layout.ts +15 -4
  145. package/src/renderer/lyrics.ts +163 -0
  146. package/src/renderer/measure.tsx +16 -9
  147. package/src/renderer/measureBounds.ts +10 -0
  148. package/src/renderer/measureModifiers.ts +27 -7
  149. package/src/renderer/noteModifiers.ts +8 -9
  150. package/src/renderer/{fixedNoteSpacing.ts → noteSpacing.ts} +60 -9
  151. package/src/renderer/render.tsx +51 -25
  152. package/src/renderer/types.ts +17 -7
  153. package/src/state/types.ts +8 -2
  154. package/lib/module/renderer/fixedNoteSpacing.js.map +0 -1
  155. package/lib/module/utils.js +0 -44
  156. package/lib/module/utils.js.map +0 -1
  157. package/lib/typescript/src/renderer/fixedNoteSpacing.d.ts.map +0 -1
  158. package/lib/typescript/src/utils.d.ts +0 -1
  159. package/lib/typescript/src/utils.d.ts.map +0 -1
  160. package/src/utils.tsx +0 -42
@@ -1,35 +1,21 @@
1
- import type {
2
- Chord,
3
- Direction,
4
- DurationValue,
5
- Measure,
6
- MeasureState,
7
- Pitch,
8
- Voice,
9
- VoiceItem,
10
- } from '../../state';
1
+ import type { Direction, Measure, MeasureState, Voice } from '../../state';
11
2
  import type { ParserState, StaffBuild } from './InternalTypes';
12
- import {
13
- mapBarline,
14
- mapClef,
15
- mapDurationType,
16
- mapKeySignature,
17
- mapPitch,
18
- mapStemDirection,
19
- } from './mappers';
3
+ import { mapBarline, mapClef, mapKeySignature } from './mappers';
20
4
  import { parseDirection } from './MusicXmlDirections';
21
5
  import {
22
- appendSpacerRests,
23
- getMeasureDurationTicks,
24
- inferDurationType,
25
- } from './MusicXmlTiming';
26
- import { applyNoteAttachments } from './Spanners';
6
+ attachPendingGraceNotes,
7
+ collectGraceNote,
8
+ createVoiceItem,
9
+ mergeChordTone,
10
+ pushItem,
11
+ } from './MusicXmlNotes';
12
+ import { appendSpacerRests, getMeasureDurationTicks } from './MusicXmlTiming';
13
+ import { applyNoteAttachments, applyTies } from './Spanners';
27
14
  import { MusicXmlParseError } from './types';
28
15
  import {
29
16
  attr,
30
17
  childText,
31
18
  childrenNamed,
32
- firstChild,
33
19
  hasChild,
34
20
  numberText,
35
21
  optionalChild,
@@ -168,6 +154,15 @@ function applyAttributes(
168
154
  state.defaults.meter = measureState.meter;
169
155
  }
170
156
 
157
+ childrenNamed(attributes, 'staff-details').forEach((details) => {
158
+ const staff = staves[Number(attr(details, 'number') ?? '1') - 1];
159
+ const lines = numberText(details, 'staff-lines');
160
+
161
+ if (staff && (lines === 1 || lines === 3 || lines === 5)) {
162
+ staff.lines ??= lines;
163
+ }
164
+ });
165
+
171
166
  childrenNamed(attributes, 'clef').forEach((clef) => {
172
167
  const staffIndex = Number(attr(clef, 'number') ?? '1') - 1;
173
168
  const staff = staves[staffIndex];
@@ -178,6 +173,7 @@ function applyAttributes(
178
173
 
179
174
  const mappedClef = mapClef(clef);
180
175
  staff.defaultClef ??= mappedClef;
176
+ staff.clef = mappedClef;
181
177
  measures[staffIndex]!.state = {
182
178
  ...measures[staffIndex]!.state,
183
179
  clef: mappedClef,
@@ -192,8 +188,8 @@ function applyAttributes(
192
188
  };
193
189
  measure.leftModifiers = {
194
190
  ...measure.leftModifiers,
195
- showKeySignature: Boolean(measureState.keySignature),
196
- showMeter: Boolean(measureState.meter),
191
+ ...(measureState.keySignature && { showKeySignature: true }),
192
+ ...(measureState.meter && { showMeter: true }),
197
193
  };
198
194
  });
199
195
  }
@@ -224,7 +220,7 @@ function parseNote(
224
220
  const graceVoiceName = childText(note, 'voice');
225
221
 
226
222
  if (graceVoiceName) {
227
- collectGraceNote(note, graceVoiceName, state);
223
+ collectGraceNote(note, graceVoiceName, targetStaff.clef, state);
228
224
  }
229
225
 
230
226
  return currentPosition;
@@ -241,7 +237,13 @@ function parseNote(
241
237
  );
242
238
  const voice = cursor.voice;
243
239
  const isChordTone = hasChild(note, 'chord');
244
- const item = createVoiceItem(note, voice, isChordTone, state);
240
+ const item = createVoiceItem(
241
+ note,
242
+ voice,
243
+ isChordTone,
244
+ targetStaff.clef,
245
+ state
246
+ );
245
247
  const ownerStaff = staves[cursor.ownerStaffIndex];
246
248
  const noteDuration = isChordTone ? 0 : requiredNumberText(note, 'duration');
247
249
 
@@ -268,6 +270,7 @@ function parseNote(
268
270
 
269
271
  attachPendingGraceNotes(itemId, voiceName, state);
270
272
  applyNoteAttachments(note, itemId, staffNumber, voice.id, state, pitchIndex);
273
+ applyTies(note, item, itemId, voiceName, state);
271
274
 
272
275
  if (isChordTone) {
273
276
  return currentPosition;
@@ -352,160 +355,6 @@ function getVoiceCursor(
352
355
  return cursor;
353
356
  }
354
357
 
355
- function createVoiceItem(
356
- note: XmlElement,
357
- voice: Voice,
358
- isChordTone: boolean,
359
- state: ParserState
360
- ): VoiceItem {
361
- const duration = parseDuration(note, state);
362
-
363
- if (hasChild(note, 'rest')) {
364
- if (isChordTone) {
365
- throw new MusicXmlParseError('Rest chord tones are unsupported');
366
- }
367
-
368
- return {
369
- id: '',
370
- type: 'rest',
371
- duration,
372
- voiceId: voice.id,
373
- kind: attr(note, 'print-object') === 'no' ? 'hidden' : 'visible',
374
- };
375
- }
376
-
377
- return {
378
- id: '',
379
- type: 'note',
380
- pitch: parsePitch(note),
381
- duration,
382
- voiceId: voice.id,
383
- stemDirection: mapStemDirection(childText(note, 'stem')),
384
- };
385
- }
386
-
387
- function parseDuration(note: XmlElement, state: ParserState): DurationValue {
388
- const typeText = childText(note, 'type');
389
- const type = typeText
390
- ? mapDurationType(typeText)
391
- : inferDurationType(requiredNumberText(note, 'duration'), state.divisions);
392
- const dotCount = childrenNamed(note, 'dot').length;
393
-
394
- if (dotCount > 3) {
395
- throw new MusicXmlParseError('More than three dots are unsupported');
396
- }
397
-
398
- return {
399
- length: type,
400
- dots: dotCount ? (dotCount as 1 | 2 | 3) : undefined,
401
- };
402
- }
403
-
404
- function parsePitch(note: XmlElement): Pitch {
405
- const pitch = mapPitch(
406
- firstChild(note, 'pitch'),
407
- childText(note, 'accidental')
408
- );
409
- const notehead = optionalChild(note, 'notehead');
410
-
411
- if (notehead && attr(notehead, 'parentheses') === 'yes') {
412
- pitch.parenthesized = true;
413
- }
414
-
415
- return pitch;
416
- }
417
-
418
- /**
419
- * Grace chord tones keep only their first pitch; the group's slash comes
420
- * from its first grace note.
421
- */
422
- function collectGraceNote(
423
- note: XmlElement,
424
- voiceName: string,
425
- state: ParserState
426
- ) {
427
- if (hasChild(note, 'chord')) {
428
- return;
429
- }
430
-
431
- const graceNote = {
432
- pitch: parsePitch(note),
433
- duration: parseDuration(note, state),
434
- };
435
- const pending = state.pendingGraceNotes.get(voiceName);
436
-
437
- if (pending) {
438
- pending.notes.push(graceNote);
439
- return;
440
- }
441
-
442
- state.pendingGraceNotes.set(voiceName, {
443
- notes: [graceNote],
444
- slash: attr(firstChild(note, 'grace'), 'slash') === 'yes',
445
- });
446
- }
447
-
448
- function attachPendingGraceNotes(
449
- itemId: string,
450
- voiceName: string,
451
- state: ParserState
452
- ) {
453
- const pending = state.pendingGraceNotes.get(voiceName);
454
-
455
- if (!pending) {
456
- return;
457
- }
458
-
459
- state.pendingGraceNotes.delete(voiceName);
460
- state.attachments.push({
461
- id: `${itemId}-grace`,
462
- ownerId: itemId,
463
- type: 'grace',
464
- notes: pending.notes,
465
- ...(pending.slash ? { slash: true } : {}),
466
- });
467
- }
468
-
469
- function pushItem(voice: Voice, item: VoiceItem): string {
470
- item.id = `${voice.id}-item-${(voice.items.length + 1).toString()}`;
471
- voice.items.push(item);
472
- return item.id;
473
- }
474
-
475
- function mergeChordTone(
476
- voice: Voice,
477
- item: VoiceItem,
478
- note: XmlElement
479
- ): string {
480
- const previous = voice.items[voice.items.length - 1];
481
-
482
- if (!previous || item.type !== 'note') {
483
- throw new MusicXmlParseError('Chord tone has no previous pitched note');
484
- }
485
-
486
- if (previous.type === 'note') {
487
- const chord: Chord = {
488
- id: previous.id,
489
- type: 'chord',
490
- pitches: [previous.pitch, item.pitch],
491
- duration: previous.duration,
492
- voiceId: previous.voiceId,
493
- targetStaffId: previous.targetStaffId ?? item.targetStaffId,
494
- stemDirection:
495
- mapStemDirection(childText(note, 'stem')) ?? previous.stemDirection,
496
- };
497
- voice.items[voice.items.length - 1] = chord;
498
- return chord.id;
499
- }
500
-
501
- if (previous.type === 'chord') {
502
- previous.pitches.push(item.pitch);
503
- return previous.id;
504
- }
505
-
506
- throw new MusicXmlParseError('Chord tone cannot follow a rest');
507
- }
508
-
509
358
  function finalizeMeasureVoices(
510
359
  measure: Measure,
511
360
  voiceCursors: Map<string, VoiceCursor>,
@@ -0,0 +1,198 @@
1
+ import type {
2
+ Chord,
3
+ Clef,
4
+ DurationValue,
5
+ Pitch,
6
+ Voice,
7
+ VoiceItem,
8
+ } from '../../state';
9
+ import type { ParserState } from './InternalTypes';
10
+ import {
11
+ mapDurationType,
12
+ mapNotehead,
13
+ mapPitch,
14
+ mapStemDirection,
15
+ mapUnpitched,
16
+ } from './mappers';
17
+ import { inferDurationType } from './MusicXmlTiming';
18
+ import { MusicXmlParseError } from './types';
19
+ import {
20
+ attr,
21
+ childText,
22
+ childrenNamed,
23
+ firstChild,
24
+ hasChild,
25
+ optionalChild,
26
+ requiredNumberText,
27
+ textOf,
28
+ type XmlElement,
29
+ } from './XmlOrder';
30
+
31
+ export function createVoiceItem(
32
+ note: XmlElement,
33
+ voice: Voice,
34
+ isChordTone: boolean,
35
+ clef: Clef,
36
+ state: ParserState
37
+ ): VoiceItem {
38
+ const duration = parseDuration(note, state);
39
+
40
+ if (hasChild(note, 'rest')) {
41
+ if (isChordTone) {
42
+ throw new MusicXmlParseError('Rest chord tones are unsupported');
43
+ }
44
+
45
+ return {
46
+ id: '',
47
+ type: 'rest',
48
+ duration,
49
+ voiceId: voice.id,
50
+ kind: attr(note, 'print-object') === 'no' ? 'hidden' : 'visible',
51
+ };
52
+ }
53
+
54
+ return {
55
+ id: '',
56
+ type: 'note',
57
+ pitch: parsePitch(note, clef),
58
+ duration,
59
+ voiceId: voice.id,
60
+ stemDirection: mapStemDirection(childText(note, 'stem')),
61
+ };
62
+ }
63
+
64
+ function parseDuration(note: XmlElement, state: ParserState): DurationValue {
65
+ const typeText = childText(note, 'type');
66
+ const type = typeText
67
+ ? mapDurationType(typeText)
68
+ : inferDurationType(requiredNumberText(note, 'duration'), state.divisions);
69
+ const dotCount = childrenNamed(note, 'dot').length;
70
+
71
+ if (dotCount > 3) {
72
+ throw new MusicXmlParseError('More than three dots are unsupported');
73
+ }
74
+
75
+ return {
76
+ length: type,
77
+ dots: dotCount ? (dotCount as 1 | 2 | 3) : undefined,
78
+ };
79
+ }
80
+
81
+ function parsePitch(note: XmlElement, clef: Clef): Pitch {
82
+ const pitched = optionalChild(note, 'pitch');
83
+ const unpitched = optionalChild(note, 'unpitched');
84
+ const pitch = pitched
85
+ ? mapPitch(pitched, childText(note, 'accidental'))
86
+ : unpitched && mapUnpitched(unpitched, clef);
87
+
88
+ if (!pitch) {
89
+ throw new MusicXmlParseError('Missing <pitch> or <unpitched>', {
90
+ path: note.name,
91
+ });
92
+ }
93
+
94
+ const notehead = optionalChild(note, 'notehead');
95
+ const shape = notehead && mapNotehead(textOf(notehead));
96
+
97
+ if (shape) {
98
+ pitch.notehead = shape;
99
+ }
100
+
101
+ if (notehead && attr(notehead, 'parentheses') === 'yes') {
102
+ pitch.parenthesized = true;
103
+ }
104
+
105
+ return pitch;
106
+ }
107
+
108
+ /**
109
+ * Grace chord tones keep only their first pitch; the group's slash comes
110
+ * from its first grace note.
111
+ */
112
+ export function collectGraceNote(
113
+ note: XmlElement,
114
+ voiceName: string,
115
+ clef: Clef,
116
+ state: ParserState
117
+ ) {
118
+ if (hasChild(note, 'chord')) {
119
+ return;
120
+ }
121
+
122
+ const graceNote = {
123
+ pitch: parsePitch(note, clef),
124
+ duration: parseDuration(note, state),
125
+ };
126
+ const pending = state.pendingGraceNotes.get(voiceName);
127
+
128
+ if (pending) {
129
+ pending.notes.push(graceNote);
130
+ return;
131
+ }
132
+
133
+ state.pendingGraceNotes.set(voiceName, {
134
+ notes: [graceNote],
135
+ slash: attr(firstChild(note, 'grace'), 'slash') === 'yes',
136
+ });
137
+ }
138
+
139
+ export function attachPendingGraceNotes(
140
+ itemId: string,
141
+ voiceName: string,
142
+ state: ParserState
143
+ ) {
144
+ const pending = state.pendingGraceNotes.get(voiceName);
145
+
146
+ if (!pending) {
147
+ return;
148
+ }
149
+
150
+ state.pendingGraceNotes.delete(voiceName);
151
+ state.attachments.push({
152
+ id: `${itemId}-grace`,
153
+ ownerId: itemId,
154
+ type: 'grace',
155
+ notes: pending.notes,
156
+ ...(pending.slash ? { slash: true } : {}),
157
+ });
158
+ }
159
+
160
+ export function pushItem(voice: Voice, item: VoiceItem): string {
161
+ item.id = `${voice.id}-item-${(voice.items.length + 1).toString()}`;
162
+ voice.items.push(item);
163
+ return item.id;
164
+ }
165
+
166
+ export function mergeChordTone(
167
+ voice: Voice,
168
+ item: VoiceItem,
169
+ note: XmlElement
170
+ ): string {
171
+ const previous = voice.items[voice.items.length - 1];
172
+
173
+ if (!previous || item.type !== 'note') {
174
+ throw new MusicXmlParseError('Chord tone has no previous pitched note');
175
+ }
176
+
177
+ if (previous.type === 'note') {
178
+ const chord: Chord = {
179
+ id: previous.id,
180
+ type: 'chord',
181
+ pitches: [previous.pitch, item.pitch],
182
+ duration: previous.duration,
183
+ voiceId: previous.voiceId,
184
+ targetStaffId: previous.targetStaffId ?? item.targetStaffId,
185
+ stemDirection:
186
+ mapStemDirection(childText(note, 'stem')) ?? previous.stemDirection,
187
+ };
188
+ voice.items[voice.items.length - 1] = chord;
189
+ return chord.id;
190
+ }
191
+
192
+ if (previous.type === 'chord') {
193
+ previous.pitches.push(item.pitch);
194
+ return previous.id;
195
+ }
196
+
197
+ throw new MusicXmlParseError('Chord tone cannot follow a rest');
198
+ }
@@ -1,4 +1,4 @@
1
- import type { Staff, StaffGroup } from '../../state';
1
+ import type { Clef, Staff, StaffGroup } from '../../state';
2
2
  import type { PartInfo, ParserState, StaffBuild } from './InternalTypes';
3
3
  import { parseMeasure } from './MeasureParser';
4
4
  import {
@@ -25,6 +25,7 @@ export function parsePart(
25
25
  ? partInfo.name
26
26
  : `${partInfo.name ?? partInfo.id} ${index + 1}`,
27
27
  order: orderOffset + index,
28
+ clef: fallbackClef(orderOffset + index),
28
29
  measures: [],
29
30
  })
30
31
  );
@@ -36,14 +37,16 @@ export function parsePart(
36
37
  });
37
38
 
38
39
  state.pendingGraceNotes.clear();
40
+ state.pendingDynamics.clear();
41
+ state.activeTies.clear();
39
42
 
40
43
  const staves = builds.map(
41
44
  (staff): Staff => ({
42
45
  id: staff.id,
43
46
  name: staff.name,
44
47
  order: staff.order,
45
- defaultClef:
46
- staff.defaultClef ?? (staff.order % 2 === 0 ? 'treble' : 'bass'),
48
+ defaultClef: staff.defaultClef ?? fallbackClef(staff.order),
49
+ lines: staff.lines,
47
50
  measures: staff.measures,
48
51
  })
49
52
  );
@@ -64,6 +67,10 @@ export function parsePart(
64
67
  };
65
68
  }
66
69
 
70
+ function fallbackClef(order: number): Clef {
71
+ return order % 2 === 0 ? 'treble' : 'bass';
72
+ }
73
+
67
74
  function getStaffCount(measure: XmlElement): number {
68
75
  const attributes = optionalChild(measure, 'attributes');
69
76
  const staves = attributes ? numberText(attributes, 'staves') : undefined;
@@ -1,6 +1,6 @@
1
- import type { Pitch, TupletGroup } from '../../state';
1
+ import type { Pitch, TupletGroup, VoiceItem } from '../../state';
2
2
  import type { ParserState } from './InternalTypes';
3
- import { articulationsFromNotations, mapPitch } from './mappers';
3
+ import { articulationsFromNotations } from './mappers';
4
4
  import { MusicXmlParseError } from './types';
5
5
  import {
6
6
  attr,
@@ -78,20 +78,23 @@ export function applyNoteAttachments(
78
78
  }
79
79
  });
80
80
 
81
- applyTies(note, itemId, voiceId, state);
82
81
  applySlurs(notations, itemId, voiceId, state);
83
82
  applyTuplets(note, notations, itemId, voiceId, state);
84
83
  }
85
84
 
86
- function applyTies(
85
+ /**
86
+ * Ties match by MusicXML voice name and pitch, so they cross barlines (each
87
+ * measure has its own `Voice`).
88
+ */
89
+ export function applyTies(
87
90
  note: XmlElement,
91
+ item: VoiceItem,
88
92
  itemId: string,
89
- voiceId: string,
93
+ voiceName: string,
90
94
  state: ParserState
91
95
  ) {
92
- const pitch = optionalChild(note, 'pitch');
93
- const tieKey = `${voiceId}:${
94
- pitch ? pitchKey(mapPitch(pitch, childText(note, 'accidental'))) : 'rest'
96
+ const tieKey = `${voiceName}:${
97
+ item.type === 'note' ? pitchKey(item.pitch) : 'rest'
95
98
  }`;
96
99
 
97
100
  childrenNamed(note, 'tie').forEach((tie) => {
@@ -6,6 +6,7 @@ import type {
6
6
  Dynamic,
7
7
  KeyMode,
8
8
  KeySignature,
9
+ Notehead,
9
10
  NoteLength,
10
11
  Pitch,
11
12
  Step,
@@ -144,17 +145,65 @@ export function mapDurationType(type: string): NoteLength {
144
145
  }
145
146
 
146
147
  export function mapPitch(pitch: XmlElement, accidental?: string): Pitch {
147
- const step = requiredChildText(pitch, 'step');
148
+ return {
149
+ ...mapStaffPosition(pitch, 'step', 'octave'),
150
+ accidental: mapAccidental(childText(pitch, 'alter'), accidental),
151
+ };
152
+ }
153
+
154
+ /** Pitch on the middle staff line under each clef (VexFlow line positions). */
155
+ const MIDDLE_LINE_PITCHES: Record<Clef, Pitch> = {
156
+ 'treble': { step: 'B', octave: 4 },
157
+ 'bass': { step: 'D', octave: 3 },
158
+ 'alto': { step: 'C', octave: 4 },
159
+ 'tenor': { step: 'A', octave: 3 },
160
+ 'soprano': { step: 'G', octave: 4 },
161
+ 'mezzo-soprano': { step: 'E', octave: 4 },
162
+ 'baritone-c': { step: 'F', octave: 3 },
163
+ 'baritone-f': { step: 'F', octave: 3 },
164
+ 'subbass': { step: 'B', octave: 2 },
165
+ 'french': { step: 'D', octave: 5 },
166
+ 'percussion': { step: 'B', octave: 4 },
167
+ 'tab': { step: 'B', octave: 4 },
168
+ };
169
+
170
+ /**
171
+ * `<display-step>`/`<display-octave>` place the note under `clef`; without
172
+ * them it sits on the middle line.
173
+ */
174
+ export function mapUnpitched(unpitched: XmlElement, clef: Clef): Pitch {
175
+ return hasChild(unpitched, 'display-step')
176
+ ? mapStaffPosition(unpitched, 'display-step', 'display-octave')
177
+ : { ...MIDDLE_LINE_PITCHES[clef] };
178
+ }
179
+
180
+ function mapStaffPosition(
181
+ element: XmlElement,
182
+ stepName: string,
183
+ octaveName: string
184
+ ): Pitch {
185
+ const step = requiredChildText(element, stepName);
148
186
 
149
187
  if (!isStep(step)) {
150
188
  throw new MusicXmlParseError(`Unsupported pitch step "${step}"`);
151
189
  }
152
190
 
153
- return {
154
- step,
155
- octave: Number(requiredChildText(pitch, 'octave')),
156
- accidental: mapAccidental(childText(pitch, 'alter'), accidental),
157
- };
191
+ return { step, octave: Number(requiredChildText(element, octaveName)) };
192
+ }
193
+
194
+ const NOTEHEADS: Record<string, Notehead> = {
195
+ 'x': 'x',
196
+ 'circle-x': 'circle-x',
197
+ 'diamond': 'diamond',
198
+ 'circled': 'circle',
199
+ 'square': 'square',
200
+ 'triangle': 'triangle',
201
+ 'inverted triangle': 'triangle-down',
202
+ 'slash': 'slash',
203
+ };
204
+
205
+ export function mapNotehead(value: string): Notehead | undefined {
206
+ return NOTEHEADS[value];
158
207
  }
159
208
 
160
209
  export function mapStemDirection(value?: string) {
@@ -11,6 +11,7 @@ import { getSystemGap } from './LayoutMetrics';
11
11
  import {
12
12
  resolveSystemVerticalLayout,
13
13
  resolveUniformSystemVerticalLayout,
14
+ resolveVerseBaselines,
14
15
  } from './VerticalSpacing';
15
16
 
16
17
  type DocumentLine = {
@@ -41,13 +42,14 @@ export function resolveDocumentEvenMeasureWidth(
41
42
  Math.floor(availableWidth / widestMeasure)
42
43
  );
43
44
 
44
- // A clef added at a system start has to fit the equal measure width too.
45
+ // A clef and key signature added at a system start have to fit the equal
46
+ // measure width too.
45
47
  while (
46
48
  measuresPerFullLine > 1 &&
47
49
  measuredScore.measures.some(
48
50
  (measure) =>
49
51
  measure.measureIndex % measuresPerFullLine === 0 &&
50
- measure.intrinsicNoteWidth + (measure.systemStartClefWidth ?? 0) >
52
+ measure.intrinsicNoteWidth + (measure.systemStartModifierWidth ?? 0) >
51
53
  availableWidth / measuresPerFullLine
52
54
  )
53
55
  ) {
@@ -112,6 +114,7 @@ export function layoutDocumentEvenGroup(
112
114
  group.staves.length,
113
115
  options.spacing.staffGap
114
116
  );
117
+ const verseBaselines = resolveVerseBaselines(chunk, group.staves.length);
115
118
  let cursorX = origin.x;
116
119
 
117
120
  systems.push({
@@ -138,6 +141,7 @@ export function layoutDocumentEvenGroup(
138
141
  width: equalMeasureWidth,
139
142
  height: verticalLayout.height,
140
143
  staffYOffsets: verticalLayout.staffYOffsets,
144
+ ...(verseBaselines ? { verseBaselines } : {}),
141
145
  systemIndex,
142
146
  });
143
147
 
@@ -195,6 +199,10 @@ export function layoutDocumentGroup(
195
199
  group.staves.length,
196
200
  options.spacing.staffGap
197
201
  );
202
+ const verseBaselines = resolveVerseBaselines(
203
+ line.measures,
204
+ group.staves.length
205
+ );
198
206
  const systemX = origin.x + line.indent;
199
207
  let cursorX = systemX;
200
208
 
@@ -224,6 +232,7 @@ export function layoutDocumentGroup(
224
232
  width,
225
233
  height: verticalLayout.height,
226
234
  staffYOffsets: verticalLayout.staffYOffsets,
235
+ ...(verseBaselines ? { verseBaselines } : {}),
227
236
  systemIndex,
228
237
  });
229
238
 
@@ -286,5 +295,5 @@ function getMeasureLineWidth(measure: MeasuredGroupMeasure): number {
286
295
  }
287
296
 
288
297
  function getSystemStartWidth(measure: MeasuredGroupMeasure): number {
289
- return getMeasureLineWidth(measure) + (measure.systemStartClefWidth ?? 0);
298
+ return getMeasureLineWidth(measure) + (measure.systemStartModifierWidth ?? 0);
290
299
  }