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