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, 76);
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,
250 cairo_show_glyphs (cr, ¬e_glyph, 1);
256 _draw_staff (score_t *score, cairo_t *cr,
257 score_staff_t *staff, int staff_width)
260 cairo_glyph_t clef_glyph;
264 cairo_translate (cr, 0, staff->y_pos);
266 cairo_select_font_face (cr, "Gonville-26", 0, 0);
268 cairo_set_font_size (cr, score->staff_height);
270 /* XXX: The hard-coded glyph indices here are very ugly. We should
271 * figure out how to lookup glyphs by name from this font. */
272 switch (staff->clef) {
275 clef_glyph.index = 46;
276 clef_glyph.y = 3 * score->space_height;
279 clef_glyph.index = 45;
280 clef_glyph.y = 1 * score->space_height;
283 clef_glyph.x = 3 * score->line_width;
284 clef_glyph.y += score->line_width / 2.0;
286 cairo_set_source_rgb (cr, 0.0, 0.0, 0.0); /* black */
287 cairo_show_glyphs (cr, &clef_glyph, 1);
289 /* Draw staff lines */
290 for (i = 0; i < 5; i++) {
291 cairo_move_to (cr, 0, i * score->space_height + score->line_width / 2.0);
292 cairo_rel_line_to (cr, staff_width, 0);
295 cairo_set_line_width (cr, score->line_width);
297 cairo_set_source_rgb (cr, 0.0, 0.0, 0.0); /* black */
300 /* Make space for clef before drawing notes */
301 cairo_translate (cr, (int) (4 * score->space_height), 0);
304 for (i = 0; i < staff->num_notes; i++) {
305 _draw_note (score, cr, staff, staff->notes[i]);
306 /* Draw all notes concurrent for now (as a chord)
307 cairo_translate (cr, score->space_height * 2.0, 0);
315 score_draw (score_t *score, cairo_t *cr)
318 int staff_width = score->width;
321 if (score->num_staves == 0)
326 /* Before drawing anything, position each staff based on the size
327 * of each (including ledger lines) */
329 for (i = 0; i < score->num_staves; i++) {
330 score_staff_t *staff = score->staves[i];
331 staff_y_pos += staff->upper_ledger_lines * score->space_height;
332 staff->y_pos = staff_y_pos;
333 staff_y_pos += (score->staff_height +
334 staff->lower_ledger_lines * score->space_height +
335 score->staff_height);
338 if (score->num_braces)
342 for (i = 0; i < score->num_braces; i++)
343 _draw_brace (score, cr, score->braces[i], &brace_width);
345 /* Subtract space for brace itself */
346 cairo_translate (cr, brace_width, 0);
347 staff_width -= brace_width;
349 /* As well as some padding */
350 cairo_translate (cr, 2, 0);
354 /* Vertical lines at each end */
356 score->line_width / 2.0,
357 score->staves[0]->y_pos + score->line_width / 2.0,
358 staff_width - score->line_width,
359 score->staves[score->num_staves-1]->y_pos + score->staff_height - score->staves[0]->y_pos);
360 cairo_set_source_rgb (cr, 0.0, 0.0, 0.0); /* black */
361 cairo_set_line_width (cr, score->line_width);
364 for (i = 0; i < score->num_staves; i++) {
365 score_staff_t *staff = score->staves[i];
366 _draw_staff (score, cr, staff, staff_width);
373 score_add_brace (score_t *score, int staves)
375 score_brace_t *brace;
377 brace = talloc (score, score_brace_t);
381 brace->first_staff = score->num_staves;
382 brace->num_staves = staves;
385 score->braces = talloc_realloc (score,
389 if (score->braces == NULL) {
390 score->num_braces = 0;
394 score->braces[score->num_braces - 1] = brace;
399 score_add_staff (score_t *score, score_clef_t clef)
401 score_staff_t *staff;
403 staff = talloc (score, score_staff_t);
410 staff->num_notes = 0;
412 staff->upper_ledger_lines = 0;
413 staff->lower_ledger_lines = 0;
416 score->staves = talloc_realloc (score,
420 if (score->staves == NULL) {
421 score->num_staves = 0;
425 score->staves[score->num_staves - 1] = staff;
431 score_add_note (score_staff_t *staff,
434 score_duration_t duration)
439 note = talloc (staff, score_note_t);
445 note->octave = octave;
446 note->duration = duration;
452 line = _score_note_to_line (staff, note);
454 int lines = (int) (- line);
455 if (lines > staff->upper_ledger_lines)
456 staff->upper_ledger_lines = lines;
458 int lines = (int) (line - 4);
459 if (lines > staff->lower_ledger_lines)
460 staff->lower_ledger_lines = lines;
464 staff->notes = talloc_realloc (staff,
468 if (staff->notes == NULL) {
469 staff->num_notes = 0;
473 staff->notes[staff->num_notes - 1] = note;
479 score_remove_note (score_note_t *note)
481 score_staff_t *staff = note->staff;
484 for (i = 0; i < staff->num_notes; i++)
485 if (staff->notes[i] == note)
488 if (i == staff->num_notes)
491 if (i < staff->num_notes - 1)
493 memmove (staff->notes + i,
494 staff->notes + i + 1,
495 (staff->num_notes - 1 - i) * sizeof (score_note_t *));
498 staff->num_notes -= 1;
500 if (staff->num_notes == 0) {
501 staff->upper_ledger_lines = 0;
502 staff->lower_ledger_lines = 0;
507 score_set_note_color_rgb (score_note_t *note,
518 score_staff_find_note (score_staff_t *staff,
521 score_duration_t duration)
526 for (i = 0; i < staff->num_notes; i++) {
527 note = staff->notes[i];
528 if (note->pitch == pitch &&
529 note->octave == octave &&
530 note->duration == duration)