From 5179418ae7f4497ad6fec2e7d1ac2a8ad7492fd8 Mon Sep 17 00:00:00 2001 From: Tor Andersson Date: Thu, 14 May 2015 00:19:44 +0200 Subject: epub: Apply @page selector margins. --- source/html/css-apply.c | 23 +++++++++++++++++++++++ source/html/epub-doc.c | 11 +++++++---- source/html/html-doc.c | 14 ++++++++++---- source/html/html-layout.c | 3 +++ 4 files changed, 43 insertions(+), 8 deletions(-) (limited to 'source/html') diff --git a/source/html/css-apply.c b/source/html/css-apply.c index d72079e8..6d99c07d 100644 --- a/source/html/css-apply.c +++ b/source/html/css-apply.c @@ -623,6 +623,29 @@ fz_match_css(fz_context *ctx, fz_css_match *match, fz_css_rule *css, fz_xml *nod } } +void +fz_match_css_at_page(fz_context *ctx, fz_css_match *match, fz_css_rule *css) +{ + fz_css_rule *rule; + fz_css_selector *sel; + fz_css_property *prop; + + for (rule = css; rule; rule = rule->next) + { + sel = rule->selector; + while (sel) + { + if (sel->name && !strcmp(sel->name, "@page")) + { + for (prop = rule->declaration; prop; prop = prop->next) + add_property(match, prop->name, prop->value, selector_specificity(sel)); + break; + } + sel = sel->next; + } + } +} + static fz_css_value * value_from_raw_property(fz_css_match *match, const char *name) { diff --git a/source/html/epub-doc.c b/source/html/epub-doc.c index b7b24c42..90c86493 100644 --- a/source/html/epub-doc.c +++ b/source/html/epub-doc.c @@ -37,10 +37,13 @@ epub_layout(fz_context *ctx, fz_document *doc_, float w, float h, float em) epub_document *doc = (epub_document*)doc_; epub_chapter *ch; - doc->page_margin[T] = em; - doc->page_margin[B] = em; - doc->page_margin[L] = 0; - doc->page_margin[R] = 0; + if (doc->spine && doc->spine->box) + { + doc->page_margin[T] = fz_from_css_number(doc->spine->box->style.margin[T], em, em); + doc->page_margin[B] = fz_from_css_number(doc->spine->box->style.margin[B], em, em); + doc->page_margin[L] = fz_from_css_number(doc->spine->box->style.margin[L], em, em); + doc->page_margin[R] = fz_from_css_number(doc->spine->box->style.margin[R], em, em); + } doc->page_w = w - doc->page_margin[L] - doc->page_margin[R]; doc->page_h = h - doc->page_margin[T] - doc->page_margin[B]; diff --git a/source/html/html-doc.c b/source/html/html-doc.c index 90033717..04286f7c 100644 --- a/source/html/html-doc.c +++ b/source/html/html-doc.c @@ -44,13 +44,19 @@ static void htdoc_layout(fz_context *ctx, fz_document *doc_, float w, float h, float em) { html_document *doc = (html_document*)doc_; - doc->page_margin[T] = em; - doc->page_margin[B] = em; - doc->page_margin[L] = 0; - doc->page_margin[R] = 0; + + if (doc->box) + { + doc->page_margin[T] = fz_from_css_number(doc->box->style.margin[T], em, em); + doc->page_margin[B] = fz_from_css_number(doc->box->style.margin[B], em, em); + doc->page_margin[L] = fz_from_css_number(doc->box->style.margin[L], em, em); + doc->page_margin[R] = fz_from_css_number(doc->box->style.margin[R], em, em); + } + doc->page_w = w - doc->page_margin[L] - doc->page_margin[R]; doc->page_h = h - doc->page_margin[T] - doc->page_margin[B]; doc->em = em; + fz_layout_html(ctx, doc->box, doc->page_w, doc->page_h, doc->em); } diff --git a/source/html/html-layout.c b/source/html/html-layout.c index 5fd3937b..52171ef8 100644 --- a/source/html/html-layout.c +++ b/source/html/html-layout.c @@ -3,6 +3,7 @@ enum { T, R, B, L }; static const char *default_css = +"@page{margin:1em 0}" "html,address,blockquote,body,dd,div,dl,dt,h1,h2,h3,h4,h5,h6,ol,p,ul,center,hr,pre{display:block}" "span{display:inline}" "li{display:list-item}" @@ -1262,6 +1263,8 @@ fz_parse_html(fz_context *ctx, fz_html_font_set *set, fz_archive *zip, const cha match.up = NULL; match.count = 0; + fz_match_css_at_page(ctx, &match, css); + fz_apply_css_style(ctx, set, &box->style, &match); generate_boxes(ctx, set, zip, base_uri, xml, box, css, &match, 0); -- cgit v1.2.3