missing NULL terminator in set_config_x
[geda-gaf.git] / contrib / olib / geda_sym_format.h
blob0221efffd59223fe721717effe8fd38e3b80f656
2 /*
4 geda_sym_format.h - gEDA symbol write function
5 Copyright (C) 2002-2010 Mario Pascucci <m.pas@libero.it>
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21 visit the URL http://www.fsf.org/
27 /* general sizes */
28 #define SCALE 100
29 #define PIN_SPACE 3*SCALE
30 #define PIN_LEN 3*SCALE
31 #define LABEL_SPACE 2*SCALE
32 #define DOT_SIZE SCALE
33 #define CLK_SIZE SCALE
36 /* text alignment */
37 #define EDATEXT_LB 0
38 #define EDATEXT_LC 1
39 #define EDATEXT_LT 2
40 #define EDATEXT_CB 3
41 #define EDATEXT_CC 4
42 #define EDATEXT_CT 5
43 #define EDATEXT_RB 6
44 #define EDATEXT_RC 7
45 #define EDATEXT_RT 8
48 /* color macros */
49 #define EDACOLOR_REF 8 /* detached attrib = RED */
50 #define EDACOLOR_LABEL 9 /* text and label = GREEN */
51 #define EDACOLOR_ATTRIB 5 /* attached attribute = YELLOW */
52 #define EDACOLOR_PIN 1 /* pin = WHITE */
53 #define EDACOLOR_DOT 6 /* logic bubble = CYAN */
54 #define EDACOLOR_BODY 3 /* body part = GREEN */
57 /* text size macros */
58 #define EDASIZE_LABEL 10 /* label size */
59 #define EDASIZE_ATTRIB 10 /* attribute */
60 #define EDASIZE_PIN 8 /* pin number size */
61 #define EDASIZE_TYPE 8 /* type of pin */
65 /* scale for text */
66 static int text_scale[] = {
74 * Format of gEDA .sym files
76 * text
77 * T x y color size visibility show_name_value angle alignment
79 * pin
80 * P x1 y1 x2 y2 color
82 * line
83 * L x1 y1 x2 y2 color width end type length space
85 * box
86 * type x y width height color width end type length space filling fillwidth angle1 pitch1 angle2 pitch2
87 * Example:
88 * B 33000 67300 2000 2000 3 60 0 2 75 50 0 -1 -1 -1 -1 -1
90 * circle
91 * V 38000 67000 900 3 0 0 2 75 50 0 -1 -1 -1 -1 -1
92 * type x y radius color width end type length space filling fillwidth angle1 pitch1 angle2 pitch2
94 * arc
95 * type x y radius start_angle sweep_angle color width end type length space
96 * Example:
97 * A 30600 75000 2000 0 45 3 0 0 3 75 50
102 #define PI 3.141592653588
106 int initangle(float dx, float dy)
108 float a,b;
110 if (dx == 0.0)
112 if (dy >= 0.0) return 90;
113 else return 270;
115 if (dy == 0.0)
117 if (dx >= 0.0) return 0;
118 else return 180;
120 a = atan2(dy,dx);
121 b = 180.0/PI * a;
122 return (int) b;
127 /* write down the gEDA symbol file
128 most of the work must be done for
129 correct symbol rendering
131 int write_sym(void)
133 int i,xs,ys,xe,ye;
134 int xt,yt,xb,yb;
135 int labely;
136 int txtangle;
137 int pinx,piny,pinalign; /* pin number */
138 int npinx,npiny,npinalign; /* pin name */
139 int tpinx,tpiny,tpinalign; /* pin type */
141 xe = ye = xs = ys = txtangle = pinx = piny = pinalign =
142 npinx = npiny = npinalign = tpinx = tpiny = tpinalign = 0;
143 if (pincount == 0)
145 fprintf(stderr,"Unable to write down sym file, no pin definition available\n");
146 return -1;
148 /* if 'VECTOR' keyword was found, but no draw instruction
149 * makes file .sym.part that stands for "partial" symbol */
150 if (vector_found && vector_count == 0)
151 strcat(fnsym,".part");
152 if (!(fsym = fopen(fnsym,"wb")))
154 perror("Opening sym output file");
155 return -1;
157 xt = sizex + PIN_LEN;
158 xb = PIN_LEN;
159 yt = sizey + PIN_LEN;
160 yb = PIN_LEN;
161 /* gEDA file version */
162 fprintf(fsym,"v %s\n",sym_version);
163 /* reference: U? */
164 fprintf(fsym,"T %d %d %d %d 1 1 0 %d\n",xt,yt+SCALE,EDACOLOR_REF,EDASIZE_ATTRIB,EDATEXT_RB);
165 if (use_old_version)
166 fprintf(fsym,"uref=%s?\n",ref_str);
167 else
168 fprintf(fsym,"refdes=%s?\n",ref_str);
169 /* part name */
170 fprintf(fsym,"T %d %d %d %d 1 0 0 %d\n",xb,yt+SCALE,EDACOLOR_LABEL,EDASIZE_LABEL,EDATEXT_LB);
171 fprintf(fsym,"%s\n",part_aliases[0]);
172 /* device name */
173 fprintf(fsym,"T %d %d %d %d 0 0 0 %d\n",xb,yt+3*SCALE,EDACOLOR_ATTRIB,EDASIZE_ATTRIB,EDATEXT_LB);
174 fprintf(fsym,"device=%s\n",part_aliases[0]);
175 if (vector_count == 0)
177 /* main box */
178 fprintf(fsym,"B %d %d %d %d %d 0 0 0 -1 -1 0 -1 -1 -1 -1 -1\n",xb,yb,sizex,sizey,EDACOLOR_BODY);
180 else
182 /* drawing vectors */
183 for (i = 0; i< vector_count; i++)
185 switch (part_vectors[i].type)
187 case 'T':
188 /* text */
189 xs = (int) (part_vectors[i].p[0] * PIN_SPACE + 0.5) + PIN_LEN;
190 ys = (int) yt - (part_vectors[i].p[1] * PIN_SPACE + 0.5);
191 fprintf(fsym,"T %d %d %d %d 1 0 0 %d\n",xs,ys,EDACOLOR_BODY,
192 text_scale[(int)part_vectors[i].p[2]],EDATEXT_LB);
193 fprintf(fsym,"%s\n",part_vectors[i].str);
194 free(part_vectors[i].str);
195 break;
196 case 'L':
197 /* line */
198 xs = (int) (part_vectors[i].p[0] * PIN_SPACE + 0.5) + PIN_LEN;
199 ys = (int) yt - (part_vectors[i].p[1] * PIN_SPACE + 0.5);
200 xe = (int) (part_vectors[i].p[2] * PIN_SPACE + 0.5) + PIN_LEN;
201 ye = (int) yt - (part_vectors[i].p[3] * PIN_SPACE + 0.5);
202 fprintf(fsym,"L %d %d %d %d %d 0 0 0 -1 -1\n",xs,ys,xe,ye,
203 EDACOLOR_BODY);
204 break;
205 case 'V':
206 /* circle */
207 xs = (int) (part_vectors[i].p[0] * PIN_SPACE + PIN_LEN + 0.5);
208 ys = (int) yt - (part_vectors[i].p[1] * PIN_SPACE + 0.5);
209 xe = (int) (part_vectors[i].p[2] * PIN_SPACE + 0.5); /* radius */
210 fprintf(fsym,"V %d %d %d %d 0 0 0 -1 -1 0 -1 -1 -1 -1 -1\n",xs,ys,xe,
211 EDACOLOR_BODY);
212 break;
213 case 'A':
214 /* arc */
215 xs = (int) (part_vectors[i].p[0] * PIN_SPACE + PIN_LEN + 0.5);
216 ys = (int) yt - (part_vectors[i].p[1] * PIN_SPACE + 0.5);
217 xe = initangle(part_vectors[i].p[2],-part_vectors[i].p[3]); /* init angle */
218 ye = initangle(part_vectors[i].p[4],-part_vectors[i].p[5]); /* end angle */
219 if (xe < 0) xe += 360;
220 if (ye < 0) ye += 360;
221 ye -= xe;
222 if (abs(ye) > 90)
224 if (ye > 0)
225 ye -= 360;
226 else
227 ye += 360;
229 pinx = (int) (part_vectors[i].p[6] * PIN_SPACE + 0.5); /* radius */
230 fprintf(fsym,"A %d %d %d %d %d %d 0 0 0 -1 -1\n",xs,ys,pinx,xe,ye,
231 EDACOLOR_BODY);
232 break;
236 /* pin definiton and drawing */
237 labely = 5 * SCALE;
238 for (i=0; i<pincount; i++)
240 if ((pinlist[i].num == 0) && (pinlist[i].type == PIN_TYPE_PWR) &&
241 ((pinlist[i].pos == 'T') || (pinlist[i].pos == 'B')))
243 /* is a "virtual" pin, i.e. a net */
244 fprintf(fsym,"T %d %d %d %d 0 0 0 %d\n",xb,yt+labely,EDACOLOR_ATTRIB,
245 EDASIZE_ATTRIB,EDATEXT_LB);
246 fprintf(fsym,"net=%s:%d\n",pinlist[i].name,pinlist[i].pin);
247 labely += LABEL_SPACE;
248 continue;
250 switch (pinlist[i].pos)
252 case 'L':
253 xs = xb;
254 xe = 0;
255 ys = ye = yt - pinlist[i].num * PIN_SPACE;
256 txtangle = 0;
257 /* pin number */
258 pinx = xs - SCALE;
259 piny = ys + SCALE/2;
260 pinalign = EDATEXT_RB;
261 /* pin name */
262 npinx = xs + SCALE;
263 npiny = ys;
264 npinalign = EDATEXT_LB;
265 /* pin type */
266 tpinx = xs + SCALE;
267 tpiny = ys;
268 tpinalign = EDATEXT_LT;
269 if ((pinlist[i].flags & PIN_CLKFLAG) != 0)
271 /* there is "CLK" modifier */
272 npinx += CLK_SIZE/2;
273 tpinx += CLK_SIZE/2;
274 fprintf(fsym,"L %d %d %d %d %d 0 0 0 -1 -1\n",xs,ys+CLK_SIZE,xs+CLK_SIZE,ys,
275 EDACOLOR_BODY);
276 fprintf(fsym,"L %d %d %d %d %d 0 0 0 -1 -1\n",xs,ys-CLK_SIZE,xs+CLK_SIZE,ys,
277 EDACOLOR_BODY);
279 if ((pinlist[i].flags & PIN_DOTFLAG) != 0)
281 /* there is "DOT" modifier */
282 xs -= DOT_SIZE;
283 pinx -= DOT_SIZE/2;
284 fprintf(fsym,"V %d %d %d %d 0 0 0 -1 -1 0 -1 -1 -1 -1 -1\n",xs+DOT_SIZE/2,ys,DOT_SIZE/2,
285 EDACOLOR_DOT);
287 break;
288 case 'R':
289 xs = xt;
290 xe = xt + PIN_LEN;
291 ys = ye = yt - pinlist[i].num * PIN_SPACE;
292 txtangle = 0;
293 /* pin number */
294 pinx = xs + SCALE;
295 piny = ys + SCALE/2;
296 pinalign = EDATEXT_LB;
297 /* pin name */
298 npinx = xs - SCALE;
299 npiny = ys;
300 npinalign = EDATEXT_RB;
301 /* pin type */
302 tpinx = xs - SCALE;
303 tpiny = ys;
304 tpinalign = EDATEXT_RT;
305 if ((pinlist[i].flags & PIN_CLKFLAG) != 0)
307 /* there is "CLK" modifier */
308 npinx -= CLK_SIZE/2;
309 tpinx -= CLK_SIZE/2;
310 fprintf(fsym,"L %d %d %d %d %d 0 0 0 -1 -1\n",xs,ys+CLK_SIZE,xs-CLK_SIZE,ys,
311 EDACOLOR_BODY);
312 fprintf(fsym,"L %d %d %d %d %d 0 0 0 -1 -1\n",xs,ys-CLK_SIZE,xs-CLK_SIZE,ys,
313 EDACOLOR_BODY);
315 if ((pinlist[i].flags & PIN_DOTFLAG) != 0)
317 /* there is "DOT" modifier */
318 xs += DOT_SIZE;
319 pinx += DOT_SIZE/2;
320 fprintf(fsym,"V %d %d %d %d 0 0 0 -1 -1 0 -1 -1 -1 -1 -1\n",xs-DOT_SIZE/2,ys,DOT_SIZE/2,
321 EDACOLOR_DOT);
323 break;
324 case 'T':
325 ys = yt;
326 ye = yt + PIN_LEN;
327 xs = xe = xb + pinlist[i].num * PIN_SPACE;
328 txtangle = 90;
329 /* pin number */
330 pinx = xs - SCALE/2;
331 piny = ys + SCALE;
332 pinalign = EDATEXT_LB;
333 /* pin name */
334 npinx = xs;
335 npiny = ys - SCALE;
336 npinalign = EDATEXT_RB;
337 /* pin type */
338 tpinx = xs;
339 tpiny = ys - SCALE;
340 tpinalign = EDATEXT_RT;
341 if ((pinlist[i].flags & PIN_CLKFLAG) != 0)
343 /* there is "CLK" modifier */
344 npiny -= CLK_SIZE/2;
345 tpiny -= CLK_SIZE/2;
346 fprintf(fsym,"L %d %d %d %d %d 0 0 0 -1 -1\n",xs-CLK_SIZE,ys,xs,ys-CLK_SIZE,
347 EDACOLOR_BODY);
348 fprintf(fsym,"L %d %d %d %d %d 0 0 0 -1 -1\n",xs,ys-CLK_SIZE,xs+CLK_SIZE,ys,
349 EDACOLOR_BODY);
351 if ((pinlist[i].flags & PIN_DOTFLAG) != 0)
353 /* there is "DOT" modifier */
354 ys += DOT_SIZE;
355 piny += DOT_SIZE/2;
356 fprintf(fsym,"V %d %d %d %d 0 0 0 -1 -1 0 -1 -1 -1 -1 -1\n",xs,ys-DOT_SIZE/2,DOT_SIZE/2,
357 EDACOLOR_DOT);
359 break;
360 case 'B':
361 ys = yb;
362 ye = yb - PIN_LEN; /* should be always 0 */
363 xs = xe = xb + pinlist[i].num * PIN_SPACE;
364 txtangle = 270;
365 /* pin number */
366 pinx = xs + SCALE/2;
367 piny = ys - SCALE;
368 pinalign = EDATEXT_LB;
369 /* pin name */
370 npinx = xs;
371 npiny = ys + SCALE;
372 npinalign = EDATEXT_RB;
373 /* pin type */
374 tpinx = xs;
375 tpiny = ys + SCALE;
376 tpinalign = EDATEXT_RT;
377 if ((pinlist[i].flags & PIN_CLKFLAG) != 0)
379 /* there is "CLK" modifier */
380 npiny += CLK_SIZE/2;
381 tpiny += CLK_SIZE/2;
382 fprintf(fsym,"L %d %d %d %d %d 0 0 0 -1 -1\n",xs-CLK_SIZE,ys,xs,ys+CLK_SIZE,
383 EDACOLOR_BODY);
384 fprintf(fsym,"L %d %d %d %d %d 0 0 0 -1 -1\n",xs,ys+CLK_SIZE,xs+CLK_SIZE,ys,
385 EDACOLOR_BODY);
387 if ((pinlist[i].flags & PIN_DOTFLAG) != 0)
389 /* there is "DOT" modifier */
390 ys -= DOT_SIZE;
391 piny -= DOT_SIZE/2;
392 fprintf(fsym,"V %d %d %d %d 0 0 0 -1 -1 0 -1 -1 -1 -1 -1\n",xs,ys+DOT_SIZE/2,DOT_SIZE/2,
393 EDACOLOR_DOT);
395 break;
397 /* pin draw */
398 fprintf(fsym,"P %d %d %d %d %d\n",xs,ys,xe,ye,EDACOLOR_PIN);
399 /* pin attributes */
400 fputs("{\n",fsym);
401 /* pin sequence and number */
402 if (partspp != 0) /* handling for parts per package = 0 */
404 fprintf(fsym,"T %d %d %d %d 1 1 %d %d\n",pinx,piny,EDACOLOR_ATTRIB,
405 EDASIZE_PIN,txtangle,pinalign);
407 else
409 /* if no pin number in the original symbol, make pin number hidden */
410 fprintf(fsym,"T %d %d %d %d 0 1 %d %d\n",pinx,piny,EDACOLOR_ATTRIB,
411 EDASIZE_PIN,txtangle,pinalign);
413 if (use_old_version)
414 fprintf(fsym,"pin%d=%d\n",i+1,pinlist[i].pin);
415 else
417 fprintf(fsym,"pinnumber=%d\n",pinlist[i].pin);
418 fprintf(fsym,"T %d %d %d %d 0 1 %d %d\n",pinx,piny,EDACOLOR_ATTRIB,
419 EDASIZE_PIN,txtangle,pinalign);
420 fprintf(fsym,"pinseq=%d\n",i+1);
422 /* pin name */
423 /* hides pin name if symbol is VECTOR drawed and '-n' switch given */
424 if ((vector_count != 0) && (pin_name_hidden))
426 fprintf(fsym,"T %d %d %d %d 0 1 %d %d\n",npinx,npiny,EDACOLOR_LABEL,
427 EDASIZE_LABEL,txtangle,npinalign);
429 else
431 fprintf(fsym,"T %d %d %d %d 1 1 %d %d\n",npinx,npiny,EDACOLOR_LABEL,
432 EDASIZE_LABEL,txtangle,npinalign);
434 if (use_old_version)
435 fprintf(fsym,"label=%s\n",pinlist[i].name);
436 else
437 fprintf(fsym,"pinlabel=%s\n",pinlist[i].name);
438 /* pin type */
439 fprintf(fsym,"T %d %d %d %d 0 1 %d %d\n",tpinx,tpiny,EDACOLOR_ATTRIB,
440 EDASIZE_LABEL,txtangle,tpinalign);
441 if (use_old_version)
442 fprintf(fsym,"type=%s\n",edapintype[pinlist[i].type]);
443 else
444 fprintf(fsym,"pintype=%s\n",edapintype[pinlist[i].type]);
445 fputs("}\n",fsym);
447 fclose(fsym);
448 return 0;