]> git.cworth.org Git - scherzo/blob - score.c
Add some scaling controls with plus/minus keybindings.
[scherzo] / score.c
1 /* scherzo - Music notation training
2  *
3  *      score - Utilities for drawing (simple) musical scores
4  *
5  * Copyright © 2010 Carl Worth
6  *
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.
11  *
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.
16  *
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/ .
19  */
20
21 #include "score.h"
22
23 struct score
24 {
25     /* Height of a single staff */
26     int staff_height;
27
28     /* Height of one space within a staff */
29     int space_height;
30
31     /* Minimal line width for staff lines */
32     int line_width;
33
34     /* Full width of staff */
35     int width;
36 };
37
38 score_t *
39 score_create (void *ctx)
40 {
41     score_t *score;
42
43     score = talloc (ctx, score_t);
44     score_set_staff_height (score, 24);
45
46     return score;
47 }
48
49 int
50 score_set_staff_height (score_t *score, int height)
51 {
52     score->space_height = (int) height / 4;
53     score->staff_height = score->space_height * 4;
54
55     score->line_width = score->space_height / 15;
56     if (score->line_width == 0)
57         score->line_width = 1;
58
59     return score->staff_height;
60 }
61
62 void
63 score_set_width (score_t *score, int width)
64 {
65     score->width = width;
66 }
67
68 typedef enum score_clef
69 {
70     SCORE_CLEF_G,
71     SCORE_CLEF_F
72 } score_clef_t;
73
74 static void
75 _draw_staff (score_t *score, cairo_t *cr, score_clef_t clef)
76 {
77     int i;
78     cairo_glyph_t glyph;
79
80     cairo_save (cr);
81
82     cairo_select_font_face (cr, "Gonville-26", 0, 0);
83
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);
87
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. */
90     switch (clef) {
91     case SCORE_CLEF_G:
92     default:
93         glyph.index = 46;
94         glyph.y = 3 * score->space_height;
95         break;
96     case SCORE_CLEF_F:
97         glyph.index = 45;
98         glyph.y = 1 * score->space_height;
99         break;
100     }
101     glyph.x = 2;
102
103     cairo_set_source_rgb (cr, 0.0, 0.0, 0.0); /* black */
104     cairo_show_glyphs (cr, &glyph, 1);
105
106     cairo_rectangle (cr,
107                      score->line_width / 2.0,
108                      score->line_width / 2.0,
109                      score->width - score->line_width,
110                      score->space_height * 4);
111     
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);
115     }
116
117     cairo_set_line_width (cr, score->line_width);
118
119     cairo_set_source_rgb (cr, 0.0, 0.0, 0.0); /* black */
120     cairo_stroke (cr);
121
122     cairo_restore (cr);
123 }
124
125 static void
126 _draw_grand_staff (score_t *score, cairo_t *cr)
127 {
128 #define BRACE_GLYPHS 1
129     cairo_glyph_t brace;
130     cairo_save (cr);
131
132     /* Brace test */
133     cairo_select_font_face (cr, "Gonville-Brace", 0, 0);
134
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);
140
141     cairo_translate (cr, 5, 0);
142     score->width -= 5;
143
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). */
149     brace.index = 185;
150     brace.x = 0;
151     brace.y = score->staff_height * 1.5;
152
153     cairo_set_source_rgb (cr, 0.0, 0.0, 0.0); /* black */
154     cairo_show_glyphs (cr, &brace, 1);
155
156     cairo_translate (cr, 2, 0);
157     score->width -= 2;
158
159     /* Vertical lines at each end */
160     cairo_rectangle (cr,
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);
167     cairo_stroke (cr);
168
169     /* Top staff */
170     _draw_staff (score, cr, SCORE_CLEF_G);
171
172     /* Bottom staff */
173     cairo_translate (cr, 0, score->staff_height * 2);
174     _draw_staff (score, cr, SCORE_CLEF_F);
175
176     cairo_restore (cr);
177 }
178
179 void
180 score_draw (score_t *score, cairo_t *cr)
181 {
182     _draw_grand_staff (score, cr);
183 }