summaryrefslogtreecommitdiff
path: root/render.c
blob: 65976d1102888c89bdf29cabb69143319f7ff28e (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
// Compile with: gcc render.c -I/usr/local/include/cairo -L/usr/local/lib/ -lcairo -odggs-render
#include <stdio.h>
#include <ctype.h>
#include <string.h>
#include <cairo/cairo.h>
#include <stdlib.h>

/** Indental **/
struct indental {
	char *key, *value, *txt;
	int indent;
	struct indental *next, *children;
};

void parse_indental_line(char *line, struct indental *entry) {
	entry->txt = line; // For deallocator.

	// parse indentation
	entry->indent = 0;
	while (isspace(*line)) line++, entry->indent++;

	entry->key = strtok(line, ":"); // parse key
	entry->value = strtok(NULL, "\n"); // parse value

	// Drop excess space
	strtok(entry->key, " \t\r\n");
	if (entry->value != NULL) while (isspace(*entry->value)) entry->value++;
}

void free_indental_stack(struct indental*);
struct indental *parse_indental(char *filename) {
	FILE *file = fopen(filename, "r");
	struct indental *stack = NULL, // next is temporarily a back pointer.
		*ret = NULL; // Track the root to be returned.

	char *line = NULL;
	size_t line_len = 0;
	int lineno = 0;
	while (getline(&line, &line_len, file) != -1) {
		char *l = line;
		// Skip comments & blank lines
		while (isspace(*l)) l++;
		if (*l == ';' || *l == '#' || *l == '\0') {
			lineno++;
			continue;
		}

		// Parse the line into newly allocated memory.
		struct indental *entry = malloc(sizeof (struct indental));
		if (entry == NULL) {
			fprintf(stderr, "Out of memory!\n");
			free_indental_stack(stack);
			return NULL;
		}
		parse_indental_line(line, entry);
		line = NULL; line_len = 0; // Read memory claimed by `entry`.

		// Insert into appropriate parent entry
		if (stack == NULL) stack = ret = entry;
		else if (entry->indent > stack->indent) {
			// New level of indentation, push new parent to stack
			stack->children = entry;
			entry->next = stack;
			stack = entry;
		} else {
			// Find ancestor at appropriate indentation level
			while (stack != NULL && entry->indent < stack->indent) {
				struct indental *parent = stack->next;
				stack->next = NULL;
				stack = parent;
			}
			if (stack == NULL || entry->indent > stack->indent) {
				fprintf(stderr, "Incorrect indent %i on %s:%i\n", entry->indent, filename, lineno);
				free_indental_stack(stack);
				return NULL;
			}

			// Insert it there
			entry->next = stack->next;
			stack->next = entry;
			stack = entry;
		}
	}

	// Tidyup tree
	fclose(file);
	while (stack->next != NULL) {
		struct indental *parent = stack->next;
		stack->next = NULL;
		stack = parent;
	}
	return ret; // The above would give us the root, unless the file has multiple.
}

void free_indental(struct indental *data) {
	if (data == NULL) return;

	if (data->txt != NULL) free(data->txt);
	free_indental(data->next);
	free_indental(data->children);
	free(data);
}

void free_indental_stack(struct indental *stack) {
	if (stack == NULL) return;

	while (stack->next != NULL) {
		struct indental *parent = stack->next;
		stack->next = NULL;
		stack = parent;
	}
	free_indental(stack);
}

/** Trie **/
struct attr {
	char *key, *value;
	struct attr *next;
};
struct trie {
	char label;
	int present : 1;
	int child_present : 1;
	struct trie *first_child;
	struct trie *next;
	struct attr *attrs;
};

struct trie init_trie() {
	struct trie ret = {
		.label = '\0',
		.present = 0,
		.child_present = 0,
		.first_child = NULL,
		.next = NULL,
		.attrs = NULL
	};
	return ret;
}
struct trie *insert_trie(struct trie *root, char *key) {
	while (root->first_child != NULL && *key != '\0') { // For each character
		root->child_present = 1; // Flag for rendering usage, when hitting depth count

		// Find the appropriate child to store the key under
		root = root->first_child;
		while (root->label != *key) {
			if (root->next == NULL) {
				// If that child didn't exist, create it
				struct trie *next = malloc(sizeof(struct trie));
				next->label = *key;
				next->present = 0;
				next->child_present = 1;
				next->next = NULL;
				next->first_child = NULL;

				root->next = next;
			}
			root = root->next;
		}
		key++; // Iterate to next character
	}

	// Create all necessary child nodes
	while (*key != '\0' && !isspace(*key)) {
		struct trie *child = malloc(sizeof(struct trie));
		child->label = *key;
		child->present = 0;
		child->child_present = 1;
		child->first_child = NULL;
		child->next = NULL;

		root->first_child = child;
		root = child;
		key++;
	}

	root->present = 1; // Indicates this value is in the set
	return root;
}
char *strclone(char *in) {
	if (in == NULL) in = "";

	char *ret = malloc(strlen(in) + 1);
	strcpy(ret, in);
	return ret;
}
struct attr *indental_to_attr(struct indental *entry, struct trie *dest) {
	struct attr *ret = malloc(sizeof (struct attr));
	ret->key = strclone(entry->key); ret->value = strclone(entry->value);

	ret->next = dest->attrs; dest->attrs = ret;
	return ret;
}
struct trie parse_trie(struct indental *file) {
	struct trie ret = init_trie();

	// Parse root-level attrs
	for (struct indental *iter = file->children; iter != NULL; iter = iter->next) {
		if (iter->value != NULL) indental_to_attr(iter, &ret);
	}
	// Parse component zone IDs
	for (struct indental *iter = file->children; iter != NULL; iter = iter->next) {
		if (iter->value == NULL) {
			struct trie *leaf = insert_trie(&ret, iter->key); // Found one, add to trie

			// Parse attrs
			leaf->attrs = ret.attrs; // Include root attrs
			for (struct indental *child = iter->children; child != NULL; child = child->next) {
				indental_to_attr(iter, leaf);
			}
		}
	}

	return ret;
}

struct trie *lookup_trie(struct trie *root, char *key) {
	// Validate inputs
	if (root == NULL) return NULL;
	if (root->first_child == NULL) return NULL;
	root = root->first_child; // Root node is just a container to add to

	while (*key != '\0') { // Iterate over key.
		while (root->label != *key) { // Find the correct child
			if (root->next == NULL) return NULL; // It didn't exist
			root = root->next;
		}

		// Iterate to next character
		key++;
		if (*key == '\0') return root;

		// Iterate to next node
		if (root->first_child == NULL) return root;
		root = root->first_child;
	}
	return root; // Unreachable!
}

void free_trie(struct trie *root) {
	if (root == NULL) return;
	free_trie(root->first_child);
	free_trie(root->next);

	struct attr *attr = NULL, *next = root->attrs;
	while ((attr = next) != NULL) {
		next = attr->next;
		if (attr->key != NULL) free(attr->key);
		if (attr->value != NULL) free(attr->value);
		free(attr);
	}

	free(root);
}

/** Rendering **/
float get_attrf(char *name, struct trie *node) {
	// For each attr
	for (struct attr *iter = node->attrs; iter != NULL; iter = iter->next) {
		if (strcmp(name, iter->key) != 0) continue; // It's not the one

		// It is the one, parse and return it's value
		float ret;
		sscanf(iter->value, "%f", &ret);
		return ret;
	}
	return 0.0;
}
void render_trie(cairo_t *target, struct trie *trie, int depth,
		float x, float y, float width, float height) {
	if (trie == NULL || target == NULL) return; // Invalid
	if (trie->present) { // Make sure root gets a chance to render
		cairo_set_source_rgba(target,
				get_attrf("red", trie), get_attrf("green", trie), get_attrf("blue", trie), get_attrf("alpha", trie));
		cairo_rectangle(target, x, y, width, height);
		cairo_fill(target);
	}
	trie = trie->first_child;
	if (trie == NULL) return;

	float subwidth = width/3, subheight = height/3; // Sizing info

	for (; trie != NULL; trie = trie->next) { // Foreach child
		// Compute it's row & column
		int label = trie->label - '0';
		int row = label / 3;
		int column = label % 3;

		if ((trie->child_present && depth == 0) || trie->present) {
			// Fill present children, or an ancestor if needed
			cairo_set_source_rgba(target,
					get_attrf("red", trie), get_attrf("green", trie), get_attrf("blue", trie), get_attrf("alpha", trie));
			cairo_rectangle(target, x + column*subwidth, y + row*subheight, subwidth, subheight);
			cairo_fill(target);
		} else if (trie->child_present) {
			// More data to render, recurse!
			render_trie(target, trie, depth-1,
					x + column*subwidth, y + row*subheight, subwidth, subheight);
		}
	}
}

/* Main program */
float WIDTH = 1024, HEIGHT = 1024;
int main(int argc, char **argv) {
	// Validate args
	if (argc < 3) {
		fprintf(stderr, "USAGE: %s CELLID FILE [OUTFILE] [FILES..]\n", argv[0]);
		return 1;
	}

	// Create target surface
	cairo_surface_t *surface = cairo_image_surface_create(CAIRO_FORMAT_ARGB32, WIDTH, HEIGHT);
	cairo_t *cr = cairo_create(surface);

	// Construct specified trie
	struct indental *file = parse_indental(argv[2]);
	for (struct indental *layer = file; layer != NULL; layer = layer->next) {
		struct trie geom = parse_trie(layer);
		struct trie *view = lookup_trie(&geom, argv[1]);
		int depth = 5;

		// Render the trie
		cairo_set_source_rgb(cr, 0, 0, 0);
		render_trie(cr, view, depth, 0, 0, WIDTH, HEIGHT);
	}
	free_indental(file);

	// Render overlays
	for (int i = 4; i < argc; i++) {
		file = parse_indental(argv[i]);
		for (struct indental *layer = file; layer != NULL; layer = layer->next) {
			struct trie geom = parse_trie(layer);
			struct trie *view = lookup_trie(&geom, argv[1]);
			cairo_set_source_rgb(cr, 0, 0, 0);
			render_trie(cr, view, 5, 0, 0, WIDTH, HEIGHT);
		}
		free_indental(file);
	}

	// Output the trie
	cairo_surface_write_to_png(surface, argc == 4 ? argv[3] : "out.png");
	cairo_destroy(cr);
	cairo_surface_destroy(surface);
	return 0;
}