vexflow-native 1.2.0 → 1.4.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 (45) hide show
  1. package/README.md +16 -1
  2. package/lib/module/renderer/ScoreRenderer/ScorePlayhead.js +54 -0
  3. package/lib/module/renderer/ScoreRenderer/ScorePlayhead.js.map +1 -0
  4. package/lib/module/renderer/ScoreRenderer/ScoreRenderer.js +36 -4
  5. package/lib/module/renderer/ScoreRenderer/ScoreRenderer.js.map +1 -1
  6. package/lib/module/renderer/ScoreRenderer/index.js +1 -0
  7. package/lib/module/renderer/ScoreRenderer/index.js.map +1 -1
  8. package/lib/module/renderer/ScoreRenderer/useScoreScroll.js +3 -1
  9. package/lib/module/renderer/ScoreRenderer/useScoreScroll.js.map +1 -1
  10. package/lib/module/renderer/constants.js +2 -1
  11. package/lib/module/renderer/constants.js.map +1 -1
  12. package/lib/module/renderer/render.js +60 -9
  13. package/lib/module/renderer/render.js.map +1 -1
  14. package/lib/module/renderer/scale.js +5 -1
  15. package/lib/module/renderer/scale.js.map +1 -1
  16. package/lib/module/renderer/scoreParsing.js +26 -2
  17. package/lib/module/renderer/scoreParsing.js.map +1 -1
  18. package/lib/typescript/src/renderer/ScoreRenderer/ScorePlayhead.d.ts +32 -0
  19. package/lib/typescript/src/renderer/ScoreRenderer/ScorePlayhead.d.ts.map +1 -0
  20. package/lib/typescript/src/renderer/ScoreRenderer/ScoreRenderer.d.ts.map +1 -1
  21. package/lib/typescript/src/renderer/ScoreRenderer/index.d.ts +1 -0
  22. package/lib/typescript/src/renderer/ScoreRenderer/index.d.ts.map +1 -1
  23. package/lib/typescript/src/renderer/ScoreRenderer/types.d.ts +2 -2
  24. package/lib/typescript/src/renderer/ScoreRenderer/types.d.ts.map +1 -1
  25. package/lib/typescript/src/renderer/ScoreRenderer/useScoreScroll.d.ts +5 -1
  26. package/lib/typescript/src/renderer/ScoreRenderer/useScoreScroll.d.ts.map +1 -1
  27. package/lib/typescript/src/renderer/constants.d.ts.map +1 -1
  28. package/lib/typescript/src/renderer/render.d.ts +18 -4
  29. package/lib/typescript/src/renderer/render.d.ts.map +1 -1
  30. package/lib/typescript/src/renderer/scale.d.ts.map +1 -1
  31. package/lib/typescript/src/renderer/scoreParsing.d.ts +11 -0
  32. package/lib/typescript/src/renderer/scoreParsing.d.ts.map +1 -1
  33. package/lib/typescript/src/renderer/types.d.ts +73 -4
  34. package/lib/typescript/src/renderer/types.d.ts.map +1 -1
  35. package/package.json +1 -1
  36. package/src/renderer/ScoreRenderer/ScorePlayhead.tsx +123 -0
  37. package/src/renderer/ScoreRenderer/ScoreRenderer.tsx +45 -1
  38. package/src/renderer/ScoreRenderer/index.ts +1 -0
  39. package/src/renderer/ScoreRenderer/types.ts +2 -2
  40. package/src/renderer/ScoreRenderer/useScoreScroll.ts +7 -1
  41. package/src/renderer/constants.ts +1 -0
  42. package/src/renderer/render.tsx +83 -7
  43. package/src/renderer/scale.ts +4 -0
  44. package/src/renderer/scoreParsing.tsx +27 -1
  45. package/src/renderer/types.ts +77 -4
@@ -6,6 +6,7 @@ import {
6
6
  useDerivedValue,
7
7
  useSharedValue,
8
8
  withDecay,
9
+ type SharedValue,
9
10
  } from 'react-native-reanimated';
10
11
 
11
12
  import type { RendererSize, RendererType } from '../types';
@@ -15,16 +16,21 @@ const EMPTY_SIZE: RendererSize = { width: 0, height: 0 };
15
16
 
16
17
  export function useScoreScroll({
17
18
  contentSize,
19
+ externalScrollOffset,
18
20
  rendererType,
19
21
  scrollEnabled,
20
22
  viewportSize,
21
23
  }: {
22
24
  contentSize: RendererSize;
25
+ /** Controlled offset — pan/clamping write it instead of the internal one.
26
+ * Must be the same SharedValue for the hook's lifetime. */
27
+ externalScrollOffset?: SharedValue<number>;
23
28
  rendererType: RendererType;
24
29
  scrollEnabled: boolean;
25
30
  viewportSize: RendererSize;
26
31
  }): ScoreScrollState {
27
- const scrollOffset = useSharedValue(0);
32
+ const internalScrollOffset = useSharedValue(0);
33
+ const scrollOffset = externalScrollOffset ?? internalScrollOffset;
28
34
  const viewportSizeValue = useSharedValue<RendererSize>(EMPTY_SIZE);
29
35
  const contentSizeValue = useSharedValue<RendererSize>(EMPTY_SIZE);
30
36
  const panStartOffset = useSharedValue(0);
@@ -16,4 +16,5 @@ export const renderOptions: RenderOptions = {
16
16
  pixelRatio: 1,
17
17
  scale: 1,
18
18
  debug: false,
19
+ fixedNoteSpacing: false,
19
20
  };
@@ -13,7 +13,11 @@ import type {
13
13
  MeasureLayoutPlan,
14
14
  ScoreLayoutPlan,
15
15
  } from './layout';
16
- import { indexAttachmentsByOwner, makeVFVoice } from './scoreParsing';
16
+ import {
17
+ indexAttachmentsByOwner,
18
+ makeVFVoice,
19
+ noteheadWidth,
20
+ } from './scoreParsing';
17
21
  import type { ScoreItemsLayout, ScoreOptions } from './types';
18
22
  import type { VFVoiceNote } from './scoreParsing';
19
23
 
@@ -25,7 +29,7 @@ export function renderScore(
25
29
  ctx: VexflowRecordingContext,
26
30
  score: Score,
27
31
  layoutPlan: ScoreLayoutPlan,
28
- _options: ScoreOptions
32
+ options: ScoreOptions
29
33
  ): ScoreItemsLayout {
30
34
  const groupsById = new Map(
31
35
  layoutPlan.groups.map((group) => [group.groupId, group])
@@ -61,6 +65,7 @@ export function renderScore(
61
65
  {
62
66
  isFirstMeasureInSystem: measureIndex === 0,
63
67
  isLastMeasureInSystem: measureIndex === measurePlans.length - 1,
68
+ fixedNoteSpacing: options.render.fixedNoteSpacing,
64
69
  },
65
70
  itemsLayout
66
71
  );
@@ -88,6 +93,65 @@ type StaffRenderArtifacts = {
88
93
  interface RenderMeasureOptions {
89
94
  isFirstMeasureInSystem: boolean;
90
95
  isLastMeasureInSystem: boolean;
96
+ fixedNoteSpacing: boolean;
97
+ }
98
+
99
+ /**
100
+ * Re-positions every tick context at its time-proportional x inside the note
101
+ * area, overriding the formatter's duration-weighted spacing. Runs after
102
+ * `formatToStave` (the same post-format `setX` mechanism the formatter's own
103
+ * tuning uses), so beams, tuplets and modifiers pick the final positions up
104
+ * at draw time. The proportional span is clamped only by the LAST context's
105
+ * width — with a spacer voice on the lattice that width is constant, so the
106
+ * mapping stays a pure function of time.
107
+ */
108
+ export function applyFixedNoteSpacing(
109
+ formatter: Formatter,
110
+ voices: VFVoice[],
111
+ stave: Stave
112
+ ) {
113
+ const contexts = formatter.getTickContexts();
114
+ const lastTick = contexts?.list[contexts.list.length - 1];
115
+
116
+ if (
117
+ !contexts ||
118
+ voices.length === 0 ||
119
+ lastTick === undefined ||
120
+ lastTick <= 0
121
+ ) {
122
+ return;
123
+ }
124
+
125
+ const totalTicks = voices[0]?.getTotalTicks().value() ?? 0;
126
+
127
+ if (!(totalTicks > 0)) {
128
+ return;
129
+ }
130
+
131
+ // Tick context x is relative to noteStartX + the stave's left note padding
132
+ // (see Tickable.getAbsoluteX), so the usable span ends at noteEndX. The
133
+ // padding is derived from Stave statics because the `Metrics` module is not
134
+ // re-exported by every vexflow build.
135
+ const stavePadding = Stave.defaultPadding - Stave.rightPadding;
136
+ const noteAreaWidth =
137
+ stave.getNoteEndX() - stave.getNoteStartX() - stavePadding;
138
+ let span = noteAreaWidth;
139
+
140
+ const lastContext = contexts.map[lastTick]!;
141
+ const lastOverflow =
142
+ (lastTick / totalTicks) * span + lastContext.getWidth() - noteAreaWidth;
143
+
144
+ if (lastOverflow > 0) {
145
+ span -= (lastOverflow * totalTicks) / lastTick;
146
+ }
147
+
148
+ if (!(span > 0)) {
149
+ return;
150
+ }
151
+
152
+ for (const tick of contexts.list) {
153
+ contexts.map[tick]!.setX((tick / totalTicks) * span);
154
+ }
91
155
  }
92
156
 
93
157
  function renderMeasure(
@@ -202,7 +266,12 @@ function renderMeasure(
202
266
  // Format against the stave with the narrowest note area, so notes never
203
267
  // overrun a stave whose clef or time signature pushes its note start
204
268
  // further right.
205
- formatter.formatToStave(allVoices, getFormatReferenceStave(renderedStaves));
269
+ const referenceStave = getFormatReferenceStave(renderedStaves);
270
+ formatter.formatToStave(allVoices, referenceStave);
271
+
272
+ if (options.fixedNoteSpacing && referenceStave) {
273
+ applyFixedNoteSpacing(formatter, allVoices, referenceStave);
274
+ }
206
275
  }
207
276
 
208
277
  collectMeasureItemsLayout(
@@ -285,6 +354,10 @@ function collectMeasureItemsLayout(
285
354
  width: measurePlan.width,
286
355
  staveNoteStartX: stave.getNoteStartX(),
287
356
  staveNoteEndX: stave.getNoteEndX(),
357
+ y: measurePlan.y,
358
+ height: measurePlan.height,
359
+ staveLineTopY: stave.getTopLineTopY(),
360
+ staveLineBottomY: stave.getBottomLineBottomY(),
288
361
  });
289
362
  });
290
363
  }
@@ -296,9 +369,12 @@ type NoteHeadSpan = {
296
369
  };
297
370
 
298
371
  /**
299
- * Center of a formatted note's visual notehead span. Falls back to the block
300
- * center when the getters are missing or the reported span lands outside the
301
- * note block.
372
+ * Center of a formatted note's visual notehead span. When the getters are
373
+ * missing (GhostNotes) the fallback is the center of a NOTIONAL notehead at
374
+ * the block's left edge, capped by the block width — so a `spacer` rest
375
+ * anchors where a real note's head would sit on the same tick, and external
376
+ * UI aligned to it does not hop when the note appears. Zero-width `hidden`
377
+ * rests keep anchoring at their tick x.
302
378
  */
303
379
  export function resolveItemHeadCenterX(
304
380
  note: VFVoiceNote,
@@ -319,7 +395,7 @@ export function resolveItemHeadCenterX(
319
395
  }
320
396
  }
321
397
 
322
- return x + width / 2;
398
+ return x + Math.min(width, noteheadWidth()) / 2;
323
399
  }
324
400
 
325
401
  function drawVoiceItems(
@@ -87,6 +87,10 @@ export function scaleItemsLayoutToViewSpace(
87
87
  width: measure.width * scale,
88
88
  staveNoteStartX: measure.staveNoteStartX * scale,
89
89
  staveNoteEndX: measure.staveNoteEndX * scale,
90
+ y: measure.y * scale,
91
+ height: measure.height * scale,
92
+ staveLineTopY: measure.staveLineTopY * scale,
93
+ staveLineBottomY: measure.staveLineBottomY * scale,
90
94
  })),
91
95
  contentSize: toViewSize(itemsLayout.contentSize, scale),
92
96
  };
@@ -3,6 +3,7 @@ import {
3
3
  Articulation as VFArticulation,
4
4
  Beam,
5
5
  Dot,
6
+ Element,
6
7
  Fraction as VFFraction,
7
8
  GhostNote,
8
9
  ModifierPosition,
@@ -224,6 +225,27 @@ export function indexAttachmentsByOwner(
224
225
  return attachmentsByOwner;
225
226
  }
226
227
 
228
+ // SMuFL noteheadBlack — vexflow 5's entry point does not re-export `Glyphs`.
229
+ const NOTEHEAD_BLACK = '\uE0A4';
230
+
231
+ /**
232
+ * Width a `spacer` rest reserves on its tick: an up-stem flagged note
233
+ * (2 × notehead + padding), the widest ordinary tickable. A TickContext takes
234
+ * the MAX of its members' widths, so reserving the worst case keeps a tick's
235
+ * width constant whether or not a real note shares it — a voice of spacers
236
+ * therefore holds engraved spacing still while notes toggle on and off its
237
+ * lattice. `hidden` rests stay zero-width (pure timing placeholders).
238
+ * Measured live because the value depends on the active music font.
239
+ */
240
+ /** Measured width of the black notehead under the active music font. */
241
+ export function noteheadWidth(): number {
242
+ return Element.measureWidth(NOTEHEAD_BLACK, 'NoteHead');
243
+ }
244
+
245
+ function spacerTickWidth(): number {
246
+ return 2 * noteheadWidth() + StaveNote.minNoteheadPadding;
247
+ }
248
+
227
249
  /**
228
250
  * Builds a VexFlow stave note from a score voice item and clef.
229
251
  */
@@ -234,7 +256,11 @@ export function voiceItemToStaveNote(
234
256
  ): VFVoiceNote {
235
257
  if (item.type === 'rest') {
236
258
  if (item.kind === 'hidden' || item.kind === 'spacer') {
237
- return new GhostNote(durationToVF(item.duration));
259
+ const note = new GhostNote(durationToVF(item.duration));
260
+ if (item.kind === 'spacer') {
261
+ note.setWidth(spacerTickWidth());
262
+ }
263
+ return note;
238
264
  }
239
265
 
240
266
  const note = new StaveNote({
@@ -9,6 +9,44 @@ export type RendererType = 'infiniteScore' | 'document' | 'documentEven';
9
9
  export type ScoreItemStyleOverride = VexflowStyleOverride;
10
10
  export type ScoreItemStyleOverrides = Record<string, ScoreItemStyleOverride>;
11
11
 
12
+ /** Scroll direction of a renderer type: `infiniteScore` pans horizontally,
13
+ * the document types scroll vertically. */
14
+ export type ScrollAxis = 'horizontal' | 'vertical';
15
+
16
+ /**
17
+ * Position of the playhead overlay, in the same view-space coordinates as
18
+ * {@link ScoreItemsLayout} (scroll offset 0). `x` is the playhead's center
19
+ * line; `y`/`height` bound the system band it should span. `null` hides it.
20
+ */
21
+ export interface ScorePlayheadState {
22
+ x: number;
23
+ y: number;
24
+ height: number;
25
+ }
26
+
27
+ /** Appearance of the playhead overlay; unset fields use renderer defaults. */
28
+ export interface ScorePlayheadStyle {
29
+ /** Defaults to the resolved color scheme's foreground. */
30
+ color?: string;
31
+ /** Line width in view-space points, default 2. */
32
+ width?: number;
33
+ /** Corner radius, default half the width. */
34
+ borderRadius?: number;
35
+ /** Opacity while visible, default 0.9. */
36
+ opacity?: number;
37
+ }
38
+
39
+ /**
40
+ * Scroll envelope of the current recording pass, in view space. `maxScroll`
41
+ * is the largest valid scroll offset along `axis` (0 when content fits).
42
+ */
43
+ export interface ScoreScrollGeometry {
44
+ axis: ScrollAxis;
45
+ viewportSize: RendererSize;
46
+ contentSize: RendererSize;
47
+ maxScroll: number;
48
+ }
49
+
12
50
  export interface ScoreRendererProps {
13
51
  score: Score;
14
52
  defaultFont: string;
@@ -19,12 +57,30 @@ export interface ScoreRendererProps {
19
57
  options?: ScoreRendererOptions;
20
58
  scrollEnabled?: boolean;
21
59
  showScrollbars?: boolean;
60
+ /**
61
+ * Controlled scroll offset along the renderer's {@link ScrollAxis}. When
62
+ * provided, the renderer reads AND writes this value (pan gesture, decay,
63
+ * re-clamping) instead of an internal one, and external writes move the
64
+ * content. Must be the same SharedValue for the component's lifetime.
65
+ */
66
+ scrollOffset?: SharedValue<number>;
67
+ /**
68
+ * Playhead overlay position in {@link ScoreItemsLayout} view-space
69
+ * coordinates (scroll offset 0); `null` hides the overlay. Drivable from
70
+ * the UI thread — the overlay follows scrolling and clipping on its own.
71
+ */
72
+ playhead?: SharedValue<ScorePlayheadState | null>;
73
+ playheadStyle?: ScorePlayheadStyle;
74
+ /**
75
+ * Fired whenever the scroll envelope changes: the axis, the viewport size,
76
+ * or the view-space content size (and with it `maxScroll`).
77
+ */
78
+ onScrollGeometry?: (geometry: ScoreScrollGeometry) => void;
22
79
  /**
23
80
  * Fired after every recording pass with the final geometry of the rendered
24
81
  * score. Coordinates are view-space points at scroll offset 0 and do not
25
- * track scrolling. For the `document` and `documentEven` renderer types they
26
- * map 1:1 to view-local coordinates; for `infiniteScore` they cannot be
27
- * mapped to on-screen positions.
82
+ * track scrolling; subtract the current scroll offset along the scroll
83
+ * axis to obtain on-screen positions.
28
84
  *
29
85
  * @param layout Geometry of all rendered items and measures.
30
86
  */
@@ -58,6 +114,14 @@ export interface RenderOptions {
58
114
  */
59
115
  scale: number;
60
116
  debug: boolean;
117
+ /**
118
+ * Position every tick context at its time-proportional x inside the note
119
+ * area instead of VexFlow's duration-weighted spacing. With a `spacer`
120
+ * voice covering each lattice tick, engraved x positions become a pure
121
+ * function of time — content edits cannot reflow them. Meant for
122
+ * step-editor previews; default false (classic engraving spacing).
123
+ */
124
+ fixedNoteSpacing: boolean;
61
125
  }
62
126
 
63
127
  export interface ScoreOptions {
@@ -88,7 +152,8 @@ export interface ScoreItemLayout {
88
152
  /**
89
153
  * Center of the item's notehead span — the coordinate external UI should
90
154
  * align to. Items without a notehead (hidden and spacer rests) fall back to
91
- * the block center.
155
+ * the center of a notional notehead at the block's left edge, so they agree
156
+ * with a real note on the same tick.
92
157
  */
93
158
  headCenterX: number;
94
159
  measureIndex: number;
@@ -112,6 +177,14 @@ export interface ScoreMeasureItemsLayout {
112
177
  staveNoteStartX: number;
113
178
  /** X where the note area of this stave ends. */
114
179
  staveNoteEndX: number;
180
+ /** Top of the measure's system block (all staves of the group). */
181
+ y: number;
182
+ /** Height of the measure's system block. */
183
+ height: number;
184
+ /** Top edge of this stave's top staff line. */
185
+ staveLineTopY: number;
186
+ /** Bottom edge of this stave's bottom staff line. */
187
+ staveLineBottomY: number;
115
188
  }
116
189
 
117
190
  /**