1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: wxPostScriptDC implementation
4 // Author: Julian Smart, Robert Roebling, Markus Holzhem
8 // Copyright: (c) Julian Smart and Markus Holzem
9 // Licence: wxWindows license
10 /////////////////////////////////////////////////////////////////////////////
13 #pragma implementation
17 #include "wx/postscrp.h"
18 #include "wx/dcmemory.h"
21 #include "wx/filedlg.h"
23 #include "wx/msgdlg.h"
27 //-----------------------------------------------------------------------------
28 // start and end of document/page
29 //-----------------------------------------------------------------------------
31 static const char *wxPostScriptHeaderEllipse
= "\
32 /ellipsedict 8 dict def\n\
33 ellipsedict /mtrx matrix put\n\
37 /startangle exch def\n\
42 /savematrix mtrx currentmatrix def\n\
45 0 0 1 startangle endangle arc\n\
46 savematrix setmatrix\n\
51 static const char *wxPostScriptHeaderEllipticArc
= "\
52 /ellipticarcdict 8 dict def\n\
53 ellipticarcdict /mtrx matrix put\n\
55 { ellipticarcdict begin\n\
58 /startangle exch def\n\
63 /savematrix mtrx currentmatrix def\n\
66 do_fill { 0 0 moveto } if\n\
67 0 0 1 startangle endangle arc\n\
68 savematrix setmatrix\n\
69 do_fill { fill }{ stroke } ifelse\n\
73 static const char *wxPostScriptHeaderSpline
= "\
74 /DrawSplineSection {\n\
81 /xa x1 x2 x1 sub 0.666667 mul add def\n\
82 /ya y1 y2 y1 sub 0.666667 mul add def\n\
83 /xb x3 x2 x3 sub 0.666667 mul add def\n\
84 /yb y3 y2 y3 sub 0.666667 mul add def\n\
86 xa ya xb yb x3 y3 curveto\n\
90 static const char *wxPostScriptHeaderColourImage
= "\
91 % define 'colorimage' if it isn't defined\n\
92 % ('colortogray' and 'mergeprocs' come from xwd2ps\n\
94 /colorimage where % do we know about 'colorimage'?\n\
95 { pop } % yes: pop off the 'dict' returned\n\
97 /colortogray { % define an RGB->I function\n\
98 /rgbdata exch store % call input 'rgbdata'\n\
99 rgbdata length 3 idiv\n\
100 /npixls exch store\n\
102 0 1 npixls 1 sub {\n\
104 rgbdata rgbindx get 20 mul % Red\n\
105 rgbdata rgbindx 1 add get 32 mul % Green\n\
106 rgbdata rgbindx 2 add get 12 mul % Blue\n\
107 add add 64 idiv % I = .5G + .31R + .18B\n\
109 /rgbindx rgbindx 3 add store\n\
111 grays 0 npixls getinterval\n\
114 % Utility procedure for colorimage operator.\n\
115 % This procedure takes two procedures off the\n\
116 % stack and merges them into a single procedure.\n\
118 /mergeprocs { % def\n\
137 /colorimage { % def\n\
138 pop pop % remove 'false 3' operands\n\
139 {colortogray} mergeprocs\n\
142 } ifelse % end of 'false' case\n\
145 static char wxPostScriptHeaderReencodeISO1
[] =
147 "dup dup findfont dup length dict begin\n"
148 "{ 1 index /FID ne { def }{ pop pop } ifelse } forall\n"
149 "/Encoding ISOLatin1Encoding def\n"
150 "currentdict end definefont\n"
152 "/ISOLatin1Encoding [\n"
153 "/.notdef/.notdef/.notdef/.notdef/.notdef/.notdef/.notdef/.notdef\n"
154 "/.notdef/.notdef/.notdef/.notdef/.notdef/.notdef/.notdef/.notdef\n"
155 "/.notdef/.notdef/.notdef/.notdef/.notdef/.notdef/.notdef/.notdef\n"
156 "/.notdef/.notdef/.notdef/.notdef/.notdef/.notdef/.notdef/.notdef\n"
157 "/space/exclam/quotedbl/numbersign/dollar/percent/ampersand/quoteright\n"
158 "/parenleft/parenright/asterisk/plus/comma/minus/period/slash\n"
159 "/zero/one/two/three/four/five/six/seven/eight/nine/colon/semicolon\n"
160 "/less/equal/greater/question/at/A/B/C/D/E/F/G/H/I/J/K/L/M/N\n"
161 "/O/P/Q/R/S/T/U/V/W/X/Y/Z/bracketleft/backslash/bracketright\n"
162 "/asciicircum/underscore/quoteleft/a/b/c/d/e/f/g/h/i/j/k/l/m\n"
163 "/n/o/p/q/r/s/t/u/v/w/x/y/z/braceleft/bar/braceright/asciitilde\n"
164 "/.notdef/.notdef/.notdef/.notdef/.notdef/.notdef/.notdef/.notdef\n"
165 "/.notdef/.notdef/.notdef/.notdef/.notdef/.notdef/.notdef/.notdef\n"
166 "/.notdef/dotlessi/grave/acute/circumflex/tilde/macron/breve\n"
167 "/dotaccent/dieresis/.notdef/ring/cedilla/.notdef/hungarumlaut\n";
169 static char wxPostScriptHeaderReencodeISO2
[] =
170 "/ogonek/caron/space/exclamdown/cent/sterling/currency/yen/brokenbar\n"
171 "/section/dieresis/copyright/ordfeminine/guillemotleft/logicalnot\n"
172 "/hyphen/registered/macron/degree/plusminus/twosuperior/threesuperior\n"
173 "/acute/mu/paragraph/periodcentered/cedilla/onesuperior/ordmasculine\n"
174 "/guillemotright/onequarter/onehalf/threequarters/questiondown\n"
175 "/Agrave/Aacute/Acircumflex/Atilde/Adieresis/Aring/AE/Ccedilla\n"
176 "/Egrave/Eacute/Ecircumflex/Edieresis/Igrave/Iacute/Icircumflex\n"
177 "/Idieresis/Eth/Ntilde/Ograve/Oacute/Ocircumflex/Otilde/Odieresis\n"
178 "/multiply/Oslash/Ugrave/Uacute/Ucircumflex/Udieresis/Yacute\n"
179 "/Thorn/germandbls/agrave/aacute/acircumflex/atilde/adieresis\n"
180 "/aring/ae/ccedilla/egrave/eacute/ecircumflex/edieresis/igrave\n"
181 "/iacute/icircumflex/idieresis/eth/ntilde/ograve/oacute/ocircumflex\n"
182 "/otilde/odieresis/divide/oslash/ugrave/uacute/ucircumflex/udieresis\n"
183 "/yacute/thorn/ydieresis\n"
186 //-------------------------------------------------------------------------------
188 //-------------------------------------------------------------------------------
190 wxPostScriptDC::wxPostScriptDC ()
192 m_pstream
= (ofstream
*) NULL
;
202 m_underlinePosition
= 0.0;
203 m_underlineThickness
= 0.0;
205 m_signX
= 1; // default x-axis left to right
206 m_signY
= -1; // default y-axis bottom up -> top down
209 wxPostScriptDC::wxPostScriptDC (const wxString
& file
, bool interactive
, wxWindow
*parent
)
211 m_pstream
= (ofstream
*) NULL
;
221 m_underlinePosition
= 0.0;
222 m_underlineThickness
= 0.0;
224 m_signX
= 1; // default x-axis left to right
225 m_signY
= -1; // default y-axis bottom up -> top down
227 Create(file
, interactive
, parent
);
230 bool wxPostScriptDC::Create(const wxString
& file
, bool interactive
, wxWindow
*parent
)
232 m_isInteractive
= interactive
;
238 // Can only send to file in Windows
239 wxThePrintSetupData
->SetPrinterMode(PS_FILE
);
244 if ((m_ok
= PrinterDialog (parent
) ) == FALSE
) return FALSE
;
254 wxPostScriptDC::~wxPostScriptDC ()
256 if (m_pstream
) delete m_pstream
;
259 bool wxPostScriptDC::Ok() const
264 bool wxPostScriptDC::PrinterDialog(wxWindow
*parent
)
266 wxPostScriptPrintDialog
dialog( parent
, _("Printer Settings"), wxPoint(150, 150), wxSize(400, 400),
267 wxDEFAULT_DIALOG_STYLE
| wxDIALOG_MODAL
);
268 m_ok
= (dialog
.ShowModal () == wxID_OK
);
270 if (!m_ok
) return FALSE
;
272 if ((m_filename
== "") &&
273 (wxThePrintSetupData
->GetPrinterMode() == PS_PREVIEW
||
274 wxThePrintSetupData
->GetPrinterMode() == PS_PRINTER
))
278 wxThePrintSetupData
->SetPrinterFile("preview");
280 // For PS_PRINTER action this depends on a Unix-style print spooler
281 // since the wx_printer_file can be destroyed during a session
282 // @@@ TODO: a Windows-style answer for non-Unix
284 wxGetUserId (userId
, sizeof (userId
) / sizeof (char));
286 strcpy (tmp
, "/tmp/preview_");
287 strcat (tmp
, userId
);
288 wxThePrintSetupData
->SetPrinterFile(tmp
);
291 strcpy(tmp2
, wxThePrintSetupData
->GetPrinterFile());
292 strcat (tmp2
, ".ps");
293 wxThePrintSetupData
->SetPrinterFile(tmp2
);
296 else if ((m_filename
== "") && (wxThePrintSetupData
->GetPrinterMode() == PS_FILE
))
298 char *file
= wxSaveFileSelector (_("PostScript"), "ps");
304 wxThePrintSetupData
->SetPrinterFile(file
);
312 void wxPostScriptDC::SetClippingRegion (long x
, long y
, long w
, long h
)
314 wxCHECK_RET( m_ok
&& m_pstream
, "invalid postscript dc" );
316 if (m_clipping
) return;
318 wxDC::SetClippingRegion( x
, y
, w
, h
);
321 *m_pstream
<< "gsave\n"
323 << XLOG2DEV(x
) << " " << YLOG2DEV(y
) << " moveto\n"
324 << XLOG2DEV(x
+w
) << " " << YLOG2DEV(y
) << " lineto\n"
325 << XLOG2DEV(x
+w
) << " " << YLOG2DEV(y
+h
) << " lineto\n"
326 << XLOG2DEV(x
) << " " << YLOG2DEV(y
+h
) << " lineto\n"
327 << "closepath clip newpath\n";
330 void wxPostScriptDC::SetClippingRegion( const wxRegion
&WXUNUSED(region
) )
334 void wxPostScriptDC::DestroyClippingRegion()
336 wxCHECK_RET( m_ok
&& m_pstream
, "invalid postscript dc" );
338 wxDC::DestroyClippingRegion();
343 *m_pstream
<< "grestore\n";
347 void wxPostScriptDC::Clear()
349 wxFAIL_MSG( "wxPostScriptDC::Clear not implemented." );
352 void wxPostScriptDC::FloodFill (long WXUNUSED(x
), long WXUNUSED(y
), const wxColour
&WXUNUSED(col
), int WXUNUSED(style
))
354 wxFAIL_MSG( "wxPostScriptDC::FloodFill not implemented." );
357 bool wxPostScriptDC::GetPixel (long WXUNUSED(x
), long WXUNUSED(y
), wxColour
* WXUNUSED(col
)) const
359 wxFAIL_MSG( "wxPostScriptDC::GetPixel not implemented." );
363 void wxPostScriptDC::CrossHair (long WXUNUSED(x
), long WXUNUSED(y
))
365 wxFAIL_MSG( "wxPostScriptDC::CrossHair not implemented." );
368 void wxPostScriptDC::DrawLine (long x1
, long y1
, long x2
, long y2
)
370 wxCHECK_RET( m_ok
&& m_pstream
, "invalid postscript dc" );
372 if (m_pen
.GetStyle() == wxTRANSPARENT
) return;
376 *m_pstream
<< "newpath\n"
377 << XLOG2DEV(x1
) << " " << YLOG2DEV (y1
) << " moveto\n"
378 << XLOG2DEV(x2
) << " " << YLOG2DEV (y2
) << " lineto\n"
381 CalcBoundingBox( x1
, y1
);
382 CalcBoundingBox( x2
, y2
);
385 #define RAD2DEG 57.29577951308
387 void wxPostScriptDC::DrawArc (long x1
, long y1
, long x2
, long y2
, long xc
, long yc
)
389 wxCHECK_RET( m_ok
&& m_pstream
, "invalid postscript dc" );
393 long radius
= (long) sqrt(dx
*dx
+dy
*dy
);
394 double alpha1
, alpha2
;
396 if (x1
== x2
&& y1
== y2
)
400 } else if (radius
== 0.0)
402 alpha1
= alpha2
= 0.0;
405 alpha1
= (x1
- xc
== 0) ?
406 (y1
- yc
< 0) ? 90.0 : -90.0 :
407 -atan2(double(y1
-yc
), double(x1
-xc
)) * RAD2DEG
;
408 alpha2
= (x2
- xc
== 0) ?
409 (y2
- yc
< 0) ? 90.0 : -90.0 :
410 -atan2(double(y2
-yc
), double(x2
-xc
)) * RAD2DEG
;
412 while (alpha1
<= 0) alpha1
+= 360;
413 while (alpha2
<= 0) alpha2
+= 360; // adjust angles to be between
414 while (alpha1
> 360) alpha1
-= 360; // 0 and 360 degree
415 while (alpha2
> 360) alpha2
-= 360;
417 if (m_brush
.GetStyle() != wxTRANSPARENT
)
420 *m_pstream
<< "newpath\n"
421 << XLOG2DEV(xc
) << " "
422 << YLOG2DEV(yc
) << " "
423 << XLOG2DEVREL(radius
) << " "
424 << YLOG2DEVREL(radius
) << " "
426 << alpha2
<< " ellipse\n"
427 << XLOG2DEV(xc
) << " "
428 << YLOG2DEV(yc
) << " lineto\n"
433 if (m_pen
.GetStyle() != wxTRANSPARENT
)
436 *m_pstream
<< "newpath\n"
437 << XLOG2DEV(xc
) << " "
438 << YLOG2DEV(yc
) << " "
439 << XLOG2DEVREL(radius
) << " "
440 << YLOG2DEVREL(radius
) << " "
442 << alpha2
<< " ellipse\n"
446 CalcBoundingBox( xc
-radius
, yc
-radius
);
447 CalcBoundingBox( xc
+radius
, yc
+radius
);
450 void wxPostScriptDC::DrawEllipticArc(long x
,long y
,long w
,long h
,double sa
,double ea
)
452 wxCHECK_RET( m_ok
&& m_pstream
, "invalid postscript dc" );
454 if (sa
>=360 || sa
<=-360) sa
=sa
-int(sa
/360)*360;
455 if (ea
>=360 || ea
<=-360) ea
=ea
-int(ea
/360)*360;
461 DrawEllipse(x
,y
,w
,h
);
465 if (m_brush
.GetStyle () != wxTRANSPARENT
)
469 *m_pstream
<< "newpath\n"
470 << XLOG2DEV(x
+w
/2) << " " << YLOG2DEV(y
+h
/2) << " "
471 << XLOG2DEVREL(w
/2) << " " << YLOG2DEVREL(h
/2) << " "
472 << int(sa
) <<" "<< int(ea
) << " true ellipticarc\n";
474 CalcBoundingBox( x
,y
);
475 CalcBoundingBox( x
+w
, y
+h
);
478 if (m_pen
.GetStyle () != wxTRANSPARENT
)
482 *m_pstream
<< "newpath\n"
483 << XLOG2DEV(x
+w
/2) << " " << YLOG2DEV(y
+h
/2) << " "
484 << XLOG2DEVREL(w
/2) << " " << XLOG2DEVREL(h
/2) << " "
485 << int(sa
) <<" "<< int(ea
) << " false ellipticarc\n";
487 CalcBoundingBox( x
, y
);
488 CalcBoundingBox( x
+w
, y
+h
);
492 void wxPostScriptDC::DrawPoint (long x
, long y
)
494 wxCHECK_RET( m_ok
&& m_pstream
, "invalid postscript dc" );
496 if (m_pen
.GetStyle() == wxTRANSPARENT
) return;
500 *m_pstream
<< "newpath\n"
501 << XLOG2DEV(x
) << " " << YLOG2DEV (y
) << " moveto\n"
502 << XLOG2DEV(x
+1) << " " << YLOG2DEV (y
) << " lineto\n"
505 CalcBoundingBox( x
, y
);
508 void wxPostScriptDC::DrawPolygon (int n
, wxPoint points
[], long xoffset
, long yoffset
, int WXUNUSED(fillStyle
))
510 wxCHECK_RET( m_ok
&& m_pstream
, "invalid postscript dc" );
514 if (m_brush
.GetStyle () != wxTRANSPARENT
)
518 *m_pstream
<< "newpath\n";
520 long xx
= XLOG2DEV(points
[0].x
+ xoffset
);
521 long yy
= YLOG2DEV(points
[0].y
+ yoffset
);
522 *m_pstream
<< xx
<< " " << yy
<< " moveto\n";
523 CalcBoundingBox( points
[0].x
+ xoffset
, points
[0].y
+ yoffset
);
525 for (int i
= 1; i
< n
; i
++)
527 xx
= XLOG2DEV(points
[i
].x
+ xoffset
);
528 yy
= YLOG2DEV(points
[i
].y
+ yoffset
);
529 *m_pstream
<< xx
<< " " << yy
<< " lineto\n";
530 CalcBoundingBox( points
[i
].x
+ xoffset
, points
[i
].y
+ yoffset
);
532 *m_pstream
<< "fill\n";
535 if (m_pen
.GetStyle () != wxTRANSPARENT
)
539 *m_pstream
<< "newpath\n";
541 long xx
= XLOG2DEV(points
[0].x
+ xoffset
);
542 long yy
= YLOG2DEV(points
[0].y
+ yoffset
);
543 *m_pstream
<< xx
<< " " << yy
<< " moveto\n";
544 CalcBoundingBox( points
[0].x
+ xoffset
, points
[0].y
+ yoffset
);
546 for (int i
= 1; i
< n
; i
++)
548 xx
= XLOG2DEV(points
[i
].x
+ xoffset
);
549 yy
= YLOG2DEV(points
[i
].y
+ yoffset
);
550 *m_pstream
<< xx
<< " " << yy
<< " lineto\n";
551 CalcBoundingBox( points
[i
].x
+ xoffset
, points
[i
].y
+ yoffset
);
554 *m_pstream
<< "stroke\n";
558 void wxPostScriptDC::DrawLines (int n
, wxPoint points
[], long xoffset
, long yoffset
)
560 wxCHECK_RET( m_ok
&& m_pstream
, "invalid postscript dc" );
562 if (m_pen
.GetStyle() == wxTRANSPARENT
) return;
567 for (int i
=0; i
<n
; ++i
)
569 CalcBoundingBox( XLOG2DEV(points
[i
].x
+xoffset
), YLOG2DEV(points
[i
].y
+yoffset
));
572 *m_pstream
<< "newpath\n"
573 << XLOG2DEV(points
[0].x
+xoffset
) << " "
574 << YLOG2DEV(points
[0].y
+yoffset
) << " moveto\n";
576 for (int i
= 1; i
< n
; i
++)
578 *m_pstream
<< XLOG2DEV(points
[i
].x
+xoffset
) << " "
579 << YLOG2DEV(points
[i
].y
+yoffset
) << " lineto\n";
582 *m_pstream
<< "stroke\n";
585 void wxPostScriptDC::DrawRectangle (long x
, long y
, long width
, long height
)
587 wxCHECK_RET( m_ok
&& m_pstream
, "invalid postscript dc" );
589 if (m_brush
.GetStyle () != wxTRANSPARENT
)
593 *m_pstream
<< "newpath\n"
594 << XLOG2DEV(x
) << " " << YLOG2DEV(y
) << " moveto\n"
595 << XLOG2DEV(x
+ width
) << " " << YLOG2DEV(y
) << " lineto\n"
596 << XLOG2DEV(x
+ width
) << " " << YLOG2DEV(y
+ height
) << " lineto\n"
597 << XLOG2DEV(x
) << " " << YLOG2DEV(y
+ height
) << " lineto\n"
601 CalcBoundingBox( x
, y
);
602 CalcBoundingBox( x
+ width
, y
+ height
);
605 if (m_pen
.GetStyle () != wxTRANSPARENT
)
609 *m_pstream
<< "newpath\n"
610 << XLOG2DEV(x
) << " " << YLOG2DEV(y
) << " moveto\n"
611 << XLOG2DEV(x
+ width
) << " " << YLOG2DEV(y
) << " lineto\n"
612 << XLOG2DEV(x
+ width
) << " " << YLOG2DEV(y
+ height
) << " lineto\n"
613 << XLOG2DEV(x
) << " " << YLOG2DEV(y
+ height
) << " lineto\n"
617 CalcBoundingBox( x
, y
);
618 CalcBoundingBox( x
+ width
, y
+ height
);
622 void wxPostScriptDC::DrawRoundedRectangle (long x
, long y
, long width
, long height
, double radius
)
624 wxCHECK_RET( m_ok
&& m_pstream
, "invalid postscript dc" );
628 // Now, a negative radius is interpreted to mean
629 // 'the proportion of the smallest X or Y dimension'
630 double smallest
= 0.0;
635 radius
= (-radius
* smallest
);
638 long rad
= (long) radius
;
640 if (m_brush
.GetStyle () != wxTRANSPARENT
)
644 // Draw rectangle anticlockwise
645 *m_pstream
<< "newpath\n"
646 << XLOG2DEV(x
+ rad
) << " " << YLOG2DEV(y
+ rad
) << " " << XLOG2DEVREL(rad
) << " 90 180 arc\n"
647 << XLOG2DEV(x
) << " " << YLOG2DEV(y
+ rad
) << " moveto\n"
648 << XLOG2DEV(x
+ rad
) << " " << YLOG2DEV(y
+ height
- rad
) << " " << XLOG2DEVREL(rad
) << " 180 270 arc\n"
649 << XLOG2DEV(x
+ width
- rad
) << " " << YLOG2DEV(y
+ height
) << " lineto\n"
650 << XLOG2DEV(x
+ width
- rad
) << " " << YLOG2DEV(y
+ height
- rad
) << " " << XLOG2DEVREL(rad
) << " 270 0 arc\n"
651 << XLOG2DEV(x
+ width
) << " " << YLOG2DEV(y
+ rad
) << " lineto\n"
652 << XLOG2DEV(x
+ width
- rad
) << " " << YLOG2DEV(y
+ rad
) << " " << XLOG2DEVREL(rad
) << " 0 90 arc\n"
653 << XLOG2DEV(x
+ rad
) << " " << YLOG2DEV(y
) << " lineto\n"
657 CalcBoundingBox( x
, y
);
658 CalcBoundingBox( x
+ width
, y
+ height
);
661 if (m_pen
.GetStyle () != wxTRANSPARENT
)
665 // Draw rectangle anticlockwise
666 *m_pstream
<< "newpath\n"
667 << XLOG2DEV(x
+ rad
) << " " << YLOG2DEV(y
+ rad
) << " " << XLOG2DEVREL(rad
) << " 90 180 arc\n"
668 << XLOG2DEV(x
) << " " << YLOG2DEV(y
+ rad
) << " moveto\n"
669 << XLOG2DEV(x
+ rad
) << " " << YLOG2DEV(y
+ height
- rad
) << " " << XLOG2DEVREL(rad
) << " 180 270 arc\n"
670 << XLOG2DEV(x
+ width
- rad
) << " " << YLOG2DEV(y
+ height
) << " lineto\n"
671 << XLOG2DEV(x
+ width
- rad
) << " " << YLOG2DEV(y
+ height
- rad
) << " " << XLOG2DEVREL(rad
) << " 270 0 arc\n"
672 << XLOG2DEV(x
+ width
) << " " << YLOG2DEV(y
+ rad
) << " lineto\n"
673 << XLOG2DEV(x
+ width
- rad
) << " " << YLOG2DEV(y
+ rad
) << " " << XLOG2DEVREL(rad
) << " 0 90 arc\n"
674 << XLOG2DEV(x
+ rad
) << " " << YLOG2DEV(y
) << " lineto\n"
678 CalcBoundingBox( x
, y
);
679 CalcBoundingBox( x
+ width
, y
+ height
);
683 void wxPostScriptDC::DrawEllipse (long x
, long y
, long width
, long height
)
685 wxCHECK_RET( m_ok
&& m_pstream
, "invalid postscript dc" );
687 if (m_brush
.GetStyle () != wxTRANSPARENT
)
691 *m_pstream
<< "newpath\n"
692 << XLOG2DEV(x
+ width
/ 2) << " " << YLOG2DEV(y
+ height
/ 2) << " "
693 << XLOG2DEV(width
/ 2) << " " << YLOG2DEVREL(height
/ 2) << " 0 360 ellipse\n"
696 CalcBoundingBox( x
- width
, y
- height
);
697 CalcBoundingBox( x
+ width
, y
+ height
);
700 if (m_pen
.Ok() && m_pen
.GetStyle () != wxTRANSPARENT
)
704 *m_pstream
<< "newpath\n"
705 << XLOG2DEV(x
+ width
/ 2) << " " << YLOG2DEV(y
+ height
/ 2) << " "
706 << XLOG2DEV(width
/ 2) << " " << YLOG2DEVREL(height
/ 2) << " 0 360 ellipse\n"
709 CalcBoundingBox( x
- width
, y
- height
);
710 CalcBoundingBox( x
+ width
, y
+ height
);
714 void wxPostScriptDC::DrawIcon (const wxIcon
& icon
, long x
, long y
)
716 DrawBitmap( icon
, x
, y
, TRUE
);
719 void wxPostScriptDC::DrawBitmap( const wxBitmap
& bitmap
, long x
, long y
, bool WXUNUSED(useMask
) )
721 wxCHECK_RET( m_ok
&& m_pstream
, "invalid postscript dc" );
723 if (!bitmap
.Ok()) return;
725 wxImage
image( bitmap
);
727 if (!image
.Ok()) return;
729 int ww
= XLOG2DEVREL(image
.GetWidth());
730 int hh
= YLOG2DEVREL(image
.GetHeight());
732 image
= image
.Scale( ww
, hh
);
734 if (!image
.Ok()) return;
736 int xx
= XLOG2DEV(x
);
737 int yy
= YLOG2DEV(y
+ bitmap
.GetHeight());
739 *m_pstream
<< "/origstate save def\n"
741 << "/pix " << ww
<< " string def\n"
742 << "/grays " << ww
<< " string def\n"
743 << "/npixels 0 def\n"
744 << "/rgbindx 0 def\n"
745 << xx
<< " " << yy
<< " translate\n"
746 << ww
<< " " << hh
<< " scale\n"
747 << ww
<< " " << hh
<< " 8\n"
748 << "[" << ww
<< " 0 0 " << (-hh
) << " 0 " << hh
<< "]\n"
749 << "{currentfile pix readhexstring pop}\n"
750 << "false 3 colorimage\n";
752 for (int j
= 0; j
< hh
; j
++)
754 for (int i
= 0; i
< ww
; i
++)
758 wxDecToHex( image
.GetRed(i
,j
), buffer
);
759 *m_pstream
<< buffer
;
760 wxDecToHex( image
.GetGreen(i
,j
), buffer
);
761 *m_pstream
<< buffer
;
762 wxDecToHex( image
.GetBlue(i
,j
), buffer
);
763 *m_pstream
<< buffer
;
768 *m_pstream
<< "end\n";
769 *m_pstream
<< "origstate restore\n";
773 void wxPostScriptDC::SetFont (const wxFont
& font
)
775 wxCHECK_RET( m_ok
&& m_pstream
, "invalid postscript dc" );
777 if (!font
.Ok()) return;
781 char *name
= wxTheFontNameDirectory
->GetPostScriptName( m_font
.GetFamily(),
784 if (!name
) name
= "Times-Roman";
786 *m_pstream
<< "/" << name
<< " reencodeISO def\n"
787 << "/" << name
<< " findfont\n"
788 << YLOG2DEVREL(font
.GetPointSize())
789 << " scalefont setfont\n";
792 void wxPostScriptDC::SetPen( const wxPen
& pen
)
794 wxCHECK_RET( m_ok
&& m_pstream
, "invalid postscript dc" );
796 if (!pen
.Ok()) return;
798 int oldStyle
= m_pen
.GetStyle();
802 *m_pstream
<< XLOG2DEVREL(m_pen
.GetWidth()) << " setlinewidth\n";
805 Line style - WRONG: 2nd arg is OFFSET
807 Here, I'm afraid you do not conceive meaning of parameters of 'setdash'
808 operator correctly. You should look-up this in the Red Book: the 2nd parame-
809 ter is not number of values in the array of the first one, but an offset
810 into this description of the pattern. I mean a real *offset* not index
811 into array. I.e. If the command is [3 4] 1 setdash is used, then there
812 will be first black line *2* units long, then space 4 units, then the
813 pattern of *3* units black, 4 units space will be repeated.
816 static const char *dotted
= "[2 5] 2";
817 static const char *short_dashed
= "[4 4] 2";
818 static const char *long_dashed
= "[4 8] 2";
819 static const char *dotted_dashed
= "[6 6 2 6] 4";
821 const char *psdash
= (char *) NULL
;
822 switch (m_pen
.GetStyle ())
824 case wxDOT
: psdash
= dotted
; break;
825 case wxSHORT_DASH
: psdash
= short_dashed
; break;
826 case wxLONG_DASH
: psdash
= long_dashed
; break;
827 case wxDOT_DASH
: psdash
= dotted_dashed
; break;
830 default: psdash
= "[] 0"; break;
833 if (oldStyle
!= m_pen
.GetStyle())
835 *m_pstream
<< psdash
<< " setdash\n";
839 unsigned char red
= m_pen
.GetColour().Red();
840 unsigned char blue
= m_pen
.GetColour().Blue();
841 unsigned char green
= m_pen
.GetColour().Green();
845 // Anything not white is black
846 if (!(red
== (unsigned char) 255 && blue
== (unsigned char) 255
847 && green
== (unsigned char) 255))
849 red
= (unsigned char) 0;
850 green
= (unsigned char) 0;
851 blue
= (unsigned char) 0;
857 if (!(red
== m_currentRed
&& green
== m_currentGreen
&& blue
== m_currentBlue
))
859 long redPS
= (long) (((int) red
) / 255.0);
860 long bluePS
= (long) (((int) blue
) / 255.0);
861 long greenPS
= (long) (((int) green
) / 255.0);
863 *m_pstream
<< redPS
<< " " << greenPS
<< " " << bluePS
<< " setrgbcolor\n";
866 m_currentBlue
= blue
;
867 m_currentGreen
= green
;
871 void wxPostScriptDC::SetBrush( const wxBrush
& brush
)
873 wxCHECK_RET( m_ok
&& m_pstream
, "invalid postscript dc" );
875 if (!brush
.Ok()) return;
880 unsigned char red
= m_brush
.GetColour ().Red ();
881 unsigned char blue
= m_brush
.GetColour ().Blue ();
882 unsigned char green
= m_brush
.GetColour ().Green ();
886 // Anything not black is white
887 if (!(red
== (unsigned char) 0 && blue
== (unsigned char) 0
888 && green
== (unsigned char) 0))
890 red
= (unsigned char) 255;
891 green
= (unsigned char) 255;
892 blue
= (unsigned char) 255;
898 if (!(red
== m_currentRed
&& green
== m_currentGreen
&& blue
== m_currentBlue
))
900 long redPS
= (long) (((int) red
) / 255.0);
901 long bluePS
= (long) (((int) blue
) / 255.0);
902 long greenPS
= (long) (((int) green
) / 255.0);
903 *m_pstream
<< redPS
<< " " << greenPS
<< " " << bluePS
<< " setrgbcolor\n";
905 m_currentBlue
= blue
;
906 m_currentGreen
= green
;
910 void wxPostScriptDC::DrawText( const wxString
& text
, long x
, long y
, bool WXUNUSED(use16bit
) )
912 wxCHECK_RET( m_ok
&& m_pstream
, "invalid postscript dc" );
916 if (m_textForegroundColour
.Ok ())
918 unsigned char red
= m_textForegroundColour
.Red ();
919 unsigned char blue
= m_textForegroundColour
.Blue ();
920 unsigned char green
= m_textForegroundColour
.Green ();
924 // Anything not white is black
925 if (!(red
== (unsigned char) 255 && blue
== (unsigned char) 255
926 && green
== (unsigned char) 255))
928 red
= (unsigned char) 0;
929 green
= (unsigned char) 0;
930 blue
= (unsigned char) 0;
934 // maybe setgray here ?
936 if (!(red
== m_currentRed
&& green
== m_currentGreen
&& blue
== m_currentBlue
))
938 long redPS
= (long) (((int) red
) / 255.0);
939 long bluePS
= (long) (((int) blue
) / 255.0);
940 long greenPS
= (long) (((int) green
) / 255.0);
941 *m_pstream
<< redPS
<< " " << greenPS
<< " " << bluePS
<< " setrgbcolor\n";
944 m_currentBlue
= blue
;
945 m_currentGreen
= green
;
949 int size
= m_font
.GetPointSize();
951 long by
= y
+ (long)floor( float(size
) * 2.0 / 3.0 ); // approximate baseline
952 *m_pstream
<< XLOG2DEV(x
) << " " << YLOG2DEV(by
) << " moveto\n";
955 int len
= strlen ((char *)(const char *)text
);
957 for (i
= 0; i
< len
; i
++)
959 int c
= (unsigned char) text
[i
];
960 if ( c
== ')' || c
== '(' || c
== '\\')
962 *m_pstream
<< "\\" << (char) c
;
966 // Cope with character codes > 127
968 sprintf(tmp
, "\\%o", c
);
972 *m_pstream
<< (char) c
;
975 *m_pstream
<< ")" << " show\n";
977 if (m_font
.GetUnderlined())
979 long uy
= (long)(y
+ size
- m_underlinePosition
);
981 GetTextExtent(text
, &w
, &h
);
983 *m_pstream
<< "gsave " << XLOG2DEV(x
) << " " << YLOG2DEV(uy
)
985 << (long)m_underlineThickness
<< " setlinewidth "
986 << XLOG2DEV(x
+ w
) << " " << YLOG2DEV(uy
)
987 << " lineto stroke grestore\n";
990 CalcBoundingBox( x
, y
);
991 CalcBoundingBox( x
+ size
* text
.Length() * 2/3 , y
);
995 void wxPostScriptDC::SetBackground (const wxBrush
& brush
)
997 m_backgroundBrush
= brush
;
1000 void wxPostScriptDC::SetLogicalFunction (int WXUNUSED(function
))
1002 wxFAIL_MSG( "wxPostScriptDC::SetLogicalFunction not implemented." );
1005 void wxPostScriptDC::DrawSpline( wxList
*points
)
1007 wxCHECK_RET( m_ok
&& m_pstream
, "invalid postscript dc" );
1011 double a
, b
, c
, d
, x1
, y1
, x2
, y2
, x3
, y3
;
1014 wxNode
*node
= points
->First();
1015 p
= (wxPoint
*)node
->Data();
1019 node
= node
->Next();
1020 p
= (wxPoint
*)node
->Data();
1023 x3
= a
= (double)(x1
+ c
) / 2;
1024 y3
= b
= (double)(y1
+ d
) / 2;
1026 *m_pstream
<< "newpath "
1027 << XLOG2DEV((long)x1
) << " " << YLOG2DEV((long)y1
) << " moveto "
1028 << XLOG2DEV((long)x3
) << " " << YLOG2DEV((long)y3
) << " lineto\n";
1030 CalcBoundingBox( (long)x1
, (long)y1
);
1031 CalcBoundingBox( (long)x3
, (long)y3
);
1033 while ((node
= node
->Next()) != NULL
)
1035 q
= (wxPoint
*)node
->Data();
1043 x3
= (double)(x2
+ c
) / 2;
1044 y3
= (double)(y2
+ d
) / 2;
1045 *m_pstream
<< XLOG2DEV((long)x1
) << " " << YLOG2DEV((long)y1
) << " "
1046 << XLOG2DEV((long)x2
) << " " << YLOG2DEV((long)y2
) << " "
1047 << XLOG2DEV((long)x3
) << " " << YLOG2DEV((long)y3
) << " DrawSplineSection\n";
1049 CalcBoundingBox( (long)x1
, (long)y1
);
1050 CalcBoundingBox( (long)x3
, (long)y3
);
1054 At this point, (x2,y2) and (c,d) are the position of the
1055 next-to-last and last point respectively, in the point list
1058 *m_pstream
<< XLOG2DEV((long)c
) << " " << YLOG2DEV((long)d
) << " lineto stroke\n";
1061 long wxPostScriptDC::GetCharWidth ()
1063 // Chris Breeze: reasonable approximation using wxMODERN/Courier
1064 return (long) (GetCharHeight() * 72.0 / 120.0);
1068 void wxPostScriptDC::SetAxisOrientation( bool xLeftRight
, bool yBottomUp
)
1070 wxCHECK_RET( m_ok
&& m_pstream
, "invalid postscript dc" );
1072 m_signX
= (xLeftRight
? 1 : -1);
1073 m_signY
= (yBottomUp
? 1 : -1);
1075 ComputeScaleAndOrigin();
1078 void wxPostScriptDC::SetDeviceOrigin( long x
, long y
)
1080 wxCHECK_RET( m_ok
&& m_pstream
, "invalid postscript dc" );
1086 wxDC::SetDeviceOrigin( x
, h
-y
);
1089 void wxPostScriptDC::GetSize(int* width
, int* height
) const
1091 const char *paperType
= wxThePrintSetupData
->GetPaperName();
1093 if (!paperType
) paperType
= _("A4 210 x 297 mm");
1095 wxPrintPaperType
*paper
= wxThePrintPaperDatabase
->FindPaperType(paperType
);
1097 if (!paper
) paper
= wxThePrintPaperDatabase
->FindPaperType(_("A4 210 x 297 mm"));
1101 if (width
) *width
= paper
->widthPixels
;
1102 if (height
) *height
= paper
->heightPixels
;
1106 if (width
) *width
= 595;
1107 if (height
) *height
= 842;
1111 bool wxPostScriptDC::StartDoc (const wxString
& message
)
1113 wxCHECK_MSG( m_ok
, FALSE
, "invalid postscript dc" );
1115 if (m_filename
== "")
1117 m_filename
= wxGetTempFileName("ps");
1118 wxThePrintSetupData
->SetPrinterFile((char *)(const char *)m_filename
);
1123 wxThePrintSetupData
->SetPrinterFile((char *)(const char *)m_filename
);
1126 m_pstream
= new ofstream (wxThePrintSetupData
->GetPrinterFile());
1128 if (!m_pstream
|| !m_pstream
->good())
1130 wxMessageBox (_("Cannot open file!"), _("Error"), wxOK
);
1137 SetBrush( *wxBLACK_BRUSH
);
1138 SetPen( *wxBLACK_PEN
);
1139 SetBackground( *wxWHITE_BRUSH
);
1140 SetTextForeground( *wxBLACK
);
1142 // set origin according to paper size
1143 SetDeviceOrigin( 0,0 );
1151 void wxPostScriptDC::EndDoc ()
1153 wxCHECK_RET( m_ok
&& m_pstream
, "invalid postscript dc" );
1158 *m_pstream
<< "grestore\n";
1164 m_pstream
= (ofstream
*) NULL
;
1167 char *header_file
= wxGetTempFileName("ps");
1169 m_pstream
= new ofstream( header_file
);
1171 *m_pstream
<< "%!PS-Adobe-2.0\n"; /* PostScript magic strings */
1172 *m_pstream
<< "%%Title: " << (const char *) m_title
<< "\n";
1173 *m_pstream
<< "%%Creator: " << wxTheApp
->argv
[0] << "\n";
1174 *m_pstream
<< "%%CreationDate: " << wxNow() << "\n";
1177 if ( wxGetEmailAddress(userID
, sizeof(userID
)) )
1179 *m_pstream
<< "%%For: " << (char *)userID
;
1181 if (wxGetUserName(userName
, sizeof(userName
)))
1182 *m_pstream
<< " (" << (char *)userName
<< ")";
1185 else if ( wxGetUserName(userID
, sizeof(userID
)) )
1187 *m_pstream
<< "%%For: " << (char *)userID
<< "\n";
1190 // THE FOLLOWING HAS BEEN CONTRIBUTED BY Andy Fyfe <andy@hyperparallel.com>
1192 long wx_printer_translate_x
, wx_printer_translate_y
;
1193 double wx_printer_scale_x
, wx_printer_scale_y
;
1194 wxThePrintSetupData
->GetPrinterTranslation(&wx_printer_translate_x
, &wx_printer_translate_y
);
1195 wxThePrintSetupData
->GetPrinterScaling(&wx_printer_scale_x
, &wx_printer_scale_y
);
1197 if (wxThePrintSetupData
->GetPrinterOrientation() == PS_LANDSCAPE
)
1199 *m_pstream
<< "%%Orientation: Landscape\n";
1203 *m_pstream
<< "%%Orientation: Portrait\n";
1206 // Compute the bounding box. Note that it is in the default user
1207 // coordinate system, thus we have to convert the values.
1208 long llx
= (long) ((XLOG2DEV(m_minX
)+wx_printer_translate_x
)*wx_printer_scale_x
);
1209 long lly
= (long) ((YLOG2DEV(m_minY
)+wx_printer_translate_y
)*wx_printer_scale_y
);
1210 long urx
= (long) ((XLOG2DEV(m_maxX
)+wx_printer_translate_x
)*wx_printer_scale_x
);
1211 long ury
= (long) ((YLOG2DEV(m_maxY
)+wx_printer_translate_y
)*wx_printer_scale_y
);
1213 // If we're landscape, our sense of "x" and "y" is reversed.
1214 if (wxThePrintSetupData
->GetPrinterOrientation() == PS_LANDSCAPE
)
1217 tmp
= llx
; llx
= lly
; lly
= tmp
;
1218 tmp
= urx
; urx
= ury
; ury
= tmp
;
1220 // We need either the two lines that follow, or we need to subtract
1221 // min_x from real_translate_y, which is commented out below.
1222 llx
= llx
- (long)(m_minX
*wx_printer_scale_y
);
1223 urx
= urx
- (long)(m_minX
*wx_printer_scale_y
);
1226 // The Adobe specifications call for integers; we round as to make
1227 // the bounding larger.
1228 *m_pstream
<< "%%BoundingBox: "
1229 << floor((double)llx
) << " " << floor((double)lly
) << " "
1230 << ceil((double)urx
) << " " << ceil((double)ury
) << "\n";
1231 *m_pstream
<< "%%Pages: " << (wxPageNumber
- 1) << "\n";
1232 *m_pstream
<< "%%EndComments\n\n";
1234 // To check the correctness of the bounding box, postscript commands
1235 // to draw a box corresponding to the bounding box are generated below.
1236 // But since we typically don't want to print such a box, the postscript
1237 // commands are generated within comments. These lines appear before any
1238 // adjustment of scale, rotation, or translation, and hence are in the
1239 // default user coordinates.
1240 *m_pstream
<< "% newpath\n";
1241 *m_pstream
<< "% " << llx
<< " " << lly
<< " moveto\n";
1242 *m_pstream
<< "% " << urx
<< " " << lly
<< " lineto\n";
1243 *m_pstream
<< "% " << urx
<< " " << ury
<< " lineto\n";
1244 *m_pstream
<< "% " << llx
<< " " << ury
<< " lineto closepath stroke\n";
1246 *m_pstream
<< "%%BeginProlog\n";
1247 *m_pstream
<< wxPostScriptHeaderEllipse
;
1248 *m_pstream
<< wxPostScriptHeaderEllipticArc
;
1249 *m_pstream
<< wxPostScriptHeaderColourImage
;
1250 *m_pstream
<< wxPostScriptHeaderReencodeISO1
;
1251 *m_pstream
<< wxPostScriptHeaderReencodeISO2
;
1253 if (wxPostScriptHeaderSpline
)
1254 *m_pstream
<< wxPostScriptHeaderSpline
;
1255 *m_pstream
<< "%%EndProlog\n";
1258 m_pstream
= (ofstream
*) NULL
;
1260 char *tmp_file
= wxGetTempFileName("ps");
1262 // Paste header Before wx_printer_file
1263 wxConcatFiles (header_file
, wxThePrintSetupData
->GetPrinterFile(), tmp_file
);
1264 wxRemoveFile (header_file
);
1265 wxRemoveFile (wxThePrintSetupData
->GetPrinterFile());
1266 wxRenameFile(tmp_file
, wxThePrintSetupData
->GetPrinterFile());
1268 #if defined(__X__) || defined(__WXGTK__)
1271 switch (wxThePrintSetupData
->GetPrinterMode()) {
1275 argv
[0] = wxThePrintSetupData
->GetPrintPreviewCommand();
1276 argv
[1] = wxThePrintSetupData
->GetPrinterFile();
1277 argv
[2] = (char *) NULL
;
1278 wxExecute (argv
, TRUE
);
1279 wxRemoveFile(wxThePrintSetupData
->GetPrinterFile());
1287 argv
[argc
++] = wxThePrintSetupData
->GetPrinterCommand();
1289 // !SM! If we simply assign to argv[1] here, if printer options
1290 // are blank, we get an annoying and confusing message from lpr.
1291 char * opts
= wxThePrintSetupData
->GetPrinterOptions();
1293 argv
[argc
++] = opts
;
1295 argv
[argc
++] = wxThePrintSetupData
->GetPrinterFile();
1296 argv
[argc
++] = (char *) NULL
;
1297 wxExecute (argv
, TRUE
);
1298 wxRemoveFile(wxThePrintSetupData
->GetPrinterFile());
1309 void wxPostScriptDC::StartPage ()
1311 wxCHECK_RET( m_ok
&& m_pstream
, "invalid postscript dc" );
1313 *m_pstream
<< "%%Page: " << (wxPageNumber
++) << "\n";
1315 // *m_pstream << "matrix currentmatrix\n";
1317 // Added by Chris Breeze
1319 // Each page starts with an "initgraphics" which resets the
1320 // transformation and so we need to reset the origin
1321 // (and rotate the page for landscape printing)
1324 m_scaleFactor = 1.0;
1325 m_logicalOriginX = 0;
1326 m_logicalOriginY = 0;
1330 long translate_x
, translate_y
;
1331 double scale_x
, scale_y
;
1332 wxThePrintSetupData
->GetPrinterTranslation(&translate_x
, &translate_y
);
1333 wxThePrintSetupData
->GetPrinterScaling(&scale_x
, &scale_y
);
1335 if (wxThePrintSetupData
->GetPrinterOrientation() == PS_LANDSCAPE
)
1337 translate_y
-= m_maxY
;
1338 *m_pstream
<< "90 rotate\n";
1341 *m_pstream
<< scale_x
<< " " << scale_y
<< " scale\n";
1342 *m_pstream
<< translate_x
<< " " << translate_y
<< " translate\n";
1345 void wxPostScriptDC::EndPage ()
1347 wxCHECK_RET( m_ok
&& m_pstream
, "invalid postscript dc" );
1349 *m_pstream
<< "showpage\n";
1352 bool wxPostScriptDC::Blit( long WXUNUSED(xdest
), long WXUNUSED(ydest
),
1353 long WXUNUSED(fwidth
), long WXUNUSED(fheight
),
1354 wxDC
*WXUNUSED(source
),
1355 long WXUNUSED(xsrc
), long WXUNUSED(ysrc
),
1356 int WXUNUSED(rop
), bool WXUNUSED(useMask
) )
1358 wxCHECK_MSG( m_ok
&& m_pstream
, FALSE
, "invalid postscript dc" );
1360 wxFAIL_MSG( "wxPostScriptDC::Blit no yet implemented." );
1365 long wxPostScriptDC::GetCharHeight ()
1368 return m_font
.GetPointSize();
1373 void wxPostScriptDC::GetTextExtent (const wxString
& string
, long *x
, long *y
,
1374 long *descent
, long *externalLeading
, wxFont
*theFont
,
1375 bool WXUNUSED(use16
))
1377 // if (!m_pstream) return;
1379 wxFont
*fontToUse
= theFont
;
1381 if (!fontToUse
) fontToUse
= (wxFont
*) &m_font
;
1383 #if !USE_AFM_FOR_POSTSCRIPT
1384 // Provide a VERY rough estimate (avoid using it)
1385 // Chris Breeze 5/11/97: produces accurate results for mono-spaced
1386 // font such as Courier (aka wxMODERN)
1390 height
= fontToUse
->GetPointSize();
1392 *x
= strlen (string
) * height
* 72 / 120;
1393 *y
= (long) (height
* 1.32); // allow for descender
1397 if (externalLeading
)
1398 *externalLeading
= 0;
1400 // +++++ start of contributed code +++++
1402 // ************************************************************
1403 // method for calculating string widths in postscript:
1404 // read in the AFM (adobe font metrics) file for the
1405 // actual font, parse it and extract the character widths
1406 // and also the descender. this may be improved, but for now
1407 // it works well. the AFM file is only read in if the
1408 // font is changed. this may be chached in the future.
1409 // calls to GetTextExtent with the font unchanged are rather
1412 // for each font and style used there is an AFM file necessary.
1413 // currently i have only files for the roman font family.
1414 // i try to get files for the other ones!
1416 // CAVE: the size of the string is currently always calculated
1417 // in 'points' (1/72 of an inch). this should later on be
1418 // changed to depend on the mapping mode.
1419 // CAVE: the path to the AFM files must be set before calling this
1420 // function. this is usually done by a call like the following:
1421 // wxSetAFMPath("d:\\wxw161\\afm\\");
1425 // wxPostScriptDC dc(NULL, TRUE);
1427 // wxSetAFMPath("d:\\wxw161\\afm\\");
1428 // dc.StartDoc("Test");
1431 // dc.SetFont(new wxFont(10, wxROMAN, wxNORMAL, wxNORMAL));
1432 // dc.GetTextExtent("Hallo",&w,&h);
1437 // by steve (stefan.hammes@urz.uni-heidelberg.de)
1438 // created: 10.09.94
1439 // updated: 14.05.95
1441 assert(fontToUse
&& "void wxPostScriptDC::GetTextExtent: no font defined");
1442 assert(x
&& "void wxPostScriptDC::GetTextExtent: x == NULL");
1443 assert(y
&& "void wxPostScriptDC::GetTextExtent: y == NULL");
1445 // these static vars are for storing the state between calls
1446 static int lastFamily
= INT_MIN
;
1447 static int lastSize
= INT_MIN
;
1448 static int lastStyle
= INT_MIN
;
1449 static int lastWeight
= INT_MIN
;
1450 static int lastDescender
= INT_MIN
;
1451 static int lastWidths
[256]; // widths of the characters
1453 // get actual parameters
1454 const int Family
= fontToUse
->GetFamily();
1455 const int Size
= fontToUse
->GetPointSize();
1456 const int Style
= fontToUse
->GetStyle();
1457 const int Weight
= fontToUse
->GetWeight();
1459 // if we have another font, read the font-metrics
1460 if(Family
!=lastFamily
||Size
!=lastSize
||Style
!=lastStyle
||Weight
!=lastWeight
){
1461 // store actual values
1462 lastFamily
= Family
;
1465 lastWeight
= Weight
;
1467 // read in new font metrics **************************************
1469 // 1. construct filename ******************************************
1470 /* MATTHEW: [2] Use wxTheFontNameDirectory */
1473 // Julian - we'll need to do this a different way now we've removed the
1474 // font directory system. Must find Stefan's original code.
1476 name
= wxTheFontNameDirectory
->GetAFMName(Family
, Weight
, Style
);
1480 // get the directory of the AFM files
1484 strcpy(afmName
,wxGetAFMPath());
1486 // 2. open and process the file **********************************
1488 // a short explanation of the AFM format:
1489 // we have for each character a line, which gives its size
1492 // C 63 ; WX 444 ; N question ; B 49 -14 395 676 ;
1494 // that means, we have a character with ascii code 63, and width
1495 // (444/1000 * fontSize) points.
1496 // the other data is ignored for now!
1498 // when the font has changed, we read in the right AFM file and store the
1499 // character widths in an array, which is processed below (see point 3.).
1501 // new elements JC Sun Aug 25 23:21:44 MET DST 1996
1504 strcat(afmName
,name
);
1505 strcat(afmName
,".afm");
1506 FILE *afmFile
= fopen(afmName
,"r");
1508 wxLogDebug("GetTextExtent: can't open AFM file '%s'\n",afmName
);
1509 wxLogDebug(" using approximate values\n");
1511 for (i
=0; i
<256; i
++) lastWidths
[i
] = 500; // an approximate value
1512 lastDescender
= -150; // dito.
1515 // init the widths array
1516 for(i
=0; i
<256; i
++) lastWidths
[i
]= INT_MIN
;
1517 // some variables for holding parts of a line
1518 char cString
[10],semiString
[10],WXString
[10],descString
[20];
1519 char upString
[30], utString
[30], encString
[50];
1522 // read in the file and parse it
1523 while(fgets(line
,sizeof(line
),afmFile
)!=NULL
){
1524 // A.) check for descender definition
1525 if(strncmp(line
,"Descender",9)==0){
1526 if((sscanf(line
,"%s%d",descString
,&lastDescender
)!=2)
1527 || (strcmp(descString
,"Descender")!=0)) {
1528 wxLogDebug("AFM-file '%s': line '%s' has error (bad descender)\n",
1532 // JC 1.) check for UnderlinePosition
1533 else if(strncmp(line
,"UnderlinePosition",17)==0){
1534 if((sscanf(line
,"%s%lf",upString
,&UnderlinePosition
)!=2)
1535 || (strcmp(upString
,"UnderlinePosition")!=0)) {
1536 wxLogDebug("AFM-file '%s': line '%s' has error (bad UnderlinePosition)\n",
1540 // JC 2.) check for UnderlineThickness
1541 else if(strncmp(line
,"UnderlineThickness",18)==0){
1542 if((sscanf(line
,"%s%lf",utString
,&UnderlineThickness
)!=2)
1543 || (strcmp(utString
,"UnderlineThickness")!=0)) {
1544 wxLogDebug("AFM-file '%s': line '%s' has error (bad UnderlineThickness)\n",
1548 // JC 3.) check for EncodingScheme
1549 else if(strncmp(line
,"EncodingScheme",14)==0){
1550 if((sscanf(line
,"%s%s",utString
,encString
)!=2)
1551 || (strcmp(utString
,"EncodingScheme")!=0)) {
1552 wxLogDebug("AFM-file '%s': line '%s' has error (bad EncodingScheme)\n",
1555 else if (strncmp(encString
, "AdobeStandardEncoding", 21))
1557 wxLogDebug("AFM-file '%s': line '%s' has error (unsupported EncodingScheme %s)\n",
1558 afmName
,line
, encString
);
1561 // B.) check for char-width
1562 else if(strncmp(line
,"C ",2)==0){
1563 if(sscanf(line
,"%s%d%s%s%d",
1564 cString
,&ascii
,semiString
,WXString
,&cWidth
)!=5){
1565 wxLogDebug("AFM-file '%s': line '%s' has an error (bad character width)\n",afmName
,line
);
1567 if(strcmp(cString
,"C")!=0 || strcmp(semiString
,";")!=0 ||
1568 strcmp(WXString
,"WX")!=0){
1569 wxLogDebug("AFM-file '%s': line '%s' has a format error\n",afmName
,line
);
1571 //printf(" char '%c'=%d has width '%d'\n",ascii,ascii,cWidth);
1572 if(ascii
>=0 && ascii
<256){
1573 lastWidths
[ascii
] = cWidth
; // store width
1575 /* MATTHEW: this happens a lot; don't print an error */
1576 // wxLogDebug("AFM-file '%s': ASCII value %d out of range\n",afmName,ascii);
1579 // C.) ignore other entries.
1583 // hack to compute correct values for german 'Umlaute'
1584 // the correct way would be to map the character names
1585 // like 'adieresis' to corresp. positions of ISOEnc and read
1586 // these values from AFM files, too. Maybe later ...
1587 lastWidths
[196] = lastWidths
['A']; // Ä
1588 lastWidths
[228] = lastWidths
['a']; // ä
1589 lastWidths
[214] = lastWidths
['O']; // Ö
1590 lastWidths
[246] = lastWidths
['o']; // ö
1591 lastWidths
[220] = lastWidths
['U']; // Ü
1592 lastWidths
[252] = lastWidths
['u']; // ü
1593 lastWidths
[223] = lastWidths
[251]; // ß
1596 // JC: calculate UnderlineThickness/UnderlinePosition
1597 m_underlinePosition
= m_underlinePosition
* fontToUse
->GetPointSize() / 1000.0f
;
1598 m_underlineThickness
= m_underlineThickness
* fontToUse
->GetPointSize() / 1000.0f
* m_scaleFactor
;
1600 // 3. now the font metrics are read in, calc size *******************
1601 // this is done by adding the widths of the characters in the
1602 // string. they are given in 1/1000 of the size!
1605 long height
=Size
; // by default
1607 for(p
=(unsigned char *)(const char *)string
; *p
; p
++){
1608 if(lastWidths
[*p
]== INT_MIN
){
1609 wxLogDebug("GetTextExtent: undefined width for character '%c' (%d)\n",
1611 widthSum
+= (long)(lastWidths
[' ']/1000.0F
* Size
); // assume space
1613 widthSum
+= (long)((lastWidths
[*p
]/1000.0F
)*Size
);
1616 // add descender to height (it is usually a negative value)
1617 if(lastDescender
!=INT_MIN
){
1618 height
+= (long)(((-lastDescender
)/1000.0F
) * Size
); /* MATTHEW: forgot scale */
1621 // return size values
1625 // return other parameters
1627 if(lastDescender
!=INT_MIN
){
1628 *descent
= (long)(((-lastDescender
)/1000.0F
) * Size
); /* MATTHEW: forgot scale */
1634 // currently no idea how to calculate this!
1635 // if (externalLeading) *externalLeading = 0;
1636 if (externalLeading
)
1637 *externalLeading
= 0;
1639 // ----- end of contributed code -----
1643 void wxPostScriptDC::GetSizeMM(long *width
, long *height
) const
1645 const char *paperType
= wxThePrintSetupData
->GetPaperName();
1647 if (!paperType
) paperType
= _("A4 210 x 297 mm");
1649 wxPrintPaperType
*paper
= wxThePrintPaperDatabase
->FindPaperType(paperType
);
1651 if (!paper
) paper
= wxThePrintPaperDatabase
->FindPaperType(_("A4 210 x 297 mm"));
1655 if (width
) *width
= paper
->widthMM
;
1656 if (height
) *height
= paper
->heightMM
;
1660 if (width
) *width
= 210;
1661 if (height
) *height
= 297;