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"
30 //-----------------------------------------------------------------------------
31 // start and end of document/page
32 //-----------------------------------------------------------------------------
34 static const char *wxPostScriptHeaderEllipse
= "\
35 /ellipsedict 8 dict def\n\
36 ellipsedict /mtrx matrix put\n\
40 /startangle exch def\n\
45 /savematrix mtrx currentmatrix def\n\
48 0 0 1 startangle endangle arc\n\
49 savematrix setmatrix\n\
54 static const char *wxPostScriptHeaderEllipticArc
= "\
55 /ellipticarcdict 8 dict def\n\
56 ellipticarcdict /mtrx matrix put\n\
58 { ellipticarcdict begin\n\
61 /startangle exch def\n\
66 /savematrix mtrx currentmatrix def\n\
69 do_fill { 0 0 moveto } if\n\
70 0 0 1 startangle endangle arc\n\
71 savematrix setmatrix\n\
72 do_fill { fill }{ stroke } ifelse\n\
76 static const char *wxPostScriptHeaderSpline
= "\
77 /DrawSplineSection {\n\
84 /xa x1 x2 x1 sub 0.666667 mul add def\n\
85 /ya y1 y2 y1 sub 0.666667 mul add def\n\
86 /xb x3 x2 x3 sub 0.666667 mul add def\n\
87 /yb y3 y2 y3 sub 0.666667 mul add def\n\
89 xa ya xb yb x3 y3 curveto\n\
93 static const char *wxPostScriptHeaderColourImage
= "\
94 % define 'colorimage' if it isn't defined\n\
95 % ('colortogray' and 'mergeprocs' come from xwd2ps\n\
97 /colorimage where % do we know about 'colorimage'?\n\
98 { pop } % yes: pop off the 'dict' returned\n\
100 /colortogray { % define an RGB->I function\n\
101 /rgbdata exch store % call input 'rgbdata'\n\
102 rgbdata length 3 idiv\n\
103 /npixls exch store\n\
105 0 1 npixls 1 sub {\n\
107 rgbdata rgbindx get 20 mul % Red\n\
108 rgbdata rgbindx 1 add get 32 mul % Green\n\
109 rgbdata rgbindx 2 add get 12 mul % Blue\n\
110 add add 64 idiv % I = .5G + .31R + .18B\n\
112 /rgbindx rgbindx 3 add store\n\
114 grays 0 npixls getinterval\n\
117 % Utility procedure for colorimage operator.\n\
118 % This procedure takes two procedures off the\n\
119 % stack and merges them into a single procedure.\n\
121 /mergeprocs { % def\n\
140 /colorimage { % def\n\
141 pop pop % remove 'false 3' operands\n\
142 {colortogray} mergeprocs\n\
145 } ifelse % end of 'false' case\n\
148 static char wxPostScriptHeaderReencodeISO1
[] =
150 "dup dup findfont dup length dict begin\n"
151 "{ 1 index /FID ne { def }{ pop pop } ifelse } forall\n"
152 "/Encoding ISOLatin1Encoding def\n"
153 "currentdict end definefont\n"
155 "/ISOLatin1Encoding [\n"
156 "/.notdef/.notdef/.notdef/.notdef/.notdef/.notdef/.notdef/.notdef\n"
157 "/.notdef/.notdef/.notdef/.notdef/.notdef/.notdef/.notdef/.notdef\n"
158 "/.notdef/.notdef/.notdef/.notdef/.notdef/.notdef/.notdef/.notdef\n"
159 "/.notdef/.notdef/.notdef/.notdef/.notdef/.notdef/.notdef/.notdef\n"
160 "/space/exclam/quotedbl/numbersign/dollar/percent/ampersand/quoteright\n"
161 "/parenleft/parenright/asterisk/plus/comma/minus/period/slash\n"
162 "/zero/one/two/three/four/five/six/seven/eight/nine/colon/semicolon\n"
163 "/less/equal/greater/question/at/A/B/C/D/E/F/G/H/I/J/K/L/M/N\n"
164 "/O/P/Q/R/S/T/U/V/W/X/Y/Z/bracketleft/backslash/bracketright\n"
165 "/asciicircum/underscore/quoteleft/a/b/c/d/e/f/g/h/i/j/k/l/m\n"
166 "/n/o/p/q/r/s/t/u/v/w/x/y/z/braceleft/bar/braceright/asciitilde\n"
167 "/.notdef/.notdef/.notdef/.notdef/.notdef/.notdef/.notdef/.notdef\n"
168 "/.notdef/.notdef/.notdef/.notdef/.notdef/.notdef/.notdef/.notdef\n"
169 "/.notdef/dotlessi/grave/acute/circumflex/tilde/macron/breve\n"
170 "/dotaccent/dieresis/.notdef/ring/cedilla/.notdef/hungarumlaut\n";
172 static char wxPostScriptHeaderReencodeISO2
[] =
173 "/ogonek/caron/space/exclamdown/cent/sterling/currency/yen/brokenbar\n"
174 "/section/dieresis/copyright/ordfeminine/guillemotleft/logicalnot\n"
175 "/hyphen/registered/macron/degree/plusminus/twosuperior/threesuperior\n"
176 "/acute/mu/paragraph/periodcentered/cedilla/onesuperior/ordmasculine\n"
177 "/guillemotright/onequarter/onehalf/threequarters/questiondown\n"
178 "/Agrave/Aacute/Acircumflex/Atilde/Adieresis/Aring/AE/Ccedilla\n"
179 "/Egrave/Eacute/Ecircumflex/Edieresis/Igrave/Iacute/Icircumflex\n"
180 "/Idieresis/Eth/Ntilde/Ograve/Oacute/Ocircumflex/Otilde/Odieresis\n"
181 "/multiply/Oslash/Ugrave/Uacute/Ucircumflex/Udieresis/Yacute\n"
182 "/Thorn/germandbls/agrave/aacute/acircumflex/atilde/adieresis\n"
183 "/aring/ae/ccedilla/egrave/eacute/ecircumflex/edieresis/igrave\n"
184 "/iacute/icircumflex/idieresis/eth/ntilde/ograve/oacute/ocircumflex\n"
185 "/otilde/odieresis/divide/oslash/ugrave/uacute/ucircumflex/udieresis\n"
186 "/yacute/thorn/ydieresis\n"
189 //-------------------------------------------------------------------------------
191 //-------------------------------------------------------------------------------
193 wxPostScriptDC::wxPostScriptDC ()
195 m_pstream
= (ofstream
*) NULL
;
205 m_underlinePosition
= 0.0;
206 m_underlineThickness
= 0.0;
208 m_signX
= 1; // default x-axis left to right
209 m_signY
= -1; // default y-axis bottom up -> top down
212 wxPostScriptDC::wxPostScriptDC (const wxString
& file
, bool interactive
, wxWindow
*parent
)
214 m_pstream
= (ofstream
*) NULL
;
224 m_underlinePosition
= 0.0;
225 m_underlineThickness
= 0.0;
227 m_signX
= 1; // default x-axis left to right
228 m_signY
= -1; // default y-axis bottom up -> top down
230 Create(file
, interactive
, parent
);
233 bool wxPostScriptDC::Create(const wxString
& file
, bool interactive
, wxWindow
*parent
)
235 m_isInteractive
= interactive
;
241 // Can only send to file in Windows
242 wxThePrintSetupData
->SetPrinterMode(PS_FILE
);
247 if ((m_ok
= PrinterDialog (parent
) ) == FALSE
) return FALSE
;
257 wxPostScriptDC::~wxPostScriptDC ()
259 if (m_pstream
) delete m_pstream
;
262 bool wxPostScriptDC::Ok() const
267 bool wxPostScriptDC::PrinterDialog(wxWindow
*parent
)
269 wxPostScriptPrintDialog
dialog( parent
, _("Printer Settings"), wxPoint(150, 150), wxSize(400, 400),
270 wxDEFAULT_DIALOG_STYLE
| wxDIALOG_MODAL
);
271 m_ok
= (dialog
.ShowModal () == wxID_OK
);
273 if (!m_ok
) return FALSE
;
275 if ((m_filename
== "") &&
276 (wxThePrintSetupData
->GetPrinterMode() == PS_PREVIEW
||
277 wxThePrintSetupData
->GetPrinterMode() == PS_PRINTER
))
281 wxThePrintSetupData
->SetPrinterFile("preview");
283 // For PS_PRINTER action this depends on a Unix-style print spooler
284 // since the wx_printer_file can be destroyed during a session
285 // @@@ TODO: a Windows-style answer for non-Unix
287 wxGetUserId (userId
, sizeof (userId
) / sizeof (char));
289 strcpy (tmp
, "/tmp/preview_");
290 strcat (tmp
, userId
);
291 wxThePrintSetupData
->SetPrinterFile(tmp
);
294 strcpy(tmp2
, wxThePrintSetupData
->GetPrinterFile());
295 strcat (tmp2
, ".ps");
296 wxThePrintSetupData
->SetPrinterFile(tmp2
);
299 else if ((m_filename
== "") && (wxThePrintSetupData
->GetPrinterMode() == PS_FILE
))
301 char *file
= wxSaveFileSelector (_("PostScript"), "ps");
307 wxThePrintSetupData
->SetPrinterFile(file
);
315 void wxPostScriptDC::SetClippingRegion (long x
, long y
, long w
, long h
)
317 wxCHECK_RET( m_ok
&& m_pstream
, "invalid postscript dc" );
319 if (m_clipping
) return;
321 wxDC::SetClippingRegion( x
, y
, w
, h
);
324 *m_pstream
<< "gsave\n"
326 << XLOG2DEV(x
) << " " << YLOG2DEV(y
) << " moveto\n"
327 << XLOG2DEV(x
+w
) << " " << YLOG2DEV(y
) << " lineto\n"
328 << XLOG2DEV(x
+w
) << " " << YLOG2DEV(y
+h
) << " lineto\n"
329 << XLOG2DEV(x
) << " " << YLOG2DEV(y
+h
) << " lineto\n"
330 << "closepath clip newpath\n";
333 void wxPostScriptDC::SetClippingRegion( const wxRegion
&WXUNUSED(region
) )
337 void wxPostScriptDC::DestroyClippingRegion()
339 wxCHECK_RET( m_ok
&& m_pstream
, "invalid postscript dc" );
341 wxDC::DestroyClippingRegion();
346 *m_pstream
<< "grestore\n";
350 void wxPostScriptDC::Clear()
352 wxFAIL_MSG( "wxPostScriptDC::Clear not implemented." );
355 void wxPostScriptDC::FloodFill (long WXUNUSED(x
), long WXUNUSED(y
), const wxColour
&WXUNUSED(col
), int WXUNUSED(style
))
357 wxFAIL_MSG( "wxPostScriptDC::FloodFill not implemented." );
360 bool wxPostScriptDC::GetPixel (long WXUNUSED(x
), long WXUNUSED(y
), wxColour
* WXUNUSED(col
)) const
362 wxFAIL_MSG( "wxPostScriptDC::GetPixel not implemented." );
366 void wxPostScriptDC::CrossHair (long WXUNUSED(x
), long WXUNUSED(y
))
368 wxFAIL_MSG( "wxPostScriptDC::CrossHair not implemented." );
371 void wxPostScriptDC::DrawLine (long x1
, long y1
, long x2
, long y2
)
373 wxCHECK_RET( m_ok
&& m_pstream
, "invalid postscript dc" );
375 if (m_pen
.GetStyle() == wxTRANSPARENT
) return;
379 *m_pstream
<< "newpath\n"
380 << XLOG2DEV(x1
) << " " << YLOG2DEV (y1
) << " moveto\n"
381 << XLOG2DEV(x2
) << " " << YLOG2DEV (y2
) << " lineto\n"
384 CalcBoundingBox( x1
, y1
);
385 CalcBoundingBox( x2
, y2
);
388 #define RAD2DEG 57.29577951308
390 void wxPostScriptDC::DrawArc (long x1
, long y1
, long x2
, long y2
, long xc
, long yc
)
392 wxCHECK_RET( m_ok
&& m_pstream
, "invalid postscript dc" );
396 long radius
= (long) sqrt(dx
*dx
+dy
*dy
);
397 double alpha1
, alpha2
;
399 if (x1
== x2
&& y1
== y2
)
403 } else if (radius
== 0.0)
405 alpha1
= alpha2
= 0.0;
408 alpha1
= (x1
- xc
== 0) ?
409 (y1
- yc
< 0) ? 90.0 : -90.0 :
410 -atan2(double(y1
-yc
), double(x1
-xc
)) * RAD2DEG
;
411 alpha2
= (x2
- xc
== 0) ?
412 (y2
- yc
< 0) ? 90.0 : -90.0 :
413 -atan2(double(y2
-yc
), double(x2
-xc
)) * RAD2DEG
;
415 while (alpha1
<= 0) alpha1
+= 360;
416 while (alpha2
<= 0) alpha2
+= 360; // adjust angles to be between
417 while (alpha1
> 360) alpha1
-= 360; // 0 and 360 degree
418 while (alpha2
> 360) alpha2
-= 360;
420 if (m_brush
.GetStyle() != wxTRANSPARENT
)
423 *m_pstream
<< "newpath\n"
424 << XLOG2DEV(xc
) << " "
425 << YLOG2DEV(yc
) << " "
426 << XLOG2DEVREL(radius
) << " "
427 << YLOG2DEVREL(radius
) << " "
429 << alpha2
<< " ellipse\n"
430 << XLOG2DEV(xc
) << " "
431 << YLOG2DEV(yc
) << " lineto\n"
436 if (m_pen
.GetStyle() != wxTRANSPARENT
)
439 *m_pstream
<< "newpath\n"
440 << XLOG2DEV(xc
) << " "
441 << YLOG2DEV(yc
) << " "
442 << XLOG2DEVREL(radius
) << " "
443 << YLOG2DEVREL(radius
) << " "
445 << alpha2
<< " ellipse\n"
449 CalcBoundingBox( xc
-radius
, yc
-radius
);
450 CalcBoundingBox( xc
+radius
, yc
+radius
);
453 void wxPostScriptDC::DrawEllipticArc(long x
,long y
,long w
,long h
,double sa
,double ea
)
455 wxCHECK_RET( m_ok
&& m_pstream
, "invalid postscript dc" );
457 if (sa
>=360 || sa
<=-360) sa
=sa
-int(sa
/360)*360;
458 if (ea
>=360 || ea
<=-360) ea
=ea
-int(ea
/360)*360;
464 DrawEllipse(x
,y
,w
,h
);
468 if (m_brush
.GetStyle () != wxTRANSPARENT
)
472 *m_pstream
<< "newpath\n"
473 << XLOG2DEV(x
+w
/2) << " " << YLOG2DEV(y
+h
/2) << " "
474 << XLOG2DEVREL(w
/2) << " " << YLOG2DEVREL(h
/2) << " "
475 << int(sa
) <<" "<< int(ea
) << " true ellipticarc\n";
477 CalcBoundingBox( x
,y
);
478 CalcBoundingBox( x
+w
, y
+h
);
481 if (m_pen
.GetStyle () != wxTRANSPARENT
)
485 *m_pstream
<< "newpath\n"
486 << XLOG2DEV(x
+w
/2) << " " << YLOG2DEV(y
+h
/2) << " "
487 << XLOG2DEVREL(w
/2) << " " << XLOG2DEVREL(h
/2) << " "
488 << int(sa
) <<" "<< int(ea
) << " false ellipticarc\n";
490 CalcBoundingBox( x
, y
);
491 CalcBoundingBox( x
+w
, y
+h
);
495 void wxPostScriptDC::DrawPoint (long x
, long y
)
497 wxCHECK_RET( m_ok
&& m_pstream
, "invalid postscript dc" );
499 if (m_pen
.GetStyle() == wxTRANSPARENT
) return;
503 *m_pstream
<< "newpath\n"
504 << XLOG2DEV(x
) << " " << YLOG2DEV (y
) << " moveto\n"
505 << XLOG2DEV(x
+1) << " " << YLOG2DEV (y
) << " lineto\n"
508 CalcBoundingBox( x
, y
);
511 void wxPostScriptDC::DrawPolygon (int n
, wxPoint points
[], long xoffset
, long yoffset
, int WXUNUSED(fillStyle
))
513 wxCHECK_RET( m_ok
&& m_pstream
, "invalid postscript dc" );
517 if (m_brush
.GetStyle () != wxTRANSPARENT
)
521 *m_pstream
<< "newpath\n";
523 long xx
= XLOG2DEV(points
[0].x
+ xoffset
);
524 long yy
= YLOG2DEV(points
[0].y
+ yoffset
);
525 *m_pstream
<< xx
<< " " << yy
<< " moveto\n";
526 CalcBoundingBox( points
[0].x
+ xoffset
, points
[0].y
+ yoffset
);
528 for (int i
= 1; i
< n
; i
++)
530 xx
= XLOG2DEV(points
[i
].x
+ xoffset
);
531 yy
= YLOG2DEV(points
[i
].y
+ yoffset
);
532 *m_pstream
<< xx
<< " " << yy
<< " lineto\n";
533 CalcBoundingBox( points
[i
].x
+ xoffset
, points
[i
].y
+ yoffset
);
535 *m_pstream
<< "fill\n";
538 if (m_pen
.GetStyle () != wxTRANSPARENT
)
542 *m_pstream
<< "newpath\n";
544 long xx
= XLOG2DEV(points
[0].x
+ xoffset
);
545 long yy
= YLOG2DEV(points
[0].y
+ yoffset
);
546 *m_pstream
<< xx
<< " " << yy
<< " moveto\n";
547 CalcBoundingBox( points
[0].x
+ xoffset
, points
[0].y
+ yoffset
);
549 for (int i
= 1; i
< n
; i
++)
551 xx
= XLOG2DEV(points
[i
].x
+ xoffset
);
552 yy
= YLOG2DEV(points
[i
].y
+ yoffset
);
553 *m_pstream
<< xx
<< " " << yy
<< " lineto\n";
554 CalcBoundingBox( points
[i
].x
+ xoffset
, points
[i
].y
+ yoffset
);
557 *m_pstream
<< "stroke\n";
561 void wxPostScriptDC::DrawLines (int n
, wxPoint points
[], long xoffset
, long yoffset
)
563 wxCHECK_RET( m_ok
&& m_pstream
, "invalid postscript dc" );
565 if (m_pen
.GetStyle() == wxTRANSPARENT
) return;
570 for (int i
=0; i
<n
; ++i
)
572 CalcBoundingBox( XLOG2DEV(points
[i
].x
+xoffset
), YLOG2DEV(points
[i
].y
+yoffset
));
575 *m_pstream
<< "newpath\n"
576 << XLOG2DEV(points
[0].x
+xoffset
) << " "
577 << YLOG2DEV(points
[0].y
+yoffset
) << " moveto\n";
579 for (int i
= 1; i
< n
; i
++)
581 *m_pstream
<< XLOG2DEV(points
[i
].x
+xoffset
) << " "
582 << YLOG2DEV(points
[i
].y
+yoffset
) << " lineto\n";
585 *m_pstream
<< "stroke\n";
588 void wxPostScriptDC::DrawRectangle (long x
, long y
, long width
, long height
)
590 wxCHECK_RET( m_ok
&& m_pstream
, "invalid postscript dc" );
592 if (m_brush
.GetStyle () != wxTRANSPARENT
)
596 *m_pstream
<< "newpath\n"
597 << XLOG2DEV(x
) << " " << YLOG2DEV(y
) << " moveto\n"
598 << XLOG2DEV(x
+ width
) << " " << YLOG2DEV(y
) << " lineto\n"
599 << XLOG2DEV(x
+ width
) << " " << YLOG2DEV(y
+ height
) << " lineto\n"
600 << XLOG2DEV(x
) << " " << YLOG2DEV(y
+ height
) << " lineto\n"
604 CalcBoundingBox( x
, y
);
605 CalcBoundingBox( x
+ width
, y
+ height
);
608 if (m_pen
.GetStyle () != wxTRANSPARENT
)
612 *m_pstream
<< "newpath\n"
613 << XLOG2DEV(x
) << " " << YLOG2DEV(y
) << " moveto\n"
614 << XLOG2DEV(x
+ width
) << " " << YLOG2DEV(y
) << " lineto\n"
615 << XLOG2DEV(x
+ width
) << " " << YLOG2DEV(y
+ height
) << " lineto\n"
616 << XLOG2DEV(x
) << " " << YLOG2DEV(y
+ height
) << " lineto\n"
620 CalcBoundingBox( x
, y
);
621 CalcBoundingBox( x
+ width
, y
+ height
);
625 void wxPostScriptDC::DrawRoundedRectangle (long x
, long y
, long width
, long height
, double radius
)
627 wxCHECK_RET( m_ok
&& m_pstream
, "invalid postscript dc" );
631 // Now, a negative radius is interpreted to mean
632 // 'the proportion of the smallest X or Y dimension'
633 double smallest
= 0.0;
638 radius
= (-radius
* smallest
);
641 long rad
= (long) radius
;
643 if (m_brush
.GetStyle () != wxTRANSPARENT
)
647 // Draw rectangle anticlockwise
648 *m_pstream
<< "newpath\n"
649 << XLOG2DEV(x
+ rad
) << " " << YLOG2DEV(y
+ rad
) << " " << XLOG2DEVREL(rad
) << " 90 180 arc\n"
650 << XLOG2DEV(x
) << " " << YLOG2DEV(y
+ rad
) << " moveto\n"
651 << XLOG2DEV(x
+ rad
) << " " << YLOG2DEV(y
+ height
- rad
) << " " << XLOG2DEVREL(rad
) << " 180 270 arc\n"
652 << XLOG2DEV(x
+ width
- rad
) << " " << YLOG2DEV(y
+ height
) << " lineto\n"
653 << XLOG2DEV(x
+ width
- rad
) << " " << YLOG2DEV(y
+ height
- rad
) << " " << XLOG2DEVREL(rad
) << " 270 0 arc\n"
654 << XLOG2DEV(x
+ width
) << " " << YLOG2DEV(y
+ rad
) << " lineto\n"
655 << XLOG2DEV(x
+ width
- rad
) << " " << YLOG2DEV(y
+ rad
) << " " << XLOG2DEVREL(rad
) << " 0 90 arc\n"
656 << XLOG2DEV(x
+ rad
) << " " << YLOG2DEV(y
) << " lineto\n"
660 CalcBoundingBox( x
, y
);
661 CalcBoundingBox( x
+ width
, y
+ height
);
664 if (m_pen
.GetStyle () != wxTRANSPARENT
)
668 // Draw rectangle anticlockwise
669 *m_pstream
<< "newpath\n"
670 << XLOG2DEV(x
+ rad
) << " " << YLOG2DEV(y
+ rad
) << " " << XLOG2DEVREL(rad
) << " 90 180 arc\n"
671 << XLOG2DEV(x
) << " " << YLOG2DEV(y
+ rad
) << " moveto\n"
672 << XLOG2DEV(x
+ rad
) << " " << YLOG2DEV(y
+ height
- rad
) << " " << XLOG2DEVREL(rad
) << " 180 270 arc\n"
673 << XLOG2DEV(x
+ width
- rad
) << " " << YLOG2DEV(y
+ height
) << " lineto\n"
674 << XLOG2DEV(x
+ width
- rad
) << " " << YLOG2DEV(y
+ height
- rad
) << " " << XLOG2DEVREL(rad
) << " 270 0 arc\n"
675 << XLOG2DEV(x
+ width
) << " " << YLOG2DEV(y
+ rad
) << " lineto\n"
676 << XLOG2DEV(x
+ width
- rad
) << " " << YLOG2DEV(y
+ rad
) << " " << XLOG2DEVREL(rad
) << " 0 90 arc\n"
677 << XLOG2DEV(x
+ rad
) << " " << YLOG2DEV(y
) << " lineto\n"
681 CalcBoundingBox( x
, y
);
682 CalcBoundingBox( x
+ width
, y
+ height
);
686 void wxPostScriptDC::DrawEllipse (long x
, long y
, long width
, long height
)
688 wxCHECK_RET( m_ok
&& m_pstream
, "invalid postscript dc" );
690 if (m_brush
.GetStyle () != wxTRANSPARENT
)
694 *m_pstream
<< "newpath\n"
695 << XLOG2DEV(x
+ width
/ 2) << " " << YLOG2DEV(y
+ height
/ 2) << " "
696 << XLOG2DEV(width
/ 2) << " " << YLOG2DEVREL(height
/ 2) << " 0 360 ellipse\n"
699 CalcBoundingBox( x
- width
, y
- height
);
700 CalcBoundingBox( x
+ width
, y
+ height
);
703 if (m_pen
.Ok() && m_pen
.GetStyle () != wxTRANSPARENT
)
707 *m_pstream
<< "newpath\n"
708 << XLOG2DEV(x
+ width
/ 2) << " " << YLOG2DEV(y
+ height
/ 2) << " "
709 << XLOG2DEV(width
/ 2) << " " << YLOG2DEVREL(height
/ 2) << " 0 360 ellipse\n"
712 CalcBoundingBox( x
- width
, y
- height
);
713 CalcBoundingBox( x
+ width
, y
+ height
);
717 void wxPostScriptDC::DrawIcon (const wxIcon
& icon
, long x
, long y
)
719 DrawBitmap( icon
, x
, y
, TRUE
);
722 void wxPostScriptDC::DrawBitmap( const wxBitmap
& bitmap
, long x
, long y
, bool WXUNUSED(useMask
) )
724 wxCHECK_RET( m_ok
&& m_pstream
, "invalid postscript dc" );
726 if (!bitmap
.Ok()) return;
728 wxImage
image( bitmap
);
730 if (!image
.Ok()) return;
732 int ww
= XLOG2DEVREL(image
.GetWidth());
733 int hh
= YLOG2DEVREL(image
.GetHeight());
735 image
= image
.Scale( ww
, hh
);
737 if (!image
.Ok()) return;
739 int xx
= XLOG2DEV(x
);
740 int yy
= YLOG2DEV(y
+ bitmap
.GetHeight());
742 *m_pstream
<< "/origstate save def\n"
744 << "/pix " << ww
<< " string def\n"
745 << "/grays " << ww
<< " string def\n"
746 << "/npixels 0 def\n"
747 << "/rgbindx 0 def\n"
748 << xx
<< " " << yy
<< " translate\n"
749 << ww
<< " " << hh
<< " scale\n"
750 << ww
<< " " << hh
<< " 8\n"
751 << "[" << ww
<< " 0 0 " << (-hh
) << " 0 " << hh
<< "]\n"
752 << "{currentfile pix readhexstring pop}\n"
753 << "false 3 colorimage\n";
755 for (int j
= 0; j
< hh
; j
++)
757 for (int i
= 0; i
< ww
; i
++)
761 wxDecToHex( image
.GetRed(i
,j
), buffer
);
762 *m_pstream
<< buffer
;
763 wxDecToHex( image
.GetGreen(i
,j
), buffer
);
764 *m_pstream
<< buffer
;
765 wxDecToHex( image
.GetBlue(i
,j
), buffer
);
766 *m_pstream
<< buffer
;
771 *m_pstream
<< "end\n";
772 *m_pstream
<< "origstate restore\n";
776 void wxPostScriptDC::SetFont (const wxFont
& font
)
778 wxCHECK_RET( m_ok
&& m_pstream
, "invalid postscript dc" );
780 if (!font
.Ok()) return;
784 char *name
= wxTheFontNameDirectory
->GetPostScriptName( m_font
.GetFamily(),
787 if (!name
) name
= "Times-Roman";
789 *m_pstream
<< "/" << name
<< " reencodeISO def\n"
790 << "/" << name
<< " findfont\n"
791 << YLOG2DEVREL(font
.GetPointSize())
792 << " scalefont setfont\n";
795 void wxPostScriptDC::SetPen( const wxPen
& pen
)
797 wxCHECK_RET( m_ok
&& m_pstream
, "invalid postscript dc" );
799 if (!pen
.Ok()) return;
801 int oldStyle
= m_pen
.GetStyle();
805 *m_pstream
<< XLOG2DEVREL(m_pen
.GetWidth()) << " setlinewidth\n";
808 Line style - WRONG: 2nd arg is OFFSET
810 Here, I'm afraid you do not conceive meaning of parameters of 'setdash'
811 operator correctly. You should look-up this in the Red Book: the 2nd parame-
812 ter is not number of values in the array of the first one, but an offset
813 into this description of the pattern. I mean a real *offset* not index
814 into array. I.e. If the command is [3 4] 1 setdash is used, then there
815 will be first black line *2* units long, then space 4 units, then the
816 pattern of *3* units black, 4 units space will be repeated.
819 static const char *dotted
= "[2 5] 2";
820 static const char *short_dashed
= "[4 4] 2";
821 static const char *long_dashed
= "[4 8] 2";
822 static const char *dotted_dashed
= "[6 6 2 6] 4";
824 const char *psdash
= (char *) NULL
;
825 switch (m_pen
.GetStyle ())
827 case wxDOT
: psdash
= dotted
; break;
828 case wxSHORT_DASH
: psdash
= short_dashed
; break;
829 case wxLONG_DASH
: psdash
= long_dashed
; break;
830 case wxDOT_DASH
: psdash
= dotted_dashed
; break;
833 default: psdash
= "[] 0"; break;
836 if (oldStyle
!= m_pen
.GetStyle())
838 *m_pstream
<< psdash
<< " setdash\n";
842 unsigned char red
= m_pen
.GetColour().Red();
843 unsigned char blue
= m_pen
.GetColour().Blue();
844 unsigned char green
= m_pen
.GetColour().Green();
848 // Anything not white is black
849 if (!(red
== (unsigned char) 255 && blue
== (unsigned char) 255
850 && green
== (unsigned char) 255))
852 red
= (unsigned char) 0;
853 green
= (unsigned char) 0;
854 blue
= (unsigned char) 0;
860 if (!(red
== m_currentRed
&& green
== m_currentGreen
&& blue
== m_currentBlue
))
862 long redPS
= (long) (((int) red
) / 255.0);
863 long bluePS
= (long) (((int) blue
) / 255.0);
864 long greenPS
= (long) (((int) green
) / 255.0);
866 *m_pstream
<< redPS
<< " " << greenPS
<< " " << bluePS
<< " setrgbcolor\n";
869 m_currentBlue
= blue
;
870 m_currentGreen
= green
;
874 void wxPostScriptDC::SetBrush( const wxBrush
& brush
)
876 wxCHECK_RET( m_ok
&& m_pstream
, "invalid postscript dc" );
878 if (!brush
.Ok()) return;
883 unsigned char red
= m_brush
.GetColour ().Red ();
884 unsigned char blue
= m_brush
.GetColour ().Blue ();
885 unsigned char green
= m_brush
.GetColour ().Green ();
889 // Anything not black is white
890 if (!(red
== (unsigned char) 0 && blue
== (unsigned char) 0
891 && green
== (unsigned char) 0))
893 red
= (unsigned char) 255;
894 green
= (unsigned char) 255;
895 blue
= (unsigned char) 255;
901 if (!(red
== m_currentRed
&& green
== m_currentGreen
&& blue
== m_currentBlue
))
903 long redPS
= (long) (((int) red
) / 255.0);
904 long bluePS
= (long) (((int) blue
) / 255.0);
905 long greenPS
= (long) (((int) green
) / 255.0);
906 *m_pstream
<< redPS
<< " " << greenPS
<< " " << bluePS
<< " setrgbcolor\n";
908 m_currentBlue
= blue
;
909 m_currentGreen
= green
;
913 void wxPostScriptDC::DrawText( const wxString
& text
, long x
, long y
, bool WXUNUSED(use16bit
) )
915 wxCHECK_RET( m_ok
&& m_pstream
, "invalid postscript dc" );
919 if (m_textForegroundColour
.Ok ())
921 unsigned char red
= m_textForegroundColour
.Red ();
922 unsigned char blue
= m_textForegroundColour
.Blue ();
923 unsigned char green
= m_textForegroundColour
.Green ();
927 // Anything not white is black
928 if (!(red
== (unsigned char) 255 && blue
== (unsigned char) 255
929 && green
== (unsigned char) 255))
931 red
= (unsigned char) 0;
932 green
= (unsigned char) 0;
933 blue
= (unsigned char) 0;
937 // maybe setgray here ?
939 if (!(red
== m_currentRed
&& green
== m_currentGreen
&& blue
== m_currentBlue
))
941 long redPS
= (long) (((int) red
) / 255.0);
942 long bluePS
= (long) (((int) blue
) / 255.0);
943 long greenPS
= (long) (((int) green
) / 255.0);
944 *m_pstream
<< redPS
<< " " << greenPS
<< " " << bluePS
<< " setrgbcolor\n";
947 m_currentBlue
= blue
;
948 m_currentGreen
= green
;
952 int size
= m_font
.GetPointSize();
954 long by
= y
+ (long)floor( float(size
) * 2.0 / 3.0 ); // approximate baseline
955 *m_pstream
<< XLOG2DEV(x
) << " " << YLOG2DEV(by
) << " moveto\n";
958 int len
= strlen ((char *)(const char *)text
);
960 for (i
= 0; i
< len
; i
++)
962 int c
= (unsigned char) text
[i
];
963 if ( c
== ')' || c
== '(' || c
== '\\')
965 *m_pstream
<< "\\" << (char) c
;
969 // Cope with character codes > 127
971 sprintf(tmp
, "\\%o", c
);
975 *m_pstream
<< (char) c
;
978 *m_pstream
<< ")" << " show\n";
980 if (m_font
.GetUnderlined())
982 long uy
= (long)(y
+ size
- m_underlinePosition
);
984 GetTextExtent(text
, &w
, &h
);
986 *m_pstream
<< "gsave " << XLOG2DEV(x
) << " " << YLOG2DEV(uy
)
988 << (long)m_underlineThickness
<< " setlinewidth "
989 << XLOG2DEV(x
+ w
) << " " << YLOG2DEV(uy
)
990 << " lineto stroke grestore\n";
993 CalcBoundingBox( x
, y
);
994 CalcBoundingBox( x
+ size
* text
.Length() * 2/3 , y
);
998 void wxPostScriptDC::SetBackground (const wxBrush
& brush
)
1000 m_backgroundBrush
= brush
;
1003 void wxPostScriptDC::SetLogicalFunction (int WXUNUSED(function
))
1005 wxFAIL_MSG( "wxPostScriptDC::SetLogicalFunction not implemented." );
1008 void wxPostScriptDC::DrawSpline( wxList
*points
)
1010 wxCHECK_RET( m_ok
&& m_pstream
, "invalid postscript dc" );
1014 double a
, b
, c
, d
, x1
, y1
, x2
, y2
, x3
, y3
;
1017 wxNode
*node
= points
->First();
1018 p
= (wxPoint
*)node
->Data();
1022 node
= node
->Next();
1023 p
= (wxPoint
*)node
->Data();
1026 x3
= a
= (double)(x1
+ c
) / 2;
1027 y3
= b
= (double)(y1
+ d
) / 2;
1029 *m_pstream
<< "newpath "
1030 << XLOG2DEV((long)x1
) << " " << YLOG2DEV((long)y1
) << " moveto "
1031 << XLOG2DEV((long)x3
) << " " << YLOG2DEV((long)y3
) << " lineto\n";
1033 CalcBoundingBox( (long)x1
, (long)y1
);
1034 CalcBoundingBox( (long)x3
, (long)y3
);
1036 while ((node
= node
->Next()) != NULL
)
1038 q
= (wxPoint
*)node
->Data();
1046 x3
= (double)(x2
+ c
) / 2;
1047 y3
= (double)(y2
+ d
) / 2;
1048 *m_pstream
<< XLOG2DEV((long)x1
) << " " << YLOG2DEV((long)y1
) << " "
1049 << XLOG2DEV((long)x2
) << " " << YLOG2DEV((long)y2
) << " "
1050 << XLOG2DEV((long)x3
) << " " << YLOG2DEV((long)y3
) << " DrawSplineSection\n";
1052 CalcBoundingBox( (long)x1
, (long)y1
);
1053 CalcBoundingBox( (long)x3
, (long)y3
);
1057 At this point, (x2,y2) and (c,d) are the position of the
1058 next-to-last and last point respectively, in the point list
1061 *m_pstream
<< XLOG2DEV((long)c
) << " " << YLOG2DEV((long)d
) << " lineto stroke\n";
1064 long wxPostScriptDC::GetCharWidth ()
1066 // Chris Breeze: reasonable approximation using wxMODERN/Courier
1067 return (long) (GetCharHeight() * 72.0 / 120.0);
1071 void wxPostScriptDC::SetAxisOrientation( bool xLeftRight
, bool yBottomUp
)
1073 wxCHECK_RET( m_ok
&& m_pstream
, "invalid postscript dc" );
1075 m_signX
= (xLeftRight
? 1 : -1);
1076 m_signY
= (yBottomUp
? 1 : -1);
1078 ComputeScaleAndOrigin();
1081 void wxPostScriptDC::SetDeviceOrigin( long x
, long y
)
1083 wxCHECK_RET( m_ok
&& m_pstream
, "invalid postscript dc" );
1089 wxDC::SetDeviceOrigin( x
, h
-y
);
1092 void wxPostScriptDC::GetSize(int* width
, int* height
) const
1094 const char *paperType
= wxThePrintSetupData
->GetPaperName();
1096 if (!paperType
) paperType
= _("A4 210 x 297 mm");
1098 wxPrintPaperType
*paper
= wxThePrintPaperDatabase
->FindPaperType(paperType
);
1100 if (!paper
) paper
= wxThePrintPaperDatabase
->FindPaperType(_("A4 210 x 297 mm"));
1104 if (width
) *width
= paper
->widthPixels
;
1105 if (height
) *height
= paper
->heightPixels
;
1109 if (width
) *width
= 595;
1110 if (height
) *height
= 842;
1114 bool wxPostScriptDC::StartDoc (const wxString
& message
)
1116 wxCHECK_MSG( m_ok
, FALSE
, "invalid postscript dc" );
1118 if (m_filename
== "")
1120 m_filename
= wxGetTempFileName("ps");
1121 wxThePrintSetupData
->SetPrinterFile((char *)(const char *)m_filename
);
1126 wxThePrintSetupData
->SetPrinterFile((char *)(const char *)m_filename
);
1129 m_pstream
= new ofstream (wxThePrintSetupData
->GetPrinterFile());
1131 if (!m_pstream
|| !m_pstream
->good())
1133 wxMessageBox (_("Cannot open file!"), _("Error"), wxOK
);
1140 SetBrush( *wxBLACK_BRUSH
);
1141 SetPen( *wxBLACK_PEN
);
1142 SetBackground( *wxWHITE_BRUSH
);
1143 SetTextForeground( *wxBLACK
);
1145 // set origin according to paper size
1146 SetDeviceOrigin( 0,0 );
1154 void wxPostScriptDC::EndDoc ()
1156 wxCHECK_RET( m_ok
&& m_pstream
, "invalid postscript dc" );
1161 *m_pstream
<< "grestore\n";
1167 m_pstream
= (ofstream
*) NULL
;
1170 char *header_file
= wxGetTempFileName("ps");
1172 m_pstream
= new ofstream( header_file
);
1174 *m_pstream
<< "%!PS-Adobe-2.0\n"; /* PostScript magic strings */
1175 *m_pstream
<< "%%Title: " << (const char *) m_title
<< "\n";
1176 *m_pstream
<< "%%Creator: " << wxTheApp
->argv
[0] << "\n";
1177 *m_pstream
<< "%%CreationDate: " << wxNow() << "\n";
1180 if ( wxGetEmailAddress(userID
, sizeof(userID
)) )
1182 *m_pstream
<< "%%For: " << (char *)userID
;
1184 if (wxGetUserName(userName
, sizeof(userName
)))
1185 *m_pstream
<< " (" << (char *)userName
<< ")";
1188 else if ( wxGetUserName(userID
, sizeof(userID
)) )
1190 *m_pstream
<< "%%For: " << (char *)userID
<< "\n";
1193 // THE FOLLOWING HAS BEEN CONTRIBUTED BY Andy Fyfe <andy@hyperparallel.com>
1195 long wx_printer_translate_x
, wx_printer_translate_y
;
1196 double wx_printer_scale_x
, wx_printer_scale_y
;
1197 wxThePrintSetupData
->GetPrinterTranslation(&wx_printer_translate_x
, &wx_printer_translate_y
);
1198 wxThePrintSetupData
->GetPrinterScaling(&wx_printer_scale_x
, &wx_printer_scale_y
);
1200 if (wxThePrintSetupData
->GetPrinterOrientation() == PS_LANDSCAPE
)
1202 *m_pstream
<< "%%Orientation: Landscape\n";
1206 *m_pstream
<< "%%Orientation: Portrait\n";
1209 // Compute the bounding box. Note that it is in the default user
1210 // coordinate system, thus we have to convert the values.
1211 long llx
= (long) ((XLOG2DEV(m_minX
)+wx_printer_translate_x
)*wx_printer_scale_x
);
1212 long lly
= (long) ((YLOG2DEV(m_minY
)+wx_printer_translate_y
)*wx_printer_scale_y
);
1213 long urx
= (long) ((XLOG2DEV(m_maxX
)+wx_printer_translate_x
)*wx_printer_scale_x
);
1214 long ury
= (long) ((YLOG2DEV(m_maxY
)+wx_printer_translate_y
)*wx_printer_scale_y
);
1216 // If we're landscape, our sense of "x" and "y" is reversed.
1217 if (wxThePrintSetupData
->GetPrinterOrientation() == PS_LANDSCAPE
)
1220 tmp
= llx
; llx
= lly
; lly
= tmp
;
1221 tmp
= urx
; urx
= ury
; ury
= tmp
;
1223 // We need either the two lines that follow, or we need to subtract
1224 // min_x from real_translate_y, which is commented out below.
1225 llx
= llx
- (long)(m_minX
*wx_printer_scale_y
);
1226 urx
= urx
- (long)(m_minX
*wx_printer_scale_y
);
1229 // The Adobe specifications call for integers; we round as to make
1230 // the bounding larger.
1231 *m_pstream
<< "%%BoundingBox: "
1232 << floor((double)llx
) << " " << floor((double)lly
) << " "
1233 << ceil((double)urx
) << " " << ceil((double)ury
) << "\n";
1234 *m_pstream
<< "%%Pages: " << (wxPageNumber
- 1) << "\n";
1235 *m_pstream
<< "%%EndComments\n\n";
1237 // To check the correctness of the bounding box, postscript commands
1238 // to draw a box corresponding to the bounding box are generated below.
1239 // But since we typically don't want to print such a box, the postscript
1240 // commands are generated within comments. These lines appear before any
1241 // adjustment of scale, rotation, or translation, and hence are in the
1242 // default user coordinates.
1243 *m_pstream
<< "% newpath\n";
1244 *m_pstream
<< "% " << llx
<< " " << lly
<< " moveto\n";
1245 *m_pstream
<< "% " << urx
<< " " << lly
<< " lineto\n";
1246 *m_pstream
<< "% " << urx
<< " " << ury
<< " lineto\n";
1247 *m_pstream
<< "% " << llx
<< " " << ury
<< " lineto closepath stroke\n";
1249 *m_pstream
<< "%%BeginProlog\n";
1250 *m_pstream
<< wxPostScriptHeaderEllipse
;
1251 *m_pstream
<< wxPostScriptHeaderEllipticArc
;
1252 *m_pstream
<< wxPostScriptHeaderColourImage
;
1253 *m_pstream
<< wxPostScriptHeaderReencodeISO1
;
1254 *m_pstream
<< wxPostScriptHeaderReencodeISO2
;
1256 if (wxPostScriptHeaderSpline
)
1257 *m_pstream
<< wxPostScriptHeaderSpline
;
1258 *m_pstream
<< "%%EndProlog\n";
1261 m_pstream
= (ofstream
*) NULL
;
1263 char *tmp_file
= wxGetTempFileName("ps");
1265 // Paste header Before wx_printer_file
1266 wxConcatFiles (header_file
, wxThePrintSetupData
->GetPrinterFile(), tmp_file
);
1267 wxRemoveFile (header_file
);
1268 wxRemoveFile (wxThePrintSetupData
->GetPrinterFile());
1269 wxRenameFile(tmp_file
, wxThePrintSetupData
->GetPrinterFile());
1271 #if defined(__X__) || defined(__WXGTK__)
1274 switch (wxThePrintSetupData
->GetPrinterMode()) {
1278 argv
[0] = wxThePrintSetupData
->GetPrintPreviewCommand();
1279 argv
[1] = wxThePrintSetupData
->GetPrinterFile();
1280 argv
[2] = (char *) NULL
;
1281 wxExecute (argv
, TRUE
);
1282 wxRemoveFile(wxThePrintSetupData
->GetPrinterFile());
1290 argv
[argc
++] = wxThePrintSetupData
->GetPrinterCommand();
1292 // !SM! If we simply assign to argv[1] here, if printer options
1293 // are blank, we get an annoying and confusing message from lpr.
1294 char * opts
= wxThePrintSetupData
->GetPrinterOptions();
1296 argv
[argc
++] = opts
;
1298 argv
[argc
++] = wxThePrintSetupData
->GetPrinterFile();
1299 argv
[argc
++] = (char *) NULL
;
1300 wxExecute (argv
, TRUE
);
1301 wxRemoveFile(wxThePrintSetupData
->GetPrinterFile());
1312 void wxPostScriptDC::StartPage ()
1314 wxCHECK_RET( m_ok
&& m_pstream
, "invalid postscript dc" );
1316 *m_pstream
<< "%%Page: " << (wxPageNumber
++) << "\n";
1318 // *m_pstream << "matrix currentmatrix\n";
1320 // Added by Chris Breeze
1322 // Each page starts with an "initgraphics" which resets the
1323 // transformation and so we need to reset the origin
1324 // (and rotate the page for landscape printing)
1327 m_scaleFactor = 1.0;
1328 m_logicalOriginX = 0;
1329 m_logicalOriginY = 0;
1333 long translate_x
, translate_y
;
1334 double scale_x
, scale_y
;
1335 wxThePrintSetupData
->GetPrinterTranslation(&translate_x
, &translate_y
);
1336 wxThePrintSetupData
->GetPrinterScaling(&scale_x
, &scale_y
);
1338 if (wxThePrintSetupData
->GetPrinterOrientation() == PS_LANDSCAPE
)
1340 translate_y
-= m_maxY
;
1341 *m_pstream
<< "90 rotate\n";
1344 *m_pstream
<< scale_x
<< " " << scale_y
<< " scale\n";
1345 *m_pstream
<< translate_x
<< " " << translate_y
<< " translate\n";
1348 void wxPostScriptDC::EndPage ()
1350 wxCHECK_RET( m_ok
&& m_pstream
, "invalid postscript dc" );
1352 *m_pstream
<< "showpage\n";
1355 bool wxPostScriptDC::Blit( long WXUNUSED(xdest
), long WXUNUSED(ydest
),
1356 long WXUNUSED(fwidth
), long WXUNUSED(fheight
),
1357 wxDC
*WXUNUSED(source
),
1358 long WXUNUSED(xsrc
), long WXUNUSED(ysrc
),
1359 int WXUNUSED(rop
), bool WXUNUSED(useMask
) )
1361 wxCHECK_MSG( m_ok
&& m_pstream
, FALSE
, "invalid postscript dc" );
1363 wxFAIL_MSG( "wxPostScriptDC::Blit no yet implemented." );
1368 long wxPostScriptDC::GetCharHeight ()
1371 return m_font
.GetPointSize();
1376 void wxPostScriptDC::GetTextExtent (const wxString
& string
, long *x
, long *y
,
1377 long *descent
, long *externalLeading
, wxFont
*theFont
,
1378 bool WXUNUSED(use16
))
1380 // if (!m_pstream) return;
1382 wxFont
*fontToUse
= theFont
;
1384 if (!fontToUse
) fontToUse
= (wxFont
*) &m_font
;
1386 #if !USE_AFM_FOR_POSTSCRIPT
1387 // Provide a VERY rough estimate (avoid using it)
1388 // Chris Breeze 5/11/97: produces accurate results for mono-spaced
1389 // font such as Courier (aka wxMODERN)
1393 height
= fontToUse
->GetPointSize();
1395 *x
= strlen (string
) * height
* 72 / 120;
1396 *y
= (long) (height
* 1.32); // allow for descender
1400 if (externalLeading
)
1401 *externalLeading
= 0;
1403 // +++++ start of contributed code +++++
1405 // ************************************************************
1406 // method for calculating string widths in postscript:
1407 // read in the AFM (adobe font metrics) file for the
1408 // actual font, parse it and extract the character widths
1409 // and also the descender. this may be improved, but for now
1410 // it works well. the AFM file is only read in if the
1411 // font is changed. this may be chached in the future.
1412 // calls to GetTextExtent with the font unchanged are rather
1415 // for each font and style used there is an AFM file necessary.
1416 // currently i have only files for the roman font family.
1417 // i try to get files for the other ones!
1419 // CAVE: the size of the string is currently always calculated
1420 // in 'points' (1/72 of an inch). this should later on be
1421 // changed to depend on the mapping mode.
1422 // CAVE: the path to the AFM files must be set before calling this
1423 // function. this is usually done by a call like the following:
1424 // wxSetAFMPath("d:\\wxw161\\afm\\");
1428 // wxPostScriptDC dc(NULL, TRUE);
1430 // wxSetAFMPath("d:\\wxw161\\afm\\");
1431 // dc.StartDoc("Test");
1434 // dc.SetFont(new wxFont(10, wxROMAN, wxNORMAL, wxNORMAL));
1435 // dc.GetTextExtent("Hallo",&w,&h);
1440 // by steve (stefan.hammes@urz.uni-heidelberg.de)
1441 // created: 10.09.94
1442 // updated: 14.05.95
1444 assert(fontToUse
&& "void wxPostScriptDC::GetTextExtent: no font defined");
1445 assert(x
&& "void wxPostScriptDC::GetTextExtent: x == NULL");
1446 assert(y
&& "void wxPostScriptDC::GetTextExtent: y == NULL");
1448 // these static vars are for storing the state between calls
1449 static int lastFamily
= INT_MIN
;
1450 static int lastSize
= INT_MIN
;
1451 static int lastStyle
= INT_MIN
;
1452 static int lastWeight
= INT_MIN
;
1453 static int lastDescender
= INT_MIN
;
1454 static int lastWidths
[256]; // widths of the characters
1456 // get actual parameters
1457 const int Family
= fontToUse
->GetFamily();
1458 const int Size
= fontToUse
->GetPointSize();
1459 const int Style
= fontToUse
->GetStyle();
1460 const int Weight
= fontToUse
->GetWeight();
1462 // if we have another font, read the font-metrics
1463 if(Family
!=lastFamily
||Size
!=lastSize
||Style
!=lastStyle
||Weight
!=lastWeight
){
1464 // store actual values
1465 lastFamily
= Family
;
1468 lastWeight
= Weight
;
1470 // read in new font metrics **************************************
1472 // 1. construct filename ******************************************
1473 /* MATTHEW: [2] Use wxTheFontNameDirectory */
1476 // Julian - we'll need to do this a different way now we've removed the
1477 // font directory system. Must find Stefan's original code.
1479 name
= wxTheFontNameDirectory
->GetAFMName(Family
, Weight
, Style
);
1483 // get the directory of the AFM files
1487 strcpy(afmName
,wxGetAFMPath());
1489 // 2. open and process the file **********************************
1491 // a short explanation of the AFM format:
1492 // we have for each character a line, which gives its size
1495 // C 63 ; WX 444 ; N question ; B 49 -14 395 676 ;
1497 // that means, we have a character with ascii code 63, and width
1498 // (444/1000 * fontSize) points.
1499 // the other data is ignored for now!
1501 // when the font has changed, we read in the right AFM file and store the
1502 // character widths in an array, which is processed below (see point 3.).
1504 // new elements JC Sun Aug 25 23:21:44 MET DST 1996
1507 strcat(afmName
,name
);
1508 strcat(afmName
,".afm");
1509 FILE *afmFile
= fopen(afmName
,"r");
1511 wxLogDebug("GetTextExtent: can't open AFM file '%s'\n",afmName
);
1512 wxLogDebug(" using approximate values\n");
1514 for (i
=0; i
<256; i
++) lastWidths
[i
] = 500; // an approximate value
1515 lastDescender
= -150; // dito.
1518 // init the widths array
1519 for(i
=0; i
<256; i
++) lastWidths
[i
]= INT_MIN
;
1520 // some variables for holding parts of a line
1521 char cString
[10],semiString
[10],WXString
[10],descString
[20];
1522 char upString
[30], utString
[30], encString
[50];
1525 // read in the file and parse it
1526 while(fgets(line
,sizeof(line
),afmFile
)!=NULL
){
1527 // A.) check for descender definition
1528 if(strncmp(line
,"Descender",9)==0){
1529 if((sscanf(line
,"%s%d",descString
,&lastDescender
)!=2)
1530 || (strcmp(descString
,"Descender")!=0)) {
1531 wxLogDebug("AFM-file '%s': line '%s' has error (bad descender)\n",
1535 // JC 1.) check for UnderlinePosition
1536 else if(strncmp(line
,"UnderlinePosition",17)==0){
1537 if((sscanf(line
,"%s%lf",upString
,&UnderlinePosition
)!=2)
1538 || (strcmp(upString
,"UnderlinePosition")!=0)) {
1539 wxLogDebug("AFM-file '%s': line '%s' has error (bad UnderlinePosition)\n",
1543 // JC 2.) check for UnderlineThickness
1544 else if(strncmp(line
,"UnderlineThickness",18)==0){
1545 if((sscanf(line
,"%s%lf",utString
,&UnderlineThickness
)!=2)
1546 || (strcmp(utString
,"UnderlineThickness")!=0)) {
1547 wxLogDebug("AFM-file '%s': line '%s' has error (bad UnderlineThickness)\n",
1551 // JC 3.) check for EncodingScheme
1552 else if(strncmp(line
,"EncodingScheme",14)==0){
1553 if((sscanf(line
,"%s%s",utString
,encString
)!=2)
1554 || (strcmp(utString
,"EncodingScheme")!=0)) {
1555 wxLogDebug("AFM-file '%s': line '%s' has error (bad EncodingScheme)\n",
1558 else if (strncmp(encString
, "AdobeStandardEncoding", 21))
1560 wxLogDebug("AFM-file '%s': line '%s' has error (unsupported EncodingScheme %s)\n",
1561 afmName
,line
, encString
);
1564 // B.) check for char-width
1565 else if(strncmp(line
,"C ",2)==0){
1566 if(sscanf(line
,"%s%d%s%s%d",
1567 cString
,&ascii
,semiString
,WXString
,&cWidth
)!=5){
1568 wxLogDebug("AFM-file '%s': line '%s' has an error (bad character width)\n",afmName
,line
);
1570 if(strcmp(cString
,"C")!=0 || strcmp(semiString
,";")!=0 ||
1571 strcmp(WXString
,"WX")!=0){
1572 wxLogDebug("AFM-file '%s': line '%s' has a format error\n",afmName
,line
);
1574 //printf(" char '%c'=%d has width '%d'\n",ascii,ascii,cWidth);
1575 if(ascii
>=0 && ascii
<256){
1576 lastWidths
[ascii
] = cWidth
; // store width
1578 /* MATTHEW: this happens a lot; don't print an error */
1579 // wxLogDebug("AFM-file '%s': ASCII value %d out of range\n",afmName,ascii);
1582 // C.) ignore other entries.
1586 // hack to compute correct values for german 'Umlaute'
1587 // the correct way would be to map the character names
1588 // like 'adieresis' to corresp. positions of ISOEnc and read
1589 // these values from AFM files, too. Maybe later ...
1590 lastWidths
[196] = lastWidths
['A']; // Ä
1591 lastWidths
[228] = lastWidths
['a']; // ä
1592 lastWidths
[214] = lastWidths
['O']; // Ö
1593 lastWidths
[246] = lastWidths
['o']; // ö
1594 lastWidths
[220] = lastWidths
['U']; // Ü
1595 lastWidths
[252] = lastWidths
['u']; // ü
1596 lastWidths
[223] = lastWidths
[251]; // ß
1599 // JC: calculate UnderlineThickness/UnderlinePosition
1600 m_underlinePosition
= m_underlinePosition
* fontToUse
->GetPointSize() / 1000.0f
;
1601 m_underlineThickness
= m_underlineThickness
* fontToUse
->GetPointSize() / 1000.0f
* m_scaleFactor
;
1603 // 3. now the font metrics are read in, calc size *******************
1604 // this is done by adding the widths of the characters in the
1605 // string. they are given in 1/1000 of the size!
1608 long height
=Size
; // by default
1610 for(p
=(unsigned char *)(const char *)string
; *p
; p
++){
1611 if(lastWidths
[*p
]== INT_MIN
){
1612 wxLogDebug("GetTextExtent: undefined width for character '%c' (%d)\n",
1614 widthSum
+= (long)(lastWidths
[' ']/1000.0F
* Size
); // assume space
1616 widthSum
+= (long)((lastWidths
[*p
]/1000.0F
)*Size
);
1619 // add descender to height (it is usually a negative value)
1620 if(lastDescender
!=INT_MIN
){
1621 height
+= (long)(((-lastDescender
)/1000.0F
) * Size
); /* MATTHEW: forgot scale */
1624 // return size values
1628 // return other parameters
1630 if(lastDescender
!=INT_MIN
){
1631 *descent
= (long)(((-lastDescender
)/1000.0F
) * Size
); /* MATTHEW: forgot scale */
1637 // currently no idea how to calculate this!
1638 // if (externalLeading) *externalLeading = 0;
1639 if (externalLeading
)
1640 *externalLeading
= 0;
1642 // ----- end of contributed code -----
1646 void wxPostScriptDC::GetSizeMM(long *width
, long *height
) const
1648 const char *paperType
= wxThePrintSetupData
->GetPaperName();
1650 if (!paperType
) paperType
= _("A4 210 x 297 mm");
1652 wxPrintPaperType
*paper
= wxThePrintPaperDatabase
->FindPaperType(paperType
);
1654 if (!paper
) paper
= wxThePrintPaperDatabase
->FindPaperType(_("A4 210 x 297 mm"));
1658 if (width
) *width
= paper
->widthMM
;
1659 if (height
) *height
= paper
->heightMM
;
1663 if (width
) *width
= 210;
1664 if (height
) *height
= 297;