vexflow 5.3.0 → 5.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.
- package/README.md +2 -0
- 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 +4189 -772
- package/build/cjs/vexflow-debug-with-tests.js.map +1 -1
- package/build/cjs/vexflow-debug.js +4323 -906
- 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/alternatingtremolo.js +68 -0
- package/build/esm/src/annotation.js +19 -3
- package/build/esm/src/articulation.js +5 -1
- package/build/esm/src/beam.js +13 -0
- package/build/esm/src/bend.js +57 -6
- package/build/esm/src/clef.js +3 -0
- package/build/esm/src/continuouslayout.js +681 -0
- package/build/esm/src/continuousspan.js +159 -0
- package/build/esm/src/curve.js +21 -2
- package/build/esm/src/easyscore/composition.js +13 -2
- package/build/esm/src/easyscore/policies.js +6 -3
- package/build/esm/src/engravinggeometry.js +8 -1
- package/build/esm/src/factory.js +16 -4
- package/build/esm/src/font.js +9 -1
- package/build/esm/src/fretdiagram.js +77 -0
- package/build/esm/src/guitargesture.js +33 -0
- package/build/esm/src/guitarmodifiers.js +26 -0
- package/build/esm/src/index.js +8 -0
- package/build/esm/src/instrumentprofile.js +123 -0
- package/build/esm/src/keysignature.js +23 -1
- package/build/esm/src/notationspelling.js +73 -0
- package/build/esm/src/note.js +3 -0
- package/build/esm/src/ornament.js +5 -0
- package/build/esm/src/pagebreaks.js +58 -0
- package/build/esm/src/pedalmarking.js +39 -17
- package/build/esm/src/phraselayout.js +503 -0
- package/build/esm/src/phraseribbon.js +28 -0
- package/build/esm/src/publication.js +402 -0
- package/build/esm/src/publicationview.js +114 -0
- package/build/esm/src/scorenavigation.js +172 -0
- package/build/esm/src/sheet.js +14 -2
- package/build/esm/src/stave.js +33 -16
- package/build/esm/src/stavebarline.js +5 -7
- package/build/esm/src/staveconnector.js +1 -0
- package/build/esm/src/stavehairpin.js +53 -27
- package/build/esm/src/stavenote.js +6 -1
- package/build/esm/src/stavetie.js +27 -2
- package/build/esm/src/strokes.js +4 -0
- package/build/esm/src/system.js +19 -4
- package/build/esm/src/systembreaks.js +42 -0
- package/build/esm/src/tabnote.js +6 -1
- package/build/esm/src/textbracket.js +24 -5
- package/build/esm/src/timesignature.js +15 -6
- package/build/esm/src/tuplet.js +1 -0
- package/build/esm/src/version.js +3 -3
- package/build/esm/src/vexflow.js +16 -0
- package/build/esm/src/voice.js +7 -3
- package/build/esm/tests/engraving/continuous-studies.mjs +140 -0
- package/build/esm/tests/engraving/instrument-studies.mjs +273 -0
- package/build/esm/tests/engraving/phrase-studies.mjs +63 -0
- package/build/esm/tests/engraving/publication-studies.mjs +87 -0
- package/build/esm/tests/engraving/specialist-studies.mjs +347 -0
- package/build/esm/tests/engraving/studies.mjs +88 -0
- package/build/esm/tests/node/continuous-score-tests.mjs +282 -0
- package/build/esm/tests/node/instrument-profile-tests.mjs +224 -0
- package/build/esm/tests/node/phrase-layout-tests.mjs +281 -0
- package/build/esm/tests/node/publication-tests.mjs +219 -0
- package/build/esm/tools/easyscore-structure.mjs +1 -1
- package/build/esm/tools/explorer/catalog/entries.mjs +2 -1
- package/build/esm/tools/explorer/catalog/expression.mjs +14 -9
- package/build/esm/tools/explorer/catalog/guitar.mjs +238 -0
- package/build/esm/tools/explorer/catalog/layout.mjs +8 -6
- package/build/esm/tools/explorer/catalog/notes.mjs +16 -14
- package/build/esm/tools/explorer/catalog/staves.mjs +12 -6
- package/build/esm/tools/explorer/catalog/strings.mjs +10 -7
- package/build/types/src/alternatingtremolo.d.ts +13 -0
- package/build/types/src/annotation.d.ts +9 -0
- package/build/types/src/articulation.d.ts +3 -0
- package/build/types/src/beam.d.ts +2 -0
- package/build/types/src/bend.d.ts +8 -0
- package/build/types/src/clef.d.ts +3 -0
- package/build/types/src/continuouslayout.d.ts +103 -0
- package/build/types/src/continuousspan.d.ts +60 -0
- package/build/types/src/curve.d.ts +11 -0
- package/build/types/src/easyscore/composition.d.ts +8 -0
- package/build/types/src/easyscore/policies.d.ts +1 -1
- package/build/types/src/fretdiagram.d.ts +23 -0
- package/build/types/src/guitargesture.d.ts +26 -0
- package/build/types/src/guitarmodifiers.d.ts +3 -0
- package/build/types/src/index.d.ts +10 -0
- package/build/types/src/instrumentprofile.d.ts +124 -0
- package/build/types/src/keysignature.d.ts +9 -0
- package/build/types/src/notationspelling.d.ts +25 -0
- package/build/types/src/note.d.ts +15 -0
- package/build/types/src/ornament.d.ts +3 -1
- package/build/types/src/pagebreaks.d.ts +26 -0
- package/build/types/src/pedalmarking.d.ts +9 -0
- package/build/types/src/phraselayout.d.ts +87 -0
- package/build/types/src/phraseribbon.d.ts +7 -0
- package/build/types/src/publication.d.ts +61 -0
- package/build/types/src/publicationview.d.ts +27 -0
- package/build/types/src/scorenavigation.d.ts +33 -0
- package/build/types/src/sheet.d.ts +5 -0
- package/build/types/src/stave.d.ts +13 -0
- package/build/types/src/stavehairpin.d.ts +18 -0
- package/build/types/src/stavetie.d.ts +5 -0
- package/build/types/src/system.d.ts +8 -1
- package/build/types/src/systembreaks.d.ts +9 -0
- package/build/types/src/textbracket.d.ts +7 -0
- package/build/types/src/timesignature.d.ts +4 -0
- package/build/types/src/vexflow.d.ts +16 -0
- package/build/types/src/voice.d.ts +2 -0
- package/changelog/CHANGELOG.md +8 -0
- package/package.json +9 -2
|
@@ -0,0 +1,503 @@
|
|
|
1
|
+
import { Annotation } from './annotation.js';
|
|
2
|
+
import { Articulation } from './articulation.js';
|
|
3
|
+
import { BoundingBox } from './boundingbox.js';
|
|
4
|
+
import { Clef } from './clef.js';
|
|
5
|
+
import { measureEngraving } from './engravinggeometry.js';
|
|
6
|
+
import { GhostNote } from './ghostnote.js';
|
|
7
|
+
import { KeySignature } from './keysignature.js';
|
|
8
|
+
import { Modifier } from './modifier.js';
|
|
9
|
+
import { Ornament } from './ornament.js';
|
|
10
|
+
import { Barline } from './stavebarline.js';
|
|
11
|
+
import { StaveHairpin } from './stavehairpin.js';
|
|
12
|
+
import { StaveNote } from './stavenote.js';
|
|
13
|
+
import { StaveTie } from './stavetie.js';
|
|
14
|
+
import { TextBracket } from './textbracket.js';
|
|
15
|
+
import { TimeSignature } from './timesignature.js';
|
|
16
|
+
import { RuntimeError } from './util.js';
|
|
17
|
+
const supportedText = new RegExp('^[\\p{Script=Latin}\\p{Script=Greek}\\p{Script=Cyrillic}\\p{Script=Han}\\p{Script=Hiragana}\\p{Script=Katakana}\\p{Script=Hangul}\\p{M}\\p{N}\\p{P}\\p{Zs}]*$', 'u');
|
|
18
|
+
function overlaps(a, b, gap) {
|
|
19
|
+
return a.x < b.x + b.w + gap && b.x < a.x + a.w + gap && a.y < b.y + b.h + gap && b.y < a.y + a.h + gap;
|
|
20
|
+
}
|
|
21
|
+
function obstacle(box, owner, stave, bridge = false) {
|
|
22
|
+
return { x: box.x, y: box.y, w: box.w, h: box.h, owner, stave, bridge };
|
|
23
|
+
}
|
|
24
|
+
export class PhraseLayout {
|
|
25
|
+
constructor() {
|
|
26
|
+
this.diagnostics = [];
|
|
27
|
+
this.lanes = [];
|
|
28
|
+
this.routes = [];
|
|
29
|
+
this.fixed = [];
|
|
30
|
+
this.pins = new Set();
|
|
31
|
+
this.managed = new Set();
|
|
32
|
+
this.restLines = new Map();
|
|
33
|
+
this.obstacles = [];
|
|
34
|
+
this.connectors = [];
|
|
35
|
+
this.messages = new Set();
|
|
36
|
+
this.iterationLimit = 4;
|
|
37
|
+
this.lockedStaves = new Set();
|
|
38
|
+
this.passes = 0;
|
|
39
|
+
}
|
|
40
|
+
addObstacle(stave, owner) {
|
|
41
|
+
this.fixed.push({ stave, owner });
|
|
42
|
+
return this;
|
|
43
|
+
}
|
|
44
|
+
pin(owner) {
|
|
45
|
+
this.pins.add(owner);
|
|
46
|
+
return this;
|
|
47
|
+
}
|
|
48
|
+
lockStaffGap(stave) {
|
|
49
|
+
this.lockedStaves.add(stave);
|
|
50
|
+
return this;
|
|
51
|
+
}
|
|
52
|
+
setPassLimit(limit) {
|
|
53
|
+
if (!Number.isInteger(limit) || limit < 1 || limit > 16)
|
|
54
|
+
throw new RangeError('Phrase pass limit must be 1 to 16.');
|
|
55
|
+
this.iterationLimit = limit;
|
|
56
|
+
return this;
|
|
57
|
+
}
|
|
58
|
+
route(span) {
|
|
59
|
+
if (!this.routes.includes(span))
|
|
60
|
+
this.routes.push(span);
|
|
61
|
+
return this;
|
|
62
|
+
}
|
|
63
|
+
addLane(stave, marks, options = {}) {
|
|
64
|
+
if (!marks.length || new Set(marks).size !== marks.length || marks.some((m) => this.managed.has(m)))
|
|
65
|
+
throw new RangeError('Each phrase mark must belong to exactly one lane.');
|
|
66
|
+
if (options.baselineSpaces !== undefined && !Number.isFinite(options.baselineSpaces))
|
|
67
|
+
throw new RangeError('Expected a finite lane baseline.');
|
|
68
|
+
this.lanes.push({ stave, marks: marks.slice(), options: Object.assign({}, options) });
|
|
69
|
+
for (const mark of marks) {
|
|
70
|
+
this.managed.add(mark);
|
|
71
|
+
if (mark instanceof Annotation)
|
|
72
|
+
mark.phraseManaged = true;
|
|
73
|
+
}
|
|
74
|
+
return this;
|
|
75
|
+
}
|
|
76
|
+
addVerse(stave, syllables, options = {}) {
|
|
77
|
+
if (!syllables.length ||
|
|
78
|
+
syllables.some((s, i) => !s.text || (s.hyphenAfter && (s.extendTo || i === syllables.length - 1))))
|
|
79
|
+
throw new RangeError('Expected syllables with a following syllable for each hyphen.');
|
|
80
|
+
if (syllables.some((s) => s.note.getStave() && s.note.getStave() !== stave))
|
|
81
|
+
throw new RangeError('A syllable must belong to the requested stave.');
|
|
82
|
+
const marks = syllables.map((s, i) => {
|
|
83
|
+
var _a;
|
|
84
|
+
return new Annotation(s.text)
|
|
85
|
+
.setVerticalJustification(options.side === 'above' ? 'top' : 'bottom')
|
|
86
|
+
.setConnectorPadding(s.hyphenAfter || ((_a = syllables[i - 1]) === null || _a === void 0 ? void 0 : _a.hyphenAfter) ? stave.space(0.4) : 0);
|
|
87
|
+
});
|
|
88
|
+
this.addLane(stave, marks, options);
|
|
89
|
+
this.lanes[this.lanes.length - 1].syllables = syllables.map((s) => (Object.assign({}, s)));
|
|
90
|
+
marks.forEach((mark, i) => syllables[i].note.addModifier(mark, 0));
|
|
91
|
+
return marks;
|
|
92
|
+
}
|
|
93
|
+
resetPlacement() {
|
|
94
|
+
for (const lane of this.lanes)
|
|
95
|
+
for (const mark of lane.marks)
|
|
96
|
+
mark.setLayoutBaseline(undefined);
|
|
97
|
+
for (const route of this.routes)
|
|
98
|
+
route.setLayoutRise(0);
|
|
99
|
+
for (const [note, line] of this.restLines)
|
|
100
|
+
if (note.getKeyLine(0) === line.after)
|
|
101
|
+
note.setKeyLine(0, line.before);
|
|
102
|
+
this.restLines.clear();
|
|
103
|
+
}
|
|
104
|
+
report(code, ...owners) {
|
|
105
|
+
const sources = [...new Set(owners.map((o) => o.getAttribute('id')))];
|
|
106
|
+
const key = JSON.stringify([code, ...sources]);
|
|
107
|
+
if (!this.messages.has(key)) {
|
|
108
|
+
this.messages.add(key);
|
|
109
|
+
this.diagnostics.push({ code, sources });
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
addParts(owner, stave, parts, bridge = false) {
|
|
113
|
+
for (const box of parts)
|
|
114
|
+
this.obstacles.push(obstacle(box, owner, stave, bridge));
|
|
115
|
+
}
|
|
116
|
+
measure(owner, stave) {
|
|
117
|
+
const g = measureEngraving(owner);
|
|
118
|
+
if (g.diagnostics.length)
|
|
119
|
+
this.report('UnsupportedGeometry', owner);
|
|
120
|
+
this.addParts(owner, stave, g.parts);
|
|
121
|
+
}
|
|
122
|
+
collect(notes, staves, extras) {
|
|
123
|
+
const beams = new Set(), tuplets = new Set(), rests = [], marks = [];
|
|
124
|
+
for (const n of notes) {
|
|
125
|
+
const stave = n.checkStave(), beam = n.getBeam();
|
|
126
|
+
if (n.renderOptions.draw === false)
|
|
127
|
+
continue;
|
|
128
|
+
if (beam) {
|
|
129
|
+
beam.postFormat();
|
|
130
|
+
beams.add(beam);
|
|
131
|
+
}
|
|
132
|
+
for (const t of n.getTupletStack())
|
|
133
|
+
tuplets.add(t);
|
|
134
|
+
if (n.isRest() && !beam) {
|
|
135
|
+
rests.push(n);
|
|
136
|
+
continue;
|
|
137
|
+
}
|
|
138
|
+
const geometry = measureEngraving(n, { extension: (e) => (e === n ? undefined : { parts: [], anchors: {} }) });
|
|
139
|
+
if (geometry.diagnostics.length)
|
|
140
|
+
this.report('UnsupportedGeometry', n);
|
|
141
|
+
const bridge = beam === null || beam === void 0 ? void 0 : beam.getNotes().some((x) => x.getStave() !== stave);
|
|
142
|
+
geometry.parts.forEach((b, i) => this.addParts(n, stave, [b], bridge && i === n.noteHeads.length));
|
|
143
|
+
for (const m of n.getModifiers()) {
|
|
144
|
+
if (this.managed.has(m))
|
|
145
|
+
continue;
|
|
146
|
+
if ((m instanceof Articulation || m instanceof Ornament) &&
|
|
147
|
+
[Modifier.Position.ABOVE, Modifier.Position.BELOW].includes(m.getPosition()))
|
|
148
|
+
marks.push(m);
|
|
149
|
+
else
|
|
150
|
+
this.measure(m, stave);
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
for (const beam of beams) {
|
|
154
|
+
const owners = beam.getNotes(), stave = owners[0].checkStave(), bridge = owners.some((n) => n.getStave() !== stave);
|
|
155
|
+
const strips = beam.getStripBounds();
|
|
156
|
+
for (const s of new Set(owners.map((n) => n.checkStave()))) {
|
|
157
|
+
this.addParts(beam, s, strips, bridge);
|
|
158
|
+
if (bridge) {
|
|
159
|
+
for (const peer of notes) {
|
|
160
|
+
if (peer.getStave() !== s || owners.includes(peer) || peer.isRest() || peer.renderOptions.draw === false)
|
|
161
|
+
continue;
|
|
162
|
+
if (peer.getHeadInk().some((head) => strips.some((strip) => overlaps(head, strip, 0))))
|
|
163
|
+
this.report('UnresolvedCollision', beam, peer);
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
}
|
|
167
|
+
}
|
|
168
|
+
for (const stave of staves) {
|
|
169
|
+
for (const modifier of stave.getModifiers()) {
|
|
170
|
+
if (modifier instanceof Clef || modifier instanceof TimeSignature)
|
|
171
|
+
this.addParts(modifier, stave, [modifier.getInkBounds()]);
|
|
172
|
+
else if (modifier instanceof KeySignature)
|
|
173
|
+
this.addParts(modifier, stave, modifier.getInkParts());
|
|
174
|
+
else if (!(modifier instanceof Barline))
|
|
175
|
+
this.report('UnsupportedGeometry', modifier);
|
|
176
|
+
}
|
|
177
|
+
}
|
|
178
|
+
for (const e of extras) {
|
|
179
|
+
if (e instanceof GhostNote && !e.getModifiers().length)
|
|
180
|
+
continue;
|
|
181
|
+
const stave = e.getStave();
|
|
182
|
+
if (stave && !this.fixed.some((f) => f.owner === e))
|
|
183
|
+
this.measure(e, stave);
|
|
184
|
+
}
|
|
185
|
+
for (const { owner, stave } of this.fixed)
|
|
186
|
+
if (!tuplets.has(owner))
|
|
187
|
+
this.measure(owner, stave);
|
|
188
|
+
for (const n of rests)
|
|
189
|
+
this.placeRest(n);
|
|
190
|
+
for (const m of marks)
|
|
191
|
+
this.placeAttachment(m);
|
|
192
|
+
for (const t of tuplets)
|
|
193
|
+
this.measure(t, t.getNotes()[0].checkStave());
|
|
194
|
+
for (const stave of staves) {
|
|
195
|
+
const top = stave.getYForLine(0), bottom = stave.getYForLine(stave.getNumLines() - 1);
|
|
196
|
+
this.addParts(stave, stave, [new BoundingBox(stave.getX(), top, stave.getWidth(), bottom - top)]);
|
|
197
|
+
}
|
|
198
|
+
}
|
|
199
|
+
hits(parts, stave, gap) {
|
|
200
|
+
return this.obstacles.filter((o) => o.stave === stave && parts.some((b) => overlaps(b, o, gap)));
|
|
201
|
+
}
|
|
202
|
+
placeRest(note) {
|
|
203
|
+
const stave = note.checkStave(), before = note.getKeyLine(0), gap = stave.space(0.15);
|
|
204
|
+
const original = measureEngraving(note, {
|
|
205
|
+
extension: (e) => (this.managed.has(e) ? { parts: [], anchors: {} } : undefined),
|
|
206
|
+
});
|
|
207
|
+
if (original.diagnostics.length)
|
|
208
|
+
this.report('UnsupportedGeometry', note);
|
|
209
|
+
let parts = original.parts;
|
|
210
|
+
const hits = this.hits(parts, stave, gap);
|
|
211
|
+
if (hits.length && !this.pins.has(note)) {
|
|
212
|
+
const sign = note.getStemDirection();
|
|
213
|
+
for (const step of [1, -1, 2, -2, 3, -3, 4, -4]) {
|
|
214
|
+
const delta = step * sign;
|
|
215
|
+
const translated = parts.map((b) => (Object.assign(Object.assign({}, b), { y: b.y - stave.space(delta) })));
|
|
216
|
+
if (this.hits(translated, stave, gap).length)
|
|
217
|
+
continue;
|
|
218
|
+
note.setKeyLine(0, before + delta).preFormat();
|
|
219
|
+
this.restLines.set(note, { before, after: before + delta });
|
|
220
|
+
parts = measureEngraving(note, {
|
|
221
|
+
extension: (e) => (this.managed.has(e) ? { parts: [], anchors: {} } : undefined),
|
|
222
|
+
}).parts;
|
|
223
|
+
break;
|
|
224
|
+
}
|
|
225
|
+
}
|
|
226
|
+
const remaining = this.hits(parts, stave, gap);
|
|
227
|
+
if (remaining.length)
|
|
228
|
+
this.report(this.pins.has(note) ? 'PinnedCollision' : 'UnresolvedCollision', note, remaining[0].owner);
|
|
229
|
+
this.addParts(note, stave, parts);
|
|
230
|
+
}
|
|
231
|
+
placeAttachment(mark) {
|
|
232
|
+
const stave = mark.checkAttachedNote().checkStave(), gap = stave.space(0.2), above = mark.getPosition() === Modifier.Position.ABOVE;
|
|
233
|
+
mark.setLayoutShiftY(0);
|
|
234
|
+
let parts = measureEngraving(mark).parts;
|
|
235
|
+
for (let step = 1; step <= 12 && !this.pins.has(mark) && this.hits(parts, stave, gap).length; step++) {
|
|
236
|
+
mark.setLayoutShiftY(stave.space(step) * (above ? -1 : 1));
|
|
237
|
+
parts = measureEngraving(mark).parts;
|
|
238
|
+
}
|
|
239
|
+
const hits = this.hits(parts, stave, gap);
|
|
240
|
+
if (hits.length)
|
|
241
|
+
this.report(this.pins.has(mark) ? 'PinnedCollision' : 'UnresolvedCollision', mark, hits[0].owner);
|
|
242
|
+
this.addParts(mark, stave, parts);
|
|
243
|
+
}
|
|
244
|
+
placeRoutes(notes) {
|
|
245
|
+
const membership = new Set(notes);
|
|
246
|
+
const routes = this.routes
|
|
247
|
+
.flatMap((span) => {
|
|
248
|
+
var _a, _b;
|
|
249
|
+
if (span instanceof StaveTie && span.text)
|
|
250
|
+
this.report('UnsupportedGeometry', span);
|
|
251
|
+
const { firstNote: first, lastNote: last } = span.getNotes();
|
|
252
|
+
if (!first ||
|
|
253
|
+
!last ||
|
|
254
|
+
!membership.has(first) ||
|
|
255
|
+
!membership.has(last) ||
|
|
256
|
+
first.getStave() !== last.getStave() ||
|
|
257
|
+
last.getAbsoluteX() <= first.getAbsoluteX() ||
|
|
258
|
+
(span instanceof StaveTie &&
|
|
259
|
+
(((_a = span.getNotes().firstIndexes) === null || _a === void 0 ? void 0 : _a.some((i) => !Number.isInteger(i) || i < 0 || i >= first.getYs().length)) ||
|
|
260
|
+
((_b = span.getNotes().lastIndexes) === null || _b === void 0 ? void 0 : _b.some((i) => !Number.isInteger(i) || i < 0 || i >= last.getYs().length))))) {
|
|
261
|
+
this.report('InvalidAnchor', span);
|
|
262
|
+
return [];
|
|
263
|
+
}
|
|
264
|
+
return [{ span, first, last, stave: first.checkStave(), x: first.getAbsoluteX(), end: last.getAbsoluteX() }];
|
|
265
|
+
})
|
|
266
|
+
.sort((a, b) => Number(b.span instanceof StaveTie) - Number(a.span instanceof StaveTie) || a.end - a.x - (b.end - b.x));
|
|
267
|
+
for (const [index, r] of routes.entries()) {
|
|
268
|
+
const { span, stave } = r;
|
|
269
|
+
const crossing = routes
|
|
270
|
+
.slice(0, index)
|
|
271
|
+
.find((p) => p.stave === stave &&
|
|
272
|
+
((p.x < r.x && r.x < p.end && p.end < r.end) || (r.x < p.x && p.x < r.end && r.end < p.end)));
|
|
273
|
+
if (crossing)
|
|
274
|
+
this.report('CrossingSpans', span, crossing.span);
|
|
275
|
+
const pinned = this.pins.has(span) || span.hasManualControls();
|
|
276
|
+
const fixed = this.obstacles.filter((o) => o.stave === stave && o.owner !== stave && o.owner !== r.first && o.owner !== r.last);
|
|
277
|
+
const gap = stave.space(0.2);
|
|
278
|
+
let bestRise = 0, bestCost = Infinity;
|
|
279
|
+
const limit = pinned || crossing ? 0 : span instanceof StaveTie ? 6 : 16;
|
|
280
|
+
for (let step = 0; step <= limit; step++) {
|
|
281
|
+
const rise = stave.space(step / 2);
|
|
282
|
+
span.setLayoutRise(rise);
|
|
283
|
+
const parts = span.getRouteParts();
|
|
284
|
+
const count = parts.filter((b) => fixed.some((o) => overlaps(b, o, gap))).length;
|
|
285
|
+
if (count < bestCost) {
|
|
286
|
+
bestCost = count;
|
|
287
|
+
bestRise = rise;
|
|
288
|
+
}
|
|
289
|
+
if (!count)
|
|
290
|
+
break;
|
|
291
|
+
}
|
|
292
|
+
span.setLayoutRise(bestRise);
|
|
293
|
+
const parts = span.getRouteParts();
|
|
294
|
+
const hit = fixed.find((o) => parts.some((b) => overlaps(b, o, gap)));
|
|
295
|
+
if (hit)
|
|
296
|
+
this.report(pinned ? 'PinnedCollision' : 'UnresolvedCollision', span, hit.owner);
|
|
297
|
+
this.addParts(span, stave, parts);
|
|
298
|
+
}
|
|
299
|
+
}
|
|
300
|
+
validLane(lane, members) {
|
|
301
|
+
for (const mark of lane.marks) {
|
|
302
|
+
let anchors;
|
|
303
|
+
try {
|
|
304
|
+
if (mark instanceof Annotation)
|
|
305
|
+
anchors = [mark.checkAttachedNote()];
|
|
306
|
+
else if (mark instanceof TextBracket)
|
|
307
|
+
anchors = [mark.start, mark.stop];
|
|
308
|
+
else
|
|
309
|
+
anchors = mark.getNotes();
|
|
310
|
+
}
|
|
311
|
+
catch (error) {
|
|
312
|
+
if (!(error instanceof RuntimeError))
|
|
313
|
+
throw error;
|
|
314
|
+
return false;
|
|
315
|
+
}
|
|
316
|
+
if (!anchors.length ||
|
|
317
|
+
anchors.some((n, i) => !n ||
|
|
318
|
+
!members.has(n) ||
|
|
319
|
+
n.getStave() !== lane.stave ||
|
|
320
|
+
(i > 0 && n.getAbsoluteX() < anchors[i - 1].getAbsoluteX())))
|
|
321
|
+
return false;
|
|
322
|
+
if (!measureEngraving(mark).ink) {
|
|
323
|
+
this.report('UnsupportedGeometry', mark);
|
|
324
|
+
return false;
|
|
325
|
+
}
|
|
326
|
+
}
|
|
327
|
+
return true;
|
|
328
|
+
}
|
|
329
|
+
lyricLines(lane, ink, gap) {
|
|
330
|
+
const { stave, syllables } = lane;
|
|
331
|
+
const result = [];
|
|
332
|
+
if (!syllables)
|
|
333
|
+
return result;
|
|
334
|
+
syllables.forEach((s, i) => {
|
|
335
|
+
const text = ink[i].owner, origin = text.computeY().y + text.getYShift(), b = ink[i].box;
|
|
336
|
+
if (!supportedText.test(s.text))
|
|
337
|
+
this.report('UnsupportedScript', text);
|
|
338
|
+
if (!s.extendTo && !s.hyphenAfter)
|
|
339
|
+
return;
|
|
340
|
+
const left = b.x + b.w + gap / 2;
|
|
341
|
+
const right = s.extendTo
|
|
342
|
+
? s.extendTo.getNoteHeadBeginX() + s.extendTo.getGlyphWidth()
|
|
343
|
+
: ink[i + 1].box.x - gap / 2;
|
|
344
|
+
if (right - left < gap) {
|
|
345
|
+
this.report('InsufficientLyricSpace', text);
|
|
346
|
+
return;
|
|
347
|
+
}
|
|
348
|
+
if (s.extendTo)
|
|
349
|
+
result.push(new BoundingBox(left, origin + 1, right - left, 1));
|
|
350
|
+
else {
|
|
351
|
+
const width = Math.min(stave.space(0.55), right - left);
|
|
352
|
+
result.push(new BoundingBox((left + right - width) / 2, origin - text.fontSizeInPixels * 0.28, width, 1));
|
|
353
|
+
}
|
|
354
|
+
});
|
|
355
|
+
return result;
|
|
356
|
+
}
|
|
357
|
+
placeLane(lane) {
|
|
358
|
+
var _a, _b;
|
|
359
|
+
const { stave, marks, options } = lane, above = options.side === 'above', gap = stave.space(0.35);
|
|
360
|
+
const authored = marks.filter((m) => m instanceof Annotation && m.getTextBaseline() !== undefined);
|
|
361
|
+
const pinned = options.baselineSpaces !== undefined || authored.length > 0;
|
|
362
|
+
let y = options.baselineSpaces !== undefined
|
|
363
|
+
? stave.getYForLine(0) + stave.space(options.baselineSpaces)
|
|
364
|
+
: ((_b = (_a = authored[0]) === null || _a === void 0 ? void 0 : _a.getTextBaseline()) !== null && _b !== void 0 ? _b : stave.getYForLine(above ? 0 : stave.getNumLines() - 1));
|
|
365
|
+
for (const m of marks)
|
|
366
|
+
m.setLayoutBaseline(y);
|
|
367
|
+
const labels = marks.filter((m) => m instanceof Annotation);
|
|
368
|
+
const boxes = labels.map((m) => measureEngraving(m).ink);
|
|
369
|
+
const middle = boxes.length ? boxes.reduce((sum, b) => sum + b.y + b.h / 2 - y, 0) / boxes.length : 0;
|
|
370
|
+
const trims = new Map();
|
|
371
|
+
for (const m of marks)
|
|
372
|
+
if (m instanceof StaveHairpin) {
|
|
373
|
+
const { left, right } = m.getLayoutEdges();
|
|
374
|
+
let l = 0, r = 0;
|
|
375
|
+
for (const b of boxes) {
|
|
376
|
+
if (b.x - gap <= left && left <= b.x + b.w + gap)
|
|
377
|
+
l = Math.max(l, b.x + b.w + gap - left);
|
|
378
|
+
if (b.x - gap <= right && right <= b.x + b.w + gap)
|
|
379
|
+
r = Math.max(r, right - b.x + gap);
|
|
380
|
+
}
|
|
381
|
+
trims.set(m, [l, r]);
|
|
382
|
+
if (left + l >= right - r)
|
|
383
|
+
this.report('UnresolvedCollision', m, ...labels);
|
|
384
|
+
m.setLayoutBaseline(y + middle, l, r);
|
|
385
|
+
}
|
|
386
|
+
const shapes = () => marks.map((m) => ({ owner: m, box: measureEngraving(m).ink }));
|
|
387
|
+
const initial = shapes();
|
|
388
|
+
const blocks = [...initial.map((s) => s.box), ...this.lyricLines(lane, initial, gap)];
|
|
389
|
+
if (!pinned) {
|
|
390
|
+
let shift = 0;
|
|
391
|
+
for (const b of blocks)
|
|
392
|
+
for (const o of this.obstacles) {
|
|
393
|
+
if (o.stave !== stave || o.x + o.w + gap <= b.x || b.x + b.w + gap <= o.x)
|
|
394
|
+
continue;
|
|
395
|
+
shift = above ? Math.min(shift, o.y - gap - b.y - b.h) : Math.max(shift, o.y + o.h + gap - b.y);
|
|
396
|
+
}
|
|
397
|
+
y += shift;
|
|
398
|
+
}
|
|
399
|
+
for (const m of marks) {
|
|
400
|
+
if (m instanceof StaveHairpin)
|
|
401
|
+
m.setLayoutBaseline(y + middle, ...trims.get(m));
|
|
402
|
+
else
|
|
403
|
+
m.setLayoutBaseline(y);
|
|
404
|
+
if (m instanceof Annotation && m.getTextBaseline() !== undefined && Math.abs(m.getTextBaseline() - y) > 0.01)
|
|
405
|
+
this.report('PinnedCollision', m);
|
|
406
|
+
}
|
|
407
|
+
const final = shapes();
|
|
408
|
+
for (const { owner, box } of final) {
|
|
409
|
+
for (const hit of this.hits([box], stave, 0.01))
|
|
410
|
+
this.report(pinned ? 'PinnedCollision' : 'UnresolvedCollision', owner, hit.owner);
|
|
411
|
+
this.addParts(owner, stave, [box]);
|
|
412
|
+
}
|
|
413
|
+
for (const box of this.lyricLines(lane, final, gap)) {
|
|
414
|
+
const hit = this.hits([box], stave, 0)[0];
|
|
415
|
+
if (hit)
|
|
416
|
+
this.report(pinned ? 'PinnedCollision' : 'UnresolvedCollision', marks[0], hit.owner);
|
|
417
|
+
this.addParts(marks[0], stave, [box]);
|
|
418
|
+
this.connectors.push(box);
|
|
419
|
+
}
|
|
420
|
+
}
|
|
421
|
+
format(staves, voices) {
|
|
422
|
+
var _a;
|
|
423
|
+
const ticks = voices.flatMap((v) => v.getTickables());
|
|
424
|
+
const notes = ticks.filter((n) => n instanceof StaveNote);
|
|
425
|
+
const members = new Set(notes);
|
|
426
|
+
const beams = new Set(notes.map((n) => n.getBeam()).filter((b) => !!b));
|
|
427
|
+
for (let pass = 1; pass <= this.iterationLimit; pass++) {
|
|
428
|
+
this.passes = pass;
|
|
429
|
+
this.obstacles = [];
|
|
430
|
+
this.connectors = [];
|
|
431
|
+
this.diagnostics.length = 0;
|
|
432
|
+
this.messages.clear();
|
|
433
|
+
this.collect(notes, staves, ticks.filter((n) => !(n instanceof StaveNote)));
|
|
434
|
+
const isPinned = (l) => l.options.baselineSpaces !== undefined ||
|
|
435
|
+
l.marks.some((m) => m instanceof Annotation && m.getTextBaseline() !== undefined);
|
|
436
|
+
const automatic = [];
|
|
437
|
+
for (const lane of this.lanes) {
|
|
438
|
+
if (!staves.includes(lane.stave) ||
|
|
439
|
+
!this.validLane(lane, members) ||
|
|
440
|
+
((_a = lane.syllables) === null || _a === void 0 ? void 0 : _a.some((s, i) => !members.has(s.note) ||
|
|
441
|
+
s.note.getStave() !== lane.stave ||
|
|
442
|
+
(i > 0 && s.note.getAbsoluteX() <= lane.syllables[i - 1].note.getAbsoluteX()) ||
|
|
443
|
+
(s.extendTo &&
|
|
444
|
+
(!members.has(s.extendTo) ||
|
|
445
|
+
s.extendTo.getStave() !== lane.stave ||
|
|
446
|
+
s.extendTo.getAbsoluteX() < s.note.getAbsoluteX()))))) {
|
|
447
|
+
this.report('InvalidAnchor', lane.marks[0]);
|
|
448
|
+
continue;
|
|
449
|
+
}
|
|
450
|
+
if (isPinned(lane))
|
|
451
|
+
this.placeLane(lane);
|
|
452
|
+
else
|
|
453
|
+
automatic.push(lane);
|
|
454
|
+
}
|
|
455
|
+
this.placeRoutes(notes);
|
|
456
|
+
for (const lane of automatic)
|
|
457
|
+
this.placeLane(lane);
|
|
458
|
+
const envelopes = staves.map((stave) => {
|
|
459
|
+
const boxes = this.obstacles.filter((o) => o.stave === stave && !o.bridge);
|
|
460
|
+
return { top: Math.min(...boxes.map((b) => b.y)), bottom: Math.max(...boxes.map((b) => b.y + b.h)) };
|
|
461
|
+
});
|
|
462
|
+
let shift = 0;
|
|
463
|
+
const shifts = staves.map((s, i) => {
|
|
464
|
+
if (i === 0)
|
|
465
|
+
return 0;
|
|
466
|
+
const need = envelopes[i - 1].bottom + s.space(0.6) - envelopes[i].top;
|
|
467
|
+
if (need > 0.01) {
|
|
468
|
+
if (this.lockedStaves.has(s))
|
|
469
|
+
this.report('PinnedCollision', staves[i - 1], s);
|
|
470
|
+
else
|
|
471
|
+
shift += need;
|
|
472
|
+
}
|
|
473
|
+
return shift;
|
|
474
|
+
});
|
|
475
|
+
if (!shift)
|
|
476
|
+
break;
|
|
477
|
+
if (pass === this.iterationLimit) {
|
|
478
|
+
this.report('IterationLimit', ...staves);
|
|
479
|
+
break;
|
|
480
|
+
}
|
|
481
|
+
this.resetPlacement();
|
|
482
|
+
beams.forEach((b) => b.resetLayout());
|
|
483
|
+
staves.forEach((s, i) => s.setY(s.getY() + shifts[i]));
|
|
484
|
+
for (const n of ticks) {
|
|
485
|
+
const s = n.getStave();
|
|
486
|
+
if (s)
|
|
487
|
+
n.setStave(s);
|
|
488
|
+
}
|
|
489
|
+
}
|
|
490
|
+
this.bounds = undefined;
|
|
491
|
+
for (const box of this.obstacles) {
|
|
492
|
+
const part = new BoundingBox(box.x, box.y, box.w, box.h);
|
|
493
|
+
this.bounds = this.bounds ? this.bounds.mergeWith(part) : part;
|
|
494
|
+
}
|
|
495
|
+
}
|
|
496
|
+
getLyricConnectors() {
|
|
497
|
+
return this.connectors.map((box) => BoundingBox.copy(box));
|
|
498
|
+
}
|
|
499
|
+
draw(context) {
|
|
500
|
+
for (const b of this.connectors)
|
|
501
|
+
context.fillRect(b.x, b.y, b.w, b.h);
|
|
502
|
+
}
|
|
503
|
+
}
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import { BoundingBox } from './boundingbox.js';
|
|
2
|
+
function halves(points) {
|
|
3
|
+
const left = [points[0]], right = [points[points.length - 1]];
|
|
4
|
+
let row = points;
|
|
5
|
+
while (row.length > 1) {
|
|
6
|
+
row = row.slice(1).map((p, i) => ({ x: (p.x + row[i].x) / 2, y: (p.y + row[i].y) / 2 }));
|
|
7
|
+
left.push(row[0]);
|
|
8
|
+
right.unshift(row[row.length - 1]);
|
|
9
|
+
}
|
|
10
|
+
return [left, right];
|
|
11
|
+
}
|
|
12
|
+
export function ribbonParts(upper, lower, padding = 0) {
|
|
13
|
+
const parts = [];
|
|
14
|
+
function divide(a, b, depth) {
|
|
15
|
+
const hull = [...a, ...b];
|
|
16
|
+
const xs = hull.map((p) => p.x), ys = hull.map((p) => p.y);
|
|
17
|
+
const x = Math.min(...xs), y = Math.min(...ys), w = Math.max(...xs) - x;
|
|
18
|
+
if (depth === 6 || w <= 6) {
|
|
19
|
+
parts.push(new BoundingBox(x - padding, y - padding, w + 2 * padding, Math.max(...ys) - y + 2 * padding));
|
|
20
|
+
return;
|
|
21
|
+
}
|
|
22
|
+
const [a1, a2] = halves(a), [b1, b2] = halves(b);
|
|
23
|
+
divide(a1, b1, depth + 1);
|
|
24
|
+
divide(a2, b2, depth + 1);
|
|
25
|
+
}
|
|
26
|
+
divide(upper, lower, 0);
|
|
27
|
+
return parts;
|
|
28
|
+
}
|