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/ .
31 typedef struct score_brace
39 /* Height of a single staff */
42 /* Height of one space within a staff */
45 /* Minimal line width for staff lines */
48 /* Full width of staff */
51 score_brace_t **braces;
55 score_staff_t **staves;
59 typedef struct score_note
63 score_duration_t duration;
67 score_create (void *ctx)
71 score = talloc (ctx, score_t);
75 /* Also sets space_height and line_width */
76 score_set_staff_height (score, 24);
78 /* Just to have some nominal width. */
82 score->num_braces = 0;
85 score->num_staves = 0;
91 score_set_staff_height (score_t *score, int height)
93 score->space_height = (int) height / 4;
94 score->staff_height = score->space_height * 4;
96 score->line_width = score->space_height / 10;
97 if (score->line_width == 0)
98 score->line_width = 1;
100 return score->staff_height;
104 score_set_width (score_t *score, int width)
106 score->width = width;
109 /* Returns in brace_width the width of the brace */
111 _draw_brace (score_t *score, cairo_t *cr,
112 score_brace_t *brace, int *brace_width)
114 cairo_glyph_t brace_glyph;
115 cairo_text_extents_t brace_extents;
119 cairo_select_font_face (cr, "Gonville-Brace", 0, 0);
121 /* XXX: This hard-coded glyph index is pretty ugly. We should
122 * figure out how to lookup the glyph we want, (though, as it
123 * turns out, this brace font pretty much just has numbered glyph
124 * names for different sizes, so it wouldn't be all that different
125 * than just the bare index here). */
126 brace_glyph.index = 300;
128 brace_glyph.y = score->staff_height * (brace->first_staff + (2 * brace->num_staves - 1) / 2.0) + 1;
130 /* XXX: This font size (in conjunction with the glyph selection)
131 * is a rough guess at best. We should figure out how the brace
132 * font is intended to be used and actually measure to find the
133 * correctly-sized glyph. */
134 cairo_set_font_size (cr, (score->staff_height * 3) / 3.85);
136 cairo_glyph_extents (cr, &brace_glyph, 1, &brace_extents);
138 /* Subtract space for brace itself */
139 cairo_translate (cr, -brace_extents.x_bearing, 0);
141 cairo_set_source_rgb (cr, 0.0, 0.0, 0.0); /* black */
142 cairo_show_glyphs (cr, &brace_glyph, 1);
146 *brace_width = (int) -brace_extents.x_bearing;
149 /* Line containing middle C for the given clef. */
151 _score_clef_c_line (score_clef_t clef)
164 _score_note_to_line (score_staff_t *staff, score_note_t *note)
166 score_pitch_name_t name = SCORE_PITCH_NAME (note->pitch);
167 int c_line = _score_clef_c_line (staff->clef);
169 return c_line - (name - SCORE_PITCH_NAME_C) / 2.0 - 3.5 * (note->octave - 4);
173 _draw_note (score_t *score, cairo_t *cr,
174 score_staff_t *staff, score_note_t *note)
177 cairo_glyph_t note_glyph;
181 /* Which line should the note appear on? Line 0 is the top line of
182 * the staff and increasing downwards. (Negative values indicate a
183 * note on a ledger line above the staff). Values half way between
184 * integers indicate notes appearing on a space between two staff
185 * lines (or ledger lines). */
186 line = _score_note_to_line (staff, note);
188 cairo_select_font_face (cr, "Gonville-26", 0, 0);
189 cairo_set_font_size (cr, score->staff_height);
191 /* XXX: The hard-coded glyph indices here are very ugly. We should
192 * figure out how to lookup glyphs by name from this font. */
193 switch (note->duration) {
194 case SCORE_DURATION_1:
195 note_glyph.index = 127;
197 case SCORE_DURATION_2:
198 note_glyph.index = 85;
200 case SCORE_DURATION_4:
201 case SCORE_DURATION_8:
202 case SCORE_DURATION_16:
203 case SCORE_DURATION_32:
204 case SCORE_DURATION_64:
205 case SCORE_DURATION_128:
207 note_glyph.index = 84;
211 note_glyph.y = score->space_height * line + score->line_width / 2.0;
213 cairo_set_source_rgb (cr, 0.0, 0.0, 0.0); /* black */
214 cairo_show_glyphs (cr, ¬e_glyph, 1);
220 _draw_staff (score_t *score, cairo_t *cr,
221 score_staff_t *staff, int staff_width)
224 cairo_glyph_t clef_glyph;
225 cairo_text_extents_t clef_extents;
229 cairo_select_font_face (cr, "Gonville-26", 0, 0);
231 cairo_set_font_size (cr, score->staff_height);
233 /* XXX: The hard-coded glyph indices here are very ugly. We should
234 * figure out how to lookup glyphs by name from this font. */
235 switch (staff->clef) {
238 clef_glyph.index = 46;
239 clef_glyph.y = 3 * score->space_height;
242 clef_glyph.index = 45;
243 clef_glyph.y = 1 * score->space_height;
246 clef_glyph.x = 3 * score->line_width;
249 cairo_set_source_rgb (cr, 0.0, 0.0, 0.0); /* black */
250 cairo_show_glyphs (cr, &clef_glyph, 1);
252 cairo_glyph_extents (cr, &clef_glyph, 1, &clef_extents);
254 /* Draw staff lines */
255 for (i = 0; i < 5; i++) {
256 cairo_move_to (cr, 0, i * score->space_height + score->line_width / 2.0);
257 cairo_rel_line_to (cr, staff_width, 0);
260 cairo_set_line_width (cr, score->line_width);
262 cairo_set_source_rgb (cr, 0.0, 0.0, 0.0); /* black */
265 /* Make space for clef before drawing notes */
266 cairo_translate (cr, (int) (1.5 * clef_extents.width), 0);
269 for (i = 0; i < staff->num_notes; i++) {
270 _draw_note (score, cr, staff, staff->notes[i]);
271 cairo_translate (cr, score->space_height * 2.0, 0);
278 score_draw (score_t *score, cairo_t *cr)
281 int staff_width = score->width;
285 if (score->num_braces)
289 for (i = 0; i < score->num_braces; i++)
290 _draw_brace (score, cr, score->braces[i], &brace_width);
292 /* Subtract space for brace itself */
293 cairo_translate (cr, brace_width, 0);
294 staff_width -= brace_width;
296 /* As well as some padding */
297 cairo_translate (cr, 2, 0);
301 /* Vertical lines at each end */
303 score->line_width / 2.0,
304 score->line_width / 2.0,
305 staff_width - score->line_width,
306 score->staff_height * 3);
307 cairo_set_source_rgb (cr, 0.0, 0.0, 0.0); /* black */
308 cairo_set_line_width (cr, score->line_width);
311 for (i = 0; i < score->num_staves; i++) {
312 _draw_staff (score, cr, score->staves[i], staff_width);
313 cairo_translate (cr, 0, 2 * score->staff_height);
320 score_add_brace (score_t *score, int staves)
322 score_brace_t *brace;
324 brace = talloc (score, score_brace_t);
328 brace->first_staff = score->num_staves;
329 brace->num_staves = staves;
332 score->braces = talloc_realloc (score,
336 if (score->braces == NULL) {
337 score->num_braces = 0;
341 score->braces[score->num_braces - 1] = brace;
346 score_add_staff (score_t *score, score_clef_t clef)
348 score_staff_t *staff;
350 staff = talloc (score, score_staff_t);
357 staff->num_notes = 0;
360 score->staves = talloc_realloc (score,
364 if (score->staves == NULL) {
365 score->num_staves = 0;
369 score->staves[score->num_staves - 1] = staff;
375 score_staff_add_note (score_staff_t *staff,
378 score_duration_t duration)
382 note = talloc (staff, score_note_t);
387 note->octave = octave;
388 note->duration = duration;
391 staff->notes = talloc_realloc (staff,
395 if (staff->notes == NULL) {
396 staff->num_notes = 0;
400 staff->notes[staff->num_notes - 1] = note;