]> git.cworth.org Git - scherzo/blob - score.c
0c868c6f4a255b93787533db9cb6a80a6ff85919
[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 <pango/pangocairo.h>
22
23 #include <string.h>
24
25 #include "score.h"
26
27 struct score_staff
28 {
29     score_clef_t clef;
30
31     score_chord_t **chords;
32     int num_chords;
33
34     score_note_t **notes;
35     int num_notes;
36
37     /* How many ledger lines are needed for current notes */
38     int upper_ledger_lines;
39     int lower_ledger_lines;
40
41     /* Y position of top full line of staff */
42     int y_pos;
43 };
44
45 typedef struct score_brace
46 {
47     int first_staff;
48     int num_staves;
49 } score_brace_t;
50
51 struct score
52 {
53     /* Nominal height of a single staff (ledger lines may make it larger) */
54     int staff_height;
55
56     /* Height of one space within a staff */
57     int space_height;
58
59     /* Minimal line width for staff lines */
60     int line_width;
61
62     /* Full width of staff */
63     int width;
64
65     score_brace_t **braces;
66     int num_braces;
67     int brace_width;
68
69     score_staff_t **staves;
70     int num_staves;
71 };
72
73 score_t *
74 score_create (void *ctx)
75 {
76     score_t *score;
77
78     score = talloc (ctx, score_t);
79     if (score == NULL)
80         return NULL;
81
82     /* Also sets space_height and line_width */
83     score_set_staff_height (score, 76);
84
85     /* Just to have some nominal width. */
86     score->width = 1000;
87
88     score->braces = NULL;
89     score->num_braces = 0;
90
91     score->staves = NULL;
92     score->num_staves = 0;
93
94     return score;
95 }
96
97 int
98 score_set_staff_height (score_t *score, int height)
99 {
100     score->space_height = (int) height / 4;
101     score->staff_height = score->space_height * 4;
102
103     score->line_width = score->space_height / 10;
104     if (score->line_width == 0)
105         score->line_width = 1;
106
107     return score->staff_height;
108 }
109
110 void
111 score_set_width (score_t *score, int width)
112 {
113     score->width = width;
114 }
115
116 /* Returns in brace_width the width of the brace */
117 static void
118 _draw_brace (score_t *score, cairo_t *cr,
119              score_brace_t *brace, int *brace_width)
120 {
121     cairo_glyph_t brace_glyph;
122     cairo_text_extents_t brace_extents;
123     double top, bottom;
124
125     if (brace->num_staves == 0)
126         return;
127
128     cairo_save (cr);
129
130     top = score->staves[brace->first_staff]->y_pos;
131     bottom = score->staves[brace->first_staff + brace->num_staves - 1]->y_pos + score->staff_height;
132
133     cairo_select_font_face (cr, "Gonville-Brace", 0, 0);
134
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). */
140     brace_glyph.index = 300;
141     brace_glyph.x = 0;
142     brace_glyph.y = top + (bottom - top) / 2.0 + score->line_width / 2.0;
143
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, (bottom - top) / 3.85);
149
150     cairo_glyph_extents (cr, &brace_glyph, 1, &brace_extents);
151
152     /* Subtract space for brace itself */
153     cairo_translate (cr, -brace_extents.x_bearing, 0);
154
155     cairo_set_source_rgb (cr, 0.0, 0.0, 0.0); /* black */
156     cairo_show_glyphs (cr, &brace_glyph, 1);
157
158     cairo_restore (cr);
159
160     *brace_width = (int) -brace_extents.x_bearing;
161 }
162
163 /* Line containing middle C for the given clef. */
164 static int
165 _score_clef_c_line (score_clef_t clef)
166 {
167     switch (clef)
168     {
169     default:
170     case SCORE_CLEF_G:
171         return 5;
172     case SCORE_CLEF_F:
173         return -1;
174     }
175 }
176
177 static double
178 _score_note_to_line (score_staff_t *staff, score_note_t *note)
179 {
180     pitch_name_t name = PITCH_NAME (note->pitch);
181     int octave = PITCH_OCTAVE (note->pitch);
182     int c_line = _score_clef_c_line (staff->clef);
183
184     return c_line - (name - PITCH_NAME_C) / 2.0 - 3.5 * (octave - 4);
185 }
186
187 /* chord->width is updated as a side effect */
188 static void
189 _draw_chord (score_t *score, cairo_t *cr,
190              score_staff_t *staff, score_chord_t *chord)
191 {
192     PangoRectangle ink_extents;
193     PangoRectangle logical_extents;
194     double total_staff_height;
195     PangoLayout *layout;
196     PangoFontDescription *font_description;
197
198     /* XXX: The staff should manage this height itself. */
199     total_staff_height = (staff->upper_ledger_lines * score->space_height +
200                           score->staff_height +
201                           staff->lower_ledger_lines * score->space_height);
202
203     cairo_save (cr);
204
205     font_description = pango_font_description_new ();
206     pango_font_description_set_family (font_description, "serif");
207     pango_font_description_set_absolute_size (font_description,
208                         score->space_height * 3 * PANGO_SCALE);
209
210     layout = pango_cairo_create_layout (cr);
211     pango_layout_set_font_description (layout, font_description);
212     pango_layout_set_markup (layout, chord->name, -1);
213
214     pango_layout_line_get_pixel_extents (pango_layout_get_line (layout, 0),
215                                          &ink_extents, &logical_extents);
216
217     if (staff->clef == SCORE_CLEF_G)
218         cairo_move_to (cr, 0, - score->space_height * 0.5);
219     else
220         cairo_move_to (cr, 0, score->space_height * 0.5 + total_staff_height +
221                        logical_extents.height);
222
223     pango_cairo_show_layout_line (cr, pango_layout_get_line (layout, 0));
224
225     g_object_unref (layout);
226     pango_font_description_free (font_description);
227
228     chord->width = logical_extents.width;
229
230     cairo_restore (cr);
231 }
232
233 static void
234 _draw_note (score_t *score, cairo_t *cr,
235             score_staff_t *staff, score_note_t *note)
236 {
237     double line;
238     cairo_glyph_t note_glyph[2];
239     static double extend_factor = 0.25;
240     cairo_text_extents_t extents;
241     int num_glyphs = 0;
242
243     void _draw_ledger_line (double line, double offset, double width) {
244         cairo_move_to (cr, offset - extend_factor * width / 2.0,
245                        score->space_height * line + score->line_width / 2.0);
246         cairo_rel_line_to (cr, (1 + extend_factor) * width, 0);
247         cairo_stroke (cr);
248     }
249
250     cairo_save (cr);
251
252     /* Move right so that X==0 is natural position for non-displaced
253      * noteheads.
254      */
255     cairo_translate (cr, score->space_height, 0);
256
257     /* Which line should the note appear on? Line 0 is the top line of
258      * the staff and increasing downwards. (Negative values indicate a
259      * note on a ledger line above the staff). Values half way between
260      * integers indicate notes appearing on a space between two staff
261      * lines (or ledger lines). */
262     line = _score_note_to_line (staff, note);
263
264     cairo_select_font_face (cr, "Gonville-26", 0, 0);
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 (PITCH_ACCIDENTAL (note->pitch)) {
270     case PITCH_ACCIDENTAL_DOUBLE_FLAT:
271             note_glyph[num_glyphs].index = 77;
272             break;
273     case PITCH_ACCIDENTAL_FLAT:
274             note_glyph[num_glyphs].index = 68;
275             break;
276     case PITCH_ACCIDENTAL_NATURAL:
277             note_glyph[num_glyphs].index = 101;
278             break;
279     case PITCH_ACCIDENTAL_SHARP:
280             note_glyph[num_glyphs].index = 134;
281             break;
282     case PITCH_ACCIDENTAL_DOUBLE_SHARP:
283             note_glyph[num_glyphs].index = 142;
284             break;
285     }
286
287     if (PITCH_ACCIDENTAL (note->pitch) != PITCH_ACCIDENTAL_NATURAL)
288     {
289             note_glyph[num_glyphs].x = 0;
290
291             note_glyph[num_glyphs].y = score->space_height * line;
292
293             num_glyphs++;
294
295             cairo_glyph_extents (cr, note_glyph, num_glyphs, &extents);
296
297 #define ACCIDENTAL_NOTE_SPACING (score->space_height * .15)
298
299             note_glyph[0].x = - (extents.width + ACCIDENTAL_NOTE_SPACING);
300     }
301
302     switch (note->duration) {
303     case SCORE_DURATION_1:
304         note_glyph[num_glyphs].index = 127;
305         break;
306     case SCORE_DURATION_2:
307         note_glyph[num_glyphs].index = 85;
308         break;
309     case SCORE_DURATION_4:
310     case SCORE_DURATION_8:
311     case SCORE_DURATION_16:
312     case SCORE_DURATION_32:
313     case SCORE_DURATION_64:
314     case SCORE_DURATION_128:
315     default:
316         note_glyph[num_glyphs].index = 84;
317     }
318
319     note_glyph[num_glyphs].x = 0;
320     note_glyph[num_glyphs].y = score->space_height * line;
321
322     num_glyphs++;
323
324     if (line < 0 || line > 4) {
325         double offset, width;
326         int i;
327
328         cairo_glyph_extents (cr, note_glyph, num_glyphs, &extents);
329         offset = note_glyph[0].x + extents.x_bearing;
330         width = extents.width;
331
332         if (line < 0) {
333             for (i = -1; i >= line; i--)
334                 _draw_ledger_line (i, offset, width);
335         } else {
336             for (i = 5; i <= line; i++)
337                 _draw_ledger_line (i, offset, width);
338         }
339     }
340
341     cairo_set_source_rgb (cr,
342                           note->color.r,
343                           note->color.g,
344                           note->color.b);
345     cairo_show_glyphs (cr, note_glyph, num_glyphs);
346
347     cairo_restore (cr);
348 }
349
350 static void
351 _draw_staff (score_t *score, cairo_t *cr,
352              score_staff_t *staff, int staff_width)
353 {
354     int i;
355     cairo_glyph_t clef_glyph;
356
357     cairo_save (cr);
358
359     cairo_translate (cr, 0, staff->y_pos);
360
361     cairo_select_font_face (cr, "Gonville-26", 0, 0);
362
363     cairo_set_font_size (cr, score->staff_height);
364
365     /* XXX: The hard-coded glyph indices here are very ugly. We should
366      * figure out how to lookup glyphs by name from this font. */
367     switch (staff->clef) {
368     case SCORE_CLEF_G:
369     default:
370         clef_glyph.index = 46;
371         clef_glyph.y = 3 * score->space_height;
372         break;
373     case SCORE_CLEF_F:
374         clef_glyph.index = 45;
375         clef_glyph.y = 1 * score->space_height;
376         break;
377     }
378     clef_glyph.x = 3 * score->line_width;
379     clef_glyph.y += score->line_width / 2.0;
380
381     cairo_set_source_rgb (cr, 0.0, 0.0, 0.0); /* black */
382     cairo_show_glyphs (cr, &clef_glyph, 1);
383
384     /* Draw staff lines */
385     for (i = 0; i < 5; i++) {
386         cairo_move_to (cr, 0, i * score->space_height + score->line_width / 2.0);
387         cairo_rel_line_to (cr, staff_width, 0);
388     }
389
390     cairo_set_line_width (cr, score->line_width);
391
392     cairo_set_source_rgb (cr, 0.0, 0.0, 0.0); /* black */
393     cairo_stroke (cr);
394
395     /* Make space for clef before drawing notes */
396     cairo_translate (cr, (int) (4 * score->space_height), 0);
397
398     /* Draw chord symbols */
399     cairo_save (cr);
400     {
401         for (i = 0; i < staff->num_chords; i++) {
402             _draw_chord (score, cr, staff, staff->chords[i]);
403             cairo_translate (cr, staff->chords[i]->width, 0.0);
404         }
405     }
406     cairo_restore (cr);
407
408     /* Draw notes */
409     for (i = 0; i < staff->num_notes; i++) {
410         _draw_note (score, cr, staff, staff->notes[i]);
411         /* Draw all notes concurrent for now (as a chord)
412         cairo_translate (cr, score->space_height * 2.0, 0);
413         */
414     }
415
416     cairo_restore (cr);
417 }
418
419 void
420 score_draw (score_t *score, cairo_t *cr)
421 {
422     int i;
423     int staff_width = score->width;
424     int staff_y_pos;
425
426     if (score->num_staves == 0)
427         return;
428
429     cairo_save (cr);
430
431     /* Before drawing anything, position each staff based on the size
432      * of each (including ledger lines) */
433     staff_y_pos = 0;
434     for (i = 0; i < score->num_staves; i++) {
435         score_staff_t *staff = score->staves[i];
436         staff_y_pos += staff->upper_ledger_lines * score->space_height;
437         staff->y_pos = staff_y_pos;
438         staff_y_pos += (score->staff_height +
439                         staff->lower_ledger_lines * score->space_height +
440                         score->staff_height);
441     }
442
443     if (score->num_braces)
444     {
445         /* Initialize to keep the compiler quiet. */
446         int brace_width = 0;
447
448         for (i = 0; i < score->num_braces; i++)
449             _draw_brace (score, cr, score->braces[i], &brace_width);
450
451         /* Subtract space for brace itself */
452         cairo_translate (cr, brace_width, 0);
453         staff_width -= brace_width;
454
455         /* As well as some padding */
456         cairo_translate (cr, 2, 0);
457         staff_width -= 2;
458     }
459
460     /* Vertical lines at each end */
461     cairo_rectangle (cr,
462                      score->line_width / 2.0,
463                      score->staves[0]->y_pos + score->line_width / 2.0,
464                      staff_width - score->line_width,
465                      score->staves[score->num_staves-1]->y_pos + score->staff_height - score->staves[0]->y_pos);
466     cairo_set_source_rgb (cr, 0.0, 0.0, 0.0); /* black */
467     cairo_set_line_width (cr, score->line_width);
468     cairo_stroke (cr);
469
470     for (i = 0; i < score->num_staves; i++) {
471         score_staff_t *staff = score->staves[i];
472         _draw_staff (score, cr, staff, staff_width);
473     }
474
475     cairo_restore (cr);
476 }
477
478 void
479 score_add_brace (score_t *score, int staves)
480 {
481     score_brace_t *brace;
482
483     brace = talloc (score, score_brace_t);
484     if (brace == NULL)
485         return;
486
487     brace->first_staff = score->num_staves;
488     brace->num_staves = staves;
489
490     score->num_braces++;
491     score->braces = talloc_realloc (score,
492                                     score->braces,
493                                     score_brace_t*,
494                                     score->num_braces);
495     if (score->braces == NULL) {
496         score->num_braces = 0;
497         return;
498     }
499
500     score->braces[score->num_braces - 1] = brace;
501
502 }
503
504 score_staff_t *
505 score_add_staff (score_t *score, score_clef_t clef)
506 {
507     score_staff_t *staff;
508
509     staff = talloc (score, score_staff_t);
510     if (staff == NULL)
511         return NULL;
512
513     staff->clef = clef;
514
515     staff->notes = NULL;
516     staff->num_notes = 0;
517
518     staff->chords = NULL;
519     staff->num_chords = 0;
520
521     staff->upper_ledger_lines = 0;
522     staff->lower_ledger_lines = 0;
523
524     score->num_staves++;
525     score->staves = talloc_realloc (score,
526                                     score->staves,
527                                     score_staff_t*,
528                                     score->num_staves);
529     if (score->staves == NULL) {
530         score->num_staves = 0;
531         return NULL;
532     }
533
534     score->staves[score->num_staves - 1] = staff;
535
536     return staff;
537 }
538
539 score_chord_t *
540 score_add_chord (score_staff_t *staff,
541                  const char *name)
542 {
543     score_chord_t *chord;
544
545     chord = talloc (staff, score_chord_t);
546     if (chord == NULL)
547         return NULL;
548
549     talloc_steal (chord, name);
550
551     chord->staff = staff;
552     chord->name = talloc_strdup (chord, name);
553
554     /* The width will get set correctly the first time _draw_chord is
555      * called. */
556     chord->width = 0.0;
557
558     staff->num_chords++;
559     staff->chords = talloc_realloc (staff,
560                                     staff->chords,
561                                     score_chord_t*,
562                                     staff->num_chords);
563     if (staff->chords == NULL) {
564         staff->num_chords = 0;
565         return NULL;
566     }
567
568     staff->chords[staff->num_chords - 1] = chord;
569
570     return chord;
571 }
572
573 void
574 score_remove_chord (score_chord_t *chord)
575 {
576     score_staff_t *staff = chord->staff;
577     int i;
578
579     for (i = 0; i < staff->num_chords; i++)
580         if (staff->chords[i] == chord)
581             break;
582
583     if (i == staff->num_chords)
584         return;
585
586     if (i < staff->num_chords - 1)
587     {
588         memmove (staff->chords + i,
589                  staff->chords + i + 1, 
590                  (staff->num_chords - 1 - i) * sizeof (score_chord_t *));
591     }
592
593     staff->num_chords -= 1;
594 }
595
596 score_note_t *
597 score_add_note (score_staff_t *staff,
598                 pitch_t pitch,
599                 score_duration_t duration)
600 {
601     score_note_t *note;
602     double line;
603     int i;
604
605     /* Return existing note if already present. */
606     for (i = 0; i < staff->num_notes; i++) {
607         note = staff->notes[i];
608         if (note->pitch == pitch &&
609             note->duration == duration)
610         {
611             return note;
612         }
613     }
614
615     note = talloc (staff, score_note_t);
616     if (note == NULL)
617         return NULL;
618
619     note->staff = staff;
620     note->pitch = pitch;
621     note->duration = duration;
622
623     note->color.r = 0.0;
624     note->color.g = 0.0;
625     note->color.b = 0.0;
626
627     line = _score_note_to_line (staff, note);
628     if (line < 0) {
629         int lines = (int) (- line);
630         if (lines > staff->upper_ledger_lines)
631             staff->upper_ledger_lines = lines;
632     } else {
633         int lines = (int) (line - 4);
634         if (lines > staff->lower_ledger_lines)
635             staff->lower_ledger_lines = lines;
636     }
637
638     staff->num_notes++;
639     staff->notes = talloc_realloc (staff,
640                                    staff->notes,
641                                    score_note_t*,
642                                    staff->num_notes);
643     if (staff->notes == NULL) {
644         staff->num_notes = 0;
645         return NULL;
646     }
647
648     staff->notes[staff->num_notes - 1] = note;
649
650     return note;
651 }
652
653 void
654 score_remove_note (score_note_t *note)
655 {
656     score_staff_t *staff = note->staff;
657     int i;
658
659     for (i = 0; i < staff->num_notes; i++)
660         if (staff->notes[i] == note)
661             break;
662
663     if (i == staff->num_notes)
664         return;
665
666     if (i < staff->num_notes - 1)
667     {
668         memmove (staff->notes + i,
669                  staff->notes + i + 1, 
670                  (staff->num_notes - 1 - i) * sizeof (score_note_t *));
671     }
672
673     staff->num_notes -= 1;
674
675     if (staff->num_notes == 0) {
676         staff->upper_ledger_lines = 0;
677         staff->lower_ledger_lines = 0;
678     }
679 }
680
681 void
682 score_set_note_color_rgb (score_note_t *note,
683                           double r,
684                           double g,
685                           double b)
686 {
687     note->color.r = r;
688     note->color.g = g;
689     note->color.b = b;
690 }
691
692 score_note_t *
693 score_staff_find_note (score_staff_t *staff,
694                        pitch_t pitch,
695                        score_duration_t duration)
696 {
697     int i;
698     score_note_t *note;
699
700     for (i = 0; i < staff->num_notes; i++) {
701         note = staff->notes[i];
702         if (note->pitch == pitch && note->duration == duration)
703             return note;
704     }
705
706     return NULL;
707 }