vexflow-native 1.8.0 → 2.0.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 (165) hide show
  1. package/README.md +71 -403
  2. package/lib/module/base/FontManager.js +5 -5
  3. package/lib/module/base/FontManager.js.map +1 -1
  4. package/lib/module/base/VexflowCanvas.js +1 -1
  5. package/lib/module/base/VexflowCanvas.js.map +1 -1
  6. package/lib/module/base/VexflowRecordingContext.js +2 -0
  7. package/lib/module/base/VexflowRecordingContext.js.map +1 -1
  8. package/lib/module/base/VexflowRecordingReplay.js +1 -1
  9. package/lib/module/base/VexflowRecordingReplay.js.map +1 -1
  10. package/lib/module/base/index.js +1 -0
  11. package/lib/module/base/index.js.map +1 -1
  12. package/lib/module/musicxml/MusicXmlParser/MeasureParser.js +24 -7
  13. package/lib/module/musicxml/MusicXmlParser/MeasureParser.js.map +1 -1
  14. package/lib/module/musicxml/MusicXmlParser/MusicXmlDirections.js +11 -3
  15. package/lib/module/musicxml/MusicXmlParser/MusicXmlDirections.js.map +1 -1
  16. package/lib/module/musicxml/MusicXmlParser/Spanners.js +21 -5
  17. package/lib/module/musicxml/MusicXmlParser/Spanners.js.map +1 -1
  18. package/lib/module/musicxml/MusicXmlParser/mappers.js +1 -1
  19. package/lib/module/musicxml/MusicXmlParser/mappers.js.map +1 -1
  20. package/lib/module/{state/constants.js → percussion/index.js} +3 -3
  21. package/lib/module/percussion/index.js.map +1 -0
  22. package/lib/module/renderer/ScoreRenderer/ScoreOverlayPicture.js +4 -3
  23. package/lib/module/renderer/ScoreRenderer/ScoreOverlayPicture.js.map +1 -1
  24. package/lib/module/renderer/ScoreRenderer/ScorePlayhead.js +1 -1
  25. package/lib/module/renderer/ScoreRenderer/ScorePlayhead.js.map +1 -1
  26. package/lib/module/renderer/ScoreRenderer/ScoreRenderer.js +5 -1
  27. package/lib/module/renderer/ScoreRenderer/ScoreRenderer.js.map +1 -1
  28. package/lib/module/renderer/ScoreRenderer/utils.js +1 -1
  29. package/lib/module/renderer/ScoreRenderer/utils.js.map +1 -1
  30. package/lib/module/renderer/fixedNoteSpacing.js +56 -0
  31. package/lib/module/renderer/fixedNoteSpacing.js.map +1 -0
  32. package/lib/module/renderer/index.js +1 -0
  33. package/lib/module/renderer/index.js.map +1 -1
  34. package/lib/module/renderer/itemLayout.js +108 -0
  35. package/lib/module/renderer/itemLayout.js.map +1 -0
  36. package/lib/module/renderer/measure.js +30 -106
  37. package/lib/module/renderer/measure.js.map +1 -1
  38. package/lib/module/renderer/measureBounds.js +73 -0
  39. package/lib/module/renderer/measureBounds.js.map +1 -0
  40. package/lib/module/renderer/measureModifiers.js +99 -0
  41. package/lib/module/renderer/measureModifiers.js.map +1 -0
  42. package/lib/module/renderer/noteModifiers.js +212 -0
  43. package/lib/module/renderer/noteModifiers.js.map +1 -0
  44. package/lib/module/renderer/render.js +33 -116
  45. package/lib/module/renderer/render.js.map +1 -1
  46. package/lib/module/renderer/scale.js +16 -2
  47. package/lib/module/renderer/scale.js.map +1 -1
  48. package/lib/module/renderer/scoreParsing.js +37 -199
  49. package/lib/module/renderer/scoreParsing.js.map +1 -1
  50. package/lib/module/renderer/stave.js +19 -3
  51. package/lib/module/renderer/stave.js.map +1 -1
  52. package/lib/module/renderer/useScoreRecording.js +10 -3
  53. package/lib/module/renderer/useScoreRecording.js.map +1 -1
  54. package/lib/module/renderer/vfKeys.js +72 -0
  55. package/lib/module/renderer/vfKeys.js.map +1 -0
  56. package/lib/module/state/index.js +1 -1
  57. package/lib/module/state/index.js.map +1 -1
  58. package/lib/typescript/src/base/FontManager.d.ts +3 -2
  59. package/lib/typescript/src/base/FontManager.d.ts.map +1 -1
  60. package/lib/typescript/src/base/VexflowRecordingContext.d.ts +2 -1
  61. package/lib/typescript/src/base/VexflowRecordingContext.d.ts.map +1 -1
  62. package/lib/typescript/src/base/VexflowRecordingReplay.d.ts +1 -1
  63. package/lib/typescript/src/base/VexflowRecordingReplay.d.ts.map +1 -1
  64. package/lib/typescript/src/base/index.d.ts +1 -0
  65. package/lib/typescript/src/base/index.d.ts.map +1 -1
  66. package/lib/typescript/src/base/types.d.ts +1 -1
  67. package/lib/typescript/src/base/types.d.ts.map +1 -1
  68. package/lib/typescript/src/musicxml/MusicXmlParser/InternalTypes.d.ts +4 -2
  69. package/lib/typescript/src/musicxml/MusicXmlParser/InternalTypes.d.ts.map +1 -1
  70. package/lib/typescript/src/musicxml/MusicXmlParser/MeasureParser.d.ts.map +1 -1
  71. package/lib/typescript/src/musicxml/MusicXmlParser/MusicXmlDirections.d.ts +6 -1
  72. package/lib/typescript/src/musicxml/MusicXmlParser/MusicXmlDirections.d.ts.map +1 -1
  73. package/lib/typescript/src/musicxml/MusicXmlParser/Spanners.d.ts +6 -1
  74. package/lib/typescript/src/musicxml/MusicXmlParser/Spanners.d.ts.map +1 -1
  75. package/lib/typescript/src/musicxml/MusicXmlParser/mappers.d.ts.map +1 -1
  76. package/lib/typescript/src/musicxml/index.d.ts +1 -0
  77. package/lib/typescript/src/musicxml/index.d.ts.map +1 -1
  78. package/lib/typescript/src/percussion/index.d.ts +4 -0
  79. package/lib/typescript/src/percussion/index.d.ts.map +1 -0
  80. package/lib/typescript/src/renderer/ScoreRenderer/ScoreOverlayPicture.d.ts +1 -1
  81. package/lib/typescript/src/renderer/ScoreRenderer/ScoreOverlayPicture.d.ts.map +1 -1
  82. package/lib/typescript/src/renderer/ScoreRenderer/ScorePlayhead.d.ts +1 -1
  83. package/lib/typescript/src/renderer/ScoreRenderer/ScorePlayhead.d.ts.map +1 -1
  84. package/lib/typescript/src/renderer/ScoreRenderer/ScoreRenderer.d.ts.map +1 -1
  85. package/lib/typescript/src/renderer/ScoreRenderer/types.d.ts +1 -1
  86. package/lib/typescript/src/renderer/ScoreRenderer/types.d.ts.map +1 -1
  87. package/lib/typescript/src/renderer/ScoreRenderer/utils.d.ts +1 -1
  88. package/lib/typescript/src/renderer/ScoreRenderer/utils.d.ts.map +1 -1
  89. package/lib/typescript/src/renderer/fixedNoteSpacing.d.ts +17 -0
  90. package/lib/typescript/src/renderer/fixedNoteSpacing.d.ts.map +1 -0
  91. package/lib/typescript/src/renderer/index.d.ts +2 -0
  92. package/lib/typescript/src/renderer/index.d.ts.map +1 -1
  93. package/lib/typescript/src/renderer/itemLayout.d.ts +23 -0
  94. package/lib/typescript/src/renderer/itemLayout.d.ts.map +1 -0
  95. package/lib/typescript/src/renderer/layout.d.ts +1 -1
  96. package/lib/typescript/src/renderer/layout.d.ts.map +1 -1
  97. package/lib/typescript/src/renderer/measure.d.ts +3 -6
  98. package/lib/typescript/src/renderer/measure.d.ts.map +1 -1
  99. package/lib/typescript/src/renderer/measureBounds.d.ts +13 -0
  100. package/lib/typescript/src/renderer/measureBounds.d.ts.map +1 -0
  101. package/lib/typescript/src/renderer/measureModifiers.d.ts +20 -0
  102. package/lib/typescript/src/renderer/measureModifiers.d.ts.map +1 -0
  103. package/lib/typescript/src/renderer/noteModifiers.d.ts +29 -0
  104. package/lib/typescript/src/renderer/noteModifiers.d.ts.map +1 -0
  105. package/lib/typescript/src/renderer/render.d.ts +2 -27
  106. package/lib/typescript/src/renderer/render.d.ts.map +1 -1
  107. package/lib/typescript/src/renderer/scale.d.ts.map +1 -1
  108. package/lib/typescript/src/renderer/scoreParsing.d.ts +14 -42
  109. package/lib/typescript/src/renderer/scoreParsing.d.ts.map +1 -1
  110. package/lib/typescript/src/renderer/stave.d.ts +2 -0
  111. package/lib/typescript/src/renderer/stave.d.ts.map +1 -1
  112. package/lib/typescript/src/renderer/types.d.ts +52 -3
  113. package/lib/typescript/src/renderer/types.d.ts.map +1 -1
  114. package/lib/typescript/src/renderer/useScoreRecording.d.ts +3 -3
  115. package/lib/typescript/src/renderer/useScoreRecording.d.ts.map +1 -1
  116. package/lib/typescript/src/renderer/vfKeys.d.ts +17 -0
  117. package/lib/typescript/src/renderer/vfKeys.d.ts.map +1 -0
  118. package/lib/typescript/src/state/index.d.ts +1 -1
  119. package/lib/typescript/src/state/index.d.ts.map +1 -1
  120. package/lib/typescript/src/state/types.d.ts +23 -5
  121. package/lib/typescript/src/state/types.d.ts.map +1 -1
  122. package/package.json +31 -17
  123. package/src/base/FontManager.ts +5 -11
  124. package/src/base/TextMeasureContext.ts +1 -1
  125. package/src/base/VexflowCanvas.tsx +1 -1
  126. package/src/base/VexflowRecordingContext.ts +2 -4
  127. package/src/base/VexflowRecordingReplay.ts +1 -1
  128. package/src/base/index.ts +1 -0
  129. package/src/base/types.ts +1 -1
  130. package/src/musicxml/MusicXmlParser/InternalTypes.ts +5 -2
  131. package/src/musicxml/MusicXmlParser/MeasureParser.ts +36 -7
  132. package/src/musicxml/MusicXmlParser/MusicXmlDirections.ts +25 -8
  133. package/src/musicxml/MusicXmlParser/Spanners.ts +27 -4
  134. package/src/musicxml/MusicXmlParser/mappers.ts +7 -0
  135. package/src/musicxml/index.ts +1 -0
  136. package/src/percussion/index.ts +7 -0
  137. package/src/renderer/Layout/VerticalSpacing.ts +1 -1
  138. package/src/renderer/ScoreRenderer/ScoreOverlayPicture.tsx +4 -7
  139. package/src/renderer/ScoreRenderer/ScorePlayhead.tsx +1 -1
  140. package/src/renderer/ScoreRenderer/ScoreRenderer.tsx +5 -7
  141. package/src/renderer/ScoreRenderer/types.ts +1 -4
  142. package/src/renderer/ScoreRenderer/utils.ts +1 -1
  143. package/src/renderer/fixedNoteSpacing.ts +78 -0
  144. package/src/renderer/index.ts +8 -0
  145. package/src/renderer/itemLayout.ts +137 -0
  146. package/src/renderer/layout.ts +1 -1
  147. package/src/renderer/measure.tsx +84 -186
  148. package/src/renderer/measureBounds.ts +109 -0
  149. package/src/renderer/measureModifiers.ts +133 -0
  150. package/src/renderer/noteModifiers.ts +298 -0
  151. package/src/renderer/render.tsx +59 -169
  152. package/src/renderer/scale.ts +21 -1
  153. package/src/renderer/scoreParsing.tsx +77 -288
  154. package/src/renderer/stave.ts +22 -3
  155. package/src/renderer/types.ts +66 -3
  156. package/src/renderer/useScoreRecording.ts +12 -4
  157. package/src/renderer/vfKeys.ts +88 -0
  158. package/src/state/index.ts +1 -1
  159. package/src/state/types.ts +30 -5
  160. package/lib/module/musicxml/testfiles/lg-8102429.xml +0 -79620
  161. package/lib/module/state/constants.js.map +0 -1
  162. package/lib/typescript/src/state/constants.d.ts +0 -4
  163. package/lib/typescript/src/state/constants.d.ts.map +0 -1
  164. package/src/musicxml/testfiles/lg-8102429.xml +0 -79620
  165. package/src/state/constants.ts +0 -4
@@ -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,
@@ -1,10 +1,7 @@
1
1
  import { useEffect, useMemo } from 'react';
2
- import { Picture, type SkPicture } from '@shopify/react-native-skia';
3
- import {
4
- runOnUI,
5
- useDerivedValue,
6
- useSharedValue,
7
- } from 'react-native-reanimated';
2
+ import { Picture, type SkPicture } from 'react-native-skia';
3
+ import { useDerivedValue, useSharedValue } from 'react-native-reanimated';
4
+ import { scheduleOnUI } from 'react-native-worklets';
8
5
 
9
6
  import type { VexflowRecordingCommand } from '../../base';
10
7
  import { isVexflowNativeDebugEnabled } from '../../shared/debug';
@@ -110,7 +107,7 @@ export default function ScoreOverlayPicture({
110
107
 
111
108
  useEffect(() => {
112
109
  return () => {
113
- runOnUI(disposeOverlayPictures)(disposal);
110
+ scheduleOnUI(disposeOverlayPictures, disposal);
114
111
  };
115
112
  }, [disposal]);
116
113
 
@@ -1,5 +1,5 @@
1
1
  import React from 'react';
2
- import { RoundedRect, Skia, type SkRRect } from '@shopify/react-native-skia';
2
+ import { RoundedRect, Skia, type SkRRect } from 'react-native-skia';
3
3
  import { useDerivedValue, type SharedValue } from 'react-native-reanimated';
4
4
 
5
5
  import type {
@@ -1,12 +1,6 @@
1
1
  import type React from 'react';
2
2
  import { memo, useCallback, useEffect, useMemo, useRef, useState } from 'react';
3
- import {
4
- Canvas,
5
- Group,
6
- Picture,
7
- Skia,
8
- useCanvasRef,
9
- } from '@shopify/react-native-skia';
3
+ import { Canvas, Group, Picture, Skia, useCanvasRef } from 'react-native-skia';
10
4
  import { StyleSheet, View, type LayoutChangeEvent } from 'react-native';
11
5
  import { GestureDetector } from 'react-native-gesture-handler';
12
6
  import { useDerivedValue } from 'react-native-reanimated';
@@ -40,6 +34,8 @@ const ScoreRenderer: React.FC<ScoreRendererProps> = ({
40
34
  fontManager,
41
35
  colorScheme,
42
36
  itemStyleOverrides,
37
+ decorateItem,
38
+ onDrawItem,
43
39
  options: userOptions = EMPTY_OPTIONS,
44
40
  scrollEnabled = true,
45
41
  showScrollbars = true,
@@ -101,10 +97,12 @@ const ScoreRenderer: React.FC<ScoreRendererProps> = ({
101
97
  layoutPlan,
102
98
  itemsLayout,
103
99
  } = useScoreRecording({
100
+ decorateItem,
104
101
  defaultFont,
105
102
  enabled: hasViewportSize,
106
103
  fontManager,
107
104
  colorScheme: resolvedColorScheme,
105
+ onDrawItem,
108
106
  options,
109
107
  rendererType: effectiveRendererType,
110
108
  score,
@@ -1,7 +1,4 @@
1
- import type {
2
- SkPicture,
3
- SkTypefaceFontProvider,
4
- } from '@shopify/react-native-skia';
1
+ import type { SkPicture, SkTypefaceFontProvider } from 'react-native-skia';
5
2
  import type { GestureType } from 'react-native-gesture-handler';
6
3
  import type { SharedValue } from 'react-native-reanimated';
7
4
 
@@ -3,7 +3,7 @@ import {
3
3
  type SkPicture,
4
4
  type SkTypefaceFontProvider,
5
5
  type Transforms3d,
6
- } from '@shopify/react-native-skia';
6
+ } from 'react-native-skia';
7
7
  import type { SharedValue } from 'react-native-reanimated';
8
8
 
9
9
  import FontManager from '../../base/FontManager';
@@ -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,