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;
150 _draw_staff (score_t *score, cairo_t *cr,
151 score_staff_t *staff, int staff_width)
158 cairo_select_font_face (cr, "Gonville-26", 0, 0);
160 cairo_set_font_size (cr, score->staff_height);
162 /* XXX: The hard-coded glyph indices here are very ugly. We should
163 * figure out how to lookup glyphs by name from this font. */
164 switch (staff->clef) {
168 glyph.y = 3 * score->space_height;
172 glyph.y = 1 * score->space_height;
175 glyph.x = 3 * score->line_width;
178 cairo_set_source_rgb (cr, 0.0, 0.0, 0.0); /* black */
179 cairo_show_glyphs (cr, &glyph, 1);
182 score->line_width / 2.0,
183 score->line_width / 2.0,
184 staff_width - score->line_width,
185 score->space_height * 4);
187 for (i = 1; i < 4; i++) {
188 cairo_move_to (cr, 0, i * score->space_height + score->line_width / 2.0);
189 cairo_rel_line_to (cr, staff_width, 0);
192 cairo_set_line_width (cr, score->line_width);
194 cairo_set_source_rgb (cr, 0.0, 0.0, 0.0); /* black */
201 score_draw (score_t *score, cairo_t *cr)
204 int staff_width = score->width;
208 if (score->num_braces)
212 for (i = 0; i < score->num_braces; i++)
213 _draw_brace (score, cr, score->braces[i], &brace_width);
215 /* Subtract space for brace itself */
216 cairo_translate (cr, brace_width, 0);
217 staff_width -= brace_width;
219 /* As well as some padding */
220 cairo_translate (cr, 2, 0);
224 /* Vertical lines at each end */
226 score->line_width / 2.0,
227 score->line_width / 2.0,
228 staff_width - score->line_width,
229 score->staff_height * 3);
230 cairo_set_source_rgb (cr, 0.0, 0.0, 0.0); /* black */
231 cairo_set_line_width (cr, score->line_width);
234 for (i = 0; i < score->num_staves; i++) {
235 _draw_staff (score, cr, score->staves[i], staff_width);
236 cairo_translate (cr, 0, 2 * score->staff_height);
243 score_add_brace (score_t *score, int staves)
245 score_brace_t *brace;
247 brace = talloc (score, score_brace_t);
251 brace->first_staff = score->num_staves;
252 brace->num_staves = staves;
255 score->braces = talloc_realloc (score,
259 if (score->braces == NULL) {
260 score->num_braces = 0;
264 score->braces[score->num_braces - 1] = brace;
269 score_add_staff (score_t *score, score_clef_t clef)
271 score_staff_t *staff;
273 staff = talloc (score, score_staff_t);
280 staff->num_notes = 0;
283 score->staves = talloc_realloc (score,
287 if (score->staves == NULL) {
288 score->num_staves = 0;
292 score->staves[score->num_staves - 1] = staff;
298 score_staff_add_note (score_staff_t *staff,
301 score_duration_t duration)
305 note = talloc (staff, score_note_t);
310 note->octave = octave;
311 note->duration = duration;
314 staff->notes = talloc_realloc (staff,
318 if (staff->notes == NULL) {
319 staff->num_notes = 0;
323 staff->notes[staff->num_notes - 1] = note;