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
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
|
/***** Autogenerated from runlabel.in; changes will be overwritten *****/
#line 1 "runtimebase.in"
/*****
* runtimebase.in
* Andy Hammerlindl 2009/07/28
*
* Common declarations needed for all code-generating .in files.
*
*****/
#line 1 "runlabel.in"
/*****
* runlabel.in
*
* Runtime functions for label operations.
*
*****/
#line 1 "runtimebase.in"
#include "stack.h"
#include "types.h"
#include "builtin.h"
#include "entry.h"
#include "errormsg.h"
#include "array.h"
#include "triple.h"
#include "callable.h"
using vm::stack;
using vm::error;
using vm::array;
using vm::read;
using vm::callable;
using types::formal;
using types::function;
using camp::triple;
#define PRIMITIVE(name,Name,asyName) using types::prim##Name;
#include <primitives.h>
#undef PRIMITIVE
typedef double real;
void unused(void *);
namespace run {
array *copyArray(array *a);
array *copyArray2(array *a);
array *copyArray3(array *a);
inline size_t checkdimension(const array *a, size_t dim)
{
size_t size=checkArray(a);
if(dim && size != dim) {
ostringstream buf;
buf << "array of length " << dim << " expected";
error(buf);
}
return size;
}
template<class T>
void copyArrayC(T* &dest, const array *a, size_t dim=0,
GCPlacement placement=NoGC)
{
size_t size=checkdimension(a,dim);
dest=(placement == NoGC) ? new T[size] : new(placement) T[size];
for(size_t i=0; i < size; i++)
dest[i]=read<T>(a,i);
}
template<class T>
void copyArray2C(T* &dest, const array *a, bool square=true, size_t dim2=0,
GCPlacement placement=NoGC)
{
size_t n=checkArray(a);
size_t m=(square || n == 0) ? n : checkArray(read<array*>(a,0));
if(n > 0 && dim2 && m != dim2) {
ostringstream buf;
buf << "second matrix dimension must be " << dim2;
error(buf);
}
dest=(placement == NoGC) ? new T[n*m] : new(placement) T[n*m];
for(size_t i=0; i < n; i++) {
array *ai=read<array*>(a,i);
size_t aisize=checkArray(ai);
if(aisize == m) {
T *desti=dest+i*m;
for(size_t j=0; j < m; j++)
desti[j]=read<T>(ai,j);
} else
error(square ? "matrix must be square" : "matrix must be rectangular");
}
}
double *copyTripleArray2Components(array *a, bool square=true, size_t dim2=0,
GCPlacement placement=NoGC);
}
function *realRealFunction();
#define CURRENTPEN processData().currentpen
#line 16 "runlabel.in"
#include "picture.h"
#include "drawlabel.h"
using namespace camp;
using namespace vm;
using namespace settings;
typedef array realarray;
typedef array patharray;
using types::realArray;
using types::pathArray;
void cannotread(const string& s)
{
ostringstream buf;
buf << "Cannot read from " << s;
error(buf);
}
void cannotwrite(const string& s)
{
ostringstream buf;
buf << "Cannot write to " << s;
error(buf);
}
pair readpair(stringstream& s, double hscale=1.0, double vscale=1.0)
{
double x,y;
s >> y;
s >> x;
return pair(hscale*x,vscale*y);
}
string ASYx="/ASYx {( ) print ASYX sub 12 string cvs print} bind def";
string ASYy="/ASYy {( ) print ASYY sub 12 string cvs print} bind def";
string pathforall="{(M) print ASYy ASYx} {(L) print ASYy ASYx} {(C) print ASYy ASYx ASYy ASYx ASYy ASYx} {(c) print} pathforall";
string currentpoint="print currentpoint ASYy ASYx ";
string ASYinit="/ASYX currentpoint pop def /ASYY currentpoint exch pop def ";
string ASY1="ASY1 {"+ASYinit+"/ASY1 false def} if ";
void showpath(std::ofstream& ps)
{
ps << ASYx << newl
<< ASYy << newl
<< "/ASY1 true def" << newl
<< "/fill {" << ASY1
<< pathforall << " (M) " << currentpoint
<< "currentpoint newpath moveto } bind def" << newl
<< "/stroke {" << ASY1 << "strokepath "
<< pathforall << " (M) " << currentpoint
<< "currentpoint newpath moveto } bind def" << endl;
}
array *readpath(const string& psname, bool keep,
double hscale=1.0, double vsign=1.0)
{
double vscale=vsign*hscale;
array *P=new array(0);
mem::vector<string> cmd;
cmd.push_back(getSetting<string>("gs"));
cmd.push_back("-q");
cmd.push_back("-dNOPAUSE");
cmd.push_back("-dBATCH");
if(safe) cmd.push_back("-dSAFER");
#ifdef __CYGWIN__
const string null="NUL";
#else
const string null="/dev/null";
#endif
cmd.push_back("-sDEVICE=epswrite");
cmd.push_back("-sOutputFile="+null);
cmd.push_back(psname);
iopipestream gs(cmd,"gs","Ghostscript");
stringstream buf;
while(true) {
string out;
gs >> out;
if(out.empty() && !gs.running()) break;
buf << out;
}
if(verbose > 2) cout << endl;
mem::vector<solvedKnot> nodes;
solvedKnot node;
bool cyclic=false;
bool active=false;
while(!buf.eof()) {
char c;
buf >> c;
switch(c) {
case 'M':
{
if(active) {
if(cyclic) {
if(node.point == nodes[0].point)
nodes[0].pre=node.pre;
else {
pair delta=(nodes[0].point-node.point)*third;
node.post=node.point+delta;
nodes[0].pre=nodes[0].point-delta;
node.straight=true;
nodes.push_back(node);
}
} else {
node.post=node.point;
node.straight=false;
nodes.push_back(node);
}
P->push(path(nodes,nodes.size(),cyclic));
nodes.clear();
}
active=false;
cyclic=false;
node.pre=node.point=readpair(buf,hscale,vscale);
node.straight=false;
break;
}
case 'L':
{
pair point=readpair(buf,hscale,vscale);
pair delta=(point-node.point)*third;
node.post=node.point+delta;
node.straight=true;
nodes.push_back(node);
active=true;
node.pre=point-delta;
node.point=point;
break;
}
case 'C':
{
pair point=readpair(buf,hscale,vscale);
pair pre=readpair(buf,hscale,vscale);
node.post=readpair(buf,hscale,vscale);
node.straight=false;
nodes.push_back(node);
active=true;
node.pre=pre;
node.point=point;
break;
}
case 'c':
{
cyclic=true;
break;
}
}
}
if(!keep)
unlink(psname.c_str());
return P;
}
// Autogenerated routines:
namespace run {
#line 176 "runlabel.in"
// void label(picture *f, string *s, string *size, transform t, pair position, pair align, pen p);
void gen_runlabel0(stack *Stack)
{
pen p=vm::pop<pen>(Stack);
pair align=vm::pop<pair>(Stack);
pair position=vm::pop<pair>(Stack);
transform t=vm::pop<transform>(Stack);
string * size=vm::pop<string *>(Stack);
string * s=vm::pop<string *>(Stack);
picture * f=vm::pop<picture *>(Stack);
#line 178 "runlabel.in"
f->append(new drawLabel(*s,*size,t,position,align,p));
}
#line 182 "runlabel.in"
// bool labels(picture *f);
void gen_runlabel1(stack *Stack)
{
picture * f=vm::pop<picture *>(Stack);
#line 183 "runlabel.in"
{Stack->push<bool>(f->havelabels()); return;}
}
#line 187 "runlabel.in"
// realarray* texsize(string *s, pen p=CURRENTPEN);
void gen_runlabel2(stack *Stack)
{
pen p=vm::pop<pen>(Stack,CURRENTPEN);
string * s=vm::pop<string *>(Stack);
#line 188 "runlabel.in"
texinit();
processDataStruct &pd=processData();
string texengine=getSetting<string>("tex");
const char **abort=texabort(texengine);
setpen(pd.tex,texengine,p);
double width,height,depth;
if(!texbounds(width,height,depth,pd.tex,*s,abort,false,true))
{Stack->push<realarray*>(new array(0)); return;}
array *t=new array(3);
(*t)[0]=width;
(*t)[1]=height;
(*t)[2]=depth;
{Stack->push<realarray*>(t); return;}
}
#line 207 "runlabel.in"
// patharray* _texpath(string *s, pen p=CURRENTPEN);
void gen_runlabel3(stack *Stack)
{
pen p=vm::pop<pen>(Stack,CURRENTPEN);
string * s=vm::pop<string *>(Stack);
#line 208 "runlabel.in"
array *P=new array(0);
if(s->empty()) {Stack->push<patharray*>(P); return;}
string prefix=cleanpath(outname());
string psname=auxname(prefix,"ps");
string texname=auxname(prefix,"tex");
string dviname=auxname(prefix,"dvi");
bbox b;
string texengine=getSetting<string>("tex");
bool pdf=settings::pdf(texengine);
texfile tex(texname,b,true);
tex.miniprologue();
tex.setfont(p);
if(!pdf) {
tex.verbatimline("\\special{ps:");
tex.verbatimline(ASYx);
tex.verbatimline(ASYy);
tex.verbatimline("/ASY1 true def");
tex.verbatimline("/v {"+ASY1+"neg exch 4 copy 4 2 roll 2 copy 6 2 roll 2 copy (M) print ASYy ASYx (L) print ASYy add ASYx (L) print add ASYy add ASYx (L) print add ASYy ASYx (c) print} bind def");
tex.verbatimline("/show {"+ASY1+
"currentpoint newpath moveto false charpath "+
pathforall+"} bind def}");
}
tex.verbatimline(*s+"%");
tex.epilogue(true);
tex.close();
int status=opentex(texname,prefix);
string pdfname;
if(!status) {
if(pdf) {
pdfname=auxname(prefix,"pdf");
std::ofstream ps(psname.c_str());
if(!ps) cannotwrite(psname);
showpath(ps);
mem::vector<string> cmd;
cmd.push_back(getSetting<string>("gs"));
cmd.push_back("-q");
cmd.push_back("-dNOCACHE");
cmd.push_back("-dNOPAUSE");
cmd.push_back("-dBATCH");
if(safe) cmd.push_back("-dSAFER");
cmd.push_back("-sDEVICE=epswrite");
cmd.push_back("-sOutputFile=-");
cmd.push_back(pdfname);
iopipestream gs(cmd,"gs","Ghostscript");
gs.block(false);
while(true) {
string line;
gs >> line;
if(line.empty() && !gs.running()) break;
ps << line;
}
ps.close();
} else {
mem::vector<string> dcmd;
dcmd.push_back(getSetting<string>("dvips"));
dcmd.push_back("-R");
dcmd.push_back("-Pdownload35");
dcmd.push_back("-D600");
push_split(dcmd,getSetting<string>("dvipsOptions"));
if(verbose <= 2) dcmd.push_back("-q");
dcmd.push_back("-o"+psname);
dcmd.push_back(dviname);
status=System(dcmd,0,true,"dvips");
}
}
if(status != 0)
error("texpath failed");
bool keep=getSetting<bool>("keep");
if(!keep) { // Delete temporary files.
unlink(texname.c_str());
if(!getSetting<bool>("keepaux"))
unlink(auxname(prefix,"aux").c_str());
unlink(auxname(prefix,"log").c_str());
if(pdf)
unlink(pdfname.c_str());
else
unlink(dviname.c_str());
if(settings::context(texengine)) {
unlink(auxname(prefix,"top").c_str());
unlink(auxname(prefix,"tua").c_str());
unlink(auxname(prefix,"tui").c_str());
}
}
{Stack->push<patharray*>(pdf ? readpath(psname,keep,0.1) : readpath(psname,keep,0.12,-1.0)); return;}
}
#line 304 "runlabel.in"
// patharray* textpath(string *s, pen p=CURRENTPEN);
void gen_runlabel4(stack *Stack)
{
pen p=vm::pop<pen>(Stack,CURRENTPEN);
string * s=vm::pop<string *>(Stack);
#line 305 "runlabel.in"
array *P=new array(0);
if(s->empty()) {Stack->push<patharray*>(P); return;}
string prefix=cleanpath(outname());
string outputname=auxname(prefix,getSetting<string>("textoutformat"));
string textname=auxname(prefix,getSetting<string>("textextension"));
std::ofstream text(textname.c_str());
if(!text) cannotwrite(textname);
text << getSetting<string>("textprologue") << newl
<< p.Font() << newl
<< *s << newl
<< getSetting<string>("textepilogue") << endl;
text.close();
string psname=auxname(prefix,"ps");
std::ofstream ps(psname.c_str());
if(!ps) cannotwrite(psname);
showpath(ps);
mem::vector<string> cmd;
cmd.push_back(getSetting<string>("textcommand"));
push_split(cmd,getSetting<string>("textcommandOptions"));
cmd.push_back(textname);
iopipestream typesetter(cmd);
mem::vector<string> cmd2;
cmd2.push_back(getSetting<string>("gs"));
cmd2.push_back("-q");
cmd2.push_back("-dNOCACHE");
cmd2.push_back("-dNOPAUSE");
cmd2.push_back("-dBATCH");
if(safe) cmd2.push_back("-dSAFER");
cmd2.push_back("-sDEVICE=epswrite");
cmd2.push_back("-sOutputFile=-");
cmd2.push_back("-");
iopipestream gs(cmd2,"gs","Ghostscript");
gs.block(false);
// TODO: Simplify by connecting the pipes directly.
while(true) {
string out;
if(typesetter.isopen()) {
typesetter >> out;
if(!out.empty()) gs << out;
else if(!typesetter.running()) {
typesetter.pipeclose();
gs.eof();
}
}
string out2;
gs >> out2;
if(out2.empty() && !gs.running()) break;
ps << out2;
}
ps.close();
if(verbose > 2) cout << endl;
bool keep=getSetting<bool>("keep");
if(!keep) // Delete temporary files.
unlink(textname.c_str());
{Stack->push<patharray*>(readpath(psname,keep,0.1)); return;}
}
#line 374 "runlabel.in"
// patharray* _strokepath(path g, pen p=CURRENTPEN);
void gen_runlabel5(stack *Stack)
{
pen p=vm::pop<pen>(Stack,CURRENTPEN);
path g=vm::pop<path>(Stack);
#line 375 "runlabel.in"
array *P=new array(0);
if(g.size() == 0) {Stack->push<patharray*>(P); return;}
string prefix=cleanpath(outname());
string psname=auxname(prefix,"ps");
bbox b;
psfile ps(psname,false);
ps.prologue(b);
ps.verbatimline(ASYx);
ps.verbatimline(ASYy);
ps.verbatimline("/stroke {"+ASYinit+pathforall+"} bind def");
ps.resetpen();
ps.setpen(p);
ps.write(g);
ps.strokepath();
ps.stroke(p);
ps.verbatimline("(M) "+currentpoint);
ps.epilogue();
ps.close();
{Stack->push<patharray*>(readpath(psname,getSetting<bool>("keep"))); return;}
}
} // namespace run
namespace trans {
void gen_runlabel_venv(venv &ve)
{
#line 176 "runlabel.in"
addFunc(ve, run::gen_runlabel0, primVoid(), "label", formal(primPicture(), "f", false, false), formal(primString(), "s", false, false), formal(primString(), "size", false, false), formal(primTransform(), "t", false, false), formal(primPair(), "position", false, false), formal(primPair(), "align", false, false), formal(primPen(), "p", false, false));
#line 182 "runlabel.in"
addFunc(ve, run::gen_runlabel1, primBoolean(), "labels", formal(primPicture(), "f", false, false));
#line 187 "runlabel.in"
addFunc(ve, run::gen_runlabel2, realArray(), "texsize", formal(primString(), "s", false, false), formal(primPen(), "p", true, false));
#line 207 "runlabel.in"
addFunc(ve, run::gen_runlabel3, pathArray() , "_texpath", formal(primString(), "s", false, false), formal(primPen(), "p", true, false));
#line 304 "runlabel.in"
addFunc(ve, run::gen_runlabel4, pathArray() , "textpath", formal(primString(), "s", false, false), formal(primPen(), "p", true, false));
#line 374 "runlabel.in"
addFunc(ve, run::gen_runlabel5, pathArray() , "_strokepath", formal(primPath(), "g", false, false), formal(primPen(), "p", true, false));
}
} // namespace trans
|