From 7159498b095238ca8c8c87cc99d83c46fc5cd19b Mon Sep 17 00:00:00 2001
From: Tor Andersson <tor.andersson@artifex.com>
Date: Thu, 23 Oct 2014 16:13:42 +0200
Subject: html: Apply margins.

---
 source/html/css-apply.c | 30 +++++++++++++++---------------
 source/html/layout.c    | 34 ++++++++++++++++++++++++++++------
 2 files changed, 43 insertions(+), 21 deletions(-)

(limited to 'source/html')

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);
-- 
cgit v1.2.3