abcjs 6.1.8 → 6.2.0

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 (132) hide show
  1. package/RELEASE.md +66 -0
  2. package/dist/abcjs-basic-min.js +2 -2
  3. package/dist/abcjs-basic.js +4373 -4424
  4. package/dist/abcjs-basic.js.map +1 -1
  5. package/dist/abcjs-plugin-min.js +2 -2
  6. package/index.js +1 -1
  7. package/package.json +6 -6
  8. package/plugin.js +1 -1
  9. package/src/api/abc_timing_callbacks.js +7 -3
  10. package/src/api/abc_tunebook_svg.js +1 -2
  11. package/src/data/abc_tune.js +2 -2
  12. package/src/midi/abc_midi_create.js +8 -2
  13. package/src/parse/abc_common.js +0 -47
  14. package/src/parse/abc_parse.js +16 -16
  15. package/src/parse/abc_parse_book.js +3 -3
  16. package/src/parse/abc_parse_directive.js +26 -7
  17. package/src/parse/abc_parse_header.js +11 -9
  18. package/src/parse/abc_parse_key_voice.js +17 -17
  19. package/src/parse/abc_parse_music.js +89 -106
  20. package/src/parse/abc_tokenizer.js +60 -60
  21. package/src/parse/abc_transpose.js +14 -3
  22. package/src/parse/tune-builder.js +19 -14
  23. package/src/synth/abc_midi_flattener.js +25 -9
  24. package/src/synth/abc_midi_sequencer.js +1 -1
  25. package/src/synth/create-synth.js +41 -0
  26. package/src/synth/note-to-midi.js +50 -0
  27. package/src/tablatures/instruments/guitar/tab-guitar.js +0 -2
  28. package/src/tablatures/instruments/string-patterns.js +46 -28
  29. package/src/tablatures/instruments/tab-note.js +26 -103
  30. package/src/tablatures/instruments/violin/tab-violin.js +0 -2
  31. package/src/tablatures/tab-absolute-elements.js +9 -31
  32. package/src/tablatures/tab-renderer.js +2 -2
  33. package/src/test/abc_parser_lint.js +8 -5
  34. package/src/write/README.md +31 -0
  35. package/src/write/creation/abstract-engraver.js +1036 -0
  36. package/src/write/creation/add-chord.js +102 -0
  37. package/src/write/{add-text-if.js → creation/add-text-if.js} +6 -6
  38. package/src/write/{calcHeight.js → creation/calc-height.js} +2 -2
  39. package/src/write/creation/create-clef.js +72 -0
  40. package/src/write/creation/create-key-signature.js +31 -0
  41. package/src/write/creation/create-note-head.js +107 -0
  42. package/src/write/creation/create-time-signature.js +55 -0
  43. package/src/write/creation/decoration.js +357 -0
  44. package/src/write/{abc_absolute_element.js → creation/elements/absolute-element.js} +14 -15
  45. package/src/write/creation/elements/beam-element.js +113 -0
  46. package/src/write/{bottom-text.js → creation/elements/bottom-text.js} +14 -15
  47. package/src/write/{abc_brace_element.js → creation/elements/brace-element.js} +5 -5
  48. package/src/write/creation/elements/free-text.js +41 -0
  49. package/src/write/{abc_relative_element.js → creation/elements/relative-element.js} +8 -7
  50. package/src/write/{separator.js → creation/elements/separator.js} +2 -2
  51. package/src/write/{abc_staff_group_element.js → creation/elements/staff-group-element.js} +4 -4
  52. package/src/write/{subtitle.js → creation/elements/subtitle.js} +3 -3
  53. package/src/write/creation/elements/tempo-element.js +63 -0
  54. package/src/write/{abc_tie_element.js → creation/elements/tie-element.js} +15 -11
  55. package/src/write/{top-text.js → creation/elements/top-text.js} +12 -12
  56. package/src/write/creation/elements/triplet-element.js +28 -0
  57. package/src/write/{abc_voice_element.js → creation/elements/voice-element.js} +3 -3
  58. package/src/write/creation/glyphs.js +226 -0
  59. package/src/write/creation/translate-chord.js +37 -0
  60. package/src/write/draw/absolute.js +5 -5
  61. package/src/write/draw/beam.js +8 -8
  62. package/src/write/draw/brace.js +33 -33
  63. package/src/write/draw/crescendo.js +4 -4
  64. package/src/write/draw/debug-box.js +1 -1
  65. package/src/write/draw/draw.js +7 -7
  66. package/src/write/draw/dynamics.js +2 -2
  67. package/src/write/draw/ending.js +6 -6
  68. package/src/write/draw/glissando.js +17 -17
  69. package/src/write/draw/group-elements.js +51 -51
  70. package/src/write/draw/horizontal-line.js +9 -9
  71. package/src/write/draw/non-music.js +1 -1
  72. package/src/write/draw/print-line.js +15 -16
  73. package/src/write/draw/print-stem.js +5 -5
  74. package/src/write/draw/print-symbol.js +12 -12
  75. package/src/write/draw/print-vertical-line.js +8 -8
  76. package/src/write/draw/relative.js +17 -16
  77. package/src/write/draw/selectables.js +5 -5
  78. package/src/write/draw/separator.js +4 -4
  79. package/src/write/draw/set-paper-size.js +2 -2
  80. package/src/write/draw/sprintf.js +31 -31
  81. package/src/write/draw/staff-group.js +36 -30
  82. package/src/write/draw/staff-line.js +2 -2
  83. package/src/write/draw/staff.js +4 -4
  84. package/src/write/draw/tab-line.js +26 -26
  85. package/src/write/draw/tempo.js +30 -30
  86. package/src/write/draw/text.js +5 -5
  87. package/src/write/draw/tie.js +18 -18
  88. package/src/write/draw/triplet.js +6 -6
  89. package/src/write/draw/voice.js +16 -17
  90. package/src/write/{abc_engraver_controller.js → engraver-controller.js} +58 -51
  91. package/src/write/{classes.js → helpers/classes.js} +6 -6
  92. package/src/write/{get-font-and-attr.js → helpers/get-font-and-attr.js} +9 -7
  93. package/src/write/{get-text-size.js → helpers/get-text-size.js} +5 -5
  94. package/src/write/{set-class.js → helpers/set-class.js} +1 -1
  95. package/src/write/{abc_spacing.js → helpers/spacing.js} +1 -1
  96. package/src/write/{highlight.js → interactive/highlight.js} +1 -1
  97. package/src/write/{selection.js → interactive/selection.js} +27 -27
  98. package/src/write/{unhighlight.js → interactive/unhighlight.js} +1 -1
  99. package/src/write/layout/beam.js +13 -13
  100. package/src/write/layout/get-left-edge-of-staff.js +4 -4
  101. package/src/write/layout/layout.js +74 -74
  102. package/src/write/layout/{setUpperAndLowerElements.js → set-upper-and-lower-elements.js} +8 -8
  103. package/src/write/layout/{staffGroup.js → staff-group.js} +32 -32
  104. package/src/write/layout/triplet.js +4 -4
  105. package/src/write/layout/{VoiceElements.js → voice-elements.js} +23 -23
  106. package/src/write/layout/voice.js +6 -6
  107. package/src/write/{abc_renderer.js → renderer.js} +35 -32
  108. package/src/write/svg.js +35 -35
  109. package/test.js +1 -1
  110. package/types/index.d.ts +99 -22
  111. package/version.js +1 -1
  112. package/src/tablatures/instruments/guitar/guitar-fonts.js +0 -19
  113. package/src/tablatures/instruments/violin/violin-fonts.js +0 -19
  114. package/src/tablatures/transposer.js +0 -110
  115. package/src/write/abc_abstract_engraver.js +0 -1026
  116. package/src/write/abc_beam_element.js +0 -113
  117. package/src/write/abc_create_clef.js +0 -72
  118. package/src/write/abc_create_key_signature.js +0 -33
  119. package/src/write/abc_create_note_head.js +0 -107
  120. package/src/write/abc_create_time_signature.js +0 -55
  121. package/src/write/abc_decoration.js +0 -341
  122. package/src/write/abc_glyphs.js +0 -224
  123. package/src/write/abc_tempo_element.js +0 -63
  124. package/src/write/abc_triplet_element.js +0 -28
  125. package/src/write/add-chord.js +0 -103
  126. package/src/write/format-jazz-chord.js +0 -15
  127. package/src/write/free-text.js +0 -41
  128. /package/src/write/{abc_crescendo_element.js → creation/elements/crescendo-element.js} +0 -0
  129. /package/src/write/{abc_dynamic_decoration.js → creation/elements/dynamic-decoration.js} +0 -0
  130. /package/src/write/{abc_ending_element.js → creation/elements/ending-element.js} +0 -0
  131. /package/src/write/{abc_glissando_element.js → creation/elements/glissando-element.js} +0 -0
  132. /package/src/write/layout/{getBarYAt.js → get-bar-y-at.js} +0 -0
@@ -0,0 +1,1036 @@
1
+ // abc_abstract_engraver.js: Creates a data structure suitable for printing a line of abc
2
+
3
+ var AbsoluteElement = require('./elements/absolute-element');
4
+ var BeamElem = require('./elements/beam-element');
5
+ var BraceElem = require('./elements/brace-element');
6
+ var createClef = require('./create-clef');
7
+ var createKeySignature = require('./create-key-signature');
8
+ var createNoteHead = require('./create-note-head');
9
+ var createTimeSignature = require('./create-time-signature');
10
+ var Decoration = require('./decoration');
11
+ var EndingElem = require('./elements/ending-element');
12
+ var glyphs = require('./glyphs');
13
+ var RelativeElement = require('./elements/relative-element');
14
+ var spacing = require('../helpers/spacing');
15
+ var StaffGroupElement = require('./elements/staff-group-element');
16
+ var TempoElement = require('./elements/tempo-element');
17
+ var TieElem = require('./elements/tie-element');
18
+ var TripletElem = require('./elements/triplet-element');
19
+ var VoiceElement = require('./elements/voice-element');
20
+ var addChord = require('./add-chord');
21
+ var pitchesToPerc = require('../../synth/pitches-to-perc')
22
+
23
+ var parseCommon = require('../../parse/abc_common');
24
+
25
+ var getDuration = function (elem) {
26
+ var d = 0;
27
+ if (elem.duration) {
28
+ d = elem.duration;
29
+ }
30
+ return d;
31
+ };
32
+
33
+ var hint = false;
34
+
35
+ var chartable = {
36
+ rest: { 0: "rests.whole", 1: "rests.half", 2: "rests.quarter", 3: "rests.8th", 4: "rests.16th", 5: "rests.32nd", 6: "rests.64th", 7: "rests.128th", "multi": "rests.multimeasure" },
37
+ note: { "-1": "noteheads.dbl", 0: "noteheads.whole", 1: "noteheads.half", 2: "noteheads.quarter", 3: "noteheads.quarter", 4: "noteheads.quarter", 5: "noteheads.quarter", 6: "noteheads.quarter", 7: "noteheads.quarter", 'nostem': "noteheads.quarter" },
38
+ rhythm: { "-1": "noteheads.slash.whole", 0: "noteheads.slash.whole", 1: "noteheads.slash.whole", 2: "noteheads.slash.quarter", 3: "noteheads.slash.quarter", 4: "noteheads.slash.quarter", 5: "noteheads.slash.quarter", 6: "noteheads.slash.quarter", 7: "noteheads.slash.quarter", nostem: "noteheads.slash.nostem" },
39
+ x: { "-1": "noteheads.indeterminate", 0: "noteheads.indeterminate", 1: "noteheads.indeterminate", 2: "noteheads.indeterminate", 3: "noteheads.indeterminate", 4: "noteheads.indeterminate", 5: "noteheads.indeterminate", 6: "noteheads.indeterminate", 7: "noteheads.indeterminate", nostem: "noteheads.indeterminate" },
40
+ harmonic: { "-1": "noteheads.harmonic.quarter", 0: "noteheads.harmonic.quarter", 1: "noteheads.harmonic.quarter", 2: "noteheads.harmonic.quarter", 3: "noteheads.harmonic.quarter", 4: "noteheads.harmonic.quarter", 5: "noteheads.harmonic.quarter", 6: "noteheads.harmonic.quarter", 7: "noteheads.harmonic.quarter", nostem: "noteheads.harmonic.quarter" },
41
+ triangle: { "-1": "noteheads.triangle.quarter", 0: "noteheads.triangle.quarter", 1: "noteheads.triangle.quarter", 2: "noteheads.triangle.quarter", 3: "noteheads.triangle.quarter", 4: "noteheads.triangle.quarter", 5: "noteheads.triangle.quarter", 6: "noteheads.triangle.quarter", 7: "noteheads.triangle.quarter", nostem: "noteheads.triangle.quarter" },
42
+ uflags: { 3: "flags.u8th", 4: "flags.u16th", 5: "flags.u32nd", 6: "flags.u64th" },
43
+ dflags: { 3: "flags.d8th", 4: "flags.d16th", 5: "flags.d32nd", 6: "flags.d64th" }
44
+ };
45
+
46
+ var AbstractEngraver = function (getTextSize, tuneNumber, options) {
47
+ this.decoration = new Decoration();
48
+ this.getTextSize = getTextSize;
49
+ this.tuneNumber = tuneNumber;
50
+ this.isBagpipes = options.bagpipes;
51
+ this.flatBeams = options.flatbeams;
52
+ this.graceSlurs = options.graceSlurs;
53
+ this.percmap = options.percmap;
54
+ this.initialClef = options.initialClef
55
+ this.jazzchords = !!options.jazzchords
56
+ this.germanAlphabet = !!options.germanAlphabet
57
+ this.reset();
58
+ };
59
+
60
+ AbstractEngraver.prototype.reset = function () {
61
+ this.slurs = {};
62
+ this.ties = [];
63
+ this.voiceScale = 1;
64
+ this.voiceColor = undefined;
65
+ this.slursbyvoice = {};
66
+ this.tiesbyvoice = {};
67
+ this.endingsbyvoice = {};
68
+ this.scaleByVoice = {};
69
+ this.colorByVoice = {};
70
+ this.tripletmultiplier = 1;
71
+
72
+ this.abcline = undefined;
73
+ this.accidentalSlot = undefined;
74
+ this.accidentalshiftx = undefined;
75
+ this.dotshiftx = undefined;
76
+ this.hasVocals = false;
77
+ this.minY = undefined;
78
+ this.partstartelem = undefined;
79
+ this.startlimitelem = undefined;
80
+ this.stemdir = undefined;
81
+ };
82
+
83
+ AbstractEngraver.prototype.setStemHeight = function (heightInPixels) {
84
+ this.stemHeight = Math.round(heightInPixels * 10 / spacing.STEP) / 10;
85
+ };
86
+
87
+ AbstractEngraver.prototype.getCurrentVoiceId = function (s, v) {
88
+ return "s" + s + "v" + v;
89
+ };
90
+
91
+ AbstractEngraver.prototype.pushCrossLineElems = function (s, v) {
92
+ this.slursbyvoice[this.getCurrentVoiceId(s, v)] = this.slurs;
93
+ this.tiesbyvoice[this.getCurrentVoiceId(s, v)] = this.ties;
94
+ this.endingsbyvoice[this.getCurrentVoiceId(s, v)] = this.partstartelem;
95
+ this.scaleByVoice[this.getCurrentVoiceId(s, v)] = this.voiceScale;
96
+ if (this.voiceColor)
97
+ this.colorByVoice[this.getCurrentVoiceId(s, v)] = this.voiceColor;
98
+ };
99
+
100
+ AbstractEngraver.prototype.popCrossLineElems = function (s, v) {
101
+ this.slurs = this.slursbyvoice[this.getCurrentVoiceId(s, v)] || {};
102
+ this.ties = this.tiesbyvoice[this.getCurrentVoiceId(s, v)] || [];
103
+ this.partstartelem = this.endingsbyvoice[this.getCurrentVoiceId(s, v)];
104
+ this.voiceScale = this.scaleByVoice[this.getCurrentVoiceId(s, v)];
105
+ if (this.voiceScale === undefined) this.voiceScale = 1;
106
+ this.voiceColor = this.colorByVoice[this.getCurrentVoiceId(s, v)];
107
+ };
108
+
109
+ AbstractEngraver.prototype.containsLyrics = function (staves) {
110
+ for (var i = 0; i < staves.length; i++) {
111
+ for (var j = 0; j < staves[i].voices.length; j++) {
112
+ for (var k = 0; k < staves[i].voices[j].length; k++) {
113
+ var el = staves[i].voices[j][k];
114
+ if (el.lyric) {
115
+ // We just want to see if there are vocals below the music to know where to put the dynamics.
116
+ if (!el.positioning || el.positioning.vocalPosition === 'below')
117
+ this.hasVocals = true;
118
+ return;
119
+ }
120
+ }
121
+ }
122
+ }
123
+ };
124
+
125
+ AbstractEngraver.prototype.createABCLine = function (staffs, tempo, l) {
126
+ this.minY = 2; // PER: This will be the lowest that any note reaches. It will be used to set the dynamics row.
127
+ // See if there are any lyrics on this line.
128
+ this.containsLyrics(staffs);
129
+ var staffgroup = new StaffGroupElement(this.getTextSize);
130
+ this.tempoSet = false;
131
+ for (var s = 0; s < staffs.length; s++) {
132
+ if (hint)
133
+ this.restoreState();
134
+ hint = false;
135
+ this.createABCStaff(staffgroup, staffs[s], tempo, s, l);
136
+ }
137
+ return staffgroup;
138
+ };
139
+
140
+ AbstractEngraver.prototype.createABCStaff = function (staffgroup, abcstaff, tempo, s, l) {
141
+ // If the tempo is passed in, then the first element should get the tempo attached to it.
142
+ staffgroup.getTextSize.updateFonts(abcstaff);
143
+ for (var v = 0; v < abcstaff.voices.length; v++) {
144
+ var voice = new VoiceElement(v, abcstaff.voices.length);
145
+ if (v === 0) {
146
+ voice.barfrom = (abcstaff.connectBarLines === "start" || abcstaff.connectBarLines === "continue");
147
+ voice.barto = (abcstaff.connectBarLines === "continue" || abcstaff.connectBarLines === "end");
148
+ } else {
149
+ voice.duplicate = true; // bar lines and other duplicate info need not be created
150
+ }
151
+ if (abcstaff.title && abcstaff.title[v]) {
152
+ voice.header = abcstaff.title[v].replace(/\\n/g, "\n");
153
+ voice.headerPosition = 6 + staffgroup.getTextSize.baselineToCenter(voice.header, "voicefont", 'staff-extra voice-name', v, abcstaff.voices.length) / spacing.STEP;
154
+ }
155
+ if (abcstaff.clef && abcstaff.clef.type === "perc")
156
+ voice.isPercussion = true;
157
+ var clef = (!this.initialClef || l === 0) && createClef(abcstaff.clef, this.tuneNumber);
158
+ if (clef) {
159
+ if (v === 0 && abcstaff.barNumber) {
160
+ this.addMeasureNumber(abcstaff.barNumber, clef);
161
+ }
162
+ voice.addChild(clef);
163
+ this.startlimitelem = clef; // limit ties here
164
+ }
165
+ var keySig = createKeySignature(abcstaff.key, this.tuneNumber);
166
+ if (keySig) {
167
+ voice.addChild(keySig);
168
+ this.startlimitelem = keySig; // limit ties here
169
+ }
170
+ if (abcstaff.meter) {
171
+ if (abcstaff.meter.type === 'specified') {
172
+ this.measureLength = abcstaff.meter.value[0].num / abcstaff.meter.value[0].den;
173
+ } else
174
+ this.measureLength = 1;
175
+ var ts = createTimeSignature(abcstaff.meter, this.tuneNumber);
176
+ voice.addChild(ts);
177
+ this.startlimitelem = ts; // limit ties here
178
+ }
179
+ if (voice.duplicate)
180
+ voice.children = []; // we shouldn't reprint the above if we're reusing the same staff. We just created them to get the right spacing.
181
+ var staffLines = abcstaff.clef.stafflines || abcstaff.clef.stafflines === 0 ? abcstaff.clef.stafflines : 5;
182
+ staffgroup.addVoice(voice, s, staffLines);
183
+ var isSingleLineStaff = staffLines === 1;
184
+ this.createABCVoice(abcstaff.voices[v], tempo, s, v, isSingleLineStaff, voice);
185
+ staffgroup.setStaffLimits(voice);
186
+ if (v === 0) {
187
+ // only do brace and bracket processing on the first voice, otherwise it would be done twice.
188
+ if (abcstaff.brace === "start" || (!staffgroup.brace && abcstaff.brace)) {
189
+ if (!staffgroup.brace)
190
+ staffgroup.brace = [];
191
+ staffgroup.brace.push(new BraceElem(voice, "brace"));
192
+ } else if (abcstaff.brace === "end" && staffgroup.brace) {
193
+ staffgroup.brace[staffgroup.brace.length - 1].setBottomStaff(voice);
194
+ } else if (abcstaff.brace === "continue" && staffgroup.brace) {
195
+ staffgroup.brace[staffgroup.brace.length - 1].continuing(voice);
196
+ }
197
+ if (abcstaff.bracket === "start" || (!staffgroup.bracket && abcstaff.bracket)) {
198
+ if (!staffgroup.bracket)
199
+ staffgroup.bracket = [];
200
+ staffgroup.bracket.push(new BraceElem(voice, "bracket"));
201
+ } else if (abcstaff.bracket === "end" && staffgroup.bracket) {
202
+ staffgroup.bracket[staffgroup.bracket.length - 1].setBottomStaff(voice);
203
+ } else if (abcstaff.bracket === "continue" && staffgroup.bracket) {
204
+ staffgroup.bracket[staffgroup.bracket.length - 1].continuing(voice);
205
+ }
206
+ }
207
+ }
208
+ };
209
+
210
+ function getBeamGroup(abcline, pos) {
211
+ // If there are notes beamed together, they are handled as a group, so find all of them here.
212
+ var elem = abcline[pos];
213
+ if (elem.el_type !== 'note' || !elem.startBeam || elem.endBeam)
214
+ return { count: 1, elem: elem };
215
+
216
+ var group = [];
217
+ while (pos < abcline.length && abcline[pos].el_type === 'note') {
218
+ group.push(abcline[pos]);
219
+ if (abcline[pos].endBeam)
220
+ break;
221
+ pos++;
222
+ }
223
+ return { count: group.length, elem: group };
224
+ }
225
+
226
+ AbstractEngraver.prototype.createABCVoice = function (abcline, tempo, s, v, isSingleLineStaff, voice) {
227
+ this.popCrossLineElems(s, v);
228
+ this.stemdir = (this.isBagpipes) ? "down" : null;
229
+ this.abcline = abcline;
230
+ if (this.partstartelem) {
231
+ this.partstartelem = new EndingElem("", null, null);
232
+ voice.addOther(this.partstartelem);
233
+ }
234
+ var voiceNumber = voice.voicetotal < 2 ? -1 : voice.voicenumber;
235
+ for (var slur in this.slurs) {
236
+ if (this.slurs.hasOwnProperty(slur)) {
237
+ // this is already a slur element, but it was created for the last line, so recreate it.
238
+ this.slurs[slur] = new TieElem({ force: this.slurs[slur].force, voiceNumber: voiceNumber, stemDir: this.slurs[slur].stemDir, style: this.slurs[slur].dotted });
239
+ if (hint) this.slurs[slur].setHint();
240
+ voice.addOther(this.slurs[slur]);
241
+ }
242
+ }
243
+ for (var i = 0; i < this.ties.length; i++) {
244
+ // this is already a tie element, but it was created for the last line, so recreate it.
245
+ this.ties[i] = new TieElem({ force: this.ties[i].force, stemDir: this.ties[i].stemDir, voiceNumber: voiceNumber, style: this.ties[i].dotted });
246
+ if (hint) this.ties[i].setHint();
247
+ voice.addOther(this.ties[i]);
248
+ }
249
+
250
+ for (var j = 0; j < this.abcline.length; j++) {
251
+ setAveragePitch(this.abcline[j]);
252
+ this.minY = Math.min(this.abcline[j].minpitch, this.minY);
253
+ }
254
+
255
+ var isFirstStaff = (s === 0);
256
+ var pos = 0;
257
+ while (pos < this.abcline.length) {
258
+ var ret = getBeamGroup(this.abcline, pos);
259
+ var abselems = this.createABCElement(isFirstStaff, isSingleLineStaff, voice, ret.elem);
260
+ if (abselems) {
261
+ for (i = 0; i < abselems.length; i++) {
262
+ if (!this.tempoSet && tempo && !tempo.suppress) {
263
+ this.tempoSet = true;
264
+ var tempoElement = new AbsoluteElement(tempo, 0, 0, "tempo", this.tuneNumber, {});
265
+ tempoElement.addFixedX(new TempoElement(tempo, this.tuneNumber, createNoteHead));
266
+ voice.addChild(tempoElement);
267
+ }
268
+ voice.addChild(abselems[i]);
269
+ }
270
+ }
271
+ pos += ret.count;
272
+ }
273
+ this.pushCrossLineElems(s, v);
274
+ };
275
+
276
+ AbstractEngraver.prototype.saveState = function () {
277
+ this.tiesSave = parseCommon.cloneArray(this.ties);
278
+ this.slursSave = parseCommon.cloneHashOfHash(this.slurs);
279
+ this.slursbyvoiceSave = parseCommon.cloneHashOfHash(this.slursbyvoice);
280
+ this.tiesbyvoiceSave = parseCommon.cloneHashOfArrayOfHash(this.tiesbyvoice);
281
+ };
282
+
283
+ AbstractEngraver.prototype.restoreState = function () {
284
+ this.ties = parseCommon.cloneArray(this.tiesSave);
285
+ this.slurs = parseCommon.cloneHashOfHash(this.slursSave);
286
+ this.slursbyvoice = parseCommon.cloneHashOfHash(this.slursbyvoiceSave);
287
+ this.tiesbyvoice = parseCommon.cloneHashOfArrayOfHash(this.tiesbyvoiceSave);
288
+ };
289
+
290
+ // function writeMeasureWidth(voice) {
291
+ // var width = 0;
292
+ // for (var i = voice.children.length-1; i >= 0; i--) {
293
+ // var elem = voice.children[i];
294
+ // if (elem.abcelem.el_type === 'bar')
295
+ // break;
296
+ // width += elem.w;
297
+ // }
298
+ // return new RelativeElement(width.toFixed(2), -70, 0, undefined, {type:"debug"});
299
+ // }
300
+
301
+ // return an array of AbsoluteElement
302
+ AbstractEngraver.prototype.createABCElement = function (isFirstStaff, isSingleLineStaff, voice, elem) {
303
+ var elemset = [];
304
+ switch (elem.el_type) {
305
+ case undefined:
306
+ // it is undefined if we were passed an array in - an array means a set of notes that should be beamed together.
307
+ elemset = this.createBeam(isSingleLineStaff, voice, elem);
308
+ break;
309
+ case "note":
310
+ elemset[0] = this.createNote(elem, false, isSingleLineStaff, voice);
311
+ if (this.triplet && this.triplet.isClosed()) {
312
+ voice.addOther(this.triplet);
313
+ this.triplet = null;
314
+ this.tripletmultiplier = 1;
315
+ }
316
+ break;
317
+ case "bar":
318
+ elemset[0] = this.createBarLine(voice, elem, isFirstStaff);
319
+ if (voice.duplicate && elemset.length > 0) elemset[0].invisible = true;
320
+ // elemset[0].addChild(writeMeasureWidth(voice));
321
+ break;
322
+ case "meter":
323
+ elemset[0] = createTimeSignature(elem, this.tuneNumber);
324
+ this.startlimitelem = elemset[0]; // limit ties here
325
+ if (voice.duplicate && elemset.length > 0) elemset[0].invisible = true;
326
+ break;
327
+ case "clef":
328
+ elemset[0] = createClef(elem, this.tuneNumber);
329
+ if (!elemset[0]) return null;
330
+ if (voice.duplicate && elemset.length > 0) elemset[0].invisible = true;
331
+ break;
332
+ case "key":
333
+ var absKey = createKeySignature(elem, this.tuneNumber);
334
+ if (absKey) {
335
+ elemset[0] = absKey;
336
+ this.startlimitelem = elemset[0]; // limit ties here
337
+ }
338
+ if (voice.duplicate && elemset.length > 0) elemset[0].invisible = true;
339
+ break;
340
+ case "stem":
341
+ this.stemdir = elem.direction === "auto" ? undefined : elem.direction;
342
+ break;
343
+ case "part":
344
+ var abselem = new AbsoluteElement(elem, 0, 0, 'part', this.tuneNumber);
345
+ var dim = this.getTextSize.calc(elem.title, 'partsfont', "part");
346
+ abselem.addFixedX(new RelativeElement(elem.title, 0, 0, undefined, { type: "part", height: dim.height / spacing.STEP }));
347
+ elemset[0] = abselem;
348
+ break;
349
+ case "tempo":
350
+ var abselem3 = new AbsoluteElement(elem, 0, 0, 'tempo', this.tuneNumber);
351
+ abselem3.addFixedX(new TempoElement(elem, this.tuneNumber, createNoteHead));
352
+ elemset[0] = abselem3;
353
+ break;
354
+ case "style":
355
+ if (elem.head === "normal")
356
+ delete this.style;
357
+ else
358
+ this.style = elem.head;
359
+ break;
360
+ case "hint":
361
+ hint = true;
362
+ this.saveState();
363
+ break;
364
+ case "midi":
365
+ // This has no effect on the visible music, so just skip it.
366
+ break;
367
+ case "scale":
368
+ this.voiceScale = elem.size;
369
+ break;
370
+ case "color":
371
+ this.voiceColor = elem.color;
372
+ voice.color = this.voiceColor;
373
+ break;
374
+
375
+ default:
376
+ var abselem2 = new AbsoluteElement(elem, 0, 0, 'unsupported', this.tuneNumber);
377
+ abselem2.addFixed(new RelativeElement("element type " + elem.el_type, 0, 0, undefined, { type: "debug" }));
378
+ elemset[0] = abselem2;
379
+ }
380
+
381
+ return elemset;
382
+ };
383
+
384
+ function setAveragePitch(elem) {
385
+ if (elem.pitches) {
386
+ sortPitch(elem);
387
+ var sum = 0;
388
+ for (var p = 0; p < elem.pitches.length; p++) {
389
+ sum += elem.pitches[p].verticalPos;
390
+ }
391
+ elem.averagepitch = sum / elem.pitches.length;
392
+ elem.minpitch = elem.pitches[0].verticalPos;
393
+ elem.maxpitch = elem.pitches[elem.pitches.length - 1].verticalPos;
394
+ }
395
+ }
396
+
397
+ AbstractEngraver.prototype.createBeam = function (isSingleLineStaff, voice, elems) {
398
+ var abselemset = [];
399
+
400
+ var beamelem = new BeamElem(this.stemHeight * this.voiceScale, this.stemdir, this.flatBeams, elems[0]);
401
+ if (hint) beamelem.setHint();
402
+ for (var i = 0; i < elems.length; i++) {
403
+ // Do a first pass to figure out the stem direction before creating the notes, so that staccatos and other decorations can be placed correctly.
404
+ beamelem.runningDirection(elems[i])
405
+ }
406
+ beamelem.setStemDirection()
407
+ var tempStemDir = this.stemdir
408
+ this.stemdir = beamelem.stemsUp ? 'up' : 'down'
409
+ for (i = 0; i < elems.length; i++) {
410
+ var elem = elems[i];
411
+ var abselem = this.createNote(elem, true, isSingleLineStaff, voice);
412
+ abselemset.push(abselem);
413
+ beamelem.add(abselem);
414
+ if (this.triplet && this.triplet.isClosed()) {
415
+ voice.addOther(this.triplet);
416
+ this.triplet = null;
417
+ this.tripletmultiplier = 1;
418
+ }
419
+ }
420
+ beamelem.calcDir();
421
+ voice.addBeam(beamelem);
422
+ this.stemdir = tempStemDir
423
+ return abselemset;
424
+ };
425
+
426
+ var sortPitch = function (elem) {
427
+ var sorted;
428
+ do {
429
+ sorted = true;
430
+ for (var p = 0; p < elem.pitches.length - 1; p++) {
431
+ if (elem.pitches[p].pitch > elem.pitches[p + 1].pitch) {
432
+ sorted = false;
433
+ var tmp = elem.pitches[p];
434
+ elem.pitches[p] = elem.pitches[p + 1];
435
+ elem.pitches[p + 1] = tmp;
436
+ }
437
+ }
438
+ } while (!sorted);
439
+ };
440
+
441
+ var ledgerLines = function (abselem, minPitch, maxPitch, isRest, symbolWidth, additionalLedgers, dir, dx, scale) {
442
+ for (var i = maxPitch; i > 11; i--) {
443
+ if (i % 2 === 0 && !isRest) {
444
+ abselem.addFixed(new RelativeElement(null, dx, (symbolWidth + 4) * scale, i, { type: "ledger" }));
445
+ }
446
+ }
447
+
448
+ for (i = minPitch; i < 1; i++) {
449
+ if (i % 2 === 0 && !isRest) {
450
+ abselem.addFixed(new RelativeElement(null, dx, (symbolWidth + 4) * scale, i, { type: "ledger" }));
451
+ }
452
+ }
453
+
454
+ for (i = 0; i < additionalLedgers.length; i++) { // PER: draw additional ledgers
455
+ var ofs = symbolWidth;
456
+ if (dir === 'down') ofs = -ofs;
457
+ abselem.addFixed(new RelativeElement(null, ofs + dx, (symbolWidth + 4) * scale, additionalLedgers[i], { type: "ledger" }));
458
+ }
459
+ };
460
+
461
+ AbstractEngraver.prototype.addGraceNotes = function (elem, voice, abselem, notehead, stemHeight, isBagpipes, roomtaken) {
462
+ var gracescale = 3 / 5;
463
+ var graceScaleStem = 3.5 / 5; // TODO-PER: empirically found constant.
464
+ stemHeight = Math.round(stemHeight * graceScaleStem);
465
+ var gracebeam = null;
466
+ var flag;
467
+
468
+ if (elem.gracenotes.length > 1) {
469
+ gracebeam = new BeamElem(stemHeight, "grace", isBagpipes);
470
+ if (hint) gracebeam.setHint();
471
+ gracebeam.mainNote = abselem; // this gives us a reference back to the note this is attached to so that the stems can be attached somewhere.
472
+ }
473
+
474
+ var i;
475
+ var graceoffsets = [];
476
+ for (i = elem.gracenotes.length - 1; i >= 0; i--) { // figure out where to place each gracenote
477
+ roomtaken += 10;
478
+ graceoffsets[i] = roomtaken;
479
+ if (elem.gracenotes[i].accidental) {
480
+ roomtaken += 7;
481
+ }
482
+ }
483
+
484
+ for (i = 0; i < elem.gracenotes.length; i++) {
485
+ var gracepitch = elem.gracenotes[i].verticalPos;
486
+
487
+ flag = (gracebeam) ? null : chartable.uflags[(isBagpipes) ? 5 : 3];
488
+ var accidentalSlot = [];
489
+ var ret = createNoteHead(abselem, "noteheads.quarter", elem.gracenotes[i],
490
+ { dir: "up", headx: -graceoffsets[i], extrax: -graceoffsets[i], flag: flag, scale: gracescale * this.voiceScale, accidentalSlot: accidentalSlot });
491
+ ret.notehead.highestVert = ret.notehead.pitch + stemHeight;
492
+ var grace = ret.notehead;
493
+ this.addSlursAndTies(abselem, elem.gracenotes[i], grace, voice, "up", true);
494
+
495
+ abselem.addExtra(grace);
496
+ // PER: added acciaccatura slash
497
+ if (elem.gracenotes[i].acciaccatura) {
498
+ var pos = elem.gracenotes[i].verticalPos + 7 * gracescale; // the same formula that determines the flag position.
499
+ var dAcciaccatura = gracebeam ? 5 : 6; // just an offset to make it line up correctly.
500
+ abselem.addRight(new RelativeElement("flags.ugrace", -graceoffsets[i] + dAcciaccatura, 0, pos, { scalex: gracescale, scaley: gracescale }));
501
+ }
502
+ if (gracebeam) { // give the beam the necessary info
503
+ var graceDuration = elem.gracenotes[i].duration / 2;
504
+ if (isBagpipes) graceDuration /= 2;
505
+ var pseudoabselem = {
506
+ heads: [grace],
507
+ abcelem: { averagepitch: gracepitch, minpitch: gracepitch, maxpitch: gracepitch, duration: graceDuration }
508
+ };
509
+ gracebeam.add(pseudoabselem);
510
+ } else { // draw the stem
511
+ var p1 = gracepitch + 1 / 3 * gracescale;
512
+ var p2 = gracepitch + 7 * gracescale;
513
+ var dx = grace.dx + grace.w;
514
+ var width = -0.6;
515
+ abselem.addExtra(new RelativeElement(null, dx, 0, p1, { "type": "stem", "pitch2": p2, linewidth: width }));
516
+ }
517
+ ledgerLines(abselem, gracepitch, gracepitch, false, glyphs.getSymbolWidth("noteheads.quarter"), [], true, grace.dx - 1, 0.6);
518
+
519
+ // if this is the first grace note, we might want to start a slur.
520
+ // there is a slur if graceSlurs is specifically set.
521
+ // there is no slur if it is bagpipes.
522
+ // there is not a slur if the element is a spacer or invisible rest.
523
+ var isInvisibleRest = elem.rest && (elem.rest.type === "spacer" || elem.rest.type === "invisible");
524
+ if (i === 0 && !isBagpipes && this.graceSlurs && !isInvisibleRest) {
525
+ // This is the overall slur that is under the grace notes.
526
+ voice.addOther(new TieElem({ anchor1: grace, anchor2: notehead, isGrace: true }));
527
+ }
528
+ }
529
+
530
+ if (gracebeam) {
531
+ gracebeam.calcDir();
532
+ voice.addBeam(gracebeam);
533
+ }
534
+ return roomtaken;
535
+ };
536
+
537
+ function addRestToAbsElement(abselem, elem, duration, dot, isMultiVoice, stemdir, isSingleLineStaff, durlog, voiceScale) {
538
+ var c;
539
+ var restpitch = 7;
540
+ var noteHead;
541
+ var roomTaken;
542
+ var roomTakenRight;
543
+
544
+ if (isMultiVoice) {
545
+ if (stemdir === "down") restpitch = 3;
546
+ if (stemdir === "up") restpitch = 11;
547
+ }
548
+ // There is special placement for the percussion staff. If there is one staff line, then move the rest position.
549
+ if (isSingleLineStaff) {
550
+ // The half and whole rests are attached to different lines normally, so we need to tweak their position to get them to both be attached to the same one.
551
+ if (duration < 0.5)
552
+ restpitch = 7;
553
+ else if (duration < 1)
554
+ restpitch = 7; // half rest
555
+ else
556
+ restpitch = 5; // whole rest
557
+ }
558
+ switch (elem.rest.type) {
559
+ case "whole":
560
+ c = chartable.rest[0];
561
+ elem.averagepitch = restpitch;
562
+ elem.minpitch = restpitch;
563
+ elem.maxpitch = restpitch;
564
+ dot = 0;
565
+ break;
566
+ case "rest":
567
+ if (elem.style === "rhythm") // special case for rhythm: rests are a handy way to express the rhythm.
568
+ c = chartable.rhythm[-durlog];
569
+ else
570
+ c = chartable.rest[-durlog];
571
+ elem.averagepitch = restpitch;
572
+ elem.minpitch = restpitch;
573
+ elem.maxpitch = restpitch;
574
+ break;
575
+ case "invisible":
576
+ case "invisible-multimeasure":
577
+ case "spacer":
578
+ c = "";
579
+ elem.averagepitch = restpitch;
580
+ elem.minpitch = restpitch;
581
+ elem.maxpitch = restpitch;
582
+ break;
583
+ case "multimeasure":
584
+ c = chartable.rest['multi'];
585
+ elem.averagepitch = restpitch;
586
+ elem.minpitch = restpitch;
587
+ elem.maxpitch = restpitch;
588
+ dot = 0;
589
+ var mmWidth = glyphs.getSymbolWidth(c);
590
+ abselem.addHead(new RelativeElement(c, mmWidth, mmWidth * 2, 7));
591
+ var numMeasures = new RelativeElement("" + elem.rest.text, mmWidth, mmWidth, 16, { type: "multimeasure-text" });
592
+ abselem.addExtra(numMeasures);
593
+ }
594
+ if (elem.rest.type.indexOf("multimeasure") < 0 && elem.rest.type !== "invisible") {
595
+ var ret = createNoteHead(abselem, c, { verticalPos: restpitch },
596
+ { dot: dot, scale: voiceScale });
597
+ noteHead = ret.notehead;
598
+ if (noteHead) {
599
+ abselem.addHead(noteHead);
600
+ roomTaken = ret.accidentalshiftx;
601
+ roomTakenRight = ret.dotshiftx;
602
+ }
603
+ }
604
+ return { noteHead: noteHead, roomTaken: roomTaken, roomTakenRight: roomTakenRight };
605
+ }
606
+
607
+ function addIfNotExist(arr, item) {
608
+ for (var i = 0; i < arr.length; i++) {
609
+ if (JSON.stringify(arr[i]) === JSON.stringify(item))
610
+ return;
611
+ }
612
+ arr.push(item);
613
+ }
614
+
615
+ AbstractEngraver.prototype.addNoteToAbcElement = function (abselem, elem, dot, stemdir, style, zeroDuration, durlog, nostem, voice) {
616
+ var dotshiftx = 0; // room taken by chords with displaced noteheads which cause dots to shift
617
+ var noteHead;
618
+ var roomTaken = 0;
619
+ var roomTakenRight = 0;
620
+ var min;
621
+ var i;
622
+ var additionalLedgers = [];
623
+ // The accidentalSlot will hold a list of all the accidentals on this chord. Each element is a vertical place,
624
+ // and contains a pitch, which is the last pitch that contains an accidental in that slot. The slots are numbered
625
+ // from closest to the note to farther left. We only need to know the last accidental we placed because
626
+ // we know that the pitches are sorted by now.
627
+ var accidentalSlot = [];
628
+ var symbolWidth = 0;
629
+
630
+ var dir = (elem.averagepitch >= 6) ? "down" : "up";
631
+ if (stemdir) dir = stemdir;
632
+
633
+ style = elem.style ? elem.style : style; // get the style of note head.
634
+ if (!style || style === "normal") style = "note";
635
+ var noteSymbol;
636
+ if (zeroDuration)
637
+ noteSymbol = chartable[style].nostem;
638
+ else
639
+ noteSymbol = chartable[style][-durlog];
640
+ if (!noteSymbol)
641
+ console.log("noteSymbol:", style, durlog, zeroDuration);
642
+
643
+ // determine elements of chords which should be shifted
644
+ var p;
645
+ for (p = (dir === "down") ? elem.pitches.length - 2 : 1; (dir === "down") ? p >= 0 : p < elem.pitches.length; p = (dir === "down") ? p - 1 : p + 1) {
646
+ var prev = elem.pitches[(dir === "down") ? p + 1 : p - 1];
647
+ var curr = elem.pitches[p];
648
+ var delta = (dir === "down") ? prev.pitch - curr.pitch : curr.pitch - prev.pitch;
649
+ if (delta <= 1 && !prev.printer_shift) {
650
+ curr.printer_shift = (delta) ? "different" : "same";
651
+ if (curr.verticalPos > 11 || curr.verticalPos < 1) { // PER: add extra ledger line
652
+ additionalLedgers.push(curr.verticalPos - (curr.verticalPos % 2));
653
+ }
654
+ if (dir === "down") {
655
+ roomTaken = glyphs.getSymbolWidth(noteSymbol) + 2;
656
+ } else {
657
+ dotshiftx = glyphs.getSymbolWidth(noteSymbol) + 2;
658
+ }
659
+ }
660
+ }
661
+
662
+ var pp = elem.pitches.length;
663
+ for (p = 0; p < elem.pitches.length; p++) {
664
+
665
+ if (!nostem) {
666
+ var flag;
667
+ if ((dir === "down" && p !== 0) || (dir === "up" && p !== pp - 1)) { // not the stemmed elem of the chord
668
+ flag = null;
669
+ } else {
670
+ flag = chartable[(dir === "down") ? "dflags" : "uflags"][-durlog];
671
+ }
672
+ }
673
+ var c;
674
+ if (elem.pitches[p].style) { // There is a style for the whole group of pitches, but there could also be an override for a particular pitch.
675
+ c = chartable[elem.pitches[p].style][-durlog];
676
+ } else if (voice.isPercussion && this.percmap) {
677
+ c = noteSymbol;
678
+ var percHead = this.percmap[pitchesToPerc(elem.pitches[p])];
679
+ if (percHead && percHead.noteHead) {
680
+ if (chartable[percHead.noteHead])
681
+ c = chartable[percHead.noteHead][-durlog];
682
+ }
683
+ } else
684
+ c = noteSymbol;
685
+ // The highest position for the sake of placing slurs is itself if the slur is internal. It is the highest position possible if the slur is for the whole chord.
686
+ // If the note is the only one in the chord, then any slur it has counts as if it were on the whole chord.
687
+ elem.pitches[p].highestVert = elem.pitches[p].verticalPos;
688
+ var isTopWhenStemIsDown = (stemdir === "up" || dir === "up") && p === 0;
689
+ var isBottomWhenStemIsUp = (stemdir === "down" || dir === "down") && p === pp - 1;
690
+ if (isTopWhenStemIsDown || isBottomWhenStemIsUp) { // place to put slurs if not already on pitches
691
+
692
+ if (elem.startSlur || pp === 1) {
693
+ elem.pitches[p].highestVert = elem.pitches[pp - 1].verticalPos;
694
+ if (getDuration(elem) < 1 && (stemdir === "up" || dir === "up"))
695
+ elem.pitches[p].highestVert += 6; // If the stem is up, then compensate for the length of the stem
696
+ }
697
+ if (elem.startSlur) {
698
+ if (!elem.pitches[p].startSlur) elem.pitches[p].startSlur = []; //TODO possibly redundant, provided array is not optional
699
+ for (i = 0; i < elem.startSlur.length; i++) {
700
+ addIfNotExist(elem.pitches[p].startSlur, elem.startSlur[i]);
701
+ }
702
+ }
703
+
704
+ if (elem.endSlur) {
705
+ elem.pitches[p].highestVert = elem.pitches[pp - 1].verticalPos;
706
+ if (getDuration(elem) < 1 && (stemdir === "up" || dir === "up"))
707
+ elem.pitches[p].highestVert += 6; // If the stem is up, then compensate for the length of the stem
708
+ if (!elem.pitches[p].endSlur) elem.pitches[p].endSlur = []; //TODO possibly redundant, provided array is not optional
709
+ for (i = 0; i < elem.endSlur.length; i++) {
710
+ addIfNotExist(elem.pitches[p].endSlur, elem.endSlur[i]);
711
+ }
712
+ }
713
+ }
714
+
715
+ var hasStem = !nostem && durlog <= -1;
716
+ var ret = createNoteHead(abselem, c, elem.pitches[p],
717
+ { dir: dir, extrax: -roomTaken, flag: flag, dot: dot, dotshiftx: dotshiftx, scale: this.voiceScale, accidentalSlot: accidentalSlot, shouldExtendStem: !stemdir, printAccidentals: !voice.isPercussion });
718
+ symbolWidth = Math.max(glyphs.getSymbolWidth(c), symbolWidth);
719
+ abselem.extraw -= ret.extraLeft;
720
+ noteHead = ret.notehead;
721
+ if (noteHead) {
722
+ this.addSlursAndTies(abselem, elem.pitches[p], noteHead, voice, hasStem ? dir : null, false);
723
+
724
+ if (elem.gracenotes && elem.gracenotes.length > 0)
725
+ noteHead.bottom = noteHead.bottom - 1; // If there is a tie to the grace notes, leave a little more room for the note to avoid collisions.
726
+ abselem.addHead(noteHead);
727
+ }
728
+ roomTaken += ret.accidentalshiftx;
729
+ roomTakenRight = Math.max(roomTakenRight, ret.dotshiftx);
730
+ }
731
+
732
+ // draw stem from the furthest note to a pitch above/below the stemmed note
733
+ if (hasStem) {
734
+ var stemHeight = Math.round(70 * this.voiceScale) / 10;
735
+ var p1 = (dir === "down") ? elem.minpitch - stemHeight : elem.minpitch + 1 / 3;
736
+ // PER added stemdir test to make the line meet the note.
737
+ if (p1 > 6 && !stemdir) p1 = 6;
738
+ var p2 = (dir === "down") ? elem.maxpitch - 1 / 3 : elem.maxpitch + stemHeight;
739
+ // PER added stemdir test to make the line meet the note.
740
+ if (p2 < 6 && !stemdir) p2 = 6;
741
+ var dx = (dir === "down" || abselem.heads.length === 0) ? 0 : abselem.heads[0].w;
742
+ var width = (dir === "down") ? 1 : -1;
743
+ // TODO-PER-HACK: One type of note head has a different placement of the stem. This should be more generically calculated:
744
+ if (noteHead && noteHead.c === 'noteheads.slash.quarter') {
745
+ if (dir === 'down')
746
+ p2 -= 1;
747
+ else
748
+ p1 += 1;
749
+ }
750
+ abselem.addRight(new RelativeElement(null, dx, 0, p1, { "type": "stem", "pitch2": p2, linewidth: width, bottom: p1 - 1 }));
751
+ //var RelativeElement = function RelativeElement(c, dx, w, pitch, opt) {
752
+ min = Math.min(p1, p2);
753
+ }
754
+ return { noteHead: noteHead, roomTaken: roomTaken, roomTakenRight: roomTakenRight, min: min, additionalLedgers: additionalLedgers, dir: dir, symbolWidth: symbolWidth };
755
+ };
756
+
757
+ AbstractEngraver.prototype.addLyric = function (abselem, elem) {
758
+ var lyricStr = "";
759
+ elem.lyric.forEach(function (ly) {
760
+ var div = ly.divider === ' ' ? "" : ly.divider;
761
+ lyricStr += ly.syllable + div + "\n";
762
+ });
763
+ var lyricDim = this.getTextSize.calc(lyricStr, 'vocalfont', "lyric");
764
+ var position = elem.positioning ? elem.positioning.vocalPosition : 'below';
765
+ abselem.addCentered(new RelativeElement(lyricStr, 0, lyricDim.width, undefined, { type: "lyric", position: position, height: lyricDim.height / spacing.STEP, dim: this.getTextSize.attr('vocalfont', "lyric") }));
766
+ };
767
+
768
+ AbstractEngraver.prototype.createNote = function (elem, nostem, isSingleLineStaff, voice) { //stem presence: true for drawing stemless notehead
769
+ var notehead = null;
770
+ var roomtaken = 0; // room needed to the left of the note
771
+ var roomtakenright = 0; // room needed to the right of the note
772
+ var symbolWidth = 0;
773
+ var additionalLedgers = []; // PER: handle the case of [bc'], where the b doesn't have a ledger line
774
+
775
+ var dir;
776
+
777
+ var duration = getDuration(elem);
778
+ var zeroDuration = false;
779
+ if (duration === 0) { zeroDuration = true; duration = 0.25; nostem = true; } //PER: zero duration will draw a quarter note head.
780
+ var durlog = Math.floor(Math.log(duration) / Math.log(2)); //TODO use getDurlog
781
+ var dot = 0;
782
+
783
+ for (var tot = Math.pow(2, durlog), inc = tot / 2; tot < duration; dot++, tot += inc, inc /= 2);
784
+
785
+
786
+ if (elem.startTriplet) {
787
+ this.tripletmultiplier = elem.tripletMultiplier;
788
+ }
789
+
790
+ var durationForSpacing = duration * this.tripletmultiplier;
791
+ if (elem.rest && elem.rest.type === 'multimeasure')
792
+ durationForSpacing = 1;
793
+ if (elem.rest && elem.rest.type === 'invisible-multimeasure')
794
+ durationForSpacing = this.measureLength * elem.rest.text;
795
+ var absType = elem.rest ? "rest" : "note";
796
+ var abselem = new AbsoluteElement(elem, durationForSpacing, 1, absType, this.tuneNumber, { durationClassOveride: elem.duration * this.tripletmultiplier });
797
+ if (hint) abselem.setHint();
798
+
799
+ if (elem.rest) {
800
+ if (this.measureLength === duration && elem.rest.type !== 'invisible' && elem.rest.type !== 'spacer' && elem.rest.type.indexOf('multimeasure') < 0)
801
+ elem.rest.type = 'whole'; // If the rest is exactly a measure, always use a whole rest
802
+ var ret1 = addRestToAbsElement(abselem, elem, duration, dot, voice.voicetotal > 1, this.stemdir, isSingleLineStaff, durlog, this.voiceScale);
803
+ notehead = ret1.noteHead;
804
+ roomtaken = ret1.roomTaken;
805
+ roomtakenright = ret1.roomTakenRight;
806
+ } else {
807
+ var ret2 = this.addNoteToAbcElement(abselem, elem, dot, this.stemdir, this.style, zeroDuration, durlog, nostem, voice);
808
+ if (ret2.min !== undefined)
809
+ this.minY = Math.min(ret2.min, this.minY);
810
+ notehead = ret2.noteHead;
811
+ roomtaken = ret2.roomTaken;
812
+ roomtakenright = ret2.roomTakenRight;
813
+ additionalLedgers = ret2.additionalLedgers;
814
+ dir = ret2.dir;
815
+ symbolWidth = ret2.symbolWidth;
816
+ }
817
+
818
+ if (elem.lyric !== undefined) {
819
+ this.addLyric(abselem, elem);
820
+ }
821
+
822
+ if (elem.gracenotes !== undefined) {
823
+ roomtaken += this.addGraceNotes(elem, voice, abselem, notehead, this.stemHeight * this.voiceScale, this.isBagpipes, roomtaken);
824
+ }
825
+
826
+ if (elem.decoration) {
827
+ this.decoration.createDecoration(voice, elem.decoration, abselem.top, (notehead) ? notehead.w : 0, abselem, roomtaken, dir, abselem.bottom, elem.positioning, this.hasVocals);
828
+ }
829
+
830
+ if (elem.barNumber) {
831
+ abselem.addFixed(new RelativeElement(elem.barNumber, -10, 0, 0, { type: "barNumber" }));
832
+ }
833
+
834
+ // ledger lines
835
+ ledgerLines(abselem, elem.minpitch, elem.maxpitch, elem.rest, symbolWidth, additionalLedgers, dir, -2, 1);
836
+
837
+ if (elem.chord !== undefined) {
838
+ var ret3 = addChord(this.getTextSize, abselem, elem, roomtaken, roomtakenright, symbolWidth, this.jazzchords, this.germanAlphabet);
839
+ roomtaken = ret3.roomTaken;
840
+ roomtakenright = ret3.roomTakenRight;
841
+ }
842
+
843
+ if (elem.startTriplet) {
844
+ this.triplet = new TripletElem(elem.startTriplet, notehead, { flatBeams: this.flatBeams }); // above is opposite from case of slurs
845
+ }
846
+
847
+ if (elem.endTriplet && this.triplet) {
848
+ this.triplet.setCloseAnchor(notehead);
849
+ }
850
+
851
+ if (this.triplet && !elem.startTriplet && !elem.endTriplet && !(elem.rest && elem.rest.type === "spacer")) {
852
+ this.triplet.middleNote(notehead);
853
+ }
854
+
855
+ return abselem;
856
+ };
857
+
858
+ AbstractEngraver.prototype.addSlursAndTies = function (abselem, pitchelem, notehead, voice, dir, isGrace) {
859
+ if (pitchelem.endTie) {
860
+ if (this.ties.length > 0) {
861
+ // If there are multiple open ties, find the one that applies by matching the pitch, if possible.
862
+ var found = false;
863
+ for (var j = 0; j < this.ties.length; j++) {
864
+ if (this.ties[j].anchor1 && this.ties[j].anchor1.pitch === notehead.pitch) {
865
+ this.ties[j].setEndAnchor(notehead);
866
+ voice.setRange(this.ties[j])
867
+ this.ties.splice(j, 1);
868
+ found = true;
869
+ break;
870
+ }
871
+ }
872
+ if (!found) {
873
+ this.ties[0].setEndAnchor(notehead);
874
+ voice.setRange(this.ties[0])
875
+ this.ties.splice(0, 1);
876
+ }
877
+ }
878
+ }
879
+
880
+ var voiceNumber = voice.voicetotal < 2 ? -1 : voice.voicenumber;
881
+ if (pitchelem.startTie) {
882
+ var tie = new TieElem({ anchor1: notehead, force: (this.stemdir === "down" || this.stemdir === "up"), stemDir: this.stemdir, isGrace: isGrace, voiceNumber: voiceNumber, style: pitchelem.startTie.style });
883
+ if (hint) tie.setHint();
884
+
885
+ this.ties[this.ties.length] = tie;
886
+ voice.addOther(tie);
887
+ // HACK-PER: For the animation, we need to know if a note is tied to the next one, so here's a flag.
888
+ // Unfortunately, only some of the notes in the current event might be tied, but this will consider it
889
+ // tied if any one of them is. That will work for most cases.
890
+ abselem.startTie = true;
891
+ }
892
+
893
+ var slur;
894
+ var slurid;
895
+ if (pitchelem.endSlur) {
896
+ for (var i = 0; i < pitchelem.endSlur.length; i++) {
897
+ slurid = pitchelem.endSlur[i];
898
+ if (this.slurs[slurid]) {
899
+ slur = this.slurs[slurid];
900
+ slur.setEndAnchor(notehead);
901
+ voice.setRange(slur)
902
+ delete this.slurs[slurid];
903
+ } else {
904
+ slur = new TieElem({ anchor2: notehead, stemDir: this.stemdir, voiceNumber: voiceNumber });
905
+ if (hint) slur.setHint();
906
+ voice.addOther(slur);
907
+ }
908
+ if (this.startlimitelem) {
909
+ slur.setStartX(this.startlimitelem);
910
+ }
911
+ }
912
+ } else if (!isGrace) {
913
+ for (var s in this.slurs) {
914
+ if (this.slurs.hasOwnProperty(s)) {
915
+ this.slurs[s].addInternalNote(notehead);
916
+ }
917
+ }
918
+ }
919
+
920
+ if (pitchelem.startSlur) {
921
+ for (i = 0; i < pitchelem.startSlur.length; i++) {
922
+ slurid = pitchelem.startSlur[i].label;
923
+ slur = new TieElem({ anchor1: notehead, stemDir: this.stemdir, voiceNumber: voiceNumber, style: pitchelem.startSlur[i].style });
924
+ if (hint) slur.setHint();
925
+ this.slurs[slurid] = slur;
926
+ voice.addOther(slur);
927
+ }
928
+ }
929
+ };
930
+
931
+ AbstractEngraver.prototype.addMeasureNumber = function (number, abselem) {
932
+ var measureNumDim = this.getTextSize.calc(number, "measurefont", 'bar-number');
933
+ var dx = 0;
934
+ if (abselem.isClef) // If this is a clef rather than bar line, then the number shouldn't be centered because it could overlap the left side. This is an easy way to let it be centered but move it over, too.
935
+ dx += measureNumDim.width / 2
936
+ var vert = measureNumDim.width > 10 && abselem.abcelem.type === "treble" ? 13 : 11
937
+ abselem.addFixed(new RelativeElement(number, dx, measureNumDim.width, vert + measureNumDim.height / spacing.STEP, { type: "barNumber", dim: this.getTextSize.attr("measurefont", 'bar-number') }));
938
+ };
939
+
940
+ AbstractEngraver.prototype.createBarLine = function (voice, elem, isFirstStaff) {
941
+ // bar_thin, bar_thin_thick, bar_thin_thin, bar_thick_thin, bar_right_repeat, bar_left_repeat, bar_double_repeat
942
+
943
+ var abselem = new AbsoluteElement(elem, 0, 10, 'bar', this.tuneNumber);
944
+ var anchor = null; // place to attach part lines
945
+ var dx = 0;
946
+
947
+ if (elem.barNumber) {
948
+ this.addMeasureNumber(elem.barNumber, abselem);
949
+ }
950
+
951
+
952
+ var firstdots = (elem.type === "bar_right_repeat" || elem.type === "bar_dbl_repeat");
953
+ var firstthin = (elem.type !== "bar_left_repeat" && elem.type !== "bar_thick_thin" && elem.type !== "bar_invisible");
954
+ var thick = (elem.type === "bar_right_repeat" || elem.type === "bar_dbl_repeat" || elem.type === "bar_left_repeat" ||
955
+ elem.type === "bar_thin_thick" || elem.type === "bar_thick_thin");
956
+ var secondthin = (elem.type === "bar_left_repeat" || elem.type === "bar_thick_thin" || elem.type === "bar_thin_thin" || elem.type === "bar_dbl_repeat");
957
+ var seconddots = (elem.type === "bar_left_repeat" || elem.type === "bar_dbl_repeat");
958
+
959
+ // limit positioning of slurs
960
+ if (firstdots || seconddots) {
961
+ for (var slur in this.slurs) {
962
+ if (this.slurs.hasOwnProperty(slur)) {
963
+ this.slurs[slur].setEndX(abselem);
964
+ }
965
+ }
966
+ this.startlimitelem = abselem;
967
+ }
968
+
969
+ if (firstdots) {
970
+ abselem.addRight(new RelativeElement("dots.dot", dx, 1, 7));
971
+ abselem.addRight(new RelativeElement("dots.dot", dx, 1, 5));
972
+ dx += 6; //2 hardcoded, twice;
973
+ }
974
+
975
+ if (firstthin) {
976
+ anchor = new RelativeElement(null, dx, 1, 2, { "type": "bar", "pitch2": 10, linewidth: 0.6 });
977
+ abselem.addRight(anchor);
978
+ }
979
+
980
+ if (elem.type === "bar_invisible") {
981
+ anchor = new RelativeElement(null, dx, 1, 2, { "type": "none", "pitch2": 10, linewidth: 0.6 });
982
+ abselem.addRight(anchor);
983
+ }
984
+
985
+ if (elem.decoration) {
986
+ this.decoration.createDecoration(voice, elem.decoration, 12, (thick) ? 3 : 1, abselem, 0, "down", 2, elem.positioning, this.hasVocals);
987
+ }
988
+
989
+ if (thick) {
990
+ dx += 4; //3 hardcoded;
991
+ anchor = new RelativeElement(null, dx, 4, 2, { "type": "bar", "pitch2": 10, linewidth: 4 });
992
+ abselem.addRight(anchor);
993
+ dx += 5;
994
+ }
995
+
996
+ // if (this.partstartelem && (thick || (firstthin && secondthin))) { // means end of nth part
997
+ // this.partstartelem.anchor2=anchor;
998
+ // this.partstartelem = null;
999
+ // }
1000
+
1001
+ if (this.partstartelem && elem.endEnding) {
1002
+ this.partstartelem.anchor2 = anchor;
1003
+ this.partstartelem = null;
1004
+ }
1005
+
1006
+ if (secondthin) {
1007
+ dx += 3; //3 hardcoded;
1008
+ anchor = new RelativeElement(null, dx, 1, 2, { "type": "bar", "pitch2": 10, linewidth: 0.6 });
1009
+ abselem.addRight(anchor); // 3 is hardcoded
1010
+ }
1011
+
1012
+ if (seconddots) {
1013
+ dx += 3; //3 hardcoded;
1014
+ abselem.addRight(new RelativeElement("dots.dot", dx, 1, 7));
1015
+ abselem.addRight(new RelativeElement("dots.dot", dx, 1, 5));
1016
+ } // 2 is hardcoded
1017
+
1018
+ if (elem.startEnding && isFirstStaff) { // only put the first & second ending marks on the first staff
1019
+ var textWidth = this.getTextSize.calc(elem.startEnding, "repeatfont", '').width;
1020
+ abselem.minspacing += textWidth + 10; // Give plenty of room for the ending number.
1021
+ this.partstartelem = new EndingElem(elem.startEnding, anchor, null);
1022
+ voice.addOther(this.partstartelem);
1023
+ }
1024
+
1025
+ // Add a little space to the left of the bar line so that nothing can crowd it.
1026
+ abselem.extraw -= 5;
1027
+
1028
+ if (elem.chord !== undefined) {
1029
+ var ret3 = addChord(this.getTextSize, abselem, elem, 0, 0, 0, false, this.germanAlphabet);
1030
+ }
1031
+
1032
+ return abselem;
1033
+
1034
+ };
1035
+
1036
+ module.exports = AbstractEngraver;