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