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