vexflow 5.0.0-alpha.1 → 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/stavenote.ts DELETED
@@ -1,1230 +0,0 @@
1
- // Copyright (c) 2023-present VexFlow contributors: https://github.com/vexflow/vexflow/graphs/contributors
2
- //
3
- // This file implements notes for standard notation. This consists of one or
4
- // more `NoteHeads`, an optional stem, and an optional flag.
5
- //
6
- // Throughout these comments, a "note" refers to the entire `StaveNote`,
7
- // and a "key" refers to a specific pitch/notehead within a note.
8
- //
9
- // See `tests/stavenote_tests.ts` for usage examples.
10
-
11
- import { Beam } from './beam';
12
- import { BoundingBox } from './boundingbox';
13
- import { ElementStyle } from './element';
14
- import { Glyphs } from './glyphs';
15
- import { Metrics } from './metrics';
16
- import { Modifier } from './modifier';
17
- import { ModifierContextState } from './modifiercontext';
18
- import { KeyProps, Note, NoteStruct } from './note';
19
- import { NoteHead } from './notehead';
20
- import { Stave } from './stave';
21
- import { Stem, StemOptions } from './stem';
22
- import { StemmableNote } from './stemmablenote';
23
- import { Tables } from './tables';
24
- import { Category } from './typeguard';
25
- import { defined, log, midLine, RuntimeError } from './util';
26
-
27
- export interface StaveNoteHeadBounds {
28
- yTop: number;
29
- yBottom: number;
30
- displacedX?: number;
31
- nonDisplacedX?: number;
32
- highestLine: number;
33
- lowestLine: number;
34
- highestDisplacedLine?: number;
35
- lowestDisplacedLine?: number;
36
- highestNonDisplacedLine: number;
37
- lowestNonDisplacedLine: number;
38
- }
39
-
40
- export interface StaveNoteFormatSettings {
41
- line: number;
42
- maxLine: number;
43
- minLine: number;
44
- isrest: boolean;
45
- stemDirection?: number;
46
- stemMax: number;
47
- stemMin: number;
48
- voiceShift: number;
49
- isDisplaced: boolean;
50
- note: StaveNote;
51
- }
52
-
53
- export interface StaveNoteStruct extends NoteStruct {
54
- /** `Stem.UP` or `Stem.DOWN`. */
55
- stemDirection?: number;
56
- autoStem?: boolean;
57
- strokePx?: number;
58
- octaveShift?: number;
59
- clef?: string;
60
- }
61
-
62
- // To enable logging for this class. Set `VexFlow.StaveNote.DEBUG` to `true`.
63
- // eslint-disable-next-line
64
- function L(...args: any[]) {
65
- if (StaveNote.DEBUG) log('VexFlow.StaveNote', args);
66
- }
67
-
68
- const isInnerNoteIndex = (note: StaveNote, index: number) =>
69
- index === (note.getStemDirection() === Stem.UP ? note.keyProps.length - 1 : 0);
70
-
71
- // Helper methods for rest positioning in ModifierContext.
72
- function shiftRestVertical(rest: StaveNoteFormatSettings, note: StaveNoteFormatSettings, dir: number) {
73
- const delta = dir;
74
-
75
- rest.line += delta;
76
- rest.maxLine += delta;
77
- rest.minLine += delta;
78
- rest.note.setKeyLine(0, rest.note.getKeyLine(0) + delta);
79
- }
80
-
81
- // Called from formatNotes :: center a rest between two notes
82
- function centerRest(rest: StaveNoteFormatSettings, noteU: StaveNoteFormatSettings, noteL: StaveNoteFormatSettings) {
83
- const delta = rest.line - midLine(noteU.minLine, noteL.maxLine);
84
- rest.note.setKeyLine(0, rest.note.getKeyLine(0) - delta);
85
- rest.line -= delta;
86
- rest.maxLine -= delta;
87
- rest.minLine -= delta;
88
- }
89
-
90
- export class StaveNote extends StemmableNote {
91
- static DEBUG: boolean = false;
92
-
93
- static get CATEGORY(): string {
94
- return Category.StaveNote;
95
- }
96
-
97
- static get LEDGER_LINE_OFFSET(): number {
98
- return 3;
99
- }
100
-
101
- static get minNoteheadPadding(): number {
102
- return Metrics.get('NoteHead.minPadding');
103
- }
104
-
105
- /** Format notes inside a ModifierContext. */
106
- static format(notes: StaveNote[], state: ModifierContextState): boolean {
107
- if (!notes || notes.length < 2) return false;
108
-
109
- const notesList: StaveNoteFormatSettings[] = [];
110
-
111
- for (let i = 0; i < notes.length; i++) {
112
- // Formatting uses sortedKeyProps to calculate line and minL.
113
- const props = notes[i].#sortedKeyProps;
114
- const line = props[0].keyProps.line;
115
- let minL = props[props.length - 1].keyProps.line;
116
- const stemDirection = notes[i].getStemDirection();
117
- const stemMax = notes[i].getStemLength() / 10;
118
- const stemMin = notes[i].getStemMinimumLength() / 10;
119
-
120
- let maxL;
121
- if (notes[i].isRest()) {
122
- maxL =
123
- line +
124
- Math.ceil(notes[i].#noteHeads[0].getTextMetrics().actualBoundingBoxAscent / Tables.STAVE_LINE_DISTANCE);
125
- minL =
126
- line -
127
- Math.ceil(notes[i].#noteHeads[0].getTextMetrics().actualBoundingBoxDescent / Tables.STAVE_LINE_DISTANCE);
128
- } else {
129
- maxL =
130
- stemDirection === 1 ? props[props.length - 1].keyProps.line + stemMax : props[props.length - 1].keyProps.line;
131
-
132
- minL = stemDirection === 1 ? props[0].keyProps.line : props[0].keyProps.line - stemMax;
133
- }
134
-
135
- notesList.push({
136
- line: props[0].keyProps.line, // note/rest base line
137
- maxLine: maxL, // note/rest upper bounds line
138
- minLine: minL, // note/rest lower bounds line
139
- isrest: notes[i].isRest(),
140
- stemDirection,
141
- stemMax, // Maximum (default) note stem length;
142
- stemMin, // minimum note stem length
143
- voiceShift: notes[i].getVoiceShiftWidth(),
144
- isDisplaced: notes[i].isDisplaced(), // note manually displaced
145
- note: notes[i],
146
- });
147
- }
148
-
149
- let voices = 0;
150
- let noteU = undefined;
151
- let noteM = undefined;
152
- let noteL = undefined;
153
- const draw = [false, false, false];
154
-
155
- for (let i = 0; i < notesList.length; i++) {
156
- // If .draw is true or undefined, we set draw[i] = true
157
- draw[i] = notesList[i].note.renderOptions.draw !== false;
158
- }
159
-
160
- if (draw[0] && draw[1] && draw[2]) {
161
- // Three visible notes
162
- voices = 3;
163
- noteU = notesList[0];
164
- noteM = notesList[1];
165
- noteL = notesList[2];
166
- } else if (draw[0] && draw[1]) {
167
- // Two visible notes, 0 & 1
168
- voices = 2;
169
- noteU = notesList[0];
170
- noteL = notesList[1];
171
- } else if (draw[0] && draw[2]) {
172
- // Two visible notes, 0 & 2
173
- voices = 2;
174
- noteU = notesList[0];
175
- noteL = notesList[2];
176
- } else if (draw[1] && draw[2]) {
177
- // Two visible notes, 1 & 2
178
- voices = 2;
179
- noteU = notesList[1];
180
- noteL = notesList[2];
181
- } else {
182
- // No shift required for less than 2 visible notes
183
- return true;
184
- }
185
-
186
- // for two voice backward compatibility, ensure upper voice is stems up
187
- // for three voices, the voices must be in order (upper, middle, lower)
188
- if (voices === 2 && noteU.stemDirection === -1 && noteL.stemDirection === 1) {
189
- noteU = notesList[1];
190
- noteL = notesList[0];
191
- }
192
-
193
- const voiceXShift = Math.max(noteU.voiceShift, noteL.voiceShift);
194
- let xShift = 0;
195
-
196
- // Test for two voice note intersection
197
- if (voices === 2) {
198
- const lineSpacing =
199
- noteU.note.hasStem() && noteL.note.hasStem() && noteU.stemDirection === noteL.stemDirection ? 0.0 : 0.5;
200
- if (noteL.isrest && noteU.isrest && noteU.note.duration === noteL.note.duration) {
201
- noteL.note.renderOptions.draw = false;
202
- } else if (noteU.minLine <= noteL.maxLine + lineSpacing) {
203
- if (noteU.isrest) {
204
- // shift rest up
205
- shiftRestVertical(noteU, noteL, 1);
206
- } else if (noteL.isrest) {
207
- // shift rest down
208
- shiftRestVertical(noteL, noteU, -1);
209
- } else {
210
- //Instead of shifting notes, remove the appropriate flag
211
- //If we are sharing a line, switch one notes stem direction.
212
- //If we are sharing a line and in the same voice, only then offset one note
213
- const lineDiff = Math.abs(noteU.line - noteL.line);
214
- if (noteU.note.hasStem() && noteL.note.hasStem()) {
215
- const noteUHead = noteU.note.#sortedKeyProps[0].keyProps.code;
216
- const noteLHead = noteL.note.#sortedKeyProps[noteL.note.#sortedKeyProps.length - 1].keyProps.code;
217
- if (
218
- // If unison is not configured, shift
219
- !Tables.UNISON ||
220
- // If we have different noteheads, shift
221
- noteUHead !== noteLHead ||
222
- // If we have different dot values, shift
223
- noteU.note.getModifiers().filter((item) => item.getCategory() === Category.Dot && item.getIndex() === 0)
224
- .length !==
225
- noteL.note.getModifiers().filter((item) => item.getCategory() === Category.Dot && item.getIndex() === 0)
226
- .length ||
227
- // If the notes are quite close but not on the same line, shift
228
- (lineDiff < 1 && lineDiff > 0) ||
229
- // If styles are different, shift
230
- JSON.stringify(noteU.note.getStyle()) !== JSON.stringify(noteL.note.getStyle())
231
- ) {
232
- xShift = voiceXShift + 2;
233
- if (noteU.stemDirection === noteL.stemDirection) {
234
- // upper voice is middle voice, so shift it right
235
- noteU.note.setXShift(xShift);
236
- } else {
237
- // shift lower voice right
238
- noteL.note.setXShift(xShift);
239
- }
240
- } else if (noteU.note.voice !== noteL.note.voice) {
241
- //If we are not in the same voice
242
- if (noteU.stemDirection === noteL.stemDirection) {
243
- if (noteU.line !== noteL.line) {
244
- xShift = voiceXShift + 2;
245
- noteU.note.setXShift(xShift);
246
- } else {
247
- //same line, swap stem direction for one note
248
- if (noteL.stemDirection === 1) {
249
- noteL.stemDirection = -1;
250
- noteL.note.setStemDirection(-1);
251
- }
252
- }
253
- }
254
- } //Very close whole notes
255
- } else if (lineDiff < 1) {
256
- xShift = voiceXShift + 2;
257
- if (noteU.note.duration < noteL.note.duration) {
258
- // upper voice is shorter, so shift it right
259
- noteU.note.setXShift(xShift);
260
- } else {
261
- // shift lower voice right
262
- noteL.note.setXShift(xShift);
263
- }
264
- } else if (noteU.note.hasStem()) {
265
- noteU.stemDirection = -noteU.note.getStemDirection();
266
- noteU.note.setStemDirection(noteU.stemDirection);
267
- } else if (noteL.note.hasStem()) {
268
- noteL.stemDirection = -noteL.note.getStemDirection();
269
- noteL.note.setStemDirection(noteL.stemDirection);
270
- }
271
- }
272
- }
273
-
274
- // format complete
275
- state.rightShift += xShift;
276
- return true;
277
- }
278
-
279
- if (!noteM) throw new RuntimeError('InvalidState', 'noteM not defined.');
280
-
281
- // For three voices, test if rests can be repositioned
282
- //
283
- // Special case 1 :: middle voice rest between two notes
284
- //
285
- if (noteM.isrest && !noteU.isrest && !noteL.isrest) {
286
- if (noteU.minLine <= noteM.maxLine || noteM.minLine <= noteL.maxLine) {
287
- const restHeight = noteM.maxLine - noteM.minLine;
288
- const space = noteU.minLine - noteL.maxLine;
289
- if (restHeight < space) {
290
- // center middle voice rest between the upper and lower voices
291
- centerRest(noteM, noteU, noteL);
292
- } else {
293
- xShift = voiceXShift + 2; // shift middle rest right
294
- noteM.note.setXShift(xShift);
295
- if (noteL.note.hasBeam() === false) {
296
- noteL.stemDirection = -1;
297
- noteL.note.setStemDirection(-1);
298
- }
299
- if (noteU.minLine <= noteL.maxLine && noteU.note.hasBeam() === false) {
300
- noteU.stemDirection = 1;
301
- noteU.note.setStemDirection(1);
302
- }
303
- }
304
- // format complete
305
- state.rightShift += xShift;
306
- return true;
307
- }
308
- }
309
-
310
- // Special case 2 :: all voices are rests
311
- if (noteU.isrest && noteM.isrest && noteL.isrest) {
312
- // Hide upper voice rest
313
- noteU.note.renderOptions.draw = false;
314
- // Hide lower voice rest
315
- noteL.note.renderOptions.draw = false;
316
- // format complete
317
- state.rightShift += xShift;
318
- return true;
319
- }
320
-
321
- // Test if any other rests can be repositioned
322
- if (noteM.isrest && noteU.isrest && noteM.minLine <= noteL.maxLine) {
323
- // Hide middle voice rest
324
- noteM.note.renderOptions.draw = false;
325
- }
326
- if (noteM.isrest && noteL.isrest && noteU.minLine <= noteM.maxLine) {
327
- // Hide middle voice rest
328
- noteM.note.renderOptions.draw = false;
329
- }
330
- if (noteU.isrest && noteU.minLine <= noteM.maxLine) {
331
- // shift upper voice rest up;
332
- shiftRestVertical(noteU, noteM, 1);
333
- }
334
- if (noteL.isrest && noteM.minLine <= noteL.maxLine) {
335
- // shift lower voice rest down
336
- shiftRestVertical(noteL, noteM, -1);
337
- }
338
- // If middle voice intersects upper or lower voice
339
- if (noteU.minLine <= noteM.maxLine + 0.5 || noteM.minLine <= noteL.maxLine) {
340
- // shift middle note right
341
- xShift = voiceXShift + 2;
342
- noteM.note.setXShift(xShift);
343
- if (noteL.note.hasBeam() === false) {
344
- noteL.stemDirection = -1;
345
- noteL.note.setStemDirection(-1);
346
- }
347
- if (noteU.minLine <= noteL.maxLine && noteU.note.hasBeam() === false) {
348
- noteU.stemDirection = 1;
349
- noteU.note.setStemDirection(1);
350
- }
351
- }
352
-
353
- state.rightShift += xShift;
354
- return true;
355
- }
356
-
357
- static postFormat(notes: Note[]): boolean {
358
- if (!notes) return false;
359
-
360
- notes.forEach((note) => note.postFormat());
361
-
362
- return true;
363
- }
364
-
365
- //////////////////////////////////////////////////////////////////////////////////////////////////
366
- // INSTANCE MEMBERS
367
-
368
- minLine: number = 0;
369
- maxLine: number = 0;
370
-
371
- protected readonly clef: string;
372
- protected readonly octaveShift?: number;
373
-
374
- protected displaced: boolean;
375
- protected dotShiftY: number;
376
- protected useDefaultHeadX: boolean;
377
- protected ledgerLineStyle: ElementStyle;
378
-
379
- #noteHeads: NoteHead[];
380
-
381
- // Sorted variant of keyProps used internally.
382
- #sortedKeyProps: { keyProps: KeyProps; index: number }[] = [];
383
-
384
- constructor(noteStruct: StaveNoteStruct) {
385
- super(noteStruct);
386
-
387
- this.ledgerLineStyle = {};
388
-
389
- this.clef = noteStruct.clef ?? 'treble';
390
- this.octaveShift = noteStruct.octaveShift ?? 0;
391
-
392
- // Pull note rendering properties.
393
- this.glyphProps = Note.getGlyphProps(this.duration, this.noteType);
394
- defined(
395
- this.glyphProps,
396
- 'BadArguments',
397
- `No glyph found for duration '${this.duration}' and type '${this.noteType}'`
398
- );
399
-
400
- // if true, displace note to right
401
- this.displaced = false;
402
- this.dotShiftY = 0;
403
- // for displaced ledger lines
404
- this.useDefaultHeadX = false;
405
-
406
- // Drawing
407
- this.#noteHeads = [];
408
- this.modifiers = [];
409
-
410
- this.renderOptions = {
411
- ...this.renderOptions,
412
- // number of stroke px to the left and right of head
413
- strokePx: noteStruct.strokePx || StaveNote.LEDGER_LINE_OFFSET,
414
- };
415
-
416
- this.calculateKeyProps();
417
- this.buildStem();
418
-
419
- // Set the stem direction
420
- if (noteStruct.autoStem) {
421
- this.autoStem();
422
- } else {
423
- this.setStemDirection(noteStruct.stemDirection ?? Stem.UP);
424
- }
425
- this.reset();
426
- this.buildFlag();
427
- }
428
-
429
- reset(): this {
430
- super.reset();
431
-
432
- // Save prior noteHead styles & reapply them after making new noteheads.
433
- const noteHeadStyles = this.#noteHeads.map((noteHead) => noteHead.getStyle());
434
- this.buildNoteHeads();
435
- this.#noteHeads.forEach((noteHead, index) => {
436
- const noteHeadStyle = noteHeadStyles[index];
437
- if (noteHeadStyle) noteHead.setStyle(noteHeadStyle);
438
- });
439
-
440
- const stave = this.stave;
441
- if (stave) {
442
- this.setStave(stave);
443
- }
444
- this.calcNoteDisplacements();
445
- return this;
446
- }
447
-
448
- setBeam(beam: Beam): this {
449
- this.beam = beam;
450
- this.calcNoteDisplacements();
451
- // Update stem extension if a beam is assigned.
452
- if (this.stem) {
453
- this.stem.setExtension(this.getStemExtension());
454
- }
455
- return this;
456
- }
457
-
458
- // Builds a `Stem` for the note
459
- buildStem(): this {
460
- this.setStem(new Stem({ hide: !!this.isRest() }));
461
- return this;
462
- }
463
-
464
- // Builds a `NoteHead` for each key in the note
465
- buildNoteHeads(): NoteHead[] {
466
- this.#noteHeads = [];
467
- const stemDirection = this.getStemDirection();
468
- const keys = this.getKeys();
469
-
470
- let lastLine = undefined;
471
- let lineDiff = undefined;
472
- let displaced = false;
473
-
474
- // Draw notes from bottom to top.
475
-
476
- // For down-stem notes, we draw from top to bottom.
477
- let start: number;
478
- let end: number;
479
- let step: number;
480
- if (stemDirection === Stem.UP) {
481
- start = 0;
482
- end = keys.length;
483
- step = 1;
484
- } else {
485
- start = keys.length - 1;
486
- end = -1;
487
- step = -1;
488
- }
489
-
490
- for (let i = start; i !== end; i += step) {
491
- // Building noteheads rely on sortedKeNotes in order to calculate the displacements
492
- const noteProps = this.#sortedKeyProps[i].keyProps;
493
- const line = noteProps.line;
494
-
495
- // Keep track of last line with a note head, so that consecutive heads
496
- // are correctly displaced.
497
- if (lastLine === undefined) {
498
- lastLine = line;
499
- } else {
500
- lineDiff = Math.abs(lastLine - line);
501
- if (lineDiff === 0 || lineDiff === 0.5) {
502
- displaced = !displaced;
503
- } else {
504
- displaced = false;
505
- this.useDefaultHeadX = true;
506
- }
507
- }
508
- lastLine = line;
509
-
510
- const notehead = new NoteHead({
511
- duration: this.duration,
512
- noteType: this.noteType,
513
- displaced,
514
- stemDirection,
515
- customGlyphCode: noteProps.code,
516
- line: noteProps.line,
517
- });
518
-
519
- notehead.fontInfo = this.fontInfo;
520
-
521
- this.addChildElement(notehead);
522
- this.#noteHeads[this.#sortedKeyProps[i].index] = notehead;
523
- }
524
- return this.#noteHeads;
525
- }
526
-
527
- // Automatically sets the stem direction based on the keys in the note
528
- autoStem(): void {
529
- this.setStemDirection(this.calculateOptimalStemDirection());
530
- }
531
-
532
- calculateOptimalStemDirection(): number {
533
- // Figure out optimal stem direction based on given notes
534
- // minLine & maxLine rely on sortedKeyProps
535
- this.minLine = this.#sortedKeyProps[0].keyProps.line;
536
- this.maxLine = this.#sortedKeyProps[this.keyProps.length - 1].keyProps.line;
537
-
538
- const MIDDLE_LINE = 3;
539
- const decider = (this.minLine + this.maxLine) / 2;
540
- const stemDirection = decider < MIDDLE_LINE ? Stem.UP : Stem.DOWN;
541
-
542
- return stemDirection;
543
- }
544
-
545
- // Calculates and stores the properties for each key in the note
546
- calculateKeyProps(): void {
547
- let lastLine: number | undefined;
548
- for (let i = 0; i < this.keys.length; ++i) {
549
- const key = this.keys[i];
550
-
551
- const options = { octaveShift: this.octaveShift ?? 0, duration: this.duration };
552
- const props = Tables.keyProperties(key, this.clef, this.noteType, options);
553
-
554
- if (!props) {
555
- throw new RuntimeError('BadArguments', `Invalid key for note properties: ${key}`);
556
- }
557
-
558
- // Override line placement for default rests
559
- if (props.key === 'R') {
560
- if (this.duration === '1' || this.duration === 'w') {
561
- props.line = 4;
562
- } else {
563
- props.line = 3;
564
- }
565
- }
566
-
567
- // Calculate displacement of this note
568
- const line = props.line;
569
- if (lastLine === undefined) {
570
- lastLine = line;
571
- } else {
572
- if (Math.abs(lastLine - line) === 0.5) {
573
- this.displaced = true;
574
- props.displaced = true;
575
-
576
- // Have to mark the previous note as
577
- // displaced as well, for modifier placement
578
- if (this.keyProps.length > 0) {
579
- this.keyProps[i - 1].displaced = true;
580
- }
581
- }
582
- }
583
-
584
- lastLine = line;
585
- this.keyProps.push(props);
586
- }
587
- // Sort the notes from lowest line to highest line in sortedKeyProps
588
- // Warn no longer required as keyProps remains unsorted
589
- this.keyProps.forEach((keyProps, index) => {
590
- this.#sortedKeyProps.push({ keyProps, index });
591
- });
592
- this.#sortedKeyProps.sort((a, b) => a.keyProps.line - b.keyProps.line);
593
- }
594
-
595
- // Get the `BoundingBox` for the entire note
596
- getBoundingBox(): BoundingBox {
597
- const boundingBox = new BoundingBox(this.getAbsoluteX(), this.ys[0], 0, 0);
598
- this.#noteHeads.forEach((notehead) => {
599
- boundingBox.mergeWith(notehead.getBoundingBox());
600
- });
601
- const { yTop, yBottom } = this.getNoteHeadBounds();
602
- // eslint-disable-next-line
603
- const noteStemHeight = this.stem!.getHeight();
604
- const flagX = this.getStemX() - Tables.STEM_WIDTH / 2;
605
- const flagY =
606
- this.getStemDirection() === Stem.DOWN
607
- ? yTop - noteStemHeight - this.flag.getTextMetrics().actualBoundingBoxDescent
608
- : yBottom - noteStemHeight + this.flag.getTextMetrics().actualBoundingBoxAscent;
609
-
610
- if (!this.isRest() && this.hasStem()) {
611
- boundingBox.mergeWith(new BoundingBox(this.getAbsoluteX(), flagY, 0, 0));
612
- }
613
- if (this.hasFlag()) {
614
- const bbFlag = this.flag.getBoundingBox();
615
- boundingBox.mergeWith(bbFlag.move(flagX, flagY));
616
- }
617
- for (let i = 0; i < this.modifiers.length; i++) {
618
- boundingBox.mergeWith(this.modifiers[i].getBoundingBox());
619
- }
620
- return boundingBox;
621
- }
622
-
623
- // Gets the line number of the bottom note in the chord.
624
- // If `isTopNote` is `true` then get the top note's line number instead
625
- getLineNumber(isTopNote?: boolean): number {
626
- if (!this.keyProps.length) {
627
- throw new RuntimeError('NoKeyProps', "Can't get bottom note line, because note is not initialized properly.");
628
- }
629
-
630
- let resultLine = this.keyProps[0].line;
631
-
632
- // No precondition assumed for sortedness of keyProps array
633
- for (let i = 0; i < this.keyProps.length; i++) {
634
- const thisLine = this.keyProps[i].line;
635
- if (isTopNote) {
636
- if (thisLine > resultLine) resultLine = thisLine;
637
- } else {
638
- if (thisLine < resultLine) resultLine = thisLine;
639
- }
640
- }
641
-
642
- return resultLine;
643
- }
644
-
645
- /**
646
- * @returns true if this note is a type of rest. Rests don't have pitches, but take up space in the score.
647
- */
648
- isRest(): boolean {
649
- const val = this.glyphProps.codeHead;
650
- return val >= '\ue4e0' && val <= '\ue4ff';
651
- }
652
-
653
- // Determine if the current note is a chord
654
- isChord(): boolean {
655
- return !this.isRest() && this.keys.length > 1;
656
- }
657
-
658
- // Determine if the `StaveNote` has a stem
659
- hasStem(): boolean {
660
- return this.glyphProps.stem;
661
- }
662
-
663
- hasFlag(): boolean {
664
- return super.hasFlag() && !this.isRest();
665
- }
666
-
667
- getStemX(): number {
668
- if (this.noteType === 'r') {
669
- return this.getCenterGlyphX();
670
- } else {
671
- // We adjust the origin of the stem because we want the stem left-aligned
672
- // with the notehead if stemmed-down, and right-aligned if stemmed-up
673
- return super.getStemX() + (this.stemDirection ? Stem.WIDTH / (2 * -this.stemDirection) : 0);
674
- }
675
- }
676
-
677
- // Get the `y` coordinate for text placed on the top/bottom of a
678
- // note at a desired `textLine`
679
- getYForTopText(textLine: number): number {
680
- const extents = this.getStemExtents();
681
- return Math.min(
682
- this.checkStave().getYForTopText(textLine),
683
- extents.topY - this.renderOptions.annotationSpacing * (textLine + 1)
684
- );
685
- }
686
- getYForBottomText(textLine: number): number {
687
- const extents = this.getStemExtents();
688
- return Math.max(
689
- this.checkStave().getYForTopText(textLine),
690
- extents.baseY + this.renderOptions.annotationSpacing * textLine
691
- );
692
- }
693
-
694
- // Sets the current note to the provided `stave`. This applies
695
- // `y` values to the `NoteHeads`.
696
- setStave(stave: Stave): this {
697
- super.setStave(stave);
698
-
699
- const ys = this.#noteHeads.map((notehead) => {
700
- notehead.setStave(stave);
701
- return notehead.getY();
702
- });
703
-
704
- this.setYs(ys);
705
-
706
- if (this.stem) {
707
- const { yTop, yBottom } = this.getNoteHeadBounds();
708
- this.stem.setYBounds(yTop, yBottom);
709
- }
710
-
711
- return this;
712
- }
713
-
714
- // Check if note is shifted to the right
715
- isDisplaced(): boolean {
716
- return this.displaced;
717
- }
718
-
719
- // Sets whether shift note to the right. `displaced` is a `boolean`
720
- setNoteDisplaced(displaced: boolean): this {
721
- this.displaced = displaced;
722
- return this;
723
- }
724
-
725
- // Get the starting `x` coordinate for a `StaveTie`
726
- getTieRightX(): number {
727
- let tieStartX = this.getAbsoluteX();
728
- tieStartX += this.getGlyphWidth() + this.xShift + this.rightDisplacedHeadPx;
729
- if (this.modifierContext) tieStartX += this.modifierContext.getRightShift();
730
- return tieStartX;
731
- }
732
-
733
- // Get the ending `x` coordinate for a `StaveTie`
734
- getTieLeftX(): number {
735
- let tieEndX = this.getAbsoluteX();
736
- tieEndX += this.xShift - this.leftDisplacedHeadPx;
737
- return tieEndX;
738
- }
739
-
740
- // Get the stave line on which to place a rest
741
- getLineForRest(): number {
742
- let restLine = this.keyProps[0].line;
743
- if (this.keyProps.length > 1) {
744
- const lastLine = this.keyProps[this.keyProps.length - 1].line;
745
- const top = Math.max(restLine, lastLine);
746
- const bot = Math.min(restLine, lastLine);
747
- restLine = midLine(top, bot);
748
- }
749
-
750
- return restLine;
751
- }
752
-
753
- // Get the default `x` and `y` coordinates for the provided `position`
754
- // and key `index`
755
- getModifierStartXY(
756
- position: number,
757
- index: number,
758
- options: { forceFlagRight?: boolean } = {}
759
- ): { x: number; y: number } {
760
- if (!this.preFormatted) {
761
- throw new RuntimeError('UnformattedNote', "Can't call GetModifierStartXY on an unformatted note");
762
- }
763
-
764
- if (this.ys.length === 0) {
765
- throw new RuntimeError('NoYValues', 'No Y-Values calculated for this note.');
766
- }
767
-
768
- const { ABOVE, BELOW, LEFT, RIGHT } = Modifier.Position;
769
- let x = 0;
770
- if (position === LEFT) {
771
- // FIXME: Left modifier padding, move to font file
772
- x = -1 * 2;
773
- } else if (position === RIGHT) {
774
- // FIXME: Right modifier padding, move to font file
775
- x = this.getGlyphWidth() + this.xShift + 2;
776
-
777
- if (
778
- this.stemDirection === Stem.UP &&
779
- this.hasFlag() &&
780
- (options.forceFlagRight || isInnerNoteIndex(this, index))
781
- ) {
782
- x += this.flag.getWidth();
783
- }
784
- } else if (position === BELOW || position === ABOVE) {
785
- x = this.getGlyphWidth() / 2;
786
- }
787
-
788
- // addtional y shifts for rests
789
- let restShift = 0;
790
- switch (this.#noteHeads[index].getText()) {
791
- case Glyphs.restDoubleWhole:
792
- case Glyphs.restWhole:
793
- restShift += 0.5;
794
- break;
795
- case Glyphs.restHalf:
796
- case Glyphs.restQuarter:
797
- case Glyphs.rest8th:
798
- case Glyphs.rest16th:
799
- restShift -= 0.5;
800
- break;
801
- case Glyphs.rest32nd:
802
- case Glyphs.rest64th:
803
- restShift -= 1.5;
804
- break;
805
- case Glyphs.rest128th:
806
- restShift -= 2.5;
807
- break;
808
- }
809
-
810
- return {
811
- x: this.getAbsoluteX() + x,
812
- y: this.ys[index] + restShift * this.checkStave().getSpacingBetweenLines(),
813
- };
814
- }
815
-
816
- // Sets the style of the complete StaveNote, including all keys
817
- // and the stem.
818
- setStyle(style: ElementStyle): this {
819
- return super.setGroupStyle(style);
820
- }
821
-
822
- setStemStyle(style: ElementStyle): this {
823
- const stem = this.getStem();
824
- if (stem) stem.setStyle(style);
825
- return this;
826
- }
827
- getStemStyle(): ElementStyle | undefined {
828
- return this.stem?.getStyle();
829
- }
830
-
831
- setLedgerLineStyle(style: ElementStyle): void {
832
- this.ledgerLineStyle = style;
833
- }
834
-
835
- getLedgerLineStyle(): ElementStyle {
836
- return this.ledgerLineStyle;
837
- }
838
-
839
- setFlagStyle(style: ElementStyle): void {
840
- this.flagStyle = style;
841
- }
842
- getFlagStyle(): ElementStyle | undefined {
843
- return this.flagStyle;
844
- }
845
-
846
- /** Get the glyph width. */
847
- getGlyphWidth(): number {
848
- return this.noteHeads[0].getWidth();
849
- }
850
-
851
- // Sets the notehead at `index` to the provided coloring `style`.
852
- //
853
- // `style` is an `object` with the following properties: `shadowColor`,
854
- // `shadowBlur`, `fillStyle`, `strokeStyle`
855
- setKeyStyle(index: number, style: ElementStyle): this {
856
- this.#noteHeads[index].setStyle(style);
857
- return this;
858
- }
859
-
860
- setKeyLine(index: number, line: number): this {
861
- this.keyProps[index].line = line;
862
- this.reset();
863
- return this;
864
- }
865
-
866
- getKeyLine(index: number): number {
867
- return this.keyProps[index].line;
868
- }
869
-
870
- // Get the width of the note if it is displaced. Used for `Voice`
871
- // formatting
872
- getVoiceShiftWidth(): number {
873
- // TODO: may need to accommodate for dot here.
874
- return this.getGlyphWidth() * (this.displaced ? 2 : 1);
875
- }
876
-
877
- // Calculates and sets the extra pixels to the left or right
878
- // if the note is displaced.
879
- calcNoteDisplacements(): void {
880
- this.setLeftDisplacedHeadPx(this.displaced && this.stemDirection === Stem.DOWN ? this.getGlyphWidth() : 0);
881
-
882
- // For upstems with flags, the extra space is unnecessary, since it's taken
883
- // up by the flag.
884
- this.setRightDisplacedHeadPx(
885
- !this.hasFlag() && this.displaced && this.stemDirection === Stem.UP ? this.getGlyphWidth() : 0
886
- );
887
- }
888
-
889
- // Pre-render formatting
890
- preFormat(): void {
891
- if (this.preFormatted) return;
892
-
893
- let noteHeadPadding = 0;
894
- if (this.modifierContext) {
895
- this.modifierContext.preFormat();
896
- // If there are no modifiers on this note, make sure there is adequate padding
897
- // between the notes.
898
- if (this.modifierContext.getWidth() === 0) {
899
- noteHeadPadding = StaveNote.minNoteheadPadding;
900
- }
901
- }
902
-
903
- let width = this.getGlyphWidth() + this.leftDisplacedHeadPx + this.rightDisplacedHeadPx + noteHeadPadding;
904
-
905
- // For upward flagged notes, the width of the flag needs to be added
906
- if (this.shouldDrawFlag() && this.stemDirection === Stem.UP) {
907
- width += this.getGlyphWidth();
908
- // TODO: Add flag width as a separate metric
909
- }
910
-
911
- this.setWidth(width);
912
- this.preFormatted = true;
913
- }
914
-
915
- /**
916
- * @typedef {Object} noteHeadBounds
917
- * @property {number} yTop the highest notehead bound
918
- * @property {number} yBottom the lowest notehead bound
919
- * @property {number|Null} displacedX the starting x for displaced noteheads
920
- * @property {number|Null} nonDisplacedX the starting x for non-displaced noteheads
921
- * @property {number} highestLine the highest notehead line in traditional music line
922
- * numbering (bottom line = 1, top line = 5)
923
- * @property {number} lowestLine the lowest notehead line
924
- * @property {number|false} highestDisplacedLine the highest staff line number
925
- * for a displaced notehead
926
- * @property {number|false} lowestDisplacedLine
927
- * @property {number} highestNonDisplacedLine
928
- * @property {number} lowestNonDisplacedLine
929
- */
930
-
931
- /**
932
- * Get the staff line and y value for the highest & lowest noteheads
933
- * @returns {noteHeadBounds}
934
- */
935
- getNoteHeadBounds(): StaveNoteHeadBounds {
936
- // Top and bottom Y values for stem.
937
- let yTop: number = +Infinity;
938
- let yBottom: number = -Infinity;
939
- let nonDisplacedX: number | undefined;
940
- let displacedX: number | undefined;
941
-
942
- let highestLine = this.checkStave().getNumLines();
943
- let lowestLine = 1;
944
- let highestDisplacedLine: number | undefined;
945
- let lowestDisplacedLine: number | undefined;
946
- let highestNonDisplacedLine = highestLine;
947
- let lowestNonDisplacedLine = lowestLine;
948
-
949
- this.#noteHeads.forEach((notehead) => {
950
- const line: number = notehead.getLine();
951
- const y = notehead.getY();
952
-
953
- yTop = Math.min(y, yTop);
954
- yBottom = Math.max(y, yBottom);
955
-
956
- if (displacedX === undefined && notehead.isDisplaced()) {
957
- displacedX = notehead.getAbsoluteX();
958
- }
959
-
960
- if (nonDisplacedX === undefined && !notehead.isDisplaced()) {
961
- nonDisplacedX = notehead.getAbsoluteX();
962
- }
963
-
964
- highestLine = Math.max(line, highestLine);
965
- lowestLine = Math.min(line, lowestLine);
966
-
967
- if (notehead.isDisplaced()) {
968
- highestDisplacedLine = highestDisplacedLine === undefined ? line : Math.max(line, highestDisplacedLine);
969
- lowestDisplacedLine = lowestDisplacedLine === undefined ? line : Math.min(line, lowestDisplacedLine);
970
- } else {
971
- highestNonDisplacedLine = Math.max(line, highestNonDisplacedLine);
972
- lowestNonDisplacedLine = Math.min(line, lowestNonDisplacedLine);
973
- }
974
- }, this);
975
-
976
- return {
977
- yTop,
978
- yBottom,
979
- displacedX,
980
- nonDisplacedX,
981
- highestLine,
982
- lowestLine,
983
- highestDisplacedLine,
984
- lowestDisplacedLine,
985
- highestNonDisplacedLine,
986
- lowestNonDisplacedLine,
987
- };
988
- }
989
-
990
- // Get the starting `x` coordinate for the noteheads
991
- getNoteHeadBeginX(): number {
992
- return this.getAbsoluteX() + this.xShift;
993
- }
994
-
995
- // Get the ending `x` coordinate for the noteheads
996
- getNoteHeadEndX(): number {
997
- const xBegin = this.getNoteHeadBeginX();
998
- return xBegin + this.getGlyphWidth();
999
- }
1000
-
1001
- get noteHeads(): NoteHead[] {
1002
- return this.#noteHeads.slice();
1003
- }
1004
-
1005
- // Draw the ledger lines between the stave and the highest/lowest keys
1006
- drawLedgerLines(): void {
1007
- const stave = this.checkStave();
1008
- const {
1009
- renderOptions: { strokePx },
1010
- } = this;
1011
- const ctx = this.checkContext();
1012
- const width = this.getGlyphWidth() + strokePx * 2;
1013
- const doubleWidth = 2 * (this.getGlyphWidth() + strokePx) - Stem.WIDTH / 2;
1014
-
1015
- if (this.isRest()) return;
1016
- if (!ctx) {
1017
- throw new RuntimeError('NoCanvasContext', "Can't draw without a canvas context.");
1018
- }
1019
-
1020
- const {
1021
- highestLine,
1022
- lowestLine,
1023
- highestDisplacedLine,
1024
- highestNonDisplacedLine,
1025
- lowestDisplacedLine,
1026
- lowestNonDisplacedLine,
1027
- displacedX,
1028
- nonDisplacedX,
1029
- } = this.getNoteHeadBounds();
1030
-
1031
- // Early out if there are no ledger lines to draw.
1032
- if (highestLine < 6 && lowestLine > 0) return;
1033
-
1034
- const minX = Math.min(displacedX ?? 0, nonDisplacedX ?? 0);
1035
-
1036
- const drawLedgerLine = (y: number, normal: boolean, displaced: boolean) => {
1037
- let x;
1038
- if (displaced && normal) x = minX - strokePx;
1039
- else if (normal) x = (nonDisplacedX ?? 0) - strokePx;
1040
- else x = (displacedX ?? 0) - strokePx;
1041
- const ledgerWidth = normal && displaced ? doubleWidth : width;
1042
-
1043
- ctx.beginPath();
1044
- ctx.moveTo(x, y);
1045
- ctx.lineTo(x + ledgerWidth, y);
1046
- ctx.stroke();
1047
- };
1048
-
1049
- const style = { ...stave.getDefaultLedgerLineStyle(), ...this.getLedgerLineStyle() };
1050
- this.applyStyle(ctx, style);
1051
-
1052
- // Draw ledger lines below the staff:
1053
- for (let line = 6; line <= highestLine; ++line) {
1054
- const normal = nonDisplacedX !== undefined && line <= highestNonDisplacedLine;
1055
- const displaced = highestDisplacedLine !== undefined && line <= highestDisplacedLine;
1056
- drawLedgerLine(stave.getYForNote(line), normal, displaced);
1057
- }
1058
-
1059
- // Draw ledger lines above the staff:
1060
- for (let line = 0; line >= lowestLine; --line) {
1061
- const normal = nonDisplacedX !== undefined && line >= lowestNonDisplacedLine;
1062
- const displaced = lowestDisplacedLine !== undefined && line >= lowestDisplacedLine;
1063
- drawLedgerLine(stave.getYForNote(line), normal, displaced);
1064
- }
1065
-
1066
- this.restoreStyle(ctx, style);
1067
- }
1068
-
1069
- // Draw all key modifiers
1070
- drawModifiers(noteheadParam: NoteHead): void {
1071
- const ctx = this.checkContext();
1072
- for (let i = 0; i < this.modifiers.length; i++) {
1073
- const modifier = this.modifiers[i];
1074
- const index = modifier.checkIndex();
1075
- const notehead = this.#noteHeads[index];
1076
- if (notehead === noteheadParam) {
1077
- const noteheadStyle = notehead.getStyle();
1078
- notehead.applyStyle(ctx, noteheadStyle);
1079
- modifier.setContext(ctx);
1080
- modifier.drawWithStyle();
1081
- notehead.restoreStyle(ctx, noteheadStyle);
1082
- }
1083
- }
1084
- }
1085
-
1086
- shouldDrawFlag(): boolean {
1087
- const hasStem = this.stem !== undefined;
1088
- const hasFlag = this.glyphProps.codeFlagUp !== undefined;
1089
- const hasNoBeam = this.beam === undefined;
1090
- return hasStem && hasFlag && hasNoBeam && !this.isRest();
1091
- }
1092
-
1093
- // Draw the flag for the note
1094
- drawFlag(): void {
1095
- const ctx = this.checkContext();
1096
- if (!ctx) {
1097
- throw new RuntimeError('NoCanvasContext', "Can't draw without a canvas context.");
1098
- }
1099
-
1100
- if (this.shouldDrawFlag()) {
1101
- const { yTop, yBottom } = this.getNoteHeadBounds();
1102
- // eslint-disable-next-line
1103
- const noteStemHeight = this.stem!.getHeight();
1104
- const flagX = this.getStemX() - Tables.STEM_WIDTH / 2;
1105
- const flagY =
1106
- this.getStemDirection() === Stem.DOWN
1107
- ? // Down stems are below the note head and have flags on the right.
1108
- yTop - noteStemHeight - this.flag.getTextMetrics().actualBoundingBoxDescent
1109
- : // Up stems are above the note head and have flags on the right.
1110
- yBottom - noteStemHeight + this.flag.getTextMetrics().actualBoundingBoxAscent;
1111
-
1112
- // Draw the Flag
1113
- this.applyStyle(ctx, this.flagStyle);
1114
- this.flag.renderText(ctx, flagX, flagY);
1115
- this.restoreStyle(ctx, this.flagStyle);
1116
- }
1117
- }
1118
-
1119
- // Draw the NoteHeads
1120
- drawNoteHeads(): void {
1121
- const ctx = this.checkContext();
1122
- this.#noteHeads.forEach((notehead) => {
1123
- notehead.applyStyle(ctx);
1124
- ctx.openGroup('notehead', notehead.getAttribute('id'));
1125
- notehead.setContext(ctx).draw();
1126
- this.drawModifiers(notehead);
1127
- ctx.closeGroup();
1128
- notehead.restoreStyle(ctx);
1129
- });
1130
- }
1131
-
1132
- drawStem(stemOptions?: StemOptions): void {
1133
- // GCR TODO: I can't find any context in which this is called with the stemStruct
1134
- // argument in the codebase or tests. Nor can I find a case where super.drawStem
1135
- // is called at all. Perhaps these should be removed?
1136
- const ctx = this.checkContext();
1137
-
1138
- if (stemOptions) {
1139
- this.setStem(new Stem(stemOptions));
1140
- }
1141
-
1142
- // If we will render a flag, we shorten the stem so that the tip
1143
- // does not poke through the flag.
1144
- if (this.shouldDrawFlag() && this.stem) {
1145
- this.stem.adjustHeightForFlag();
1146
- }
1147
-
1148
- if (this.stem) {
1149
- this.stem.setContext(ctx).draw();
1150
- }
1151
- }
1152
-
1153
- /**
1154
- * Override stemmablenote stem extension to adjust for distance from middle line.
1155
- */
1156
- getStemExtension(): number {
1157
- const superStemExtension = super.getStemExtension();
1158
- if (!this.glyphProps.stem) {
1159
- return superStemExtension;
1160
- }
1161
-
1162
- const stemDirection = this.getStemDirection();
1163
- if (stemDirection !== this.calculateOptimalStemDirection()) {
1164
- return superStemExtension; // no adjustment for manually set stem direction.
1165
- }
1166
- let midLineDistance;
1167
- const MIDDLE_LINE = 3;
1168
- if (stemDirection === Stem.UP) {
1169
- // Note that the use of maxLine here instead of minLine might
1170
- // seem counterintuitive, but in the case of (say) treble clef
1171
- // chord(F2, E4) stem up, we do not want to extend the stem because
1172
- // of F2, when a normal octave-length stem above E4 is fine.
1173
- //
1174
- // maxLine and minLine are set in calculateOptimalStemDirection() so
1175
- // will be known.
1176
- midLineDistance = MIDDLE_LINE - this.maxLine;
1177
- } else {
1178
- midLineDistance = this.minLine - MIDDLE_LINE;
1179
- }
1180
-
1181
- // how many lines more than an octave is the relevant notehead?
1182
- const linesOverOctaveFromMidLine = midLineDistance - 3.5;
1183
- if (linesOverOctaveFromMidLine <= 0) {
1184
- return superStemExtension;
1185
- }
1186
- const stave = this.getStave();
1187
- let spacingBetweenLines = 10;
1188
- if (stave !== undefined) {
1189
- spacingBetweenLines = stave.getSpacingBetweenLines();
1190
- }
1191
- return superStemExtension + linesOverOctaveFromMidLine * spacingBetweenLines;
1192
- }
1193
-
1194
- // Draws all the `StaveNote` parts. This is the main drawing method.
1195
- draw(): void {
1196
- if (this.renderOptions.draw === false) return;
1197
-
1198
- if (this.ys.length === 0) {
1199
- throw new RuntimeError('NoYValues', "Can't draw note without Y values.");
1200
- }
1201
-
1202
- const ctx = this.checkContext();
1203
- const xBegin = this.getNoteHeadBeginX();
1204
- const shouldRenderStem = this.hasStem() && !this.beam;
1205
-
1206
- // Format note head x positions
1207
- this.#noteHeads.forEach((notehead) => notehead.setX(xBegin));
1208
-
1209
- if (this.stem) {
1210
- // Format stem x positions
1211
- const stemX = this.getStemX();
1212
- this.stem.setNoteHeadXBounds(stemX, stemX);
1213
- }
1214
-
1215
- L('Rendering ', this.isChord() ? 'chord :' : 'note :', this.keys);
1216
-
1217
- // Apply the overall style -- may be contradicted by local settings:
1218
- this.applyStyle();
1219
- ctx.openGroup('stavenote', this.getAttribute('id'));
1220
- this.drawLedgerLines();
1221
- if (shouldRenderStem) this.drawStem();
1222
- this.drawNoteHeads();
1223
- this.drawFlag();
1224
- const bb = this.getBoundingBox();
1225
- ctx.pointerRect(bb.getX(), bb.getY(), bb.getW(), bb.getH());
1226
- ctx.closeGroup();
1227
- this.restoreStyle();
1228
- this.setRendered();
1229
- }
1230
- }