]> git.cworth.org Git - scherzo/blobdiff - score.c
Draw ledger lines.
[scherzo] / score.c
diff --git a/score.c b/score.c
index 7a1c231a4b82cfdcef0d972dcf88ac6c1168e645..0228b6e2ab3aea5d94f8d2ef5b8e430c63abc98a 100644 (file)
--- a/score.c
+++ b/score.c
@@ -18,6 +18,8 @@
  * along with this program.  If not, see http://www.gnu.org/licenses/ .
  */
 
+#include <string.h>
+
 #include "score.h"
 
 struct score_staff
@@ -56,13 +58,6 @@ struct score
     int num_staves;
 };
 
-typedef struct score_note
-{
-    score_pitch_t pitch;
-    int octave;
-    score_duration_t duration;
-} score_note_t;
-
 score_t *
 score_create (void *ctx)
 {
@@ -175,6 +170,15 @@ _draw_note (score_t *score, cairo_t *cr,
 {
     double line;
     cairo_glyph_t note_glyph;
+    static double extend_factor = 0.25;
+
+    void _draw_ledger_line (double line, double width) {
+       cairo_move_to (cr,
+                      - width * extend_factor / 2.0,
+                      score->space_height * line + score->line_width / 2.0);
+       cairo_rel_line_to (cr, (1 + extend_factor) * width, 0);
+       cairo_stroke (cr);
+    }
 
     cairo_save (cr);
 
@@ -208,7 +212,22 @@ _draw_note (score_t *score, cairo_t *cr,
     }
 
     note_glyph.x = 0;
-    note_glyph.y = score->space_height * line + score->line_width / 2.0;
+    note_glyph.y = score->space_height * line;
+
+    if (line < 0 || line > 4) {
+       int i;
+       cairo_text_extents_t note_extents;
+
+       cairo_glyph_extents (cr, &note_glyph, 1, &note_extents);
+
+       if (line < 0) {
+           for (i = -1; i >= line; i--)
+               _draw_ledger_line (i, note_extents.width);
+       } else {
+           for (i = 5; i <= line; i++)
+               _draw_ledger_line (i, note_extents.width);
+       }
+    }
 
     cairo_set_source_rgb (cr, 0.0, 0.0, 0.0); /* black */
     cairo_show_glyphs (cr, &note_glyph, 1);
@@ -222,7 +241,6 @@ _draw_staff (score_t *score, cairo_t *cr,
 {
     int i;
     cairo_glyph_t clef_glyph;
-    cairo_text_extents_t clef_extents;
 
     cairo_save (cr);
 
@@ -249,8 +267,6 @@ _draw_staff (score_t *score, cairo_t *cr,
     cairo_set_source_rgb (cr, 0.0, 0.0, 0.0); /* black */
     cairo_show_glyphs (cr, &clef_glyph, 1);
 
-    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);
@@ -263,12 +279,14 @@ _draw_staff (score_t *score, cairo_t *cr,
     cairo_stroke (cr);
 
     /* Make space for clef before drawing notes */
-    cairo_translate (cr, (int) (1.5 * clef_extents.width), 0);
+    cairo_translate (cr, (int) (4 * score->space_height), 0);
 
     /* Draw notes */
     for (i = 0; i < staff->num_notes; i++) {
        _draw_note (score, cr, staff, staff->notes[i]);
+       /* Draw all notes concurrent for now (as a chord)
        cairo_translate (cr, score->space_height * 2.0, 0);
+       */
     }
 
     cairo_restore (cr);
@@ -372,10 +390,10 @@ score_add_staff (score_t *score, score_clef_t clef)
 }
 
 score_note_t *
-score_staff_add_note (score_staff_t *staff,
-                     score_pitch_t pitch,
-                     int octave,
-                     score_duration_t duration)
+score_add_note (score_staff_t *staff,
+               score_pitch_t pitch,
+               int octave,
+               score_duration_t duration)
 {
     score_note_t *note;
 
@@ -383,6 +401,7 @@ score_staff_add_note (score_staff_t *staff,
     if (note == NULL)
        return NULL;
 
+    note->staff = staff;
     note->pitch = pitch;
     note->octave = octave;
     note->duration = duration;
@@ -402,3 +421,48 @@ score_staff_add_note (score_staff_t *staff,
     return note;
 }
 
+void
+score_remove_note (score_note_t *note)
+{
+    score_staff_t *staff = note->staff;
+    int i;
+
+    for (i = 0; i < staff->num_notes; i++)
+       if (staff->notes[i] == note)
+           break;
+
+    if (i == staff->num_notes)
+       return;
+
+    if (i < staff->num_notes - 1)
+    {
+       memmove (staff->notes + i,
+                staff->notes + i + 1, 
+                (staff->num_notes - 1 - i) * sizeof (score_note_t *));
+    }
+
+    staff->num_notes -= 1;
+}
+
+score_note_t *
+score_staff_find_note (score_staff_t *staff,
+                      score_pitch_t pitch,
+                      int octave,
+                      score_duration_t duration)
+{
+    int i;
+    score_note_t *note;
+
+    for (i = 0; i < staff->num_notes; i++) {
+       note = staff->notes[i];
+       if (note->pitch == pitch &&
+           note->octave == octave &&
+           note->duration == duration)
+       {
+           return note;
+       }
+    }
+
+    return NULL;
+}
+