1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 | Vex.Flow.Note = (function() {
|
14 |
|
15 |
|
16 |
|
17 |
|
18 |
|
19 |
|
20 |
|
21 |
|
22 | function Note(note_struct) {
|
23 | if (arguments.length > 0) this.init(note_struct);
|
24 | }
|
25 | Note.CATEGORY = "note";
|
26 |
|
27 |
|
28 |
|
29 |
|
30 |
|
31 | Vex.Inherit(Note, Vex.Flow.Tickable, {
|
32 |
|
33 | init: function(note_struct) {
|
34 | Note.superclass.init.call(this);
|
35 |
|
36 | if (!note_struct) {
|
37 | throw new Vex.RuntimeError("BadArguments",
|
38 | "Note must have valid initialization data to identify " +
|
39 | "duration and type.");
|
40 | }
|
41 |
|
42 |
|
43 | var initData = Vex.Flow.parseNoteData(note_struct);
|
44 | if (!initData) {
|
45 | throw new Vex.RuntimeError("BadArguments",
|
46 | "Invalid note initialization object: " + JSON.stringify(note_struct));
|
47 | }
|
48 |
|
49 |
|
50 | this.duration = initData.duration;
|
51 | this.dots = initData.dots;
|
52 | this.noteType = initData.type;
|
53 |
|
54 | if (note_struct.duration_override) {
|
55 |
|
56 | this.setDuration(note_struct.duration_override);
|
57 | } else {
|
58 |
|
59 | this.setIntrinsicTicks(initData.ticks);
|
60 | }
|
61 |
|
62 | this.modifiers = [];
|
63 |
|
64 |
|
65 | this.glyph = Vex.Flow.durationToGlyph(this.duration, this.noteType);
|
66 |
|
67 | if (this.positions &&
|
68 | (typeof(this.positions) != "object" || !this.positions.length)) {
|
69 | throw new Vex.RuntimeError(
|
70 | "BadArguments", "Note keys must be array type.");
|
71 | }
|
72 |
|
73 |
|
74 | this.playNote = null;
|
75 |
|
76 |
|
77 | this.tickContext = null;
|
78 | this.modifierContext = null;
|
79 | this.ignore_ticks = false;
|
80 |
|
81 |
|
82 | this.width = 0;
|
83 | this.extraLeftPx = 0;
|
84 | this.extraRightPx = 0;
|
85 | this.x_shift = 0;
|
86 | this.left_modPx = 0;
|
87 | this.right_modPx = 0;
|
88 | this.voice = null;
|
89 | this.preFormatted = false;
|
90 | this.ys = [];
|
91 |
|
92 |
|
93 | if (note_struct.align_center) {
|
94 | this.setCenterAlignment(note_struct.align_center);
|
95 | }
|
96 |
|
97 |
|
98 | this.context = null;
|
99 | this.stave = null;
|
100 | this.render_options = {
|
101 | annotation_spacing: 5,
|
102 | stave_padding: 12
|
103 | };
|
104 | },
|
105 |
|
106 |
|
107 |
|
108 | getPlayNote: function() { return this.playNote; },
|
109 | setPlayNote: function(note) { this.playNote = note; return this; },
|
110 |
|
111 |
|
112 |
|
113 | isRest: function() { return false; },
|
114 |
|
115 |
|
116 | addStroke: function(index, stroke) {
|
117 | stroke.setNote(this);
|
118 | stroke.setIndex(index);
|
119 | this.modifiers.push(stroke);
|
120 | this.setPreFormatted(false);
|
121 | return this;
|
122 | },
|
123 |
|
124 |
|
125 | getStave: function() { return this.stave; },
|
126 | setStave: function(stave) {
|
127 | this.stave = stave;
|
128 | this.setYs([stave.getYForLine(0)]);
|
129 | this.context = this.stave.context;
|
130 | return this;
|
131 | },
|
132 |
|
133 |
|
134 |
|
135 |
|
136 | getCategory: function() { return this.constructor.CATEGORY; },
|
137 |
|
138 |
|
139 | setContext: function(context) { this.context = context; return this; },
|
140 |
|
141 |
|
142 | getExtraLeftPx: function() { return this.extraLeftPx; },
|
143 | getExtraRightPx: function() { return this.extraRightPx; },
|
144 | setExtraLeftPx: function(x) { this.extraLeftPx = x; return this; },
|
145 | setExtraRightPx: function(x) { this.extraRightPx = x; return this; },
|
146 |
|
147 |
|
148 | shouldIgnoreTicks: function() { return this.ignore_ticks; },
|
149 |
|
150 |
|
151 | getLineNumber: function() { return 0; },
|
152 |
|
153 |
|
154 | getLineForRest: function() { return 0; },
|
155 |
|
156 |
|
157 | getGlyph: function() { return this.glyph; },
|
158 |
|
159 |
|
160 |
|
161 | setYs: function(ys) { this.ys = ys; return this; },
|
162 | getYs: function() {
|
163 | if (this.ys.length === 0) throw new Vex.RERR("NoYValues",
|
164 | "No Y-values calculated for this note.");
|
165 | return this.ys;
|
166 | },
|
167 |
|
168 |
|
169 |
|
170 | getYForTopText: function(text_line) {
|
171 | if (!this.stave) throw new Vex.RERR("NoStave",
|
172 | "No stave attached to this note.");
|
173 | return this.stave.getYForTopText(text_line);
|
174 | },
|
175 |
|
176 |
|
177 | getBoundingBox: function() { return null; },
|
178 |
|
179 |
|
180 | getVoice: function() {
|
181 | if (!this.voice) throw new Vex.RERR("NoVoice", "Note has no voice.");
|
182 | return this.voice;
|
183 | },
|
184 |
|
185 |
|
186 | setVoice: function(voice) {
|
187 | this.voice = voice;
|
188 | this.preFormatted = false;
|
189 | return this;
|
190 | },
|
191 |
|
192 |
|
193 | getTickContext: function() { return this.tickContext; },
|
194 | setTickContext: function(tc) {
|
195 | this.tickContext = tc;
|
196 | this.preFormatted = false;
|
197 | return this;
|
198 | },
|
199 |
|
200 |
|
201 | getDuration: function() { return this.duration; },
|
202 | isDotted: function() { return (this.dots > 0); },
|
203 | hasStem: function() { return false; },
|
204 | getDots: function() { return this.dots; },
|
205 | getNoteType: function() { return this.noteType; },
|
206 | setBeam: function() { return this; },
|
207 |
|
208 |
|
209 | setModifierContext: function(mc) { this.modifierContext = mc; return this; },
|
210 |
|
211 |
|
212 | addModifier: function(modifier, index) {
|
213 | modifier.setNote(this);
|
214 | modifier.setIndex(index || 0);
|
215 | this.modifiers.push(modifier);
|
216 | this.setPreFormatted(false);
|
217 | return this;
|
218 | },
|
219 |
|
220 |
|
221 | getModifierStartXY: function() {
|
222 | if (!this.preFormatted) throw new Vex.RERR("UnformattedNote",
|
223 | "Can't call GetModifierStartXY on an unformatted note");
|
224 | return {x: this.getAbsoluteX(), y: this.ys[0]};
|
225 | },
|
226 |
|
227 |
|
228 |
|
229 |
|
230 |
|
231 |
|
232 |
|
233 |
|
234 |
|
235 |
|
236 |
|
237 | getMetrics: function() {
|
238 | if (!this.preFormatted) throw new Vex.RERR("UnformattedNote",
|
239 | "Can't call getMetrics on an unformatted note.");
|
240 | var modLeftPx = 0;
|
241 | var modRightPx = 0;
|
242 | if (this.modifierContext != null) {
|
243 | modLeftPx = this.modifierContext.state.left_shift;
|
244 | modRightPx = this.modifierContext.state.right_shift;
|
245 | }
|
246 |
|
247 | var width = this.getWidth();
|
248 | return { width: width,
|
249 | noteWidth: width -
|
250 | modLeftPx - modRightPx -
|
251 | this.extraLeftPx - this.extraRightPx,
|
252 | left_shift: this.x_shift,
|
253 | modLeftPx: modLeftPx,
|
254 | modRightPx: modRightPx,
|
255 | extraLeftPx: this.extraLeftPx,
|
256 | extraRightPx: this.extraRightPx };
|
257 | },
|
258 |
|
259 |
|
260 | setWidth: function(width) { this.width = width; },
|
261 | getWidth: function() {
|
262 | if (!this.preFormatted) throw new Vex.RERR("UnformattedNote",
|
263 | "Can't call GetWidth on an unformatted note.");
|
264 | return this.width +
|
265 | (this.modifierContext ? this.modifierContext.getWidth() : 0);
|
266 | },
|
267 |
|
268 |
|
269 | setXShift: function(x) {
|
270 | this.x_shift = x;
|
271 | return this;
|
272 | },
|
273 |
|
274 |
|
275 | getX: function() {
|
276 | if (!this.tickContext) throw new Vex.RERR("NoTickContext",
|
277 | "Note needs a TickContext assigned for an X-Value");
|
278 | return this.tickContext.getX() + this.x_shift;
|
279 | },
|
280 |
|
281 |
|
282 | getAbsoluteX: function() {
|
283 | if (!this.tickContext) throw new Vex.RERR("NoTickContext",
|
284 | "Note needs a TickContext assigned for an X-Value");
|
285 |
|
286 |
|
287 | var x = this.tickContext.getX();
|
288 | if (this.stave) {
|
289 | x += this.stave.getNoteStartX() + this.render_options.stave_padding;
|
290 | }
|
291 |
|
292 | if (this.isCenterAligned()){
|
293 | x += this.getCenterXShift();
|
294 | }
|
295 |
|
296 | return x;
|
297 | },
|
298 |
|
299 | setPreFormatted: function(value) {
|
300 | this.preFormatted = value;
|
301 |
|
302 |
|
303 | if (this.preFormatted) {
|
304 | var extra = this.tickContext.getExtraPx();
|
305 | this.left_modPx = Math.max(this.left_modPx, extra.left);
|
306 | this.right_modPx = Math.max(this.right_modPx, extra.right);
|
307 | }
|
308 | }
|
309 | });
|
310 |
|
311 | return Note;
|
312 | }()); |
\ | No newline at end of file |