-
Notifications
You must be signed in to change notification settings - Fork 5
/
ps.cpp
434 lines (398 loc) · 11.2 KB
/
ps.cpp
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
/******************************************************/
/* */
/* ps.cpp - PostScript output */
/* */
/******************************************************/
/* Copyright 2019,2020 Pierre Abbat.
* This file is part of PerfectTIN.
*
* PerfectTIN is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* PerfectTIN is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License and Lesser General Public License
* for more details.
*
* You should have received a copy of the GNU General Public License
* and Lesser General Public License along with PerfectTIN. If not, see
* <http://www.gnu.org/licenses/>.
*/
#include <cstdio>
#include <fstream>
#include <cmath>
#include <cstdlib>
#include <cstring>
#include <string>
#include <cassert>
#include <iomanip>
#include "ldecimal.h"
#include "config.h"
#include "ps.h"
#include "point.h"
#include "pointlist.h"
using namespace std;
#define PAPERRES 0.004
const char rscales[]={10,12,15,20,25,30,40,50,60,80};
const double PSPoint=25.4/72;
map<string,papersize> papersizes=
/* These mean the physical orientation of the paper in the printer. If you
* want to print in landscape, but the paper is portrait in the printer,
* set pageorientation to 1.
*/
{
{"A4 portrait",{210000,297000}},
{"A4 landscape",{297000,210000}},
{"US Letter portrait",{215900,279400}},
{"US Letter landscape",{279400,215900}},
{"US Legal portrait",{215900,355600}},
{"US Legal landscape",{355600,215900}}
};
int fibmod3(int n)
{
int i,a,b;
for (i=a=0,b=1;a<n;i++)
{
b+=a;
a=b-a;
}
return (a==n)?(i%3):-1;
}
bool isstraight(vector<xyz> seg)
{
int i;
double toler;
bool ret=true;
toler=dist(seg[0],seg.back())*1e-9+(seg[0].length()+seg.back().length())*1e-15;
/* 1e-9, not 1e-15, because segments of bezier3d are generated by
* segment::approx3d using start and end bearings rounded to the
* nearest π/1073741824. Edges of triangles are drawn with curveto
* because they are curved vertically.
*/
for (i=1;i<seg.size()-1;i++)
if (dist(seg[i]-seg[i-1],seg[i+1]-seg[i])>toler)
ret=false;
return ret;
}
PostScript::PostScript()
{
oldr=oldg=oldb=NAN;
paper=xy(210,297);
scale=1;
pageorientation=orientation=pages=0;
indocument=inpage=inlin=false;
psfile=nullptr;
pl=nullptr;
}
PostScript::~PostScript()
{
if (psfile)
close();
}
void PostScript::setpaper(papersize pap,int ori)
/* ori is 0 for no rotation, 1 for 90° rotation, making portrait
* into landscape and vice versa. Do this before each page,
* or before calling prolog if all pages are the same size.
*/
{
paper=xy(pap.width/1e3,pap.height/1e3);
pageorientation=ori;
}
double PostScript::aspectRatio()
// Returns >1 for landscape, <1 for portrait.
{
if (pageorientation&1)
return paper.gety()/paper.getx();
else
return paper.getx()/paper.gety();
}
void PostScript::open(string psfname)
{
if (psfile)
close();
psfile=new ofstream(psfname);
}
bool PostScript::isOpen()
{
return psfile!=nullptr;
}
void PostScript::prolog()
{
if (psfile && !indocument)
{
*psfile<<"%!PS-Adobe-3.0\n%%BeginProlog\n%%%%Pages: (atend)"<<endl;
*psfile<<"%%BoundingBox: 0 0 "<<rint(paper.getx()/PSPoint)<<' '<<rint(paper.gety()/PSPoint)<<endl;
*psfile<<"\n/. % ( x y )\n{ newpath 0.1 0 360 arc fill } bind def\n\n";
*psfile<<"/- % ( x1 y1 x2 y2 )\n\n{ newpath moveto lineto stroke } bind def\n\n";
*psfile<<"/c. % ( str )\n{ dup stringwidth -2 div exch -2 div exch\n"<<
"3 2 roll 2 index 2 index rmoveto show rmoveto } bind def\n\n";
*psfile<<"/mmscale { 720 254 div dup scale } bind def\n";
*psfile<<"/col { setrgbcolor } def\n";
*psfile<<"/n { newpath } def\n";
*psfile<<"/m { moveto } def\n";
*psfile<<"/l { lineto } def\n";
*psfile<<"/c { curveto } def\n";
*psfile<<"/s { stroke } def\n";
*psfile<<"/af { arc fill } def\n";
*psfile<<"%%EndProlog"<<endl;
indocument=true;
pages=0;
}
}
void PostScript::startpage()
{
if (psfile && indocument && !inpage)
{
++pages;
*psfile<<"%%Page: "<<pages<<' '<<pages<<"\n<< /PageSize [";
*psfile<<paper.getx()*36e1/127<<' '<<paper.gety()*36e1/127<<"] >> setpagedevice\n";
*psfile<<"gsave mmscale 0.1 setlinewidth\n";
*psfile<<paper.getx()/2<<' '<<paper.gety()/2<<' '<<"translate ";
*psfile<<(pageorientation&3)*90<<" rotate ";
*psfile<<paper.getx()/-2<<' '<<paper.gety()/-2<<' '<<"translate"<<endl;
*psfile<<"/Helvetica findfont 3 scalefont setfont"<<endl;
oldr=oldg=oldb=NAN;
inpage=true;
}
}
void PostScript::endpage()
{
if (psfile && indocument && inpage)
{
*psfile<<"grestore showpage"<<endl;
inpage=false;
}
}
void PostScript::trailer()
{
if (inpage)
endpage();
if (psfile && indocument)
{
*psfile<<"%%BeginTrailer\n%%Pages: "<<pages<<"\n%%EndTrailer"<<endl;
indocument=false;
}
}
void PostScript::close()
{
if (indocument)
trailer();
delete(psfile);
psfile=nullptr;
}
void PostScript::setPointlist(pointlist &plist)
{
pl=&plist;
}
int PostScript::getPages()
{
return pages;
}
double PostScript::xscale(double x)
{
return scale*(x-modelcenter.east())+paper.getx()/2;
}
double PostScript::yscale(double y)
{
return scale*(y-modelcenter.north())+paper.gety()/2;
}
string PostScript::escape(string text)
{
string ret;
int ch;
while (text.length())
{
ch=text[0];
if (ch=='(' || ch==')')
ret+='\\';
ret+=ch;
text.erase(0,1);
}
return ret;
}
void PostScript::setcolor(double r,double g,double b)
{
if (r!=oldr || g!=oldg || b!=oldb)
{
*psfile<<fixed<<setprecision(3)<<r<<' '<<g<<' '<<b<<" col"<<endl;
oldr=r;
oldg=g;
oldb=b;
}
}
void PostScript::setscale(double minx,double miny,double maxx,double maxy,int ori)
/* To compute minx etc. using dirbound on e.g. a pointlist pl:
* minx=pl.dirbound(-ori);
* miny=pl.dirbound(DEG90-ori);
* maxx=-pl.dirbound(DEG180-ori);
* maxy=-pl.dirbound(DEG270-ori);
*/
{
double xsize,ysize,papx,papy;
int i;
orientation=ori;
modelcenter=xy(minx+maxx,miny+maxy)/2;
xsize=fabs(minx-maxx);
ysize=fabs(miny-maxy);
papx=paper.getx();
papy=paper.gety();
if (pageorientation&1)
swap(papx,papy);
for (scale=1;scale*xsize/10<papx && scale*ysize/10<papy;scale*=10);
for (;scale*xsize/80>papx*0.9 || scale*ysize/80>papy*0.9;scale/=10);
for (i=0;i<9 && (scale*xsize/rscales[i]>papx*0.9 || scale*ysize/rscales[i]>papy*0.9);i++);
scale/=rscales[i];
*psfile<<"% minx="<<minx<<" miny="<<miny<<" maxx="<<maxx<<" maxy="<<maxy<<" scale="<<scale<<endl;
}
void PostScript::setscale(BoundRect br)
{
setscale(br.left(),br.bottom(),br.right(),br.top(),br.getOrientation());
}
double PostScript::getscale()
{
return scale;
}
void PostScript::dot(xy pnt,string comment)
{
assert(psfile);
pnt=turn(pnt,orientation);
if (isfinite(pnt.east()) && isfinite(pnt.north()))
{
*psfile<<ldecimal(xscale(pnt.east()),PAPERRES)<<' '<<ldecimal(yscale(pnt.north()),PAPERRES)<<" .";
if (comment.length())
*psfile<<" %"<<comment;
*psfile<<endl;
}
}
void PostScript::circle(xy pnt,double radius)
{
assert(psfile);
pnt=turn(pnt,orientation);
if (isfinite(pnt.east()) && isfinite(pnt.north()))
*psfile<<ldecimal(xscale(pnt.east()),PAPERRES)<<' '<<ldecimal(yscale(pnt.north()),PAPERRES)
<<" n "<<ldecimal(scale*radius,PAPERRES)<<" 0 360 af %"
<<ldecimal(radius*radius,radius*radius/1000)<<endl;
}
void PostScript::line(edge lin,int num,bool colorfibaster,bool directed)
/* Used in bezitest to show the 2D TIN before the 3D triangles are constructed on it.
* In bezitopo, use spline(lin.getsegment().approx3d(x)) to show it in 3D.
*/
{
xy mid,disp,base,ab1,ab2,a,b;
a=*lin.a;
b=*lin.b;
a=turn(a,orientation);
b=turn(b,orientation);
if (colorfibaster)
switch (fibmod3(abs(pl->revpoints[lin.a]-pl->revpoints[lin.b])))
{
case -1:
setcolor(0.3,0.3,0.3);
break;
case 0:
setcolor(1,0.3,0.3);
break;
case 1:
setcolor(0,1,0);
break;
case 2:
setcolor(0.3,0.3,1);
break;
}
else
setcolor(0,0,1);
if (directed)
{
disp=b-a;
base=xy(disp.north()/40,disp.east()/-40);
ab1=a+base;
ab2=a-base;
*psfile<<"n "<<xscale(b.east())<<' '<<yscale(b.north())<<" m "<<xscale(ab1.east())<<' '<<yscale(ab1.north())<<" l "<<xscale(ab2.east())<<' '<<yscale(ab2.north())<<" l closepath fill"<<endl;
}
else
*psfile<<xscale(a.east())<<' '<<yscale(a.north())<<' '<<xscale(b.east())<<' '<<yscale(b.north())<<" -"<<endl;
mid=(a+b)/2;
//fprintf(psfile,"%7.3f %7.3f m (%d) show\n",xscale(mid.east()),yscale(mid.north()),num);
}
void PostScript::line2p(xy pnt1,xy pnt2)
{
pnt1=turn(pnt1,orientation);
pnt2=turn(pnt2,orientation);
if (isfinite(pnt1.east()) && isfinite(pnt1.north()) && isfinite(pnt2.east()) && isfinite(pnt2.north()))
*psfile<<ldecimal(xscale(pnt1.east()),PAPERRES)<<' '<<ldecimal(yscale(pnt1.north()),PAPERRES)
<<' '<<ldecimal(xscale(pnt2.east()),PAPERRES)<<' '<<ldecimal(yscale(pnt2.north()),PAPERRES)<<" -"<<endl;
}
void PostScript::startline()
{
assert(psfile);
*psfile<<"n"<<endl;
}
void PostScript::lineto(xy pnt)
{
assert(psfile);
pnt=turn(pnt,orientation);
*psfile<<ldecimal(xscale(pnt.east()),PAPERRES)<<' '<<ldecimal(yscale(pnt.north()),PAPERRES)<<(inlin?" l":" m");
*psfile<<endl;
inlin=true;
}
void PostScript::endline(bool closed,bool fill)
{
assert(psfile);
if (closed)
*psfile<<"closepath ";
*psfile<<(fill?"fill":"s")<<endl;
inlin=false;
}
void PostScript::spline(bezier3d spl,bool fill)
{
int i,j,n;
vector<xyz> seg;
xy pnt;
n=spl.size();
pnt=turn(xy(spl[0][0]),orientation);
*psfile<<ldecimal(xscale(pnt.east()),PAPERRES)<<' '<<ldecimal(yscale(pnt.north()),PAPERRES)<<" m\n";
for (i=0;i<n;i++)
{
seg=spl[i];
if (isstraight(seg))
{
pnt=turn(xy(seg[3]),orientation);
*psfile<<ldecimal(xscale(pnt.east()),PAPERRES)<<' '<<ldecimal(yscale(pnt.north()),PAPERRES)<<' '<<"l\n";
}
else
{
for (j=1;j<4;j++)
{
pnt=turn(xy(seg[j]),orientation);
if (pnt.isnan())
cerr<<"NaN point"<<endl;
*psfile<<ldecimal(xscale(pnt.east()),PAPERRES)<<' '<<ldecimal(yscale(pnt.north()),PAPERRES)<<' ';
}
*psfile<<"c\n";
}
}
*psfile<<(fill?"fill":"s")<<endl;
}
void PostScript::widen(double factor)
{
*psfile<<"currentlinewidth "<<ldecimal(factor)<<" mul setlinewidth"<<endl;
}
void PostScript::write(xy pnt,const string &text)
{
pnt=turn(pnt,orientation);
*psfile<<ldecimal(xscale(pnt.east()),PAPERRES)<<' '<<ldecimal(yscale(pnt.north()),PAPERRES)
<<" m ("<<escape(text)<<") show"<<endl;
}
void PostScript::centerWrite(xy pnt,const string &text)
{
pnt=turn(pnt,orientation);
*psfile<<ldecimal(xscale(pnt.east()),PAPERRES)<<' '<<ldecimal(yscale(pnt.north()),PAPERRES)
<<" m ("<<escape(text)<<") c."<<endl;
}
void PostScript::comment(const string &text)
{
*psfile<<'%'<<text<<endl;
}