]> git.cworth.org Git - scherzo/blob - score.c
Drop the enumerated pitch values.
[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 c_line = _score_clef_c_line (staff->clef);
182
183     return c_line - (name - PITCH_NAME_C) / 2.0 - 3.5 * (note->octave - 4);
184 }
185
186 /* chord->width is updated as a side effect */
187 static void
188 _draw_chord (score_t *score, cairo_t *cr,
189              score_staff_t *staff, score_chord_t *chord)
190 {
191     PangoRectangle ink_extents;
192     PangoRectangle logical_extents;
193     double total_staff_height;
194     PangoLayout *layout;
195     PangoFontDescription *font_description;
196
197     /* XXX: The staff should manage this height itself. */
198     total_staff_height = (staff->upper_ledger_lines * score->space_height +
199                           score->staff_height +
200                           staff->lower_ledger_lines * score->space_height);
201
202     cairo_save (cr);
203
204     font_description = pango_font_description_new ();
205     pango_font_description_set_family (font_description, "serif");
206     pango_font_description_set_absolute_size (font_description,
207                         score->space_height * 3 * PANGO_SCALE);
208
209     layout = pango_cairo_create_layout (cr);
210     pango_layout_set_font_description (layout, font_description);
211     pango_layout_set_markup (layout, chord->name, -1);
212
213     pango_layout_line_get_pixel_extents (pango_layout_get_line (layout, 0),
214                                          &ink_extents, &logical_extents);
215
216     if (staff->clef == SCORE_CLEF_G)
217         cairo_move_to (cr, 0, - score->space_height * 0.5);
218     else
219         cairo_move_to (cr, 0, score->space_height * 0.5 + total_staff_height +
220                        logical_extents.height);
221
222     pango_cairo_show_layout_line (cr, pango_layout_get_line (layout, 0));
223
224     g_object_unref (layout);
225     pango_font_description_free (font_description);
226
227     chord->width = logical_extents.width;
228
229     cairo_restore (cr);
230 }
231
232 static void
233 _draw_note (score_t *score, cairo_t *cr,
234             score_staff_t *staff, score_note_t *note)
235 {
236     double line;
237     cairo_glyph_t note_glyph[2];
238     static double extend_factor = 0.25;
239     cairo_text_extents_t extents;
240     int num_glyphs = 0;
241
242     void _draw_ledger_line (double line, double offset, double width) {
243         cairo_move_to (cr, offset - extend_factor * width / 2.0,
244                        score->space_height * line + score->line_width / 2.0);
245         cairo_rel_line_to (cr, (1 + extend_factor) * width, 0);
246         cairo_stroke (cr);
247     }
248
249     cairo_save (cr);
250
251     /* Move right so that X==0 is natural position for non-displaced
252      * noteheads.
253      */
254     cairo_translate (cr, score->space_height, 0);
255
256     /* Which line should the note appear on? Line 0 is the top line of
257      * the staff and increasing downwards. (Negative values indicate a
258      * note on a ledger line above the staff). Values half way between
259      * integers indicate notes appearing on a space between two staff
260      * lines (or ledger lines). */
261     line = _score_note_to_line (staff, note);
262
263     cairo_select_font_face (cr, "Gonville-26", 0, 0);
264     cairo_set_font_size (cr, score->staff_height);
265
266     /* XXX: The hard-coded glyph indices here are very ugly. We should
267      * figure out how to lookup glyphs by name from this font. */
268     switch (PITCH_ACCIDENTAL (note->pitch)) {
269     case PITCH_ACCIDENTAL_DOUBLE_FLAT:
270             note_glyph[num_glyphs].index = 77;
271             break;
272     case PITCH_ACCIDENTAL_FLAT:
273             note_glyph[num_glyphs].index = 68;
274             break;
275     case PITCH_ACCIDENTAL_NATURAL:
276             note_glyph[num_glyphs].index = 101;
277             break;
278     case PITCH_ACCIDENTAL_SHARP:
279             note_glyph[num_glyphs].index = 134;
280             break;
281     case PITCH_ACCIDENTAL_DOUBLE_SHARP:
282             note_glyph[num_glyphs].index = 142;
283             break;
284     }
285
286     if (PITCH_ACCIDENTAL (note->pitch) != PITCH_ACCIDENTAL_NATURAL)
287     {
288             note_glyph[num_glyphs].x = 0;
289
290             note_glyph[num_glyphs].y = score->space_height * line;
291
292             num_glyphs++;
293
294             cairo_glyph_extents (cr, note_glyph, num_glyphs, &extents);
295
296 #define ACCIDENTAL_NOTE_SPACING (score->space_height * .15)
297
298             note_glyph[0].x = - (extents.width + ACCIDENTAL_NOTE_SPACING);
299     }
300
301     switch (note->duration) {
302     case SCORE_DURATION_1:
303         note_glyph[num_glyphs].index = 127;
304         break;
305     case SCORE_DURATION_2:
306         note_glyph[num_glyphs].index = 85;
307         break;
308     case SCORE_DURATION_4:
309     case SCORE_DURATION_8:
310     case SCORE_DURATION_16:
311     case SCORE_DURATION_32:
312     case SCORE_DURATION_64:
313     case SCORE_DURATION_128:
314     default:
315         note_glyph[num_glyphs].index = 84;
316     }
317
318     note_glyph[num_glyphs].x = 0;
319     note_glyph[num_glyphs].y = score->space_height * line;
320
321     num_glyphs++;
322
323     if (line < 0 || line > 4) {
324         double offset, width;
325         int i;
326
327         cairo_glyph_extents (cr, note_glyph, num_glyphs, &extents);
328         offset = note_glyph[0].x + extents.x_bearing;
329         width = extents.width;
330
331         if (line < 0) {
332             for (i = -1; i >= line; i--)
333                 _draw_ledger_line (i, offset, width);
334         } else {
335             for (i = 5; i <= line; i++)
336                 _draw_ledger_line (i, offset, width);
337         }
338     }
339
340     cairo_set_source_rgb (cr,
341                           note->color.r,
342                           note->color.g,
343                           note->color.b);
344     cairo_show_glyphs (cr, note_glyph, num_glyphs);
345
346     cairo_restore (cr);
347 }
348
349 static void
350 _draw_staff (score_t *score, cairo_t *cr,
351              score_staff_t *staff, int staff_width)
352 {
353     int i;
354     cairo_glyph_t clef_glyph;
355
356     cairo_save (cr);
357
358     cairo_translate (cr, 0, staff->y_pos);
359
360     cairo_select_font_face (cr, "Gonville-26", 0, 0);
361
362     cairo_set_font_size (cr, score->staff_height);
363
364     /* XXX: The hard-coded glyph indices here are very ugly. We should
365      * figure out how to lookup glyphs by name from this font. */
366     switch (staff->clef) {
367     case SCORE_CLEF_G:
368     default:
369         clef_glyph.index = 46;
370         clef_glyph.y = 3 * score->space_height;
371         break;
372     case SCORE_CLEF_F:
373         clef_glyph.index = 45;
374         clef_glyph.y = 1 * score->space_height;
375         break;
376     }
377     clef_glyph.x = 3 * score->line_width;
378     clef_glyph.y += score->line_width / 2.0;
379
380     cairo_set_source_rgb (cr, 0.0, 0.0, 0.0); /* black */
381     cairo_show_glyphs (cr, &clef_glyph, 1);
382
383     /* Draw staff lines */
384     for (i = 0; i < 5; i++) {
385         cairo_move_to (cr, 0, i * score->space_height + score->line_width / 2.0);
386         cairo_rel_line_to (cr, staff_width, 0);
387     }
388
389     cairo_set_line_width (cr, score->line_width);
390
391     cairo_set_source_rgb (cr, 0.0, 0.0, 0.0); /* black */
392     cairo_stroke (cr);
393
394     /* Make space for clef before drawing notes */
395     cairo_translate (cr, (int) (4 * score->space_height), 0);
396
397     /* Draw chord symbols */
398     cairo_save (cr);
399     {
400         for (i = 0; i < staff->num_chords; i++) {
401             _draw_chord (score, cr, staff, staff->chords[i]);
402             cairo_translate (cr, staff->chords[i]->width, 0.0);
403         }
404     }
405     cairo_restore (cr);
406
407     /* Draw notes */
408     for (i = 0; i < staff->num_notes; i++) {
409         _draw_note (score, cr, staff, staff->notes[i]);
410         /* Draw all notes concurrent for now (as a chord)
411         cairo_translate (cr, score->space_height * 2.0, 0);
412         */
413     }
414
415     cairo_restore (cr);
416 }
417
418 void
419 score_draw (score_t *score, cairo_t *cr)
420 {
421     int i;
422     int staff_width = score->width;
423     int staff_y_pos;
424
425     if (score->num_staves == 0)
426         return;
427
428     cairo_save (cr);
429
430     /* Before drawing anything, position each staff based on the size
431      * of each (including ledger lines) */
432     staff_y_pos = 0;
433     for (i = 0; i < score->num_staves; i++) {
434         score_staff_t *staff = score->staves[i];
435         staff_y_pos += staff->upper_ledger_lines * score->space_height;
436         staff->y_pos = staff_y_pos;
437         staff_y_pos += (score->staff_height +
438                         staff->lower_ledger_lines * score->space_height +
439                         score->staff_height);
440     }
441
442     if (score->num_braces)
443     {
444         /* Initialize to keep the compiler quiet. */
445         int brace_width = 0;
446
447         for (i = 0; i < score->num_braces; i++)
448             _draw_brace (score, cr, score->braces[i], &brace_width);
449
450         /* Subtract space for brace itself */
451         cairo_translate (cr, brace_width, 0);
452         staff_width -= brace_width;
453
454         /* As well as some padding */
455         cairo_translate (cr, 2, 0);
456         staff_width -= 2;
457     }
458
459     /* Vertical lines at each end */
460     cairo_rectangle (cr,
461                      score->line_width / 2.0,
462                      score->staves[0]->y_pos + score->line_width / 2.0,
463                      staff_width - score->line_width,
464                      score->staves[score->num_staves-1]->y_pos + score->staff_height - score->staves[0]->y_pos);
465     cairo_set_source_rgb (cr, 0.0, 0.0, 0.0); /* black */
466     cairo_set_line_width (cr, score->line_width);
467     cairo_stroke (cr);
468
469     for (i = 0; i < score->num_staves; i++) {
470         score_staff_t *staff = score->staves[i];
471         _draw_staff (score, cr, staff, staff_width);
472     }
473
474     cairo_restore (cr);
475 }
476
477 void
478 score_add_brace (score_t *score, int staves)
479 {
480     score_brace_t *brace;
481
482     brace = talloc (score, score_brace_t);
483     if (brace == NULL)
484         return;
485
486     brace->first_staff = score->num_staves;
487     brace->num_staves = staves;
488
489     score->num_braces++;
490     score->braces = talloc_realloc (score,
491                                     score->braces,
492                                     score_brace_t*,
493                                     score->num_braces);
494     if (score->braces == NULL) {
495         score->num_braces = 0;
496         return;
497     }
498
499     score->braces[score->num_braces - 1] = brace;
500
501 }
502
503 score_staff_t *
504 score_add_staff (score_t *score, score_clef_t clef)
505 {
506     score_staff_t *staff;
507
508     staff = talloc (score, score_staff_t);
509     if (staff == NULL)
510         return NULL;
511
512     staff->clef = clef;
513
514     staff->notes = NULL;
515     staff->num_notes = 0;
516
517     staff->chords = NULL;
518     staff->num_chords = 0;
519
520     staff->upper_ledger_lines = 0;
521     staff->lower_ledger_lines = 0;
522
523     score->num_staves++;
524     score->staves = talloc_realloc (score,
525                                     score->staves,
526                                     score_staff_t*,
527                                     score->num_staves);
528     if (score->staves == NULL) {
529         score->num_staves = 0;
530         return NULL;
531     }
532
533     score->staves[score->num_staves - 1] = staff;
534
535     return staff;
536 }
537
538 score_chord_t *
539 score_add_chord (score_staff_t *staff,
540                  const char *name)
541 {
542     score_chord_t *chord;
543
544     chord = talloc (staff, score_chord_t);
545     if (chord == NULL)
546         return NULL;
547
548     talloc_steal (chord, name);
549
550     chord->staff = staff;
551     chord->name = talloc_strdup (chord, name);
552
553     /* The width will get set correctly the first time _draw_chord is
554      * called. */
555     chord->width = 0.0;
556
557     staff->num_chords++;
558     staff->chords = talloc_realloc (staff,
559                                     staff->chords,
560                                     score_chord_t*,
561                                     staff->num_chords);
562     if (staff->chords == NULL) {
563         staff->num_chords = 0;
564         return NULL;
565     }
566
567     staff->chords[staff->num_chords - 1] = chord;
568
569     return chord;
570 }
571
572 void
573 score_remove_chord (score_chord_t *chord)
574 {
575     score_staff_t *staff = chord->staff;
576     int i;
577
578     for (i = 0; i < staff->num_chords; i++)
579         if (staff->chords[i] == chord)
580             break;
581
582     if (i == staff->num_chords)
583         return;
584
585     if (i < staff->num_chords - 1)
586     {
587         memmove (staff->chords + i,
588                  staff->chords + i + 1, 
589                  (staff->num_chords - 1 - i) * sizeof (score_chord_t *));
590     }
591
592     staff->num_chords -= 1;
593 }
594
595 score_note_t *
596 score_add_note (score_staff_t *staff,
597                 pitch_t pitch,
598                 int octave,
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->octave == octave &&
610             note->duration == duration)
611         {
612             return note;
613         }
614     }
615
616     note = talloc (staff, score_note_t);
617     if (note == NULL)
618         return NULL;
619
620     note->staff = staff;
621     note->pitch = pitch;
622     note->octave = octave;
623     note->duration = duration;
624
625     note->color.r = 0.0;
626     note->color.g = 0.0;
627     note->color.b = 0.0;
628
629     line = _score_note_to_line (staff, note);
630     if (line < 0) {
631         int lines = (int) (- line);
632         if (lines > staff->upper_ledger_lines)
633             staff->upper_ledger_lines = lines;
634     } else {
635         int lines = (int) (line - 4);
636         if (lines > staff->lower_ledger_lines)
637             staff->lower_ledger_lines = lines;
638     }
639
640     staff->num_notes++;
641     staff->notes = talloc_realloc (staff,
642                                    staff->notes,
643                                    score_note_t*,
644                                    staff->num_notes);
645     if (staff->notes == NULL) {
646         staff->num_notes = 0;
647         return NULL;
648     }
649
650     staff->notes[staff->num_notes - 1] = note;
651
652     return note;
653 }
654
655 void
656 score_remove_note (score_note_t *note)
657 {
658     score_staff_t *staff = note->staff;
659     int i;
660
661     for (i = 0; i < staff->num_notes; i++)
662         if (staff->notes[i] == note)
663             break;
664
665     if (i == staff->num_notes)
666         return;
667
668     if (i < staff->num_notes - 1)
669     {
670         memmove (staff->notes + i,
671                  staff->notes + i + 1, 
672                  (staff->num_notes - 1 - i) * sizeof (score_note_t *));
673     }
674
675     staff->num_notes -= 1;
676
677     if (staff->num_notes == 0) {
678         staff->upper_ledger_lines = 0;
679         staff->lower_ledger_lines = 0;
680     }
681 }
682
683 void
684 score_set_note_color_rgb (score_note_t *note,
685                           double r,
686                           double g,
687                           double b)
688 {
689     note->color.r = r;
690     note->color.g = g;
691     note->color.b = b;
692 }
693
694 score_note_t *
695 score_staff_find_note (score_staff_t *staff,
696                        pitch_t pitch,
697                        int octave,
698                        score_duration_t duration)
699 {
700     int i;
701     score_note_t *note;
702
703     for (i = 0; i < staff->num_notes; i++) {
704         note = staff->notes[i];
705         if (note->pitch == pitch &&
706             note->octave == octave &&
707             note->duration == duration)
708         {
709             return note;
710         }
711     }
712
713     return NULL;
714 }