From bbaa7f5a81cfaf26f3d5dd51edb38cf56284cb41 Mon Sep 17 00:00:00 2001 From: Aki Date: Sun, 19 Sep 2021 20:10:28 +0200 Subject: Offsets are now calculated during arranging --- text.c | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/text.c b/text.c index 6e3da86..3f65048 100644 --- a/text.c +++ b/text.c @@ -72,6 +72,7 @@ struct Layout { PangoFontDescription * desc; int size; + int * offsets; PangoLayout ** v; }; @@ -211,6 +212,7 @@ void finalize(struct State * state, struct Layout * layout) for (int i = 0; i < layout->size; ++i) g_object_unref(layout->v[i]); free(layout->v); + free(layout->offsets); cairo_destroy(state->ctx); cairo_surface_destroy(state->surface); xcb_disconnect(state->c); @@ -332,8 +334,9 @@ void arrange(struct State * state, struct Layout * layout, union Command body[]) { for (int i = 0; i < layout->size; ++i) g_object_unref(layout->v[i]); - layout->v = malloc(sizeof(PangoLayout *) * size); - if (NULL == layout->v) + layout->v = malloc(sizeof(PangoLayout *) * size); // TODO: Free when changing data + layout->offsets = malloc(sizeof(int) * size); + if (NULL == layout->v || NULL == layout->offsets) abort(); // TODO: Be more graceful? for (int i = 0; i < size; ++i) layout->v[i] = pango_cairo_create_layout(state->ctx); @@ -341,6 +344,7 @@ void arrange(struct State * state, struct Layout * layout, union Command body[]) } PangoAttrList * attrs = pango_attr_list_new(); int i = 0; + int offset = MARGIN; for (union Command * c = body; COMMAND_DONE != c->type; ++c) { switch (c->type) @@ -368,6 +372,10 @@ void arrange(struct State * state, struct Layout * layout, union Command body[]) pango_layout_set_wrap(layout->v[i], PANGO_WRAP_WORD); pango_layout_set_width(layout->v[i], (state->width - 2 * MARGIN) * PANGO_SCALE); pango_layout_set_text(layout->v[i], c->block.data, c->block.length); + int height; + pango_layout_get_pixel_size(layout->v[i], NULL, &height); + layout->offsets[i] = offset; + offset += height + MARGIN; i++; break; case COMMAND_NOTHING: @@ -384,15 +392,11 @@ void draw(struct State * state, struct Layout * layout) cairo_set_source_rgb(state->ctx, 1., 1., 1.); cairo_paint(state->ctx); cairo_set_source_rgb(state->ctx, 0., 0., 0.); - int y = MARGIN; for (int i = 0; i < layout->size; ++i) { - cairo_move_to(state->ctx, MARGIN, y); + cairo_move_to(state->ctx, MARGIN, layout->offsets[i]); pango_cairo_update_layout(state->ctx, layout->v[i]); pango_cairo_show_layout(state->ctx, layout->v[i]); - int height; - pango_layout_get_pixel_size(layout->v[i], NULL, &height); - y += height + MARGIN; } cairo_surface_flush(state->surface); xcb_flush(state->c); -- cgit v1.1