From d63d1831982f071c2a2fc08e30483bc96018f247 Mon Sep 17 00:00:00 2001 From: Werner Almesberger Date: Fri, 29 Jul 2016 19:29:59 -0300 Subject: [PATCH] sch2fig/: support hierarchical labels (with new geometry approach) --- sch2fig/dwg.c | 150 ++++++++++++++++++++++++++++++++++++++++++++++++ sch2fig/dwg.h | 2 + sch2fig/sch.c | 4 +- sch2fig/style.h | 4 ++ 4 files changed, 158 insertions(+), 2 deletions(-) diff --git a/sch2fig/dwg.c b/sch2fig/dwg.c index 698a3b7..9de8e75 100644 --- a/sch2fig/dwg.c +++ b/sch2fig/dwg.c @@ -14,6 +14,7 @@ #include #include #include +#include #include #include "util.h" @@ -231,6 +232,155 @@ void dwg_glabel(int x, int y, const char *s, int dir, int dim, } +static int make_box(enum box_type box, int h, int *vx, int *vy) +{ + int r = h / 2; + + switch (box) { + case box_simple: + vx[0] = 0; + vy[0] = -r; + vx[1] = 2 * r; + vy[1] = -r; + vx[2] = 2 * r; + vy[2] = r; + vx[3] = 0; + vy[3] = r; + return 4; + case box_right: + vx[0] = 0; + vy[0] = -r; + vx[1] = r; + vy[1] = -r; + vx[2] = 2 * r; + vy[2] = 0; + vx[3] = r; + vy[3] = r; + vx[4] = 0; + vy[4] = r; + return 5; + case box_left: + vx[0] = r; + vy[0] = -r; + vx[1] = 2 * r; + vy[1] = -r; + vx[2] = 2 * r; + vy[2] = r; + vx[3] = r; + vy[3] = r; + vx[4] = 0; + vy[4] = 0; + return 5; + case box_both: + vx[0] = 0; + vy[0] = 0; + vx[1] = r; + vy[1] = -r; + vx[2] = 2 * r; + vy[2] = 0; + vx[3] = r; + vy[3] = r; + return 4; + default: + assert(0); + } +} + + +/* @@@ rx, ry are from text.c; should go to misc.h or misc.c */ + + +static int rx(int x, int y, int rot) +{ + float a = rot / 180.0 * M_PI; + + return cos(a) * x + sin(a) * y; +} + + +static int ry(int x, int y, int rot) +{ + float a = rot / 180.0 * M_PI; + + return -sin(a) * x + cos(a) * y; +} + + +void dwg_hlabel(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 vx[6], vy[6]; + int rot; + int n, i; + + switch (shape) { + case fig_unspec: + n = make_box(box_simple, dim, vx, vy); + break; + case fig_in: + n = make_box(box_left, dim, vx, vy); + break; + case fig_out: + n = make_box(box_right, dim, vx, vy); + break; + case fig_bidir: + n = make_box(box_both, dim, vx, vy); + break; + default: + assert(0); + } + + switch (dir) { + case 0: /* right */ + rot = 180; + txt.hor = text_max; + break; + case 1: /* up */ + rot = 90; + txt.hor = text_min; + break; + case 2: /* left */ + rot = 0; + txt.hor = text_min; + break; + case 3: /* down */ + rot = 270; + txt.hor = text_max; + break; + default: + assert(0); + } + + txt.x += rx((1 + HLABEL_OFFSET_F) * dim, 0, rot); + txt.y += ry((1 + HLABEL_OFFSET_F) * dim, 0, rot); + + for (i = 0; i != n; i++) { + int tmp; + + tmp = x + rx(vx[i], vy[i], rot); + vy[i] = y + ry(vx[i], vy[i], rot); + vx[i] = tmp; + } + + vx[n] = vx[0]; + vy[n] = vy[0]; + + txt.rot = rot % 180; + + text_fig(&txt, COLOR_HLABEL, LAYER_HLABEL); + fig_poly(n + 1, vx, vy, COLOR_HLABEL, COLOR_NONE, LAYER_HLABEL); +} + + void dwg_junction(int x, int y) { fig_circ(x, y, JUNCTION_R, COLOR_NONE, COLOR_WIRE, LAYER_WIRES); diff --git a/sch2fig/dwg.h b/sch2fig/dwg.h index 28e498c..88ce0db 100644 --- a/sch2fig/dwg.h +++ b/sch2fig/dwg.h @@ -19,6 +19,8 @@ void dwg_label(int x, int y, const char *s, int dir, int dim, enum fig_shape shape); +void dwg_hlabel(int x, int y, const char *s, int dir, int dim, + enum fig_shape shape); void dwg_glabel(int x, int y, const char *s, int dir, int dim, enum fig_shape shape); diff --git a/sch2fig/sch.c b/sch2fig/sch.c index 1028dfa..2253410 100644 --- a/sch2fig/sch.c +++ b/sch2fig/sch.c @@ -297,8 +297,8 @@ bool sch_parse(struct sch_ctx *ctx, const char *line) if (sscanf(line, "Text HLabel %d %d %d %d %ms", &ctx->x, &ctx->y, &ctx->dir, &ctx->dim, &s) == 5) { ctx->state = sch_text; - unsupported("Text HLabel"); - ctx->text = NULL; + ctx->shape = decode_shape(s); + ctx->text = dwg_hlabel; return 1; } if (sscanf(line, "Text Label %d %d %d %d", diff --git a/sch2fig/style.h b/sch2fig/style.h index 0b10946..12e4119 100644 --- a/sch2fig/style.h +++ b/sch2fig/style.h @@ -22,6 +22,7 @@ #define COLOR_CYAN3 16 #define COLOR_RED4 18 #define COLOR_RED3 19 +#define COLOR_BROWN2 26 #define COLOR_COMP_DWG COLOR_RED4 #define COLOR_COMP_DWG_BG COLOR_YELLOW @@ -30,6 +31,7 @@ #define COLOR_WIRE COLOR_GREEN4 #define COLOR_NOCONN COLOR_BLUE #define COLOR_GLABEL COLOR_RED4 +#define COLOR_HLABEL COLOR_BROWN2 /* @@@ */ #define COLOR_LABEL COLOR_BLACK #define COLOR_FIELD COLOR_CYAN4 #define COLOR_PIN_NAME COLOR_FIELD @@ -38,6 +40,7 @@ #define FONT_HELVETICA_BOLD 18 #define LAYER_GLABEL 20 +#define LAYER_HLABEL LAYER_GLABEL #define LAYER_LABEL LAYER_GLABEL #define LAYER_TEXT 30 #define LAYER_NOCONN 40 @@ -59,6 +62,7 @@ #define LABEL_OFFSET 15 // eeschema has more like 10 #define GLABEL_OFFSET 20 +#define HLABEL_OFFSET_F 0.4 // * text size #define PIN_NUM_OFFSET 15 // eeschema has more like 10 #endif /* !STYLE_H */