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/ .
21 #include <pango/pangocairo.h>
32 score_chord_t **chords;
38 /* How many ledger lines are needed for current notes */
39 int upper_ledger_lines;
40 int lower_ledger_lines;
42 /* Y position of top full line of staff */
46 typedef struct score_brace
54 /* Nominal height of a single staff (ledger lines may make it larger) */
57 /* Height of one space within a staff */
60 /* Minimal line width for staff lines */
63 /* Full width of staff */
66 score_brace_t **braces;
70 score_staff_t **staves;
75 score_create (void *ctx)
79 score = talloc (ctx, score_t);
83 /* Also sets space_height and line_width */
84 score_set_staff_height (score, 76);
86 /* Just to have some nominal width. */
90 score->num_braces = 0;
93 score->num_staves = 0;
99 score_set_staff_height (score_t *score, int height)
101 score->space_height = (int) height / 4;
102 score->staff_height = score->space_height * 4;
104 score->line_width = score->space_height / 10;
105 if (score->line_width == 0)
106 score->line_width = 1;
108 return score->staff_height;
112 score_set_width (score_t *score, int width)
114 score->width = width;
117 /* Returns in brace_width the width of the brace */
119 _draw_brace (score_t *score, cairo_t *cr,
120 score_brace_t *brace, int *brace_width)
122 cairo_glyph_t brace_glyph;
123 cairo_text_extents_t brace_extents;
126 if (brace->num_staves == 0)
131 top = score->staves[brace->first_staff]->y_pos;
132 bottom = score->staves[brace->first_staff + brace->num_staves - 1]->y_pos + score->staff_height;
134 cairo_select_font_face (cr, "Gonville-Brace", 0, 0);
136 /* XXX: This hard-coded glyph index is pretty ugly. We should
137 * figure out how to lookup the glyph we want, (though, as it
138 * turns out, this brace font pretty much just has numbered glyph
139 * names for different sizes, so it wouldn't be all that different
140 * than just the bare index here). */
141 brace_glyph.index = 300;
143 brace_glyph.y = top + (bottom - top) / 2.0 + score->line_width / 2.0;
145 /* XXX: This font size (in conjunction with the glyph selection)
146 * is a rough guess at best. We should figure out how the brace
147 * font is intended to be used and actually measure to find the
148 * correctly-sized glyph. */
149 cairo_set_font_size (cr, (bottom - top) / 3.85);
151 cairo_glyph_extents (cr, &brace_glyph, 1, &brace_extents);
153 /* Subtract space for brace itself */
154 cairo_translate (cr, -brace_extents.x_bearing, 0);
156 cairo_set_source_rgb (cr, 0.0, 0.0, 0.0); /* black */
157 cairo_show_glyphs (cr, &brace_glyph, 1);
161 *brace_width = (int) -brace_extents.x_bearing;
164 /* Line containing middle C for the given clef. */
166 _score_clef_c_line (score_clef_t clef)
178 /* On which line would 'pitch' appear on 'staff'.
180 * Lines are numbered with line 0 as the top full line of the staff
181 * and increasing downward. So line values less than 0 will appear as
182 * ledger lines above the staff while line values greater than 4 will
183 * appear on ledger lines below the staff.
185 * A line value of 2 will be centered verticall on the staff.
187 * For notes appearing on a space, the line value will be half-way
188 * between two integers. */
190 _score_staff_pitch_to_line (score_staff_t *staff, pitch_t pitch)
192 pitch_name_t name = PITCH_NAME (pitch);
193 int octave = PITCH_OCTAVE (pitch);
194 int c_line = _score_clef_c_line (staff->clef);
196 return c_line - (name - PITCH_NAME_C) / 2.0 - 3.5 * (octave - 4);
199 /* chord->width is updated as a side effect */
201 _draw_chord (score_t *score, cairo_t *cr,
202 score_staff_t *staff, score_chord_t *chord)
204 PangoRectangle ink_extents;
205 PangoRectangle logical_extents;
206 double total_staff_height;
208 PangoFontDescription *font_description;
210 /* XXX: The staff should manage this height itself. */
211 total_staff_height = (staff->upper_ledger_lines * score->space_height +
212 score->staff_height +
213 staff->lower_ledger_lines * score->space_height);
217 font_description = pango_font_description_new ();
218 pango_font_description_set_family (font_description, "serif");
219 pango_font_description_set_absolute_size (font_description,
220 score->space_height * 3 * PANGO_SCALE);
222 layout = pango_cairo_create_layout (cr);
223 pango_layout_set_font_description (layout, font_description);
224 pango_layout_set_markup (layout, chord->name, -1);
226 pango_layout_line_get_pixel_extents (pango_layout_get_line (layout, 0),
227 &ink_extents, &logical_extents);
229 if (staff->clef == SCORE_CLEF_G)
230 cairo_move_to (cr, 0, - score->space_height * 0.5);
232 cairo_move_to (cr, 0, score->space_height * 0.5 + total_staff_height +
233 logical_extents.height);
235 pango_cairo_show_layout_line (cr, pango_layout_get_line (layout, 0));
237 g_object_unref (layout);
238 pango_font_description_free (font_description);
240 chord->width = logical_extents.width;
246 _draw_note (score_t *score, cairo_t *cr,
247 score_staff_t *staff, score_note_t *note)
250 cairo_glyph_t note_glyph[2];
251 static double extend_factor = 0.25;
252 cairo_text_extents_t extents;
255 void _draw_ledger_line (double line, double offset, double width) {
256 cairo_move_to (cr, offset - extend_factor * width / 2.0,
257 score->space_height * line + score->line_width / 2.0);
258 cairo_rel_line_to (cr, (1 + extend_factor) * width, 0);
264 /* Move right so that X==0 is natural position for non-displaced
267 cairo_translate (cr, score->space_height, 0);
269 /* Which line should the note appear on? Line 0 is the top line of
270 * the staff and increasing downwards. (Negative values indicate a
271 * note on a ledger line above the staff). Values half way between
272 * integers indicate notes appearing on a space between two staff
273 * lines (or ledger lines). */
274 line = _score_staff_pitch_to_line (staff, note->pitch);
276 cairo_select_font_face (cr, "Gonville-26", 0, 0);
277 cairo_set_font_size (cr, score->staff_height);
279 /* XXX: The hard-coded glyph indices here are very ugly. We should
280 * figure out how to lookup glyphs by name from this font. */
281 switch (PITCH_ACCIDENTAL (note->pitch)) {
282 case PITCH_ACCIDENTAL_DOUBLE_FLAT:
283 note_glyph[num_glyphs].index = 77;
285 case PITCH_ACCIDENTAL_FLAT:
286 note_glyph[num_glyphs].index = 68;
288 case PITCH_ACCIDENTAL_NATURAL:
289 note_glyph[num_glyphs].index = 101;
291 case PITCH_ACCIDENTAL_SHARP:
292 note_glyph[num_glyphs].index = 134;
294 case PITCH_ACCIDENTAL_DOUBLE_SHARP:
295 note_glyph[num_glyphs].index = 142;
299 if (PITCH_ACCIDENTAL (note->pitch) != PITCH_ACCIDENTAL_NATURAL)
301 note_glyph[num_glyphs].x = 0;
303 note_glyph[num_glyphs].y = score->space_height * line;
307 cairo_glyph_extents (cr, note_glyph, num_glyphs, &extents);
309 #define ACCIDENTAL_NOTE_SPACING (score->space_height * .15)
311 note_glyph[0].x = - (extents.width + ACCIDENTAL_NOTE_SPACING);
314 switch (note->duration) {
315 case SCORE_DURATION_1:
316 note_glyph[num_glyphs].index = 127;
318 case SCORE_DURATION_2:
319 note_glyph[num_glyphs].index = 85;
321 case SCORE_DURATION_4:
322 case SCORE_DURATION_8:
323 case SCORE_DURATION_16:
324 case SCORE_DURATION_32:
325 case SCORE_DURATION_64:
326 case SCORE_DURATION_128:
328 note_glyph[num_glyphs].index = 84;
331 note_glyph[num_glyphs].x = 0;
332 note_glyph[num_glyphs].y = score->space_height * line;
336 if (line < 0 || line > 4) {
337 double offset, width;
340 cairo_glyph_extents (cr, note_glyph, num_glyphs, &extents);
341 offset = note_glyph[0].x + extents.x_bearing;
342 width = extents.width;
345 for (i = -1; i >= line; i--)
346 _draw_ledger_line (i, offset, width);
348 for (i = 5; i <= line; i++)
349 _draw_ledger_line (i, offset, width);
353 cairo_set_source_rgb (cr,
357 cairo_show_glyphs (cr, note_glyph, num_glyphs);
363 _draw_staff (score_t *score, cairo_t *cr,
364 score_staff_t *staff, int staff_width)
367 cairo_glyph_t clef_glyph;
371 cairo_translate (cr, 0, staff->y_pos);
373 cairo_select_font_face (cr, "Gonville-26", 0, 0);
375 cairo_set_font_size (cr, score->staff_height);
377 /* XXX: The hard-coded glyph indices here are very ugly. We should
378 * figure out how to lookup glyphs by name from this font. */
379 switch (staff->clef) {
382 clef_glyph.index = 46;
383 clef_glyph.y = 3 * score->space_height;
386 clef_glyph.index = 45;
387 clef_glyph.y = 1 * score->space_height;
390 clef_glyph.x = 3 * score->line_width;
391 clef_glyph.y += score->line_width / 2.0;
393 cairo_set_source_rgb (cr, 0.0, 0.0, 0.0); /* black */
394 cairo_show_glyphs (cr, &clef_glyph, 1);
396 /* Draw staff lines */
397 for (i = 0; i < 5; i++) {
398 cairo_move_to (cr, 0, i * score->space_height + score->line_width / 2.0);
399 cairo_rel_line_to (cr, staff_width, 0);
402 cairo_set_line_width (cr, score->line_width);
404 cairo_set_source_rgb (cr, 0.0, 0.0, 0.0); /* black */
407 /* Make space for clef before drawing notes */
408 cairo_translate (cr, (int) (4 * score->space_height), 0);
410 /* Draw chord symbols */
413 for (i = 0; i < staff->num_chords; i++) {
414 _draw_chord (score, cr, staff, staff->chords[i]);
415 cairo_translate (cr, staff->chords[i]->width, 0.0);
421 for (i = 0; i < staff->num_notes; i++) {
422 _draw_note (score, cr, staff, staff->notes[i]);
423 /* Draw all notes concurrent for now (as a chord)
424 cairo_translate (cr, score->space_height * 2.0, 0);
432 score_draw (score_t *score, cairo_t *cr)
435 int staff_width = score->width;
438 if (score->num_staves == 0)
443 /* Before drawing anything, position each staff based on the size
444 * of each (including ledger lines) */
446 for (i = 0; i < score->num_staves; i++) {
447 score_staff_t *staff = score->staves[i];
448 staff_y_pos += staff->upper_ledger_lines * score->space_height;
449 staff->y_pos = staff_y_pos;
450 staff_y_pos += (score->staff_height +
451 staff->lower_ledger_lines * score->space_height +
452 score->staff_height);
455 if (score->num_braces)
457 /* Initialize to keep the compiler quiet. */
460 for (i = 0; i < score->num_braces; i++)
461 _draw_brace (score, cr, score->braces[i], &brace_width);
463 /* Subtract space for brace itself */
464 cairo_translate (cr, brace_width, 0);
465 staff_width -= brace_width;
467 /* As well as some padding */
468 cairo_translate (cr, 2, 0);
472 /* Vertical lines at each end */
474 score->line_width / 2.0,
475 score->staves[0]->y_pos + score->line_width / 2.0,
476 staff_width - score->line_width,
477 score->staves[score->num_staves-1]->y_pos + score->staff_height - score->staves[0]->y_pos);
478 cairo_set_source_rgb (cr, 0.0, 0.0, 0.0); /* black */
479 cairo_set_line_width (cr, score->line_width);
482 for (i = 0; i < score->num_staves; i++) {
483 score_staff_t *staff = score->staves[i];
484 _draw_staff (score, cr, staff, staff_width);
491 score_add_brace (score_t *score, int staves)
493 score_brace_t *brace;
495 brace = talloc (score, score_brace_t);
499 brace->first_staff = score->num_staves;
500 brace->num_staves = staves;
503 score->braces = talloc_realloc (score,
507 if (score->braces == NULL) {
508 score->num_braces = 0;
512 score->braces[score->num_braces - 1] = brace;
517 score_add_staff (score_t *score, score_clef_t clef)
519 score_staff_t *staff;
521 staff = talloc (score, score_staff_t);
528 staff->num_notes = 0;
530 staff->chords = NULL;
531 staff->num_chords = 0;
533 staff->upper_ledger_lines = 0;
534 staff->lower_ledger_lines = 0;
537 score->staves = talloc_realloc (score,
541 if (score->staves == NULL) {
542 score->num_staves = 0;
546 score->staves[score->num_staves - 1] = staff;
552 score_add_chord (score_staff_t *staff,
555 score_chord_t *chord;
557 chord = talloc (staff, score_chord_t);
561 talloc_steal (chord, name);
563 chord->staff = staff;
564 chord->name = talloc_strdup (chord, name);
566 /* The width will get set correctly the first time _draw_chord is
571 staff->chords = talloc_realloc (staff,
575 if (staff->chords == NULL) {
576 staff->num_chords = 0;
580 staff->chords[staff->num_chords - 1] = chord;
586 score_remove_chord (score_chord_t *chord)
588 score_staff_t *staff = chord->staff;
591 for (i = 0; i < staff->num_chords; i++)
592 if (staff->chords[i] == chord)
595 if (i == staff->num_chords)
598 if (i < staff->num_chords - 1)
600 memmove (staff->chords + i,
601 staff->chords + i + 1,
602 (staff->num_chords - 1 - i) * sizeof (score_chord_t *));
605 staff->num_chords -= 1;
609 score_staff_add_note (score_staff_t *staff,
611 score_duration_t duration)
617 /* Return existing note if already present. */
618 for (i = 0; i < staff->num_notes; i++) {
619 note = staff->notes[i];
620 if (note->pitch == pitch &&
621 note->duration == duration)
627 note = talloc (staff, score_note_t);
633 note->duration = duration;
639 line = _score_staff_pitch_to_line (staff, note->pitch);
641 int lines = (int) (- line);
642 if (lines > staff->upper_ledger_lines)
643 staff->upper_ledger_lines = lines;
645 int lines = (int) (line - 4);
646 if (lines > staff->lower_ledger_lines)
647 staff->lower_ledger_lines = lines;
651 staff->notes = talloc_realloc (staff,
655 if (staff->notes == NULL) {
656 staff->num_notes = 0;
660 staff->notes[staff->num_notes - 1] = note;
666 score_add_note (score_t *score, pitch_t pitch, score_duration_t duration)
668 score_staff_t *staff, *nearest_staff = NULL;
669 double distance, nearest_distance = 0.0;
672 /* Nothing to do if we have no staff, (there's no place to add a note) . */
673 if (score->num_staves == 0)
676 /* Find the staff where the note will be closest to the center of
678 for (i = 0; i < score->num_staves; i++) {
679 staff = score->staves[i];
680 distance = fabs (_score_staff_pitch_to_line (staff, pitch) - 2.0);
681 if (nearest_staff == NULL || distance < nearest_distance) {
682 nearest_staff = staff;
683 nearest_distance = distance;
687 return score_staff_add_note (nearest_staff, pitch, duration);
691 score_remove_note (score_note_t *note)
693 score_staff_t *staff = note->staff;
696 for (i = 0; i < staff->num_notes; i++)
697 if (staff->notes[i] == note)
700 if (i == staff->num_notes)
703 if (i < staff->num_notes - 1)
705 memmove (staff->notes + i,
706 staff->notes + i + 1,
707 (staff->num_notes - 1 - i) * sizeof (score_note_t *));
710 staff->num_notes -= 1;
712 if (staff->num_notes == 0) {
713 staff->upper_ledger_lines = 0;
714 staff->lower_ledger_lines = 0;
719 score_staff_remove_notes (score_staff_t *staff)
721 talloc_free (staff->notes);
723 staff->num_notes = 0;
727 score_remove_notes (score_t *score)
731 for (i = 0; i < score->num_staves; i++)
732 score_staff_remove_notes (score->staves[i]);
736 score_set_note_color_rgb (score_note_t *note,
747 score_staff_find_note (score_staff_t *staff,
749 score_duration_t duration)
754 for (i = 0; i < staff->num_notes; i++) {
755 note = staff->notes[i];
756 if (note->pitch == pitch && note->duration == duration)