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/ .
25 /* Height of a single staff */
28 /* Height of one space within a staff */
31 /* Minimal line width for staff lines */
34 /* Full width of staff */
39 score_create (void *ctx)
43 score = talloc (ctx, score_t);
44 score_set_staff_height (score, 24);
50 score_set_staff_height (score_t *score, int height)
52 score->space_height = (int) height / 4;
53 score->staff_height = score->space_height * 4;
55 score->line_width = score->space_height / 15;
56 if (score->line_width == 0)
57 score->line_width = 1;
59 return score->staff_height;
63 score_set_width (score_t *score, int width)
68 typedef enum score_clef
75 _draw_staff (score_t *score, cairo_t *cr, score_clef_t clef)
82 cairo_select_font_face (cr, "Gonville-26", 0, 0);
84 /* XXX: This font size is a rough guess at best. We should figure
85 * out to correctly measure, size, and place clefs. */
86 cairo_set_font_size (cr, 24);
88 /* XXX: The hard-coded glyph indices here are very ugly. We should
89 * figure out how to lookup glyphs by name from this font. */
94 glyph.y = 3 * score->space_height;
98 glyph.y = 1 * score->space_height;
103 cairo_set_source_rgb (cr, 0.0, 0.0, 0.0); /* black */
104 cairo_show_glyphs (cr, &glyph, 1);
107 score->line_width / 2.0,
108 score->line_width / 2.0,
109 score->width - score->line_width,
110 score->space_height * 4);
112 for (i = 1; i < 4; i++) {
113 cairo_move_to (cr, 0, i * score->space_height + score->line_width / 2.0);
114 cairo_rel_line_to (cr, score->width, 0);
117 cairo_set_line_width (cr, score->line_width);
119 cairo_set_source_rgb (cr, 0.0, 0.0, 0.0); /* black */
126 _draw_grand_staff (score_t *score, cairo_t *cr)
128 #define BRACE_GLYPHS 1
133 cairo_select_font_face (cr, "Gonville-Brace", 0, 0);
135 /* XXX: This font size (in conjunction with the glyph selection)
136 * is a rough guess at best. We should figure out how the brace
137 * font is intended to be used and actually measure to find the
138 * correctly sized glyph. */
139 cairo_set_font_size (cr, 40);
141 cairo_translate (cr, 5, 0);
144 /* XXX: This hard-coded glyph index is pretty ugly. We should
145 * figure out how to lookup the glyph we want, (though, as it
146 * turns out, this brace font pretty much just has numbered glyph
147 * names for different sizes, so it wouldn't be all that different
148 * than just the bare index here). */
151 brace.y = score->staff_height * 1.5;
153 cairo_set_source_rgb (cr, 0.0, 0.0, 0.0); /* black */
154 cairo_show_glyphs (cr, &brace, 1);
156 cairo_translate (cr, 2, 0);
159 /* Vertical lines at each end */
161 score->line_width / 2.0,
162 score->line_width / 2.0,
163 score->width - score->line_width,
164 score->staff_height * 3);
165 cairo_set_source_rgb (cr, 0.0, 0.0, 0.0); /* black */
166 cairo_set_line_width (cr, score->line_width);
170 _draw_staff (score, cr, SCORE_CLEF_G);
173 cairo_translate (cr, 0, score->staff_height * 2);
174 _draw_staff (score, cr, SCORE_CLEF_F);
180 score_draw (score_t *score, cairo_t *cr)
182 _draw_grand_staff (score, cr);