forked from Smorodov/Multitarget-tracker
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgport.cpp
More file actions
333 lines (269 loc) · 10.3 KB
/
gport.cpp
File metadata and controls
333 lines (269 loc) · 10.3 KB
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
#include "gport.h"
// System specific defines
#if GPORT_MAC
// From MacTech 4(6) "Comments about PICTs"
#define picGrpBeg 140
#define picGrpEnd 141
#endif
// The global port
GBasePort *Port = NULL;
// A sensible default font
GBaseFont::GBaseFont ()
{
description = "Times-Roman";
name = "Times-Roman";
size = 10;
bold = false;
italic = true;
}
GPostscriptPort::GPostscriptPort ()
{
PenWidth = 1;
DocumentFonts = "";
Device = devPostscript;
DisplayRect.SetRect (0, 0, 595-144, 842-144);
fill_r = 1;
fill_g = fill_b = 0;
}
void GPostscriptPort::DrawArc (const GPoint &pt, const int radius,
const double startAngleDegrees, const double endAngleDegrees)
{
PostscriptStream << "newpath" << std::endl;
PostscriptStream << pt.GetX() << " " << -pt.GetY()
<< " " << radius
<< " " << (360.0 -startAngleDegrees)
<< " " << (360.0 - endAngleDegrees)
<< " arcn" << std::endl;
PostscriptStream << "stroke" << std::endl;
PostscriptStream << std::endl;
}
void GPostscriptPort::DrawLine (const int x1, const int y1, const int x2, const int y2)
{
//PostscriptStream << x2 << " " << -y2 << " " << x1 << " " << -y1 << " " << PenWidth << " DrawLine" << endl;
PostscriptStream << " gsave" << std::endl;
PostscriptStream << PenWidth << " setlinewidth" << std::endl;
// We may not always want to set this as it works best with rectangular trees...
// PostscriptStream << " 2 setlinecap" << endl;
// PostscriptStream << " 0 setgray" << endl;
//PostscriptStream << " 0.7 setgray" << endl;
PostscriptStream << fill_r << " " << fill_g << " " << fill_b << " setrgbcolor" << std::endl;
PostscriptStream << x2 << " " << -y2 << " moveto" << std::endl;
PostscriptStream << x1 << " " << -y1 << " lineto" << std::endl;
PostscriptStream << " stroke" << std::endl;
PostscriptStream << " grestore" << std::endl;
}
void GPostscriptPort::DrawCircle (const GPoint &pt, const int radius)
{
PostscriptStream << "newpath" << std::endl;
PostscriptStream << pt.GetX() << " " << -pt.GetY() << " " << radius << " 0 360 arc" << std::endl;
PostscriptStream << "stroke" << std::endl;
PostscriptStream << std::endl;
}
void GPostscriptPort::FillCircle (const GPoint &pt, const int radius)
{
PostscriptStream << "newpath" << std::endl;
PostscriptStream << pt.GetX() << " " << -pt.GetY() << " " << radius << " 0 360 arc" << std::endl;
//PostscriptStream << "gsave" << endl;
//PostscriptStream << "0.90 setgray" << endl;
PostscriptStream << fill_r << " " << fill_g << " " << fill_b << " setrgbcolor" << std::endl;
PostscriptStream << "fill" << std::endl;
//PostscriptStream << "grestore" << endl;
PostscriptStream << std::endl;
}
void GPostscriptPort::DrawRect (const GRect &r)
{
PostscriptStream << r.GetLeft() << " " << -r.GetTop() << " moveto" << std::endl;
PostscriptStream << r.GetWidth() << " 0 rlineto" << std::endl;
PostscriptStream << "0 " << -r.GetHeight() << " rlineto" << std::endl;
PostscriptStream << -r.GetWidth() << " 0 rlineto" << std::endl;
PostscriptStream << "0 " << r.GetHeight() << " rlineto" << std::endl;
PostscriptStream << "closepath" << std::endl;
PostscriptStream << "stroke" << std::endl;
PostscriptStream << std::endl;
}
void GPostscriptPort::DrawText (const int x, const int y, const char *text)
{
PostscriptStream << "(" << text << ") " << x << " " << -y << " DrawText" << std::endl;
}
void GPostscriptPort::GetPrintingRect (GRect &r)
{
// A4, with 1" margin
r.SetRect (0, 0, 595-144, 842-144);
}
void GPostscriptPort::SetCurrentFont (GBaseFont &font)
{
std::string face = font.GetName();
if (font.IsBold() || font.IsItalic())
{
face += "-";
if (font.IsBold())
face += "Bold";
if (font.IsItalic())
face += "Italic";
}
/*
// Duh -- need to do this earlier, perhaps scan the list of
// fonts already created and output those...
// Store this font in the list of fonts we need for our document
int found = DocumentFonts.find_first_of (face, 0);
if ((found < 0) || (found > DocumentFonts.length()))
{
if (DocumentFonts.length() > 0)
DocumentFonts += ", ";
DocumentFonts += face;
}
*/
PostscriptStream << std::endl;
PostscriptStream << "/" << face << " findfont" << std::endl;
PostscriptStream << font.GetSize () << " scalefont" << std::endl;
PostscriptStream << "setfont" << std::endl;
PostscriptStream << std::endl;
}
// Mac
// Win
// Postscript
void GPostscriptPort::SetPenWidth (int w)
{
PenWidth = w;
PostscriptStream << w << " setlinewidth" << std::endl;
PostscriptStream << std::endl;
}
void GPostscriptPort::StartPicture (char *pictFileName)
{
PostscriptStream.open (pictFileName);
// Postscript header
PostscriptStream << "%!PS-Adobe-2.0" << std::endl;
PostscriptStream << "%%Creator: Roderic D. M. Page" << std::endl;
PostscriptStream << "%%DocumentFonts: Times-Roman" << std::endl;
PostscriptStream << "%%Title:" << pictFileName << std::endl;
PostscriptStream << "%%BoundingBox: 0 0 595 842" << std::endl; // A4
PostscriptStream << "%%Pages: 1" << std::endl;
PostscriptStream << "%%EndComments" << std::endl;
PostscriptStream << std::endl;
// Move origin to top left corner
PostscriptStream << "0 842 translate" << std::endl;
PostscriptStream << "72 -72 translate" << std::endl; // one inch margin
// Some definitions for drawing lines, etc.
// Drawline draws text with encaps that project...
PostscriptStream << "% Encapsulate drawing a line" << std::endl;
PostscriptStream << "% arguments x1 y1 x2 xy2 width" << std::endl;
PostscriptStream << "/DrawLine {" << std::endl;
PostscriptStream << " gsave" << std::endl;
PostscriptStream << " setlinewidth" << std::endl;
// We may not always want to set this as it works best with rectangular trees...
// PostscriptStream << " 2 setlinecap" << endl;
PostscriptStream << " 0 setgray" << std::endl;
//PostscriptStream << " 0.7 setgray" << endl;
PostscriptStream << " moveto" << std::endl;
PostscriptStream << " lineto" << std::endl;
PostscriptStream << " stroke" << std::endl;
PostscriptStream << " grestore" << std::endl;
PostscriptStream << " } bind def" << std::endl;
PostscriptStream << std::endl;
PostscriptStream << "% Encapsulate drawing text" << std::endl;
PostscriptStream << "% arguments x y text" << std::endl;
PostscriptStream << "/DrawText {" << std::endl;
PostscriptStream << " gsave 1 setlinewidth 0 setgray" << std::endl;
PostscriptStream << " moveto" << std::endl;
PostscriptStream << " show grestore" << std::endl;
PostscriptStream << "} bind def" << std::endl;
PostscriptStream << std::endl;
}
void GPostscriptPort::EndPicture ()
{
PostscriptStream << "showpage" << std::endl;
PostscriptStream << "%%Trailer" << std::endl;
PostscriptStream << "%%end" << std::endl;
PostscriptStream << "%%EOF" << std::endl;
PostscriptStream.close ();
}
#if GPORT_MAC
// Macintosh
void GMacPort::BeginGroup ()
{
// ::PicComment (picGrpBeg, 0, NULL);
}
void GMacPort::EndGroup ()
{
// ::PicComment (picGrpEnd, 0, NULL);
}
#endif
SVGPort::SVGPort ()
{
fontString = "font-family:Times;font-size:12";
DisplayRect.SetRect (0, 0, 400, 400);
}
void SVGPort::DrawLine (const int x1, const int y1, const int x2, const int y2)
{
svgStream << "<path style=\"stroke:black;";
svgStream << ";stroke-width:" << PenWidth;
svgStream << ";stroke-linecap:square";
svgStream << "\" ";
svgStream << "d=\"M";
svgStream << x1 << " " << y1 << " " << x2 << " " << y2 << "\"/>";
// svgStream << "<g style=\"fill:none;stroke:black\"><path d=\"M";
// svgStream << x1 << " " << y1 << " " << x2 << " " << y2 << "\"/>";
// svgStream << "</g>" << endl;
}
void SVGPort::DrawCircle (const GPoint &/*pt*/, const int /*radius*/)
{
/* PostscriptStream << "newpath" << endl;
PostscriptStream << pt.GetX() << " " << -pt.GetY() << " " << radius << " 0 360 arc" << endl;
PostscriptStream << "stroke" << endl;
PostscriptStream << endl;
*/
}
void SVGPort::DrawRect (const GRect &/*r*/)
{
/* PostscriptStream << r.GetLeft() << " " << -r.GetTop() << " moveto" << endl;
PostscriptStream << r.GetWidth() << " 0 rlineto" << endl;
PostscriptStream << "0 " << -r.GetHeight() << " rlineto" << endl;
PostscriptStream << -r.GetWidth() << " 0 rlineto" << endl;
PostscriptStream << "0 " << r.GetHeight() << " rlineto" << endl;
PostscriptStream << "closepath" << endl;
PostscriptStream << "stroke" << endl;
PostscriptStream << endl;
*/
}
void SVGPort::DrawText (const int x, const int y, const char *text)
{
svgStream << "<text x=\"" << x << "\" y=\"" << y << "\" style=\"" << fontString << "\" >"
<< text << "</text>" << std::endl;
}
void SVGPort::StartPicture (char *pictFileName)
{
svgStream.open (pictFileName);
svgStream << "<?xml version=\"1.0\" standalone=\"no\"?>" << std::endl;
svgStream << "<!DOCTYPE svg PUBLIC \"-//W3C//DTD SVG 20010904//EN\" " << std::endl;
svgStream << "\"http://www.w3.org/TR/2001/REC-SVG-20010904/DTD/svg10.dtd\"> " << std::endl;
svgStream << "<svg width=\"" << DisplayRect.GetWidth() << "pt\" "
<< "height=\"" << DisplayRect.GetHeight() << "pt\" >" << std::endl;
// <title>test</title>
// <desc>test</desc>
}
void SVGPort::EndPicture ()
{
svgStream << "</svg>" << std::endl;
svgStream.close ();
}
void SVGPort::GetPrintingRect (GRect &r)
{
r = DisplayRect;
}
void SVGPort::SetCurrentFont (GBaseFont &font)
{
fontString = "";
fontString += "font-family:";
fontString += font.GetName();
char buf[32];
sprintf (buf, ";font-size:%d", font.GetSize());
fontString += buf;
if (font.IsItalic())
{
fontString += ";font-style:italic";
}
if (font.IsBold())
{
fontString += ";font-weight:bold";
}
}