From 4267efae0d5702006feaf5a9d46f148020637bf0 Mon Sep 17 00:00:00 2001 From: Werner Almesberger Date: Sat, 30 Jul 2016 21:50:02 -0300 Subject: [PATCH] sch2fig/: abstract graphical output interface --- sch2fig/Makefile | 2 +- sch2fig/dwg.c | 16 +++++----- sch2fig/fig.c | 33 ++++++++++++++++----- sch2fig/fig.h | 22 ++------------ sch2fig/gfx.c | 76 ++++++++++++++++++++++++++++++++++++++++++++++++ sch2fig/gfx.h | 54 ++++++++++++++++++++++++++++++++++ sch2fig/lib.c | 24 +++++++-------- sch2fig/main.c | 3 +- sch2fig/sch.c | 6 ++-- sch2fig/text.c | 4 +-- 10 files changed, 185 insertions(+), 55 deletions(-) create mode 100644 sch2fig/gfx.c create mode 100644 sch2fig/gfx.h diff --git a/sch2fig/Makefile b/sch2fig/Makefile index 6784e72..aff6edd 100644 --- a/sch2fig/Makefile +++ b/sch2fig/Makefile @@ -11,7 +11,7 @@ # NAME = sch2fig -OBJS = main.o sch.o lib.o fig.o dwg.o text.o misc.o +OBJS = main.o sch.o lib.o fig.o gfx.o dwg.o text.o misc.o CFLAGS = -g -O -Wall -Wextra -Wno-unused-parameter -Wshadow LIBS = -lm diff --git a/sch2fig/dwg.c b/sch2fig/dwg.c index ecef942..27b18cd 100644 --- a/sch2fig/dwg.c +++ b/sch2fig/dwg.c @@ -20,7 +20,7 @@ #include "misc.h" #include "style.h" #include "text.h" -#include "fig.h" +#include "gfx.h" #include "dwg.h" @@ -231,7 +231,7 @@ void dwg_glabel(int x, int y, const char *s, int dir, int dim, vx[0] = vx[n - 1]; vy[0] = vy[n - 1]; - fig_poly(n, vx, vy, COLOR_GLABEL, COLOR_NONE, LAYER_GLABEL); + gfx_poly(n, vx, vy, COLOR_GLABEL, COLOR_NONE, LAYER_GLABEL); } @@ -361,7 +361,7 @@ void dwg_hlabel(int x, int y, const char *s, int dir, int dim, txt.rot = rot % 180; text_fig(&txt, COLOR_HLABEL, LAYER_HLABEL); - fig_poly(n + 1, vx, vy, COLOR_HLABEL, COLOR_NONE, LAYER_HLABEL); + gfx_poly(n + 1, vx, vy, COLOR_HLABEL, COLOR_NONE, LAYER_HLABEL); } @@ -370,7 +370,7 @@ void dwg_hlabel(int x, int y, const char *s, int dir, int dim, void dwg_junction(int x, int y) { - fig_circ(x, y, JUNCTION_R, COLOR_NONE, COLOR_WIRE, LAYER_WIRES); + gfx_circ(x, y, JUNCTION_R, COLOR_NONE, COLOR_WIRE, LAYER_WIRES); } @@ -379,9 +379,9 @@ void dwg_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); + gfx_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); + gfx_poly(2, vx, vy, COLOR_NOCONN, COLOR_NONE, LAYER_NOCONN); } @@ -394,7 +394,7 @@ void dwg_wire(int sx, int sy, int ex, int ey) int vy[] = { sy, ey }; // WIDTH_WIRE - fig_poly(2, vx, vy, COLOR_WIRE, COLOR_NONE, LAYER_WIRES); + gfx_poly(2, vx, vy, COLOR_WIRE, COLOR_NONE, LAYER_WIRES); } @@ -404,5 +404,5 @@ void dwg_bus(int sx, int sy, int ex, int ey) int vy[] = { sy, ey }; // WIDTH_BUS - fig_poly(2, vx, vy, COLOR_BUS, COLOR_NONE, LAYER_BUSSES); + gfx_poly(2, vx, vy, COLOR_BUS, COLOR_NONE, LAYER_BUSSES); } diff --git a/sch2fig/fig.c b/sch2fig/fig.c index a95b17e..481c733 100644 --- a/sch2fig/fig.c +++ b/sch2fig/fig.c @@ -51,7 +51,7 @@ static inline float pt(int x) /* ----- Schematics items -------------------------------------------------- */ -void fig_line(int sx, int sy, int ex, int ey) +void fig_line(void *ctx, int sx, int sy, int ex, int ey) { // TypeStyle FillCol AreaFil Cap FwdAr // SubTy Color Pen StyleV Rad BwdAr @@ -65,7 +65,7 @@ void fig_line(int sx, int sy, int ex, int ey) /* ----- General items ----------------------------------------------------- */ -void fig_rect(int sx, int sy, int ex, int ey, +void fig_rect(void *ctx, int sx, int sy, int ex, int ey, int color, int fill_color, unsigned layer) { // Type Thick Depth StyleV Rad @@ -80,7 +80,7 @@ void fig_rect(int sx, int sy, int ex, int ey, } -void fig_poly(int points, int x[points], int y[points], +void fig_poly(void *ctx, int points, int x[points], int y[points], int color, int fill_color, unsigned layer) { int i; @@ -100,7 +100,8 @@ void fig_poly(int points, int x[points], int y[points], } -void fig_circ(int x, int y, int r, int color, int fill_color, unsigned layer) +void fig_circ(void *ctx, 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 @@ -129,7 +130,7 @@ static int ay(int x, int y, int r, int angle) } -void fig_arc(int x, int y, int r, int sa, int ea, +void fig_arc(void *ctx, int x, int y, int r, int sa, int ea, int color, int fill_color, unsigned layer) { int ma = (sa + ea) / 2; @@ -147,7 +148,7 @@ void fig_arc(int x, int y, int r, int sa, int ea, } -void fig_text(int x, int y, const char *s, unsigned size, +void fig_text(void *ctx, int x, int y, const char *s, unsigned size, enum text_align align, int rot, unsigned color, unsigned layer) { // Type Depth FontSiz Height @@ -201,14 +202,14 @@ static bool apply_vars(char *buf, int n_vars, const char **vars) } -void fig_init(const char *template, int n_vars, const char **vars) +void *fig_init(const char *template, int n_vars, const char **vars) { FILE *file; char buf[1000]; if (!template) { fig_header(); - return; + return NULL; } file = fopen(template, "r"); @@ -221,4 +222,20 @@ void fig_init(const char *template, int n_vars, const char **vars) printf("%s", buf); } fclose(file); + + return NULL; } + + +/* ----- Operations -------------------------------------------------------- */ + + +const struct gfx_ops fig_ops = { + .line = fig_line, + .rect = fig_rect, + .poly = fig_poly, + .circ = fig_circ, + .arc = fig_arc, + .text = fig_text, + .init = fig_init, +}; diff --git a/sch2fig/fig.h b/sch2fig/fig.h index 5d3608e..6e6a361 100644 --- a/sch2fig/fig.h +++ b/sch2fig/fig.h @@ -14,27 +14,9 @@ #ifndef FIG_H #define FIG_H -#include "text.h" +#include "gfx.h" -/* schematics */ - -void fig_line(int sx, int sy, int ex, int ey); - -/* general */ - -void fig_rect(int sx, int sy, int ex, int ey, - int color, int fill_color, unsigned layer); -void fig_poly(int points, int x[points], int y[points], - int color, int fill_color, unsigned layer); -void fig_circ(int x, int y, int r, int color, int fill_color, unsigned layer); -void fig_arc(int x, int y, int r, int sa, int ea, - int color, int fill_color, unsigned layer); -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(const char *template, int n_vars, const char **vars); +extern const struct gfx_ops fig_ops; #endif /* !FIG_H */ diff --git a/sch2fig/gfx.c b/sch2fig/gfx.c new file mode 100644 index 0000000..6142ae1 --- /dev/null +++ b/sch2fig/gfx.c @@ -0,0 +1,76 @@ +/* + * gfx.c - Generate graphical 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 "text.h" +#include "gfx.h" + + +static const struct gfx_ops *gfx_ops; +static void *gfx_ctx; + + +void gfx_line(int sx, int sy, int ex, int ey) +{ + gfx_ops->line(gfx_ctx, sx, sy, ex, ey); +} + + +void gfx_rect(int sx, int sy, int ex, int ey, + int color, int fill_color, unsigned layer) +{ + if (gfx_ops->rect) { + gfx_ops->rect(gfx_ctx, sx, sy, ex, ey, + color, fill_color, layer); + return; + } + + int vx[] = { sx, ex, ex, sx, sx }; + int vy[] = { sy, sy, ey, ey, sy }; + + gfx_poly(5, vx, vy, color, fill_color, layer); +} + + +void gfx_poly(int points, int x[points], int y[points], + int color, int fill_color, unsigned layer) +{ + gfx_ops->poly(gfx_ctx, points, x, y, color, fill_color, layer); +} + + +void gfx_circ(int x, int y, int r, int color, int fill_color, unsigned layer) +{ + gfx_ops->circ(gfx_ctx, x, y, r, color, fill_color, layer); +} + + +void gfx_arc(int x, int y, int r, int sa, int ea, + int color, int fill_color, unsigned layer) +{ + gfx_ops->arc(gfx_ctx, x, y, r, sa, ea, color, fill_color, layer); +} + + +void gfx_text(int x, int y, const char *s, unsigned size, + enum text_align align, int rot, unsigned color, unsigned layer) +{ + gfx_ops->text(gfx_ctx, x, y, s, size, align, rot, color, layer); +} + + +void gfx_init(const struct gfx_ops *ops, + const char *template, int n_vars, const char **vars) +{ + gfx_ops = ops; + gfx_ctx = ops->init(template, n_vars, vars); +} diff --git a/sch2fig/gfx.h b/sch2fig/gfx.h new file mode 100644 index 0000000..c6f0a17 --- /dev/null +++ b/sch2fig/gfx.h @@ -0,0 +1,54 @@ +/* + * gfx.h - Generate graphical 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. + */ + + +#ifndef GFX_H +#define GFX_H + +#include "text.h" + + +struct gfx_ops { + void (*line)(void *ctx, int sx, int sy, int ex, int ey); + void (*rect)(void *ctx, int sx, int sy, int ex, int ey, + int color, int fill_color, unsigned layer); + void (*poly)(void *ctx, int points, int x[points], int y[points], + int color, int fill_color, unsigned layer); + void (*circ)(void *ctx, int x, int y, int r, + int color, int fill_color, unsigned layer); + void (*arc)(void *ctx, int x, int y, int r, int sa, int ea, + int color, int fill_color, unsigned layer); + void (*text)(void *ctx, int x, int y, const char *s, unsigned size, + enum text_align align, int rot, unsigned color, unsigned layer); + void *(*init)(const char *template, int n_vars, const char **vars); +}; + + +/* wrappers */ + +void gfx_line(int sx, int sy, int ex, int ey); +void gfx_rect(int sx, int sy, int ex, int ey, + int color, int fill_color, unsigned layer); +void gfx_poly(int points, int x[points], int y[points], + int color, int fill_color, unsigned layer); +void gfx_circ(int x, int y, int r, int color, int fill_color, unsigned layer); +void gfx_arc(int x, int y, int r, int sa, int ea, + int color, int fill_color, unsigned layer); +void gfx_text(int x, int y, const char *s, unsigned size, + enum text_align align, int rot, unsigned color, unsigned layer); + +/* inititalization */ + +void gfx_init(const struct gfx_ops *ops, + const char *template, int n_vars, const char **vars); + +#endif /* !GFX_H */ diff --git a/sch2fig/lib.c b/sch2fig/lib.c index a3adac6..4f57cc6 100644 --- a/sch2fig/lib.c +++ b/sch2fig/lib.c @@ -20,7 +20,7 @@ #include "util.h" #include "misc.h" #include "style.h" -#include "fig.h" +#include "gfx.h" #include "text.h" #include "sch.h" #include "lib.h" @@ -114,17 +114,17 @@ static void draw_poly(const struct poly_obj *poly, int m[6]) y[i] = my(poly->x[i], poly->y[i], m); } - fig_poly(n, x, y, COLOR_COMP_DWG, COLOR_NONE, LAYER_COMP_DWG); + gfx_poly(n, x, y, COLOR_COMP_DWG, COLOR_NONE, LAYER_COMP_DWG); switch (poly->fill) { case 'N': break; case 'F': - fig_poly(n, x, y, COLOR_NONE, COLOR_COMP_DWG, + gfx_poly(n, x, y, COLOR_NONE, COLOR_COMP_DWG, LAYER_COMP_DWG_BG); break; case 'f': - fig_poly(n, x, y, COLOR_NONE, COLOR_COMP_DWG_BG, + gfx_poly(n, x, y, COLOR_NONE, COLOR_COMP_DWG_BG, LAYER_COMP_DWG_BG); break; default: @@ -140,17 +140,17 @@ static void draw_rect(const struct rect_obj *rect, int m[6]) int ex = mx(rect->ex, rect->ey, m); int ey = my(rect->ex, rect->ey, m); - fig_rect(sx, sy, ex, ey, COLOR_COMP_DWG, COLOR_NONE, LAYER_COMP_DWG); + gfx_rect(sx, sy, ex, ey, COLOR_COMP_DWG, COLOR_NONE, LAYER_COMP_DWG); switch (rect->fill) { case 'N': break; case 'F': - fig_rect(sx, sy, ex, ey, COLOR_NONE, COLOR_COMP_DWG, + gfx_rect(sx, sy, ex, ey, COLOR_NONE, COLOR_COMP_DWG, LAYER_COMP_DWG_BG); break; case 'f': - fig_rect(sx, sy, ex, ey, COLOR_NONE, COLOR_COMP_DWG_BG, + gfx_rect(sx, sy, ex, ey, COLOR_NONE, COLOR_COMP_DWG_BG, LAYER_COMP_DWG_BG); break; default: @@ -165,17 +165,17 @@ static void draw_circ(const struct circ_obj *circ, int m[6]) int y = my(circ->x, circ->y, m); int r = circ->r; - fig_circ(x, y, r, COLOR_COMP_DWG, COLOR_NONE, LAYER_COMP_DWG); + gfx_circ(x, y, r, COLOR_COMP_DWG, COLOR_NONE, LAYER_COMP_DWG); switch (circ->fill) { case 'N': break; case 'F': - fig_circ(x, y, r, COLOR_NONE, COLOR_COMP_DWG, + gfx_circ(x, y, r, COLOR_NONE, COLOR_COMP_DWG, LAYER_COMP_DWG_BG); break; case 'f': - fig_circ(x, y, r, COLOR_NONE, COLOR_COMP_DWG_BG, + gfx_circ(x, y, r, COLOR_NONE, COLOR_COMP_DWG_BG, LAYER_COMP_DWG_BG); break; default: @@ -205,7 +205,7 @@ static void draw_arc(const struct arc_obj *arc, int m[6]) } } - fig_arc(x, y, arc->r, sa, ea, + gfx_arc(x, y, arc->r, sa, ea, COLOR_COMP_DWG, COLOR_NONE, LAYER_COMP_DWG); assert(arc->fill == 'N'); @@ -358,7 +358,7 @@ static void draw_pin(const struct comp *comp, const struct pin_obj *pin, y[0] = my(pin->x, pin->y, m); x[1] = mx(pin->x + dx * pin->length, pin->y + dy * pin->length, m); y[1] = my(pin->x + dx * pin->length, pin->y + dy * pin->length, m); - fig_poly(2, x, y, COLOR_COMP_DWG, COLOR_NONE, LAYER_COMP_DWG); + gfx_poly(2, x, y, COLOR_COMP_DWG, COLOR_NONE, LAYER_COMP_DWG); if (comp->show_pin_name) draw_pin_name(comp, pin, m, dx, dy, rot, hor); diff --git a/sch2fig/main.c b/sch2fig/main.c index d0ef7de..82366e9 100644 --- a/sch2fig/main.c +++ b/sch2fig/main.c @@ -17,6 +17,7 @@ #include #include "fig.h" +#include "gfx.h" #include "lib.h" #include "sch.h" @@ -92,7 +93,7 @@ int main(int argc, char **argv) if (argc - optind < 1) usage(*argv); - fig_init(template, n_vars, vars); + gfx_init(&fig_ops, template, n_vars, vars); for (arg = optind; arg != argc; arg++) { if (arg == argc - 1) { struct sch_ctx ctx; diff --git a/sch2fig/sch.c b/sch2fig/sch.c index e0ece45..032f133 100644 --- a/sch2fig/sch.c +++ b/sch2fig/sch.c @@ -22,7 +22,7 @@ #include "util.h" #include "misc.h" #include "style.h" -#include "fig.h" +#include "gfx.h" #include "dwg.h" #include "lib.h" #include "sch.h" @@ -479,7 +479,7 @@ bool sch_parse(struct sch_ctx *ctx, const char *line) } if (sscanf(line, "Wire Notes Line%n", &n) == 0 && n) { ctx->state = sch_wire; - ctx->wire = fig_line; + ctx->wire = gfx_line; return 1; } @@ -554,7 +554,7 @@ bool sch_parse(struct sch_ctx *ctx, const char *line) } if (sscanf(line, "S %d %d %u %u", &ctx->x, &ctx->y, &ctx->w, &ctx->h) == 4) { - fig_rect(ctx->x, ctx->y, + gfx_rect(ctx->x, ctx->y, ctx->x + ctx->w, ctx->y + ctx->h, COLOR_HSHEET_BOX, COLOR_NONE, LAYER_HSHEET_BOX); return 1; diff --git a/sch2fig/text.c b/sch2fig/text.c index 107cd8a..20ac45d 100644 --- a/sch2fig/text.c +++ b/sch2fig/text.c @@ -19,7 +19,7 @@ #include "util.h" #include "misc.h" #include "style.h" -#include "fig.h" +#include "gfx.h" #include "text.h" @@ -107,7 +107,7 @@ void text_fig(const struct text *txt, int color, unsigned layer) if (!s) break; tmp = NULL; - fig_text(x, y, s, txt->size, txt->hor, txt->rot, color, layer); + gfx_text(x, y, s, txt->size, txt->hor, txt->rot, color, layer); x += rx(0, NEWLINE_SKIP * txt->size, txt->rot); y += ry(0, NEWLINE_SKIP * txt->size, txt->rot); }