X-Git-Url: https://git.cworth.org/git?a=blobdiff_plain;ds=sidebyside;f=score.c;h=e319af0c238abaf594afee0088bc8c580de6f59e;hb=05713bff8bc084c401d58b1b9526d83d9a42b641;hp=db07d5e9d57f33436688db8741d2a4442d519760;hpb=1fe478f5aaa4f8ebff2a3f7771809c3d1aaa5ef4;p=scherzo diff --git a/score.c b/score.c index db07d5e..e319af0 100644 --- a/score.c +++ b/score.c @@ -26,6 +26,9 @@ struct score_staff { score_clef_t clef; + score_chord_t **chords; + int num_chords; + score_note_t **notes; int num_notes; @@ -178,6 +181,38 @@ _score_note_to_line (score_staff_t *staff, score_note_t *note) return c_line - (name - SCORE_PITCH_NAME_C) / 2.0 - 3.5 * (note->octave - 4); } +/* chord->width is updated as a side effect */ +static void +_draw_chord (score_t *score, cairo_t *cr, + score_staff_t *staff, score_chord_t *chord) +{ + cairo_text_extents_t chord_extents; + double total_staff_height; + + /* XXX: The staff should manage this height itself. */ + total_staff_height = (staff->upper_ledger_lines * score->space_height + + score->staff_height + + staff->lower_ledger_lines * score->space_height); + + cairo_save (cr); + + cairo_text_extents (cr, chord->name, &chord_extents); + + cairo_select_font_face (cr, "serif", 0, 0); + cairo_set_font_size (cr, score->space_height * 3); + + if (staff->clef == SCORE_CLEF_G) + cairo_move_to (cr, 0, - score->space_height * 0.5); + else + cairo_move_to (cr, 0, score->space_height * 0.5 + total_staff_height + chord_extents.y_bearing); + + cairo_show_text (cr, chord->name); + + chord->width = chord_extents.x_advance; + + cairo_restore (cr); +} + static void _draw_note (score_t *score, cairo_t *cr, score_staff_t *staff, score_note_t *note) @@ -300,6 +335,16 @@ _draw_staff (score_t *score, cairo_t *cr, /* Make space for clef before drawing notes */ cairo_translate (cr, (int) (4 * score->space_height), 0); + /* Draw chord symbols */ + cairo_save (cr); + { + for (i = 0; i < staff->num_chords; i++) { + _draw_chord (score, cr, staff, staff->chords[i]); + cairo_translate (cr, staff->chords[i]->width, 0.0); + } + } + cairo_restore (cr); + /* Draw notes */ for (i = 0; i < staff->num_notes; i++) { _draw_note (score, cr, staff, staff->notes[i]); @@ -409,6 +454,9 @@ score_add_staff (score_t *score, score_clef_t clef) staff->notes = NULL; staff->num_notes = 0; + staff->chords = NULL; + staff->num_chords = 0; + staff->upper_ledger_lines = 0; staff->lower_ledger_lines = 0; @@ -427,6 +475,61 @@ score_add_staff (score_t *score, score_clef_t clef) return staff; } +score_chord_t * +score_add_chord (score_staff_t *staff, + const char *name) +{ + score_chord_t *chord; + + chord = talloc (staff, score_chord_t); + if (chord == NULL) + return NULL; + + chord->staff = staff; + chord->name = talloc_strdup (chord, name); + + /* The width will get set correctly the first time _draw_chord is + * called. */ + chord->width = 0.0; + + staff->num_chords++; + staff->chords = talloc_realloc (staff, + staff->chords, + score_chord_t*, + staff->num_chords); + if (staff->chords == NULL) { + staff->num_chords = 0; + return NULL; + } + + staff->chords[staff->num_chords - 1] = chord; + + return chord; +} + +void +score_remove_chord (score_chord_t *chord) +{ + score_staff_t *staff = chord->staff; + int i; + + for (i = 0; i < staff->num_chords; i++) + if (staff->chords[i] == chord) + break; + + if (i == staff->num_chords) + return; + + if (i < staff->num_chords - 1) + { + memmove (staff->chords + i, + staff->chords + i + 1, + (staff->num_chords - 1 - i) * sizeof (score_chord_t *)); + } + + staff->num_chords -= 1; +} + score_note_t * score_add_note (score_staff_t *staff, score_pitch_t pitch,