]> git.cworth.org Git - scherzo/blob - score.c
Makefile: Fix automatic generation of dependencies.
[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_staff
24 {
25     score_clef_t clef;
26
27     score_note_t **notes;
28     int num_notes;
29 };
30
31 typedef struct score_brace
32 {
33     int first_staff;
34     int num_staves;
35 } score_brace_t;
36
37 struct score
38 {
39     /* Height of a single staff */
40     int staff_height;
41
42     /* Height of one space within a staff */
43     int space_height;
44
45     /* Minimal line width for staff lines */
46     int line_width;
47
48     /* Full width of staff */
49     int width;
50
51     score_brace_t **braces;
52     int num_braces;
53     int brace_width;
54
55     score_staff_t **staves;
56     int num_staves;
57 };
58
59 typedef struct score_note
60 {
61     score_pitch_t pitch;
62     int octave;
63     score_duration_t duration;
64 } score_note_t;
65
66 score_t *
67 score_create (void *ctx)
68 {
69     score_t *score;
70
71     score = talloc (ctx, score_t);
72     if (score == NULL)
73         return NULL;
74
75     /* Also sets space_height and line_width */
76     score_set_staff_height (score, 24);
77
78     /* Just to have some nominal width. */
79     score->width = 800;
80
81     score->braces = NULL;
82     score->num_braces = 0;
83
84     score->staves = NULL;
85     score->num_staves = 0;
86
87     return score;
88 }
89
90 int
91 score_set_staff_height (score_t *score, int height)
92 {
93     score->space_height = (int) height / 4;
94     score->staff_height = score->space_height * 4;
95
96     score->line_width = score->space_height / 10;
97     if (score->line_width == 0)
98         score->line_width = 1;
99
100     return score->staff_height;
101 }
102
103 void
104 score_set_width (score_t *score, int width)
105 {
106     score->width = width;
107 }
108
109 /* Returns in brace_width the width of the brace */
110 static void
111 _draw_brace (score_t *score, cairo_t *cr,
112              score_brace_t *brace, int *brace_width)
113 {
114     cairo_glyph_t brace_glyph;
115     cairo_text_extents_t brace_extents;
116
117     cairo_save (cr);
118
119     cairo_select_font_face (cr, "Gonville-Brace", 0, 0);
120
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;
127     brace_glyph.x = 0;
128     brace_glyph.y = score->staff_height * (brace->first_staff + (2 * brace->num_staves - 1) / 2.0) + 1;
129
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);
135
136     cairo_glyph_extents (cr, &brace_glyph, 1, &brace_extents);
137
138     /* Subtract space for brace itself */
139     cairo_translate (cr, -brace_extents.x_bearing, 0);
140
141     cairo_set_source_rgb (cr, 0.0, 0.0, 0.0); /* black */
142     cairo_show_glyphs (cr, &brace_glyph, 1);
143
144     cairo_restore (cr);
145
146     *brace_width = (int) -brace_extents.x_bearing;
147 }
148
149 static void
150 _draw_staff (score_t *score, cairo_t *cr,
151              score_staff_t *staff, int staff_width)
152 {
153     int i;
154     cairo_glyph_t glyph;
155
156     cairo_save (cr);
157
158     cairo_select_font_face (cr, "Gonville-26", 0, 0);
159
160     cairo_set_font_size (cr, score->staff_height);
161
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) {
165     case SCORE_CLEF_G:
166     default:
167         glyph.index = 46;
168         glyph.y = 3 * score->space_height;
169         break;
170     case SCORE_CLEF_F:
171         glyph.index = 45;
172         glyph.y = 1 * score->space_height;
173         break;
174     }
175     glyph.x = 3 * score->line_width;
176     glyph.y += 1;
177
178     cairo_set_source_rgb (cr, 0.0, 0.0, 0.0); /* black */
179     cairo_show_glyphs (cr, &glyph, 1);
180
181     cairo_rectangle (cr,
182                      score->line_width / 2.0,
183                      score->line_width / 2.0,
184                      staff_width - score->line_width,
185                      score->space_height * 4);
186     
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);
190     }
191
192     cairo_set_line_width (cr, score->line_width);
193
194     cairo_set_source_rgb (cr, 0.0, 0.0, 0.0); /* black */
195     cairo_stroke (cr);
196
197     cairo_restore (cr);
198 }
199
200 void
201 score_draw (score_t *score, cairo_t *cr)
202 {
203     int i;
204     int staff_width = score->width;
205
206     cairo_save (cr);
207
208     if (score->num_braces)
209     {
210         int brace_width;
211
212         for (i = 0; i < score->num_braces; i++)
213             _draw_brace (score, cr, score->braces[i], &brace_width);
214
215         /* Subtract space for brace itself */
216         cairo_translate (cr, brace_width, 0);
217         staff_width -= brace_width;
218
219         /* As well as some padding */
220         cairo_translate (cr, 2, 0);
221         staff_width -= 2;
222     }
223
224     /* Vertical lines at each end */
225     cairo_rectangle (cr,
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);
232     cairo_stroke (cr);
233
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);
237     }
238
239     cairo_restore (cr);
240 }
241
242 void
243 score_add_brace (score_t *score, int staves)
244 {
245     score_brace_t *brace;
246
247     brace = talloc (score, score_brace_t);
248     if (brace == NULL)
249         return;
250
251     brace->first_staff = score->num_staves;
252     brace->num_staves = staves;
253
254     score->num_braces++;
255     score->braces = talloc_realloc (score,
256                                     score->braces,
257                                     score_brace_t*,
258                                     score->num_braces);
259     if (score->braces == NULL) {
260         score->num_braces = 0;
261         return;
262     }
263
264     score->braces[score->num_braces - 1] = brace;
265
266 }
267
268 score_staff_t *
269 score_add_staff (score_t *score, score_clef_t clef)
270 {
271     score_staff_t *staff;
272
273     staff = talloc (score, score_staff_t);
274     if (staff == NULL)
275         return NULL;
276
277     staff->clef = clef;
278
279     staff->notes = NULL;
280     staff->num_notes = 0;
281
282     score->num_staves++;
283     score->staves = talloc_realloc (score,
284                                     score->staves,
285                                     score_staff_t*,
286                                     score->num_staves);
287     if (score->staves == NULL) {
288         score->num_staves = 0;
289         return NULL;
290     }
291
292     score->staves[score->num_staves - 1] = staff;
293
294     return staff;
295 }
296
297 score_note_t *
298 score_staff_add_note (score_staff_t *staff,
299                       score_pitch_t pitch,
300                       int octave,
301                       score_duration_t duration)
302 {
303     score_note_t *note;
304
305     note = talloc (staff, score_note_t);
306     if (note == NULL)
307         return NULL;
308
309     note->pitch = pitch;
310     note->octave = octave;
311     note->duration = duration;
312
313     staff->num_notes++;
314     staff->notes = talloc_realloc (staff,
315                                    staff->notes,
316                                    score_note_t*,
317                                    staff->num_notes);
318     if (staff->notes == NULL) {
319         staff->num_notes = 0;
320         return NULL;
321     }
322
323     staff->notes[staff->num_notes - 1] = note;
324
325     return note;
326 }
327