]> git.saurik.com Git - wxWidgets.git/blame - src/generic/dcpsg.cpp
Fix resizing of wxGrid columns when they were reordered.
[wxWidgets.git] / src / generic / dcpsg.cpp
CommitLineData
ed880dd4 1/////////////////////////////////////////////////////////////////////////////
389076f1 2// Name: src/generic/dcpsg.cpp
bf38cbff 3// Purpose: Generic wxPostScriptDC implementation
ed880dd4
RR
4// Author: Julian Smart, Robert Roebling, Markus Holzhem
5// Modified by:
6// Created: 04/01/98
7// RCS-ID: $Id$
6aa89a22 8// Copyright: (c) Julian Smart
65571936 9// Licence: wxWindows licence
ed880dd4
RR
10/////////////////////////////////////////////////////////////////////////////
11
9a6be59a
VZ
12#include "wx/wxprec.h"
13
14#ifdef __BORLANDC__
15 #pragma hdrstop
16#endif
17
88a7a4e1 18#if wxUSE_PRINTING_ARCHITECTURE && wxUSE_POSTSCRIPT
06cfab17 19
88a7a4e1 20#include "wx/generic/dcpsg.h"
ce4169a4 21
88a7a4e1
WS
22#ifndef WX_PRECOMP
23 #include "wx/intl.h"
e4db172a 24 #include "wx/log.h"
de6185e2 25 #include "wx/utils.h"
f38924e8 26 #include "wx/dcmemory.h"
18680f86 27 #include "wx/math.h"
155ecd4c 28 #include "wx/image.h"
f2e1afc9 29 #include "wx/icon.h"
88a7a4e1 30#endif // WX_PRECOMP
06cfab17 31
9a05fd8d 32#include "wx/prntbase.h"
8850cbd3 33#include "wx/generic/prntdlgg.h"
7bcb11d3 34#include "wx/paper.h"
1a7bfacc 35#include "wx/filename.h"
d36d1be9 36#include "wx/stdpaths.h"
57c208c5 37
bf38cbff
JS
38#ifdef __WXMSW__
39
40#ifdef DrawText
41#undef DrawText
42#endif
43
44#ifdef StartDoc
45#undef StartDoc
46#endif
47
48#ifdef GetCharWidth
49#undef GetCharWidth
50#endif
51
52#ifdef FindWindow
53#undef FindWindow
54#endif
55
56#endif
57
ed880dd4
RR
58//-----------------------------------------------------------------------------
59// start and end of document/page
60//-----------------------------------------------------------------------------
61
c31c771b
RR
62static const char *wxPostScriptHeaderConicTo = "\
63/conicto {\n\
64 /to_y exch def\n\
65 /to_x exch def\n\
66 /conic_cntrl_y exch def\n\
67 /conic_cntrl_x exch def\n\
68 currentpoint\n\
69 /p0_y exch def\n\
70 /p0_x exch def\n\
71 /p1_x p0_x conic_cntrl_x p0_x sub 2 3 div mul add def\n\
72 /p1_y p0_y conic_cntrl_y p0_y sub 2 3 div mul add def\n\
73 /p2_x p1_x to_x p0_x sub 1 3 div mul add def\n\
74 /p2_y p1_y to_y p0_y sub 1 3 div mul add def\n\
75 p1_x p1_y p2_x p2_y to_x to_y curveto\n\
76} bind def\n\
c31c771b 77";
a47391f3 78
ed880dd4
RR
79static const char *wxPostScriptHeaderEllipse = "\
80/ellipsedict 8 dict def\n\
81ellipsedict /mtrx matrix put\n\
82/ellipse {\n\
9a6be59a
VZ
83 ellipsedict begin\n\
84 /endangle exch def\n\
85 /startangle exch def\n\
86 /yrad exch def\n\
87 /xrad exch def\n\
88 /y exch def\n\
89 /x exch def\n\
90 /savematrix mtrx currentmatrix def\n\
91 x y translate\n\
92 xrad yrad scale\n\
93 0 0 1 startangle endangle arc\n\
94 savematrix setmatrix\n\
95 end\n\
96 } def\n\
ed880dd4
RR
97";
98
99static const char *wxPostScriptHeaderEllipticArc= "\
100/ellipticarcdict 8 dict def\n\
101ellipticarcdict /mtrx matrix put\n\
102/ellipticarc\n\
103{ ellipticarcdict begin\n\
104 /do_fill exch def\n\
105 /endangle exch def\n\
106 /startangle exch def\n\
107 /yrad exch def\n\
108 /xrad exch def \n\
109 /y exch def\n\
110 /x exch def\n\
111 /savematrix mtrx currentmatrix def\n\
112 x y translate\n\
113 xrad yrad scale\n\
114 do_fill { 0 0 moveto } if\n\
115 0 0 1 startangle endangle arc\n\
116 savematrix setmatrix\n\
117 do_fill { fill }{ stroke } ifelse\n\
118 end\n\
119} def\n";
120
121static const char *wxPostScriptHeaderSpline = "\
122/DrawSplineSection {\n\
9a6be59a
VZ
123 /y3 exch def\n\
124 /x3 exch def\n\
125 /y2 exch def\n\
126 /x2 exch def\n\
127 /y1 exch def\n\
128 /x1 exch def\n\
129 /xa x1 x2 x1 sub 0.666667 mul add def\n\
130 /ya y1 y2 y1 sub 0.666667 mul add def\n\
131 /xb x3 x2 x3 sub 0.666667 mul add def\n\
132 /yb y3 y2 y3 sub 0.666667 mul add def\n\
133 x1 y1 lineto\n\
134 xa ya xb yb x3 y3 curveto\n\
135 } def\n\
ed880dd4
RR
136";
137
138static const char *wxPostScriptHeaderColourImage = "\
244e5e34
VZ
139% define 'colorimage' if it isn't defined\n\
140% ('colortogray' and 'mergeprocs' come from xwd2ps\n\
141% via xgrab)\n\
142/colorimage where % do we know about 'colorimage'?\n\
143 { pop } % yes: pop off the 'dict' returned\n\
144 { % no: define one\n\
145 /colortogray { % define an RGB->I function\n\
146 /rgbdata exch store % call input 'rgbdata'\n\
ed880dd4
RR
147 rgbdata length 3 idiv\n\
148 /npixls exch store\n\
149 /rgbindx 0 store\n\
150 0 1 npixls 1 sub {\n\
151 grays exch\n\
244e5e34
VZ
152 rgbdata rgbindx get 20 mul % Red\n\
153 rgbdata rgbindx 1 add get 32 mul % Green\n\
154 rgbdata rgbindx 2 add get 12 mul % Blue\n\
155 add add 64 idiv % I = .5G + .31R + .18B\n\
ed880dd4
RR
156 put\n\
157 /rgbindx rgbindx 3 add store\n\
158 } for\n\
159 grays 0 npixls getinterval\n\
160 } bind def\n\
161\n\
244e5e34
VZ
162 % Utility procedure for colorimage operator.\n\
163 % This procedure takes two procedures off the\n\
164 % stack and merges them into a single procedure.\n\
ed880dd4 165\n\
244e5e34 166 /mergeprocs { % def\n\
ed880dd4
RR
167 dup length\n\
168 3 -1 roll\n\
169 dup\n\
170 length\n\
171 dup\n\
172 5 1 roll\n\
173 3 -1 roll\n\
174 add\n\
175 array cvx\n\
176 dup\n\
177 3 -1 roll\n\
178 0 exch\n\
179 putinterval\n\
180 dup\n\
181 4 2 roll\n\
182 putinterval\n\
183 } bind def\n\
184\n\
244e5e34
VZ
185 /colorimage { % def\n\
186 pop pop % remove 'false 3' operands\n\
ed880dd4
RR
187 {colortogray} mergeprocs\n\
188 image\n\
189 } bind def\n\
244e5e34 190 } ifelse % end of 'false' case\n\
ed880dd4
RR
191";
192
a243da29 193static const char wxPostScriptHeaderReencodeISO1[] =
ed880dd4
RR
194 "\n/reencodeISO {\n"
195"dup dup findfont dup length dict begin\n"
196"{ 1 index /FID ne { def }{ pop pop } ifelse } forall\n"
197"/Encoding ISOLatin1Encoding def\n"
198"currentdict end definefont\n"
199"} def\n"
200"/ISOLatin1Encoding [\n"
201"/.notdef/.notdef/.notdef/.notdef/.notdef/.notdef/.notdef/.notdef\n"
202"/.notdef/.notdef/.notdef/.notdef/.notdef/.notdef/.notdef/.notdef\n"
203"/.notdef/.notdef/.notdef/.notdef/.notdef/.notdef/.notdef/.notdef\n"
204"/.notdef/.notdef/.notdef/.notdef/.notdef/.notdef/.notdef/.notdef\n"
205"/space/exclam/quotedbl/numbersign/dollar/percent/ampersand/quoteright\n"
206"/parenleft/parenright/asterisk/plus/comma/minus/period/slash\n"
207"/zero/one/two/three/four/five/six/seven/eight/nine/colon/semicolon\n"
208"/less/equal/greater/question/at/A/B/C/D/E/F/G/H/I/J/K/L/M/N\n"
209"/O/P/Q/R/S/T/U/V/W/X/Y/Z/bracketleft/backslash/bracketright\n"
210"/asciicircum/underscore/quoteleft/a/b/c/d/e/f/g/h/i/j/k/l/m\n"
211"/n/o/p/q/r/s/t/u/v/w/x/y/z/braceleft/bar/braceright/asciitilde\n"
212"/.notdef/.notdef/.notdef/.notdef/.notdef/.notdef/.notdef/.notdef\n"
213"/.notdef/.notdef/.notdef/.notdef/.notdef/.notdef/.notdef/.notdef\n"
214"/.notdef/dotlessi/grave/acute/circumflex/tilde/macron/breve\n"
215"/dotaccent/dieresis/.notdef/ring/cedilla/.notdef/hungarumlaut\n";
216
a243da29 217static const char wxPostScriptHeaderReencodeISO2[] =
ed880dd4
RR
218"/ogonek/caron/space/exclamdown/cent/sterling/currency/yen/brokenbar\n"
219"/section/dieresis/copyright/ordfeminine/guillemotleft/logicalnot\n"
220"/hyphen/registered/macron/degree/plusminus/twosuperior/threesuperior\n"
221"/acute/mu/paragraph/periodcentered/cedilla/onesuperior/ordmasculine\n"
222"/guillemotright/onequarter/onehalf/threequarters/questiondown\n"
223"/Agrave/Aacute/Acircumflex/Atilde/Adieresis/Aring/AE/Ccedilla\n"
224"/Egrave/Eacute/Ecircumflex/Edieresis/Igrave/Iacute/Icircumflex\n"
225"/Idieresis/Eth/Ntilde/Ograve/Oacute/Ocircumflex/Otilde/Odieresis\n"
226"/multiply/Oslash/Ugrave/Uacute/Ucircumflex/Udieresis/Yacute\n"
227"/Thorn/germandbls/agrave/aacute/acircumflex/atilde/adieresis\n"
228"/aring/ae/ccedilla/egrave/eacute/ecircumflex/edieresis/igrave\n"
229"/iacute/icircumflex/idieresis/eth/ntilde/ograve/oacute/ocircumflex\n"
230"/otilde/odieresis/divide/oslash/ugrave/uacute/ucircumflex/udieresis\n"
231"/yacute/thorn/ydieresis\n"
232 "] def\n\n";
233
ed880dd4
RR
234//-------------------------------------------------------------------------------
235// wxPostScriptDC
236//-------------------------------------------------------------------------------
237
4f37154e 238
4f37154e
RR
239IMPLEMENT_DYNAMIC_CLASS(wxPostScriptDC, wxDC)
240
241wxPostScriptDC::wxPostScriptDC()
b1f0e54a 242 : wxDC(new wxPostScriptDCImpl(this))
4f37154e 243{
4f37154e
RR
244}
245
246wxPostScriptDC::wxPostScriptDC(const wxPrintData& printData)
b1f0e54a 247 : wxDC(new wxPostScriptDCImpl(this, printData))
4f37154e 248{
4f37154e
RR
249}
250
eb7637b5
RR
251// conversion
252static const double RAD2DEG = 180.0 / M_PI;
253
254// we don't want to use only 72 dpi from PS print
255static const int DPI = 600;
256static const double PS2DEV = 600.0 / 72.0;
257static const double DEV2PS = 72.0 / 600.0;
258
259#define XLOG2DEV(x) ((double)(LogicalToDeviceX(x)) * DEV2PS)
260#define XLOG2DEVREL(x) ((double)(LogicalToDeviceXRel(x)) * DEV2PS)
261#define YLOG2DEV(x) ((m_pageHeight - (double)LogicalToDeviceY(x)) * DEV2PS)
262#define YLOG2DEVREL(x) ((double)(LogicalToDeviceYRel(x)) * DEV2PS)
263
4f37154e 264
888dde65 265IMPLEMENT_ABSTRACT_CLASS(wxPostScriptDCImpl, wxDCImpl)
eba33006 266
eb7637b5 267//-------------------------------------------------------------------------------
b64de916 268
888dde65
RR
269wxPostScriptDCImpl::wxPostScriptDCImpl( wxPostScriptDC *owner ) :
270 wxDCImpl( owner )
b64de916 271{
4f37154e 272 Init();
b64de916 273
4f37154e 274 m_pageHeight = 842 * PS2DEV;
b1f0e54a 275
4f37154e 276 m_ok = true;
b64de916 277}
48c31b28 278
888dde65
RR
279wxPostScriptDCImpl::wxPostScriptDCImpl( wxPostScriptDC *owner, const wxPrintData& data ) :
280 wxDCImpl( owner )
ed880dd4 281{
4f37154e 282 Init();
b1f0e54a 283
4f37154e
RR
284 // this calculates m_pageHeight required for
285 // taking the inverted Y axis into account
286 SetPrintData( data );
afce4c03 287
4f37154e
RR
288 m_ok = true;
289}
afce4c03 290
afce4c03 291
888dde65
RR
292wxPostScriptDCImpl::wxPostScriptDCImpl( wxPrinterDC *owner ) :
293 wxDCImpl( owner )
4f37154e
RR
294{
295 Init();
ed880dd4 296
eb7637b5 297 m_pageHeight = 842 * PS2DEV;
b1f0e54a 298
4f37154e
RR
299 m_ok = true;
300}
301
888dde65
RR
302wxPostScriptDCImpl::wxPostScriptDCImpl( wxPrinterDC *owner, const wxPrintData& data ) :
303 wxDCImpl( owner )
4f37154e
RR
304{
305 Init();
b1f0e54a 306
4f37154e
RR
307 // this calculates m_pageHeight required for
308 // taking the inverted Y axis into account
309 SetPrintData( data );
310
311 m_ok = true;
ed880dd4
RR
312}
313
888dde65 314void wxPostScriptDCImpl::Init()
7bcb11d3 315{
d3b9f782 316 m_pstream = NULL;
7bcb11d3
JS
317
318 m_currentRed = 0;
319 m_currentGreen = 0;
320 m_currentBlue = 0;
321
322 m_pageNumber = 0;
323
ca65c044 324 m_clipping = false;
7bcb11d3
JS
325
326 m_underlinePosition = 0.0;
327 m_underlineThickness = 0.0;
328
7bcb11d3
JS
329}
330
888dde65 331wxPostScriptDCImpl::~wxPostScriptDCImpl ()
ed880dd4 332{
d7657f75
RR
333 if (m_pstream)
334 {
335 fclose( m_pstream );
d3b9f782 336 m_pstream = NULL;
d7657f75 337 }
ed880dd4
RR
338}
339
888dde65 340bool wxPostScriptDCImpl::IsOk() const
4bc67cc5 341{
ef539066 342 return m_ok;
4bc67cc5 343}
afce4c03 344
6d52ca53 345wxRect wxPostScriptDCImpl::GetPaperRect() const
4f37154e
RR
346{
347 int w = 0;
348 int h = 0;
349 DoGetSize( &w, &h );
350 return wxRect(0,0,w,h);
351}
352
6d52ca53 353int wxPostScriptDCImpl::GetResolution() const
4f37154e
RR
354{
355 return DPI;
356}
357
888dde65 358void wxPostScriptDCImpl::DoSetClippingRegion (wxCoord x, wxCoord y, wxCoord w, wxCoord h)
ed880dd4 359{
846051ec 360 wxCHECK_RET( m_ok , wxT("invalid postscript dc") );
afce4c03 361
4f37154e
RR
362 if (m_clipping)
363 DestroyClippingRegion();
ed880dd4 364
4f37154e
RR
365 m_clipX1 = x;
366 m_clipY1 = y;
367 m_clipX2 = x + w;
368 m_clipY2 = y + h;
afce4c03 369
ca65c044 370 m_clipping = true;
846051ec 371
eb7637b5
RR
372 wxString buffer;
373 buffer.Printf( "gsave\n"
374 "newpath\n"
375 "%f %f moveto\n"
376 "%f %f lineto\n"
377 "%f %f lineto\n"
378 "%f %f lineto\n"
379 "closepath clip newpath\n",
380 XLOG2DEV(x), YLOG2DEV(y),
381 XLOG2DEV(x+w), YLOG2DEV(y),
382 XLOG2DEV(x+w), YLOG2DEV(y+h),
383 XLOG2DEV(x), YLOG2DEV(y+h) );
384 buffer.Replace( ",", "." );
385 PsPrint( buffer );
ed880dd4
RR
386}
387
ed880dd4 388
888dde65 389void wxPostScriptDCImpl::DestroyClippingRegion()
ed880dd4 390{
846051ec 391 wxCHECK_RET( m_ok , wxT("invalid postscript dc") );
afce4c03 392
ed880dd4
RR
393 if (m_clipping)
394 {
ca65c044 395 m_clipping = false;
244e5e34 396 PsPrint( "grestore\n" );
ed880dd4 397 }
004fd0c8 398
888dde65 399 wxDCImpl::DestroyClippingRegion();
ed880dd4
RR
400}
401
888dde65 402void wxPostScriptDCImpl::Clear()
ed880dd4 403{
f632aa1e
JS
404 // This should fail silently to avoid unnecessary
405 // asserts
888dde65 406 // wxFAIL_MSG( wxT("wxPostScriptDCImpl::Clear not implemented.") );
ed880dd4
RR
407}
408
89efaf2b 409bool wxPostScriptDCImpl::DoFloodFill (wxCoord WXUNUSED(x), wxCoord WXUNUSED(y), const wxColour &WXUNUSED(col), wxFloodFillStyle WXUNUSED(style))
ed880dd4 410{
888dde65 411 wxFAIL_MSG( wxT("wxPostScriptDCImpl::FloodFill not implemented.") );
ca65c044 412 return false;
ed880dd4
RR
413}
414
888dde65 415bool wxPostScriptDCImpl::DoGetPixel (wxCoord WXUNUSED(x), wxCoord WXUNUSED(y), wxColour * WXUNUSED(col)) const
ed880dd4 416{
888dde65 417 wxFAIL_MSG( wxT("wxPostScriptDCImpl::GetPixel not implemented.") );
ca65c044 418 return false;
ed880dd4
RR
419}
420
888dde65 421void wxPostScriptDCImpl::DoCrossHair (wxCoord WXUNUSED(x), wxCoord WXUNUSED(y))
ed880dd4 422{
888dde65 423 wxFAIL_MSG( wxT("wxPostScriptDCImpl::CrossHair not implemented.") );
ed880dd4
RR
424}
425
888dde65 426void wxPostScriptDCImpl::DoDrawLine (wxCoord x1, wxCoord y1, wxCoord x2, wxCoord y2)
ed880dd4 427{
846051ec 428 wxCHECK_RET( m_ok, wxT("invalid postscript dc") );
afce4c03 429
e6777e65
VZ
430 if ( m_pen.IsTransparent() )
431 return;
afce4c03 432
ed880dd4 433 SetPen( m_pen );
afce4c03 434
eb7637b5
RR
435 wxString buffer;
436 buffer.Printf( "newpath\n"
437 "%f %f moveto\n"
438 "%f %f lineto\n"
439 "stroke\n",
440 XLOG2DEV(x1), YLOG2DEV(y1),
441 XLOG2DEV(x2), YLOG2DEV(y2) );
442 buffer.Replace( ",", "." );
443 PsPrint( buffer );
afce4c03 444
ed880dd4
RR
445 CalcBoundingBox( x1, y1 );
446 CalcBoundingBox( x2, y2 );
447}
448
888dde65 449void wxPostScriptDCImpl::DoDrawArc (wxCoord x1, wxCoord y1, wxCoord x2, wxCoord y2, wxCoord xc, wxCoord yc)
ed880dd4 450{
846051ec 451 wxCHECK_RET( m_ok, wxT("invalid postscript dc") );
afce4c03 452
72cdf4c9
VZ
453 wxCoord dx = x1 - xc;
454 wxCoord dy = y1 - yc;
eb7637b5 455 double radius = sqrt( (double)(dx*dx+dy*dy) );
ed880dd4
RR
456 double alpha1, alpha2;
457
afce4c03 458 if (x1 == x2 && y1 == y2)
ed880dd4 459 {
d7657f75
RR
460 alpha1 = 0.0;
461 alpha2 = 360.0;
72cdf4c9 462 }
c77a6796 463 else if ( wxIsNullDouble(radius) )
ed880dd4 464 {
c77a6796
VZ
465 alpha1 =
466 alpha2 = 0.0;
72cdf4c9 467 }
d7657f75 468 else
ed880dd4 469 {
d7657f75
RR
470 alpha1 = (x1 - xc == 0) ?
471 (y1 - yc < 0) ? 90.0 : -90.0 :
472 -atan2(double(y1-yc), double(x1-xc)) * RAD2DEG;
473 alpha2 = (x2 - xc == 0) ?
474 (y2 - yc < 0) ? 90.0 : -90.0 :
475 -atan2(double(y2-yc), double(x2-xc)) * RAD2DEG;
ed880dd4
RR
476 }
477 while (alpha1 <= 0) alpha1 += 360;
478 while (alpha2 <= 0) alpha2 += 360; // adjust angles to be between
479 while (alpha1 > 360) alpha1 -= 360; // 0 and 360 degree
480 while (alpha2 > 360) alpha2 -= 360;
481
eb7637b5
RR
482 int i_radius = wxRound( radius );
483
e6777e65 484 if ( m_brush.IsNonTransparent() )
ed880dd4 485 {
d7657f75 486 SetBrush( m_brush );
72cdf4c9 487
eb7637b5
RR
488 wxString buffer;
489 buffer.Printf( "newpath\n"
490 "%f %f %f %f %f %f ellipse\n"
491 "%f %f lineto\n"
492 "closepath\n"
493 "fill\n",
84ef8365
VZ
494 XLOG2DEV(xc), YLOG2DEV(yc),
495 XLOG2DEVREL(i_radius), YLOG2DEVREL(i_radius),
eb7637b5
RR
496 alpha1, alpha2,
497 XLOG2DEV(xc), YLOG2DEV(yc) );
498 buffer.Replace( ",", "." );
499 PsPrint( buffer );
d7657f75 500
eb7637b5
RR
501 CalcBoundingBox( xc-i_radius, yc-i_radius );
502 CalcBoundingBox( xc+i_radius, yc+i_radius );
ed880dd4 503 }
afce4c03 504
e6777e65 505 if ( m_pen.IsNonTransparent() )
ed880dd4 506 {
d7657f75 507 SetPen( m_pen );
84ef8365 508
eb7637b5
RR
509 wxString buffer;
510 buffer.Printf( "newpath\n"
511 "%f %f %f %f %f %f ellipse\n"
512 "%f %f lineto\n"
ed838581
RR
513 "closepath\n"
514 "stroke\n",
84ef8365
VZ
515 XLOG2DEV(xc), YLOG2DEV(yc),
516 XLOG2DEVREL(i_radius), YLOG2DEVREL(i_radius),
eb7637b5
RR
517 alpha1, alpha2,
518 XLOG2DEV(xc), YLOG2DEV(yc) );
519 buffer.Replace( ",", "." );
520 PsPrint( buffer );
72cdf4c9 521
eb7637b5
RR
522 CalcBoundingBox( xc-i_radius, yc-i_radius );
523 CalcBoundingBox( xc+i_radius, yc+i_radius );
ed880dd4 524 }
ed880dd4
RR
525}
526
888dde65 527void wxPostScriptDCImpl::DoDrawEllipticArc(wxCoord x,wxCoord y,wxCoord w,wxCoord h,double sa,double ea)
ed880dd4 528{
846051ec 529 wxCHECK_RET( m_ok, wxT("invalid postscript dc") );
afce4c03 530
c77a6796
VZ
531 if ( sa >= 360 || sa <= -360 )
532 sa -= int(sa/360)*360;
533 if ( ea >= 360 || ea <=- 360 )
534 ea -= int(ea/360)*360;
535 if ( sa < 0 )
536 sa += 360;
537 if ( ea < 0 )
538 ea += 360;
afce4c03 539
c77a6796 540 if ( wxIsSameDouble(sa, ea) )
ed880dd4 541 {
4f37154e 542 DoDrawEllipse(x,y,w,h);
ed880dd4
RR
543 return;
544 }
545
e6777e65 546 if ( m_brush.IsNonTransparent() )
ed880dd4
RR
547 {
548 SetBrush( m_brush );
72cdf4c9 549
eb7637b5
RR
550 wxString buffer;
551 buffer.Printf( "newpath\n"
552 "%f %f %f %f %f %f true ellipticarc\n",
553 XLOG2DEV(x+w/2), YLOG2DEV(y+h/2),
554 XLOG2DEVREL(w/2), YLOG2DEVREL(h/2),
555 sa, ea );
556 buffer.Replace( ",", "." );
557 PsPrint( buffer );
72cdf4c9 558
ed880dd4
RR
559 CalcBoundingBox( x ,y );
560 CalcBoundingBox( x+w, y+h );
72cdf4c9
VZ
561 }
562
e6777e65 563 if ( m_pen.IsNonTransparent() )
ed880dd4
RR
564 {
565 SetPen( m_pen );
566
eb7637b5
RR
567 wxString buffer;
568 buffer.Printf( "newpath\n"
569 "%f %f %f %f %f %f false ellipticarc\n",
570 XLOG2DEV(x+w/2), YLOG2DEV(y+h/2),
571 XLOG2DEVREL(w/2), YLOG2DEVREL(h/2),
ed838581 572 sa, ea );
eb7637b5
RR
573 buffer.Replace( ",", "." );
574 PsPrint( buffer );
72cdf4c9 575
d7657f75 576 CalcBoundingBox( x ,y );
ed880dd4
RR
577 CalcBoundingBox( x+w, y+h );
578 }
579}
580
888dde65 581void wxPostScriptDCImpl::DoDrawPoint (wxCoord x, wxCoord y)
ed880dd4 582{
846051ec 583 wxCHECK_RET( m_ok, wxT("invalid postscript dc") );
afce4c03 584
e6777e65
VZ
585 if ( m_pen.IsTransparent() )
586 return;
afce4c03 587
ed880dd4 588 SetPen (m_pen);
afce4c03 589
eb7637b5
RR
590 wxString buffer;
591 buffer.Printf( "newpath\n"
592 "%f %f moveto\n"
593 "%f %f lineto\n"
594 "stroke\n",
595 XLOG2DEV(x), YLOG2DEV(y),
596 XLOG2DEV(x+1), YLOG2DEV(y) );
597 buffer.Replace( ",", "." );
598 PsPrint( buffer );
afce4c03 599
ed880dd4
RR
600 CalcBoundingBox( x, y );
601}
602
89efaf2b 603void wxPostScriptDCImpl::DoDrawPolygon (int n, wxPoint points[], wxCoord xoffset, wxCoord yoffset, wxPolygonFillMode fillStyle)
ed880dd4 604{
846051ec 605 wxCHECK_RET( m_ok, wxT("invalid postscript dc") );
afce4c03 606
ed880dd4 607 if (n <= 0) return;
afce4c03 608
e6777e65 609 if ( m_brush.IsNonTransparent() )
ed880dd4 610 {
d7657f75
RR
611 SetBrush( m_brush );
612
244e5e34 613 PsPrint( "newpath\n" );
afce4c03 614
eb7637b5
RR
615 double xx = XLOG2DEV(points[0].x + xoffset);
616 double yy = YLOG2DEV(points[0].y + yoffset);
72cdf4c9 617
eb7637b5
RR
618 wxString buffer;
619 buffer.Printf( "%f %f moveto\n", xx, yy );
620 buffer.Replace( ",", "." );
621 PsPrint( buffer );
72cdf4c9 622
d7657f75 623 CalcBoundingBox( points[0].x + xoffset, points[0].y + yoffset );
ed880dd4 624
d7657f75
RR
625 for (int i = 1; i < n; i++)
626 {
eb7637b5
RR
627 xx = XLOG2DEV(points[i].x + xoffset);
628 yy = YLOG2DEV(points[i].y + yoffset);
72cdf4c9 629
eb7637b5
RR
630 buffer.Printf( "%f %f lineto\n", xx, yy );
631 buffer.Replace( ",", "." );
632 PsPrint( buffer );
ed880dd4 633
d7657f75
RR
634 CalcBoundingBox( points[i].x + xoffset, points[i].y + yoffset);
635 }
72cdf4c9 636
244e5e34 637 PsPrint( (fillStyle == wxODDEVEN_RULE ? "eofill\n" : "fill\n") );
ed880dd4
RR
638 }
639
e6777e65 640 if ( m_pen.IsNonTransparent() )
ed880dd4 641 {
d7657f75 642 SetPen( m_pen );
72cdf4c9 643
244e5e34 644 PsPrint( "newpath\n" );
ed880dd4 645
eb7637b5
RR
646 double xx = XLOG2DEV(points[0].x + xoffset);
647 double yy = YLOG2DEV(points[0].y + yoffset);
72cdf4c9 648
eb7637b5
RR
649 wxString buffer;
650 buffer.Printf( "%f %f moveto\n", xx, yy );
651 buffer.Replace( ",", "." );
652 PsPrint( buffer );
72cdf4c9 653
d7657f75 654 CalcBoundingBox( points[0].x + xoffset, points[0].y + yoffset );
ed880dd4 655
d7657f75
RR
656 for (int i = 1; i < n; i++)
657 {
eb7637b5
RR
658 xx = XLOG2DEV(points[i].x + xoffset);
659 yy = YLOG2DEV(points[i].y + yoffset);
72cdf4c9 660
eb7637b5
RR
661 buffer.Printf( "%f %f lineto\n", xx, yy );
662 buffer.Replace( ",", "." );
663 PsPrint( buffer );
72cdf4c9 664
d7657f75
RR
665 CalcBoundingBox( points[i].x + xoffset, points[i].y + yoffset);
666 }
ed880dd4 667
244e5e34
VZ
668 PsPrint( "closepath\n" );
669 PsPrint( "stroke\n" );
ed880dd4
RR
670 }
671}
672
89efaf2b 673void wxPostScriptDCImpl::DoDrawPolyPolygon (int n, int count[], wxPoint points[], wxCoord xoffset, wxCoord yoffset, wxPolygonFillMode fillStyle)
6e76b35d 674{
244e5e34 675 wxCHECK_RET( m_ok, wxT("invalid postscript dc") );
6e76b35d
VZ
676
677 if (n <= 0) return;
678
e6777e65 679 if ( m_brush.IsNonTransparent() )
6e76b35d
VZ
680 {
681 SetBrush( m_brush );
682
244e5e34 683 PsPrint( "newpath\n" );
6e76b35d
VZ
684
685 int ofs = 0;
793db755 686 for (int i = 0; i < n; ofs += count[i++])
6e76b35d 687 {
eb7637b5
RR
688 double xx = XLOG2DEV(points[ofs].x + xoffset);
689 double yy = YLOG2DEV(points[ofs].y + yoffset);
6e76b35d 690
eb7637b5
RR
691 wxString buffer;
692 buffer.Printf( "%f %f moveto\n", xx, yy );
693 buffer.Replace( ",", "." );
694 PsPrint( buffer );
6e76b35d
VZ
695
696 CalcBoundingBox( points[ofs].x + xoffset, points[ofs].y + yoffset );
697
793db755 698 for (int j = 1; j < count[i]; j++)
6e76b35d 699 {
eb7637b5
RR
700 xx = XLOG2DEV(points[ofs+j].x + xoffset);
701 yy = YLOG2DEV(points[ofs+j].y + yoffset);
6e76b35d 702
eb7637b5
RR
703 buffer.Printf( "%f %f lineto\n", xx, yy );
704 buffer.Replace( ",", "." );
705 PsPrint( buffer );
6e76b35d
VZ
706
707 CalcBoundingBox( points[ofs+j].x + xoffset, points[ofs+j].y + yoffset);
708 }
709 }
244e5e34 710 PsPrint( (fillStyle == wxODDEVEN_RULE ? "eofill\n" : "fill\n") );
6e76b35d
VZ
711 }
712
e6777e65 713 if ( m_pen.IsNonTransparent() )
6e76b35d
VZ
714 {
715 SetPen( m_pen );
716
244e5e34 717 PsPrint( "newpath\n" );
6e76b35d
VZ
718
719 int ofs = 0;
793db755 720 for (int i = 0; i < n; ofs += count[i++])
6e76b35d 721 {
eb7637b5
RR
722 double xx = XLOG2DEV(points[ofs].x + xoffset);
723 double yy = YLOG2DEV(points[ofs].y + yoffset);
6e76b35d 724
eb7637b5
RR
725 wxString buffer;
726 buffer.Printf( "%f %f moveto\n", xx, yy );
727 buffer.Replace( ",", "." );
728 PsPrint( buffer );
6e76b35d
VZ
729
730 CalcBoundingBox( points[ofs].x + xoffset, points[ofs].y + yoffset );
731
793db755 732 for (int j = 1; j < count[i]; j++)
6e76b35d 733 {
eb7637b5
RR
734 xx = XLOG2DEV(points[ofs+j].x + xoffset);
735 yy = YLOG2DEV(points[ofs+j].y + yoffset);
6e76b35d 736
eb7637b5
RR
737 buffer.Printf( "%f %f lineto\n", xx, yy );
738 buffer.Replace( ",", "." );
739 PsPrint( buffer );
6e76b35d
VZ
740
741 CalcBoundingBox( points[ofs+j].x + xoffset, points[ofs+j].y + yoffset);
742 }
743 }
244e5e34
VZ
744 PsPrint( "closepath\n" );
745 PsPrint( "stroke\n" );
6e76b35d
VZ
746 }
747}
748
888dde65 749void wxPostScriptDCImpl::DoDrawLines (int n, wxPoint points[], wxCoord xoffset, wxCoord yoffset)
ed880dd4 750{
846051ec 751 wxCHECK_RET( m_ok, wxT("invalid postscript dc") );
afce4c03 752
e6777e65
VZ
753 if ( m_pen.IsTransparent() )
754 return;
72cdf4c9 755
d7657f75 756 if (n <= 0) return;
afce4c03 757
ed880dd4 758 SetPen (m_pen);
afce4c03 759
9a6be59a
VZ
760 int i;
761 for ( i =0; i<n ; i++ )
eb7637b5
RR
762 CalcBoundingBox( points[i].x+xoffset, points[i].y+yoffset );
763
764 wxString buffer;
765 buffer.Printf( "newpath\n"
766 "%f %f moveto\n",
767 XLOG2DEV(points[0].x+xoffset),
768 YLOG2DEV(points[0].y+yoffset) );
769 buffer.Replace( ",", "." );
770 PsPrint( buffer );
84ef8365 771
244e5e34 772 for (i = 1; i < n; i++)
846051ec 773 {
eb7637b5
RR
774 buffer.Printf( "%f %f lineto\n",
775 XLOG2DEV(points[i].x+xoffset),
776 YLOG2DEV(points[i].y+yoffset) );
777 buffer.Replace( ",", "." );
778 PsPrint( buffer );
ed880dd4 779 }
ca65c044 780
244e5e34 781 PsPrint( "stroke\n" );
ed880dd4
RR
782}
783
888dde65 784void wxPostScriptDCImpl::DoDrawRectangle (wxCoord x, wxCoord y, wxCoord width, wxCoord height)
ed880dd4 785{
846051ec 786 wxCHECK_RET( m_ok, wxT("invalid postscript dc") );
84ef8365 787
728ddc45
RR
788 width--;
789 height--;
afce4c03 790
e6777e65 791 if ( m_brush.IsNonTransparent() )
ed880dd4
RR
792 {
793 SetBrush( m_brush );
72cdf4c9 794
eb7637b5
RR
795 wxString buffer;
796 buffer.Printf( "newpath\n"
797 "%f %f moveto\n"
798 "%f %f lineto\n"
799 "%f %f lineto\n"
800 "%f %f lineto\n"
801 "closepath\n"
802 "fill\n",
803 XLOG2DEV(x), YLOG2DEV(y),
804 XLOG2DEV(x + width), YLOG2DEV(y),
805 XLOG2DEV(x + width), YLOG2DEV(y + height),
806 XLOG2DEV(x), YLOG2DEV(y + height) );
807 buffer.Replace( ",", "." );
808 PsPrint( buffer );
ed880dd4
RR
809
810 CalcBoundingBox( x, y );
811 CalcBoundingBox( x + width, y + height );
812 }
afce4c03 813
e6777e65 814 if ( m_pen.IsNonTransparent() )
ed880dd4
RR
815 {
816 SetPen (m_pen);
817
eb7637b5
RR
818 wxString buffer;
819 buffer.Printf( "newpath\n"
820 "%f %f moveto\n"
821 "%f %f lineto\n"
822 "%f %f lineto\n"
823 "%f %f lineto\n"
824 "closepath\n"
825 "stroke\n",
826 XLOG2DEV(x), YLOG2DEV(y),
827 XLOG2DEV(x + width), YLOG2DEV(y),
828 XLOG2DEV(x + width), YLOG2DEV(y + height),
829 XLOG2DEV(x), YLOG2DEV(y + height) );
830 buffer.Replace( ",", "." );
831 PsPrint( buffer );
72cdf4c9 832
ed880dd4
RR
833 CalcBoundingBox( x, y );
834 CalcBoundingBox( x + width, y + height );
835 }
836}
837
888dde65 838void wxPostScriptDCImpl::DoDrawRoundedRectangle (wxCoord x, wxCoord y, wxCoord width, wxCoord height, double radius)
ed880dd4 839{
846051ec 840 wxCHECK_RET( m_ok, wxT("invalid postscript dc") );
afce4c03 841
728ddc45
RR
842 width--;
843 height--;
844
ed880dd4
RR
845 if (radius < 0.0)
846 {
847 // Now, a negative radius is interpreted to mean
848 // 'the proportion of the smallest X or Y dimension'
999836aa 849 double smallest = width < height ? width : height;
ed880dd4
RR
850 radius = (-radius * smallest);
851 }
afce4c03 852
72cdf4c9 853 wxCoord rad = (wxCoord) radius;
ed880dd4 854
e6777e65 855 if ( m_brush.IsNonTransparent() )
ed880dd4
RR
856 {
857 SetBrush( m_brush );
afce4c03 858
d7657f75 859 /* Draw rectangle anticlockwise */
eb7637b5
RR
860 wxString buffer;
861 buffer.Printf( "newpath\n"
862 "%f %f %f 90 180 arc\n"
863 "%f %f lineto\n"
864 "%f %f %f 180 270 arc\n"
865 "%f %f lineto\n"
866 "%f %f %f 270 0 arc\n"
867 "%f %f lineto\n"
868 "%f %f %f 0 90 arc\n"
869 "%f %f lineto\n"
870 "closepath\n"
871 "fill\n",
872 XLOG2DEV(x + rad), YLOG2DEV(y + rad), XLOG2DEVREL(rad),
873 XLOG2DEV(x), YLOG2DEV(y + height - rad),
874 XLOG2DEV(x + rad), YLOG2DEV(y + height - rad), XLOG2DEVREL(rad),
875 XLOG2DEV(x + width - rad), YLOG2DEV(y + height),
876 XLOG2DEV(x + width - rad), YLOG2DEV(y + height - rad), XLOG2DEVREL(rad),
877 XLOG2DEV(x + width), YLOG2DEV(y + rad),
878 XLOG2DEV(x + width - rad), YLOG2DEV(y + rad), XLOG2DEVREL(rad),
879 XLOG2DEV(x + rad), YLOG2DEV(y) );
880 buffer.Replace( ",", "." );
881 PsPrint( buffer );
ed880dd4
RR
882
883 CalcBoundingBox( x, y );
884 CalcBoundingBox( x + width, y + height );
885 }
afce4c03 886
e6777e65 887 if ( m_pen.IsNonTransparent() )
ed880dd4
RR
888 {
889 SetPen (m_pen);
afce4c03 890
d7657f75 891 /* Draw rectangle anticlockwise */
eb7637b5
RR
892 wxString buffer;
893 buffer.Printf( "newpath\n"
894 "%f %f %f 90 180 arc\n"
895 "%f %f lineto\n"
896 "%f %f %f 180 270 arc\n"
897 "%f %f lineto\n"
898 "%f %f %f 270 0 arc\n"
899 "%f %f lineto\n"
900 "%f %f %f 0 90 arc\n"
901 "%f %f lineto\n"
902 "closepath\n"
903 "stroke\n",
904 XLOG2DEV(x + rad), YLOG2DEV(y + rad), XLOG2DEVREL(rad),
905 XLOG2DEV(x), YLOG2DEV(y + height - rad),
906 XLOG2DEV(x + rad), YLOG2DEV(y + height - rad), XLOG2DEVREL(rad),
907 XLOG2DEV(x + width - rad), YLOG2DEV(y + height),
908 XLOG2DEV(x + width - rad), YLOG2DEV(y + height - rad), XLOG2DEVREL(rad),
909 XLOG2DEV(x + width), YLOG2DEV(y + rad),
910 XLOG2DEV(x + width - rad), YLOG2DEV(y + rad), XLOG2DEVREL(rad),
911 XLOG2DEV(x + rad), YLOG2DEV(y) );
912 buffer.Replace( ",", "." );
913 PsPrint( buffer );
ed880dd4
RR
914
915 CalcBoundingBox( x, y );
916 CalcBoundingBox( x + width, y + height );
917 }
918}
919
888dde65 920void wxPostScriptDCImpl::DoDrawEllipse (wxCoord x, wxCoord y, wxCoord width, wxCoord height)
ed880dd4 921{
846051ec 922 wxCHECK_RET( m_ok, wxT("invalid postscript dc") );
afce4c03 923
728ddc45
RR
924 width--;
925 height--;
926
e6777e65 927 if ( m_brush.IsNonTransparent() )
ed880dd4
RR
928 {
929 SetBrush (m_brush);
930
eb7637b5
RR
931 wxString buffer;
932 buffer.Printf( "newpath\n"
933 "%f %f %f %f 0 360 ellipse\n"
934 "fill\n",
935 XLOG2DEV(x + width / 2), YLOG2DEV(y + height / 2),
936 XLOG2DEVREL(width / 2), YLOG2DEVREL(height / 2) );
937 buffer.Replace( ",", "." );
938 PsPrint( buffer );
ed880dd4
RR
939
940 CalcBoundingBox( x - width, y - height );
941 CalcBoundingBox( x + width, y + height );
942 }
afce4c03 943
e6777e65 944 if ( m_pen.IsNonTransparent() )
ed880dd4
RR
945 {
946 SetPen (m_pen);
947
eb7637b5
RR
948 wxString buffer;
949 buffer.Printf( "newpath\n"
950 "%f %f %f %f 0 360 ellipse\n"
951 "stroke\n",
952 XLOG2DEV(x + width / 2), YLOG2DEV(y + height / 2),
953 XLOG2DEVREL(width / 2), YLOG2DEVREL(height / 2) );
954 buffer.Replace( ",", "." );
955 PsPrint( buffer );
ed880dd4
RR
956
957 CalcBoundingBox( x - width, y - height );
958 CalcBoundingBox( x + width, y + height );
959 }
960}
961
888dde65 962void wxPostScriptDCImpl::DoDrawIcon( const wxIcon& icon, wxCoord x, wxCoord y )
ed880dd4 963{
4f37154e 964 DoDrawBitmap( icon, x, y, true );
ed880dd4
RR
965}
966
d7657f75 967/* this has to be char, not wxChar */
a243da29 968static const char hexArray[] = "0123456789ABCDEF";
d7657f75 969
888dde65 970void wxPostScriptDCImpl::DoDrawBitmap( const wxBitmap& bitmap, wxCoord x, wxCoord y, bool WXUNUSED(useMask) )
ed880dd4 971{
846051ec 972 wxCHECK_RET( m_ok, wxT("invalid postscript dc") );
ef539066
RR
973
974 if (!bitmap.Ok()) return;
afce4c03 975
368d59f0 976 wxImage image = bitmap.ConvertToImage();
afce4c03 977
ef539066 978 if (!image.Ok()) return;
afce4c03 979
b64de916
VS
980 wxCoord w = image.GetWidth();
981 wxCoord h = image.GetHeight();
982
eb7637b5
RR
983 double ww = XLOG2DEVREL(image.GetWidth());
984 double hh = YLOG2DEVREL(image.GetHeight());
985
986 double xx = XLOG2DEV(x);
987 double yy = YLOG2DEV(y + bitmap.GetHeight());
988
989 wxString buffer;
990 buffer.Printf( "/origstate save def\n"
991 "20 dict begin\n"
ed838581
RR
992 "/pix %d string def\n"
993 "/grays %d string def\n"
eb7637b5
RR
994 "/npixels 0 def\n"
995 "/rgbindx 0 def\n"
996 "%f %f translate\n"
997 "%f %f scale\n"
ed838581
RR
998 "%d %d 8\n"
999 "[%d 0 0 %d 0 %d]\n"
eb7637b5
RR
1000 "{currentfile pix readhexstring pop}\n"
1001 "false 3 colorimage\n",
b64de916 1002 w, w, xx, yy, ww, hh, w, h, w, -h, h );
eb7637b5
RR
1003 buffer.Replace( ",", "." );
1004 PsPrint( buffer );
244e5e34
VZ
1005
1006 unsigned char* data = image.GetData();
1007
c56ae042 1008 // size of the buffer = width*rgb(3)*hexa(2)+'\n'
eb7637b5 1009 wxCharBuffer charbuffer(w*6 + 1);
244e5e34
VZ
1010 int firstDigit, secondDigit;
1011
1012 //rows
1013 for (int j = 0; j < h; j++)
1014 {
eb7637b5 1015 char* bufferindex = charbuffer.data();
244e5e34
VZ
1016
1017 //cols
1018 for (int i = 0; i < w*3; i++)
ef539066 1019 {
244e5e34
VZ
1020 firstDigit = (int)(*data/16.0);
1021 secondDigit = (int)(*data - (firstDigit*16.0));
1022 *(bufferindex++) = hexArray[firstDigit];
1023 *(bufferindex++) = hexArray[secondDigit];
1024
1025 data++;
d7657f75 1026 }
244e5e34
VZ
1027 *(bufferindex++) = '\n';
1028 *bufferindex = 0;
84ef8365 1029
ed838581
RR
1030 if (m_pstream)
1031 fwrite( charbuffer, 1, strlen( charbuffer ), m_pstream );
1032 else
1033 PsPrint( charbuffer );
ef539066 1034 }
244e5e34
VZ
1035
1036 PsPrint( "end\n" );
1037 PsPrint( "origstate restore\n" );
ed880dd4
RR
1038}
1039
888dde65 1040void wxPostScriptDCImpl::SetFont( const wxFont& font )
ed880dd4 1041{
846051ec 1042 wxCHECK_RET( m_ok, wxT("invalid postscript dc") );
afce4c03 1043
ed880dd4 1044 if (!font.Ok()) return;
afce4c03 1045
ed880dd4 1046 m_font = font;
57c208c5 1047
36b3b54a
RR
1048 int Style = m_font.GetStyle();
1049 int Weight = m_font.GetWeight();
1050
733b25e1 1051 const char *name;
d7657f75 1052 switch (m_font.GetFamily())
36b3b54a
RR
1053 {
1054 case wxTELETYPE:
1055 case wxMODERN:
733b25e1
RR
1056 {
1057 if (Style == wxITALIC)
1058 {
1059 if (Weight == wxBOLD)
1060 name = "/Courier-BoldOblique";
1061 else
1062 name = "/Courier-Oblique";
1063 }
1064 else
1065 {
1066 if (Weight == wxBOLD)
1067 name = "/Courier-Bold";
1068 else
1069 name = "/Courier";
1070 }
36b3b54a 1071 break;
733b25e1 1072 }
36b3b54a 1073 case wxROMAN:
733b25e1
RR
1074 {
1075 if (Style == wxITALIC)
1076 {
1077 if (Weight == wxBOLD)
1078 name = "/Times-BoldItalic";
1079 else
1080 name = "/Times-Italic";
1081 }
1082 else
1083 {
1084 if (Weight == wxBOLD)
1085 name = "/Times-Bold";
1086 else
1087 name = "/Times-Roman";
1088 }
36b3b54a 1089 break;
733b25e1 1090 }
36b3b54a 1091 case wxSCRIPT:
733b25e1
RR
1092 {
1093 name = "/ZapfChancery-MediumItalic";
36b3b54a 1094 break;
733b25e1
RR
1095 }
1096 case wxSWISS:
36b3b54a 1097 default:
733b25e1
RR
1098 {
1099 if (Style == wxITALIC)
1100 {
1101 if (Weight == wxBOLD)
1102 name = "/Helvetica-BoldOblique";
1103 else
1104 name = "/Helvetica-Oblique";
1105 }
1106 else
1107 {
1108 if (Weight == wxBOLD)
1109 name = "/Helvetica-Bold";
1110 else
1111 name = "/Helvetica";
1112 }
1113 break;
1114 }
36b3b54a 1115 }
57c208c5 1116
29402f45
JS
1117 // We may legitimately call SetFont before BeginDoc
1118 if (!m_pstream)
1119 return;
ca65c044 1120
244e5e34
VZ
1121 PsPrint( name );
1122 PsPrint( " reencodeISO def\n" );
1123 PsPrint( name );
1124 PsPrint( " findfont\n" );
ca65c044 1125
84ef8365 1126
12ca5586
VS
1127 float size = float(m_font.GetPointSize());
1128 size = size * GetFontPointSizeAdjustment(DPI);
eb7637b5 1129 wxString buffer;
12ca5586 1130 buffer.Printf( "%f scalefont setfont\n", size * m_scaleX );
eb7637b5 1131 buffer.Replace( ",", "." );
244e5e34 1132 PsPrint( buffer );
ed880dd4
RR
1133}
1134
888dde65 1135void wxPostScriptDCImpl::SetPen( const wxPen& pen )
ed880dd4 1136{
846051ec 1137 wxCHECK_RET( m_ok, wxT("invalid postscript dc") );
afce4c03 1138
ed880dd4 1139 if (!pen.Ok()) return;
afce4c03 1140
ed880dd4
RR
1141 int oldStyle = m_pen.GetStyle();
1142
1143 m_pen = pen;
1144
eb7637b5 1145 double width;
84ef8365 1146
eb7637b5
RR
1147 if (m_pen.GetWidth() <= 0)
1148 width = 0.1;
1149 else
1150 width = (double) m_pen.GetWidth();
1151
1152 wxString buffer;
1153 buffer.Printf( "%f setlinewidth\n", width * DEV2PS * m_scaleX );
1154 buffer.Replace( ",", "." );
244e5e34 1155 PsPrint( buffer );
f6bcfd97 1156
ed880dd4
RR
1157/*
1158 Line style - WRONG: 2nd arg is OFFSET
afce4c03 1159
ed880dd4
RR
1160 Here, I'm afraid you do not conceive meaning of parameters of 'setdash'
1161 operator correctly. You should look-up this in the Red Book: the 2nd parame-
1162 ter is not number of values in the array of the first one, but an offset
1163 into this description of the pattern. I mean a real *offset* not index
1164 into array. I.e. If the command is [3 4] 1 setdash is used, then there
72cdf4c9 1165 will be first black line *2* units wxCoord, then space 4 units, then the
ed880dd4
RR
1166 pattern of *3* units black, 4 units space will be repeated.
1167*/
1168
1169 static const char *dotted = "[2 5] 2";
1170 static const char *short_dashed = "[4 4] 2";
72cdf4c9 1171 static const char *wxCoord_dashed = "[4 8] 2";
ed880dd4
RR
1172 static const char *dotted_dashed = "[6 6 2 6] 4";
1173
999836aa
VZ
1174 const char *psdash;
1175
d7657f75 1176 switch (m_pen.GetStyle())
ed880dd4 1177 {
04ee05f9
PC
1178 case wxPENSTYLE_DOT: psdash = dotted; break;
1179 case wxPENSTYLE_SHORT_DASH: psdash = short_dashed; break;
1180 case wxPENSTYLE_LONG_DASH: psdash = wxCoord_dashed; break;
1181 case wxPENSTYLE_DOT_DASH: psdash = dotted_dashed; break;
1182 case wxPENSTYLE_USER_DASH:
f081c944
RR
1183 {
1184 wxDash *dashes;
1185 int nDashes = m_pen.GetDashes (&dashes);
1186 PsPrint ("[");
1187 for (int i = 0; i < nDashes; ++i)
1188 {
eb7637b5 1189 buffer.Printf( "%d ", dashes [i] );
f081c944
RR
1190 PsPrint( buffer );
1191 }
1192 PsPrint ("] 0 setdash\n");
88a7a4e1
WS
1193 psdash = 0;
1194 }
f081c944 1195 break;
04ee05f9
PC
1196 case wxPENSTYLE_SOLID:
1197 case wxPENSTYLE_TRANSPARENT:
ed880dd4
RR
1198 default: psdash = "[] 0"; break;
1199 }
afce4c03 1200
f081c944 1201 if ( psdash && (oldStyle != m_pen.GetStyle()) )
ed880dd4 1202 {
244e5e34
VZ
1203 PsPrint( psdash );
1204 PsPrint( " setdash\n" );
ed880dd4
RR
1205 }
1206
1207 // Line colour
1208 unsigned char red = m_pen.GetColour().Red();
1209 unsigned char blue = m_pen.GetColour().Blue();
1210 unsigned char green = m_pen.GetColour().Green();
1211
1212 if (!m_colour)
1213 {
1214 // Anything not white is black
72cdf4c9
VZ
1215 if (! (red == (unsigned char) 255 &&
1216 blue == (unsigned char) 255 &&
d7657f75
RR
1217 green == (unsigned char) 255) )
1218 {
1219 red = (unsigned char) 0;
1220 green = (unsigned char) 0;
1221 blue = (unsigned char) 0;
72cdf4c9 1222 }
d7657f75 1223 // setgray here ?
ed880dd4
RR
1224 }
1225
1226 if (!(red == m_currentRed && green == m_currentGreen && blue == m_currentBlue))
1227 {
d7657f75
RR
1228 double redPS = (double)(red) / 255.0;
1229 double bluePS = (double)(blue) / 255.0;
1230 double greenPS = (double)(green) / 255.0;
ca65c044 1231
eb7637b5
RR
1232 buffer.Printf( "%f %f %f setrgbcolor\n", redPS, greenPS, bluePS );
1233 buffer.Replace( ",", "." );
244e5e34 1234 PsPrint( buffer );
ca65c044 1235
d7657f75
RR
1236 m_currentRed = red;
1237 m_currentBlue = blue;
1238 m_currentGreen = green;
ed880dd4
RR
1239 }
1240}
1241
888dde65 1242void wxPostScriptDCImpl::SetBrush( const wxBrush& brush )
ed880dd4 1243{
846051ec 1244 wxCHECK_RET( m_ok, wxT("invalid postscript dc") );
afce4c03 1245
ed880dd4 1246 if (!brush.Ok()) return;
afce4c03 1247
ed880dd4
RR
1248 m_brush = brush;
1249
1250 // Brush colour
3c1b65a4
RR
1251 unsigned char red = m_brush.GetColour().Red();
1252 unsigned char blue = m_brush.GetColour().Blue();
1253 unsigned char green = m_brush.GetColour().Green();
ed880dd4
RR
1254
1255 if (!m_colour)
1256 {
d7657f75 1257 // Anything not white is black
72cdf4c9
VZ
1258 if (! (red == (unsigned char) 255 &&
1259 blue == (unsigned char) 255 &&
d7657f75
RR
1260 green == (unsigned char) 255) )
1261 {
1262 red = (unsigned char) 0;
1263 green = (unsigned char) 0;
1264 blue = (unsigned char) 0;
72cdf4c9 1265 }
d7657f75 1266 // setgray here ?
ed880dd4 1267 }
72cdf4c9 1268
ed880dd4
RR
1269 if (!(red == m_currentRed && green == m_currentGreen && blue == m_currentBlue))
1270 {
d7657f75
RR
1271 double redPS = (double)(red) / 255.0;
1272 double bluePS = (double)(blue) / 255.0;
1273 double greenPS = (double)(green) / 255.0;
72cdf4c9 1274
eb7637b5
RR
1275 wxString buffer;
1276 buffer.Printf( "%f %f %f setrgbcolor\n", redPS, greenPS, bluePS );
1277 buffer.Replace( ",", "." );
244e5e34 1278 PsPrint( buffer );
72cdf4c9 1279
d7657f75
RR
1280 m_currentRed = red;
1281 m_currentBlue = blue;
1282 m_currentGreen = green;
ed880dd4
RR
1283 }
1284}
1285
888dde65 1286void wxPostScriptDCImpl::DoDrawText( const wxString& text, wxCoord x, wxCoord y )
ed880dd4 1287{
846051ec 1288 wxCHECK_RET( m_ok, wxT("invalid postscript dc") );
479cd5de 1289
eb7637b5 1290 const wxWX2MBbuf textbuf = text.mb_str();
84ef8365 1291 if ( !textbuf )
eb7637b5 1292 return;
84ef8365 1293
a148cfb6
RR
1294 if (m_textForegroundColour.Ok())
1295 {
1296 unsigned char red = m_textForegroundColour.Red();
1297 unsigned char blue = m_textForegroundColour.Blue();
1298 unsigned char green = m_textForegroundColour.Green();
1299
1300 if (!m_colour)
1301 {
1302 // Anything not white is black
1303 if (! (red == (unsigned char) 255 &&
1304 blue == (unsigned char) 255 &&
1305 green == (unsigned char) 255))
1306 {
1307 red = (unsigned char) 0;
1308 green = (unsigned char) 0;
1309 blue = (unsigned char) 0;
1310 }
1311 }
1312
1313 // maybe setgray here ?
1314 if (!(red == m_currentRed && green == m_currentGreen && blue == m_currentBlue))
1315 {
1316 double redPS = (double)(red) / 255.0;
1317 double bluePS = (double)(blue) / 255.0;
1318 double greenPS = (double)(green) / 255.0;
1319
eb7637b5
RR
1320 wxString buffer;
1321 buffer.Printf( "%f %f %f setrgbcolor\n", redPS, greenPS, bluePS );
1322 buffer.Replace( ",", "." );
244e5e34 1323 PsPrint( buffer );
a148cfb6
RR
1324
1325 m_currentRed = red;
1326 m_currentBlue = blue;
1327 m_currentGreen = green;
1328 }
1329 }
a47391f3 1330
6c3d9ced 1331 wxCoord text_w, text_h, text_descent;
479cd5de 1332
4f37154e 1333 GetOwner()->GetTextExtent(text, &text_w, &text_h, &text_descent);
afce4c03 1334
ed880dd4
RR
1335 int size = m_font.GetPointSize();
1336
6c3d9ced
VS
1337// wxCoord by = y + (wxCoord)floor( double(size) * 2.0 / 3.0 ); // approximate baseline
1338// commented by V. Slavik and replaced by accurate version
1339// - note that there is still rounding error in text_descent!
1340 wxCoord by = y + size - text_descent; // baseline
244e5e34 1341
eb7637b5
RR
1342 wxString buffer;
1343 buffer.Printf( "%f %f moveto\n", XLOG2DEV(x), YLOG2DEV(by) );
1344 buffer.Replace( ",", "." );
1345 PsPrint( buffer );
244e5e34 1346 PsPrint( "(" );
ca65c044 1347
f99b00fa 1348 for ( const char *p = textbuf; *p != '\0'; p++ )
ed880dd4 1349 {
f99b00fa 1350 int c = (unsigned char)*p;
244e5e34 1351 if (c == ')' || c == '(' || c == '\\')
ed880dd4 1352 {
244e5e34
VZ
1353 /* Cope with special characters */
1354 PsPrint( "\\" );
eb7637b5 1355 PsPrint( (char) c );
ed880dd4 1356 }
244e5e34 1357 else if ( c >= 128 )
72cdf4c9 1358 {
244e5e34 1359 /* Cope with character codes > 127 */
eb7637b5
RR
1360 buffer.Printf( "\\%o", c );
1361 PsPrint( buffer );
244e5e34
VZ
1362 }
1363 else
1364 {
eb7637b5 1365 PsPrint( (char) c );
244e5e34
VZ
1366 }
1367 }
ca65c044 1368
244e5e34 1369 PsPrint( ") show\n" );
ca65c044 1370
244e5e34
VZ
1371 if (m_font.GetUnderlined())
1372 {
1373 wxCoord uy = (wxCoord)(y + size - m_underlinePosition);
84ef8365 1374
eb7637b5
RR
1375 buffer.Printf( "gsave\n"
1376 "%f %f moveto\n"
1377 "%f setlinewidth\n"
1378 "%f %f lineto\n"
1379 "stroke\n"
1380 "grestore\n",
1381 XLOG2DEV(x), YLOG2DEV(uy),
f6bcfd97 1382 m_underlineThickness,
eb7637b5
RR
1383 XLOG2DEV(x + text_w), YLOG2DEV(uy) );
1384 buffer.Replace( ",", "." );
244e5e34 1385 PsPrint( buffer );
ed880dd4 1386 }
afce4c03 1387
ed880dd4 1388 CalcBoundingBox( x, y );
88a7a4e1 1389 CalcBoundingBox( x + size * text.length() * 2/3 , y );
ed880dd4
RR
1390}
1391
888dde65 1392void wxPostScriptDCImpl::DoDrawRotatedText( const wxString& text, wxCoord x, wxCoord y, double angle )
95724b1a 1393{
c77a6796 1394 if ( wxIsNullDouble(angle) )
95724b1a
VZ
1395 {
1396 DoDrawText(text, x, y);
1397 return;
1398 }
1399
846051ec 1400 wxCHECK_RET( m_ok, wxT("invalid postscript dc") );
95724b1a
VZ
1401
1402 SetFont( m_font );
1403
1404 if (m_textForegroundColour.Ok())
1405 {
1406 unsigned char red = m_textForegroundColour.Red();
1407 unsigned char blue = m_textForegroundColour.Blue();
1408 unsigned char green = m_textForegroundColour.Green();
1409
1410 if (!m_colour)
1411 {
1412 // Anything not white is black
479cd5de
VZ
1413 if (! (red == (unsigned char) 255 &&
1414 blue == (unsigned char) 255 &&
1415 green == (unsigned char) 255))
95724b1a
VZ
1416 {
1417 red = (unsigned char) 0;
1418 green = (unsigned char) 0;
1419 blue = (unsigned char) 0;
1420 }
1421 }
1422
1423 // maybe setgray here ?
1424 if (!(red == m_currentRed && green == m_currentGreen && blue == m_currentBlue))
1425 {
1426 double redPS = (double)(red) / 255.0;
1427 double bluePS = (double)(blue) / 255.0;
1428 double greenPS = (double)(green) / 255.0;
479cd5de 1429
eb7637b5
RR
1430 wxString buffer;
1431 buffer.Printf( "%f %f %f setrgbcolor\n", redPS, greenPS, bluePS );
1432 buffer.Replace( ",", "." );
244e5e34 1433 PsPrint( buffer );
84ef8365 1434
95724b1a
VZ
1435 m_currentRed = red;
1436 m_currentBlue = blue;
1437 m_currentGreen = green;
1438 }
1439 }
1440
1441 int size = m_font.GetPointSize();
1442
eb7637b5
RR
1443 wxString buffer;
1444 buffer.Printf( "%f %f moveto\n", XLOG2DEV(x), YLOG2DEV(y));
1445 buffer.Replace( ",", "." );
1446 PsPrint( buffer );
ca65c044 1447
eb7637b5
RR
1448 buffer.Printf( "%f rotate\n", angle );
1449 buffer.Replace( ",", "." );
1450 PsPrint( buffer );
ca65c044 1451
244e5e34
VZ
1452 PsPrint( "(" );
1453 const wxWX2MBbuf textbuf = text.mb_str();
f99b00fa 1454 if ( textbuf )
244e5e34 1455 {
f99b00fa 1456 for ( const char *p = textbuf; *p != '\0'; p++ )
95724b1a 1457 {
f99b00fa
VZ
1458 int c = (unsigned char)*p;
1459 if (c == ')' || c == '(' || c == '\\')
1460 {
1461 /* Cope with special characters */
1462 PsPrint( "\\" );
1463 PsPrint( (char) c );
1464 }
1465 else if ( c >= 128 )
1466 {
1467 /* Cope with character codes > 127 */
1468 buffer.Printf( "\\%o", c);
1469 PsPrint( buffer );
1470 }
1471 else
1472 {
1473 PsPrint( (char) c );
1474 }
846051ec 1475 }
244e5e34 1476 }
ca65c044 1477
244e5e34 1478 PsPrint( ") show\n" );
ca65c044 1479
eb7637b5
RR
1480 buffer.Printf( "%f rotate\n", -angle );
1481 buffer.Replace( ",", "." );
244e5e34 1482 PsPrint( buffer );
ca65c044 1483
244e5e34
VZ
1484 if (m_font.GetUnderlined())
1485 {
1486 wxCoord uy = (wxCoord)(y + size - m_underlinePosition);
1487 wxCoord w, h;
4f37154e 1488 GetOwner()->GetTextExtent(text, &w, &h);
244e5e34 1489
84ef8365 1490 buffer.Printf(
846051ec 1491 "gsave\n"
eb7637b5 1492 "%f %f moveto\n"
846051ec 1493 "%f setlinewidth\n"
eb7637b5 1494 "%f %f lineto\n"
846051ec
JS
1495 "stroke\n"
1496 "grestore\n",
eb7637b5 1497 XLOG2DEV(x), YLOG2DEV(uy),
846051ec 1498 m_underlineThickness,
eb7637b5
RR
1499 XLOG2DEV(x + w), YLOG2DEV(uy) );
1500 buffer.Replace( ",", "." );
244e5e34 1501 PsPrint( buffer );
95724b1a 1502 }
ca65c044 1503
95724b1a 1504 CalcBoundingBox( x, y );
88a7a4e1 1505 CalcBoundingBox( x + size * text.length() * 2/3 , y );
95724b1a 1506}
ed880dd4 1507
888dde65 1508void wxPostScriptDCImpl::SetBackground (const wxBrush& brush)
ed880dd4
RR
1509{
1510 m_backgroundBrush = brush;
1511}
1512
89efaf2b 1513void wxPostScriptDCImpl::SetLogicalFunction(wxRasterOperationMode WXUNUSED(function))
ed880dd4 1514{
888dde65 1515 wxFAIL_MSG( wxT("wxPostScriptDCImpl::SetLogicalFunction not implemented.") );
ed880dd4
RR
1516}
1517
389076f1 1518#if wxUSE_SPLINES
888dde65 1519void wxPostScriptDCImpl::DoDrawSpline( const wxPointList *points )
ed880dd4 1520{
846051ec 1521 wxCHECK_RET( m_ok, wxT("invalid postscript dc") );
afce4c03 1522
ed880dd4
RR
1523 SetPen( m_pen );
1524
8a39593e 1525 // a and b are not used
846051ec 1526 //double a, b;
8a39593e 1527 double c, d, x1, y1, x2, y2, x3, y3;
ed880dd4
RR
1528 wxPoint *p, *q;
1529
b0d7707b
RR
1530 wxPointList::compatibility_iterator node = points->GetFirst();
1531 p = node->GetData();
afce4c03 1532 x1 = p->x;
ed880dd4
RR
1533 y1 = p->y;
1534
b1d4dd7a 1535 node = node->GetNext();
b0d7707b 1536 p = node->GetData();
afce4c03 1537 c = p->x;
ed880dd4 1538 d = p->y;
a47391f3 1539 x3 =
8a39593e 1540 #if 0
a47391f3
JS
1541 a =
1542 #endif
8a39593e 1543 (double)(x1 + c) / 2;
a47391f3 1544 y3 =
8a39593e 1545 #if 0
a47391f3
JS
1546 b =
1547 #endif
8a39593e 1548 (double)(y1 + d) / 2;
ed880dd4 1549
eb7637b5
RR
1550 wxString buffer;
1551 buffer.Printf( "newpath\n"
1552 "%f %f moveto\n"
1553 "%f %f lineto\n",
1554 XLOG2DEV(wxRound(x1)), YLOG2DEV(wxRound(y1)),
1555 XLOG2DEV(wxRound(x3)), YLOG2DEV(wxRound(y3)) );
1556 buffer.Replace( ",", "." );
1557 PsPrint( buffer );
afce4c03 1558
72cdf4c9
VZ
1559 CalcBoundingBox( (wxCoord)x1, (wxCoord)y1 );
1560 CalcBoundingBox( (wxCoord)x3, (wxCoord)y3 );
ed880dd4 1561
244e5e34
VZ
1562 node = node->GetNext();
1563 while (node)
ed880dd4 1564 {
b0d7707b 1565 q = node->GetData();
ed880dd4 1566
d7657f75
RR
1567 x1 = x3;
1568 y1 = y3;
1569 x2 = c;
1570 y2 = d;
1571 c = q->x;
1572 d = q->y;
ed880dd4
RR
1573 x3 = (double)(x2 + c) / 2;
1574 y3 = (double)(y2 + d) / 2;
72cdf4c9 1575
eb7637b5
RR
1576 buffer.Printf( "%f %f %f %f %f %f DrawSplineSection\n",
1577 XLOG2DEV(wxRound(x1)), YLOG2DEV(wxRound(y1)),
1578 XLOG2DEV(wxRound(x2)), YLOG2DEV(wxRound(y2)),
1579 XLOG2DEV(wxRound(x3)), YLOG2DEV(wxRound(y3)) );
1580 buffer.Replace( ",", "." );
1581 PsPrint( buffer );
72cdf4c9
VZ
1582
1583 CalcBoundingBox( (wxCoord)x1, (wxCoord)y1 );
1584 CalcBoundingBox( (wxCoord)x3, (wxCoord)y3 );
999836aa
VZ
1585
1586 node = node->GetNext();
ed880dd4 1587 }
afce4c03 1588
72cdf4c9
VZ
1589 /*
1590 At this point, (x2,y2) and (c,d) are the position of the
1591 next-to-last and last point respectively, in the point list
1592 */
ed880dd4 1593
eb7637b5
RR
1594 buffer.Printf( "%f %f lineto\nstroke\n", XLOG2DEV(wxRound(c)), YLOG2DEV(wxRound(d)) );
1595 buffer.Replace( ",", "." );
1596 PsPrint( buffer );
ed880dd4 1597}
389076f1 1598#endif // wxUSE_SPLINES
ed880dd4 1599
888dde65 1600wxCoord wxPostScriptDCImpl::GetCharWidth() const
ed880dd4
RR
1601{
1602 // Chris Breeze: reasonable approximation using wxMODERN/Courier
72cdf4c9 1603 return (wxCoord) (GetCharHeight() * 72.0 / 120.0);
ed880dd4
RR
1604}
1605
888dde65 1606void wxPostScriptDCImpl::SetPrintData(const wxPrintData& data)
eb7637b5 1607{
84ef8365 1608 m_printData = data;
eb7637b5
RR
1609
1610 wxPaperSize id = m_printData.GetPaperId();
1611 wxPrintPaperType *paper = wxThePrintPaperDatabase->FindPaperType(id);
1612 if (!paper) paper = wxThePrintPaperDatabase->FindPaperType(wxPAPER_A4);
1613 int w = 595;
1614 int h = 842;
1615 if (paper)
1616 {
1617 w = paper->GetSizeDeviceUnits().x;
1618 h = paper->GetSizeDeviceUnits().y;
1619 }
1620
1621 if (m_printData.GetOrientation() == wxLANDSCAPE)
1622 m_pageHeight = w * PS2DEV;
1623 else
1624 m_pageHeight = h * PS2DEV;
04ab8b6d 1625}
ed880dd4 1626
888dde65 1627void wxPostScriptDCImpl::ComputeScaleAndOrigin()
ed838581
RR
1628{
1629 const wxRealPoint origScale(m_scaleX, m_scaleY);
1630
888dde65 1631 wxDCImpl::ComputeScaleAndOrigin();
ed838581
RR
1632
1633 // If scale has changed call SetPen to recalulate the line width
1634 // and SetFont to recalculate font size
1635 if ( wxRealPoint(m_scaleX, m_scaleY) != origScale && m_pen.IsOk() )
1636 {
1637 SetPen( m_pen );
1638 SetFont( m_font );
1639 }
1640}
1641
888dde65 1642void wxPostScriptDCImpl::DoGetSize(int* width, int* height) const
ed880dd4 1643{
7bcb11d3 1644 wxPaperSize id = m_printData.GetPaperId();
ed880dd4 1645
7bcb11d3 1646 wxPrintPaperType *paper = wxThePrintPaperDatabase->FindPaperType(id);
afce4c03 1647
7bcb11d3 1648 if (!paper) paper = wxThePrintPaperDatabase->FindPaperType(wxPAPER_A4);
afce4c03 1649
ac2def68
RR
1650 int w = 595;
1651 int h = 842;
ed880dd4
RR
1652 if (paper)
1653 {
ac2def68
RR
1654 w = paper->GetSizeDeviceUnits().x;
1655 h = paper->GetSizeDeviceUnits().y;
ed880dd4 1656 }
72cdf4c9 1657
ac2def68 1658 if (m_printData.GetOrientation() == wxLANDSCAPE)
ed880dd4 1659 {
ac2def68 1660 int tmp = w;
72cdf4c9
VZ
1661 w = h;
1662 h = tmp;
ed880dd4 1663 }
72cdf4c9 1664
84ef8365 1665 if (width)
eb7637b5 1666 *width = wxRound( w * PS2DEV );
84ef8365
VZ
1667
1668 if (height)
eb7637b5 1669 *height = wxRound( h * PS2DEV );
ed880dd4
RR
1670}
1671
888dde65 1672void wxPostScriptDCImpl::DoGetSizeMM(int *width, int *height) const
ed880dd4 1673{
7bcb11d3
JS
1674 wxPaperSize id = m_printData.GetPaperId();
1675
1676 wxPrintPaperType *paper = wxThePrintPaperDatabase->FindPaperType(id);
afce4c03 1677
7bcb11d3
JS
1678 if (!paper) paper = wxThePrintPaperDatabase->FindPaperType(wxPAPER_A4);
1679
ac2def68
RR
1680 int w = 210;
1681 int h = 297;
7bcb11d3 1682 if (paper)
ed880dd4 1683 {
ac2def68
RR
1684 w = paper->GetWidth() / 10;
1685 h = paper->GetHeight() / 10;
ed880dd4 1686 }
72cdf4c9 1687
ac2def68 1688 if (m_printData.GetOrientation() == wxLANDSCAPE)
ed880dd4 1689 {
ac2def68 1690 int tmp = w;
72cdf4c9
VZ
1691 w = h;
1692 h = tmp;
ed880dd4 1693 }
72cdf4c9 1694
ac2def68
RR
1695 if (width) *width = w;
1696 if (height) *height = h;
7bcb11d3
JS
1697}
1698
1699// Resolution in pixels per logical inch
888dde65 1700wxSize wxPostScriptDCImpl::GetPPI(void) const
7bcb11d3 1701{
eb7637b5 1702 return wxSize( DPI, DPI );
7bcb11d3 1703}
ed880dd4 1704
7bcb11d3 1705
0c75b29e 1706bool wxPostScriptDCImpl::StartDoc( const wxString& WXUNUSED(message) )
7bcb11d3 1707{
ca65c044 1708 wxCHECK_MSG( m_ok, false, wxT("invalid postscript dc") );
32b13913 1709
6038ec8e 1710 if (m_printData.GetPrintMode() != wxPRINT_MODE_STREAM )
7bcb11d3 1711 {
ca65c044 1712 if (m_printData.GetFilename() == wxEmptyString)
244e5e34 1713 {
1a7bfacc 1714 wxString filename = wxFileName::CreateTempFileName( wxT("ps") );
244e5e34
VZ
1715 m_printData.SetFilename(filename);
1716 }
72cdf4c9 1717
392857fb 1718 m_pstream = wxFopen( m_printData.GetFilename(), wxT("w+") );
7bcb11d3 1719
244e5e34
VZ
1720 if (!m_pstream)
1721 {
1722 wxLogError( _("Cannot open file for PostScript printing!"));
ca65c044
WS
1723 m_ok = false;
1724 return false;
244e5e34 1725 }
ed880dd4 1726 }
afce4c03 1727
ca65c044 1728 m_ok = true;
ed880dd4 1729
eb7637b5
RR
1730 wxString buffer;
1731
244e5e34 1732 PsPrint( "%!PS-Adobe-2.0\n" );
84ef8365 1733
77ffb593 1734 PsPrint( "%%Creator: wxWidgets PostScript renderer\n" );
84ef8365 1735
eb7637b5
RR
1736 buffer.Printf( "%%%%CreationDate: %s\n", wxNow() );
1737 PsPrint( buffer );
84ef8365 1738
eba33006 1739 if (m_printData.GetOrientation() == wxLANDSCAPE)
244e5e34 1740 PsPrint( "%%Orientation: Landscape\n" );
eba33006 1741 else
244e5e34 1742 PsPrint( "%%Orientation: Portrait\n" );
a47391f3 1743
244e5e34 1744 const wxChar *paper;
3fc306e9
RR
1745 switch (m_printData.GetPaperId())
1746 {
670f9935
WS
1747 case wxPAPER_LETTER: paper = wxT("Letter"); break; // Letter: paper ""; 8 1/2 by 11 inches
1748 case wxPAPER_LEGAL: paper = wxT("Legal"); break; // Legal, 8 1/2 by 14 inches
1749 case wxPAPER_A4: paper = wxT("A4"); break; // A4 Sheet, 210 by 297 millimeters
244e5e34 1750 case wxPAPER_TABLOID: paper = wxT("Tabloid"); break; // Tabloid, 11 by 17 inches
670f9935
WS
1751 case wxPAPER_LEDGER: paper = wxT("Ledger"); break; // Ledger, 17 by 11 inches
1752 case wxPAPER_STATEMENT: paper = wxT("Statement"); break; // Statement, 5 1/2 by 8 1/2 inches
1753 case wxPAPER_EXECUTIVE: paper = wxT("Executive"); break; // Executive, 7 1/4 by 10 1/2 inches
1754 case wxPAPER_A3: paper = wxT("A3"); break; // A3 sheet, 297 by 420 millimeters
1755 case wxPAPER_A5: paper = wxT("A5"); break; // A5 sheet, 148 by 210 millimeters
1756 case wxPAPER_B4: paper = wxT("B4"); break; // B4 sheet, 250 by 354 millimeters
1757 case wxPAPER_B5: paper = wxT("B5"); break; // B5 sheet, 182-by-257-millimeter paper
1758 case wxPAPER_FOLIO: paper = wxT("Folio"); break; // Folio, 8-1/2-by-13-inch paper
1759 case wxPAPER_QUARTO: paper = wxT("Quaro"); break; // Quarto, 215-by-275-millimeter paper
1760 case wxPAPER_10X14: paper = wxT("10x14"); break; // 10-by-14-inch sheet
244e5e34 1761 default: paper = wxT("A4");
3fc306e9 1762 }
84ef8365 1763
eb7637b5
RR
1764 buffer.Printf( "%%%%DocumentPaperSizes: %s\n", paper );
1765 PsPrint( buffer );
84ef8365 1766
244e5e34
VZ
1767 PsPrint( "%%EndComments\n\n" );
1768
1769 PsPrint( "%%BeginProlog\n" );
1770 PsPrint( wxPostScriptHeaderConicTo );
1771 PsPrint( wxPostScriptHeaderEllipse );
1772 PsPrint( wxPostScriptHeaderEllipticArc );
1773 PsPrint( wxPostScriptHeaderColourImage );
244e5e34
VZ
1774 PsPrint( wxPostScriptHeaderReencodeISO1 );
1775 PsPrint( wxPostScriptHeaderReencodeISO2 );
d7657f75 1776 if (wxPostScriptHeaderSpline)
244e5e34
VZ
1777 PsPrint( wxPostScriptHeaderSpline );
1778 PsPrint( "%%EndProlog\n" );
d7657f75 1779
ed880dd4
RR
1780 SetBrush( *wxBLACK_BRUSH );
1781 SetPen( *wxBLACK_PEN );
1782 SetBackground( *wxWHITE_BRUSH );
1783 SetTextForeground( *wxBLACK );
1784
1785 // set origin according to paper size
1786 SetDeviceOrigin( 0,0 );
afce4c03 1787
ed880dd4 1788 m_pageNumber = 1;
ca65c044 1789 return true;
ed880dd4
RR
1790}
1791
888dde65 1792void wxPostScriptDCImpl::EndDoc ()
ed880dd4 1793{
244e5e34 1794 wxCHECK_RET( m_ok, wxT("invalid postscript dc") );
afce4c03 1795
ed880dd4
RR
1796 if (m_clipping)
1797 {
ca65c044 1798 m_clipping = false;
244e5e34 1799 PsPrint( "grestore\n" );
ed880dd4
RR
1800 }
1801
244e5e34
VZ
1802 if ( m_pstream ) {
1803 fclose( m_pstream );
d3b9f782 1804 m_pstream = NULL;
244e5e34 1805 }
ed880dd4 1806
a47391f3 1807#if 0
ed880dd4 1808 // THE FOLLOWING HAS BEEN CONTRIBUTED BY Andy Fyfe <andy@hyperparallel.com>
72cdf4c9 1809 wxCoord wx_printer_translate_x, wx_printer_translate_y;
ed880dd4 1810 double wx_printer_scale_x, wx_printer_scale_y;
ed880dd4 1811
479cd5de
VZ
1812 wx_printer_translate_x = (wxCoord)m_printData.GetPrinterTranslateX();
1813 wx_printer_translate_y = (wxCoord)m_printData.GetPrinterTranslateY();
7bcb11d3
JS
1814
1815 wx_printer_scale_x = m_printData.GetPrinterScaleX();
1816 wx_printer_scale_y = m_printData.GetPrinterScaleY();
1817
ed880dd4
RR
1818 // Compute the bounding box. Note that it is in the default user
1819 // coordinate system, thus we have to convert the values.
eb7637b5
RR
1820 wxCoord minX = (wxCoord) XLOG2DEV(m_minX);
1821 wxCoord minY = (wxCoord) YLOG2DEV(m_minY);
1822 wxCoord maxX = (wxCoord) XLOG2DEV(m_maxX);
1823 wxCoord maxY = (wxCoord) YLOG2DEV(m_maxY);
48c31b28 1824
a30f8da8
VS
1825 // LOG2DEV may have changed the minimum to maximum vice versa
1826 if ( minX > maxX ) { wxCoord tmp = minX; minX = maxX; maxX = tmp; }
1827 if ( minY > maxY ) { wxCoord tmp = minY; minY = maxY; maxY = tmp; }
48c31b28 1828
a30f8da8
VS
1829 // account for used scaling (boundingbox is before scaling in ps-file)
1830 double scale_x = m_printData.GetPrinterScaleX() / ms_PSScaleFactor;
1831 double scale_y = m_printData.GetPrinterScaleY() / ms_PSScaleFactor;
48c31b28
VZ
1832
1833 wxCoord llx, lly, urx, ury;
a30f8da8
VS
1834 llx = (wxCoord) ((minX+wx_printer_translate_x)*scale_x);
1835 lly = (wxCoord) ((minY+wx_printer_translate_y)*scale_y);
1836 urx = (wxCoord) ((maxX+wx_printer_translate_x)*scale_x);
1837 ury = (wxCoord) ((maxY+wx_printer_translate_y)*scale_y);
1838 // (end of bounding box computation)
1839
ed880dd4
RR
1840
1841 // If we're landscape, our sense of "x" and "y" is reversed.
7bcb11d3 1842 if (m_printData.GetOrientation() == wxLANDSCAPE)
ed880dd4 1843 {
72cdf4c9 1844 wxCoord tmp;
ed880dd4
RR
1845 tmp = llx; llx = lly; lly = tmp;
1846 tmp = urx; urx = ury; ury = tmp;
1847
1848 // We need either the two lines that follow, or we need to subtract
1849 // min_x from real_translate_y, which is commented out below.
72cdf4c9
VZ
1850 llx = llx - (wxCoord)(m_minX*wx_printer_scale_y);
1851 urx = urx - (wxCoord)(m_minX*wx_printer_scale_y);
ed880dd4
RR
1852 }
1853
1854 // The Adobe specifications call for integers; we round as to make
1855 // the bounding larger.
244e5e34 1856 PsPrintf( wxT("%%%%BoundingBox: %d %d %d %d\n"),
72cdf4c9
VZ
1857 (wxCoord)floor((double)llx), (wxCoord)floor((double)lly),
1858 (wxCoord)ceil((double)urx), (wxCoord)ceil((double)ury) );
ed880dd4
RR
1859
1860 // To check the correctness of the bounding box, postscript commands
1861 // to draw a box corresponding to the bounding box are generated below.
1862 // But since we typically don't want to print such a box, the postscript
1863 // commands are generated within comments. These lines appear before any
1864 // adjustment of scale, rotation, or translation, and hence are in the
1865 // default user coordinates.
244e5e34
VZ
1866 PsPrint( "% newpath\n" );
1867 PsPrintf( wxT("%% %d %d moveto\n"), llx, lly );
1868 PsPrintf( wxT("%% %d %d lineto\n"), urx, lly );
1869 PsPrintf( wxT("%% %d %d lineto\n"), urx, ury );
1870 PsPrintf( wxT("%% %d %d lineto closepath stroke\n"), llx, ury );
09c9194a 1871#endif
ed880dd4 1872
8850cbd3 1873#ifndef __WXMSW__
32b13913 1874 wxPostScriptPrintNativeData *data =
8850cbd3
RR
1875 (wxPostScriptPrintNativeData *) m_printData.GetNativeData();
1876
6038ec8e 1877 if (m_ok && (m_printData.GetPrintMode() == wxPRINT_MODE_PRINTER))
ed880dd4 1878 {
09c9194a 1879 wxString command;
8850cbd3 1880 command += data->GetPrinterCommand();
09c9194a 1881 command += wxT(" ");
8850cbd3 1882 command += data->GetPrinterOptions();
cb27bab1 1883 command += wxT(" ");
09c9194a 1884 command += m_printData.GetFilename();
ed880dd4 1885
ca65c044 1886 wxExecute( command, true );
09c9194a 1887 wxRemoveFile( m_printData.GetFilename() );
ed880dd4
RR
1888 }
1889#endif
1890}
1891
888dde65 1892void wxPostScriptDCImpl::StartPage()
ed880dd4 1893{
846051ec 1894 wxCHECK_RET( m_ok, wxT("invalid postscript dc") );
afce4c03 1895
eb7637b5
RR
1896 wxString buffer;
1897 buffer.Printf( wxT("%%%%Page: %d\n"), m_pageNumber++ );
1898 PsPrint( buffer );
afce4c03 1899
eb7637b5 1900#if 0
32b13913 1901 wxPostScriptPrintNativeData *data =
8850cbd3
RR
1902 (wxPostScriptPrintNativeData *) m_printData.GetNativeData();
1903
eb7637b5
RR
1904 wxCoord translate_x = (wxCoord)data->GetPrinterTranslateX();
1905 wxCoord translate_y = (wxCoord)data->GetPrinterTranslateY();
7bcb11d3 1906
eb7637b5
RR
1907 buffer.Printf( "%d %d translate\n", translate_x, translate_y );
1908 PsPrint( buffer );
84ef8365 1909
eb7637b5
RR
1910 double scale_x = data->GetPrinterScaleX();
1911 double scale_y = data->GetPrinterScaleY();
84ef8365 1912
eb7637b5
RR
1913 buffer.Printf( "%f %f scale\n", scale_x, scale_y );
1914 buffer.Replace( ",", "." );
1915 PsPrint( buffer );
84ef8365 1916
eb7637b5 1917#endif
7bcb11d3 1918
eb7637b5 1919 // Each page starts with an "initgraphics" which resets the
84ef8365 1920 // transformation and so we need to rotate the page for
eb7637b5
RR
1921 // landscape printing)
1922
1923 // I copied this one from a PostScript tutorial, but to no avail. RR.
1924 // PsPrint( "90 rotate llx neg ury nef translate\n" );
84ef8365 1925
7bcb11d3 1926 if (m_printData.GetOrientation() == wxLANDSCAPE)
244e5e34 1927 PsPrint( "90 rotate\n" );
ed880dd4
RR
1928}
1929
888dde65 1930void wxPostScriptDCImpl::EndPage ()
ed880dd4 1931{
846051ec 1932 wxCHECK_RET( m_ok , wxT("invalid postscript dc") );
afce4c03 1933
244e5e34 1934 PsPrint( "showpage\n" );
ed880dd4
RR
1935}
1936
888dde65 1937bool wxPostScriptDCImpl::DoBlit( wxCoord xdest, wxCoord ydest,
72cdf4c9 1938 wxCoord fwidth, wxCoord fheight,
afce4c03 1939 wxDC *source,
72cdf4c9 1940 wxCoord xsrc, wxCoord ysrc,
03647350 1941 wxRasterOperationMode rop,
89efaf2b 1942 bool WXUNUSED(useMask), wxCoord WXUNUSED(xsrcMask), wxCoord WXUNUSED(ysrcMask) )
ed880dd4 1943{
ca65c044 1944 wxCHECK_MSG( m_ok, false, wxT("invalid postscript dc") );
afce4c03 1945
ca65c044 1946 wxCHECK_MSG( source, false, wxT("invalid source dc") );
afce4c03 1947
4f37154e 1948 // blit into a bitmap
afce4c03 1949 wxBitmap bitmap( (int)fwidth, (int)fheight );
45b776d4
JS
1950 wxMemoryDC memDC;
1951 memDC.SelectObject(bitmap);
342b6a2f 1952 memDC.Blit(0, 0, fwidth, fheight, source, xsrc, ysrc, rop); /* TODO: Blit transparently? */
45b776d4 1953 memDC.SelectObject(wxNullBitmap);
ea6f44b5 1954
4f37154e
RR
1955 //draw bitmap. scaling and positioning is done there
1956 GetOwner()->DrawBitmap( bitmap, xdest, ydest );
afce4c03 1957
ca65c044 1958 return true;
ed880dd4
RR
1959}
1960
888dde65 1961wxCoord wxPostScriptDCImpl::GetCharHeight() const
ed880dd4
RR
1962{
1963 if (m_font.Ok())
b64de916 1964 return m_font.GetPointSize();
ed880dd4
RR
1965 else
1966 return 12;
1967}
1968
888dde65 1969void wxPostScriptDCImpl::PsPrint( const wxString& str )
eb7637b5
RR
1970{
1971 const wxCharBuffer psdata(str.utf8_str());
1972
1973 wxPostScriptPrintNativeData *data =
1974 (wxPostScriptPrintNativeData *) m_printData.GetNativeData();
1975
1976 switch (m_printData.GetPrintMode())
1977 {
1978#if wxUSE_STREAMS
1979 // append to output stream
1980 case wxPRINT_MODE_STREAM:
1981 {
1982 wxOutputStream* outputstream = data->GetOutputStream();
1983 wxCHECK_RET( outputstream, wxT("invalid outputstream") );
1984 outputstream->Write( psdata, strlen( psdata ) );
1985 }
1986 break;
1987#endif // wxUSE_STREAMS
1988
1989 // save data into file
1990 default:
1991 wxCHECK_RET( m_pstream, wxT("invalid postscript dc") );
1992 fwrite( psdata, 1, strlen( psdata ), m_pstream );
1993 }
1994}
1995
888dde65 1996void wxPostScriptDCImpl::DoGetTextExtent(const wxString& string,
72cdf4c9
VZ
1997 wxCoord *x, wxCoord *y,
1998 wxCoord *descent, wxCoord *externalLeading,
c94f845b 1999 const wxFont *theFont ) const
ed880dd4 2000{
c94f845b 2001 const wxFont *fontToUse = theFont;
afce4c03 2002
c94f845b 2003 if (!fontToUse) fontToUse = &m_font;
36b3b54a 2004
12ca5586
VS
2005 const float fontSize =
2006 fontToUse->GetPointSize() * GetFontPointSizeAdjustment(72.0);
87138c52 2007
32b13913 2008 if (string.empty())
3fc306e9
RR
2009 {
2010 if (x) (*x) = 0;
2011 if (y) (*y) = 0;
272995af
JS
2012 if (descent) (*descent) = 0;
2013 if (externalLeading) (*externalLeading) = 0;
3fc306e9
RR
2014 return;
2015 }
a47391f3 2016
3fc306e9
RR
2017 // GTK 2.0
2018
9626e0bf 2019 const wxWX2MBbuf strbuf = string.mb_str();
84ef8365 2020
eb7637b5 2021 // conversion failed (non e.g. ISO characters)
84ef8365 2022 if ( !strbuf )
eb7637b5 2023 return;
ed880dd4 2024
f036b31c 2025#if !wxUSE_AFM_FOR_POSTSCRIPT
36b3b54a
RR
2026 /* Provide a VERY rough estimate (avoid using it).
2027 * Produces accurate results for mono-spaced font
2028 * such as Courier (aka wxMODERN) */
afce4c03 2029
72cdf4c9 2030 if ( x )
12ca5586 2031 *x = strlen (strbuf) * fontSize * 72.0 / 120.0;
72cdf4c9 2032 if ( y )
12ca5586 2033 *y = (wxCoord) (fontSize * 1.32); /* allow for descender */
36b3b54a
RR
2034 if (descent) *descent = 0;
2035 if (externalLeading) *externalLeading = 0;
ed880dd4 2036#else
ed880dd4 2037
afce4c03
VZ
2038 /* method for calculating string widths in postscript:
2039 / read in the AFM (adobe font metrics) file for the
2040 / actual font, parse it and extract the character widths
2041 / and also the descender. this may be improved, but for now
2042 / it works well. the AFM file is only read in if the
2043 / font is changed. this may be chached in the future.
2044 / calls to GetTextExtent with the font unchanged are rather
2045 / efficient!!!
2046 /
2047 / for each font and style used there is an AFM file necessary.
2048 / currently i have only files for the roman font family.
2049 / I try to get files for the other ones!
2050 /
2051 / CAVE: the size of the string is currently always calculated
2052 / in 'points' (1/72 of an inch). this should later on be
2053 / changed to depend on the mapping mode.
2054 / CAVE: the path to the AFM files must be set before calling this
eb7637b5 2055 / fun3B3Bction. this is usually done by a call like the following:
afce4c03
VZ
2056 / wxSetAFMPath("d:\\wxw161\\afm\\");
2057 /
2058 / example:
2059 /
ca65c044 2060 / wxPostScriptDC dc(NULL, true);
afce4c03
VZ
2061 / if (dc.Ok()){
2062 / wxSetAFMPath("d:\\wxw161\\afm\\");
2063 / dc.StartDoc("Test");
2064 / dc.StartPage();
72cdf4c9 2065 / wxCoord w,h;
afce4c03
VZ
2066 / dc.SetFont(new wxFont(10, wxROMAN, wxNORMAL, wxNORMAL));
2067 / dc.GetTextExtent("Hallo",&w,&h);
2068 / dc.EndPage();
2069 / dc.EndDoc();
2070 / }
2071 /
2072 / by steve (stefan.hammes@urz.uni-heidelberg.de)
2073 / created: 10.09.94
2074 / updated: 14.05.95 */
36b3b54a
RR
2075
2076 /* these static vars are for storing the state between calls */
2077 static int lastFamily= INT_MIN;
2078 static int lastSize= INT_MIN;
2079 static int lastStyle= INT_MIN;
2080 static int lastWeight= INT_MIN;
2081 static int lastDescender = INT_MIN;
2082 static int lastWidths[256]; /* widths of the characters */
72cdf4c9 2083
a439ecef
VS
2084 double UnderlinePosition = 0.0;
2085 double UnderlineThickness = 0.0;
36b3b54a 2086
eba33006 2087 // Get actual parameters
733b25e1
RR
2088 int Family = fontToUse->GetFamily();
2089 int Size = fontToUse->GetPointSize();
2090 int Style = fontToUse->GetStyle();
2091 int Weight = fontToUse->GetWeight();
36b3b54a 2092
eba33006 2093 // If we have another font, read the font-metrics
36b3b54a
RR
2094 if (Family!=lastFamily || Size!=lastSize || Style!=lastStyle || Weight!=lastWeight)
2095 {
eba33006 2096 // Store actual values
36b3b54a
RR
2097 lastFamily = Family;
2098 lastSize = Size;
2099 lastStyle = Style;
2100 lastWeight = Weight;
2101
999836aa 2102 const wxChar *name;
36b3b54a
RR
2103
2104 switch (Family)
afce4c03
VZ
2105 {
2106 case wxMODERN:
733b25e1
RR
2107 case wxTELETYPE:
2108 {
2b5f62a0
VZ
2109 if ((Style == wxITALIC) && (Weight == wxBOLD)) name = wxT("CourBoO.afm");
2110 else if ((Style != wxITALIC) && (Weight == wxBOLD)) name = wxT("CourBo.afm");
2111 else if ((Style == wxITALIC) && (Weight != wxBOLD)) name = wxT("CourO.afm");
2112 else name = wxT("Cour.afm");
afce4c03 2113 break;
733b25e1 2114 }
afce4c03 2115 case wxROMAN:
733b25e1 2116 {
2b5f62a0
VZ
2117 if ((Style == wxITALIC) && (Weight == wxBOLD)) name = wxT("TimesBoO.afm");
2118 else if ((Style != wxITALIC) && (Weight == wxBOLD)) name = wxT("TimesBo.afm");
2119 else if ((Style == wxITALIC) && (Weight != wxBOLD)) name = wxT("TimesO.afm");
2120 else name = wxT("TimesRo.afm");
afce4c03 2121 break;
733b25e1
RR
2122 }
2123 case wxSCRIPT:
2124 {
2b5f62a0 2125 name = wxT("Zapf.afm");
999836aa 2126 break;
733b25e1
RR
2127 }
2128 case wxSWISS:
afce4c03 2129 default:
733b25e1 2130 {
2b5f62a0
VZ
2131 if ((Style == wxITALIC) && (Weight == wxBOLD)) name = wxT("HelvBoO.afm");
2132 else if ((Style != wxITALIC) && (Weight == wxBOLD)) name = wxT("HelvBo.afm");
2133 else if ((Style == wxITALIC) && (Weight != wxBOLD)) name = wxT("HelvO.afm");
2134 else name = wxT("Helv.afm");
afce4c03 2135 break;
733b25e1 2136 }
afce4c03 2137 }
36b3b54a 2138
eba33006 2139 FILE *afmFile = NULL;
a47391f3 2140
d36d1be9
VZ
2141 // Get the directory of the AFM files
2142 wxString afmName;
2143
2144 // VZ: I don't know if the cast always works under Unix but it clearly
2145 // never does under Windows where the pointer is
2146 // wxWindowsPrintNativeData and so calling GetFontMetricPath() on
2147 // it just crashes
2148#ifndef __WIN32__
32b13913 2149 wxPostScriptPrintNativeData *data =
398c701f 2150 wxDynamicCast(m_printData.GetNativeData(), wxPostScriptPrintNativeData);
32b13913 2151
398c701f 2152 if (data && !data->GetFontMetricPath().empty())
72cdf4c9 2153 {
8850cbd3 2154 afmName = data->GetFontMetricPath();
eba33006 2155 afmName << wxFILE_SEP_PATH << name;
d361e74e 2156 }
d36d1be9 2157#endif // __WIN32__
72cdf4c9 2158
d36d1be9
VZ
2159 if ( !afmName.empty() )
2160 afmFile = wxFopen(afmName, wxT("r"));
2161
d36d1be9
VZ
2162 if ( !afmFile )
2163 {
2164#if defined(__UNIX__) && !defined(__VMS__)
2c18f21d 2165 afmName = wxGetDataDir();
d36d1be9
VZ
2166#else // !__UNIX__
2167 afmName = wxStandardPaths::Get().GetDataDir();
2168#endif // __UNIX__/!__UNIX__
2169
3e469ea3 2170 afmName << wxFILE_SEP_PATH
733b25e1
RR
2171#if defined(__LINUX__) || defined(__FREEBSD__)
2172 << wxT("gs_afm") << wxFILE_SEP_PATH
2173#else
021e0f21 2174 << wxT("afm") << wxFILE_SEP_PATH
733b25e1
RR
2175#endif
2176 << name;
392857fb 2177 afmFile = wxFopen(afmName,wxT("r"));
3e469ea3 2178 }
479cd5de 2179
eba33006
RR
2180 /* 2. open and process the file
2181 / a short explanation of the AFM format:
2182 / we have for each character a line, which gives its size
2183 / e.g.:
2184 /
2185 / C 63 ; WX 444 ; N question ; B 49 -14 395 676 ;
2186 /
2187 / that means, we have a character with ascii code 63, and width
2188 / (444/1000 * fontSize) points.
2189 / the other data is ignored for now!
2190 /
2191 / when the font has changed, we read in the right AFM file and store the
2192 / character widths in an array, which is processed below (see point 3.). */
385bcb35 2193 if (afmFile==NULL)
36b3b54a 2194 {
ccbfa8ec 2195 wxLogDebug( wxT("GetTextExtent: can't open AFM file '%s'"), afmName.c_str() );
48c31b28 2196 wxLogDebug( wxT(" using approximate values"));
36b3b54a
RR
2197 for (int i=0; i<256; i++) lastWidths[i] = 500; /* an approximate value */
2198 lastDescender = -150; /* dito. */
ed880dd4 2199 }
afce4c03
VZ
2200 else
2201 {
36b3b54a
RR
2202 /* init the widths array */
2203 for(int i=0; i<256; i++) lastWidths[i] = INT_MIN;
2204 /* some variables for holding parts of a line */
32b13913
WS
2205 char cString[10], semiString[10], WXString[10];
2206 char descString[20];
2207 char upString[30], utString[30];
2208 char encString[50];
36b3b54a
RR
2209 char line[256];
2210 int ascii,cWidth;
2211 /* read in the file and parse it */
2212 while(fgets(line,sizeof(line),afmFile)!=NULL)
afce4c03 2213 {
36b3b54a 2214 /* A.) check for descender definition */
021e0f21 2215 if (strncmp(line,"Descender",9)==0)
afce4c03 2216 {
36b3b54a 2217 if ((sscanf(line,"%s%d",descString,&lastDescender)!=2) ||
021e0f21 2218 (strcmp(descString,"Descender")!=0))
afce4c03 2219 {
ccbfa8ec 2220 wxLogDebug( wxT("AFM-file '%s': line '%s' has error (bad descender)"), afmName.c_str(),line );
36b3b54a
RR
2221 }
2222 }
2223 /* JC 1.) check for UnderlinePosition */
021e0f21 2224 else if(strncmp(line,"UnderlinePosition",17)==0)
afce4c03 2225 {
36b3b54a 2226 if ((sscanf(line,"%s%lf",upString,&UnderlinePosition)!=2) ||
021e0f21 2227 (strcmp(upString,"UnderlinePosition")!=0))
afce4c03 2228 {
ccbfa8ec 2229 wxLogDebug( wxT("AFM-file '%s': line '%s' has error (bad UnderlinePosition)"), afmName.c_str(), line );
36b3b54a
RR
2230 }
2231 }
afce4c03 2232 /* JC 2.) check for UnderlineThickness */
021e0f21 2233 else if(strncmp(line,"UnderlineThickness",18)==0)
afce4c03 2234 {
36b3b54a 2235 if ((sscanf(line,"%s%lf",utString,&UnderlineThickness)!=2) ||
021e0f21 2236 (strcmp(utString,"UnderlineThickness")!=0))
afce4c03 2237 {
ccbfa8ec 2238 wxLogDebug( wxT("AFM-file '%s': line '%s' has error (bad UnderlineThickness)"), afmName.c_str(), line );
36b3b54a
RR
2239 }
2240 }
afce4c03 2241 /* JC 3.) check for EncodingScheme */
021e0f21 2242 else if(strncmp(line,"EncodingScheme",14)==0)
afce4c03 2243 {
36b3b54a 2244 if ((sscanf(line,"%s%s",utString,encString)!=2) ||
021e0f21 2245 (strcmp(utString,"EncodingScheme")!=0))
afce4c03 2246 {
ccbfa8ec 2247 wxLogDebug( wxT("AFM-file '%s': line '%s' has error (bad EncodingScheme)"), afmName.c_str(), line );
36b3b54a 2248 }
021e0f21 2249 else if (strncmp(encString, "AdobeStandardEncoding", 21))
36b3b54a 2250 {
ccbfa8ec 2251 wxLogDebug( wxT("AFM-file '%s': line '%s' has error (unsupported EncodingScheme %s)"),
3e469ea3 2252 afmName.c_str(),line, encString);
36b3b54a
RR
2253 }
2254 }
2255 /* B.) check for char-width */
021e0f21 2256 else if(strncmp(line,"C ",2)==0)
afce4c03 2257 {
36b3b54a 2258 if (sscanf(line,"%s%d%s%s%d",cString,&ascii,semiString,WXString,&cWidth)!=5)
afce4c03 2259 {
ccbfa8ec 2260 wxLogDebug(wxT("AFM-file '%s': line '%s' has an error (bad character width)"),afmName.c_str(),line);
36b3b54a 2261 }
021e0f21 2262 if(strcmp(cString,"C")!=0 || strcmp(semiString,";")!=0 || strcmp(WXString,"WX")!=0)
afce4c03 2263 {
ccbfa8ec 2264 wxLogDebug(wxT("AFM-file '%s': line '%s' has a format error"),afmName.c_str(),line);
36b3b54a
RR
2265 }
2266 /* printf(" char '%c'=%d has width '%d'\n",ascii,ascii,cWidth); */
2267 if (ascii>=0 && ascii<256)
afce4c03 2268 {
36b3b54a
RR
2269 lastWidths[ascii] = cWidth; /* store width */
2270 }
afce4c03
VZ
2271 else
2272 {
2273 /* MATTHEW: this happens a lot; don't print an error */
48c31b28 2274 /* wxLogDebug("AFM-file '%s': ASCII value %d out of range",afmName.c_str(),ascii); */
36b3b54a
RR
2275 }
2276 }
2277 /* C.) ignore other entries. */
2278 }
2279 fclose(afmFile);
ed880dd4 2280 }
36b3b54a 2281 /* hack to compute correct values for german 'Umlaute'
afce4c03
VZ
2282 / the correct way would be to map the character names
2283 / like 'adieresis' to corresp. positions of ISOEnc and read
2284 / these values from AFM files, too. Maybe later ... */
b52e49f5
VZ
2285
2286 // NB: casts to int are needed to suppress gcc 3.3 warnings
9d55bfef
VZ
2287 lastWidths[196] = lastWidths[(int)'A']; // U+00C4 A Umlaute
2288 lastWidths[228] = lastWidths[(int)'a']; // U+00E4 a Umlaute
2289 lastWidths[214] = lastWidths[(int)'O']; // U+00D6 O Umlaute
2290 lastWidths[246] = lastWidths[(int)'o']; // U+00F6 o Umlaute
2291 lastWidths[220] = lastWidths[(int)'U']; // U+00DC U Umlaute
2292 lastWidths[252] = lastWidths[(int)'u']; // U+00FC u Umlaute
2293 lastWidths[223] = lastWidths[(int)251]; // U+00DF eszett (scharfes s)
36b3b54a 2294
f6bcfd97
BP
2295 /* JC: calculate UnderlineThickness/UnderlinePosition */
2296
a439ecef
VS
2297 // VS: dirty, but is there any better solution?
2298 double *pt;
2299 pt = (double*) &m_underlinePosition;
12ca5586 2300 *pt = YLOG2DEVREL((wxCoord)(UnderlinePosition * fontSize)) / 1000.0f;
a439ecef 2301 pt = (double*) &m_underlineThickness;
12ca5586 2302 *pt = YLOG2DEVREL((wxCoord)(UnderlineThickness * fontSize)) / 1000.0f;
f6bcfd97 2303
a439ecef 2304 }
36b3b54a 2305
f6bcfd97 2306
36b3b54a 2307 /* 3. now the font metrics are read in, calc size this
afce4c03
VZ
2308 / is done by adding the widths of the characters in the
2309 / string. they are given in 1/1000 of the size! */
36b3b54a 2310
733b25e1 2311 long sum=0;
12ca5586 2312 float height=fontSize; /* by default */
36b3b54a 2313 unsigned char *p;
6eec2bee 2314 for(p=(unsigned char *)wxMBSTRINGCAST strbuf; *p; p++)
36b3b54a
RR
2315 {
2316 if(lastWidths[*p]== INT_MIN)
afce4c03 2317 {
ccbfa8ec 2318 wxLogDebug(wxT("GetTextExtent: undefined width for character '%c' (%d)"), *p,*p);
67bbb910 2319 sum += lastWidths[(unsigned char)' ']; /* assume space */
ed880dd4 2320 }
afce4c03
VZ
2321 else
2322 {
733b25e1 2323 sum += lastWidths[*p];
ed880dd4 2324 }
36b3b54a 2325 }
48c31b28 2326
733b25e1 2327 double widthSum = sum;
12ca5586 2328 widthSum *= fontSize;
733b25e1 2329 widthSum /= 1000.0F;
48c31b28 2330
36b3b54a 2331 /* add descender to height (it is usually a negative value) */
6c3d9ced
VS
2332 //if (lastDescender != INT_MIN)
2333 //{
2334 // height += (wxCoord)(((-lastDescender)/1000.0F) * Size); /* MATTHEW: forgot scale */
2335 //}
2336 // - commented by V. Slavik - height already contains descender in it
2337 // (judging from few experiments)
36b3b54a
RR
2338
2339 /* return size values */
72cdf4c9
VZ
2340 if ( x )
2341 *x = (wxCoord)widthSum;
2342 if ( y )
12ca5586 2343 *y = (wxCoord)height;
36b3b54a
RR
2344
2345 /* return other parameters */
2346 if (descent)
2347 {
2348 if(lastDescender!=INT_MIN)
afce4c03 2349 {
12ca5586 2350 *descent = (wxCoord)(((-lastDescender)/1000.0F) * fontSize); /* MATTHEW: forgot scale */
ed880dd4 2351 }
afce4c03
VZ
2352 else
2353 {
2354 *descent = 0;
36b3b54a
RR
2355 }
2356 }
2357
2358 /* currently no idea how to calculate this! */
2359 if (externalLeading) *externalLeading = 0;
3fc306e9
RR
2360#endif
2361 // Use AFM
ed880dd4
RR
2362}
2363
244e5e34 2364
88a7a4e1 2365#endif // wxUSE_PRINTING_ARCHITECTURE && wxUSE_POSTSCRIPT
21e3b862
RL
2366
2367// vi:sts=4:sw=4:et