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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
|
/* Copyright (C) 2001-2017 Peter Selinger.
This file is part of Potrace. It is free software and it is covered
by the GNU General Public License. See the file COPYING for details. */
/* The GeoJSON backend of Potrace. */
/* Written 2012 by Christoph Hormann <chris_hormann@gmx.de> */
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <stdio.h>
#include <stdarg.h>
#include <string.h>
#include <math.h>
#include "potracelib.h"
#include "curve.h"
#include "main.h"
#include "backend_geojson.h"
#include "lists.h"
#include "auxiliary.h"
/* ---------------------------------------------------------------------- */
/* auxiliary */
/* return a point on a 1-dimensional Bezier segment */
static inline double bezier(double t, double x0, double x1, double x2, double x3) {
double s = 1-t;
return s*s*s*x0 + 3*(s*s*t)*x1 + 3*(t*t*s)*x2 + t*t*t*x3;
}
static const char *format = "%f";
/* Convert a floating-point number to a string, using a pre-determined
format. The format must be previously set with set_format().
Returns one of a small number of statically allocated strings. */
static char *round_to_unit(double x) {
static int n = 0;
static char buf[4][100];
n++;
if (n >= 4) {
n = 0;
}
sprintf(buf[n], format, x);
return buf[n];
}
/* Select a print format for floating point numbers, appropriate for
the given scaling and info.unit. Note: the format must be so that
the resulting number fits into a buffer of size 100. */
static void set_format(trans_t tr) {
double s;
int d;
static char buf[10];
s = min(fabs(tr.scalex), fabs(tr.scaley));
if (info.unit != 0.0 && s != 0.0) {
d = (int)ceil(log(info.unit/s) / log(10));
} else {
d = 0;
}
if (d <= 0) {
format = "%.0f";
} else if (d <= 20) {
sprintf(buf, "%%.%df", d);
format = buf;
} else {
format = "%e";
}
}
/* ---------------------------------------------------------------------- */
/* path-drawing auxiliary functions */
static dpoint_t cur;
static void geojson_moveto(FILE *fout, dpoint_t p, trans_t tr) {
dpoint_t q;
q = trans(p, tr);
fprintf(fout, "[%s, %s]", round_to_unit(q.x), round_to_unit(q.y));
cur = q;
}
static void geojson_lineto(FILE *fout, dpoint_t p, trans_t tr) {
dpoint_t q;
q = trans(p, tr);
fprintf(fout, ", [%s, %s]", round_to_unit(q.x), round_to_unit(q.y));
cur = q;
}
static void geojson_curveto(FILE *fout, dpoint_t p1, dpoint_t p2, dpoint_t p3, trans_t tr) {
dpoint_t q1;
dpoint_t q2;
dpoint_t q3;
double step, t;
int i;
double x, y;
q1 = trans(p1, tr);
q2 = trans(p2, tr);
q3 = trans(p3, tr);
step = 1.0 / 8.0;
for (i=0, t=step; i<8; i++, t+=step) {
x = bezier(t, cur.x, q1.x, q2.x, q3.x);
y = bezier(t, cur.y, q1.y, q2.y, q3.y);
fprintf(fout, ", [%s, %s]", round_to_unit(x), round_to_unit(y));
}
cur = q3;
}
/* ---------------------------------------------------------------------- */
/* functions for converting a path to an SVG path element */
static int geojson_path(FILE *fout, potrace_curve_t *curve, trans_t tr) {
int i;
dpoint_t *c;
int m = curve->n;
fprintf(fout, " [");
c = curve->c[m-1];
geojson_moveto(fout, c[2], tr);
for (i=0; i<m; i++) {
c = curve->c[i];
switch (curve->tag[i]) {
case POTRACE_CORNER:
geojson_lineto(fout, c[1], tr);
geojson_lineto(fout, c[2], tr);
break;
case POTRACE_CURVETO:
geojson_curveto(fout, c[0], c[1], c[2], tr);
break;
}
}
fprintf(fout, " ]");
return 0;
}
static void write_polygons(FILE *fout, potrace_path_t *tree, trans_t tr, int first) {
potrace_path_t *p, *q;
for (p=tree; p; p=p->sibling) {
if (!first) fprintf(fout, ",\n");
fprintf(fout, "{ \"type\": \"Feature\",\n");
fprintf(fout, " \"properties\": { },\n");
fprintf(fout, " \"geometry\": {\n");
fprintf(fout, " \"type\": \"Polygon\",\n");
fprintf(fout, " \"coordinates\": [\n");
geojson_path(fout, &p->curve, tr);
for (q=p->childlist; q; q=q->sibling) {
fprintf(fout, ",\n");
geojson_path(fout, &q->curve, tr);
}
fprintf(fout, " ]\n");
fprintf(fout, " }\n");
fprintf(fout, "}");
for (q=p->childlist; q; q=q->sibling) {
write_polygons(fout, q->childlist, tr, 0);
}
first = 0;
}
}
/* ---------------------------------------------------------------------- */
/* Backend. */
/* public interface for GeoJSON */
int page_geojson(FILE *fout, potrace_path_t *plist, imginfo_t *imginfo) {
trans_t tr;
/* set up the coordinate transform (rotation) */
tr.bb[0] = imginfo->trans.bb[0]+imginfo->lmar+imginfo->rmar;
tr.bb[1] = imginfo->trans.bb[1]+imginfo->tmar+imginfo->bmar;
tr.orig[0] = imginfo->trans.orig[0]+imginfo->lmar;
tr.orig[1] = imginfo->trans.orig[1]+imginfo->bmar;
tr.x[0] = imginfo->trans.x[0];
tr.x[1] = imginfo->trans.x[1];
tr.y[0] = imginfo->trans.y[0];
tr.y[1] = imginfo->trans.y[1];
/* set the print format for floating point numbers */
set_format(imginfo->trans);
/* header */
fprintf(fout, "{\n");
fprintf(fout, "\"type\": \"FeatureCollection\",\n");
fprintf(fout, "\"features\": [\n");
write_polygons(fout, plist, tr, 1);
/* write footer */
fprintf(fout, "\n]\n");
fprintf(fout, "}\n");
fflush(fout);
return 0;
}
|