1
0
mirror of git://projects.qi-hardware.com/eda-tools.git synced 2024-11-06 06:24:03 +02:00
eda-tools/sch2fig/dwg.c

249 lines
5.4 KiB
C

/*
* 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);
}