From a27f2e924f7c8d55fa62b89833c61621a9ca1b87 Mon Sep 17 00:00:00 2001 From: Tor Andersson Date: Wed, 26 Nov 2014 02:25:18 +0100 Subject: html: Rename files. --- source/html/html-doc.c | 162 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 162 insertions(+) create mode 100644 source/html/html-doc.c (limited to 'source/html/html-doc.c') diff --git a/source/html/html-doc.c b/source/html/html-doc.c new file mode 100644 index 00000000..11d21d56 --- /dev/null +++ b/source/html/html-doc.c @@ -0,0 +1,162 @@ +#include "mupdf/html.h" + +#define DEFW (450) +#define DEFH (600) +#define DEFEM (12) + +typedef struct html_document_s html_document; + +struct html_document_s +{ + fz_document super; + fz_context *ctx; + fz_archive *zip; + fz_html_font_set *set; + float page_w, page_h, em; + struct box *box; +}; + +static void +htdoc_close_document(html_document *doc) +{ + fz_close_archive(doc->ctx, doc->zip); + fz_free_html_font_set(doc->ctx, doc->set); + fz_free(doc->ctx, doc); +} + +static int +htdoc_count_pages(html_document *doc) +{ + int count; + + // TODO: reflow + + count = ceilf(doc->box->h / doc->page_h); +printf("count pages! %g / %g = %d\n", doc->box->h, doc->page_h, count); + return count; +} + +static void * +htdoc_load_page(html_document *doc, int number) +{ +printf("load page %d\n", number); + // TODO: reflow + return (void*)((intptr_t)number + 1); +} + +static void +htdoc_free_page(html_document *doc, void *page) +{ +} + +static void +htdoc_layout(html_document *doc, float w, float h, float em) +{ + doc->page_w = w; + doc->page_h = h; + doc->em = em; + fz_layout_html(doc->ctx, doc->box, w, h, em); +} + +static fz_rect * +htdoc_bound_page(html_document *doc, void *page, fz_rect *bbox) +{ + // TODO: reflow + printf("html: bound page\n"); + bbox->x0 = bbox->y0 = 0; + bbox->x1 = doc->page_w; + bbox->y1 = doc->page_h; + return bbox; +} + +static void +htdoc_run_page(html_document *doc, void *page, fz_device *dev, const fz_matrix *ctm, fz_cookie *cookie) +{ + int n = ((intptr_t)page) - 1; + printf("html: run page %d\n", n); + fz_draw_html(doc->ctx, doc->box, n * doc->page_h, (n+1) * doc->page_h, dev, ctm); +} + +static html_document * +htdoc_open_document_with_stream(fz_context *ctx, fz_stream *file) +{ + html_document *doc; + fz_buffer *buf; + + doc = fz_malloc_struct(ctx, html_document); + doc->ctx = ctx; + doc->zip = fz_open_directory(ctx, "."); + doc->set = fz_new_html_font_set(ctx); + + doc->super.close = (void*)htdoc_close_document; + doc->super.layout = (void*)htdoc_layout; + doc->super.count_pages = (void*)htdoc_count_pages; + doc->super.load_page = (void*)htdoc_load_page; + doc->super.bound_page = (void*)htdoc_bound_page; + doc->super.run_page_contents = (void*)htdoc_run_page; + doc->super.free_page = (void*)htdoc_free_page; + + buf = fz_read_all(file, 0); + fz_write_buffer_byte(ctx, buf, 0); + doc->box = fz_generate_html(ctx, doc->set, doc->zip, ".", buf); + fz_drop_buffer(ctx, buf); + + htdoc_layout(doc, DEFW, DEFH, DEFEM); + + return doc; +} + +static html_document * +htdoc_open_document(fz_context *ctx, const char *filename) +{ + char dirname[2048]; + fz_buffer *buf; + html_document *doc; + + fz_dirname(dirname, filename, sizeof dirname); + + doc = fz_malloc_struct(ctx, html_document); + doc->ctx = ctx; + doc->zip = fz_open_directory(ctx, dirname); + doc->set = fz_new_html_font_set(ctx); + + doc->super.close = (void*)htdoc_close_document; + doc->super.layout = (void*)htdoc_layout; + doc->super.count_pages = (void*)htdoc_count_pages; + doc->super.load_page = (void*)htdoc_load_page; + doc->super.bound_page = (void*)htdoc_bound_page; + doc->super.run_page_contents = (void*)htdoc_run_page; + doc->super.free_page = (void*)htdoc_free_page; + + buf = fz_read_file(ctx, filename); + fz_write_buffer_byte(ctx, buf, 0); + doc->box = fz_generate_html(ctx, doc->set, doc->zip, ".", buf); + fz_drop_buffer(ctx, buf); + + htdoc_layout(doc, DEFW, DEFH, DEFEM); + + return doc; +} + +static int +htdoc_recognize(fz_context *doc, const char *magic) +{ + char *ext = strrchr(magic, '.'); + + if (ext) + { + if (!fz_strcasecmp(ext, ".xml") || !fz_strcasecmp(ext, ".xhtml") || !fz_strcasecmp(ext, ".html")) + return 100; + } + if (!strcmp(magic, "application/html+xml") || !strcmp(magic, "application/xml") || !strcmp(magic, "text/xml")) + return 100; + + return 0; +} + +fz_document_handler html_document_handler = +{ + (fz_document_recognize_fn *)&htdoc_recognize, + (fz_document_open_fn *)&htdoc_open_document, + (fz_document_open_with_stream_fn *)&htdoc_open_document_with_stream +}; -- cgit v1.2.3 From ce6e762510c9232d2594bd8d302c6d63f56dcff6 Mon Sep 17 00:00:00 2001 From: Tor Andersson Date: Wed, 26 Nov 2014 13:36:38 +0100 Subject: html: Rename style, box and flow structs. --- source/html/html-doc.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'source/html/html-doc.c') diff --git a/source/html/html-doc.c b/source/html/html-doc.c index 11d21d56..531e61c4 100644 --- a/source/html/html-doc.c +++ b/source/html/html-doc.c @@ -13,7 +13,7 @@ struct html_document_s fz_archive *zip; fz_html_font_set *set; float page_w, page_h, em; - struct box *box; + fz_html_box *box; }; static void -- cgit v1.2.3 From c4a45e25b92269e9e44594fd410b272718d939bc Mon Sep 17 00:00:00 2001 From: Tor Andersson Date: Wed, 26 Nov 2014 16:27:20 +0100 Subject: html: Free css structures. --- source/html/html-doc.c | 1 + 1 file changed, 1 insertion(+) (limited to 'source/html/html-doc.c') diff --git a/source/html/html-doc.c b/source/html/html-doc.c index 531e61c4..688a9f88 100644 --- a/source/html/html-doc.c +++ b/source/html/html-doc.c @@ -20,6 +20,7 @@ static void htdoc_close_document(html_document *doc) { fz_close_archive(doc->ctx, doc->zip); + fz_free_html(doc->ctx, doc->box); fz_free_html_font_set(doc->ctx, doc->set); fz_free(doc->ctx, doc); } -- cgit v1.2.3 From 46c3e7d3731c4e869e8b6b7b1e7f1a1b2d2491cd Mon Sep 17 00:00:00 2001 From: Tor Andersson Date: Fri, 28 Nov 2014 11:52:08 +0100 Subject: html: Clean up some naming. --- source/html/html-doc.c | 18 ++++-------------- 1 file changed, 4 insertions(+), 14 deletions(-) (limited to 'source/html/html-doc.c') diff --git a/source/html/html-doc.c b/source/html/html-doc.c index 688a9f88..ea5f7f3a 100644 --- a/source/html/html-doc.c +++ b/source/html/html-doc.c @@ -13,7 +13,7 @@ struct html_document_s fz_archive *zip; fz_html_font_set *set; float page_w, page_h, em; - fz_html_box *box; + fz_html *box; }; static void @@ -28,20 +28,13 @@ htdoc_close_document(html_document *doc) static int htdoc_count_pages(html_document *doc) { - int count; - - // TODO: reflow - - count = ceilf(doc->box->h / doc->page_h); -printf("count pages! %g / %g = %d\n", doc->box->h, doc->page_h, count); + int count = ceilf(doc->box->h / doc->page_h); return count; } static void * htdoc_load_page(html_document *doc, int number) { -printf("load page %d\n", number); - // TODO: reflow return (void*)((intptr_t)number + 1); } @@ -62,8 +55,6 @@ htdoc_layout(html_document *doc, float w, float h, float em) static fz_rect * htdoc_bound_page(html_document *doc, void *page, fz_rect *bbox) { - // TODO: reflow - printf("html: bound page\n"); bbox->x0 = bbox->y0 = 0; bbox->x1 = doc->page_w; bbox->y1 = doc->page_h; @@ -74,7 +65,6 @@ static void htdoc_run_page(html_document *doc, void *page, fz_device *dev, const fz_matrix *ctm, fz_cookie *cookie) { int n = ((intptr_t)page) - 1; - printf("html: run page %d\n", n); fz_draw_html(doc->ctx, doc->box, n * doc->page_h, (n+1) * doc->page_h, dev, ctm); } @@ -99,7 +89,7 @@ htdoc_open_document_with_stream(fz_context *ctx, fz_stream *file) buf = fz_read_all(file, 0); fz_write_buffer_byte(ctx, buf, 0); - doc->box = fz_generate_html(ctx, doc->set, doc->zip, ".", buf); + doc->box = fz_parse_html(ctx, doc->set, doc->zip, ".", buf, NULL); fz_drop_buffer(ctx, buf); htdoc_layout(doc, DEFW, DEFH, DEFEM); @@ -131,7 +121,7 @@ htdoc_open_document(fz_context *ctx, const char *filename) buf = fz_read_file(ctx, filename); fz_write_buffer_byte(ctx, buf, 0); - doc->box = fz_generate_html(ctx, doc->set, doc->zip, ".", buf); + doc->box = fz_parse_html(ctx, doc->set, doc->zip, ".", buf, NULL); fz_drop_buffer(ctx, buf); htdoc_layout(doc, DEFW, DEFH, DEFEM); -- cgit v1.2.3