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