File: gport.cpp

package info (click to toggle)
treeviewx 0.5.1%2Bgit20100823.7e4d0e9-3
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 2,664 kB
  • sloc: cpp: 14,362; xml: 82; makefile: 66
file content (500 lines) | stat: -rwxr-xr-x 13,447 bytes parent folder | download | duplicates (6)
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
#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 = false;
}

GPostscriptPort::GPostscriptPort ()
{
    PenWidth = 1;
    DocumentFonts = "";
    Device = devPostscript;
    DisplayRect.SetRect (0, 0, 595-144, 842-144);
}

void GPostscriptPort::DrawArc (const GPoint &pt, const int radius,
	const double startAngleDegrees, const double endAngleDegrees)
{
	portStream << "newpath" << endl;
	portStream << pt.GetX() << " " << -pt.GetY()
    	<< " " << radius
        << " " << (360.0 -startAngleDegrees)
        << " " << (360.0 - endAngleDegrees)
        << " arcn" 		<< endl;
	portStream << "stroke" 										<< endl;
	portStream << endl;
}


void GPostscriptPort::DrawLine (const int x1, const int y1, const int x2, const int y2)
{
	portStream  << x2 << " " << -y2 << " " << x1 << " " << -y1 << " " << PenWidth << " DrawLine" << endl;
}

void GPostscriptPort::DrawCircle (const GPoint &pt, const int radius)
{
	portStream << "newpath" << endl;
	portStream << pt.GetX() << " " << -pt.GetY() << " " << radius << " 0 360 arc" 		<< endl;
	portStream << "stroke" 										<< endl;
	portStream << endl;
}


void GPostscriptPort::DrawRect (const GRect &r)
{
	portStream << r.GetLeft() << " " << -r.GetTop() << " moveto" 	<< endl;
	portStream << r.GetWidth() << " 0 rlineto" 					<< endl;
	portStream << "0 " << -r.GetHeight() << " rlineto" 			<< endl;
	portStream << -r.GetWidth() << " 0 rlineto" 					<< endl;
	portStream << "0 " << r.GetHeight() << " rlineto"				<< endl;
	portStream << "closepath" 									<< endl;
	portStream << "stroke" 										<< endl;
	portStream << endl;
}

void GPostscriptPort::DrawFilledRect (const GRect &r, double graylevel)
{
	portStream << r.GetLeft() << " " << -r.GetTop() << " moveto" 	<< endl;
	portStream << r.GetWidth() << " 0 rlineto" 					<< endl;
	portStream << "0 " << -r.GetHeight() << " rlineto" 			<< endl;
	portStream << -r.GetWidth() << " 0 rlineto" 					<< endl;
	portStream << "0 " << r.GetHeight() << " rlineto"				<< endl;
	portStream << "closepath" 									<< endl;
	portStream << "gsave"                                         << endl;
	portStream << graylevel << " setgray fill"                    << endl;
	portStream << "grestore"                                      << endl;
	portStream << "stroke" 										<< endl;
	portStream << endl;
}

void GPostscriptPort::DrawText (const int x, const int y, const char *text)
{
	portStream  << "(" << text << ") " << x << " " << -y << " DrawText" << 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;
    }
*/
    portStream << endl;
    portStream << "/" << face << " findfont" << endl;
    portStream << font.GetSize () << " scalefont" << endl;
    portStream << "setfont" << endl;
    portStream << endl;
}


// Mac
// Win
// Postscript

void GPostscriptPort::SetPenWidth (int w)
{
    PenWidth = w;
    portStream << w << " setlinewidth" 						<< endl;
    portStream << endl;
}

void GPostscriptPort::AddPSCommands(char* c)
{
	portStream << c;
}

void GPostscriptPort::StartPicture (char *pictFileName)
{
    portStream.open (pictFileName);
        

    // Postscript header
    portStream << "%!PS-Adobe-2.0" 							<< endl;
    portStream << "%%Creator: Roderic D. M. Page" 			<< endl;
    portStream << "%%DocumentFonts: Times-Roman" 		 		<< endl;
    portStream << "%%Title:" <<  pictFileName 				<< endl;
    portStream << "%%BoundingBox: 0 0 595 842" 				<< endl; // A4
    portStream << "%%Pages: 1" 								<< endl;
    portStream << "%%EndComments" 							<< endl;
    portStream << endl;

    // Move origin to top left corner
    portStream << "0 842 translate" << endl;
    portStream << "72 -72 translate" << endl; // one inch margin

    // Some definitions for drawing lines, etc.

    // Drawline draws text with encaps that project...
    portStream << "% Encapsulate drawing a line" 				<< endl;
    portStream << "%    arguments x1 y1 x2 xy2 width" 		<< endl;
    portStream << "/DrawLine {" 								<< endl;
    portStream << "   gsave" 									<< endl;
    portStream << "   setlinewidth" 							<< endl;
    // We may not always want to set this as it works best with rectangular trees...
//    portStream << "   2 setlinecap"							<< endl;
    portStream << "   0 setgray" 								<< endl;
    portStream << "   moveto" 								<< endl;
    portStream << "   lineto" 								<< endl;
    portStream << "   stroke" 								<< endl;
    portStream << "   grestore" 								<< endl;
    portStream << "   } bind def" 							<< endl;
    portStream << endl;

    portStream << "% Encapsulate drawing text" 				<< endl;
    portStream << "%    arguments x y text" 					<< endl;
    portStream << "/DrawText {" 								<< endl;
    portStream << "  gsave 1 setlinewidth 0 setgray" 			<< endl;
    portStream << "  moveto" 									<< endl;
    portStream << "  show grestore" 							<< endl;
	portStream << "} bind def" 								<< endl;
    portStream << endl;

}

void GPostscriptPort::EndPicture ()
{
    portStream << "showpage" 									<< endl;
    portStream << "%%Trailer" 								<< endl;
    portStream << "%%end" 									<< endl;
    portStream << "%%EOF" 									<< endl;
    portStream.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)
{
    portStream << "<path style=\"stroke:black;";
    portStream << ";stroke-width:" << PenWidth;
    portStream << ";stroke-linecap:square";
    portStream << "\" "; 
    
    
    portStream << "d=\"M";
    portStream << x1 << " " << y1 << " " << x2 << " " << y2 << "\"/>";


//    portStream << "<g style=\"fill:none;stroke:black\"><path d=\"M";
//    portStream << x1 << " " << y1 << " " << x2 << " " << y2 << "\"/>";
//    portStream << "</g>" << endl;
}

void SVGPort::DrawCircle (const GPoint &pt, const int radius)
{
/*	portStream << "newpath" << endl;
	portStream << pt.GetX() << " " << -pt.GetY() << " " << radius << " 0 360 arc" 		<< endl;
	portStream << "stroke" 										<< endl;
	portStream << endl;
*/
}


void SVGPort::DrawRect (const GRect &r)
{
/*	portStream << r.GetLeft() << " " << -r.GetTop() << " moveto" 	<< endl;
	portStream << r.GetWidth() << " 0 rlineto" 					<< endl;
	portStream << "0 " << -r.GetHeight() << " rlineto" 			<< endl;
	portStream << -r.GetWidth() << " 0 rlineto" 					<< endl;
	portStream << "0 " << r.GetHeight() << " rlineto"				<< endl;
	portStream << "closepath" 									<< endl;
	portStream << "stroke" 										<< endl;
	portStream << endl;
*/
}

void SVGPort::DrawText (const int x, const int y, const char *text)
{
    portStream << "<text x=\"" << x << "\" y=\"" << y << "\" style=\"" << fontString << "\" >"
            << text << "</text>" << endl;
}


void SVGPort::StartPicture (char *pictFileName)
{
    portStream.open (pictFileName);
    
    portStream << "<?xml version=\"1.0\" ?>" << endl;

	portStream << "<svg xmlns:xlink=\"http://www.w3.org/1999/xlink\" "
			<< "xmlns=\"http://www.w3.org/2000/svg\" "
			<< " width=\"" << DisplayRect.GetWidth() << "px\" "
        	<< "height=\"" << DisplayRect.GetHeight() << "px\" >" << endl;
        
// <title>test</title>
// <desc>test</desc>
}

void SVGPort::EndPicture ()
{
    portStream << "</svg>" << endl;
    portStream.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:%dpx", font.GetSize());
    fontString += buf;
    
    if (font.IsItalic())
    {
        fontString += ";font-style:italic";
    }
    if (font.IsBold())
    {
        fontString += ";font-weight:bold";
    }

}

MVGPort::MVGPort ()
{ 
	// for now
	fontString = "font '/users/rpage/Sites/CLARITY_.TTF'\n";
	fontString += "font-size 12\n";
	fontString += "text-antialias 0\n"; 
    DisplayRect.SetRect (0, 0, 400, 400);
}

void MVGPort::DrawLine (const int x1, const int y1, const int x2, const int y2)
{
	portStream << "line " <<  x1 << " " << y1 << " " << x2 << " " << y2 << std::endl;
}

void MVGPort::DrawCircle (const GPoint &pt, const int radius)
{
/*	portStream << "newpath" << endl;
	portStream << pt.GetX() << " " << -pt.GetY() << " " << radius << " 0 360 arc" 		<< endl;
	portStream << "stroke" 										<< endl;
	portStream << endl;
*/
}


void MVGPort::DrawRect (const GRect &r)
{
/*	portStream << r.GetLeft() << " " << -r.GetTop() << " moveto" 	<< endl;
	portStream << r.GetWidth() << " 0 rlineto" 					<< endl;
	portStream << "0 " << -r.GetHeight() << " rlineto" 			<< endl;
	portStream << -r.GetWidth() << " 0 rlineto" 					<< endl;
	portStream << "0 " << r.GetHeight() << " rlineto"				<< endl;
	portStream << "closepath" 									<< endl;
	portStream << "stroke" 										<< endl;
	portStream << endl;
*/
}

void MVGPort::DrawText (const int x, const int y, const char *text)
{
	portStream << "text " << x << " " << y << "\"" << text << "\"" << std::endl;
}


void MVGPort::StartPicture (char *pictFileName)
{
    portStream.open (pictFileName);
	
	portStream << "viewbox 0 0 "  << DisplayRect.GetWidth() << " " << DisplayRect.GetHeight() <<  std::endl;
	portStream << fontString;
}

void MVGPort::EndPicture ()
{
     portStream.close ();
}


void MVGPort::GetPrintingRect (GRect &r)
{
    r = DisplayRect;
}

void MVGPort::SetCurrentFont (GBaseFont &font)
{
	// for now
	fontString = "font '/users/rpage/Sites/CLARITY_.TTF'\n";
	fontString += "font-size 12\n";
	fontString += "text-antialias 0\n";

/*    fontString = "";
    fontString += "font-family:";
    fontString += font.GetName();
	
    char buf[32];
    sprintf (buf, ";font-size:%dpx", font.GetSize());
    fontString += buf;
    
    if (font.IsItalic())
    {
        fontString += ";font-style:italic";
    }
    if (font.IsBold())
    {
        fontString += ";font-weight:bold";
    }
*/
}

ImageMapPort::ImageMapPort ()
{ 
//    fontString = "font-family:Times;font-size:12"; 
    DisplayRect.SetRect (0, 0, 400, 400);
}

void ImageMapPort::DrawLine (const int x1, const int y1, const int x2, const int y2)
{
//	portStream << "line " <<  x1 << " " << y1 << " " << x2 << " " << y2 << std::endl;
}

void ImageMapPort::DrawCircle (const GPoint &pt, const int radius)
{
/*	portStream << "newpath" << endl;
	portStream << pt.GetX() << " " << -pt.GetY() << " " << radius << " 0 360 arc" 		<< endl;
	portStream << "stroke" 										<< endl;
	portStream << endl;
*/
}


void ImageMapPort::DrawRect (const GRect &r)
{
/*	portStream << r.GetLeft() << " " << -r.GetTop() << " moveto" 	<< endl;
	portStream << r.GetWidth() << " 0 rlineto" 					<< endl;
	portStream << "0 " << -r.GetHeight() << " rlineto" 			<< endl;
	portStream << -r.GetWidth() << " 0 rlineto" 					<< endl;
	portStream << "0 " << r.GetHeight() << " rlineto"				<< endl;
	portStream << "closepath" 									<< endl;
	portStream << "stroke" 										<< endl;
	portStream << endl;
*/
}

void ImageMapPort::DrawText (const int x, const int y, const char *text)
{
	// x and y specify the bottom left corner of the text string
	
	portStream
		<< "<area shape=\"rect\""
		<< " coords=\"" << x << "," << (y-6) << "," << (x + strlen(text) * 5) << "," << y << "\""
		<< " href=\"http://www.google.com/search?q=" << text << "\" target=\"_new\""
		<< " title = \"" << text << "\""
		<< " onMouseOver=\"writeText('" << text << "')\""
		<< " onMouseOut=\"writeText('')\""
		<< "/>" << std::endl;
}


void ImageMapPort::StartPicture (char *pictFileName)
{
    portStream.open (pictFileName);
	portStream << "<map name=\"leaves\">" << std::endl;
}

void ImageMapPort::EndPicture ()
{
	portStream << "</map>"  << std::endl;
     portStream.close ();
}


void ImageMapPort::GetPrintingRect (GRect &r)
{
    r = DisplayRect;
}

void ImageMapPort::SetCurrentFont (GBaseFont &font)
{

/*    fontString = "";
    fontString += "font-family:";
    fontString += font.GetName();
	
    char buf[32];
    sprintf (buf, ";font-size:%dpx", font.GetSize());
    fontString += buf;
    
    if (font.IsItalic())
    {
        fontString += ";font-style:italic";
    }
    if (font.IsBold())
    {
        fontString += ";font-weight:bold";
    }
*/
}