]> git.cworth.org Git - scherzo/blob - score.c
598c298752d13d42634b487176dd90267419169c
[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
24 {
25     /* Height of a single staff */
26     int staff_height;
27
28     /* Height of one space within a staff */
29     int space_height;
30
31     /* Full width of staff */
32     int width;
33 };
34
35 score_t *
36 score_create (void *ctx)
37 {
38     score_t *score;
39
40     score = talloc (ctx, score_t);
41     score_set_staff_height (score, 24);
42
43     return score;
44 }
45
46 int
47 score_set_staff_height (score_t *score, int height)
48 {
49     score->space_height = (int) height / 4;
50     score->staff_height = score->space_height * 4;
51     return score->staff_height;
52 }
53
54 void
55 score_set_width (score_t *score, int width)
56 {
57     score->width = width;
58 }
59
60 typedef enum score_clef
61 {
62     SCORE_CLEF_G,
63     SCORE_CLEF_F
64 } score_clef_t;
65
66 static void
67 _draw_staff (score_t *score, cairo_t *cr, score_clef_t clef)
68 {
69     int i;
70     cairo_glyph_t glyph;
71
72     cairo_save (cr);
73
74     cairo_select_font_face (cr, "Gonville-26", 0, 0);
75
76     /* XXX: This font size is a rough guess at best. We should figure
77      * out to correctly measure, size, and place clefs. */
78     cairo_set_font_size (cr, 24);
79
80     /* XXX: The hard-coded glyph indices here are very ugly. We should
81      * figure out how to lookup glyphs by name from this font. */
82     switch (clef) {
83     case SCORE_CLEF_G:
84     default:
85         glyph.index = 46;
86         glyph.y = 3 * score->space_height;
87         break;
88     case SCORE_CLEF_F:
89         glyph.index = 45;
90         glyph.y = 1 * score->space_height;
91         break;
92     }
93     glyph.x = 2;
94
95     cairo_set_source_rgb (cr, 0.0, 0.0, 0.0); /* black */
96     cairo_show_glyphs (cr, &glyph, 1);
97
98     cairo_rectangle (cr,
99                      0.5, 0.5,
100                      score->width - 1.0, score->space_height * 4);
101     
102     for (i = 1; i < 4; i++) {
103         cairo_move_to (cr, 0, i * score->space_height + 0.5);
104         cairo_rel_line_to (cr, score->width, 0);
105     }
106
107     cairo_set_line_width (cr, 1.0);
108
109     cairo_set_source_rgb (cr, 0.0, 0.0, 0.0); /* black */
110     cairo_stroke (cr);
111
112     cairo_restore (cr);
113 }
114
115 static void
116 _draw_grand_staff (score_t *score, cairo_t *cr)
117 {
118 #define BRACE_GLYPHS 1
119     cairo_glyph_t brace;
120     cairo_save (cr);
121
122     /* Brace test */
123     cairo_select_font_face (cr, "Gonville-Brace", 0, 0);
124
125     /* XXX: This font size (in conjunction with the glyph selection)
126      * is a rough guess at best. We should figure out how the brace
127      * font is intended to be used and actually measure to find the
128      * correctly sized glyph. */
129     cairo_set_font_size (cr, 40);
130
131     cairo_translate (cr, 5, 0);
132     score->width -= 5;
133
134     /* XXX: This hard-coded glyph index is pretty ugly. We should
135      * figure out how to lookup the glyph we want, (though, as it
136      * turns out, this brace font pretty much just has numbered glyph
137      * names for different sizes, so it wouldn't be all that different
138      * than just the bare index here). */
139     brace.index = 185;
140     brace.x = 0;
141     brace.y = score->staff_height * 1.5;
142
143     cairo_set_source_rgb (cr, 0.0, 0.0, 0.0); /* black */
144     cairo_show_glyphs (cr, &brace, 1);
145
146     cairo_translate (cr, 2, 0);
147     score->width -= 2;
148
149     /* Vertical lines at each end */
150     cairo_rectangle (cr,
151                      0.5, 0.5,
152                      score->width - 1.0,
153                      score->staff_height * 3);
154     cairo_set_source_rgb (cr, 0.0, 0.0, 0.0); /* black */
155     cairo_set_line_width (cr, 1.0);
156     cairo_stroke (cr);
157
158     /* Top staff */
159     _draw_staff (score, cr, SCORE_CLEF_G);
160
161     /* Bottom staff */
162     cairo_translate (cr, 0, score->staff_height * 2);
163     _draw_staff (score, cr, SCORE_CLEF_F);
164
165     cairo_restore (cr);
166 }
167
168 void
169 score_draw (score_t *score, cairo_t *cr)
170 {
171     _draw_grand_staff (score, cr);
172 }