]> git.saurik.com Git - wxWidgets.git/blob - src/gtk1/dcps.cpp
#ifdefed out Ole... functions for Cygwin.
[wxWidgets.git] / src / gtk1 / dcps.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: dcps.cpp
3 // Purpose: wxPostScriptDC implementation
4 // Author: Julian Smart, Robert Roebling, Markus Holzhem
5 // Modified by:
6 // Created: 04/01/98
7 // RCS-ID: $Id$
8 // Copyright: (c) Julian Smart and Markus Holzem
9 // Licence: wxWindows license
10 /////////////////////////////////////////////////////////////////////////////
11
12 #ifdef __GNUG__
13 #pragma implementation
14 #pragma interface
15 #endif
16
17 #include "wx/postscrp.h"
18 #include "wx/dcmemory.h"
19 #include "wx/utils.h"
20 #include "wx/intl.h"
21 #include "wx/filedlg.h"
22 #include "wx/app.h"
23 #include "wx/msgdlg.h"
24 #include "wx/image.h"
25 #include "wx/log.h"
26
27 //-----------------------------------------------------------------------------
28 // start and end of document/page
29 //-----------------------------------------------------------------------------
30
31 static const char *wxPostScriptHeaderEllipse = "\
32 /ellipsedict 8 dict def\n\
33 ellipsedict /mtrx matrix put\n\
34 /ellipse {\n\
35 ellipsedict begin\n\
36 /endangle exch def\n\
37 /startangle exch def\n\
38 /yrad exch def\n\
39 /xrad exch def\n\
40 /y exch def\n\
41 /x exch def\n\
42 /savematrix mtrx currentmatrix def\n\
43 x y translate\n\
44 xrad yrad scale\n\
45 0 0 1 startangle endangle arc\n\
46 savematrix setmatrix\n\
47 end\n\
48 } def\n\
49 ";
50
51 static const char *wxPostScriptHeaderEllipticArc= "\
52 /ellipticarcdict 8 dict def\n\
53 ellipticarcdict /mtrx matrix put\n\
54 /ellipticarc\n\
55 { ellipticarcdict begin\n\
56 /do_fill exch def\n\
57 /endangle exch def\n\
58 /startangle exch def\n\
59 /yrad exch def\n\
60 /xrad exch def \n\
61 /y exch def\n\
62 /x exch def\n\
63 /savematrix mtrx currentmatrix def\n\
64 x y translate\n\
65 xrad yrad scale\n\
66 do_fill { 0 0 moveto } if\n\
67 0 0 1 startangle endangle arc\n\
68 savematrix setmatrix\n\
69 do_fill { fill }{ stroke } ifelse\n\
70 end\n\
71 } def\n";
72
73 static const char *wxPostScriptHeaderSpline = "\
74 /DrawSplineSection {\n\
75 /y3 exch def\n\
76 /x3 exch def\n\
77 /y2 exch def\n\
78 /x2 exch def\n\
79 /y1 exch def\n\
80 /x1 exch def\n\
81 /xa x1 x2 x1 sub 0.666667 mul add def\n\
82 /ya y1 y2 y1 sub 0.666667 mul add def\n\
83 /xb x3 x2 x3 sub 0.666667 mul add def\n\
84 /yb y3 y2 y3 sub 0.666667 mul add def\n\
85 x1 y1 lineto\n\
86 xa ya xb yb x3 y3 curveto\n\
87 } def\n\
88 ";
89
90 static const char *wxPostScriptHeaderColourImage = "\
91 % define 'colorimage' if it isn't defined\n\
92 % ('colortogray' and 'mergeprocs' come from xwd2ps\n\
93 % via xgrab)\n\
94 /colorimage where % do we know about 'colorimage'?\n\
95 { pop } % yes: pop off the 'dict' returned\n\
96 { % no: define one\n\
97 /colortogray { % define an RGB->I function\n\
98 /rgbdata exch store % call input 'rgbdata'\n\
99 rgbdata length 3 idiv\n\
100 /npixls exch store\n\
101 /rgbindx 0 store\n\
102 0 1 npixls 1 sub {\n\
103 grays exch\n\
104 rgbdata rgbindx get 20 mul % Red\n\
105 rgbdata rgbindx 1 add get 32 mul % Green\n\
106 rgbdata rgbindx 2 add get 12 mul % Blue\n\
107 add add 64 idiv % I = .5G + .31R + .18B\n\
108 put\n\
109 /rgbindx rgbindx 3 add store\n\
110 } for\n\
111 grays 0 npixls getinterval\n\
112 } bind def\n\
113 \n\
114 % Utility procedure for colorimage operator.\n\
115 % This procedure takes two procedures off the\n\
116 % stack and merges them into a single procedure.\n\
117 \n\
118 /mergeprocs { % def\n\
119 dup length\n\
120 3 -1 roll\n\
121 dup\n\
122 length\n\
123 dup\n\
124 5 1 roll\n\
125 3 -1 roll\n\
126 add\n\
127 array cvx\n\
128 dup\n\
129 3 -1 roll\n\
130 0 exch\n\
131 putinterval\n\
132 dup\n\
133 4 2 roll\n\
134 putinterval\n\
135 } bind def\n\
136 \n\
137 /colorimage { % def\n\
138 pop pop % remove 'false 3' operands\n\
139 {colortogray} mergeprocs\n\
140 image\n\
141 } bind def\n\
142 } ifelse % end of 'false' case\n\
143 ";
144
145 static char wxPostScriptHeaderReencodeISO1[] =
146 "\n/reencodeISO {\n"
147 "dup dup findfont dup length dict begin\n"
148 "{ 1 index /FID ne { def }{ pop pop } ifelse } forall\n"
149 "/Encoding ISOLatin1Encoding def\n"
150 "currentdict end definefont\n"
151 "} def\n"
152 "/ISOLatin1Encoding [\n"
153 "/.notdef/.notdef/.notdef/.notdef/.notdef/.notdef/.notdef/.notdef\n"
154 "/.notdef/.notdef/.notdef/.notdef/.notdef/.notdef/.notdef/.notdef\n"
155 "/.notdef/.notdef/.notdef/.notdef/.notdef/.notdef/.notdef/.notdef\n"
156 "/.notdef/.notdef/.notdef/.notdef/.notdef/.notdef/.notdef/.notdef\n"
157 "/space/exclam/quotedbl/numbersign/dollar/percent/ampersand/quoteright\n"
158 "/parenleft/parenright/asterisk/plus/comma/minus/period/slash\n"
159 "/zero/one/two/three/four/five/six/seven/eight/nine/colon/semicolon\n"
160 "/less/equal/greater/question/at/A/B/C/D/E/F/G/H/I/J/K/L/M/N\n"
161 "/O/P/Q/R/S/T/U/V/W/X/Y/Z/bracketleft/backslash/bracketright\n"
162 "/asciicircum/underscore/quoteleft/a/b/c/d/e/f/g/h/i/j/k/l/m\n"
163 "/n/o/p/q/r/s/t/u/v/w/x/y/z/braceleft/bar/braceright/asciitilde\n"
164 "/.notdef/.notdef/.notdef/.notdef/.notdef/.notdef/.notdef/.notdef\n"
165 "/.notdef/.notdef/.notdef/.notdef/.notdef/.notdef/.notdef/.notdef\n"
166 "/.notdef/dotlessi/grave/acute/circumflex/tilde/macron/breve\n"
167 "/dotaccent/dieresis/.notdef/ring/cedilla/.notdef/hungarumlaut\n";
168
169 static char wxPostScriptHeaderReencodeISO2[] =
170 "/ogonek/caron/space/exclamdown/cent/sterling/currency/yen/brokenbar\n"
171 "/section/dieresis/copyright/ordfeminine/guillemotleft/logicalnot\n"
172 "/hyphen/registered/macron/degree/plusminus/twosuperior/threesuperior\n"
173 "/acute/mu/paragraph/periodcentered/cedilla/onesuperior/ordmasculine\n"
174 "/guillemotright/onequarter/onehalf/threequarters/questiondown\n"
175 "/Agrave/Aacute/Acircumflex/Atilde/Adieresis/Aring/AE/Ccedilla\n"
176 "/Egrave/Eacute/Ecircumflex/Edieresis/Igrave/Iacute/Icircumflex\n"
177 "/Idieresis/Eth/Ntilde/Ograve/Oacute/Ocircumflex/Otilde/Odieresis\n"
178 "/multiply/Oslash/Ugrave/Uacute/Ucircumflex/Udieresis/Yacute\n"
179 "/Thorn/germandbls/agrave/aacute/acircumflex/atilde/adieresis\n"
180 "/aring/ae/ccedilla/egrave/eacute/ecircumflex/edieresis/igrave\n"
181 "/iacute/icircumflex/idieresis/eth/ntilde/ograve/oacute/ocircumflex\n"
182 "/otilde/odieresis/divide/oslash/ugrave/uacute/ucircumflex/udieresis\n"
183 "/yacute/thorn/ydieresis\n"
184 "] def\n\n";
185
186 //-------------------------------------------------------------------------------
187 // wxPostScriptDC
188 //-------------------------------------------------------------------------------
189
190 wxPostScriptDC::wxPostScriptDC ()
191 {
192 m_pstream = (ofstream*) NULL;
193
194 m_currentRed = 0;
195 m_currentGreen = 0;
196 m_currentBlue = 0;
197
198 m_pageNumber = 0;
199
200 m_clipping = FALSE;
201
202 m_underlinePosition = 0.0;
203 m_underlineThickness = 0.0;
204
205 m_signX = 1; // default x-axis left to right
206 m_signY = -1; // default y-axis bottom up -> top down
207 }
208
209 wxPostScriptDC::wxPostScriptDC (const wxString& file, bool interactive, wxWindow *parent)
210 {
211 m_pstream = (ofstream*) NULL;
212
213 m_currentRed = 0;
214 m_currentGreen = 0;
215 m_currentBlue = 0;
216
217 m_pageNumber = 0;
218
219 m_clipping = FALSE;
220
221 m_underlinePosition = 0.0;
222 m_underlineThickness = 0.0;
223
224 m_signX = 1; // default x-axis left to right
225 m_signY = -1; // default y-axis bottom up -> top down
226
227 Create(file, interactive, parent);
228 }
229
230 bool wxPostScriptDC::Create(const wxString& file, bool interactive, wxWindow *parent)
231 {
232 m_isInteractive = interactive;
233
234 m_title = "";
235 m_filename = file;
236
237 #ifdef __WXMSW__
238 // Can only send to file in Windows
239 wxThePrintSetupData->SetPrinterMode(PS_FILE);
240 #endif
241
242 if (m_isInteractive)
243 {
244 if ((m_ok = PrinterDialog (parent) ) == FALSE) return FALSE;
245 }
246 else
247 {
248 m_ok = TRUE;
249 }
250
251 return m_ok;
252 }
253
254 wxPostScriptDC::~wxPostScriptDC ()
255 {
256 if (m_pstream) delete m_pstream;
257 }
258
259 bool wxPostScriptDC::Ok() const
260 {
261 return m_ok;
262 }
263
264 bool wxPostScriptDC::PrinterDialog(wxWindow *parent)
265 {
266 wxPostScriptPrintDialog dialog( parent, _("Printer Settings"), wxPoint(150, 150), wxSize(400, 400),
267 wxDEFAULT_DIALOG_STYLE | wxDIALOG_MODAL );
268 m_ok = (dialog.ShowModal () == wxID_OK);
269
270 if (!m_ok) return FALSE;
271
272 if ((m_filename == "") &&
273 (wxThePrintSetupData->GetPrinterMode() == PS_PREVIEW ||
274 wxThePrintSetupData->GetPrinterMode() == PS_PRINTER))
275 {
276 // steve, 05.09.94
277 #ifdef __VMS__
278 wxThePrintSetupData->SetPrinterFile("preview");
279 #else
280 // For PS_PRINTER action this depends on a Unix-style print spooler
281 // since the wx_printer_file can be destroyed during a session
282 // @@@ TODO: a Windows-style answer for non-Unix
283 char userId[256];
284 wxGetUserId (userId, sizeof (userId) / sizeof (char));
285 char tmp[256];
286 strcpy (tmp, "/tmp/preview_");
287 strcat (tmp, userId);
288 wxThePrintSetupData->SetPrinterFile(tmp);
289 #endif
290 char tmp2[256];
291 strcpy(tmp2, wxThePrintSetupData->GetPrinterFile());
292 strcat (tmp2, ".ps");
293 wxThePrintSetupData->SetPrinterFile(tmp2);
294 m_filename = tmp2;
295 }
296 else if ((m_filename == "") && (wxThePrintSetupData->GetPrinterMode() == PS_FILE))
297 {
298 char *file = wxSaveFileSelector (_("PostScript"), "ps");
299 if (!file)
300 {
301 m_ok = FALSE;
302 return FALSE;
303 }
304 wxThePrintSetupData->SetPrinterFile(file);
305 m_filename = file;
306 m_ok = TRUE;
307 }
308
309 return m_ok;
310 }
311
312 void wxPostScriptDC::SetClippingRegion (long x, long y, long w, long h)
313 {
314 wxCHECK_RET( m_ok && m_pstream, "invalid postscript dc" );
315
316 if (m_clipping) return;
317
318 wxDC::SetClippingRegion( x, y, w, h );
319
320 m_clipping = TRUE;
321 *m_pstream << "gsave\n"
322 << "newpath\n"
323 << XLOG2DEV(x) << " " << YLOG2DEV(y) << " moveto\n"
324 << XLOG2DEV(x+w) << " " << YLOG2DEV(y) << " lineto\n"
325 << XLOG2DEV(x+w) << " " << YLOG2DEV(y+h) << " lineto\n"
326 << XLOG2DEV(x) << " " << YLOG2DEV(y+h) << " lineto\n"
327 << "closepath clip newpath\n";
328 }
329
330 void wxPostScriptDC::SetClippingRegion( const wxRegion &WXUNUSED(region) )
331 {
332 }
333
334 void wxPostScriptDC::DestroyClippingRegion()
335 {
336 wxCHECK_RET( m_ok && m_pstream, "invalid postscript dc" );
337
338 wxDC::DestroyClippingRegion();
339
340 if (m_clipping)
341 {
342 m_clipping = FALSE;
343 *m_pstream << "grestore\n";
344 }
345 }
346
347 void wxPostScriptDC::Clear()
348 {
349 wxFAIL_MSG( "wxPostScriptDC::Clear not implemented." );
350 }
351
352 void wxPostScriptDC::FloodFill (long WXUNUSED(x), long WXUNUSED(y), const wxColour &WXUNUSED(col), int WXUNUSED(style))
353 {
354 wxFAIL_MSG( "wxPostScriptDC::FloodFill not implemented." );
355 }
356
357 bool wxPostScriptDC::GetPixel (long WXUNUSED(x), long WXUNUSED(y), wxColour * WXUNUSED(col)) const
358 {
359 wxFAIL_MSG( "wxPostScriptDC::GetPixel not implemented." );
360 return FALSE;
361 }
362
363 void wxPostScriptDC::CrossHair (long WXUNUSED(x), long WXUNUSED(y))
364 {
365 wxFAIL_MSG( "wxPostScriptDC::CrossHair not implemented." );
366 }
367
368 void wxPostScriptDC::DrawLine (long x1, long y1, long x2, long y2)
369 {
370 wxCHECK_RET( m_ok && m_pstream, "invalid postscript dc" );
371
372 if (m_pen.GetStyle() == wxTRANSPARENT) return;
373
374 SetPen( m_pen );
375
376 *m_pstream << "newpath\n"
377 << XLOG2DEV(x1) << " " << YLOG2DEV (y1) << " moveto\n"
378 << XLOG2DEV(x2) << " " << YLOG2DEV (y2) << " lineto\n"
379 << "stroke\n";
380
381 CalcBoundingBox( x1, y1 );
382 CalcBoundingBox( x2, y2 );
383 }
384
385 #define RAD2DEG 57.29577951308
386
387 void wxPostScriptDC::DrawArc (long x1, long y1, long x2, long y2, long xc, long yc)
388 {
389 wxCHECK_RET( m_ok && m_pstream, "invalid postscript dc" );
390
391 long dx = x1 - xc;
392 long dy = y1 - yc;
393 long radius = (long) sqrt(dx*dx+dy*dy);
394 double alpha1, alpha2;
395
396 if (x1 == x2 && y1 == y2)
397 {
398 alpha1 = 0.0;
399 alpha2 = 360.0;
400 } else if (radius == 0.0)
401 {
402 alpha1 = alpha2 = 0.0;
403 } else
404 {
405 alpha1 = (x1 - xc == 0) ?
406 (y1 - yc < 0) ? 90.0 : -90.0 :
407 -atan2(double(y1-yc), double(x1-xc)) * RAD2DEG;
408 alpha2 = (x2 - xc == 0) ?
409 (y2 - yc < 0) ? 90.0 : -90.0 :
410 -atan2(double(y2-yc), double(x2-xc)) * RAD2DEG;
411 }
412 while (alpha1 <= 0) alpha1 += 360;
413 while (alpha2 <= 0) alpha2 += 360; // adjust angles to be between
414 while (alpha1 > 360) alpha1 -= 360; // 0 and 360 degree
415 while (alpha2 > 360) alpha2 -= 360;
416
417 if (m_brush.GetStyle() != wxTRANSPARENT)
418 {
419 SetBrush( m_brush );
420 *m_pstream << "newpath\n"
421 << XLOG2DEV(xc) << " "
422 << YLOG2DEV(yc) << " "
423 << XLOG2DEVREL(radius) << " "
424 << YLOG2DEVREL(radius) << " "
425 << alpha1 << " "
426 << alpha2 << " ellipse\n"
427 << XLOG2DEV(xc) << " "
428 << YLOG2DEV(yc) << " lineto\n"
429 << "closepath\n"
430 << "fill\n";
431 }
432
433 if (m_pen.GetStyle() != wxTRANSPARENT)
434 {
435 SetPen( m_pen );
436 *m_pstream << "newpath\n"
437 << XLOG2DEV(xc) << " "
438 << YLOG2DEV(yc) << " "
439 << XLOG2DEVREL(radius) << " "
440 << YLOG2DEVREL(radius) << " "
441 << alpha1 << " "
442 << alpha2 << " ellipse\n"
443 << "stroke\n";
444 }
445
446 CalcBoundingBox( xc-radius, yc-radius );
447 CalcBoundingBox( xc+radius, yc+radius );
448 }
449
450 void wxPostScriptDC::DrawEllipticArc(long x,long y,long w,long h,double sa,double ea)
451 {
452 wxCHECK_RET( m_ok && m_pstream, "invalid postscript dc" );
453
454 if (sa>=360 || sa<=-360) sa=sa-int(sa/360)*360;
455 if (ea>=360 || ea<=-360) ea=ea-int(ea/360)*360;
456 if (sa<0) sa+=360;
457 if (ea<0) ea+=360;
458
459 if (sa==ea)
460 {
461 DrawEllipse(x,y,w,h);
462 return;
463 }
464
465 if (m_brush.GetStyle () != wxTRANSPARENT)
466 {
467 SetBrush( m_brush );
468
469 *m_pstream << "newpath\n"
470 << XLOG2DEV(x+w/2) << " " << YLOG2DEV(y+h/2) << " "
471 << XLOG2DEVREL(w/2) << " " << YLOG2DEVREL(h/2) << " "
472 << int(sa) <<" "<< int(ea) << " true ellipticarc\n";
473
474 CalcBoundingBox( x ,y );
475 CalcBoundingBox( x+w, y+h );
476 }
477
478 if (m_pen.GetStyle () != wxTRANSPARENT)
479 {
480 SetPen( m_pen );
481
482 *m_pstream << "newpath\n"
483 << XLOG2DEV(x+w/2) << " " << YLOG2DEV(y+h/2) << " "
484 << XLOG2DEVREL(w/2) << " " << XLOG2DEVREL(h/2) << " "
485 << int(sa) <<" "<< int(ea) << " false ellipticarc\n";
486
487 CalcBoundingBox( x, y );
488 CalcBoundingBox( x+w, y+h );
489 }
490 }
491
492 void wxPostScriptDC::DrawPoint (long x, long y)
493 {
494 wxCHECK_RET( m_ok && m_pstream, "invalid postscript dc" );
495
496 if (m_pen.GetStyle() == wxTRANSPARENT) return;
497
498 SetPen (m_pen);
499
500 *m_pstream << "newpath\n"
501 << XLOG2DEV(x) << " " << YLOG2DEV (y) << " moveto\n"
502 << XLOG2DEV(x+1) << " " << YLOG2DEV (y) << " lineto\n"
503 << "stroke\n";
504
505 CalcBoundingBox( x, y );
506 }
507
508 void wxPostScriptDC::DrawPolygon (int n, wxPoint points[], long xoffset, long yoffset, int WXUNUSED(fillStyle))
509 {
510 wxCHECK_RET( m_ok && m_pstream, "invalid postscript dc" );
511
512 if (n <= 0) return;
513
514 if (m_brush.GetStyle () != wxTRANSPARENT)
515 {
516 SetBrush( m_brush );
517
518 *m_pstream << "newpath\n";
519
520 long xx = XLOG2DEV(points[0].x + xoffset);
521 long yy = YLOG2DEV(points[0].y + yoffset);
522 *m_pstream << xx << " " << yy << " moveto\n";
523 CalcBoundingBox( points[0].x + xoffset, points[0].y + yoffset );
524
525 for (int i = 1; i < n; i++)
526 {
527 xx = XLOG2DEV(points[i].x + xoffset);
528 yy = YLOG2DEV(points[i].y + yoffset);
529 *m_pstream << xx << " " << yy << " lineto\n";
530 CalcBoundingBox( points[i].x + xoffset, points[i].y + yoffset);
531 }
532 *m_pstream << "fill\n";
533 }
534
535 if (m_pen.GetStyle () != wxTRANSPARENT)
536 {
537 SetPen( m_pen );
538
539 *m_pstream << "newpath\n";
540
541 long xx = XLOG2DEV(points[0].x + xoffset);
542 long yy = YLOG2DEV(points[0].y + yoffset);
543 *m_pstream << xx << " " << yy << " moveto\n";
544 CalcBoundingBox( points[0].x + xoffset, points[0].y + yoffset );
545
546 for (int i = 1; i < n; i++)
547 {
548 xx = XLOG2DEV(points[i].x + xoffset);
549 yy = YLOG2DEV(points[i].y + yoffset);
550 *m_pstream << xx << " " << yy << " lineto\n";
551 CalcBoundingBox( points[i].x + xoffset, points[i].y + yoffset);
552 }
553
554 *m_pstream << "stroke\n";
555 }
556 }
557
558 void wxPostScriptDC::DrawLines (int n, wxPoint points[], long xoffset, long yoffset)
559 {
560 wxCHECK_RET( m_ok && m_pstream, "invalid postscript dc" );
561
562 if (m_pen.GetStyle() == wxTRANSPARENT) return;
563 if (n <= 0) return;
564
565 SetPen (m_pen);
566
567 for (int i=0; i<n ; ++i)
568 {
569 CalcBoundingBox( XLOG2DEV(points[i].x+xoffset), YLOG2DEV(points[i].y+yoffset));
570 }
571
572 *m_pstream << "newpath\n"
573 << XLOG2DEV(points[0].x+xoffset) << " "
574 << YLOG2DEV(points[0].y+yoffset) << " moveto\n";
575
576 for (int i = 1; i < n; i++)
577 {
578 *m_pstream << XLOG2DEV(points[i].x+xoffset) << " "
579 << YLOG2DEV(points[i].y+yoffset) << " lineto\n";
580 }
581
582 *m_pstream << "stroke\n";
583 }
584
585 void wxPostScriptDC::DrawRectangle (long x, long y, long width, long height)
586 {
587 wxCHECK_RET( m_ok && m_pstream, "invalid postscript dc" );
588
589 if (m_brush.GetStyle () != wxTRANSPARENT)
590 {
591 SetBrush( m_brush );
592
593 *m_pstream << "newpath\n"
594 << XLOG2DEV(x) << " " << YLOG2DEV(y) << " moveto\n"
595 << XLOG2DEV(x + width) << " " << YLOG2DEV(y) << " lineto\n"
596 << XLOG2DEV(x + width) << " " << YLOG2DEV(y + height) << " lineto\n"
597 << XLOG2DEV(x) << " " << YLOG2DEV(y + height) << " lineto\n"
598 << "closepath\n"
599 << "fill\n";
600
601 CalcBoundingBox( x, y );
602 CalcBoundingBox( x + width, y + height );
603 }
604
605 if (m_pen.GetStyle () != wxTRANSPARENT)
606 {
607 SetPen (m_pen);
608
609 *m_pstream << "newpath\n"
610 << XLOG2DEV(x) << " " << YLOG2DEV(y) << " moveto\n"
611 << XLOG2DEV(x + width) << " " << YLOG2DEV(y) << " lineto\n"
612 << XLOG2DEV(x + width) << " " << YLOG2DEV(y + height) << " lineto\n"
613 << XLOG2DEV(x) << " " << YLOG2DEV(y + height) << " lineto\n"
614 << "closepath\n"
615 << "stroke\n";
616
617 CalcBoundingBox( x, y );
618 CalcBoundingBox( x + width, y + height );
619 }
620 }
621
622 void wxPostScriptDC::DrawRoundedRectangle (long x, long y, long width, long height, double radius)
623 {
624 wxCHECK_RET( m_ok && m_pstream, "invalid postscript dc" );
625
626 if (radius < 0.0)
627 {
628 // Now, a negative radius is interpreted to mean
629 // 'the proportion of the smallest X or Y dimension'
630 double smallest = 0.0;
631 if (width < height)
632 smallest = width;
633 else
634 smallest = height;
635 radius = (-radius * smallest);
636 }
637
638 long rad = (long) radius;
639
640 if (m_brush.GetStyle () != wxTRANSPARENT)
641 {
642 SetBrush( m_brush );
643
644 // Draw rectangle anticlockwise
645 *m_pstream << "newpath\n"
646 << XLOG2DEV(x + rad) << " " << YLOG2DEV(y + rad) << " " << XLOG2DEVREL(rad) << " 90 180 arc\n"
647 << XLOG2DEV(x) << " " << YLOG2DEV(y + rad) << " moveto\n"
648 << XLOG2DEV(x + rad) << " " << YLOG2DEV(y + height - rad) << " " << XLOG2DEVREL(rad) << " 180 270 arc\n"
649 << XLOG2DEV(x + width - rad) << " " << YLOG2DEV(y + height) << " lineto\n"
650 << XLOG2DEV(x + width - rad) << " " << YLOG2DEV(y + height - rad) << " " << XLOG2DEVREL(rad) << " 270 0 arc\n"
651 << XLOG2DEV(x + width) << " " << YLOG2DEV(y + rad) << " lineto\n"
652 << XLOG2DEV(x + width - rad) << " " << YLOG2DEV(y + rad) << " " << XLOG2DEVREL(rad) << " 0 90 arc\n"
653 << XLOG2DEV(x + rad) << " " << YLOG2DEV(y) << " lineto\n"
654 << "closepath\n"
655 << "fill\n";
656
657 CalcBoundingBox( x, y );
658 CalcBoundingBox( x + width, y + height );
659 }
660
661 if (m_pen.GetStyle () != wxTRANSPARENT)
662 {
663 SetPen (m_pen);
664
665 // Draw rectangle anticlockwise
666 *m_pstream << "newpath\n"
667 << XLOG2DEV(x + rad) << " " << YLOG2DEV(y + rad) << " " << XLOG2DEVREL(rad) << " 90 180 arc\n"
668 << XLOG2DEV(x) << " " << YLOG2DEV(y + rad) << " moveto\n"
669 << XLOG2DEV(x + rad) << " " << YLOG2DEV(y + height - rad) << " " << XLOG2DEVREL(rad) << " 180 270 arc\n"
670 << XLOG2DEV(x + width - rad) << " " << YLOG2DEV(y + height) << " lineto\n"
671 << XLOG2DEV(x + width - rad) << " " << YLOG2DEV(y + height - rad) << " " << XLOG2DEVREL(rad) << " 270 0 arc\n"
672 << XLOG2DEV(x + width) << " " << YLOG2DEV(y + rad) << " lineto\n"
673 << XLOG2DEV(x + width - rad) << " " << YLOG2DEV(y + rad) << " " << XLOG2DEVREL(rad) << " 0 90 arc\n"
674 << XLOG2DEV(x + rad) << " " << YLOG2DEV(y) << " lineto\n"
675 << "closepath\n"
676 << "stroke\n";
677
678 CalcBoundingBox( x, y );
679 CalcBoundingBox( x + width, y + height );
680 }
681 }
682
683 void wxPostScriptDC::DrawEllipse (long x, long y, long width, long height)
684 {
685 wxCHECK_RET( m_ok && m_pstream, "invalid postscript dc" );
686
687 if (m_brush.GetStyle () != wxTRANSPARENT)
688 {
689 SetBrush (m_brush);
690
691 *m_pstream << "newpath\n"
692 << XLOG2DEV(x + width / 2) << " " << YLOG2DEV(y + height / 2) << " "
693 << XLOG2DEV(width / 2) << " " << YLOG2DEVREL(height / 2) << " 0 360 ellipse\n"
694 << "fill\n";
695
696 CalcBoundingBox( x - width, y - height );
697 CalcBoundingBox( x + width, y + height );
698 }
699
700 if (m_pen.Ok() && m_pen.GetStyle () != wxTRANSPARENT)
701 {
702 SetPen (m_pen);
703
704 *m_pstream << "newpath\n"
705 << XLOG2DEV(x + width / 2) << " " << YLOG2DEV(y + height / 2) << " "
706 << XLOG2DEV(width / 2) << " " << YLOG2DEVREL(height / 2) << " 0 360 ellipse\n"
707 << "stroke\n";
708
709 CalcBoundingBox( x - width, y - height );
710 CalcBoundingBox( x + width, y + height );
711 }
712 }
713
714 void wxPostScriptDC::DrawIcon (const wxIcon& icon, long x, long y)
715 {
716 DrawBitmap( icon, x, y, TRUE );
717 }
718
719 void wxPostScriptDC::DrawBitmap( const wxBitmap& bitmap, long x, long y, bool WXUNUSED(useMask) )
720 {
721 wxCHECK_RET( m_ok && m_pstream, "invalid postscript dc" );
722
723 if (!bitmap.Ok()) return;
724
725 wxImage image( bitmap );
726
727 if (!image.Ok()) return;
728
729 int ww = XLOG2DEVREL(image.GetWidth());
730 int hh = YLOG2DEVREL(image.GetHeight());
731
732 image = image.Scale( ww, hh );
733
734 if (!image.Ok()) return;
735
736 int xx = XLOG2DEV(x);
737 int yy = YLOG2DEV(y + bitmap.GetHeight());
738
739 *m_pstream << "/origstate save def\n"
740 << "20 dict begin\n"
741 << "/pix " << ww << " string def\n"
742 << "/grays " << ww << " string def\n"
743 << "/npixels 0 def\n"
744 << "/rgbindx 0 def\n"
745 << xx << " " << yy << " translate\n"
746 << ww << " " << hh << " scale\n"
747 << ww << " " << hh << " 8\n"
748 << "[" << ww << " 0 0 " << (-hh) << " 0 " << hh << "]\n"
749 << "{currentfile pix readhexstring pop}\n"
750 << "false 3 colorimage\n";
751
752 for (int j = 0; j < hh; j++)
753 {
754 for (int i = 0; i < ww; i++)
755 {
756 char buffer[5];
757 buffer[2] = 0;
758 wxDecToHex( image.GetRed(i,j), buffer );
759 *m_pstream << buffer;
760 wxDecToHex( image.GetGreen(i,j), buffer );
761 *m_pstream << buffer;
762 wxDecToHex( image.GetBlue(i,j), buffer );
763 *m_pstream << buffer;
764 }
765 *m_pstream << "\n";
766 }
767
768 *m_pstream << "end\n";
769 *m_pstream << "origstate restore\n";
770
771 }
772
773 void wxPostScriptDC::SetFont (const wxFont& font)
774 {
775 wxCHECK_RET( m_ok && m_pstream, "invalid postscript dc" );
776
777 if (!font.Ok()) return;
778
779 m_font = font;
780
781 char *name = wxTheFontNameDirectory->GetPostScriptName( m_font.GetFamily(),
782 m_font.GetWeight(),
783 m_font.GetStyle() );
784 if (!name) name = "Times-Roman";
785
786 *m_pstream << "/" << name << " reencodeISO def\n"
787 << "/" << name << " findfont\n"
788 << YLOG2DEVREL(font.GetPointSize())
789 << " scalefont setfont\n";
790 }
791
792 void wxPostScriptDC::SetPen( const wxPen& pen )
793 {
794 wxCHECK_RET( m_ok && m_pstream, "invalid postscript dc" );
795
796 if (!pen.Ok()) return;
797
798 int oldStyle = m_pen.GetStyle();
799
800 m_pen = pen;
801
802 *m_pstream << XLOG2DEVREL(m_pen.GetWidth()) << " setlinewidth\n";
803
804 /*
805 Line style - WRONG: 2nd arg is OFFSET
806
807 Here, I'm afraid you do not conceive meaning of parameters of 'setdash'
808 operator correctly. You should look-up this in the Red Book: the 2nd parame-
809 ter is not number of values in the array of the first one, but an offset
810 into this description of the pattern. I mean a real *offset* not index
811 into array. I.e. If the command is [3 4] 1 setdash is used, then there
812 will be first black line *2* units long, then space 4 units, then the
813 pattern of *3* units black, 4 units space will be repeated.
814 */
815
816 static const char *dotted = "[2 5] 2";
817 static const char *short_dashed = "[4 4] 2";
818 static const char *long_dashed = "[4 8] 2";
819 static const char *dotted_dashed = "[6 6 2 6] 4";
820
821 const char *psdash = (char *) NULL;
822 switch (m_pen.GetStyle ())
823 {
824 case wxDOT: psdash = dotted; break;
825 case wxSHORT_DASH: psdash = short_dashed; break;
826 case wxLONG_DASH: psdash = long_dashed; break;
827 case wxDOT_DASH: psdash = dotted_dashed; break;
828 case wxSOLID:
829 case wxTRANSPARENT:
830 default: psdash = "[] 0"; break;
831 }
832
833 if (oldStyle != m_pen.GetStyle())
834 {
835 *m_pstream << psdash << " setdash\n";
836 }
837
838 // Line colour
839 unsigned char red = m_pen.GetColour().Red();
840 unsigned char blue = m_pen.GetColour().Blue();
841 unsigned char green = m_pen.GetColour().Green();
842
843 if (!m_colour)
844 {
845 // Anything not white is black
846 if (!(red == (unsigned char) 255 && blue == (unsigned char) 255
847 && green == (unsigned char) 255))
848 {
849 red = (unsigned char) 0;
850 green = (unsigned char) 0;
851 blue = (unsigned char) 0;
852 }
853
854 // setgray here ?
855 }
856
857 if (!(red == m_currentRed && green == m_currentGreen && blue == m_currentBlue))
858 {
859 long redPS = (long) (((int) red) / 255.0);
860 long bluePS = (long) (((int) blue) / 255.0);
861 long greenPS = (long) (((int) green) / 255.0);
862
863 *m_pstream << redPS << " " << greenPS << " " << bluePS << " setrgbcolor\n";
864
865 m_currentRed = red;
866 m_currentBlue = blue;
867 m_currentGreen = green;
868 }
869 }
870
871 void wxPostScriptDC::SetBrush( const wxBrush& brush )
872 {
873 wxCHECK_RET( m_ok && m_pstream, "invalid postscript dc" );
874
875 if (!brush.Ok()) return;
876
877 m_brush = brush;
878
879 // Brush colour
880 unsigned char red = m_brush.GetColour ().Red ();
881 unsigned char blue = m_brush.GetColour ().Blue ();
882 unsigned char green = m_brush.GetColour ().Green ();
883
884 if (!m_colour)
885 {
886 // Anything not black is white
887 if (!(red == (unsigned char) 0 && blue == (unsigned char) 0
888 && green == (unsigned char) 0))
889 {
890 red = (unsigned char) 255;
891 green = (unsigned char) 255;
892 blue = (unsigned char) 255;
893 }
894
895 // setgray here ?
896 }
897
898 if (!(red == m_currentRed && green == m_currentGreen && blue == m_currentBlue))
899 {
900 long redPS = (long) (((int) red) / 255.0);
901 long bluePS = (long) (((int) blue) / 255.0);
902 long greenPS = (long) (((int) green) / 255.0);
903 *m_pstream << redPS << " " << greenPS << " " << bluePS << " setrgbcolor\n";
904 m_currentRed = red;
905 m_currentBlue = blue;
906 m_currentGreen = green;
907 }
908 }
909
910 void wxPostScriptDC::DrawText( const wxString& text, long x, long y, bool WXUNUSED(use16bit) )
911 {
912 wxCHECK_RET( m_ok && m_pstream, "invalid postscript dc" );
913
914 SetFont( m_font );
915
916 if (m_textForegroundColour.Ok ())
917 {
918 unsigned char red = m_textForegroundColour.Red ();
919 unsigned char blue = m_textForegroundColour.Blue ();
920 unsigned char green = m_textForegroundColour.Green ();
921
922 if (!m_colour)
923 {
924 // Anything not white is black
925 if (!(red == (unsigned char) 255 && blue == (unsigned char) 255
926 && green == (unsigned char) 255))
927 {
928 red = (unsigned char) 0;
929 green = (unsigned char) 0;
930 blue = (unsigned char) 0;
931 }
932 }
933
934 // maybe setgray here ?
935
936 if (!(red == m_currentRed && green == m_currentGreen && blue == m_currentBlue))
937 {
938 long redPS = (long) (((int) red) / 255.0);
939 long bluePS = (long) (((int) blue) / 255.0);
940 long greenPS = (long) (((int) green) / 255.0);
941 *m_pstream << redPS << " " << greenPS << " " << bluePS << " setrgbcolor\n";
942
943 m_currentRed = red;
944 m_currentBlue = blue;
945 m_currentGreen = green;
946 }
947 }
948
949 int size = m_font.GetPointSize();
950
951 long by = y + (long)floor( float(size) * 2.0 / 3.0 ); // approximate baseline
952 *m_pstream << XLOG2DEV(x) << " " << YLOG2DEV(by) << " moveto\n";
953
954 *m_pstream << "(";
955 int len = strlen ((char *)(const char *)text);
956 int i;
957 for (i = 0; i < len; i++)
958 {
959 int c = (unsigned char) text[i];
960 if ( c == ')' || c == '(' || c == '\\')
961 {
962 *m_pstream << "\\" << (char) c;
963 }
964 else if ( c >= 128 )
965 {
966 // Cope with character codes > 127
967 char tmp[5];
968 sprintf(tmp, "\\%o", c);
969 *m_pstream << tmp;
970 }
971 else
972 *m_pstream << (char) c;
973 }
974
975 *m_pstream << ")" << " show\n";
976
977 if (m_font.GetUnderlined())
978 {
979 long uy = (long)(y + size - m_underlinePosition);
980 long w, h;
981 GetTextExtent(text, &w, &h);
982
983 *m_pstream << "gsave " << XLOG2DEV(x) << " " << YLOG2DEV(uy)
984 << " moveto\n"
985 << (long)m_underlineThickness << " setlinewidth "
986 << XLOG2DEV(x + w) << " " << YLOG2DEV(uy)
987 << " lineto stroke grestore\n";
988 }
989
990 CalcBoundingBox( x, y );
991 CalcBoundingBox( x + size * text.Length() * 2/3 , y );
992 }
993
994
995 void wxPostScriptDC::SetBackground (const wxBrush& brush)
996 {
997 m_backgroundBrush = brush;
998 }
999
1000 void wxPostScriptDC::SetLogicalFunction (int WXUNUSED(function))
1001 {
1002 wxFAIL_MSG( "wxPostScriptDC::SetLogicalFunction not implemented." );
1003 }
1004
1005 void wxPostScriptDC::DrawSpline( wxList *points )
1006 {
1007 wxCHECK_RET( m_ok && m_pstream, "invalid postscript dc" );
1008
1009 SetPen( m_pen );
1010
1011 double a, b, c, d, x1, y1, x2, y2, x3, y3;
1012 wxPoint *p, *q;
1013
1014 wxNode *node = points->First();
1015 p = (wxPoint *)node->Data();
1016 x1 = p->x;
1017 y1 = p->y;
1018
1019 node = node->Next();
1020 p = (wxPoint *)node->Data();
1021 c = p->x;
1022 d = p->y;
1023 x3 = a = (double)(x1 + c) / 2;
1024 y3 = b = (double)(y1 + d) / 2;
1025
1026 *m_pstream << "newpath "
1027 << XLOG2DEV((long)x1) << " " << YLOG2DEV((long)y1) << " moveto "
1028 << XLOG2DEV((long)x3) << " " << YLOG2DEV((long)y3) << " lineto\n";
1029
1030 CalcBoundingBox( (long)x1, (long)y1 );
1031 CalcBoundingBox( (long)x3, (long)y3 );
1032
1033 while ((node = node->Next()) != NULL)
1034 {
1035 q = (wxPoint *)node->Data();
1036
1037 x1 = x3;
1038 y1 = y3;
1039 x2 = c;
1040 y2 = d;
1041 c = q->x;
1042 d = q->y;
1043 x3 = (double)(x2 + c) / 2;
1044 y3 = (double)(y2 + d) / 2;
1045 *m_pstream << XLOG2DEV((long)x1) << " " << YLOG2DEV((long)y1) << " "
1046 << XLOG2DEV((long)x2) << " " << YLOG2DEV((long)y2) << " "
1047 << XLOG2DEV((long)x3) << " " << YLOG2DEV((long)y3) << " DrawSplineSection\n";
1048
1049 CalcBoundingBox( (long)x1, (long)y1 );
1050 CalcBoundingBox( (long)x3, (long)y3 );
1051 }
1052
1053 /*
1054 At this point, (x2,y2) and (c,d) are the position of the
1055 next-to-last and last point respectively, in the point list
1056 */
1057
1058 *m_pstream << XLOG2DEV((long)c) << " " << YLOG2DEV((long)d) << " lineto stroke\n";
1059 }
1060
1061 long wxPostScriptDC::GetCharWidth ()
1062 {
1063 // Chris Breeze: reasonable approximation using wxMODERN/Courier
1064 return (long) (GetCharHeight() * 72.0 / 120.0);
1065 }
1066
1067
1068 void wxPostScriptDC::SetAxisOrientation( bool xLeftRight, bool yBottomUp )
1069 {
1070 wxCHECK_RET( m_ok && m_pstream, "invalid postscript dc" );
1071
1072 m_signX = (xLeftRight ? 1 : -1);
1073 m_signY = (yBottomUp ? 1 : -1);
1074
1075 ComputeScaleAndOrigin();
1076 }
1077
1078 void wxPostScriptDC::SetDeviceOrigin( long x, long y )
1079 {
1080 wxCHECK_RET( m_ok && m_pstream, "invalid postscript dc" );
1081
1082 int h = 0;
1083 int w = 0;
1084 GetSize( &w, &h );
1085
1086 wxDC::SetDeviceOrigin( x, h-y );
1087 }
1088
1089 void wxPostScriptDC::GetSize(int* width, int* height) const
1090 {
1091 const char *paperType = wxThePrintSetupData->GetPaperName();
1092
1093 if (!paperType) paperType = _("A4 210 x 297 mm");
1094
1095 wxPrintPaperType *paper = wxThePrintPaperDatabase->FindPaperType(paperType);
1096
1097 if (!paper) paper = wxThePrintPaperDatabase->FindPaperType(_("A4 210 x 297 mm"));
1098
1099 if (paper)
1100 {
1101 if (width) *width = paper->widthPixels;
1102 if (height) *height = paper->heightPixels;
1103 }
1104 else
1105 {
1106 if (width) *width = 595;
1107 if (height) *height = 842;
1108 }
1109 }
1110
1111 bool wxPostScriptDC::StartDoc (const wxString& message)
1112 {
1113 wxCHECK_MSG( m_ok, FALSE, "invalid postscript dc" );
1114
1115 if (m_filename == "")
1116 {
1117 m_filename = wxGetTempFileName("ps");
1118 wxThePrintSetupData->SetPrinterFile((char *)(const char *)m_filename);
1119 m_ok = TRUE;
1120 }
1121 else
1122 {
1123 wxThePrintSetupData->SetPrinterFile((char *)(const char *)m_filename);
1124 }
1125
1126 m_pstream = new ofstream (wxThePrintSetupData->GetPrinterFile());
1127
1128 if (!m_pstream || !m_pstream->good())
1129 {
1130 wxMessageBox (_("Cannot open file!"), _("Error"), wxOK);
1131 m_ok = FALSE;
1132 return FALSE;
1133 }
1134
1135 m_ok = TRUE;
1136
1137 SetBrush( *wxBLACK_BRUSH );
1138 SetPen( *wxBLACK_PEN );
1139 SetBackground( *wxWHITE_BRUSH );
1140 SetTextForeground( *wxBLACK );
1141
1142 // set origin according to paper size
1143 SetDeviceOrigin( 0,0 );
1144
1145 wxPageNumber = 1;
1146 m_pageNumber = 1;
1147 m_title = message;
1148 return TRUE;
1149 }
1150
1151 void wxPostScriptDC::EndDoc ()
1152 {
1153 wxCHECK_RET( m_ok && m_pstream, "invalid postscript dc" );
1154
1155 if (m_clipping)
1156 {
1157 m_clipping = FALSE;
1158 *m_pstream << "grestore\n";
1159 }
1160
1161 if (m_pstream)
1162 {
1163 delete m_pstream;
1164 m_pstream = (ofstream *) NULL;
1165 }
1166
1167 char *header_file = wxGetTempFileName("ps");
1168
1169 m_pstream = new ofstream( header_file );
1170
1171 *m_pstream << "%!PS-Adobe-2.0\n"; /* PostScript magic strings */
1172 *m_pstream << "%%Title: " << (const char *) m_title << "\n";
1173 *m_pstream << "%%Creator: " << wxTheApp->argv[0] << "\n";
1174 *m_pstream << "%%CreationDate: " << wxNow() << "\n";
1175
1176 char userID[256];
1177 if ( wxGetEmailAddress(userID, sizeof(userID)) )
1178 {
1179 *m_pstream << "%%For: " << (char *)userID;
1180 char userName[245];
1181 if (wxGetUserName(userName, sizeof(userName)))
1182 *m_pstream << " (" << (char *)userName << ")";
1183 *m_pstream << "\n";
1184 }
1185 else if ( wxGetUserName(userID, sizeof(userID)) )
1186 {
1187 *m_pstream << "%%For: " << (char *)userID << "\n";
1188 }
1189
1190 // THE FOLLOWING HAS BEEN CONTRIBUTED BY Andy Fyfe <andy@hyperparallel.com>
1191
1192 long wx_printer_translate_x, wx_printer_translate_y;
1193 double wx_printer_scale_x, wx_printer_scale_y;
1194 wxThePrintSetupData->GetPrinterTranslation(&wx_printer_translate_x, &wx_printer_translate_y);
1195 wxThePrintSetupData->GetPrinterScaling(&wx_printer_scale_x, &wx_printer_scale_y);
1196
1197 if (wxThePrintSetupData->GetPrinterOrientation() == PS_LANDSCAPE)
1198 {
1199 *m_pstream << "%%Orientation: Landscape\n";
1200 }
1201 else
1202 {
1203 *m_pstream << "%%Orientation: Portrait\n";
1204 }
1205
1206 // Compute the bounding box. Note that it is in the default user
1207 // coordinate system, thus we have to convert the values.
1208 long llx = (long) ((XLOG2DEV(m_minX)+wx_printer_translate_x)*wx_printer_scale_x);
1209 long lly = (long) ((YLOG2DEV(m_minY)+wx_printer_translate_y)*wx_printer_scale_y);
1210 long urx = (long) ((XLOG2DEV(m_maxX)+wx_printer_translate_x)*wx_printer_scale_x);
1211 long ury = (long) ((YLOG2DEV(m_maxY)+wx_printer_translate_y)*wx_printer_scale_y);
1212
1213 // If we're landscape, our sense of "x" and "y" is reversed.
1214 if (wxThePrintSetupData->GetPrinterOrientation() == PS_LANDSCAPE)
1215 {
1216 long tmp;
1217 tmp = llx; llx = lly; lly = tmp;
1218 tmp = urx; urx = ury; ury = tmp;
1219
1220 // We need either the two lines that follow, or we need to subtract
1221 // min_x from real_translate_y, which is commented out below.
1222 llx = llx - (long)(m_minX*wx_printer_scale_y);
1223 urx = urx - (long)(m_minX*wx_printer_scale_y);
1224 }
1225
1226 // The Adobe specifications call for integers; we round as to make
1227 // the bounding larger.
1228 *m_pstream << "%%BoundingBox: "
1229 << floor((double)llx) << " " << floor((double)lly) << " "
1230 << ceil((double)urx) << " " << ceil((double)ury) << "\n";
1231 *m_pstream << "%%Pages: " << (wxPageNumber - 1) << "\n";
1232 *m_pstream << "%%EndComments\n\n";
1233
1234 // To check the correctness of the bounding box, postscript commands
1235 // to draw a box corresponding to the bounding box are generated below.
1236 // But since we typically don't want to print such a box, the postscript
1237 // commands are generated within comments. These lines appear before any
1238 // adjustment of scale, rotation, or translation, and hence are in the
1239 // default user coordinates.
1240 *m_pstream << "% newpath\n";
1241 *m_pstream << "% " << llx << " " << lly << " moveto\n";
1242 *m_pstream << "% " << urx << " " << lly << " lineto\n";
1243 *m_pstream << "% " << urx << " " << ury << " lineto\n";
1244 *m_pstream << "% " << llx << " " << ury << " lineto closepath stroke\n";
1245
1246 *m_pstream << "%%BeginProlog\n";
1247 *m_pstream << wxPostScriptHeaderEllipse;
1248 *m_pstream << wxPostScriptHeaderEllipticArc;
1249 *m_pstream << wxPostScriptHeaderColourImage;
1250 *m_pstream << wxPostScriptHeaderReencodeISO1;
1251 *m_pstream << wxPostScriptHeaderReencodeISO2;
1252
1253 if (wxPostScriptHeaderSpline)
1254 *m_pstream << wxPostScriptHeaderSpline;
1255 *m_pstream << "%%EndProlog\n";
1256
1257 delete m_pstream;
1258 m_pstream = (ofstream *) NULL;
1259
1260 char *tmp_file = wxGetTempFileName("ps");
1261
1262 // Paste header Before wx_printer_file
1263 wxConcatFiles (header_file, wxThePrintSetupData->GetPrinterFile(), tmp_file);
1264 wxRemoveFile (header_file);
1265 wxRemoveFile (wxThePrintSetupData->GetPrinterFile());
1266 wxRenameFile(tmp_file, wxThePrintSetupData->GetPrinterFile());
1267
1268 #if defined(__X__) || defined(__WXGTK__)
1269 if (m_ok)
1270 {
1271 switch (wxThePrintSetupData->GetPrinterMode()) {
1272 case PS_PREVIEW:
1273 {
1274 char *argv[3];
1275 argv[0] = wxThePrintSetupData->GetPrintPreviewCommand();
1276 argv[1] = wxThePrintSetupData->GetPrinterFile();
1277 argv[2] = (char *) NULL;
1278 wxExecute (argv, TRUE);
1279 wxRemoveFile(wxThePrintSetupData->GetPrinterFile());
1280 }
1281 break;
1282
1283 case PS_PRINTER:
1284 {
1285 char *argv[4];
1286 int argc = 0;
1287 argv[argc++] = wxThePrintSetupData->GetPrinterCommand();
1288
1289 // !SM! If we simply assign to argv[1] here, if printer options
1290 // are blank, we get an annoying and confusing message from lpr.
1291 char * opts = wxThePrintSetupData->GetPrinterOptions();
1292 if (opts && *opts)
1293 argv[argc++] = opts;
1294
1295 argv[argc++] = wxThePrintSetupData->GetPrinterFile();
1296 argv[argc++] = (char *) NULL;
1297 wxExecute (argv, TRUE);
1298 wxRemoveFile(wxThePrintSetupData->GetPrinterFile());
1299 }
1300 break;
1301
1302 case PS_FILE:
1303 break;
1304 }
1305 }
1306 #endif
1307 }
1308
1309 void wxPostScriptDC::StartPage ()
1310 {
1311 wxCHECK_RET( m_ok && m_pstream, "invalid postscript dc" );
1312
1313 *m_pstream << "%%Page: " << (wxPageNumber++) << "\n";
1314
1315 // *m_pstream << "matrix currentmatrix\n";
1316
1317 // Added by Chris Breeze
1318
1319 // Each page starts with an "initgraphics" which resets the
1320 // transformation and so we need to reset the origin
1321 // (and rotate the page for landscape printing)
1322
1323 /*
1324 m_scaleFactor = 1.0;
1325 m_logicalOriginX = 0;
1326 m_logicalOriginY = 0;
1327 */
1328
1329 // Output scaling
1330 long translate_x, translate_y;
1331 double scale_x, scale_y;
1332 wxThePrintSetupData->GetPrinterTranslation(&translate_x, &translate_y);
1333 wxThePrintSetupData->GetPrinterScaling(&scale_x, &scale_y);
1334
1335 if (wxThePrintSetupData->GetPrinterOrientation() == PS_LANDSCAPE)
1336 {
1337 translate_y -= m_maxY;
1338 *m_pstream << "90 rotate\n";
1339 }
1340
1341 *m_pstream << scale_x << " " << scale_y << " scale\n";
1342 *m_pstream << translate_x << " " << translate_y << " translate\n";
1343 }
1344
1345 void wxPostScriptDC::EndPage ()
1346 {
1347 wxCHECK_RET( m_ok && m_pstream, "invalid postscript dc" );
1348
1349 *m_pstream << "showpage\n";
1350 }
1351
1352 bool wxPostScriptDC::Blit (long xdest, long ydest, long fwidth, long fheight,
1353 wxDC *source, long xsrc, long ysrc, int WXUNUSED(rop), bool WXUNUSED(useMask))
1354 {
1355 wxCHECK_MSG( m_ok && m_pstream, FALSE, "invalid postscript dc" );
1356
1357 return TRUE;
1358 }
1359
1360 long wxPostScriptDC::GetCharHeight ()
1361 {
1362 if (m_font.Ok())
1363 return m_font.GetPointSize();
1364 else
1365 return 12;
1366 }
1367
1368 void wxPostScriptDC::GetTextExtent (const wxString& string, long *x, long *y,
1369 long *descent, long *externalLeading, wxFont *theFont,
1370 bool WXUNUSED(use16))
1371 {
1372 // if (!m_pstream) return;
1373
1374 wxFont *fontToUse = theFont;
1375
1376 if (!fontToUse) fontToUse = (wxFont*) &m_font;
1377
1378 #if !USE_AFM_FOR_POSTSCRIPT
1379 // Provide a VERY rough estimate (avoid using it)
1380 // Chris Breeze 5/11/97: produces accurate results for mono-spaced
1381 // font such as Courier (aka wxMODERN)
1382 int height = 12;
1383 if (fontToUse)
1384 {
1385 height = fontToUse->GetPointSize();
1386 }
1387 *x = strlen (string) * height * 72 / 120;
1388 *y = (long) (height * 1.32); // allow for descender
1389
1390 if (descent)
1391 *descent = 0;
1392 if (externalLeading)
1393 *externalLeading = 0;
1394 #else
1395 // +++++ start of contributed code +++++
1396
1397 // ************************************************************
1398 // method for calculating string widths in postscript:
1399 // read in the AFM (adobe font metrics) file for the
1400 // actual font, parse it and extract the character widths
1401 // and also the descender. this may be improved, but for now
1402 // it works well. the AFM file is only read in if the
1403 // font is changed. this may be chached in the future.
1404 // calls to GetTextExtent with the font unchanged are rather
1405 // efficient!!!
1406 //
1407 // for each font and style used there is an AFM file necessary.
1408 // currently i have only files for the roman font family.
1409 // i try to get files for the other ones!
1410 //
1411 // CAVE: the size of the string is currently always calculated
1412 // in 'points' (1/72 of an inch). this should later on be
1413 // changed to depend on the mapping mode.
1414 // CAVE: the path to the AFM files must be set before calling this
1415 // function. this is usually done by a call like the following:
1416 // wxSetAFMPath("d:\\wxw161\\afm\\");
1417 //
1418 // example:
1419 //
1420 // wxPostScriptDC dc(NULL, TRUE);
1421 // if (dc.Ok()){
1422 // wxSetAFMPath("d:\\wxw161\\afm\\");
1423 // dc.StartDoc("Test");
1424 // dc.StartPage();
1425 // long w,h;
1426 // dc.SetFont(new wxFont(10, wxROMAN, wxNORMAL, wxNORMAL));
1427 // dc.GetTextExtent("Hallo",&w,&h);
1428 // dc.EndPage();
1429 // dc.EndDoc();
1430 // }
1431 //
1432 // by steve (stefan.hammes@urz.uni-heidelberg.de)
1433 // created: 10.09.94
1434 // updated: 14.05.95
1435
1436 assert(fontToUse && "void wxPostScriptDC::GetTextExtent: no font defined");
1437 assert(x && "void wxPostScriptDC::GetTextExtent: x == NULL");
1438 assert(y && "void wxPostScriptDC::GetTextExtent: y == NULL");
1439
1440 // these static vars are for storing the state between calls
1441 static int lastFamily= INT_MIN;
1442 static int lastSize= INT_MIN;
1443 static int lastStyle= INT_MIN;
1444 static int lastWeight= INT_MIN;
1445 static int lastDescender = INT_MIN;
1446 static int lastWidths[256]; // widths of the characters
1447
1448 // get actual parameters
1449 const int Family = fontToUse->GetFamily();
1450 const int Size = fontToUse->GetPointSize();
1451 const int Style = fontToUse->GetStyle();
1452 const int Weight = fontToUse->GetWeight();
1453
1454 // if we have another font, read the font-metrics
1455 if(Family!=lastFamily||Size!=lastSize||Style!=lastStyle||Weight!=lastWeight){
1456 // store actual values
1457 lastFamily = Family;
1458 lastSize = Size;
1459 lastStyle = Style;
1460 lastWeight = Weight;
1461
1462 // read in new font metrics **************************************
1463
1464 // 1. construct filename ******************************************
1465 /* MATTHEW: [2] Use wxTheFontNameDirectory */
1466 const char *name;
1467
1468 // Julian - we'll need to do this a different way now we've removed the
1469 // font directory system. Must find Stefan's original code.
1470
1471 name = wxTheFontNameDirectory->GetAFMName(Family, Weight, Style);
1472 if (!name)
1473 name = "unknown";
1474
1475 // get the directory of the AFM files
1476 char afmName[256];
1477 afmName[0] = 0;
1478 if (wxGetAFMPath())
1479 strcpy(afmName,wxGetAFMPath());
1480
1481 // 2. open and process the file **********************************
1482
1483 // a short explanation of the AFM format:
1484 // we have for each character a line, which gives its size
1485 // e.g.:
1486 //
1487 // C 63 ; WX 444 ; N question ; B 49 -14 395 676 ;
1488 //
1489 // that means, we have a character with ascii code 63, and width
1490 // (444/1000 * fontSize) points.
1491 // the other data is ignored for now!
1492 //
1493 // when the font has changed, we read in the right AFM file and store the
1494 // character widths in an array, which is processed below (see point 3.).
1495
1496 // new elements JC Sun Aug 25 23:21:44 MET DST 1996
1497
1498
1499 strcat(afmName,name);
1500 strcat(afmName,".afm");
1501 FILE *afmFile = fopen(afmName,"r");
1502 if(afmFile==NULL){
1503 wxLogDebug("GetTextExtent: can't open AFM file '%s'\n",afmName);
1504 wxLogDebug(" using approximate values\n");
1505 int i;
1506 for (i=0; i<256; i++) lastWidths[i] = 500; // an approximate value
1507 lastDescender = -150; // dito.
1508 }else{
1509 int i;
1510 // init the widths array
1511 for(i=0; i<256; i++) lastWidths[i]= INT_MIN;
1512 // some variables for holding parts of a line
1513 char cString[10],semiString[10],WXString[10],descString[20];
1514 char upString[30], utString[30], encString[50];
1515 char line[256];
1516 int ascii,cWidth;
1517 // read in the file and parse it
1518 while(fgets(line,sizeof(line),afmFile)!=NULL){
1519 // A.) check for descender definition
1520 if(strncmp(line,"Descender",9)==0){
1521 if((sscanf(line,"%s%d",descString,&lastDescender)!=2)
1522 || (strcmp(descString,"Descender")!=0)) {
1523 wxLogDebug("AFM-file '%s': line '%s' has error (bad descender)\n",
1524 afmName,line);
1525 }
1526 }
1527 // JC 1.) check for UnderlinePosition
1528 else if(strncmp(line,"UnderlinePosition",17)==0){
1529 if((sscanf(line,"%s%lf",upString,&UnderlinePosition)!=2)
1530 || (strcmp(upString,"UnderlinePosition")!=0)) {
1531 wxLogDebug("AFM-file '%s': line '%s' has error (bad UnderlinePosition)\n",
1532 afmName,line);
1533 }
1534 }
1535 // JC 2.) check for UnderlineThickness
1536 else if(strncmp(line,"UnderlineThickness",18)==0){
1537 if((sscanf(line,"%s%lf",utString,&UnderlineThickness)!=2)
1538 || (strcmp(utString,"UnderlineThickness")!=0)) {
1539 wxLogDebug("AFM-file '%s': line '%s' has error (bad UnderlineThickness)\n",
1540 afmName,line);
1541 }
1542 }
1543 // JC 3.) check for EncodingScheme
1544 else if(strncmp(line,"EncodingScheme",14)==0){
1545 if((sscanf(line,"%s%s",utString,encString)!=2)
1546 || (strcmp(utString,"EncodingScheme")!=0)) {
1547 wxLogDebug("AFM-file '%s': line '%s' has error (bad EncodingScheme)\n",
1548 afmName,line);
1549 }
1550 else if (strncmp(encString, "AdobeStandardEncoding", 21))
1551 {
1552 wxLogDebug("AFM-file '%s': line '%s' has error (unsupported EncodingScheme %s)\n",
1553 afmName,line, encString);
1554 }
1555 }
1556 // B.) check for char-width
1557 else if(strncmp(line,"C ",2)==0){
1558 if(sscanf(line,"%s%d%s%s%d",
1559 cString,&ascii,semiString,WXString,&cWidth)!=5){
1560 wxLogDebug("AFM-file '%s': line '%s' has an error (bad character width)\n",afmName,line);
1561 }
1562 if(strcmp(cString,"C")!=0 || strcmp(semiString,";")!=0 ||
1563 strcmp(WXString,"WX")!=0){
1564 wxLogDebug("AFM-file '%s': line '%s' has a format error\n",afmName,line);
1565 }
1566 //printf(" char '%c'=%d has width '%d'\n",ascii,ascii,cWidth);
1567 if(ascii>=0 && ascii<256){
1568 lastWidths[ascii] = cWidth; // store width
1569 }else{
1570 /* MATTHEW: this happens a lot; don't print an error */
1571 // wxLogDebug("AFM-file '%s': ASCII value %d out of range\n",afmName,ascii);
1572 }
1573 }
1574 // C.) ignore other entries.
1575 }
1576 fclose(afmFile);
1577 }
1578 // hack to compute correct values for german 'Umlaute'
1579 // the correct way would be to map the character names
1580 // like 'adieresis' to corresp. positions of ISOEnc and read
1581 // these values from AFM files, too. Maybe later ...
1582 lastWidths[196] = lastWidths['A']; // Ä
1583 lastWidths[228] = lastWidths['a']; // ä
1584 lastWidths[214] = lastWidths['O']; // Ö
1585 lastWidths[246] = lastWidths['o']; // ö
1586 lastWidths[220] = lastWidths['U']; // Ü
1587 lastWidths[252] = lastWidths['u']; // ü
1588 lastWidths[223] = lastWidths[251]; // ß
1589 }
1590
1591 // JC: calculate UnderlineThickness/UnderlinePosition
1592 m_underlinePosition = m_underlinePosition * fontToUse->GetPointSize() / 1000.0f;
1593 m_underlineThickness = m_underlineThickness * fontToUse->GetPointSize() / 1000.0f * m_scaleFactor;
1594
1595 // 3. now the font metrics are read in, calc size *******************
1596 // this is done by adding the widths of the characters in the
1597 // string. they are given in 1/1000 of the size!
1598
1599 long widthSum=0;
1600 long height=Size; // by default
1601 unsigned char *p;
1602 for(p=(unsigned char *)(const char *)string; *p; p++){
1603 if(lastWidths[*p]== INT_MIN){
1604 wxLogDebug("GetTextExtent: undefined width for character '%c' (%d)\n",
1605 *p,*p);
1606 widthSum += (long)(lastWidths[' ']/1000.0F * Size); // assume space
1607 }else{
1608 widthSum += (long)((lastWidths[*p]/1000.0F)*Size);
1609 }
1610 }
1611 // add descender to height (it is usually a negative value)
1612 if(lastDescender!=INT_MIN){
1613 height += (long)(((-lastDescender)/1000.0F) * Size); /* MATTHEW: forgot scale */
1614 }
1615
1616 // return size values
1617 *x = widthSum;
1618 *y = height;
1619
1620 // return other parameters
1621 if (descent){
1622 if(lastDescender!=INT_MIN){
1623 *descent = (long)(((-lastDescender)/1000.0F) * Size); /* MATTHEW: forgot scale */
1624 }else{
1625 *descent = 0;
1626 }
1627 }
1628
1629 // currently no idea how to calculate this!
1630 // if (externalLeading) *externalLeading = 0;
1631 if (externalLeading)
1632 *externalLeading = 0;
1633
1634 // ----- end of contributed code -----
1635 #endif
1636 }
1637
1638 void wxPostScriptDC::GetSizeMM(long *width, long *height) const
1639 {
1640 const char *paperType = wxThePrintSetupData->GetPaperName();
1641
1642 if (!paperType) paperType = _("A4 210 x 297 mm");
1643
1644 wxPrintPaperType *paper = wxThePrintPaperDatabase->FindPaperType(paperType);
1645
1646 if (!paper) paper = wxThePrintPaperDatabase->FindPaperType(_("A4 210 x 297 mm"));
1647
1648 if (paper)
1649 {
1650 if (width) *width = paper->widthMM;
1651 if (height) *height = paper->heightMM;
1652 }
1653 else
1654 {
1655 if (width) *width = 210;
1656 if (height) *height = 297;
1657 }
1658 }
1659