From cd3ced9cfb542b530b2269294c659676383499ba Mon Sep 17 00:00:00 2001 From: Werner Almesberger Date: Sat, 23 Jul 2016 00:48:59 -0300 Subject: [PATCH] sch2fig/: restructure text handing; on-going development --- sch2fig/Makefile | 2 +- sch2fig/fig.c | 64 ++++++++++---------------- sch2fig/fig.h | 10 +++- sch2fig/lib.c | 117 ++++++++++++++++++++++++++++++++++++++++++++--- sch2fig/sch.c | 10 +++- sch2fig/style.h | 40 ++++++++++++++++ sch2fig/text.c | 73 +++++++++++++++++++++++++++++ sch2fig/text.h | 43 +++++++++++++++++ 8 files changed, 307 insertions(+), 52 deletions(-) create mode 100644 sch2fig/style.h create mode 100644 sch2fig/text.c create mode 100644 sch2fig/text.h diff --git a/sch2fig/Makefile b/sch2fig/Makefile index 7b257f4..4df5ab5 100644 --- a/sch2fig/Makefile +++ b/sch2fig/Makefile @@ -11,7 +11,7 @@ # NAME = sch2fig -OBJS = main.o sch.o lib.o fig.o +OBJS = main.o sch.o lib.o fig.o text.o CFLAGS = -g -Wall -Wextra -Wno-unused-parameter diff --git a/sch2fig/fig.c b/sch2fig/fig.c index d0b22f8..495124a 100644 --- a/sch2fig/fig.c +++ b/sch2fig/fig.c @@ -13,29 +13,11 @@ #include +#include "style.h" +#include "text.h" #include "fig.h" -#define COLOR_BLACK 0 -#define COLOR_BLUE 1 -#define COLOR_GREEN4 12 -#define COLOR_RED4 18 -#define COLOR_RED3 19 - -#define FONT_HELVETICA_BOLD 18 - -#define LAYER_TEXT 30 -#define LAYER_WIRES 50 -#define LAYER_LINES 60 -#define LAYER_COMP_DWG 70 - -#define WIDTH_WIRE 2 -#define WIDTH_LINE 2 -#define WIDTH_COMP_DWG 2 - -#define JUNCTION_R 50 - - /* * FIG works with 1/1200 in * KiCad works with mil @@ -55,9 +37,9 @@ static inline int cy(int y) } -static inline int pt(int x) +static inline float pt(int x) { - return cx(x) * 72 * 1.5 / 1200; + return cx(x) * 72 * 1.5 / 1200.0; } @@ -67,7 +49,7 @@ void fig_rect(int sx, int sy, int ex, int ey) // SubTy Color Pen Join FwdAr // Style FillCol AreaFil Cap BwdAr printf("2 2 0 %d %d 7 %d -1 -1 0.0 1 1 -1 0 0 5\n", - WIDTH_COMP_DWG, COLOR_RED4, LAYER_COMP_DWG); + WIDTH_COMP_DWG, COLOR_COMP_DWG, LAYER_COMP_DWG); 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)); @@ -83,7 +65,7 @@ void fig_poly(int points, int x[points], int y[points]) // SubTy Color Pen Join FwdAr // Style FillCol AreaFil Cap BwdAr printf("2 1 0 %d %d 7 %d -1 -1 0.0 1 1 -1 0 0 %d\n", - WIDTH_COMP_DWG, COLOR_RED4, LAYER_COMP_DWG, points); + WIDTH_COMP_DWG, COLOR_COMP_DWG, LAYER_COMP_DWG, points); for (i = 0; i != points; i++) { printf("%c%d %d", ch, cx(x[i]), cy(y[i])); ch = ' '; @@ -92,18 +74,6 @@ void fig_poly(int points, int x[points], int y[points]) } -void fig_text(int x, int y, const char *s, int dir, int dim, - enum fig_shape shape) -{ - // Type Depth FontSiz Height - // Just Pen Angle Length - // Color Font Flags X Y - printf("4 0 %d %d -1 %d %d %d 4 0.0 0.0 %d %d %s\\001\n", - COLOR_BLACK, LAYER_TEXT, FONT_HELVETICA_BOLD, - pt(dim), 0, cx(x), cy(y), s); -} - - void fig_glabel(int x, int y, const char *s, int dir, int dim, enum fig_shape shape) { @@ -116,7 +86,7 @@ void fig_junction(int x, int y) // 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_GREEN4, LAYER_WIRES, cx(x), cy(y), JUNCTION_R, JUNCTION_R, + COLOR_WIRE, LAYER_WIRES, cx(x), cy(y), JUNCTION_R, JUNCTION_R, cx(x), cy(y), cx(x) + JUNCTION_R, cy(y)); } @@ -127,7 +97,7 @@ void fig_wire(int sx, int sy, int ex, int ey) // 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_GREEN4, LAYER_WIRES); + WIDTH_WIRE, COLOR_WIRE, LAYER_WIRES); printf("\t%d %d %d %d\n", cx(sx), cy(sy), cx(ex), cy(ey)); } @@ -137,12 +107,24 @@ 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 0 %d %d 7 %d -1 -1 0.0 1 1 -1 0 0 2\n", - WIDTH_LINE, COLOR_BLUE, LAYER_LINES); + 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)); } +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 / 3.1415926 * 180, cx(x), cy(y), s); +} + + void fig_init(void) { printf("#FIG 3.2\n"); @@ -150,7 +132,7 @@ void fig_init(void) printf("Center\n"); printf("Metric\n"); printf("A4\n"); - printf("10.00\n"); + printf("100.00\n"); printf("Single\n"); printf("-2\n"); printf("1200 2\n"); diff --git a/sch2fig/fig.h b/sch2fig/fig.h index 66f28e2..b6d8563 100644 --- a/sch2fig/fig.h +++ b/sch2fig/fig.h @@ -14,6 +14,9 @@ #ifndef FIG_H #define FIG_H +#include "text.h" + + enum fig_shape { fig_unspec, // UnSpc fig_in, // Input @@ -29,8 +32,6 @@ void fig_poly(int points, int x[points], int y[points]); /* schematics */ -void fig_text(int x, int y, const char *s, int dir, int dim, - enum fig_shape shape); void fig_glabel(int x, int y, const char *s, int dir, int dim, enum fig_shape shape); @@ -39,6 +40,11 @@ void fig_junction(int x, int y); void fig_wire(int sx, int sy, int ex, int ey); void fig_line(int sx, int sy, int ex, int ey); +/* general */ + +void fig_text(int x, int y, const char *s, unsigned size, + enum text_align align, int rot, unsigned color, unsigned layer); + /* inititalization */ void fig_init(void); diff --git a/sch2fig/lib.c b/sch2fig/lib.c index 12dc193..b248f14 100644 --- a/sch2fig/lib.c +++ b/sch2fig/lib.c @@ -16,10 +16,16 @@ #include #include +#include "style.h" #include "fig.h" +#include "text.h" #include "lib.h" +enum text_style { + text_normal, +}; + struct obj { enum obj_type { obj_poly, @@ -51,13 +57,16 @@ struct obj { int thick; char fill; } circ; - struct { - char orient; + struct text_obj { + int orient; int x, y; int dim; char *s; + enum text_style style; + char hor_align; + char vert_align; } text; - struct { + struct pin_obj { char *name; char *number; int x, y; @@ -112,6 +121,70 @@ static void draw_poly(const struct poly *poly, int m[6]) } +static void draw_pin(const struct pin_obj *pin, int m[6]) +{ + int x[2], y[2]; + int dx = 0, dy = 0; + + switch (pin->orient) { + case 'U': + dy = pin->length; + break; + case 'D': + dy = -pin->length; + break; + case 'R': + dx = pin->length; + break; + case 'L': + dx = -pin->length; + 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->y + dy, m); + y[1] = my(pin->x + dx, pin->y + dy, m); + fig_poly(2, x, y); +} + + +static bool eq(int m[6], int xx, int xy, int yx, int yy) +{ + return m[1] == xx && m[2] == xy && m[4] == yx && m[5] == yy; +} + + +static unsigned matrix_to_angle(int m[6]) +{ + if (eq(m, 1, 0, 0, -1)) + return 0; + if (eq(m, 0, -1, -1, 0)) + return 90; + if (eq(m, 0, 1, 1, 0)) + return 270; + fprintf(stderr, "unrecognized matrix %d %d %d %d\n", + m[1], m[2], m[4], m[5]); + exit(1); +} + + +static void draw_text(const struct text_obj *text, 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 = matrix_to_angle(m), + .hor = text_mid, + .vert = text_mid, + }; + text_fig(&txt, COLOR_COMP_DWG, WIDTH_COMP_DWG); +} + + static void draw(const struct obj *obj, int m[6]) { switch (obj->type) { @@ -127,9 +200,13 @@ static void draw(const struct obj *obj, int m[6]) break; case obj_circ: break; + case obj_arc: + break; case obj_text: + draw_text(&obj->u.text, m); break; case obj_pin: + draw_pin(&obj->u.pin, m); break; default: abort(); @@ -157,6 +234,14 @@ found: } +static enum text_style decode_style(const char *s) +{ + if (!strcmp(s, "Normal")) + return text_normal; + abort(); +} + + static bool parse_poly(struct obj *obj, const char *line, int points) { int i, n; @@ -183,6 +268,8 @@ bool lib_parse(struct lib_ctx *ctx, const char *line) unsigned points; struct comp *comp; struct obj *obj; + char *style; + unsigned zero1, zero2; ctx->lineno++; @@ -245,10 +332,26 @@ bool lib_parse(struct lib_ctx *ctx, const char *line) obj->type = obj_arc; return 1; // @@@ } - if (sscanf(line, "T %c %d %d %d %u %u %ms", + n = sscanf(line, + "T %d %d %d %d %u %u %u \"%m[^\"]\" %ms %u %c %c", &obj->u.text.orient, &obj->u.text.x, &obj->u.text.y, - &obj->u.text.dim, &obj->unit, &obj->convert, - &obj->u.text.s) == 7) { + &obj->u.text.dim, &zero1, &obj->unit, &obj->convert, + &obj->u.text.s, &style, &zero2, + &obj->u.text.hor_align, &obj->u.text.vert_align); + if (n != 12) + n = sscanf(line, + "T %d %d %d %d %u %u %u %ms %ms %u %c %c", + &obj->u.text.orient, &obj->u.text.x, &obj->u.text.y, + &obj->u.text.dim, &zero1, &obj->unit, &obj->convert, + &obj->u.text.s, &style, &zero2, + &obj->u.text.hor_align, &obj->u.text.vert_align); + if (n == 12) { + if (zero1 || zero2) { + fprintf(stderr, "%u: only understand 0 x x\n", + ctx->lineno); + exit(1); + } + obj->u.text.style = decode_style(style); obj->type = obj_text; return 1; } @@ -273,5 +376,5 @@ bool lib_parse(struct lib_ctx *ctx, const char *line) void lib_init(struct lib_ctx *ctx) { ctx->state = lib_skip; - ctx->lineno++; + ctx->lineno = 0; } diff --git a/sch2fig/sch.c b/sch2fig/sch.c index 45ce7b3..3bfbe2f 100644 --- a/sch2fig/sch.c +++ b/sch2fig/sch.c @@ -16,6 +16,7 @@ #include #include +#include "style.h" #include "fig.h" #include "lib.h" #include "sch.h" @@ -46,6 +47,13 @@ static enum fig_shape decode_shape(const char *s) } +static void draw_text(int x, int y, const char *s, int dir, int dim, + enum fig_shape shape) +{ + fig_text(x, y, s, dim, text_min, 0, COLOR_TEXT, LAYER_TEXT); +} + + bool sch_parse(struct sch_ctx *ctx, const char *line) { int n = 0; @@ -71,7 +79,7 @@ bool sch_parse(struct sch_ctx *ctx, const char *line) if (sscanf(line, "Text Notes %d %d %d %d", &ctx->x, &ctx->y, &ctx->dir, &ctx->dim) == 4) { ctx->state = sch_text; - ctx->text = fig_text; + ctx->text = draw_text; return 1; } if (sscanf(line, "Text GLabel %d %d %d %d %ms", diff --git a/sch2fig/style.h b/sch2fig/style.h new file mode 100644 index 0000000..b1543f9 --- /dev/null +++ b/sch2fig/style.h @@ -0,0 +1,40 @@ +/* + * style.h - FIG drawing style + * + * 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. + */ + +#ifndef STYLE_H +#define STYLE_H + +#define COLOR_BLACK 0 +#define COLOR_BLUE 1 +#define COLOR_GREEN4 12 +#define COLOR_RED4 18 +#define COLOR_RED3 19 + +#define COLOR_COMP_DWG COLOR_RED4 +#define COLOR_SHEET_DWG COLOR_BLUE +#define COLOR_TEXT COLOR_BLACK +#define COLOR_WIRE COLOR_GREEN4 + +#define FONT_HELVETICA_BOLD 18 + +#define LAYER_TEXT 30 +#define LAYER_WIRES 50 +#define LAYER_LINES 60 +#define LAYER_COMP_DWG 70 + +#define WIDTH_WIRE 2 +#define WIDTH_LINE 2 +#define WIDTH_COMP_DWG 2 + +#define JUNCTION_R 40 + +#endif /* !STYLE_H */ diff --git a/sch2fig/text.c b/sch2fig/text.c new file mode 100644 index 0000000..2d9905b --- /dev/null +++ b/sch2fig/text.c @@ -0,0 +1,73 @@ +/* + * text.c - FIG text object + * + * 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 "fig.h" +#include "text.h" + + +void text_init(struct text *txt) +{ + txt->s = NULL; + txt->size = 0; + txt->x = txt->y = 0; + txt->rot = 0; + txt->hor = text_mid; + txt->vert = text_mid; +} + + +void text_free(struct text *txt) +{ + free((void *) txt->s); +} + +void text_set(struct text *txt, const char *s) +{ + free((void *) txt->s); + txt->s = strdup(s); +} + + +void text_fig(struct text *txt, int color, unsigned layer) +{ + int x = txt->x; + int y = txt->y; + + switch (txt->vert) { + case text_min: + break; + case text_mid: + y += txt->size >> 1; + break; + case text_max: + y += txt->size; + break; + default: + abort(); + } + fig_text(x, y, txt->s, txt->size, txt->hor, txt->rot, color, layer); +} + + +void text_rel_x(struct text *txt, int x, int y) +{ +} + + +void text_rel_y(struct text *txt, int x, int y) +{ +} diff --git a/sch2fig/text.h b/sch2fig/text.h new file mode 100644 index 0000000..9756ef4 --- /dev/null +++ b/sch2fig/text.h @@ -0,0 +1,43 @@ +/* + * text.h - FIG text object + * + * 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. + */ + + +#ifndef TEXT_H +#define TEXT_H + + +/* use constants of FIG text sub_type */ + +enum text_align { + text_min = 0, // left or bottom + text_mid = 1, // center + text_max = 2, // right or top +}; + +struct text { + const char *s; + int size; + int x, y; + int rot; + enum text_align hor; + enum text_align vert; +}; + +void text_init(struct text *txt); +void text_free(struct text *txt); +void text_set(struct text *txt, const char *s); + +void text_fig(struct text *txt, int color, unsigned layer); +void text_rel_x(struct text *txt, int x, int y); +void text_rel_y(struct text *txt, int x, int y); + +#endif /* !TEXT_H */