summaryrefslogtreecommitdiff
path: root/render/layout.c
blob: 0070d6b950de8ae21c58e670f307e05ea53d2c7d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
/**
 * $Id: layout.c,v 1.4 2002/05/21 21:32:35 bursa Exp $
 */

#include <assert.h>
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "libxml/HTMLparser.h"
#include "css.h"
#include "font.h"
#include "box.h"
#include "utils.h"

/**
 * internal functions
 */

signed long len(struct css_length * length, unsigned long em);

unsigned long layout_block_children(struct box * box, unsigned long width);
void layout_inline_container(struct box * box, unsigned long width);
void layout_table(struct box * box, unsigned long width);


/**
 * convert a struct css_length to pixels
 */

signed long len(struct css_length * length, unsigned long em)
{
	switch (length->unit) {
		case CSS_UNIT_EM: return length->value * em;
		case CSS_UNIT_EX: return length->value * em * 0.6;
		case CSS_UNIT_PX: return length->value;
		case CSS_UNIT_IN: return length->value * 90.0;
		case CSS_UNIT_CM: return length->value * 35.0;
		case CSS_UNIT_MM: return length->value * 3.5;
		case CSS_UNIT_PT: return length->value * 90.0 / 72.0;
		case CSS_UNIT_PC: return length->value * 90.0 / 6.0;
		default: return 0;
	}
	return 0;
}

/**
 * layout algorithm
 */

void layout_block(struct box * box, unsigned long width)
{
	struct css_style * style = box->style;
	switch (style->width.width) {
		case CSS_WIDTH_AUTO:
			box->width = width;
			break;
		case CSS_WIDTH_LENGTH:
			box->width = len(&style->width.value.length, 20);
			break;
		case CSS_WIDTH_PERCENT:
			box->width = width * style->width.value.percent / 100;
			break;
	}
	box->height = layout_block_children(box, box->width);
	switch (style->height.height) {
		case CSS_HEIGHT_AUTO:
			break;
		case CSS_HEIGHT_LENGTH:
			box->height = len(&style->height.length, 20);
			break;
	}
}

unsigned long layout_block_children(struct box * box, unsigned long width)
{
	struct box * c;
	unsigned long y = 0;
	
	for (c = box->children; c != 0; c = c->next) {
		switch (c->type) {
			case BOX_BLOCK:
				layout_block(c, width);
				c->x = 0;
				c->y = y;
				y += c->height;
				break;
			case BOX_INLINE_CONTAINER:
				layout_inline_container(c, width);
				c->x = 0;
				c->y = y;
				y += c->height;
				break;
			case BOX_TABLE:
				layout_table(c, width);
				c->x = 0;
				c->y = y;
				y += c->height;
				break;
			default:
				printf("%s -> %s\n",
						box->node ? box->node->name : "()",
						c->node ? c->node->name : "()");
				die("block child not block, table, or inline container");
		}
	}
	return y;
}

void layout_inline_container(struct box * box, unsigned long width)
{
	/* TODO: write this */
	struct box * c;
	unsigned long y = 0;
	unsigned long x = 0;
	const char * end;
	struct box * c2;
	struct font_split split;

	for (c = box->children; c != 0; ) {
		if (c->type == BOX_FLOAT) {
			layout_block(c, width);
			c->x = 0;
			c->y = y;
			c = c->next;
			continue;
		}
		
		assert(c->type == BOX_INLINE);
		split = font_split(0, c->font, c->text, width - x, x == 0);
		if (*(split.end) == 0) {
			/* fits into this line */
			c->x = x;
			c->y = y;
			c->width = split.width;
			c->height = split.height;
			c->length = split.end - c->text;
			x += c->width;
			c = c->next;
		} else if (split.end == c->text) {
			/* doesn't fit at all: move down a line */
			x = 0;
			y += 30;
		} else {
			/* split into two lines */ 
			c->x = x;
			c->y = y;
			c->width = split.width;
			c->height = split.height;
			c->length = split.end - c->text;
			x = 0;
			y += 30;
			c2 = memcpy(xcalloc(1, sizeof(struct box)), c, sizeof(struct box));
			c2->text = split.end;
			c2->next = c->next;
			c->next = c2;
			c = c2;
		}
	}

	box->width = width;
	box->height = y + 30;
}

/**
 * layout a table
 *
 * this is the fixed table layout algorithm,
 * <http://www.w3.org/TR/REC-CSS2/tables.html#fixed-table-layout>
 */

void layout_table(struct box * table, unsigned long width)
{
	unsigned int columns;  /* total columns */
	unsigned int auto_columns;  /* number of columns with auto width */
	unsigned long table_width;
	unsigned long used_width = 0;  /* width used by fixed or percent columns */
	unsigned long auto_width;  /* width of each auto column (all equal) */
	unsigned long extra_width = 0;  /* extra width for each column if table is wider than columns */
	unsigned long x;
	unsigned long y = 0;
	unsigned long * xs;
	unsigned int i;
	struct box * c;
	struct box * r;

	assert(table->type == BOX_TABLE);

	/* find table width */
	switch (table->style->width.width) {
		case CSS_WIDTH_LENGTH:
			table_width = len(&table->style->width.value.length, 20);
			break;
		case CSS_WIDTH_PERCENT:
			table_width = width * table->style->width.value.percent / 100;
			break;
		case CSS_WIDTH_AUTO:
		default:
			table_width = width;
			break;
	}

	/* calculate column statistics */
	for (columns = 0, auto_columns = 0, c = table->children->children;
			c != 0; c = c->next) {
		assert(c->type == BOX_TABLE_CELL);
		switch (c->style->width.width) {
			case CSS_WIDTH_LENGTH:
				used_width += len(&c->style->width.value.length, 20);
				break;
			case CSS_WIDTH_PERCENT:
				used_width += table_width * c->style->width.value.percent / 100;
				break;
			case CSS_WIDTH_AUTO:
				auto_columns++;
				break;
		}
		columns++;
	}

	if (auto_columns == 0 && table->style->width.width != CSS_WIDTH_AUTO)
		extra_width = (table_width - used_width) / columns;
	else if (auto_columns != 0)
		auto_width = (table_width - used_width) / auto_columns;

	/*printf("%i %i %i %i %i\n", table_width, columns, auto_columns, used_width, auto_width);*/
	
	/* find column widths */
	xs = xcalloc(columns + 1, sizeof(*xs));
	xs[0] = x = 0;
	for (i = 1, c = table->children->children; c != 0; i++, c = c->next) {
		switch (c->style->width.width) {
			case CSS_WIDTH_LENGTH:
				x += len(&c->style->width.value.length, 10) + extra_width;
				break;
			case CSS_WIDTH_PERCENT:
				x += table_width * c->style->width.value.percent / 100 + extra_width;
				break;
			case CSS_WIDTH_AUTO:
				x += auto_width;
				break;
		}
		xs[i] = x;
		/*printf("%i ", x);*/
	}
	/*printf("\n");*/

	if (auto_columns == 0 && table->style->width.width == CSS_WIDTH_AUTO)
		table_width = used_width;
	
	/* position cells */
	for (r = table->children; r != 0; r = r->next) {
		unsigned long height = 0;
		for (i = 0, c = r->children; c != 0; i++, c = c->next) {
			c->width = xs[i+1] - xs[i];
			c->height = layout_block_children(c, c->width);
			switch (c->style->height.height) {
				case CSS_HEIGHT_AUTO:
					break;
				case CSS_HEIGHT_LENGTH:
					c->height = len(&c->style->height.length, 10);
					break;
			}
			c->x = xs[i];
			c->y = 0;
			if (c->height > height) height = c->height;
		}
		r->x = 0;
		r->y = y;
		r->width = table_width;
		r->height = height;
		y += height;
	}

	free(xs);
	
	table->width = table_width;
	table->height = y;
}