-
Notifications
You must be signed in to change notification settings - Fork 3
/
vectorize.c
248 lines (222 loc) · 4.22 KB
/
vectorize.c
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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define GW 32
#define SVGDIM 4
int gw,gh;
char g[GW*17];
int linenum=1;
int unhex(char*src,int lgt)
{
int i;
int sum=0;
for(i=0;i<lgt;i++)
{
int a=src[i]|32;
sum<<=4;
if(a>='0' && a<='9') sum+=a-'0'; else
if(a>='a' && a<='f') sum+=a-'a'+10; else
{
fprintf(stderr,"illegal char at %d here: %s\n",i,src);
exit(1);
}
}
// fprintf(stderr,"unhex:%x\n",sum);
return sum;
}
void drawgrid()
{
int i,j;
for(i=0;i<gh;i++)
{
for(j=0;j<gw;j++)
putchar(".#**"[g[i*GW+j]]);
putchar('\n');
}
}
int unpackgrid(char*src)
{
int i,j;
gw=(strlen(src)*4)/gh;
for(i=0;i<gh;i++)
{
int a=unhex(src+(i*(gw/4)),gw/4);
for(j=gw-1;j>=0;j--)
{
g[i*GW+j] = a&1;
a>>=1;
}
}
}
int leftholes(int x,int y)
{
while(x>=0 && g[y*GW+x]) x--;
if(x<0) return 0;
while(x>=0 && !g[y*GW+x]) x--;
if(x<0) return 0;
return 1;
}
int dir,steps;
void changedir(int d)
{
if(dir!=d)
{
if(dir==0) printf("h%d",steps*SVGDIM); else
if(dir==1) printf("h%d",-steps*SVGDIM); else
if(dir==3) printf("v%d",steps*SVGDIM); else
if(dir==2) printf("v%d",-steps*SVGDIM);
steps=0;
}
dir=d;
if(d==-1) printf("z");
}
void startglyph(int index,int width) // todo also note wcwidth()==0
{
printf("<glyph unicode=\"&#%d;\" ",index);
if(width!=8) printf("horiz-adv=\"%d\"",width*SVGDIM);
printf("><path d=\"");
}
void endglyph()
{
printf("\" /></glyph>\n");
}
void setpos(int x,int y)
{
printf("M%d %d",x*SVGDIM,y*SVGDIM);
steps=0;
dir=-2;
}
void right()
{
changedir(0);
steps++;
}
void left()
{
changedir(1);
steps++;
}
void up()
{
changedir(2);
steps++;
}
void down()
{
changedir(3);
steps++;
}
void makepath(int x0,int y0)
{
int x=x0,y=y0,c=0,returning=0;
int prevdir;
setpos(x,y);
for(;;)
{
if(!returning)
{
if((x<GW-1) && g[y*GW+x] && !leftholes(x-1,y)) { x++; prevdir=2; right(); }
else
if((y<gh) && x>0 && g[y*GW+(x-1)] && !leftholes(x-1,y))
{
int i=x-1;
while(i>=0 && g[y*GW+i]) { g[y*GW+i]|=2; i--; }
y++; down(); prevdir=3;
} else returning=1;
} else
{
if(x==x0 && y==y0) break;
if((x>0) && g[(y-1)*GW+x-1]) { x--; prevdir=0; left(); }
else if(y>0 && g[(y-1)*GW+x]) { y--; prevdir=1; up(); }
else if(g[y*GW+x] && (prevdir!=0)) { prevdir=2; x++; right(); }
else
{
fprintf(stderr,"GOT STUCK!\n");
exit(1);
}
}
/*
x
if(y>0 && g[(y-1)*GW+(x-1)]) { x--; left(); }
else if(y<=0) break;
else { y--; up(); }
*/
}
changedir(-1);
// drawgrid();
for(y=0;y<gh;y++)
for(x=0;x<gw;x++)
if(g[y*GW+x]&2) { g[y*GW+x]=0; c++; }
if(!c)
{
fprintf(stderr,"makepath fail!\n");
exit(1);
}
}
int vectorizepart()
{
int y,x;
for(y=0;y<gh;y++)
for(x=0;x<gw;x++)
{
if(g[y*GW+x]==1)
{
makepath(x,y);
return 1;
}
}
return 0;
}
void makeheader(int is8,char*extraname)
{
printf(
"<?xml version=\"1.0\" standalone=\"yes\"?>\n"
"<svg version=\"1.1\" xmlns = 'http://www.w3.org/2000/svg'>\n"
"<defs><font id=\"Unscii\" horiz-adv-x=\"32\">\n"
"<font-face font-family=\"Unscii\" font-weight=\"medium\" font-style=\"normal\"\n"
"units-per-em=\"%d\" cap-height=\"%d\" x-height=\"%d\" ascent=\"%d\" descent=\"%d\">\n"
"<font-face-src><font-face-name name=\"Unscii %s\" /></font-face-src></font-face>\n",
(is8?8:16)*SVGDIM,
(is8?8:16)*SVGDIM,
(is8?5:7)*SVGDIM,
(is8?8:16)*SVGDIM,
0,
extraname
);
}
void makefooter()
{
printf("</font></defs></svg>\n");
}
int main(int argc,char**argv)
{
char buf[128];
// char*buf="0040:00007CC6C6C6DEDEDEDCC0C07C000000";
int i=0;
int glyphidx;
gh=(argc==1 || argv[1][0]=='8')?8:16;
if(argc>=2 && !strcmp(argv[2],"tall")) gh=16;
makeheader((gh==8)?1:0, argc>2?argv[2]:"");
for(;;)
{
if(feof(stdin)) break;
fgets(buf,128,stdin);
while(buf[i] && (buf[i]!=':')) i++;
glyphidx=unhex(buf,i);
if(glyphidx>=32)
{
unpackgrid(buf+i+1);
startglyph(glyphidx,gw);
if(!vectorizepart())
{
printf("M0 0");
} else
{
while(vectorizepart());
}
endglyph();
}
}
makefooter();
return 0;
}