]> git.saurik.com Git - wxWidgets.git/blob - src/generic/dcpsg.cpp
A few tweaks and cleanups
[wxWidgets.git] / src / generic / dcpsg.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: dcpsg.cpp
3 // Purpose: Generic 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 licence
10 /////////////////////////////////////////////////////////////////////////////
11
12 #ifdef __GNUG__
13 #pragma implementation
14 #pragma interface
15 #endif
16
17 #include "wx/wxprec.h"
18
19 #ifdef __BORLANDC__
20 #pragma hdrstop
21 #endif
22
23 #ifndef WX_PRECOMP
24 #endif // WX_PRECOMP
25
26 #if wxUSE_PRINTING_ARCHITECTURE
27
28 #if wxUSE_POSTSCRIPT
29
30 #include "wx/window.h"
31 #include "wx/dcmemory.h"
32 #include "wx/utils.h"
33 #include "wx/intl.h"
34 #include "wx/filedlg.h"
35 #include "wx/app.h"
36 #include "wx/msgdlg.h"
37 #include "wx/image.h"
38 #include "wx/log.h"
39 #include "wx/generic/dcpsg.h"
40 #include "wx/printdlg.h"
41 #include "wx/button.h"
42 #include "wx/stattext.h"
43 #include "wx/radiobox.h"
44 #include "wx/textctrl.h"
45 #include "wx/prntbase.h"
46 #include "wx/paper.h"
47 #include "wx/filefn.h"
48
49 #include <math.h>
50
51 #ifdef __WXMSW__
52
53 #ifdef DrawText
54 #undef DrawText
55 #endif
56
57 #ifdef StartDoc
58 #undef StartDoc
59 #endif
60
61 #ifdef GetCharWidth
62 #undef GetCharWidth
63 #endif
64
65 #ifdef FindWindow
66 #undef FindWindow
67 #endif
68
69 #endif
70
71 //-----------------------------------------------------------------------------
72 // start and end of document/page
73 //-----------------------------------------------------------------------------
74
75 static const char *wxPostScriptHeaderEllipse = "\
76 /ellipsedict 8 dict def\n\
77 ellipsedict /mtrx matrix put\n\
78 /ellipse {\n\
79 ellipsedict begin\n\
80 /endangle exch def\n\
81 /startangle exch def\n\
82 /yrad exch def\n\
83 /xrad exch def\n\
84 /y exch def\n\
85 /x exch def\n\
86 /savematrix mtrx currentmatrix def\n\
87 x y translate\n\
88 xrad yrad scale\n\
89 0 0 1 startangle endangle arc\n\
90 savematrix setmatrix\n\
91 end\n\
92 } def\n\
93 ";
94
95 static const char *wxPostScriptHeaderEllipticArc= "\
96 /ellipticarcdict 8 dict def\n\
97 ellipticarcdict /mtrx matrix put\n\
98 /ellipticarc\n\
99 { ellipticarcdict begin\n\
100 /do_fill exch def\n\
101 /endangle exch def\n\
102 /startangle exch def\n\
103 /yrad exch def\n\
104 /xrad exch def \n\
105 /y exch def\n\
106 /x exch def\n\
107 /savematrix mtrx currentmatrix def\n\
108 x y translate\n\
109 xrad yrad scale\n\
110 do_fill { 0 0 moveto } if\n\
111 0 0 1 startangle endangle arc\n\
112 savematrix setmatrix\n\
113 do_fill { fill }{ stroke } ifelse\n\
114 end\n\
115 } def\n";
116
117 static const char *wxPostScriptHeaderSpline = "\
118 /DrawSplineSection {\n\
119 /y3 exch def\n\
120 /x3 exch def\n\
121 /y2 exch def\n\
122 /x2 exch def\n\
123 /y1 exch def\n\
124 /x1 exch def\n\
125 /xa x1 x2 x1 sub 0.666667 mul add def\n\
126 /ya y1 y2 y1 sub 0.666667 mul add def\n\
127 /xb x3 x2 x3 sub 0.666667 mul add def\n\
128 /yb y3 y2 y3 sub 0.666667 mul add def\n\
129 x1 y1 lineto\n\
130 xa ya xb yb x3 y3 curveto\n\
131 } def\n\
132 ";
133
134 static const char *wxPostScriptHeaderColourImage = "\
135 %% define 'colorimage' if it isn't defined\n\
136 %% ('colortogray' and 'mergeprocs' come from xwd2ps\n\
137 %% via xgrab)\n\
138 /colorimage where %% do we know about 'colorimage'?\n\
139 { pop } %% yes: pop off the 'dict' returned\n\
140 { %% no: define one\n\
141 /colortogray { %% define an RGB->I function\n\
142 /rgbdata exch store %% call input 'rgbdata'\n\
143 rgbdata length 3 idiv\n\
144 /npixls exch store\n\
145 /rgbindx 0 store\n\
146 0 1 npixls 1 sub {\n\
147 grays exch\n\
148 rgbdata rgbindx get 20 mul %% Red\n\
149 rgbdata rgbindx 1 add get 32 mul %% Green\n\
150 rgbdata rgbindx 2 add get 12 mul %% Blue\n\
151 add add 64 idiv %% I = .5G + .31R + .18B\n\
152 put\n\
153 /rgbindx rgbindx 3 add store\n\
154 } for\n\
155 grays 0 npixls getinterval\n\
156 } bind def\n\
157 \n\
158 %% Utility procedure for colorimage operator.\n\
159 %% This procedure takes two procedures off the\n\
160 %% stack and merges them into a single procedure.\n\
161 \n\
162 /mergeprocs { %% def\n\
163 dup length\n\
164 3 -1 roll\n\
165 dup\n\
166 length\n\
167 dup\n\
168 5 1 roll\n\
169 3 -1 roll\n\
170 add\n\
171 array cvx\n\
172 dup\n\
173 3 -1 roll\n\
174 0 exch\n\
175 putinterval\n\
176 dup\n\
177 4 2 roll\n\
178 putinterval\n\
179 } bind def\n\
180 \n\
181 /colorimage { %% def\n\
182 pop pop %% remove 'false 3' operands\n\
183 {colortogray} mergeprocs\n\
184 image\n\
185 } bind def\n\
186 } ifelse %% end of 'false' case\n\
187 ";
188
189 static char wxPostScriptHeaderReencodeISO1[] =
190 "\n/reencodeISO {\n"
191 "dup dup findfont dup length dict begin\n"
192 "{ 1 index /FID ne { def }{ pop pop } ifelse } forall\n"
193 "/Encoding ISOLatin1Encoding def\n"
194 "currentdict end definefont\n"
195 "} def\n"
196 "/ISOLatin1Encoding [\n"
197 "/.notdef/.notdef/.notdef/.notdef/.notdef/.notdef/.notdef/.notdef\n"
198 "/.notdef/.notdef/.notdef/.notdef/.notdef/.notdef/.notdef/.notdef\n"
199 "/.notdef/.notdef/.notdef/.notdef/.notdef/.notdef/.notdef/.notdef\n"
200 "/.notdef/.notdef/.notdef/.notdef/.notdef/.notdef/.notdef/.notdef\n"
201 "/space/exclam/quotedbl/numbersign/dollar/percent/ampersand/quoteright\n"
202 "/parenleft/parenright/asterisk/plus/comma/minus/period/slash\n"
203 "/zero/one/two/three/four/five/six/seven/eight/nine/colon/semicolon\n"
204 "/less/equal/greater/question/at/A/B/C/D/E/F/G/H/I/J/K/L/M/N\n"
205 "/O/P/Q/R/S/T/U/V/W/X/Y/Z/bracketleft/backslash/bracketright\n"
206 "/asciicircum/underscore/quoteleft/a/b/c/d/e/f/g/h/i/j/k/l/m\n"
207 "/n/o/p/q/r/s/t/u/v/w/x/y/z/braceleft/bar/braceright/asciitilde\n"
208 "/.notdef/.notdef/.notdef/.notdef/.notdef/.notdef/.notdef/.notdef\n"
209 "/.notdef/.notdef/.notdef/.notdef/.notdef/.notdef/.notdef/.notdef\n"
210 "/.notdef/dotlessi/grave/acute/circumflex/tilde/macron/breve\n"
211 "/dotaccent/dieresis/.notdef/ring/cedilla/.notdef/hungarumlaut\n";
212
213 static char wxPostScriptHeaderReencodeISO2[] =
214 "/ogonek/caron/space/exclamdown/cent/sterling/currency/yen/brokenbar\n"
215 "/section/dieresis/copyright/ordfeminine/guillemotleft/logicalnot\n"
216 "/hyphen/registered/macron/degree/plusminus/twosuperior/threesuperior\n"
217 "/acute/mu/paragraph/periodcentered/cedilla/onesuperior/ordmasculine\n"
218 "/guillemotright/onequarter/onehalf/threequarters/questiondown\n"
219 "/Agrave/Aacute/Acircumflex/Atilde/Adieresis/Aring/AE/Ccedilla\n"
220 "/Egrave/Eacute/Ecircumflex/Edieresis/Igrave/Iacute/Icircumflex\n"
221 "/Idieresis/Eth/Ntilde/Ograve/Oacute/Ocircumflex/Otilde/Odieresis\n"
222 "/multiply/Oslash/Ugrave/Uacute/Ucircumflex/Udieresis/Yacute\n"
223 "/Thorn/germandbls/agrave/aacute/acircumflex/atilde/adieresis\n"
224 "/aring/ae/ccedilla/egrave/eacute/ecircumflex/edieresis/igrave\n"
225 "/iacute/icircumflex/idieresis/eth/ntilde/ograve/oacute/ocircumflex\n"
226 "/otilde/odieresis/divide/oslash/ugrave/uacute/ucircumflex/udieresis\n"
227 "/yacute/thorn/ydieresis\n"
228 "] def\n\n";
229
230 //-------------------------------------------------------------------------------
231 // wxPostScriptDC
232 //-------------------------------------------------------------------------------
233
234 float wxPostScriptDC::ms_PSScaleFactor = 10.0;
235
236 void wxPostScriptDC::SetResolution(int ppi)
237 {
238 ms_PSScaleFactor = (float)ppi / 72.0;
239 }
240
241 int wxPostScriptDC::GetResolution()
242 {
243 return (int)(ms_PSScaleFactor * 72.0);
244 }
245
246
247
248 wxPostScriptDC::wxPostScriptDC ()
249 {
250 m_pstream = (FILE*) NULL;
251
252 m_currentRed = 0;
253 m_currentGreen = 0;
254 m_currentBlue = 0;
255
256 m_pageNumber = 0;
257
258 m_clipping = FALSE;
259
260 m_underlinePosition = 0.0;
261 m_underlineThickness = 0.0;
262
263 m_signX = 1; // default x-axis left to right
264 m_signY = -1; // default y-axis bottom up -> top down
265
266 // Compatibility only
267 // HH: Doesn't seem to work for wxMSW...
268 #ifndef __WXMSW__
269 m_printData = * wxThePrintSetupData;
270 #endif
271 }
272
273 wxPostScriptDC::wxPostScriptDC (const wxString& file, bool interactive, wxWindow *parent)
274 {
275 m_pstream = (FILE*) NULL;
276
277 m_currentRed = 0;
278 m_currentGreen = 0;
279 m_currentBlue = 0;
280
281 m_pageNumber = 0;
282
283 m_clipping = FALSE;
284
285 m_underlinePosition = 0.0;
286 m_underlineThickness = 0.0;
287
288 m_signX = 1; // default x-axis left to right
289 m_signY = -1; // default y-axis bottom up -> top down
290
291 Create(file, interactive, parent);
292 }
293
294 bool wxPostScriptDC::Create(const wxString& file, bool interactive, wxWindow *parent)
295 {
296 m_isInteractive = interactive;
297
298 m_title = "";
299 m_printData.SetFilename(file);
300
301 #ifdef __WXMSW__
302 // Can only send to file in Windows
303 m_printData.SetPrintMode(wxPRINT_MODE_FILE);
304 #endif
305
306 if (m_isInteractive)
307 {
308 if ((m_ok = PrinterDialog (parent) ) == FALSE) return FALSE;
309 }
310 else
311 {
312 m_ok = TRUE;
313 }
314
315 return m_ok;
316 }
317
318 wxPostScriptDC::wxPostScriptDC (const wxPrintData& printData)
319 {
320 m_pstream = (FILE*) NULL;
321
322 m_currentRed = 0;
323 m_currentGreen = 0;
324 m_currentBlue = 0;
325
326 m_pageNumber = 0;
327
328 m_clipping = FALSE;
329
330 m_underlinePosition = 0.0;
331 m_underlineThickness = 0.0;
332
333 m_signX = 1; // default x-axis left to right
334 m_signY = -1; // default y-axis bottom up -> top down
335
336 m_printData = printData;
337
338 m_ok = TRUE;
339 }
340
341 wxPostScriptDC::~wxPostScriptDC ()
342 {
343 if (m_pstream)
344 {
345 fclose( m_pstream );
346 m_pstream = (FILE*) NULL;
347 }
348 }
349
350 bool wxPostScriptDC::Ok() const
351 {
352 return m_ok;
353 }
354
355 // This dialog is deprecated now: use wxGenericPrintDialog or the printing framework
356 bool wxPostScriptDC::PrinterDialog(wxWindow *parent)
357 {
358 wxPostScriptPrintDialog dialog( parent, _("Printer Settings"), wxPoint(150, 150), wxSize(400, 400),
359 wxDEFAULT_DIALOG_STYLE | wxDIALOG_MODAL );
360 m_ok = (dialog.ShowModal () == wxID_OK);
361
362 if (!m_ok) return FALSE;
363
364 if ((m_printData.GetFilename() == "") &&
365 (m_printData.GetPrintMode() == wxPRINT_MODE_PREVIEW ||
366 m_printData.GetPrintMode() == wxPRINT_MODE_PRINTER))
367 {
368 // steve, 05.09.94
369 #ifdef __VMS__
370 m_printData.SetFilename("preview");
371 #else
372 // For PS_PRINTER action this depends on a Unix-style print spooler
373 // since the wx_printer_file can be destroyed during a session
374 // @@@ TODO: a Windows-style answer for non-Unix
375 wxChar userId[256];
376 wxGetUserId (userId, sizeof (userId) / sizeof (char));
377 wxChar tmp[256];
378 wxStrcpy (tmp, wxT("/tmp/preview_"));
379 wxStrcat (tmp, userId);
380 m_printData.SetFilename(tmp);
381 #endif
382 wxChar tmp2[256];
383 wxStrcpy(tmp2, m_printData.GetFilename());
384 wxStrcat (tmp2, wxT(".ps"));
385 m_printData.SetFilename(tmp2);
386 }
387 else if ((m_printData.GetFilename() == wxT("")) && (m_printData.GetPrintMode() == wxPRINT_MODE_FILE))
388 {
389 wxString file = wxSaveFileSelector (_("PostScript"), wxT("ps"));
390 if ( file.IsEmpty() )
391 {
392 m_ok = FALSE;
393 return FALSE;
394 }
395
396 m_printData.SetFilename(file);
397 m_ok = TRUE;
398 }
399
400 return m_ok;
401 }
402
403 void wxPostScriptDC::DoSetClippingRegion (wxCoord x, wxCoord y, wxCoord w, wxCoord h)
404 {
405 wxCHECK_RET( m_ok && m_pstream, wxT("invalid postscript dc") );
406
407 if (m_clipping) DestroyClippingRegion();
408
409 wxDC::DoSetClippingRegion(x, y, w, h);
410
411 m_clipping = TRUE;
412 fprintf( m_pstream,
413 "gsave\n newpath\n"
414 "%d %d moveto\n"
415 "%d %d lineto\n"
416 "%d %d lineto\n"
417 "%d %d lineto\n"
418 "closepath clip newpath\n",
419 LogicalToDeviceX(x), LogicalToDeviceY(y),
420 LogicalToDeviceX(x+w), LogicalToDeviceY(y),
421 LogicalToDeviceX(x+w), LogicalToDeviceY(y+h),
422 LogicalToDeviceX(x), LogicalToDeviceY(y+h) );
423 }
424
425
426 void wxPostScriptDC::DestroyClippingRegion()
427 {
428 wxCHECK_RET( m_ok && m_pstream, wxT("invalid postscript dc") );
429
430 if (m_clipping)
431 {
432 m_clipping = FALSE;
433 fprintf( m_pstream, "grestore\n" );
434 }
435
436 wxDC::DestroyClippingRegion();
437 }
438
439 void wxPostScriptDC::Clear()
440 {
441 wxFAIL_MSG( wxT("wxPostScriptDC::Clear not implemented.") );
442 }
443
444 bool wxPostScriptDC::DoFloodFill (wxCoord WXUNUSED(x), wxCoord WXUNUSED(y), const wxColour &WXUNUSED(col), int WXUNUSED(style))
445 {
446 wxFAIL_MSG( wxT("wxPostScriptDC::FloodFill not implemented.") );
447 return FALSE;
448 }
449
450 bool wxPostScriptDC::DoGetPixel (wxCoord WXUNUSED(x), wxCoord WXUNUSED(y), wxColour * WXUNUSED(col)) const
451 {
452 wxFAIL_MSG( wxT("wxPostScriptDC::GetPixel not implemented.") );
453 return FALSE;
454 }
455
456 void wxPostScriptDC::DoCrossHair (wxCoord WXUNUSED(x), wxCoord WXUNUSED(y))
457 {
458 wxFAIL_MSG( wxT("wxPostScriptDC::CrossHair not implemented.") );
459 }
460
461 void wxPostScriptDC::DoDrawLine (wxCoord x1, wxCoord y1, wxCoord x2, wxCoord y2)
462 {
463 wxCHECK_RET( m_ok && m_pstream, wxT("invalid postscript dc") );
464
465 if (m_pen.GetStyle() == wxTRANSPARENT) return;
466
467 SetPen( m_pen );
468
469 fprintf( m_pstream,
470 "newpath\n"
471 "%d %d moveto\n"
472 "%d %d lineto\n"
473 "stroke\n",
474 LogicalToDeviceX(x1), LogicalToDeviceY(y1),
475 LogicalToDeviceX(x2), LogicalToDeviceY (y2) );
476
477 CalcBoundingBox( x1, y1 );
478 CalcBoundingBox( x2, y2 );
479 }
480
481 #define RAD2DEG 57.29577951308
482
483 void wxPostScriptDC::DoDrawArc (wxCoord x1, wxCoord y1, wxCoord x2, wxCoord y2, wxCoord xc, wxCoord yc)
484 {
485 wxCHECK_RET( m_ok && m_pstream, wxT("invalid postscript dc") );
486
487 wxCoord dx = x1 - xc;
488 wxCoord dy = y1 - yc;
489 wxCoord radius = (wxCoord) sqrt( (double)(dx*dx+dy*dy) );
490 double alpha1, alpha2;
491
492 if (x1 == x2 && y1 == y2)
493 {
494 alpha1 = 0.0;
495 alpha2 = 360.0;
496 }
497 else if (radius == 0.0)
498 {
499 alpha1 = alpha2 = 0.0;
500 }
501 else
502 {
503 alpha1 = (x1 - xc == 0) ?
504 (y1 - yc < 0) ? 90.0 : -90.0 :
505 -atan2(double(y1-yc), double(x1-xc)) * RAD2DEG;
506 alpha2 = (x2 - xc == 0) ?
507 (y2 - yc < 0) ? 90.0 : -90.0 :
508 -atan2(double(y2-yc), double(x2-xc)) * RAD2DEG;
509 }
510 while (alpha1 <= 0) alpha1 += 360;
511 while (alpha2 <= 0) alpha2 += 360; // adjust angles to be between
512 while (alpha1 > 360) alpha1 -= 360; // 0 and 360 degree
513 while (alpha2 > 360) alpha2 -= 360;
514
515 if (m_brush.GetStyle() != wxTRANSPARENT)
516 {
517 SetBrush( m_brush );
518
519 fprintf( m_pstream,
520 "newpath\n"
521 "%d %d %d %d %d %d ellipse\n"
522 "%d %d lineto\n"
523 "closepath\n"
524 "fill\n",
525 LogicalToDeviceX(xc), LogicalToDeviceY(yc), LogicalToDeviceXRel(radius), LogicalToDeviceYRel(radius), (wxCoord)alpha1, (wxCoord) alpha2,
526 LogicalToDeviceX(xc), LogicalToDeviceY(yc) );
527
528 CalcBoundingBox( xc-radius, yc-radius );
529 CalcBoundingBox( xc+radius, yc+radius );
530 }
531
532 if (m_pen.GetStyle() != wxTRANSPARENT)
533 {
534 SetPen( m_pen );
535
536 fprintf( m_pstream,
537 "newpath\n"
538 "%d %d %d %d %d %d ellipse\n"
539 "%d %d lineto\n"
540 "stroke\n"
541 "fill\n",
542 LogicalToDeviceX(xc), LogicalToDeviceY(yc), LogicalToDeviceXRel(radius), LogicalToDeviceYRel(radius), (wxCoord)alpha1, (wxCoord) alpha2,
543 LogicalToDeviceX(xc), LogicalToDeviceY(yc) );
544
545 CalcBoundingBox( xc-radius, yc-radius );
546 CalcBoundingBox( xc+radius, yc+radius );
547 }
548 }
549
550 void wxPostScriptDC::DoDrawEllipticArc(wxCoord x,wxCoord y,wxCoord w,wxCoord h,double sa,double ea)
551 {
552 wxCHECK_RET( m_ok && m_pstream, wxT("invalid postscript dc") );
553
554 if (sa>=360 || sa<=-360) sa=sa-int(sa/360)*360;
555 if (ea>=360 || ea<=-360) ea=ea-int(ea/360)*360;
556 if (sa<0) sa+=360;
557 if (ea<0) ea+=360;
558
559 if (sa==ea)
560 {
561 DrawEllipse(x,y,w,h);
562 return;
563 }
564
565 if (m_brush.GetStyle () != wxTRANSPARENT)
566 {
567 SetBrush( m_brush );
568
569 fprintf( m_pstream,
570 "newpath\n"
571 "%d %d %d %d %d %d true ellipticarc\n",
572 LogicalToDeviceX(x+w/2), LogicalToDeviceY(y+h/2), LogicalToDeviceXRel(w/2), LogicalToDeviceYRel(h/2), (wxCoord)sa, (wxCoord)ea );
573
574 CalcBoundingBox( x ,y );
575 CalcBoundingBox( x+w, y+h );
576 }
577
578 if (m_pen.GetStyle () != wxTRANSPARENT)
579 {
580 SetPen( m_pen );
581
582 fprintf(m_pstream,
583 "newpath\n"
584 "%d %d %d %d %d %d false ellipticarc\n",
585 LogicalToDeviceX(x+w/2), LogicalToDeviceY(y+h/2), LogicalToDeviceXRel(w/2), LogicalToDeviceYRel(h/2), (wxCoord)sa, (wxCoord)ea );
586
587 CalcBoundingBox( x ,y );
588 CalcBoundingBox( x+w, y+h );
589 }
590 }
591
592 void wxPostScriptDC::DoDrawPoint (wxCoord x, wxCoord y)
593 {
594 wxCHECK_RET( m_ok && m_pstream, wxT("invalid postscript dc") );
595
596 if (m_pen.GetStyle() == wxTRANSPARENT) return;
597
598 SetPen (m_pen);
599
600 fprintf( m_pstream,
601 "newpath\n"
602 "%d %d moveto\n"
603 "%d %d lineto\n"
604 "stroke\n",
605 LogicalToDeviceX(x), LogicalToDeviceY(y),
606 LogicalToDeviceX(x+1), LogicalToDeviceY(y) );
607
608 CalcBoundingBox( x, y );
609 }
610
611 void wxPostScriptDC::DoDrawPolygon (int n, wxPoint points[], wxCoord xoffset, wxCoord yoffset, int WXUNUSED(fillStyle))
612 {
613 wxCHECK_RET( m_ok && m_pstream, wxT("invalid postscript dc") );
614
615 if (n <= 0) return;
616
617 if (m_brush.GetStyle () != wxTRANSPARENT)
618 {
619 SetBrush( m_brush );
620
621 fprintf( m_pstream, "newpath\n" );
622
623 wxCoord xx = LogicalToDeviceX(points[0].x + xoffset);
624 wxCoord yy = LogicalToDeviceY(points[0].y + yoffset);
625
626 fprintf( m_pstream, "%d %d moveto\n", xx, yy );
627
628 CalcBoundingBox( points[0].x + xoffset, points[0].y + yoffset );
629
630 for (int i = 1; i < n; i++)
631 {
632 xx = LogicalToDeviceX(points[i].x + xoffset);
633 yy = LogicalToDeviceY(points[i].y + yoffset);
634
635 fprintf( m_pstream, "%d %d lineto\n", xx, yy );
636
637 CalcBoundingBox( points[i].x + xoffset, points[i].y + yoffset);
638 }
639
640 fprintf( m_pstream, "fill\n" );
641 }
642
643 if (m_pen.GetStyle () != wxTRANSPARENT)
644 {
645 SetPen( m_pen );
646
647 fprintf( m_pstream, "newpath\n" );
648
649 wxCoord xx = LogicalToDeviceX(points[0].x + xoffset);
650 wxCoord yy = LogicalToDeviceY(points[0].y + yoffset);
651
652 fprintf( m_pstream, "%d %d moveto\n", xx, yy );
653
654 CalcBoundingBox( points[0].x + xoffset, points[0].y + yoffset );
655
656 for (int i = 1; i < n; i++)
657 {
658 xx = LogicalToDeviceX(points[i].x + xoffset);
659 yy = LogicalToDeviceY(points[i].y + yoffset);
660
661 fprintf( m_pstream, "%d %d lineto\n", xx, yy );
662
663 CalcBoundingBox( points[i].x + xoffset, points[i].y + yoffset);
664 }
665
666 fprintf( m_pstream, "closepath\n" );
667 fprintf( m_pstream, "stroke\n" );
668 }
669 }
670
671 void wxPostScriptDC::DoDrawLines (int n, wxPoint points[], wxCoord xoffset, wxCoord yoffset)
672 {
673 wxCHECK_RET( m_ok && m_pstream, wxT("invalid postscript dc") );
674
675 if (m_pen.GetStyle() == wxTRANSPARENT) return;
676
677 if (n <= 0) return;
678
679 SetPen (m_pen);
680
681 int i;
682 for ( i =0; i<n ; i++ )
683 {
684 CalcBoundingBox( LogicalToDeviceX(points[i].x+xoffset), LogicalToDeviceY(points[i].y+yoffset));
685 }
686
687 fprintf( m_pstream,
688 "newpath\n"
689 "%d %d moveto\n",
690 LogicalToDeviceX(points[0].x+xoffset), LogicalToDeviceY(points[0].y+yoffset) );
691
692 for (i = 1; i < n; i++)
693 {
694 fprintf( m_pstream,
695 "%d %d lineto\n",
696 LogicalToDeviceX(points[i].x+xoffset), LogicalToDeviceY(points[i].y+yoffset) );
697 }
698
699 fprintf( m_pstream, "stroke\n" );
700 }
701
702 void wxPostScriptDC::DoDrawRectangle (wxCoord x, wxCoord y, wxCoord width, wxCoord height)
703 {
704 wxCHECK_RET( m_ok && m_pstream, wxT("invalid postscript dc") );
705
706 if (m_brush.GetStyle () != wxTRANSPARENT)
707 {
708 SetBrush( m_brush );
709
710 fprintf( m_pstream,
711 "newpath\n"
712 "%d %d moveto\n"
713 "%d %d lineto\n"
714 "%d %d lineto\n"
715 "%d %d lineto\n"
716 "closepath\n"
717 "fill\n",
718 LogicalToDeviceX(x), LogicalToDeviceY(y),
719 LogicalToDeviceX(x + width), LogicalToDeviceY(y),
720 LogicalToDeviceX(x + width), LogicalToDeviceY(y + height),
721 LogicalToDeviceX(x), LogicalToDeviceY(y + height) );
722
723 CalcBoundingBox( x, y );
724 CalcBoundingBox( x + width, y + height );
725 }
726
727 if (m_pen.GetStyle () != wxTRANSPARENT)
728 {
729 SetPen (m_pen);
730
731 fprintf( m_pstream,
732 "newpath\n"
733 "%d %d moveto\n"
734 "%d %d lineto\n"
735 "%d %d lineto\n"
736 "%d %d lineto\n"
737 "closepath\n"
738 "stroke\n",
739 LogicalToDeviceX(x), LogicalToDeviceY(y),
740 LogicalToDeviceX(x + width), LogicalToDeviceY(y),
741 LogicalToDeviceX(x + width), LogicalToDeviceY(y + height),
742 LogicalToDeviceX(x), LogicalToDeviceY(y + height) );
743
744 CalcBoundingBox( x, y );
745 CalcBoundingBox( x + width, y + height );
746 }
747 }
748
749 void wxPostScriptDC::DoDrawRoundedRectangle (wxCoord x, wxCoord y, wxCoord width, wxCoord height, double radius)
750 {
751 wxCHECK_RET( m_ok && m_pstream, wxT("invalid postscript dc") );
752
753 if (radius < 0.0)
754 {
755 // Now, a negative radius is interpreted to mean
756 // 'the proportion of the smallest X or Y dimension'
757 double smallest = 0.0;
758 if (width < height)
759 smallest = width;
760 else
761 smallest = height;
762 radius = (-radius * smallest);
763 }
764
765 wxCoord rad = (wxCoord) radius;
766
767 if (m_brush.GetStyle () != wxTRANSPARENT)
768 {
769 SetBrush( m_brush );
770
771 /* Draw rectangle anticlockwise */
772 fprintf( m_pstream,
773 "newpath\n"
774 "%d %d %d 90 180 arc\n"
775 "%d %d moveto\n"
776 "%d %d %d 180 270 arc\n"
777 "%d %d lineto\n"
778 "%d %d %d 270 0 arc\n"
779 "%d %d lineto\n"
780 "%d %d %d 0 90 arc\n"
781 "%d %d lineto\n"
782 "closepath\n"
783 "fill\n",
784 LogicalToDeviceX(x + rad), LogicalToDeviceY(y + rad), LogicalToDeviceXRel(rad),
785 LogicalToDeviceX(x), LogicalToDeviceY(y + rad),
786 LogicalToDeviceX(x + rad), LogicalToDeviceY(y + height - rad), LogicalToDeviceXRel(rad),
787 LogicalToDeviceX(x + width - rad), LogicalToDeviceY(y + height),
788 LogicalToDeviceX(x + width - rad), LogicalToDeviceY(y + height - rad), LogicalToDeviceXRel(rad),
789 LogicalToDeviceX(x + width), LogicalToDeviceY(y + rad),
790 LogicalToDeviceX(x + width - rad), LogicalToDeviceY(y + rad), LogicalToDeviceXRel(rad),
791 LogicalToDeviceX(x + rad), LogicalToDeviceY(y) );
792
793 CalcBoundingBox( x, y );
794 CalcBoundingBox( x + width, y + height );
795 }
796
797 if (m_pen.GetStyle () != wxTRANSPARENT)
798 {
799 SetPen (m_pen);
800
801 /* Draw rectangle anticlockwise */
802 fprintf( m_pstream,
803 "newpath\n"
804 "%d %d %d 90 180 arc\n"
805 "%d %d moveto\n"
806 "%d %d %d 180 270 arc\n"
807 "%d %d lineto\n"
808 "%d %d %d 270 0 arc\n"
809 "%d %d lineto\n"
810 "%d %d %d 0 90 arc\n"
811 "%d %d lineto\n"
812 "closepath\n"
813 "stroke\n",
814 LogicalToDeviceX(x + rad), LogicalToDeviceY(y + rad), LogicalToDeviceXRel(rad),
815 LogicalToDeviceX(x), LogicalToDeviceY(y + rad),
816 LogicalToDeviceX(x + rad), LogicalToDeviceY(y + height - rad), LogicalToDeviceXRel(rad),
817 LogicalToDeviceX(x + width - rad), LogicalToDeviceY(y + height),
818 LogicalToDeviceX(x + width - rad), LogicalToDeviceY(y + height - rad), LogicalToDeviceXRel(rad),
819 LogicalToDeviceX(x + width), LogicalToDeviceY(y + rad),
820 LogicalToDeviceX(x + width - rad), LogicalToDeviceY(y + rad), LogicalToDeviceXRel(rad),
821 LogicalToDeviceX(x + rad), LogicalToDeviceY(y) );
822
823 CalcBoundingBox( x, y );
824 CalcBoundingBox( x + width, y + height );
825 }
826 }
827
828 void wxPostScriptDC::DoDrawEllipse (wxCoord x, wxCoord y, wxCoord width, wxCoord height)
829 {
830 wxCHECK_RET( m_ok && m_pstream, wxT("invalid postscript dc") );
831
832 if (m_brush.GetStyle () != wxTRANSPARENT)
833 {
834 SetBrush (m_brush);
835
836 fprintf( m_pstream,
837 "newpath\n"
838 "%d %d %d %d 0 360 ellipse\n"
839 "fill\n",
840 LogicalToDeviceX(x + width / 2), LogicalToDeviceY(y + height / 2),
841 LogicalToDeviceXRel(width / 2), LogicalToDeviceYRel(height / 2) );
842
843 CalcBoundingBox( x - width, y - height );
844 CalcBoundingBox( x + width, y + height );
845 }
846
847 if (m_pen.GetStyle () != wxTRANSPARENT)
848 {
849 SetPen (m_pen);
850
851 fprintf( m_pstream,
852 "newpath\n"
853 "%d %d %d %d 0 360 ellipse\n"
854 "stroke\n",
855 LogicalToDeviceX(x + width / 2), LogicalToDeviceY(y + height / 2),
856 LogicalToDeviceXRel(width / 2), LogicalToDeviceYRel(height / 2) );
857
858 CalcBoundingBox( x - width, y - height );
859 CalcBoundingBox( x + width, y + height );
860 }
861 }
862
863 void wxPostScriptDC::DoDrawIcon( const wxIcon& icon, wxCoord x, wxCoord y )
864 {
865 DrawBitmap( icon, x, y, TRUE );
866 }
867
868 /* this has to be char, not wxChar */
869 static char hexArray[] = "0123456789ABCDEF";
870 static void LocalDecToHex( int dec, char *buf )
871 {
872 int firstDigit = (int)(dec/16.0);
873 int secondDigit = (int)(dec - (firstDigit*16.0));
874 buf[0] = hexArray[firstDigit];
875 buf[1] = hexArray[secondDigit];
876 buf[2] = 0;
877 }
878
879 void wxPostScriptDC::DoDrawBitmap( const wxBitmap& bitmap, wxCoord x, wxCoord y, bool WXUNUSED(useMask) )
880 {
881 wxCHECK_RET( m_ok && m_pstream, wxT("invalid postscript dc") );
882
883 if (!bitmap.Ok()) return;
884
885 wxImage image = bitmap.ConvertToImage();
886
887 if (!image.Ok()) return;
888
889 wxCoord w = image.GetWidth();
890 wxCoord h = image.GetHeight();
891
892 wxCoord ww = LogicalToDeviceXRel(image.GetWidth());
893 wxCoord hh = LogicalToDeviceYRel(image.GetHeight());
894
895 wxCoord xx = LogicalToDeviceX(x);
896 wxCoord yy = LogicalToDeviceY(y + bitmap.GetHeight());
897
898 fprintf( m_pstream,
899 "/origstate save def\n"
900 "20 dict begin\n"
901 "/pix %d string def\n"
902 "/grays %d string def\n"
903 "/npixels 0 def\n"
904 "/rgbindx 0 def\n"
905 "%d %d translate\n"
906 "%d %d scale\n"
907 "%d %d 8\n"
908 "[%d 0 0 %d 0 %d]\n"
909 "{currentfile pix readhexstring pop}\n"
910 "false 3 colorimage\n",
911 w, w, xx, yy, ww, hh, w, h, w, -h, h );
912
913
914 for (int j = 0; j < h; j++)
915 {
916 for (int i = 0; i < w; i++)
917 {
918 char buffer[5];
919 LocalDecToHex( image.GetRed(i,j), buffer );
920 fprintf( m_pstream, buffer );
921 LocalDecToHex( image.GetGreen(i,j), buffer );
922 fprintf( m_pstream, buffer );
923 LocalDecToHex( image.GetBlue(i,j), buffer );
924 fprintf( m_pstream, buffer );
925 }
926 fprintf( m_pstream, "\n" );
927 }
928
929 fprintf( m_pstream, "end\n" );
930 fprintf( m_pstream, "origstate restore\n" );
931 }
932
933 void wxPostScriptDC::SetFont( const wxFont& font )
934 {
935 wxCHECK_RET( m_ok && m_pstream, wxT("invalid postscript dc") );
936
937 if (!font.Ok()) return;
938
939 m_font = font;
940
941 int Style = m_font.GetStyle();
942 int Weight = m_font.GetWeight();
943
944 const char *name;
945 switch (m_font.GetFamily())
946 {
947 case wxTELETYPE:
948 case wxMODERN:
949 {
950 if (Style == wxITALIC)
951 {
952 if (Weight == wxBOLD)
953 name = "/Courier-BoldOblique";
954 else
955 name = "/Courier-Oblique";
956 }
957 else
958 {
959 if (Weight == wxBOLD)
960 name = "/Courier-Bold";
961 else
962 name = "/Courier";
963 }
964 break;
965 }
966 case wxROMAN:
967 {
968 if (Style == wxITALIC)
969 {
970 if (Weight == wxBOLD)
971 name = "/Times-BoldItalic";
972 else
973 name = "/Times-Italic";
974 }
975 else
976 {
977 if (Weight == wxBOLD)
978 name = "/Times-Bold";
979 else
980 name = "/Times-Roman";
981 }
982 break;
983 }
984 case wxSCRIPT:
985 {
986 name = "/ZapfChancery-MediumItalic";
987 Style = wxNORMAL;
988 Weight = wxNORMAL;
989 break;
990 }
991 case wxSWISS:
992 default:
993 {
994 if (Style == wxITALIC)
995 {
996 if (Weight == wxBOLD)
997 name = "/Helvetica-BoldOblique";
998 else
999 name = "/Helvetica-Oblique";
1000 }
1001 else
1002 {
1003 if (Weight == wxBOLD)
1004 name = "/Helvetica-Bold";
1005 else
1006 name = "/Helvetica";
1007 }
1008 break;
1009 }
1010 }
1011
1012 fprintf( m_pstream, name );
1013 fprintf( m_pstream, " reencodeISO def\n" );
1014 fprintf( m_pstream, name );
1015 fprintf( m_pstream, " findfont\n" );
1016
1017 char buffer[100];
1018 sprintf( buffer, "%f scalefont setfont\n", LogicalToDeviceYRel(m_font.GetPointSize() * 1000) / 1000.0F);
1019 // this is a hack - we must scale font size (in pts) according to m_scaleY but
1020 // LogicalToDeviceYRel works with wxCoord type (int or longint). Se we first convert font size
1021 // to 1/1000th of pt and then back.
1022 for (int i = 0; i < 100; i++)
1023 if (buffer[i] == ',') buffer[i] = '.';
1024 fprintf( m_pstream, buffer );
1025 }
1026
1027 void wxPostScriptDC::SetPen( const wxPen& pen )
1028 {
1029 wxCHECK_RET( m_ok && m_pstream, wxT("invalid postscript dc") );
1030
1031 if (!pen.Ok()) return;
1032
1033 int oldStyle = m_pen.GetStyle();
1034
1035 m_pen = pen;
1036
1037 {
1038 char buffer[100];
1039 #ifdef __WXMSW__
1040 sprintf( buffer, "%f setlinewidth\n", LogicalToDeviceXRel(1000 * m_pen.GetWidth()) / 1000.0f );
1041 #else
1042 sprintf( buffer, "%f setlinewidth\n", LogicalToDeviceXRel(1000 * m_pen.GetWidth()) / 1000.0f );
1043 #endif
1044 for (int i = 0; i < 100; i++)
1045 if (buffer[i] == ',') buffer[i] = '.';
1046 fprintf( m_pstream, buffer );
1047 }
1048
1049 /*
1050 Line style - WRONG: 2nd arg is OFFSET
1051
1052 Here, I'm afraid you do not conceive meaning of parameters of 'setdash'
1053 operator correctly. You should look-up this in the Red Book: the 2nd parame-
1054 ter is not number of values in the array of the first one, but an offset
1055 into this description of the pattern. I mean a real *offset* not index
1056 into array. I.e. If the command is [3 4] 1 setdash is used, then there
1057 will be first black line *2* units wxCoord, then space 4 units, then the
1058 pattern of *3* units black, 4 units space will be repeated.
1059 */
1060
1061 static const char *dotted = "[2 5] 2";
1062 static const char *short_dashed = "[4 4] 2";
1063 static const char *wxCoord_dashed = "[4 8] 2";
1064 static const char *dotted_dashed = "[6 6 2 6] 4";
1065
1066 const char *psdash = (char *) NULL;
1067 switch (m_pen.GetStyle())
1068 {
1069 case wxDOT: psdash = dotted; break;
1070 case wxSHORT_DASH: psdash = short_dashed; break;
1071 case wxLONG_DASH: psdash = wxCoord_dashed; break;
1072 case wxDOT_DASH: psdash = dotted_dashed; break;
1073 case wxSOLID:
1074 case wxTRANSPARENT:
1075 default: psdash = "[] 0"; break;
1076 }
1077
1078 if (oldStyle != m_pen.GetStyle())
1079 {
1080 fprintf( m_pstream, psdash );
1081 fprintf( m_pstream," setdash\n" );
1082 }
1083
1084 // Line colour
1085 unsigned char red = m_pen.GetColour().Red();
1086 unsigned char blue = m_pen.GetColour().Blue();
1087 unsigned char green = m_pen.GetColour().Green();
1088
1089 if (!m_colour)
1090 {
1091 // Anything not white is black
1092 if (! (red == (unsigned char) 255 &&
1093 blue == (unsigned char) 255 &&
1094 green == (unsigned char) 255) )
1095 {
1096 red = (unsigned char) 0;
1097 green = (unsigned char) 0;
1098 blue = (unsigned char) 0;
1099 }
1100 // setgray here ?
1101 }
1102
1103 if (!(red == m_currentRed && green == m_currentGreen && blue == m_currentBlue))
1104 {
1105 double redPS = (double)(red) / 255.0;
1106 double bluePS = (double)(blue) / 255.0;
1107 double greenPS = (double)(green) / 255.0;
1108
1109 char buffer[100];
1110 sprintf( buffer,
1111 "%.8f %.8f %.8f setrgbcolor\n",
1112 redPS, greenPS, bluePS );
1113 for (int i = 0; i < 100; i++)
1114 if (buffer[i] == ',') buffer[i] = '.';
1115 fprintf( m_pstream, buffer );
1116
1117 m_currentRed = red;
1118 m_currentBlue = blue;
1119 m_currentGreen = green;
1120 }
1121 }
1122
1123 void wxPostScriptDC::SetBrush( const wxBrush& brush )
1124 {
1125 wxCHECK_RET( m_ok && m_pstream, wxT("invalid postscript dc") );
1126
1127 if (!brush.Ok()) return;
1128
1129 m_brush = brush;
1130
1131 // Brush colour
1132 unsigned char red = m_brush.GetColour().Red();
1133 unsigned char blue = m_brush.GetColour().Blue();
1134 unsigned char green = m_brush.GetColour().Green();
1135
1136 if (!m_colour)
1137 {
1138 // Anything not white is black
1139 if (! (red == (unsigned char) 255 &&
1140 blue == (unsigned char) 255 &&
1141 green == (unsigned char) 255) )
1142 {
1143 red = (unsigned char) 0;
1144 green = (unsigned char) 0;
1145 blue = (unsigned char) 0;
1146 }
1147 // setgray here ?
1148 }
1149
1150 if (!(red == m_currentRed && green == m_currentGreen && blue == m_currentBlue))
1151 {
1152 double redPS = (double)(red) / 255.0;
1153 double bluePS = (double)(blue) / 255.0;
1154 double greenPS = (double)(green) / 255.0;
1155
1156 char buffer[100];
1157 sprintf( buffer,
1158 "%.8f %.8f %.8f setrgbcolor\n",
1159 redPS, greenPS, bluePS );
1160 for (int i = 0; i < 100; i++)
1161 if (buffer[i] == ',') buffer[i] = '.';
1162 fprintf( m_pstream, buffer );
1163
1164 m_currentRed = red;
1165 m_currentBlue = blue;
1166 m_currentGreen = green;
1167 }
1168 }
1169
1170 void wxPostScriptDC::DoDrawText( const wxString& text, wxCoord x, wxCoord y )
1171 {
1172 wxCHECK_RET( m_ok && m_pstream, wxT("invalid postscript dc") );
1173
1174 wxCoord text_w, text_h, text_descent;
1175
1176 GetTextExtent(text, &text_w, &text_h, &text_descent);
1177
1178 // VZ: this seems to be unnecessary, so taking it out for now, if it
1179 // doesn't create any problems, remove this comment entirely
1180 //SetFont( m_font );
1181
1182 if (m_textForegroundColour.Ok())
1183 {
1184 unsigned char red = m_textForegroundColour.Red();
1185 unsigned char blue = m_textForegroundColour.Blue();
1186 unsigned char green = m_textForegroundColour.Green();
1187
1188 if (!m_colour)
1189 {
1190 // Anything not white is black
1191 if (! (red == (unsigned char) 255 &&
1192 blue == (unsigned char) 255 &&
1193 green == (unsigned char) 255))
1194 {
1195 red = (unsigned char) 0;
1196 green = (unsigned char) 0;
1197 blue = (unsigned char) 0;
1198 }
1199 }
1200
1201 // maybe setgray here ?
1202 if (!(red == m_currentRed && green == m_currentGreen && blue == m_currentBlue))
1203 {
1204 double redPS = (double)(red) / 255.0;
1205 double bluePS = (double)(blue) / 255.0;
1206 double greenPS = (double)(green) / 255.0;
1207
1208 char buffer[100];
1209 sprintf( buffer,
1210 "%.8f %.8f %.8f setrgbcolor\n",
1211 redPS, greenPS, bluePS );
1212 for (int i = 0; i < 100; i++)
1213 if (buffer[i] == ',') buffer[i] = '.';
1214 fprintf( m_pstream, buffer );
1215
1216 m_currentRed = red;
1217 m_currentBlue = blue;
1218 m_currentGreen = green;
1219 }
1220 }
1221
1222 int size = m_font.GetPointSize();
1223
1224 // wxCoord by = y + (wxCoord)floor( double(size) * 2.0 / 3.0 ); // approximate baseline
1225 // commented by V. Slavik and replaced by accurate version
1226 // - note that there is still rounding error in text_descent!
1227 wxCoord by = y + size - text_descent; // baseline
1228 fprintf( m_pstream, "%d %d moveto\n", LogicalToDeviceX(x), LogicalToDeviceY(by) );
1229
1230 fprintf( m_pstream, "(" );
1231 const wxWX2MBbuf textbuf = text.mb_str();
1232 size_t len = strlen(textbuf);
1233 size_t i;
1234 for (i = 0; i < len; i++)
1235 {
1236 int c = (unsigned char) textbuf[i];
1237 if (c == ')' || c == '(' || c == '\\')
1238 {
1239 /* Cope with special characters */
1240 fprintf( m_pstream, "\\" );
1241 fputc(c, m_pstream);
1242 }
1243 else if ( c >= 128 )
1244 {
1245 /* Cope with character codes > 127 */
1246 fprintf(m_pstream, "\\%o", c);
1247 }
1248 else
1249 {
1250 fputc(c, m_pstream);
1251 }
1252 }
1253
1254 fprintf( m_pstream, ") show\n" );
1255
1256 if (m_font.GetUnderlined())
1257 {
1258 wxCoord uy = (wxCoord)(y + size - m_underlinePosition);
1259 char buffer[100];
1260
1261 sprintf( buffer,
1262 "gsave\n"
1263 "%d %d moveto\n"
1264 "%f setlinewidth\n"
1265 "%d %d lineto\n"
1266 "stroke\n"
1267 "grestore\n",
1268 LogicalToDeviceX(x), LogicalToDeviceY(uy),
1269 m_underlineThickness,
1270 LogicalToDeviceX(x + text_w), LogicalToDeviceY(uy) );
1271 for (i = 0; i < 100; i++)
1272 if (buffer[i] == ',') buffer[i] = '.';
1273 fprintf( m_pstream, buffer );
1274 }
1275
1276 CalcBoundingBox( x, y );
1277 CalcBoundingBox( x + size * text.Length() * 2/3 , y );
1278 }
1279
1280 void wxPostScriptDC::DoDrawRotatedText( const wxString& text, wxCoord x, wxCoord y, double angle )
1281 {
1282 if (angle == 0.0)
1283 {
1284 DoDrawText(text, x, y);
1285 return;
1286 }
1287
1288 wxCHECK_RET( m_ok && m_pstream, wxT("invalid postscript dc") );
1289
1290 SetFont( m_font );
1291
1292 if (m_textForegroundColour.Ok())
1293 {
1294 unsigned char red = m_textForegroundColour.Red();
1295 unsigned char blue = m_textForegroundColour.Blue();
1296 unsigned char green = m_textForegroundColour.Green();
1297
1298 if (!m_colour)
1299 {
1300 // Anything not white is black
1301 if (! (red == (unsigned char) 255 &&
1302 blue == (unsigned char) 255 &&
1303 green == (unsigned char) 255))
1304 {
1305 red = (unsigned char) 0;
1306 green = (unsigned char) 0;
1307 blue = (unsigned char) 0;
1308 }
1309 }
1310
1311 // maybe setgray here ?
1312 if (!(red == m_currentRed && green == m_currentGreen && blue == m_currentBlue))
1313 {
1314 double redPS = (double)(red) / 255.0;
1315 double bluePS = (double)(blue) / 255.0;
1316 double greenPS = (double)(green) / 255.0;
1317
1318 char buffer[100];
1319 sprintf( buffer,
1320 "%.8f %.8f %.8f setrgbcolor\n",
1321 redPS, greenPS, bluePS );
1322 for (int i = 0; i < 100; i++)
1323 if (buffer[i] == ',') buffer[i] = '.';
1324 fprintf( m_pstream, buffer );
1325
1326 m_currentRed = red;
1327 m_currentBlue = blue;
1328 m_currentGreen = green;
1329 }
1330 }
1331
1332 int size = m_font.GetPointSize();
1333
1334 long by = y + (long)floor( double(size) * 2.0 / 3.0 ); // approximate baseline
1335
1336 // FIXME only correct for 90 degrees
1337 fprintf(m_pstream, "%d %d moveto\n",
1338 LogicalToDeviceX((wxCoord)(x + size)), LogicalToDeviceY((wxCoord)by) );
1339
1340 char buffer[100];
1341 sprintf(buffer, "%.8f rotate\n", angle);
1342 size_t i;
1343 for (i = 0; i < 100; i++)
1344 if (buffer[i] == ',') buffer[i] = '.';
1345 fprintf(m_pstream, buffer);
1346
1347 fprintf( m_pstream, "(" );
1348 const wxWX2MBbuf textbuf = text.mb_str();
1349 size_t len = strlen(textbuf);
1350 for (i = 0; i < len; i++)
1351 {
1352 int c = (unsigned char) textbuf[i];
1353 if (c == ')' || c == '(' || c == '\\')
1354 {
1355 /* Cope with special characters */
1356 fprintf( m_pstream, "\\" );
1357 fputc(c, m_pstream);
1358 }
1359 else if ( c >= 128 )
1360 {
1361 /* Cope with character codes > 127 */
1362 fprintf(m_pstream, "\\%o", c);
1363 }
1364 else
1365 {
1366 fputc(c, m_pstream);
1367 }
1368 }
1369
1370 fprintf( m_pstream, ") show\n" );
1371
1372 sprintf( buffer, "%.8f rotate\n", -angle );
1373 for (i = 0; i < 100; i++)
1374 if (buffer[i] == ',') buffer[i] = '.';
1375 fprintf( m_pstream, buffer );
1376
1377 if (m_font.GetUnderlined())
1378 {
1379 wxCoord uy = (wxCoord)(y + size - m_underlinePosition);
1380 wxCoord w, h;
1381 char buffer[100];
1382 GetTextExtent(text, &w, &h);
1383
1384 sprintf( buffer,
1385 "gsave\n"
1386 "%d %d moveto\n"
1387 "%f setlinewidth\n"
1388 "%d %d lineto\n"
1389 "stroke\n"
1390 "grestore\n",
1391 LogicalToDeviceX(x), LogicalToDeviceY(uy),
1392 m_underlineThickness,
1393 LogicalToDeviceX(x + w), LogicalToDeviceY(uy) );
1394 for (i = 0; i < 100; i++)
1395 if (buffer[i] == ',') buffer[i] = '.';
1396 fprintf( m_pstream, buffer );
1397 }
1398
1399 CalcBoundingBox( x, y );
1400 CalcBoundingBox( x + size * text.Length() * 2/3 , y );
1401 }
1402
1403 void wxPostScriptDC::SetBackground (const wxBrush& brush)
1404 {
1405 m_backgroundBrush = brush;
1406 }
1407
1408 void wxPostScriptDC::SetLogicalFunction (int WXUNUSED(function))
1409 {
1410 wxFAIL_MSG( wxT("wxPostScriptDC::SetLogicalFunction not implemented.") );
1411 }
1412
1413 void wxPostScriptDC::DoDrawSpline( wxList *points )
1414 {
1415 wxCHECK_RET( m_ok && m_pstream, wxT("invalid postscript dc") );
1416
1417 SetPen( m_pen );
1418
1419 double a, b, c, d, x1, y1, x2, y2, x3, y3;
1420 wxPoint *p, *q;
1421
1422 wxNode *node = points->First();
1423 p = (wxPoint *)node->Data();
1424 x1 = p->x;
1425 y1 = p->y;
1426
1427 node = node->Next();
1428 p = (wxPoint *)node->Data();
1429 c = p->x;
1430 d = p->y;
1431 x3 = a = (double)(x1 + c) / 2;
1432 y3 = b = (double)(y1 + d) / 2;
1433
1434 fprintf( m_pstream,
1435 "newpath\n"
1436 "%d %d moveto\n"
1437 "%d %d lineto\n",
1438 LogicalToDeviceX((wxCoord)x1), LogicalToDeviceY((wxCoord)y1),
1439 LogicalToDeviceX((wxCoord)x3), LogicalToDeviceY((wxCoord)y3) );
1440
1441 CalcBoundingBox( (wxCoord)x1, (wxCoord)y1 );
1442 CalcBoundingBox( (wxCoord)x3, (wxCoord)y3 );
1443
1444 while ((node = node->Next()) != NULL)
1445 {
1446 q = (wxPoint *)node->Data();
1447
1448 x1 = x3;
1449 y1 = y3;
1450 x2 = c;
1451 y2 = d;
1452 c = q->x;
1453 d = q->y;
1454 x3 = (double)(x2 + c) / 2;
1455 y3 = (double)(y2 + d) / 2;
1456
1457 fprintf( m_pstream,
1458 "%d %d %d %d %d %d DrawSplineSection\n",
1459 LogicalToDeviceX((wxCoord)x1), LogicalToDeviceY((wxCoord)y1),
1460 LogicalToDeviceX((wxCoord)x2), LogicalToDeviceY((wxCoord)y2),
1461 LogicalToDeviceX((wxCoord)x3), LogicalToDeviceY((wxCoord)y3) );
1462
1463 CalcBoundingBox( (wxCoord)x1, (wxCoord)y1 );
1464 CalcBoundingBox( (wxCoord)x3, (wxCoord)y3 );
1465 }
1466
1467 /*
1468 At this point, (x2,y2) and (c,d) are the position of the
1469 next-to-last and last point respectively, in the point list
1470 */
1471
1472 fprintf( m_pstream,
1473 "%d %d lineto\n"
1474 "stroke\n",
1475 LogicalToDeviceX((wxCoord)c), LogicalToDeviceY((wxCoord)d) );
1476 }
1477
1478 wxCoord wxPostScriptDC::GetCharWidth() const
1479 {
1480 // Chris Breeze: reasonable approximation using wxMODERN/Courier
1481 return (wxCoord) (GetCharHeight() * 72.0 / 120.0);
1482 }
1483
1484
1485 void wxPostScriptDC::SetAxisOrientation( bool xLeftRight, bool yBottomUp )
1486 {
1487 wxCHECK_RET( m_ok && m_pstream, wxT("invalid postscript dc") );
1488
1489 m_signX = (xLeftRight ? 1 : -1);
1490 m_signY = (yBottomUp ? 1 : -1);
1491
1492 // FIXME there is no such function in MSW nor in OS2/PM
1493 #if !defined(__WXMSW__) && !defined(__WXPM__)
1494 ComputeScaleAndOrigin();
1495 #endif
1496 }
1497
1498 void wxPostScriptDC::SetDeviceOrigin( wxCoord x, wxCoord y )
1499 {
1500 wxCHECK_RET( m_ok && m_pstream, wxT("invalid postscript dc") );
1501
1502 int h = 0;
1503 int w = 0;
1504 GetSize( &w, &h );
1505
1506 wxDC::SetDeviceOrigin( x, h-y );
1507 }
1508
1509 void wxPostScriptDC::DoGetSize(int* width, int* height) const
1510 {
1511 wxPaperSize id = m_printData.GetPaperId();
1512
1513 wxPrintPaperType *paper = wxThePrintPaperDatabase->FindPaperType(id);
1514
1515 if (!paper) paper = wxThePrintPaperDatabase->FindPaperType(wxPAPER_A4);
1516
1517 int w = 595;
1518 int h = 842;
1519 if (paper)
1520 {
1521 w = paper->GetSizeDeviceUnits().x;
1522 h = paper->GetSizeDeviceUnits().y;
1523 }
1524
1525 if (m_printData.GetOrientation() == wxLANDSCAPE)
1526 {
1527 int tmp = w;
1528 w = h;
1529 h = tmp;
1530 }
1531
1532 if (width) *width = (int)(w * ms_PSScaleFactor);
1533 if (height) *height = (int)(h * ms_PSScaleFactor);
1534 }
1535
1536 void wxPostScriptDC::DoGetSizeMM(int *width, int *height) const
1537 {
1538 wxPaperSize id = m_printData.GetPaperId();
1539
1540 wxPrintPaperType *paper = wxThePrintPaperDatabase->FindPaperType(id);
1541
1542 if (!paper) paper = wxThePrintPaperDatabase->FindPaperType(wxPAPER_A4);
1543
1544 int w = 210;
1545 int h = 297;
1546 if (paper)
1547 {
1548 w = paper->GetWidth() / 10;
1549 h = paper->GetHeight() / 10;
1550 }
1551
1552 if (m_printData.GetOrientation() == wxLANDSCAPE)
1553 {
1554 int tmp = w;
1555 w = h;
1556 h = tmp;
1557 }
1558
1559 if (width) *width = w;
1560 if (height) *height = h;
1561 }
1562
1563 // Resolution in pixels per logical inch
1564 wxSize wxPostScriptDC::GetPPI(void) const
1565 {
1566 return wxSize((int)(72 * ms_PSScaleFactor),
1567 (int)(72 * ms_PSScaleFactor));
1568 }
1569
1570
1571 bool wxPostScriptDC::StartDoc( const wxString& message )
1572 {
1573 wxCHECK_MSG( m_ok, FALSE, wxT("invalid postscript dc") );
1574
1575 if (m_printData.GetFilename() == "")
1576 {
1577 wxString filename = wxGetTempFileName("ps");
1578 m_printData.SetFilename(filename);
1579 }
1580
1581 m_pstream = wxFopen( m_printData.GetFilename().fn_str(), wxT("w+") );
1582
1583 if (!m_pstream)
1584 {
1585 wxLogError( _("Cannot open file for PostScript printing!"));
1586 m_ok = FALSE;
1587 return FALSE;
1588 }
1589
1590 m_ok = TRUE;
1591
1592 fprintf( m_pstream, "%%%%BeginProlog\n" );
1593 fprintf( m_pstream, wxPostScriptHeaderEllipse );
1594 fprintf( m_pstream, wxPostScriptHeaderEllipticArc );
1595 fprintf( m_pstream, wxPostScriptHeaderColourImage );
1596 fprintf( m_pstream, wxPostScriptHeaderReencodeISO1 );
1597 fprintf( m_pstream, wxPostScriptHeaderReencodeISO2 );
1598 if (wxPostScriptHeaderSpline)
1599 fprintf( m_pstream, wxPostScriptHeaderSpline );
1600 fprintf( m_pstream, "%%%%EndProlog\n" );
1601
1602 SetBrush( *wxBLACK_BRUSH );
1603 SetPen( *wxBLACK_PEN );
1604 SetBackground( *wxWHITE_BRUSH );
1605 SetTextForeground( *wxBLACK );
1606
1607 // set origin according to paper size
1608 SetDeviceOrigin( 0,0 );
1609
1610 wxPageNumber = 1;
1611 m_pageNumber = 1;
1612 m_title = message;
1613 return TRUE;
1614 }
1615
1616 void wxPostScriptDC::EndDoc ()
1617 {
1618 wxCHECK_RET( m_ok && m_pstream, wxT("invalid postscript dc") );
1619
1620 if (m_clipping)
1621 {
1622 m_clipping = FALSE;
1623 fprintf( m_pstream, "grestore\n" );
1624 }
1625
1626 fclose( m_pstream );
1627 m_pstream = (FILE *) NULL;
1628
1629 wxChar *header_file = wxGetTempFileName("ps");
1630
1631 m_pstream = fopen( wxConvFile.cWX2MB(header_file) , "w+" );
1632
1633 fprintf( m_pstream, "%%!PS-Adobe-2.0\n" ); // PostScript magic strings
1634 fprintf( m_pstream, "%%%%Title: %s\n", (const char *)m_title.mb_str() );
1635 fprintf( m_pstream, "%%%%Creator: %s\n", (const char*)wxConvCurrent->cWX2MB(wxTheApp->argv[0]) );
1636 fprintf( m_pstream, "%%%%CreationDate: %s\n", (const char *)wxNow().mb_str() );
1637
1638 wxChar userID[256];
1639 if ( wxGetEmailAddress(userID, sizeof(userID)) )
1640 {
1641 fprintf( m_pstream, "%%%%For: %s ", wxMBSTRINGCAST wxConvCurrent->cWX2MB(userID) );
1642 wxChar userName[245];
1643 if (wxGetUserName(userName, sizeof(userName)))
1644 fprintf( m_pstream, " (%s)", wxMBSTRINGCAST wxConvCurrent->cWX2MB(userName) );
1645 fprintf( m_pstream, "\n" );
1646 }
1647 else if ( wxGetUserName(userID, sizeof(userID)) )
1648 {
1649 fprintf( m_pstream, "%%%%For: %s\n", wxMBSTRINGCAST wxConvCurrent->cWX2MB(userID) );;
1650 }
1651
1652 // THE FOLLOWING HAS BEEN CONTRIBUTED BY Andy Fyfe <andy@hyperparallel.com>
1653
1654 wxCoord wx_printer_translate_x, wx_printer_translate_y;
1655 double wx_printer_scale_x, wx_printer_scale_y;
1656
1657 wx_printer_translate_x = (wxCoord)m_printData.GetPrinterTranslateX();
1658 wx_printer_translate_y = (wxCoord)m_printData.GetPrinterTranslateY();
1659
1660 wx_printer_scale_x = m_printData.GetPrinterScaleX();
1661 wx_printer_scale_y = m_printData.GetPrinterScaleY();
1662
1663 if (m_printData.GetOrientation() == wxLANDSCAPE)
1664 fprintf( m_pstream, "%%%%Orientation: Landscape\n" );
1665 else
1666 fprintf( m_pstream, "%%%%Orientation: Portrait\n" );
1667
1668 // Compute the bounding box. Note that it is in the default user
1669 // coordinate system, thus we have to convert the values.
1670 wxCoord minX = (wxCoord) LogicalToDeviceX(m_minX);
1671 wxCoord minY = (wxCoord) LogicalToDeviceY(m_minY);
1672 wxCoord maxX = (wxCoord) LogicalToDeviceX(m_maxX);
1673 wxCoord maxY = (wxCoord) LogicalToDeviceY(m_maxY);
1674
1675 // LOG2DEV may have changed the minimum to maximum vice versa
1676 if ( minX > maxX ) { wxCoord tmp = minX; minX = maxX; maxX = tmp; }
1677 if ( minY > maxY ) { wxCoord tmp = minY; minY = maxY; maxY = tmp; }
1678
1679 // account for used scaling (boundingbox is before scaling in ps-file)
1680 double scale_x = m_printData.GetPrinterScaleX() / ms_PSScaleFactor;
1681 double scale_y = m_printData.GetPrinterScaleY() / ms_PSScaleFactor;
1682
1683 wxCoord llx, lly, urx, ury;
1684 llx = (wxCoord) ((minX+wx_printer_translate_x)*scale_x);
1685 lly = (wxCoord) ((minY+wx_printer_translate_y)*scale_y);
1686 urx = (wxCoord) ((maxX+wx_printer_translate_x)*scale_x);
1687 ury = (wxCoord) ((maxY+wx_printer_translate_y)*scale_y);
1688 // (end of bounding box computation)
1689
1690
1691 // If we're landscape, our sense of "x" and "y" is reversed.
1692 if (m_printData.GetOrientation() == wxLANDSCAPE)
1693 {
1694 wxCoord tmp;
1695 tmp = llx; llx = lly; lly = tmp;
1696 tmp = urx; urx = ury; ury = tmp;
1697
1698 // We need either the two lines that follow, or we need to subtract
1699 // min_x from real_translate_y, which is commented out below.
1700 llx = llx - (wxCoord)(m_minX*wx_printer_scale_y);
1701 urx = urx - (wxCoord)(m_minX*wx_printer_scale_y);
1702 }
1703
1704 // The Adobe specifications call for integers; we round as to make
1705 // the bounding larger.
1706 fprintf( m_pstream,
1707 "%%%%BoundingBox: %d %d %d %d\n",
1708 (wxCoord)floor((double)llx), (wxCoord)floor((double)lly),
1709 (wxCoord)ceil((double)urx), (wxCoord)ceil((double)ury) );
1710 fprintf( m_pstream, "%%%%Pages: %d\n", (wxPageNumber - 1) );
1711 fprintf( m_pstream, "%%%%EndComments\n\n" );
1712
1713 // To check the correctness of the bounding box, postscript commands
1714 // to draw a box corresponding to the bounding box are generated below.
1715 // But since we typically don't want to print such a box, the postscript
1716 // commands are generated within comments. These lines appear before any
1717 // adjustment of scale, rotation, or translation, and hence are in the
1718 // default user coordinates.
1719 fprintf( m_pstream, "%% newpath\n" );
1720 fprintf( m_pstream, "%% %d %d moveto\n", llx, lly );
1721 fprintf( m_pstream, "%% %d %d lineto\n", urx, lly );
1722 fprintf( m_pstream, "%% %d %d lineto\n", urx, ury );
1723 fprintf( m_pstream, "%% %d %d lineto closepath stroke\n", llx, ury );
1724
1725 fclose( m_pstream );
1726 m_pstream = (FILE*) NULL;
1727
1728 wxChar *tmp_file = wxGetTempFileName("ps");
1729
1730 // Paste header Before wx_printer_file
1731 wxConcatFiles (header_file, m_printData.GetFilename(), tmp_file );
1732 wxRemoveFile( header_file );
1733 wxRemoveFile( m_printData.GetFilename() );
1734 wxRenameFile( tmp_file, m_printData.GetFilename() );
1735
1736 #if defined(__X__) || defined(__WXGTK__)
1737 if (m_ok)
1738 {
1739 wxString previewCommand(m_printData.GetPreviewCommand());
1740 wxString printerCommand(m_printData.GetPrinterCommand());
1741 wxString printerOptions(m_printData.GetPrinterOptions());
1742 wxString filename(m_printData.GetFilename());
1743
1744 switch (m_printData.GetPrintMode())
1745 {
1746 case wxPRINT_MODE_PREVIEW:
1747 {
1748 wxChar *argv[3];
1749 argv[0] = WXSTRINGCAST previewCommand;
1750 argv[1] = WXSTRINGCAST filename;
1751 argv[2] = (wxChar*) NULL;
1752 wxExecute( argv, TRUE );
1753 wxRemoveFile( m_printData.GetFilename() );
1754 }
1755 break;
1756 case wxPRINT_MODE_PRINTER:
1757 {
1758 wxChar *argv[4];
1759 int argc = 0;
1760 argv[argc++] = WXSTRINGCAST printerCommand;
1761
1762 // !SM! If we simply assign to argv[1] here, if printer options
1763 // are blank, we get an annoying and confusing message from lpr.
1764 wxChar *opts = WXSTRINGCAST printerOptions;
1765 if (opts && *opts)
1766 argv[argc++] = opts;
1767
1768 argv[argc++] = WXSTRINGCAST filename;
1769 argv[argc++] = (wxChar *) NULL;
1770 wxExecute( argv, TRUE );
1771 wxRemoveFile( filename );
1772 }
1773 break;
1774 case wxPRINT_MODE_FILE:
1775 case wxPRINT_MODE_NONE:
1776 break;
1777 }
1778 }
1779 #endif
1780 }
1781
1782 void wxPostScriptDC::StartPage()
1783 {
1784 wxCHECK_RET( m_ok && m_pstream, wxT("invalid postscript dc") );
1785
1786 fprintf( m_pstream, "%%%%Page: %d\n", wxPageNumber++ );
1787
1788 // What is this one supposed to do? RR.
1789 // *m_pstream << "matrix currentmatrix\n";
1790
1791 // Added by Chris Breeze
1792
1793 // Each page starts with an "initgraphics" which resets the
1794 // transformation and so we need to reset the origin
1795 // (and rotate the page for landscape printing)
1796
1797 // Output scaling
1798 wxCoord translate_x, translate_y;
1799 double scale_x, scale_y;
1800
1801 translate_x = (wxCoord)m_printData.GetPrinterTranslateX();
1802 translate_y = (wxCoord)m_printData.GetPrinterTranslateY();
1803
1804 scale_x = m_printData.GetPrinterScaleX();
1805 scale_y = m_printData.GetPrinterScaleY();
1806
1807 if (m_printData.GetOrientation() == wxLANDSCAPE)
1808 {
1809 int h;
1810 GetSize( (int*) NULL, &h );
1811 translate_y -= h;
1812 fprintf( m_pstream, "90 rotate\n" );
1813
1814 // I copied this one from a PostScript tutorial, but to no avail. RR.
1815 // fprintf( m_pstream, "90 rotate llx neg ury nef translate\n" );
1816 }
1817
1818 char buffer[100];
1819 sprintf( buffer, "%.8f %.8f scale\n", scale_x / ms_PSScaleFactor,
1820 scale_y / ms_PSScaleFactor);
1821 for (int i = 0; i < 100; i++)
1822 if (buffer[i] == ',') buffer[i] = '.';
1823 fprintf( m_pstream, buffer );
1824
1825 fprintf( m_pstream, "%d %d translate\n", translate_x, translate_y );
1826 }
1827
1828 void wxPostScriptDC::EndPage ()
1829 {
1830 wxCHECK_RET( m_ok && m_pstream, wxT("invalid postscript dc") );
1831
1832 fprintf( m_pstream, "showpage\n" );
1833 }
1834
1835 bool wxPostScriptDC::DoBlit( wxCoord xdest, wxCoord ydest,
1836 wxCoord fwidth, wxCoord fheight,
1837 wxDC *source,
1838 wxCoord xsrc, wxCoord ysrc,
1839 int rop, bool WXUNUSED(useMask), wxCoord WXUNUSED(xsrcMask), wxCoord WXUNUSED(ysrcMask) )
1840 {
1841 wxCHECK_MSG( m_ok && m_pstream, FALSE, wxT("invalid postscript dc") );
1842
1843 wxCHECK_MSG( source, FALSE, wxT("invalid source dc") );
1844
1845 /* blit into a bitmap */
1846 wxBitmap bitmap( (int)fwidth, (int)fheight );
1847 wxMemoryDC memDC;
1848 memDC.SelectObject(bitmap);
1849 memDC.Blit(0, 0, fwidth, fheight, source, xsrc, ysrc, rop); /* TODO: Blit transparently? */
1850 memDC.SelectObject(wxNullBitmap);
1851
1852 /* draw bitmap. scaling and positioning is done there */
1853 DrawBitmap( bitmap, xdest, ydest );
1854
1855 return TRUE;
1856 }
1857
1858 wxCoord wxPostScriptDC::GetCharHeight() const
1859 {
1860 if (m_font.Ok())
1861 return m_font.GetPointSize();
1862 else
1863 return 12;
1864 }
1865
1866 void wxPostScriptDC::DoGetTextExtent(const wxString& string,
1867 wxCoord *x, wxCoord *y,
1868 wxCoord *descent, wxCoord *externalLeading,
1869 wxFont *theFont ) const
1870 {
1871 wxFont *fontToUse = theFont;
1872
1873 if (!fontToUse) fontToUse = (wxFont*) &m_font;
1874
1875 wxCHECK_RET( fontToUse, wxT("GetTextExtent: no font defined") );
1876
1877 const wxWX2MBbuf strbuf = string.mb_str();
1878
1879 #if !wxUSE_AFM_FOR_POSTSCRIPT
1880 /* Provide a VERY rough estimate (avoid using it).
1881 * Produces accurate results for mono-spaced font
1882 * such as Courier (aka wxMODERN) */
1883
1884 int height = 12;
1885 if (fontToUse)
1886 {
1887 height = fontToUse->GetPointSize();
1888 }
1889 if ( x )
1890 *x = strlen (strbuf) * height * 72 / 120;
1891 if ( y )
1892 *y = (wxCoord) (height * 1.32); /* allow for descender */
1893 if (descent) *descent = 0;
1894 if (externalLeading) *externalLeading = 0;
1895 #else
1896
1897 /* method for calculating string widths in postscript:
1898 / read in the AFM (adobe font metrics) file for the
1899 / actual font, parse it and extract the character widths
1900 / and also the descender. this may be improved, but for now
1901 / it works well. the AFM file is only read in if the
1902 / font is changed. this may be chached in the future.
1903 / calls to GetTextExtent with the font unchanged are rather
1904 / efficient!!!
1905 /
1906 / for each font and style used there is an AFM file necessary.
1907 / currently i have only files for the roman font family.
1908 / I try to get files for the other ones!
1909 /
1910 / CAVE: the size of the string is currently always calculated
1911 / in 'points' (1/72 of an inch). this should later on be
1912 / changed to depend on the mapping mode.
1913 / CAVE: the path to the AFM files must be set before calling this
1914 / function. this is usually done by a call like the following:
1915 / wxSetAFMPath("d:\\wxw161\\afm\\");
1916 /
1917 / example:
1918 /
1919 / wxPostScriptDC dc(NULL, TRUE);
1920 / if (dc.Ok()){
1921 / wxSetAFMPath("d:\\wxw161\\afm\\");
1922 / dc.StartDoc("Test");
1923 / dc.StartPage();
1924 / wxCoord w,h;
1925 / dc.SetFont(new wxFont(10, wxROMAN, wxNORMAL, wxNORMAL));
1926 / dc.GetTextExtent("Hallo",&w,&h);
1927 / dc.EndPage();
1928 / dc.EndDoc();
1929 / }
1930 /
1931 / by steve (stefan.hammes@urz.uni-heidelberg.de)
1932 / created: 10.09.94
1933 / updated: 14.05.95 */
1934
1935 /* these static vars are for storing the state between calls */
1936 static int lastFamily= INT_MIN;
1937 static int lastSize= INT_MIN;
1938 static int lastStyle= INT_MIN;
1939 static int lastWeight= INT_MIN;
1940 static int lastDescender = INT_MIN;
1941 static int lastWidths[256]; /* widths of the characters */
1942
1943 double UnderlinePosition = 0.0;
1944 double UnderlineThickness = 0.0;
1945
1946 /* get actual parameters */
1947 int Family = fontToUse->GetFamily();
1948 int Size = fontToUse->GetPointSize();
1949 int Style = fontToUse->GetStyle();
1950 int Weight = fontToUse->GetWeight();
1951
1952 /* if we have another font, read the font-metrics */
1953 if (Family!=lastFamily || Size!=lastSize || Style!=lastStyle || Weight!=lastWeight)
1954 {
1955 /* store actual values */
1956 lastFamily = Family;
1957 lastSize = Size;
1958 lastStyle = Style;
1959 lastWeight = Weight;
1960
1961 const char *name = NULL;
1962
1963 switch (Family)
1964 {
1965 case wxMODERN:
1966 case wxTELETYPE:
1967 {
1968 if ((Style == wxITALIC) && (Weight == wxBOLD)) name = "CourBoO.afm";
1969 else if ((Style != wxITALIC) && (Weight == wxBOLD)) name = "CourBo.afm";
1970 else if ((Style == wxITALIC) && (Weight != wxBOLD)) name = "CourO.afm";
1971 else name = "Cour.afm";
1972 break;
1973 }
1974 case wxROMAN:
1975 {
1976 if ((Style == wxITALIC) && (Weight == wxBOLD)) name = "TimesBoO.afm";
1977 else if ((Style != wxITALIC) && (Weight == wxBOLD)) name = "TimesBo.afm";
1978 else if ((Style == wxITALIC) && (Weight != wxBOLD)) name = "TimesO.afm";
1979 else name = "TimesRo.afm";
1980 break;
1981 }
1982 case wxSCRIPT:
1983 {
1984 name = "Zapf.afm";
1985 Style = wxNORMAL;
1986 Weight = wxNORMAL;
1987 }
1988 case wxSWISS:
1989 default:
1990 {
1991 if ((Style == wxITALIC) && (Weight == wxBOLD)) name = "HelvBoO.afm";
1992 else if ((Style != wxITALIC) && (Weight == wxBOLD)) name = "HelvBo.afm";
1993 else if ((Style == wxITALIC) && (Weight != wxBOLD)) name = "HelvO.afm";
1994 else name = "Helv.afm";
1995 break;
1996 }
1997 }
1998
1999 /* get the directory of the AFM files */
2000 wxString afmName = wxEmptyString;
2001 if (!m_printData.GetFontMetricPath().IsEmpty())
2002 {
2003 afmName = m_printData.GetFontMetricPath();
2004 }
2005
2006 /* 2. open and process the file
2007 / a short explanation of the AFM format:
2008 / we have for each character a line, which gives its size
2009 / e.g.:
2010 /
2011 / C 63 ; WX 444 ; N question ; B 49 -14 395 676 ;
2012 /
2013 / that means, we have a character with ascii code 63, and width
2014 / (444/1000 * fontSize) points.
2015 / the other data is ignored for now!
2016 /
2017 / when the font has changed, we read in the right AFM file and store the
2018 / character widths in an array, which is processed below (see point 3.). */
2019
2020 /* new elements JC Sun Aug 25 23:21:44 MET DST 1996 */
2021
2022 afmName << name;
2023 FILE *afmFile = wxFopen(afmName,wxT("r"));
2024
2025 if (afmFile==NULL)
2026 {
2027 afmName = wxThePrintSetupData->GetAFMPath();
2028 afmName << wxFILE_SEP_PATH << name;
2029 afmFile = wxFopen(afmName,wxT("r"));
2030 }
2031
2032 #if defined(__UNIX__) && !defined(__VMS__)
2033 if (afmFile==NULL)
2034 /* please do NOT change the line above to "else if (afmFile==NULL)" -
2035 - afmFile = fopen() may fail and in that case the next if branch
2036 MUST be executed - and it would not if there was "else" */
2037 {
2038 afmName = wxGetDataDir();
2039 afmName << wxFILE_SEP_PATH
2040 #if defined(__LINUX__) || defined(__FREEBSD__)
2041 << wxT("gs_afm") << wxFILE_SEP_PATH
2042 #else
2043 << wxT("afm") << wxFILE_SEP_PATH
2044 #endif
2045 << name;
2046 afmFile = wxFopen(afmName,wxT("r"));
2047 }
2048 #endif
2049
2050 if (afmFile==NULL)
2051 {
2052 wxLogDebug( wxT("GetTextExtent: can't open AFM file '%hs'"), afmName.c_str() );
2053 wxLogDebug( wxT(" using approximate values"));
2054 for (int i=0; i<256; i++) lastWidths[i] = 500; /* an approximate value */
2055 lastDescender = -150; /* dito. */
2056 }
2057 else
2058 {
2059 /* init the widths array */
2060 for(int i=0; i<256; i++) lastWidths[i] = INT_MIN;
2061 /* some variables for holding parts of a line */
2062 char cString[10],semiString[10],WXString[10],descString[20];
2063 char upString[30], utString[30], encString[50];
2064 char line[256];
2065 int ascii,cWidth;
2066 /* read in the file and parse it */
2067 while(fgets(line,sizeof(line),afmFile)!=NULL)
2068 {
2069 /* A.) check for descender definition */
2070 if (strncmp(line,"Descender",9)==0)
2071 {
2072 if ((sscanf(line,"%s%d",descString,&lastDescender)!=2) ||
2073 (strcmp(descString,"Descender")!=0))
2074 {
2075 wxLogDebug( wxT("AFM-file '%hs': line '%hs' has error (bad descender)"), afmName.c_str(),line );
2076 }
2077 }
2078 /* JC 1.) check for UnderlinePosition */
2079 else if(strncmp(line,"UnderlinePosition",17)==0)
2080 {
2081 if ((sscanf(line,"%s%lf",upString,&UnderlinePosition)!=2) ||
2082 (strcmp(upString,"UnderlinePosition")!=0))
2083 {
2084 wxLogDebug( wxT("AFM-file '%hs': line '%hs' has error (bad UnderlinePosition)"), afmName.c_str(), line );
2085 }
2086 }
2087 /* JC 2.) check for UnderlineThickness */
2088 else if(strncmp(line,"UnderlineThickness",18)==0)
2089 {
2090 if ((sscanf(line,"%s%lf",utString,&UnderlineThickness)!=2) ||
2091 (strcmp(utString,"UnderlineThickness")!=0))
2092 {
2093 wxLogDebug( wxT("AFM-file '%hs': line '%hs' has error (bad UnderlineThickness)"), afmName.c_str(), line );
2094 }
2095 }
2096 /* JC 3.) check for EncodingScheme */
2097 else if(strncmp(line,"EncodingScheme",14)==0)
2098 {
2099 if ((sscanf(line,"%s%s",utString,encString)!=2) ||
2100 (strcmp(utString,"EncodingScheme")!=0))
2101 {
2102 wxLogDebug( wxT("AFM-file '%hs': line '%hs' has error (bad EncodingScheme)"), afmName.c_str(), line );
2103 }
2104 else if (strncmp(encString, "AdobeStandardEncoding", 21))
2105 {
2106 wxLogDebug( wxT("AFM-file '%hs': line '%hs' has error (unsupported EncodingScheme %hs)"),
2107 afmName.c_str(),line, encString);
2108 }
2109 }
2110 /* B.) check for char-width */
2111 else if(strncmp(line,"C ",2)==0)
2112 {
2113 if (sscanf(line,"%s%d%s%s%d",cString,&ascii,semiString,WXString,&cWidth)!=5)
2114 {
2115 wxLogDebug(wxT("AFM-file '%hs': line '%hs' has an error (bad character width)"),afmName.c_str(),line);
2116 }
2117 if(strcmp(cString,"C")!=0 || strcmp(semiString,";")!=0 || strcmp(WXString,"WX")!=0)
2118 {
2119 wxLogDebug(wxT("AFM-file '%hs': line '%hs' has a format error"),afmName.c_str(),line);
2120 }
2121 /* printf(" char '%c'=%d has width '%d'\n",ascii,ascii,cWidth); */
2122 if (ascii>=0 && ascii<256)
2123 {
2124 lastWidths[ascii] = cWidth; /* store width */
2125 }
2126 else
2127 {
2128 /* MATTHEW: this happens a lot; don't print an error */
2129 /* wxLogDebug("AFM-file '%s': ASCII value %d out of range",afmName.c_str(),ascii); */
2130 }
2131 }
2132 /* C.) ignore other entries. */
2133 }
2134 fclose(afmFile);
2135 }
2136 /* hack to compute correct values for german 'Umlaute'
2137 / the correct way would be to map the character names
2138 / like 'adieresis' to corresp. positions of ISOEnc and read
2139 / these values from AFM files, too. Maybe later ... */
2140 lastWidths[196] = lastWidths['A']; // Ä
2141 lastWidths[228] = lastWidths['a']; // ä
2142 lastWidths[214] = lastWidths['O']; // Ö
2143 lastWidths[246] = lastWidths['o']; // ö
2144 lastWidths[220] = lastWidths['U']; // Ü
2145 lastWidths[252] = lastWidths['u']; // ü
2146 lastWidths[223] = lastWidths[251]; // ß
2147
2148 /* JC: calculate UnderlineThickness/UnderlinePosition */
2149
2150 // VS: dirty, but is there any better solution?
2151 double *pt;
2152 pt = (double*) &m_underlinePosition;
2153 *pt = LogicalToDeviceYRel((wxCoord)(UnderlinePosition * fontToUse->GetPointSize())) / 1000.0f;
2154 pt = (double*) &m_underlineThickness;
2155 *pt = LogicalToDeviceYRel((wxCoord)(UnderlineThickness * fontToUse->GetPointSize())) / 1000.0f;
2156
2157 }
2158
2159
2160 /* 3. now the font metrics are read in, calc size this
2161 / is done by adding the widths of the characters in the
2162 / string. they are given in 1/1000 of the size! */
2163
2164 long sum=0;
2165 wxCoord height=Size; /* by default */
2166 unsigned char *p;
2167 for(p=(unsigned char *)wxMBSTRINGCAST strbuf; *p; p++)
2168 {
2169 if(lastWidths[*p]== INT_MIN)
2170 {
2171 wxLogDebug(wxT("GetTextExtent: undefined width for character '%hc' (%d)"), *p,*p);
2172 sum += lastWidths[' ']; /* assume space */
2173 }
2174 else
2175 {
2176 sum += lastWidths[*p];
2177 }
2178 }
2179
2180 double widthSum = sum;
2181 widthSum *= Size;
2182 widthSum /= 1000.0F;
2183
2184 /* add descender to height (it is usually a negative value) */
2185 //if (lastDescender != INT_MIN)
2186 //{
2187 // height += (wxCoord)(((-lastDescender)/1000.0F) * Size); /* MATTHEW: forgot scale */
2188 //}
2189 // - commented by V. Slavik - height already contains descender in it
2190 // (judging from few experiments)
2191
2192 /* return size values */
2193 if ( x )
2194 *x = (wxCoord)widthSum;
2195 if ( y )
2196 *y = height;
2197
2198 /* return other parameters */
2199 if (descent)
2200 {
2201 if(lastDescender!=INT_MIN)
2202 {
2203 *descent = (wxCoord)(((-lastDescender)/1000.0F) * Size); /* MATTHEW: forgot scale */
2204 }
2205 else
2206 {
2207 *descent = 0;
2208 }
2209 }
2210
2211 /* currently no idea how to calculate this! */
2212 if (externalLeading) *externalLeading = 0;
2213
2214 #endif
2215 }
2216
2217 // Determine the Default Postscript Previewer
2218 // available on the platform
2219 #if defined(__SUN__) && defined(__XVIEW__)
2220 // OpenWindow/NeWS's Postscript Previewer
2221 # define PS_VIEWER_PROG "pageview"
2222 #elif defined(__VMS__)
2223 #define PS_VIEWER_PROG "view/format=ps/select=x_display"
2224 #elif defined(__SGI__)
2225 // SGI's Display Postscript Previewer
2226 //# define PS_VIEWER_PROG "dps"
2227 # define PS_VIEWER_PROG "xpsview"
2228 #elif defined(__X__) || defined(__WXGTK__)
2229 // Front-end to ghostscript
2230 # define PS_VIEWER_PROG "ghostview"
2231 #else
2232 // Windows ghostscript/ghostview
2233 # define PS_VIEWER_PROG "gsview"
2234 #endif
2235
2236 wxPrintSetupData *wxThePrintSetupData = (wxPrintSetupData *) NULL;
2237
2238 IMPLEMENT_DYNAMIC_CLASS(wxPostScriptDC, wxDC)
2239 IMPLEMENT_DYNAMIC_CLASS(wxPrintSetupData, wxObject)
2240
2241 // Redundant now I think
2242 #if 1
2243 IMPLEMENT_CLASS(wxPostScriptPrintDialog, wxDialog)
2244
2245 wxPostScriptPrintDialog::wxPostScriptPrintDialog (wxWindow *parent, const wxString& title,
2246 const wxPoint& pos, const wxSize& size, wxCoord style):
2247 wxDialog(parent, -1, title, pos, size, style)
2248 {
2249 wxBeginBusyCursor();
2250
2251 char buf[100];
2252 int yPos = 40;
2253 wxString
2254 *orientation = new wxString[2],
2255 *print_modes = new wxString[3];
2256 int features;
2257 wxCoord wx_printer_translate_x, wx_printer_translate_y;
2258 double wx_printer_scale_x, wx_printer_scale_y;
2259
2260 orientation[0] = _("Portrait");
2261 orientation[1] = _("Landscape");
2262
2263 print_modes[0] = _("Send to Printer");
2264 print_modes[1] = _("Print to File");
2265 print_modes[2] = _("Preview Only");
2266
2267
2268
2269 wxButton *okBut = new wxButton (this, wxID_OK, _("OK"), wxPoint(5, 5));
2270 (void) new wxButton (this, wxID_CANCEL, _("Cancel"), wxPoint(40, 5));
2271 okBut->SetDefault();
2272
2273
2274 #if defined(__WXGTK__) || defined (__WXMOTIF__)
2275 (void) new wxStaticText( this, -1, _("Printer Command: "),
2276 wxPoint(5, yPos) );
2277 (void) new wxTextCtrl( this, wxID_PRINTER_COMMAND, wxThePrintSetupData->GetPrinterCommand(),
2278 wxPoint(100, yPos), wxSize(100, -1) );
2279
2280 (void) new wxStaticText( this, -1, _("Printer Options: "),
2281 wxPoint(210, yPos) );
2282 (void) new wxTextCtrl( this, wxID_PRINTER_OPTIONS, wxThePrintSetupData->GetPrinterOptions(),
2283 wxPoint(305, yPos), wxSize(150, -1) );
2284
2285 yPos += 40;
2286 #endif
2287
2288
2289 wxRadioBox *radio0 = new wxRadioBox(this, wxID_PRINTER_ORIENTATION, "Orientation: ", wxPoint(5, yPos), wxSize(-1,-1),
2290 2,orientation,2,wxRA_SPECIFY_ROWS);
2291 radio0->SetSelection((int)wxThePrintSetupData->GetPrinterOrientation() - 1);
2292
2293 // @@@ Configuration hook
2294 if ( !wxThePrintSetupData->GetPrintPreviewCommand() )
2295 wxThePrintSetupData->SetPrintPreviewCommand(PS_VIEWER_PROG);
2296
2297 // wxGetResource ("wxWindows", "PSView", &wxThePrintSetupData->m_previewCommand);
2298
2299 features = (wxThePrintSetupData->GetPrintPreviewCommand() &&
2300 *wxThePrintSetupData->GetPrintPreviewCommand()) ? 3 : 2;
2301
2302 wxRadioBox *radio1 = new wxRadioBox(this, wxID_PRINTER_MODES, _("PostScript:"),
2303 wxPoint(150, yPos),
2304 wxSize(-1,-1), features,
2305 print_modes, features, wxRA_SPECIFY_ROWS);
2306
2307 #ifdef __WXMSW__
2308 radio1->Enable(0, FALSE);
2309 if (wxThePrintSetupData->GetPrintPreviewCommand() && *wxThePrintSetupData->GetPrintPreviewCommand())
2310 radio1->Enable(2, FALSE);
2311 #endif
2312
2313 radio1->SetSelection((int)wxThePrintSetupData->GetPrinterMode());
2314 wxThePrintSetupData->GetPrinterTranslation(&wx_printer_translate_x, &wx_printer_translate_y);
2315 wxThePrintSetupData->GetPrinterScaling(&wx_printer_scale_x, &wx_printer_scale_y);
2316
2317 sprintf (buf, "%.2f", wx_printer_scale_x);
2318
2319 yPos += 90;
2320 (void) new wxStaticText(this, -1, _("X Scaling"), wxPoint(5, yPos));
2321 /* wxTextCtrl *text1 = */ (void) new wxTextCtrl(this, wxID_PRINTER_X_SCALE, buf, wxPoint(100, yPos), wxSize(100, -1));
2322
2323 sprintf (buf, "%.2f", wx_printer_scale_y);
2324 (void) new wxStaticText(this, -1, _("Y Scaling"), wxPoint(220, yPos));
2325 /* wxTextCtrl *text2 = */ (void) new wxTextCtrl(this, wxID_PRINTER_Y_SCALE, buf, wxPoint(320, yPos), wxSize(100, -1));
2326
2327 yPos += 25;
2328
2329 (void) new wxStaticText(this, -1, _("X Translation"), wxPoint(5, yPos));
2330 sprintf (buf, "%.2d", wx_printer_translate_x);
2331 /* wxTextCtrl *text3 = */ (void) new wxTextCtrl(this, wxID_PRINTER_X_TRANS, buf, wxPoint(100, yPos), wxSize(100, -1));
2332
2333 (void) new wxStaticText(this, -1, _("Y Translation"), wxPoint(220, yPos));
2334 sprintf (buf, "%.2d", wx_printer_translate_y);
2335 /* wxTextCtrl *text4 = */ (void) new wxTextCtrl(this, wxID_PRINTER_Y_TRANS, buf, wxPoint(320, yPos), wxSize(100, -1));
2336
2337 Fit ();
2338
2339 delete[] orientation;
2340 delete[] print_modes;
2341
2342 wxEndBusyCursor();
2343 }
2344
2345 int wxPostScriptPrintDialog::ShowModal ()
2346 {
2347 if ( wxDialog::ShowModal() == wxID_OK )
2348 {
2349 // wxTextCtrl *text0 = (wxTextCtrl *)FindWindow(wxID_PRINTER_OPTIONS);
2350 wxTextCtrl *text1 = (wxTextCtrl *)FindWindow(wxID_PRINTER_X_SCALE);
2351 wxTextCtrl *text2 = (wxTextCtrl *)FindWindow(wxID_PRINTER_Y_SCALE);
2352 wxTextCtrl *text3 = (wxTextCtrl *)FindWindow(wxID_PRINTER_X_TRANS);
2353 wxTextCtrl *text4 = (wxTextCtrl *)FindWindow(wxID_PRINTER_Y_TRANS);
2354 // wxTextCtrl *text_prt = (wxTextCtrl *)FindWindow(wxID_PRINTER_COMMAND);
2355 wxRadioBox *radio0 = (wxRadioBox *)FindWindow(wxID_PRINTER_ORIENTATION);
2356 wxRadioBox *radio1 = (wxRadioBox *)FindWindow(wxID_PRINTER_MODES);
2357
2358 StringToDouble (WXSTRINGCAST text1->GetValue (), &wxThePrintSetupData->m_printerScaleX);
2359 StringToDouble (WXSTRINGCAST text2->GetValue (), &wxThePrintSetupData->m_printerScaleY);
2360
2361 long dummy;
2362 StringToLong (WXSTRINGCAST text3->GetValue (), &dummy);
2363 wxThePrintSetupData->m_printerTranslateX = (wxCoord)dummy;
2364 StringToLong (WXSTRINGCAST text4->GetValue (), &dummy);
2365 wxThePrintSetupData->m_printerTranslateY = (wxCoord)dummy;
2366
2367 #ifdef __X__
2368 // wxThePrintSetupData->SetPrinterOptions(WXSTRINGCAST text0->GetValue ());
2369 // wxThePrintSetupData->SetPrinterCommand(WXSTRINGCAST text_prt->GetValue ());
2370 #endif
2371
2372 wxThePrintSetupData->SetPrinterOrientation((radio0->GetSelection() == 1 ? wxLANDSCAPE : wxPORTRAIT));
2373
2374 // C++ wants this
2375 switch ( radio1->GetSelection() ) {
2376 case 0: wxThePrintSetupData->SetPrinterMode(PS_PRINTER); break;
2377 case 1: wxThePrintSetupData->SetPrinterMode(PS_FILE); break;
2378 case 2: wxThePrintSetupData->SetPrinterMode(PS_PREVIEW); break;
2379 }
2380 return wxID_OK;
2381 }
2382 return wxID_CANCEL;
2383 }
2384 #endif
2385 // 0 (redundant)
2386
2387 // PostScript printer settings
2388 // RETAINED FOR BACKWARD COMPATIBILITY
2389 void wxSetPrinterCommand(const wxString& cmd)
2390 {
2391 wxThePrintSetupData->SetPrinterCommand(cmd);
2392 }
2393
2394 void wxSetPrintPreviewCommand(const wxString& cmd)
2395 {
2396 wxThePrintSetupData->SetPrintPreviewCommand(cmd);
2397 }
2398
2399 void wxSetPrinterOptions(const wxString& flags)
2400 {
2401 wxThePrintSetupData->SetPrinterOptions(flags);
2402 }
2403
2404 void wxSetPrinterFile(const wxString& f)
2405 {
2406 wxThePrintSetupData->SetPrinterFile(f);
2407 }
2408
2409 void wxSetPrinterOrientation(int orient)
2410 {
2411 wxThePrintSetupData->SetPrinterOrientation(orient);
2412 }
2413
2414 void wxSetPrinterScaling(double x, double y)
2415 {
2416 wxThePrintSetupData->SetPrinterScaling(x, y);
2417 }
2418
2419 void wxSetPrinterTranslation(wxCoord x, wxCoord y)
2420 {
2421 wxThePrintSetupData->SetPrinterTranslation(x, y);
2422 }
2423
2424 // 1 = Preview, 2 = print to file, 3 = send to printer
2425 void wxSetPrinterMode(int mode)
2426 {
2427 wxThePrintSetupData->SetPrinterMode(mode);
2428 }
2429
2430 void wxSetAFMPath(const wxString& f)
2431 {
2432 wxThePrintSetupData->SetAFMPath(f);
2433 }
2434
2435 // Get current values
2436 wxString wxGetPrinterCommand()
2437 {
2438 return wxThePrintSetupData->GetPrinterCommand();
2439 }
2440
2441 wxString wxGetPrintPreviewCommand()
2442 {
2443 return wxThePrintSetupData->GetPrintPreviewCommand();
2444 }
2445
2446 wxString wxGetPrinterOptions()
2447 {
2448 return wxThePrintSetupData->GetPrinterOptions();
2449 }
2450
2451 wxString wxGetPrinterFile()
2452 {
2453 return wxThePrintSetupData->GetPrinterFile();
2454 }
2455
2456 int wxGetPrinterOrientation()
2457 {
2458 return wxThePrintSetupData->GetPrinterOrientation();
2459 }
2460
2461 void wxGetPrinterScaling(double* x, double* y)
2462 {
2463 wxThePrintSetupData->GetPrinterScaling(x, y);
2464 }
2465
2466 void wxGetPrinterTranslation(wxCoord *x, wxCoord *y)
2467 {
2468 wxThePrintSetupData->GetPrinterTranslation(x, y);
2469 }
2470
2471 int wxGetPrinterMode()
2472 {
2473 return wxThePrintSetupData->GetPrinterMode();
2474 }
2475
2476 wxString wxGetAFMPath()
2477 {
2478 return wxThePrintSetupData->GetAFMPath();
2479 }
2480
2481 /*
2482 * Print setup data
2483 */
2484
2485 wxPrintSetupData::wxPrintSetupData()
2486 {
2487 m_printerOrient = wxPORTRAIT;
2488 m_printerScaleX = (double)1.0;
2489 m_printerScaleY = (double)1.0;
2490 m_printerTranslateX = 0;
2491 m_printerTranslateY = 0;
2492 m_printerMode = wxPRINT_MODE_FILE;
2493 m_printColour = TRUE;
2494 }
2495
2496 wxPrintSetupData::~wxPrintSetupData()
2497 {
2498 }
2499
2500 void wxPrintSetupData::operator=(wxPrintSetupData& data)
2501 {
2502 SetPrinterCommand(data.GetPrinterCommand());
2503 SetPrintPreviewCommand(data.GetPrintPreviewCommand());
2504 SetPrinterOptions(data.GetPrinterOptions());
2505 wxCoord x, y;
2506 data.GetPrinterTranslation(&x, &y);
2507 SetPrinterTranslation(x, y);
2508
2509 double x1, y1;
2510 data.GetPrinterScaling(&x1, &y1);
2511 SetPrinterScaling(x1, y1);
2512
2513 SetPrinterOrientation(data.GetPrinterOrientation());
2514 SetPrinterMode(data.GetPrinterMode());
2515 SetAFMPath(data.GetAFMPath());
2516 SetPaperName(data.GetPaperName());
2517 SetColour(data.GetColour());
2518 }
2519
2520 // Initialize from a wxPrintData object (wxPrintData should now be used instead of wxPrintSetupData).
2521 void wxPrintSetupData::operator=(const wxPrintData& data)
2522 {
2523 SetPrinterCommand(data.GetPrinterCommand());
2524 SetPrintPreviewCommand(data.GetPreviewCommand());
2525 SetPrinterOptions(data.GetPrinterOptions());
2526 SetPrinterTranslation((wxCoord)data.GetPrinterTranslateX(),
2527 (wxCoord)data.GetPrinterTranslateY());
2528 SetPrinterScaling(data.GetPrinterScaleX(), data.GetPrinterScaleY());
2529 SetPrinterOrientation(data.GetOrientation());
2530 SetPrinterMode((int) data.GetPrintMode());
2531 SetAFMPath(data.GetFontMetricPath());
2532 SetPaperName(wxThePrintPaperDatabase->ConvertIdToName(data.GetPaperId()));
2533 SetColour(data.GetColour());
2534 SetPrinterFile(data.GetFilename());
2535 }
2536
2537 void wxInitializePrintSetupData(bool init)
2538 {
2539 if (init)
2540 {
2541 wxThePrintSetupData = new wxPrintSetupData;
2542
2543 wxThePrintSetupData->SetPrintPreviewCommand(PS_VIEWER_PROG);
2544 wxThePrintSetupData->SetPrinterOrientation(wxPORTRAIT);
2545 wxThePrintSetupData->SetPrinterMode(wxPRINT_MODE_PREVIEW);
2546 wxThePrintSetupData->SetPaperName(_("A4 sheet, 210 x 297 mm"));
2547
2548 // Could have a .ini file to read in some defaults
2549 // - and/or use environment variables, e.g. WXWIN
2550 #ifdef __VMS__
2551 wxThePrintSetupData->SetPrinterCommand("print");
2552 wxThePrintSetupData->SetPrinterOptions("/nonotify/queue=psqueue");
2553 wxThePrintSetupData->SetAFMPath("sys$ps_font_metrics:");
2554 #endif
2555 #ifdef __WXMSW__
2556 wxThePrintSetupData->SetPrinterCommand("print");
2557 wxThePrintSetupData->SetAFMPath("c:\\windows\\system\\");
2558 wxThePrintSetupData->SetPrinterOptions("");
2559 #endif
2560 #if !defined(__VMS__) && !defined(__WXMSW__)
2561 wxThePrintSetupData->SetPrinterCommand("lpr");
2562 wxThePrintSetupData->SetPrinterOptions("");
2563 wxThePrintSetupData->SetAFMPath("");
2564 #endif
2565 }
2566 else
2567 {
2568 if (wxThePrintSetupData)
2569 delete wxThePrintSetupData;
2570 wxThePrintSetupData = (wxPrintSetupData *) NULL;
2571 }
2572 }
2573
2574 // A module to allow initialization/cleanup of PostScript-related
2575 // things without calling these functions from app.cpp.
2576
2577 class WXDLLEXPORT wxPostScriptModule: public wxModule
2578 {
2579 DECLARE_DYNAMIC_CLASS(wxPostScriptModule)
2580 public:
2581 wxPostScriptModule() {}
2582 bool OnInit();
2583 void OnExit();
2584 };
2585
2586 IMPLEMENT_DYNAMIC_CLASS(wxPostScriptModule, wxModule)
2587
2588 /*
2589 * Initialization/cleanup module
2590 */
2591
2592 bool wxPostScriptModule::OnInit()
2593 {
2594 wxInitializePrintSetupData();
2595
2596 return TRUE;
2597 }
2598
2599 void wxPostScriptModule::OnExit()
2600 {
2601 wxInitializePrintSetupData(FALSE);
2602 }
2603
2604 #endif
2605 // wxUSE_POSTSCRIPT
2606
2607 #endif
2608 // wxUSE_PRINTING_ARCHITECTURE