vexflow 5.0.0-alpha.2 → 5.0.0-alpha.4

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.
Files changed (157) hide show
  1. package/build/cjs/vexflow-bravura.js +2 -2
  2. package/build/cjs/vexflow-bravura.js.map +1 -1
  3. package/build/cjs/vexflow-core.js +2 -2
  4. package/build/cjs/vexflow-core.js.map +1 -1
  5. package/build/cjs/vexflow-debug-with-tests.js +528 -492
  6. package/build/cjs/vexflow-debug-with-tests.js.map +1 -1
  7. package/build/cjs/vexflow-debug.js +460 -452
  8. package/build/cjs/vexflow-debug.js.map +1 -1
  9. package/build/cjs/vexflow.js +2 -2
  10. package/build/cjs/vexflow.js.map +1 -1
  11. package/build/esm/src/accidental.js +22 -28
  12. package/build/esm/src/beam.js +24 -42
  13. package/build/esm/src/clefnote.js +6 -1
  14. package/build/esm/src/element.js +83 -101
  15. package/build/esm/src/fonts/gonville.js +1 -1
  16. package/build/esm/src/formatter.js +9 -17
  17. package/build/esm/src/fraction.js +15 -22
  18. package/build/esm/src/glyphnote.js +3 -2
  19. package/build/esm/src/metrics.js +3 -0
  20. package/build/esm/src/modifier.js +6 -21
  21. package/build/esm/src/multimeasurerest.js +16 -25
  22. package/build/esm/src/note.js +7 -0
  23. package/build/esm/src/registry.js +5 -19
  24. package/build/esm/src/stave.js +4 -6
  25. package/build/esm/src/stavenote.js +26 -40
  26. package/build/esm/src/stavetempo.js +4 -11
  27. package/build/esm/src/stavetie.js +82 -36
  28. package/build/esm/src/svgcontext.js +5 -12
  29. package/build/esm/src/tickable.js +13 -26
  30. package/build/esm/src/tickcontext.js +20 -2
  31. package/build/esm/src/version.js +3 -3
  32. package/build/esm/src/vibrato.js +6 -1
  33. package/build/esm/tests/clef_tests.js +5 -1
  34. package/build/esm/tests/glyphnote_tests.js +6 -0
  35. package/build/esm/tests/stavetie_tests.js +29 -0
  36. package/build/esm/tests/tabstave_tests.js +1 -0
  37. package/build/esm/tests/vexflow_test_helpers.js +25 -39
  38. package/build/types/src/accidental.d.ts +19 -1
  39. package/build/types/src/annotation.d.ts +1 -1
  40. package/build/types/src/beam.d.ts +12 -1
  41. package/build/types/src/clefnote.d.ts +2 -0
  42. package/build/types/src/element.d.ts +13 -3
  43. package/build/types/src/fonts/gonville.d.ts +1 -1
  44. package/build/types/src/formatter.d.ts +5 -3
  45. package/build/types/src/fraction.d.ts +12 -7
  46. package/build/types/src/modifier.d.ts +2 -1
  47. package/build/types/src/multimeasurerest.d.ts +4 -1
  48. package/build/types/src/note.d.ts +2 -0
  49. package/build/types/src/registry.d.ts +1 -1
  50. package/build/types/src/stavenote.d.ts +2 -1
  51. package/build/types/src/stavetempo.d.ts +3 -3
  52. package/build/types/src/stavetie.d.ts +39 -2
  53. package/build/types/src/svgcontext.d.ts +1 -1
  54. package/build/types/src/tickable.d.ts +2 -1
  55. package/build/types/src/tickcontext.d.ts +30 -0
  56. package/package.json +2 -4
  57. package/tests/flow.html +2 -2
  58. package/entry/vexflow-bravura.ts +0 -25
  59. package/entry/vexflow-core.ts +0 -16
  60. package/entry/vexflow-debug-with-tests.ts +0 -64
  61. package/entry/vexflow-debug.ts +0 -54
  62. package/entry/vexflow.ts +0 -47
  63. package/src/accidental.ts +0 -570
  64. package/src/annotation.ts +0 -294
  65. package/src/articulation.ts +0 -383
  66. package/src/barnote.ts +0 -95
  67. package/src/beam.ts +0 -999
  68. package/src/bend.ts +0 -268
  69. package/src/boundingbox.ts +0 -108
  70. package/src/canvascontext.ts +0 -348
  71. package/src/chordsymbol.ts +0 -473
  72. package/src/clef.ts +0 -164
  73. package/src/clefnote.ts +0 -50
  74. package/src/crescendo.ts +0 -132
  75. package/src/curve.ts +0 -195
  76. package/src/dot.ts +0 -171
  77. package/src/easyscore.ts +0 -538
  78. package/src/element.ts +0 -692
  79. package/src/factory.ts +0 -694
  80. package/src/font.ts +0 -343
  81. package/src/fonts/academico.ts +0 -2
  82. package/src/fonts/academicobold.ts +0 -2
  83. package/src/fonts/bravura.ts +0 -2
  84. package/src/fonts/gonville.ts +0 -2
  85. package/src/fonts/petaluma.ts +0 -2
  86. package/src/fonts/petalumascript.ts +0 -2
  87. package/src/formatter.ts +0 -1107
  88. package/src/fraction.ts +0 -245
  89. package/src/frethandfinger.ts +0 -180
  90. package/src/ghostnote.ts +0 -75
  91. package/src/glyphnote.ts +0 -68
  92. package/src/glyphs.ts +0 -5880
  93. package/src/gracenote.ts +0 -146
  94. package/src/gracenotegroup.ts +0 -194
  95. package/src/gracetabnote.ts +0 -30
  96. package/src/index.ts +0 -90
  97. package/src/keymanager.ts +0 -153
  98. package/src/keysignature.ts +0 -272
  99. package/src/keysignote.ts +0 -46
  100. package/src/metrics.ts +0 -222
  101. package/src/modifier.ts +0 -216
  102. package/src/modifiercontext.ts +0 -177
  103. package/src/multimeasurerest.ts +0 -229
  104. package/src/music.ts +0 -443
  105. package/src/note.ts +0 -690
  106. package/src/notehead.ts +0 -158
  107. package/src/notesubgroup.ts +0 -88
  108. package/src/ornament.ts +0 -301
  109. package/src/parenthesis.ts +0 -89
  110. package/src/parser.ts +0 -263
  111. package/src/pedalmarking.ts +0 -273
  112. package/src/registry.ts +0 -146
  113. package/src/rendercontext.ts +0 -98
  114. package/src/renderer.ts +0 -166
  115. package/src/repeatnote.ts +0 -25
  116. package/src/stave.ts +0 -885
  117. package/src/stavebarline.ts +0 -231
  118. package/src/staveconnector.ts +0 -281
  119. package/src/stavehairpin.ts +0 -209
  120. package/src/staveline.ts +0 -343
  121. package/src/stavemodifier.ts +0 -93
  122. package/src/stavenote.ts +0 -1230
  123. package/src/staverepetition.ts +0 -148
  124. package/src/stavesection.ts +0 -54
  125. package/src/stavetempo.ts +0 -162
  126. package/src/stavetext.ts +0 -69
  127. package/src/stavetie.ts +0 -235
  128. package/src/stavevolta.ts +0 -66
  129. package/src/stem.ts +0 -225
  130. package/src/stemmablenote.ts +0 -248
  131. package/src/stringnumber.ts +0 -286
  132. package/src/strokes.ts +0 -214
  133. package/src/svgcontext.ts +0 -639
  134. package/src/system.ts +0 -327
  135. package/src/tables.ts +0 -796
  136. package/src/tabnote.ts +0 -449
  137. package/src/tabslide.ts +0 -115
  138. package/src/tabstave.ts +0 -31
  139. package/src/tabtie.ts +0 -44
  140. package/src/textbracket.ts +0 -231
  141. package/src/textdynamics.ts +0 -101
  142. package/src/textnote.ts +0 -175
  143. package/src/tickable.ts +0 -374
  144. package/src/tickcontext.ts +0 -280
  145. package/src/timesignature.ts +0 -229
  146. package/src/timesignote.ts +0 -45
  147. package/src/tremolo.ts +0 -51
  148. package/src/tuning.ts +0 -90
  149. package/src/tuplet.ts +0 -349
  150. package/src/typeguard.ts +0 -136
  151. package/src/util.ts +0 -169
  152. package/src/version.ts +0 -4
  153. package/src/vexflow.ts +0 -378
  154. package/src/vibrato.ts +0 -99
  155. package/src/vibratobracket.ts +0 -81
  156. package/src/voice.ts +0 -302
  157. 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
- }
@@ -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
- }