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[2];
222 static double extend_factor = 0.25;
223 cairo_text_extents_t extents;
226 void _draw_ledger_line (double line, double offset, double width) {
227 cairo_move_to (cr, offset - extend_factor * width / 2.0,
228 score->space_height * line + score->line_width / 2.0);
229 cairo_rel_line_to (cr, (1 + extend_factor) * width, 0);
235 /* Move right so that X==0 is natural position for non-displaced
238 cairo_translate (cr, score->space_height, 0);
240 /* Which line should the note appear on? Line 0 is the top line of
241 * the staff and increasing downwards. (Negative values indicate a
242 * note on a ledger line above the staff). Values half way between
243 * integers indicate notes appearing on a space between two staff
244 * lines (or ledger lines). */
245 line = _score_note_to_line (staff, note);
247 cairo_select_font_face (cr, "Gonville-26", 0, 0);
248 cairo_set_font_size (cr, score->staff_height);
250 /* XXX: The hard-coded glyph indices here are very ugly. We should
251 * figure out how to lookup glyphs by name from this font. */
252 switch (SCORE_PITCH_ACCIDENTAL (note->pitch)) {
253 case SCORE_PITCH_ACCIDENTAL_DOUBLE_FLAT:
254 note_glyph[num_glyphs].index = 77;
256 case SCORE_PITCH_ACCIDENTAL_FLAT:
257 note_glyph[num_glyphs].index = 68;
259 case SCORE_PITCH_ACCIDENTAL_NATURAL:
260 note_glyph[num_glyphs].index = 101;
262 case SCORE_PITCH_ACCIDENTAL_SHARP:
263 note_glyph[num_glyphs].index = 134;
265 case SCORE_PITCH_ACCIDENTAL_DOUBLE_SHARP:
266 note_glyph[num_glyphs].index = 142;
270 if (SCORE_PITCH_ACCIDENTAL (note->pitch) != SCORE_PITCH_ACCIDENTAL_NATURAL)
272 note_glyph[num_glyphs].x = 0;
274 note_glyph[num_glyphs].y = score->space_height * line;
278 cairo_glyph_extents (cr, note_glyph, num_glyphs, &extents);
280 #define ACCIDENTAL_NOTE_SPACING (score->space_height * .15)
282 note_glyph[0].x = - (extents.width + ACCIDENTAL_NOTE_SPACING);
285 switch (note->duration) {
286 case SCORE_DURATION_1:
287 note_glyph[num_glyphs].index = 127;
289 case SCORE_DURATION_2:
290 note_glyph[num_glyphs].index = 85;
292 case SCORE_DURATION_4:
293 case SCORE_DURATION_8:
294 case SCORE_DURATION_16:
295 case SCORE_DURATION_32:
296 case SCORE_DURATION_64:
297 case SCORE_DURATION_128:
299 note_glyph[num_glyphs].index = 84;
302 note_glyph[num_glyphs].x = 0;
303 note_glyph[num_glyphs].y = score->space_height * line;
307 if (line < 0 || line > 4) {
308 double offset, width;
311 cairo_glyph_extents (cr, note_glyph, num_glyphs, &extents);
312 offset = note_glyph[0].x + extents.x_bearing;
313 width = extents.width;
316 for (i = -1; i >= line; i--)
317 _draw_ledger_line (i, offset, width);
319 for (i = 5; i <= line; i++)
320 _draw_ledger_line (i, offset, width);
324 cairo_set_source_rgb (cr,
328 cairo_show_glyphs (cr, note_glyph, num_glyphs);
334 _draw_staff (score_t *score, cairo_t *cr,
335 score_staff_t *staff, int staff_width)
338 cairo_glyph_t clef_glyph;
342 cairo_translate (cr, 0, staff->y_pos);
344 cairo_select_font_face (cr, "Gonville-26", 0, 0);
346 cairo_set_font_size (cr, score->staff_height);
348 /* XXX: The hard-coded glyph indices here are very ugly. We should
349 * figure out how to lookup glyphs by name from this font. */
350 switch (staff->clef) {
353 clef_glyph.index = 46;
354 clef_glyph.y = 3 * score->space_height;
357 clef_glyph.index = 45;
358 clef_glyph.y = 1 * score->space_height;
361 clef_glyph.x = 3 * score->line_width;
362 clef_glyph.y += score->line_width / 2.0;
364 cairo_set_source_rgb (cr, 0.0, 0.0, 0.0); /* black */
365 cairo_show_glyphs (cr, &clef_glyph, 1);
367 /* Draw staff lines */
368 for (i = 0; i < 5; i++) {
369 cairo_move_to (cr, 0, i * score->space_height + score->line_width / 2.0);
370 cairo_rel_line_to (cr, staff_width, 0);
373 cairo_set_line_width (cr, score->line_width);
375 cairo_set_source_rgb (cr, 0.0, 0.0, 0.0); /* black */
378 /* Make space for clef before drawing notes */
379 cairo_translate (cr, (int) (4 * score->space_height), 0);
381 /* Draw chord symbols */
384 for (i = 0; i < staff->num_chords; i++) {
385 _draw_chord (score, cr, staff, staff->chords[i]);
386 cairo_translate (cr, staff->chords[i]->width, 0.0);
392 for (i = 0; i < staff->num_notes; i++) {
393 _draw_note (score, cr, staff, staff->notes[i]);
394 /* Draw all notes concurrent for now (as a chord)
395 cairo_translate (cr, score->space_height * 2.0, 0);
403 score_draw (score_t *score, cairo_t *cr)
406 int staff_width = score->width;
409 if (score->num_staves == 0)
414 /* Before drawing anything, position each staff based on the size
415 * of each (including ledger lines) */
417 for (i = 0; i < score->num_staves; i++) {
418 score_staff_t *staff = score->staves[i];
419 staff_y_pos += staff->upper_ledger_lines * score->space_height;
420 staff->y_pos = staff_y_pos;
421 staff_y_pos += (score->staff_height +
422 staff->lower_ledger_lines * score->space_height +
423 score->staff_height);
426 if (score->num_braces)
430 for (i = 0; i < score->num_braces; i++)
431 _draw_brace (score, cr, score->braces[i], &brace_width);
433 /* Subtract space for brace itself */
434 cairo_translate (cr, brace_width, 0);
435 staff_width -= brace_width;
437 /* As well as some padding */
438 cairo_translate (cr, 2, 0);
442 /* Vertical lines at each end */
444 score->line_width / 2.0,
445 score->staves[0]->y_pos + score->line_width / 2.0,
446 staff_width - score->line_width,
447 score->staves[score->num_staves-1]->y_pos + score->staff_height - score->staves[0]->y_pos);
448 cairo_set_source_rgb (cr, 0.0, 0.0, 0.0); /* black */
449 cairo_set_line_width (cr, score->line_width);
452 for (i = 0; i < score->num_staves; i++) {
453 score_staff_t *staff = score->staves[i];
454 _draw_staff (score, cr, staff, staff_width);
461 score_add_brace (score_t *score, int staves)
463 score_brace_t *brace;
465 brace = talloc (score, score_brace_t);
469 brace->first_staff = score->num_staves;
470 brace->num_staves = staves;
473 score->braces = talloc_realloc (score,
477 if (score->braces == NULL) {
478 score->num_braces = 0;
482 score->braces[score->num_braces - 1] = brace;
487 score_add_staff (score_t *score, score_clef_t clef)
489 score_staff_t *staff;
491 staff = talloc (score, score_staff_t);
498 staff->num_notes = 0;
500 staff->chords = NULL;
501 staff->num_chords = 0;
503 staff->upper_ledger_lines = 0;
504 staff->lower_ledger_lines = 0;
507 score->staves = talloc_realloc (score,
511 if (score->staves == NULL) {
512 score->num_staves = 0;
516 score->staves[score->num_staves - 1] = staff;
522 score_add_chord (score_staff_t *staff,
525 score_chord_t *chord;
527 chord = talloc (staff, score_chord_t);
531 chord->staff = staff;
532 chord->name = talloc_strdup (chord, name);
534 /* The width will get set correctly the first time _draw_chord is
539 staff->chords = talloc_realloc (staff,
543 if (staff->chords == NULL) {
544 staff->num_chords = 0;
548 staff->chords[staff->num_chords - 1] = chord;
554 score_remove_chord (score_chord_t *chord)
556 score_staff_t *staff = chord->staff;
559 for (i = 0; i < staff->num_chords; i++)
560 if (staff->chords[i] == chord)
563 if (i == staff->num_chords)
566 if (i < staff->num_chords - 1)
568 memmove (staff->chords + i,
569 staff->chords + i + 1,
570 (staff->num_chords - 1 - i) * sizeof (score_chord_t *));
573 staff->num_chords -= 1;
577 score_add_note (score_staff_t *staff,
580 score_duration_t duration)
586 /* Return existing note if already present. */
587 for (i = 0; i < staff->num_notes; i++) {
588 note = staff->notes[i];
589 if (note->pitch == pitch &&
590 note->octave == octave &&
591 note->duration == duration)
597 note = talloc (staff, score_note_t);
603 note->octave = octave;
604 note->duration = duration;
610 line = _score_note_to_line (staff, note);
612 int lines = (int) (- line);
613 if (lines > staff->upper_ledger_lines)
614 staff->upper_ledger_lines = lines;
616 int lines = (int) (line - 4);
617 if (lines > staff->lower_ledger_lines)
618 staff->lower_ledger_lines = lines;
622 staff->notes = talloc_realloc (staff,
626 if (staff->notes == NULL) {
627 staff->num_notes = 0;
631 staff->notes[staff->num_notes - 1] = note;
637 score_remove_note (score_note_t *note)
639 score_staff_t *staff = note->staff;
642 for (i = 0; i < staff->num_notes; i++)
643 if (staff->notes[i] == note)
646 if (i == staff->num_notes)
649 if (i < staff->num_notes - 1)
651 memmove (staff->notes + i,
652 staff->notes + i + 1,
653 (staff->num_notes - 1 - i) * sizeof (score_note_t *));
656 staff->num_notes -= 1;
658 if (staff->num_notes == 0) {
659 staff->upper_ledger_lines = 0;
660 staff->lower_ledger_lines = 0;
665 score_set_note_color_rgb (score_note_t *note,
676 score_staff_find_note (score_staff_t *staff,
679 score_duration_t duration)
684 for (i = 0; i < staff->num_notes; i++) {
685 note = staff->notes[i];
686 if (note->pitch == pitch &&
687 note->octave == octave &&
688 note->duration == duration)