1
0
mirror of git://projects.qi-hardware.com/eda-tools.git synced 2024-11-30 07:47:10 +02:00

sch2fig/fig.c: more high-level drawing (labels, etc.) functions to dwg.c

This commit is contained in:
Werner Almesberger 2016-07-28 23:09:38 -03:00
parent 8830b9a223
commit ab59b4f952
6 changed files with 282 additions and 246 deletions

View File

@ -11,7 +11,7 @@
#
NAME = sch2fig
OBJS = main.o sch.o lib.o fig.o text.o misc.o
OBJS = main.o sch.o lib.o fig.o dwg.o text.o misc.o
CFLAGS = -g -Wall -Wextra -Wno-unused-parameter -Wshadow
LIBS = -lm

248
sch2fig/dwg.c Normal file
View File

@ -0,0 +1,248 @@
/*
* dwg.c - Complex drawing functions
*
* 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 <stdbool.h>
#include <stdlib.h>
#include <stdio.h>
#include <assert.h>
#include "util.h"
#include "style.h"
#include "text.h"
#include "fig.h"
#include "dwg.h"
enum box_type { // ___
box_simple, // [___]
box_left, // <___]
box_right, // [___>
box_both, // <___>
};
static enum box_type flip_box(enum box_type box)
{
switch (box) {
case box_simple:
return box_simple;
case box_left:
return box_right;
case box_right:
return box_left;
case box_both:
return box_both;
default:
abort();
}
}
void dwg_label(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_min,
};
int dx = 0, dy = 0;
switch (dir) {
case 0: /* right */
txt.rot = 0;
txt.hor = text_min;
dy = 1;
break;
case 1: /* up */
txt.rot = 90;
txt.hor = text_min;
dx = -1;
break;
case 2: /* left */
txt.rot = 0;
txt.hor = text_max;
dy = 1;
break;
case 3: /* down */
txt.rot = 90;
txt.hor = text_max;
dx = -1;
break;
default:
assert(0);
}
txt.y -= dy * LABEL_OFFSET;
txt.x += dx * LABEL_OFFSET;
text_fig(&txt, COLOR_LABEL, LAYER_LABEL);
}
void dwg_glabel(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 n = 6;
int vx[7];
int vy[7];
int half = (dim >> 1) + GLABEL_OFFSET;
enum box_type box;
int dx, shift_flat, shift_tip;
bool anchor_right = 1;
switch (shape) {
case fig_unspec:
box = box_simple;
break;
case fig_in:
box = box_right;
break;
case fig_out:
box = box_left;
break;
case fig_bidir:
box = box_both;
break;
default:
assert(0);
}
switch (dir) {
case 0: /* left */
txt.rot = 0;
txt.hor = text_max;
dx = -1;
break;
case 1: /* up */
txt.rot = 90;
txt.hor = text_min;
dx = 1;
box = flip_box(box);
anchor_right = !anchor_right;
break;
case 2: /* right */
txt.rot = 0;
txt.hor = text_min;
dx = 1;
box = flip_box(box);
anchor_right = !anchor_right;
break;
case 3: /* down */
txt.rot = 90;
txt.hor = text_max;
dx = -1;
break;
default:
assert(0);
}
shift_flat = dx * GLABEL_OFFSET;
shift_tip = dx * (GLABEL_OFFSET + half);
switch (box) {
case box_simple:
n = 5;
text_shift(&txt, txt.hor, text_mid, shift_flat, 0);
text_rel(&txt, text_min, text_min,
-GLABEL_OFFSET, GLABEL_OFFSET, vx + 1, vy + 1);
text_rel(&txt, text_max, text_min,
GLABEL_OFFSET, GLABEL_OFFSET, vx + 2, vy + 2);
text_rel(&txt, text_max, text_max,
GLABEL_OFFSET, -GLABEL_OFFSET, vx + 3, vy + 3);
text_rel(&txt, text_min, text_max,
-GLABEL_OFFSET, -GLABEL_OFFSET, vx + 4, vy + 4);
break;
case box_right:
text_shift(&txt, txt.hor, text_mid,
anchor_right ? shift_tip : shift_flat, 0);
text_rel(&txt, text_min, text_min,
-GLABEL_OFFSET, GLABEL_OFFSET, vx + 1, vy + 1);
text_rel(&txt, text_max, text_min,
GLABEL_OFFSET, GLABEL_OFFSET, vx + 2, vy + 2);
text_rel(&txt, text_max, text_mid, GLABEL_OFFSET + half, 0,
vx + 3, vy + 3);
text_rel(&txt, text_max, text_max,
GLABEL_OFFSET, -GLABEL_OFFSET, vx + 4, vy + 4);
text_rel(&txt, text_min, text_max,
-GLABEL_OFFSET, -GLABEL_OFFSET, vx + 5, vy + 5);
break;
case box_left:
text_shift(&txt, txt.hor, text_mid,
anchor_right ? shift_flat : shift_tip, 0);
text_rel(&txt, text_min, text_min,
-GLABEL_OFFSET, GLABEL_OFFSET, vx + 1, vy + 1);
text_rel(&txt, text_max, text_min,
GLABEL_OFFSET, GLABEL_OFFSET, vx + 2, vy + 2);
text_rel(&txt, text_max, text_max,
GLABEL_OFFSET, -GLABEL_OFFSET, vx + 3, vy + 3);
text_rel(&txt, text_min, text_max,
-GLABEL_OFFSET, -GLABEL_OFFSET, vx + 4, vy + 4);
text_rel(&txt, text_min, text_mid, -GLABEL_OFFSET- half, 0,
vx + 5, vy + 5);
break;
case box_both:
n = 7;
text_shift(&txt, txt.hor, text_mid, shift_tip, 0);
text_rel(&txt, text_min, text_min,
-GLABEL_OFFSET, GLABEL_OFFSET, vx + 1, vy + 1);
text_rel(&txt, text_max, text_min,
GLABEL_OFFSET, GLABEL_OFFSET, vx + 2, vy + 2);
text_rel(&txt, text_max, text_mid, GLABEL_OFFSET + half, 0,
vx + 3, vy + 3);
text_rel(&txt, text_max, text_max,
GLABEL_OFFSET, -GLABEL_OFFSET, vx + 4, vy + 4);
text_rel(&txt, text_min, text_max,
-GLABEL_OFFSET, -GLABEL_OFFSET, vx + 5, vy + 5);
text_rel(&txt, text_min, text_mid, -GLABEL_OFFSET- half, 0,
vx + 6, vy + 6);
break;
default:
assert(0);
}
text_fig(&txt, COLOR_GLABEL, LAYER_GLABEL);
vx[0] = vx[n - 1];
vy[0] = vy[n - 1];
fig_poly(n, vx, vy, COLOR_GLABEL, COLOR_NONE, LAYER_GLABEL);
}
void dwg_junction(int x, int y)
{
fig_circ(x, y, JUNCTION_R, COLOR_NONE, COLOR_WIRE, LAYER_WIRES);
}
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);
swap(vy[0], vy[1]);
fig_poly(2, vx, vy, COLOR_NOCONN, COLOR_NONE, LAYER_NOCONN);
}

28
sch2fig/dwg.h Normal file
View File

@ -0,0 +1,28 @@
/*
* dwg.h - Complex drawing functions
*
* 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 DWG_H
#define DWG_H
#include "fig.h"
void dwg_label(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);
void dwg_junction(int x, int y);
void dwg_noconn(int x, int y);
#endif /* !DWG_H */

View File

@ -51,239 +51,6 @@ static inline float pt(int x)
/* ----- Schematics items -------------------------------------------------- */
enum box_type { // ___
box_simple, // [___]
box_left, // <___]
box_right, // [___>
box_both, // <___>
};
static enum box_type flip_box(enum box_type box)
{
switch (box) {
case box_simple:
return box_simple;
case box_left:
return box_right;
case box_right:
return box_left;
case box_both:
return box_both;
default:
abort();
}
}
void fig_label(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_min,
};
int dx = 0, dy = 0;
switch (dir) {
case 0: /* right */
txt.rot = 0;
txt.hor = text_min;
dy = 1;
break;
case 1: /* up */
txt.rot = 90;
txt.hor = text_min;
dx = -1;
break;
case 2: /* left */
txt.rot = 0;
txt.hor = text_max;
dy = 1;
break;
case 3: /* down */
txt.rot = 90;
txt.hor = text_max;
dx = -1;
break;
default:
assert(0);
}
txt.y -= dy * LABEL_OFFSET;
txt.x += dx * LABEL_OFFSET;
text_fig(&txt, COLOR_LABEL, LAYER_LABEL);
}
void fig_glabel(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 n = 6;
int vx[7];
int vy[7];
int half = (dim >> 1) + GLABEL_OFFSET;
enum box_type box;
int dx, shift_flat, shift_tip;
bool anchor_right = 1;
switch (shape) {
case fig_unspec:
box = box_simple;
break;
case fig_in:
box = box_right;
break;
case fig_out:
box = box_left;
break;
case fig_bidir:
box = box_both;
break;
default:
assert(0);
}
switch (dir) {
case 0: /* left */
txt.rot = 0;
txt.hor = text_max;
dx = -1;
break;
case 1: /* up */
txt.rot = 90;
txt.hor = text_min;
dx = 1;
box = flip_box(box);
anchor_right = !anchor_right;
break;
case 2: /* right */
txt.rot = 0;
txt.hor = text_min;
dx = 1;
box = flip_box(box);
anchor_right = !anchor_right;
break;
case 3: /* down */
txt.rot = 90;
txt.hor = text_max;
dx = -1;
break;
default:
assert(0);
}
shift_flat = dx * GLABEL_OFFSET;
shift_tip = dx * (GLABEL_OFFSET + half);
switch (box) {
case box_simple:
n = 5;
text_shift(&txt, txt.hor, text_mid, shift_flat, 0);
text_rel(&txt, text_min, text_min,
-GLABEL_OFFSET, GLABEL_OFFSET, vx + 1, vy + 1);
text_rel(&txt, text_max, text_min,
GLABEL_OFFSET, GLABEL_OFFSET, vx + 2, vy + 2);
text_rel(&txt, text_max, text_max,
GLABEL_OFFSET, -GLABEL_OFFSET, vx + 3, vy + 3);
text_rel(&txt, text_min, text_max,
-GLABEL_OFFSET, -GLABEL_OFFSET, vx + 4, vy + 4);
break;
case box_right:
text_shift(&txt, txt.hor, text_mid,
anchor_right ? shift_tip : shift_flat, 0);
text_rel(&txt, text_min, text_min,
-GLABEL_OFFSET, GLABEL_OFFSET, vx + 1, vy + 1);
text_rel(&txt, text_max, text_min,
GLABEL_OFFSET, GLABEL_OFFSET, vx + 2, vy + 2);
text_rel(&txt, text_max, text_mid, GLABEL_OFFSET + half, 0,
vx + 3, vy + 3);
text_rel(&txt, text_max, text_max,
GLABEL_OFFSET, -GLABEL_OFFSET, vx + 4, vy + 4);
text_rel(&txt, text_min, text_max,
-GLABEL_OFFSET, -GLABEL_OFFSET, vx + 5, vy + 5);
break;
case box_left:
text_shift(&txt, txt.hor, text_mid,
anchor_right ? shift_flat : shift_tip, 0);
text_rel(&txt, text_min, text_min,
-GLABEL_OFFSET, GLABEL_OFFSET, vx + 1, vy + 1);
text_rel(&txt, text_max, text_min,
GLABEL_OFFSET, GLABEL_OFFSET, vx + 2, vy + 2);
text_rel(&txt, text_max, text_max,
GLABEL_OFFSET, -GLABEL_OFFSET, vx + 3, vy + 3);
text_rel(&txt, text_min, text_max,
-GLABEL_OFFSET, -GLABEL_OFFSET, vx + 4, vy + 4);
text_rel(&txt, text_min, text_mid, -GLABEL_OFFSET- half, 0,
vx + 5, vy + 5);
break;
case box_both:
n = 7;
text_shift(&txt, txt.hor, text_mid, shift_tip, 0);
text_rel(&txt, text_min, text_min,
-GLABEL_OFFSET, GLABEL_OFFSET, vx + 1, vy + 1);
text_rel(&txt, text_max, text_min,
GLABEL_OFFSET, GLABEL_OFFSET, vx + 2, vy + 2);
text_rel(&txt, text_max, text_mid, GLABEL_OFFSET + half, 0,
vx + 3, vy + 3);
text_rel(&txt, text_max, text_max,
GLABEL_OFFSET, -GLABEL_OFFSET, vx + 4, vy + 4);
text_rel(&txt, text_min, text_max,
-GLABEL_OFFSET, -GLABEL_OFFSET, vx + 5, vy + 5);
text_rel(&txt, text_min, text_mid, -GLABEL_OFFSET- half, 0,
vx + 6, vy + 6);
break;
default:
assert(0);
}
text_fig(&txt, COLOR_GLABEL, LAYER_GLABEL);
vx[0] = vx[n - 1];
vy[0] = vy[n - 1];
fig_poly(n, vx, vy, COLOR_GLABEL, COLOR_NONE, LAYER_GLABEL);
}
void fig_junction(int x, int y)
{
#if 0
// Type Thick Depth StyleV Cx Rx Sx Ex
// 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_WIRE, LAYER_WIRES, cx(x), cy(y), JUNCTION_R, JUNCTION_R,
cx(x), cy(y), cx(x) + JUNCTION_R, cy(y));
#endif
fig_circ(x, y, JUNCTION_R, COLOR_NONE, COLOR_WIRE, LAYER_WIRES);
}
void fig_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);
swap(vy[0], vy[1]);
fig_poly(2, vx, vy, COLOR_NOCONN, COLOR_NONE, LAYER_NOCONN);
}
void fig_wire(int sx, int sy, int ex, int ey)
{
// TypeStyle FillCol AreaFil Cap FwdAr

View File

@ -28,14 +28,6 @@ enum fig_shape {
/* schematics */
void fig_label(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);
void fig_junction(int x, int y);
void fig_noconn(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);

View File

@ -22,6 +22,7 @@
#include "misc.h"
#include "style.h"
#include "fig.h"
#include "dwg.h"
#include "lib.h"
#include "sch.h"
@ -290,7 +291,7 @@ bool sch_parse(struct sch_ctx *ctx, const char *line)
&ctx->x, &ctx->y, &ctx->dir, &ctx->dim, &s) == 5) {
ctx->state = sch_text;
ctx->shape = decode_shape(s);
ctx->text = fig_glabel;
ctx->text = dwg_glabel;
return 1;
}
if (sscanf(line, "Text HLabel %d %d %d %d %ms",
@ -303,7 +304,7 @@ bool sch_parse(struct sch_ctx *ctx, const char *line)
if (sscanf(line, "Text Label %d %d %d %d",
&ctx->x, &ctx->y, &ctx->dir, &ctx->dim) == 4) {
ctx->state = sch_text;
ctx->text = fig_label;
ctx->text = dwg_label;
return 1;
}
if (sscanf(line, "Entry Wire Line%n", &n) == 0 && n) {
@ -322,14 +323,14 @@ bool sch_parse(struct sch_ctx *ctx, const char *line)
/* Connection */
if (sscanf(line, "Connection ~ %d %d", &x, &y) == 2) {
fig_junction(x, y);
dwg_junction(x, y);
return 1;
}
/* NoConn */
if (sscanf(line, "NoConn ~ %d %d", &x, &y) == 2) {
fig_noconn(x, y);
dwg_noconn(x, y);
return 1;
}