From f3ed4edbd21eb6ce2ccfd848dc6e88d0008e5f0c Mon Sep 17 00:00:00 2001 From: Werner Almesberger Date: Sun, 31 Jul 2016 21:52:35 -0300 Subject: [PATCH] sch2fig/: graphics backend can be selected from the command line --- sch2fig/cairo.c | 1 + sch2fig/fig.c | 1 + sch2fig/gfx.h | 1 + sch2fig/main.c | 47 ++++++++++++++++++++++++++++++++------------- sch2fig/sch2pdf | 5 +++-- sch2fig/test/genpng | 2 +- sch2fig/util.h | 4 ++++ 7 files changed, 45 insertions(+), 16 deletions(-) diff --git a/sch2fig/cairo.c b/sch2fig/cairo.c index a6c287d..38a00a2 100644 --- a/sch2fig/cairo.c +++ b/sch2fig/cairo.c @@ -232,6 +232,7 @@ static void cr_end(void *ctx) const struct gfx_ops cairo_ops = { + .name = "cairo", // .line = cr_line, @@@ later .poly = cr_poly, .circ = cr_circ, diff --git a/sch2fig/fig.c b/sch2fig/fig.c index 82750fd..ef1e90c 100644 --- a/sch2fig/fig.c +++ b/sch2fig/fig.c @@ -268,6 +268,7 @@ static void *fig_init(int argc, char *const *argv) const struct gfx_ops fig_ops = { + .name = "fig", .line = fig_line, .rect = fig_rect, .poly = fig_poly, diff --git a/sch2fig/gfx.h b/sch2fig/gfx.h index 62a476e..bdbb124 100644 --- a/sch2fig/gfx.h +++ b/sch2fig/gfx.h @@ -20,6 +20,7 @@ struct gfx_ops { + const char *name; void (*line)(void *ctx, int sx, int sy, int ex, int ey, int color, unsigned layer); void (*rect)(void *ctx, int sx, int sy, int ex, int ey, diff --git a/sch2fig/main.c b/sch2fig/main.c index 8d46445..a8438e4 100644 --- a/sch2fig/main.c +++ b/sch2fig/main.c @@ -27,6 +27,12 @@ #include "main.h" +static struct gfx_ops const *ops_list[] = { + &fig_ops, + &cairo_ops, +}; + + static bool do_lib_parse(void *user, const char *line) { return lib_parse(user, line); @@ -36,9 +42,11 @@ static bool do_lib_parse(void *user, const char *line) void usage(const char *name) { fprintf(stderr, -"usage: %s [-r] [file.lib ...] file.sch -- driver_options\n\n" -" FIG driver options:\n" -" [-t template.fig] [var=value ...]\n" +"usage: %s [-r] [file.lib ...] file.sch -- driver_spec\n\n" +" FIG driver spec:\n" +" fig [-t template.fig] [var=value ...]\n" +" Cairo driver spec:\n" +" cairo\n" , name); exit(1); } @@ -52,6 +60,7 @@ int main(int argc, char *const *argv) int arg, dashdash; int gfx_argc; char **gfx_argv; + const struct gfx_ops **ops = ops_list; for (dashdash = 1; dashdash != argc; dashdash++) if (!strcmp(argv[dashdash], "--")) @@ -76,19 +85,31 @@ int main(int argc, char *const *argv) file_read(argv[arg], do_lib_parse, &ctx); } - /* - * Special case: if we have no --, we don't subtract it and argc - * becomes 1 (and no 0). - */ - gfx_argc = argc == dashdash ? 1 : argc - dashdash; - gfx_argv = alloc_size(sizeof(const char *) * (gfx_argc + 1)); - gfx_argv[0] = argv[0]; - memcpy(gfx_argv + 1, argv + dashdash + 1, - sizeof(const char *) * gfx_argc); + if (dashdash == argc) { + gfx_argc = 1; + gfx_argv = alloc_size(sizeof(const char *) * 2); + gfx_argv[0] = (char *) (*ops)->name; + gfx_argv[1] = NULL; + } else { + gfx_argc = argc - dashdash - 1; + gfx_argv = alloc_size(sizeof(const char *) * (gfx_argc + 1)); + memcpy(gfx_argv, argv + dashdash + 1, + sizeof(const char *) * (gfx_argc + 1)); + for (ops = ops_list; ops != ARRAY_END(ops_list); ops++) + if (!strcmp((*ops)->name, *gfx_argv)) + goto found; + fprintf(stderr, "graphics backend \"%s\" not found\n", + *gfx_argv); + exit(1); +found: + ; + } + + sch_init(&sch_ctx, recurse); sch_parse(&sch_ctx, argv[dashdash - 1]); - gfx_init(&fig_ops, gfx_argc, gfx_argv); + gfx_init(*ops, gfx_argc, gfx_argv); if (recurse) { const struct sheet *sheet; diff --git a/sch2fig/sch2pdf b/sch2fig/sch2pdf index 58716e7..823ab7e 100755 --- a/sch2fig/sch2pdf +++ b/sch2fig/sch2pdf @@ -56,7 +56,8 @@ while [ "$2" ]; do shift done -./sch2fig $libs "$1" -- $template "TITLE=`basename \"$1\" .sch`" NUMBER=$num | +./sch2fig $libs "$1" \ + -- fig $template "TITLE=`basename \"$1\" .sch`" NUMBER=$num | fig2dev -L pdf >"$out" sheet=false @@ -83,7 +84,7 @@ while read line; do $quiet || echo "$file" 1>&2 ./sch2fig $libs `dirname "$1"`/$file \ - -- $template "TITLE=$name" NUMBER=$num | + -- fig $template "TITLE=$name" NUMBER=$num | fig2dev -L pdf >_tmp.pdf pdfunite "$out" _tmp.pdf _tmp2.pdf mv _tmp2.pdf "$out" diff --git a/sch2fig/test/genpng b/sch2fig/test/genpng index 956ac71..77275b9 100755 --- a/sch2fig/test/genpng +++ b/sch2fig/test/genpng @@ -30,7 +30,7 @@ while [ $sheet -le 38 ]; do file=$dir/$prefix$sn.png $dir/../sch2fig $dir/neo900-ee/hw/neo900.lib \ $dir/kicad-libs/components/powered.lib "$in" \ - -- -t $dir/frame.fig SHEET=$sn | + -- fig -t $dir/frame.fig SHEET=$sn | fig2dev -L png -m 2 >$file sheet=`expr $sheet + 1` done diff --git a/sch2fig/util.h b/sch2fig/util.h index 8849421..ea40ce5 100644 --- a/sch2fig/util.h +++ b/sch2fig/util.h @@ -38,6 +38,10 @@ stralloc_tmp; }) +#define ARRAY_ELEMENTS(a) (sizeof(a) / sizeof(a[0])) +#define ARRAY_END(a) ((a) + ARRAY_ELEMENTS(a)) + + #define swap(a, b) \ ({ typeof(a) _tmp = (a); a = (b); b = _tmp; })