mirror of
git://projects.qi-hardware.com/eda-tools.git
synced 2024-11-23 05:31:53 +02:00
sch2fig/: restructure text handing; on-going development
This commit is contained in:
parent
09ae0229c3
commit
cd3ced9cfb
@ -11,7 +11,7 @@
|
||||
#
|
||||
|
||||
NAME = sch2fig
|
||||
OBJS = main.o sch.o lib.o fig.o
|
||||
OBJS = main.o sch.o lib.o fig.o text.o
|
||||
|
||||
CFLAGS = -g -Wall -Wextra -Wno-unused-parameter
|
||||
|
||||
|
@ -13,29 +13,11 @@
|
||||
|
||||
#include <stdio.h>
|
||||
|
||||
#include "style.h"
|
||||
#include "text.h"
|
||||
#include "fig.h"
|
||||
|
||||
|
||||
#define COLOR_BLACK 0
|
||||
#define COLOR_BLUE 1
|
||||
#define COLOR_GREEN4 12
|
||||
#define COLOR_RED4 18
|
||||
#define COLOR_RED3 19
|
||||
|
||||
#define FONT_HELVETICA_BOLD 18
|
||||
|
||||
#define LAYER_TEXT 30
|
||||
#define LAYER_WIRES 50
|
||||
#define LAYER_LINES 60
|
||||
#define LAYER_COMP_DWG 70
|
||||
|
||||
#define WIDTH_WIRE 2
|
||||
#define WIDTH_LINE 2
|
||||
#define WIDTH_COMP_DWG 2
|
||||
|
||||
#define JUNCTION_R 50
|
||||
|
||||
|
||||
/*
|
||||
* FIG works with 1/1200 in
|
||||
* KiCad works with mil
|
||||
@ -55,9 +37,9 @@ static inline int cy(int y)
|
||||
}
|
||||
|
||||
|
||||
static inline int pt(int x)
|
||||
static inline float pt(int x)
|
||||
{
|
||||
return cx(x) * 72 * 1.5 / 1200;
|
||||
return cx(x) * 72 * 1.5 / 1200.0;
|
||||
}
|
||||
|
||||
|
||||
@ -67,7 +49,7 @@ void fig_rect(int sx, int sy, int ex, int ey)
|
||||
// SubTy Color Pen Join FwdAr
|
||||
// Style FillCol AreaFil Cap BwdAr
|
||||
printf("2 2 0 %d %d 7 %d -1 -1 0.0 1 1 -1 0 0 5\n",
|
||||
WIDTH_COMP_DWG, COLOR_RED4, LAYER_COMP_DWG);
|
||||
WIDTH_COMP_DWG, COLOR_COMP_DWG, LAYER_COMP_DWG);
|
||||
printf("\t%d %d %d %d %d %d %d %d %d %d\n",
|
||||
cx(sx), cy(sy), cx(ex), cy(sy), cx(ex), cy(ey), cx(sx), cy(ey),
|
||||
cx(sx), cy(sy));
|
||||
@ -83,7 +65,7 @@ void fig_poly(int points, int x[points], int y[points])
|
||||
// SubTy Color Pen Join FwdAr
|
||||
// Style FillCol AreaFil Cap BwdAr
|
||||
printf("2 1 0 %d %d 7 %d -1 -1 0.0 1 1 -1 0 0 %d\n",
|
||||
WIDTH_COMP_DWG, COLOR_RED4, LAYER_COMP_DWG, points);
|
||||
WIDTH_COMP_DWG, COLOR_COMP_DWG, LAYER_COMP_DWG, points);
|
||||
for (i = 0; i != points; i++) {
|
||||
printf("%c%d %d", ch, cx(x[i]), cy(y[i]));
|
||||
ch = ' ';
|
||||
@ -92,18 +74,6 @@ void fig_poly(int points, int x[points], int y[points])
|
||||
}
|
||||
|
||||
|
||||
void fig_text(int x, int y, const char *s, int dir, int dim,
|
||||
enum fig_shape shape)
|
||||
{
|
||||
// Type Depth FontSiz Height
|
||||
// Just Pen Angle Length
|
||||
// Color Font Flags X Y
|
||||
printf("4 0 %d %d -1 %d %d %d 4 0.0 0.0 %d %d %s\\001\n",
|
||||
COLOR_BLACK, LAYER_TEXT, FONT_HELVETICA_BOLD,
|
||||
pt(dim), 0, cx(x), cy(y), s);
|
||||
}
|
||||
|
||||
|
||||
void fig_glabel(int x, int y, const char *s, int dir, int dim,
|
||||
enum fig_shape shape)
|
||||
{
|
||||
@ -116,7 +86,7 @@ void fig_junction(int x, int y)
|
||||
// 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_GREEN4, LAYER_WIRES, cx(x), cy(y), JUNCTION_R, JUNCTION_R,
|
||||
COLOR_WIRE, LAYER_WIRES, cx(x), cy(y), JUNCTION_R, JUNCTION_R,
|
||||
cx(x), cy(y), cx(x) + JUNCTION_R, cy(y));
|
||||
}
|
||||
|
||||
@ -127,7 +97,7 @@ void fig_wire(int sx, int sy, int ex, int ey)
|
||||
// SubTy Color Pen StyleV Rad BwdAr
|
||||
// Thick Depth Join Points
|
||||
printf("2 1 0 %d %d 7 %d -1 -1 0.0 1 1 -1 0 0 2\n",
|
||||
WIDTH_WIRE, COLOR_GREEN4, LAYER_WIRES);
|
||||
WIDTH_WIRE, COLOR_WIRE, LAYER_WIRES);
|
||||
printf("\t%d %d %d %d\n", cx(sx), cy(sy), cx(ex), cy(ey));
|
||||
}
|
||||
|
||||
@ -137,12 +107,24 @@ void fig_line(int sx, int sy, int ex, int ey)
|
||||
// TypeStyle FillCol AreaFil Cap FwdAr
|
||||
// SubTy Color Pen StyleV Rad BwdAr
|
||||
// Thick Depth Join Points
|
||||
printf("2 1 0 %d %d 7 %d -1 -1 0.0 1 1 -1 0 0 2\n",
|
||||
WIDTH_LINE, COLOR_BLUE, LAYER_LINES);
|
||||
printf("2 1 2 %d %d 7 %d -1 -1 3.0 1 1 -1 0 0 2\n",
|
||||
WIDTH_LINE, COLOR_SHEET_DWG, LAYER_LINES);
|
||||
printf("\t%d %d %d %d\n", cx(sx), cy(sy), cx(ex), cy(ey));
|
||||
}
|
||||
|
||||
|
||||
void fig_text(int x, int y, const char *s, unsigned size,
|
||||
enum text_align align, int rot, unsigned color, unsigned layer)
|
||||
{
|
||||
// Type Depth FontSiz Height
|
||||
// Just Pen Angle Length
|
||||
// Color Font Flags X Y
|
||||
printf("4 %u %d %d -1 %d %f %f 4 0.0 0.0 %d %d %s\\001\n",
|
||||
align, color, layer, FONT_HELVETICA_BOLD,
|
||||
pt(size), rot / 3.1415926 * 180, cx(x), cy(y), s);
|
||||
}
|
||||
|
||||
|
||||
void fig_init(void)
|
||||
{
|
||||
printf("#FIG 3.2\n");
|
||||
@ -150,7 +132,7 @@ void fig_init(void)
|
||||
printf("Center\n");
|
||||
printf("Metric\n");
|
||||
printf("A4\n");
|
||||
printf("10.00\n");
|
||||
printf("100.00\n");
|
||||
printf("Single\n");
|
||||
printf("-2\n");
|
||||
printf("1200 2\n");
|
||||
|
@ -14,6 +14,9 @@
|
||||
#ifndef FIG_H
|
||||
#define FIG_H
|
||||
|
||||
#include "text.h"
|
||||
|
||||
|
||||
enum fig_shape {
|
||||
fig_unspec, // UnSpc
|
||||
fig_in, // Input
|
||||
@ -29,8 +32,6 @@ void fig_poly(int points, int x[points], int y[points]);
|
||||
|
||||
/* schematics */
|
||||
|
||||
void fig_text(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);
|
||||
|
||||
@ -39,6 +40,11 @@ void fig_junction(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);
|
||||
|
||||
/* general */
|
||||
|
||||
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(void);
|
||||
|
117
sch2fig/lib.c
117
sch2fig/lib.c
@ -16,10 +16,16 @@
|
||||
#include <string.h>
|
||||
#include <assert.h>
|
||||
|
||||
#include "style.h"
|
||||
#include "fig.h"
|
||||
#include "text.h"
|
||||
#include "lib.h"
|
||||
|
||||
|
||||
enum text_style {
|
||||
text_normal,
|
||||
};
|
||||
|
||||
struct obj {
|
||||
enum obj_type {
|
||||
obj_poly,
|
||||
@ -51,13 +57,16 @@ struct obj {
|
||||
int thick;
|
||||
char fill;
|
||||
} circ;
|
||||
struct {
|
||||
char orient;
|
||||
struct text_obj {
|
||||
int orient;
|
||||
int x, y;
|
||||
int dim;
|
||||
char *s;
|
||||
enum text_style style;
|
||||
char hor_align;
|
||||
char vert_align;
|
||||
} text;
|
||||
struct {
|
||||
struct pin_obj {
|
||||
char *name;
|
||||
char *number;
|
||||
int x, y;
|
||||
@ -112,6 +121,70 @@ static void draw_poly(const struct poly *poly, int m[6])
|
||||
}
|
||||
|
||||
|
||||
static void draw_pin(const struct pin_obj *pin, int m[6])
|
||||
{
|
||||
int x[2], y[2];
|
||||
int dx = 0, dy = 0;
|
||||
|
||||
switch (pin->orient) {
|
||||
case 'U':
|
||||
dy = pin->length;
|
||||
break;
|
||||
case 'D':
|
||||
dy = -pin->length;
|
||||
break;
|
||||
case 'R':
|
||||
dx = pin->length;
|
||||
break;
|
||||
case 'L':
|
||||
dx = -pin->length;
|
||||
break;
|
||||
default:
|
||||
abort();
|
||||
}
|
||||
x[0] = mx(pin->x, pin->y, m);
|
||||
y[0] = my(pin->x, pin->y, m);
|
||||
x[1] = mx(pin->x + dx, pin->y + dy, m);
|
||||
y[1] = my(pin->x + dx, pin->y + dy, m);
|
||||
fig_poly(2, x, y);
|
||||
}
|
||||
|
||||
|
||||
static bool eq(int m[6], int xx, int xy, int yx, int yy)
|
||||
{
|
||||
return m[1] == xx && m[2] == xy && m[4] == yx && m[5] == yy;
|
||||
}
|
||||
|
||||
|
||||
static unsigned matrix_to_angle(int m[6])
|
||||
{
|
||||
if (eq(m, 1, 0, 0, -1))
|
||||
return 0;
|
||||
if (eq(m, 0, -1, -1, 0))
|
||||
return 90;
|
||||
if (eq(m, 0, 1, 1, 0))
|
||||
return 270;
|
||||
fprintf(stderr, "unrecognized matrix %d %d %d %d\n",
|
||||
m[1], m[2], m[4], m[5]);
|
||||
exit(1);
|
||||
}
|
||||
|
||||
|
||||
static void draw_text(const struct text_obj *text, int m[6])
|
||||
{
|
||||
struct text txt = {
|
||||
.s = text->s,
|
||||
.size = text->dim,
|
||||
.x = mx(text->x, text->y, m),
|
||||
.y = my(text->x, text->y, m),
|
||||
.rot = matrix_to_angle(m),
|
||||
.hor = text_mid,
|
||||
.vert = text_mid,
|
||||
};
|
||||
text_fig(&txt, COLOR_COMP_DWG, WIDTH_COMP_DWG);
|
||||
}
|
||||
|
||||
|
||||
static void draw(const struct obj *obj, int m[6])
|
||||
{
|
||||
switch (obj->type) {
|
||||
@ -127,9 +200,13 @@ static void draw(const struct obj *obj, int m[6])
|
||||
break;
|
||||
case obj_circ:
|
||||
break;
|
||||
case obj_arc:
|
||||
break;
|
||||
case obj_text:
|
||||
draw_text(&obj->u.text, m);
|
||||
break;
|
||||
case obj_pin:
|
||||
draw_pin(&obj->u.pin, m);
|
||||
break;
|
||||
default:
|
||||
abort();
|
||||
@ -157,6 +234,14 @@ found:
|
||||
}
|
||||
|
||||
|
||||
static enum text_style decode_style(const char *s)
|
||||
{
|
||||
if (!strcmp(s, "Normal"))
|
||||
return text_normal;
|
||||
abort();
|
||||
}
|
||||
|
||||
|
||||
static bool parse_poly(struct obj *obj, const char *line, int points)
|
||||
{
|
||||
int i, n;
|
||||
@ -183,6 +268,8 @@ bool lib_parse(struct lib_ctx *ctx, const char *line)
|
||||
unsigned points;
|
||||
struct comp *comp;
|
||||
struct obj *obj;
|
||||
char *style;
|
||||
unsigned zero1, zero2;
|
||||
|
||||
ctx->lineno++;
|
||||
|
||||
@ -245,10 +332,26 @@ bool lib_parse(struct lib_ctx *ctx, const char *line)
|
||||
obj->type = obj_arc;
|
||||
return 1; // @@@
|
||||
}
|
||||
if (sscanf(line, "T %c %d %d %d %u %u %ms",
|
||||
n = sscanf(line,
|
||||
"T %d %d %d %d %u %u %u \"%m[^\"]\" %ms %u %c %c",
|
||||
&obj->u.text.orient, &obj->u.text.x, &obj->u.text.y,
|
||||
&obj->u.text.dim, &obj->unit, &obj->convert,
|
||||
&obj->u.text.s) == 7) {
|
||||
&obj->u.text.dim, &zero1, &obj->unit, &obj->convert,
|
||||
&obj->u.text.s, &style, &zero2,
|
||||
&obj->u.text.hor_align, &obj->u.text.vert_align);
|
||||
if (n != 12)
|
||||
n = sscanf(line,
|
||||
"T %d %d %d %d %u %u %u %ms %ms %u %c %c",
|
||||
&obj->u.text.orient, &obj->u.text.x, &obj->u.text.y,
|
||||
&obj->u.text.dim, &zero1, &obj->unit, &obj->convert,
|
||||
&obj->u.text.s, &style, &zero2,
|
||||
&obj->u.text.hor_align, &obj->u.text.vert_align);
|
||||
if (n == 12) {
|
||||
if (zero1 || zero2) {
|
||||
fprintf(stderr, "%u: only understand 0 x x\n",
|
||||
ctx->lineno);
|
||||
exit(1);
|
||||
}
|
||||
obj->u.text.style = decode_style(style);
|
||||
obj->type = obj_text;
|
||||
return 1;
|
||||
}
|
||||
@ -273,5 +376,5 @@ bool lib_parse(struct lib_ctx *ctx, const char *line)
|
||||
void lib_init(struct lib_ctx *ctx)
|
||||
{
|
||||
ctx->state = lib_skip;
|
||||
ctx->lineno++;
|
||||
ctx->lineno = 0;
|
||||
}
|
||||
|
@ -16,6 +16,7 @@
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
|
||||
#include "style.h"
|
||||
#include "fig.h"
|
||||
#include "lib.h"
|
||||
#include "sch.h"
|
||||
@ -46,6 +47,13 @@ static enum fig_shape decode_shape(const char *s)
|
||||
}
|
||||
|
||||
|
||||
static void draw_text(int x, int y, const char *s, int dir, int dim,
|
||||
enum fig_shape shape)
|
||||
{
|
||||
fig_text(x, y, s, dim, text_min, 0, COLOR_TEXT, LAYER_TEXT);
|
||||
}
|
||||
|
||||
|
||||
bool sch_parse(struct sch_ctx *ctx, const char *line)
|
||||
{
|
||||
int n = 0;
|
||||
@ -71,7 +79,7 @@ bool sch_parse(struct sch_ctx *ctx, const char *line)
|
||||
if (sscanf(line, "Text Notes %d %d %d %d",
|
||||
&ctx->x, &ctx->y, &ctx->dir, &ctx->dim) == 4) {
|
||||
ctx->state = sch_text;
|
||||
ctx->text = fig_text;
|
||||
ctx->text = draw_text;
|
||||
return 1;
|
||||
}
|
||||
if (sscanf(line, "Text GLabel %d %d %d %d %ms",
|
||||
|
40
sch2fig/style.h
Normal file
40
sch2fig/style.h
Normal file
@ -0,0 +1,40 @@
|
||||
/*
|
||||
* style.h - FIG drawing style
|
||||
*
|
||||
* 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 STYLE_H
|
||||
#define STYLE_H
|
||||
|
||||
#define COLOR_BLACK 0
|
||||
#define COLOR_BLUE 1
|
||||
#define COLOR_GREEN4 12
|
||||
#define COLOR_RED4 18
|
||||
#define COLOR_RED3 19
|
||||
|
||||
#define COLOR_COMP_DWG COLOR_RED4
|
||||
#define COLOR_SHEET_DWG COLOR_BLUE
|
||||
#define COLOR_TEXT COLOR_BLACK
|
||||
#define COLOR_WIRE COLOR_GREEN4
|
||||
|
||||
#define FONT_HELVETICA_BOLD 18
|
||||
|
||||
#define LAYER_TEXT 30
|
||||
#define LAYER_WIRES 50
|
||||
#define LAYER_LINES 60
|
||||
#define LAYER_COMP_DWG 70
|
||||
|
||||
#define WIDTH_WIRE 2
|
||||
#define WIDTH_LINE 2
|
||||
#define WIDTH_COMP_DWG 2
|
||||
|
||||
#define JUNCTION_R 40
|
||||
|
||||
#endif /* !STYLE_H */
|
73
sch2fig/text.c
Normal file
73
sch2fig/text.c
Normal file
@ -0,0 +1,73 @@
|
||||
/*
|
||||
* text.c - FIG text object
|
||||
*
|
||||
* 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 <stdlib.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
|
||||
#include "fig.h"
|
||||
#include "text.h"
|
||||
|
||||
|
||||
void text_init(struct text *txt)
|
||||
{
|
||||
txt->s = NULL;
|
||||
txt->size = 0;
|
||||
txt->x = txt->y = 0;
|
||||
txt->rot = 0;
|
||||
txt->hor = text_mid;
|
||||
txt->vert = text_mid;
|
||||
}
|
||||
|
||||
|
||||
void text_free(struct text *txt)
|
||||
{
|
||||
free((void *) txt->s);
|
||||
}
|
||||
|
||||
void text_set(struct text *txt, const char *s)
|
||||
{
|
||||
free((void *) txt->s);
|
||||
txt->s = strdup(s);
|
||||
}
|
||||
|
||||
|
||||
void text_fig(struct text *txt, int color, unsigned layer)
|
||||
{
|
||||
int x = txt->x;
|
||||
int y = txt->y;
|
||||
|
||||
switch (txt->vert) {
|
||||
case text_min:
|
||||
break;
|
||||
case text_mid:
|
||||
y += txt->size >> 1;
|
||||
break;
|
||||
case text_max:
|
||||
y += txt->size;
|
||||
break;
|
||||
default:
|
||||
abort();
|
||||
}
|
||||
fig_text(x, y, txt->s, txt->size, txt->hor, txt->rot, color, layer);
|
||||
}
|
||||
|
||||
|
||||
void text_rel_x(struct text *txt, int x, int y)
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
void text_rel_y(struct text *txt, int x, int y)
|
||||
{
|
||||
}
|
43
sch2fig/text.h
Normal file
43
sch2fig/text.h
Normal file
@ -0,0 +1,43 @@
|
||||
/*
|
||||
* text.h - FIG text object
|
||||
*
|
||||
* 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 TEXT_H
|
||||
#define TEXT_H
|
||||
|
||||
|
||||
/* use constants of FIG text sub_type */
|
||||
|
||||
enum text_align {
|
||||
text_min = 0, // left or bottom
|
||||
text_mid = 1, // center
|
||||
text_max = 2, // right or top
|
||||
};
|
||||
|
||||
struct text {
|
||||
const char *s;
|
||||
int size;
|
||||
int x, y;
|
||||
int rot;
|
||||
enum text_align hor;
|
||||
enum text_align vert;
|
||||
};
|
||||
|
||||
void text_init(struct text *txt);
|
||||
void text_free(struct text *txt);
|
||||
void text_set(struct text *txt, const char *s);
|
||||
|
||||
void text_fig(struct text *txt, int color, unsigned layer);
|
||||
void text_rel_x(struct text *txt, int x, int y);
|
||||
void text_rel_y(struct text *txt, int x, int y);
|
||||
|
||||
#endif /* !TEXT_H */
|
Loading…
Reference in New Issue
Block a user