summaryrefslogtreecommitdiff
path: root/gfx/fig.c
blob: 4e1bebed424ce18becc647bccb4f8807c0874c65 (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
/*
 * gfx/fig.c - Generate FIG output for Eeschema items
 *
 * 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 <stdio.h>
#include <unistd.h>
#include <string.h>
#include <math.h>
#include <assert.h>

#include "misc/util.h"
#include "misc/diag.h"
#include "gfx/style.h"
#include "gfx/text.h"
#include "main.h"
#include "gfx/fig.h"


struct fig_ctx {
	FILE *file;
	bool skip;
};


/*
 * FIG works with 1/1200 in
 * KiCad works with mil
 * 1 point = 1/72 in
 */


static inline int cx(int x)
{
	return x * 1200 / 1000;
}


static inline int cy(int y)
{
	return y * 1200 / 1000;
}


static inline float pt(int x)
{
	return cx(x) * 72 * 1.5 / 1200.0;
}


/* ----- Schematics items -------------------------------------------------- */


static void fig_line(void *ctx, int sx, int sy, int ex, int ey,
    int color, unsigned layer)
{
	struct fig_ctx *fig = ctx;

	if (fig->skip)
		return;

	//   TypeStyle   FillCol AreaFil  Cap  FwdAr
	//     SubTy  Color   Pen   StyleV  Rad  BwdAr
	//         Thick  Depth        Join       Points
	fprintf(fig->file,
	    "2 1 2 %d %d 7 %d -1 -1 3.0 1 1 -1 0 0 2\n",
	    WIDTH_LINE, color, layer);
	fprintf(fig->file, "\t%d %d %d %d\n", cx(sx), cy(sy), cx(ex), cy(ey));
}


/* ----- General items ----------------------------------------------------- */


static void fig_rect(void *ctx, int sx, int sy, int ex, int ey,
    int color, int fill_color, unsigned layer)
{
	struct fig_ctx *fig = ctx;

	if (fig->skip)
		return;

	//   Type  Thick    Depth    StyleV  Rad
	//     SubTy  Color    Pen       Join   FwdAr
	//       Style   FillCol  AreaFil  Cap    BwdAr
	fprintf(fig->file,
	    "2 2 0 %d %d %d %d -1 %d 0.0 1 1 -1 0 0 5\n",
	    color == -1 ? 0 : WIDTH_COMP_DWG, color, fill_color, layer,
	    fill_color == -1 ? -1 : 20);
	fprintf(fig->file, "\t%d %d %d %d %d %d %d %d %d %d\n",
	    cx(sx), cy(sy), cx(ex), cy(sy), cx(ex), cy(ey), cx(sx), cy(ey),
	    cx(sx), cy(sy));
}


static void fig_poly(void *ctx,
    int points, const int x[points], const int y[points],
    int color, int fill_color, unsigned layer)
{
	struct fig_ctx *fig = ctx;
	int i;
	char ch = '\t';

	if (fig->skip)
		return;

	//   Type  Thick     Depth    StyleV  Rad
	//     SubTy  Color     Pen       Join   FwdAr
	//       Style   FillCol   AreaFil  Cap    BwdAr
	fprintf(fig->file,
	    "2 1 0 %d %d %d  %d -1 %d 0.0 1 1 -1 0 0 %d\n",
	    color == -1 ? 0 : WIDTH_COMP_DWG, color, fill_color, layer,
	    fill_color == -1 ? -1 : 20, points);
	for (i = 0; i != points; i++) {
		fprintf(fig->file, "%c%d %d", ch, cx(x[i]), cy(y[i]));
		ch = ' ';
	}
	fprintf(fig->file, "\n");
}


static void fig_circ(void *ctx, int x, int y, int r,
    int color, int fill_color, unsigned layer)
{
	struct fig_ctx *fig = ctx;

	if (fig->skip)
		return;

	//   Type  Thick    Depth   StyleV    Cx    Rx    Sx    Ex
	//     SubTy  Color    Pen       Dir      Cy    Ry    Sy    Ey
	//       Style   FillCol  AreaFil  Angle
	fprintf(fig->file,
	    "1 3 0 %d %d %d %d -1 %d 0.0 1 0.0 %d %d %d %d %d %d %d %d\n",
	    color == -1 ? 0 : WIDTH_COMP_DWG, color, fill_color, layer,
	    fill_color == -1 ? -1 : 20,
	    cx(x), cy(y), r, r,
	    cx(x), cy(y), cx(x) + r, cy(y));
}


static int ax(int x, int y, int r, int angle)
{
	float a = angle / 180.0 * M_PI;

	return cx(x + r * cos(a));
}


static int ay(int x, int y, int r, int angle)
{
	float a = angle / 180.0 * M_PI;

	return cy(y - r * sin(a));
}


static void fig_arc(void *ctx, int x, int y, int r, int sa, int ea,
    int color, int fill_color, unsigned layer)
{
	struct fig_ctx *fig = ctx;
	int ma = (sa + ea) / 2;

	if (fig->skip)
		return;

	//   Type  Thick    Depth   StyleV   FwdAr
	//     SubTy  Color    Pen       Cap   BwdAr
	//       Style   FillCol  AreaFil  Dir   points
	fprintf(fig->file,
	    "5 1 0 %d %d %d %d -1 %d 0.0 1 1 0 0 %d %d %d %d %d %d %d %d\n",
	    color == -1 ? 0 : WIDTH_COMP_DWG, color, fill_color, layer,
	    fill_color == -1 ? -1 : 20,
	    cx(x), cy(y),
	    ax(x, y, r, sa), ay(x, y, r, sa),
	    ax(x, y, r, ma), ay(x, y, r, ma),
	    ax(x, y, r, ea), ay(x, y, r, ea));
}


static void fig_tag(void *ctx, const char *s,
    int points, const int x[points], const int y[points])
{
	struct fig_ctx *fig = ctx;

	if (fig->skip)
		return;

	fprintf(fig->file, "# href=\"%s\" alt=\"\"\n", s);
	fig_poly(fig, points, x, y, COLOR_NONE, COLOR_NONE, 999);
}


static void fig_text(void *ctx, int x, int y, const char *s, unsigned size,
    enum text_align align, int rot, enum text_style style,
    unsigned color, unsigned layer)
{
	struct fig_ctx *fig = ctx;
	int font;

	if (fig->skip)
		return;

	switch (style) {
	case text_italic:
		font = FONT_HELVETICA_OBLIQUE;
		break;
	case text_bold:
		font = FONT_HELVETICA_BOLD;
		break;
	case text_bold_italic:
		font = FONT_HELVETICA_BOLDOB;
		break;
	default:
		font = FONT_HELVETICA;
		break;
	}

	//   Type   Depth     FontSiz Height
	//     Just    Pen       Angle    Length
	//       Color     Font     Flags     X  Y
	fprintf(fig->file,
	    "4 %u %d %d -1 %d %f %f 4 0.0 0.0 %d %d %s\\001\n",
	    align, color, layer, font,
	    pt(size), rot / 180.0 * M_PI, cx(x), cy(y), s);
}


static unsigned fig_text_width(void *ctx, const char *s, unsigned size,
    enum text_style style)
{
	unsigned n = 0;

	/*
	 * Note that we stretch the text size, so the ratio is larger than
	 * expressed here.
	 */

	while (*s) {
		n += (*s & 0xc0) != 0xc0 && *s != '~';
		s++;
	}
	return n * size * 1.0;

}


/* ----- FIG file header --------------------------------------------------- */


static void fig_header(FILE *file)
{
	fprintf(file, "#FIG 3.2\n");
	fprintf(file, "Landscape\n");
	fprintf(file, "Center\n");
	fprintf(file, "Metric\n");
	fprintf(file, "A4\n");
	fprintf(file, "100.00\n");
	fprintf(file, "Single\n");
	fprintf(file, "-2\n");
	fprintf(file, "1200 2\n");
}


static void fig_colors(FILE *file)
{
	unsigned i;

	for (i = 32; i != n_color_rgb; i++)
		fprintf(file, "0 %d #%06x\n", i, color_rgb[i]);

}


static bool apply_vars(char *buf, int n_vars, const char **vars)
{
	char *p;
	const char **var, *eq;
	int var_len, value_len;

	p = strchr(buf, '<');
	if (!p)
		return 0;
	for (var = vars; var != vars + n_vars; var++) {
		eq = strchr(*var, '=');
		assert(eq);
		var_len = eq - *var;
		if (strncmp(p + 1, *var, var_len))
			continue;
		value_len = strlen(eq + 1);
		memmove(p + value_len, p + var_len + 2,
		    strlen(p + var_len + 2) + 1);
		memcpy(p, eq + 1, value_len);
		return 1;
	}
	return 0;
}



static void *fig_init(void)
{
	struct fig_ctx *fig;

	fig = alloc_type(struct fig_ctx);
	fig->file = stdout;
	fig->skip = 0;
	return fig;
}


static bool fig_args(void *ctx, int argc, char *const *argv, const char *opts)
{
	struct fig_ctx *fig = ctx;
	static char *buf = NULL;
	static size_t n = 0;
	const char *output = NULL;
	const char *template = NULL;
	const char **vars = NULL;
	int n_vars = 0;
	const char *colon;
	char c;
	FILE *file;
	int lines_to_colors = 8;

	while ((c = getopt(argc, argv, opts)) != EOF)
		switch (c) {
		case 'o':
			colon = strchr(optarg, ':');
			output = colon ? colon + 1 : optarg;
			break;
		case 't':
			template = optarg;
			break;
		case 'D':
			if (!strchr(optarg, '='))
			    usage(*argv);
			n_vars++;
			vars = realloc_type_n(vars, const char *, n_vars);
			vars[n_vars - 1] = optarg;
			break;
		case '?':
			usage(*argv);
		default:
			break;
		}

	if (output && strcmp(output, "-")) {
		fig->file = fopen(output, "w");
		if (!fig->file)
			diag_pfatal(output);
	}

	if (!template) {
		fig_header(fig->file);
		fig_colors(fig->file);
		return 1;
	}

	file = fopen(template, "r");
	if (!file)
		diag_pfatal(template);
	while (getline(&buf, &n, file) > 0) {
		while (apply_vars(buf, n_vars, vars));
		fprintf(fig->file, "%s", buf);
		if (*buf == '#')
			continue;
		if (!--lines_to_colors)
			fig_colors(fig->file);
		/*
		 * @@@ known bug: if the template is empty, we won't output
		 * color 32.
		 */
	}
	fclose(file);

	return 1;
}


static void fig_set_extra(void *ctx, enum gfx_extra extra)
{
	struct fig_ctx *fig = ctx;

	fig->skip = extra;
}


static int fig_end(void *ctx, enum gfx_extra extra)
{
	struct fig_ctx *fig = ctx;

	/* @@@ support extra */
	if (fclose(fig->file) < 0)
		diag_pfatal("fclose");
	free(ctx);
	return 0;
}


/* ----- Operations -------------------------------------------------------- */


static const char *const fig_ext[] = { "fig" };

const struct gfx_ops fig_ops = {
	.ext		= fig_ext,
	.n_ext		= ARRAY_ELEMENTS(fig_ext),
	.opts		= "o:t:D:",

	.line		= fig_line,
	.rect		= fig_rect,
	.poly		= fig_poly,
	.circ		= fig_circ,
	.arc		= fig_arc,
	.text		= fig_text,
	.tag		= fig_tag,
	.text_width	= fig_text_width,
	.set_extra	= fig_set_extra,
	.init		= fig_init,
	.end		= fig_end,
	.args		= fig_args,
};