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.
Files changed (112) 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 +4 -4
  6. package/build/cjs/vexflow-debug-with-tests.js.map +1 -1
  7. package/build/cjs/vexflow-debug.js +4 -4
  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/version.js +3 -3
  12. package/package.json +1 -3
  13. package/entry/vexflow-bravura.ts +0 -25
  14. package/entry/vexflow-core.ts +0 -16
  15. package/entry/vexflow-debug-with-tests.ts +0 -64
  16. package/entry/vexflow-debug.ts +0 -54
  17. package/entry/vexflow.ts +0 -47
  18. package/src/accidental.ts +0 -570
  19. package/src/annotation.ts +0 -294
  20. package/src/articulation.ts +0 -383
  21. package/src/barnote.ts +0 -95
  22. package/src/beam.ts +0 -999
  23. package/src/bend.ts +0 -268
  24. package/src/boundingbox.ts +0 -108
  25. package/src/canvascontext.ts +0 -348
  26. package/src/chordsymbol.ts +0 -473
  27. package/src/clef.ts +0 -164
  28. package/src/clefnote.ts +0 -50
  29. package/src/crescendo.ts +0 -132
  30. package/src/curve.ts +0 -195
  31. package/src/dot.ts +0 -171
  32. package/src/easyscore.ts +0 -538
  33. package/src/element.ts +0 -692
  34. package/src/factory.ts +0 -694
  35. package/src/font.ts +0 -343
  36. package/src/fonts/academico.ts +0 -2
  37. package/src/fonts/academicobold.ts +0 -2
  38. package/src/fonts/bravura.ts +0 -2
  39. package/src/fonts/gonville.ts +0 -2
  40. package/src/fonts/petaluma.ts +0 -2
  41. package/src/fonts/petalumascript.ts +0 -2
  42. package/src/formatter.ts +0 -1107
  43. package/src/fraction.ts +0 -245
  44. package/src/frethandfinger.ts +0 -180
  45. package/src/ghostnote.ts +0 -75
  46. package/src/glyphnote.ts +0 -68
  47. package/src/glyphs.ts +0 -5880
  48. package/src/gracenote.ts +0 -146
  49. package/src/gracenotegroup.ts +0 -194
  50. package/src/gracetabnote.ts +0 -30
  51. package/src/index.ts +0 -90
  52. package/src/keymanager.ts +0 -153
  53. package/src/keysignature.ts +0 -272
  54. package/src/keysignote.ts +0 -46
  55. package/src/metrics.ts +0 -222
  56. package/src/modifier.ts +0 -216
  57. package/src/modifiercontext.ts +0 -177
  58. package/src/multimeasurerest.ts +0 -229
  59. package/src/music.ts +0 -443
  60. package/src/note.ts +0 -690
  61. package/src/notehead.ts +0 -158
  62. package/src/notesubgroup.ts +0 -88
  63. package/src/ornament.ts +0 -301
  64. package/src/parenthesis.ts +0 -89
  65. package/src/parser.ts +0 -263
  66. package/src/pedalmarking.ts +0 -273
  67. package/src/registry.ts +0 -146
  68. package/src/rendercontext.ts +0 -98
  69. package/src/renderer.ts +0 -166
  70. package/src/repeatnote.ts +0 -25
  71. package/src/stave.ts +0 -885
  72. package/src/stavebarline.ts +0 -231
  73. package/src/staveconnector.ts +0 -281
  74. package/src/stavehairpin.ts +0 -209
  75. package/src/staveline.ts +0 -343
  76. package/src/stavemodifier.ts +0 -93
  77. package/src/stavenote.ts +0 -1230
  78. package/src/staverepetition.ts +0 -148
  79. package/src/stavesection.ts +0 -54
  80. package/src/stavetempo.ts +0 -162
  81. package/src/stavetext.ts +0 -69
  82. package/src/stavetie.ts +0 -235
  83. package/src/stavevolta.ts +0 -66
  84. package/src/stem.ts +0 -225
  85. package/src/stemmablenote.ts +0 -248
  86. package/src/stringnumber.ts +0 -286
  87. package/src/strokes.ts +0 -214
  88. package/src/svgcontext.ts +0 -639
  89. package/src/system.ts +0 -327
  90. package/src/tables.ts +0 -796
  91. package/src/tabnote.ts +0 -449
  92. package/src/tabslide.ts +0 -115
  93. package/src/tabstave.ts +0 -31
  94. package/src/tabtie.ts +0 -44
  95. package/src/textbracket.ts +0 -231
  96. package/src/textdynamics.ts +0 -101
  97. package/src/textnote.ts +0 -175
  98. package/src/tickable.ts +0 -374
  99. package/src/tickcontext.ts +0 -280
  100. package/src/timesignature.ts +0 -229
  101. package/src/timesignote.ts +0 -45
  102. package/src/tremolo.ts +0 -51
  103. package/src/tuning.ts +0 -90
  104. package/src/tuplet.ts +0 -349
  105. package/src/typeguard.ts +0 -136
  106. package/src/util.ts +0 -169
  107. package/src/version.ts +0 -4
  108. package/src/vexflow.ts +0 -378
  109. package/src/vibrato.ts +0 -99
  110. package/src/vibratobracket.ts +0 -81
  111. package/src/voice.ts +0 -302
  112. package/src/web.ts +0 -35
package/src/stave.ts DELETED
@@ -1,885 +0,0 @@
1
- // Copyright (c) 2023-present VexFlow contributors: https://github.com/vexflow/vexflow/graphs/contributors
2
- // MIT License
3
-
4
- import { BoundingBox, Bounds } from './boundingbox';
5
- import { Clef } from './clef';
6
- import { Element, ElementStyle } from './element';
7
- import { KeySignature } from './keysignature';
8
- import { Metrics } from './metrics';
9
- import { Barline, BarlineType } from './stavebarline';
10
- import { StaveModifier, StaveModifierPosition } from './stavemodifier';
11
- import { Repetition } from './staverepetition';
12
- import { StaveSection } from './stavesection';
13
- import { StaveTempo, StaveTempoOptions } from './stavetempo';
14
- import { StaveText } from './stavetext';
15
- import { Volta } from './stavevolta';
16
- import { Tables } from './tables';
17
- import { TimeSignature } from './timesignature';
18
- import { Category, isBarline } from './typeguard';
19
- import { RuntimeError } from './util';
20
-
21
- export interface StaveLineConfig {
22
- visible?: boolean;
23
- }
24
-
25
- export interface StaveOptions {
26
- bottomTextPosition?: number;
27
- lineConfig?: StaveLineConfig[];
28
- spaceBelowStaffLn?: number;
29
- spaceAboveStaffLn?: number;
30
- verticalBarWidth?: number;
31
- fillStyle?: string;
32
- leftBar?: boolean;
33
- rightBar?: boolean;
34
- spacingBetweenLinesPx?: number;
35
- topTextPosition?: number;
36
- numLines?: number;
37
- }
38
-
39
- // Used by Stave.format() to sort the modifiers at the beginning and end of a stave.
40
- // The keys (computed property names) match the CATEGORY property in the
41
- // Barline, Clef, KeySignature, TimeSignature classes.
42
- const SORT_ORDER_BEG_MODIFIERS = {
43
- [Barline.CATEGORY]: 0,
44
- [Clef.CATEGORY]: 1,
45
- [KeySignature.CATEGORY]: 2,
46
- [TimeSignature.CATEGORY]: 3,
47
- };
48
-
49
- const SORT_ORDER_END_MODIFIERS = {
50
- [TimeSignature.CATEGORY]: 0,
51
- [KeySignature.CATEGORY]: 1,
52
- [Barline.CATEGORY]: 2,
53
- [Clef.CATEGORY]: 3,
54
- };
55
-
56
- export class Stave extends Element {
57
- static get CATEGORY(): string {
58
- return Category.Stave;
59
- }
60
-
61
- readonly options: Required<StaveOptions>;
62
-
63
- protected startX: number;
64
- protected endX: number;
65
- protected clef: string;
66
- protected endClef?: string;
67
-
68
- protected formatted: boolean;
69
- protected measure: number;
70
- protected bounds: Bounds;
71
- protected readonly modifiers: StaveModifier[];
72
-
73
- protected defaultLedgerLineStyle: ElementStyle;
74
-
75
- // This is the sum of the padding that normally goes on left + right of a stave during
76
- // drawing. Used to size staves correctly with content width.
77
- static get defaultPadding(): number {
78
- return Metrics.get('Stave.padding') + Metrics.get('Stave.endPaddingMax');
79
- }
80
-
81
- // Right padding, used by system if startX is already determined.
82
- static get rightPadding(): number {
83
- return Metrics.get('Stave.endPaddingMax');
84
- }
85
-
86
- constructor(x: number, y: number, width: number, options?: StaveOptions) {
87
- super();
88
-
89
- this.x = x;
90
- this.y = y;
91
- this.width = width;
92
- this.formatted = false;
93
- this.startX = x + 5;
94
- this.endX = x + width;
95
- this.modifiers = []; // stave modifiers (clef, key, time, barlines, coda, segno, etc.)
96
- this.measure = 0;
97
- this.clef = 'treble';
98
- this.endClef = undefined;
99
-
100
- this.options = {
101
- verticalBarWidth: 10, // Width around vertical bar end-marker
102
- numLines: 5,
103
- fillStyle: '#999999',
104
- leftBar: true, // draw vertical bar on left
105
- rightBar: true, // draw vertical bar on right
106
- spacingBetweenLinesPx: Tables.STAVE_LINE_DISTANCE, // in pixels
107
- spaceAboveStaffLn: 4, // in staff lines
108
- spaceBelowStaffLn: 4, // in staff lines
109
- topTextPosition: 1, // in staff lines
110
- bottomTextPosition: 4, // in staff lines
111
- lineConfig: [],
112
- ...options,
113
- };
114
- this.bounds = { x: this.x, y: this.y, w: this.width, h: 0 };
115
- this.defaultLedgerLineStyle = { strokeStyle: '#444', lineWidth: 1.4 };
116
-
117
- this.resetLines();
118
-
119
- // beg bar
120
- this.addModifier(new Barline(this.options.leftBar ? BarlineType.SINGLE : BarlineType.NONE));
121
- // end bar
122
- this.addEndModifier(new Barline(this.options.rightBar ? BarlineType.SINGLE : BarlineType.NONE));
123
- }
124
-
125
- /** Set default style for ledger lines. */
126
- setDefaultLedgerLineStyle(style: ElementStyle): void {
127
- this.defaultLedgerLineStyle = style;
128
- }
129
-
130
- /** Get default style for ledger lines. */
131
- getDefaultLedgerLineStyle(): ElementStyle {
132
- return { ...this.getStyle(), ...this.defaultLedgerLineStyle };
133
- }
134
-
135
- space(spacing: number): number {
136
- return this.options.spacingBetweenLinesPx * spacing;
137
- }
138
-
139
- resetLines(): void {
140
- this.options.lineConfig = [];
141
- for (let i = 0; i < this.options.numLines; i++) {
142
- this.options.lineConfig.push({ visible: true });
143
- }
144
- this.height = (this.options.numLines + this.options.spaceAboveStaffLn) * this.options.spacingBetweenLinesPx;
145
- this.options.bottomTextPosition = this.options.numLines;
146
- }
147
-
148
- setNoteStartX(x: number): this {
149
- if (!this.formatted) this.format();
150
-
151
- this.startX = x;
152
- return this;
153
- }
154
-
155
- getNoteStartX(): number {
156
- if (!this.formatted) this.format();
157
-
158
- return this.startX;
159
- }
160
-
161
- getNoteEndX(): number {
162
- if (!this.formatted) this.format();
163
-
164
- return this.endX;
165
- }
166
-
167
- getTieStartX(): number {
168
- return this.startX;
169
- }
170
-
171
- getTieEndX(): number {
172
- return this.endX;
173
- }
174
-
175
- getNumLines(): number {
176
- return this.options.numLines;
177
- }
178
-
179
- setNumLines(n: number): this {
180
- this.options.numLines = n;
181
- this.resetLines();
182
- return this;
183
- }
184
-
185
- getTopLineTopY(): number {
186
- return this.getYForLine(0) - Tables.STAVE_LINE_THICKNESS / 2;
187
- }
188
-
189
- getBottomLineBottomY(): number {
190
- return this.getYForLine(this.getNumLines() - 1) + Tables.STAVE_LINE_THICKNESS / 2;
191
- }
192
-
193
- setX(x: number): this {
194
- const shift = x - this.x;
195
- this.formatted = false;
196
- this.x = x;
197
- this.startX += shift;
198
- this.endX += shift;
199
- for (let i = 0; i < this.modifiers.length; i++) {
200
- const mod = this.modifiers[i];
201
- mod.setX(mod.getX() + shift);
202
- }
203
- return this;
204
- }
205
-
206
- setWidth(width: number): this {
207
- this.formatted = false;
208
- this.width = width;
209
- this.endX = this.x + width;
210
-
211
- // reset the x position of the end barline (TODO(0xfe): This makes no sense)
212
- // this.modifiers[1].setX(this.endX);
213
- return this;
214
- }
215
-
216
- getStyle(): ElementStyle {
217
- return {
218
- fillStyle: this.options.fillStyle,
219
- strokeStyle: this.options.fillStyle, // yes, this is correct for legacy compatibility
220
- lineWidth: Tables.STAVE_LINE_THICKNESS,
221
- ...super.getStyle(),
222
- };
223
- }
224
-
225
- /**
226
- * Set the measure number of this Stave.
227
- */
228
- setMeasure(measure: number): this {
229
- this.measure = measure;
230
- return this;
231
- }
232
-
233
- /**
234
- * Return the measure number of this Stave.
235
- */
236
- getMeasure(): number {
237
- return this.measure;
238
- }
239
-
240
- /**
241
- * Gets the pixels to shift from the beginning of the stave
242
- * following the modifier at the provided index
243
- * @param {Number} index The index from which to determine the shift
244
- * @return {Number} The amount of pixels shifted
245
- */
246
- getModifierXShift(index: number = 0): number {
247
- if (typeof index !== 'number') {
248
- throw new RuntimeError('InvalidIndex', 'Must be of number type');
249
- }
250
-
251
- if (!this.formatted) this.format();
252
-
253
- if (this.getModifiers(StaveModifierPosition.BEGIN).length === 1) {
254
- return 0;
255
- }
256
-
257
- // for right position modifiers zero shift seems correct, see 'Volta + Modifier Measure Test'
258
- if (this.modifiers[index].getPosition() === StaveModifierPosition.RIGHT) {
259
- return 0;
260
- }
261
-
262
- let startX = this.startX - this.x;
263
- const begBarline = this.modifiers[0] as Barline;
264
- if (begBarline.getType() === BarlineType.REPEAT_BEGIN && startX > begBarline.getWidth()) {
265
- startX -= begBarline.getWidth();
266
- }
267
-
268
- return startX;
269
- }
270
-
271
- /** Coda & Segno Symbol functions */
272
- setRepetitionType(type: number, yShift: number = 0): this {
273
- this.modifiers.push(new Repetition(type, this.x, yShift));
274
- return this;
275
- }
276
-
277
- // Volta functions
278
- setVoltaType(type: number, label: string, y: number): this {
279
- this.modifiers.push(new Volta(type, label, this.x, y));
280
- return this;
281
- }
282
-
283
- // Section functions
284
- setSection(section: string, y: number, xOffset = 0, fontSize?: number, drawRect = true) {
285
- const staveSection = new StaveSection(section, this.x + xOffset, y, drawRect);
286
- if (fontSize) {
287
- staveSection.setFontSize(fontSize);
288
- }
289
- this.modifiers.push(staveSection);
290
- return this;
291
- }
292
-
293
- // Tempo functions
294
- setTempo(tempo: StaveTempoOptions, y: number): this {
295
- this.modifiers.push(new StaveTempo(tempo, this.x, y));
296
- return this;
297
- }
298
-
299
- // Text functions
300
- setStaveText(
301
- text: string,
302
- position: number,
303
- options: {
304
- shiftX?: number;
305
- shiftY?: number;
306
- justification?: number;
307
- } = {}
308
- ): this {
309
- this.modifiers.push(new StaveText(text, position, options));
310
- return this;
311
- }
312
-
313
- getSpacingBetweenLines(): number {
314
- return this.options.spacingBetweenLinesPx;
315
- }
316
-
317
- getBoundingBox(): BoundingBox {
318
- return new BoundingBox(this.x, this.y, this.width, this.getBottomY() - this.y);
319
- }
320
-
321
- getBottomY(): number {
322
- const options = this.options;
323
- const spacing = options.spacingBetweenLinesPx;
324
- const scoreBottom = this.getYForLine(options.numLines) + options.spaceBelowStaffLn * spacing;
325
- return scoreBottom;
326
- }
327
-
328
- getBottomLineY(): number {
329
- return this.getYForLine(this.options.numLines);
330
- }
331
-
332
- // This returns
333
- /** @returns the y for the *center* of a staff line */
334
- getYForLine(line: number): number {
335
- const options = this.options;
336
- const spacing = options.spacingBetweenLinesPx;
337
- const headroom = options.spaceAboveStaffLn;
338
-
339
- const y = this.y + line * spacing + headroom * spacing;
340
-
341
- return y;
342
- }
343
-
344
- getLineForY(y: number): number {
345
- // Does the reverse of getYForLine - somewhat dumb and just calls
346
- // getYForLine until the right value is reaches
347
-
348
- const options = this.options;
349
- const spacing = options.spacingBetweenLinesPx;
350
- const headroom = options.spaceAboveStaffLn;
351
- return (y - this.y) / spacing - headroom;
352
- }
353
-
354
- getYForTopText(line: number = 0): number {
355
- return this.getYForLine(-line - this.options.topTextPosition);
356
- }
357
-
358
- getYForBottomText(line: number = 0): number {
359
- return this.getYForLine(this.options.bottomTextPosition + line);
360
- }
361
-
362
- getYForNote(line: number): number {
363
- const options = this.options;
364
- const spacing = options.spacingBetweenLinesPx;
365
- const headroom = options.spaceAboveStaffLn;
366
- return this.y + headroom * spacing + 5 * spacing - line * spacing;
367
- }
368
-
369
- getYForGlyphs(): number {
370
- return this.getYForLine(3);
371
- }
372
-
373
- // This method adds a stave modifier to the stave. Note that the first two
374
- // modifiers (BarLines) are automatically added upon construction.
375
- addModifier(modifier: StaveModifier, position?: number): this {
376
- if (position !== undefined) {
377
- modifier.setPosition(position);
378
- }
379
-
380
- modifier.setStave(this);
381
- this.formatted = false;
382
- this.modifiers.push(modifier);
383
- return this;
384
- }
385
-
386
- addEndModifier(modifier: StaveModifier): this {
387
- this.addModifier(modifier, StaveModifierPosition.END);
388
- return this;
389
- }
390
-
391
- // Bar Line functions
392
- setBegBarType(type: number | BarlineType): this {
393
- // Only valid bar types at beginning of stave is none, single or begin repeat
394
- const { SINGLE, REPEAT_BEGIN, NONE } = BarlineType;
395
- if (type === SINGLE || type === REPEAT_BEGIN || type === NONE) {
396
- (this.modifiers[0] as Barline).setType(type);
397
- this.formatted = false;
398
- }
399
- return this;
400
- }
401
-
402
- setEndBarType(type: number | BarlineType): this {
403
- // Repeat end not valid at end of stave
404
- if (type !== BarlineType.REPEAT_BEGIN) {
405
- (this.modifiers[1] as Barline).setType(type);
406
- this.formatted = false;
407
- }
408
- return this;
409
- }
410
-
411
- setClef(clefSpec: string, size?: string, annotation?: string, position?: number): this {
412
- if (position === undefined) {
413
- position = StaveModifierPosition.BEGIN;
414
- }
415
-
416
- if (position === StaveModifierPosition.END) {
417
- this.endClef = clefSpec;
418
- } else {
419
- this.clef = clefSpec;
420
- }
421
-
422
- const clefs = this.getModifiers(position, Clef.CATEGORY) as Clef[];
423
- if (clefs.length === 0) {
424
- this.addClef(clefSpec, size, annotation, position);
425
- } else {
426
- clefs[0].setType(clefSpec, size, annotation);
427
- }
428
-
429
- return this;
430
- }
431
-
432
- getClef(): string {
433
- return this.clef;
434
- }
435
-
436
- setEndClef(clefSpec: string, size?: string, annotation?: string): this {
437
- this.setClef(clefSpec, size, annotation, StaveModifierPosition.END);
438
- return this;
439
- }
440
-
441
- getEndClef(): string | undefined {
442
- return this.endClef;
443
- }
444
-
445
- setKeySignature(keySpec: string, cancelKeySpec?: string, position?: number): this {
446
- if (position === undefined) {
447
- position = StaveModifierPosition.BEGIN;
448
- }
449
-
450
- const keySignatures = this.getModifiers(position, KeySignature.CATEGORY) as KeySignature[];
451
- if (keySignatures.length === 0) {
452
- this.addKeySignature(keySpec, cancelKeySpec, position);
453
- } else {
454
- keySignatures[0].setKeySig(keySpec, cancelKeySpec);
455
- }
456
-
457
- return this;
458
- }
459
-
460
- setEndKeySignature(keySpec: string, cancelKeySpec?: string): this {
461
- this.setKeySignature(keySpec, cancelKeySpec, StaveModifierPosition.END);
462
- return this;
463
- }
464
-
465
- setTimeSignature(timeSpec: string, customPadding?: number, position?: number): this {
466
- if (position === undefined) {
467
- position = StaveModifierPosition.BEGIN;
468
- }
469
-
470
- const timeSignatures = this.getModifiers(position, TimeSignature.CATEGORY) as TimeSignature[];
471
- if (timeSignatures.length === 0) {
472
- this.addTimeSignature(timeSpec, customPadding, position);
473
- } else {
474
- timeSignatures[0].setTimeSig(timeSpec);
475
- }
476
-
477
- return this;
478
- }
479
-
480
- setEndTimeSignature(timeSpec: string, customPadding?: number): this {
481
- this.setTimeSignature(timeSpec, customPadding, StaveModifierPosition.END);
482
- return this;
483
- }
484
-
485
- /**
486
- * Add a key signature to the stave.
487
- *
488
- * Example:
489
- * `stave.addKeySignature('Db');`
490
- * @param keySpec new key specification `[A-G][b|#]?`
491
- * @param cancelKeySpec
492
- * @param position
493
- * @returns
494
- */
495
- addKeySignature(keySpec: string, cancelKeySpec?: string, position?: number): this {
496
- if (position === undefined) {
497
- position = StaveModifierPosition.BEGIN;
498
- }
499
- this.addModifier(new KeySignature(keySpec, cancelKeySpec).setPosition(position), position);
500
- return this;
501
- }
502
-
503
- /**
504
- * Add a clef to the stave.
505
- *
506
- * Example:
507
- *
508
- * stave.addClef('treble')
509
- * @param clef clef (treble|bass|...) see {@link Clef.types}
510
- * @param size
511
- * @param annotation
512
- * @param position
513
- * @returns
514
- */
515
- addClef(clef: string, size?: string, annotation?: string, position?: number): this {
516
- if (position === undefined || position === StaveModifierPosition.BEGIN) {
517
- this.clef = clef;
518
- } else if (position === StaveModifierPosition.END) {
519
- this.endClef = clef;
520
- }
521
-
522
- this.addModifier(new Clef(clef, size, annotation), position);
523
- return this;
524
- }
525
-
526
- addEndClef(clef: string, size?: string, annotation?: string): this {
527
- this.addClef(clef, size, annotation, StaveModifierPosition.END);
528
- return this;
529
- }
530
-
531
- /**
532
- * Add a time signature to the stave
533
- *
534
- * Example:
535
- *
536
- * `stave.addTimeSignature('4/4');`
537
- * @param timeSpec time signature specification `(C\||C|\d\/\d)`
538
- * @param customPadding
539
- * @param position
540
- * @returns
541
- */
542
- addTimeSignature(timeSpec: string, customPadding?: number, position?: number): this {
543
- this.addModifier(new TimeSignature(timeSpec, customPadding), position);
544
- return this;
545
- }
546
-
547
- addEndTimeSignature(timeSpec: string, customPadding?: number): this {
548
- this.addTimeSignature(timeSpec, customPadding, StaveModifierPosition.END);
549
- return this;
550
- }
551
-
552
- // Deprecated
553
- addTrebleGlyph(): this {
554
- this.addClef('treble');
555
- return this;
556
- }
557
-
558
- /**
559
- * @param position
560
- * @param category
561
- * @returns array of StaveModifiers that match the provided position and category.
562
- */
563
- getModifiers(position?: number, category?: string): StaveModifier[] {
564
- const noPosition = position === undefined;
565
- const noCategory = category === undefined;
566
- if (noPosition && noCategory) {
567
- return this.modifiers;
568
- } else if (noPosition) {
569
- // A category was provided.
570
- return this.modifiers.filter((m: StaveModifier) => category === m.getCategory());
571
- } else if (noCategory) {
572
- // A position was provided.
573
- return this.modifiers.filter((m: StaveModifier) => position === m.getPosition());
574
- } else {
575
- // Both position and category were provided!
576
- return this.modifiers.filter((m: StaveModifier) => position === m.getPosition() && category === m.getCategory());
577
- }
578
- }
579
-
580
- /**
581
- * Use the modifier's `getCategory()` as a key for the `order` array.
582
- * The retrieved value is used to sort modifiers from left to right (0 to to 3).
583
- */
584
- sortByCategory(items: StaveModifier[], order: Record<string, number>): void {
585
- for (let i = items.length - 1; i >= 0; i--) {
586
- for (let j = 0; j < i; j++) {
587
- if (order[items[j].getCategory()] > order[items[j + 1].getCategory()]) {
588
- const temp = items[j];
589
- items[j] = items[j + 1];
590
- items[j + 1] = temp;
591
- }
592
- }
593
- }
594
- }
595
-
596
- format(): void {
597
- const begBarline = this.modifiers[0] as Barline;
598
- const endBarline = this.modifiers[1];
599
-
600
- const begModifiers = this.getModifiers(StaveModifierPosition.BEGIN);
601
- const endModifiers = this.getModifiers(StaveModifierPosition.END);
602
-
603
- this.sortByCategory(begModifiers, SORT_ORDER_BEG_MODIFIERS);
604
- this.sortByCategory(endModifiers, SORT_ORDER_END_MODIFIERS);
605
-
606
- if (begModifiers.length > 1 && begBarline.getType() === BarlineType.REPEAT_BEGIN) {
607
- begModifiers.push(begModifiers.splice(0, 1)[0]);
608
- begModifiers.splice(0, 0, new Barline(BarlineType.SINGLE));
609
- }
610
-
611
- if (endModifiers.indexOf(endBarline) > 0) {
612
- endModifiers.splice(0, 0, new Barline(BarlineType.NONE));
613
- }
614
-
615
- let width;
616
- let padding;
617
- let modifier;
618
- let offset = 0;
619
- let x = this.x;
620
- for (let i = 0; i < begModifiers.length; i++) {
621
- modifier = begModifiers[i];
622
- padding = modifier.getPadding(i + offset);
623
- width = modifier.getWidth();
624
-
625
- x += padding;
626
- modifier.setX(x);
627
- x += width;
628
-
629
- if (padding + width === 0) offset--;
630
- }
631
-
632
- this.startX = x;
633
- x = this.x + this.width;
634
-
635
- const widths = {
636
- left: 0,
637
- right: 0,
638
- paddingRight: 0,
639
- paddingLeft: 0,
640
- };
641
-
642
- let lastBarlineIdx = 0;
643
-
644
- for (let i = 0; i < endModifiers.length; i++) {
645
- modifier = endModifiers[i];
646
- lastBarlineIdx = isBarline(modifier) ? i : lastBarlineIdx;
647
-
648
- widths.right = 0;
649
- widths.left = 0;
650
- widths.paddingRight = 0;
651
- widths.paddingLeft = 0;
652
- const layoutMetrics = modifier.getLayoutMetrics();
653
-
654
- if (layoutMetrics) {
655
- if (i !== 0) {
656
- widths.right = layoutMetrics.xMax ?? 0;
657
- widths.paddingRight = layoutMetrics.paddingRight ?? 0;
658
- }
659
- widths.left = -layoutMetrics.xMin ?? 0;
660
- widths.paddingLeft = layoutMetrics.paddingLeft ?? 0;
661
-
662
- if (i === endModifiers.length - 1) {
663
- widths.paddingLeft = 0;
664
- }
665
- } else {
666
- widths.paddingRight = modifier.getPadding(i - lastBarlineIdx);
667
- if (i !== 0) {
668
- widths.right = modifier.getWidth();
669
- }
670
- if (i === 0) {
671
- widths.left = modifier.getWidth();
672
- }
673
- }
674
- x -= widths.paddingRight;
675
- x -= widths.right;
676
-
677
- modifier.setX(x);
678
-
679
- x -= widths.left;
680
- x -= widths.paddingLeft;
681
- }
682
-
683
- this.endX = endModifiers.length === 1 ? this.x + this.width : x;
684
- this.formatted = true;
685
- }
686
-
687
- /**
688
- * All drawing functions below need the context to be set.
689
- */
690
- draw(): this {
691
- const ctx = this.checkContext();
692
- this.setRendered();
693
-
694
- this.applyStyle();
695
- ctx.openGroup('stave', this.getAttribute('id'));
696
- if (!this.formatted) this.format();
697
-
698
- const numLines = this.options.numLines;
699
- const width = this.width;
700
- const x = this.x;
701
- let y;
702
-
703
- // Render lines
704
- for (let line = 0; line < numLines; line++) {
705
- y = this.getYForLine(line);
706
-
707
- if (this.options.lineConfig[line].visible) {
708
- ctx.beginPath();
709
- ctx.moveTo(x, y);
710
- ctx.lineTo(x + width, y);
711
- ctx.stroke();
712
- }
713
- }
714
-
715
- ctx.closeGroup();
716
- this.restoreStyle();
717
-
718
- // Draw the modifiers (bar lines, coda, segno, repeat brackets, etc.)
719
- for (let i = 0; i < this.modifiers.length; i++) {
720
- const modifier = this.modifiers[i];
721
- // Only draw modifier if it has a draw function
722
- if (typeof modifier.draw === 'function') {
723
- modifier.applyStyle(ctx);
724
- modifier.draw(this, this.getModifierXShift(i));
725
- modifier.restoreStyle(ctx);
726
- }
727
- }
728
-
729
- // Render measure numbers
730
- if (this.measure > 0) {
731
- ctx.save();
732
- ctx.setFont(this.fontInfo);
733
- const textWidth = ctx.measureText('' + this.measure).width;
734
- y = this.getYForTopText(0) + 3;
735
- ctx.fillText('' + this.measure, this.x - textWidth / 2, y);
736
- ctx.restore();
737
- }
738
- return this;
739
- }
740
-
741
- getVerticalBarWidth(): number {
742
- return this.options.verticalBarWidth;
743
- }
744
-
745
- /**
746
- * Get the current configuration for the Stave.
747
- * @return {Array} An array of configuration objects.
748
- */
749
- getConfigForLines(): StaveLineConfig[] {
750
- return this.options.lineConfig;
751
- }
752
-
753
- /**
754
- * Configure properties of the lines in the Stave
755
- * @param lineNumber The index of the line to configure.
756
- * @param lineConfig An configuration object for the specified line.
757
- * @throws RuntimeError "StaveConfigError" When the specified line number is out of
758
- * range of the number of lines specified in the constructor.
759
- */
760
- setConfigForLine(lineNumber: number, lineConfig: StaveLineConfig): this {
761
- if (lineNumber >= this.options.numLines || lineNumber < 0) {
762
- throw new RuntimeError(
763
- 'StaveConfigError',
764
- 'The line number must be within the range of the number of lines in the Stave.'
765
- );
766
- }
767
-
768
- if (lineConfig.visible === undefined) {
769
- throw new RuntimeError('StaveConfigError', "The line configuration object is missing the 'visible' property.");
770
- }
771
-
772
- if (typeof lineConfig.visible !== 'boolean') {
773
- throw new RuntimeError(
774
- 'StaveConfigError',
775
- "The line configuration objects 'visible' property must be true or false."
776
- );
777
- }
778
-
779
- this.options.lineConfig[lineNumber] = lineConfig;
780
-
781
- return this;
782
- }
783
-
784
- /**
785
- * Set the staff line configuration array for all of the lines at once.
786
- * @param linesConfiguration An array of line configuration objects. These objects
787
- * are of the same format as the single one passed in to setLineConfiguration().
788
- * The caller can set null for any line config entry if it is desired that the default be used
789
- * @throws RuntimeError "StaveConfigError" When the lines_configuration array does not have
790
- * exactly the same number of elements as the numLines configuration object set in
791
- * the constructor.
792
- */
793
- setConfigForLines(linesConfiguration: StaveLineConfig[]): this {
794
- if (linesConfiguration.length !== this.options.numLines) {
795
- throw new RuntimeError(
796
- 'StaveConfigError',
797
- 'The length of the lines configuration array must match the number of lines in the Stave'
798
- );
799
- }
800
-
801
- // Make sure the defaults are present in case an incomplete set of
802
- // configuration options were supplied.
803
- // eslint-disable-next-line
804
- for (const lineConfig in linesConfiguration) {
805
- // Allow '{}' to be used if the caller just wants the default for a particular node.
806
- if (linesConfiguration[lineConfig].visible === undefined) {
807
- linesConfiguration[lineConfig] = this.options.lineConfig[lineConfig];
808
- }
809
- this.options.lineConfig[lineConfig] = {
810
- ...this.options.lineConfig[lineConfig],
811
- ...linesConfiguration[lineConfig],
812
- };
813
- }
814
-
815
- this.options.lineConfig = linesConfiguration;
816
-
817
- return this;
818
- }
819
-
820
- static formatBegModifiers(staves: Stave[]): void {
821
- const adjustCategoryStartX = (category: Category) => {
822
- let minStartX = 0;
823
- // Calculate min start X for the category
824
- staves.forEach((stave) => {
825
- const modifiers = stave.getModifiers(StaveModifierPosition.BEGIN, category);
826
- // Consider only the first instance
827
- if (modifiers.length > 0 && modifiers[0].getX() > minStartX) minStartX = modifiers[0].getX();
828
- });
829
- let adjustX = 0;
830
- staves.forEach((stave) => {
831
- adjustX = 0;
832
- const modifiers = stave.getModifiers(StaveModifierPosition.BEGIN, category);
833
- // Calculate adjustement required for the stave
834
- modifiers.forEach((modifier) => {
835
- if (minStartX - modifier.getX() > adjustX) adjustX = minStartX - modifier.getX();
836
- });
837
- const allModifiers = stave.getModifiers(StaveModifierPosition.BEGIN);
838
- let bAdjust = false;
839
- // Apply adjustment to all the modifiers in and beyond the category
840
- allModifiers.forEach((modifier) => {
841
- if (modifier.getCategory() === category) bAdjust = true;
842
- if (bAdjust && adjustX > 0) modifier.setX(modifier.getX() + adjustX);
843
- });
844
- // Apply adjustment also to note start.
845
- stave.setNoteStartX(stave.getNoteStartX() + adjustX);
846
- });
847
- };
848
-
849
- // Make sure that staves are formatted
850
- staves.forEach((stave) => {
851
- if (!stave.formatted) stave.format();
852
- });
853
- // Align Clefs
854
- adjustCategoryStartX(Category.Clef);
855
- // Align key signatures
856
- adjustCategoryStartX(Category.KeySignature);
857
- // Align time signatures
858
- adjustCategoryStartX(Category.TimeSignature);
859
-
860
- let maxX = 0;
861
- // align note start
862
- staves.forEach((stave) => {
863
- if (stave.getNoteStartX() > maxX) maxX = stave.getNoteStartX();
864
- });
865
- staves.forEach((stave) => {
866
- stave.setNoteStartX(maxX);
867
- });
868
-
869
- maxX = 0;
870
- // align REPEAT_BEGIN
871
- staves.forEach((stave) => {
872
- const modifiers = stave.getModifiers(StaveModifierPosition.BEGIN, Category.Barline);
873
- modifiers.forEach((modifier) => {
874
- if ((modifier as Barline).getType() === BarlineType.REPEAT_BEGIN)
875
- if (modifier.getX() > maxX) maxX = modifier.getX();
876
- });
877
- });
878
- staves.forEach((stave) => {
879
- const modifiers = stave.getModifiers(StaveModifierPosition.BEGIN, Category.Barline);
880
- modifiers.forEach((modifier) => {
881
- if ((modifier as Barline).getType() === BarlineType.REPEAT_BEGIN) modifier.setX(maxX);
882
- });
883
- });
884
- }
885
- }