-
Notifications
You must be signed in to change notification settings - Fork 0
/
ps.cpp
319 lines (290 loc) · 8.49 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
/******************************************************/
/* */
/* ps.cpp - PostScript output */
/* */
/******************************************************/
/* Copyright 2014,2016-2018,2023 Pierre Abbat.
* This file is part of the Quadlods program.
*
* The Quadlods 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 3 of the
* License, or (at your option) any later version.
*
* Quadlods 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 Quadlods. If not, see
* <http://www.gnu.org/licenses/>.
*/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <cstring>
#include <cassert>
#include <unistd.h>
#include <iomanip>
#include "ps.h"
#include "ldecimal.h"
using namespace std;
#define PAPERRES 0.004
char rscales[]={10,12,15,20,25,30,40,50,60,80};
const double PSPoint=25.4/72;
papersize a4land={297,210};
papersize a4port={210,297};
PostScript::PostScript()
{
oldr=oldg=oldb=NAN;
paperx=210;
papery=297;
scale=1;
pageorientation=orientation=pages=0;
indocument=inpage=inlin=false;
psfile=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.
*/
{
paperx=pap.width;
papery=pap.height;
pageorientation=ori;
}
double PostScript::aspectRatio()
// Returns >1 for landscape, <1 for portrait.
{
if (pageorientation&1)
return papery/paperx;
else
return paperx/papery;
}
void PostScript::open(string psfname)
{
if (psfile)
close();
psfile=new ofstream(psfname);
}
void PostScript::prolog()
{
if (psfile && !indocument)
{
*psfile<<"%!PS-Adobe-3.0\n%%BeginProlog\n%%%%Pages: (atend)"<<endl;
*psfile<<"%%BoundingBox: 0 0 "<<rint(paperx/PSPoint)<<' '<<rint(papery/PSPoint)<<endl;
*psfile<<"\n/. % ( x y )\n{ newpath 0.1 0 360 arc fill } bind def\n\n";
*psfile<<"/- % ( x1 y1 x2 y2 )\n{ newpath moveto lineto stroke } bind def\n\n";
*psfile<<"/l % ( x y )\n{ lineto } 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<<"%%EndProlog"<<endl;
indocument=true;
pages=0;
}
}
void PostScript::startpage()
{
if (psfile && indocument && !inpage)
{
++pages;
*psfile<<"%%Page: "<<pages<<' '<<pages<<"\ngsave mmscale 0.1 setlinewidth\n";
*psfile<<paperx/2<<' '<<papery/2<<' '<<"translate ";
*psfile<<(pageorientation&3)*90<<" rotate ";
*psfile<<paperx/-2<<' '<<papery/-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;
}
double PostScript::xscale(double x)
{
return scale*(x-centerx)+paperx/2;
}
double PostScript::yscale(double y)
{
return scale*(y-centery)+papery/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<<" setrgbcolor"<<endl;
oldr=r;
oldg=g;
oldb=b;
}
}
void PostScript::setscale(double minx,double miny,double maxx,double maxy,int ori)
{
double xsize,ysize,papx,papy;
int i;
orientation=ori;
centerx=(minx+maxx)/2;
centery=(miny+maxy)/2;
xsize=fabs(minx-maxx);
ysize=fabs(miny-maxy);
papx=paperx;
papy=papery;
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::dot(double x,double y)
{
assert(psfile);
*psfile<<fixed<<setprecision(2)<<xscale(x)<<' '<<yscale(y)<<" .";
*psfile<<endl;
}
void PostScript::subdot(double x,double y,int n)
{
assert(psfile);
*psfile<<fixed<<setprecision(2)<<xscale(x)<<' '<<yscale(y)<<" .";
if (n)
*psfile<<n<<'-';
*psfile<<endl;
}
void PostScript::line2p(xy pnt1,xy pnt2)
{
pnt1=turn(pnt1,orientation);
pnt2=turn(pnt2,orientation);
if (isfinite(pnt1.getx()) && isfinite(pnt1.gety()) && isfinite(pnt2.getx()) && isfinite(pnt2.gety()))
*psfile<<ldecimal(xscale(pnt1.getx()),PAPERRES)<<' '<<ldecimal(yscale(pnt1.gety()),PAPERRES)
<<' '<<ldecimal(xscale(pnt2.getx()),PAPERRES)<<' '<<ldecimal(yscale(pnt2.gety()),PAPERRES)<<" -"<<endl;
}
void PostScript::startline()
{
assert(psfile);
*psfile<<"newpath"<<endl;
}
void PostScript::lineto(double x,double y)
{
assert(psfile);
*psfile<<fixed<<setprecision(2)<<xscale(x)<<' '<<yscale(y)<<(inlin?" l":" moveto");
*psfile<<endl;
inlin=true;
}
void PostScript::endline(bool closed)
{
assert(psfile);
if (closed)
*psfile<<"closepath ";
*psfile<<"stroke"<<endl;
inlin=false;
}
void PostScript::plot(polyline pl,bool fill)
{
int i,j,n;
xy pnt;
n=pl.size();
pnt=turn(pl.getEndpoint(0),orientation);
//pnt=pl.getEndpoint(0);
*psfile<<ldecimal(xscale(pnt.getx()),PAPERRES)<<' '<<ldecimal(yscale(pnt.gety()),PAPERRES)<<" moveto\n";
for (i=1;i<n;i++)
{
pnt=pl.getEndpoint(i);
*psfile<<ldecimal(xscale(pnt.getx()),PAPERRES)<<' '<<ldecimal(yscale(pnt.gety()),PAPERRES)<<" l\n";
}
if (!pl.isopen())
*psfile<<"closepath ";
*psfile<<(fill?"fill":"stroke")<<endl;
}
void PostScript::draw(PairCompressor &pnts)
{
int i;
map<int64_t,PairDot>::iterator j;
string subStr;
assert(*psfile);
for (i=1;i<pnts.pairPoints.size();i++)
{
subStr=".";
if (pnts.pairPoints[i].sub)
subStr+=to_string(pnts.pairPoints[i].sub)+'-';
*psfile<<"/."<<i<<"- {";
*psfile<<" 1 index "<<ldecimal(scale*pnts.pairPoints[i].sep.getx(),PAPERRES/100)<<" add";
*psfile<<" 1 index "<<ldecimal(scale*pnts.pairPoints[i].sep.gety(),PAPERRES/100)<<" add ";
*psfile<<subStr<<' '<<subStr<<" } def\n";
//*psfile<<"% "<<pnts.pairPoints[i].lowleft.getx()<<','<<pnts.pairPoints[i].lowleft.gety();
//*psfile<<" - "<<pnts.pairPoints[i].upright.getx()<<','<<pnts.pairPoints[i].upright.gety()<<endl;
}
for (i=0;i<pnts.layers.size();i++)
for (j=pnts.layers[i].dots.begin();j!=pnts.layers[i].dots.end();++j)
{
subdot(j->second.location.getx(),j->second.location.gety(),j->second.inx);
if (j->second.location.getx()+pnts.pairPoints[j->second.inx].lowleft.getx()<-1e-9 ||
j->second.location.gety()+pnts.pairPoints[j->second.inx].lowleft.gety()<-1e-9 ||
j->second.location.getx()+pnts.pairPoints[j->second.inx].upright.getx()>1+1e-9 ||
j->second.location.gety()+pnts.pairPoints[j->second.inx].upright.gety()>1+1e-9)
*psfile<<"% "<<j->second.location.getx()<<','<<j->second.location.gety()<<" Out of range\n";
}
}
void PostScript::write(double x,double y,string text)
{
*psfile<<fixed<<setprecision(2)<<xscale(x)<<' '<<yscale(y)
<<" moveto ("<<text<<") show"<<endl;
}
void PostScript::centerWrite(xy pnt,string text)
{
pnt=turn(pnt,orientation);
*psfile<<ldecimal(xscale(pnt.getx()),PAPERRES)<<' '<<ldecimal(yscale(pnt.gety()),PAPERRES)
<<" moveto ("<<escape(text)<<") c."<<endl;
}
void PostScript::comment(string text)
{
*psfile<<'%'<<text<<endl;
}