vexflow 5.1.2 → 5.3.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 +7 -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 +5325 -1730
- package/build/cjs/vexflow-debug-with-tests.js.map +1 -1
- package/build/cjs/vexflow-debug.js +4228 -1545
- 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 +13 -3
- package/build/esm/src/articulation.js +51 -20
- package/build/esm/src/beam.js +170 -30
- package/build/esm/src/bend.js +24 -19
- package/build/esm/src/clef.js +3 -0
- package/build/esm/src/clefnote.js +5 -1
- package/build/esm/src/curve.js +27 -53
- package/build/esm/src/dot.js +14 -5
- package/build/esm/src/easyscore/composition.js +336 -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 +97 -0
- package/build/esm/src/easyscore/rhythm.js +42 -0
- package/build/esm/src/easyscore.js +448 -26
- package/build/esm/src/element.js +17 -2
- package/build/esm/src/engravinggeometry.js +150 -0
- package/build/esm/src/engravingprofile.js +44 -0
- package/build/esm/src/factory.js +268 -64
- package/build/esm/src/formatter.js +40 -4
- package/build/esm/src/gracenote.js +43 -38
- package/build/esm/src/gracenotegroup.js +53 -56
- package/build/esm/src/index.js +2 -0
- package/build/esm/src/keysignature.js +10 -2
- package/build/esm/src/keysignote.js +2 -2
- package/build/esm/src/modifiercontext.js +11 -0
- package/build/esm/src/note.js +5 -0
- package/build/esm/src/notehead.js +11 -6
- package/build/esm/src/ornament.js +38 -18
- package/build/esm/src/parenthesis.js +1 -1
- package/build/esm/src/parser.js +6 -1
- package/build/esm/src/proportionalformatter.js +234 -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 +140 -127
- package/build/esm/src/stavetie.js +39 -7
- package/build/esm/src/stem.js +26 -11
- package/build/esm/src/stemmablenote.js +6 -1
- package/build/esm/src/tickable.js +5 -0
- package/build/esm/src/tickcontext.js +19 -0
- package/build/esm/src/timesignature.js +5 -0
- package/build/esm/src/tuplet.js +60 -19
- package/build/esm/src/version.js +3 -3
- package/build/esm/src/vexflow.js +6 -1
- package/build/esm/tests/beam_tests.js +35 -0
- package/build/esm/tests/bend_tests.js +14 -1
- package/build/esm/tests/curve_tests.js +14 -4
- package/build/esm/tests/demo_repertoire_tests.js +27 -0
- package/build/esm/tests/demos/drums.js +54 -0
- package/build/esm/tests/demos/electric.js +63 -0
- package/build/esm/tests/demos/greensleeves.js +90 -0
- package/build/esm/tests/demos/joy.js +61 -0
- package/build/esm/tests/demos/kesh.js +80 -0
- package/build/esm/tests/demos/prelude.js +59 -0
- package/build/esm/tests/demos/quartertones.js +50 -0
- package/build/esm/tests/demos/saints.js +65 -0
- package/build/esm/tests/easyscore_tests.js +40 -0
- package/build/esm/tests/easyscore_types.js +65 -0
- package/build/esm/tests/engraving/score-layout.mjs +75 -0
- package/build/esm/tests/engraving/score-persistence.mjs +262 -0
- package/build/esm/tests/engraving/studies.mjs +279 -0
- package/build/esm/tests/formatter_tests.js +51 -1
- package/build/esm/tests/index.js +1 -0
- package/build/esm/tests/node/audit-dependencies-tests.mjs +42 -0
- package/build/esm/tests/node/easyscore-rendering-tests.mjs +332 -0
- package/build/esm/tests/node/easyscore-tests.mjs +1024 -0
- package/build/esm/tests/node/engraving-layout-tests.mjs +449 -0
- package/build/esm/tests/node/local-engraving-tests.mjs +128 -0
- package/build/esm/tests/node/proportional-spacing-tests.mjs +278 -0
- package/build/esm/tests/node/rendering-tests.mjs +6 -2
- package/build/esm/tests/threevoice_tests.js +11 -1
- package/build/esm/tests/vexflow_test_helpers.js +19 -10
- package/build/esm/tools/audit-dependencies.mjs +61 -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/esm/tools/score-fixtures.mjs +23 -6
- package/build/types/src/accidental.d.ts +6 -0
- package/build/types/src/articulation.d.ts +12 -1
- package/build/types/src/beam.d.ts +16 -0
- package/build/types/src/clef.d.ts +2 -0
- package/build/types/src/curve.d.ts +9 -0
- package/build/types/src/dot.d.ts +5 -0
- package/build/types/src/easyscore/composition.d.ts +126 -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 +7 -1
- package/build/types/src/engravinggeometry.d.ts +31 -0
- package/build/types/src/engravingprofile.d.ts +20 -0
- package/build/types/src/factory.d.ts +97 -9
- package/build/types/src/formatter.d.ts +7 -1
- package/build/types/src/gracenote.d.ts +7 -0
- package/build/types/src/gracenotegroup.d.ts +7 -2
- package/build/types/src/index.d.ts +2 -0
- package/build/types/src/keysignature.d.ts +3 -0
- package/build/types/src/keysignote.d.ts +1 -1
- package/build/types/src/modifiercontext.d.ts +4 -0
- package/build/types/src/note.d.ts +2 -0
- package/build/types/src/notehead.d.ts +5 -0
- package/build/types/src/ornament.d.ts +7 -0
- package/build/types/src/parser.d.ts +5 -0
- package/build/types/src/proportionalformatter.d.ts +39 -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 +21 -3
- package/build/types/src/stavetie.d.ts +4 -0
- package/build/types/src/stem.d.ts +10 -1
- package/build/types/src/stemmablenote.d.ts +6 -1
- package/build/types/src/tickable.d.ts +2 -0
- package/build/types/src/tickcontext.d.ts +5 -0
- package/build/types/src/timesignature.d.ts +2 -0
- package/build/types/src/tuplet.d.ts +5 -48
- package/build/types/src/vexflow.d.ts +6 -1
- package/changelog/CHANGELOG.md +52 -0
- package/package.json +13 -3
- package/tests/all.html +277 -0
- package/tests/demo_petzold_tests.ts +435 -0
- package/tests/demos/drums.ts +55 -0
- package/tests/demos/drums.webp +0 -0
- package/tests/demos/electric.ts +70 -0
- package/tests/demos/electric.webp +0 -0
- package/tests/demos/greensleeves.ts +97 -0
- package/tests/demos/greensleeves.webp +0 -0
- package/tests/demos/joy.ts +65 -0
- package/tests/demos/joy.webp +0 -0
- package/tests/demos/kesh.ts +86 -0
- package/tests/demos/kesh.webp +0 -0
- package/tests/demos/prelude.ts +65 -0
- package/tests/demos/prelude.webp +0 -0
- package/tests/demos/quartertones.ts +51 -0
- package/tests/demos/quartertones.webp +0 -0
- package/tests/demos/saints.ts +70 -0
- package/tests/demos/saints.webp +0 -0
- package/tests/flow.html +550 -239
- package/tests/fonts.css +493 -0
- package/tests/minuet.webp +0 -0
package/build/esm/src/parser.js
CHANGED
|
@@ -18,6 +18,7 @@ function flattenMatches(r) {
|
|
|
18
18
|
}
|
|
19
19
|
export class Parser {
|
|
20
20
|
constructor(grammar) {
|
|
21
|
+
this.furthestErrorPos = NO_ERROR_POS;
|
|
21
22
|
this.grammar = grammar;
|
|
22
23
|
this.line = '';
|
|
23
24
|
this.pos = 0;
|
|
@@ -27,8 +28,10 @@ export class Parser {
|
|
|
27
28
|
this.line = line;
|
|
28
29
|
this.pos = 0;
|
|
29
30
|
this.errorPos = NO_ERROR_POS;
|
|
31
|
+
this.furthestErrorPos = NO_ERROR_POS;
|
|
30
32
|
const result = this.expect(this.grammar.begin());
|
|
31
33
|
result.errorPos = this.errorPos;
|
|
34
|
+
result.furthestErrorPos = result.success ? NO_ERROR_POS : this.furthestErrorPos;
|
|
32
35
|
return result;
|
|
33
36
|
}
|
|
34
37
|
matchFail(returnPos) {
|
|
@@ -52,6 +55,7 @@ export class Parser {
|
|
|
52
55
|
};
|
|
53
56
|
}
|
|
54
57
|
else {
|
|
58
|
+
this.furthestErrorPos = Math.max(this.furthestErrorPos, this.pos);
|
|
55
59
|
return { success: false, pos: this.pos };
|
|
56
60
|
}
|
|
57
61
|
}
|
|
@@ -127,6 +131,7 @@ export class Parser {
|
|
|
127
131
|
if (!ruleFunc) {
|
|
128
132
|
throw new RuntimeError('Invalid rule function');
|
|
129
133
|
}
|
|
134
|
+
const start = this.pos;
|
|
130
135
|
let result;
|
|
131
136
|
const rule = ruleFunc.bind(this.grammar)();
|
|
132
137
|
if (rule.token) {
|
|
@@ -156,7 +161,7 @@ export class Parser {
|
|
|
156
161
|
result.results.forEach((r) => matches.push(flattenMatches(r)));
|
|
157
162
|
}
|
|
158
163
|
if (rule.run && result.success) {
|
|
159
|
-
rule.run({ matches });
|
|
164
|
+
rule.run({ matches, start, end: this.pos });
|
|
160
165
|
}
|
|
161
166
|
return result;
|
|
162
167
|
}
|
|
@@ -0,0 +1,234 @@
|
|
|
1
|
+
import { Fraction } from './fraction.js';
|
|
2
|
+
import { TickContext } from './tickcontext.js';
|
|
3
|
+
import { RuntimeError } from './util.js';
|
|
4
|
+
const PADDING = 2;
|
|
5
|
+
const EPSILON = 1e-7;
|
|
6
|
+
function required(value) {
|
|
7
|
+
return (Math.ceil(value * 100) / 100).toFixed(2);
|
|
8
|
+
}
|
|
9
|
+
export class ProportionalFormatter {
|
|
10
|
+
constructor(voices, resolutionMultiplier) {
|
|
11
|
+
var _a;
|
|
12
|
+
var _b;
|
|
13
|
+
this.origin = 0;
|
|
14
|
+
this.tail = PADDING;
|
|
15
|
+
this.minimumScale = 0;
|
|
16
|
+
this.centeredWidth = 0;
|
|
17
|
+
const resolution = voices[0].getActualResolution() / voices[0].getResolutionMultiplier();
|
|
18
|
+
this.quarterTicks = resolution / 4;
|
|
19
|
+
let durationTicks = voices[0].getTotalTicks().value();
|
|
20
|
+
for (const voice of voices) {
|
|
21
|
+
if (voice.getActualResolution() / voice.getResolutionMultiplier() !== resolution) {
|
|
22
|
+
throw new RuntimeError('BadArgument', 'Proportional voices must use the same tick resolution.');
|
|
23
|
+
}
|
|
24
|
+
durationTicks = Math.max(durationTicks, voice.getTicksUsed().value());
|
|
25
|
+
for (const note of voice.getTickables()) {
|
|
26
|
+
if (note.shouldIgnoreTicks())
|
|
27
|
+
continue;
|
|
28
|
+
const ticks = note.getTicks();
|
|
29
|
+
if (ticks.value() <= 0)
|
|
30
|
+
throw new RuntimeError('BadArgument', 'Rhythmic durations must be positive.');
|
|
31
|
+
resolutionMultiplier = Fraction.LCM(resolutionMultiplier, ticks.denominator);
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
if (!Number.isSafeInteger(resolutionMultiplier) || resolutionMultiplier <= 0) {
|
|
35
|
+
throw new RuntimeError('BadArgument', 'Proportional timeline exceeds the supported rational tick range.');
|
|
36
|
+
}
|
|
37
|
+
this.duration = durationTicks / this.quarterTicks;
|
|
38
|
+
const byTick = new Map();
|
|
39
|
+
for (const [voiceIndex, voice] of voices.entries()) {
|
|
40
|
+
const elapsed = new Fraction(0, resolutionMultiplier);
|
|
41
|
+
for (const note of voice.getTickables()) {
|
|
42
|
+
const tick = elapsed.numerator;
|
|
43
|
+
if (!Number.isSafeInteger(tick)) {
|
|
44
|
+
throw new RuntimeError('BadArgument', 'Proportional timeline exceeds the supported rational tick range.');
|
|
45
|
+
}
|
|
46
|
+
let anchor = byTick.get(tick);
|
|
47
|
+
if (!anchor) {
|
|
48
|
+
anchor = { tick, context: new TickContext({ tickID: tick }), prefixes: [], extents: new Map() };
|
|
49
|
+
byTick.set(tick, anchor);
|
|
50
|
+
}
|
|
51
|
+
if (note.shouldIgnoreTicks()) {
|
|
52
|
+
const prefix = ((_a = (_b = anchor.prefixes)[voiceIndex]) !== null && _a !== void 0 ? _a : (_b[voiceIndex] = { contexts: [] }));
|
|
53
|
+
prefix.contexts.push(new TickContext({ tickID: tick }).addTickable(note, voiceIndex));
|
|
54
|
+
}
|
|
55
|
+
else {
|
|
56
|
+
anchor.context.addTickable(note, voiceIndex);
|
|
57
|
+
elapsed.add(note.getTicks());
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
this.anchors = [...byTick.values()].sort((a, b) => a.tick - b.tick);
|
|
62
|
+
const map = {};
|
|
63
|
+
const array = [];
|
|
64
|
+
for (const anchor of this.anchors) {
|
|
65
|
+
map[anchor.tick] = anchor.context;
|
|
66
|
+
anchor.prefixes.forEach((prefix) => array.push(...prefix.contexts));
|
|
67
|
+
array.push(anchor.context);
|
|
68
|
+
}
|
|
69
|
+
TickContext.linkTimeline(this.anchors.map((anchor) => anchor.context));
|
|
70
|
+
array.forEach((context) => (context.tContexts = array));
|
|
71
|
+
this.contexts = { map, array, list: this.anchors.map((anchor) => anchor.tick), resolutionMultiplier };
|
|
72
|
+
}
|
|
73
|
+
quarters(anchor) {
|
|
74
|
+
return anchor.tick / this.contexts.resolutionMultiplier / this.quarterTicks;
|
|
75
|
+
}
|
|
76
|
+
measure(proportional = true) {
|
|
77
|
+
var _a, _b, _c, _d;
|
|
78
|
+
this.origin = PADDING;
|
|
79
|
+
this.tail = PADDING;
|
|
80
|
+
this.minimumScale = 0;
|
|
81
|
+
this.centeredWidth = 0;
|
|
82
|
+
for (const anchor of this.anchors) {
|
|
83
|
+
anchor.extents.clear();
|
|
84
|
+
anchor.context.preFormat();
|
|
85
|
+
for (const note of anchor.context.getTickables()) {
|
|
86
|
+
const metrics = note.getMetrics();
|
|
87
|
+
const left = metrics.modLeftPx + metrics.leftDisplacedHeadPx;
|
|
88
|
+
const right = metrics.notePx + metrics.modRightPx + metrics.rightDisplacedHeadPx;
|
|
89
|
+
if (note.isCenterAligned()) {
|
|
90
|
+
this.centeredWidth = Math.max(this.centeredWidth, 2 * Math.max(left, right));
|
|
91
|
+
continue;
|
|
92
|
+
}
|
|
93
|
+
const extent = (_a = anchor.extents.get(note.getStave())) !== null && _a !== void 0 ? _a : { left: 0, right: 0 };
|
|
94
|
+
extent.left = Math.max(extent.left, left);
|
|
95
|
+
extent.right = Math.max(extent.right, right);
|
|
96
|
+
anchor.extents.set(note.getStave(), extent);
|
|
97
|
+
}
|
|
98
|
+
const prefixWidths = new Map();
|
|
99
|
+
anchor.prefixes.forEach((prefix) => {
|
|
100
|
+
var _a;
|
|
101
|
+
let width = 0;
|
|
102
|
+
for (const context of prefix.contexts)
|
|
103
|
+
width += context.preFormat().getWidth();
|
|
104
|
+
const stave = prefix.contexts[0].getTickables()[0].getStave();
|
|
105
|
+
prefixWidths.set(stave, Math.max((_a = prefixWidths.get(stave)) !== null && _a !== void 0 ? _a : 0, width));
|
|
106
|
+
});
|
|
107
|
+
for (const [stave, width] of prefixWidths) {
|
|
108
|
+
const extent = (_b = anchor.extents.get(stave)) !== null && _b !== void 0 ? _b : { left: 0, right: 0 };
|
|
109
|
+
extent.left += width;
|
|
110
|
+
anchor.extents.set(stave, extent);
|
|
111
|
+
}
|
|
112
|
+
for (const extent of anchor.extents.values()) {
|
|
113
|
+
if (this.quarters(anchor) === 0)
|
|
114
|
+
this.origin = Math.max(this.origin, extent.left + PADDING);
|
|
115
|
+
if (this.quarters(anchor) === this.duration)
|
|
116
|
+
this.tail = Math.max(this.tail, extent.right + PADDING);
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
if (!proportional)
|
|
120
|
+
return;
|
|
121
|
+
const previous = new Map();
|
|
122
|
+
for (const anchor of this.anchors) {
|
|
123
|
+
const time = this.quarters(anchor);
|
|
124
|
+
for (const [stave, extent] of anchor.extents) {
|
|
125
|
+
for (const before of (_c = previous.get(stave)) !== null && _c !== void 0 ? _c : []) {
|
|
126
|
+
this.minimumScale = Math.max(this.minimumScale, (before.extent.right + extent.left + PADDING) / (time - before.time));
|
|
127
|
+
}
|
|
128
|
+
if (time > 0)
|
|
129
|
+
this.minimumScale = Math.max(this.minimumScale, (extent.left + PADDING - this.origin) / time);
|
|
130
|
+
if (time < this.duration) {
|
|
131
|
+
this.minimumScale = Math.max(this.minimumScale, (extent.right + PADDING - this.tail) / (this.duration - time));
|
|
132
|
+
}
|
|
133
|
+
const list = (_d = previous.get(stave)) !== null && _d !== void 0 ? _d : [];
|
|
134
|
+
list.push({ time, extent });
|
|
135
|
+
previous.set(stave, list);
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
this.minimumScale = Math.max(this.minimumScale, this.centeredWidth / this.duration);
|
|
139
|
+
}
|
|
140
|
+
dimensions(spacing, width) {
|
|
141
|
+
if (!Number.isFinite(width) || width < 0)
|
|
142
|
+
throw new RuntimeError('BadArgument', 'Width must be finite and nonnegative.');
|
|
143
|
+
const requested = spacing.pixelsPerQuarter;
|
|
144
|
+
if (requested !== undefined && (!Number.isFinite(requested) || requested <= 0)) {
|
|
145
|
+
throw new RuntimeError('BadArgument', 'pixelsPerQuarter must be positive and finite.');
|
|
146
|
+
}
|
|
147
|
+
this.measure();
|
|
148
|
+
const minimumWidth = this.origin + this.duration * this.minimumScale + this.tail;
|
|
149
|
+
const scale = requested !== null && requested !== void 0 ? requested : (width > 0 ? (width - this.origin - this.tail) / this.duration : this.minimumScale);
|
|
150
|
+
const neededWidth = this.origin + this.duration * scale + this.tail;
|
|
151
|
+
if (!Number.isFinite(neededWidth) || !Number.isFinite(this.minimumScale)) {
|
|
152
|
+
throw new RuntimeError('BadArgument', 'Proportional dimensions exceed the supported pixel range.');
|
|
153
|
+
}
|
|
154
|
+
if (scale + EPSILON < this.minimumScale || (width > 0 && width + EPSILON < neededWidth)) {
|
|
155
|
+
throw new RuntimeError('InsufficientProportionalSpace', `Proportional spacing requires at least ${required(this.minimumScale)} px/quarter and ` +
|
|
156
|
+
`${required(Math.max(minimumWidth, neededWidth))} px width; received ${scale.toFixed(2)} px/quarter` +
|
|
157
|
+
(width > 0 ? ` and ${width.toFixed(2)} px width.` : '.'));
|
|
158
|
+
}
|
|
159
|
+
return { scale, width: neededWidth };
|
|
160
|
+
}
|
|
161
|
+
opticalLayout(width) {
|
|
162
|
+
var _a;
|
|
163
|
+
if (!Number.isFinite(width) || width < 0)
|
|
164
|
+
throw new RuntimeError('BadArgument', 'Width must be finite and nonnegative.');
|
|
165
|
+
this.measure(false);
|
|
166
|
+
const rightEdges = new Map();
|
|
167
|
+
const positions = [];
|
|
168
|
+
const weights = [];
|
|
169
|
+
let priorX = 0;
|
|
170
|
+
let time = 0;
|
|
171
|
+
let weight = 0;
|
|
172
|
+
for (const anchor of this.anchors) {
|
|
173
|
+
const onset = this.quarters(anchor);
|
|
174
|
+
let x = priorX + PADDING;
|
|
175
|
+
for (const [stave, extent] of anchor.extents)
|
|
176
|
+
x = Math.max(x, ((_a = rightEdges.get(stave)) !== null && _a !== void 0 ? _a : 0) + PADDING + extent.left);
|
|
177
|
+
for (const [stave, extent] of anchor.extents)
|
|
178
|
+
rightEdges.set(stave, x + extent.right);
|
|
179
|
+
weight += Math.sqrt(onset - time);
|
|
180
|
+
weights.push(weight);
|
|
181
|
+
positions.push(x);
|
|
182
|
+
priorX = x;
|
|
183
|
+
time = onset;
|
|
184
|
+
}
|
|
185
|
+
weight += Math.sqrt(this.duration - time);
|
|
186
|
+
const minimum = Math.max(this.centeredWidth + 2 * PADDING, priorX + PADDING, ...Array.from(rightEdges.values(), (x) => x + PADDING));
|
|
187
|
+
if (width > 0 && width + EPSILON < minimum)
|
|
188
|
+
throw new RuntimeError('InsufficientOpticalSpace', `Optical spacing requires at least ${required(minimum)} px width; received ${width.toFixed(2)} px.`);
|
|
189
|
+
const available = width || minimum;
|
|
190
|
+
return {
|
|
191
|
+
width: available,
|
|
192
|
+
center: available / 2,
|
|
193
|
+
positions: positions.map((x, i) => x + (weight ? weights[i] / weight : 0) * (available - minimum)),
|
|
194
|
+
};
|
|
195
|
+
}
|
|
196
|
+
minimumWidth(spacing) {
|
|
197
|
+
if (spacing.mode === 'optical')
|
|
198
|
+
return this.opticalLayout(0).width;
|
|
199
|
+
return this.dimensions(spacing, 0).width;
|
|
200
|
+
}
|
|
201
|
+
proportionalLayout(spacing, width) {
|
|
202
|
+
const dimensions = this.dimensions(spacing, width);
|
|
203
|
+
return {
|
|
204
|
+
width: dimensions.width,
|
|
205
|
+
center: this.origin + (this.duration * dimensions.scale) / 2,
|
|
206
|
+
positions: this.anchors.map((anchor) => this.origin + this.quarters(anchor) * dimensions.scale),
|
|
207
|
+
};
|
|
208
|
+
}
|
|
209
|
+
format(spacing, width) {
|
|
210
|
+
const { positions, center, width: usedWidth, } = spacing.mode === 'optical' ? this.opticalLayout(width) : this.proportionalLayout(spacing, width);
|
|
211
|
+
for (const [i, anchor] of this.anchors.entries()) {
|
|
212
|
+
const x = positions[i];
|
|
213
|
+
anchor.context.setX(x);
|
|
214
|
+
anchor.context.getCenterAlignedTickables().forEach((note) => {
|
|
215
|
+
note.setCenterXShift(center - x);
|
|
216
|
+
});
|
|
217
|
+
anchor.prefixes.forEach((prefix) => {
|
|
218
|
+
const stave = prefix.contexts[0].getTickables()[0].getStave();
|
|
219
|
+
const notes = anchor.context.getTickables().filter((note) => note.getStave() === stave);
|
|
220
|
+
let right = x -
|
|
221
|
+
Math.max(0, ...notes.map((note) => {
|
|
222
|
+
const metrics = note.getMetrics();
|
|
223
|
+
return metrics.modLeftPx + metrics.leftDisplacedHeadPx;
|
|
224
|
+
}));
|
|
225
|
+
for (const context of [...prefix.contexts].reverse()) {
|
|
226
|
+
const metrics = context.getMetrics();
|
|
227
|
+
context.setX(right - PADDING / 2 - metrics.notePx - metrics.totalRightPx);
|
|
228
|
+
right -= context.getWidth();
|
|
229
|
+
}
|
|
230
|
+
});
|
|
231
|
+
}
|
|
232
|
+
return usedWidth;
|
|
233
|
+
}
|
|
234
|
+
}
|
|
@@ -19,7 +19,33 @@ export class Registry {
|
|
|
19
19
|
static disableDefaultRegistry() {
|
|
20
20
|
Registry.defaultRegistry = undefined;
|
|
21
21
|
}
|
|
22
|
+
transaction(build) {
|
|
23
|
+
const before = new Index();
|
|
24
|
+
for (const name of Object.keys(this.index)) {
|
|
25
|
+
for (const [value, elements] of Object.entries(this.index[name])) {
|
|
26
|
+
before[name][value] = Object.assign(Object.create(null), elements);
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
const existing = new Set(Object.values(before.id).flatMap((elements) => Object.values(elements)));
|
|
30
|
+
const created = new Set();
|
|
31
|
+
this.transactions.push(created);
|
|
32
|
+
try {
|
|
33
|
+
return build();
|
|
34
|
+
}
|
|
35
|
+
catch (error) {
|
|
36
|
+
created.forEach((element) => {
|
|
37
|
+
if (!existing.has(element))
|
|
38
|
+
element.onRegister(undefined);
|
|
39
|
+
});
|
|
40
|
+
this.index = before;
|
|
41
|
+
throw error;
|
|
42
|
+
}
|
|
43
|
+
finally {
|
|
44
|
+
this.transactions.pop();
|
|
45
|
+
}
|
|
46
|
+
}
|
|
22
47
|
constructor() {
|
|
48
|
+
this.transactions = [];
|
|
23
49
|
this.index = new Index();
|
|
24
50
|
}
|
|
25
51
|
clear() {
|
|
@@ -53,6 +79,7 @@ export class Registry {
|
|
|
53
79
|
if (!id) {
|
|
54
80
|
throw new RuntimeError("Can't add element without `id` attribute to registry");
|
|
55
81
|
}
|
|
82
|
+
this.transactions.forEach((transaction) => transaction.add(elem));
|
|
56
83
|
elem.setAttribute('id', id);
|
|
57
84
|
this.setIndexValue('id', id, id, elem);
|
|
58
85
|
this.updateIndex({ id, name: 'type', value: elem.getAttribute('type'), oldValue: undefined });
|
package/build/esm/src/sheet.js
CHANGED
|
@@ -1,3 +1,7 @@
|
|
|
1
|
+
import { ClefNote } from './clefnote.js';
|
|
2
|
+
import { Note } from './note.js';
|
|
3
|
+
import { StaveNote } from './stavenote.js';
|
|
4
|
+
import { StemmableNote } from './stemmablenote.js';
|
|
1
5
|
import { RuntimeError } from './util.js';
|
|
2
6
|
export class Sheet {
|
|
3
7
|
constructor(options) {
|
|
@@ -74,9 +78,22 @@ export class Sheet {
|
|
|
74
78
|
let bottom = system.getBoundingBox().getH();
|
|
75
79
|
for (const voice of system.getVoices()) {
|
|
76
80
|
for (const note of voice.getTickables()) {
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
81
|
+
if (note instanceof StaveNote || note instanceof ClefNote) {
|
|
82
|
+
const box = note instanceof StaveNote ? note.getBoundingBox(false) : note.getBoundingBox();
|
|
83
|
+
top = Math.min(top, box.getY());
|
|
84
|
+
bottom = Math.max(bottom, box.getY() + box.getH());
|
|
85
|
+
}
|
|
86
|
+
else if (note instanceof Note) {
|
|
87
|
+
const padding = note.checkStave().getSpacingBetweenLines();
|
|
88
|
+
const ys = note.getYs();
|
|
89
|
+
top = Math.min(top, ...ys.map((y) => y - padding));
|
|
90
|
+
bottom = Math.max(bottom, ...ys.map((y) => y + padding));
|
|
91
|
+
if (note instanceof StemmableNote && note.hasStem()) {
|
|
92
|
+
const { topY, baseY } = note.getStemExtents();
|
|
93
|
+
top = Math.min(top, topY, baseY);
|
|
94
|
+
bottom = Math.max(bottom, topY, baseY);
|
|
95
|
+
}
|
|
96
|
+
}
|
|
80
97
|
}
|
|
81
98
|
}
|
|
82
99
|
return { top, bottom };
|
package/build/esm/src/stave.js
CHANGED
|
@@ -37,6 +37,7 @@ export class Stave extends Element {
|
|
|
37
37
|
return Metrics.get('Stave.endPaddingMax');
|
|
38
38
|
}
|
|
39
39
|
constructor(x, y, width, options) {
|
|
40
|
+
var _a;
|
|
40
41
|
super();
|
|
41
42
|
this.x = x;
|
|
42
43
|
this.y = y;
|
|
@@ -46,9 +47,9 @@ export class Stave extends Element {
|
|
|
46
47
|
this.endX = x + width;
|
|
47
48
|
this.modifiers = [];
|
|
48
49
|
this.measure = 0;
|
|
49
|
-
this.clef = 'treble';
|
|
50
|
+
this.clef = (_a = options === null || options === void 0 ? void 0 : options.clef) !== null && _a !== void 0 ? _a : 'treble';
|
|
50
51
|
this.endClef = undefined;
|
|
51
|
-
this.options = Object.assign({ verticalBarWidth: 10, numLines: 5, leftBar: true, rightBar: true, spacingBetweenLinesPx: Tables.STAVE_LINE_DISTANCE, spaceAboveStaffLn: 4, spaceBelowStaffLn: 4, topTextPosition: 1, bottomTextPosition: 4, lineConfig: [] }, options);
|
|
52
|
+
this.options = Object.assign({ clef: 'treble', verticalBarWidth: 10, numLines: 5, leftBar: true, rightBar: true, spacingBetweenLinesPx: Tables.STAVE_LINE_DISTANCE, spaceAboveStaffLn: 4, spaceBelowStaffLn: 4, topTextPosition: 1, bottomTextPosition: 4, lineConfig: [] }, options);
|
|
52
53
|
this.bounds = { x: this.x, y: this.y, w: this.width, h: 0 };
|
|
53
54
|
this.defaultLedgerLineStyle = { lineWidth: 2 };
|
|
54
55
|
this.resetLines();
|
|
@@ -227,6 +228,12 @@ export class Stave extends Element {
|
|
|
227
228
|
if (position !== undefined) {
|
|
228
229
|
modifier.setPosition(position);
|
|
229
230
|
}
|
|
231
|
+
if (modifier instanceof Clef) {
|
|
232
|
+
if (modifier.getPosition() === StaveModifierPosition.BEGIN)
|
|
233
|
+
this.clef = modifier.getType();
|
|
234
|
+
else if (modifier.getPosition() === StaveModifierPosition.END)
|
|
235
|
+
this.endClef = modifier.getType();
|
|
236
|
+
}
|
|
230
237
|
modifier.setStave(this);
|
|
231
238
|
this.formatted = false;
|
|
232
239
|
this.modifiers.push(modifier);
|