From 791dd8c29c612965a0b5b5d8b923b8bbb12cbc10 Mon Sep 17 00:00:00 2001 From: Tor Andersson Date: Tue, 5 Apr 2016 20:59:10 +0200 Subject: epub: Try to keep to one fz_text per paragraph. We will need to split if the color changes, or an image is spliced in. List item bullets also get their own fz_text element. --- source/html/html-layout.c | 39 ++++++++++++++++++++++++++++++++++----- 1 file changed, 34 insertions(+), 5 deletions(-) (limited to 'source/html') diff --git a/source/html/html-layout.c b/source/html/html-layout.c index 091ce052..435d1767 100644 --- a/source/html/html-layout.c +++ b/source/html/html-layout.c @@ -1270,9 +1270,15 @@ static void draw_flow_box(fz_context *ctx, fz_html *box, float page_top, float p fz_text *text; fz_matrix trm; float color[3]; + float prev_color[3]; /* FIXME: HB_DIRECTION_TTB? */ + text = NULL; + prev_color[0] = 0; + prev_color[1] = 0; + prev_color[2] = 0; + for (node = box->flow_head; node; node = node->next) { if (node->type == FLOW_IMAGE) @@ -1305,8 +1311,21 @@ static void draw_flow_box(fz_context *ctx, fz_html *box, float page_top, float p color[1] = node->style->color.g / 255.0f; color[2] = node->style->color.b / 255.0f; - /* TODO: reuse text object if color is unchanged */ - text = fz_new_text(ctx); + if (color[0] != prev_color[0] || color[1] != prev_color[1] || color[2] != prev_color[2]) + { + if (text) + { + fz_fill_text(ctx, dev, text, ctm, fz_device_rgb(ctx), prev_color, 1); + fz_drop_text(ctx, text); + text = NULL; + } + prev_color[0] = color[0]; + prev_color[1] = color[1]; + prev_color[2] = color[2]; + } + + if (!text) + text = fz_new_text(ctx); if (node->bidi_level & 1) x = node->x + node->w; @@ -1378,12 +1397,15 @@ static void draw_flow_box(fz_context *ctx, fz_html *box, float page_top, float p y += y_advance * node_scale; } - - fz_fill_text(ctx, dev, text, ctm, fz_device_rgb(ctx), color, 1); - fz_drop_text(ctx, text); } else if (node->type == FLOW_IMAGE) { + if (text) + { + fz_fill_text(ctx, dev, text, ctm, fz_device_rgb(ctx), color, 1); + fz_drop_text(ctx, text); + text = NULL; + } if (node->style->visibility == V_VISIBLE) { fz_matrix local_ctm = *ctm; @@ -1393,6 +1415,13 @@ static void draw_flow_box(fz_context *ctx, fz_html *box, float page_top, float p } } } + + if (text) + { + fz_fill_text(ctx, dev, text, ctm, fz_device_rgb(ctx), color, 1); + fz_drop_text(ctx, text); + text = NULL; + } } static void draw_rect(fz_context *ctx, fz_device *dev, const fz_matrix *ctm, fz_css_color color, float x0, float y0, float x1, float y1) -- cgit v1.2.3