From c100c4c77a88782ba5c4634994171db611952d44 Mon Sep 17 00:00:00 2001 From: Tor Andersson Date: Thu, 2 Feb 2017 15:55:40 +0100 Subject: Add bookmarks so we can find a location after reflowing a document. --- source/fitz/document.c | 14 ++++++++ source/html/epub-doc.c | 35 +++++++++++++++++++ source/html/html-doc.c | 18 +++++++++- source/html/html-layout.c | 85 +++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 151 insertions(+), 1 deletion(-) (limited to 'source') diff --git a/source/fitz/document.c b/source/fitz/document.c index 684c8523..41abb736 100644 --- a/source/fitz/document.c +++ b/source/fitz/document.c @@ -182,6 +182,20 @@ fz_is_document_reflowable(fz_context *ctx, fz_document *doc) return doc ? doc->is_reflowable : 0; } +fz_bookmark fz_make_bookmark(fz_context *ctx, fz_document *doc, int page) +{ + if (doc && doc->make_bookmark) + return doc->make_bookmark(ctx, doc, page); + return (fz_bookmark)page; +} + +int fz_lookup_bookmark(fz_context *ctx, fz_document *doc, fz_bookmark mark) +{ + if (doc && doc->lookup_bookmark) + return doc->lookup_bookmark(ctx, doc, mark); + return (int)mark; +} + int fz_needs_password(fz_context *ctx, fz_document *doc) { diff --git a/source/html/epub-doc.c b/source/html/epub-doc.c index d220816d..3cfa3f06 100644 --- a/source/html/epub-doc.c +++ b/source/html/epub-doc.c @@ -178,6 +178,39 @@ epub_load_links(fz_context *ctx, fz_page *page_) return NULL; } +static fz_bookmark +epub_make_bookmark(fz_context *ctx, fz_document *doc_, int n) +{ + epub_document *doc = (epub_document*)doc_; + epub_chapter *ch; + int count = 0; + + for (ch = doc->spine; ch; ch = ch->next) + { + int cn = ceilf(ch->html->root->h / ch->html->page_h); + if (n < count + cn) + return fz_make_html_bookmark(ctx, ch->html, n - count); + count += cn; + } + + return 0; +} + +static int +epub_lookup_bookmark(fz_context *ctx, fz_document *doc_, fz_bookmark mark) +{ + epub_document *doc = (epub_document*)doc_; + epub_chapter *ch; + + for (ch = doc->spine; ch; ch = ch->next) + { + int p = fz_lookup_html_bookmark(ctx, ch->html, mark); + if (p != -1) + return ch->start + p; + } + return -1; +} + static fz_page * epub_load_page(fz_context *ctx, fz_document *doc_, int number) { @@ -440,6 +473,8 @@ epub_init(fz_context *ctx, fz_archive *zip) doc->super.layout = epub_layout; doc->super.load_outline = epub_load_outline; doc->super.resolve_link = epub_resolve_link; + doc->super.make_bookmark = epub_make_bookmark; + doc->super.lookup_bookmark = epub_lookup_bookmark; doc->super.count_pages = epub_count_pages; doc->super.load_page = epub_load_page; doc->super.lookup_metadata = epub_lookup_metadata; diff --git a/source/html/html-doc.c b/source/html/html-doc.c index c19d5049..47225c06 100644 --- a/source/html/html-doc.c +++ b/source/html/html-doc.c @@ -97,6 +97,20 @@ htdoc_load_links(fz_context *ctx, fz_page *page_) return fz_load_html_links(ctx, doc->html, page->number, ""); } +static fz_bookmark +htdoc_make_bookmark(fz_context *ctx, fz_document *doc_, int page) +{ + html_document *doc = (html_document*)doc_; + return fz_make_html_bookmark(ctx, doc->html, page); +} + +static int +htdoc_lookup_bookmark(fz_context *ctx, fz_document *doc_, fz_bookmark mark) +{ + html_document *doc = (html_document*)doc_; + return fz_lookup_html_bookmark(ctx, doc->html, mark); +} + static fz_page * htdoc_load_page(fz_context *ctx, fz_document *doc_, int number) { @@ -111,7 +125,7 @@ htdoc_load_page(fz_context *ctx, fz_document *doc_, int number) return (fz_page*)page; } -int +static int htdoc_lookup_metadata(fz_context *ctx, fz_document *doc_, const char *key, char *buf, int size) { if (!strcmp(key, "format")) @@ -162,6 +176,8 @@ htdoc_open_document(fz_context *ctx, const char *filename) doc->super.drop_document = htdoc_drop_document; doc->super.layout = htdoc_layout; doc->super.resolve_link = htdoc_resolve_link; + doc->super.make_bookmark = htdoc_make_bookmark; + doc->super.lookup_bookmark = htdoc_lookup_bookmark; doc->super.count_pages = htdoc_count_pages; doc->super.load_page = htdoc_load_page; doc->super.lookup_metadata = htdoc_lookup_metadata; diff --git a/source/html/html-layout.c b/source/html/html-layout.c index f06e6cbd..1cac935b 100644 --- a/source/html/html-layout.c +++ b/source/html/html-layout.c @@ -2101,6 +2101,91 @@ fz_find_html_target(fz_context *ctx, fz_html *html, const char *id) return find_box_target(html->root, id); } +static fz_html_flow * +make_flow_bookmark(fz_context *ctx, fz_html_flow *flow, float y) +{ + while (flow) + { + if (flow->y >= y) + return flow; + flow = flow->next; + } + return NULL; +} + +static fz_html_flow * +make_box_bookmark(fz_context *ctx, fz_html_box *box, float y) +{ + fz_html_flow *mark; + while (box) + { + if (box->type == BOX_FLOW) + { + if (box->y >= y) + { + mark = make_flow_bookmark(ctx, box->flow_head, y); + if (mark) + return mark; + } + } + else + { + mark = make_box_bookmark(ctx, box->down, y); + if (mark) + return mark; + } + box = box->next; + } + return NULL; +} + +fz_bookmark +fz_make_html_bookmark(fz_context *ctx, fz_html *html, int page) +{ + return (fz_bookmark)make_box_bookmark(ctx, html->root, page * html->page_h); +} + +static int +lookup_flow_bookmark(fz_context *ctx, fz_html_flow *flow, fz_html_flow *mark) +{ + while (flow) + { + if (flow == mark) + return 1; + flow = flow->next; + } + return 0; +} + +static int +lookup_box_bookmark(fz_context *ctx, fz_html_box *box, fz_html_flow *mark) +{ + while (box) + { + if (box->type == BOX_FLOW) + { + if (lookup_flow_bookmark(ctx, box->flow_head, mark)) + return 1; + } + else + { + if (lookup_box_bookmark(ctx, box->down, mark)) + return 1; + } + box = box->next; + } + return 0; +} + +int +fz_lookup_html_bookmark(fz_context *ctx, fz_html *html, fz_bookmark mark) +{ + fz_html_flow *flow = (fz_html_flow*)mark; + if (flow && lookup_box_bookmark(ctx, html->root, flow)) + return (int)(flow->y / html->page_h); + return -1; +} + static char *concat_text(fz_context *ctx, fz_xml *root) { fz_xml *node; -- cgit v1.2.3