summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--include/mupdf/html.h2
-rw-r--r--source/html/css-apply.c21
-rw-r--r--source/html/layout.c59
3 files changed, 68 insertions, 14 deletions
diff --git a/include/mupdf/html.h b/include/mupdf/html.h
index 419fa0a3..ba603ed1 100644
--- a/include/mupdf/html.h
+++ b/include/mupdf/html.h
@@ -121,6 +121,6 @@ struct computed_style
};
void apply_styles(fz_context *ctx, struct style *style, struct rule *rule, fz_xml *node);
-void compute_style(struct computed_style *cstyle, struct style *style);
+void compute_style(struct computed_style *cstyle, struct style *style, float width);
#endif
diff --git a/source/html/css-apply.c b/source/html/css-apply.c
index 1a93e3e4..9a73ee2a 100644
--- a/source/html/css-apply.c
+++ b/source/html/css-apply.c
@@ -761,11 +761,10 @@ compute_number(struct value *value, float em, float hundred, float scale, float
}
void
-compute_style(struct computed_style *style, struct style *node)
+compute_style(struct computed_style *style, struct style *node, float width)
{
struct value *value;
float em = 12;
- float hundred = 100;
memset(style, 0, sizeof *style);
@@ -829,25 +828,25 @@ compute_style(struct computed_style *style, struct style *node)
style->line_height = compute_number(value, em, em, em, 1.2 * em);
value = get_style_property(node, "text-indent");
- style->text_indent = compute_number(value, em, hundred, 1, 0);
+ style->text_indent = compute_number(value, em, width, 1, 0);
value = get_style_property(node, "margin-top");
- style->margin[0] = compute_number(value, em, hundred, 1, 0);
+ style->margin[0] = compute_number(value, em, width, 1, 0);
value = get_style_property(node, "margin-right");
- style->margin[1] = compute_number(value, em, hundred, 1, 0);
+ style->margin[1] = compute_number(value, em, width, 1, 0);
value = get_style_property(node, "margin-bottom");
- style->margin[2] = compute_number(value, em, hundred, 1, 0);
+ style->margin[2] = compute_number(value, em, width, 1, 0);
value = get_style_property(node, "margin-left");
- style->margin[3] = compute_number(value, em, hundred, 1, 0);
+ style->margin[3] = compute_number(value, em, width, 1, 0);
value = get_style_property(node, "padding-top");
- style->padding[0] = compute_number(value, em, hundred, 1, 0);
+ style->padding[0] = compute_number(value, em, width, 1, 0);
value = get_style_property(node, "padding-right");
- style->padding[1] = compute_number(value, em, hundred, 1, 0);
+ style->padding[1] = compute_number(value, em, width, 1, 0);
value = get_style_property(node, "padding-bottom");
- style->padding[2] = compute_number(value, em, hundred, 1, 0);
+ style->padding[2] = compute_number(value, em, width, 1, 0);
value = get_style_property(node, "padding-left");
- style->padding[3] = compute_number(value, em, hundred, 1, 0);
+ style->padding[3] = compute_number(value, em, width, 1, 0);
{
const char *font_family = get_style_property_string(node, "font-family", "serif");
diff --git a/source/html/layout.c b/source/html/layout.c
index 1d92b502..2960de73 100644
--- a/source/html/layout.c
+++ b/source/html/layout.c
@@ -186,8 +186,55 @@ static void generate_boxes(fz_context *ctx, fz_xml *node, struct box *top, struc
}
}
-static void layout_boxes(fz_context *ctx, struct box *top, float w, float h)
+static void layout_inline(fz_context *ctx, struct box *box, struct box *top)
{
+ struct computed_style style;
+ struct box *child;
+
+ compute_style(&style, &box->style, top->w);
+
+ box->y = top->y;
+ box->h = style.font_size;
+ for (child = box->down; child; child = child->next)
+ {
+ layout_inline(ctx, child, top);
+ }
+}
+
+static void layout_anonymous(fz_context *ctx, struct box *box, struct box *top)
+{
+ struct computed_style style;
+ struct box *child;
+
+ compute_style(&style, &box->style, top->w);
+
+ box->y = top->y + top->h;
+ box->h = 0;
+ for (child = box->down; child; child = child->next)
+ {
+ layout_inline(ctx, child, box);
+ if (child->h > box->h)
+ box->h = child->h;
+ }
+}
+
+static void layout_block(fz_context *ctx, struct box *box, struct box *top)
+{
+ struct computed_style style;
+ struct box *child;
+
+ compute_style(&style, &box->style, top->w);
+
+ box->y = top->y + top->h;
+ box->h = 0;
+ for (child = box->down; child; child = child->next)
+ {
+ if (child->type == BOX_BLOCK)
+ layout_block(ctx, child, box);
+ else if (child->type == BOX_ANONYMOUS)
+ layout_anonymous(ctx, child, box);
+ box->h += child->h;
+ }
}
static void indent(int level)
@@ -199,6 +246,7 @@ static void print_box(fz_context *ctx, struct box *box, int level)
{
while (box)
{
+ printf("%-5d", (int)box->y);
indent(level);
switch (box->type)
{
@@ -285,6 +333,7 @@ html_layout_document(html_document *doc, float w, float h)
{
struct rule *css = NULL;
struct box *root_box;
+ struct box *win_box;
#if 0
strcpy(dirname, argv[i]);
@@ -301,6 +350,12 @@ html_layout_document(html_document *doc, float w, float h)
root_box = new_box(doc->ctx, NULL, NULL);
generate_boxes(doc->ctx, doc->root, root_box, css);
- layout_boxes(doc->ctx, root_box, w, h);
+
+ win_box = new_box(doc->ctx, NULL, NULL);
+ win_box->w = w;
+ win_box->h = 0;
+
+ layout_block(doc->ctx, root_box, win_box);
+
print_box(doc->ctx, root_box, 0);
}