]> git.cworth.org Git - scherzo/blobdiff - score.c
Start drawing some very rudimentary notes.
[scherzo] / score.c
diff --git a/score.c b/score.c
index fb54b95ea4c6c72575ee30f971dde29252324c12..7a1c231a4b82cfdcef0d972dcf88ac6c1168e645 100644 (file)
--- a/score.c
+++ b/score.c
@@ -146,12 +146,83 @@ _draw_brace (score_t *score, cairo_t *cr,
     *brace_width = (int) -brace_extents.x_bearing;
 }
 
+/* Line containing middle C for the given clef. */
+static int
+_score_clef_c_line (score_clef_t clef)
+{
+    switch (clef)
+    {
+    default:
+    case SCORE_CLEF_G:
+       return 5;
+    case SCORE_CLEF_F:
+       return -1;
+    }
+}
+
+static double
+_score_note_to_line (score_staff_t *staff, score_note_t *note)
+{
+    score_pitch_name_t name = SCORE_PITCH_NAME (note->pitch);
+    int c_line = _score_clef_c_line (staff->clef);
+
+    return c_line - (name - SCORE_PITCH_NAME_C) / 2.0 - 3.5 * (note->octave - 4);
+}
+
+static void
+_draw_note (score_t *score, cairo_t *cr,
+           score_staff_t *staff, score_note_t *note)
+{
+    double line;
+    cairo_glyph_t note_glyph;
+
+    cairo_save (cr);
+
+    /* Which line should the note appear on? Line 0 is the top line of
+     * the staff and increasing downwards. (Negative values indicate a
+     * note on a ledger line above the staff). Values half way between
+     * integers indicate notes appearing on a space between two staff
+     * lines (or ledger lines). */
+    line = _score_note_to_line (staff, note);
+
+    cairo_select_font_face (cr, "Gonville-26", 0, 0);
+    cairo_set_font_size (cr, score->staff_height);
+
+    /* XXX: The hard-coded glyph indices here are very ugly. We should
+     * figure out how to lookup glyphs by name from this font. */
+    switch (note->duration) {
+    case SCORE_DURATION_1:
+       note_glyph.index = 127;
+       break;
+    case SCORE_DURATION_2:
+       note_glyph.index = 85;
+       break;
+    case SCORE_DURATION_4:
+    case SCORE_DURATION_8:
+    case SCORE_DURATION_16:
+    case SCORE_DURATION_32:
+    case SCORE_DURATION_64:
+    case SCORE_DURATION_128:
+    default:
+       note_glyph.index = 84;
+    }
+
+    note_glyph.x = 0;
+    note_glyph.y = score->space_height * line + score->line_width / 2.0;
+
+    cairo_set_source_rgb (cr, 0.0, 0.0, 0.0); /* black */
+    cairo_show_glyphs (cr, &note_glyph, 1);
+
+    cairo_restore (cr);
+}
+
 static void
 _draw_staff (score_t *score, cairo_t *cr,
             score_staff_t *staff, int staff_width)
 {
     int i;
-    cairo_glyph_t glyph;
+    cairo_glyph_t clef_glyph;
+    cairo_text_extents_t clef_extents;
 
     cairo_save (cr);
 
@@ -164,27 +235,24 @@ _draw_staff (score_t *score, cairo_t *cr,
     switch (staff->clef) {
     case SCORE_CLEF_G:
     default:
-       glyph.index = 46;
-       glyph.y = 3 * score->space_height;
+       clef_glyph.index = 46;
+       clef_glyph.y = 3 * score->space_height;
        break;
     case SCORE_CLEF_F:
-       glyph.index = 45;
-       glyph.y = 1 * score->space_height;
+       clef_glyph.index = 45;
+       clef_glyph.y = 1 * score->space_height;
        break;
     }
-    glyph.x = 3 * score->line_width;
-    glyph.y += 1;
+    clef_glyph.x = 3 * score->line_width;
+    clef_glyph.y += 1;
 
     cairo_set_source_rgb (cr, 0.0, 0.0, 0.0); /* black */
-    cairo_show_glyphs (cr, &glyph, 1);
+    cairo_show_glyphs (cr, &clef_glyph, 1);
 
-    cairo_rectangle (cr,
-                    score->line_width / 2.0,
-                    score->line_width / 2.0,
-                    staff_width - score->line_width,
-                    score->space_height * 4);
-    
-    for (i = 1; i < 4; i++) {
+    cairo_glyph_extents (cr, &clef_glyph, 1, &clef_extents);
+
+    /* Draw staff lines */
+    for (i = 0; i < 5; i++) {
        cairo_move_to (cr, 0, i * score->space_height + score->line_width / 2.0);
        cairo_rel_line_to (cr, staff_width, 0);
     }
@@ -194,6 +262,15 @@ _draw_staff (score_t *score, cairo_t *cr,
     cairo_set_source_rgb (cr, 0.0, 0.0, 0.0); /* black */
     cairo_stroke (cr);
 
+    /* Make space for clef before drawing notes */
+    cairo_translate (cr, (int) (1.5 * clef_extents.width), 0);
+
+    /* Draw notes */
+    for (i = 0; i < staff->num_notes; i++) {
+       _draw_note (score, cr, staff, staff->notes[i]);
+       cairo_translate (cr, score->space_height * 2.0, 0);
+    }
+
     cairo_restore (cr);
 }