diff options
Diffstat (limited to 'source/html')
-rw-r--r-- | source/html/css-parse.c | 26 | ||||
-rw-r--r-- | source/html/epub-doc.c | 90 | ||||
-rw-r--r-- | source/html/html-doc.c | 97 | ||||
-rw-r--r-- | source/html/html-font.c | 4 | ||||
-rw-r--r-- | source/html/html-layout.c | 22 |
5 files changed, 132 insertions, 107 deletions
diff --git a/source/html/css-parse.c b/source/html/css-parse.c index abe20e74..e3ddd48f 100644 --- a/source/html/css-parse.c +++ b/source/html/css-parse.c @@ -70,19 +70,19 @@ static fz_css_value *fz_new_css_value(fz_context *ctx, int type, const char *dat return val; } -static void fz_free_css_value(fz_context *ctx, fz_css_value *val) +static void fz_drop_css_value(fz_context *ctx, fz_css_value *val) { while (val) { fz_css_value *next = val->next; - fz_free_css_value(ctx, val->args); + fz_drop_css_value(ctx, val->args); fz_free(ctx, val->data); fz_free(ctx, val); val = next; } } -static void fz_free_css_condition(fz_context *ctx, fz_css_condition *cond) +static void fz_drop_css_condition(fz_context *ctx, fz_css_condition *cond) { while (cond) { @@ -94,40 +94,40 @@ static void fz_free_css_condition(fz_context *ctx, fz_css_condition *cond) } } -static void fz_free_css_selector(fz_context *ctx, fz_css_selector *sel) +static void fz_drop_css_selector(fz_context *ctx, fz_css_selector *sel) { while (sel) { fz_css_selector *next = sel->next; fz_free(ctx, sel->name); - fz_free_css_condition(ctx, sel->cond); - fz_free_css_selector(ctx, sel->left); - fz_free_css_selector(ctx, sel->right); + fz_drop_css_condition(ctx, sel->cond); + fz_drop_css_selector(ctx, sel->left); + fz_drop_css_selector(ctx, sel->right); fz_free(ctx, sel); sel = next; } } -static void fz_free_css_property(fz_context *ctx, fz_css_property *prop) +static void fz_drop_css_property(fz_context *ctx, fz_css_property *prop) { while (prop) { fz_css_property *next = prop->next; fz_free(ctx, prop->name); - fz_free_css_value(ctx, prop->value); + fz_drop_css_value(ctx, prop->value); fz_free(ctx, prop); prop = next; } } -void fz_free_css(fz_context *ctx, fz_css_rule *rule) +void fz_drop_css(fz_context *ctx, fz_css_rule *rule) { while (rule) { fz_css_rule *next = rule->next; - fz_free_css_selector(ctx, rule->selector); - fz_free_css_property(ctx, rule->declaration); - fz_free_css_property(ctx, rule->garbage); + fz_drop_css_selector(ctx, rule->selector); + fz_drop_css_property(ctx, rule->declaration); + fz_drop_css_property(ctx, rule->garbage); fz_free(ctx, rule); rule = next; } diff --git a/source/html/epub-doc.c b/source/html/epub-doc.c index 3aaedd33..69963ff0 100644 --- a/source/html/epub-doc.c +++ b/source/html/epub-doc.c @@ -11,7 +11,6 @@ typedef struct epub_page_s epub_page; struct epub_document_s { fz_document super; - fz_context *ctx; fz_archive *zip; fz_html_font_set *set; float page_w, page_h, em; @@ -26,10 +25,17 @@ struct epub_chapter_s epub_chapter *next; }; +struct epub_page_s +{ + fz_page super; + epub_document *doc; + int number; +}; + static void -epub_layout(epub_document *doc, float w, float h, float em) +epub_layout(fz_context *ctx, fz_document *doc_, float w, float h, float em) { - fz_context *ctx = doc->ctx; + epub_document *doc = (epub_document*)doc_; epub_chapter *ch; doc->page_w = w; @@ -43,8 +49,9 @@ epub_layout(epub_document *doc, float w, float h, float em) } static int -epub_count_pages(epub_document *doc) +epub_count_pages(fz_context *ctx, fz_document *doc_) { + epub_document *doc = (epub_document*)doc_; epub_chapter *ch; int count = 0; for (ch = doc->spine; ch; ch = ch->next) @@ -52,20 +59,16 @@ epub_count_pages(epub_document *doc) return count; } -static epub_page * -epub_load_page(epub_document *doc, int number) -{ - return (void*)((intptr_t)number + 1); -} - static void -epub_free_page(epub_document *doc, epub_page *page) +epub_drop_page_imp(fz_context *ctx, fz_page *page_) { } static fz_rect * -epub_bound_page(epub_document *doc, epub_page *page, fz_rect *bbox) +epub_bound_page(fz_context *ctx, fz_page *page_, fz_rect *bbox) { + epub_page *page = (epub_page*)page_; + epub_document *doc = page->doc; bbox->x0 = 0; bbox->y0 = 0; bbox->x1 = doc->page_w; @@ -74,11 +77,13 @@ epub_bound_page(epub_document *doc, epub_page *page, fz_rect *bbox) } static void -epub_run_page(epub_document *doc, epub_page *page, fz_device *dev, const fz_matrix *ctm, fz_cookie *cookie) +epub_run_page(fz_context *ctx, fz_page *page_, fz_device *dev, const fz_matrix *ctm, fz_cookie *cookie) { - fz_context *ctx = doc->ctx; + epub_page *page = (epub_page*)page_; + epub_document *doc = page->doc; epub_chapter *ch; - int n = ((intptr_t)page) - 1; + int n = page->number; + int count = 0; for (ch = doc->spine; ch; ch = ch->next) { @@ -92,21 +97,34 @@ epub_run_page(epub_document *doc, epub_page *page, fz_device *dev, const fz_matr } } +static fz_page * +epub_load_page(fz_context *ctx, fz_document *doc_, int number) +{ + epub_document *doc = (epub_document*)doc_; + epub_page *page = fz_new_page(ctx, sizeof *page); + page->super.bound_page = epub_bound_page; + page->super.run_page_contents = epub_run_page; + page->super.drop_page_imp = epub_drop_page_imp; + page->doc = doc; + page->number = number; + return (fz_page*)page; +} + static void -epub_close_document(epub_document *doc) +epub_close_document(fz_context *ctx, fz_document *doc_) { - fz_context *ctx = doc->ctx; + epub_document *doc = (epub_document*)doc_; epub_chapter *ch, *next; ch = doc->spine; while (ch) { next = ch->next; - fz_free_html(ctx, ch->box); + fz_drop_html(ctx, ch->box); fz_free(ctx, ch); ch = next; } - fz_close_archive(ctx, doc->zip); - fz_free_html_font_set(ctx, doc->set); + fz_drop_archive(ctx, doc->zip); + fz_drop_html_font_set(ctx, doc->set); fz_free(ctx, doc); } @@ -143,9 +161,8 @@ path_from_idref(char *path, fz_xml *manifest, const char *base_uri, const char * } static epub_chapter * -epub_parse_chapter(epub_document *doc, const char *path) +epub_parse_chapter(fz_context *ctx, epub_document *doc, const char *path) { - fz_context *ctx = doc->ctx; fz_archive *zip = doc->zip; fz_buffer *buf; epub_chapter *ch; @@ -166,9 +183,8 @@ epub_parse_chapter(epub_document *doc, const char *path) } static void -epub_parse_header(epub_document *doc) +epub_parse_header(fz_context *ctx, epub_document *doc) { - fz_context *ctx = doc->ctx; fz_archive *zip = doc->zip; fz_buffer *buf; fz_xml *container_xml, *content_opf; @@ -222,9 +238,9 @@ epub_parse_header(epub_document *doc) { printf("epub: found spine %s\n", s); if (!head) - head = tail = epub_parse_chapter(doc, s); + head = tail = epub_parse_chapter(ctx, doc, s); else - tail = tail->next = epub_parse_chapter(doc, s); + tail = tail->next = epub_parse_chapter(ctx, doc, s); } itemref = fz_xml_find_next(itemref, "itemref"); } @@ -233,8 +249,8 @@ epub_parse_header(epub_document *doc) printf("epub: done.\n"); - fz_free_xml(ctx, container_xml); - fz_free_xml(ctx, content_opf); + fz_drop_xml(ctx, container_xml); + fz_drop_xml(ctx, content_opf); } static epub_document * @@ -243,26 +259,22 @@ epub_init(fz_context *ctx, fz_archive *zip) epub_document *doc; doc = fz_malloc_struct(ctx, epub_document); - doc->ctx = ctx; doc->zip = zip; doc->set = fz_new_html_font_set(ctx); - doc->super.close = (void*)epub_close_document; - doc->super.layout = (void*)epub_layout; - doc->super.count_pages = (void*)epub_count_pages; - doc->super.load_page = (void*)epub_load_page; - doc->super.bound_page = (void*)epub_bound_page; - doc->super.run_page_contents = (void*)epub_run_page; - doc->super.free_page = (void*)epub_free_page; + doc->super.close = epub_close_document; + doc->super.layout = epub_layout; + doc->super.count_pages = epub_count_pages; + doc->super.load_page = epub_load_page; fz_try(ctx) { - epub_parse_header(doc); - epub_layout(doc, DEFW, DEFH, DEFEM); + epub_parse_header(ctx, doc); + epub_layout(ctx, (fz_document*)doc, DEFW, DEFH, DEFEM); } fz_catch(ctx) { - epub_close_document(doc); + epub_close_document(ctx, (fz_document*)doc); fz_rethrow(ctx); } diff --git a/source/html/html-doc.c b/source/html/html-doc.c index ea5f7f3a..7217f74c 100644 --- a/source/html/html-doc.c +++ b/source/html/html-doc.c @@ -5,56 +5,62 @@ #define DEFEM (12) typedef struct html_document_s html_document; +typedef struct html_page_s html_page; struct html_document_s { fz_document super; - fz_context *ctx; fz_archive *zip; fz_html_font_set *set; float page_w, page_h, em; fz_html *box; }; +struct html_page_s +{ + fz_page super; + html_document *doc; + int number; +}; + static void -htdoc_close_document(html_document *doc) +htdoc_close_document(fz_context *ctx, fz_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); + html_document *doc = (html_document*)doc_; + fz_drop_archive(ctx, doc->zip); + fz_drop_html(ctx, doc->box); + fz_drop_html_font_set(ctx, doc->set); + fz_free(ctx, doc); } static int -htdoc_count_pages(html_document *doc) +htdoc_count_pages(fz_context *ctx, fz_document *doc_) { + html_document *doc = (html_document*)doc_; int count = ceilf(doc->box->h / doc->page_h); return count; } -static void * -htdoc_load_page(html_document *doc, int number) -{ - return (void*)((intptr_t)number + 1); -} - static void -htdoc_free_page(html_document *doc, void *page) +htdoc_layout(fz_context *ctx, fz_document *doc_, float w, float h, float em) { + html_document *doc = (html_document*)doc_; + doc->page_w = w; + doc->page_h = h; + doc->em = em; + fz_layout_html(ctx, doc->box, w, h, em); } static void -htdoc_layout(html_document *doc, float w, float h, float em) +htdoc_drop_page_imp(fz_context *ctx, fz_page *page_) { - 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) +htdoc_bound_page(fz_context *ctx, fz_page *page_, fz_rect *bbox) { + html_page *page = (html_page*)page_; + html_document *doc = page->doc; bbox->x0 = bbox->y0 = 0; bbox->x1 = doc->page_w; bbox->y1 = doc->page_h; @@ -62,10 +68,25 @@ htdoc_bound_page(html_document *doc, void *page, fz_rect *bbox) } static void -htdoc_run_page(html_document *doc, void *page, fz_device *dev, const fz_matrix *ctm, fz_cookie *cookie) +htdoc_run_page(fz_context *ctx, fz_page *page_, fz_device *dev, const fz_matrix *ctm, fz_cookie *cookie) +{ + html_page *page = (html_page*)page_; + html_document *doc = page->doc; + int n = page->number; + fz_draw_html(ctx, doc->box, n * doc->page_h, (n+1) * doc->page_h, dev, ctm); +} + +static fz_page * +htdoc_load_page(fz_context *ctx, fz_document *doc_, int number) { - int n = ((intptr_t)page) - 1; - fz_draw_html(doc->ctx, doc->box, n * doc->page_h, (n+1) * doc->page_h, dev, ctm); + html_document *doc = (html_document*)doc_; + html_page *page = fz_new_page(ctx, sizeof *page); + page->super.bound_page = htdoc_bound_page; + page->super.run_page_contents = htdoc_run_page; + page->super.drop_page_imp = htdoc_drop_page_imp; + page->doc = doc; + page->number = number; + return (fz_page*)page; } static html_document * @@ -75,24 +96,20 @@ htdoc_open_document_with_stream(fz_context *ctx, fz_stream *file) 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; + doc->super.close = htdoc_close_document; + doc->super.layout = htdoc_layout; + doc->super.count_pages = htdoc_count_pages; + doc->super.load_page = htdoc_load_page; - buf = fz_read_all(file, 0); + buf = fz_read_all(ctx, file, 0); fz_write_buffer_byte(ctx, buf, 0); doc->box = fz_parse_html(ctx, doc->set, doc->zip, ".", buf, NULL); fz_drop_buffer(ctx, buf); - htdoc_layout(doc, DEFW, DEFH, DEFEM); + htdoc_layout(ctx, (fz_document*)doc, DEFW, DEFH, DEFEM); return doc; } @@ -107,24 +124,20 @@ htdoc_open_document(fz_context *ctx, const char *filename) 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; + doc->super.close = htdoc_close_document; + doc->super.layout = htdoc_layout; + doc->super.count_pages = htdoc_count_pages; + doc->super.load_page = htdoc_load_page; buf = fz_read_file(ctx, filename); fz_write_buffer_byte(ctx, buf, 0); doc->box = fz_parse_html(ctx, doc->set, doc->zip, ".", buf, NULL); fz_drop_buffer(ctx, buf); - htdoc_layout(doc, DEFW, DEFH, DEFEM); + htdoc_layout(ctx, (fz_document*)doc, DEFW, DEFH, DEFEM); return doc; } diff --git a/source/html/html-font.c b/source/html/html-font.c index c7b1cf3c..ae6568c9 100644 --- a/source/html/html-font.c +++ b/source/html/html-font.c @@ -24,7 +24,7 @@ fz_load_html_font(fz_context *ctx, fz_html_font_set *set, int idx = is_mono * 8 + is_sans * 4 + is_bold * 2 + is_italic; if (!set->fonts[idx]) { - data = pdf_lookup_builtin_font(font_names[idx], &size); + data = pdf_lookup_builtin_font(ctx, font_names[idx], &size); if (!data) fz_throw(ctx, FZ_ERROR_GENERIC, "cannot load html font: %s", font_names[idx]); set->fonts[idx] = fz_new_font_from_memory(ctx, font_names[idx], data, size, 0, 1); @@ -38,7 +38,7 @@ fz_html_font_set *fz_new_html_font_set(fz_context *ctx) return fz_malloc_struct(ctx, fz_html_font_set); } -void fz_free_html_font_set(fz_context *ctx, fz_html_font_set *set) +void fz_drop_html_font_set(fz_context *ctx, fz_html_font_set *set) { int i; for (i = 0; i < nelem(set->fonts); ++i) diff --git a/source/html/html-layout.c b/source/html/html-layout.c index 0f304616..5a1a3157 100644 --- a/source/html/html-layout.c +++ b/source/html/html-layout.c @@ -39,7 +39,7 @@ static int iswhite(int c) return c == ' ' || c == '\t' || c == '\r' || c == '\n'; } -static void fz_free_html_flow(fz_context *ctx, fz_html_flow *flow) +static void fz_drop_html_flow(fz_context *ctx, fz_html_flow *flow) { while (flow) { @@ -163,13 +163,13 @@ static void init_box(fz_context *ctx, fz_html *box) fz_default_css_style(ctx, &box->style); } -void fz_free_html(fz_context *ctx, fz_html *box) +void fz_drop_html(fz_context *ctx, fz_html *box) { while (box) { fz_html *next = box->next; - fz_free_html_flow(ctx, box->flow_head); - fz_free_html(ctx, box->down); + fz_drop_html_flow(ctx, box->flow_head); + fz_drop_html(ctx, box->down); fz_free(ctx, box); box = next; } @@ -704,16 +704,16 @@ 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; - fz_fill_text(dev, text, ctm, fz_device_rgb(ctx), color, 1); + fz_fill_text(ctx, dev, text, ctm, fz_device_rgb(ctx), color, 1); - fz_free_text(ctx, text); + fz_drop_text(ctx, text); } else if (node->type == FLOW_IMAGE) { fz_matrix local_ctm = *ctm; fz_pre_translate(&local_ctm, node->x, node->y); fz_pre_scale(&local_ctm, node->w, node->h); - fz_fill_image(dev, node->image, &local_ctm, 1); + fz_fill_image(ctx, dev, node->image, &local_ctm, 1); } } } @@ -728,9 +728,9 @@ static void draw_rect(fz_context *ctx, fz_device *dev, const fz_matrix *ctm, flo fz_lineto(ctx, path, x0, y1); fz_closepath(ctx, path); - fz_fill_path(dev, path, 0, ctm, fz_device_rgb(ctx), rgba, rgba[3]); + fz_fill_path(ctx, dev, path, 0, ctm, fz_device_rgb(ctx), rgba, rgba[3]); - fz_free_path(ctx, path); + fz_drop_path(ctx, path); } static void draw_block_box(fz_context *ctx, fz_html *box, float page_top, float page_bot, fz_device *dev, const fz_matrix *ctm) @@ -902,8 +902,8 @@ fz_parse_html(fz_context *ctx, fz_html_font_set *set, fz_archive *zip, const cha generate_boxes(ctx, set, zip, base_uri, xml, box, css, &match); - fz_free_css(ctx, css); - fz_free_xml(ctx, xml); + fz_drop_css(ctx, css); + fz_drop_xml(ctx, xml); return box; } |