@mcqj/vexflow 0.0.1-alpha.8 → 0.0.1-alpha.9

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.
@@ -332,6 +332,7 @@ export class Formatter {
332
332
  throw new RuntimeError('NoTickContexts', 'preFormat requires TickContexts');
333
333
  }
334
334
  const { spacingList: contextList, map: contextMap } = contexts;
335
+ this.justifyWidth = justifyWidth;
335
336
  this.lossHistory = [];
336
337
  if (voicesParam && stave) {
337
338
  voicesParam.forEach((voice) => voice.setStave(stave).preFormat());
@@ -490,7 +491,6 @@ export class Formatter {
490
491
  actualWidth = shiftToIdealDistances(calculateIdealDistances(targetWidth));
491
492
  iterations--;
492
493
  }
493
- this.justifyWidth = justifyWidth;
494
494
  this.positionDependentContexts();
495
495
  return this.evaluate();
496
496
  }
@@ -501,6 +501,18 @@ export class Formatter {
501
501
  const { spacingList, dependentList, map } = contexts;
502
502
  if (spacingList.length === 0 || dependentList.length === 0)
503
503
  return;
504
+ const firstSpacingContext = map[spacingList[0]];
505
+ const lastSpacingTick = spacingList[spacingList.length - 1];
506
+ const lastSpacingContext = map[lastSpacingTick];
507
+ const finalTick = lastSpacingTick + lastSpacingContext.getMaxTicks().value();
508
+ const centerX = this.justifyWidth > 0
509
+ ? (this.justifyWidth -
510
+ lastSpacingContext.getMetrics().notePx -
511
+ lastSpacingContext.getMetrics().totalRightPx -
512
+ firstSpacingContext.getMetrics().totalLeftPx) /
513
+ 2
514
+ : undefined;
515
+ const endX = this.justifyWidth > 0 ? this.justifyWidth : undefined;
504
516
  let nextIndex = 0;
505
517
  dependentList.forEach((tick) => {
506
518
  while (nextIndex < spacingList.length && spacingList[nextIndex] < tick) {
@@ -517,11 +529,23 @@ export class Formatter {
517
529
  context.setX(prevX + (nextX - prevX) * ratio);
518
530
  }
519
531
  else if (prevTick !== undefined) {
520
- context.setX(map[prevTick].getX());
532
+ const prevX = map[prevTick].getX();
533
+ if (endX !== undefined && finalTick > prevTick) {
534
+ const ratio = Math.min(1, (tick - prevTick) / (finalTick - prevTick));
535
+ context.setX(prevX + (endX - prevX) * ratio);
536
+ }
537
+ else {
538
+ context.setX(prevX);
539
+ }
521
540
  }
522
541
  else if (nextTick !== undefined) {
523
542
  context.setX(map[nextTick].getX());
524
543
  }
544
+ if (centerX !== undefined) {
545
+ context.getCenterAlignedTickables().forEach((tickable) => {
546
+ tickable.setCenterXShift(centerX - context.getX());
547
+ });
548
+ }
525
549
  });
526
550
  }
527
551
  evaluate() {
@@ -1,3 +1,3 @@
1
1
  export const VERSION = '0.0.1-alpha.8';
2
- export const ID = '42e29bbb2b9017041df14c9597ce8a3252077ea9';
3
- export const DATE = '2026-11-22T12:24:07.969Z';
2
+ export const ID = 'beeb3933573a1cbf00a85d294d8524fabba6f406';
3
+ export const DATE = '2026-11-23T01:19:49.890Z';
@@ -77,6 +77,7 @@ const FormatterTests = {
77
77
  VexFlowTests.test('Inline clef and time signatures preserve note spacing', inlineSpacersPreserveNoteSpacing);
78
78
  VexFlowTests.test('Dependent voices do not affect music layout', dependentVoicesDoNotAffectMusicLayout);
79
79
  VexFlowTests.test('Off-grid dependent voices do not affect music layout', dependentOffGridVoicesDoNotAffectMusicLayout);
80
+ VexFlowTests.test('Off-grid dependent voices honor center alignment', dependentOffGridVoicesCenterAlign);
80
81
  run('Proportional Formatting - No Tuning', proportional, { debug: true, iterations: 0 });
81
82
  run('Proportional Formatting - No Justification', proportional, { justify: false, debug: true, iterations: 0 });
82
83
  run('Proportional Formatting (20 iterations)', proportional, { debug: true, iterations: 20, alpha: 0.5 });
@@ -159,6 +160,31 @@ function dependentOffGridVoicesDoNotAffectMusicLayout(assert) {
159
160
  musicNotes.forEach((note, index) => assert.strictEqual(note.getX(), baselineNotes[index].getX()));
160
161
  assert.ok(lyricNotes[1].getX() > musicNotes[0].getX());
161
162
  assert.ok(lyricNotes[1].getX() < musicNotes[1].getX());
163
+ const lastMusicX = musicNotes[3].getX();
164
+ const trailingLyricX = lyricNotes[13].getX();
165
+ const lastLyricX = lyricNotes[15].getX();
166
+ assert.ok(trailingLyricX > lastMusicX, 'First trailing lyric should follow the final music onset.');
167
+ assert.ok(lastLyricX > trailingLyricX, 'Trailing lyrics should not overlap.');
168
+ assert.ok(lastLyricX < 500, 'Trailing lyrics should stay within the justified width.');
169
+ }
170
+ function dependentOffGridVoicesCenterAlign(assert) {
171
+ const musicNotes = Array.from({ length: 4 }, () => new StaveNote({ keys: ['c/4'], duration: '4' }));
172
+ const musicVoice = new Voice('4/4').addTickables(musicNotes);
173
+ const centered = new TextNote({ text: 'x', duration: '16', alignCenter: true });
174
+ const lyricNotes = Array.from({ length: 16 }, (_, i) => (i === 5 ? centered : new GhostNote({ duration: '16' })));
175
+ const lyricVoice = new Voice('4/4').setMode(Voice.Mode.SOFT).setDependent(true).addTickables(lyricNotes);
176
+ const formatter = new Formatter().joinVoices([musicVoice, lyricVoice]);
177
+ formatter.format([musicVoice, lyricVoice], 500);
178
+ const tickContexts = formatter.getTickContexts();
179
+ assert.ok(tickContexts);
180
+ if (!tickContexts)
181
+ return;
182
+ const firstContext = tickContexts.map[tickContexts.spacingList[0]];
183
+ const lastContext = tickContexts.map[tickContexts.spacingList[tickContexts.spacingList.length - 1]];
184
+ const adjustedJustifyWidth = 500 - lastContext.getMetrics().notePx - lastContext.getMetrics().totalRightPx - firstContext.getMetrics().totalLeftPx;
185
+ const expectedCenterXShift = adjustedJustifyWidth / 2 - centered.getTickContext().getX();
186
+ assert.ok(expectedCenterXShift !== 0);
187
+ assert.ok(Math.abs(centered.getCenterXShift() - expectedCenterXShift) < 1e-9, 'Center-aligned dependent tickables should use the same justified center as spacing tickables.');
162
188
  }
163
189
  function getResolutionMultiplier(assert) {
164
190
  const [voice1, voice2] = getTestVoices();
@@ -0,0 +1,4 @@
1
+ {
2
+ "status": "passed",
3
+ "failedTests": []
4
+ }