summaryrefslogtreecommitdiff
path: root/kicad/lib-render.c
blob: 079d776cd5bd453dbb9381a0407956dbd128609e (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
/*
 * kicad/lib-render.c - Render component from library
 *
 * 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 <stdlib.h>
#include <stdio.h>
#include <assert.h>

#include "util.h"
#include "diag.h"
#include "misc.h"
#include "style.h"
#include "gfx.h"
#include "text.h"
#include "kicad/sch.h"
#include "kicad/lib.h"


/* ----- Drawing ----------------------------------------------------------- */


static void draw_poly(const struct lib_poly *poly, const int m[6])
{
	int n = poly->points;
	int x[n];
	int y[n];
	int i;

	for (i = 0; i != n; i++) {
		x[i] = mx(poly->x[i], poly->y[i], m);
		y[i] = my(poly->x[i], poly->y[i], m);
	}

	gfx_poly(n, x, y, COLOR_COMP_DWG, COLOR_NONE, LAYER_COMP_DWG);

	switch (poly->fill) {
	case 'N':
		break;
	case 'F':
		gfx_poly(n, x, y, COLOR_NONE, COLOR_COMP_DWG,
		    LAYER_COMP_DWG_BG);
		break;
	case 'f':
		gfx_poly(n, x, y, COLOR_NONE, COLOR_COMP_DWG_BG,
		    LAYER_COMP_DWG_BG);
		break;
	default:
		abort();
	}
}


static void draw_rect(const struct lib_rect *rect, const int m[6])
{
	int sx = mx(rect->sx, rect->sy, m);
	int sy = my(rect->sx, rect->sy, m);
	int ex = mx(rect->ex, rect->ey, m);
	int ey = my(rect->ex, rect->ey, m);

	gfx_rect(sx, sy, ex, ey, COLOR_COMP_DWG, COLOR_NONE, LAYER_COMP_DWG);

	switch (rect->fill) {
	case 'N':
		break;
	case 'F':
		gfx_rect(sx, sy, ex, ey, COLOR_NONE, COLOR_COMP_DWG,
		    LAYER_COMP_DWG_BG);
		break;
	case 'f':
		gfx_rect(sx, sy, ex, ey, COLOR_NONE, COLOR_COMP_DWG_BG,
		    LAYER_COMP_DWG_BG);
		break;
	default:
		abort();
	}
}


static void draw_circ(const struct lib_circ *circ, const int m[6])
{
	int x = mx(circ->x, circ->y, m);
	int y = my(circ->x, circ->y, m);
	int r = circ->r;

	gfx_circ(x, y, r, COLOR_COMP_DWG, COLOR_NONE, LAYER_COMP_DWG);

	switch (circ->fill) {
	case 'N':
		break;
	case 'F':
		gfx_circ(x, y, r, COLOR_NONE, COLOR_COMP_DWG,
		    LAYER_COMP_DWG_BG);
		break;
	case 'f':
		gfx_circ(x, y, r, COLOR_NONE, COLOR_COMP_DWG_BG,
		    LAYER_COMP_DWG_BG);
		break;
	default:
		abort();
	}
}


static void draw_arc(const struct lib_arc *arc, const int m[6])
{
	int a = matrix_to_angle(m);
	int x = mx(arc->x, arc->y, m);
	int y = my(arc->x, arc->y, m);
	int sa = angle_add(arc->start_a, a);
	int ea = angle_add(arc->end_a, a);

	if (matrix_is_mirrored(m)) {
		sa = 180 - sa;
		ea = 180 - ea;
		while (ea < sa)
			ea += 360;
		while (ea - sa > 360)
			ea -= 360;
		if (ea - sa >= 180) {
			swap(sa, ea);
			sa += 360;
		}
	}

	/*
	 * cr_arc (and maybe others) close the arc if filling, so we supply a
	 * foreground color as well. Other objects are closed and don't need
	 * need a foreground color when filling.
	 */
	switch (arc->fill) {
	case 'N':
		break;
	case 'F':
		gfx_arc(x, y, arc->r, sa, ea,
		    COLOR_COMP_DWG, COLOR_COMP_DWG, LAYER_COMP_DWG_BG);
		break;
	case 'f':
		gfx_arc(x, y, arc->r, sa, ea,
		    COLOR_COMP_DWG_BG, COLOR_COMP_DWG_BG, LAYER_COMP_DWG_BG);
		break;
	default:
		assert(0);
	}

	gfx_arc(x, y, arc->r, sa, ea,
	    COLOR_COMP_DWG, COLOR_NONE, LAYER_COMP_DWG);
}


static void draw_pin_name(const struct comp *comp, const struct lib_pin *pin,
    const int m[6], int dx, int dy, int rot, enum text_align hor)
{
	int ox, oy, sx, sy;

	if (!strcmp(pin->name, "~"))
		return;

	if (comp->name_offset) {
		ox = dx * (pin->length + comp->name_offset);
		oy = dy * (pin->length + comp->name_offset);
		sx = sy = 0;
	} else {
		ox = dx * pin->length / 2;
		oy = dy * pin->length / 2;
		sx = mxr(-dy * PIN_NUM_OFFSET, dx * PIN_NUM_OFFSET, m);
		sy = myr(-dy * PIN_NUM_OFFSET, dx * PIN_NUM_OFFSET, m);
		if (sx > 0)
			sx = -sx;
		if (sy > 0)
			sy = -sy;
	}

	struct text txt = {
		.s = pin->name,
		.x = mx(pin->x + ox, pin->y + oy, m) + sx,
		.y = my(pin->x + ox, pin->y + oy, m) + sy,
		.size = pin->name_size,
		.rot = rot,
		.hor = comp->name_offset ? hor : text_mid,
		.vert = comp->name_offset ? text_mid : text_min,
	};

	text_rot(&txt, matrix_to_angle(m));
	if (matrix_is_mirrored(m)) {
		if ((txt.rot % 180) == 0)
			txt.hor = text_flip(txt.hor);
		else
			txt.vert = text_flip(txt.vert);
	}

	switch (txt.rot) {
	case 180:
	case 270:
		text_flip_x(&txt);
		break;
	default:
		break;
	}

	text_fig(&txt, COLOR_PIN_NAME, LAYER_PIN_NAME);
}


static void draw_pin_num(const struct comp *comp, const struct lib_pin *pin,
    const int m[6], int dx, int dy, int rot, enum text_align hor)
{
	int ox, oy, sx, sy;

	ox = dx * pin->length / 2;
	oy = dy * pin->length / 2;

	sx = mxr(-dy * PIN_NUM_OFFSET, dx * PIN_NUM_OFFSET, m);
	sy = myr(-dy * PIN_NUM_OFFSET, dx * PIN_NUM_OFFSET, m);
	if (sx > 0)
		sx = -sx;
	if (sy > 0)
		sy = -sy;

	if (!comp->name_offset) {
		sx = -sx;
		sy = -sy;
	}

	struct text txt = {
		.s = pin->number,
		.x = mx(pin->x + ox, pin->y + oy, m) + sx,
		.y = my(pin->x + ox, pin->y + oy, m) + sy,
		.size = pin->number_size,
		.rot = rot,
		.hor = text_mid,
		.vert = comp->name_offset ? text_min : text_max,
	};

	text_rot(&txt, matrix_to_angle(m) % 180);
	if (matrix_is_mirrored(m)) {
		switch (txt.rot) {
		case 0:
			txt.hor = text_flip(txt.hor);
			break;
		case 90:
			break;
		case 180:
			txt.hor = text_flip(txt.hor);
			break;
		case 270:
			break;
		}
	}

	switch (txt.rot) {
	case 180:
	case 270:
		text_flip_x(&txt);
		break;
	default:
		break;
	}

	text_fig(&txt, COLOR_PIN_NUMBER, LAYER_PIN_NUMBER);
}


static void draw_pin(const struct comp *comp, const struct lib_pin *pin,
    const int m[6])
{
	int x[2], y[2];
	int dx = 0, dy = 0;
	int rot;
	enum text_align hor;

	switch (pin->orient) {
	case 'U':
		dy = 1;
		rot = 90;
		hor = text_min;
		break;
	case 'D':
		dy = -1;
		rot = 90;
		hor = text_max;
		break;
	case 'R':
		dx = 1;
		rot = 0;
		hor = text_min;
		break;
	case 'L':
		dx = -1;
		rot = 0;
		hor = text_max;
		break;
	default:
		abort();
	}
	x[0] = mx(pin->x, pin->y, m);
	y[0] = my(pin->x, pin->y, m);
	x[1] = mx(pin->x + dx * pin->length, pin->y + dy * pin->length, m);
	y[1] = my(pin->x + dx * pin->length, pin->y + dy * pin->length, m);
	gfx_poly(2, x, y, COLOR_COMP_DWG, COLOR_NONE, LAYER_COMP_DWG);

	if (comp->show_pin_name)
		draw_pin_name(comp, pin, m, dx, dy, rot, hor);

	if (comp->show_pin_num)
		draw_pin_num(comp, pin, m, dx, dy, rot, hor);
}


static void draw_text(const struct lib_text *text, const int m[6])
{
	struct text txt = {
		.s = text->s,
		.size = text->dim,
		.x = mx(text->x, text->y, m),
		.y = my(text->x, text->y, m),
		.rot = angle_add(text->orient / 10, matrix_to_angle(m)),
	};

	decode_alignment(&txt, text->hor_align, text->vert_align);

	switch (txt.rot) {
	case 180:
	case 270:
		/* @@@ consolidate this with text_flip_x */
		txt.rot = angle_add(txt.rot, 180);
		txt.hor = text_flip(txt.hor);
		txt.vert = text_flip(txt.vert);
//		text_flip_x(&txt);
		break;
	default:
		break;
	}

	if (matrix_is_mirrored(m))
		switch (txt.rot) {
		case 0:
		case 180:
			txt.hor = text_flip(txt.hor);
			break;
		case 90:
		case 270:
			txt.vert = text_flip(txt.vert);
			break;
		default:
			abort();
		}

	text_fig(&txt, COLOR_COMP_DWG, WIDTH_COMP_DWG);
}


static void draw(const struct comp *comp, const struct lib_obj *obj,
    const int m[6])
{
	switch (obj->type) {
	case lib_obj_poly:
		draw_poly(&obj->u.poly, m);
		break;
	case lib_obj_rect:
		draw_rect(&obj->u.rect, m);
		break;
	case lib_obj_circ:
		draw_circ(&obj->u.circ, m);
		break;
	case lib_obj_arc:
		draw_arc(&obj->u.arc, m);
		break;
	case lib_obj_text:
		draw_text(&obj->u.text, m);
		break;
	case lib_obj_pin:
		draw_pin(comp, &obj->u.pin, m);
		break;
	default:
		abort();
	}
}


const struct comp *lib_find(const struct lib *lib, const char *name)
{
	const struct comp *comp;
	const struct comp_alias *alias;

	for (comp = lib->comps; comp; comp = comp->next) {
		if (!strcmp(comp->name, name))
			return comp;
		for (alias = comp->aliases; alias; alias = alias->next)
			if (!strcmp(alias->name, name))
				return comp;
	}
	error("\"%s\" not found\n", name);
	return NULL;
}


bool lib_field_visible(const struct comp *comp, int n)
{
	return (comp->visible >> n) & 1;
}


static void missing_component(const int m[4])
{
	int sx = mx(0, 0, m);
	int sy = my(0, 0, m);
	int ex = mx(MISSING_WIDTH, MISSING_HEIGHT, m);
	int ey = my(MISSING_WIDTH, MISSING_HEIGHT, m);

	gfx_rect(sx, sy, ex, ey, COLOR_MISSING_FG, COLOR_MISSING_BG,
	    LAYER_COMP_DWG);
}


void lib_render(const struct comp *comp, unsigned unit, unsigned convert,
    const int m[4])
{
	const struct lib_obj *obj;

	if (!comp) {
		missing_component(m);
		return;
	}
	if (!unit)
		unit = 1;
	for (obj = comp->objs; obj; obj = obj->next) {
		if (obj->unit && obj->unit != unit)
			continue;
		if (obj->convert && obj->convert != convert)
			continue;
		draw(comp, obj, m);
	}
}