@mcqj/vexflow 0.0.1-alpha.1

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 (300) hide show
  1. package/LICENSE +22 -0
  2. package/README.md +208 -0
  3. package/build/cjs/vexflow-bravura.js +2 -0
  4. package/build/cjs/vexflow-bravura.js.map +1 -0
  5. package/build/cjs/vexflow-core.js +2 -0
  6. package/build/cjs/vexflow-core.js.map +1 -0
  7. package/build/cjs/vexflow-debug-with-tests.js +43475 -0
  8. package/build/cjs/vexflow-debug-with-tests.js.map +1 -0
  9. package/build/cjs/vexflow-debug.js +19194 -0
  10. package/build/cjs/vexflow-debug.js.map +1 -0
  11. package/build/cjs/vexflow.js +2 -0
  12. package/build/cjs/vexflow.js.map +1 -0
  13. package/build/esm/entry/vexflow-bravura.js +17 -0
  14. package/build/esm/entry/vexflow-core.js +4 -0
  15. package/build/esm/entry/vexflow-debug-with-tests.js +38 -0
  16. package/build/esm/entry/vexflow-debug.js +31 -0
  17. package/build/esm/entry/vexflow.js +31 -0
  18. package/build/esm/package.json +3 -0
  19. package/build/esm/src/accidental.js +330 -0
  20. package/build/esm/src/annotation.js +231 -0
  21. package/build/esm/src/articulation.js +281 -0
  22. package/build/esm/src/barnote.js +56 -0
  23. package/build/esm/src/beam.js +644 -0
  24. package/build/esm/src/bend.js +186 -0
  25. package/build/esm/src/boundingbox.js +59 -0
  26. package/build/esm/src/canvascontext.js +216 -0
  27. package/build/esm/src/chordsymbol.js +325 -0
  28. package/build/esm/src/clef.js +120 -0
  29. package/build/esm/src/clefnote.js +35 -0
  30. package/build/esm/src/crescendo.js +78 -0
  31. package/build/esm/src/curve.js +126 -0
  32. package/build/esm/src/dot.js +129 -0
  33. package/build/esm/src/easyscore.js +374 -0
  34. package/build/esm/src/element.js +409 -0
  35. package/build/esm/src/factory.js +463 -0
  36. package/build/esm/src/flag.js +22 -0
  37. package/build/esm/src/font.js +237 -0
  38. package/build/esm/src/fonts/academico.js +1 -0
  39. package/build/esm/src/fonts/academicobold.js +1 -0
  40. package/build/esm/src/fonts/bravura.js +1 -0
  41. package/build/esm/src/fonts/gonville.js +1 -0
  42. package/build/esm/src/fonts/petaluma.js +1 -0
  43. package/build/esm/src/fonts/petalumascript.js +1 -0
  44. package/build/esm/src/formatter.js +610 -0
  45. package/build/esm/src/fraction.js +170 -0
  46. package/build/esm/src/frethandfinger.js +141 -0
  47. package/build/esm/src/ghostnote.js +50 -0
  48. package/build/esm/src/glyphnote.js +44 -0
  49. package/build/esm/src/glyphs.js +2936 -0
  50. package/build/esm/src/gracenote.js +106 -0
  51. package/build/esm/src/gracenotegroup.js +138 -0
  52. package/build/esm/src/gracetabnote.js +11 -0
  53. package/build/esm/src/index.js +86 -0
  54. package/build/esm/src/keymanager.js +99 -0
  55. package/build/esm/src/keysignature.js +220 -0
  56. package/build/esm/src/keysignote.js +28 -0
  57. package/build/esm/src/metrics.js +237 -0
  58. package/build/esm/src/modifier.js +121 -0
  59. package/build/esm/src/modifiercontext.js +110 -0
  60. package/build/esm/src/multimeasurerest.js +127 -0
  61. package/build/esm/src/music.js +322 -0
  62. package/build/esm/src/note.js +383 -0
  63. package/build/esm/src/notehead.js +89 -0
  64. package/build/esm/src/notesubgroup.js +59 -0
  65. package/build/esm/src/ornament.js +192 -0
  66. package/build/esm/src/parenthesis.js +61 -0
  67. package/build/esm/src/parser.js +164 -0
  68. package/build/esm/src/pedalmarking.js +168 -0
  69. package/build/esm/src/registry.js +81 -0
  70. package/build/esm/src/rendercontext.js +20 -0
  71. package/build/esm/src/renderer.js +112 -0
  72. package/build/esm/src/repeatnote.js +17 -0
  73. package/build/esm/src/stave.js +579 -0
  74. package/build/esm/src/stavebarline.js +237 -0
  75. package/build/esm/src/staveconnector.js +180 -0
  76. package/build/esm/src/stavehairpin.js +115 -0
  77. package/build/esm/src/staveline.js +199 -0
  78. package/build/esm/src/stavemodifier.js +56 -0
  79. package/build/esm/src/stavenote.js +861 -0
  80. package/build/esm/src/staverepetition.js +120 -0
  81. package/build/esm/src/stavesection.js +36 -0
  82. package/build/esm/src/stavetempo.js +110 -0
  83. package/build/esm/src/stavetext.js +55 -0
  84. package/build/esm/src/stavetie.js +186 -0
  85. package/build/esm/src/stavevolta.js +53 -0
  86. package/build/esm/src/stem.js +133 -0
  87. package/build/esm/src/stemmablenote.js +193 -0
  88. package/build/esm/src/stringnumber.js +228 -0
  89. package/build/esm/src/strokes.js +164 -0
  90. package/build/esm/src/svgcontext.js +402 -0
  91. package/build/esm/src/system.js +166 -0
  92. package/build/esm/src/tables.js +696 -0
  93. package/build/esm/src/tabnote.js +278 -0
  94. package/build/esm/src/tabslide.js +68 -0
  95. package/build/esm/src/tabstave.js +17 -0
  96. package/build/esm/src/tabtie.js +19 -0
  97. package/build/esm/src/textbracket.js +128 -0
  98. package/build/esm/src/textdynamics.js +53 -0
  99. package/build/esm/src/textnote.js +116 -0
  100. package/build/esm/src/tickable.js +224 -0
  101. package/build/esm/src/tickcontext.js +189 -0
  102. package/build/esm/src/timesignature.js +169 -0
  103. package/build/esm/src/timesignote.js +30 -0
  104. package/build/esm/src/tremolo.js +30 -0
  105. package/build/esm/src/tuning.js +55 -0
  106. package/build/esm/src/tuplet.js +258 -0
  107. package/build/esm/src/typeguard.js +30 -0
  108. package/build/esm/src/util.js +67 -0
  109. package/build/esm/src/version.js +3 -0
  110. package/build/esm/src/vexflow.js +309 -0
  111. package/build/esm/src/vibrato.js +68 -0
  112. package/build/esm/src/vibratobracket.js +44 -0
  113. package/build/esm/src/voice.js +178 -0
  114. package/build/esm/src/web.js +15 -0
  115. package/build/esm/tests/accidental_tests.js +1055 -0
  116. package/build/esm/tests/annotation_tests.js +381 -0
  117. package/build/esm/tests/articulation_tests.js +432 -0
  118. package/build/esm/tests/auto_beam_formatting_tests.js +551 -0
  119. package/build/esm/tests/barline_tests.js +52 -0
  120. package/build/esm/tests/beam_tests.js +669 -0
  121. package/build/esm/tests/bend_tests.js +223 -0
  122. package/build/esm/tests/boundingbox_tests.js +53 -0
  123. package/build/esm/tests/browser_test_runner.js +128 -0
  124. package/build/esm/tests/chordsymbol_tests.js +405 -0
  125. package/build/esm/tests/clef_tests.js +141 -0
  126. package/build/esm/tests/crossbeam_tests.js +306 -0
  127. package/build/esm/tests/curve_tests.js +152 -0
  128. package/build/esm/tests/demo_petzold_tests.js +335 -0
  129. package/build/esm/tests/dot_tests.js +119 -0
  130. package/build/esm/tests/easyscore_tests.js +488 -0
  131. package/build/esm/tests/factory_tests.js +44 -0
  132. package/build/esm/tests/font_tests.js +134 -0
  133. package/build/esm/tests/formatter/framestack.js +61 -0
  134. package/build/esm/tests/formatter/tests.js +251 -0
  135. package/build/esm/tests/formatter_tests.js +759 -0
  136. package/build/esm/tests/fraction_tests.js +72 -0
  137. package/build/esm/tests/ghostnote_tests.js +77 -0
  138. package/build/esm/tests/glyphnote_tests.js +119 -0
  139. package/build/esm/tests/gracenote_tests.js +414 -0
  140. package/build/esm/tests/gracetabnote_tests.js +79 -0
  141. package/build/esm/tests/index.js +68 -0
  142. package/build/esm/tests/key_clef_tests.js +106 -0
  143. package/build/esm/tests/keymanager_tests.js +68 -0
  144. package/build/esm/tests/keysignature_tests.js +322 -0
  145. package/build/esm/tests/mocks.js +70 -0
  146. package/build/esm/tests/modifier_tests.js +24 -0
  147. package/build/esm/tests/multimeasurerest_tests.js +141 -0
  148. package/build/esm/tests/music_tests.js +161 -0
  149. package/build/esm/tests/node_test_runner.js +31 -0
  150. package/build/esm/tests/notehead_tests.js +224 -0
  151. package/build/esm/tests/notesubgroup_tests.js +149 -0
  152. package/build/esm/tests/offscreencanvas_tests.js +47 -0
  153. package/build/esm/tests/ornament_tests.js +333 -0
  154. package/build/esm/tests/parser_tests.js +122 -0
  155. package/build/esm/tests/pedalmarking_tests.js +91 -0
  156. package/build/esm/tests/percussion_tests.js +184 -0
  157. package/build/esm/tests/playwright/flow.spec.js +127 -0
  158. package/build/esm/tests/playwright/visual_helpers.js +30 -0
  159. package/build/esm/tests/registry_tests.js +64 -0
  160. package/build/esm/tests/renderer_tests.js +113 -0
  161. package/build/esm/tests/rests_tests.js +266 -0
  162. package/build/esm/tests/rhythm_tests.js +357 -0
  163. package/build/esm/tests/stave_tests.js +570 -0
  164. package/build/esm/tests/staveconnector_tests.js +402 -0
  165. package/build/esm/tests/stavehairpin_tests.js +90 -0
  166. package/build/esm/tests/staveline_tests.js +123 -0
  167. package/build/esm/tests/stavemodifier_tests.js +51 -0
  168. package/build/esm/tests/stavenote_tests.js +936 -0
  169. package/build/esm/tests/stavetie_tests.js +137 -0
  170. package/build/esm/tests/stringnumber_tests.js +283 -0
  171. package/build/esm/tests/strokes_tests.js +289 -0
  172. package/build/esm/tests/style_tests.js +96 -0
  173. package/build/esm/tests/tabnote_tests.js +462 -0
  174. package/build/esm/tests/tabslide_tests.js +133 -0
  175. package/build/esm/tests/tabstave_tests.js +24 -0
  176. package/build/esm/tests/tabtie_tests.js +171 -0
  177. package/build/esm/tests/test_runner.js +23 -0
  178. package/build/esm/tests/textbracket_tests.js +100 -0
  179. package/build/esm/tests/textnote_tests.js +216 -0
  180. package/build/esm/tests/threevoice_tests.js +132 -0
  181. package/build/esm/tests/tickcontext_tests.js +36 -0
  182. package/build/esm/tests/timesignature_tests.js +173 -0
  183. package/build/esm/tests/tremolo_tests.js +34 -0
  184. package/build/esm/tests/tuning_tests.js +56 -0
  185. package/build/esm/tests/tuplet_tests.js +711 -0
  186. package/build/esm/tests/typeguard_tests.js +82 -0
  187. package/build/esm/tests/unison_tests.js +74 -0
  188. package/build/esm/tests/vexflow_test_helpers.js +318 -0
  189. package/build/esm/tests/vf_prefix_tests.js +101 -0
  190. package/build/esm/tests/vibrato_tests.js +93 -0
  191. package/build/esm/tests/vibratobracket_tests.js +45 -0
  192. package/build/esm/tests/vitest/job-0.test.js +2 -0
  193. package/build/esm/tests/vitest/job-1.test.js +2 -0
  194. package/build/esm/tests/vitest/job-2.test.js +2 -0
  195. package/build/esm/tests/vitest/job-3.test.js +2 -0
  196. package/build/esm/tests/vitest/run.js +5 -0
  197. package/build/esm/tests/vitest.setup.js +44 -0
  198. package/build/esm/tests/voice_tests.js +69 -0
  199. package/build/types/entry/vexflow-bravura.d.ts +3 -0
  200. package/build/types/entry/vexflow-core.d.ts +3 -0
  201. package/build/types/entry/vexflow-debug.d.ts +3 -0
  202. package/build/types/entry/vexflow.d.ts +3 -0
  203. package/build/types/src/accidental.d.ts +60 -0
  204. package/build/types/src/annotation.d.ts +58 -0
  205. package/build/types/src/articulation.d.ts +78 -0
  206. package/build/types/src/barnote.d.ts +29 -0
  207. package/build/types/src/beam.d.ts +158 -0
  208. package/build/types/src/bend.d.ts +67 -0
  209. package/build/types/src/boundingbox.d.ts +42 -0
  210. package/build/types/src/canvascontext.d.ts +93 -0
  211. package/build/types/src/chordsymbol.d.ts +109 -0
  212. package/build/types/src/clef.d.ts +38 -0
  213. package/build/types/src/clefnote.d.ts +17 -0
  214. package/build/types/src/crescendo.d.ts +26 -0
  215. package/build/types/src/curve.d.ts +42 -0
  216. package/build/types/src/dot.d.ts +19 -0
  217. package/build/types/src/easyscore.d.ts +152 -0
  218. package/build/types/src/element.d.ts +278 -0
  219. package/build/types/src/factory.d.ts +273 -0
  220. package/build/types/src/flag.d.ts +12 -0
  221. package/build/types/src/font.d.ts +96 -0
  222. package/build/types/src/fonts/academico.d.ts +1 -0
  223. package/build/types/src/fonts/academicobold.d.ts +1 -0
  224. package/build/types/src/fonts/bravura.d.ts +1 -0
  225. package/build/types/src/fonts/gonville.d.ts +1 -0
  226. package/build/types/src/fonts/petaluma.d.ts +1 -0
  227. package/build/types/src/fonts/petalumascript.d.ts +1 -0
  228. package/build/types/src/formatter.d.ts +226 -0
  229. package/build/types/src/fraction.d.ts +65 -0
  230. package/build/types/src/frethandfinger.d.ts +19 -0
  231. package/build/types/src/ghostnote.d.ts +16 -0
  232. package/build/types/src/glyphnote.d.ts +14 -0
  233. package/build/types/src/glyphs.d.ts +2935 -0
  234. package/build/types/src/gracenote.d.ts +17 -0
  235. package/build/types/src/gracenotegroup.d.ts +33 -0
  236. package/build/types/src/gracetabnote.d.ts +5 -0
  237. package/build/types/src/index.d.ts +86 -0
  238. package/build/types/src/keymanager.d.ts +25 -0
  239. package/build/types/src/keysignature.d.ts +50 -0
  240. package/build/types/src/keysignote.d.ts +11 -0
  241. package/build/types/src/metrics.d.ts +25 -0
  242. package/build/types/src/modifier.d.ts +90 -0
  243. package/build/types/src/modifiercontext.d.ts +48 -0
  244. package/build/types/src/multimeasurerest.d.ts +61 -0
  245. package/build/types/src/music.d.ts +91 -0
  246. package/build/types/src/note.d.ts +232 -0
  247. package/build/types/src/notehead.d.ts +50 -0
  248. package/build/types/src/notesubgroup.d.ts +18 -0
  249. package/build/types/src/ornament.d.ts +71 -0
  250. package/build/types/src/parenthesis.d.ts +21 -0
  251. package/build/types/src/parser.d.ts +45 -0
  252. package/build/types/src/pedalmarking.d.ts +63 -0
  253. package/build/types/src/registry.d.ts +40 -0
  254. package/build/types/src/rendercontext.d.ts +59 -0
  255. package/build/types/src/renderer.d.ts +35 -0
  256. package/build/types/src/repeatnote.d.ts +6 -0
  257. package/build/types/src/stave.d.ts +181 -0
  258. package/build/types/src/stavebarline.d.ts +38 -0
  259. package/build/types/src/staveconnector.d.ts +53 -0
  260. package/build/types/src/stavehairpin.d.ts +56 -0
  261. package/build/types/src/staveline.d.ts +49 -0
  262. package/build/types/src/stavemodifier.d.ts +35 -0
  263. package/build/types/src/stavenote.d.ts +150 -0
  264. package/build/types/src/staverepetition.d.ts +30 -0
  265. package/build/types/src/stavesection.d.ts +8 -0
  266. package/build/types/src/stavetempo.d.ts +43 -0
  267. package/build/types/src/stavetext.d.ts +11 -0
  268. package/build/types/src/stavetie.d.ts +104 -0
  269. package/build/types/src/stavevolta.d.ts +15 -0
  270. package/build/types/src/stem.d.ts +58 -0
  271. package/build/types/src/stemmablenote.d.ts +39 -0
  272. package/build/types/src/stringnumber.d.ts +26 -0
  273. package/build/types/src/strokes.d.ts +35 -0
  274. package/build/types/src/svgcontext.d.ts +135 -0
  275. package/build/types/src/system.d.ts +121 -0
  276. package/build/types/src/tables.d.ts +86 -0
  277. package/build/types/src/tabnote.d.ts +45 -0
  278. package/build/types/src/tabslide.d.ts +36 -0
  279. package/build/types/src/tabstave.d.ts +7 -0
  280. package/build/types/src/tabtie.d.ts +18 -0
  281. package/build/types/src/textbracket.d.ts +45 -0
  282. package/build/types/src/textdynamics.d.ts +34 -0
  283. package/build/types/src/textnote.d.ts +48 -0
  284. package/build/types/src/tickable.d.ts +149 -0
  285. package/build/types/src/tickcontext.d.ts +112 -0
  286. package/build/types/src/timesignature.d.ts +75 -0
  287. package/build/types/src/timesignote.d.ts +11 -0
  288. package/build/types/src/tremolo.d.ts +12 -0
  289. package/build/types/src/tuning.d.ts +23 -0
  290. package/build/types/src/tuplet.d.ts +98 -0
  291. package/build/types/src/typeguard.d.ts +100 -0
  292. package/build/types/src/util.d.ts +42 -0
  293. package/build/types/src/version.d.ts +3 -0
  294. package/build/types/src/vexflow.d.ts +261 -0
  295. package/build/types/src/vibrato.d.ts +20 -0
  296. package/build/types/src/vibratobracket.d.ts +28 -0
  297. package/build/types/src/voice.d.ts +109 -0
  298. package/build/types/src/web.d.ts +2 -0
  299. package/package.json +145 -0
  300. package/tests/flow.html +256 -0
@@ -0,0 +1,861 @@
1
+ import { BoundingBox } from './boundingbox.js';
2
+ import { Glyphs } from './glyphs.js';
3
+ import { Metrics } from './metrics.js';
4
+ import { Modifier } from './modifier.js';
5
+ import { Note } from './note.js';
6
+ import { NoteHead } from './notehead.js';
7
+ import { Stem } from './stem.js';
8
+ import { StemmableNote } from './stemmablenote.js';
9
+ import { Tables } from './tables.js';
10
+ import { defined, log, midLine, RuntimeError } from './util.js';
11
+ function L(...args) {
12
+ if (StaveNote.DEBUG)
13
+ log('VexFlow.StaveNote', args);
14
+ }
15
+ const isInnerNoteIndex = (note, index) => index === (note.getStemDirection() === Stem.UP ? note.keyProps.length - 1 : 0);
16
+ function shiftRestVertical(rest, note, dir) {
17
+ const delta = dir;
18
+ rest.line += delta;
19
+ rest.maxLine += delta;
20
+ rest.minLine += delta;
21
+ rest.note.setKeyLine(0, rest.note.getKeyLine(0) + delta);
22
+ }
23
+ function centerRest(rest, noteU, noteL) {
24
+ const delta = rest.line - midLine(noteU.minLine, noteL.maxLine);
25
+ rest.note.setKeyLine(0, rest.note.getKeyLine(0) - delta);
26
+ rest.line -= delta;
27
+ rest.maxLine -= delta;
28
+ rest.minLine -= delta;
29
+ }
30
+ export class StaveNote extends StemmableNote {
31
+ static get CATEGORY() {
32
+ return "StaveNote";
33
+ }
34
+ static get LEDGER_LINE_OFFSET() {
35
+ return 3;
36
+ }
37
+ static get minNoteheadPadding() {
38
+ return Metrics.get('NoteHead.minPadding');
39
+ }
40
+ static format(notes, state) {
41
+ if (!notes || notes.length < 2)
42
+ return false;
43
+ const notesList = [];
44
+ for (let i = 0; i < notes.length; i++) {
45
+ const props = notes[i].sortedKeyProps;
46
+ const line = props[0].keyProps.line;
47
+ let minL = props[props.length - 1].keyProps.line;
48
+ const stemDirection = notes[i].getStemDirection();
49
+ const stemMax = notes[i].getStemLength() / 10;
50
+ const stemMin = notes[i].getStemMinimumLength() / 10;
51
+ let maxL;
52
+ if (notes[i].isRest()) {
53
+ maxL =
54
+ line +
55
+ Math.ceil(notes[i]._noteHeads[0].getTextMetrics().actualBoundingBoxAscent / Tables.STAVE_LINE_DISTANCE);
56
+ minL =
57
+ line -
58
+ Math.ceil(notes[i]._noteHeads[0].getTextMetrics().actualBoundingBoxDescent / Tables.STAVE_LINE_DISTANCE);
59
+ }
60
+ else {
61
+ maxL =
62
+ stemDirection === 1 ? props[props.length - 1].keyProps.line + stemMax : props[props.length - 1].keyProps.line;
63
+ minL = stemDirection === 1 ? props[0].keyProps.line : props[0].keyProps.line - stemMax;
64
+ }
65
+ notesList.push({
66
+ line: props[0].keyProps.line,
67
+ maxLine: maxL,
68
+ minLine: minL,
69
+ isrest: notes[i].isRest(),
70
+ stemDirection,
71
+ stemMax,
72
+ stemMin,
73
+ voiceShift: notes[i].getVoiceShiftWidth(),
74
+ isDisplaced: notes[i].isDisplaced(),
75
+ note: notes[i],
76
+ });
77
+ }
78
+ let voices = 0;
79
+ let noteU = undefined;
80
+ let noteM = undefined;
81
+ let noteL = undefined;
82
+ const draw = [false, false, false];
83
+ for (let i = 0; i < notesList.length; i++) {
84
+ draw[i] = notesList[i].note.renderOptions.draw !== false;
85
+ }
86
+ if (draw[0] && draw[1] && draw[2]) {
87
+ voices = 3;
88
+ noteU = notesList[0];
89
+ noteM = notesList[1];
90
+ noteL = notesList[2];
91
+ }
92
+ else if (draw[0] && draw[1]) {
93
+ voices = 2;
94
+ noteU = notesList[0];
95
+ noteL = notesList[1];
96
+ }
97
+ else if (draw[0] && draw[2]) {
98
+ voices = 2;
99
+ noteU = notesList[0];
100
+ noteL = notesList[2];
101
+ }
102
+ else if (draw[1] && draw[2]) {
103
+ voices = 2;
104
+ noteU = notesList[1];
105
+ noteL = notesList[2];
106
+ }
107
+ else {
108
+ return true;
109
+ }
110
+ if (voices === 2 && noteU.stemDirection === -1 && noteL.stemDirection === 1) {
111
+ noteU = notesList[1];
112
+ noteL = notesList[0];
113
+ }
114
+ const voiceXShift = Math.max(noteU.voiceShift, noteL.voiceShift);
115
+ let xShift = 0;
116
+ if (voices === 2) {
117
+ const lineSpacing = noteU.note.hasStem() && noteL.note.hasStem() && noteU.stemDirection === noteL.stemDirection ? 0.0 : 0.5;
118
+ if (noteL.isrest && noteU.isrest && noteU.note.duration === noteL.note.duration) {
119
+ noteL.note.renderOptions.draw = false;
120
+ }
121
+ else if (noteU.minLine <= noteL.maxLine + lineSpacing) {
122
+ if (noteU.isrest) {
123
+ shiftRestVertical(noteU, noteL, 1);
124
+ }
125
+ else if (noteL.isrest) {
126
+ shiftRestVertical(noteL, noteU, -1);
127
+ }
128
+ else {
129
+ const lineDiff = Math.abs(noteU.line - noteL.line);
130
+ if (noteU.note.hasStem() && noteL.note.hasStem()) {
131
+ const noteUHead = noteU.note.sortedKeyProps[0].keyProps.code;
132
+ const noteLHead = noteL.note.sortedKeyProps[noteL.note.sortedKeyProps.length - 1].keyProps.code;
133
+ if (!Tables.UNISON ||
134
+ noteUHead !== noteLHead ||
135
+ noteU.note.getModifiers().filter((item) => item.getCategory() === "Dot" && item.getIndex() === 0)
136
+ .length !==
137
+ noteL.note.getModifiers().filter((item) => item.getCategory() === "Dot" && item.getIndex() === 0)
138
+ .length ||
139
+ (lineDiff < 1 && lineDiff > 0) ||
140
+ JSON.stringify(noteU.note.getStyle()) !== JSON.stringify(noteL.note.getStyle())) {
141
+ xShift = voiceXShift + 2;
142
+ if (noteU.stemDirection === noteL.stemDirection) {
143
+ noteU.note.setXShift(xShift);
144
+ }
145
+ else {
146
+ noteL.note.setXShift(xShift);
147
+ }
148
+ }
149
+ else if (noteU.note.voice !== noteL.note.voice) {
150
+ if (noteU.stemDirection === noteL.stemDirection) {
151
+ if (noteU.line !== noteL.line) {
152
+ xShift = voiceXShift + 2;
153
+ noteU.note.setXShift(xShift);
154
+ }
155
+ else {
156
+ if (noteL.stemDirection === 1) {
157
+ noteL.stemDirection = -1;
158
+ noteL.note.setStemDirection(-1);
159
+ }
160
+ }
161
+ }
162
+ }
163
+ }
164
+ else if (lineDiff < 1) {
165
+ xShift = voiceXShift + 2;
166
+ if (noteU.note.duration < noteL.note.duration) {
167
+ noteU.note.setXShift(xShift);
168
+ }
169
+ else {
170
+ noteL.note.setXShift(xShift);
171
+ }
172
+ }
173
+ else if (noteU.note.hasStem()) {
174
+ noteU.stemDirection = -noteU.note.getStemDirection();
175
+ noteU.note.setStemDirection(noteU.stemDirection);
176
+ }
177
+ else if (noteL.note.hasStem()) {
178
+ noteL.stemDirection = -noteL.note.getStemDirection();
179
+ noteL.note.setStemDirection(noteL.stemDirection);
180
+ }
181
+ }
182
+ }
183
+ state.rightShift += xShift;
184
+ return true;
185
+ }
186
+ if (!noteM)
187
+ throw new RuntimeError('InvalidState', 'noteM not defined.');
188
+ if (noteM.isrest && !noteU.isrest && !noteL.isrest) {
189
+ if (noteU.minLine <= noteM.maxLine || noteM.minLine <= noteL.maxLine) {
190
+ const restHeight = noteM.maxLine - noteM.minLine;
191
+ const space = noteU.minLine - noteL.maxLine;
192
+ if (restHeight < space) {
193
+ centerRest(noteM, noteU, noteL);
194
+ }
195
+ else {
196
+ xShift = voiceXShift + 2;
197
+ noteM.note.setXShift(xShift);
198
+ if (noteL.note.hasBeam() === false) {
199
+ noteL.stemDirection = -1;
200
+ noteL.note.setStemDirection(-1);
201
+ }
202
+ if (noteU.minLine <= noteL.maxLine && noteU.note.hasBeam() === false) {
203
+ noteU.stemDirection = 1;
204
+ noteU.note.setStemDirection(1);
205
+ }
206
+ }
207
+ state.rightShift += xShift;
208
+ return true;
209
+ }
210
+ }
211
+ if (noteU.isrest && noteM.isrest && noteL.isrest) {
212
+ noteU.note.renderOptions.draw = false;
213
+ noteL.note.renderOptions.draw = false;
214
+ state.rightShift += xShift;
215
+ return true;
216
+ }
217
+ if (noteM.isrest && noteU.isrest && noteM.minLine <= noteL.maxLine) {
218
+ noteM.note.renderOptions.draw = false;
219
+ }
220
+ if (noteM.isrest && noteL.isrest && noteU.minLine <= noteM.maxLine) {
221
+ noteM.note.renderOptions.draw = false;
222
+ }
223
+ if (noteU.isrest && noteU.minLine <= noteM.maxLine) {
224
+ shiftRestVertical(noteU, noteM, 1);
225
+ }
226
+ if (noteL.isrest && noteM.minLine <= noteL.maxLine) {
227
+ shiftRestVertical(noteL, noteM, -1);
228
+ }
229
+ if (noteU.minLine <= noteM.maxLine + 0.5 || noteM.minLine <= noteL.maxLine) {
230
+ xShift = voiceXShift + 2;
231
+ noteM.note.setXShift(xShift);
232
+ if (noteL.note.hasBeam() === false) {
233
+ noteL.stemDirection = -1;
234
+ noteL.note.setStemDirection(-1);
235
+ }
236
+ if (noteU.minLine <= noteL.maxLine && noteU.note.hasBeam() === false) {
237
+ noteU.stemDirection = 1;
238
+ noteU.note.setStemDirection(1);
239
+ }
240
+ }
241
+ state.rightShift += xShift;
242
+ return true;
243
+ }
244
+ static postFormat(notes) {
245
+ if (!notes)
246
+ return false;
247
+ notes.forEach((note) => note.postFormat());
248
+ return true;
249
+ }
250
+ constructor(noteStruct) {
251
+ var _a, _b, _c;
252
+ super(noteStruct);
253
+ this.minLine = 0;
254
+ this.maxLine = 0;
255
+ this.sortedKeyProps = [];
256
+ this.ledgerLineStyle = {};
257
+ this.clef = (_a = noteStruct.clef) !== null && _a !== void 0 ? _a : 'treble';
258
+ this.octaveShift = (_b = noteStruct.octaveShift) !== null && _b !== void 0 ? _b : 0;
259
+ this.glyphProps = Note.getGlyphProps(this.duration, this.noteType);
260
+ defined(this.glyphProps, 'BadArguments', `No glyph found for duration '${this.duration}' and type '${this.noteType}'`);
261
+ this.displaced = false;
262
+ this.dotShiftY = 0;
263
+ this.useDefaultHeadX = false;
264
+ this._noteHeads = [];
265
+ this.modifiers = [];
266
+ this.renderOptions = Object.assign(Object.assign({}, this.renderOptions), { strokePx: noteStruct.strokePx || StaveNote.LEDGER_LINE_OFFSET });
267
+ this.calculateKeyProps();
268
+ this.buildStem();
269
+ if (noteStruct.autoStem) {
270
+ this.autoStem();
271
+ }
272
+ else {
273
+ this.setStemDirection((_c = noteStruct.stemDirection) !== null && _c !== void 0 ? _c : Stem.UP);
274
+ }
275
+ this.reset();
276
+ this.buildFlag();
277
+ }
278
+ reset() {
279
+ super.reset();
280
+ const noteHeadStyles = this._noteHeads.map((noteHead) => noteHead.getStyle());
281
+ this.buildNoteHeads();
282
+ this._noteHeads.forEach((noteHead, index) => {
283
+ const noteHeadStyle = noteHeadStyles[index];
284
+ if (noteHeadStyle)
285
+ noteHead.setStyle(noteHeadStyle);
286
+ });
287
+ const stave = this.stave;
288
+ if (stave) {
289
+ this.setStave(stave);
290
+ }
291
+ this.calcNoteDisplacements();
292
+ return this;
293
+ }
294
+ setBeam(beam) {
295
+ this.beam = beam;
296
+ this.calcNoteDisplacements();
297
+ if (this.stem) {
298
+ this.stem.setExtension(this.getStemExtension());
299
+ }
300
+ return this;
301
+ }
302
+ buildStem() {
303
+ this.setStem(new Stem({ hide: this.isRest() }));
304
+ return this;
305
+ }
306
+ buildNoteHeads() {
307
+ this._noteHeads = [];
308
+ const stemDirection = this.getStemDirection();
309
+ const keys = this.getKeys();
310
+ let lastLine = undefined;
311
+ let lineDiff = undefined;
312
+ let displaced = false;
313
+ let start;
314
+ let end;
315
+ let step;
316
+ if (stemDirection === Stem.UP) {
317
+ start = 0;
318
+ end = keys.length;
319
+ step = 1;
320
+ }
321
+ else {
322
+ start = keys.length - 1;
323
+ end = -1;
324
+ step = -1;
325
+ }
326
+ for (let i = start; i !== end; i += step) {
327
+ const noteProps = this.sortedKeyProps[i].keyProps;
328
+ const line = noteProps.line;
329
+ if (lastLine === undefined) {
330
+ lastLine = line;
331
+ }
332
+ else {
333
+ lineDiff = Math.abs(lastLine - line);
334
+ if (lineDiff === 0 || lineDiff === 0.5) {
335
+ displaced = !displaced;
336
+ }
337
+ else {
338
+ displaced = false;
339
+ this.useDefaultHeadX = true;
340
+ }
341
+ }
342
+ lastLine = line;
343
+ const notehead = new NoteHead({
344
+ duration: this.duration,
345
+ noteType: this.noteType,
346
+ displaced,
347
+ stemDirection,
348
+ customGlyphCode: noteProps.code,
349
+ line: noteProps.line,
350
+ });
351
+ notehead.fontInfo = this.fontInfo;
352
+ this.addChild(notehead);
353
+ this._noteHeads[this.sortedKeyProps[i].index] = notehead;
354
+ }
355
+ return this._noteHeads;
356
+ }
357
+ autoStem() {
358
+ this.setStemDirection(this.calculateOptimalStemDirection());
359
+ }
360
+ calculateOptimalStemDirection() {
361
+ this.minLine = this.sortedKeyProps[0].keyProps.line;
362
+ this.maxLine = this.sortedKeyProps[this.keyProps.length - 1].keyProps.line;
363
+ const MIDDLE_LINE = 3;
364
+ const decider = (this.minLine + this.maxLine) / 2;
365
+ const stemDirection = decider < MIDDLE_LINE ? Stem.UP : Stem.DOWN;
366
+ return stemDirection;
367
+ }
368
+ calculateKeyProps() {
369
+ var _a;
370
+ let lastLine;
371
+ for (let i = 0; i < this.keys.length; ++i) {
372
+ const key = this.keys[i];
373
+ const options = { octaveShift: (_a = this.octaveShift) !== null && _a !== void 0 ? _a : 0, duration: this.duration };
374
+ const props = Tables.keyProperties(key, this.clef, this.noteType, options);
375
+ if (!props) {
376
+ throw new RuntimeError('BadArguments', `Invalid key for note properties: ${key}`);
377
+ }
378
+ if (props.key === 'R') {
379
+ if (this.duration === '1' || this.duration === 'w') {
380
+ props.line = 4;
381
+ }
382
+ else {
383
+ props.line = 3;
384
+ }
385
+ }
386
+ const line = props.line;
387
+ if (lastLine === undefined) {
388
+ lastLine = line;
389
+ }
390
+ else {
391
+ if (Math.abs(lastLine - line) < 1) {
392
+ this.displaced = true;
393
+ props.displaced = true;
394
+ if (this.keyProps.length > 0) {
395
+ this.keyProps[i - 1].displaced = true;
396
+ }
397
+ }
398
+ }
399
+ lastLine = line;
400
+ this.keyProps.push(props);
401
+ }
402
+ this.keyProps.forEach((keyProps, index) => {
403
+ this.sortedKeyProps.push({ keyProps, index });
404
+ });
405
+ this.sortedKeyProps.sort((a, b) => a.keyProps.line - b.keyProps.line);
406
+ }
407
+ getBoundingBox() {
408
+ const boundingBox = new BoundingBox(this.getAbsoluteX(), this.ys[0], 0, 0);
409
+ this._noteHeads.forEach((notehead) => {
410
+ boundingBox.mergeWith(notehead.getBoundingBox());
411
+ });
412
+ const { yTop, yBottom } = this.getNoteHeadBounds();
413
+ if (!this.isRest() && this.hasStem()) {
414
+ const noteStemHeight = this.stem.getHeight();
415
+ const stemY = this.getStemDirection() === Stem.DOWN
416
+ ? yTop - noteStemHeight - this.flag.getTextMetrics().actualBoundingBoxDescent
417
+ : yBottom - noteStemHeight + this.flag.getTextMetrics().actualBoundingBoxAscent;
418
+ boundingBox.mergeWith(new BoundingBox(this.getAbsoluteX(), stemY, 0, 0));
419
+ }
420
+ if (this.hasFlag()) {
421
+ const bbFlag = this.flag.getBoundingBox();
422
+ boundingBox.mergeWith(bbFlag);
423
+ }
424
+ for (let i = 0; i < this.modifiers.length; i++) {
425
+ boundingBox.mergeWith(this.modifiers[i].getBoundingBox());
426
+ }
427
+ return boundingBox;
428
+ }
429
+ getLineNumber(isTopNote) {
430
+ if (!this.keyProps.length) {
431
+ throw new RuntimeError('NoKeyProps', "Can't get bottom note line, because note is not initialized properly.");
432
+ }
433
+ let resultLine = this.keyProps[0].line;
434
+ for (let i = 0; i < this.keyProps.length; i++) {
435
+ const thisLine = this.keyProps[i].line;
436
+ if (isTopNote) {
437
+ if (thisLine > resultLine)
438
+ resultLine = thisLine;
439
+ }
440
+ else {
441
+ if (thisLine < resultLine)
442
+ resultLine = thisLine;
443
+ }
444
+ }
445
+ return resultLine;
446
+ }
447
+ isRest() {
448
+ const val = this.glyphProps.codeHead;
449
+ return val >= '\ue4e0' && val <= '\ue4ff';
450
+ }
451
+ isChord() {
452
+ return !this.isRest() && this.keys.length > 1;
453
+ }
454
+ hasStem() {
455
+ return this.glyphProps.stem;
456
+ }
457
+ hasFlag() {
458
+ return super.hasFlag() && !this.isRest();
459
+ }
460
+ getStemX() {
461
+ if (this.noteType === 'r') {
462
+ return this.getCenterGlyphX();
463
+ }
464
+ else {
465
+ return super.getStemX() + (this.stemDirection ? Stem.WIDTH / (2 * -this.stemDirection) : 0);
466
+ }
467
+ }
468
+ getYForTopText(textLine) {
469
+ const extents = this.getStemExtents();
470
+ return Math.min(this.checkStave().getYForTopText(textLine), extents.topY - this.renderOptions.annotationSpacing * (textLine + 1));
471
+ }
472
+ getYForBottomText(textLine) {
473
+ const extents = this.getStemExtents();
474
+ return Math.max(this.checkStave().getYForTopText(textLine), extents.baseY + this.renderOptions.annotationSpacing * textLine);
475
+ }
476
+ setStave(stave) {
477
+ super.setStave(stave);
478
+ const ys = this._noteHeads.map((notehead) => {
479
+ notehead.setStave(stave);
480
+ return notehead.getY();
481
+ });
482
+ this.setYs(ys);
483
+ if (this.stem) {
484
+ const { yTop, yBottom } = this.getStemYBounds();
485
+ this.stem.setYBounds(yTop, yBottom);
486
+ }
487
+ return this;
488
+ }
489
+ isDisplaced() {
490
+ return this.displaced;
491
+ }
492
+ setNoteDisplaced(displaced) {
493
+ this.displaced = displaced;
494
+ return this;
495
+ }
496
+ getTieRightX() {
497
+ let tieStartX = this.getAbsoluteX();
498
+ tieStartX += this.getGlyphWidth() + this.xShift + this.rightDisplacedHeadPx;
499
+ if (this.modifierContext)
500
+ tieStartX += this.modifierContext.getRightShift();
501
+ return tieStartX;
502
+ }
503
+ getTieLeftX() {
504
+ let tieEndX = this.getAbsoluteX();
505
+ tieEndX += this.xShift - this.leftDisplacedHeadPx;
506
+ return tieEndX;
507
+ }
508
+ getLineForRest() {
509
+ let restLine = this.keyProps[0].line;
510
+ if (this.keyProps.length > 1) {
511
+ const lastLine = this.keyProps[this.keyProps.length - 1].line;
512
+ const top = Math.max(restLine, lastLine);
513
+ const bot = Math.min(restLine, lastLine);
514
+ restLine = midLine(top, bot);
515
+ }
516
+ return restLine;
517
+ }
518
+ getModifierStartXY(position, index, options = {}) {
519
+ if (!this.preFormatted) {
520
+ throw new RuntimeError('UnformattedNote', "Can't call GetModifierStartXY on an unformatted note");
521
+ }
522
+ if (this.ys.length === 0) {
523
+ throw new RuntimeError('NoYValues', 'No Y-Values calculated for this note.');
524
+ }
525
+ const { ABOVE, BELOW, LEFT, RIGHT } = Modifier.Position;
526
+ let x = 0;
527
+ if (position === LEFT) {
528
+ x = -1 * 2;
529
+ }
530
+ else if (position === RIGHT) {
531
+ x = this.getGlyphWidth() + this.xShift + 2;
532
+ if (this.stemDirection === Stem.UP &&
533
+ this.hasFlag() &&
534
+ (options.forceFlagRight || isInnerNoteIndex(this, index))) {
535
+ x += this.flag.getWidth();
536
+ }
537
+ }
538
+ else if (position === BELOW || position === ABOVE) {
539
+ x = this.getGlyphWidth() / 2;
540
+ }
541
+ let restShift = 0;
542
+ switch (this._noteHeads[index].getText()) {
543
+ case Glyphs.restWhole:
544
+ restShift += 0.5;
545
+ break;
546
+ case Glyphs.restHalf:
547
+ case Glyphs.restQuarter:
548
+ case Glyphs.rest8th:
549
+ case Glyphs.rest16th:
550
+ restShift -= 0.5;
551
+ break;
552
+ case Glyphs.rest32nd:
553
+ case Glyphs.rest64th:
554
+ restShift -= 1.5;
555
+ break;
556
+ case Glyphs.rest128th:
557
+ case Glyphs.rest256th:
558
+ restShift -= 2.5;
559
+ break;
560
+ case Glyphs.rest512th:
561
+ restShift -= 3.5;
562
+ break;
563
+ case Glyphs.rest1024th:
564
+ restShift -= 4.5;
565
+ break;
566
+ }
567
+ return {
568
+ x: this.getAbsoluteX() + x,
569
+ y: this.ys[index] + restShift * this.checkStave().getSpacingBetweenLines(),
570
+ };
571
+ }
572
+ setStyle(style) {
573
+ return super.setGroupStyle(style);
574
+ }
575
+ setStemStyle(style) {
576
+ const stem = this.getStem();
577
+ if (stem)
578
+ stem.setStyle(style);
579
+ return this;
580
+ }
581
+ getStemStyle() {
582
+ var _a;
583
+ return (_a = this.stem) === null || _a === void 0 ? void 0 : _a.getStyle();
584
+ }
585
+ setLedgerLineStyle(style) {
586
+ this.ledgerLineStyle = style;
587
+ }
588
+ getLedgerLineStyle() {
589
+ return this.ledgerLineStyle;
590
+ }
591
+ setFlagStyle(style) {
592
+ this.flag.setStyle(style);
593
+ }
594
+ getFlagStyle() {
595
+ return this.flag.getStyle();
596
+ }
597
+ getGlyphWidth() {
598
+ return this.noteHeads[0].getWidth();
599
+ }
600
+ setKeyStyle(index, style) {
601
+ this._noteHeads[index].setStyle(style);
602
+ return this;
603
+ }
604
+ setKeyLine(index, line) {
605
+ this.keyProps[index].line = line;
606
+ this.reset();
607
+ return this;
608
+ }
609
+ getKeyLine(index) {
610
+ return this.keyProps[index].line;
611
+ }
612
+ getVoiceShiftWidth() {
613
+ return this.getGlyphWidth() * (this.displaced ? 2 : 1);
614
+ }
615
+ calcNoteDisplacements() {
616
+ this.setLeftDisplacedHeadPx(this.displaced && this.stemDirection === Stem.DOWN ? this.getGlyphWidth() : 0);
617
+ this.setRightDisplacedHeadPx(!this.hasFlag() && this.displaced && this.stemDirection === Stem.UP ? this.getGlyphWidth() : 0);
618
+ }
619
+ preFormat() {
620
+ if (this.preFormatted)
621
+ return;
622
+ let noteHeadPadding = 0;
623
+ if (this.modifierContext) {
624
+ this.modifierContext.preFormat();
625
+ if (this.modifierContext.getWidth() === 0) {
626
+ noteHeadPadding = StaveNote.minNoteheadPadding;
627
+ }
628
+ }
629
+ let width = this.getGlyphWidth() + this.leftDisplacedHeadPx + this.rightDisplacedHeadPx + noteHeadPadding;
630
+ if (this.shouldDrawFlag() && this.stemDirection === Stem.UP) {
631
+ width += this.getGlyphWidth();
632
+ }
633
+ this.setWidth(width);
634
+ this.preFormatted = true;
635
+ }
636
+ getStemYBounds() {
637
+ const anchorKey = this.stemDirection === Stem.UP ? 'up' : 'down';
638
+ let yTop = +Infinity;
639
+ let yBottom = -Infinity;
640
+ this._noteHeads.forEach((nh) => {
641
+ const offset = Tables.noteHeadStemYOffsets[nh.text];
642
+ const anchor = offset ? offset[anchorKey] : 0;
643
+ const y = nh.getY() - anchor * Tables.STAVE_LINE_DISTANCE;
644
+ yTop = Math.min(y, yTop);
645
+ yBottom = Math.max(y, yBottom);
646
+ });
647
+ if (!isFinite(yTop) || !isFinite(yBottom)) {
648
+ const bounds = this.getNoteHeadBounds();
649
+ return { yTop: bounds.yTop, yBottom: bounds.yBottom };
650
+ }
651
+ return { yTop, yBottom };
652
+ }
653
+ getNoteHeadBounds() {
654
+ let yTop = +Infinity;
655
+ let yBottom = -Infinity;
656
+ let nonDisplacedX;
657
+ let displacedX;
658
+ let highestLine = this.checkStave().getNumLines();
659
+ let lowestLine = 1;
660
+ let highestDisplacedLine;
661
+ let lowestDisplacedLine;
662
+ let highestNonDisplacedLine = highestLine;
663
+ let lowestNonDisplacedLine = lowestLine;
664
+ this._noteHeads.forEach((notehead) => {
665
+ const line = notehead.getLine();
666
+ const y = notehead.getY();
667
+ yTop = Math.min(y, yTop);
668
+ yBottom = Math.max(y, yBottom);
669
+ if (displacedX === undefined && notehead.isDisplaced()) {
670
+ displacedX = notehead.getAbsoluteX();
671
+ }
672
+ if (nonDisplacedX === undefined && !notehead.isDisplaced()) {
673
+ nonDisplacedX = notehead.getAbsoluteX();
674
+ }
675
+ highestLine = Math.max(line, highestLine);
676
+ lowestLine = Math.min(line, lowestLine);
677
+ if (notehead.isDisplaced()) {
678
+ highestDisplacedLine = highestDisplacedLine === undefined ? line : Math.max(line, highestDisplacedLine);
679
+ lowestDisplacedLine = lowestDisplacedLine === undefined ? line : Math.min(line, lowestDisplacedLine);
680
+ }
681
+ else {
682
+ highestNonDisplacedLine = Math.max(line, highestNonDisplacedLine);
683
+ lowestNonDisplacedLine = Math.min(line, lowestNonDisplacedLine);
684
+ }
685
+ }, this);
686
+ return {
687
+ yTop,
688
+ yBottom,
689
+ displacedX,
690
+ nonDisplacedX,
691
+ highestLine,
692
+ lowestLine,
693
+ highestDisplacedLine,
694
+ lowestDisplacedLine,
695
+ highestNonDisplacedLine,
696
+ lowestNonDisplacedLine,
697
+ };
698
+ }
699
+ getNoteHeadBeginX() {
700
+ return this.getAbsoluteX() + this.xShift;
701
+ }
702
+ getNoteHeadEndX() {
703
+ const xBegin = this.getNoteHeadBeginX();
704
+ return xBegin + this.getGlyphWidth();
705
+ }
706
+ get noteHeads() {
707
+ return this._noteHeads.slice();
708
+ }
709
+ drawLedgerLines() {
710
+ const stave = this.checkStave();
711
+ const { renderOptions: { strokePx }, } = this;
712
+ const ctx = this.checkContext();
713
+ const width = this.getGlyphWidth() + strokePx * 2;
714
+ const doubleWidth = 2 * (this.getGlyphWidth() + strokePx) - Stem.WIDTH / 2;
715
+ if (this.isRest())
716
+ return;
717
+ if (!ctx) {
718
+ throw new RuntimeError('NoCanvasContext', "Can't draw without a canvas context.");
719
+ }
720
+ const { highestLine, lowestLine, highestDisplacedLine, highestNonDisplacedLine, lowestDisplacedLine, lowestNonDisplacedLine, displacedX, nonDisplacedX, } = this.getNoteHeadBounds();
721
+ if (highestLine < 6 && lowestLine > 0)
722
+ return;
723
+ const minX = Math.min(displacedX !== null && displacedX !== void 0 ? displacedX : 0, nonDisplacedX !== null && nonDisplacedX !== void 0 ? nonDisplacedX : 0);
724
+ const drawLedgerLine = (y, normal, displaced) => {
725
+ let x;
726
+ if (displaced && normal)
727
+ x = minX - strokePx;
728
+ else if (normal)
729
+ x = (nonDisplacedX !== null && nonDisplacedX !== void 0 ? nonDisplacedX : 0) - strokePx;
730
+ else
731
+ x = (displacedX !== null && displacedX !== void 0 ? displacedX : 0) - strokePx;
732
+ const ledgerWidth = normal && displaced ? doubleWidth : width;
733
+ ctx.beginPath();
734
+ ctx.moveTo(x, y);
735
+ ctx.lineTo(x + ledgerWidth, y);
736
+ ctx.stroke();
737
+ };
738
+ const style = Object.assign(Object.assign({}, stave.getDefaultLedgerLineStyle()), this.getLedgerLineStyle());
739
+ ctx.save();
740
+ this.applyStyle(ctx, style);
741
+ for (let line = 6; line <= highestLine; ++line) {
742
+ const normal = nonDisplacedX !== undefined && line <= highestNonDisplacedLine;
743
+ const displaced = highestDisplacedLine !== undefined && line <= highestDisplacedLine;
744
+ drawLedgerLine(stave.getYForNote(line), normal, displaced);
745
+ }
746
+ for (let line = 0; line >= lowestLine; --line) {
747
+ const normal = nonDisplacedX !== undefined && line >= lowestNonDisplacedLine;
748
+ const displaced = lowestDisplacedLine !== undefined && line >= lowestDisplacedLine;
749
+ drawLedgerLine(stave.getYForNote(line), normal, displaced);
750
+ }
751
+ ctx.restore();
752
+ }
753
+ drawModifiers(noteheadParam) {
754
+ const ctx = this.checkContext();
755
+ for (let i = 0; i < this.modifiers.length; i++) {
756
+ const modifier = this.modifiers[i];
757
+ const index = modifier.checkIndex();
758
+ const notehead = this._noteHeads[index];
759
+ if (notehead === noteheadParam) {
760
+ modifier.setContext(ctx);
761
+ modifier.drawWithStyle();
762
+ }
763
+ }
764
+ }
765
+ shouldDrawFlag() {
766
+ const hasStem = this.stem !== undefined;
767
+ const hasFlag = this.glyphProps.codeFlagUp !== undefined;
768
+ const hasNoBeam = this.beam === undefined;
769
+ return hasStem && hasFlag && hasNoBeam && !this.isRest();
770
+ }
771
+ drawFlag() {
772
+ const ctx = this.checkContext();
773
+ if (!ctx) {
774
+ throw new RuntimeError('NoCanvasContext', "Can't draw without a canvas context.");
775
+ }
776
+ if (this.shouldDrawFlag()) {
777
+ const { yTop, yBottom } = this.getNoteHeadBounds();
778
+ const noteStemHeight = this.stem.getHeight();
779
+ const flagX = this.getStemX() - Tables.STEM_WIDTH / 2;
780
+ const flagY = this.getStemDirection() === Stem.DOWN
781
+ ?
782
+ yTop - noteStemHeight - this.flag.getTextMetrics().actualBoundingBoxDescent
783
+ :
784
+ yBottom - noteStemHeight + this.flag.getTextMetrics().actualBoundingBoxAscent;
785
+ this.flag.setContext(ctx).setX(flagX).setY(flagY).drawWithStyle();
786
+ }
787
+ }
788
+ drawNoteHeads() {
789
+ const ctx = this.checkContext();
790
+ this._noteHeads.forEach((notehead) => {
791
+ notehead.setContext(ctx).drawWithStyle();
792
+ });
793
+ }
794
+ drawStem(stemOptions) {
795
+ const ctx = this.checkContext();
796
+ if (stemOptions) {
797
+ this.setStem(new Stem(stemOptions));
798
+ }
799
+ if (this.shouldDrawFlag() && this.stem) {
800
+ this.stem.adjustHeightForFlag();
801
+ }
802
+ if (this.stem) {
803
+ this.stem.setContext(ctx).drawWithStyle();
804
+ }
805
+ }
806
+ getStemExtension() {
807
+ const superStemExtension = super.getStemExtension();
808
+ if (!this.glyphProps.stem) {
809
+ return superStemExtension;
810
+ }
811
+ const stemDirection = this.getStemDirection();
812
+ if (stemDirection !== this.calculateOptimalStemDirection()) {
813
+ return superStemExtension;
814
+ }
815
+ let midLineDistance;
816
+ const MIDDLE_LINE = 3;
817
+ if (stemDirection === Stem.UP) {
818
+ midLineDistance = MIDDLE_LINE - this.maxLine;
819
+ }
820
+ else {
821
+ midLineDistance = this.minLine - MIDDLE_LINE;
822
+ }
823
+ const linesOverOctaveFromMidLine = midLineDistance - 3.5;
824
+ if (linesOverOctaveFromMidLine <= 0) {
825
+ return superStemExtension;
826
+ }
827
+ const stave = this.getStave();
828
+ let spacingBetweenLines = 10;
829
+ if (stave !== undefined) {
830
+ spacingBetweenLines = stave.getSpacingBetweenLines();
831
+ }
832
+ return superStemExtension + linesOverOctaveFromMidLine * spacingBetweenLines;
833
+ }
834
+ draw() {
835
+ if (this.renderOptions.draw === false)
836
+ return;
837
+ if (this.ys.length === 0) {
838
+ throw new RuntimeError('NoYValues', "Can't draw note without Y values.");
839
+ }
840
+ const ctx = this.checkContext();
841
+ const xBegin = this.getNoteHeadBeginX();
842
+ const shouldRenderStem = this.hasStem() && !this.beam;
843
+ this._noteHeads.forEach((notehead) => notehead.setX(xBegin));
844
+ if (this.stem) {
845
+ const stemX = this.getStemX();
846
+ this.stem.setNoteHeadXBounds(stemX, stemX);
847
+ }
848
+ L('Rendering ', this.isChord() ? 'chord :' : 'note :', this.keys);
849
+ const clsAttribute = this.getAttribute('class');
850
+ ctx.openGroup('stavenote' + (clsAttribute ? ' ' + clsAttribute : ''), this.getAttribute('id'));
851
+ this.drawLedgerLines();
852
+ if (shouldRenderStem)
853
+ this.drawStem();
854
+ this.drawNoteHeads();
855
+ this.drawFlag();
856
+ this.drawPointerRect();
857
+ ctx.closeGroup();
858
+ this.setRendered();
859
+ }
860
+ }
861
+ StaveNote.DEBUG = false;