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