]> git.cworth.org Git - scherzo/blob - score.c
Add correct sizing/spacing of staves and braces for ledger lines.
[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 <string.h>
22
23 #include "score.h"
24
25 struct score_staff
26 {
27     score_clef_t clef;
28
29     score_note_t **notes;
30     int num_notes;
31
32     /* How many ledger lines are needed for current notes */
33     int upper_ledger_lines;
34     int lower_ledger_lines;
35
36     /* Y position of top full line of staff */
37     int y_pos;
38 };
39
40 typedef struct score_brace
41 {
42     int first_staff;
43     int num_staves;
44 } score_brace_t;
45
46 struct score
47 {
48     /* Nominal height of a single staff (ledger lines may make it larger) */
49     int staff_height;
50
51     /* Height of one space within a staff */
52     int space_height;
53
54     /* Minimal line width for staff lines */
55     int line_width;
56
57     /* Full width of staff */
58     int width;
59
60     score_brace_t **braces;
61     int num_braces;
62     int brace_width;
63
64     score_staff_t **staves;
65     int num_staves;
66 };
67
68 score_t *
69 score_create (void *ctx)
70 {
71     score_t *score;
72
73     score = talloc (ctx, score_t);
74     if (score == NULL)
75         return NULL;
76
77     /* Also sets space_height and line_width */
78     score_set_staff_height (score, 24);
79
80     /* Just to have some nominal width. */
81     score->width = 800;
82
83     score->braces = NULL;
84     score->num_braces = 0;
85
86     score->staves = NULL;
87     score->num_staves = 0;
88
89     return score;
90 }
91
92 int
93 score_set_staff_height (score_t *score, int height)
94 {
95     score->space_height = (int) height / 4;
96     score->staff_height = score->space_height * 4;
97
98     score->line_width = score->space_height / 10;
99     if (score->line_width == 0)
100         score->line_width = 1;
101
102     return score->staff_height;
103 }
104
105 void
106 score_set_width (score_t *score, int width)
107 {
108     score->width = width;
109 }
110
111 /* Returns in brace_width the width of the brace */
112 static void
113 _draw_brace (score_t *score, cairo_t *cr,
114              score_brace_t *brace, int *brace_width)
115 {
116     cairo_glyph_t brace_glyph;
117     cairo_text_extents_t brace_extents;
118     double top, bottom;
119
120     if (brace->num_staves == 0)
121         return;
122
123     cairo_save (cr);
124
125     top = score->staves[brace->first_staff]->y_pos;
126     bottom = score->staves[brace->first_staff + brace->num_staves - 1]->y_pos + score->staff_height;
127
128     cairo_select_font_face (cr, "Gonville-Brace", 0, 0);
129
130     /* XXX: This hard-coded glyph index is pretty ugly. We should
131      * figure out how to lookup the glyph we want, (though, as it
132      * turns out, this brace font pretty much just has numbered glyph
133      * names for different sizes, so it wouldn't be all that different
134      * than just the bare index here). */
135     brace_glyph.index = 300;
136     brace_glyph.x = 0;
137     brace_glyph.y = top + (bottom - top) / 2.0 + score->line_width / 2.0;
138
139     /* XXX: This font size (in conjunction with the glyph selection)
140      * is a rough guess at best. We should figure out how the brace
141      * font is intended to be used and actually measure to find the
142      * correctly-sized glyph. */
143     cairo_set_font_size (cr, (bottom - top) / 3.85);
144
145     cairo_glyph_extents (cr, &brace_glyph, 1, &brace_extents);
146
147     /* Subtract space for brace itself */
148     cairo_translate (cr, -brace_extents.x_bearing, 0);
149
150     cairo_set_source_rgb (cr, 0.0, 0.0, 0.0); /* black */
151     cairo_show_glyphs (cr, &brace_glyph, 1);
152
153     cairo_restore (cr);
154
155     *brace_width = (int) -brace_extents.x_bearing;
156 }
157
158 /* Line containing middle C for the given clef. */
159 static int
160 _score_clef_c_line (score_clef_t clef)
161 {
162     switch (clef)
163     {
164     default:
165     case SCORE_CLEF_G:
166         return 5;
167     case SCORE_CLEF_F:
168         return -1;
169     }
170 }
171
172 static double
173 _score_note_to_line (score_staff_t *staff, score_note_t *note)
174 {
175     score_pitch_name_t name = SCORE_PITCH_NAME (note->pitch);
176     int c_line = _score_clef_c_line (staff->clef);
177
178     return c_line - (name - SCORE_PITCH_NAME_C) / 2.0 - 3.5 * (note->octave - 4);
179 }
180
181 static void
182 _draw_note (score_t *score, cairo_t *cr,
183             score_staff_t *staff, score_note_t *note)
184 {
185     double line;
186     cairo_glyph_t note_glyph;
187     static double extend_factor = 0.25;
188
189     void _draw_ledger_line (double line, double width) {
190         cairo_move_to (cr,
191                        - width * extend_factor / 2.0,
192                        score->space_height * line + score->line_width / 2.0);
193         cairo_rel_line_to (cr, (1 + extend_factor) * width, 0);
194         cairo_stroke (cr);
195     }
196
197     cairo_save (cr);
198
199     /* Which line should the note appear on? Line 0 is the top line of
200      * the staff and increasing downwards. (Negative values indicate a
201      * note on a ledger line above the staff). Values half way between
202      * integers indicate notes appearing on a space between two staff
203      * lines (or ledger lines). */
204     line = _score_note_to_line (staff, note);
205
206     cairo_select_font_face (cr, "Gonville-26", 0, 0);
207     cairo_set_font_size (cr, score->staff_height);
208
209     /* XXX: The hard-coded glyph indices here are very ugly. We should
210      * figure out how to lookup glyphs by name from this font. */
211     switch (note->duration) {
212     case SCORE_DURATION_1:
213         note_glyph.index = 127;
214         break;
215     case SCORE_DURATION_2:
216         note_glyph.index = 85;
217         break;
218     case SCORE_DURATION_4:
219     case SCORE_DURATION_8:
220     case SCORE_DURATION_16:
221     case SCORE_DURATION_32:
222     case SCORE_DURATION_64:
223     case SCORE_DURATION_128:
224     default:
225         note_glyph.index = 84;
226     }
227
228     note_glyph.x = 0;
229     note_glyph.y = score->space_height * line;
230
231     if (line < 0 || line > 4) {
232         int i;
233         cairo_text_extents_t note_extents;
234
235         cairo_glyph_extents (cr, &note_glyph, 1, &note_extents);
236
237         if (line < 0) {
238             for (i = -1; i >= line; i--)
239                 _draw_ledger_line (i, note_extents.width);
240         } else {
241             for (i = 5; i <= line; i++)
242                 _draw_ledger_line (i, note_extents.width);
243         }
244     }
245
246     cairo_set_source_rgb (cr, 0.0, 0.0, 0.0); /* black */
247     cairo_show_glyphs (cr, &note_glyph, 1);
248
249     cairo_restore (cr);
250 }
251
252 static void
253 _draw_staff (score_t *score, cairo_t *cr,
254              score_staff_t *staff, int staff_width)
255 {
256     int i;
257     cairo_glyph_t clef_glyph;
258
259     cairo_save (cr);
260
261     cairo_translate (cr, 0, staff->y_pos);
262
263     cairo_select_font_face (cr, "Gonville-26", 0, 0);
264
265     cairo_set_font_size (cr, score->staff_height);
266
267     /* XXX: The hard-coded glyph indices here are very ugly. We should
268      * figure out how to lookup glyphs by name from this font. */
269     switch (staff->clef) {
270     case SCORE_CLEF_G:
271     default:
272         clef_glyph.index = 46;
273         clef_glyph.y = 3 * score->space_height;
274         break;
275     case SCORE_CLEF_F:
276         clef_glyph.index = 45;
277         clef_glyph.y = 1 * score->space_height;
278         break;
279     }
280     clef_glyph.x = 3 * score->line_width;
281     clef_glyph.y += score->line_width / 2.0;
282
283     cairo_set_source_rgb (cr, 0.0, 0.0, 0.0); /* black */
284     cairo_show_glyphs (cr, &clef_glyph, 1);
285
286     /* Draw staff lines */
287     for (i = 0; i < 5; i++) {
288         cairo_move_to (cr, 0, i * score->space_height + score->line_width / 2.0);
289         cairo_rel_line_to (cr, staff_width, 0);
290     }
291
292     cairo_set_line_width (cr, score->line_width);
293
294     cairo_set_source_rgb (cr, 0.0, 0.0, 0.0); /* black */
295     cairo_stroke (cr);
296
297     /* Make space for clef before drawing notes */
298     cairo_translate (cr, (int) (4 * score->space_height), 0);
299
300     /* Draw notes */
301     for (i = 0; i < staff->num_notes; i++) {
302         _draw_note (score, cr, staff, staff->notes[i]);
303         /* Draw all notes concurrent for now (as a chord)
304         cairo_translate (cr, score->space_height * 2.0, 0);
305         */
306     }
307
308     cairo_restore (cr);
309 }
310
311 void
312 score_draw (score_t *score, cairo_t *cr)
313 {
314     int i;
315     int staff_width = score->width;
316     int staff_y_pos;
317
318     if (score->num_staves == 0)
319         return;
320
321     cairo_save (cr);
322
323     /* Before drawing anything, position each staff based on the size
324      * of each (including ledger lines) */
325     staff_y_pos = 0;
326     for (i = 0; i < score->num_staves; i++) {
327         score_staff_t *staff = score->staves[i];
328         staff_y_pos += staff->upper_ledger_lines * score->space_height;
329         staff->y_pos = staff_y_pos;
330         staff_y_pos += (score->staff_height +
331                         staff->lower_ledger_lines * score->space_height +
332                         score->staff_height);
333     }
334
335     if (score->num_braces)
336     {
337         int brace_width;
338
339         for (i = 0; i < score->num_braces; i++)
340             _draw_brace (score, cr, score->braces[i], &brace_width);
341
342         /* Subtract space for brace itself */
343         cairo_translate (cr, brace_width, 0);
344         staff_width -= brace_width;
345
346         /* As well as some padding */
347         cairo_translate (cr, 2, 0);
348         staff_width -= 2;
349     }
350
351     /* Vertical lines at each end */
352     cairo_rectangle (cr,
353                      score->line_width / 2.0,
354                      score->staves[0]->y_pos + score->line_width / 2.0,
355                      staff_width - score->line_width,
356                      score->staves[score->num_staves-1]->y_pos + score->staff_height - score->staves[0]->y_pos);
357     cairo_set_source_rgb (cr, 0.0, 0.0, 0.0); /* black */
358     cairo_set_line_width (cr, score->line_width);
359     cairo_stroke (cr);
360
361     for (i = 0; i < score->num_staves; i++) {
362         score_staff_t *staff = score->staves[i];
363         _draw_staff (score, cr, staff, staff_width);
364     }
365
366     cairo_restore (cr);
367 }
368
369 void
370 score_add_brace (score_t *score, int staves)
371 {
372     score_brace_t *brace;
373
374     brace = talloc (score, score_brace_t);
375     if (brace == NULL)
376         return;
377
378     brace->first_staff = score->num_staves;
379     brace->num_staves = staves;
380
381     score->num_braces++;
382     score->braces = talloc_realloc (score,
383                                     score->braces,
384                                     score_brace_t*,
385                                     score->num_braces);
386     if (score->braces == NULL) {
387         score->num_braces = 0;
388         return;
389     }
390
391     score->braces[score->num_braces - 1] = brace;
392
393 }
394
395 score_staff_t *
396 score_add_staff (score_t *score, score_clef_t clef)
397 {
398     score_staff_t *staff;
399
400     staff = talloc (score, score_staff_t);
401     if (staff == NULL)
402         return NULL;
403
404     staff->clef = clef;
405
406     staff->notes = NULL;
407     staff->num_notes = 0;
408
409     staff->upper_ledger_lines = 0;
410     staff->lower_ledger_lines = 0;
411
412     score->num_staves++;
413     score->staves = talloc_realloc (score,
414                                     score->staves,
415                                     score_staff_t*,
416                                     score->num_staves);
417     if (score->staves == NULL) {
418         score->num_staves = 0;
419         return NULL;
420     }
421
422     score->staves[score->num_staves - 1] = staff;
423
424     return staff;
425 }
426
427 score_note_t *
428 score_add_note (score_staff_t *staff,
429                 score_pitch_t pitch,
430                 int octave,
431                 score_duration_t duration)
432 {
433     score_note_t *note;
434     double line;
435
436     note = talloc (staff, score_note_t);
437     if (note == NULL)
438         return NULL;
439
440     note->staff = staff;
441     note->pitch = pitch;
442     note->octave = octave;
443     note->duration = duration;
444
445     line = _score_note_to_line (staff, note);
446     if (line < 0) {
447         int lines = (int) (- line);
448         if (lines > staff->upper_ledger_lines)
449             staff->upper_ledger_lines = lines;
450     } else {
451         int lines = (int) (line - 4);
452         if (lines > staff->lower_ledger_lines)
453             staff->lower_ledger_lines = lines;
454     }
455
456     staff->num_notes++;
457     staff->notes = talloc_realloc (staff,
458                                    staff->notes,
459                                    score_note_t*,
460                                    staff->num_notes);
461     if (staff->notes == NULL) {
462         staff->num_notes = 0;
463         return NULL;
464     }
465
466     staff->notes[staff->num_notes - 1] = note;
467
468     return note;
469 }
470
471 void
472 score_remove_note (score_note_t *note)
473 {
474     score_staff_t *staff = note->staff;
475     int i;
476
477     for (i = 0; i < staff->num_notes; i++)
478         if (staff->notes[i] == note)
479             break;
480
481     if (i == staff->num_notes)
482         return;
483
484     if (i < staff->num_notes - 1)
485     {
486         memmove (staff->notes + i,
487                  staff->notes + i + 1, 
488                  (staff->num_notes - 1 - i) * sizeof (score_note_t *));
489     }
490
491     staff->num_notes -= 1;
492 }
493
494 score_note_t *
495 score_staff_find_note (score_staff_t *staff,
496                        score_pitch_t pitch,
497                        int octave,
498                        score_duration_t duration)
499 {
500     int i;
501     score_note_t *note;
502
503     for (i = 0; i < staff->num_notes; i++) {
504         note = staff->notes[i];
505         if (note->pitch == pitch &&
506             note->octave == octave &&
507             note->duration == duration)
508         {
509             return note;
510         }
511     }
512
513     return NULL;
514 }
515