]> git.saurik.com Git - wxWidgets.git/blame - src/generic/dcpsg.cpp
SN: Replaced __WXOS2__ by __WXPM__ in #ifdefs
[wxWidgets.git] / src / generic / dcpsg.cpp
CommitLineData
ed880dd4 1/////////////////////////////////////////////////////////////////////////////
bf38cbff
JS
2// Name: dcpsg.cpp
3// Purpose: Generic wxPostScriptDC implementation
ed880dd4
RR
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
9a6be59a 9// Licence: wxWindows licence
ed880dd4
RR
10/////////////////////////////////////////////////////////////////////////////
11
12#ifdef __GNUG__
ed880dd4
RR
13#pragma implementation
14#pragma interface
15#endif
16
9a6be59a
VZ
17#include "wx/wxprec.h"
18
19#ifdef __BORLANDC__
20 #pragma hdrstop
21#endif
22
23#ifndef WX_PRECOMP
24 #include "wx/setup.h"
25 #include "wx/defs.h"
26#endif // WX_PRECOMP
06cfab17 27
ce4169a4
RR
28#if wxUSE_PRINTING_ARCHITECTURE
29
06cfab17
RR
30#if wxUSE_POSTSCRIPT
31
e6f1ad22 32#include "wx/window.h"
f04371f0 33#include "wx/dcmemory.h"
ed880dd4 34#include "wx/utils.h"
f04371f0 35#include "wx/intl.h"
ed880dd4 36#include "wx/filedlg.h"
ed880dd4 37#include "wx/app.h"
f04371f0 38#include "wx/msgdlg.h"
ef539066 39#include "wx/image.h"
3069ac4e 40#include "wx/log.h"
bf38cbff
JS
41#include "wx/generic/dcpsg.h"
42#include "wx/generic/prntdlgg.h"
43#include "wx/button.h"
44#include "wx/stattext.h"
45#include "wx/radiobox.h"
46#include "wx/textctrl.h"
9a05fd8d 47#include "wx/prntbase.h"
7bcb11d3 48#include "wx/paper.h"
d361e74e 49#include "wx/filefn.h"
ed880dd4 50
57c208c5
JS
51#include <math.h>
52
bf38cbff
JS
53#ifdef __WXMSW__
54
55#ifdef DrawText
56#undef DrawText
57#endif
58
59#ifdef StartDoc
60#undef StartDoc
61#endif
62
63#ifdef GetCharWidth
64#undef GetCharWidth
65#endif
66
67#ifdef FindWindow
68#undef FindWindow
69#endif
70
71#endif
72
ed880dd4
RR
73//-----------------------------------------------------------------------------
74// start and end of document/page
75//-----------------------------------------------------------------------------
76
77static const char *wxPostScriptHeaderEllipse = "\
78/ellipsedict 8 dict def\n\
79ellipsedict /mtrx matrix put\n\
80/ellipse {\n\
9a6be59a
VZ
81 ellipsedict begin\n\
82 /endangle exch def\n\
83 /startangle exch def\n\
84 /yrad exch def\n\
85 /xrad exch def\n\
86 /y exch def\n\
87 /x exch def\n\
88 /savematrix mtrx currentmatrix def\n\
89 x y translate\n\
90 xrad yrad scale\n\
91 0 0 1 startangle endangle arc\n\
92 savematrix setmatrix\n\
93 end\n\
94 } def\n\
ed880dd4
RR
95";
96
97static const char *wxPostScriptHeaderEllipticArc= "\
98/ellipticarcdict 8 dict def\n\
99ellipticarcdict /mtrx matrix put\n\
100/ellipticarc\n\
101{ ellipticarcdict begin\n\
102 /do_fill exch def\n\
103 /endangle exch def\n\
104 /startangle exch def\n\
105 /yrad exch def\n\
106 /xrad exch def \n\
107 /y exch def\n\
108 /x exch def\n\
109 /savematrix mtrx currentmatrix def\n\
110 x y translate\n\
111 xrad yrad scale\n\
112 do_fill { 0 0 moveto } if\n\
113 0 0 1 startangle endangle arc\n\
114 savematrix setmatrix\n\
115 do_fill { fill }{ stroke } ifelse\n\
116 end\n\
117} def\n";
118
119static const char *wxPostScriptHeaderSpline = "\
120/DrawSplineSection {\n\
9a6be59a
VZ
121 /y3 exch def\n\
122 /x3 exch def\n\
123 /y2 exch def\n\
124 /x2 exch def\n\
125 /y1 exch def\n\
126 /x1 exch def\n\
127 /xa x1 x2 x1 sub 0.666667 mul add def\n\
128 /ya y1 y2 y1 sub 0.666667 mul add def\n\
129 /xb x3 x2 x3 sub 0.666667 mul add def\n\
130 /yb y3 y2 y3 sub 0.666667 mul add def\n\
131 x1 y1 lineto\n\
132 xa ya xb yb x3 y3 curveto\n\
133 } def\n\
ed880dd4
RR
134";
135
136static const char *wxPostScriptHeaderColourImage = "\
d7657f75
RR
137%% define 'colorimage' if it isn't defined\n\
138%% ('colortogray' and 'mergeprocs' come from xwd2ps\n\
139%% via xgrab)\n\
140/colorimage where %% do we know about 'colorimage'?\n\
141 { pop } %% yes: pop off the 'dict' returned\n\
142 { %% no: define one\n\
143 /colortogray { %% define an RGB->I function\n\
144 /rgbdata exch store %% call input 'rgbdata'\n\
ed880dd4
RR
145 rgbdata length 3 idiv\n\
146 /npixls exch store\n\
147 /rgbindx 0 store\n\
148 0 1 npixls 1 sub {\n\
149 grays exch\n\
d7657f75
RR
150 rgbdata rgbindx get 20 mul %% Red\n\
151 rgbdata rgbindx 1 add get 32 mul %% Green\n\
152 rgbdata rgbindx 2 add get 12 mul %% Blue\n\
153 add add 64 idiv %% I = .5G + .31R + .18B\n\
ed880dd4
RR
154 put\n\
155 /rgbindx rgbindx 3 add store\n\
156 } for\n\
157 grays 0 npixls getinterval\n\
158 } bind def\n\
159\n\
d7657f75
RR
160 %% Utility procedure for colorimage operator.\n\
161 %% This procedure takes two procedures off the\n\
162 %% stack and merges them into a single procedure.\n\
ed880dd4 163\n\
d7657f75 164 /mergeprocs { %% def\n\
ed880dd4
RR
165 dup length\n\
166 3 -1 roll\n\
167 dup\n\
168 length\n\
169 dup\n\
170 5 1 roll\n\
171 3 -1 roll\n\
172 add\n\
173 array cvx\n\
174 dup\n\
175 3 -1 roll\n\
176 0 exch\n\
177 putinterval\n\
178 dup\n\
179 4 2 roll\n\
180 putinterval\n\
181 } bind def\n\
182\n\
d7657f75
RR
183 /colorimage { %% def\n\
184 pop pop %% remove 'false 3' operands\n\
ed880dd4
RR
185 {colortogray} mergeprocs\n\
186 image\n\
187 } bind def\n\
d7657f75 188 } ifelse %% end of 'false' case\n\
ed880dd4
RR
189";
190
191static char wxPostScriptHeaderReencodeISO1[] =
192 "\n/reencodeISO {\n"
193"dup dup findfont dup length dict begin\n"
194"{ 1 index /FID ne { def }{ pop pop } ifelse } forall\n"
195"/Encoding ISOLatin1Encoding def\n"
196"currentdict end definefont\n"
197"} def\n"
198"/ISOLatin1Encoding [\n"
199"/.notdef/.notdef/.notdef/.notdef/.notdef/.notdef/.notdef/.notdef\n"
200"/.notdef/.notdef/.notdef/.notdef/.notdef/.notdef/.notdef/.notdef\n"
201"/.notdef/.notdef/.notdef/.notdef/.notdef/.notdef/.notdef/.notdef\n"
202"/.notdef/.notdef/.notdef/.notdef/.notdef/.notdef/.notdef/.notdef\n"
203"/space/exclam/quotedbl/numbersign/dollar/percent/ampersand/quoteright\n"
204"/parenleft/parenright/asterisk/plus/comma/minus/period/slash\n"
205"/zero/one/two/three/four/five/six/seven/eight/nine/colon/semicolon\n"
206"/less/equal/greater/question/at/A/B/C/D/E/F/G/H/I/J/K/L/M/N\n"
207"/O/P/Q/R/S/T/U/V/W/X/Y/Z/bracketleft/backslash/bracketright\n"
208"/asciicircum/underscore/quoteleft/a/b/c/d/e/f/g/h/i/j/k/l/m\n"
209"/n/o/p/q/r/s/t/u/v/w/x/y/z/braceleft/bar/braceright/asciitilde\n"
210"/.notdef/.notdef/.notdef/.notdef/.notdef/.notdef/.notdef/.notdef\n"
211"/.notdef/.notdef/.notdef/.notdef/.notdef/.notdef/.notdef/.notdef\n"
212"/.notdef/dotlessi/grave/acute/circumflex/tilde/macron/breve\n"
213"/dotaccent/dieresis/.notdef/ring/cedilla/.notdef/hungarumlaut\n";
214
215static char wxPostScriptHeaderReencodeISO2[] =
216"/ogonek/caron/space/exclamdown/cent/sterling/currency/yen/brokenbar\n"
217"/section/dieresis/copyright/ordfeminine/guillemotleft/logicalnot\n"
218"/hyphen/registered/macron/degree/plusminus/twosuperior/threesuperior\n"
219"/acute/mu/paragraph/periodcentered/cedilla/onesuperior/ordmasculine\n"
220"/guillemotright/onequarter/onehalf/threequarters/questiondown\n"
221"/Agrave/Aacute/Acircumflex/Atilde/Adieresis/Aring/AE/Ccedilla\n"
222"/Egrave/Eacute/Ecircumflex/Edieresis/Igrave/Iacute/Icircumflex\n"
223"/Idieresis/Eth/Ntilde/Ograve/Oacute/Ocircumflex/Otilde/Odieresis\n"
224"/multiply/Oslash/Ugrave/Uacute/Ucircumflex/Udieresis/Yacute\n"
225"/Thorn/germandbls/agrave/aacute/acircumflex/atilde/adieresis\n"
226"/aring/ae/ccedilla/egrave/eacute/ecircumflex/edieresis/igrave\n"
227"/iacute/icircumflex/idieresis/eth/ntilde/ograve/oacute/ocircumflex\n"
228"/otilde/odieresis/divide/oslash/ugrave/uacute/ucircumflex/udieresis\n"
229"/yacute/thorn/ydieresis\n"
230 "] def\n\n";
231
ed880dd4
RR
232//-------------------------------------------------------------------------------
233// wxPostScriptDC
234//-------------------------------------------------------------------------------
235
236wxPostScriptDC::wxPostScriptDC ()
237{
d7657f75 238 m_pstream = (FILE*) NULL;
afce4c03 239
ed880dd4
RR
240 m_currentRed = 0;
241 m_currentGreen = 0;
242 m_currentBlue = 0;
afce4c03 243
ed880dd4 244 m_pageNumber = 0;
afce4c03 245
ed880dd4 246 m_clipping = FALSE;
afce4c03 247
ed880dd4
RR
248 m_underlinePosition = 0.0;
249 m_underlineThickness = 0.0;
250
251 m_signX = 1; // default x-axis left to right
252 m_signY = -1; // default y-axis bottom up -> top down
7bcb11d3
JS
253
254 // Compatibility only
e6f1ad22
HH
255 // HH: Doesn't seem to work for wxMSW...
256 #ifndef __WXMSW__
7bcb11d3 257 m_printData = * wxThePrintSetupData;
e6f1ad22 258 #endif
ed880dd4
RR
259}
260
261wxPostScriptDC::wxPostScriptDC (const wxString& file, bool interactive, wxWindow *parent)
262{
d7657f75 263 m_pstream = (FILE*) NULL;
afce4c03 264
ed880dd4
RR
265 m_currentRed = 0;
266 m_currentGreen = 0;
267 m_currentBlue = 0;
afce4c03 268
ed880dd4 269 m_pageNumber = 0;
afce4c03 270
ed880dd4 271 m_clipping = FALSE;
afce4c03 272
ed880dd4
RR
273 m_underlinePosition = 0.0;
274 m_underlineThickness = 0.0;
275
276 m_signX = 1; // default x-axis left to right
277 m_signY = -1; // default y-axis bottom up -> top down
278
279 Create(file, interactive, parent);
280}
281
282bool wxPostScriptDC::Create(const wxString& file, bool interactive, wxWindow *parent)
283{
284 m_isInteractive = interactive;
285
286 m_title = "";
7bcb11d3 287 m_printData.SetFilename(file);
ed880dd4
RR
288
289#ifdef __WXMSW__
290 // Can only send to file in Windows
7bcb11d3 291 m_printData.SetPrintMode(wxPRINT_MODE_FILE);
ed880dd4
RR
292#endif
293
294 if (m_isInteractive)
295 {
296 if ((m_ok = PrinterDialog (parent) ) == FALSE) return FALSE;
297 }
298 else
299 {
300 m_ok = TRUE;
301 }
afce4c03 302
ed880dd4
RR
303 return m_ok;
304}
305
7bcb11d3
JS
306wxPostScriptDC::wxPostScriptDC (const wxPrintData& printData)
307{
d7657f75 308 m_pstream = (FILE*) NULL;
7bcb11d3
JS
309
310 m_currentRed = 0;
311 m_currentGreen = 0;
312 m_currentBlue = 0;
313
314 m_pageNumber = 0;
315
316 m_clipping = FALSE;
317
318 m_underlinePosition = 0.0;
319 m_underlineThickness = 0.0;
320
321 m_signX = 1; // default x-axis left to right
322 m_signY = -1; // default y-axis bottom up -> top down
323
324 m_printData = printData;
75737d05
JS
325
326 m_ok = TRUE;
7bcb11d3
JS
327}
328
ed880dd4
RR
329wxPostScriptDC::~wxPostScriptDC ()
330{
d7657f75
RR
331 if (m_pstream)
332 {
333 fclose( m_pstream );
72cdf4c9 334 m_pstream = (FILE*) NULL;
d7657f75 335 }
ed880dd4
RR
336}
337
4bc67cc5
RR
338bool wxPostScriptDC::Ok() const
339{
ef539066 340 return m_ok;
4bc67cc5 341}
afce4c03 342
7bcb11d3 343// This dialog is deprecated now: use wxGenericPrintDialog or the printing framework
ed880dd4
RR
344bool wxPostScriptDC::PrinterDialog(wxWindow *parent)
345{
afce4c03 346 wxPostScriptPrintDialog dialog( parent, _("Printer Settings"), wxPoint(150, 150), wxSize(400, 400),
ed880dd4
RR
347 wxDEFAULT_DIALOG_STYLE | wxDIALOG_MODAL );
348 m_ok = (dialog.ShowModal () == wxID_OK);
349
350 if (!m_ok) return FALSE;
351
7bcb11d3
JS
352 if ((m_printData.GetFilename() == "") &&
353 (m_printData.GetPrintMode() == wxPRINT_MODE_PREVIEW ||
354 m_printData.GetPrintMode() == wxPRINT_MODE_PRINTER))
ed880dd4
RR
355 {
356// steve, 05.09.94
357#ifdef __VMS__
7bcb11d3 358 m_printData.SetFilename("preview");
ed880dd4
RR
359#else
360 // For PS_PRINTER action this depends on a Unix-style print spooler
361 // since the wx_printer_file can be destroyed during a session
362 // @@@ TODO: a Windows-style answer for non-Unix
87138c52 363 wxChar userId[256];
ed880dd4 364 wxGetUserId (userId, sizeof (userId) / sizeof (char));
87138c52 365 wxChar tmp[256];
223d09f6 366 wxStrcpy (tmp, wxT("/tmp/preview_"));
87138c52 367 wxStrcat (tmp, userId);
7bcb11d3 368 m_printData.SetFilename(tmp);
ed880dd4 369#endif
87138c52
OK
370 wxChar tmp2[256];
371 wxStrcpy(tmp2, m_printData.GetFilename());
223d09f6 372 wxStrcat (tmp2, wxT(".ps"));
7bcb11d3 373 m_printData.SetFilename(tmp2);
ed880dd4 374 }
223d09f6 375 else if ((m_printData.GetFilename() == wxT("")) && (m_printData.GetPrintMode() == wxPRINT_MODE_FILE))
ed880dd4 376 {
223d09f6 377 wxString file = wxSaveFileSelector (_("PostScript"), wxT("ps"));
ba681060 378 if ( file.IsEmpty() )
ed880dd4
RR
379 {
380 m_ok = FALSE;
381 return FALSE;
382 }
ba681060 383
7bcb11d3 384 m_printData.SetFilename(file);
ed880dd4
RR
385 m_ok = TRUE;
386 }
387
4bc67cc5 388 return m_ok;
ed880dd4
RR
389}
390
72cdf4c9 391void wxPostScriptDC::DoSetClippingRegion (wxCoord x, wxCoord y, wxCoord w, wxCoord h)
ed880dd4 392{
223d09f6 393 wxCHECK_RET( m_ok && m_pstream, wxT("invalid postscript dc") );
afce4c03 394
7087444f 395 if (m_clipping) DestroyClippingRegion();
ed880dd4 396
7087444f 397 wxDC::DoSetClippingRegion(x, y, w, h);
afce4c03 398
ed880dd4 399 m_clipping = TRUE;
72cdf4c9 400 fprintf( m_pstream,
d7657f75 401 "gsave\n newpath\n"
72cdf4c9
VZ
402 "%d %d moveto\n"
403 "%d %d lineto\n"
404 "%d %d lineto\n"
405 "%d %d lineto\n"
406 "closepath clip newpath\n",
407 XLOG2DEV(x), YLOG2DEV(y),
408 XLOG2DEV(x+w), YLOG2DEV(y),
409 XLOG2DEV(x+w), YLOG2DEV(y+h),
410 XLOG2DEV(x), YLOG2DEV(y+h) );
ed880dd4
RR
411}
412
ed880dd4
RR
413
414void wxPostScriptDC::DestroyClippingRegion()
415{
223d09f6 416 wxCHECK_RET( m_ok && m_pstream, wxT("invalid postscript dc") );
afce4c03 417
ed880dd4
RR
418 if (m_clipping)
419 {
420 m_clipping = FALSE;
72cdf4c9 421 fprintf( m_pstream, "grestore\n" );
ed880dd4 422 }
004fd0c8 423
7087444f 424 wxDC::DestroyClippingRegion();
ed880dd4
RR
425}
426
427void wxPostScriptDC::Clear()
428{
223d09f6 429 wxFAIL_MSG( wxT("wxPostScriptDC::Clear not implemented.") );
ed880dd4
RR
430}
431
72cdf4c9 432void wxPostScriptDC::DoFloodFill (wxCoord WXUNUSED(x), wxCoord WXUNUSED(y), const wxColour &WXUNUSED(col), int WXUNUSED(style))
ed880dd4 433{
223d09f6 434 wxFAIL_MSG( wxT("wxPostScriptDC::FloodFill not implemented.") );
ed880dd4
RR
435}
436
72cdf4c9 437bool wxPostScriptDC::DoGetPixel (wxCoord WXUNUSED(x), wxCoord WXUNUSED(y), wxColour * WXUNUSED(col)) const
ed880dd4 438{
223d09f6 439 wxFAIL_MSG( wxT("wxPostScriptDC::GetPixel not implemented.") );
ed880dd4
RR
440 return FALSE;
441}
442
72cdf4c9 443void wxPostScriptDC::DoCrossHair (wxCoord WXUNUSED(x), wxCoord WXUNUSED(y))
ed880dd4 444{
223d09f6 445 wxFAIL_MSG( wxT("wxPostScriptDC::CrossHair not implemented.") );
ed880dd4
RR
446}
447
72cdf4c9 448void wxPostScriptDC::DoDrawLine (wxCoord x1, wxCoord y1, wxCoord x2, wxCoord y2)
ed880dd4 449{
223d09f6 450 wxCHECK_RET( m_ok && m_pstream, wxT("invalid postscript dc") );
afce4c03 451
ed880dd4 452 if (m_pen.GetStyle() == wxTRANSPARENT) return;
afce4c03 453
ed880dd4 454 SetPen( m_pen );
afce4c03 455
d7657f75
RR
456 fprintf( m_pstream,
457 "newpath\n"
72cdf4c9
VZ
458 "%d %d moveto\n"
459 "%d %d lineto\n"
460 "stroke\n",
461 XLOG2DEV(x1), YLOG2DEV(y1),
462 XLOG2DEV(x2), YLOG2DEV (y2) );
afce4c03 463
ed880dd4
RR
464 CalcBoundingBox( x1, y1 );
465 CalcBoundingBox( x2, y2 );
466}
467
468#define RAD2DEG 57.29577951308
469
72cdf4c9 470void wxPostScriptDC::DoDrawArc (wxCoord x1, wxCoord y1, wxCoord x2, wxCoord y2, wxCoord xc, wxCoord yc)
ed880dd4 471{
223d09f6 472 wxCHECK_RET( m_ok && m_pstream, wxT("invalid postscript dc") );
afce4c03 473
72cdf4c9
VZ
474 wxCoord dx = x1 - xc;
475 wxCoord dy = y1 - yc;
476 wxCoord radius = (wxCoord) sqrt( (double)(dx*dx+dy*dy) );
ed880dd4
RR
477 double alpha1, alpha2;
478
afce4c03 479 if (x1 == x2 && y1 == y2)
ed880dd4 480 {
d7657f75
RR
481 alpha1 = 0.0;
482 alpha2 = 360.0;
72cdf4c9 483 }
d7657f75 484 else if (radius == 0.0)
ed880dd4 485 {
d7657f75 486 alpha1 = alpha2 = 0.0;
72cdf4c9 487 }
d7657f75 488 else
ed880dd4 489 {
d7657f75
RR
490 alpha1 = (x1 - xc == 0) ?
491 (y1 - yc < 0) ? 90.0 : -90.0 :
492 -atan2(double(y1-yc), double(x1-xc)) * RAD2DEG;
493 alpha2 = (x2 - xc == 0) ?
494 (y2 - yc < 0) ? 90.0 : -90.0 :
495 -atan2(double(y2-yc), double(x2-xc)) * RAD2DEG;
ed880dd4
RR
496 }
497 while (alpha1 <= 0) alpha1 += 360;
498 while (alpha2 <= 0) alpha2 += 360; // adjust angles to be between
499 while (alpha1 > 360) alpha1 -= 360; // 0 and 360 degree
500 while (alpha2 > 360) alpha2 -= 360;
501
afce4c03 502 if (m_brush.GetStyle() != wxTRANSPARENT)
ed880dd4 503 {
d7657f75 504 SetBrush( m_brush );
72cdf4c9
VZ
505
506 fprintf( m_pstream,
507 "newpath\n"
508 "%d %d %d %d %d %d ellipse\n"
509 "%d %d lineto\n"
510 "closepath\n"
511 "fill\n",
512 XLOG2DEV(xc), YLOG2DEV(yc), XLOG2DEVREL(radius), YLOG2DEVREL(radius), (wxCoord)alpha1, (wxCoord) alpha2,
513 XLOG2DEV(xc), YLOG2DEV(yc) );
d7657f75
RR
514
515 CalcBoundingBox( xc-radius, yc-radius );
516 CalcBoundingBox( xc+radius, yc+radius );
ed880dd4 517 }
afce4c03
VZ
518
519 if (m_pen.GetStyle() != wxTRANSPARENT)
ed880dd4 520 {
d7657f75 521 SetPen( m_pen );
72cdf4c9
VZ
522
523 fprintf( m_pstream,
524 "newpath\n"
525 "%d %d %d %d %d %d ellipse\n"
526 "%d %d lineto\n"
527 "stroke\n"
528 "fill\n",
529 XLOG2DEV(xc), YLOG2DEV(yc), XLOG2DEVREL(radius), YLOG2DEVREL(radius), (wxCoord)alpha1, (wxCoord) alpha2,
530 XLOG2DEV(xc), YLOG2DEV(yc) );
d7657f75
RR
531
532 CalcBoundingBox( xc-radius, yc-radius );
533 CalcBoundingBox( xc+radius, yc+radius );
ed880dd4 534 }
ed880dd4
RR
535}
536
72cdf4c9 537void wxPostScriptDC::DoDrawEllipticArc(wxCoord x,wxCoord y,wxCoord w,wxCoord h,double sa,double ea)
ed880dd4 538{
223d09f6 539 wxCHECK_RET( m_ok && m_pstream, wxT("invalid postscript dc") );
afce4c03 540
ed880dd4
RR
541 if (sa>=360 || sa<=-360) sa=sa-int(sa/360)*360;
542 if (ea>=360 || ea<=-360) ea=ea-int(ea/360)*360;
543 if (sa<0) sa+=360;
544 if (ea<0) ea+=360;
afce4c03 545
ed880dd4
RR
546 if (sa==ea)
547 {
548 DrawEllipse(x,y,w,h);
549 return;
550 }
551
552 if (m_brush.GetStyle () != wxTRANSPARENT)
553 {
554 SetBrush( m_brush );
72cdf4c9
VZ
555
556 fprintf( m_pstream,
557 "newpath\n"
558 "%d %d %d %d %d %d true ellipticarc\n",
559 XLOG2DEV(x+w/2), YLOG2DEV(y+h/2), XLOG2DEVREL(w/2), YLOG2DEVREL(h/2), (wxCoord)sa, (wxCoord)ea );
560
ed880dd4
RR
561 CalcBoundingBox( x ,y );
562 CalcBoundingBox( x+w, y+h );
72cdf4c9
VZ
563 }
564
ed880dd4
RR
565 if (m_pen.GetStyle () != wxTRANSPARENT)
566 {
567 SetPen( m_pen );
568
72cdf4c9
VZ
569 fprintf(m_pstream,
570 "newpath\n"
571 "%d %d %d %d %d %d false ellipticarc\n",
572 XLOG2DEV(x+w/2), YLOG2DEV(y+h/2), XLOG2DEVREL(w/2), YLOG2DEVREL(h/2), (wxCoord)sa, (wxCoord)ea );
573
d7657f75 574 CalcBoundingBox( x ,y );
ed880dd4
RR
575 CalcBoundingBox( x+w, y+h );
576 }
577}
578
72cdf4c9 579void wxPostScriptDC::DoDrawPoint (wxCoord x, wxCoord y)
ed880dd4 580{
223d09f6 581 wxCHECK_RET( m_ok && m_pstream, wxT("invalid postscript dc") );
afce4c03 582
ed880dd4 583 if (m_pen.GetStyle() == wxTRANSPARENT) return;
afce4c03 584
ed880dd4 585 SetPen (m_pen);
afce4c03 586
d7657f75
RR
587 fprintf( m_pstream,
588 "newpath\n"
72cdf4c9
VZ
589 "%d %d moveto\n"
590 "%d %d lineto\n"
591 "stroke\n",
592 XLOG2DEV(x), YLOG2DEV(y),
593 XLOG2DEV(x+1), YLOG2DEV(y) );
afce4c03 594
ed880dd4
RR
595 CalcBoundingBox( x, y );
596}
597
72cdf4c9 598void wxPostScriptDC::DoDrawPolygon (int n, wxPoint points[], wxCoord xoffset, wxCoord yoffset, int WXUNUSED(fillStyle))
ed880dd4 599{
223d09f6 600 wxCHECK_RET( m_ok && m_pstream, wxT("invalid postscript dc") );
afce4c03 601
ed880dd4 602 if (n <= 0) return;
afce4c03 603
ed880dd4
RR
604 if (m_brush.GetStyle () != wxTRANSPARENT)
605 {
d7657f75
RR
606 SetBrush( m_brush );
607
608 fprintf( m_pstream, "newpath\n" );
afce4c03 609
72cdf4c9
VZ
610 wxCoord xx = XLOG2DEV(points[0].x + xoffset);
611 wxCoord yy = YLOG2DEV(points[0].y + yoffset);
612
613 fprintf( m_pstream, "%d %d moveto\n", xx, yy );
614
d7657f75 615 CalcBoundingBox( points[0].x + xoffset, points[0].y + yoffset );
ed880dd4 616
d7657f75
RR
617 for (int i = 1; i < n; i++)
618 {
619 xx = XLOG2DEV(points[i].x + xoffset);
620 yy = YLOG2DEV(points[i].y + yoffset);
72cdf4c9
VZ
621
622 fprintf( m_pstream, "%d %d lineto\n", xx, yy );
ed880dd4 623
d7657f75
RR
624 CalcBoundingBox( points[i].x + xoffset, points[i].y + yoffset);
625 }
72cdf4c9
VZ
626
627 fprintf( m_pstream, "fill\n" );
ed880dd4
RR
628 }
629
630 if (m_pen.GetStyle () != wxTRANSPARENT)
631 {
d7657f75 632 SetPen( m_pen );
72cdf4c9 633
d7657f75 634 fprintf( m_pstream, "newpath\n" );
ed880dd4 635
72cdf4c9
VZ
636 wxCoord xx = XLOG2DEV(points[0].x + xoffset);
637 wxCoord yy = YLOG2DEV(points[0].y + yoffset);
638
639 fprintf( m_pstream, "%d %d moveto\n", xx, yy );
640
d7657f75 641 CalcBoundingBox( points[0].x + xoffset, points[0].y + yoffset );
ed880dd4 642
d7657f75
RR
643 for (int i = 1; i < n; i++)
644 {
645 xx = XLOG2DEV(points[i].x + xoffset);
646 yy = YLOG2DEV(points[i].y + yoffset);
72cdf4c9
VZ
647
648 fprintf( m_pstream, "%d %d lineto\n", xx, yy );
649
d7657f75
RR
650 CalcBoundingBox( points[i].x + xoffset, points[i].y + yoffset);
651 }
ed880dd4 652
72cdf4c9 653 fprintf( m_pstream, "fill\n" );
ed880dd4
RR
654 }
655}
656
72cdf4c9 657void wxPostScriptDC::DoDrawLines (int n, wxPoint points[], wxCoord xoffset, wxCoord yoffset)
ed880dd4 658{
223d09f6 659 wxCHECK_RET( m_ok && m_pstream, wxT("invalid postscript dc") );
afce4c03 660
d7657f75 661 if (m_pen.GetStyle() == wxTRANSPARENT) return;
72cdf4c9 662
d7657f75 663 if (n <= 0) return;
afce4c03 664
ed880dd4 665 SetPen (m_pen);
afce4c03 666
9a6be59a
VZ
667 int i;
668 for ( i =0; i<n ; i++ )
ed880dd4 669 {
9a6be59a 670 CalcBoundingBox( XLOG2DEV(points[i].x+xoffset), YLOG2DEV(points[i].y+yoffset));
ed880dd4
RR
671 }
672
d7657f75
RR
673 fprintf( m_pstream,
674 "newpath\n"
72cdf4c9
VZ
675 "%d %d moveto\n",
676 XLOG2DEV(points[0].x+xoffset), YLOG2DEV(points[0].y+yoffset) );
afce4c03
VZ
677
678 for (i = 1; i < n; i++)
ed880dd4 679 {
d7657f75 680 fprintf( m_pstream,
72cdf4c9
VZ
681 "%d %d lineto\n",
682 XLOG2DEV(points[i].x+xoffset), YLOG2DEV(points[i].y+yoffset) );
ed880dd4 683 }
afce4c03 684
d7657f75 685 fprintf( m_pstream, "stroke\n" );
ed880dd4
RR
686}
687
72cdf4c9 688void wxPostScriptDC::DoDrawRectangle (wxCoord x, wxCoord y, wxCoord width, wxCoord height)
ed880dd4 689{
223d09f6 690 wxCHECK_RET( m_ok && m_pstream, wxT("invalid postscript dc") );
afce4c03 691
ed880dd4
RR
692 if (m_brush.GetStyle () != wxTRANSPARENT)
693 {
694 SetBrush( m_brush );
72cdf4c9
VZ
695
696 fprintf( m_pstream,
697 "newpath\n"
698 "%d %d moveto\n"
699 "%d %d lineto\n"
700 "%d %d lineto\n"
701 "%d %d lineto\n"
d7657f75
RR
702 "closepath\n"
703 "fill\n",
72cdf4c9
VZ
704 XLOG2DEV(x), YLOG2DEV(y),
705 XLOG2DEV(x + width), YLOG2DEV(y),
706 XLOG2DEV(x + width), YLOG2DEV(y + height),
707 XLOG2DEV(x), YLOG2DEV(y + height) );
ed880dd4
RR
708
709 CalcBoundingBox( x, y );
710 CalcBoundingBox( x + width, y + height );
711 }
afce4c03 712
ed880dd4
RR
713 if (m_pen.GetStyle () != wxTRANSPARENT)
714 {
715 SetPen (m_pen);
716
72cdf4c9
VZ
717 fprintf( m_pstream,
718 "newpath\n"
719 "%d %d moveto\n"
720 "%d %d lineto\n"
721 "%d %d lineto\n"
722 "%d %d lineto\n"
d7657f75
RR
723 "closepath\n"
724 "stroke\n",
72cdf4c9
VZ
725 XLOG2DEV(x), YLOG2DEV(y),
726 XLOG2DEV(x + width), YLOG2DEV(y),
727 XLOG2DEV(x + width), YLOG2DEV(y + height),
728 XLOG2DEV(x), YLOG2DEV(y + height) );
729
ed880dd4
RR
730 CalcBoundingBox( x, y );
731 CalcBoundingBox( x + width, y + height );
732 }
733}
734
72cdf4c9 735void wxPostScriptDC::DoDrawRoundedRectangle (wxCoord x, wxCoord y, wxCoord width, wxCoord height, double radius)
ed880dd4 736{
223d09f6 737 wxCHECK_RET( m_ok && m_pstream, wxT("invalid postscript dc") );
afce4c03 738
ed880dd4
RR
739 if (radius < 0.0)
740 {
741 // Now, a negative radius is interpreted to mean
742 // 'the proportion of the smallest X or Y dimension'
743 double smallest = 0.0;
744 if (width < height)
9a6be59a 745 smallest = width;
ed880dd4 746 else
9a6be59a 747 smallest = height;
ed880dd4
RR
748 radius = (-radius * smallest);
749 }
afce4c03 750
72cdf4c9 751 wxCoord rad = (wxCoord) radius;
ed880dd4
RR
752
753 if (m_brush.GetStyle () != wxTRANSPARENT)
754 {
755 SetBrush( m_brush );
afce4c03 756
d7657f75 757 /* Draw rectangle anticlockwise */
72cdf4c9
VZ
758 fprintf( m_pstream,
759 "newpath\n"
760 "%d %d %d 90 180 arc\n"
761 "%d %d moveto\n"
762 "%d %d %d 180 270 arc\n"
763 "%d %d lineto\n"
764 "%d %d %d 270 0 arc\n"
765 "%d %d lineto\n"
766 "%d %d %d 0 90 arc\n"
767 "%d %d lineto\n"
d7657f75
RR
768 "closepath\n"
769 "fill\n",
72cdf4c9
VZ
770 XLOG2DEV(x + rad), YLOG2DEV(y + rad), XLOG2DEVREL(rad),
771 XLOG2DEV(x), YLOG2DEV(y + rad),
d7657f75
RR
772 XLOG2DEV(x + rad), YLOG2DEV(y + height - rad), XLOG2DEVREL(rad),
773 XLOG2DEV(x + width - rad), YLOG2DEV(y + height),
72cdf4c9
VZ
774 XLOG2DEV(x + width - rad), YLOG2DEV(y + height - rad), XLOG2DEVREL(rad),
775 XLOG2DEV(x + width), YLOG2DEV(y + rad),
776 XLOG2DEV(x + width - rad), YLOG2DEV(y + rad), XLOG2DEVREL(rad),
777 XLOG2DEV(x + rad), YLOG2DEV(y) );
ed880dd4
RR
778
779 CalcBoundingBox( x, y );
780 CalcBoundingBox( x + width, y + height );
781 }
afce4c03 782
ed880dd4
RR
783 if (m_pen.GetStyle () != wxTRANSPARENT)
784 {
785 SetPen (m_pen);
afce4c03 786
d7657f75 787 /* Draw rectangle anticlockwise */
72cdf4c9
VZ
788 fprintf( m_pstream,
789 "newpath\n"
790 "%d %d %d 90 180 arc\n"
791 "%d %d moveto\n"
792 "%d %d %d 180 270 arc\n"
793 "%d %d lineto\n"
794 "%d %d %d 270 0 arc\n"
795 "%d %d lineto\n"
796 "%d %d %d 0 90 arc\n"
797 "%d %d lineto\n"
d7657f75
RR
798 "closepath\n"
799 "stroke\n",
72cdf4c9
VZ
800 XLOG2DEV(x + rad), YLOG2DEV(y + rad), XLOG2DEVREL(rad),
801 XLOG2DEV(x), YLOG2DEV(y + rad),
d7657f75
RR
802 XLOG2DEV(x + rad), YLOG2DEV(y + height - rad), XLOG2DEVREL(rad),
803 XLOG2DEV(x + width - rad), YLOG2DEV(y + height),
72cdf4c9
VZ
804 XLOG2DEV(x + width - rad), YLOG2DEV(y + height - rad), XLOG2DEVREL(rad),
805 XLOG2DEV(x + width), YLOG2DEV(y + rad),
806 XLOG2DEV(x + width - rad), YLOG2DEV(y + rad), XLOG2DEVREL(rad),
807 XLOG2DEV(x + rad), YLOG2DEV(y) );
ed880dd4
RR
808
809 CalcBoundingBox( x, y );
810 CalcBoundingBox( x + width, y + height );
811 }
812}
813
72cdf4c9 814void wxPostScriptDC::DoDrawEllipse (wxCoord x, wxCoord y, wxCoord width, wxCoord height)
ed880dd4 815{
223d09f6 816 wxCHECK_RET( m_ok && m_pstream, wxT("invalid postscript dc") );
afce4c03 817
ed880dd4
RR
818 if (m_brush.GetStyle () != wxTRANSPARENT)
819 {
820 SetBrush (m_brush);
821
72cdf4c9
VZ
822 fprintf( m_pstream,
823 "newpath\n"
824 "%d %d %d %d 0 360 ellipse\n"
825 "fill\n",
826 XLOG2DEV(x + width / 2), YLOG2DEV(y + height / 2),
827 XLOG2DEV(width / 2), YLOG2DEVREL(height / 2) );
ed880dd4
RR
828
829 CalcBoundingBox( x - width, y - height );
830 CalcBoundingBox( x + width, y + height );
831 }
afce4c03 832
d7657f75 833 if (m_pen.GetStyle () != wxTRANSPARENT)
ed880dd4
RR
834 {
835 SetPen (m_pen);
836
72cdf4c9
VZ
837 fprintf( m_pstream,
838 "newpath\n"
839 "%d %d %d %d 0 360 ellipse\n"
840 "stroke\n",
841 XLOG2DEV(x + width / 2), YLOG2DEV(y + height / 2),
842 XLOG2DEV(width / 2), YLOG2DEVREL(height / 2) );
ed880dd4
RR
843
844 CalcBoundingBox( x - width, y - height );
845 CalcBoundingBox( x + width, y + height );
846 }
847}
848
72cdf4c9 849void wxPostScriptDC::DoDrawIcon( const wxIcon& icon, wxCoord x, wxCoord y )
ed880dd4 850{
ef539066 851 DrawBitmap( icon, x, y, TRUE );
ed880dd4
RR
852}
853
d7657f75
RR
854/* this has to be char, not wxChar */
855static char hexArray[] = "0123456789ABCDEF";
856static void LocalDecToHex( int dec, char *buf )
857{
858 int firstDigit = (int)(dec/16.0);
859 int secondDigit = (int)(dec - (firstDigit*16.0));
860 buf[0] = hexArray[firstDigit];
861 buf[1] = hexArray[secondDigit];
862 buf[2] = 0;
863}
864
72cdf4c9 865void wxPostScriptDC::DoDrawBitmap( const wxBitmap& bitmap, wxCoord x, wxCoord y, bool WXUNUSED(useMask) )
ed880dd4 866{
223d09f6 867 wxCHECK_RET( m_ok && m_pstream, wxT("invalid postscript dc") );
ef539066
RR
868
869 if (!bitmap.Ok()) return;
afce4c03 870
ef539066 871 wxImage image( bitmap );
afce4c03 872
ef539066 873 if (!image.Ok()) return;
afce4c03 874
72cdf4c9
VZ
875 wxCoord ww = XLOG2DEVREL(image.GetWidth());
876 wxCoord hh = YLOG2DEVREL(image.GetHeight());
afce4c03 877
ef539066 878 image = image.Scale( ww, hh );
afce4c03 879
ef539066 880 if (!image.Ok()) return;
afce4c03 881
72cdf4c9
VZ
882 wxCoord xx = XLOG2DEV(x);
883 wxCoord yy = YLOG2DEV(y + bitmap.GetHeight());
d7657f75
RR
884
885 fprintf( m_pstream,
886 "/origstate save def\n"
72cdf4c9
VZ
887 "20 dict begin\n"
888 "/pix %d string def\n"
889 "/grays %d string def\n"
d7657f75
RR
890 "/npixels 0 def\n"
891 "/rgbindx 0 def\n"
72cdf4c9
VZ
892 "%d %d translate\n"
893 "%d %d scale\n"
894 "%d %d 8\n"
895 "[%d 0 0 %d 0 %d]\n"
896 "{currentfile pix readhexstring pop}\n"
897 "false 3 colorimage\n",
898 ww, ww, xx, yy, ww, hh, ww, hh, ww, -hh, hh );
afce4c03 899
ef539066
RR
900 for (int j = 0; j < hh; j++)
901 {
902 for (int i = 0; i < ww; i++)
903 {
d7657f75
RR
904 char buffer[5];
905 LocalDecToHex( image.GetRed(i,j), buffer );
72cdf4c9 906 fprintf( m_pstream, buffer );
d7657f75 907 LocalDecToHex( image.GetGreen(i,j), buffer );
72cdf4c9
VZ
908 fprintf( m_pstream, buffer );
909 LocalDecToHex( image.GetBlue(i,j), buffer );
910 fprintf( m_pstream, buffer );
d7657f75
RR
911 }
912 fprintf( m_pstream, "\n" );
ef539066
RR
913 }
914
d7657f75
RR
915 fprintf( m_pstream, "end\n" );
916 fprintf( m_pstream, "origstate restore\n" );
ed880dd4
RR
917}
918
36b3b54a 919void wxPostScriptDC::SetFont( const wxFont& font )
ed880dd4 920{
223d09f6 921 wxCHECK_RET( m_ok && m_pstream, wxT("invalid postscript dc") );
afce4c03 922
ed880dd4 923 if (!font.Ok()) return;
afce4c03 924
ed880dd4 925 m_font = font;
57c208c5 926
36b3b54a
RR
927 const char *name;
928 const char *style = "";
929 int Style = m_font.GetStyle();
930 int Weight = m_font.GetWeight();
931
d7657f75 932 switch (m_font.GetFamily())
36b3b54a
RR
933 {
934 case wxTELETYPE:
935 case wxMODERN:
936 name = "/Courier";
937 break;
938 case wxSWISS:
939 name = "/Helvetica";
940 break;
941 case wxROMAN:
942// name = "/Times-Roman";
943 name = "/Times"; // Altered by EDZ
944 break;
945 case wxSCRIPT:
946 name = "/Zapf-Chancery-MediumItalic";
947 Style = wxNORMAL;
948 Weight = wxNORMAL;
949 break;
950 default:
951 case wxDEFAULT: // Sans Serif Font
952 name = "/LucidaSans";
953 }
954
955 if (Style == wxNORMAL && (Weight == wxNORMAL || Weight == wxLIGHT))
57c208c5 956 {
d7657f75
RR
957 if (m_font.GetFamily () == wxROMAN)
958 style = "-Roman";
959 else
960 style = "";
57c208c5 961 }
36b3b54a
RR
962 else if (Style == wxNORMAL && Weight == wxBOLD)
963 {
d7657f75 964 style = "-Bold";
36b3b54a
RR
965 }
966 else if (Style == wxITALIC && (Weight == wxNORMAL || Weight == wxLIGHT))
57c208c5 967 {
d7657f75
RR
968 if (m_font.GetFamily () == wxROMAN)
969 style = "-Italic";
970 else
971 style = "-Oblique";
57c208c5 972 }
36b3b54a 973 else if (Style == wxITALIC && Weight == wxBOLD)
57c208c5 974 {
d7657f75
RR
975 if (m_font.GetFamily () == wxROMAN)
976 style = "-BoldItalic";
977 else
978 style = "-BoldOblique";
57c208c5 979 }
36b3b54a 980 else if (Style == wxSLANT && (Weight == wxNORMAL || Weight == wxLIGHT))
57c208c5 981 {
d7657f75
RR
982 if (m_font.GetFamily () == wxROMAN)
983 style = "-Italic";
984 else
985 style = "-Oblique";
57c208c5 986 }
36b3b54a 987 else if (Style == wxSLANT && Weight == wxBOLD)
57c208c5 988 {
d7657f75
RR
989 if (m_font.GetFamily () == wxROMAN)
990 style = "-BoldItalic";
991 else
992 style = "-BoldOblique";
57c208c5 993 }
36b3b54a
RR
994 else
995 {
d7657f75 996 style = "";
36b3b54a 997 }
57c208c5 998
d7657f75
RR
999 char buffer[100];
1000 strcpy( buffer, name );
1001 strcat( buffer, style );
afce4c03 1002
d7657f75
RR
1003 fprintf( m_pstream, buffer );
1004 fprintf( m_pstream, " reencodeISO def\n" );
1005 fprintf( m_pstream, buffer );
1006 fprintf( m_pstream, " findfont\n" );
ce7f10f2
VS
1007
1008 fprintf( m_pstream, "%f scalefont setfont\n", YLOG2DEVREL(m_font.GetPointSize() * 1000) / 1000.0F);
1009 // this is a hack - we must scale font size (in pts) according to m_scaleY but
1010 // YLOG2DEVREL works with wxCoord type (int or longint). Se we first convert font size
479cd5de 1011 // to 1/1000th of pt and then back.
ed880dd4
RR
1012}
1013
1014void wxPostScriptDC::SetPen( const wxPen& pen )
1015{
223d09f6 1016 wxCHECK_RET( m_ok && m_pstream, wxT("invalid postscript dc") );
afce4c03 1017
ed880dd4 1018 if (!pen.Ok()) return;
afce4c03 1019
ed880dd4
RR
1020 int oldStyle = m_pen.GetStyle();
1021
1022 m_pen = pen;
1023
e6f1ad22
HH
1024 #ifdef __WXMSW__
1025 fprintf( m_pstream, "%d setlinewidth\n", XLOG2DEVREL(m_pen.GetWidth()) );
1026 #else
72cdf4c9 1027 fprintf( m_pstream, "%d setlinewidth\n", XLOG2DEVREL(m_pen.GetWidth()) );
e6f1ad22 1028 #endif
ed880dd4
RR
1029/*
1030 Line style - WRONG: 2nd arg is OFFSET
afce4c03 1031
ed880dd4
RR
1032 Here, I'm afraid you do not conceive meaning of parameters of 'setdash'
1033 operator correctly. You should look-up this in the Red Book: the 2nd parame-
1034 ter is not number of values in the array of the first one, but an offset
1035 into this description of the pattern. I mean a real *offset* not index
1036 into array. I.e. If the command is [3 4] 1 setdash is used, then there
72cdf4c9 1037 will be first black line *2* units wxCoord, then space 4 units, then the
ed880dd4
RR
1038 pattern of *3* units black, 4 units space will be repeated.
1039*/
1040
1041 static const char *dotted = "[2 5] 2";
1042 static const char *short_dashed = "[4 4] 2";
72cdf4c9 1043 static const char *wxCoord_dashed = "[4 8] 2";
ed880dd4
RR
1044 static const char *dotted_dashed = "[6 6 2 6] 4";
1045
1046 const char *psdash = (char *) NULL;
d7657f75 1047 switch (m_pen.GetStyle())
ed880dd4
RR
1048 {
1049 case wxDOT: psdash = dotted; break;
1050 case wxSHORT_DASH: psdash = short_dashed; break;
72cdf4c9 1051 case wxLONG_DASH: psdash = wxCoord_dashed; break;
ed880dd4
RR
1052 case wxDOT_DASH: psdash = dotted_dashed; break;
1053 case wxSOLID:
1054 case wxTRANSPARENT:
1055 default: psdash = "[] 0"; break;
1056 }
afce4c03 1057
ed880dd4
RR
1058 if (oldStyle != m_pen.GetStyle())
1059 {
d7657f75 1060 fprintf( m_pstream, psdash );
72cdf4c9 1061 fprintf( m_pstream," setdash\n" );
ed880dd4
RR
1062 }
1063
1064 // Line colour
1065 unsigned char red = m_pen.GetColour().Red();
1066 unsigned char blue = m_pen.GetColour().Blue();
1067 unsigned char green = m_pen.GetColour().Green();
1068
1069 if (!m_colour)
1070 {
1071 // Anything not white is black
72cdf4c9
VZ
1072 if (! (red == (unsigned char) 255 &&
1073 blue == (unsigned char) 255 &&
d7657f75
RR
1074 green == (unsigned char) 255) )
1075 {
1076 red = (unsigned char) 0;
1077 green = (unsigned char) 0;
1078 blue = (unsigned char) 0;
72cdf4c9 1079 }
d7657f75 1080 // setgray here ?
ed880dd4
RR
1081 }
1082
1083 if (!(red == m_currentRed && green == m_currentGreen && blue == m_currentBlue))
1084 {
d7657f75
RR
1085 double redPS = (double)(red) / 255.0;
1086 double bluePS = (double)(blue) / 255.0;
1087 double greenPS = (double)(green) / 255.0;
72cdf4c9
VZ
1088
1089 fprintf( m_pstream,
1090 "%.8f %.8f %.8f setrgbcolor\n",
1091 redPS, greenPS, bluePS );
1092
d7657f75
RR
1093 m_currentRed = red;
1094 m_currentBlue = blue;
1095 m_currentGreen = green;
ed880dd4
RR
1096 }
1097}
1098
1099void wxPostScriptDC::SetBrush( const wxBrush& brush )
1100{
223d09f6 1101 wxCHECK_RET( m_ok && m_pstream, wxT("invalid postscript dc") );
afce4c03 1102
ed880dd4 1103 if (!brush.Ok()) return;
afce4c03 1104
ed880dd4
RR
1105 m_brush = brush;
1106
1107 // Brush colour
3c1b65a4
RR
1108 unsigned char red = m_brush.GetColour().Red();
1109 unsigned char blue = m_brush.GetColour().Blue();
1110 unsigned char green = m_brush.GetColour().Green();
ed880dd4
RR
1111
1112 if (!m_colour)
1113 {
d7657f75 1114 // Anything not white is black
72cdf4c9
VZ
1115 if (! (red == (unsigned char) 255 &&
1116 blue == (unsigned char) 255 &&
d7657f75
RR
1117 green == (unsigned char) 255) )
1118 {
1119 red = (unsigned char) 0;
1120 green = (unsigned char) 0;
1121 blue = (unsigned char) 0;
72cdf4c9 1122 }
d7657f75 1123 // setgray here ?
ed880dd4 1124 }
72cdf4c9 1125
ed880dd4
RR
1126 if (!(red == m_currentRed && green == m_currentGreen && blue == m_currentBlue))
1127 {
d7657f75
RR
1128 double redPS = (double)(red) / 255.0;
1129 double bluePS = (double)(blue) / 255.0;
1130 double greenPS = (double)(green) / 255.0;
72cdf4c9
VZ
1131
1132 fprintf( m_pstream,
1133 "%.8f %.8f %.8f setrgbcolor\n",
1134 redPS, greenPS, bluePS );
1135
d7657f75
RR
1136 m_currentRed = red;
1137 m_currentBlue = blue;
1138 m_currentGreen = green;
ed880dd4
RR
1139 }
1140}
1141
72cdf4c9 1142void wxPostScriptDC::DoDrawText( const wxString& text, wxCoord x, wxCoord y )
ed880dd4 1143{
223d09f6 1144 wxCHECK_RET( m_ok && m_pstream, wxT("invalid postscript dc") );
479cd5de 1145
6c3d9ced 1146 wxCoord text_w, text_h, text_descent;
479cd5de 1147
6c3d9ced 1148 GetTextExtent(text, &text_w, &text_h, &text_descent);
afce4c03 1149
ed880dd4
RR
1150 SetFont( m_font );
1151
d7657f75 1152 if (m_textForegroundColour.Ok())
ed880dd4 1153 {
d7657f75
RR
1154 unsigned char red = m_textForegroundColour.Red();
1155 unsigned char blue = m_textForegroundColour.Blue();
1156 unsigned char green = m_textForegroundColour.Green();
ed880dd4
RR
1157
1158 if (!m_colour)
9a6be59a 1159 {
d7657f75 1160 // Anything not white is black
72cdf4c9
VZ
1161 if (! (red == (unsigned char) 255 &&
1162 blue == (unsigned char) 255 &&
1163 green == (unsigned char) 255))
d7657f75
RR
1164 {
1165 red = (unsigned char) 0;
1166 green = (unsigned char) 0;
1167 blue = (unsigned char) 0;
1168 }
9a6be59a 1169 }
afce4c03 1170
d7657f75 1171 // maybe setgray here ?
ed880dd4
RR
1172 if (!(red == m_currentRed && green == m_currentGreen && blue == m_currentBlue))
1173 {
d7657f75
RR
1174 double redPS = (double)(red) / 255.0;
1175 double bluePS = (double)(blue) / 255.0;
1176 double greenPS = (double)(green) / 255.0;
72cdf4c9
VZ
1177
1178 fprintf( m_pstream,
1179 "%.8f %.8f %.8f setrgbcolor\n",
1180 redPS, greenPS, bluePS );
1181
ed880dd4
RR
1182 m_currentRed = red;
1183 m_currentBlue = blue;
1184 m_currentGreen = green;
1185 }
1186 }
1187
1188 int size = m_font.GetPointSize();
1189
6c3d9ced
VS
1190// wxCoord by = y + (wxCoord)floor( double(size) * 2.0 / 3.0 ); // approximate baseline
1191// commented by V. Slavik and replaced by accurate version
1192// - note that there is still rounding error in text_descent!
1193 wxCoord by = y + size - text_descent; // baseline
72cdf4c9 1194 fprintf( m_pstream, "%d %d moveto\n", XLOG2DEV(x), YLOG2DEV(by) );
afce4c03 1195
d7657f75
RR
1196 /* I don't know how to write char to a stream, so I use a mini string */
1197 char tmpbuf[2];
1198 tmpbuf[1] = 0;
72cdf4c9 1199
d7657f75 1200 fprintf( m_pstream, "(" );
9626e0bf 1201 const wxWX2MBbuf textbuf = text.mb_str();
d7657f75 1202 int len = strlen(textbuf);
ed880dd4
RR
1203 int i;
1204 for (i = 0; i < len; i++)
1205 {
87138c52 1206 int c = (unsigned char) textbuf[i];
d7657f75 1207 if (c == ')' || c == '(' || c == '\\')
ed880dd4 1208 {
d7657f75 1209 /* Cope with special characters */
72cdf4c9
VZ
1210 fprintf( m_pstream, "\\" );
1211 tmpbuf[0] = (char) c;
1212 fprintf( m_pstream, tmpbuf );
ed880dd4
RR
1213 }
1214 else if ( c >= 128 )
1215 {
d7657f75 1216 /* Cope with character codes > 127 */
72cdf4c9 1217 fprintf(m_pstream, "\\%o", c);
ed880dd4
RR
1218 }
1219 else
72cdf4c9
VZ
1220 {
1221 tmpbuf[0] = (char) c;
1222 fprintf( m_pstream, tmpbuf );
1223 }
ed880dd4 1224 }
72cdf4c9 1225
d7657f75 1226 fprintf( m_pstream, ") show\n" );
ed880dd4
RR
1227
1228 if (m_font.GetUnderlined())
1229 {
72cdf4c9 1230 wxCoord uy = (wxCoord)(y + size - m_underlinePosition);
afce4c03 1231
72cdf4c9
VZ
1232 fprintf( m_pstream,
1233 "gsave\n"
1234 "%d %d moveto\n"
1235 "%d setlinewidth\n"
1236 "%d %d lineto\n"
1237 "stroke\n"
1238 "grestore\n",
d7657f75 1239 XLOG2DEV(x), YLOG2DEV(uy),
72cdf4c9 1240 (wxCoord)m_underlineThickness,
6c3d9ced 1241 XLOG2DEV(x + text_w), YLOG2DEV(uy) );
ed880dd4 1242 }
afce4c03 1243
ed880dd4
RR
1244 CalcBoundingBox( x, y );
1245 CalcBoundingBox( x + size * text.Length() * 2/3 , y );
1246}
1247
95724b1a
VZ
1248void wxPostScriptDC::DoDrawRotatedText( const wxString& text, wxCoord x, wxCoord y, double angle )
1249{
1250 if (angle == 0.0)
1251 {
1252 DoDrawText(text, x, y);
1253 return;
1254 }
1255
1256 wxCHECK_RET( m_ok && m_pstream, wxT("invalid postscript dc") );
1257
1258 SetFont( m_font );
1259
1260 if (m_textForegroundColour.Ok())
1261 {
1262 unsigned char red = m_textForegroundColour.Red();
1263 unsigned char blue = m_textForegroundColour.Blue();
1264 unsigned char green = m_textForegroundColour.Green();
1265
1266 if (!m_colour)
1267 {
1268 // Anything not white is black
479cd5de
VZ
1269 if (! (red == (unsigned char) 255 &&
1270 blue == (unsigned char) 255 &&
1271 green == (unsigned char) 255))
95724b1a
VZ
1272 {
1273 red = (unsigned char) 0;
1274 green = (unsigned char) 0;
1275 blue = (unsigned char) 0;
1276 }
1277 }
1278
1279 // maybe setgray here ?
1280 if (!(red == m_currentRed && green == m_currentGreen && blue == m_currentBlue))
1281 {
1282 double redPS = (double)(red) / 255.0;
1283 double bluePS = (double)(blue) / 255.0;
1284 double greenPS = (double)(green) / 255.0;
479cd5de
VZ
1285
1286 fprintf( m_pstream,
1287 "%.8f %.8f %.8f setrgbcolor\n",
1288 redPS, greenPS, bluePS );
1289
95724b1a
VZ
1290 m_currentRed = red;
1291 m_currentBlue = blue;
1292 m_currentGreen = green;
1293 }
1294 }
1295
1296 int size = m_font.GetPointSize();
1297
1298 long by = y + (long)floor( double(size) * 2.0 / 3.0 ); // approximate baseline
1299
70846f0a 1300 // FIXME only correct for 90 degrees
479cd5de 1301 fprintf(m_pstream, "%d %d moveto\n", XLOG2DEV(x + size), YLOG2DEV(by) );
95724b1a
VZ
1302 fprintf(m_pstream, "%.8f rotate\n", angle);
1303
1304 /* I don't know how to write char to a stream, so I use a mini string */
1305 char tmpbuf[2];
1306 tmpbuf[1] = 0;
479cd5de 1307
95724b1a
VZ
1308 fprintf( m_pstream, "(" );
1309 const wxWX2MBbuf textbuf = text.mb_str();
1310 int len = strlen(textbuf);
1311 int i;
1312 for (i = 0; i < len; i++)
1313 {
1314 int c = (unsigned char) textbuf[i];
1315 if (c == ')' || c == '(' || c == '\\')
1316 {
1317 /* Cope with special characters */
479cd5de
VZ
1318 fprintf( m_pstream, "\\" );
1319 tmpbuf[0] = (char) c;
1320 fprintf( m_pstream, tmpbuf );
95724b1a
VZ
1321 }
1322 else if ( c >= 128 )
1323 {
1324 /* Cope with character codes > 127 */
479cd5de 1325 fprintf(m_pstream, "\\%o", c);
95724b1a
VZ
1326 }
1327 else
479cd5de
VZ
1328 {
1329 tmpbuf[0] = (char) c;
1330 fprintf( m_pstream, tmpbuf );
1331 }
95724b1a 1332 }
479cd5de 1333
95724b1a
VZ
1334 fprintf( m_pstream, ") show\n" );
1335 fprintf( m_pstream, "%.8f rotate\n", -angle );
1336
1337 if (m_font.GetUnderlined())
1338 {
1339 long uy = (long)(y + size - m_underlinePosition);
1340 long w, h;
1341 GetTextExtent(text, &w, &h);
1342
479cd5de 1343 fprintf( m_pstream,
70846f0a 1344 "gsave\n"
479cd5de 1345 "%d %d moveto\n"
70846f0a
VZ
1346 "%ld setlinewidth\n"
1347 "%d %d lineto\n"
1348 "stroke\n"
479cd5de 1349 "grestore\n",
70846f0a
VZ
1350 XLOG2DEV(x), YLOG2DEV(uy),
1351 (long)m_underlineThickness,
1352 XLOG2DEV(x + w), YLOG2DEV(uy) );
95724b1a
VZ
1353 }
1354
1355 CalcBoundingBox( x, y );
1356 CalcBoundingBox( x + size * text.Length() * 2/3 , y );
1357}
ed880dd4
RR
1358
1359void wxPostScriptDC::SetBackground (const wxBrush& brush)
1360{
1361 m_backgroundBrush = brush;
1362}
1363
1364void wxPostScriptDC::SetLogicalFunction (int WXUNUSED(function))
1365{
223d09f6 1366 wxFAIL_MSG( wxT("wxPostScriptDC::SetLogicalFunction not implemented.") );
ed880dd4
RR
1367}
1368
64698f9a 1369void wxPostScriptDC::DoDrawSpline( wxList *points )
ed880dd4 1370{
223d09f6 1371 wxCHECK_RET( m_ok && m_pstream, wxT("invalid postscript dc") );
afce4c03 1372
ed880dd4
RR
1373 SetPen( m_pen );
1374
1375 double a, b, c, d, x1, y1, x2, y2, x3, y3;
1376 wxPoint *p, *q;
1377
1378 wxNode *node = points->First();
1379 p = (wxPoint *)node->Data();
afce4c03 1380 x1 = p->x;
ed880dd4
RR
1381 y1 = p->y;
1382
1383 node = node->Next();
1384 p = (wxPoint *)node->Data();
afce4c03 1385 c = p->x;
ed880dd4
RR
1386 d = p->y;
1387 x3 = a = (double)(x1 + c) / 2;
1388 y3 = b = (double)(y1 + d) / 2;
1389
72cdf4c9 1390 fprintf( m_pstream,
d7657f75 1391 "newpath\n"
72cdf4c9
VZ
1392 "%d %d moveto\n"
1393 "%d %d lineto\n",
1394 XLOG2DEV((wxCoord)x1), YLOG2DEV((wxCoord)y1),
1395 XLOG2DEV((wxCoord)x3), YLOG2DEV((wxCoord)y3) );
afce4c03 1396
72cdf4c9
VZ
1397 CalcBoundingBox( (wxCoord)x1, (wxCoord)y1 );
1398 CalcBoundingBox( (wxCoord)x3, (wxCoord)y3 );
ed880dd4
RR
1399
1400 while ((node = node->Next()) != NULL)
1401 {
1402 q = (wxPoint *)node->Data();
1403
d7657f75
RR
1404 x1 = x3;
1405 y1 = y3;
1406 x2 = c;
1407 y2 = d;
1408 c = q->x;
1409 d = q->y;
ed880dd4
RR
1410 x3 = (double)(x2 + c) / 2;
1411 y3 = (double)(y2 + d) / 2;
72cdf4c9
VZ
1412
1413 fprintf( m_pstream,
1414 "%d %d %d %d %d %d DrawSplineSection\n",
1415 XLOG2DEV((wxCoord)x1), YLOG2DEV((wxCoord)y1),
1416 XLOG2DEV((wxCoord)x2), YLOG2DEV((wxCoord)y2),
1417 XLOG2DEV((wxCoord)x3), YLOG2DEV((wxCoord)y3) );
1418
1419 CalcBoundingBox( (wxCoord)x1, (wxCoord)y1 );
1420 CalcBoundingBox( (wxCoord)x3, (wxCoord)y3 );
ed880dd4 1421 }
afce4c03 1422
72cdf4c9
VZ
1423 /*
1424 At this point, (x2,y2) and (c,d) are the position of the
1425 next-to-last and last point respectively, in the point list
1426 */
ed880dd4 1427
d7657f75 1428 fprintf( m_pstream,
72cdf4c9
VZ
1429 "%d %d lineto\n"
1430 "stroke\n",
1431 XLOG2DEV((wxCoord)c), YLOG2DEV((wxCoord)d) );
ed880dd4
RR
1432}
1433
72cdf4c9 1434wxCoord wxPostScriptDC::GetCharWidth() const
ed880dd4
RR
1435{
1436 // Chris Breeze: reasonable approximation using wxMODERN/Courier
72cdf4c9 1437 return (wxCoord) (GetCharHeight() * 72.0 / 120.0);
ed880dd4
RR
1438}
1439
1440
1441void wxPostScriptDC::SetAxisOrientation( bool xLeftRight, bool yBottomUp )
1442{
223d09f6 1443 wxCHECK_RET( m_ok && m_pstream, wxT("invalid postscript dc") );
afce4c03 1444
ed880dd4
RR
1445 m_signX = (xLeftRight ? 1 : -1);
1446 m_signY = (yBottomUp ? 1 : -1);
afce4c03 1447
004fd0c8
DW
1448 // FIXME there is no such function in MSW nor in OS2/PM
1449#if !defined(__WXMSW__) && !defined(__WXPM__)
ed880dd4 1450 ComputeScaleAndOrigin();
9a6be59a 1451#endif
ed880dd4
RR
1452}
1453
72cdf4c9 1454void wxPostScriptDC::SetDeviceOrigin( wxCoord x, wxCoord y )
ed880dd4 1455{
223d09f6 1456 wxCHECK_RET( m_ok && m_pstream, wxT("invalid postscript dc") );
afce4c03 1457
4bc67cc5
RR
1458 int h = 0;
1459 int w = 0;
1460 GetSize( &w, &h );
afce4c03 1461
4bc67cc5 1462 wxDC::SetDeviceOrigin( x, h-y );
ed880dd4
RR
1463}
1464
4e4ea166 1465void wxPostScriptDC::DoGetSize(int* width, int* height) const
ed880dd4 1466{
7bcb11d3 1467 wxPaperSize id = m_printData.GetPaperId();
ed880dd4 1468
7bcb11d3 1469 wxPrintPaperType *paper = wxThePrintPaperDatabase->FindPaperType(id);
afce4c03 1470
7bcb11d3 1471 if (!paper) paper = wxThePrintPaperDatabase->FindPaperType(wxPAPER_A4);
afce4c03 1472
ac2def68
RR
1473 int w = 595;
1474 int h = 842;
ed880dd4
RR
1475 if (paper)
1476 {
ac2def68
RR
1477 w = paper->GetSizeDeviceUnits().x;
1478 h = paper->GetSizeDeviceUnits().y;
ed880dd4 1479 }
72cdf4c9 1480
ac2def68 1481 if (m_printData.GetOrientation() == wxLANDSCAPE)
ed880dd4 1482 {
ac2def68 1483 int tmp = w;
72cdf4c9
VZ
1484 w = h;
1485 h = tmp;
ed880dd4 1486 }
72cdf4c9 1487
ac2def68
RR
1488 if (width) *width = w;
1489 if (height) *height = h;
ed880dd4
RR
1490}
1491
4e4ea166 1492void wxPostScriptDC::DoGetSizeMM(int *width, int *height) const
ed880dd4 1493{
7bcb11d3
JS
1494 wxPaperSize id = m_printData.GetPaperId();
1495
1496 wxPrintPaperType *paper = wxThePrintPaperDatabase->FindPaperType(id);
afce4c03 1497
7bcb11d3
JS
1498 if (!paper) paper = wxThePrintPaperDatabase->FindPaperType(wxPAPER_A4);
1499
ac2def68
RR
1500 int w = 210;
1501 int h = 297;
7bcb11d3 1502 if (paper)
ed880dd4 1503 {
ac2def68
RR
1504 w = paper->GetWidth() / 10;
1505 h = paper->GetHeight() / 10;
ed880dd4 1506 }
72cdf4c9 1507
ac2def68 1508 if (m_printData.GetOrientation() == wxLANDSCAPE)
ed880dd4 1509 {
ac2def68 1510 int tmp = w;
72cdf4c9
VZ
1511 w = h;
1512 h = tmp;
ed880dd4 1513 }
72cdf4c9 1514
ac2def68
RR
1515 if (width) *width = w;
1516 if (height) *height = h;
7bcb11d3
JS
1517}
1518
1519// Resolution in pixels per logical inch
1520wxSize wxPostScriptDC::GetPPI(void) const
1521{
1522 return wxSize(72, 72);
1523}
ed880dd4 1524
7bcb11d3 1525
d7657f75 1526bool wxPostScriptDC::StartDoc( const wxString& message )
7bcb11d3 1527{
223d09f6 1528 wxCHECK_MSG( m_ok, FALSE, wxT("invalid postscript dc") );
7bcb11d3
JS
1529
1530 if (m_printData.GetFilename() == "")
1531 {
1532 wxString filename = wxGetTempFileName("ps");
1533 m_printData.SetFilename(filename);
7bcb11d3 1534 }
72cdf4c9 1535
d7657f75 1536 m_pstream = fopen( m_printData.GetFilename().fn_str(), "w+" );
7bcb11d3 1537
d7657f75 1538 if (!m_pstream)
ed880dd4 1539 {
d7657f75 1540 wxMessageBox( _("Cannot open file for PostScript printing!"), _("Error"), wxOK );
7bcb11d3
JS
1541 m_ok = FALSE;
1542 return FALSE;
ed880dd4 1543 }
afce4c03 1544
ed880dd4
RR
1545 m_ok = TRUE;
1546
d7657f75
RR
1547 fprintf( m_pstream, "%%%%BeginProlog\n" );
1548 fprintf( m_pstream, wxPostScriptHeaderEllipse );
1549 fprintf( m_pstream, wxPostScriptHeaderEllipticArc );
1550 fprintf( m_pstream, wxPostScriptHeaderColourImage );
1551 fprintf( m_pstream, wxPostScriptHeaderReencodeISO1 );
1552 fprintf( m_pstream, wxPostScriptHeaderReencodeISO2 );
1553 if (wxPostScriptHeaderSpline)
1554 fprintf( m_pstream, wxPostScriptHeaderSpline );
1555 fprintf( m_pstream, "%%%%EndProlog\n" );
1556
ed880dd4
RR
1557 SetBrush( *wxBLACK_BRUSH );
1558 SetPen( *wxBLACK_PEN );
1559 SetBackground( *wxWHITE_BRUSH );
1560 SetTextForeground( *wxBLACK );
1561
1562 // set origin according to paper size
1563 SetDeviceOrigin( 0,0 );
afce4c03 1564
ed880dd4
RR
1565 wxPageNumber = 1;
1566 m_pageNumber = 1;
1567 m_title = message;
1568 return TRUE;
1569}
1570
1571void wxPostScriptDC::EndDoc ()
1572{
223d09f6 1573 wxCHECK_RET( m_ok && m_pstream, wxT("invalid postscript dc") );
afce4c03 1574
ed880dd4
RR
1575 if (m_clipping)
1576 {
1577 m_clipping = FALSE;
72cdf4c9 1578 fprintf( m_pstream, "grestore\n" );
ed880dd4
RR
1579 }
1580
d7657f75
RR
1581 fclose( m_pstream );
1582 m_pstream = (FILE *) NULL;
ed880dd4 1583
87138c52 1584 wxChar *header_file = wxGetTempFileName("ps");
afce4c03 1585
d7657f75 1586 m_pstream = fopen( wxConvFile.cWX2MB(header_file) , "w+" );
ed880dd4 1587
d7657f75 1588 fprintf( m_pstream, "%%!PS-Adobe-2.0\n" ); // PostScript magic strings
aece0491 1589 fprintf( m_pstream, "%%%%Title: %s\n", (const char *)m_title.mb_str() );
ac2def68 1590 fprintf( m_pstream, "%%%%Creator: %s\n", (const char*)wxConvCurrent->cWX2MB(wxTheApp->argv[0]) );
aece0491 1591 fprintf( m_pstream, "%%%%CreationDate: %s\n", (const char *)wxNow().mb_str() );
ed880dd4 1592
87138c52 1593 wxChar userID[256];
ed880dd4
RR
1594 if ( wxGetEmailAddress(userID, sizeof(userID)) )
1595 {
e90c1d2a 1596 fprintf( m_pstream, "%%%%For: %s ", wxMBSTRINGCAST wxConvCurrent->cWX2MB(userID) );
87138c52 1597 wxChar userName[245];
ed880dd4 1598 if (wxGetUserName(userName, sizeof(userName)))
e90c1d2a 1599 fprintf( m_pstream, " (%s)", wxMBSTRINGCAST wxConvCurrent->cWX2MB(userName) );
72cdf4c9 1600 fprintf( m_pstream, "\n" );
ed880dd4
RR
1601 }
1602 else if ( wxGetUserName(userID, sizeof(userID)) )
1603 {
e90c1d2a 1604 fprintf( m_pstream, "%%%%For: %s\n", wxMBSTRINGCAST wxConvCurrent->cWX2MB(userID) );;
ed880dd4
RR
1605 }
1606
1607 // THE FOLLOWING HAS BEEN CONTRIBUTED BY Andy Fyfe <andy@hyperparallel.com>
1608
72cdf4c9 1609 wxCoord wx_printer_translate_x, wx_printer_translate_y;
ed880dd4 1610 double wx_printer_scale_x, wx_printer_scale_y;
ed880dd4 1611
479cd5de
VZ
1612 wx_printer_translate_x = (wxCoord)m_printData.GetPrinterTranslateX();
1613 wx_printer_translate_y = (wxCoord)m_printData.GetPrinterTranslateY();
7bcb11d3
JS
1614
1615 wx_printer_scale_x = m_printData.GetPrinterScaleX();
1616 wx_printer_scale_y = m_printData.GetPrinterScaleY();
1617
1618 if (m_printData.GetOrientation() == wxLANDSCAPE)
d7657f75 1619 fprintf( m_pstream, "%%%%Orientation: Landscape\n" );
ed880dd4 1620 else
d7657f75 1621 fprintf( m_pstream, "%%%%Orientation: Portrait\n" );
ed880dd4
RR
1622
1623 // Compute the bounding box. Note that it is in the default user
1624 // coordinate system, thus we have to convert the values.
72cdf4c9
VZ
1625 wxCoord llx = (wxCoord) ((XLOG2DEV(m_minX)+wx_printer_translate_x)*wx_printer_scale_x);
1626 wxCoord lly = (wxCoord) ((YLOG2DEV(m_minY)+wx_printer_translate_y)*wx_printer_scale_y);
1627 wxCoord urx = (wxCoord) ((XLOG2DEV(m_maxX)+wx_printer_translate_x)*wx_printer_scale_x);
1628 wxCoord ury = (wxCoord) ((YLOG2DEV(m_maxY)+wx_printer_translate_y)*wx_printer_scale_y);
ed880dd4
RR
1629
1630 // If we're landscape, our sense of "x" and "y" is reversed.
7bcb11d3 1631 if (m_printData.GetOrientation() == wxLANDSCAPE)
ed880dd4 1632 {
72cdf4c9 1633 wxCoord tmp;
ed880dd4
RR
1634 tmp = llx; llx = lly; lly = tmp;
1635 tmp = urx; urx = ury; ury = tmp;
1636
1637 // We need either the two lines that follow, or we need to subtract
1638 // min_x from real_translate_y, which is commented out below.
72cdf4c9
VZ
1639 llx = llx - (wxCoord)(m_minX*wx_printer_scale_y);
1640 urx = urx - (wxCoord)(m_minX*wx_printer_scale_y);
ed880dd4
RR
1641 }
1642
1643 // The Adobe specifications call for integers; we round as to make
1644 // the bounding larger.
72cdf4c9
VZ
1645 fprintf( m_pstream,
1646 "%%%%BoundingBox: %d %d %d %d\n",
1647 (wxCoord)floor((double)llx), (wxCoord)floor((double)lly),
1648 (wxCoord)ceil((double)urx), (wxCoord)ceil((double)ury) );
d7657f75
RR
1649 fprintf( m_pstream, "%%%%Pages: %d\n", (wxPageNumber - 1) );
1650 fprintf( m_pstream, "%%%%EndComments\n\n" );
ed880dd4
RR
1651
1652 // To check the correctness of the bounding box, postscript commands
1653 // to draw a box corresponding to the bounding box are generated below.
1654 // But since we typically don't want to print such a box, the postscript
1655 // commands are generated within comments. These lines appear before any
1656 // adjustment of scale, rotation, or translation, and hence are in the
1657 // default user coordinates.
d7657f75 1658 fprintf( m_pstream, "%% newpath\n" );
72cdf4c9
VZ
1659 fprintf( m_pstream, "%% %d %d moveto\n", llx, lly );
1660 fprintf( m_pstream, "%% %d %d lineto\n", urx, lly );
1661 fprintf( m_pstream, "%% %d %d lineto\n", urx, ury );
1662 fprintf( m_pstream, "%% %d %d lineto closepath stroke\n", llx, ury );
ed880dd4 1663
d7657f75
RR
1664 fclose( m_pstream );
1665 m_pstream = (FILE*) NULL;
ed880dd4 1666
87138c52 1667 wxChar *tmp_file = wxGetTempFileName("ps");
ed880dd4
RR
1668
1669 // Paste header Before wx_printer_file
d7657f75
RR
1670 wxConcatFiles (header_file, m_printData.GetFilename(), tmp_file );
1671 wxRemoveFile( header_file );
1672 wxRemoveFile( m_printData.GetFilename() );
1673 wxRenameFile( tmp_file, m_printData.GetFilename() );
ed880dd4
RR
1674
1675#if defined(__X__) || defined(__WXGTK__)
d7657f75 1676 if (m_ok)
ed880dd4 1677 {
d7657f75
RR
1678 wxString previewCommand(m_printData.GetPreviewCommand());
1679 wxString printerCommand(m_printData.GetPrinterCommand());
1680 wxString printerOptions(m_printData.GetPrinterOptions());
1681 wxString filename(m_printData.GetFilename());
ed880dd4 1682
72cdf4c9
VZ
1683 switch (m_printData.GetPrintMode())
1684 {
d7657f75
RR
1685 case wxPRINT_MODE_PREVIEW:
1686 {
1687 wxChar *argv[3];
1688 argv[0] = WXSTRINGCAST previewCommand;
1689 argv[1] = WXSTRINGCAST filename;
1690 argv[2] = (wxChar*) NULL;
1691 wxExecute( argv, TRUE );
1692 wxRemoveFile( m_printData.GetFilename() );
1693 }
1694 break;
1695 case wxPRINT_MODE_PRINTER:
1696 {
1697 wxChar *argv[4];
1698 int argc = 0;
1699 argv[argc++] = WXSTRINGCAST printerCommand;
1700
1701 // !SM! If we simply assign to argv[1] here, if printer options
1702 // are blank, we get an annoying and confusing message from lpr.
1703 wxChar *opts = WXSTRINGCAST printerOptions;
1704 if (opts && *opts)
1705 argv[argc++] = opts;
1706
1707 argv[argc++] = WXSTRINGCAST filename;
1708 argv[argc++] = (wxChar *) NULL;
1709 wxExecute( argv, TRUE );
1710 wxRemoveFile( filename );
1711 }
1712 break;
1713 case wxPRINT_MODE_FILE:
1714 case wxPRINT_MODE_NONE:
1715 break;
1716 }
ed880dd4
RR
1717 }
1718#endif
1719}
1720
d7657f75 1721void wxPostScriptDC::StartPage()
ed880dd4 1722{
223d09f6 1723 wxCHECK_RET( m_ok && m_pstream, wxT("invalid postscript dc") );
afce4c03 1724
d7657f75 1725 fprintf( m_pstream, "%%%%Page: %d\n", wxPageNumber++ );
afce4c03 1726
ac2def68 1727 // What is this one supposed to do? RR.
ed880dd4
RR
1728// *m_pstream << "matrix currentmatrix\n";
1729
1730 // Added by Chris Breeze
1731
9a6be59a
VZ
1732 // Each page starts with an "initgraphics" which resets the
1733 // transformation and so we need to reset the origin
1734 // (and rotate the page for landscape printing)
afce4c03 1735
afce4c03 1736 // Output scaling
72cdf4c9 1737 wxCoord translate_x, translate_y;
afce4c03 1738 double scale_x, scale_y;
afce4c03 1739
479cd5de
VZ
1740 translate_x = (wxCoord)m_printData.GetPrinterTranslateX();
1741 translate_y = (wxCoord)m_printData.GetPrinterTranslateY();
7bcb11d3
JS
1742
1743 scale_x = m_printData.GetPrinterScaleX();
1744 scale_y = m_printData.GetPrinterScaleY();
1745
1746 if (m_printData.GetOrientation() == wxLANDSCAPE)
afce4c03 1747 {
ac2def68
RR
1748 int h;
1749 GetSize( (int*) NULL, &h );
1750 translate_y -= h;
1751 fprintf( m_pstream, "90 rotate\n" );
72cdf4c9 1752
ac2def68 1753 // I copied this one from a PostScript tutorial, but to no avail. RR.
72cdf4c9 1754 // fprintf( m_pstream, "90 rotate llx neg ury nef translate\n" );
afce4c03
VZ
1755 }
1756
ac2def68 1757 fprintf( m_pstream, "%.8f %.8f scale\n", scale_x, scale_y );
72cdf4c9 1758 fprintf( m_pstream, "%d %d translate\n", translate_x, translate_y );
ed880dd4
RR
1759}
1760
1761void wxPostScriptDC::EndPage ()
1762{
223d09f6 1763 wxCHECK_RET( m_ok && m_pstream, wxT("invalid postscript dc") );
afce4c03 1764
d7657f75 1765 fprintf( m_pstream, "showpage\n" );
ed880dd4
RR
1766}
1767
72cdf4c9
VZ
1768bool wxPostScriptDC::DoBlit( wxCoord xdest, wxCoord ydest,
1769 wxCoord fwidth, wxCoord fheight,
afce4c03 1770 wxDC *source,
72cdf4c9 1771 wxCoord xsrc, wxCoord ysrc,
45b776d4 1772 int rop, bool WXUNUSED(useMask) )
ed880dd4 1773{
223d09f6 1774 wxCHECK_MSG( m_ok && m_pstream, FALSE, wxT("invalid postscript dc") );
afce4c03 1775
223d09f6 1776 wxCHECK_MSG( source, FALSE, wxT("invalid source dc") );
afce4c03 1777
342b6a2f 1778 /* blit into a bitmap */
afce4c03 1779 wxBitmap bitmap( (int)fwidth, (int)fheight );
45b776d4
JS
1780 wxMemoryDC memDC;
1781 memDC.SelectObject(bitmap);
342b6a2f 1782 memDC.Blit(0, 0, fwidth, fheight, source, xsrc, ysrc, rop); /* TODO: Blit transparently? */
45b776d4 1783 memDC.SelectObject(wxNullBitmap);
ea6f44b5
RR
1784
1785 /* draw bitmap. scaling and positioning is done there */
ea6f44b5 1786 DrawBitmap( bitmap, xdest, ydest );
afce4c03 1787
ed880dd4
RR
1788 return TRUE;
1789}
1790
72cdf4c9 1791wxCoord wxPostScriptDC::GetCharHeight() const
ed880dd4
RR
1792{
1793 if (m_font.Ok())
1794 return m_font.GetPointSize();
1795 else
1796 return 12;
1797}
1798
72cdf4c9
VZ
1799void wxPostScriptDC::DoGetTextExtent(const wxString& string,
1800 wxCoord *x, wxCoord *y,
1801 wxCoord *descent, wxCoord *externalLeading,
1802 wxFont *theFont ) const
ed880dd4 1803{
36b3b54a 1804 wxFont *fontToUse = theFont;
afce4c03 1805
36b3b54a
RR
1806 if (!fontToUse) fontToUse = (wxFont*) &m_font;
1807
223d09f6
KB
1808 wxCHECK_RET( fontToUse, wxT("GetTextExtent: no font defined") );
1809 wxCHECK_RET( x, wxT("GetTextExtent: x == NULL") );
1810 wxCHECK_RET( y, wxT("GetTextExtent: y == NULL") );
87138c52 1811
9626e0bf 1812 const wxWX2MBbuf strbuf = string.mb_str();
ed880dd4 1813
f036b31c 1814#if !wxUSE_AFM_FOR_POSTSCRIPT
36b3b54a
RR
1815 /* Provide a VERY rough estimate (avoid using it).
1816 * Produces accurate results for mono-spaced font
1817 * such as Courier (aka wxMODERN) */
afce4c03 1818
9a6be59a
VZ
1819 int height = 12;
1820 if (fontToUse)
1821 {
afce4c03 1822 height = fontToUse->GetPointSize();
9a6be59a 1823 }
72cdf4c9
VZ
1824 if ( x )
1825 *x = strlen (strbuf) * height * 72 / 120;
1826 if ( y )
1827 *y = (wxCoord) (height * 1.32); /* allow for descender */
36b3b54a
RR
1828 if (descent) *descent = 0;
1829 if (externalLeading) *externalLeading = 0;
ed880dd4 1830#else
ed880dd4 1831
afce4c03
VZ
1832 /* method for calculating string widths in postscript:
1833 / read in the AFM (adobe font metrics) file for the
1834 / actual font, parse it and extract the character widths
1835 / and also the descender. this may be improved, but for now
1836 / it works well. the AFM file is only read in if the
1837 / font is changed. this may be chached in the future.
1838 / calls to GetTextExtent with the font unchanged are rather
1839 / efficient!!!
1840 /
1841 / for each font and style used there is an AFM file necessary.
1842 / currently i have only files for the roman font family.
1843 / I try to get files for the other ones!
1844 /
1845 / CAVE: the size of the string is currently always calculated
1846 / in 'points' (1/72 of an inch). this should later on be
1847 / changed to depend on the mapping mode.
1848 / CAVE: the path to the AFM files must be set before calling this
1849 / function. this is usually done by a call like the following:
1850 / wxSetAFMPath("d:\\wxw161\\afm\\");
1851 /
1852 / example:
1853 /
1854 / wxPostScriptDC dc(NULL, TRUE);
1855 / if (dc.Ok()){
1856 / wxSetAFMPath("d:\\wxw161\\afm\\");
1857 / dc.StartDoc("Test");
1858 / dc.StartPage();
72cdf4c9 1859 / wxCoord w,h;
afce4c03
VZ
1860 / dc.SetFont(new wxFont(10, wxROMAN, wxNORMAL, wxNORMAL));
1861 / dc.GetTextExtent("Hallo",&w,&h);
1862 / dc.EndPage();
1863 / dc.EndDoc();
1864 / }
1865 /
1866 / by steve (stefan.hammes@urz.uni-heidelberg.de)
1867 / created: 10.09.94
1868 / updated: 14.05.95 */
36b3b54a
RR
1869
1870 /* these static vars are for storing the state between calls */
1871 static int lastFamily= INT_MIN;
1872 static int lastSize= INT_MIN;
1873 static int lastStyle= INT_MIN;
1874 static int lastWeight= INT_MIN;
1875 static int lastDescender = INT_MIN;
1876 static int lastWidths[256]; /* widths of the characters */
72cdf4c9 1877
a439ecef
VS
1878 double UnderlinePosition = 0.0;
1879 double UnderlineThickness = 0.0;
36b3b54a
RR
1880
1881 /* get actual parameters */
1882 const int Family = fontToUse->GetFamily();
1883 const int Size = fontToUse->GetPointSize();
1884 const int Style = fontToUse->GetStyle();
1885 const int Weight = fontToUse->GetWeight();
1886
1887 /* if we have another font, read the font-metrics */
1888 if (Family!=lastFamily || Size!=lastSize || Style!=lastStyle || Weight!=lastWeight)
1889 {
1890 /* store actual values */
1891 lastFamily = Family;
1892 lastSize = Size;
1893 lastStyle = Style;
1894 lastWeight = Weight;
1895
1896 char *name = (char*) NULL;
1897
1898 switch (Family)
afce4c03
VZ
1899 {
1900 case wxMODERN:
1901 {
1902 if ((Style == wxITALIC) && (Weight == wxBOLD)) name = "CourBoO";
1903 else if ((Style != wxITALIC) && (Weight == wxBOLD)) name = "CourBo";
1904 else if ((Style == wxITALIC) && (Weight != wxBOLD)) name = "Cour0";
1905 else name = "Cour";
1906 }
1907 break;
1908 case wxROMAN:
1909 {
1910 if ((Style == wxITALIC) && (Weight == wxBOLD)) name = "TimesBoO";
1911 else if ((Style != wxITALIC) && (Weight == wxBOLD)) name = "TimesBo";
1912 else if ((Style == wxITALIC) && (Weight != wxBOLD)) name = "TimesO";
72cdf4c9 1913 else name = "TimesRo";
afce4c03
VZ
1914 }
1915 break;
1916 default:
1917 {
1918 if ((Style == wxITALIC) && (Weight == wxBOLD)) name = "HelvBoO";
1919 else if ((Style != wxITALIC) && (Weight == wxBOLD)) name = "HelvBo";
1920 else if ((Style == wxITALIC) && (Weight != wxBOLD)) name = "Helv0";
1921 else name = "Helv";
1922 }
1923 break;
1924 }
36b3b54a
RR
1925
1926 /* get the directory of the AFM files */
5558ce7c 1927 wxString afmName = wxEmptyString;
72cdf4c9
VZ
1928 if (!m_printData.GetFontMetricPath().IsEmpty())
1929 {
021e0f21 1930 afmName = m_printData.GetFontMetricPath();
72cdf4c9 1931 }
36b3b54a 1932
afce4c03
VZ
1933 /* 2. open and process the file
1934 / a short explanation of the AFM format:
1935 / we have for each character a line, which gives its size
1936 / e.g.:
1937 /
1938 / C 63 ; WX 444 ; N question ; B 49 -14 395 676 ;
1939 /
1940 / that means, we have a character with ascii code 63, and width
1941 / (444/1000 * fontSize) points.
1942 / the other data is ignored for now!
1943 /
1944 / when the font has changed, we read in the right AFM file and store the
1945 / character widths in an array, which is processed below (see point 3.). */
36b3b54a
RR
1946
1947 /* new elements JC Sun Aug 25 23:21:44 MET DST 1996 */
1948
021e0f21
OK
1949 afmName << name << wxT(".afm");
1950 FILE *afmFile = wxFopen(afmName,wxT("r"));
d361e74e
KB
1951 if (afmFile==NULL)
1952 {
3e469ea3 1953 afmName = wxThePrintSetupData->GetAFMPath();
021e0f21
OK
1954 afmName << wxFILE_SEP_PATH << name << wxT(".afm");
1955 afmFile = wxFopen(afmName,wxT("r"));
d361e74e 1956 }
72cdf4c9 1957
385bcb35 1958#ifdef __UNIX__
79a8a691
JJ
1959#ifndef __VMS__
1960 if (afmFile==NULL)
5558ce7c
VS
1961 /* please do NOT change the line above to "else if (afmFile==NULL)" -
1962 - afmFile = fopen() may fail and in that case the next if branch
1963 MUST be executed - and it would not if there was "else" */
3e469ea3
KB
1964 {
1965 afmName = wxINSTALL_PREFIX;
1966 afmName << wxFILE_SEP_PATH
021e0f21
OK
1967 << wxT("share") << wxFILE_SEP_PATH
1968 << wxT("wx") << wxFILE_SEP_PATH
1969 << wxT("afm") << wxFILE_SEP_PATH
1970 << name << wxT(".afm");
1971 afmFile = wxFopen(afmName,wxT("r"));
3e469ea3 1972 }
79a8a691 1973#endif
3e469ea3 1974#endif
479cd5de 1975
385bcb35 1976 if (afmFile==NULL)
36b3b54a 1977 {
3e469ea3 1978 wxLogDebug( wxT("GetTextExtent: can't open AFM file '%hs'\n"), afmName.c_str() );
6eec2bee 1979 wxLogDebug( wxT(" using approximate values\n"));
36b3b54a
RR
1980 for (int i=0; i<256; i++) lastWidths[i] = 500; /* an approximate value */
1981 lastDescender = -150; /* dito. */
ed880dd4 1982 }
afce4c03
VZ
1983 else
1984 {
36b3b54a
RR
1985 /* init the widths array */
1986 for(int i=0; i<256; i++) lastWidths[i] = INT_MIN;
1987 /* some variables for holding parts of a line */
1988 char cString[10],semiString[10],WXString[10],descString[20];
1989 char upString[30], utString[30], encString[50];
1990 char line[256];
1991 int ascii,cWidth;
1992 /* read in the file and parse it */
1993 while(fgets(line,sizeof(line),afmFile)!=NULL)
afce4c03 1994 {
36b3b54a 1995 /* A.) check for descender definition */
021e0f21 1996 if (strncmp(line,"Descender",9)==0)
afce4c03 1997 {
36b3b54a 1998 if ((sscanf(line,"%s%d",descString,&lastDescender)!=2) ||
021e0f21 1999 (strcmp(descString,"Descender")!=0))
afce4c03 2000 {
3e469ea3 2001 wxLogDebug( wxT("AFM-file '%hs': line '%hs' has error (bad descender)\n"), afmName.c_str(),line );
36b3b54a
RR
2002 }
2003 }
2004 /* JC 1.) check for UnderlinePosition */
021e0f21 2005 else if(strncmp(line,"UnderlinePosition",17)==0)
afce4c03 2006 {
36b3b54a 2007 if ((sscanf(line,"%s%lf",upString,&UnderlinePosition)!=2) ||
021e0f21 2008 (strcmp(upString,"UnderlinePosition")!=0))
afce4c03 2009 {
3e469ea3 2010 wxLogDebug( wxT("AFM-file '%hs': line '%hs' has error (bad UnderlinePosition)\n"), afmName.c_str(), line );
36b3b54a
RR
2011 }
2012 }
afce4c03 2013 /* JC 2.) check for UnderlineThickness */
021e0f21 2014 else if(strncmp(line,"UnderlineThickness",18)==0)
afce4c03 2015 {
36b3b54a 2016 if ((sscanf(line,"%s%lf",utString,&UnderlineThickness)!=2) ||
021e0f21 2017 (strcmp(utString,"UnderlineThickness")!=0))
afce4c03 2018 {
3e469ea3 2019 wxLogDebug( wxT("AFM-file '%hs': line '%hs' has error (bad UnderlineThickness)\n"), afmName.c_str(), line );
36b3b54a
RR
2020 }
2021 }
afce4c03 2022 /* JC 3.) check for EncodingScheme */
021e0f21 2023 else if(strncmp(line,"EncodingScheme",14)==0)
afce4c03 2024 {
36b3b54a 2025 if ((sscanf(line,"%s%s",utString,encString)!=2) ||
021e0f21 2026 (strcmp(utString,"EncodingScheme")!=0))
afce4c03 2027 {
3e469ea3 2028 wxLogDebug( wxT("AFM-file '%hs': line '%hs' has error (bad EncodingScheme)\n"), afmName.c_str(), line );
36b3b54a 2029 }
021e0f21 2030 else if (strncmp(encString, "AdobeStandardEncoding", 21))
36b3b54a 2031 {
6eec2bee 2032 wxLogDebug( wxT("AFM-file '%hs': line '%hs' has error (unsupported EncodingScheme %hs)\n"),
3e469ea3 2033 afmName.c_str(),line, encString);
36b3b54a
RR
2034 }
2035 }
2036 /* B.) check for char-width */
021e0f21 2037 else if(strncmp(line,"C ",2)==0)
afce4c03 2038 {
36b3b54a 2039 if (sscanf(line,"%s%d%s%s%d",cString,&ascii,semiString,WXString,&cWidth)!=5)
afce4c03 2040 {
3e469ea3 2041 wxLogDebug(wxT("AFM-file '%hs': line '%hs' has an error (bad character width)\n"),afmName.c_str(),line);
36b3b54a 2042 }
021e0f21 2043 if(strcmp(cString,"C")!=0 || strcmp(semiString,";")!=0 || strcmp(WXString,"WX")!=0)
afce4c03 2044 {
3e469ea3 2045 wxLogDebug(wxT("AFM-file '%hs': line '%hs' has a format error\n"),afmName.c_str(),line);
36b3b54a
RR
2046 }
2047 /* printf(" char '%c'=%d has width '%d'\n",ascii,ascii,cWidth); */
2048 if (ascii>=0 && ascii<256)
afce4c03 2049 {
36b3b54a
RR
2050 lastWidths[ascii] = cWidth; /* store width */
2051 }
afce4c03
VZ
2052 else
2053 {
2054 /* MATTHEW: this happens a lot; don't print an error */
3e469ea3 2055 /* wxLogDebug("AFM-file '%s': ASCII value %d out of range\n",afmName.c_str(),ascii); */
36b3b54a
RR
2056 }
2057 }
2058 /* C.) ignore other entries. */
2059 }
2060 fclose(afmFile);
ed880dd4 2061 }
36b3b54a 2062 /* hack to compute correct values for german 'Umlaute'
afce4c03
VZ
2063 / the correct way would be to map the character names
2064 / like 'adieresis' to corresp. positions of ISOEnc and read
2065 / these values from AFM files, too. Maybe later ... */
36b3b54a
RR
2066