summaryrefslogtreecommitdiff
path: root/source
diff options
context:
space:
mode:
authorTor Andersson <tor.andersson@artifex.com>2014-11-13 12:06:46 +0100
committerTor Andersson <tor.andersson@artifex.com>2014-12-03 12:25:51 +0100
commiteea982f2ea75a39a0c1218f528cfaa6678baf263 (patch)
tree7f282e3459cc050b8806000d827d142274ddf1d7 /source
parent2dca8942c2ad00fefdd95133c2620f318ccde1b2 (diff)
downloadmupdf-eea982f2ea75a39a0c1218f528cfaa6678baf263.tar.xz
html: Parse external CSS files and basic pagination.
Show a translated view of the continuous layout for each page.
Diffstat (limited to 'source')
-rw-r--r--source/html/css-parse.c27
-rw-r--r--source/html/handler.c34
-rw-r--r--source/html/layout.c82
3 files changed, 76 insertions, 67 deletions
diff --git a/source/html/css-parse.c b/source/html/css-parse.c
index fe91a933..93c5ff58 100644
--- a/source/html/css-parse.c
+++ b/source/html/css-parse.c
@@ -745,3 +745,30 @@ struct rule *fz_parse_css(fz_context *ctx, struct rule *chain, const char *sourc
next(&buf);
return parse_stylesheet(&buf, chain);
}
+
+struct rule *fz_parse_css_file(fz_context *ctx, struct rule *chain, const char *filename)
+{
+ fz_stream *stm = NULL;
+ fz_buffer *buf = NULL;
+
+ fz_var(buf);
+
+ stm = fz_open_file(ctx, filename);
+ fz_try(ctx)
+ {
+ buf = fz_read_all(stm, 0);
+ fz_write_buffer_byte(ctx, buf, 0);
+ chain = fz_parse_css(ctx, chain, buf->data);
+ }
+ fz_always(ctx)
+ {
+ fz_drop_buffer(ctx, buf);
+ fz_close(stm);
+ }
+ fz_catch(ctx)
+ {
+ fz_rethrow(ctx);
+ }
+
+ return chain;
+}
diff --git a/source/html/handler.c b/source/html/handler.c
index 859721c8..0c7463cb 100644
--- a/source/html/handler.c
+++ b/source/html/handler.c
@@ -10,14 +10,21 @@ html_close_document(html_document *doc)
int
html_count_pages(html_document *doc)
{
- return 1;
+ int count;
+
+ if (!doc->box) html_layout_document(doc, 400, 400);
+
+ count = ceilf(doc->box->h / doc->page_h);
+printf("count pages! %g / %g = %d\n", doc->box->h, doc->page_h, count);
+ return count;
}
html_page *
html_load_page(html_document *doc, int number)
{
- printf("html: load page %d\n", number);
- return doc->box;
+printf("load page %d\n", number);
+ if (!doc->box) html_layout_document(doc, 400, 400);
+ return (void*)((intptr_t)number + 1);
}
void
@@ -28,18 +35,20 @@ html_free_page(html_document *doc, html_page *page)
fz_rect *
html_bound_page(html_document *doc, html_page *page, fz_rect *bbox)
{
+ if (!doc->box) html_layout_document(doc, 400, 400);
printf("html: bound page\n");
bbox->x0 = bbox->y0 = 0;
- bbox->x1 = 400;
- bbox->y1 = 600;
+ bbox->x1 = doc->page_w;
+ bbox->y1 = doc->page_h;
return bbox;
}
void
html_run_page(html_document *doc, html_page *page, fz_device *dev, const fz_matrix *ctm, fz_cookie *cookie)
{
- printf("html: run page\n");
- html_run_box(doc->ctx, page, dev, ctm);
+ int n = ((intptr_t)page) - 1;
+ printf("html: run page %d\n", n);
+ html_run_box(doc->ctx, doc->box, n * doc->page_h, dev, ctm);
}
html_document *
@@ -50,11 +59,13 @@ html_open_document_with_stream(fz_context *ctx, fz_stream *file)
fz_xml *xml;
buf = fz_read_all(file, 0);
+ fz_write_buffer_byte(ctx, buf, 0);
xml = fz_parse_xml(ctx, buf->data, buf->len, 1);
fz_drop_buffer(ctx, buf);
doc = fz_malloc_struct(ctx, html_document);
doc->ctx = ctx;
+ doc->dirname = NULL;
doc->super.close = (void*)html_close_document;
doc->super.count_pages = (void*)html_count_pages;
@@ -66,8 +77,6 @@ html_open_document_with_stream(fz_context *ctx, fz_stream *file)
doc->xml = xml;
doc->box = NULL;
- html_layout_document(doc, 400, 600);
-
return doc;
}
@@ -76,6 +85,7 @@ html_open_document(fz_context *ctx, const char *filename)
{
fz_stream *file;
html_document *doc;
+ char *s;
file = fz_open_file(ctx, filename);
if (!file)
@@ -94,6 +104,12 @@ html_open_document(fz_context *ctx, const char *filename)
fz_rethrow(ctx);
}
+ doc->dirname = fz_strdup(ctx, filename);
+ s = strrchr(doc->dirname, '/');
+ if (!s) s = strrchr(doc->dirname, '\\');
+ if (s) s[1] = 0;
+ else doc->dirname[0] = 0;
+
return doc;
}
diff --git a/source/html/layout.c b/source/html/layout.c
index d07b2dab..76d5b786 100644
--- a/source/html/layout.c
+++ b/source/html/layout.c
@@ -5,7 +5,7 @@ static const char *default_css =
"span{display:inline}"
"li{display:list-item}"
"head{display:none}"
-"body{margin:0px}"
+"body{margin:1em}"
"h1{font-size:2em;margin:.67em 0}"
"h2{font-size:1.5em;margin:.75em 0}"
"h3{font-size:1.17em;margin:.83em 0}"
@@ -30,40 +30,6 @@ static const char *default_css =
"center{text-align:center}"
"svg{display:none}";
-enum
-{
- BOX_BLOCK, /* block-level: contains block and flow boxes */
- BOX_FLOW, /* block-level: contains only inline boxes */
- BOX_INLINE, /* inline-level: contains only inline boxes */
-};
-
-struct box
-{
- int type;
- float x, y, w, h; /* content */
- float padding[4];
- float margin[4];
- struct box *up, *down, *last, *next;
- fz_xml *node;
- struct flow *flow_head, **flow_tail;
- struct computed_style style;
-};
-
-enum
-{
- FLOW_WORD,
- FLOW_GLUE,
-};
-
-struct flow
-{
- int type;
- float x, y, w, h, em;
- struct computed_style *style;
- char *text, *broken_text;
- struct flow *next;
-};
-
static int iswhite(int c)
{
return c == ' ' || c == '\t' || c == '\r' || c == '\n';
@@ -561,9 +527,11 @@ draw_block_box(fz_context *ctx, struct box *box, fz_device *dev, const fz_matrix
}
void
-html_run_box(fz_context *ctx, struct box *box, fz_device *dev, const fz_matrix *ctm)
+html_run_box(fz_context *ctx, struct box *box, float offset, fz_device *dev, const fz_matrix *inctm)
{
- draw_block_box(ctx, box, dev, ctm);
+ fz_matrix ctm = *inctm;
+ fz_pre_translate(&ctm, 0, -offset);
+ draw_block_box(ctx, box, dev, &ctm);
}
static char *concat_text(fz_context *ctx, fz_xml *root)
@@ -591,13 +559,16 @@ static char *concat_text(fz_context *ctx, fz_xml *root)
return s;
}
-static struct rule *load_css(fz_context *ctx, struct rule *css, fz_xml *root)
+static struct rule *load_css(html_document *doc, struct rule *css, fz_xml *root)
{
+ fz_context *ctx = doc->ctx;
fz_xml *node;
+ char filename[2048];
+
for (node = root; node; node = fz_xml_next(node))
{
const char *tag = fz_xml_tag(node);
-#if 0
+#if 1
if (tag && !strcmp(tag, "link"))
{
char *rel = fz_xml_att(node, "rel");
@@ -607,9 +578,9 @@ static struct rule *load_css(fz_context *ctx, struct rule *css, fz_xml *root)
if ((type && !strcmp(type, "text/css")) || !type)
{
char *href = fz_xml_att(node, "href");
- strcpy(filename, dirname);
- strcat(filename, href);
- css = css_parse_file(css, filename);
+ fz_strlcpy(filename, doc->dirname, sizeof filename);
+ fz_strlcat(filename, href, sizeof filename);
+ css = fz_parse_css_file(ctx, css, filename);
}
}
}
@@ -621,29 +592,24 @@ static struct rule *load_css(fz_context *ctx, struct rule *css, fz_xml *root)
fz_free(ctx, s);
}
if (fz_xml_down(node))
- css = load_css(ctx, css, fz_xml_down(node));
+ css = load_css(doc, css, fz_xml_down(node));
}
return css;
}
void
-html_layout_document(html_document *doc, float w, float h)
+html_layout_document(html_document *doc, float page_w, float page_h)
{
struct rule *css = NULL;
struct box *root_box;
- struct box *win_box;
+ struct box *page_box;
struct style style;
-#if 0
- strcpy(dirname, argv[i]);
- s = strrchr(dirname, '/');
- if (!s) s = strrchr(dirname, '\\');
- if (s) s[1] = 0;
- else strcpy(dirname, "./");
-#endif
+ doc->page_w = page_w;
+ doc->page_h = page_h;
css = fz_parse_css(doc->ctx, NULL, default_css);
- css = load_css(doc->ctx, css, doc->xml);
+ css = load_css(doc, css, doc->xml);
print_rules(css);
@@ -652,15 +618,15 @@ html_layout_document(html_document *doc, float w, float h)
root_box = new_box(doc->ctx, NULL);
- win_box = new_box(doc->ctx, NULL);
- win_box->w = w;
- win_box->h = 0;
+ page_box = new_box(doc->ctx, NULL);
+ page_box->w = page_w;
+ page_box->h = 0;
generate_boxes(doc, doc->xml, root_box, css, &style);
- layout_block(doc->ctx, root_box, win_box, 12, 0);
+ layout_block(doc->ctx, root_box, page_box, 12, 0);
- print_box(doc->ctx, root_box, 0);
+ // print_box(doc->ctx, root_box, 0);
doc->box = root_box;
}