summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--include/mupdf/html.h8
-rw-r--r--source/html/css-apply.c30
-rw-r--r--source/html/layout.c34
3 files changed, 48 insertions, 24 deletions
diff --git a/include/mupdf/html.h b/include/mupdf/html.h
index ba603ed1..868014d0 100644
--- a/include/mupdf/html.h
+++ b/include/mupdf/html.h
@@ -91,9 +91,11 @@ int get_style_property_display(struct style *node);
struct rule *fz_parse_css(fz_context *ctx, struct rule *old, const char *source);
struct property *fz_parse_css_properties(fz_context *ctx, const char *source);
-enum { NONE, BLOCK, INLINE, LIST_ITEM };
-enum { STATIC, RELATIVE, ABSOLUTE, FIXED };
-enum { LEFT, RIGHT, CENTER, JUSTIFY };
+enum { DIS_NONE, DIS_BLOCK, DIS_INLINE, DIS_LIST_ITEM };
+enum { POS_STATIC, POS_RELATIVE, POS_ABSOLUTE, POS_FIXED };
+enum { TA_LEFT, TA_RIGHT, TA_CENTER, TA_JUSTIFY };
+
+enum { TOP, RIGHT, BOTTOM, LEFT };
struct color
{
diff --git a/source/html/css-apply.c b/source/html/css-apply.c
index 9a73ee2a..a99eed2b 100644
--- a/source/html/css-apply.c
+++ b/source/html/css-apply.c
@@ -706,15 +706,15 @@ get_style_property_display(struct style *node)
if (value)
{
if (!strcmp(value->data, "none"))
- return NONE;
+ return DIS_NONE;
if (!strcmp(value->data, "inline"))
- return INLINE;
+ return DIS_INLINE;
if (!strcmp(value->data, "block"))
- return BLOCK;
+ return DIS_BLOCK;
if (!strcmp(value->data, "list-item"))
- return LIST_ITEM;
+ return DIS_LIST_ITEM;
}
- return INLINE;
+ return DIS_INLINE;
}
static float
@@ -768,34 +768,34 @@ compute_style(struct computed_style *style, struct style *node, float width)
memset(style, 0, sizeof *style);
- style->position = STATIC;
- style->text_align = LEFT;
+ style->position = POS_STATIC;
+ style->text_align = TA_LEFT;
style->font_size = 12;
value = get_style_property(node, "position");
if (value)
{
if (!strcmp(value->data, "static"))
- style->position = STATIC;
+ style->position = POS_STATIC;
if (!strcmp(value->data, "relative"))
- style->position = RELATIVE;
+ style->position = POS_RELATIVE;
if (!strcmp(value->data, "absolute"))
- style->position = ABSOLUTE;
+ style->position = POS_ABSOLUTE;
if (!strcmp(value->data, "fixed"))
- style->position = FIXED;
+ style->position = POS_FIXED;
}
value = get_style_property(node, "text-align");
if (value)
{
if (!strcmp(value->data, "left"))
- style->text_align = LEFT;
+ style->text_align = TA_LEFT;
if (!strcmp(value->data, "right"))
- style->text_align = RIGHT;
+ style->text_align = TA_RIGHT;
if (!strcmp(value->data, "center"))
- style->text_align = CENTER;
+ style->text_align = TA_CENTER;
if (!strcmp(value->data, "justify"))
- style->text_align = JUSTIFY;
+ style->text_align = TA_JUSTIFY;
}
value = get_style_property(node, "vertical-align");
diff --git a/source/html/layout.c b/source/html/layout.c
index 2960de73..6b2c7d44 100644
--- a/source/html/layout.c
+++ b/source/html/layout.c
@@ -157,13 +157,13 @@ static void generate_boxes(fz_context *ctx, fz_xml *node, struct box *top, struc
// TOOD: <br>
// TODO: <img>
- if (display != NONE)
+ if (display != DIS_NONE)
{
- if (display == BLOCK)
+ if (display == DIS_BLOCK)
{
top = insert_block_box(ctx, box, top);
}
- else if (display == INLINE)
+ else if (display == DIS_INLINE)
{
insert_inline_box(ctx, box, top);
}
@@ -190,14 +190,27 @@ static void layout_inline(fz_context *ctx, struct box *box, struct box *top)
{
struct computed_style style;
struct box *child;
+ const char *s;
compute_style(&style, &box->style, top->w);
+ box->x = top->x + top->w;
box->y = top->y;
box->h = style.font_size;
+ box->w = 0;
+
+ s = fz_xml_text(box->node);
+ if (s)
+ {
+ box->w += strlen(s) * 0.5 * style.font_size;
+ }
+
for (child = box->down; child; child = child->next)
{
layout_inline(ctx, child, top);
+ if (child->h > box->h)
+ box->h = child->h;
+ top->w += child->w;
}
}
@@ -208,13 +221,17 @@ static void layout_anonymous(fz_context *ctx, struct box *box, struct box *top)
compute_style(&style, &box->style, top->w);
+ box->x = top->x;
box->y = top->y + top->h;
box->h = 0;
+ box->w = 0;
+
for (child = box->down; child; child = child->next)
{
layout_inline(ctx, child, box);
if (child->h > box->h)
box->h = child->h;
+ box->w += child->w;
}
}
@@ -225,8 +242,11 @@ static void layout_block(fz_context *ctx, struct box *box, struct box *top)
compute_style(&style, &box->style, top->w);
- box->y = top->y + top->h;
+ box->x = top->x + style.margin[LEFT];
+ box->y = top->y + top->h + style.margin[TOP];
+ box->w = top->w - (style.margin[LEFT] + style.margin[RIGHT]);
box->h = 0;
+
for (child = box->down; child; child = child->next)
{
if (child->type == BOX_BLOCK)
@@ -235,6 +255,8 @@ static void layout_block(fz_context *ctx, struct box *box, struct box *top)
layout_anonymous(ctx, child, box);
box->h += child->h;
}
+
+ box->h += style.margin[BOTTOM];
}
static void indent(int level)
@@ -246,7 +268,7 @@ static void print_box(fz_context *ctx, struct box *box, int level)
{
while (box)
{
- printf("%-5d", (int)box->y);
+ printf("%-5d %-5d", (int)box->x, (int)box->y);
indent(level);
switch (box->type)
{
@@ -346,7 +368,7 @@ html_layout_document(html_document *doc, float w, float h)
css = fz_parse_css(doc->ctx, NULL, default_css);
css = load_css(doc->ctx, css, doc->root);
-// print_rules(css);
+ print_rules(css);
root_box = new_box(doc->ctx, NULL, NULL);
generate_boxes(doc->ctx, doc->root, root_box, css);