vexflow 5.1.1 → 5.2.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.
- package/README.md +8 -6
- package/build/cjs/vexflow-bravura.js +2 -2
- package/build/cjs/vexflow-bravura.js.map +1 -1
- package/build/cjs/vexflow-core.js +2 -2
- package/build/cjs/vexflow-core.js.map +1 -1
- package/build/cjs/vexflow-debug-with-tests.js +2916 -484
- package/build/cjs/vexflow-debug-with-tests.js.map +1 -1
- package/build/cjs/vexflow-debug.js +2761 -451
- package/build/cjs/vexflow-debug.js.map +1 -1
- package/build/cjs/vexflow.js +2 -2
- package/build/cjs/vexflow.js.map +1 -1
- package/build/esm/src/accidental.js +3 -0
- package/build/esm/src/articulation.js +27 -14
- package/build/esm/src/beam.js +12 -2
- package/build/esm/src/chordkerning.js +98 -0
- package/build/esm/src/chordsymbol.js +108 -27
- package/build/esm/src/clef.js +3 -0
- package/build/esm/src/clefnote.js +5 -1
- package/build/esm/src/dot.js +1 -1
- package/build/esm/src/easyscore/composition.js +297 -0
- package/build/esm/src/easyscore/elementindex.js +81 -0
- package/build/esm/src/easyscore/elements.js +324 -0
- package/build/esm/src/easyscore/policies.js +69 -0
- package/build/esm/src/easyscore/rhythm.js +42 -0
- package/build/esm/src/easyscore.js +448 -26
- package/build/esm/src/element.js +13 -2
- package/build/esm/src/factory.js +256 -60
- package/build/esm/src/formatter.js +32 -4
- package/build/esm/src/keysignature.js +9 -1
- package/build/esm/src/keysignote.js +2 -2
- package/build/esm/src/note.js +5 -0
- package/build/esm/src/parenthesis.js +1 -1
- package/build/esm/src/parser.js +6 -1
- package/build/esm/src/proportionalformatter.js +186 -0
- package/build/esm/src/registry.js +27 -0
- package/build/esm/src/sheet.js +20 -3
- package/build/esm/src/stave.js +9 -2
- package/build/esm/src/stavenote.js +12 -12
- package/build/esm/src/stemmablenote.js +6 -1
- package/build/esm/src/tickable.js +5 -0
- package/build/esm/src/tuplet.js +3 -2
- package/build/esm/src/version.js +3 -3
- package/build/esm/src/vexflow.js +2 -1
- package/build/esm/tests/beam_tests.js +35 -0
- package/build/esm/tests/easyscore_tests.js +40 -0
- package/build/esm/tests/easyscore_types.js +65 -0
- package/build/esm/tests/formatter_tests.js +41 -0
- package/build/esm/tests/node/chord-kerning-tests.mjs +173 -0
- package/build/esm/tests/node/easyscore-rendering-tests.mjs +332 -0
- package/build/esm/tests/node/easyscore-tests.mjs +953 -0
- package/build/esm/tests/node/proportional-spacing-tests.mjs +214 -0
- package/build/esm/tests/node/tool-security-tests.mjs +85 -0
- package/build/esm/tools/easyscore-api-audit.mjs +58 -0
- package/build/esm/tools/easyscore-composition.mjs +62 -0
- package/build/esm/tools/easyscore-example.mjs +16 -0
- package/build/esm/tools/easyscore-families.mjs +61 -0
- package/build/esm/tools/easyscore-references-example.mjs +26 -0
- package/build/esm/tools/easyscore-rhythm.mjs +58 -0
- package/build/esm/tools/easyscore-structure.mjs +88 -0
- package/build/esm/tools/explorer/catalog/entries.mjs +6 -0
- package/build/esm/tools/explorer/catalog/expression.mjs +68 -0
- package/build/esm/tools/explorer/catalog/helpers.mjs +29 -0
- package/build/esm/tools/explorer/catalog/layout.mjs +109 -0
- package/build/esm/tools/explorer/catalog/notes.mjs +61 -0
- package/build/esm/tools/explorer/catalog/staves.mjs +85 -0
- package/build/esm/tools/explorer/catalog/strings.mjs +25 -0
- package/build/esm/tools/explorer/catalog.mjs +14 -0
- package/build/types/src/accidental.d.ts +2 -0
- package/build/types/src/articulation.d.ts +6 -0
- package/build/types/src/beam.d.ts +2 -0
- package/build/types/src/chordkerning.d.ts +42 -0
- package/build/types/src/chordsymbol.d.ts +19 -0
- package/build/types/src/clef.d.ts +2 -0
- package/build/types/src/easyscore/composition.d.ts +107 -0
- package/build/types/src/easyscore/elementindex.d.ts +30 -0
- package/build/types/src/easyscore/elements.d.ts +156 -0
- package/build/types/src/easyscore/policies.d.ts +12 -0
- package/build/types/src/easyscore/rhythm.d.ts +9 -0
- package/build/types/src/easyscore.d.ts +153 -11
- package/build/types/src/element.d.ts +5 -1
- package/build/types/src/factory.d.ts +91 -8
- package/build/types/src/formatter.d.ts +7 -1
- package/build/types/src/keysignature.d.ts +3 -0
- package/build/types/src/keysignote.d.ts +1 -1
- package/build/types/src/note.d.ts +2 -0
- package/build/types/src/parser.d.ts +5 -0
- package/build/types/src/proportionalformatter.d.ts +30 -0
- package/build/types/src/registry.d.ts +4 -0
- package/build/types/src/stave.d.ts +2 -0
- package/build/types/src/stavenote.d.ts +1 -1
- package/build/types/src/stemmablenote.d.ts +6 -1
- package/build/types/src/tickable.d.ts +2 -0
- package/build/types/src/vexflow.d.ts +2 -1
- package/changelog/CHANGELOG.md +57 -0
- package/package.json +19 -7
- package/tests/flow.html +10 -3
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { VexFlowTests } from './vexflow_test_helpers.js';
|
|
2
2
|
import { Articulation } from '../src/articulation.js';
|
|
3
3
|
import { EasyScore } from '../src/easyscore.js';
|
|
4
|
+
import { Factory } from '../src/factory.js';
|
|
4
5
|
import { FretHandFinger } from '../src/frethandfinger.js';
|
|
5
6
|
import { Modifier } from '../src/modifier.js';
|
|
6
7
|
import { Parenthesis } from '../src/parenthesis.js';
|
|
@@ -17,6 +18,7 @@ const EasyScoreTests = {
|
|
|
17
18
|
QUnit.test('Dots', dots);
|
|
18
19
|
QUnit.test('Types', types);
|
|
19
20
|
QUnit.test('Options', options);
|
|
21
|
+
QUnit.test('Automatic beam stem policy', beamStemPolicy);
|
|
20
22
|
const run = VexFlowTests.runTests;
|
|
21
23
|
run('Draw Basic', drawBasicTest);
|
|
22
24
|
run('Draw Different KeySignature', drawDiffKeysig);
|
|
@@ -486,3 +488,41 @@ function keys(options) {
|
|
|
486
488
|
}
|
|
487
489
|
VexFlowTests.register(EasyScoreTests);
|
|
488
490
|
export { EasyScoreTests };
|
|
491
|
+
function beamStemPolicy(assert) {
|
|
492
|
+
const factory = new Factory({ renderer: { elementId: null, width: 500, height: 200 } });
|
|
493
|
+
const score = factory.EasyScore();
|
|
494
|
+
const phrase = 'C4/8, D4, E4, F4, G4, A4, B4';
|
|
495
|
+
const directions = (notes) => notes.map((note) => note.getStemDirection());
|
|
496
|
+
const automatic = score.notes(phrase);
|
|
497
|
+
assert.equal(new Set(directions(automatic)).size, 2, 'Unbeamed phrase crosses stem boundary');
|
|
498
|
+
score.beam(automatic);
|
|
499
|
+
assert.ok(automatic.every((note) => note.getStemDirection() === Stem.UP), 'Group chooses up stems');
|
|
500
|
+
assert.ok(automatic.every((note) => note.isAutoStem()), 'Beaming retains automatic policy');
|
|
501
|
+
const extended = [...automatic, ...score.notes('C6/8, C6, C6, C6, C6, C6, C6, C6')];
|
|
502
|
+
score.beam(extended);
|
|
503
|
+
assert.ok(extended.every((note) => note.getStemDirection() === Stem.DOWN), 'Regrouping recalculates direction');
|
|
504
|
+
const explicit = score.notes(phrase, { stem: 'down' });
|
|
505
|
+
score.beam(explicit);
|
|
506
|
+
assert.ok(explicit.every((note) => note.getStemDirection() === Stem.DOWN && !note.isAutoStem()), 'Explicit voice stays down');
|
|
507
|
+
score.beam(explicit, { autoStem: true });
|
|
508
|
+
assert.ok(explicit.every((note) => note.getStemDirection() === Stem.UP), 'Explicit beam override selects group direction');
|
|
509
|
+
const pinned = score.notes('C4/8, D4[stem="down"], E4');
|
|
510
|
+
score.beam(pinned);
|
|
511
|
+
assert.deepEqual(directions(pinned), [Stem.DOWN, Stem.DOWN, Stem.DOWN], 'Automatic stems follow one explicit pitch');
|
|
512
|
+
assert.notOk(pinned[1].isAutoStem(), 'Per-note override stays explicit');
|
|
513
|
+
const manual = score.notes('C4/8, D4, E4');
|
|
514
|
+
manual[0].setStemDirection(Stem.DOWN);
|
|
515
|
+
score.beam(manual);
|
|
516
|
+
assert.deepEqual(directions(manual), [Stem.DOWN, Stem.DOWN, Stem.DOWN], 'Setter opts out of automatic direction');
|
|
517
|
+
const mixed = score.notes('C4/8[stem="up"], B4[stem="down"], D4');
|
|
518
|
+
const before = directions(mixed);
|
|
519
|
+
score.beam(mixed);
|
|
520
|
+
assert.deepEqual(directions(mixed), before, 'Conflicting explicit directions remain mixed');
|
|
521
|
+
const preserved = score.notes(phrase);
|
|
522
|
+
const individual = directions(preserved);
|
|
523
|
+
score.beam(preserved, { autoStem: false });
|
|
524
|
+
assert.deepEqual(directions(preserved), individual, 'Explicit false preserves per-note automatic directions');
|
|
525
|
+
const lowLevel = score.notes(phrase);
|
|
526
|
+
factory.Beam({ notes: lowLevel });
|
|
527
|
+
assert.deepEqual(directions(lowLevel), individual, 'Factory keeps its low-level direction-preserving default');
|
|
528
|
+
}
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
2
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
3
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
4
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
5
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
6
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
7
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
8
|
+
});
|
|
9
|
+
};
|
|
10
|
+
import { Annotation, EasyScore, Fraction } from '../src/index.js';
|
|
11
|
+
export function easyScoreTypes() {
|
|
12
|
+
const score = EasyScore.create({ renderer: { elementId: 'score', width: 500, height: 240 } });
|
|
13
|
+
score.autoBeam(score.notes('C4/8, D4'), { groups: [new Fraction(2, 8)], autoStem: false });
|
|
14
|
+
score.make.tuplet({ notes: score.notes('C4/8, D4, E4'), options: { numNotes: 3, notesOccupied: 2 } });
|
|
15
|
+
score.make.hairpin({ type: 1 });
|
|
16
|
+
const composed = score.compose({
|
|
17
|
+
staves: [
|
|
18
|
+
{ id: 'staff', measures: [{ voices: [{ notes: [{ note: { keys: ['c/4'], duration: 'w' }, id: 'whole' }] }] }] },
|
|
19
|
+
],
|
|
20
|
+
spans: [{ kind: 'tie', from: 'whole' }],
|
|
21
|
+
});
|
|
22
|
+
composed.sheet.setWidth(400);
|
|
23
|
+
score.withDefaults({ clef: 'bass' }, (s) => s.notes('C3/w'));
|
|
24
|
+
score.compose({ staves: [], accidentals: 'automatic' });
|
|
25
|
+
score.withDefaults({}, () => __awaiter(this, void 0, void 0, function* () { return 1; }));
|
|
26
|
+
const tab = score.make.tabNote({ positions: [{ str: 2, fret: 'X' }], duration: 'q' }, true);
|
|
27
|
+
score.make.tabTie({ firstNote: tab }, 'H');
|
|
28
|
+
score.make.tabTie({ firstNote: score.make.note({ keys: ['c/4'], duration: 'q' }) });
|
|
29
|
+
score.attachToStave(score.make.tabStave(), score.make.clef('tab'));
|
|
30
|
+
score.make.tabNote({ positions: [{ str: 'one', fret: 3 }], duration: 'q' });
|
|
31
|
+
score.attach(tab, score.make.clef('tab'));
|
|
32
|
+
const nativeNote = score.make.note({ keys: ['c/4/x'], duration: 'q', autoStem: true });
|
|
33
|
+
score.sequence(['C4/q', nativeNote, { keys: ['d/4'], duration: 'q' }]);
|
|
34
|
+
score.make.graceNoteGroup({ notes: [score.make.graceNote({ keys: ['d/4'], duration: '16', slash: true })] });
|
|
35
|
+
score.make.articulation({ type: 'fermata' }).setBetweenLines(false);
|
|
36
|
+
score.make.ornament('tr', { upperAccidental: '#' });
|
|
37
|
+
score.make.tremolo(3);
|
|
38
|
+
score.make.note({ keys: ['c/4'] });
|
|
39
|
+
score.make.tremolo('three');
|
|
40
|
+
const notes = score.notes('C4/h, C4', { octaveShift: 1, durationOverride: new Fraction(1, 2) });
|
|
41
|
+
const annotation = score.make.annotation({ text: 'dolce' });
|
|
42
|
+
score.attach(notes[0], annotation);
|
|
43
|
+
const tie = score.make.tie({ from: notes[0], to: notes[1] });
|
|
44
|
+
tie.setDirection(1);
|
|
45
|
+
const adopted = score.adopt(new Annotation('extension'), 'modifier');
|
|
46
|
+
const resolved = score.getElement(adopted.getAttribute('id'));
|
|
47
|
+
const named = score.getNote(notes[0].getAttribute('id'));
|
|
48
|
+
const source = score.getSource(named);
|
|
49
|
+
if (resolved && source)
|
|
50
|
+
EasyScore.getSource(resolved);
|
|
51
|
+
score.make.tie({ from: named });
|
|
52
|
+
score.adopt(annotation, 'automatic');
|
|
53
|
+
score.make.tie({ from: score.getElement('note') });
|
|
54
|
+
const voice = score.voice(notes, { time: { numBeats: 4, beatValue: 4 }, mode: 'soft', softmaxFactor: 1 });
|
|
55
|
+
const system = score.system();
|
|
56
|
+
score.sheet({ width: 600 }).addSystem();
|
|
57
|
+
const built = score.build((builder) => builder.make.annotation({ text: 'batch' }));
|
|
58
|
+
built.setText('native');
|
|
59
|
+
score.build(() => __awaiter(this, void 0, void 0, function* () { return 1; }));
|
|
60
|
+
system.addStave({ voices: [voice] });
|
|
61
|
+
score.notes('C4/q', { octaveShift: 'one' });
|
|
62
|
+
score.voice(notes, { mode: 'relaxed' });
|
|
63
|
+
score.make.annotation({ text: 123 });
|
|
64
|
+
score.make.tie({ from: annotation });
|
|
65
|
+
}
|
|
@@ -64,6 +64,7 @@ const FormatterTests = {
|
|
|
64
64
|
run('Notes with Tab', notesWithTab);
|
|
65
65
|
run('Multiple Staves - Justified', multiStaves, { debug: true });
|
|
66
66
|
run('Softmax', softMax);
|
|
67
|
+
run('Strict proportional spacing', strictProportionalSpacing);
|
|
67
68
|
run('Mixtime', mixTime);
|
|
68
69
|
run('Tight', tightNotes1);
|
|
69
70
|
run('Tight 2', tightNotes2);
|
|
@@ -757,3 +758,43 @@ function annotations(options) {
|
|
|
757
758
|
}
|
|
758
759
|
VexFlowTests.register(FormatterTests);
|
|
759
760
|
export { FormatterTests };
|
|
761
|
+
function strictProportionalSpacing(options) {
|
|
762
|
+
const f = VexFlowTests.makeFactory(options, 850, 540);
|
|
763
|
+
const score = f.EasyScore();
|
|
764
|
+
const notes = score.notes('C5/16, D5, E5, F5, G5/8, A5, B5/q, C6', { stem: 'up' });
|
|
765
|
+
score.beam(notes.slice(0, 4));
|
|
766
|
+
score.beam(notes.slice(4, 6));
|
|
767
|
+
const quarters = score.notes('C4/q, E4, G4, E4', { stem: 'down' });
|
|
768
|
+
const system = f.System({
|
|
769
|
+
x: 25,
|
|
770
|
+
y: 40,
|
|
771
|
+
autoWidth: true,
|
|
772
|
+
formatIterations: 4,
|
|
773
|
+
details: { spacing: { mode: 'proportional', pixelsPerQuarter: 140 } },
|
|
774
|
+
});
|
|
775
|
+
system.addStave({ voices: [score.voice(notes), score.voice(quarters)] }).addClef('treble');
|
|
776
|
+
const thirds = score.notes('C5/8, D5, E5, F5/q, G5/h');
|
|
777
|
+
const sevenths = score.notes('C4/8, D4, E4, F4, G4, A4, B4, C5/h');
|
|
778
|
+
score.tuplet(thirds.slice(0, 3), { numNotes: 3, notesOccupied: 2 });
|
|
779
|
+
score.tuplet(sevenths.slice(0, 7), { numNotes: 7, notesOccupied: 4 });
|
|
780
|
+
score.beam(thirds.slice(0, 3));
|
|
781
|
+
score.beam(sevenths.slice(0, 7));
|
|
782
|
+
system.addStave({ voices: [score.voice(thirds)] }).addClef('treble');
|
|
783
|
+
system.addStave({ voices: [score.voice(sevenths)] }).addClef('treble');
|
|
784
|
+
f.draw();
|
|
785
|
+
const close = (a, b, label) => options.assert.ok(Math.abs(a - b) < 1e-7, label);
|
|
786
|
+
const origin = notes[0].getAbsoluteX();
|
|
787
|
+
[0, 35, 70, 105, 140, 210, 280, 420].forEach((offset, i) => close(notes[i].getAbsoluteX(), origin + offset, 'Fixed onset'));
|
|
788
|
+
quarters.forEach((note, i) => close(note.getAbsoluteX(), origin + i * 140, 'Voices align'));
|
|
789
|
+
close(thirds[4].getAbsoluteX(), sevenths[7].getAbsoluteX(), 'Rational endpoints align');
|
|
790
|
+
sevenths.slice(0, 7).forEach((note) => {
|
|
791
|
+
options.assert.equal(note.getStemDirection(), note.getBeam().getStemDirection(), 'Tuplet stem agrees with beam');
|
|
792
|
+
});
|
|
793
|
+
const ctx = f.getContext();
|
|
794
|
+
ctx.save().setStrokeStyle('#b6c4c0').setLineWidth(0.5);
|
|
795
|
+
for (let beat = 0; beat <= 4; beat++) {
|
|
796
|
+
const x = origin + beat * 140;
|
|
797
|
+
ctx.beginPath().moveTo(x, 55).lineTo(x, 505).stroke();
|
|
798
|
+
}
|
|
799
|
+
ctx.restore();
|
|
800
|
+
}
|
|
@@ -0,0 +1,173 @@
|
|
|
1
|
+
import assert from 'node:assert/strict';
|
|
2
|
+
import test from 'node:test';
|
|
3
|
+
import { createCanvas, registerFont } from 'canvas';
|
|
4
|
+
import { JSDOM } from 'jsdom';
|
|
5
|
+
const VF = await import(new URL('../../build/esm/entry/vexflow-core.js', import.meta.url));
|
|
6
|
+
const { ChordSymbol, Element, StaveNote } = VF;
|
|
7
|
+
const { Glyphs } = VF.VexFlow;
|
|
8
|
+
for (const [family, file] of [
|
|
9
|
+
['Academico', 'academico/academico'],
|
|
10
|
+
['Bravura', 'bravura/bravura'],
|
|
11
|
+
['Petaluma Script', 'petalumascript/petalumascript'],
|
|
12
|
+
['Petaluma', 'petaluma/petaluma'],
|
|
13
|
+
])
|
|
14
|
+
registerFont(new URL(`../../node_modules/@vexflow-fonts/${file}.otf`, import.meta.url).pathname, { family });
|
|
15
|
+
Element.setTextMeasurementCanvas(createCanvas(1, 1));
|
|
16
|
+
const sup = { symbolModifier: ChordSymbol.symbolModifiers.SUPERSCRIPT };
|
|
17
|
+
const sub = { symbolModifier: ChordSymbol.symbolModifiers.SUBSCRIPT };
|
|
18
|
+
const state = () => ({ leftShift: 0, rightShift: 0, topTextLine: 0, textLine: 0 });
|
|
19
|
+
const close = (a, b, tolerance = 0.04) => assert.ok(Math.abs(a - b) < tolerance, `${a} ≈ ${b}`);
|
|
20
|
+
function chord(mode = 'auto', family = 'Academico,Bravura', size = 12) {
|
|
21
|
+
const symbol = new ChordSymbol().setFont(family, size).setKerning(mode);
|
|
22
|
+
new StaveNote({ keys: ['c/5'], duration: 'q' }).addModifier(symbol, 0);
|
|
23
|
+
return symbol;
|
|
24
|
+
}
|
|
25
|
+
function format(symbol) {
|
|
26
|
+
ChordSymbol.format([symbol], state());
|
|
27
|
+
return symbol;
|
|
28
|
+
}
|
|
29
|
+
const diminished = (symbol) => symbol.addText('A').addGlyph('diminished', sup);
|
|
30
|
+
for (const family of ['Academico,Bravura', 'Petaluma Script,Petaluma']) {
|
|
31
|
+
test(`${family}: auto tightens A diminished; none preserves legacy advances`, () => {
|
|
32
|
+
const legacy = format(diminished(chord('none', family)));
|
|
33
|
+
const auto = format(diminished(chord('auto', family)));
|
|
34
|
+
assert.ok(auto.getWidth() < legacy.getWidth());
|
|
35
|
+
assert.equal(legacy.getKerning(), 'none');
|
|
36
|
+
close(legacy.getWidth(), legacy.symbolBlocks.reduce((sum, block) => sum + block.getWidth() + ChordSymbol.minPadding, 0));
|
|
37
|
+
assert.equal(auto.kernedRuns[0].font.family, family.split(',')[0]);
|
|
38
|
+
assert.equal(auto.kernedRuns[1].font.family, family.split(',')[1]);
|
|
39
|
+
});
|
|
40
|
+
test(`${family}: all root/glyph fixtures have finite, contained ink bounds`, () => {
|
|
41
|
+
for (const root of 'ABCDEFG')
|
|
42
|
+
for (const glyph of ['diminished', 'halfDiminished', 'majorSeventh', '#', 'b']) {
|
|
43
|
+
const symbol = format(chord('auto', family).addText(root).addGlyph(glyph, sup));
|
|
44
|
+
const bounds = symbol.getBoundingBox();
|
|
45
|
+
assert.ok([bounds.x, bounds.y, bounds.w, bounds.h, symbol.getWidth()].every(Number.isFinite));
|
|
46
|
+
assert.ok(bounds.x >= -0.001 && bounds.x + bounds.w <= symbol.getWidth() + 0.001);
|
|
47
|
+
}
|
|
48
|
+
});
|
|
49
|
+
test(`${family}: sizes and repeated formatting do not accumulate shifts`, () => {
|
|
50
|
+
const small = format(diminished(chord('auto', family, 12)));
|
|
51
|
+
const large = format(diminished(chord('auto', family, 24)));
|
|
52
|
+
const gap = (symbol) => {
|
|
53
|
+
const [root, script] = symbol.kernedRuns;
|
|
54
|
+
return script.x - root.x - root.metrics.width;
|
|
55
|
+
};
|
|
56
|
+
close(gap(large), gap(small) * 2);
|
|
57
|
+
small.kernedRuns.forEach((run, i) => {
|
|
58
|
+
close(VF.Font.convertSizeToPixelValue(large.kernedRuns[i].font.size), VF.Font.convertSizeToPixelValue(run.font.size) * 2);
|
|
59
|
+
close(large.kernedRuns[i].y, run.y * 2);
|
|
60
|
+
});
|
|
61
|
+
const before = JSON.stringify(large.kernedRuns);
|
|
62
|
+
const box = large.getBoundingBox();
|
|
63
|
+
box.mergeWith(new VF.BoundingBox(-1000, -1000, 1, 1));
|
|
64
|
+
for (let i = 0; i < 3; i++)
|
|
65
|
+
format(large);
|
|
66
|
+
assert.equal(JSON.stringify(large.kernedRuns), before);
|
|
67
|
+
assert.ok(large.getBoundingBox().x >= 0, 'bounds queries never mutate run metrics');
|
|
68
|
+
small.setFontSize(24);
|
|
69
|
+
format(small);
|
|
70
|
+
close(small.getWidth(), large.getWidth());
|
|
71
|
+
});
|
|
72
|
+
}
|
|
73
|
+
test('text spans retain native shaping and adjacent text fragments can form one run', () => {
|
|
74
|
+
const symbol = format(chord().addText('F').addText('maj7'));
|
|
75
|
+
assert.equal(symbol.kernedRuns.length, 1);
|
|
76
|
+
assert.equal(symbol.kernedRuns[0].text, 'Fmaj7');
|
|
77
|
+
const combined = format(chord().addGlyphOrText('C#7'));
|
|
78
|
+
assert.deepEqual(combined.kernedRuns.map((run) => run.text), ['C', Glyphs.csymAccidentalSharp, '7']);
|
|
79
|
+
});
|
|
80
|
+
test('stacked script groups share an anchor and the next group clears both advances', () => {
|
|
81
|
+
for (const [first, second] of [
|
|
82
|
+
[sup, sub],
|
|
83
|
+
[sub, sup],
|
|
84
|
+
]) {
|
|
85
|
+
const symbol = format(chord().addText('F7').addGlyphOrText('b9', first).addGlyphOrText('#11', second).addText('sus4'));
|
|
86
|
+
const runs = symbol.kernedRuns;
|
|
87
|
+
close(runs[1].x, runs[3].x);
|
|
88
|
+
const end = Math.max(...runs.slice(1, -1).map((run) => run.x + run.metrics.width));
|
|
89
|
+
assert.ok(runs[runs.length - 1].x > end);
|
|
90
|
+
const before = JSON.stringify(runs);
|
|
91
|
+
format(symbol);
|
|
92
|
+
assert.equal(JSON.stringify(symbol.kernedRuns), before);
|
|
93
|
+
}
|
|
94
|
+
});
|
|
95
|
+
test('unsupported fonts and styles retain the legacy layout', () => {
|
|
96
|
+
for (const [family, weight, style] of [
|
|
97
|
+
['Arial,Bravura', 'normal', 'normal'],
|
|
98
|
+
['Academico,Bravura', 'bold', 'normal'],
|
|
99
|
+
['Academico,Bravura', 'normal', 'italic'],
|
|
100
|
+
]) {
|
|
101
|
+
const make = (mode) => format(diminished(chord(mode, family).setFont(family, 12, weight, style)));
|
|
102
|
+
const auto = make('auto');
|
|
103
|
+
assert.equal(auto.kernedRuns, undefined);
|
|
104
|
+
close(auto.getWidth(), make('none').getWidth());
|
|
105
|
+
}
|
|
106
|
+
});
|
|
107
|
+
test('reformatting picks up font, content, cache, and mode changes', () => {
|
|
108
|
+
const symbol = format(diminished(chord()));
|
|
109
|
+
symbol.setFont('Petaluma Script,Petaluma', 18).addText('7');
|
|
110
|
+
Element.clearTextMeasurementCache();
|
|
111
|
+
format(symbol);
|
|
112
|
+
const fresh = format(diminished(chord('auto', 'Petaluma Script,Petaluma', 18)).addText('7'));
|
|
113
|
+
assert.deepEqual(symbol.kernedRuns, fresh.kernedRuns);
|
|
114
|
+
format(symbol.setKerning('none'));
|
|
115
|
+
assert.equal(symbol.kernedRuns, undefined);
|
|
116
|
+
close(symbol.getWidth(), symbol.symbolBlocks.reduce((sum, block) => sum + block.getWidth() + ChordSymbol.minPadding, 0));
|
|
117
|
+
format(symbol.setKerning('auto'));
|
|
118
|
+
assert.deepEqual(symbol.kernedRuns, fresh.kernedRuns);
|
|
119
|
+
});
|
|
120
|
+
test('custom profiles are isolated and shift the following advance as well as ink', () => {
|
|
121
|
+
const profile = {
|
|
122
|
+
textFont: 'Academico',
|
|
123
|
+
musicFont: 'Bravura',
|
|
124
|
+
pairs: [{ left: 'A', right: Glyphs.csymDiminished, position: 'superscript', adjustmentEm: 0 }],
|
|
125
|
+
};
|
|
126
|
+
const make = () => diminished(chord()).addText('7');
|
|
127
|
+
const base = format(make().setKerningProfile(profile));
|
|
128
|
+
profile.pairs[0].adjustmentEm = -0.1;
|
|
129
|
+
const adjusted = format(make().setKerningProfile(profile));
|
|
130
|
+
close(base.getWidth() - adjusted.getWidth(), 1.6);
|
|
131
|
+
close(base.kernedRuns[2].x - adjusted.kernedRuns[2].x, 1.6);
|
|
132
|
+
format(base);
|
|
133
|
+
close(base.getWidth() - adjusted.getWidth(), 1.6);
|
|
134
|
+
assert.throws(() => make().setKerningProfile(Object.assign(Object.assign({}, profile), { pairs: [Object.assign(Object.assign({}, profile.pairs[0]), { adjustmentEm: NaN })] })), /Invalid/);
|
|
135
|
+
assert.throws(() => make().setKerning('invalid'), /Kerning/);
|
|
136
|
+
assert.equal(format(chord()).getWidth(), 0);
|
|
137
|
+
});
|
|
138
|
+
test('Canvas and SVG receive identical positioned runs and right alignment uses the computed width', () => {
|
|
139
|
+
const dom = new JSDOM('<div id="svg"></div><canvas id="canvas"></canvas>');
|
|
140
|
+
const previousDocument = globalThis.document;
|
|
141
|
+
globalThis.document = dom.window.document;
|
|
142
|
+
try {
|
|
143
|
+
const results = [];
|
|
144
|
+
for (const backend of [VF.Renderer.Backends.SVG, VF.Renderer.Backends.CANVAS]) {
|
|
145
|
+
const target = document.getElementById(backend === VF.Renderer.Backends.SVG ? 'svg' : 'canvas');
|
|
146
|
+
const renderer = new VF.Renderer(target, backend).resize(600, 240);
|
|
147
|
+
const ctx = renderer.getContext();
|
|
148
|
+
const symbol = diminished(new ChordSymbol().setFont('Academico,Bravura', 18).setKerning('auto'))
|
|
149
|
+
.setHorizontal('right')
|
|
150
|
+
.setReportWidth(false);
|
|
151
|
+
const note = new StaveNote({ keys: ['c/5'], duration: 'q' }).addModifier(symbol, 0);
|
|
152
|
+
const stave = new VF.Stave(30, 80, 500).setContext(ctx);
|
|
153
|
+
note.setStave(stave);
|
|
154
|
+
VF.Formatter.FormatAndDraw(ctx, stave, [note]);
|
|
155
|
+
const calls = [];
|
|
156
|
+
const fillText = ctx.fillText.bind(ctx);
|
|
157
|
+
ctx.fillText = (text, x, y) => {
|
|
158
|
+
calls.push({ text, x, y });
|
|
159
|
+
return fillText(text, x, y);
|
|
160
|
+
};
|
|
161
|
+
symbol.setContext(ctx).draw();
|
|
162
|
+
results.push(calls);
|
|
163
|
+
const anchor = note.getModifierStartXY(VF.Modifier.Position.ABOVE, 0).x;
|
|
164
|
+
const bounds = symbol.getBoundingBox();
|
|
165
|
+
close(bounds.x + bounds.w, anchor);
|
|
166
|
+
}
|
|
167
|
+
assert.deepEqual(results[0], results[1]);
|
|
168
|
+
}
|
|
169
|
+
finally {
|
|
170
|
+
globalThis.document = previousDocument;
|
|
171
|
+
dom.window.close();
|
|
172
|
+
}
|
|
173
|
+
});
|