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