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