From 2d81bb7eb625c9ae00d38930e0e4e00a5ba73274 Mon Sep 17 00:00:00 2001 From: Robin Watts Date: Fri, 7 Oct 2016 17:57:07 +0100 Subject: HTML: Keep 'Anchors' in the html box list. Keep them as 'FLOW' entries rather than box entries. Although they technically enclose areas of text, we only ever use them as points. Thanks to Tor for fixing this code. --- include/mupdf/html.h | 3 ++- source/html/html-layout.c | 30 +++++++++++++++++++++++++++++- 2 files changed, 31 insertions(+), 2 deletions(-) diff --git a/include/mupdf/html.h b/include/mupdf/html.h index a400993f..edf84235 100644 --- a/include/mupdf/html.h +++ b/include/mupdf/html.h @@ -205,7 +205,8 @@ enum FLOW_BREAK = 2, FLOW_IMAGE = 3, FLOW_SBREAK = 4, - FLOW_SHYPHEN = 5 + FLOW_SHYPHEN = 5, + FLOW_ANCHOR = 6 }; struct fz_html_flow_s diff --git a/source/html/html-layout.c b/source/html/html-layout.c index 8f257048..3a5d16a9 100644 --- a/source/html/html-layout.c +++ b/source/html/html-layout.c @@ -198,6 +198,14 @@ static void add_flow_image(fz_context *ctx, fz_pool *pool, fz_html *top, fz_html flow->content.image = fz_keep_image(ctx, img); } +static void add_flow_anchor(fz_context *ctx, fz_pool *pool, fz_html *top, fz_html *inline_box, const char *anchor) +{ + fz_html_flow *flow = add_flow(ctx, pool, top, inline_box, FLOW_ANCHOR); + size_t len = strlen(anchor)+1; + flow->content.text = fz_pool_alloc(ctx, pool, len); + memcpy(flow->content.text, anchor, len); +} + static fz_html_flow *split_flow(fz_context *ctx, fz_pool *pool, fz_html_flow *flow, size_t offset) { fz_html_flow *new_flow; @@ -416,6 +424,14 @@ static fz_image *load_html_image(fz_context *ctx, fz_archive *zip, const char *b return img; } +static void generate_anchor(fz_context *ctx, fz_pool *pool, fz_html *box, const char *id, struct genstate *g) +{ + fz_html *flow = box; + while (flow->type != BOX_FLOW) + flow = flow->up; + add_flow_anchor(ctx, pool, flow, box, id); +} + static void generate_image(fz_context *ctx, fz_pool *pool, fz_html *box, fz_image *img, struct genstate *g) { fz_html *flow = box; @@ -658,7 +674,7 @@ static void generate_boxes(fz_context *ctx, fz_xml *node, fz_html *top, else if (display != DIS_NONE) { - const char *dir, *lang; + const char *dir, *lang, *id; int child_dir = markup_dir; int child_lang = markup_lang; @@ -701,6 +717,17 @@ static void generate_boxes(fz_context *ctx, fz_xml *node, fz_html *top, insert_box(ctx, box, BOX_BLOCK, top); } + if (!strcmp(tag, "a")) + { + id = fz_xml_att(node, "id"); + if (id) + { + /* We don't need to create a box here, because since tags are inline + * the DIS_INLINE case * above should already have done it for us. */ + generate_anchor(ctx, box, id, g); + } + } + if (fz_xml_down(node)) { int child_counter = list_counter; @@ -2032,6 +2059,7 @@ fz_print_html_flow(fz_context *ctx, fz_html_flow *flow, fz_html_flow *end) case FLOW_SHYPHEN: printf("[-]"); break; case FLOW_BREAK: printf("[!]"); break; case FLOW_IMAGE: printf(""); break; + case FLOW_ANCHOR: printf("", flow->content.text); break; } flow = flow->next; } -- cgit v1.2.3