/* * 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 #include #include #include #include #include "util.h" #include "style.h" #include "text.h" #include "fig.h" /* * 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 -------------------------------------------------- */ enum box_type { // ___ box_simple, // [___] box_left, // <___] box_right, // [___> box_both, // <___> }; static enum box_type flip_box(enum box_type box) { switch (box) { case box_simple: return box_simple; case box_left: return box_right; case box_right: return box_left; case box_both: return box_both; default: abort(); } } void fig_label(int x, int y, const char *s, int dir, int dim, enum fig_shape shape) { struct text txt = { .s = s, .size = dim, .x = x, .y = y, .rot = 0, .hor = 0, .vert = text_min, }; int dx = 0, dy = 0; switch (dir) { case 0: /* right */ txt.rot = 0; txt.hor = text_min; dy = 1; break; case 1: /* up */ txt.rot = 90; txt.hor = text_min; dx = 1; break; case 2: /* left */ txt.rot = 0; txt.hor = text_max; dy = 1; break; case 3: /* down */ txt.rot = 90; txt.hor = text_max; dx = -1; break; default: assert(0); } txt.y -= dy * LABEL_OFFSET; txt.x += dx * LABEL_OFFSET; text_fig(&txt, COLOR_LABEL, LAYER_LABEL); } void fig_glabel(int x, int y, const char *s, int dir, int dim, enum fig_shape shape) { struct text txt = { .s = s, .size = dim, .x = x, .y = y, .rot = 0, .hor = 0, .vert = text_mid, }; int n = 6; int vx[7]; int vy[7]; int half = (dim >> 1) + GLABEL_OFFSET; enum box_type box; int dx, shift_flat, shift_tip; bool anchor_right = 1; switch (shape) { case fig_unspec: box = box_simple; break; case fig_in: box = box_right; break; case fig_out: box = box_left; break; case fig_bidir: box = box_both; break; default: assert(0); } switch (dir) { case 0: /* left */ txt.rot = 0; txt.hor = text_max; dx = -1; break; case 1: /* up */ txt.rot = 90; txt.hor = text_min; dx = 1; box = flip_box(box); anchor_right = !anchor_right; break; case 2: /* right */ txt.rot = 0; txt.hor = text_min; dx = 1; box = flip_box(box); anchor_right = !anchor_right; break; case 3: /* down */ txt.rot = 90; txt.hor = text_max; dx = -1; break; default: assert(0); } shift_flat = dx * GLABEL_OFFSET; shift_tip = dx * (GLABEL_OFFSET + half); switch (box) { case box_simple: n = 5; text_shift(&txt, txt.hor, text_mid, shift_flat, 0); text_rel(&txt, text_min, text_min, -GLABEL_OFFSET, GLABEL_OFFSET, vx + 1, vy + 1); text_rel(&txt, text_max, text_min, GLABEL_OFFSET, GLABEL_OFFSET, vx + 2, vy + 2); text_rel(&txt, text_max, text_max, GLABEL_OFFSET, -GLABEL_OFFSET, vx + 3, vy + 3); text_rel(&txt, text_min, text_max, -GLABEL_OFFSET, -GLABEL_OFFSET, vx + 4, vy + 4); break; case box_right: text_shift(&txt, txt.hor, text_mid, anchor_right ? shift_tip : shift_flat, 0); text_rel(&txt, text_min, text_min, -GLABEL_OFFSET, GLABEL_OFFSET, vx + 1, vy + 1); text_rel(&txt, text_max, text_min, GLABEL_OFFSET, GLABEL_OFFSET, vx + 2, vy + 2); text_rel(&txt, text_max, text_mid, GLABEL_OFFSET + half, 0, vx + 3, vy + 3); text_rel(&txt, text_max, text_max, GLABEL_OFFSET, -GLABEL_OFFSET, vx + 4, vy + 4); text_rel(&txt, text_min, text_max, -GLABEL_OFFSET, -GLABEL_OFFSET, vx + 5, vy + 5); break; case box_left: text_shift(&txt, txt.hor, text_mid, anchor_right ? shift_flat : shift_tip, 0); text_rel(&txt, text_min, text_min, -GLABEL_OFFSET, GLABEL_OFFSET, vx + 1, vy + 1); text_rel(&txt, text_max, text_min, GLABEL_OFFSET, GLABEL_OFFSET, vx + 2, vy + 2); text_rel(&txt, text_max, text_max, GLABEL_OFFSET, -GLABEL_OFFSET, vx + 3, vy + 3); text_rel(&txt, text_min, text_max, -GLABEL_OFFSET, -GLABEL_OFFSET, vx + 4, vy + 4); text_rel(&txt, text_min, text_mid, -GLABEL_OFFSET- half, 0, vx + 5, vy + 5); break; case box_both: n = 7; text_shift(&txt, txt.hor, text_mid, shift_tip, 0); text_rel(&txt, text_min, text_min, -GLABEL_OFFSET, GLABEL_OFFSET, vx + 1, vy + 1); text_rel(&txt, text_max, text_min, GLABEL_OFFSET, GLABEL_OFFSET, vx + 2, vy + 2); text_rel(&txt, text_max, text_mid, GLABEL_OFFSET + half, 0, vx + 3, vy + 3); text_rel(&txt, text_max, text_max, GLABEL_OFFSET, -GLABEL_OFFSET, vx + 4, vy + 4); text_rel(&txt, text_min, text_max, -GLABEL_OFFSET, -GLABEL_OFFSET, vx + 5, vy + 5); text_rel(&txt, text_min, text_mid, -GLABEL_OFFSET- half, 0, vx + 6, vy + 6); break; default: assert(0); } text_fig(&txt, COLOR_GLABEL, LAYER_GLABEL); vx[0] = vx[n - 1]; vy[0] = vy[n - 1]; fig_poly(n, vx, vy, COLOR_GLABEL, COLOR_NONE, LAYER_GLABEL); } void fig_junction(int x, int y) { #if 0 // Type Thick Depth StyleV Cx Rx Sx Ex // SubTy Color Pen Dir Cy Ry Sy Ey // Style FillCol AreaFil Angle printf("1 3 0 0 -1 %d %d -1 20 0.0 1 0.0 %d %d %d %d %d %d %d %d\n", COLOR_WIRE, LAYER_WIRES, cx(x), cy(y), JUNCTION_R, JUNCTION_R, cx(x), cy(y), cx(x) + JUNCTION_R, cy(y)); #endif fig_circ(x, y, JUNCTION_R, COLOR_NONE, COLOR_WIRE, LAYER_WIRES); } void fig_noconn(int x, int y) { int vx[2] = { x - NOCONN_LEN, x + NOCONN_LEN }; int vy[2] = { y - NOCONN_LEN, y + NOCONN_LEN }; fig_poly(2, vx, vy, COLOR_NOCONN, COLOR_NONE, LAYER_NOCONN); swap(vy[0], vy[1]); fig_poly(2, vx, vy, COLOR_NOCONN, COLOR_NONE, LAYER_NOCONN); } void fig_wire(int sx, int sy, int ex, int ey) { // TypeStyle FillCol AreaFil Cap FwdAr // SubTy Color Pen StyleV Rad BwdAr // Thick Depth Join Points printf("2 1 0 %d %d 7 %d -1 -1 0.0 1 1 -1 0 0 2\n", WIDTH_WIRE, COLOR_WIRE, LAYER_WIRES); printf("\t%d %d %d %d\n", cx(sx), cy(sy), cx(ex), cy(ey)); } void fig_line(int sx, int sy, int ex, int ey) { // TypeStyle FillCol AreaFil Cap FwdAr // SubTy Color Pen StyleV Rad BwdAr // Thick Depth Join Points printf("2 1 2 %d %d 7 %d -1 -1 3.0 1 1 -1 0 0 2\n", WIDTH_LINE, COLOR_SHEET_DWG, LAYER_LINES); printf("\t%d %d %d %d\n", cx(sx), cy(sy), cx(ex), cy(ey)); } /* ----- General items ----------------------------------------------------- */ void fig_rect(int sx, int sy, int ex, int ey, int color, int fill_color, unsigned layer) { // Type Thick Depth StyleV Rad // SubTy Color Pen Join FwdAr // Style FillCol AreaFil Cap BwdAr printf("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); printf("\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)); } void fig_poly(int points, int x[points], int y[points], int color, int fill_color, unsigned layer) { int i; char ch = '\t'; // Type Thick Depth StyleV Rad // SubTy Color Pen Join FwdAr // Style FillCol AreaFil Cap BwdAr printf("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++) { printf("%c%d %d", ch, cx(x[i]), cy(y[i])); ch = ' '; } printf("\n"); } void fig_circ(int x, int y, int r, int color, int fill_color, unsigned layer) { // Type Thick Depth StyleV Cx Rx Sx Ex // SubTy Color Pen Dir Cy Ry Sy Ey // Style FillCol AreaFil Angle printf("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)); } void fig_arc(int x, int y, int r, int sa, int ea, int color, int fill_color, unsigned layer) { int ma = (sa + ea) / 2; // Type Thick Depth StyleV FwdAr // SubTy Color Pen Cap BwdAr // Style FillCol AreaFil Dir points printf("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)); } void fig_text(int x, int y, const char *s, unsigned size, enum text_align align, int rot, unsigned color, unsigned layer) { // Type Depth FontSiz Height // Just Pen Angle Length // Color Font Flags X Y printf("4 %u %d %d -1 %d %f %f 4 0.0 0.0 %d %d %s\\001\n", align, color, layer, FONT_HELVETICA_BOLD, pt(size), rot / 180.0 * M_PI, cx(x), cy(y), s); } /* ----- FIG file header --------------------------------------------------- */ static void fig_header(void) { printf("#FIG 3.2\n"); printf("Landscape\n"); printf("Center\n"); printf("Metric\n"); printf("A4\n"); printf("100.00\n"); printf("Single\n"); printf("-2\n"); printf("1200 2\n"); } 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; } void fig_init(const char *template, int n_vars, const char **vars) { FILE *file; char buf[1000]; if (!template) { fig_header(); return; } file = fopen(template, "r"); if (!file) { perror(template); exit(1); } while (fgets(buf, sizeof(buf), file)) { while (apply_vars(buf, n_vars, vars)); printf("%s", buf); } fclose(file); }