vexflow 5.0.0-alpha.2 → 5.0.0-alpha.3
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/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 +4 -4
- package/build/cjs/vexflow-debug-with-tests.js.map +1 -1
- package/build/cjs/vexflow-debug.js +4 -4
- 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/version.js +3 -3
- package/package.json +1 -3
- package/entry/vexflow-bravura.ts +0 -25
- package/entry/vexflow-core.ts +0 -16
- package/entry/vexflow-debug-with-tests.ts +0 -64
- package/entry/vexflow-debug.ts +0 -54
- package/entry/vexflow.ts +0 -47
- package/src/accidental.ts +0 -570
- package/src/annotation.ts +0 -294
- package/src/articulation.ts +0 -383
- package/src/barnote.ts +0 -95
- package/src/beam.ts +0 -999
- package/src/bend.ts +0 -268
- package/src/boundingbox.ts +0 -108
- package/src/canvascontext.ts +0 -348
- package/src/chordsymbol.ts +0 -473
- package/src/clef.ts +0 -164
- package/src/clefnote.ts +0 -50
- package/src/crescendo.ts +0 -132
- package/src/curve.ts +0 -195
- package/src/dot.ts +0 -171
- package/src/easyscore.ts +0 -538
- package/src/element.ts +0 -692
- package/src/factory.ts +0 -694
- package/src/font.ts +0 -343
- package/src/fonts/academico.ts +0 -2
- package/src/fonts/academicobold.ts +0 -2
- package/src/fonts/bravura.ts +0 -2
- package/src/fonts/gonville.ts +0 -2
- package/src/fonts/petaluma.ts +0 -2
- package/src/fonts/petalumascript.ts +0 -2
- package/src/formatter.ts +0 -1107
- package/src/fraction.ts +0 -245
- package/src/frethandfinger.ts +0 -180
- package/src/ghostnote.ts +0 -75
- package/src/glyphnote.ts +0 -68
- package/src/glyphs.ts +0 -5880
- package/src/gracenote.ts +0 -146
- package/src/gracenotegroup.ts +0 -194
- package/src/gracetabnote.ts +0 -30
- package/src/index.ts +0 -90
- package/src/keymanager.ts +0 -153
- package/src/keysignature.ts +0 -272
- package/src/keysignote.ts +0 -46
- package/src/metrics.ts +0 -222
- package/src/modifier.ts +0 -216
- package/src/modifiercontext.ts +0 -177
- package/src/multimeasurerest.ts +0 -229
- package/src/music.ts +0 -443
- package/src/note.ts +0 -690
- package/src/notehead.ts +0 -158
- package/src/notesubgroup.ts +0 -88
- package/src/ornament.ts +0 -301
- package/src/parenthesis.ts +0 -89
- package/src/parser.ts +0 -263
- package/src/pedalmarking.ts +0 -273
- package/src/registry.ts +0 -146
- package/src/rendercontext.ts +0 -98
- package/src/renderer.ts +0 -166
- package/src/repeatnote.ts +0 -25
- package/src/stave.ts +0 -885
- package/src/stavebarline.ts +0 -231
- package/src/staveconnector.ts +0 -281
- package/src/stavehairpin.ts +0 -209
- package/src/staveline.ts +0 -343
- package/src/stavemodifier.ts +0 -93
- package/src/stavenote.ts +0 -1230
- package/src/staverepetition.ts +0 -148
- package/src/stavesection.ts +0 -54
- package/src/stavetempo.ts +0 -162
- package/src/stavetext.ts +0 -69
- package/src/stavetie.ts +0 -235
- package/src/stavevolta.ts +0 -66
- package/src/stem.ts +0 -225
- package/src/stemmablenote.ts +0 -248
- package/src/stringnumber.ts +0 -286
- package/src/strokes.ts +0 -214
- package/src/svgcontext.ts +0 -639
- package/src/system.ts +0 -327
- package/src/tables.ts +0 -796
- package/src/tabnote.ts +0 -449
- package/src/tabslide.ts +0 -115
- package/src/tabstave.ts +0 -31
- package/src/tabtie.ts +0 -44
- package/src/textbracket.ts +0 -231
- package/src/textdynamics.ts +0 -101
- package/src/textnote.ts +0 -175
- package/src/tickable.ts +0 -374
- package/src/tickcontext.ts +0 -280
- package/src/timesignature.ts +0 -229
- package/src/timesignote.ts +0 -45
- package/src/tremolo.ts +0 -51
- package/src/tuning.ts +0 -90
- package/src/tuplet.ts +0 -349
- package/src/typeguard.ts +0 -136
- package/src/util.ts +0 -169
- package/src/version.ts +0 -4
- package/src/vexflow.ts +0 -378
- package/src/vibrato.ts +0 -99
- package/src/vibratobracket.ts +0 -81
- package/src/voice.ts +0 -302
- package/src/web.ts +0 -35
package/src/annotation.ts
DELETED
|
@@ -1,294 +0,0 @@
|
|
|
1
|
-
// Copyright (c) 2023-present VexFlow contributors: https://github.com/vexflow/vexflow/graphs/contributors
|
|
2
|
-
// MIT License
|
|
3
|
-
import { Font } from './font';
|
|
4
|
-
import { Metrics } from './metrics';
|
|
5
|
-
import { Modifier, ModifierPosition } from './modifier';
|
|
6
|
-
import { ModifierContextState } from './modifiercontext';
|
|
7
|
-
import { Stave } from './stave';
|
|
8
|
-
import { Stem } from './stem';
|
|
9
|
-
import { StemmableNote } from './stemmablenote';
|
|
10
|
-
import { Tables } from './tables';
|
|
11
|
-
import { Category, isStemmableNote, isTabNote } from './typeguard';
|
|
12
|
-
import { log } from './util';
|
|
13
|
-
|
|
14
|
-
// eslint-disable-next-line
|
|
15
|
-
function L(...args: any[]) {
|
|
16
|
-
if (Annotation.DEBUG) log('VexFlow.Annotation', args);
|
|
17
|
-
}
|
|
18
|
-
|
|
19
|
-
export enum AnnotationHorizontalJustify {
|
|
20
|
-
LEFT = 1,
|
|
21
|
-
CENTER = 2,
|
|
22
|
-
RIGHT = 3,
|
|
23
|
-
CENTER_STEM = 4,
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
export enum AnnotationVerticalJustify {
|
|
27
|
-
TOP = 1,
|
|
28
|
-
CENTER = 2,
|
|
29
|
-
BOTTOM = 3,
|
|
30
|
-
CENTER_STEM = 4,
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
/**
|
|
34
|
-
* Annotations are modifiers that can be attached to
|
|
35
|
-
* notes.
|
|
36
|
-
*
|
|
37
|
-
* See `tests/annotation_tests.ts` for usage examples.
|
|
38
|
-
*/
|
|
39
|
-
export class Annotation extends Modifier {
|
|
40
|
-
/** To enable logging for this class. Set `VexFlow.Annotation.DEBUG` to `true`. */
|
|
41
|
-
static DEBUG: boolean = false;
|
|
42
|
-
|
|
43
|
-
/** Annotations category string. */
|
|
44
|
-
static get CATEGORY(): string {
|
|
45
|
-
return Category.Annotation;
|
|
46
|
-
}
|
|
47
|
-
|
|
48
|
-
/** Text annotations can be positioned and justified relative to the note. */
|
|
49
|
-
static HorizontalJustify = AnnotationHorizontalJustify;
|
|
50
|
-
|
|
51
|
-
static HorizontalJustifyString: Record<string, number> = {
|
|
52
|
-
left: AnnotationHorizontalJustify.LEFT,
|
|
53
|
-
right: AnnotationHorizontalJustify.RIGHT,
|
|
54
|
-
center: AnnotationHorizontalJustify.CENTER,
|
|
55
|
-
centerStem: AnnotationHorizontalJustify.CENTER_STEM,
|
|
56
|
-
};
|
|
57
|
-
|
|
58
|
-
static VerticalJustify = AnnotationVerticalJustify;
|
|
59
|
-
|
|
60
|
-
static VerticalJustifyString: Record<string, number> = {
|
|
61
|
-
above: AnnotationVerticalJustify.TOP,
|
|
62
|
-
top: AnnotationVerticalJustify.TOP,
|
|
63
|
-
below: AnnotationVerticalJustify.BOTTOM,
|
|
64
|
-
bottom: AnnotationVerticalJustify.BOTTOM,
|
|
65
|
-
center: AnnotationVerticalJustify.CENTER,
|
|
66
|
-
centerStem: AnnotationVerticalJustify.CENTER_STEM,
|
|
67
|
-
};
|
|
68
|
-
|
|
69
|
-
// Use the same padding for annotations as note head so the
|
|
70
|
-
// words don't run into each other.
|
|
71
|
-
static get minAnnotationPadding(): number {
|
|
72
|
-
return Metrics.get('NoteHead.minPadding');
|
|
73
|
-
}
|
|
74
|
-
/** Arrange annotations within a `ModifierContext` */
|
|
75
|
-
static format(annotations: Annotation[], state: ModifierContextState): boolean {
|
|
76
|
-
if (!annotations || annotations.length === 0) return false;
|
|
77
|
-
let leftWidth = 0;
|
|
78
|
-
let rightWidth = 0;
|
|
79
|
-
let maxLeftGlyphWidth = 0;
|
|
80
|
-
let maxRightGlyphWidth = 0;
|
|
81
|
-
for (let i = 0; i < annotations.length; ++i) {
|
|
82
|
-
const annotation = annotations[i];
|
|
83
|
-
// Text height is expressed in fractional stave spaces.
|
|
84
|
-
const textLines = (2 + Font.convertSizeToPixelValue(annotation.fontInfo.size)) / Tables.STAVE_LINE_DISTANCE;
|
|
85
|
-
let verticalSpaceNeeded = textLines;
|
|
86
|
-
|
|
87
|
-
const note = annotation.checkAttachedNote();
|
|
88
|
-
const glyphWidth = note.getGlyphWidth();
|
|
89
|
-
// Get the text width from the font metrics.
|
|
90
|
-
const textWidth = annotation.getWidth();
|
|
91
|
-
if (annotation.horizontalJustification === AnnotationHorizontalJustify.RIGHT) {
|
|
92
|
-
maxLeftGlyphWidth = Math.max(glyphWidth, maxLeftGlyphWidth);
|
|
93
|
-
leftWidth = Math.max(leftWidth, textWidth) + Annotation.minAnnotationPadding;
|
|
94
|
-
} else if (annotation.horizontalJustification === AnnotationHorizontalJustify.LEFT) {
|
|
95
|
-
maxRightGlyphWidth = Math.max(glyphWidth, maxRightGlyphWidth);
|
|
96
|
-
rightWidth = Math.max(rightWidth, textWidth);
|
|
97
|
-
} else {
|
|
98
|
-
leftWidth = Math.max(leftWidth, textWidth / 2) + Annotation.minAnnotationPadding;
|
|
99
|
-
rightWidth = Math.max(rightWidth, textWidth / 2);
|
|
100
|
-
maxLeftGlyphWidth = Math.max(glyphWidth / 2, maxLeftGlyphWidth);
|
|
101
|
-
maxRightGlyphWidth = Math.max(glyphWidth / 2, maxRightGlyphWidth);
|
|
102
|
-
}
|
|
103
|
-
|
|
104
|
-
const stave: Stave | undefined = note.getStave();
|
|
105
|
-
const stemDirection = note.hasStem() ? note.getStemDirection() : Stem.UP;
|
|
106
|
-
let stemHeight = 0;
|
|
107
|
-
let lines = 5;
|
|
108
|
-
|
|
109
|
-
if (isTabNote(note)) {
|
|
110
|
-
if (note.renderOptions.drawStem) {
|
|
111
|
-
const stem = (note as StemmableNote).getStem();
|
|
112
|
-
if (stem) {
|
|
113
|
-
stemHeight = Math.abs(stem.getHeight()) / Tables.STAVE_LINE_DISTANCE;
|
|
114
|
-
}
|
|
115
|
-
} else {
|
|
116
|
-
stemHeight = 0;
|
|
117
|
-
}
|
|
118
|
-
} else if (isStemmableNote(note)) {
|
|
119
|
-
const stem = note.getStem();
|
|
120
|
-
if (stem && note.getNoteType() === 'n') {
|
|
121
|
-
stemHeight = Math.abs(stem.getHeight()) / Tables.STAVE_LINE_DISTANCE;
|
|
122
|
-
}
|
|
123
|
-
}
|
|
124
|
-
if (stave) {
|
|
125
|
-
lines = stave.getNumLines();
|
|
126
|
-
}
|
|
127
|
-
|
|
128
|
-
if (annotation.verticalJustification === this.VerticalJustify.TOP) {
|
|
129
|
-
let noteLine = note.getLineNumber(true);
|
|
130
|
-
if (isTabNote(note)) {
|
|
131
|
-
noteLine = lines - (note.leastString() - 0.5);
|
|
132
|
-
}
|
|
133
|
-
if (stemDirection === Stem.UP) {
|
|
134
|
-
noteLine += stemHeight;
|
|
135
|
-
}
|
|
136
|
-
const curTop = noteLine + state.topTextLine + 0.5;
|
|
137
|
-
if (curTop < lines) {
|
|
138
|
-
annotation.setTextLine(lines - noteLine);
|
|
139
|
-
verticalSpaceNeeded += lines - noteLine;
|
|
140
|
-
state.topTextLine = verticalSpaceNeeded;
|
|
141
|
-
} else {
|
|
142
|
-
annotation.setTextLine(state.topTextLine);
|
|
143
|
-
state.topTextLine += verticalSpaceNeeded;
|
|
144
|
-
}
|
|
145
|
-
} else if (annotation.verticalJustification === this.VerticalJustify.BOTTOM) {
|
|
146
|
-
let noteLine = lines - note.getLineNumber();
|
|
147
|
-
if (isTabNote(note)) {
|
|
148
|
-
noteLine = note.greatestString() - 1;
|
|
149
|
-
}
|
|
150
|
-
if (stemDirection === Stem.DOWN) {
|
|
151
|
-
noteLine += stemHeight;
|
|
152
|
-
}
|
|
153
|
-
const curBottom = noteLine + state.textLine + 1;
|
|
154
|
-
if (curBottom < lines) {
|
|
155
|
-
annotation.setTextLine(lines - curBottom);
|
|
156
|
-
verticalSpaceNeeded += lines - curBottom;
|
|
157
|
-
state.textLine = verticalSpaceNeeded;
|
|
158
|
-
} else {
|
|
159
|
-
annotation.setTextLine(state.textLine);
|
|
160
|
-
state.textLine += verticalSpaceNeeded;
|
|
161
|
-
}
|
|
162
|
-
} else {
|
|
163
|
-
annotation.setTextLine(state.textLine);
|
|
164
|
-
}
|
|
165
|
-
}
|
|
166
|
-
const rightOverlap = Math.min(
|
|
167
|
-
Math.max(rightWidth - maxRightGlyphWidth, 0),
|
|
168
|
-
Math.max(rightWidth - state.rightShift, 0)
|
|
169
|
-
);
|
|
170
|
-
const leftOverlap = Math.min(Math.max(leftWidth - maxLeftGlyphWidth, 0), Math.max(leftWidth - state.leftShift, 0));
|
|
171
|
-
state.leftShift += leftOverlap;
|
|
172
|
-
state.rightShift += rightOverlap;
|
|
173
|
-
return true;
|
|
174
|
-
}
|
|
175
|
-
|
|
176
|
-
protected horizontalJustification: AnnotationHorizontalJustify;
|
|
177
|
-
protected verticalJustification: AnnotationVerticalJustify;
|
|
178
|
-
|
|
179
|
-
/**
|
|
180
|
-
* Annotations inherit from `Modifier` and is positioned correctly when
|
|
181
|
-
* in a `ModifierContext`.
|
|
182
|
-
* Create a new `Annotation` with the string `text`.
|
|
183
|
-
*/
|
|
184
|
-
constructor(text: string) {
|
|
185
|
-
super();
|
|
186
|
-
|
|
187
|
-
this.text = text;
|
|
188
|
-
this.horizontalJustification = AnnotationHorizontalJustify.CENTER;
|
|
189
|
-
// warning: the default in the constructor is TOP, but in the factory the default is BOTTOM.
|
|
190
|
-
// this is to support legacy application that may expect this.
|
|
191
|
-
this.verticalJustification = AnnotationVerticalJustify.TOP;
|
|
192
|
-
}
|
|
193
|
-
|
|
194
|
-
/**
|
|
195
|
-
* Set vertical position of text (above or below stave).
|
|
196
|
-
* @param just value in `AnnotationVerticalJustify`.
|
|
197
|
-
*/
|
|
198
|
-
setVerticalJustification(just: string | AnnotationVerticalJustify): this {
|
|
199
|
-
this.verticalJustification = typeof just === 'string' ? Annotation.VerticalJustifyString[just] : just;
|
|
200
|
-
return this;
|
|
201
|
-
}
|
|
202
|
-
|
|
203
|
-
/**
|
|
204
|
-
* Get horizontal justification.
|
|
205
|
-
*/
|
|
206
|
-
getJustification(): AnnotationHorizontalJustify {
|
|
207
|
-
return this.horizontalJustification;
|
|
208
|
-
}
|
|
209
|
-
|
|
210
|
-
/**
|
|
211
|
-
* Set horizontal justification.
|
|
212
|
-
* @param justification value in `Annotation.Justify`.
|
|
213
|
-
*/
|
|
214
|
-
setJustification(just: string | AnnotationHorizontalJustify): this {
|
|
215
|
-
this.horizontalJustification = typeof just === 'string' ? Annotation.HorizontalJustifyString[just] : just;
|
|
216
|
-
return this;
|
|
217
|
-
}
|
|
218
|
-
|
|
219
|
-
/** Render text beside the note. */
|
|
220
|
-
draw(): void {
|
|
221
|
-
const ctx = this.checkContext();
|
|
222
|
-
const note = this.checkAttachedNote();
|
|
223
|
-
const stemDirection = note.hasStem() ? note.getStemDirection() : Stem.UP;
|
|
224
|
-
const start = note.getModifierStartXY(ModifierPosition.ABOVE, this.index);
|
|
225
|
-
|
|
226
|
-
this.setRendered();
|
|
227
|
-
|
|
228
|
-
// Apply style might not save context, if this.style is undefined, so we
|
|
229
|
-
// still need to save context state just before this, since we will be
|
|
230
|
-
// changing ctx parameters below.
|
|
231
|
-
this.applyStyle();
|
|
232
|
-
ctx.openGroup('annotation', this.getAttribute('id'));
|
|
233
|
-
|
|
234
|
-
const textWidth = this.getWidth();
|
|
235
|
-
const textHeight = Font.convertSizeToPixelValue(this.fontInfo.size);
|
|
236
|
-
let x;
|
|
237
|
-
let y;
|
|
238
|
-
|
|
239
|
-
if (this.horizontalJustification === AnnotationHorizontalJustify.LEFT) {
|
|
240
|
-
x = start.x;
|
|
241
|
-
} else if (this.horizontalJustification === AnnotationHorizontalJustify.RIGHT) {
|
|
242
|
-
x = start.x - textWidth;
|
|
243
|
-
} else if (this.horizontalJustification === AnnotationHorizontalJustify.CENTER) {
|
|
244
|
-
x = start.x - textWidth / 2;
|
|
245
|
-
} /* CENTER_STEM */ else {
|
|
246
|
-
x = (note as StemmableNote).getStemX() - textWidth / 2;
|
|
247
|
-
}
|
|
248
|
-
|
|
249
|
-
let stemExt: Record<string, number> = {};
|
|
250
|
-
let spacing = 0;
|
|
251
|
-
const hasStem = note.hasStem();
|
|
252
|
-
const stave = note.checkStave();
|
|
253
|
-
|
|
254
|
-
// The position of the text varies based on whether or not the note
|
|
255
|
-
// has a stem.
|
|
256
|
-
if (hasStem) {
|
|
257
|
-
stemExt = (note as StemmableNote).checkStem().getExtents();
|
|
258
|
-
spacing = stave.getSpacingBetweenLines();
|
|
259
|
-
}
|
|
260
|
-
|
|
261
|
-
if (this.verticalJustification === AnnotationVerticalJustify.BOTTOM) {
|
|
262
|
-
// Use the largest (lowest) Y value
|
|
263
|
-
const ys: number[] = note.getYs();
|
|
264
|
-
y = ys.reduce((a, b) => (a > b ? a : b));
|
|
265
|
-
y += (this.textLine + 1) * Tables.STAVE_LINE_DISTANCE + textHeight;
|
|
266
|
-
if (hasStem && stemDirection === Stem.DOWN) {
|
|
267
|
-
y = Math.max(y, stemExt.topY + textHeight + spacing * this.textLine);
|
|
268
|
-
}
|
|
269
|
-
} else if (this.verticalJustification === AnnotationVerticalJustify.CENTER) {
|
|
270
|
-
const yt = note.getYForTopText(this.textLine) - 1;
|
|
271
|
-
const yb = stave.getYForBottomText(this.textLine);
|
|
272
|
-
y = yt + (yb - yt) / 2 + textHeight / 2;
|
|
273
|
-
} else if (this.verticalJustification === AnnotationVerticalJustify.TOP) {
|
|
274
|
-
const topY = Math.min(...note.getYs());
|
|
275
|
-
y = topY - (this.textLine + 1) * Tables.STAVE_LINE_DISTANCE;
|
|
276
|
-
if (hasStem && stemDirection === Stem.UP) {
|
|
277
|
-
// If the stem is above the stave already, go with default line width vs. actual
|
|
278
|
-
// since the lines between don't really matter.
|
|
279
|
-
spacing = stemExt.topY < stave.getTopLineTopY() ? Tables.STAVE_LINE_DISTANCE : spacing;
|
|
280
|
-
y = Math.min(y, stemExt.topY - spacing * (this.textLine + 1));
|
|
281
|
-
}
|
|
282
|
-
} /* CENTER_STEM */ else {
|
|
283
|
-
const extents = note.getStemExtents();
|
|
284
|
-
y = extents.topY + (extents.baseY - extents.topY) / 2 + textHeight / 2;
|
|
285
|
-
}
|
|
286
|
-
|
|
287
|
-
L('Rendering annotation: ', this.text, x, y);
|
|
288
|
-
this.x = x;
|
|
289
|
-
this.y = y;
|
|
290
|
-
this.renderText(ctx, 0, 0);
|
|
291
|
-
ctx.closeGroup();
|
|
292
|
-
this.restoreStyle();
|
|
293
|
-
}
|
|
294
|
-
}
|
package/src/articulation.ts
DELETED
|
@@ -1,383 +0,0 @@
|
|
|
1
|
-
// Copyright (c) 2023-present VexFlow contributors: https://github.com/vexflow/vexflow/graphs/contributors
|
|
2
|
-
// @author Larry Kuhns.
|
|
3
|
-
// MIT License
|
|
4
|
-
|
|
5
|
-
import { Builder } from './easyscore';
|
|
6
|
-
import { Glyphs } from './glyphs';
|
|
7
|
-
import { Modifier } from './modifier';
|
|
8
|
-
import { ModifierContextState } from './modifiercontext';
|
|
9
|
-
import { Note } from './note';
|
|
10
|
-
import { Stave } from './stave';
|
|
11
|
-
import { Stem } from './stem';
|
|
12
|
-
import { StemmableNote } from './stemmablenote';
|
|
13
|
-
import { Tables } from './tables';
|
|
14
|
-
import { Category, isGraceNote, isStaveNote, isStemmableNote, isTabNote } from './typeguard';
|
|
15
|
-
import { log, RuntimeError } from './util';
|
|
16
|
-
|
|
17
|
-
export interface ArticulationStruct {
|
|
18
|
-
code?: string;
|
|
19
|
-
aboveCode?: string;
|
|
20
|
-
belowCode?: string;
|
|
21
|
-
betweenLines: boolean;
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
// eslint-disable-next-line
|
|
25
|
-
function L(...args: any[]) {
|
|
26
|
-
if (Articulation.DEBUG) log('VexFlow.Articulation', args);
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
const { ABOVE, BELOW } = Modifier.Position;
|
|
30
|
-
|
|
31
|
-
function roundToNearestHalf(mathFn: (a: number) => number, value: number): number {
|
|
32
|
-
return mathFn(value / 0.5) * 0.5;
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
// This includes both staff and ledger lines
|
|
36
|
-
function isWithinLines(line: number, position: number): boolean {
|
|
37
|
-
return position === ABOVE ? line <= 5 : line >= 1;
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
function getRoundingFunction(line: number, position: number): (a: number) => number {
|
|
41
|
-
if (isWithinLines(line, position)) {
|
|
42
|
-
if (position === ABOVE) {
|
|
43
|
-
return Math.ceil;
|
|
44
|
-
} else {
|
|
45
|
-
return Math.floor;
|
|
46
|
-
}
|
|
47
|
-
} else {
|
|
48
|
-
return Math.round;
|
|
49
|
-
}
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
function snapLineToStaff(canSitBetweenLines: boolean, line: number, position: number, offsetDirection: number): number {
|
|
53
|
-
// Initially, snap to nearest staff line or space
|
|
54
|
-
const snappedLine = roundToNearestHalf(getRoundingFunction(line, position), line);
|
|
55
|
-
const canSnapToStaffSpace = canSitBetweenLines && isWithinLines(snappedLine, position);
|
|
56
|
-
const onStaffLine = snappedLine % 1 === 0;
|
|
57
|
-
|
|
58
|
-
if (canSnapToStaffSpace && onStaffLine) {
|
|
59
|
-
const HALF_STAFF_SPACE = 0.5;
|
|
60
|
-
return snappedLine + HALF_STAFF_SPACE * -offsetDirection;
|
|
61
|
-
} else {
|
|
62
|
-
return snappedLine;
|
|
63
|
-
}
|
|
64
|
-
}
|
|
65
|
-
|
|
66
|
-
// Helper function for checking if a Note object is either a StaveNote or a GraceNote.
|
|
67
|
-
const isStaveOrGraceNote = (note: Note) => isStaveNote(note) || isGraceNote(note);
|
|
68
|
-
|
|
69
|
-
export function getTopY(note: Note, textLine: number): number {
|
|
70
|
-
const stemDirection = note.getStemDirection();
|
|
71
|
-
const { topY: stemTipY, baseY: stemBaseY } = note.getStemExtents();
|
|
72
|
-
|
|
73
|
-
if (isStaveOrGraceNote(note)) {
|
|
74
|
-
if (note.hasStem()) {
|
|
75
|
-
if (stemDirection === Stem.UP) {
|
|
76
|
-
return stemTipY;
|
|
77
|
-
} else {
|
|
78
|
-
return stemBaseY;
|
|
79
|
-
}
|
|
80
|
-
} else {
|
|
81
|
-
return Math.min(...note.getYs());
|
|
82
|
-
}
|
|
83
|
-
} else if (isTabNote(note)) {
|
|
84
|
-
if (note.hasStem()) {
|
|
85
|
-
if (stemDirection === Stem.UP) {
|
|
86
|
-
return stemTipY;
|
|
87
|
-
} else {
|
|
88
|
-
return note.checkStave().getYForTopText(textLine);
|
|
89
|
-
}
|
|
90
|
-
} else {
|
|
91
|
-
return note.checkStave().getYForTopText(textLine);
|
|
92
|
-
}
|
|
93
|
-
} else {
|
|
94
|
-
throw new RuntimeError('UnknownCategory', 'Only can get the top and bottom ys of stavenotes and tabnotes');
|
|
95
|
-
}
|
|
96
|
-
}
|
|
97
|
-
|
|
98
|
-
export function getBottomY(note: Note, textLine: number): number {
|
|
99
|
-
const stemDirection = note.getStemDirection();
|
|
100
|
-
const { topY: stemTipY, baseY: stemBaseY } = note.getStemExtents();
|
|
101
|
-
|
|
102
|
-
if (isStaveOrGraceNote(note)) {
|
|
103
|
-
if (note.hasStem()) {
|
|
104
|
-
if (stemDirection === Stem.UP) {
|
|
105
|
-
return stemBaseY;
|
|
106
|
-
} else {
|
|
107
|
-
return stemTipY;
|
|
108
|
-
}
|
|
109
|
-
} else {
|
|
110
|
-
return Math.max(...note.getYs());
|
|
111
|
-
}
|
|
112
|
-
} else if (isTabNote(note)) {
|
|
113
|
-
if (note.hasStem()) {
|
|
114
|
-
if (stemDirection === Stem.UP) {
|
|
115
|
-
return note.checkStave().getYForBottomText(textLine);
|
|
116
|
-
} else {
|
|
117
|
-
return stemTipY;
|
|
118
|
-
}
|
|
119
|
-
} else {
|
|
120
|
-
return note.checkStave().getYForBottomText(textLine);
|
|
121
|
-
}
|
|
122
|
-
} else {
|
|
123
|
-
throw new RuntimeError('UnknownCategory', 'Only can get the top and bottom ys of stavenotes and tabnotes');
|
|
124
|
-
}
|
|
125
|
-
}
|
|
126
|
-
|
|
127
|
-
/**
|
|
128
|
-
* Get the initial offset of the articulation from the y value of the starting position.
|
|
129
|
-
* This is required because the top/bottom text positions already have spacing applied to
|
|
130
|
-
* provide a "visually pleasant" default position. However the y values provided from
|
|
131
|
-
* the stavenote's top/bottom do *not* have any pre-applied spacing. This function
|
|
132
|
-
* normalizes this asymmetry.
|
|
133
|
-
* @param note
|
|
134
|
-
* @param position
|
|
135
|
-
* @returns
|
|
136
|
-
*/
|
|
137
|
-
export function getInitialOffset(note: Note, position: number): number {
|
|
138
|
-
const isOnStemTip =
|
|
139
|
-
(position === ABOVE && note.getStemDirection() === Stem.UP) ||
|
|
140
|
-
(position === BELOW && note.getStemDirection() === Stem.DOWN);
|
|
141
|
-
|
|
142
|
-
if (isStaveOrGraceNote(note)) {
|
|
143
|
-
if (note.hasStem() && isOnStemTip) {
|
|
144
|
-
return 0.5;
|
|
145
|
-
} else {
|
|
146
|
-
// this amount is larger than the stem-tip offset because we start from
|
|
147
|
-
// the center of the notehead
|
|
148
|
-
return 1;
|
|
149
|
-
}
|
|
150
|
-
} else {
|
|
151
|
-
if (note.hasStem() && isOnStemTip) {
|
|
152
|
-
return 1;
|
|
153
|
-
} else {
|
|
154
|
-
return 0;
|
|
155
|
-
}
|
|
156
|
-
}
|
|
157
|
-
}
|
|
158
|
-
|
|
159
|
-
/**
|
|
160
|
-
* Articulations and Accents are modifiers that can be
|
|
161
|
-
* attached to notes. The complete list of articulations is available in
|
|
162
|
-
* `tables.ts` under `VexFlow.articulationCodes`.
|
|
163
|
-
*
|
|
164
|
-
* See `tests/articulation_tests.ts` for usage examples.
|
|
165
|
-
*/
|
|
166
|
-
export class Articulation extends Modifier {
|
|
167
|
-
/** To enable logging for this class. Set `VexFlow.Articulation.DEBUG` to `true`. */
|
|
168
|
-
static DEBUG: boolean = false;
|
|
169
|
-
|
|
170
|
-
/** Articulations category string. */
|
|
171
|
-
static get CATEGORY(): string {
|
|
172
|
-
return Category.Articulation;
|
|
173
|
-
}
|
|
174
|
-
|
|
175
|
-
protected static readonly INITIAL_OFFSET: number = -0.5;
|
|
176
|
-
|
|
177
|
-
/** Articulation code provided to the constructor. */
|
|
178
|
-
readonly type: string;
|
|
179
|
-
|
|
180
|
-
protected articulation: ArticulationStruct;
|
|
181
|
-
|
|
182
|
-
protected heightShift = 0;
|
|
183
|
-
/**
|
|
184
|
-
* FIXME:
|
|
185
|
-
* Most of the complex formatting logic (ie: snapping to space) is
|
|
186
|
-
* actually done in .render(). But that logic belongs in this method.
|
|
187
|
-
*
|
|
188
|
-
* Unfortunately, this isn't possible because, by this point, stem lengths
|
|
189
|
-
* have not yet been finalized. Finalized stem lengths are required to determine the
|
|
190
|
-
* initial position of any stem-side articulation.
|
|
191
|
-
*
|
|
192
|
-
* This indicates that all objects should have their stave set before being
|
|
193
|
-
* formatted. It can't be an optional if you want accurate vertical positioning.
|
|
194
|
-
* Consistently positioned articulations that play nice with other modifiers
|
|
195
|
-
* won't be possible until we stop relying on render-time formatting.
|
|
196
|
-
*
|
|
197
|
-
* Ideally, when this function has completed, the vertical articulation positions
|
|
198
|
-
* should be ready to render without further adjustment. But the current state
|
|
199
|
-
* is far from this ideal.
|
|
200
|
-
*/
|
|
201
|
-
static format(articulations: Articulation[], state: ModifierContextState): boolean {
|
|
202
|
-
if (!articulations || articulations.length === 0) return false;
|
|
203
|
-
|
|
204
|
-
const margin = 0.5;
|
|
205
|
-
let maxGlyphWidth = 0;
|
|
206
|
-
|
|
207
|
-
const getIncrement = (articulation: Articulation, line: number, position: number) =>
|
|
208
|
-
roundToNearestHalf(getRoundingFunction(line, position), articulation.height / 10 + margin);
|
|
209
|
-
|
|
210
|
-
articulations.forEach((articulation) => {
|
|
211
|
-
const note = articulation.checkAttachedNote();
|
|
212
|
-
maxGlyphWidth = Math.max(note.getGlyphWidth(), maxGlyphWidth);
|
|
213
|
-
let lines = 5;
|
|
214
|
-
const stemDirection = note.hasStem() ? note.getStemDirection() : Stem.UP;
|
|
215
|
-
let stemHeight = 0;
|
|
216
|
-
// Decide if we need to consider beam direction in placement.
|
|
217
|
-
|
|
218
|
-
if (isStemmableNote(note)) {
|
|
219
|
-
const stem = note.getStem();
|
|
220
|
-
if (stem) {
|
|
221
|
-
stemHeight = Math.abs(stem.getHeight()) / Tables.STAVE_LINE_DISTANCE;
|
|
222
|
-
}
|
|
223
|
-
}
|
|
224
|
-
const stave: Stave | undefined = note.getStave();
|
|
225
|
-
if (stave) {
|
|
226
|
-
lines = stave.getNumLines();
|
|
227
|
-
}
|
|
228
|
-
if (articulation.getPosition() === ABOVE) {
|
|
229
|
-
let noteLine = note.getLineNumber(true);
|
|
230
|
-
if (stemDirection === Stem.UP) {
|
|
231
|
-
noteLine += stemHeight;
|
|
232
|
-
}
|
|
233
|
-
let increment = getIncrement(articulation, state.topTextLine, ABOVE);
|
|
234
|
-
const curTop = noteLine + state.topTextLine + 0.5;
|
|
235
|
-
// If articulation must be above stave, add lines between note and stave top
|
|
236
|
-
if (!articulation.articulation.betweenLines && curTop < lines) {
|
|
237
|
-
increment += lines - curTop;
|
|
238
|
-
}
|
|
239
|
-
articulation.setTextLine(state.topTextLine);
|
|
240
|
-
state.topTextLine += increment;
|
|
241
|
-
} else if (articulation.getPosition() === BELOW) {
|
|
242
|
-
let noteLine = Math.max(lines - note.getLineNumber(), 0);
|
|
243
|
-
if (stemDirection === Stem.DOWN) {
|
|
244
|
-
noteLine += stemHeight;
|
|
245
|
-
}
|
|
246
|
-
let increment = getIncrement(articulation, state.textLine, BELOW);
|
|
247
|
-
const curBottom = noteLine + state.textLine + 0.5;
|
|
248
|
-
// if articulation must be below stave, add lines from note to stave bottom
|
|
249
|
-
if (!articulation.articulation.betweenLines && curBottom < lines) {
|
|
250
|
-
increment += lines - curBottom;
|
|
251
|
-
}
|
|
252
|
-
articulation.setTextLine(state.textLine);
|
|
253
|
-
state.textLine += increment;
|
|
254
|
-
}
|
|
255
|
-
});
|
|
256
|
-
|
|
257
|
-
const width = articulations
|
|
258
|
-
.map((articulation) => articulation.getWidth())
|
|
259
|
-
.reduce((maxWidth, articWidth) => Math.max(articWidth, maxWidth));
|
|
260
|
-
const overlap = Math.min(
|
|
261
|
-
Math.max(width - maxGlyphWidth, 0),
|
|
262
|
-
Math.max(width - (state.leftShift + state.rightShift), 0)
|
|
263
|
-
);
|
|
264
|
-
|
|
265
|
-
state.leftShift += overlap / 2;
|
|
266
|
-
state.rightShift += overlap / 2;
|
|
267
|
-
return true;
|
|
268
|
-
}
|
|
269
|
-
|
|
270
|
-
static easyScoreHook({ articulations }: { articulations: string }, note: StemmableNote, builder: Builder): void {
|
|
271
|
-
if (!articulations) return;
|
|
272
|
-
|
|
273
|
-
const articNameToCode: Record<string, string> = {
|
|
274
|
-
staccato: 'a.',
|
|
275
|
-
tenuto: 'a-',
|
|
276
|
-
accent: 'a>',
|
|
277
|
-
};
|
|
278
|
-
|
|
279
|
-
articulations
|
|
280
|
-
.split(',')
|
|
281
|
-
.map((articString) => articString.trim().split('.'))
|
|
282
|
-
.map(([name, position]) => {
|
|
283
|
-
const artic: { type: string; position?: number } = { type: articNameToCode[name] };
|
|
284
|
-
if (position) artic.position = Modifier.PositionString[position];
|
|
285
|
-
return builder.getFactory().Articulation(artic);
|
|
286
|
-
})
|
|
287
|
-
.map((artic) => note.addModifier(artic, 0));
|
|
288
|
-
}
|
|
289
|
-
|
|
290
|
-
/**
|
|
291
|
-
* Create a new articulation.
|
|
292
|
-
* @param type entry in `VexFlow.articulationCodes` in `tables.ts` or Glyph code.
|
|
293
|
-
*
|
|
294
|
-
* Notes default positions (see https://w3c.github.io/smufl/latest/tables/articulation.html):
|
|
295
|
-
* - Even codes will be positioned ABOVE
|
|
296
|
-
* - Odd codes will be positioned BELOW
|
|
297
|
-
*/
|
|
298
|
-
constructor(type: string) {
|
|
299
|
-
super();
|
|
300
|
-
|
|
301
|
-
this.type = type;
|
|
302
|
-
this.position = ABOVE;
|
|
303
|
-
if (!Tables.articulationCodes(this.type)) {
|
|
304
|
-
if ((this.type.codePointAt(0) ?? 0) % 2 === 0) this.position = ABOVE;
|
|
305
|
-
else this.position = BELOW;
|
|
306
|
-
}
|
|
307
|
-
|
|
308
|
-
this.articulation = { betweenLines: false };
|
|
309
|
-
this.reset();
|
|
310
|
-
}
|
|
311
|
-
|
|
312
|
-
protected reset(): void {
|
|
313
|
-
this.articulation = Tables.articulationCodes(this.type);
|
|
314
|
-
// Use type as glyph code, if not defined as articulation code
|
|
315
|
-
if (!this.articulation) {
|
|
316
|
-
this.articulation = { code: this.type, betweenLines: false };
|
|
317
|
-
}
|
|
318
|
-
const code =
|
|
319
|
-
(this.position === ABOVE ? this.articulation.aboveCode : this.articulation.belowCode) ||
|
|
320
|
-
this.articulation.code ||
|
|
321
|
-
Glyphs.null;
|
|
322
|
-
this.text = code;
|
|
323
|
-
}
|
|
324
|
-
|
|
325
|
-
/** Set if articulation should be rendered between lines. */
|
|
326
|
-
setBetweenLines(betweenLines = true): this {
|
|
327
|
-
this.articulation.betweenLines = betweenLines;
|
|
328
|
-
return this;
|
|
329
|
-
}
|
|
330
|
-
|
|
331
|
-
/** Render articulation in position next to note. */
|
|
332
|
-
draw(): void {
|
|
333
|
-
const ctx = this.checkContext();
|
|
334
|
-
const note = this.checkAttachedNote();
|
|
335
|
-
this.setRendered();
|
|
336
|
-
|
|
337
|
-
const index = this.checkIndex();
|
|
338
|
-
const { position, textLine } = this;
|
|
339
|
-
const canSitBetweenLines = this.articulation.betweenLines;
|
|
340
|
-
|
|
341
|
-
const stave = note.checkStave();
|
|
342
|
-
const staffSpace = stave.getSpacingBetweenLines();
|
|
343
|
-
const isTab = isTabNote(note);
|
|
344
|
-
|
|
345
|
-
// Articulations are centered over/under the note head.
|
|
346
|
-
const { x } = note.getModifierStartXY(position, index);
|
|
347
|
-
const shouldSitOutsideStaff = !canSitBetweenLines || isTab;
|
|
348
|
-
|
|
349
|
-
const initialOffset = getInitialOffset(note, position);
|
|
350
|
-
|
|
351
|
-
let y = (
|
|
352
|
-
{
|
|
353
|
-
[ABOVE]: () => {
|
|
354
|
-
this.setOrigin(0.5, 1);
|
|
355
|
-
const y = getTopY(note, textLine) - (textLine + initialOffset) * staffSpace;
|
|
356
|
-
return shouldSitOutsideStaff ? Math.min(stave.getYForTopText(Articulation.INITIAL_OFFSET), y) : y;
|
|
357
|
-
},
|
|
358
|
-
[BELOW]: () => {
|
|
359
|
-
this.setOrigin(0.5, 0);
|
|
360
|
-
const y = getBottomY(note, textLine) + (textLine + initialOffset) * staffSpace;
|
|
361
|
-
return shouldSitOutsideStaff ? Math.max(stave.getYForBottomText(Articulation.INITIAL_OFFSET), y) : y;
|
|
362
|
-
},
|
|
363
|
-
} as Record<number, () => number>
|
|
364
|
-
)[position]();
|
|
365
|
-
|
|
366
|
-
if (!isTab) {
|
|
367
|
-
const offsetDirection = position === ABOVE ? -1 : +1;
|
|
368
|
-
const noteLine = note.getKeyProps()[index].line;
|
|
369
|
-
const distanceFromNote = (note.getYs()[index] - y) / staffSpace;
|
|
370
|
-
const articLine = distanceFromNote + Number(noteLine);
|
|
371
|
-
const snappedLine = snapLineToStaff(canSitBetweenLines, articLine, position, offsetDirection);
|
|
372
|
-
|
|
373
|
-
if (isWithinLines(snappedLine, position)) this.setOrigin(0.5, 0.5);
|
|
374
|
-
|
|
375
|
-
y += Math.abs(snappedLine - articLine) * staffSpace * offsetDirection;
|
|
376
|
-
}
|
|
377
|
-
|
|
378
|
-
L(`Rendering articulation at (x: ${x}, y: ${y})`);
|
|
379
|
-
this.x = x;
|
|
380
|
-
this.y = y;
|
|
381
|
-
this.renderText(ctx, 0, 0);
|
|
382
|
-
}
|
|
383
|
-
}
|