]> git.cworth.org Git - scherzo/blob - score.c
Start drawing some very rudimentary notes.
[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 /* Line containing middle C for the given clef. */
150 static int
151 _score_clef_c_line (score_clef_t clef)
152 {
153     switch (clef)
154     {
155     default:
156     case SCORE_CLEF_G:
157         return 5;
158     case SCORE_CLEF_F:
159         return -1;
160     }
161 }
162
163 static double
164 _score_note_to_line (score_staff_t *staff, score_note_t *note)
165 {
166     score_pitch_name_t name = SCORE_PITCH_NAME (note->pitch);
167     int c_line = _score_clef_c_line (staff->clef);
168
169     return c_line - (name - SCORE_PITCH_NAME_C) / 2.0 - 3.5 * (note->octave - 4);
170 }
171
172 static void
173 _draw_note (score_t *score, cairo_t *cr,
174             score_staff_t *staff, score_note_t *note)
175 {
176     double line;
177     cairo_glyph_t note_glyph;
178
179     cairo_save (cr);
180
181     /* Which line should the note appear on? Line 0 is the top line of
182      * the staff and increasing downwards. (Negative values indicate a
183      * note on a ledger line above the staff). Values half way between
184      * integers indicate notes appearing on a space between two staff
185      * lines (or ledger lines). */
186     line = _score_note_to_line (staff, note);
187
188     cairo_select_font_face (cr, "Gonville-26", 0, 0);
189     cairo_set_font_size (cr, score->staff_height);
190
191     /* XXX: The hard-coded glyph indices here are very ugly. We should
192      * figure out how to lookup glyphs by name from this font. */
193     switch (note->duration) {
194     case SCORE_DURATION_1:
195         note_glyph.index = 127;
196         break;
197     case SCORE_DURATION_2:
198         note_glyph.index = 85;
199         break;
200     case SCORE_DURATION_4:
201     case SCORE_DURATION_8:
202     case SCORE_DURATION_16:
203     case SCORE_DURATION_32:
204     case SCORE_DURATION_64:
205     case SCORE_DURATION_128:
206     default:
207         note_glyph.index = 84;
208     }
209
210     note_glyph.x = 0;
211     note_glyph.y = score->space_height * line + score->line_width / 2.0;
212
213     cairo_set_source_rgb (cr, 0.0, 0.0, 0.0); /* black */
214     cairo_show_glyphs (cr, &note_glyph, 1);
215
216     cairo_restore (cr);
217 }
218
219 static void
220 _draw_staff (score_t *score, cairo_t *cr,
221              score_staff_t *staff, int staff_width)
222 {
223     int i;
224     cairo_glyph_t clef_glyph;
225     cairo_text_extents_t clef_extents;
226
227     cairo_save (cr);
228
229     cairo_select_font_face (cr, "Gonville-26", 0, 0);
230
231     cairo_set_font_size (cr, score->staff_height);
232
233     /* XXX: The hard-coded glyph indices here are very ugly. We should
234      * figure out how to lookup glyphs by name from this font. */
235     switch (staff->clef) {
236     case SCORE_CLEF_G:
237     default:
238         clef_glyph.index = 46;
239         clef_glyph.y = 3 * score->space_height;
240         break;
241     case SCORE_CLEF_F:
242         clef_glyph.index = 45;
243         clef_glyph.y = 1 * score->space_height;
244         break;
245     }
246     clef_glyph.x = 3 * score->line_width;
247     clef_glyph.y += 1;
248
249     cairo_set_source_rgb (cr, 0.0, 0.0, 0.0); /* black */
250     cairo_show_glyphs (cr, &clef_glyph, 1);
251
252     cairo_glyph_extents (cr, &clef_glyph, 1, &clef_extents);
253
254     /* Draw staff lines */
255     for (i = 0; i < 5; i++) {
256         cairo_move_to (cr, 0, i * score->space_height + score->line_width / 2.0);
257         cairo_rel_line_to (cr, staff_width, 0);
258     }
259
260     cairo_set_line_width (cr, score->line_width);
261
262     cairo_set_source_rgb (cr, 0.0, 0.0, 0.0); /* black */
263     cairo_stroke (cr);
264
265     /* Make space for clef before drawing notes */
266     cairo_translate (cr, (int) (1.5 * clef_extents.width), 0);
267
268     /* Draw notes */
269     for (i = 0; i < staff->num_notes; i++) {
270         _draw_note (score, cr, staff, staff->notes[i]);
271         cairo_translate (cr, score->space_height * 2.0, 0);
272     }
273
274     cairo_restore (cr);
275 }
276
277 void
278 score_draw (score_t *score, cairo_t *cr)
279 {
280     int i;
281     int staff_width = score->width;
282
283     cairo_save (cr);
284
285     if (score->num_braces)
286     {
287         int brace_width;
288
289         for (i = 0; i < score->num_braces; i++)
290             _draw_brace (score, cr, score->braces[i], &brace_width);
291
292         /* Subtract space for brace itself */
293         cairo_translate (cr, brace_width, 0);
294         staff_width -= brace_width;
295
296         /* As well as some padding */
297         cairo_translate (cr, 2, 0);
298         staff_width -= 2;
299     }
300
301     /* Vertical lines at each end */
302     cairo_rectangle (cr,
303                      score->line_width / 2.0,
304                      score->line_width / 2.0,
305                      staff_width - score->line_width,
306                      score->staff_height * 3);
307     cairo_set_source_rgb (cr, 0.0, 0.0, 0.0); /* black */
308     cairo_set_line_width (cr, score->line_width);
309     cairo_stroke (cr);
310
311     for (i = 0; i < score->num_staves; i++) {
312         _draw_staff (score, cr, score->staves[i], staff_width);
313         cairo_translate (cr, 0, 2 * score->staff_height);
314     }
315
316     cairo_restore (cr);
317 }
318
319 void
320 score_add_brace (score_t *score, int staves)
321 {
322     score_brace_t *brace;
323
324     brace = talloc (score, score_brace_t);
325     if (brace == NULL)
326         return;
327
328     brace->first_staff = score->num_staves;
329     brace->num_staves = staves;
330
331     score->num_braces++;
332     score->braces = talloc_realloc (score,
333                                     score->braces,
334                                     score_brace_t*,
335                                     score->num_braces);
336     if (score->braces == NULL) {
337         score->num_braces = 0;
338         return;
339     }
340
341     score->braces[score->num_braces - 1] = brace;
342
343 }
344
345 score_staff_t *
346 score_add_staff (score_t *score, score_clef_t clef)
347 {
348     score_staff_t *staff;
349
350     staff = talloc (score, score_staff_t);
351     if (staff == NULL)
352         return NULL;
353
354     staff->clef = clef;
355
356     staff->notes = NULL;
357     staff->num_notes = 0;
358
359     score->num_staves++;
360     score->staves = talloc_realloc (score,
361                                     score->staves,
362                                     score_staff_t*,
363                                     score->num_staves);
364     if (score->staves == NULL) {
365         score->num_staves = 0;
366         return NULL;
367     }
368
369     score->staves[score->num_staves - 1] = staff;
370
371     return staff;
372 }
373
374 score_note_t *
375 score_staff_add_note (score_staff_t *staff,
376                       score_pitch_t pitch,
377                       int octave,
378                       score_duration_t duration)
379 {
380     score_note_t *note;
381
382     note = talloc (staff, score_note_t);
383     if (note == NULL)
384         return NULL;
385
386     note->pitch = pitch;
387     note->octave = octave;
388     note->duration = duration;
389
390     staff->num_notes++;
391     staff->notes = talloc_realloc (staff,
392                                    staff->notes,
393                                    score_note_t*,
394                                    staff->num_notes);
395     if (staff->notes == NULL) {
396         staff->num_notes = 0;
397         return NULL;
398     }
399
400     staff->notes[staff->num_notes - 1] = note;
401
402     return note;
403 }
404