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