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>
28 typedef struct score_note
32 score_duration_t duration;
45 score_chord_t **chords;
51 /* How many ledger lines are needed for current notes */
52 int upper_ledger_lines;
53 int lower_ledger_lines;
55 /* Y position of top full line of staff */
59 typedef struct score_brace
67 /* Nominal height of a single staff (ledger lines may make it larger) */
70 /* Height of one space within a staff */
73 /* Minimal line width for staff lines */
76 /* Full width of staff */
79 score_brace_t **braces;
83 score_staff_t **staves;
88 score_create (void *ctx)
92 score = talloc (ctx, score_t);
96 /* Also sets space_height and line_width */
97 score_set_staff_height (score, 76);
99 /* Just to have some nominal width. */
102 score->braces = NULL;
103 score->num_braces = 0;
105 score->staves = NULL;
106 score->num_staves = 0;
112 score_set_staff_height (score_t *score, int height)
114 score->space_height = (int) height / 4;
115 score->staff_height = score->space_height * 4;
117 score->line_width = score->space_height / 10;
118 if (score->line_width == 0)
119 score->line_width = 1;
121 return score->staff_height;
125 score_set_width (score_t *score, int width)
127 score->width = width;
130 /* Returns in brace_width the width of the brace */
132 _draw_brace (score_t *score, cairo_t *cr,
133 score_brace_t *brace, int *brace_width)
135 cairo_glyph_t brace_glyph;
136 cairo_text_extents_t brace_extents;
139 if (brace->num_staves == 0)
144 top = score->staves[brace->first_staff]->y_pos;
145 bottom = score->staves[brace->first_staff + brace->num_staves - 1]->y_pos + score->staff_height;
147 cairo_select_font_face (cr, "Gonville-Brace", 0, 0);
149 /* XXX: This hard-coded glyph index is pretty ugly. We should
150 * figure out how to lookup the glyph we want, (though, as it
151 * turns out, this brace font pretty much just has numbered glyph
152 * names for different sizes, so it wouldn't be all that different
153 * than just the bare index here). */
154 brace_glyph.index = 300;
156 brace_glyph.y = top + (bottom - top) / 2.0 + score->line_width / 2.0;
158 /* XXX: This font size (in conjunction with the glyph selection)
159 * is a rough guess at best. We should figure out how the brace
160 * font is intended to be used and actually measure to find the
161 * correctly-sized glyph. */
162 cairo_set_font_size (cr, (bottom - top) / 3.85);
164 cairo_glyph_extents (cr, &brace_glyph, 1, &brace_extents);
166 /* Subtract space for brace itself */
167 cairo_translate (cr, -brace_extents.x_bearing, 0);
169 cairo_set_source_rgb (cr, 0.0, 0.0, 0.0); /* black */
170 cairo_show_glyphs (cr, &brace_glyph, 1);
174 *brace_width = (int) -brace_extents.x_bearing;
177 /* Line containing middle C for the given clef. */
179 _score_clef_c_line (score_clef_t clef)
191 /* On which line would 'pitch' appear on 'staff'.
193 * Lines are numbered with line 0 as the top full line of the staff
194 * and increasing downward. So line values less than 0 will appear as
195 * ledger lines above the staff while line values greater than 4 will
196 * appear on ledger lines below the staff.
198 * A line value of 2 will be centered verticall on the staff.
200 * For notes appearing on a space, the line value will be half-way
201 * between two integers. */
203 _score_staff_pitch_to_line (score_staff_t *staff, pitch_t pitch)
205 pitch_name_t name = PITCH_NAME (pitch);
206 int octave = PITCH_OCTAVE (pitch);
207 int c_line = _score_clef_c_line (staff->clef);
209 return c_line - (name - PITCH_NAME_C) / 2.0 - 3.5 * (octave - 4);
212 /* chord->width is updated as a side effect */
214 _draw_chord (score_t *score, cairo_t *cr,
215 score_staff_t *staff, score_chord_t *chord)
217 PangoRectangle ink_extents;
218 PangoRectangle logical_extents;
219 double total_staff_height;
221 PangoFontDescription *font_description;
223 /* XXX: The staff should manage this height itself. */
224 total_staff_height = (staff->upper_ledger_lines * score->space_height +
225 score->staff_height +
226 staff->lower_ledger_lines * score->space_height);
230 font_description = pango_font_description_new ();
231 pango_font_description_set_family (font_description, "serif");
232 pango_font_description_set_absolute_size (font_description,
233 score->space_height * 3 * PANGO_SCALE);
235 layout = pango_cairo_create_layout (cr);
236 pango_layout_set_font_description (layout, font_description);
237 pango_layout_set_markup (layout, chord->name, -1);
239 pango_layout_line_get_pixel_extents (pango_layout_get_line (layout, 0),
240 &ink_extents, &logical_extents);
242 if (staff->clef == SCORE_CLEF_G)
243 cairo_move_to (cr, 0, - score->space_height * 0.5);
245 cairo_move_to (cr, 0, score->space_height * 0.5 + total_staff_height +
246 logical_extents.height);
248 pango_cairo_show_layout_line (cr, pango_layout_get_line (layout, 0));
250 g_object_unref (layout);
251 pango_font_description_free (font_description);
253 chord->width = logical_extents.width;
259 _draw_note (score_t *score, cairo_t *cr,
260 score_staff_t *staff, score_note_t *note)
263 cairo_glyph_t note_glyph[2];
264 static double extend_factor = 0.25;
265 cairo_text_extents_t extents;
268 void _draw_ledger_line (double line, double offset, double width) {
269 cairo_move_to (cr, offset - extend_factor * width / 2.0,
270 score->space_height * line + score->line_width / 2.0);
271 cairo_rel_line_to (cr, (1 + extend_factor) * width, 0);
277 /* Move right so that X==0 is natural position for non-displaced
280 cairo_translate (cr, score->space_height, 0);
282 /* Which line should the note appear on? Line 0 is the top line of
283 * the staff and increasing downwards. (Negative values indicate a
284 * note on a ledger line above the staff). Values half way between
285 * integers indicate notes appearing on a space between two staff
286 * lines (or ledger lines). */
287 line = _score_staff_pitch_to_line (staff, note->pitch);
289 cairo_select_font_face (cr, "Gonville-26", 0, 0);
290 cairo_set_font_size (cr, score->staff_height);
292 /* XXX: The hard-coded glyph indices here are very ugly. We should
293 * figure out how to lookup glyphs by name from this font. */
294 switch (PITCH_ACCIDENTAL (note->pitch)) {
295 case PITCH_ACCIDENTAL_DOUBLE_FLAT:
296 note_glyph[num_glyphs].index = 77;
298 case PITCH_ACCIDENTAL_FLAT:
299 note_glyph[num_glyphs].index = 68;
301 case PITCH_ACCIDENTAL_NATURAL:
302 note_glyph[num_glyphs].index = 101;
304 case PITCH_ACCIDENTAL_SHARP:
305 note_glyph[num_glyphs].index = 134;
307 case PITCH_ACCIDENTAL_DOUBLE_SHARP:
308 note_glyph[num_glyphs].index = 142;
312 if (PITCH_ACCIDENTAL (note->pitch) != PITCH_ACCIDENTAL_NATURAL)
314 note_glyph[num_glyphs].x = 0;
316 note_glyph[num_glyphs].y = score->space_height * line;
320 cairo_glyph_extents (cr, note_glyph, num_glyphs, &extents);
322 #define ACCIDENTAL_NOTE_SPACING (score->space_height * .15)
324 note_glyph[0].x = - (extents.width + ACCIDENTAL_NOTE_SPACING);
327 switch (note->duration) {
328 case SCORE_DURATION_1:
329 note_glyph[num_glyphs].index = 127;
331 case SCORE_DURATION_2:
332 note_glyph[num_glyphs].index = 85;
334 case SCORE_DURATION_4:
335 case SCORE_DURATION_8:
336 case SCORE_DURATION_16:
337 case SCORE_DURATION_32:
338 case SCORE_DURATION_64:
339 case SCORE_DURATION_128:
341 note_glyph[num_glyphs].index = 84;
344 note_glyph[num_glyphs].x = 0;
345 note_glyph[num_glyphs].y = score->space_height * line;
349 if (line < 0 || line > 4) {
350 double offset, width;
353 cairo_glyph_extents (cr, note_glyph, num_glyphs, &extents);
354 offset = note_glyph[0].x + extents.x_bearing;
355 width = extents.width;
358 for (i = -1; i >= line; i--)
359 _draw_ledger_line (i, offset, width);
361 for (i = 5; i <= line; i++)
362 _draw_ledger_line (i, offset, width);
366 cairo_set_source_rgb (cr,
370 cairo_show_glyphs (cr, note_glyph, num_glyphs);
376 _draw_staff (score_t *score, cairo_t *cr,
377 score_staff_t *staff, int staff_width)
380 cairo_glyph_t clef_glyph;
384 cairo_translate (cr, 0, staff->y_pos);
386 cairo_select_font_face (cr, "Gonville-26", 0, 0);
388 cairo_set_font_size (cr, score->staff_height);
390 /* XXX: The hard-coded glyph indices here are very ugly. We should
391 * figure out how to lookup glyphs by name from this font. */
392 switch (staff->clef) {
395 clef_glyph.index = 46;
396 clef_glyph.y = 3 * score->space_height;
399 clef_glyph.index = 45;
400 clef_glyph.y = 1 * score->space_height;
403 clef_glyph.x = 3 * score->line_width;
404 clef_glyph.y += score->line_width / 2.0;
406 cairo_set_source_rgb (cr, 0.0, 0.0, 0.0); /* black */
407 cairo_show_glyphs (cr, &clef_glyph, 1);
409 /* Draw staff lines */
410 for (i = 0; i < 5; i++) {
411 cairo_move_to (cr, 0, i * score->space_height + score->line_width / 2.0);
412 cairo_rel_line_to (cr, staff_width, 0);
415 cairo_set_line_width (cr, score->line_width);
417 cairo_set_source_rgb (cr, 0.0, 0.0, 0.0); /* black */
420 /* Make space for clef before drawing notes */
421 cairo_translate (cr, (int) (4 * score->space_height), 0);
423 /* Draw chord symbols */
426 for (i = 0; i < staff->num_chords; i++) {
427 _draw_chord (score, cr, staff, staff->chords[i]);
428 cairo_translate (cr, staff->chords[i]->width, 0.0);
434 for (i = 0; i < staff->num_notes; i++) {
435 _draw_note (score, cr, staff, staff->notes[i]);
436 /* Draw all notes concurrent for now (as a chord)
437 cairo_translate (cr, score->space_height * 2.0, 0);
445 score_draw (score_t *score, cairo_t *cr)
448 int staff_width = score->width;
451 if (score->num_staves == 0)
456 /* Before drawing anything, position each staff based on the size
457 * of each (including ledger lines) */
459 for (i = 0; i < score->num_staves; i++) {
460 score_staff_t *staff = score->staves[i];
461 staff_y_pos += staff->upper_ledger_lines * score->space_height;
462 staff->y_pos = staff_y_pos;
463 staff_y_pos += (score->staff_height +
464 staff->lower_ledger_lines * score->space_height +
465 score->staff_height);
468 if (score->num_braces)
470 /* Initialize to keep the compiler quiet. */
473 for (i = 0; i < score->num_braces; i++)
474 _draw_brace (score, cr, score->braces[i], &brace_width);
476 /* Subtract space for brace itself */
477 cairo_translate (cr, brace_width, 0);
478 staff_width -= brace_width;
480 /* As well as some padding */
481 cairo_translate (cr, 2, 0);
485 /* Vertical lines at each end */
487 score->line_width / 2.0,
488 score->staves[0]->y_pos + score->line_width / 2.0,
489 staff_width - score->line_width,
490 score->staves[score->num_staves-1]->y_pos + score->staff_height - score->staves[0]->y_pos);
491 cairo_set_source_rgb (cr, 0.0, 0.0, 0.0); /* black */
492 cairo_set_line_width (cr, score->line_width);
495 for (i = 0; i < score->num_staves; i++) {
496 score_staff_t *staff = score->staves[i];
497 _draw_staff (score, cr, staff, staff_width);
504 score_add_brace (score_t *score, int staves)
506 score_brace_t *brace;
508 brace = talloc (score, score_brace_t);
512 brace->first_staff = score->num_staves;
513 brace->num_staves = staves;
516 score->braces = talloc_realloc (score,
520 if (score->braces == NULL) {
521 score->num_braces = 0;
525 score->braces[score->num_braces - 1] = brace;
530 score_add_staff (score_t *score, score_clef_t clef)
532 score_staff_t *staff;
534 staff = talloc (score, score_staff_t);
541 staff->num_notes = 0;
543 staff->chords = NULL;
544 staff->num_chords = 0;
546 staff->upper_ledger_lines = 0;
547 staff->lower_ledger_lines = 0;
550 score->staves = talloc_realloc (score,
554 if (score->staves == NULL) {
555 score->num_staves = 0;
559 score->staves[score->num_staves - 1] = staff;
565 score_add_chord (score_staff_t *staff,
568 score_chord_t *chord;
570 chord = talloc (staff, score_chord_t);
574 talloc_steal (chord, name);
576 chord->staff = staff;
577 chord->name = talloc_strdup (chord, name);
579 /* The width will get set correctly the first time _draw_chord is
584 staff->chords = talloc_realloc (staff,
588 if (staff->chords == NULL) {
589 staff->num_chords = 0;
593 staff->chords[staff->num_chords - 1] = chord;
599 score_remove_chord (score_chord_t *chord)
601 score_staff_t *staff = chord->staff;
604 for (i = 0; i < staff->num_chords; i++)
605 if (staff->chords[i] == chord)
608 if (i == staff->num_chords)
611 if (i < staff->num_chords - 1)
613 memmove (staff->chords + i,
614 staff->chords + i + 1,
615 (staff->num_chords - 1 - i) * sizeof (score_chord_t *));
618 staff->num_chords -= 1;
622 score_staff_add_note (score_staff_t *staff,
624 score_duration_t duration)
630 /* Return existing note if already present. */
631 for (i = 0; i < staff->num_notes; i++) {
632 note = staff->notes[i];
633 if (note->pitch == pitch &&
634 note->duration == duration)
640 note = talloc (staff, score_note_t);
646 note->duration = duration;
652 line = _score_staff_pitch_to_line (staff, note->pitch);
654 int lines = (int) (- line);
655 if (lines > staff->upper_ledger_lines)
656 staff->upper_ledger_lines = lines;
658 int lines = (int) (line - 4);
659 if (lines > staff->lower_ledger_lines)
660 staff->lower_ledger_lines = lines;
664 staff->notes = talloc_realloc (staff,
668 if (staff->notes == NULL) {
669 staff->num_notes = 0;
673 staff->notes[staff->num_notes - 1] = note;
677 score_add_note (score_t *score, pitch_t pitch, score_duration_t duration)
679 score_staff_t *staff, *nearest_staff = NULL;
680 double distance, nearest_distance = 0.0;
683 /* Nothing to do if we have no staff, (there's no place to add a note) . */
684 if (score->num_staves == 0)
687 /* Find the staff where the note will be closest to the center of
689 for (i = 0; i < score->num_staves; i++) {
690 staff = score->staves[i];
691 distance = fabs (_score_staff_pitch_to_line (staff, pitch) - 2.0);
692 if (nearest_staff == NULL || distance < nearest_distance) {
693 nearest_staff = staff;
694 nearest_distance = distance;
698 score_staff_add_note (nearest_staff, pitch, duration);
702 score_staff_remove_notes (score_staff_t *staff)
704 talloc_free (staff->notes);
706 staff->num_notes = 0;
708 staff->upper_ledger_lines = 0;
709 staff->lower_ledger_lines = 0;
713 score_remove_notes (score_t *score)
717 for (i = 0; i < score->num_staves; i++)
718 score_staff_remove_notes (score->staves[i]);