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