1 /* scherzo - Music notation training
3 * score - Utilities for drawing (simple) musical scores
5 * Copyright © 2010 Carl Worth
7 * This program is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program. If not, see http://www.gnu.org/licenses/ .
32 /* How many ledger lines are needed for current notes */
33 int upper_ledger_lines;
34 int lower_ledger_lines;
36 /* Y position of top full line of staff */
40 typedef struct score_brace
48 /* Nominal height of a single staff (ledger lines may make it larger) */
51 /* Height of one space within a staff */
54 /* Minimal line width for staff lines */
57 /* Full width of staff */
60 score_brace_t **braces;
64 score_staff_t **staves;
69 score_create (void *ctx)
73 score = talloc (ctx, score_t);
77 /* Also sets space_height and line_width */
78 score_set_staff_height (score, 24);
80 /* Just to have some nominal width. */
84 score->num_braces = 0;
87 score->num_staves = 0;
93 score_set_staff_height (score_t *score, int height)
95 score->space_height = (int) height / 4;
96 score->staff_height = score->space_height * 4;
98 score->line_width = score->space_height / 10;
99 if (score->line_width == 0)
100 score->line_width = 1;
102 return score->staff_height;
106 score_set_width (score_t *score, int width)
108 score->width = width;
111 /* Returns in brace_width the width of the brace */
113 _draw_brace (score_t *score, cairo_t *cr,
114 score_brace_t *brace, int *brace_width)
116 cairo_glyph_t brace_glyph;
117 cairo_text_extents_t brace_extents;
120 if (brace->num_staves == 0)
125 top = score->staves[brace->first_staff]->y_pos;
126 bottom = score->staves[brace->first_staff + brace->num_staves - 1]->y_pos + score->staff_height;
128 cairo_select_font_face (cr, "Gonville-Brace", 0, 0);
130 /* XXX: This hard-coded glyph index is pretty ugly. We should
131 * figure out how to lookup the glyph we want, (though, as it
132 * turns out, this brace font pretty much just has numbered glyph
133 * names for different sizes, so it wouldn't be all that different
134 * than just the bare index here). */
135 brace_glyph.index = 300;
137 brace_glyph.y = top + (bottom - top) / 2.0 + score->line_width / 2.0;
139 /* XXX: This font size (in conjunction with the glyph selection)
140 * is a rough guess at best. We should figure out how the brace
141 * font is intended to be used and actually measure to find the
142 * correctly-sized glyph. */
143 cairo_set_font_size (cr, (bottom - top) / 3.85);
145 cairo_glyph_extents (cr, &brace_glyph, 1, &brace_extents);
147 /* Subtract space for brace itself */
148 cairo_translate (cr, -brace_extents.x_bearing, 0);
150 cairo_set_source_rgb (cr, 0.0, 0.0, 0.0); /* black */
151 cairo_show_glyphs (cr, &brace_glyph, 1);
155 *brace_width = (int) -brace_extents.x_bearing;
158 /* Line containing middle C for the given clef. */
160 _score_clef_c_line (score_clef_t clef)
173 _score_note_to_line (score_staff_t *staff, score_note_t *note)
175 score_pitch_name_t name = SCORE_PITCH_NAME (note->pitch);
176 int c_line = _score_clef_c_line (staff->clef);
178 return c_line - (name - SCORE_PITCH_NAME_C) / 2.0 - 3.5 * (note->octave - 4);
182 _draw_note (score_t *score, cairo_t *cr,
183 score_staff_t *staff, score_note_t *note)
186 cairo_glyph_t note_glyph;
187 static double extend_factor = 0.25;
189 void _draw_ledger_line (double line, double width) {
191 - width * extend_factor / 2.0,
192 score->space_height * line + score->line_width / 2.0);
193 cairo_rel_line_to (cr, (1 + extend_factor) * width, 0);
199 /* Which line should the note appear on? Line 0 is the top line of
200 * the staff and increasing downwards. (Negative values indicate a
201 * note on a ledger line above the staff). Values half way between
202 * integers indicate notes appearing on a space between two staff
203 * lines (or ledger lines). */
204 line = _score_note_to_line (staff, note);
206 cairo_select_font_face (cr, "Gonville-26", 0, 0);
207 cairo_set_font_size (cr, score->staff_height);
209 /* XXX: The hard-coded glyph indices here are very ugly. We should
210 * figure out how to lookup glyphs by name from this font. */
211 switch (note->duration) {
212 case SCORE_DURATION_1:
213 note_glyph.index = 127;
215 case SCORE_DURATION_2:
216 note_glyph.index = 85;
218 case SCORE_DURATION_4:
219 case SCORE_DURATION_8:
220 case SCORE_DURATION_16:
221 case SCORE_DURATION_32:
222 case SCORE_DURATION_64:
223 case SCORE_DURATION_128:
225 note_glyph.index = 84;
229 note_glyph.y = score->space_height * line;
231 if (line < 0 || line > 4) {
233 cairo_text_extents_t note_extents;
235 cairo_glyph_extents (cr, ¬e_glyph, 1, ¬e_extents);
238 for (i = -1; i >= line; i--)
239 _draw_ledger_line (i, note_extents.width);
241 for (i = 5; i <= line; i++)
242 _draw_ledger_line (i, note_extents.width);
246 cairo_set_source_rgb (cr, 0.0, 0.0, 0.0); /* black */
247 cairo_show_glyphs (cr, ¬e_glyph, 1);
253 _draw_staff (score_t *score, cairo_t *cr,
254 score_staff_t *staff, int staff_width)
257 cairo_glyph_t clef_glyph;
261 cairo_translate (cr, 0, staff->y_pos);
263 cairo_select_font_face (cr, "Gonville-26", 0, 0);
265 cairo_set_font_size (cr, score->staff_height);
267 /* XXX: The hard-coded glyph indices here are very ugly. We should
268 * figure out how to lookup glyphs by name from this font. */
269 switch (staff->clef) {
272 clef_glyph.index = 46;
273 clef_glyph.y = 3 * score->space_height;
276 clef_glyph.index = 45;
277 clef_glyph.y = 1 * score->space_height;
280 clef_glyph.x = 3 * score->line_width;
281 clef_glyph.y += score->line_width / 2.0;
283 cairo_set_source_rgb (cr, 0.0, 0.0, 0.0); /* black */
284 cairo_show_glyphs (cr, &clef_glyph, 1);
286 /* Draw staff lines */
287 for (i = 0; i < 5; i++) {
288 cairo_move_to (cr, 0, i * score->space_height + score->line_width / 2.0);
289 cairo_rel_line_to (cr, staff_width, 0);
292 cairo_set_line_width (cr, score->line_width);
294 cairo_set_source_rgb (cr, 0.0, 0.0, 0.0); /* black */
297 /* Make space for clef before drawing notes */
298 cairo_translate (cr, (int) (4 * score->space_height), 0);
301 for (i = 0; i < staff->num_notes; i++) {
302 _draw_note (score, cr, staff, staff->notes[i]);
303 /* Draw all notes concurrent for now (as a chord)
304 cairo_translate (cr, score->space_height * 2.0, 0);
312 score_draw (score_t *score, cairo_t *cr)
315 int staff_width = score->width;
318 if (score->num_staves == 0)
323 /* Before drawing anything, position each staff based on the size
324 * of each (including ledger lines) */
326 for (i = 0; i < score->num_staves; i++) {
327 score_staff_t *staff = score->staves[i];
328 staff_y_pos += staff->upper_ledger_lines * score->space_height;
329 staff->y_pos = staff_y_pos;
330 staff_y_pos += (score->staff_height +
331 staff->lower_ledger_lines * score->space_height +
332 score->staff_height);
335 if (score->num_braces)
339 for (i = 0; i < score->num_braces; i++)
340 _draw_brace (score, cr, score->braces[i], &brace_width);
342 /* Subtract space for brace itself */
343 cairo_translate (cr, brace_width, 0);
344 staff_width -= brace_width;
346 /* As well as some padding */
347 cairo_translate (cr, 2, 0);
351 /* Vertical lines at each end */
353 score->line_width / 2.0,
354 score->staves[0]->y_pos + score->line_width / 2.0,
355 staff_width - score->line_width,
356 score->staves[score->num_staves-1]->y_pos + score->staff_height - score->staves[0]->y_pos);
357 cairo_set_source_rgb (cr, 0.0, 0.0, 0.0); /* black */
358 cairo_set_line_width (cr, score->line_width);
361 for (i = 0; i < score->num_staves; i++) {
362 score_staff_t *staff = score->staves[i];
363 _draw_staff (score, cr, staff, staff_width);
370 score_add_brace (score_t *score, int staves)
372 score_brace_t *brace;
374 brace = talloc (score, score_brace_t);
378 brace->first_staff = score->num_staves;
379 brace->num_staves = staves;
382 score->braces = talloc_realloc (score,
386 if (score->braces == NULL) {
387 score->num_braces = 0;
391 score->braces[score->num_braces - 1] = brace;
396 score_add_staff (score_t *score, score_clef_t clef)
398 score_staff_t *staff;
400 staff = talloc (score, score_staff_t);
407 staff->num_notes = 0;
409 staff->upper_ledger_lines = 0;
410 staff->lower_ledger_lines = 0;
413 score->staves = talloc_realloc (score,
417 if (score->staves == NULL) {
418 score->num_staves = 0;
422 score->staves[score->num_staves - 1] = staff;
428 score_add_note (score_staff_t *staff,
431 score_duration_t duration)
436 note = talloc (staff, score_note_t);
442 note->octave = octave;
443 note->duration = duration;
445 line = _score_note_to_line (staff, note);
447 int lines = (int) (- line);
448 if (lines > staff->upper_ledger_lines)
449 staff->upper_ledger_lines = lines;
451 int lines = (int) (line - 4);
452 if (lines > staff->lower_ledger_lines)
453 staff->lower_ledger_lines = lines;
457 staff->notes = talloc_realloc (staff,
461 if (staff->notes == NULL) {
462 staff->num_notes = 0;
466 staff->notes[staff->num_notes - 1] = note;
472 score_remove_note (score_note_t *note)
474 score_staff_t *staff = note->staff;
477 for (i = 0; i < staff->num_notes; i++)
478 if (staff->notes[i] == note)
481 if (i == staff->num_notes)
484 if (i < staff->num_notes - 1)
486 memmove (staff->notes + i,
487 staff->notes + i + 1,
488 (staff->num_notes - 1 - i) * sizeof (score_note_t *));
491 staff->num_notes -= 1;
495 score_staff_find_note (score_staff_t *staff,
498 score_duration_t duration)
503 for (i = 0; i < staff->num_notes; i++) {
504 note = staff->notes[i];
505 if (note->pitch == pitch &&
506 note->octave == octave &&
507 note->duration == duration)