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