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/ .
29 score_chord_t **chords;
35 /* How many ledger lines are needed for current notes */
36 int upper_ledger_lines;
37 int lower_ledger_lines;
39 /* Y position of top full line of staff */
43 typedef struct score_brace
51 /* Nominal height of a single staff (ledger lines may make it larger) */
54 /* Height of one space within a staff */
57 /* Minimal line width for staff lines */
60 /* Full width of staff */
63 score_brace_t **braces;
67 score_staff_t **staves;
72 score_create (void *ctx)
76 score = talloc (ctx, score_t);
80 /* Also sets space_height and line_width */
81 score_set_staff_height (score, 76);
83 /* Just to have some nominal width. */
87 score->num_braces = 0;
90 score->num_staves = 0;
96 score_set_staff_height (score_t *score, int height)
98 score->space_height = (int) height / 4;
99 score->staff_height = score->space_height * 4;
101 score->line_width = score->space_height / 10;
102 if (score->line_width == 0)
103 score->line_width = 1;
105 return score->staff_height;
109 score_set_width (score_t *score, int width)
111 score->width = width;
114 /* Returns in brace_width the width of the brace */
116 _draw_brace (score_t *score, cairo_t *cr,
117 score_brace_t *brace, int *brace_width)
119 cairo_glyph_t brace_glyph;
120 cairo_text_extents_t brace_extents;
123 if (brace->num_staves == 0)
128 top = score->staves[brace->first_staff]->y_pos;
129 bottom = score->staves[brace->first_staff + brace->num_staves - 1]->y_pos + score->staff_height;
131 cairo_select_font_face (cr, "Gonville-Brace", 0, 0);
133 /* XXX: This hard-coded glyph index is pretty ugly. We should
134 * figure out how to lookup the glyph we want, (though, as it
135 * turns out, this brace font pretty much just has numbered glyph
136 * names for different sizes, so it wouldn't be all that different
137 * than just the bare index here). */
138 brace_glyph.index = 300;
140 brace_glyph.y = top + (bottom - top) / 2.0 + score->line_width / 2.0;
142 /* XXX: This font size (in conjunction with the glyph selection)
143 * is a rough guess at best. We should figure out how the brace
144 * font is intended to be used and actually measure to find the
145 * correctly-sized glyph. */
146 cairo_set_font_size (cr, (bottom - top) / 3.85);
148 cairo_glyph_extents (cr, &brace_glyph, 1, &brace_extents);
150 /* Subtract space for brace itself */
151 cairo_translate (cr, -brace_extents.x_bearing, 0);
153 cairo_set_source_rgb (cr, 0.0, 0.0, 0.0); /* black */
154 cairo_show_glyphs (cr, &brace_glyph, 1);
158 *brace_width = (int) -brace_extents.x_bearing;
161 /* Line containing middle C for the given clef. */
163 _score_clef_c_line (score_clef_t clef)
176 _score_note_to_line (score_staff_t *staff, score_note_t *note)
178 score_pitch_name_t name = SCORE_PITCH_NAME (note->pitch);
179 int c_line = _score_clef_c_line (staff->clef);
181 return c_line - (name - SCORE_PITCH_NAME_C) / 2.0 - 3.5 * (note->octave - 4);
184 /* chord->width is updated as a side effect */
186 _draw_chord (score_t *score, cairo_t *cr,
187 score_staff_t *staff, score_chord_t *chord)
189 cairo_text_extents_t chord_extents;
190 double total_staff_height;
192 /* XXX: The staff should manage this height itself. */
193 total_staff_height = (staff->upper_ledger_lines * score->space_height +
194 score->staff_height +
195 staff->lower_ledger_lines * score->space_height);
199 cairo_text_extents (cr, chord->name, &chord_extents);
201 cairo_select_font_face (cr, "serif", 0, 0);
202 cairo_set_font_size (cr, score->space_height * 3);
204 if (staff->clef == SCORE_CLEF_G)
205 cairo_move_to (cr, 0, - score->space_height * 0.5);
207 cairo_move_to (cr, 0, score->space_height * 0.5 + total_staff_height + chord_extents.y_bearing);
209 cairo_show_text (cr, chord->name);
211 chord->width = chord_extents.x_advance;
217 _draw_note (score_t *score, cairo_t *cr,
218 score_staff_t *staff, score_note_t *note)
221 cairo_glyph_t note_glyph;
222 static double extend_factor = 0.25;
224 void _draw_ledger_line (double line, double width) {
226 - width * extend_factor / 2.0,
227 score->space_height * line + score->line_width / 2.0);
228 cairo_rel_line_to (cr, (1 + extend_factor) * width, 0);
234 /* Which line should the note appear on? Line 0 is the top line of
235 * the staff and increasing downwards. (Negative values indicate a
236 * note on a ledger line above the staff). Values half way between
237 * integers indicate notes appearing on a space between two staff
238 * lines (or ledger lines). */
239 line = _score_note_to_line (staff, note);
241 cairo_select_font_face (cr, "Gonville-26", 0, 0);
242 cairo_set_font_size (cr, score->staff_height);
244 /* XXX: The hard-coded glyph indices here are very ugly. We should
245 * figure out how to lookup glyphs by name from this font. */
246 switch (note->duration) {
247 case SCORE_DURATION_1:
248 note_glyph.index = 127;
250 case SCORE_DURATION_2:
251 note_glyph.index = 85;
253 case SCORE_DURATION_4:
254 case SCORE_DURATION_8:
255 case SCORE_DURATION_16:
256 case SCORE_DURATION_32:
257 case SCORE_DURATION_64:
258 case SCORE_DURATION_128:
260 note_glyph.index = 84;
264 note_glyph.y = score->space_height * line;
266 if (line < 0 || line > 4) {
268 cairo_text_extents_t note_extents;
270 cairo_glyph_extents (cr, ¬e_glyph, 1, ¬e_extents);
273 for (i = -1; i >= line; i--)
274 _draw_ledger_line (i, note_extents.width);
276 for (i = 5; i <= line; i++)
277 _draw_ledger_line (i, note_extents.width);
281 cairo_set_source_rgb (cr,
285 cairo_show_glyphs (cr, ¬e_glyph, 1);
291 _draw_staff (score_t *score, cairo_t *cr,
292 score_staff_t *staff, int staff_width)
295 cairo_glyph_t clef_glyph;
299 cairo_translate (cr, 0, staff->y_pos);
301 cairo_select_font_face (cr, "Gonville-26", 0, 0);
303 cairo_set_font_size (cr, score->staff_height);
305 /* XXX: The hard-coded glyph indices here are very ugly. We should
306 * figure out how to lookup glyphs by name from this font. */
307 switch (staff->clef) {
310 clef_glyph.index = 46;
311 clef_glyph.y = 3 * score->space_height;
314 clef_glyph.index = 45;
315 clef_glyph.y = 1 * score->space_height;
318 clef_glyph.x = 3 * score->line_width;
319 clef_glyph.y += score->line_width / 2.0;
321 cairo_set_source_rgb (cr, 0.0, 0.0, 0.0); /* black */
322 cairo_show_glyphs (cr, &clef_glyph, 1);
324 /* Draw staff lines */
325 for (i = 0; i < 5; i++) {
326 cairo_move_to (cr, 0, i * score->space_height + score->line_width / 2.0);
327 cairo_rel_line_to (cr, staff_width, 0);
330 cairo_set_line_width (cr, score->line_width);
332 cairo_set_source_rgb (cr, 0.0, 0.0, 0.0); /* black */
335 /* Make space for clef before drawing notes */
336 cairo_translate (cr, (int) (4 * score->space_height), 0);
338 /* Draw chord symbols */
341 for (i = 0; i < staff->num_chords; i++) {
342 _draw_chord (score, cr, staff, staff->chords[i]);
343 cairo_translate (cr, staff->chords[i]->width, 0.0);
349 for (i = 0; i < staff->num_notes; i++) {
350 _draw_note (score, cr, staff, staff->notes[i]);
351 /* Draw all notes concurrent for now (as a chord)
352 cairo_translate (cr, score->space_height * 2.0, 0);
360 score_draw (score_t *score, cairo_t *cr)
363 int staff_width = score->width;
366 if (score->num_staves == 0)
371 /* Before drawing anything, position each staff based on the size
372 * of each (including ledger lines) */
374 for (i = 0; i < score->num_staves; i++) {
375 score_staff_t *staff = score->staves[i];
376 staff_y_pos += staff->upper_ledger_lines * score->space_height;
377 staff->y_pos = staff_y_pos;
378 staff_y_pos += (score->staff_height +
379 staff->lower_ledger_lines * score->space_height +
380 score->staff_height);
383 if (score->num_braces)
387 for (i = 0; i < score->num_braces; i++)
388 _draw_brace (score, cr, score->braces[i], &brace_width);
390 /* Subtract space for brace itself */
391 cairo_translate (cr, brace_width, 0);
392 staff_width -= brace_width;
394 /* As well as some padding */
395 cairo_translate (cr, 2, 0);
399 /* Vertical lines at each end */
401 score->line_width / 2.0,
402 score->staves[0]->y_pos + score->line_width / 2.0,
403 staff_width - score->line_width,
404 score->staves[score->num_staves-1]->y_pos + score->staff_height - score->staves[0]->y_pos);
405 cairo_set_source_rgb (cr, 0.0, 0.0, 0.0); /* black */
406 cairo_set_line_width (cr, score->line_width);
409 for (i = 0; i < score->num_staves; i++) {
410 score_staff_t *staff = score->staves[i];
411 _draw_staff (score, cr, staff, staff_width);
418 score_add_brace (score_t *score, int staves)
420 score_brace_t *brace;
422 brace = talloc (score, score_brace_t);
426 brace->first_staff = score->num_staves;
427 brace->num_staves = staves;
430 score->braces = talloc_realloc (score,
434 if (score->braces == NULL) {
435 score->num_braces = 0;
439 score->braces[score->num_braces - 1] = brace;
444 score_add_staff (score_t *score, score_clef_t clef)
446 score_staff_t *staff;
448 staff = talloc (score, score_staff_t);
455 staff->num_notes = 0;
457 staff->chords = NULL;
458 staff->num_chords = 0;
460 staff->upper_ledger_lines = 0;
461 staff->lower_ledger_lines = 0;
464 score->staves = talloc_realloc (score,
468 if (score->staves == NULL) {
469 score->num_staves = 0;
473 score->staves[score->num_staves - 1] = staff;
479 score_add_chord (score_staff_t *staff,
482 score_chord_t *chord;
484 chord = talloc (staff, score_chord_t);
488 chord->staff = staff;
489 chord->name = talloc_strdup (chord, name);
491 /* The width will get set correctly the first time _draw_chord is
496 staff->chords = talloc_realloc (staff,
500 if (staff->chords == NULL) {
501 staff->num_chords = 0;
505 staff->chords[staff->num_chords - 1] = chord;
511 score_remove_chord (score_chord_t *chord)
513 score_staff_t *staff = chord->staff;
516 for (i = 0; i < staff->num_chords; i++)
517 if (staff->chords[i] == chord)
520 if (i == staff->num_chords)
523 if (i < staff->num_chords - 1)
525 memmove (staff->chords + i,
526 staff->chords + i + 1,
527 (staff->num_chords - 1 - i) * sizeof (score_chord_t *));
530 staff->num_chords -= 1;
534 score_add_note (score_staff_t *staff,
537 score_duration_t duration)
542 note = talloc (staff, score_note_t);
548 note->octave = octave;
549 note->duration = duration;
555 line = _score_note_to_line (staff, note);
557 int lines = (int) (- line);
558 if (lines > staff->upper_ledger_lines)
559 staff->upper_ledger_lines = lines;
561 int lines = (int) (line - 4);
562 if (lines > staff->lower_ledger_lines)
563 staff->lower_ledger_lines = lines;
567 staff->notes = talloc_realloc (staff,
571 if (staff->notes == NULL) {
572 staff->num_notes = 0;
576 staff->notes[staff->num_notes - 1] = note;
582 score_remove_note (score_note_t *note)
584 score_staff_t *staff = note->staff;
587 for (i = 0; i < staff->num_notes; i++)
588 if (staff->notes[i] == note)
591 if (i == staff->num_notes)
594 if (i < staff->num_notes - 1)
596 memmove (staff->notes + i,
597 staff->notes + i + 1,
598 (staff->num_notes - 1 - i) * sizeof (score_note_t *));
601 staff->num_notes -= 1;
603 if (staff->num_notes == 0) {
604 staff->upper_ledger_lines = 0;
605 staff->lower_ledger_lines = 0;
610 score_set_note_color_rgb (score_note_t *note,
621 score_staff_find_note (score_staff_t *staff,
624 score_duration_t duration)
629 for (i = 0; i < staff->num_notes; i++) {
630 note = staff->notes[i];
631 if (note->pitch == pitch &&
632 note->octave == octave &&
633 note->duration == duration)