summaryrefslogtreecommitdiff
path: root/kicad/pl-parse.c
blob: 7a09dd8376d1e83abf1d41e2b1379b93a8dcc479 (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
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
/*
 * kicad/pl-parse.c - KiCad page layout parser
 *
 * Written 2016 by Werner Almesberger
 * Copyright 2016 by Werner Almesberger
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 */


#include <stdbool.h>
#include <stdlib.h>
#include <string.h>

#include "misc/util.h"
#include "misc/diag.h"
#include "gfx/text.h"
#include "file/file.h"
#include "kicad/kicad.h"
#include "kicad/sexpr.h"
#include "kicad/pl-common.h"
#include "kicad/pl.h"


static const char *builtin_template_paths[] = {
	DEFAULT_TEMPLATE_PATHS
};


static bool get_coord(const struct expr *e,
    float *x, float *y, int *dx, int *dy)
{
	unsigned n = 0;

	*dx = -1;
	*dy = -1;
	for (; e; e = e->next) {
		if (e->e) {
			warning("coord: ignoring list");
			continue;
		}

		if (!strcmp(e->s, "ltcorner")) {
			*dx = *dy = 1;
		} else if (!strcmp(e->s, "lbcorner")) {
			*dx = 1;
			*dy = -1;
		} else if (!strcmp(e->s, "rtcorner")) {
			*dx = -1;
			*dy = 1;
		} else if (!strcmp(e->s, "rbcorner")) {
			*dx = *dy = -1;
		} else {
			char *end;
			float f = strtof(e->s, &end);

			if (*end) {
				error("coord: not a number \"%s\"", e->s);
				return 0;
			}
			if (n++)
				*y = f;
			else
				*x = f;
		}
	}

	switch (n) {
	case 0:
	case 1:
		error("coord: not enough coordinates");
		return 0;
	case 2:
		return 1;
	default:
		error("coord: too many coordinates");
		return 0;
	}
}


static bool get_xy(const struct expr *e, float *x, float *y)
{
	unsigned n = 0;

	for (; e; e = e->next) {
		char *end;
		float f;

		if (e->e) {
			warning("size: ignoring list");
			continue;
		}

		f = strtof(e->s, &end);
		if (*end) {
			error("size: not a number \"%s\"", e->s);
			return 0;
		}
		if (n++)
			*y = f;
		else
			*x = f;
	}

	switch (n) {
	case 0:
	case 1:
		error("size: not enough coordinates");
		return 0;
	case 2:
		return 1;
	default:
		error("size: too many coordinates");
		return 0;
	}
}


static bool get_float(const struct expr *e, float *f)
{
	char *end;

	for (; e; e = e->next)
		if (e->s) {
			*f = strtod(e->s, &end);
			if (!*end)
				return 1;
			error("invalid number \"%s\"", e->s);
			return 0;
		}
	error("no number found");
	return 0;
}



static bool get_int(const struct expr *e, int *n)
{
	long tmp;
	char *end;

	for (; e; e = e->next)
		if (e->s) {
			tmp = strtol(e->s, &end, 0);
			if (!*end && (int) tmp == tmp) {
				*n = (int) tmp;
				return 1;
			}
			error("invalid number \"%s\"", e->s);
			return 0;
		}
	error("no number foundn");
	return 0;
}


static bool process_setup(struct pl_ctx *pl, const struct expr *e)
{
	const char *s;
	const struct expr *next;

	for (; e; e = e->next) {
		if (!e->e) {
			warning("setup: ignoring non-list");
			continue;
		}

		s = e->e->s;
		next = e->e->next;

		if (!strcmp(s, "comment"))
			continue;

		if (!strcmp(s, "textsize")) {
			if (!get_xy(next, &pl->tx, &pl->ty))
				return 0;
		} else if (!strcmp(s, "linewidth")) {
			// meh
		} else if (!strcmp(s, "textlinewidth")) {
			// meh
		} else if (!strcmp(s, "left_margin")) {
			if (!get_float(next, &pl->l))
				return 0;
		} else if (!strcmp(s, "right_margin")) {
			if (!get_float(next, &pl->r))
				return 0;
		} else if (!strcmp(s, "top_margin")) {
			if (!get_float(next, &pl->t))
				return 0;
		} else if (!strcmp(s, "bottom_margin")) {
			if (!get_float(next, &pl->b))
				return 0;
		} else {
			warning("setup: ignoring \"%s\"", s);
		}
	}
	return 1;
}


static bool process_font(struct pl_obj *obj, const struct expr *e)
{
	const char *s;
	const struct expr *next;

	for (; e; e = e->next) {
		if (e->s) {
			if (!strcmp(e->s, "bold"))
				obj->font |= font_bold;
			else if (!strcmp(e->s, "italic"))
				obj->font |= font_italic;
			else
				warning("font: ignoring \"%s\"", e->s);
			continue;
		}

		if (!e->e) {
			warning("font: ignoring empty list");
			continue;
		}
		s = e->e->s;
		next = e->e->next;

		if (!strcmp(s, "comment"))
			continue;

		if (!strcmp(s, "size")) {
			if (!get_xy(next, &obj->ex, &obj->ey))
				return 0;
		} else {
			warning("font: ignoring \"%s\"", s);
		}
	}
	return 1;
}


static bool process_justify(struct pl_obj *obj, const struct expr *e)
{
	for (; e; e = e->next) {
		if (e->e) {
			warning("justify: ignoring list");
			continue;
		}

		if (!strcmp(e->s, "center"))
			obj->hor = obj->vert = text_mid;
		else if (!strcmp(e->s, "left"))
			obj->hor = text_min;
		else if (!strcmp(e->s, "right"))
			obj->hor = text_max;
		else if (!strcmp(e->s, "top"))
			obj->vert = text_max;
		else if (!strcmp(e->s, "bottom"))
			obj->vert = text_min;
		else
			warning("justify: ignoring \"%s\"", e->s);
	}
	return 1;
}


static bool process_option(struct pl_obj *obj, const struct expr *e)
{
	for (; e; e = e->next) {
		if (e->e) {
			warning("option: ignoring list");
			continue;
		}

		if (!strcmp(e->s, "page1only"))
			obj->pc = pc_only_one;
		else if (!strcmp(e->s, "notonpage1"))
			obj->pc = pc_except_one;
		else
			warning("option: ignoring \"%s\"", e->s);
	}
	return 1;
}


static bool process_poly(struct pl_obj *obj, const struct expr *e)
{
	struct pl_poly *poly;
	struct pl_point *p, **anchor;
	const char *s;
	const struct expr *next;

	poly = alloc_type(struct pl_poly);
	poly->points = NULL;
	poly->next = obj->poly;
	obj->poly = poly;
	anchor = &poly->points;

	for (; e; e = e->next) {
		if (!e->e) {
			warning("pts: ignoring non-list");
			continue;
		}

		s = e->e->s;
		next = e->e->next;

		if (!strcmp(s, "xy")) {
			p = alloc_type(struct pl_point);
			*anchor = p;
			anchor = &p->next;
			if (!get_xy(next, &p->x, &p->y))
				return 0;
		} else {
			warning("pts: ignoring \"%s\"", s);
		}
	}

	*anchor = NULL;

	return 1;
}


static bool process_obj(struct pl_ctx *pl, const struct expr *e,
    enum pl_obj_type type)
{
	struct pl_obj *obj;
	const char *s;
	const struct expr *next;

	obj = alloc_type(struct pl_obj);
	obj->type = type;
	obj->pc = pc_none;
	obj->s = NULL;
	obj->repeat = 1;
	obj->x = obj->y = obj->ex = obj->ey = 0;
	obj->dx = obj->dy = obj->edx = obj->edy = 0;
	obj->incrx = 0;
	obj->incry = 0;
	obj->incrlabel = 0;
	obj->font = 0;
	obj->hor = text_min;
	obj->vert = text_mid;
	obj->rotate = 0;
	obj->poly = 0;

	for (; e; e = e->next) {
		if (e->s) {
			if (obj->s) {
				error("pl_obj: multiple strings");
				return 0;
			}
			obj->s = stralloc(e->s);
			continue;
		}
		if (!e->e) {
			warning("pl_obj: ignoring empty list");
			continue;
		}

		s = e->e->s;
		next = e->e->next;

		if (!strcmp(s, "comment"))
			continue;
		if (!strcmp(s, "name"))
			continue;

		if (!strcmp(s, "linewidth")) {
			// meh
		} else if (!strcmp(s, "start") || !strcmp(s, "pos")) {
			if (!get_coord(next, &obj->x, &obj->y,
			    &obj->dx, &obj->dy))
				return 0;
		} else if (!strcmp(s, "end")) {
			if (!get_coord(next, &obj->ex, &obj->ey,
			    &obj->edx, &obj->edy))
				return 0;
		} else if (!strcmp(s, "repeat")) {
			if (!get_int(next, &obj->repeat))
				return 0;
		} else if (!strcmp(s, "incrx")) {
			if (!get_float(next, &obj->incrx))
				return 0;
		} else if (!strcmp(s, "incry")) {
			if (!get_float(next, &obj->incry))
				return 0;
		} else if (!strcmp(s, "incrlabel")) {
			if (!get_int(next, &obj->incrlabel))
				return 0;
		} else if (!strcmp(s, "font")) {
			if (!process_font(obj, next))
				return 0;
		} else if (!strcmp(s, "justify")) {
			if (!process_justify(obj, next))
				return 0;
		} else if (!strcmp(s, "rotate")) {
			if (!get_float(next, &obj->rotate))
				return 0;
		} else if (!strcmp(s, "option")) {
			if (!process_option(obj, next))
				return 0;
		} else if (!strcmp(s, "pts")) {
			if (!process_poly(obj, next))
				return 0;
		} else
			warning("pl_obj: ignoring \"%s\"", s);
	}

	obj->next = pl->objs;
	pl->objs = obj;

	return 1;
}


static bool process_layout(struct pl_ctx *pl, const struct expr *e)
{
	const char *s;
	const struct expr *next;

	for (; e; e = e->next) {
		if (!e->e) {
			warning("layout: ignoring non-list");
			continue;
		}

		s = e->e->s;
		next = e->e->next;

		if (!strcmp(s, "comment"))
			continue;
		if (!strcmp(s, "setup")) {
			if (!process_setup(pl, next))
				return 0;
		} else if (!strcmp(s, "rect")) {
			if (!process_obj(pl, next, pl_obj_rect))
				return 0;
		} else if (!strcmp(s, "line")) {
			if (!process_obj(pl, next, pl_obj_line))
				return 0;
		} else if (!strcmp(s, "tbtext")) {
			if (!process_obj(pl, next, pl_obj_text))
				return 0;
		} else if (!strcmp(s, "polygon")) {
			if (!process_obj(pl, next, pl_obj_poly))
				return 0;
		} else {
			warning("layout: ignoring \"%s\"", s);
		}
	}
	return 1;
}


static bool process(struct pl_ctx *p, const struct expr *e)
{
	while (e) {
		if (e->e && e->e->s && !strcmp(e->e->s, "page_layout") &&
		    e->e->next && e->e->next->e)
			return process_layout(p, e->e->next);
		e = e->next;
	}
	error("no layout information found");
	return 0;
}


static bool pl_parse_line(const struct file *file,
    void *user, const char *line)
{
	struct pl_ctx *pl = user;

	return sexpr_parse(pl->sexpr_ctx, line);
}


static struct pl_ctx *new_pl_ctx(void)
{
	struct pl_ctx *pl;

	pl = alloc_type(struct pl_ctx);
	pl->sexpr_ctx = sexpr_new();
	pl->l = pl->r = pl->t = pl->b = 0;
	pl->tx = pl->ty = 0;
	pl->objs = NULL;
	return pl;
}


struct pl_ctx *pl_parse(struct file *file)
{
	struct pl_ctx *pl = new_pl_ctx();
	struct expr *e = NULL;

	if (!file_read(file, pl_parse_line, pl)) {
		sexpr_abort(pl->sexpr_ctx);
		goto fail;
	}
	if (!sexpr_finish(pl->sexpr_ctx, &e))
		goto fail;
	if (!process(pl, e))
		goto fail;
	free_expr(e);
	return pl;

fail:
	free_expr(e);
	free(pl);
	return 0;
}


static bool pl_find_file(struct file *file, const char *name,
    const struct file *related)
{
	if (file_open(file, name, related))
		return 1;
	return file_search(file, name,
	    builtin_template_paths, ARRAY_ELEMENTS(builtin_template_paths),
	    related);

}


static struct pl_ctx *default_pl(void)
{
#include "../pl-default.inc"
	struct pl_ctx *pl = new_pl_ctx();
	struct expr *e = NULL;

	if (!sexpr_parse(pl->sexpr_ctx, defaultPageLayout) ||
	    !sexpr_finish(pl->sexpr_ctx, &e) ||
	    !process(pl, e))
		BUG("cannot parse default layout");
	free_expr(e);
	return pl;
}


struct pl_ctx *pl_parse_search(const char *name, const struct file *related)
{
	struct file file;
	struct pl_ctx *pl;
	bool res;

	if (name) {
		diag_defer_begin();
		res = pl_find_file(&file, name, related);
		diag_defer_end(!res);
		if (!res)
			return NULL;
		pl = pl_parse(&file);
		file_close(&file);
		return pl;
	}

	return default_pl();
}


static void free_polys(struct pl_poly *poly)
{
	struct pl_poly *next_poly;
	struct pl_point *p, *next_point;

	while (poly) {
		next_poly = poly->next;
		for (p = poly->points; p; p = next_point) {
			next_point = p->next;
			free(p);
		}
		free(poly);
		poly = next_poly;
	}
}


void pl_free(struct pl_ctx *pl)
{
	struct pl_obj *next;

	while (pl->objs) {
		next = pl->objs->next;
		free((void *) pl->objs->s);
		free_polys(pl->objs->poly);
		free(pl->objs);
		pl->objs = next;
	}
	free(pl);
}