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"
26 //-----------------------------------------------------------------------------
27 // start and end of document/page
28 //-----------------------------------------------------------------------------
30 static const char *wxPostScriptHeaderEllipse
= "\
31 /ellipsedict 8 dict def\n\
32 ellipsedict /mtrx matrix put\n\
36 /startangle exch def\n\
41 /savematrix mtrx currentmatrix def\n\
44 0 0 1 startangle endangle arc\n\
45 savematrix setmatrix\n\
50 static const char *wxPostScriptHeaderEllipticArc
= "\
51 /ellipticarcdict 8 dict def\n\
52 ellipticarcdict /mtrx matrix put\n\
54 { ellipticarcdict begin\n\
57 /startangle exch def\n\
62 /savematrix mtrx currentmatrix def\n\
65 do_fill { 0 0 moveto } if\n\
66 0 0 1 startangle endangle arc\n\
67 savematrix setmatrix\n\
68 do_fill { fill }{ stroke } ifelse\n\
72 static const char *wxPostScriptHeaderSpline
= "\
73 /DrawSplineSection {\n\
80 /xa x1 x2 x1 sub 0.666667 mul add def\n\
81 /ya y1 y2 y1 sub 0.666667 mul add def\n\
82 /xb x3 x2 x3 sub 0.666667 mul add def\n\
83 /yb y3 y2 y3 sub 0.666667 mul add def\n\
85 xa ya xb yb x3 y3 curveto\n\
89 static const char *wxPostScriptHeaderColourImage
= "\
90 % define 'colorimage' if it isn't defined\n\
91 % ('colortogray' and 'mergeprocs' come from xwd2ps\n\
93 /colorimage where % do we know about 'colorimage'?\n\
94 { pop } % yes: pop off the 'dict' returned\n\
96 /colortogray { % define an RGB->I function\n\
97 /rgbdata exch store % call input 'rgbdata'\n\
98 rgbdata length 3 idiv\n\
101 0 1 npixls 1 sub {\n\
103 rgbdata rgbindx get 20 mul % Red\n\
104 rgbdata rgbindx 1 add get 32 mul % Green\n\
105 rgbdata rgbindx 2 add get 12 mul % Blue\n\
106 add add 64 idiv % I = .5G + .31R + .18B\n\
108 /rgbindx rgbindx 3 add store\n\
110 grays 0 npixls getinterval\n\
113 % Utility procedure for colorimage operator.\n\
114 % This procedure takes two procedures off the\n\
115 % stack and merges them into a single procedure.\n\
117 /mergeprocs { % def\n\
136 /colorimage { % def\n\
137 pop pop % remove 'false 3' operands\n\
138 {colortogray} mergeprocs\n\
141 } ifelse % end of 'false' case\n\
144 static char wxPostScriptHeaderReencodeISO1
[] =
146 "dup dup findfont dup length dict begin\n"
147 "{ 1 index /FID ne { def }{ pop pop } ifelse } forall\n"
148 "/Encoding ISOLatin1Encoding def\n"
149 "currentdict end definefont\n"
151 "/ISOLatin1Encoding [\n"
152 "/.notdef/.notdef/.notdef/.notdef/.notdef/.notdef/.notdef/.notdef\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 "/space/exclam/quotedbl/numbersign/dollar/percent/ampersand/quoteright\n"
157 "/parenleft/parenright/asterisk/plus/comma/minus/period/slash\n"
158 "/zero/one/two/three/four/five/six/seven/eight/nine/colon/semicolon\n"
159 "/less/equal/greater/question/at/A/B/C/D/E/F/G/H/I/J/K/L/M/N\n"
160 "/O/P/Q/R/S/T/U/V/W/X/Y/Z/bracketleft/backslash/bracketright\n"
161 "/asciicircum/underscore/quoteleft/a/b/c/d/e/f/g/h/i/j/k/l/m\n"
162 "/n/o/p/q/r/s/t/u/v/w/x/y/z/braceleft/bar/braceright/asciitilde\n"
163 "/.notdef/.notdef/.notdef/.notdef/.notdef/.notdef/.notdef/.notdef\n"
164 "/.notdef/.notdef/.notdef/.notdef/.notdef/.notdef/.notdef/.notdef\n"
165 "/.notdef/dotlessi/grave/acute/circumflex/tilde/macron/breve\n"
166 "/dotaccent/dieresis/.notdef/ring/cedilla/.notdef/hungarumlaut\n";
168 static char wxPostScriptHeaderReencodeISO2
[] =
169 "/ogonek/caron/space/exclamdown/cent/sterling/currency/yen/brokenbar\n"
170 "/section/dieresis/copyright/ordfeminine/guillemotleft/logicalnot\n"
171 "/hyphen/registered/macron/degree/plusminus/twosuperior/threesuperior\n"
172 "/acute/mu/paragraph/periodcentered/cedilla/onesuperior/ordmasculine\n"
173 "/guillemotright/onequarter/onehalf/threequarters/questiondown\n"
174 "/Agrave/Aacute/Acircumflex/Atilde/Adieresis/Aring/AE/Ccedilla\n"
175 "/Egrave/Eacute/Ecircumflex/Edieresis/Igrave/Iacute/Icircumflex\n"
176 "/Idieresis/Eth/Ntilde/Ograve/Oacute/Ocircumflex/Otilde/Odieresis\n"
177 "/multiply/Oslash/Ugrave/Uacute/Ucircumflex/Udieresis/Yacute\n"
178 "/Thorn/germandbls/agrave/aacute/acircumflex/atilde/adieresis\n"
179 "/aring/ae/ccedilla/egrave/eacute/ecircumflex/edieresis/igrave\n"
180 "/iacute/icircumflex/idieresis/eth/ntilde/ograve/oacute/ocircumflex\n"
181 "/otilde/odieresis/divide/oslash/ugrave/uacute/ucircumflex/udieresis\n"
182 "/yacute/thorn/ydieresis\n"
185 //-------------------------------------------------------------------------------
187 //-------------------------------------------------------------------------------
189 wxPostScriptDC::wxPostScriptDC ()
191 m_pstream
= (ofstream
*) NULL
;
201 m_underlinePosition
= 0.0;
202 m_underlineThickness
= 0.0;
204 m_signX
= 1; // default x-axis left to right
205 m_signY
= -1; // default y-axis bottom up -> top down
208 wxPostScriptDC::wxPostScriptDC (const wxString
& file
, bool interactive
, wxWindow
*parent
)
210 m_pstream
= (ofstream
*) NULL
;
220 m_underlinePosition
= 0.0;
221 m_underlineThickness
= 0.0;
223 m_signX
= 1; // default x-axis left to right
224 m_signY
= -1; // default y-axis bottom up -> top down
226 Create(file
, interactive
, parent
);
229 bool wxPostScriptDC::Create(const wxString
& file
, bool interactive
, wxWindow
*parent
)
231 m_isInteractive
= interactive
;
237 // Can only send to file in Windows
238 wxThePrintSetupData
->SetPrinterMode(PS_FILE
);
243 if ((m_ok
= PrinterDialog (parent
) ) == FALSE
) return FALSE
;
253 wxPostScriptDC::~wxPostScriptDC ()
255 if (m_pstream
) delete m_pstream
;
258 bool wxPostScriptDC::Ok() const
263 bool wxPostScriptDC::PrinterDialog(wxWindow
*parent
)
265 wxPostScriptPrintDialog
dialog( parent
, _("Printer Settings"), wxPoint(150, 150), wxSize(400, 400),
266 wxDEFAULT_DIALOG_STYLE
| wxDIALOG_MODAL
);
267 m_ok
= (dialog
.ShowModal () == wxID_OK
);
269 if (!m_ok
) return FALSE
;
271 if ((m_filename
== "") &&
272 (wxThePrintSetupData
->GetPrinterMode() == PS_PREVIEW
||
273 wxThePrintSetupData
->GetPrinterMode() == PS_PRINTER
))
277 wxThePrintSetupData
->SetPrinterFile("preview");
279 // For PS_PRINTER action this depends on a Unix-style print spooler
280 // since the wx_printer_file can be destroyed during a session
281 // @@@ TODO: a Windows-style answer for non-Unix
283 wxGetUserId (userId
, sizeof (userId
) / sizeof (char));
285 strcpy (tmp
, "/tmp/preview_");
286 strcat (tmp
, userId
);
287 wxThePrintSetupData
->SetPrinterFile(tmp
);
290 strcpy(tmp2
, wxThePrintSetupData
->GetPrinterFile());
291 strcat (tmp2
, ".ps");
292 wxThePrintSetupData
->SetPrinterFile(tmp2
);
295 else if ((m_filename
== "") && (wxThePrintSetupData
->GetPrinterMode() == PS_FILE
))
297 char *file
= wxSaveFileSelector (_("PostScript"), "ps");
303 wxThePrintSetupData
->SetPrinterFile(file
);
311 void wxPostScriptDC::SetClippingRegion (long x
, long y
, long w
, long h
)
313 wxCHECK_RET( m_ok
&& m_pstream
, "invalid postscript dc" );
315 if (m_clipping
) return;
317 wxDC::SetClippingRegion( x
, y
, w
, h
);
320 *m_pstream
<< "gsave\n"
322 << XLOG2DEV(x
) << " " << YLOG2DEV(y
) << " moveto\n"
323 << XLOG2DEV(x
+w
) << " " << YLOG2DEV(y
) << " lineto\n"
324 << XLOG2DEV(x
+w
) << " " << YLOG2DEV(y
+h
) << " lineto\n"
325 << XLOG2DEV(x
) << " " << YLOG2DEV(y
+h
) << " lineto\n"
326 << "closepath clip newpath\n";
329 void wxPostScriptDC::SetClippingRegion( const wxRegion
&WXUNUSED(region
) )
333 void wxPostScriptDC::DestroyClippingRegion()
335 wxCHECK_RET( m_ok
&& m_pstream
, "invalid postscript dc" );
337 wxDC::DestroyClippingRegion();
342 *m_pstream
<< "grestore\n";
346 void wxPostScriptDC::Clear()
348 wxFAIL_MSG( "wxPostScriptDC::Clear not implemented." );
351 void wxPostScriptDC::FloodFill (long WXUNUSED(x
), long WXUNUSED(y
), const wxColour
&WXUNUSED(col
), int WXUNUSED(style
))
353 wxFAIL_MSG( "wxPostScriptDC::FloodFill not implemented." );
356 bool wxPostScriptDC::GetPixel (long WXUNUSED(x
), long WXUNUSED(y
), wxColour
* WXUNUSED(col
)) const
358 wxFAIL_MSG( "wxPostScriptDC::GetPixel not implemented." );
362 void wxPostScriptDC::CrossHair (long WXUNUSED(x
), long WXUNUSED(y
))
364 wxFAIL_MSG( "wxPostScriptDC::CrossHair not implemented." );
367 void wxPostScriptDC::DrawLine (long x1
, long y1
, long x2
, long y2
)
369 wxCHECK_RET( m_ok
&& m_pstream
, "invalid postscript dc" );
371 if (m_pen
.GetStyle() == wxTRANSPARENT
) return;
375 *m_pstream
<< "newpath\n"
376 << XLOG2DEV(x1
) << " " << YLOG2DEV (y1
) << " moveto\n"
377 << XLOG2DEV(x2
) << " " << YLOG2DEV (y2
) << " lineto\n"
380 CalcBoundingBox( x1
, y1
);
381 CalcBoundingBox( x2
, y2
);
384 #define RAD2DEG 57.29577951308
386 void wxPostScriptDC::DrawArc (long x1
, long y1
, long x2
, long y2
, long xc
, long yc
)
388 wxCHECK_RET( m_ok
&& m_pstream
, "invalid postscript dc" );
392 long radius
= (long) sqrt(dx
*dx
+dy
*dy
);
393 double alpha1
, alpha2
;
395 if (x1
== x2
&& y1
== y2
)
399 } else if (radius
== 0.0)
401 alpha1
= alpha2
= 0.0;
404 alpha1
= (x1
- xc
== 0) ?
405 (y1
- yc
< 0) ? 90.0 : -90.0 :
406 -atan2(double(y1
-yc
), double(x1
-xc
)) * RAD2DEG
;
407 alpha2
= (x2
- xc
== 0) ?
408 (y2
- yc
< 0) ? 90.0 : -90.0 :
409 -atan2(double(y2
-yc
), double(x2
-xc
)) * RAD2DEG
;
411 while (alpha1
<= 0) alpha1
+= 360;
412 while (alpha2
<= 0) alpha2
+= 360; // adjust angles to be between
413 while (alpha1
> 360) alpha1
-= 360; // 0 and 360 degree
414 while (alpha2
> 360) alpha2
-= 360;
416 if (m_brush
.GetStyle() != wxTRANSPARENT
)
419 *m_pstream
<< "newpath\n"
420 << XLOG2DEV(xc
) << " "
421 << YLOG2DEV(yc
) << " "
422 << XLOG2DEVREL(radius
) << " "
423 << YLOG2DEVREL(radius
) << " "
425 << alpha2
<< " ellipse\n"
426 << XLOG2DEV(xc
) << " "
427 << YLOG2DEV(yc
) << " lineto\n"
432 if (m_pen
.GetStyle() != wxTRANSPARENT
)
435 *m_pstream
<< "newpath\n"
436 << XLOG2DEV(xc
) << " "
437 << YLOG2DEV(yc
) << " "
438 << XLOG2DEVREL(radius
) << " "
439 << YLOG2DEVREL(radius
) << " "
441 << alpha2
<< " ellipse\n"
445 CalcBoundingBox( xc
-radius
, yc
-radius
);
446 CalcBoundingBox( xc
+radius
, yc
+radius
);
449 void wxPostScriptDC::DrawEllipticArc(long x
,long y
,long w
,long h
,double sa
,double ea
)
451 wxCHECK_RET( m_ok
&& m_pstream
, "invalid postscript dc" );
453 if (sa
>=360 || sa
<=-360) sa
=sa
-int(sa
/360)*360;
454 if (ea
>=360 || ea
<=-360) ea
=ea
-int(ea
/360)*360;
460 DrawEllipse(x
,y
,w
,h
);
464 if (m_brush
.GetStyle () != wxTRANSPARENT
)
468 *m_pstream
<< "newpath\n"
469 << XLOG2DEV(x
+w
/2) << " " << YLOG2DEV(y
+h
/2) << " "
470 << XLOG2DEVREL(w
/2) << " " << YLOG2DEVREL(h
/2) << " "
471 << int(sa
) <<" "<< int(ea
) << " true ellipticarc\n";
473 CalcBoundingBox( x
,y
);
474 CalcBoundingBox( x
+w
, y
+h
);
477 if (m_pen
.GetStyle () != wxTRANSPARENT
)
481 *m_pstream
<< "newpath\n"
482 << XLOG2DEV(x
+w
/2) << " " << YLOG2DEV(y
+h
/2) << " "
483 << XLOG2DEVREL(w
/2) << " " << XLOG2DEVREL(h
/2) << " "
484 << int(sa
) <<" "<< int(ea
) << " false ellipticarc\n";
486 CalcBoundingBox( x
, y
);
487 CalcBoundingBox( x
+w
, y
+h
);
491 void wxPostScriptDC::DrawPoint (long x
, long y
)
493 wxCHECK_RET( m_ok
&& m_pstream
, "invalid postscript dc" );
495 if (m_pen
.GetStyle() == wxTRANSPARENT
) return;
499 *m_pstream
<< "newpath\n"
500 << XLOG2DEV(x
) << " " << YLOG2DEV (y
) << " moveto\n"
501 << XLOG2DEV(x
+1) << " " << YLOG2DEV (y
) << " lineto\n"
504 CalcBoundingBox( x
, y
);
507 void wxPostScriptDC::DrawPolygon (int n
, wxPoint points
[], long xoffset
, long yoffset
, int WXUNUSED(fillStyle
))
509 wxCHECK_RET( m_ok
&& m_pstream
, "invalid postscript dc" );
513 if (m_brush
.GetStyle () != wxTRANSPARENT
)
517 *m_pstream
<< "newpath\n";
519 long xx
= XLOG2DEV(points
[0].x
+ xoffset
);
520 long yy
= YLOG2DEV(points
[0].y
+ yoffset
);
521 *m_pstream
<< xx
<< " " << yy
<< " moveto\n";
522 CalcBoundingBox( points
[0].x
+ xoffset
, points
[0].y
+ yoffset
);
524 for (int i
= 1; i
< n
; i
++)
526 xx
= XLOG2DEV(points
[i
].x
+ xoffset
);
527 yy
= YLOG2DEV(points
[i
].y
+ yoffset
);
528 *m_pstream
<< xx
<< " " << yy
<< " lineto\n";
529 CalcBoundingBox( points
[i
].x
+ xoffset
, points
[i
].y
+ yoffset
);
531 *m_pstream
<< "fill\n";
534 if (m_pen
.GetStyle () != wxTRANSPARENT
)
538 *m_pstream
<< "newpath\n";
540 long xx
= XLOG2DEV(points
[0].x
+ xoffset
);
541 long yy
= YLOG2DEV(points
[0].y
+ yoffset
);
542 *m_pstream
<< xx
<< " " << yy
<< " moveto\n";
543 CalcBoundingBox( points
[0].x
+ xoffset
, points
[0].y
+ yoffset
);
545 for (int i
= 1; i
< n
; i
++)
547 xx
= XLOG2DEV(points
[i
].x
+ xoffset
);
548 yy
= YLOG2DEV(points
[i
].y
+ yoffset
);
549 *m_pstream
<< xx
<< " " << yy
<< " lineto\n";
550 CalcBoundingBox( points
[i
].x
+ xoffset
, points
[i
].y
+ yoffset
);
553 *m_pstream
<< "stroke\n";
557 void wxPostScriptDC::DrawLines (int n
, wxPoint points
[], long xoffset
, long yoffset
)
559 wxCHECK_RET( m_ok
&& m_pstream
, "invalid postscript dc" );
561 if (m_pen
.GetStyle() == wxTRANSPARENT
) return;
566 for (int i
=0; i
<n
; ++i
)
568 CalcBoundingBox( XLOG2DEV(points
[i
].x
+xoffset
), YLOG2DEV(points
[i
].y
+yoffset
));
571 *m_pstream
<< "newpath\n"
572 << XLOG2DEV(points
[0].x
+xoffset
) << " "
573 << YLOG2DEV(points
[0].y
+yoffset
) << " moveto\n";
575 for (int i
= 1; i
< n
; i
++)
577 *m_pstream
<< XLOG2DEV(points
[i
].x
+xoffset
) << " "
578 << YLOG2DEV(points
[i
].y
+yoffset
) << " lineto\n";
581 *m_pstream
<< "stroke\n";
584 void wxPostScriptDC::DrawRectangle (long x
, long y
, long width
, long height
)
586 wxCHECK_RET( m_ok
&& m_pstream
, "invalid postscript dc" );
588 if (m_brush
.GetStyle () != wxTRANSPARENT
)
592 *m_pstream
<< "newpath\n"
593 << XLOG2DEV(x
) << " " << YLOG2DEV(y
) << " moveto\n"
594 << XLOG2DEV(x
+ width
) << " " << YLOG2DEV(y
) << " lineto\n"
595 << XLOG2DEV(x
+ width
) << " " << YLOG2DEV(y
+ height
) << " lineto\n"
596 << XLOG2DEV(x
) << " " << YLOG2DEV(y
+ height
) << " lineto\n"
600 CalcBoundingBox( x
, y
);
601 CalcBoundingBox( x
+ width
, y
+ height
);
604 if (m_pen
.GetStyle () != wxTRANSPARENT
)
608 *m_pstream
<< "newpath\n"
609 << XLOG2DEV(x
) << " " << YLOG2DEV(y
) << " moveto\n"
610 << XLOG2DEV(x
+ width
) << " " << YLOG2DEV(y
) << " lineto\n"
611 << XLOG2DEV(x
+ width
) << " " << YLOG2DEV(y
+ height
) << " lineto\n"
612 << XLOG2DEV(x
) << " " << YLOG2DEV(y
+ height
) << " lineto\n"
616 CalcBoundingBox( x
, y
);
617 CalcBoundingBox( x
+ width
, y
+ height
);
621 void wxPostScriptDC::DrawRoundedRectangle (long x
, long y
, long width
, long height
, double radius
)
623 wxCHECK_RET( m_ok
&& m_pstream
, "invalid postscript dc" );
627 // Now, a negative radius is interpreted to mean
628 // 'the proportion of the smallest X or Y dimension'
629 double smallest
= 0.0;
634 radius
= (-radius
* smallest
);
637 long rad
= (long) radius
;
639 if (m_brush
.GetStyle () != wxTRANSPARENT
)
643 // Draw rectangle anticlockwise
644 *m_pstream
<< "newpath\n"
645 << XLOG2DEV(x
+ rad
) << " " << YLOG2DEV(y
+ rad
) << " " << XLOG2DEVREL(rad
) << " 90 180 arc\n"
646 << XLOG2DEV(x
) << " " << YLOG2DEV(y
+ rad
) << " moveto\n"
647 << XLOG2DEV(x
+ rad
) << " " << YLOG2DEV(y
+ height
- rad
) << " " << XLOG2DEVREL(rad
) << " 180 270 arc\n"
648 << XLOG2DEV(x
+ width
- rad
) << " " << YLOG2DEV(y
+ height
) << " lineto\n"
649 << XLOG2DEV(x
+ width
- rad
) << " " << YLOG2DEV(y
+ height
- rad
) << " " << XLOG2DEVREL(rad
) << " 270 0 arc\n"
650 << XLOG2DEV(x
+ width
) << " " << YLOG2DEV(y
+ rad
) << " lineto\n"
651 << XLOG2DEV(x
+ width
- rad
) << " " << YLOG2DEV(y
+ rad
) << " " << XLOG2DEVREL(rad
) << " 0 90 arc\n"
652 << XLOG2DEV(x
+ rad
) << " " << YLOG2DEV(y
) << " lineto\n"
656 CalcBoundingBox( x
, y
);
657 CalcBoundingBox( x
+ width
, y
+ height
);
660 if (m_pen
.GetStyle () != wxTRANSPARENT
)
664 // Draw rectangle anticlockwise
665 *m_pstream
<< "newpath\n"
666 << XLOG2DEV(x
+ rad
) << " " << YLOG2DEV(y
+ rad
) << " " << XLOG2DEVREL(rad
) << " 90 180 arc\n"
667 << XLOG2DEV(x
) << " " << YLOG2DEV(y
+ rad
) << " moveto\n"
668 << XLOG2DEV(x
+ rad
) << " " << YLOG2DEV(y
+ height
- rad
) << " " << XLOG2DEVREL(rad
) << " 180 270 arc\n"
669 << XLOG2DEV(x
+ width
- rad
) << " " << YLOG2DEV(y
+ height
) << " lineto\n"
670 << XLOG2DEV(x
+ width
- rad
) << " " << YLOG2DEV(y
+ height
- rad
) << " " << XLOG2DEVREL(rad
) << " 270 0 arc\n"
671 << XLOG2DEV(x
+ width
) << " " << YLOG2DEV(y
+ rad
) << " lineto\n"
672 << XLOG2DEV(x
+ width
- rad
) << " " << YLOG2DEV(y
+ rad
) << " " << XLOG2DEVREL(rad
) << " 0 90 arc\n"
673 << XLOG2DEV(x
+ rad
) << " " << YLOG2DEV(y
) << " lineto\n"
677 CalcBoundingBox( x
, y
);
678 CalcBoundingBox( x
+ width
, y
+ height
);
682 void wxPostScriptDC::DrawEllipse (long x
, long y
, long width
, long height
)
684 wxCHECK_RET( m_ok
&& m_pstream
, "invalid postscript dc" );
686 if (m_brush
.GetStyle () != wxTRANSPARENT
)
690 *m_pstream
<< "newpath\n"
691 << XLOG2DEV(x
+ width
/ 2) << " " << YLOG2DEV(y
+ height
/ 2) << " "
692 << XLOG2DEV(width
/ 2) << " " << YLOG2DEVREL(height
/ 2) << " 0 360 ellipse\n"
695 CalcBoundingBox( x
- width
, y
- height
);
696 CalcBoundingBox( x
+ width
, y
+ height
);
699 if (m_pen
.Ok() && m_pen
.GetStyle () != wxTRANSPARENT
)
703 *m_pstream
<< "newpath\n"
704 << XLOG2DEV(x
+ width
/ 2) << " " << YLOG2DEV(y
+ height
/ 2) << " "
705 << XLOG2DEV(width
/ 2) << " " << YLOG2DEVREL(height
/ 2) << " 0 360 ellipse\n"
708 CalcBoundingBox( x
- width
, y
- height
);
709 CalcBoundingBox( x
+ width
, y
+ height
);
713 void wxPostScriptDC::DrawIcon (const wxIcon
& icon
, long x
, long y
)
715 DrawBitmap( icon
, x
, y
, TRUE
);
718 void wxPostScriptDC::DrawBitmap( const wxBitmap
& bitmap
, long x
, long y
, bool WXUNUSED(useMask
) )
720 wxCHECK_RET( m_ok
&& m_pstream
, "invalid postscript dc" );
722 if (!bitmap
.Ok()) return;
724 wxImage
image( bitmap
);
726 if (!image
.Ok()) return;
728 int ww
= XLOG2DEVREL(image
.GetWidth());
729 int hh
= YLOG2DEVREL(image
.GetHeight());
731 image
= image
.Scale( ww
, hh
);
733 if (!image
.Ok()) return;
735 int xx
= XLOG2DEV(x
);
736 int yy
= YLOG2DEV(y
+ bitmap
.GetHeight());
738 *m_pstream
<< "/origstate save def\n"
740 << "/pix " << ww
<< " string def\n"
741 << "/grays " << ww
<< " string def\n"
742 << "/npixels 0 def\n"
743 << "/rgbindx 0 def\n"
744 << xx
<< " " << yy
<< " translate\n"
745 << ww
<< " " << hh
<< " scale\n"
746 << ww
<< " " << hh
<< " 8\n"
747 << "[" << ww
<< " 0 0 " << (-hh
) << " 0 " << hh
<< "]\n"
748 << "{currentfile pix readhexstring pop}\n"
749 << "false 3 colorimage\n";
751 for (int j
= 0; j
< hh
; j
++)
753 for (int i
= 0; i
< ww
; i
++)
757 wxDecToHex( image
.GetRed(i
,j
), buffer
);
758 *m_pstream
<< buffer
;
759 wxDecToHex( image
.GetGreen(i
,j
), buffer
);
760 *m_pstream
<< buffer
;
761 wxDecToHex( image
.GetBlue(i
,j
), buffer
);
762 *m_pstream
<< buffer
;
767 *m_pstream
<< "end\n";
768 *m_pstream
<< "origstate restore\n";
772 void wxPostScriptDC::SetFont (const wxFont
& font
)
774 wxCHECK_RET( m_ok
&& m_pstream
, "invalid postscript dc" );
776 if (!font
.Ok()) return;
780 char *name
= wxTheFontNameDirectory
->GetPostScriptName( m_font
.GetFamily(),
783 if (!name
) name
= "Times-Roman";
785 *m_pstream
<< "/" << name
<< " reencodeISO def\n"
786 << "/" << name
<< " findfont\n"
787 << YLOG2DEVREL(font
.GetPointSize())
788 << " scalefont setfont\n";
791 void wxPostScriptDC::SetPen( const wxPen
& pen
)
793 wxCHECK_RET( m_ok
&& m_pstream
, "invalid postscript dc" );
795 if (!pen
.Ok()) return;
797 int oldStyle
= m_pen
.GetStyle();
801 *m_pstream
<< XLOG2DEVREL(m_pen
.GetWidth()) << " setlinewidth\n";
804 Line style - WRONG: 2nd arg is OFFSET
806 Here, I'm afraid you do not conceive meaning of parameters of 'setdash'
807 operator correctly. You should look-up this in the Red Book: the 2nd parame-
808 ter is not number of values in the array of the first one, but an offset
809 into this description of the pattern. I mean a real *offset* not index
810 into array. I.e. If the command is [3 4] 1 setdash is used, then there
811 will be first black line *2* units long, then space 4 units, then the
812 pattern of *3* units black, 4 units space will be repeated.
815 static const char *dotted
= "[2 5] 2";
816 static const char *short_dashed
= "[4 4] 2";
817 static const char *long_dashed
= "[4 8] 2";
818 static const char *dotted_dashed
= "[6 6 2 6] 4";
820 const char *psdash
= (char *) NULL
;
821 switch (m_pen
.GetStyle ())
823 case wxDOT
: psdash
= dotted
; break;
824 case wxSHORT_DASH
: psdash
= short_dashed
; break;
825 case wxLONG_DASH
: psdash
= long_dashed
; break;
826 case wxDOT_DASH
: psdash
= dotted_dashed
; break;
829 default: psdash
= "[] 0"; break;
832 if (oldStyle
!= m_pen
.GetStyle())
834 *m_pstream
<< psdash
<< " setdash\n";
838 unsigned char red
= m_pen
.GetColour().Red();
839 unsigned char blue
= m_pen
.GetColour().Blue();
840 unsigned char green
= m_pen
.GetColour().Green();
844 // Anything not white is black
845 if (!(red
== (unsigned char) 255 && blue
== (unsigned char) 255
846 && green
== (unsigned char) 255))
848 red
= (unsigned char) 0;
849 green
= (unsigned char) 0;
850 blue
= (unsigned char) 0;
856 if (!(red
== m_currentRed
&& green
== m_currentGreen
&& blue
== m_currentBlue
))
858 long redPS
= (long) (((int) red
) / 255.0);
859 long bluePS
= (long) (((int) blue
) / 255.0);
860 long greenPS
= (long) (((int) green
) / 255.0);
862 *m_pstream
<< redPS
<< " " << greenPS
<< " " << bluePS
<< " setrgbcolor\n";
865 m_currentBlue
= blue
;
866 m_currentGreen
= green
;
870 void wxPostScriptDC::SetBrush( const wxBrush
& brush
)
872 wxCHECK_RET( m_ok
&& m_pstream
, "invalid postscript dc" );
874 if (!brush
.Ok()) return;
879 unsigned char red
= m_brush
.GetColour ().Red ();
880 unsigned char blue
= m_brush
.GetColour ().Blue ();
881 unsigned char green
= m_brush
.GetColour ().Green ();
885 // Anything not black is white
886 if (!(red
== (unsigned char) 0 && blue
== (unsigned char) 0
887 && green
== (unsigned char) 0))
889 red
= (unsigned char) 255;
890 green
= (unsigned char) 255;
891 blue
= (unsigned char) 255;
897 if (!(red
== m_currentRed
&& green
== m_currentGreen
&& blue
== m_currentBlue
))
899 long redPS
= (long) (((int) red
) / 255.0);
900 long bluePS
= (long) (((int) blue
) / 255.0);
901 long greenPS
= (long) (((int) green
) / 255.0);
902 *m_pstream
<< redPS
<< " " << greenPS
<< " " << bluePS
<< " setrgbcolor\n";
904 m_currentBlue
= blue
;
905 m_currentGreen
= green
;
909 void wxPostScriptDC::DrawText( const wxString
& text
, long x
, long y
, bool WXUNUSED(use16bit
) )
911 wxCHECK_RET( m_ok
&& m_pstream
, "invalid postscript dc" );
915 if (m_textForegroundColour
.Ok ())
917 unsigned char red
= m_textForegroundColour
.Red ();
918 unsigned char blue
= m_textForegroundColour
.Blue ();
919 unsigned char green
= m_textForegroundColour
.Green ();
923 // Anything not white is black
924 if (!(red
== (unsigned char) 255 && blue
== (unsigned char) 255
925 && green
== (unsigned char) 255))
927 red
= (unsigned char) 0;
928 green
= (unsigned char) 0;
929 blue
= (unsigned char) 0;
933 // maybe setgray here ?
935 if (!(red
== m_currentRed
&& green
== m_currentGreen
&& blue
== m_currentBlue
))
937 long redPS
= (long) (((int) red
) / 255.0);
938 long bluePS
= (long) (((int) blue
) / 255.0);
939 long greenPS
= (long) (((int) green
) / 255.0);
940 *m_pstream
<< redPS
<< " " << greenPS
<< " " << bluePS
<< " setrgbcolor\n";
943 m_currentBlue
= blue
;
944 m_currentGreen
= green
;
948 int size
= m_font
.GetPointSize();
950 long by
= y
+ (long)floor( float(size
) * 2.0 / 3.0 ); // approximate baseline
951 *m_pstream
<< XLOG2DEV(x
) << " " << YLOG2DEV(by
) << " moveto\n";
954 int len
= strlen ((char *)(const char *)text
);
956 for (i
= 0; i
< len
; i
++)
958 int c
= (unsigned char) text
[i
];
959 if ( c
== ')' || c
== '(' || c
== '\\')
961 *m_pstream
<< "\\" << (char) c
;
965 // Cope with character codes > 127
967 sprintf(tmp
, "\\%o", c
);
971 *m_pstream
<< (char) c
;
974 *m_pstream
<< ")" << " show\n";
976 if (m_font
.GetUnderlined())
978 long uy
= (long)(y
+ size
- m_underlinePosition
);
980 GetTextExtent(text
, &w
, &h
);
982 *m_pstream
<< "gsave " << XLOG2DEV(x
) << " " << YLOG2DEV(uy
)
984 << (long)m_underlineThickness
<< " setlinewidth "
985 << XLOG2DEV(x
+ w
) << " " << YLOG2DEV(uy
)
986 << " lineto stroke grestore\n";
989 CalcBoundingBox( x
, y
);
990 CalcBoundingBox( x
+ size
* text
.Length() * 2/3 , y
);
994 void wxPostScriptDC::SetBackground (const wxBrush
& brush
)
996 m_backgroundBrush
= brush
;
999 void wxPostScriptDC::SetLogicalFunction (int WXUNUSED(function
))
1001 wxFAIL_MSG( "wxPostScriptDC::SetLogicalFunction not implemented." );
1004 void wxPostScriptDC::DrawSpline( wxList
*points
)
1006 wxCHECK_RET( m_ok
&& m_pstream
, "invalid postscript dc" );
1010 double a
, b
, c
, d
, x1
, y1
, x2
, y2
, x3
, y3
;
1013 wxNode
*node
= points
->First();
1014 p
= (wxPoint
*)node
->Data();
1018 node
= node
->Next();
1019 p
= (wxPoint
*)node
->Data();
1022 x3
= a
= (double)(x1
+ c
) / 2;
1023 y3
= b
= (double)(y1
+ d
) / 2;
1025 *m_pstream
<< "newpath "
1026 << XLOG2DEV((long)x1
) << " " << YLOG2DEV((long)y1
) << " moveto "
1027 << XLOG2DEV((long)x3
) << " " << YLOG2DEV((long)y3
) << " lineto\n";
1029 CalcBoundingBox( (long)x1
, (long)y1
);
1030 CalcBoundingBox( (long)x3
, (long)y3
);
1032 while ((node
= node
->Next()) != NULL
)
1034 q
= (wxPoint
*)node
->Data();
1042 x3
= (double)(x2
+ c
) / 2;
1043 y3
= (double)(y2
+ d
) / 2;
1044 *m_pstream
<< XLOG2DEV((long)x1
) << " " << YLOG2DEV((long)y1
) << " "
1045 << XLOG2DEV((long)x2
) << " " << YLOG2DEV((long)y2
) << " "
1046 << XLOG2DEV((long)x3
) << " " << YLOG2DEV((long)y3
) << " DrawSplineSection\n";
1048 CalcBoundingBox( (long)x1
, (long)y1
);
1049 CalcBoundingBox( (long)x3
, (long)y3
);
1053 At this point, (x2,y2) and (c,d) are the position of the
1054 next-to-last and last point respectively, in the point list
1057 *m_pstream
<< XLOG2DEV((long)c
) << " " << YLOG2DEV((long)d
) << " lineto stroke\n";
1060 long wxPostScriptDC::GetCharWidth ()
1062 // Chris Breeze: reasonable approximation using wxMODERN/Courier
1063 return (long) (GetCharHeight() * 72.0 / 120.0);
1067 void wxPostScriptDC::SetAxisOrientation( bool xLeftRight
, bool yBottomUp
)
1069 wxCHECK_RET( m_ok
&& m_pstream
, "invalid postscript dc" );
1071 m_signX
= (xLeftRight
? 1 : -1);
1072 m_signY
= (yBottomUp
? 1 : -1);
1074 ComputeScaleAndOrigin();
1077 void wxPostScriptDC::SetDeviceOrigin( long x
, long y
)
1079 wxCHECK_RET( m_ok
&& m_pstream
, "invalid postscript dc" );
1085 wxDC::SetDeviceOrigin( x
, h
-y
);
1088 void wxPostScriptDC::GetSize(int* width
, int* height
) const
1090 const char *paperType
= wxThePrintSetupData
->GetPaperName();
1092 if (!paperType
) paperType
= _("A4 210 x 297 mm");
1094 wxPrintPaperType
*paper
= wxThePrintPaperDatabase
->FindPaperType(paperType
);
1096 if (!paper
) paper
= wxThePrintPaperDatabase
->FindPaperType(_("A4 210 x 297 mm"));
1100 if (width
) *width
= paper
->widthPixels
;
1101 if (height
) *height
= paper
->heightPixels
;
1105 if (width
) *width
= 595;
1106 if (height
) *height
= 842;
1110 bool wxPostScriptDC::StartDoc (const wxString
& message
)
1112 wxCHECK_MSG( m_ok
, FALSE
, "invalid postscript dc" );
1114 if (m_filename
== "")
1116 m_filename
= wxGetTempFileName("ps");
1117 wxThePrintSetupData
->SetPrinterFile((char *)(const char *)m_filename
);
1122 wxThePrintSetupData
->SetPrinterFile((char *)(const char *)m_filename
);
1125 m_pstream
= new ofstream (wxThePrintSetupData
->GetPrinterFile());
1127 if (!m_pstream
|| !m_pstream
->good())
1129 wxMessageBox (_("Cannot open file!"), _("Error"), wxOK
);
1136 SetBrush( *wxBLACK_BRUSH
);
1137 SetPen( *wxBLACK_PEN
);
1138 SetBackground( *wxWHITE_BRUSH
);
1139 SetTextForeground( *wxBLACK
);
1141 // set origin according to paper size
1142 SetDeviceOrigin( 0,0 );
1150 void wxPostScriptDC::EndDoc ()
1152 wxCHECK_RET( m_ok
&& m_pstream
, "invalid postscript dc" );
1157 *m_pstream
<< "grestore\n";
1163 m_pstream
= (ofstream
*) NULL
;
1166 char *header_file
= wxGetTempFileName("ps");
1168 m_pstream
= new ofstream( header_file
);
1170 *m_pstream
<< "%!PS-Adobe-2.0\n"; /* PostScript magic strings */
1171 *m_pstream
<< "%%Title: " << (const char *) m_title
<< "\n";
1172 *m_pstream
<< "%%Creator: " << wxTheApp
->argv
[0] << "\n";
1173 *m_pstream
<< "%%CreationDate: " << wxNow() << "\n";
1176 if ( wxGetEmailAddress(userID
, sizeof(userID
)) )
1178 *m_pstream
<< "%%For: " << (char *)userID
;
1180 if (wxGetUserName(userName
, sizeof(userName
)))
1181 *m_pstream
<< " (" << (char *)userName
<< ")";
1184 else if ( wxGetUserName(userID
, sizeof(userID
)) )
1186 *m_pstream
<< "%%For: " << (char *)userID
<< "\n";
1189 // THE FOLLOWING HAS BEEN CONTRIBUTED BY Andy Fyfe <andy@hyperparallel.com>
1191 long wx_printer_translate_x
, wx_printer_translate_y
;
1192 double wx_printer_scale_x
, wx_printer_scale_y
;
1193 wxThePrintSetupData
->GetPrinterTranslation(&wx_printer_translate_x
, &wx_printer_translate_y
);
1194 wxThePrintSetupData
->GetPrinterScaling(&wx_printer_scale_x
, &wx_printer_scale_y
);
1196 if (wxThePrintSetupData
->GetPrinterOrientation() == PS_LANDSCAPE
)
1198 *m_pstream
<< "%%Orientation: Landscape\n";
1202 *m_pstream
<< "%%Orientation: Portrait\n";
1205 // Compute the bounding box. Note that it is in the default user
1206 // coordinate system, thus we have to convert the values.
1207 long llx
= (long) ((XLOG2DEV(m_minX
)+wx_printer_translate_x
)*wx_printer_scale_x
);
1208 long lly
= (long) ((YLOG2DEV(m_minY
)+wx_printer_translate_y
)*wx_printer_scale_y
);
1209 long urx
= (long) ((XLOG2DEV(m_maxX
)+wx_printer_translate_x
)*wx_printer_scale_x
);
1210 long ury
= (long) ((YLOG2DEV(m_maxY
)+wx_printer_translate_y
)*wx_printer_scale_y
);
1212 // If we're landscape, our sense of "x" and "y" is reversed.
1213 if (wxThePrintSetupData
->GetPrinterOrientation() == PS_LANDSCAPE
)
1216 tmp
= llx
; llx
= lly
; lly
= tmp
;
1217 tmp
= urx
; urx
= ury
; ury
= tmp
;
1219 // We need either the two lines that follow, or we need to subtract
1220 // min_x from real_translate_y, which is commented out below.
1221 llx
= llx
- (long)(m_minX
*wx_printer_scale_y
);
1222 urx
= urx
- (long)(m_minX
*wx_printer_scale_y
);
1225 // The Adobe specifications call for integers; we round as to make
1226 // the bounding larger.
1227 *m_pstream
<< "%%BoundingBox: "
1228 << floor((double)llx
) << " " << floor((double)lly
) << " "
1229 << ceil((double)urx
) << " " << ceil((double)ury
) << "\n";
1230 *m_pstream
<< "%%Pages: " << (wxPageNumber
- 1) << "\n";
1231 *m_pstream
<< "%%EndComments\n\n";
1233 // To check the correctness of the bounding box, postscript commands
1234 // to draw a box corresponding to the bounding box are generated below.
1235 // But since we typically don't want to print such a box, the postscript
1236 // commands are generated within comments. These lines appear before any
1237 // adjustment of scale, rotation, or translation, and hence are in the
1238 // default user coordinates.
1239 *m_pstream
<< "% newpath\n";
1240 *m_pstream
<< "% " << llx
<< " " << lly
<< " moveto\n";
1241 *m_pstream
<< "% " << urx
<< " " << lly
<< " lineto\n";
1242 *m_pstream
<< "% " << urx
<< " " << ury
<< " lineto\n";
1243 *m_pstream
<< "% " << llx
<< " " << ury
<< " lineto closepath stroke\n";
1245 *m_pstream
<< "%%BeginProlog\n";
1246 *m_pstream
<< wxPostScriptHeaderEllipse
;
1247 *m_pstream
<< wxPostScriptHeaderEllipticArc
;
1248 *m_pstream
<< wxPostScriptHeaderColourImage
;
1249 *m_pstream
<< wxPostScriptHeaderReencodeISO1
;
1250 *m_pstream
<< wxPostScriptHeaderReencodeISO2
;
1252 if (wxPostScriptHeaderSpline
)
1253 *m_pstream
<< wxPostScriptHeaderSpline
;
1254 *m_pstream
<< "%%EndProlog\n";
1257 m_pstream
= (ofstream
*) NULL
;
1259 char *tmp_file
= wxGetTempFileName("ps");
1261 // Paste header Before wx_printer_file
1262 wxConcatFiles (header_file
, wxThePrintSetupData
->GetPrinterFile(), tmp_file
);
1263 wxRemoveFile (header_file
);
1264 wxRemoveFile (wxThePrintSetupData
->GetPrinterFile());
1265 wxRenameFile(tmp_file
, wxThePrintSetupData
->GetPrinterFile());
1267 #if defined(__X__) || defined(__WXGTK__)
1270 switch (wxThePrintSetupData
->GetPrinterMode()) {
1274 argv
[0] = wxThePrintSetupData
->GetPrintPreviewCommand();
1275 argv
[1] = wxThePrintSetupData
->GetPrinterFile();
1276 argv
[2] = (char *) NULL
;
1277 wxExecute (argv
, TRUE
);
1278 wxRemoveFile(wxThePrintSetupData
->GetPrinterFile());
1286 argv
[argc
++] = wxThePrintSetupData
->GetPrinterCommand();
1288 // !SM! If we simply assign to argv[1] here, if printer options
1289 // are blank, we get an annoying and confusing message from lpr.
1290 char * opts
= wxThePrintSetupData
->GetPrinterOptions();
1292 argv
[argc
++] = opts
;
1294 argv
[argc
++] = wxThePrintSetupData
->GetPrinterFile();
1295 argv
[argc
++] = (char *) NULL
;
1296 wxExecute (argv
, TRUE
);
1297 wxRemoveFile(wxThePrintSetupData
->GetPrinterFile());
1308 void wxPostScriptDC::StartPage ()
1310 wxCHECK_RET( m_ok
&& m_pstream
, "invalid postscript dc" );
1312 *m_pstream
<< "%%Page: " << (wxPageNumber
++) << "\n";
1314 // *m_pstream << "matrix currentmatrix\n";
1316 // Added by Chris Breeze
1318 // Each page starts with an "initgraphics" which resets the
1319 // transformation and so we need to reset the origin
1320 // (and rotate the page for landscape printing)
1323 m_scaleFactor = 1.0;
1324 m_logicalOriginX = 0;
1325 m_logicalOriginY = 0;
1329 long translate_x
, translate_y
;
1330 double scale_x
, scale_y
;
1331 wxThePrintSetupData
->GetPrinterTranslation(&translate_x
, &translate_y
);
1332 wxThePrintSetupData
->GetPrinterScaling(&scale_x
, &scale_y
);
1334 if (wxThePrintSetupData
->GetPrinterOrientation() == PS_LANDSCAPE
)
1336 translate_y
-= m_maxY
;
1337 *m_pstream
<< "90 rotate\n";
1340 *m_pstream
<< scale_x
<< " " << scale_y
<< " scale\n";
1341 *m_pstream
<< translate_x
<< " " << translate_y
<< " translate\n";
1344 void wxPostScriptDC::EndPage ()
1346 wxCHECK_RET( m_ok
&& m_pstream
, "invalid postscript dc" );
1348 *m_pstream
<< "showpage\n";
1351 bool wxPostScriptDC::Blit (long xdest
, long ydest
, long fwidth
, long fheight
,
1352 wxDC
*source
, long xsrc
, long ysrc
, int WXUNUSED(rop
), bool WXUNUSED(useMask
))
1354 wxCHECK_MSG( m_ok
&& m_pstream
, FALSE
, "invalid postscript dc" );
1359 long wxPostScriptDC::GetCharHeight ()
1362 return m_font
.GetPointSize();
1367 void wxPostScriptDC::GetTextExtent (const wxString
& string
, long *x
, long *y
,
1368 long *descent
, long *externalLeading
, wxFont
*theFont
,
1369 bool WXUNUSED(use16
))
1371 // if (!m_pstream) return;
1373 wxFont
*fontToUse
= theFont
;
1375 if (!fontToUse
) fontToUse
= (wxFont
*) &m_font
;
1377 #if !USE_AFM_FOR_POSTSCRIPT
1378 // Provide a VERY rough estimate (avoid using it)
1379 // Chris Breeze 5/11/97: produces accurate results for mono-spaced
1380 // font such as Courier (aka wxMODERN)
1384 height
= fontToUse
->GetPointSize();
1386 *x
= strlen (string
) * height
* 72 / 120;
1387 *y
= (long) (height
* 1.32); // allow for descender
1391 if (externalLeading
)
1392 *externalLeading
= 0;
1394 // +++++ start of contributed code +++++
1396 // ************************************************************
1397 // method for calculating string widths in postscript:
1398 // read in the AFM (adobe font metrics) file for the
1399 // actual font, parse it and extract the character widths
1400 // and also the descender. this may be improved, but for now
1401 // it works well. the AFM file is only read in if the
1402 // font is changed. this may be chached in the future.
1403 // calls to GetTextExtent with the font unchanged are rather
1406 // for each font and style used there is an AFM file necessary.
1407 // currently i have only files for the roman font family.
1408 // i try to get files for the other ones!
1410 // CAVE: the size of the string is currently always calculated
1411 // in 'points' (1/72 of an inch). this should later on be
1412 // changed to depend on the mapping mode.
1413 // CAVE: the path to the AFM files must be set before calling this
1414 // function. this is usually done by a call like the following:
1415 // wxSetAFMPath("d:\\wxw161\\afm\\");
1419 // wxPostScriptDC dc(NULL, TRUE);
1421 // wxSetAFMPath("d:\\wxw161\\afm\\");
1422 // dc.StartDoc("Test");
1425 // dc.SetFont(new wxFont(10, wxROMAN, wxNORMAL, wxNORMAL));
1426 // dc.GetTextExtent("Hallo",&w,&h);
1431 // by steve (stefan.hammes@urz.uni-heidelberg.de)
1432 // created: 10.09.94
1433 // updated: 14.05.95
1435 assert(fontToUse
&& "void wxPostScriptDC::GetTextExtent: no font defined");
1436 assert(x
&& "void wxPostScriptDC::GetTextExtent: x == NULL");
1437 assert(y
&& "void wxPostScriptDC::GetTextExtent: y == NULL");
1439 // these static vars are for storing the state between calls
1440 static int lastFamily
= INT_MIN
;
1441 static int lastSize
= INT_MIN
;
1442 static int lastStyle
= INT_MIN
;
1443 static int lastWeight
= INT_MIN
;
1444 static int lastDescender
= INT_MIN
;
1445 static int lastWidths
[256]; // widths of the characters
1447 // get actual parameters
1448 const int Family
= fontToUse
->GetFamily();
1449 const int Size
= fontToUse
->GetPointSize();
1450 const int Style
= fontToUse
->GetStyle();
1451 const int Weight
= fontToUse
->GetWeight();
1453 // if we have another font, read the font-metrics
1454 if(Family
!=lastFamily
||Size
!=lastSize
||Style
!=lastStyle
||Weight
!=lastWeight
){
1455 // store actual values
1456 lastFamily
= Family
;
1459 lastWeight
= Weight
;
1461 // read in new font metrics **************************************
1463 // 1. construct filename ******************************************
1464 /* MATTHEW: [2] Use wxTheFontNameDirectory */
1467 // Julian - we'll need to do this a different way now we've removed the
1468 // font directory system. Must find Stefan's original code.
1470 name
= wxTheFontNameDirectory
->GetAFMName(Family
, Weight
, Style
);
1474 // get the directory of the AFM files
1478 strcpy(afmName
,wxGetAFMPath());
1480 // 2. open and process the file **********************************
1482 // a short explanation of the AFM format:
1483 // we have for each character a line, which gives its size
1486 // C 63 ; WX 444 ; N question ; B 49 -14 395 676 ;
1488 // that means, we have a character with ascii code 63, and width
1489 // (444/1000 * fontSize) points.
1490 // the other data is ignored for now!
1492 // when the font has changed, we read in the right AFM file and store the
1493 // character widths in an array, which is processed below (see point 3.).
1495 // new elements JC Sun Aug 25 23:21:44 MET DST 1996
1498 strcat(afmName
,name
);
1499 strcat(afmName
,".afm");
1500 FILE *afmFile
= fopen(afmName
,"r");
1502 wxDebugMsg("GetTextExtent: can't open AFM file '%s'\n",afmName
);
1503 wxDebugMsg(" using approximate values\n");
1505 for (i
=0; i
<256; i
++) lastWidths
[i
] = 500; // an approximate value
1506 lastDescender
= -150; // dito.
1509 // init the widths array
1510 for(i
=0; i
<256; i
++) lastWidths
[i
]= INT_MIN
;
1511 // some variables for holding parts of a line
1512 char cString
[10],semiString
[10],WXString
[10],descString
[20];
1513 char upString
[30], utString
[30], encString
[50];
1516 // read in the file and parse it
1517 while(fgets(line
,sizeof(line
),afmFile
)!=NULL
){
1518 // A.) check for descender definition
1519 if(strncmp(line
,"Descender",9)==0){
1520 if((sscanf(line
,"%s%d",descString
,&lastDescender
)!=2)
1521 || (strcmp(descString
,"Descender")!=0)) {
1522 wxDebugMsg("AFM-file '%s': line '%s' has error (bad descender)\n",
1526 // JC 1.) check for UnderlinePosition
1527 else if(strncmp(line
,"UnderlinePosition",17)==0){
1528 if((sscanf(line
,"%s%lf",upString
,&UnderlinePosition
)!=2)
1529 || (strcmp(upString
,"UnderlinePosition")!=0)) {
1530 wxDebugMsg("AFM-file '%s': line '%s' has error (bad UnderlinePosition)\n",
1534 // JC 2.) check for UnderlineThickness
1535 else if(strncmp(line
,"UnderlineThickness",18)==0){
1536 if((sscanf(line
,"%s%lf",utString
,&UnderlineThickness
)!=2)
1537 || (strcmp(utString
,"UnderlineThickness")!=0)) {
1538 wxDebugMsg("AFM-file '%s': line '%s' has error (bad UnderlineThickness)\n",
1542 // JC 3.) check for EncodingScheme
1543 else if(strncmp(line
,"EncodingScheme",14)==0){
1544 if((sscanf(line
,"%s%s",utString
,encString
)!=2)
1545 || (strcmp(utString
,"EncodingScheme")!=0)) {
1546 wxDebugMsg("AFM-file '%s': line '%s' has error (bad EncodingScheme)\n",
1549 else if (strncmp(encString
, "AdobeStandardEncoding", 21))
1551 wxDebugMsg("AFM-file '%s': line '%s' has error (unsupported EncodingScheme %s)\n",
1552 afmName
,line
, encString
);
1555 // B.) check for char-width
1556 else if(strncmp(line
,"C ",2)==0){
1557 if(sscanf(line
,"%s%d%s%s%d",
1558 cString
,&ascii
,semiString
,WXString
,&cWidth
)!=5){
1559 wxDebugMsg("AFM-file '%s': line '%s' has an error (bad character width)\n",afmName
,line
);
1561 if(strcmp(cString
,"C")!=0 || strcmp(semiString
,";")!=0 ||
1562 strcmp(WXString
,"WX")!=0){
1563 wxDebugMsg("AFM-file '%s': line '%s' has a format error\n",afmName
,line
);
1565 //printf(" char '%c'=%d has width '%d'\n",ascii,ascii,cWidth);
1566 if(ascii
>=0 && ascii
<256){
1567 lastWidths
[ascii
] = cWidth
; // store width
1569 /* MATTHEW: this happens a lot; don't print an error */
1570 // wxDebugMsg("AFM-file '%s': ASCII value %d out of range\n",afmName,ascii);
1573 // C.) ignore other entries.
1577 // hack to compute correct values for german 'Umlaute'
1578 // the correct way would be to map the character names
1579 // like 'adieresis' to corresp. positions of ISOEnc and read
1580 // these values from AFM files, too. Maybe later ...
1581 lastWidths
[196] = lastWidths
['A']; // Ä
1582 lastWidths
[228] = lastWidths
['a']; // ä
1583 lastWidths
[214] = lastWidths
['O']; // Ö
1584 lastWidths
[246] = lastWidths
['o']; // ö
1585 lastWidths
[220] = lastWidths
['U']; // Ü
1586 lastWidths
[252] = lastWidths
['u']; // ü
1587 lastWidths
[223] = lastWidths
[251]; // ß
1590 // JC: calculate UnderlineThickness/UnderlinePosition
1591 m_underlinePosition
= m_underlinePosition
* fontToUse
->GetPointSize() / 1000.0f
;
1592 m_underlineThickness
= m_underlineThickness
* fontToUse
->GetPointSize() / 1000.0f
* m_scaleFactor
;
1594 // 3. now the font metrics are read in, calc size *******************
1595 // this is done by adding the widths of the characters in the
1596 // string. they are given in 1/1000 of the size!
1599 long height
=Size
; // by default
1601 for(p
=(unsigned char *)(const char *)string
; *p
; p
++){
1602 if(lastWidths
[*p
]== INT_MIN
){
1603 wxDebugMsg("GetTextExtent: undefined width for character '%c' (%d)\n",
1605 widthSum
+= (long)(lastWidths
[' ']/1000.0F
* Size
); // assume space
1607 widthSum
+= (long)((lastWidths
[*p
]/1000.0F
)*Size
);
1610 // add descender to height (it is usually a negative value)
1611 if(lastDescender
!=INT_MIN
){
1612 height
+= (long)(((-lastDescender
)/1000.0F
) * Size
); /* MATTHEW: forgot scale */
1615 // return size values
1619 // return other parameters
1621 if(lastDescender
!=INT_MIN
){
1622 *descent
= (long)(((-lastDescender
)/1000.0F
) * Size
); /* MATTHEW: forgot scale */
1628 // currently no idea how to calculate this!
1629 // if (externalLeading) *externalLeading = 0;
1630 if (externalLeading
)
1631 *externalLeading
= 0;
1633 // ----- end of contributed code -----
1637 void wxPostScriptDC::GetSizeMM(long *width
, long *height
) const
1639 const char *paperType
= wxThePrintSetupData
->GetPaperName();
1641 if (!paperType
) paperType
= _("A4 210 x 297 mm");
1643 wxPrintPaperType
*paper
= wxThePrintPaperDatabase
->FindPaperType(paperType
);
1645 if (!paper
) paper
= wxThePrintPaperDatabase
->FindPaperType(_("A4 210 x 297 mm"));
1649 if (width
) *width
= paper
->widthMM
;
1650 if (height
) *height
= paper
->heightMM
;
1654 if (width
) *width
= 210;
1655 if (height
) *height
= 297;