diff options
author | Aki <please@ignore.pl> | 2021-09-19 20:10:28 +0200 |
---|---|---|
committer | Aki <please@ignore.pl> | 2021-09-19 20:10:28 +0200 |
commit | bbaa7f5a81cfaf26f3d5dd51edb38cf56284cb41 (patch) | |
tree | cbdb1dd95e06e255a9174d21a7857e57ed4f06e1 | |
parent | f9a9b8550ef8c7ab6c80569525f3218d864b2f3b (diff) | |
download | text-master.zip text-master.tar.gz text-master.tar.bz2 |
-rw-r--r-- | text.c | 18 |
1 files changed, 11 insertions, 7 deletions
@@ -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); |