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 / 10;
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 cairo_set_font_size (cr, score->staff_height);
86 /* XXX: The hard-coded glyph indices here are very ugly. We should
87 * figure out how to lookup glyphs by name from this font. */
92 glyph.y = 3 * score->space_height;
96 glyph.y = 1 * score->space_height;
99 glyph.x = 3 * score->line_width;
102 cairo_set_source_rgb (cr, 0.0, 0.0, 0.0); /* black */
103 cairo_show_glyphs (cr, &glyph, 1);
106 score->line_width / 2.0,
107 score->line_width / 2.0,
108 score->width - score->line_width,
109 score->space_height * 4);
111 for (i = 1; i < 4; i++) {
112 cairo_move_to (cr, 0, i * score->space_height + score->line_width / 2.0);
113 cairo_rel_line_to (cr, score->width, 0);
116 cairo_set_line_width (cr, score->line_width);
118 cairo_set_source_rgb (cr, 0.0, 0.0, 0.0); /* black */
125 _draw_grand_staff (score_t *score, cairo_t *cr)
127 #define BRACE_GLYPHS 1
129 cairo_text_extents_t brace_extents;
133 cairo_select_font_face (cr, "Gonville-Brace", 0, 0);
135 /* XXX: This hard-coded glyph index is pretty ugly. We should
136 * figure out how to lookup the glyph we want, (though, as it
137 * turns out, this brace font pretty much just has numbered glyph
138 * names for different sizes, so it wouldn't be all that different
139 * than just the bare index here). */
142 brace.y = (score->staff_height * 3) / 2 + 1;
144 /* XXX: This font size (in conjunction with the glyph selection)
145 * is a rough guess at best. We should figure out how the brace
146 * font is intended to be used and actually measure to find the
147 * correctly-sized glyph. */
148 cairo_set_font_size (cr, (score->staff_height * 3) / 3.85);
150 cairo_glyph_extents (cr, &brace, 1, &brace_extents);
152 cairo_translate (cr, -brace_extents.x_bearing, 0);
153 score->width += brace_extents.x_bearing;
155 cairo_set_source_rgb (cr, 0.0, 0.0, 0.0); /* black */
156 cairo_show_glyphs (cr, &brace, 1);
158 cairo_translate (cr, 2, 0);
161 /* Vertical lines at each end */
163 score->line_width / 2.0,
164 score->line_width / 2.0,
165 score->width - score->line_width,
166 score->staff_height * 3);
167 cairo_set_source_rgb (cr, 0.0, 0.0, 0.0); /* black */
168 cairo_set_line_width (cr, score->line_width);
172 _draw_staff (score, cr, SCORE_CLEF_G);
175 cairo_translate (cr, 0, score->staff_height * 2);
176 _draw_staff (score, cr, SCORE_CLEF_F);
182 score_draw (score_t *score, cairo_t *cr)
184 _draw_grand_staff (score, cr);