vexflow-native 1.7.0 → 1.9.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 (129) hide show
  1. package/README.md +33 -348
  2. package/lib/module/base/FontManager.js +0 -0
  3. package/lib/module/base/FontManager.js.map +1 -1
  4. package/lib/module/base/VexflowRecordingContext.js +2 -0
  5. package/lib/module/base/VexflowRecordingContext.js.map +1 -1
  6. package/lib/module/base/index.js +1 -0
  7. package/lib/module/base/index.js.map +1 -1
  8. package/lib/module/musicxml/MusicXmlParser/MeasureParser.js +24 -7
  9. package/lib/module/musicxml/MusicXmlParser/MeasureParser.js.map +1 -1
  10. package/lib/module/musicxml/MusicXmlParser/MusicXmlDirections.js +11 -3
  11. package/lib/module/musicxml/MusicXmlParser/MusicXmlDirections.js.map +1 -1
  12. package/lib/module/musicxml/MusicXmlParser/Spanners.js +21 -5
  13. package/lib/module/musicxml/MusicXmlParser/Spanners.js.map +1 -1
  14. package/lib/module/musicxml/MusicXmlParser/mappers.js +1 -1
  15. package/lib/module/musicxml/MusicXmlParser/mappers.js.map +1 -1
  16. package/lib/module/percussion/index.js +8 -0
  17. package/lib/module/percussion/index.js.map +1 -0
  18. package/lib/module/renderer/ScoreRenderer/ScoreRenderer.js +4 -0
  19. package/lib/module/renderer/ScoreRenderer/ScoreRenderer.js.map +1 -1
  20. package/lib/module/renderer/fixedNoteSpacing.js +56 -0
  21. package/lib/module/renderer/fixedNoteSpacing.js.map +1 -0
  22. package/lib/module/renderer/index.js +1 -0
  23. package/lib/module/renderer/index.js.map +1 -1
  24. package/lib/module/renderer/itemLayout.js +108 -0
  25. package/lib/module/renderer/itemLayout.js.map +1 -0
  26. package/lib/module/renderer/measure.js +31 -102
  27. package/lib/module/renderer/measure.js.map +1 -1
  28. package/lib/module/renderer/measureBounds.js +73 -0
  29. package/lib/module/renderer/measureBounds.js.map +1 -0
  30. package/lib/module/renderer/measureModifiers.js +99 -0
  31. package/lib/module/renderer/measureModifiers.js.map +1 -0
  32. package/lib/module/renderer/noteModifiers.js +212 -0
  33. package/lib/module/renderer/noteModifiers.js.map +1 -0
  34. package/lib/module/renderer/render.js +34 -115
  35. package/lib/module/renderer/render.js.map +1 -1
  36. package/lib/module/renderer/scale.js +16 -2
  37. package/lib/module/renderer/scale.js.map +1 -1
  38. package/lib/module/renderer/scoreParsing.js +37 -188
  39. package/lib/module/renderer/scoreParsing.js.map +1 -1
  40. package/lib/module/renderer/stave.js +37 -0
  41. package/lib/module/renderer/stave.js.map +1 -0
  42. package/lib/module/renderer/useScoreRecording.js +10 -3
  43. package/lib/module/renderer/useScoreRecording.js.map +1 -1
  44. package/lib/module/renderer/vfKeys.js +72 -0
  45. package/lib/module/renderer/vfKeys.js.map +1 -0
  46. package/lib/module/state/index.js +1 -0
  47. package/lib/module/state/index.js.map +1 -1
  48. package/lib/typescript/src/base/FontManager.d.ts +7 -0
  49. package/lib/typescript/src/base/FontManager.d.ts.map +1 -1
  50. package/lib/typescript/src/base/VexflowRecordingContext.d.ts +1 -0
  51. package/lib/typescript/src/base/VexflowRecordingContext.d.ts.map +1 -1
  52. package/lib/typescript/src/base/index.d.ts +1 -0
  53. package/lib/typescript/src/base/index.d.ts.map +1 -1
  54. package/lib/typescript/src/musicxml/MusicXmlParser/InternalTypes.d.ts +4 -2
  55. package/lib/typescript/src/musicxml/MusicXmlParser/InternalTypes.d.ts.map +1 -1
  56. package/lib/typescript/src/musicxml/MusicXmlParser/MeasureParser.d.ts.map +1 -1
  57. package/lib/typescript/src/musicxml/MusicXmlParser/MusicXmlDirections.d.ts +6 -1
  58. package/lib/typescript/src/musicxml/MusicXmlParser/MusicXmlDirections.d.ts.map +1 -1
  59. package/lib/typescript/src/musicxml/MusicXmlParser/Spanners.d.ts +6 -1
  60. package/lib/typescript/src/musicxml/MusicXmlParser/Spanners.d.ts.map +1 -1
  61. package/lib/typescript/src/musicxml/MusicXmlParser/mappers.d.ts.map +1 -1
  62. package/lib/typescript/src/musicxml/index.d.ts +1 -0
  63. package/lib/typescript/src/musicxml/index.d.ts.map +1 -1
  64. package/lib/typescript/src/percussion/index.d.ts +4 -0
  65. package/lib/typescript/src/percussion/index.d.ts.map +1 -0
  66. package/lib/typescript/src/renderer/ScoreRenderer/ScoreRenderer.d.ts.map +1 -1
  67. package/lib/typescript/src/renderer/fixedNoteSpacing.d.ts +17 -0
  68. package/lib/typescript/src/renderer/fixedNoteSpacing.d.ts.map +1 -0
  69. package/lib/typescript/src/renderer/index.d.ts +2 -0
  70. package/lib/typescript/src/renderer/index.d.ts.map +1 -1
  71. package/lib/typescript/src/renderer/itemLayout.d.ts +23 -0
  72. package/lib/typescript/src/renderer/itemLayout.d.ts.map +1 -0
  73. package/lib/typescript/src/renderer/layout.d.ts +1 -1
  74. package/lib/typescript/src/renderer/layout.d.ts.map +1 -1
  75. package/lib/typescript/src/renderer/measure.d.ts +3 -6
  76. package/lib/typescript/src/renderer/measure.d.ts.map +1 -1
  77. package/lib/typescript/src/renderer/measureBounds.d.ts +13 -0
  78. package/lib/typescript/src/renderer/measureBounds.d.ts.map +1 -0
  79. package/lib/typescript/src/renderer/measureModifiers.d.ts +20 -0
  80. package/lib/typescript/src/renderer/measureModifiers.d.ts.map +1 -0
  81. package/lib/typescript/src/renderer/noteModifiers.d.ts +29 -0
  82. package/lib/typescript/src/renderer/noteModifiers.d.ts.map +1 -0
  83. package/lib/typescript/src/renderer/render.d.ts +2 -27
  84. package/lib/typescript/src/renderer/render.d.ts.map +1 -1
  85. package/lib/typescript/src/renderer/scale.d.ts.map +1 -1
  86. package/lib/typescript/src/renderer/scoreParsing.d.ts +14 -40
  87. package/lib/typescript/src/renderer/scoreParsing.d.ts.map +1 -1
  88. package/lib/typescript/src/renderer/stave.d.ts +11 -0
  89. package/lib/typescript/src/renderer/stave.d.ts.map +1 -0
  90. package/lib/typescript/src/renderer/types.d.ts +53 -4
  91. package/lib/typescript/src/renderer/types.d.ts.map +1 -1
  92. package/lib/typescript/src/renderer/useScoreRecording.d.ts +2 -2
  93. package/lib/typescript/src/renderer/useScoreRecording.d.ts.map +1 -1
  94. package/lib/typescript/src/renderer/vfKeys.d.ts +17 -0
  95. package/lib/typescript/src/renderer/vfKeys.d.ts.map +1 -0
  96. package/lib/typescript/src/state/index.d.ts +1 -0
  97. package/lib/typescript/src/state/index.d.ts.map +1 -1
  98. package/lib/typescript/src/state/types.d.ts +39 -6
  99. package/lib/typescript/src/state/types.d.ts.map +1 -1
  100. package/package.json +15 -5
  101. package/src/base/FontManager.ts +0 -0
  102. package/src/base/VexflowRecordingContext.ts +1 -0
  103. package/src/base/index.ts +1 -0
  104. package/src/musicxml/MusicXmlParser/InternalTypes.ts +5 -2
  105. package/src/musicxml/MusicXmlParser/MeasureParser.ts +36 -7
  106. package/src/musicxml/MusicXmlParser/MusicXmlDirections.ts +25 -8
  107. package/src/musicxml/MusicXmlParser/Spanners.ts +27 -4
  108. package/src/musicxml/MusicXmlParser/mappers.ts +7 -0
  109. package/src/musicxml/index.ts +1 -0
  110. package/src/percussion/index.ts +7 -0
  111. package/src/renderer/Layout/VerticalSpacing.ts +1 -1
  112. package/src/renderer/ScoreRenderer/ScoreRenderer.tsx +4 -0
  113. package/src/renderer/fixedNoteSpacing.ts +78 -0
  114. package/src/renderer/index.ts +8 -0
  115. package/src/renderer/itemLayout.ts +137 -0
  116. package/src/renderer/layout.ts +1 -1
  117. package/src/renderer/measure.tsx +85 -182
  118. package/src/renderer/measureBounds.ts +109 -0
  119. package/src/renderer/measureModifiers.ts +133 -0
  120. package/src/renderer/noteModifiers.ts +298 -0
  121. package/src/renderer/render.tsx +66 -171
  122. package/src/renderer/scale.ts +21 -1
  123. package/src/renderer/scoreParsing.tsx +78 -262
  124. package/src/renderer/stave.ts +44 -0
  125. package/src/renderer/types.ts +67 -4
  126. package/src/renderer/useScoreRecording.ts +11 -3
  127. package/src/renderer/vfKeys.ts +88 -0
  128. package/src/state/index.ts +1 -0
  129. package/src/state/types.ts +48 -5
@@ -94,6 +94,7 @@ export default class VexflowRecordingContext implements VexflowRenderContext {
94
94
  return this.textFont;
95
95
  }
96
96
 
97
+ /** @internal */
97
98
  getMeasurementContext() {
98
99
  return this.textMeasurementContext;
99
100
  }
package/src/base/index.ts CHANGED
@@ -1,4 +1,5 @@
1
1
  export { default as VexflowCanvas } from './VexflowCanvas';
2
+ export { default as FontManager } from './FontManager';
2
3
  export { default as VexflowRecordingContext } from './VexflowRecordingContext';
3
4
  export { renderVexflowRecordingCommands } from './VexflowRecordingReplay';
4
5
  export {
@@ -1,5 +1,5 @@
1
1
  import type {
2
- Dynamic,
2
+ DynamicAttachment,
3
3
  GraceNote,
4
4
  Measure,
5
5
  NoteAttachment,
@@ -32,6 +32,8 @@ export type PendingGraceNotes = {
32
32
  slash: boolean;
33
33
  };
34
34
 
35
+ export type PendingDynamic = Pick<DynamicAttachment, 'dynamic' | 'placement'>;
36
+
35
37
  export type ParserState = {
36
38
  scoreId: string;
37
39
  divisions: number;
@@ -40,7 +42,8 @@ export type ParserState = {
40
42
  ties: Tie[];
41
43
  slurs: Slur[];
42
44
  tuplets: TupletGroup[];
43
- pendingDynamics: Map<string, Dynamic[]>;
45
+ /** Keyed by MusicXML staff number; consumed by that staff's next note. */
46
+ pendingDynamics: Map<string, PendingDynamic[]>;
44
47
  /** Keyed by MusicXML voice name; consumed by that voice's next note. */
45
48
  pendingGraceNotes: Map<string, PendingGraceNotes>;
46
49
  activeTies: Map<string, ActiveSpanner>;
@@ -60,7 +60,7 @@ export function parseMeasure(
60
60
  );
61
61
  const voiceMapsByStaff = staves.map(() => new Map<string, Voice>());
62
62
  const voiceCursors = new Map<string, VoiceCursor>();
63
- const directions: Direction[] = [];
63
+ const directionsByStaff = new Map<number, Direction[]>();
64
64
  let currentPosition = 0;
65
65
 
66
66
  measure.children.forEach((child) => {
@@ -68,9 +68,21 @@ export function parseMeasure(
68
68
  case 'attributes':
69
69
  applyAttributes(child, measures, staves, state);
70
70
  break;
71
- case 'direction':
72
- directions.push(...parseDirection(child, state));
71
+ case 'direction': {
72
+ const { staffIndex, directions } = parseDirection(child, state);
73
+
74
+ if (!measures[staffIndex]) {
75
+ throw new MusicXmlParseError(
76
+ `Direction references unknown staff ${(staffIndex + 1).toString()}`
77
+ );
78
+ }
79
+
80
+ directionsByStaff.set(staffIndex, [
81
+ ...(directionsByStaff.get(staffIndex) ?? []),
82
+ ...directions,
83
+ ]);
73
84
  break;
85
+ }
74
86
  case 'note':
75
87
  currentPosition = parseNote(
76
88
  child,
@@ -114,8 +126,10 @@ export function parseMeasure(
114
126
  finalizeMeasureVoices(measures[0]!, voiceCursors, state);
115
127
 
116
128
  measures.forEach((staffMeasure, index) => {
117
- if (directions.length) {
118
- staffMeasure.directions = [...directions];
129
+ const directions = directionsByStaff.get(index);
130
+
131
+ if (directions?.length) {
132
+ staffMeasure.directions = directions;
119
133
  }
120
134
 
121
135
  staffMeasure.voices = [...voiceMapsByStaff[index]!.values()].sort(
@@ -246,9 +260,14 @@ function parseNote(
246
260
  const itemId = isChordTone
247
261
  ? mergeChordTone(voice, item, note)
248
262
  : pushItem(voice, item);
263
+ const owner = voice.items[voice.items.length - 1];
264
+ const pitchIndex =
265
+ isChordTone && owner?.type === 'chord'
266
+ ? owner.pitches.length - 1
267
+ : undefined;
249
268
 
250
269
  attachPendingGraceNotes(itemId, voiceName, state);
251
- applyNoteAttachments(note, itemId, staffNumber, voice.id, state);
270
+ applyNoteAttachments(note, itemId, staffNumber, voice.id, state, pitchIndex);
252
271
 
253
272
  if (isChordTone) {
254
273
  return currentPosition;
@@ -383,7 +402,17 @@ function parseDuration(note: XmlElement, state: ParserState): DurationValue {
383
402
  }
384
403
 
385
404
  function parsePitch(note: XmlElement): Pitch {
386
- return mapPitch(firstChild(note, 'pitch'), childText(note, 'accidental'));
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;
387
416
  }
388
417
 
389
418
  /**
@@ -1,5 +1,5 @@
1
- import type { Direction, Dynamic, Tempo } from '../../state';
2
- import type { ParserState } from './InternalTypes';
1
+ import type { Direction, Tempo } from '../../state';
2
+ import type { ParserState, PendingDynamic } from './InternalTypes';
3
3
  import { dynamicsFromElement, otherDynamicsText } from './mappers';
4
4
  import {
5
5
  attr,
@@ -10,12 +10,22 @@ import {
10
10
  type XmlElement,
11
11
  } from './XmlOrder';
12
12
 
13
+ export type ParsedDirection = {
14
+ /** 0-based; unvalidated, NaN for a non-numeric `<staff>`. */
15
+ staffIndex: number;
16
+ directions: Direction[];
17
+ };
18
+
13
19
  export function parseDirection(
14
20
  direction: XmlElement,
15
21
  state: ParserState
16
- ): Direction[] {
17
- const placement =
18
- attr(direction, 'placement') === 'below' ? 'below' : 'above';
22
+ ): ParsedDirection {
23
+ const explicitPlacement = attr(direction, 'placement');
24
+ const dynamicPlacement =
25
+ explicitPlacement === 'above' || explicitPlacement === 'below'
26
+ ? explicitPlacement
27
+ : undefined;
28
+ const placement = dynamicPlacement ?? 'above';
19
29
  const staffNumber = childText(direction, 'staff') ?? '1';
20
30
  const directions: Direction[] = [];
21
31
  const soundTempo = optionalChild(direction, 'sound')?.attributes.tempo;
@@ -40,7 +50,14 @@ export function parseDirection(
40
50
  const otherText = otherDynamicsText(dynamics);
41
51
 
42
52
  if (parsedDynamics.length) {
43
- addPendingDynamics(state, staffNumber, parsedDynamics);
53
+ addPendingDynamics(
54
+ state,
55
+ staffNumber,
56
+ parsedDynamics.map((dynamic) => ({
57
+ dynamic,
58
+ placement: dynamicPlacement,
59
+ }))
60
+ );
44
61
  }
45
62
 
46
63
  if (otherText) {
@@ -64,7 +81,7 @@ export function parseDirection(
64
81
  });
65
82
  }
66
83
 
67
- return directions;
84
+ return { staffIndex: Number(staffNumber) - 1, directions };
68
85
  }
69
86
 
70
87
  function parseTempo(value: string): Tempo {
@@ -77,7 +94,7 @@ function parseTempo(value: string): Tempo {
77
94
  function addPendingDynamics(
78
95
  state: ParserState,
79
96
  staffNumber: string,
80
- dynamics: Dynamic[]
97
+ dynamics: PendingDynamic[]
81
98
  ) {
82
99
  const pending = state.pendingDynamics.get(staffNumber) ?? [];
83
100
  state.pendingDynamics.set(staffNumber, [...pending, ...dynamics]);
@@ -11,32 +11,55 @@ import {
11
11
  type XmlElement,
12
12
  } from './XmlOrder';
13
13
 
14
+ /**
15
+ * `pitchIndex` is the chord-tone position of `note` inside its owner chord
16
+ * (undefined for a standalone note): a chord tone's articulation joins the
17
+ * owner's existing same-articulation attachment instead of adding a glyph.
18
+ */
14
19
  export function applyNoteAttachments(
15
20
  note: XmlElement,
16
21
  itemId: string,
17
22
  staffNumber: string,
18
23
  voiceId: string,
19
- state: ParserState
24
+ state: ParserState,
25
+ pitchIndex?: number
20
26
  ) {
21
27
  const notations = optionalChild(note, 'notations');
22
28
  const pendingDynamics = state.pendingDynamics.get(staffNumber) ?? [];
23
29
 
24
- pendingDynamics.forEach((dynamic, index) => {
30
+ pendingDynamics.forEach((pending, index) => {
25
31
  state.attachments.push({
26
32
  id: `${itemId}-dynamic-${index.toString()}`,
27
33
  ownerId: itemId,
28
34
  type: 'dynamic',
29
- dynamic,
35
+ ...pending,
30
36
  });
31
37
  });
32
38
  state.pendingDynamics.delete(staffNumber);
33
39
 
34
40
  articulationsFromNotations(notations).forEach((articulation, index) => {
41
+ if (pitchIndex !== undefined) {
42
+ const existing = state.attachments.find(
43
+ (attachment) =>
44
+ attachment.ownerId === itemId &&
45
+ attachment.type === 'articulation' &&
46
+ attachment.articulation === articulation
47
+ );
48
+
49
+ if (existing?.type === 'articulation') {
50
+ existing.pitchIndices = [...(existing.pitchIndices ?? [0]), pitchIndex];
51
+ return;
52
+ }
53
+ }
54
+
35
55
  state.attachments.push({
36
- id: `${itemId}-articulation-${index.toString()}`,
56
+ id: `${itemId}-articulation-${index.toString()}${
57
+ pitchIndex === undefined ? '' : `-${pitchIndex.toString()}`
58
+ }`,
37
59
  ownerId: itemId,
38
60
  type: 'articulation',
39
61
  articulation,
62
+ ...(pitchIndex === undefined ? {} : { pitchIndices: [pitchIndex] }),
40
63
  });
41
64
  });
42
65
 
@@ -64,7 +64,14 @@ const DYNAMICS: Dynamic[] = [
64
64
  'f',
65
65
  'ff',
66
66
  'fff',
67
+ 'fp',
68
+ 'sf',
69
+ 'sfp',
67
70
  'sfz',
71
+ 'rf',
72
+ 'rfz',
73
+ 'fz',
74
+ 'n',
68
75
  ];
69
76
 
70
77
  export function mapClef(clef: XmlElement): Clef {
@@ -3,3 +3,4 @@ export {
3
3
  parseMusicXmlToScore,
4
4
  type ParseMusicXmlOptions,
5
5
  } from './MusicXmlParser';
6
+ export type { MusicXmlParseContext } from './MusicXmlParser/types';
@@ -0,0 +1,7 @@
1
+ import type { Pitch } from '../state';
2
+
3
+ /** The pitch drawn on the single visible line of a `lines: 1` staff. */
4
+ export const ONE_LINE_STAFF_PITCH: Readonly<Pitch> = Object.freeze({
5
+ step: 'B',
6
+ octave: 4,
7
+ });
@@ -1,4 +1,4 @@
1
- import type { StaffVerticalBounds } from '../measure';
1
+ import type { StaffVerticalBounds } from '../measureBounds';
2
2
  import type { MeasuredGroupMeasure } from '../layout';
3
3
  import {
4
4
  VEXFLOW_STAVE_BOTTOM_LINE_OFFSET,
@@ -40,6 +40,8 @@ const ScoreRenderer: React.FC<ScoreRendererProps> = ({
40
40
  fontManager,
41
41
  colorScheme,
42
42
  itemStyleOverrides,
43
+ decorateItem,
44
+ onDrawItem,
43
45
  options: userOptions = EMPTY_OPTIONS,
44
46
  scrollEnabled = true,
45
47
  showScrollbars = true,
@@ -101,10 +103,12 @@ const ScoreRenderer: React.FC<ScoreRendererProps> = ({
101
103
  layoutPlan,
102
104
  itemsLayout,
103
105
  } = useScoreRecording({
106
+ decorateItem,
104
107
  defaultFont,
105
108
  enabled: hasViewportSize,
106
109
  fontManager,
107
110
  colorScheme: resolvedColorScheme,
111
+ onDrawItem,
108
112
  options,
109
113
  rendererType: effectiveRendererType,
110
114
  score,
@@ -0,0 +1,78 @@
1
+ import { Stave } from 'vexflow';
2
+ import type { Formatter, Voice as VFVoice } from 'vexflow';
3
+
4
+ /**
5
+ * Re-positions every tick context at its time-proportional x inside the note
6
+ * area, overriding the formatter's duration-weighted spacing. Runs after
7
+ * `formatToStave` (the same post-format `setX` mechanism the formatter's own
8
+ * tuning uses), so beams, tuplets and modifiers pick the final positions up
9
+ * at draw time. The lattice follows VexFlow's own convention for its first
10
+ * tick: it starts at that context's left-modifier width (`totalLeftPx`), so a
11
+ * grace group or accidental on beat 1 hangs inside the note area instead of
12
+ * over the clef. The proportional span is clamped only by that inset and the
13
+ * LAST context's width — with a spacer voice on the lattice the latter is
14
+ * constant, so the mapping stays a pure function of time and of what hangs
15
+ * left of beat 1.
16
+ */
17
+ export function applyFixedNoteSpacing(
18
+ formatter: Formatter,
19
+ voices: VFVoice[],
20
+ stave: Stave
21
+ ) {
22
+ const contexts = formatter.getTickContexts();
23
+ const lastTickKey = contexts?.list[contexts.list.length - 1];
24
+
25
+ if (
26
+ !contexts ||
27
+ voices.length === 0 ||
28
+ lastTickKey === undefined ||
29
+ lastTickKey <= 0
30
+ ) {
31
+ return;
32
+ }
33
+
34
+ const totalTicks = voices[0]?.getTotalTicks().value() ?? 0;
35
+
36
+ if (!(totalTicks > 0)) {
37
+ return;
38
+ }
39
+
40
+ // Tick context keys are ticks × the formatter's resolution multiplier (the
41
+ // lcm that keeps tuplet ticks integral: 3 with triplets, 5 with
42
+ // quintuplets…), while getTotalTicks() is in plain ticks. Compare in plain
43
+ // ticks, or every position scales by the multiplier and the overflow
44
+ // correction below crushes the last tick flush against the note end.
45
+ const multiplier = contexts.resolutionMultiplier || 1;
46
+ const lastTick = lastTickKey / multiplier;
47
+
48
+ // Tick context x is relative to noteStartX + the stave's left note padding
49
+ // (see Tickable.getAbsoluteX), so the usable span ends at noteEndX. The
50
+ // padding is derived from Stave statics because the `Metrics` module is not
51
+ // re-exported by every vexflow build.
52
+ const stavePadding = Stave.defaultPadding - Stave.rightPadding;
53
+ const noteAreaWidth =
54
+ stave.getNoteEndX() - stave.getNoteStartX() - stavePadding;
55
+ const firstLeft = contexts.map[contexts.list[0]!]!.getMetrics().totalLeftPx;
56
+ let span = noteAreaWidth - firstLeft;
57
+
58
+ const lastContext = contexts.map[lastTickKey]!;
59
+ const lastOverflow =
60
+ firstLeft +
61
+ (lastTick / totalTicks) * span +
62
+ lastContext.getWidth() -
63
+ noteAreaWidth;
64
+
65
+ if (lastOverflow > 0) {
66
+ span -= (lastOverflow * totalTicks) / lastTick;
67
+ }
68
+
69
+ if (!(span > 0)) {
70
+ return;
71
+ }
72
+
73
+ for (const tickKey of contexts.list) {
74
+ contexts.map[tickKey]!.setX(
75
+ firstLeft + (tickKey / multiplier / totalTicks) * span
76
+ );
77
+ }
78
+ }
@@ -1,6 +1,14 @@
1
1
  export { default as ScoreRenderer } from './ScoreRenderer';
2
2
  export * from './colorScheme';
3
3
  export * from './types';
4
+ export {
5
+ Annotation,
6
+ Articulation,
7
+ Modifier,
8
+ Ornament,
9
+ StaveNote,
10
+ } from 'vexflow';
11
+ export type { RenderContext } from 'vexflow';
4
12
 
5
13
  export {
6
14
  isVexflowNativeDebugEnabled,
@@ -0,0 +1,137 @@
1
+ import { Parenthesis } from 'vexflow';
2
+ import type { BoundingBox, GraceNoteGroup, Modifier } from 'vexflow';
3
+
4
+ import { noteheadWidth } from './scoreParsing';
5
+ import type { VFVoiceNote } from './scoreParsing';
6
+ import type { ScoreItemBounds, ScoreItemLayout } from './types';
7
+
8
+ function hasGraceNotes(
9
+ modifier: Modifier
10
+ ): modifier is Modifier & Pick<GraceNoteGroup, 'getGraceNotes'> {
11
+ return 'getGraceNotes' in modifier;
12
+ }
13
+
14
+ /**
15
+ * Parenthesis draws at its anchor offset without moving itself, so its box
16
+ * is re-anchored where `Parenthesis.draw()` places it.
17
+ */
18
+ function modifierBoundingBox(note: VFVoiceNote, modifier: Modifier) {
19
+ const box = modifier.getBoundingBox();
20
+
21
+ if (modifier.getCategory() !== Parenthesis.CATEGORY) {
22
+ return box;
23
+ }
24
+
25
+ const start = note.getModifierStartXY(
26
+ modifier.getPosition(),
27
+ modifier.checkIndex(),
28
+ { forceFlagRight: true }
29
+ );
30
+
31
+ return box.move(start.x, start.y);
32
+ }
33
+
34
+ /**
35
+ * Union box of a drawn note's modifiers; must run after `draw()` because
36
+ * modifiers take their position from it. undefined without modifiers or when
37
+ * none could be measured.
38
+ */
39
+ export function computeModifierBounds(
40
+ note: VFVoiceNote
41
+ ): ScoreItemBounds | undefined {
42
+ let bounds: ScoreItemBounds | undefined;
43
+
44
+ for (const modifier of note.getModifiers()) {
45
+ if (hasGraceNotes(modifier)) {
46
+ for (const graceNote of modifier.getGraceNotes()) {
47
+ bounds = mergeBox(bounds, graceNote.getBoundingBox());
48
+ }
49
+ } else {
50
+ bounds = mergeBox(bounds, modifierBoundingBox(note, modifier));
51
+ }
52
+ }
53
+
54
+ return bounds;
55
+ }
56
+
57
+ function mergeBox(
58
+ bounds: ScoreItemBounds | undefined,
59
+ box: BoundingBox
60
+ ): ScoreItemBounds | undefined {
61
+ const left = box.getX();
62
+ const top = box.getY();
63
+ const right = left + box.getW();
64
+ const bottom = top + box.getH();
65
+
66
+ if (![left, top, right, bottom].every(Number.isFinite)) {
67
+ return bounds;
68
+ }
69
+
70
+ if (!bounds) {
71
+ return { left, right, top, bottom };
72
+ }
73
+
74
+ return {
75
+ left: Math.min(bounds.left, left),
76
+ right: Math.max(bounds.right, right),
77
+ top: Math.min(bounds.top, top),
78
+ bottom: Math.max(bounds.bottom, bottom),
79
+ };
80
+ }
81
+
82
+ /** Detected structurally because `GhostNote`s lack these getters. */
83
+ type NoteHeadSpan = {
84
+ getNoteHeadBeginX?: () => number;
85
+ getNoteHeadEndX?: () => number;
86
+ };
87
+
88
+ /**
89
+ * Center of a formatted note's visual notehead span. When the getters are
90
+ * missing (GhostNotes) the fallback is the center of a NOTIONAL notehead at
91
+ * the block's left edge, capped by the block width — so a `spacer` rest
92
+ * anchors where a real note's head would sit on the same tick, and external
93
+ * UI aligned to it does not hop when the note appears. Zero-width `hidden`
94
+ * rests keep anchoring at their tick x.
95
+ */
96
+ export function resolveItemHeadCenterX(
97
+ note: VFVoiceNote,
98
+ x: number,
99
+ width: number
100
+ ): number {
101
+ const { getNoteHeadBeginX, getNoteHeadEndX } = note as NoteHeadSpan;
102
+
103
+ if (
104
+ typeof getNoteHeadBeginX === 'function' &&
105
+ typeof getNoteHeadEndX === 'function'
106
+ ) {
107
+ const center =
108
+ (getNoteHeadBeginX.call(note) + getNoteHeadEndX.call(note)) / 2;
109
+
110
+ if (Number.isFinite(center) && center > x && center <= x + width) {
111
+ return center;
112
+ }
113
+ }
114
+
115
+ return x + Math.min(width, noteheadWidth()) / 2;
116
+ }
117
+
118
+ /**
119
+ * Geometry of a drawn item; must run after `draw()` because modifiers only
120
+ * take their position there.
121
+ */
122
+ export function itemLayoutOf(
123
+ note: VFVoiceNote,
124
+ measureIndex: number
125
+ ): ScoreItemLayout {
126
+ const x = note.getAbsoluteX();
127
+ const width = note.getWidth();
128
+ const modifierBounds = computeModifierBounds(note);
129
+
130
+ return {
131
+ x,
132
+ width,
133
+ headCenterX: resolveItemHeadCenterX(note, x, width),
134
+ measureIndex,
135
+ ...(modifierBounds ? { modifierBounds } : {}),
136
+ };
137
+ }
@@ -17,7 +17,7 @@ import {
17
17
  type ResolvedMeasureState,
18
18
  } from './scoreParsing';
19
19
  import type { MeasuredScore } from './measure';
20
- import type { StaffVerticalBounds } from './measure';
20
+ import type { StaffVerticalBounds } from './measureBounds';
21
21
  import type {
22
22
  RendererRect,
23
23
  RendererSize,