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