summaryrefslogtreecommitdiff
path: root/gfx/gfx.c
blob: 01d04f3925cef9315e9c6078a9c1c45b06f36315 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
/*
 * gfx/gfx.c - Generate graphical output for Eeschema items
 *
 * 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 <unistd.h>	/* for optind */

#include "misc/util.h"
#include "gfx/style.h"
#include "gfx/text.h"
#include "gfx/gfx.h"


struct gfx {
	const struct gfx_ops *ops;
	void *user;
	enum gfx_extra extra;
};


/* ----- Wrappers for graphics primitives ---------------------------------- */


void gfx_line(struct gfx *gfx,
    int sx, int sy, int ex, int ey, int color, unsigned layer)
{
	if (gfx->ops->line) {
		gfx->ops->line(gfx->user, sx, sy, ex, ey, color, layer);
		return;
	}

	int vx[] = { sx, ex };
	int vy[] = { sy, ey };

	gfx_poly(gfx, 2, vx, vy, color, COLOR_NONE, layer);
}


void gfx_rect(struct gfx *gfx, int sx, int sy, int ex, int ey,
    int color, int fill_color, unsigned layer)
{
	if (gfx->ops->rect) {
		gfx->ops->rect(gfx->user, sx, sy, ex, ey,
		    color, fill_color, layer);
		return;
	}

	int vx[] = { sx, ex, ex, sx, sx };
	int vy[] = { sy, sy, ey, ey, sy };

	gfx_poly(gfx, 5, vx, vy, color, fill_color, layer);
}


void gfx_poly(struct gfx *gfx,
    int points, const int x[points], const int y[points],
    int color, int fill_color, unsigned layer)
{
	gfx->ops->poly(gfx->user, points, x, y, color, fill_color, layer);
}


void gfx_circ(struct gfx *gfx,
    int x, int y, int r, int color, int fill_color, unsigned layer)
{
	gfx->ops->circ(gfx->user, x, y, r, color, fill_color, layer);
}


void gfx_arc(struct gfx *gfx, int x, int y, int r, int sa, int ea,
    int color, int fill_color, unsigned layer)
{
	gfx->ops->arc(gfx->user, x, y, r, sa, ea, color, fill_color, layer);
}


void gfx_text(struct gfx *gfx, int x, int y, const char *s, unsigned size,
    enum text_align align, int rot, enum text_style style,
    unsigned color, unsigned layer)
{
	gfx->ops->text(gfx->user, x, y, s, size, align, rot, style,
	    color, layer);
}


void gfx_tag(struct gfx *gfx, const char *s,
    unsigned points, const int x[points], int const y[points])
{
	if (gfx->ops->tag)
		gfx->ops->tag(gfx->user, s, points, x, y);
}


unsigned gfx_text_width(struct gfx *gfx, const char *s, unsigned size,
    enum text_style style)
{
	return gfx->ops->text_width(gfx->user, s, size, style);
}


enum gfx_extra gfx_set_extra(struct gfx *gfx, enum gfx_extra extra)
{
	enum gfx_extra old = gfx->extra;

	if (gfx->ops->set_extra)
		gfx->ops->set_extra(gfx->user, extra);
	gfx->extra = extra;
	return old;
}


/* ----- Initialization ---------------------------------------------------- */


struct gfx *gfx_init(const struct gfx_ops *ops)
{
	struct gfx *new;

	new = alloc_type(struct gfx);
	new->user = ops->init();
	new->ops = ops;
	new->extra = 0;
	return new;
}


bool gfx_args(struct gfx *gfx, int argc, char *const *argv, const char *opts)
{
	char *combined = (char *) opts;
	bool res;

	if (gfx->ops->opts)
		alloc_printf(&combined, "%s%s", opts, gfx->ops->opts);
	optind = 0;
	res = gfx->ops->args && gfx->ops->args(gfx->user, argc, argv, combined);
	if (gfx->ops->opts)
		free(combined);
	return res;
}


/* ----- Access ------------------------------------------------------------ */


void gfx_sheet_name(struct gfx *gfx, const char *name)
{
	if (gfx->ops->sheet_name)
		gfx->ops->sheet_name(gfx->user, name);
}


void gfx_new_sheet(struct gfx *gfx)
{
	if (gfx->ops->new_sheet)
		gfx->ops->new_sheet(gfx->user);
}


bool gfx_multi_sheet(struct gfx *gfx)
{
	return !!gfx->ops->new_sheet;
}


void *gfx_user(struct gfx *gfx)
{
	return gfx->user;
}


/* ----- Termination ------------------------------------------------------- */


void gfx_destroy(struct gfx *gfx)
{
	free(gfx);
}


int gfx_end(struct gfx *gfx, enum gfx_extra extra)
{
	int res = 0;

	if (gfx->ops->end)
		res = gfx->ops->end(gfx->user, extra);
	gfx_destroy(gfx);
	return res;
}