summaryrefslogtreecommitdiff
path: root/render/render.c
blob: 3bcba3305e51a71780c8adbdb0a20e1e8b699966 (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
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
/**
 * $Id: render.c,v 1.2 2002/04/23 17:06:20 bursa Exp $
 */

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

/**
 * internal structures
 */

struct coord {
	unsigned long x, y;
};

struct data {    /* used in _private field of xmlNode */
	struct css_style * style;
	unsigned long x, y, width, height;
};

struct box {
	enum { BOX_BLOCK, BOX_INLINE_CONTAINER, BOX_INLINE, BOX_FLOAT } type;
	xmlNode * node;
	struct css_style * style;
	unsigned long x, y, width, height;
	const char * text;
	unsigned int length;
	struct box * next;
	struct box * children;
	struct box * last;
	struct box * parent;
};

void layout_element(xmlNode * e, unsigned long width);
unsigned long layout_element_children(xmlNode * e, unsigned long width);

void box_add_child(struct box * parent, struct box * child);
struct box * xml_to_box(xmlNode * n, struct css_style * parent_style, struct css_stylesheet * stylesheet,
		struct css_selector ** selector, unsigned int depth,
		struct box * parent, struct box * inline_container);
void box_dump(struct box * box, unsigned int depth);


/**
 * 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_element(xmlNode * e, unsigned long width)
{
	struct data * data = (struct data *) e->_private;
	struct css_style * style = data->style;
	switch (style->width.width) {
		case CSS_WIDTH_AUTO:
			data->width = width;
			break;
		case CSS_WIDTH_LENGTH:
			data->width = len(&style->width.value.length, 10);
			break;
		case CSS_WIDTH_PERCENT:
			data->width = width * style->width.value.percent / 100;
			break;
	}
	data->height = layout_element_children(e, data->width);
	switch (style->height.height) {
		case CSS_HEIGHT_AUTO:
			break;
		case CSS_HEIGHT_LENGTH:
			data->height = len(&style->height.length, 10);
			break;
	}
}

unsigned long layout_element_children(xmlNode * e, unsigned long width)
{
	struct coord pos;
	int inline_mode = 0;
	xmlNode * c = e->children;
	xmlNode * next;
	unsigned long y = 0;
	struct coord float_left = { 0, 0 }, float_right = { 0, 0 };  /* bottom corner of current float */
	xmlNode * line;  /* first node in current line box */

	printf("layout_element_children: starting %s\n", e->name);

	while (c != 0) {
		struct data * data = (struct data *) c->_private;
		next = c->next;
		switch (c->type) {
			case XML_ELEMENT_NODE: {
				struct css_style * style = data->style;
				printf("element %s: ", c->name);
				switch (style->float_) {
					case CSS_FLOAT_NONE:
						switch (style->display) {
							case CSS_DISPLAY_BLOCK:
								printf("block");
								if (inline_mode) {
									y = pos.y;
									inline_mode = 0;
									printf(" (inline_mode = 0)");
								}
								puts("");
								layout_element(c, width);
								data->x = 0;
								data->y = y;
								y += data->height;
								break;
							case CSS_DISPLAY_INLINE:
								puts("inline");
								next = c->children;
								/* TODO: fill x, y, width, height [1] */
								/* TODO: replaced elements */
								break;
						}
						break;
					case CSS_FLOAT_LEFT:
						puts("float left");
						layout_element(c, width);
						data->x = 0;
						if (inline_mode) {
							if (data->width <= width - pos.y) {
								xmlNode * n;
								for (n = line; n != c;
								   n = n->next ? n->next : n->parent->next) {
									printf("moving %s\n", n->name);
									if (n->_private)
									  ((struct data *) n->_private)->x +=
												data->width;
								}
								data->y = y;
							} else {
								data->y = pos.y;
							}
						} else {
							data->y = y;
						}
						float_left.x = data->width;
						float_left.y = data->y + data->height;
						break;
					case CSS_FLOAT_RIGHT:
						puts("float right");
						layout_element(c, width);
						data->x = width - data->width;
						if (inline_mode) {
							if (data->width <= width - pos.y) {
								data->y = y;
							} else {
								data->y = pos.y;
							}
						} else {
							data->y = y;
						}
						float_right.x = data->x;
						float_right.y = data->y + data->height;
						break;
				}
				break;
			}
			case XML_TEXT_NODE:
				printf("text: ");
				if (whitespace(c->content)) {
					c->_private = 0;
					puts("whitespace");
				} else {
					struct data * data = xcalloc(1, sizeof(struct data));
					unsigned int x1 = y < float_right.y ? float_right.x : width;
					if (!inline_mode) {
						pos.x = y < float_left.y ? float_left.x : 0;
						pos.y = y;
						inline_mode = 1;
						line = c;
						printf("(inline_mode = 1)");
					}
					puts("");
					c->_private = data;
					data->height = 2;
					data->width = strlen(c->content) + 1;
					/* space available is pos.x to x1 */
					if (x1 - pos.x < data->width) {
						/* insufficient space: start new line */
						y = pos.y;
						pos.x = y < float_left.y ? float_left.x : 0;
						line = c;
					}
					data->x = pos.x;
					data->y = y;
					pos.x += data->width;
					pos.y = y + 2;
				}
				break;
		}
		while (next == 0 && c->parent != e)
			/* TODO: fill coords of just finished inline element [1] */
			c = c->parent, next = c->next;
		c = next;
	}
	if (inline_mode) y = pos.y;
	return y;
}


/******************************************************************************/


void render_plain_element(char *g, xmlNode *e, unsigned long x, unsigned long y)
{
	unsigned long i;
	unsigned int l;
	xmlNode *c;
	struct data * data = e->_private;

	for (c = e->children; c != 0; c = c->next)
		render_plain_element(g, c, x + data->x, y + data->y);

	if (data == 0) return;

//	printf("render_plain_element: x0 %li y0 %li x1 %li y1 %li\n", data->x0, data->y0, data->x1, data->y1);

	for (i = (y + data->y) + 1; i < (y + data->y + data->height); i++) {
		g[80 * i + (x + data->x)] = '|';
		g[80 * i + (x + data->x + data->width)] = '|';
	}

//	if (e->style->display != INLINE) {
		for (i = (x + data->x); i < (x + data->x + data->width); i++) {
			g[80 * (y + data->y) + i] = '-';
			g[80 * (y + data->y + data->height) + i] = '-';
		}
		g[80 * (y + data->y) + (x + data->x)] = '+';
		g[80 * (y + data->y) + (x + data->x + data->width)] = '+';
		g[80 * (y + data->y + data->height) + (x + data->x)] = '+';
		g[80 * (y + data->y + data->height) + (x + data->x + data->width)] = '+';
//	}

	if (e->type == XML_TEXT_NODE && e->content) {
		l = strlen(e->content);
		if ((x + data->x + data->width) - (x + data->x) - 1 < l)
			l = (x + data->x + data->width) - (x + data->x) - 1;
		strncpy(g + 80 * ((y + data->y) + 1) + (x + data->x) + 1, e->content, l);
	}
}


void render_plain(xmlNode *doc)
{
	int i;
	char *g;

	g = calloc(10000, 1);
	if (g == 0) exit(1);

        for (i = 0; i < 10000; i++)
		g[i] = ' ';

	render_plain_element(g, doc, 0, 0);

	for (i = 0; i < 40; i++)
		printf("%.80s\n", g + (80 * i));
}


/******************************************************************************/


void walk(xmlNode *n, unsigned int depth)
{
	xmlNode *c;
	xmlAttr *a;
	struct data * data;
	unsigned int i;

	for (i = 0; i < depth; i++)
		printf("  ");

	data = n->_private;

	switch (n->type) {
		case XML_ELEMENT_NODE:
			if (data == 0)
				printf("ELEMENT %s", n->name);
			else
				printf("ELEMENT %s [%li %li %li*%li]", n->name, data->x,
							data->y, data->width, data->height);
		/*	for (a = n->properties; a != 0; a = a->next) {
				assert(a->type == XML_ATTRIBUTE_NODE);
				printf(" %s='", a->name);
				for (c = a->children; c != 0; c = c->next)
					walk(c);
				printf("'");
			}*/
			printf("\n");
			for (c = n->children; c != 0; c = c->next)
				walk(c, depth + 1);
//			printf("</%s>", n->name);
			break;

		case XML_TEXT_NODE:
			if (data == 0)
				printf("TEXT '%s'\n", n->content);
			else
				printf("TEXT [%li %li %li*%li] '%s'\n", data->x, data->y,
							data->width, data->height, n->content);
			break;

		default:
			printf("UNHANDLED\n");
			break;
	}
}


/**
 * add a child to a box tree node
 */

void box_add_child(struct box * parent, struct box * child)
{
	if (parent->children)	/* has children already */
		parent->last->next = child;
	else			/* this is the first child */
		parent->children = child;

	parent->last = child;
	child->parent = parent;
}


/**
 * make a box tree with style data from an xml tree
 *
 * arguments:
 * 	n		xml tree
 * 	parent_style	style at this point in xml tree
 * 	stylesheet	stylesheet to use
 * 	selector	element selector hierachy to this point
 * 	depth		depth in xml tree
 * 	parent		parent in box tree
 * 	inline_container	current inline container box, or 0
 *
 * returns:
 * 	updated current inline container
 */

struct box * xml_to_box(xmlNode * n, struct css_style * parent_style, struct css_stylesheet * stylesheet,
		struct css_selector ** selector, unsigned int depth,
		struct box * parent, struct box * inline_container)
{
	struct box * box;
	struct box * inline_container_c;
	struct css_style * style;
	xmlNode * c;

	if (n->type == XML_ELEMENT_NODE) {
		/* work out the style for this element */
		*selector = xrealloc(*selector, (depth + 1) * sizeof(struct css_selector));
		(*selector)[depth].element = n->name;
		(*selector)[depth].class = (*selector)[depth].id = 0;

		style = xcalloc(1, sizeof(struct css_style));
		memcpy(style, parent_style, sizeof(struct css_style));
		css_get_style(stylesheet, *selector, depth + 1, style);

		switch (style->display) {
			case CSS_DISPLAY_BLOCK:  /* blocks get a node in the box tree */
				box = xcalloc(1, sizeof(struct box));
				box->node = n;
				box->type = BOX_BLOCK;
				box->style = style;
				box_add_child(parent, box);
				inline_container_c = 0;
				for (c = n->children; c != 0; c = c->next)
					inline_container_c = xml_to_box(c, style, stylesheet,
							selector, depth + 1, box, inline_container_c);
				inline_container = 0;
				break;
			case CSS_DISPLAY_INLINE:  /* inline elements get no box, but their children do */
				for (c = n->children; c != 0; c = c->next)
					inline_container = xml_to_box(c, style, stylesheet,
							selector, depth + 1, box, inline_container);
				break;
			case CSS_DISPLAY_NONE:
			default:
		}
	} else if (n->type == XML_TEXT_NODE) {
		/* text nodes are converted to inline boxes, wrapped in an inline container block */
		if (inline_container == 0) {  /* this is the first inline node: make a container */
			inline_container = xcalloc(1, sizeof(struct box));
			inline_container->type = BOX_INLINE_CONTAINER;
			box_add_child(parent, inline_container);
		}
		box = calloc(1, sizeof(struct box));
		box->node = n;
		box->type = BOX_INLINE;
		box_add_child(inline_container, box);
	}

	return inline_container;
}


/*
 * print a box tree to standard output
 */

void box_dump(struct box * box, unsigned int depth)
{
	unsigned int i;
	struct box * c;
	
	for (i = 0; i < depth; i++)
		printf("  ");

	switch (box->type) {
		case BOX_BLOCK:            printf("BOX_BLOCK <%s>\n", box->node->name); break;
		case BOX_INLINE_CONTAINER: printf("BOX_INLINE_CONTAINER\n"); break;
		case BOX_INLINE:           printf("BOX_INLINE '%s'\n", box->node->content); break;
		default:                   printf("Unknown box type\n");
	}
	
	for (c = box->children; c != 0; c = c->next)
		box_dump(c, depth + 1);
}


int main(int argc, char *argv[])
{
/* 	struct layout canvas = { 0, 0, 79, 0 }; */

	struct css_stylesheet * stylesheet;
	struct css_style * style = xcalloc(1, sizeof(struct css_style));
	struct css_selector * selector = xcalloc(1, sizeof(struct css_selector));
	xmlNode * c;
	xmlDoc * doc;
	struct box * doc_box = xcalloc(1, sizeof(struct box));

	if (argc < 3) die("usage: render htmlfile cssfile");
	
	doc = htmlParseFile(argv[1], 0);
	if (doc == 0) die("htmlParseFile failed");

	for (c = doc->children; c != 0 && c->type != XML_ELEMENT_NODE; c = c->next)
		;
	if (c == 0) die("no element in document");
	if (strcmp(c->name, "html")) die("document is not html");

	stylesheet = css_new_stylesheet();
	css_parse_stylesheet(stylesheet, load(argv[2]));

	doc_box->type = BOX_BLOCK;
	doc_box->node = c;
	xml_to_box(c, style, stylesheet, &selector, 0, doc_box, 0);
	box_dump(doc_box->children, 0);

/*	walk(c, 0);
	layout_element(c, 79);
	walk(c, 0);
	printf("\n\n");
	render_plain(c);*/

	return 0;
}


/******************************************************************************/