From b781bd6fc85a659185b82c6584c1a42d1e92c880 Mon Sep 17 00:00:00 2001 From: Werner Almesberger Date: Sun, 31 Jul 2016 14:19:43 -0300 Subject: [PATCH] sch2fig/sch.c: split into sch-parse.c and sch-render.c --- sch2fig/Makefile | 3 +- sch2fig/{sch.c => sch-parse.c} | 157 +---------------------------- sch2fig/sch-render.c | 174 +++++++++++++++++++++++++++++++++ 3 files changed, 177 insertions(+), 157 deletions(-) rename sch2fig/{sch.c => sch-parse.c} (74%) create mode 100644 sch2fig/sch-render.c diff --git a/sch2fig/Makefile b/sch2fig/Makefile index f70a278..a16acff 100644 --- a/sch2fig/Makefile +++ b/sch2fig/Makefile @@ -11,7 +11,8 @@ # NAME = sch2fig -OBJS = main.o sch.o lib.o style.o fig.o cairo.o gfx.o dwg.o text.o misc.o +OBJS = main.o sch-parse.o sch-render.o lib.o \ + style.o fig.o cairo.o gfx.o dwg.o text.o misc.o CFLAGS = -g -O -Wall -Wextra -Wno-unused-parameter -Wshadow \ -Wmissing-prototypes -Wmissing-declarations \ diff --git a/sch2fig/sch.c b/sch2fig/sch-parse.c similarity index 74% rename from sch2fig/sch.c rename to sch2fig/sch-parse.c index 2b2c163..e43bdef 100644 --- a/sch2fig/sch.c +++ b/sch2fig/sch-parse.c @@ -1,5 +1,5 @@ /* - * sch.c - Parse Eeschema .sch file + * sch-parse.c - Parse Eeschema .sch file * * Written 2016 by Werner Almesberger * Copyright 2016 by Werner Almesberger @@ -11,7 +11,6 @@ */ -#define _GNU_SOURCE /* for asprintf */ #include #include #include @@ -20,165 +19,11 @@ #include #include "util.h" -#include "misc.h" -#include "style.h" -#include "gfx.h" #include "dwg.h" #include "lib.h" #include "sch.h" -/* ----- Rendering --------------------------------------------------------- */ - - -static void dump_field(const struct comp_field *field, const int m[6]) -{ - struct text txt = field->txt; - int dx, dy; - - dx = txt.x - m[0]; - dy = txt.y - m[3]; - txt.x = mx(dx, dy, m); - txt.y = my(dx, dy, m); - - text_rot(&txt, matrix_to_angle(m)); - - switch (txt.rot) { - case 180: - text_rot(&txt, 180); - txt.hor = text_flip(txt.hor); - txt.vert = text_flip(txt.vert); - break; - case 270: - text_rot(&txt, 180); - txt.vert = text_flip(txt.vert); - txt.hor = text_flip(txt.hor); - break; - default: - break; - } - - if (matrix_is_mirrored(m)) { - if ((txt.rot % 180) == 0) - txt.hor = text_flip(txt.hor); - else - txt.vert = text_flip(txt.vert); - } - - text_fig(&txt, COLOR_FIELD, LAYER_FIELD); -} - - -static void do_hsheet_text(const struct sch_obj *obj, - const struct sch_sheet *sheet) -{ - char *s; - - assert(sheet->sheet && sheet->file); - - struct text sheet_txt = { - .size = sheet->sheet_dim, - .x = obj->x, - .y = obj->y, - .rot = 0, - .hor = text_min, - .vert = text_min, - }; - if (asprintf(&s, "Sheet: %s", sheet->sheet)) {} - sheet_txt.s = s; /* work around "const" mismatch */ - - struct text file_txt = { - .size = sheet->file_dim, - .x = obj->x, - .y = obj->y, - .rot = 0, - .hor = text_min, - .vert = text_max, - }; - if (asprintf(&s, "File: %s", sheet->file)) {} - file_txt.s = s; /* work around "const" mismatch */ - - if (sheet->rotated) { - sheet_txt.rot = file_txt.rot = 90; - sheet_txt.x -= HSHEET_FIELD_OFFSET; - sheet_txt.y += sheet->h; - file_txt.x += sheet->w + HSHEET_FIELD_OFFSET; - file_txt.y += sheet->h; - } else { - sheet_txt.y -= HSHEET_FIELD_OFFSET; - file_txt.y += sheet->h + HSHEET_FIELD_OFFSET; - } - - text_fig(&sheet_txt, COLOR_HSHEET_SHEET, LAYER_HSHEET_FIELD); - text_fig(&file_txt, COLOR_HSHEET_FILE, LAYER_HSHEET_FIELD); - -// free((void *) ctx->sheet); -// free((void *) ctx->file); -} - - -static void render_sheet(const struct sch_obj *obj, - const struct sch_sheet *sheet) -{ - const struct sheet_field *field; - - gfx_rect(obj->x, obj->y, obj->x + sheet->w, obj->y + sheet->h, - COLOR_HSHEET_BOX, COLOR_NONE, LAYER_HSHEET_BOX); - do_hsheet_text(obj, sheet); - - for (field = sheet->fields; field; field = field->next) - dwg_hlabel(obj->x, obj->y, field->s, - field->side, field->dim, - field->shape); - // free(field->s) -} - - -void sch_render(const struct sch_ctx *ctx) -{ - const struct sch_obj *obj; - - for (obj = ctx->objs; obj; obj = obj->next) - switch (obj->type) { - case sch_obj_wire: - { - const struct sch_wire *wire = &obj->u.wire; - - wire->fn(obj->x, obj->y, wire->ex, wire->ey); - } - break; - case sch_obj_junction: - dwg_junction(obj->x, obj->y); - break; - case sch_obj_noconn: - dwg_noconn(obj->x, obj->y); - break; - case sch_obj_text: - { - const struct sch_text *text = &obj->u.text; - - text->fn(obj->x, obj->y, text->s, text->dir, - text->dim, text->shape); - } - break; - case sch_obj_comp: - { - const struct sch_comp *comp = &obj->u.comp; - const struct comp_field *field; - - lib_exec(comp->comp, comp->unit, comp->m); - for (field = comp->fields; field; - field = field->next) - dump_field(field, comp->m); - } - break; - case sch_obj_sheet: - render_sheet(obj, &obj->u.sheet); - break; - } -} - - /* ----- (Global) Labels --------------------------------------------------- */ diff --git a/sch2fig/sch-render.c b/sch2fig/sch-render.c new file mode 100644 index 0000000..15c9f59 --- /dev/null +++ b/sch2fig/sch-render.c @@ -0,0 +1,174 @@ +/* + * sch-render.c - Render schematics + * + * 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. + */ + + +#define _GNU_SOURCE /* for asprintf */ +#include +#include + +#include "misc.h" +#include "style.h" +#include "gfx.h" +#include "dwg.h" +#include "lib.h" +#include "sch.h" + + +/* ----- Rendering --------------------------------------------------------- */ + + +static void dump_field(const struct comp_field *field, const int m[6]) +{ + struct text txt = field->txt; + int dx, dy; + + dx = txt.x - m[0]; + dy = txt.y - m[3]; + txt.x = mx(dx, dy, m); + txt.y = my(dx, dy, m); + + text_rot(&txt, matrix_to_angle(m)); + + switch (txt.rot) { + case 180: + text_rot(&txt, 180); + txt.hor = text_flip(txt.hor); + txt.vert = text_flip(txt.vert); + break; + case 270: + text_rot(&txt, 180); + txt.vert = text_flip(txt.vert); + txt.hor = text_flip(txt.hor); + break; + default: + break; + } + + if (matrix_is_mirrored(m)) { + if ((txt.rot % 180) == 0) + txt.hor = text_flip(txt.hor); + else + txt.vert = text_flip(txt.vert); + } + + text_fig(&txt, COLOR_FIELD, LAYER_FIELD); +} + + +static void do_hsheet_text(const struct sch_obj *obj, + const struct sch_sheet *sheet) +{ + char *s; + + assert(sheet->sheet && sheet->file); + + struct text sheet_txt = { + .size = sheet->sheet_dim, + .x = obj->x, + .y = obj->y, + .rot = 0, + .hor = text_min, + .vert = text_min, + }; + if (asprintf(&s, "Sheet: %s", sheet->sheet)) {} + sheet_txt.s = s; /* work around "const" mismatch */ + + struct text file_txt = { + .size = sheet->file_dim, + .x = obj->x, + .y = obj->y, + .rot = 0, + .hor = text_min, + .vert = text_max, + }; + if (asprintf(&s, "File: %s", sheet->file)) {} + file_txt.s = s; /* work around "const" mismatch */ + + if (sheet->rotated) { + sheet_txt.rot = file_txt.rot = 90; + sheet_txt.x -= HSHEET_FIELD_OFFSET; + sheet_txt.y += sheet->h; + file_txt.x += sheet->w + HSHEET_FIELD_OFFSET; + file_txt.y += sheet->h; + } else { + sheet_txt.y -= HSHEET_FIELD_OFFSET; + file_txt.y += sheet->h + HSHEET_FIELD_OFFSET; + } + + text_fig(&sheet_txt, COLOR_HSHEET_SHEET, LAYER_HSHEET_FIELD); + text_fig(&file_txt, COLOR_HSHEET_FILE, LAYER_HSHEET_FIELD); + +// free((void *) ctx->sheet); +// free((void *) ctx->file); +} + + +static void render_sheet(const struct sch_obj *obj, + const struct sch_sheet *sheet) +{ + const struct sheet_field *field; + + gfx_rect(obj->x, obj->y, obj->x + sheet->w, obj->y + sheet->h, + COLOR_HSHEET_BOX, COLOR_NONE, LAYER_HSHEET_BOX); + do_hsheet_text(obj, sheet); + + for (field = sheet->fields; field; field = field->next) + dwg_hlabel(obj->x, obj->y, field->s, + field->side, field->dim, + field->shape); + // free(field->s) +} + + +void sch_render(const struct sch_ctx *ctx) +{ + const struct sch_obj *obj; + + for (obj = ctx->objs; obj; obj = obj->next) + switch (obj->type) { + case sch_obj_wire: + { + const struct sch_wire *wire = &obj->u.wire; + + wire->fn(obj->x, obj->y, wire->ex, wire->ey); + } + break; + case sch_obj_junction: + dwg_junction(obj->x, obj->y); + break; + case sch_obj_noconn: + dwg_noconn(obj->x, obj->y); + break; + case sch_obj_text: + { + const struct sch_text *text = &obj->u.text; + + text->fn(obj->x, obj->y, text->s, text->dir, + text->dim, text->shape); + } + break; + case sch_obj_comp: + { + const struct sch_comp *comp = &obj->u.comp; + const struct comp_field *field; + + lib_exec(comp->comp, comp->unit, comp->m); + for (field = comp->fields; field; + field = field->next) + dump_field(field, comp->m); + } + break; + case sch_obj_sheet: + render_sheet(obj, &obj->u.sheet); + break; + } +}