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
22 #include "wx/postscrp.h"
23 #include "wx/dcmemory.h"
26 #include "wx/filedlg.h"
28 #include "wx/msgdlg.h"
35 //-----------------------------------------------------------------------------
36 // start and end of document/page
37 //-----------------------------------------------------------------------------
39 static const char *wxPostScriptHeaderEllipse
= "\
40 /ellipsedict 8 dict def\n\
41 ellipsedict /mtrx matrix put\n\
45 /startangle exch def\n\
50 /savematrix mtrx currentmatrix def\n\
53 0 0 1 startangle endangle arc\n\
54 savematrix setmatrix\n\
59 static const char *wxPostScriptHeaderEllipticArc
= "\
60 /ellipticarcdict 8 dict def\n\
61 ellipticarcdict /mtrx matrix put\n\
63 { ellipticarcdict begin\n\
66 /startangle exch def\n\
71 /savematrix mtrx currentmatrix def\n\
74 do_fill { 0 0 moveto } if\n\
75 0 0 1 startangle endangle arc\n\
76 savematrix setmatrix\n\
77 do_fill { fill }{ stroke } ifelse\n\
81 static const char *wxPostScriptHeaderSpline
= "\
82 /DrawSplineSection {\n\
89 /xa x1 x2 x1 sub 0.666667 mul add def\n\
90 /ya y1 y2 y1 sub 0.666667 mul add def\n\
91 /xb x3 x2 x3 sub 0.666667 mul add def\n\
92 /yb y3 y2 y3 sub 0.666667 mul add def\n\
94 xa ya xb yb x3 y3 curveto\n\
98 static const char *wxPostScriptHeaderColourImage
= "\
99 % define 'colorimage' if it isn't defined\n\
100 % ('colortogray' and 'mergeprocs' come from xwd2ps\n\
102 /colorimage where % do we know about 'colorimage'?\n\
103 { pop } % yes: pop off the 'dict' returned\n\
104 { % no: define one\n\
105 /colortogray { % define an RGB->I function\n\
106 /rgbdata exch store % call input 'rgbdata'\n\
107 rgbdata length 3 idiv\n\
108 /npixls exch store\n\
110 0 1 npixls 1 sub {\n\
112 rgbdata rgbindx get 20 mul % Red\n\
113 rgbdata rgbindx 1 add get 32 mul % Green\n\
114 rgbdata rgbindx 2 add get 12 mul % Blue\n\
115 add add 64 idiv % I = .5G + .31R + .18B\n\
117 /rgbindx rgbindx 3 add store\n\
119 grays 0 npixls getinterval\n\
122 % Utility procedure for colorimage operator.\n\
123 % This procedure takes two procedures off the\n\
124 % stack and merges them into a single procedure.\n\
126 /mergeprocs { % def\n\
145 /colorimage { % def\n\
146 pop pop % remove 'false 3' operands\n\
147 {colortogray} mergeprocs\n\
150 } ifelse % end of 'false' case\n\
153 static char wxPostScriptHeaderReencodeISO1
[] =
155 "dup dup findfont dup length dict begin\n"
156 "{ 1 index /FID ne { def }{ pop pop } ifelse } forall\n"
157 "/Encoding ISOLatin1Encoding def\n"
158 "currentdict end definefont\n"
160 "/ISOLatin1Encoding [\n"
161 "/.notdef/.notdef/.notdef/.notdef/.notdef/.notdef/.notdef/.notdef\n"
162 "/.notdef/.notdef/.notdef/.notdef/.notdef/.notdef/.notdef/.notdef\n"
163 "/.notdef/.notdef/.notdef/.notdef/.notdef/.notdef/.notdef/.notdef\n"
164 "/.notdef/.notdef/.notdef/.notdef/.notdef/.notdef/.notdef/.notdef\n"
165 "/space/exclam/quotedbl/numbersign/dollar/percent/ampersand/quoteright\n"
166 "/parenleft/parenright/asterisk/plus/comma/minus/period/slash\n"
167 "/zero/one/two/three/four/five/six/seven/eight/nine/colon/semicolon\n"
168 "/less/equal/greater/question/at/A/B/C/D/E/F/G/H/I/J/K/L/M/N\n"
169 "/O/P/Q/R/S/T/U/V/W/X/Y/Z/bracketleft/backslash/bracketright\n"
170 "/asciicircum/underscore/quoteleft/a/b/c/d/e/f/g/h/i/j/k/l/m\n"
171 "/n/o/p/q/r/s/t/u/v/w/x/y/z/braceleft/bar/braceright/asciitilde\n"
172 "/.notdef/.notdef/.notdef/.notdef/.notdef/.notdef/.notdef/.notdef\n"
173 "/.notdef/.notdef/.notdef/.notdef/.notdef/.notdef/.notdef/.notdef\n"
174 "/.notdef/dotlessi/grave/acute/circumflex/tilde/macron/breve\n"
175 "/dotaccent/dieresis/.notdef/ring/cedilla/.notdef/hungarumlaut\n";
177 static char wxPostScriptHeaderReencodeISO2
[] =
178 "/ogonek/caron/space/exclamdown/cent/sterling/currency/yen/brokenbar\n"
179 "/section/dieresis/copyright/ordfeminine/guillemotleft/logicalnot\n"
180 "/hyphen/registered/macron/degree/plusminus/twosuperior/threesuperior\n"
181 "/acute/mu/paragraph/periodcentered/cedilla/onesuperior/ordmasculine\n"
182 "/guillemotright/onequarter/onehalf/threequarters/questiondown\n"
183 "/Agrave/Aacute/Acircumflex/Atilde/Adieresis/Aring/AE/Ccedilla\n"
184 "/Egrave/Eacute/Ecircumflex/Edieresis/Igrave/Iacute/Icircumflex\n"
185 "/Idieresis/Eth/Ntilde/Ograve/Oacute/Ocircumflex/Otilde/Odieresis\n"
186 "/multiply/Oslash/Ugrave/Uacute/Ucircumflex/Udieresis/Yacute\n"
187 "/Thorn/germandbls/agrave/aacute/acircumflex/atilde/adieresis\n"
188 "/aring/ae/ccedilla/egrave/eacute/ecircumflex/edieresis/igrave\n"
189 "/iacute/icircumflex/idieresis/eth/ntilde/ograve/oacute/ocircumflex\n"
190 "/otilde/odieresis/divide/oslash/ugrave/uacute/ucircumflex/udieresis\n"
191 "/yacute/thorn/ydieresis\n"
194 //-------------------------------------------------------------------------------
196 //-------------------------------------------------------------------------------
198 wxPostScriptDC::wxPostScriptDC ()
200 m_pstream
= (ofstream
*) NULL
;
210 m_underlinePosition
= 0.0;
211 m_underlineThickness
= 0.0;
213 m_signX
= 1; // default x-axis left to right
214 m_signY
= -1; // default y-axis bottom up -> top down
217 wxPostScriptDC::wxPostScriptDC (const wxString
& file
, bool interactive
, wxWindow
*parent
)
219 m_pstream
= (ofstream
*) NULL
;
229 m_underlinePosition
= 0.0;
230 m_underlineThickness
= 0.0;
232 m_signX
= 1; // default x-axis left to right
233 m_signY
= -1; // default y-axis bottom up -> top down
235 Create(file
, interactive
, parent
);
238 bool wxPostScriptDC::Create(const wxString
& file
, bool interactive
, wxWindow
*parent
)
240 m_isInteractive
= interactive
;
246 // Can only send to file in Windows
247 wxThePrintSetupData
->SetPrinterMode(PS_FILE
);
252 if ((m_ok
= PrinterDialog (parent
) ) == FALSE
) return FALSE
;
262 wxPostScriptDC::~wxPostScriptDC ()
264 if (m_pstream
) delete m_pstream
;
267 bool wxPostScriptDC::Ok() const
272 bool wxPostScriptDC::PrinterDialog(wxWindow
*parent
)
274 wxPostScriptPrintDialog
dialog( parent
, _("Printer Settings"), wxPoint(150, 150), wxSize(400, 400),
275 wxDEFAULT_DIALOG_STYLE
| wxDIALOG_MODAL
);
276 m_ok
= (dialog
.ShowModal () == wxID_OK
);
278 if (!m_ok
) return FALSE
;
280 if ((m_filename
== "") &&
281 (wxThePrintSetupData
->GetPrinterMode() == PS_PREVIEW
||
282 wxThePrintSetupData
->GetPrinterMode() == PS_PRINTER
))
286 wxThePrintSetupData
->SetPrinterFile("preview");
288 // For PS_PRINTER action this depends on a Unix-style print spooler
289 // since the wx_printer_file can be destroyed during a session
290 // @@@ TODO: a Windows-style answer for non-Unix
292 wxGetUserId (userId
, sizeof (userId
) / sizeof (char));
294 strcpy (tmp
, "/tmp/preview_");
295 strcat (tmp
, userId
);
296 wxThePrintSetupData
->SetPrinterFile(tmp
);
299 strcpy(tmp2
, wxThePrintSetupData
->GetPrinterFile());
300 strcat (tmp2
, ".ps");
301 wxThePrintSetupData
->SetPrinterFile(tmp2
);
304 else if ((m_filename
== "") && (wxThePrintSetupData
->GetPrinterMode() == PS_FILE
))
306 char *file
= wxSaveFileSelector (_("PostScript"), "ps");
312 wxThePrintSetupData
->SetPrinterFile(file
);
320 void wxPostScriptDC::SetClippingRegion (long x
, long y
, long w
, long h
)
322 wxCHECK_RET( m_ok
&& m_pstream
, "invalid postscript dc" );
324 if (m_clipping
) return;
326 wxDC::SetClippingRegion( x
, y
, w
, h
);
329 *m_pstream
<< "gsave\n"
331 << XLOG2DEV(x
) << " " << YLOG2DEV(y
) << " moveto\n"
332 << XLOG2DEV(x
+w
) << " " << YLOG2DEV(y
) << " lineto\n"
333 << XLOG2DEV(x
+w
) << " " << YLOG2DEV(y
+h
) << " lineto\n"
334 << XLOG2DEV(x
) << " " << YLOG2DEV(y
+h
) << " lineto\n"
335 << "closepath clip newpath\n";
338 void wxPostScriptDC::SetClippingRegion( const wxRegion
&WXUNUSED(region
) )
342 void wxPostScriptDC::DestroyClippingRegion()
344 wxCHECK_RET( m_ok
&& m_pstream
, "invalid postscript dc" );
346 wxDC::DestroyClippingRegion();
351 *m_pstream
<< "grestore\n";
355 void wxPostScriptDC::Clear()
357 wxFAIL_MSG( "wxPostScriptDC::Clear not implemented." );
360 void wxPostScriptDC::FloodFill (long WXUNUSED(x
), long WXUNUSED(y
), const wxColour
&WXUNUSED(col
), int WXUNUSED(style
))
362 wxFAIL_MSG( "wxPostScriptDC::FloodFill not implemented." );
365 bool wxPostScriptDC::GetPixel (long WXUNUSED(x
), long WXUNUSED(y
), wxColour
* WXUNUSED(col
)) const
367 wxFAIL_MSG( "wxPostScriptDC::GetPixel not implemented." );
371 void wxPostScriptDC::CrossHair (long WXUNUSED(x
), long WXUNUSED(y
))
373 wxFAIL_MSG( "wxPostScriptDC::CrossHair not implemented." );
376 void wxPostScriptDC::DrawLine (long x1
, long y1
, long x2
, long y2
)
378 wxCHECK_RET( m_ok
&& m_pstream
, "invalid postscript dc" );
380 if (m_pen
.GetStyle() == wxTRANSPARENT
) return;
384 *m_pstream
<< "newpath\n"
385 << XLOG2DEV(x1
) << " " << YLOG2DEV (y1
) << " moveto\n"
386 << XLOG2DEV(x2
) << " " << YLOG2DEV (y2
) << " lineto\n"
389 CalcBoundingBox( x1
, y1
);
390 CalcBoundingBox( x2
, y2
);
393 #define RAD2DEG 57.29577951308
395 void wxPostScriptDC::DrawArc (long x1
, long y1
, long x2
, long y2
, long xc
, long yc
)
397 wxCHECK_RET( m_ok
&& m_pstream
, "invalid postscript dc" );
401 long radius
= (long) sqrt(dx
*dx
+dy
*dy
);
402 double alpha1
, alpha2
;
404 if (x1
== x2
&& y1
== y2
)
408 } else if (radius
== 0.0)
410 alpha1
= alpha2
= 0.0;
413 alpha1
= (x1
- xc
== 0) ?
414 (y1
- yc
< 0) ? 90.0 : -90.0 :
415 -atan2(double(y1
-yc
), double(x1
-xc
)) * RAD2DEG
;
416 alpha2
= (x2
- xc
== 0) ?
417 (y2
- yc
< 0) ? 90.0 : -90.0 :
418 -atan2(double(y2
-yc
), double(x2
-xc
)) * RAD2DEG
;
420 while (alpha1
<= 0) alpha1
+= 360;
421 while (alpha2
<= 0) alpha2
+= 360; // adjust angles to be between
422 while (alpha1
> 360) alpha1
-= 360; // 0 and 360 degree
423 while (alpha2
> 360) alpha2
-= 360;
425 if (m_brush
.GetStyle() != wxTRANSPARENT
)
428 *m_pstream
<< "newpath\n"
429 << XLOG2DEV(xc
) << " "
430 << YLOG2DEV(yc
) << " "
431 << XLOG2DEVREL(radius
) << " "
432 << YLOG2DEVREL(radius
) << " "
434 << alpha2
<< " ellipse\n"
435 << XLOG2DEV(xc
) << " "
436 << YLOG2DEV(yc
) << " lineto\n"
441 if (m_pen
.GetStyle() != wxTRANSPARENT
)
444 *m_pstream
<< "newpath\n"
445 << XLOG2DEV(xc
) << " "
446 << YLOG2DEV(yc
) << " "
447 << XLOG2DEVREL(radius
) << " "
448 << YLOG2DEVREL(radius
) << " "
450 << alpha2
<< " ellipse\n"
454 CalcBoundingBox( xc
-radius
, yc
-radius
);
455 CalcBoundingBox( xc
+radius
, yc
+radius
);
458 void wxPostScriptDC::DrawEllipticArc(long x
,long y
,long w
,long h
,double sa
,double ea
)
460 wxCHECK_RET( m_ok
&& m_pstream
, "invalid postscript dc" );
462 if (sa
>=360 || sa
<=-360) sa
=sa
-int(sa
/360)*360;
463 if (ea
>=360 || ea
<=-360) ea
=ea
-int(ea
/360)*360;
469 DrawEllipse(x
,y
,w
,h
);
473 if (m_brush
.GetStyle () != wxTRANSPARENT
)
477 *m_pstream
<< "newpath\n"
478 << XLOG2DEV(x
+w
/2) << " " << YLOG2DEV(y
+h
/2) << " "
479 << XLOG2DEVREL(w
/2) << " " << YLOG2DEVREL(h
/2) << " "
480 << int(sa
) <<" "<< int(ea
) << " true ellipticarc\n";
482 CalcBoundingBox( x
,y
);
483 CalcBoundingBox( x
+w
, y
+h
);
486 if (m_pen
.GetStyle () != wxTRANSPARENT
)
490 *m_pstream
<< "newpath\n"
491 << XLOG2DEV(x
+w
/2) << " " << YLOG2DEV(y
+h
/2) << " "
492 << XLOG2DEVREL(w
/2) << " " << XLOG2DEVREL(h
/2) << " "
493 << int(sa
) <<" "<< int(ea
) << " false ellipticarc\n";
495 CalcBoundingBox( x
, y
);
496 CalcBoundingBox( x
+w
, y
+h
);
500 void wxPostScriptDC::DrawPoint (long x
, long y
)
502 wxCHECK_RET( m_ok
&& m_pstream
, "invalid postscript dc" );
504 if (m_pen
.GetStyle() == wxTRANSPARENT
) return;
508 *m_pstream
<< "newpath\n"
509 << XLOG2DEV(x
) << " " << YLOG2DEV (y
) << " moveto\n"
510 << XLOG2DEV(x
+1) << " " << YLOG2DEV (y
) << " lineto\n"
513 CalcBoundingBox( x
, y
);
516 void wxPostScriptDC::DrawPolygon (int n
, wxPoint points
[], long xoffset
, long yoffset
, int WXUNUSED(fillStyle
))
518 wxCHECK_RET( m_ok
&& m_pstream
, "invalid postscript dc" );
522 if (m_brush
.GetStyle () != wxTRANSPARENT
)
526 *m_pstream
<< "newpath\n";
528 long xx
= XLOG2DEV(points
[0].x
+ xoffset
);
529 long yy
= YLOG2DEV(points
[0].y
+ yoffset
);
530 *m_pstream
<< xx
<< " " << yy
<< " moveto\n";
531 CalcBoundingBox( points
[0].x
+ xoffset
, points
[0].y
+ yoffset
);
533 for (int i
= 1; i
< n
; i
++)
535 xx
= XLOG2DEV(points
[i
].x
+ xoffset
);
536 yy
= YLOG2DEV(points
[i
].y
+ yoffset
);
537 *m_pstream
<< xx
<< " " << yy
<< " lineto\n";
538 CalcBoundingBox( points
[i
].x
+ xoffset
, points
[i
].y
+ yoffset
);
540 *m_pstream
<< "fill\n";
543 if (m_pen
.GetStyle () != wxTRANSPARENT
)
547 *m_pstream
<< "newpath\n";
549 long xx
= XLOG2DEV(points
[0].x
+ xoffset
);
550 long yy
= YLOG2DEV(points
[0].y
+ yoffset
);
551 *m_pstream
<< xx
<< " " << yy
<< " moveto\n";
552 CalcBoundingBox( points
[0].x
+ xoffset
, points
[0].y
+ yoffset
);
554 for (int i
= 1; i
< n
; i
++)
556 xx
= XLOG2DEV(points
[i
].x
+ xoffset
);
557 yy
= YLOG2DEV(points
[i
].y
+ yoffset
);
558 *m_pstream
<< xx
<< " " << yy
<< " lineto\n";
559 CalcBoundingBox( points
[i
].x
+ xoffset
, points
[i
].y
+ yoffset
);
562 *m_pstream
<< "stroke\n";
566 void wxPostScriptDC::DrawLines (int n
, wxPoint points
[], long xoffset
, long yoffset
)
568 wxCHECK_RET( m_ok
&& m_pstream
, "invalid postscript dc" );
570 if (m_pen
.GetStyle() == wxTRANSPARENT
) return;
575 for (int i
=0; i
<n
; ++i
)
577 CalcBoundingBox( XLOG2DEV(points
[i
].x
+xoffset
), YLOG2DEV(points
[i
].y
+yoffset
));
580 *m_pstream
<< "newpath\n"
581 << XLOG2DEV(points
[0].x
+xoffset
) << " "
582 << YLOG2DEV(points
[0].y
+yoffset
) << " moveto\n";
584 for (int i
= 1; i
< n
; i
++)
586 *m_pstream
<< XLOG2DEV(points
[i
].x
+xoffset
) << " "
587 << YLOG2DEV(points
[i
].y
+yoffset
) << " lineto\n";
590 *m_pstream
<< "stroke\n";
593 void wxPostScriptDC::DrawRectangle (long x
, long y
, long width
, long height
)
595 wxCHECK_RET( m_ok
&& m_pstream
, "invalid postscript dc" );
597 if (m_brush
.GetStyle () != wxTRANSPARENT
)
601 *m_pstream
<< "newpath\n"
602 << XLOG2DEV(x
) << " " << YLOG2DEV(y
) << " moveto\n"
603 << XLOG2DEV(x
+ width
) << " " << YLOG2DEV(y
) << " lineto\n"
604 << XLOG2DEV(x
+ width
) << " " << YLOG2DEV(y
+ height
) << " lineto\n"
605 << XLOG2DEV(x
) << " " << YLOG2DEV(y
+ height
) << " lineto\n"
609 CalcBoundingBox( x
, y
);
610 CalcBoundingBox( x
+ width
, y
+ height
);
613 if (m_pen
.GetStyle () != wxTRANSPARENT
)
617 *m_pstream
<< "newpath\n"
618 << XLOG2DEV(x
) << " " << YLOG2DEV(y
) << " moveto\n"
619 << XLOG2DEV(x
+ width
) << " " << YLOG2DEV(y
) << " lineto\n"
620 << XLOG2DEV(x
+ width
) << " " << YLOG2DEV(y
+ height
) << " lineto\n"
621 << XLOG2DEV(x
) << " " << YLOG2DEV(y
+ height
) << " lineto\n"
625 CalcBoundingBox( x
, y
);
626 CalcBoundingBox( x
+ width
, y
+ height
);
630 void wxPostScriptDC::DrawRoundedRectangle (long x
, long y
, long width
, long height
, double radius
)
632 wxCHECK_RET( m_ok
&& m_pstream
, "invalid postscript dc" );
636 // Now, a negative radius is interpreted to mean
637 // 'the proportion of the smallest X or Y dimension'
638 double smallest
= 0.0;
643 radius
= (-radius
* smallest
);
646 long rad
= (long) radius
;
648 if (m_brush
.GetStyle () != wxTRANSPARENT
)
652 // Draw rectangle anticlockwise
653 *m_pstream
<< "newpath\n"
654 << XLOG2DEV(x
+ rad
) << " " << YLOG2DEV(y
+ rad
) << " " << XLOG2DEVREL(rad
) << " 90 180 arc\n"
655 << XLOG2DEV(x
) << " " << YLOG2DEV(y
+ rad
) << " moveto\n"
656 << XLOG2DEV(x
+ rad
) << " " << YLOG2DEV(y
+ height
- rad
) << " " << XLOG2DEVREL(rad
) << " 180 270 arc\n"
657 << XLOG2DEV(x
+ width
- rad
) << " " << YLOG2DEV(y
+ height
) << " lineto\n"
658 << XLOG2DEV(x
+ width
- rad
) << " " << YLOG2DEV(y
+ height
- rad
) << " " << XLOG2DEVREL(rad
) << " 270 0 arc\n"
659 << XLOG2DEV(x
+ width
) << " " << YLOG2DEV(y
+ rad
) << " lineto\n"
660 << XLOG2DEV(x
+ width
- rad
) << " " << YLOG2DEV(y
+ rad
) << " " << XLOG2DEVREL(rad
) << " 0 90 arc\n"
661 << XLOG2DEV(x
+ rad
) << " " << YLOG2DEV(y
) << " lineto\n"
665 CalcBoundingBox( x
, y
);
666 CalcBoundingBox( x
+ width
, y
+ height
);
669 if (m_pen
.GetStyle () != wxTRANSPARENT
)
673 // Draw rectangle anticlockwise
674 *m_pstream
<< "newpath\n"
675 << XLOG2DEV(x
+ rad
) << " " << YLOG2DEV(y
+ rad
) << " " << XLOG2DEVREL(rad
) << " 90 180 arc\n"
676 << XLOG2DEV(x
) << " " << YLOG2DEV(y
+ rad
) << " moveto\n"
677 << XLOG2DEV(x
+ rad
) << " " << YLOG2DEV(y
+ height
- rad
) << " " << XLOG2DEVREL(rad
) << " 180 270 arc\n"
678 << XLOG2DEV(x
+ width
- rad
) << " " << YLOG2DEV(y
+ height
) << " lineto\n"
679 << XLOG2DEV(x
+ width
- rad
) << " " << YLOG2DEV(y
+ height
- rad
) << " " << XLOG2DEVREL(rad
) << " 270 0 arc\n"
680 << XLOG2DEV(x
+ width
) << " " << YLOG2DEV(y
+ rad
) << " lineto\n"
681 << XLOG2DEV(x
+ width
- rad
) << " " << YLOG2DEV(y
+ rad
) << " " << XLOG2DEVREL(rad
) << " 0 90 arc\n"
682 << XLOG2DEV(x
+ rad
) << " " << YLOG2DEV(y
) << " lineto\n"
686 CalcBoundingBox( x
, y
);
687 CalcBoundingBox( x
+ width
, y
+ height
);
691 void wxPostScriptDC::DrawEllipse (long x
, long y
, long width
, long height
)
693 wxCHECK_RET( m_ok
&& m_pstream
, "invalid postscript dc" );
695 if (m_brush
.GetStyle () != wxTRANSPARENT
)
699 *m_pstream
<< "newpath\n"
700 << XLOG2DEV(x
+ width
/ 2) << " " << YLOG2DEV(y
+ height
/ 2) << " "
701 << XLOG2DEV(width
/ 2) << " " << YLOG2DEVREL(height
/ 2) << " 0 360 ellipse\n"
704 CalcBoundingBox( x
- width
, y
- height
);
705 CalcBoundingBox( x
+ width
, y
+ height
);
708 if (m_pen
.Ok() && m_pen
.GetStyle () != wxTRANSPARENT
)
712 *m_pstream
<< "newpath\n"
713 << XLOG2DEV(x
+ width
/ 2) << " " << YLOG2DEV(y
+ height
/ 2) << " "
714 << XLOG2DEV(width
/ 2) << " " << YLOG2DEVREL(height
/ 2) << " 0 360 ellipse\n"
717 CalcBoundingBox( x
- width
, y
- height
);
718 CalcBoundingBox( x
+ width
, y
+ height
);
722 void wxPostScriptDC::DrawIcon (const wxIcon
& icon
, long x
, long y
)
724 DrawBitmap( icon
, x
, y
, TRUE
);
727 void wxPostScriptDC::DrawBitmap( const wxBitmap
& bitmap
, long x
, long y
, bool WXUNUSED(useMask
) )
729 wxCHECK_RET( m_ok
&& m_pstream
, "invalid postscript dc" );
731 if (!bitmap
.Ok()) return;
733 wxImage
image( bitmap
);
735 if (!image
.Ok()) return;
737 int ww
= XLOG2DEVREL(image
.GetWidth());
738 int hh
= YLOG2DEVREL(image
.GetHeight());
740 image
= image
.Scale( ww
, hh
);
742 if (!image
.Ok()) return;
744 int xx
= XLOG2DEV(x
);
745 int yy
= YLOG2DEV(y
+ bitmap
.GetHeight());
747 *m_pstream
<< "/origstate save def\n"
749 << "/pix " << ww
<< " string def\n"
750 << "/grays " << ww
<< " string def\n"
751 << "/npixels 0 def\n"
752 << "/rgbindx 0 def\n"
753 << xx
<< " " << yy
<< " translate\n"
754 << ww
<< " " << hh
<< " scale\n"
755 << ww
<< " " << hh
<< " 8\n"
756 << "[" << ww
<< " 0 0 " << (-hh
) << " 0 " << hh
<< "]\n"
757 << "{currentfile pix readhexstring pop}\n"
758 << "false 3 colorimage\n";
760 for (int j
= 0; j
< hh
; j
++)
762 for (int i
= 0; i
< ww
; i
++)
766 wxDecToHex( image
.GetRed(i
,j
), buffer
);
767 *m_pstream
<< buffer
;
768 wxDecToHex( image
.GetGreen(i
,j
), buffer
);
769 *m_pstream
<< buffer
;
770 wxDecToHex( image
.GetBlue(i
,j
), buffer
);
771 *m_pstream
<< buffer
;
776 *m_pstream
<< "end\n";
777 *m_pstream
<< "origstate restore\n";
781 void wxPostScriptDC::SetFont (const wxFont
& font
)
783 wxCHECK_RET( m_ok
&& m_pstream
, "invalid postscript dc" );
785 if (!font
.Ok()) return;
789 char *name
= wxTheFontNameDirectory
->GetPostScriptName( m_font
.GetFamily(),
792 if (!name
) name
= "Times-Roman";
794 *m_pstream
<< "/" << name
<< " reencodeISO def\n"
795 << "/" << name
<< " findfont\n"
796 << YLOG2DEVREL(font
.GetPointSize())
797 << " scalefont setfont\n";
800 void wxPostScriptDC::SetPen( const wxPen
& pen
)
802 wxCHECK_RET( m_ok
&& m_pstream
, "invalid postscript dc" );
804 if (!pen
.Ok()) return;
806 int oldStyle
= m_pen
.GetStyle();
810 *m_pstream
<< XLOG2DEVREL(m_pen
.GetWidth()) << " setlinewidth\n";
813 Line style - WRONG: 2nd arg is OFFSET
815 Here, I'm afraid you do not conceive meaning of parameters of 'setdash'
816 operator correctly. You should look-up this in the Red Book: the 2nd parame-
817 ter is not number of values in the array of the first one, but an offset
818 into this description of the pattern. I mean a real *offset* not index
819 into array. I.e. If the command is [3 4] 1 setdash is used, then there
820 will be first black line *2* units long, then space 4 units, then the
821 pattern of *3* units black, 4 units space will be repeated.
824 static const char *dotted
= "[2 5] 2";
825 static const char *short_dashed
= "[4 4] 2";
826 static const char *long_dashed
= "[4 8] 2";
827 static const char *dotted_dashed
= "[6 6 2 6] 4";
829 const char *psdash
= (char *) NULL
;
830 switch (m_pen
.GetStyle ())
832 case wxDOT
: psdash
= dotted
; break;
833 case wxSHORT_DASH
: psdash
= short_dashed
; break;
834 case wxLONG_DASH
: psdash
= long_dashed
; break;
835 case wxDOT_DASH
: psdash
= dotted_dashed
; break;
838 default: psdash
= "[] 0"; break;
841 if (oldStyle
!= m_pen
.GetStyle())
843 *m_pstream
<< psdash
<< " setdash\n";
847 unsigned char red
= m_pen
.GetColour().Red();
848 unsigned char blue
= m_pen
.GetColour().Blue();
849 unsigned char green
= m_pen
.GetColour().Green();
853 // Anything not white is black
854 if (!(red
== (unsigned char) 255 && blue
== (unsigned char) 255
855 && green
== (unsigned char) 255))
857 red
= (unsigned char) 0;
858 green
= (unsigned char) 0;
859 blue
= (unsigned char) 0;
865 if (!(red
== m_currentRed
&& green
== m_currentGreen
&& blue
== m_currentBlue
))
867 long redPS
= (long) (((int) red
) / 255.0);
868 long bluePS
= (long) (((int) blue
) / 255.0);
869 long greenPS
= (long) (((int) green
) / 255.0);
871 *m_pstream
<< redPS
<< " " << greenPS
<< " " << bluePS
<< " setrgbcolor\n";
874 m_currentBlue
= blue
;
875 m_currentGreen
= green
;
879 void wxPostScriptDC::SetBrush( const wxBrush
& brush
)
881 wxCHECK_RET( m_ok
&& m_pstream
, "invalid postscript dc" );
883 if (!brush
.Ok()) return;
888 unsigned char red
= m_brush
.GetColour ().Red ();
889 unsigned char blue
= m_brush
.GetColour ().Blue ();
890 unsigned char green
= m_brush
.GetColour ().Green ();
894 // Anything not black is white
895 if (!(red
== (unsigned char) 0 && blue
== (unsigned char) 0
896 && green
== (unsigned char) 0))
898 red
= (unsigned char) 255;
899 green
= (unsigned char) 255;
900 blue
= (unsigned char) 255;
906 if (!(red
== m_currentRed
&& green
== m_currentGreen
&& blue
== m_currentBlue
))
908 long redPS
= (long) (((int) red
) / 255.0);
909 long bluePS
= (long) (((int) blue
) / 255.0);
910 long greenPS
= (long) (((int) green
) / 255.0);
911 *m_pstream
<< redPS
<< " " << greenPS
<< " " << bluePS
<< " setrgbcolor\n";
913 m_currentBlue
= blue
;
914 m_currentGreen
= green
;
918 void wxPostScriptDC::DrawText( const wxString
& text
, long x
, long y
, bool WXUNUSED(use16bit
) )
920 wxCHECK_RET( m_ok
&& m_pstream
, "invalid postscript dc" );
924 if (m_textForegroundColour
.Ok ())
926 unsigned char red
= m_textForegroundColour
.Red ();
927 unsigned char blue
= m_textForegroundColour
.Blue ();
928 unsigned char green
= m_textForegroundColour
.Green ();
932 // Anything not white is black
933 if (!(red
== (unsigned char) 255 && blue
== (unsigned char) 255
934 && green
== (unsigned char) 255))
936 red
= (unsigned char) 0;
937 green
= (unsigned char) 0;
938 blue
= (unsigned char) 0;
942 // maybe setgray here ?
944 if (!(red
== m_currentRed
&& green
== m_currentGreen
&& blue
== m_currentBlue
))
946 long redPS
= (long) (((int) red
) / 255.0);
947 long bluePS
= (long) (((int) blue
) / 255.0);
948 long greenPS
= (long) (((int) green
) / 255.0);
949 *m_pstream
<< redPS
<< " " << greenPS
<< " " << bluePS
<< " setrgbcolor\n";
952 m_currentBlue
= blue
;
953 m_currentGreen
= green
;
957 int size
= m_font
.GetPointSize();
959 long by
= y
+ (long)floor( float(size
) * 2.0 / 3.0 ); // approximate baseline
960 *m_pstream
<< XLOG2DEV(x
) << " " << YLOG2DEV(by
) << " moveto\n";
963 int len
= strlen ((char *)(const char *)text
);
965 for (i
= 0; i
< len
; i
++)
967 int c
= (unsigned char) text
[i
];
968 if ( c
== ')' || c
== '(' || c
== '\\')
970 *m_pstream
<< "\\" << (char) c
;
974 // Cope with character codes > 127
976 sprintf(tmp
, "\\%o", c
);
980 *m_pstream
<< (char) c
;
983 *m_pstream
<< ")" << " show\n";
985 if (m_font
.GetUnderlined())
987 long uy
= (long)(y
+ size
- m_underlinePosition
);
989 GetTextExtent(text
, &w
, &h
);
991 *m_pstream
<< "gsave " << XLOG2DEV(x
) << " " << YLOG2DEV(uy
)
993 << (long)m_underlineThickness
<< " setlinewidth "
994 << XLOG2DEV(x
+ w
) << " " << YLOG2DEV(uy
)
995 << " lineto stroke grestore\n";
998 CalcBoundingBox( x
, y
);
999 CalcBoundingBox( x
+ size
* text
.Length() * 2/3 , y
);
1003 void wxPostScriptDC::SetBackground (const wxBrush
& brush
)
1005 m_backgroundBrush
= brush
;
1008 void wxPostScriptDC::SetLogicalFunction (int WXUNUSED(function
))
1010 wxFAIL_MSG( "wxPostScriptDC::SetLogicalFunction not implemented." );
1013 void wxPostScriptDC::DrawSpline( wxList
*points
)
1015 wxCHECK_RET( m_ok
&& m_pstream
, "invalid postscript dc" );
1019 double a
, b
, c
, d
, x1
, y1
, x2
, y2
, x3
, y3
;
1022 wxNode
*node
= points
->First();
1023 p
= (wxPoint
*)node
->Data();
1027 node
= node
->Next();
1028 p
= (wxPoint
*)node
->Data();
1031 x3
= a
= (double)(x1
+ c
) / 2;
1032 y3
= b
= (double)(y1
+ d
) / 2;
1034 *m_pstream
<< "newpath "
1035 << XLOG2DEV((long)x1
) << " " << YLOG2DEV((long)y1
) << " moveto "
1036 << XLOG2DEV((long)x3
) << " " << YLOG2DEV((long)y3
) << " lineto\n";
1038 CalcBoundingBox( (long)x1
, (long)y1
);
1039 CalcBoundingBox( (long)x3
, (long)y3
);
1041 while ((node
= node
->Next()) != NULL
)
1043 q
= (wxPoint
*)node
->Data();
1051 x3
= (double)(x2
+ c
) / 2;
1052 y3
= (double)(y2
+ d
) / 2;
1053 *m_pstream
<< XLOG2DEV((long)x1
) << " " << YLOG2DEV((long)y1
) << " "
1054 << XLOG2DEV((long)x2
) << " " << YLOG2DEV((long)y2
) << " "
1055 << XLOG2DEV((long)x3
) << " " << YLOG2DEV((long)y3
) << " DrawSplineSection\n";
1057 CalcBoundingBox( (long)x1
, (long)y1
);
1058 CalcBoundingBox( (long)x3
, (long)y3
);
1062 At this point, (x2,y2) and (c,d) are the position of the
1063 next-to-last and last point respectively, in the point list
1066 *m_pstream
<< XLOG2DEV((long)c
) << " " << YLOG2DEV((long)d
) << " lineto stroke\n";
1069 long wxPostScriptDC::GetCharWidth ()
1071 // Chris Breeze: reasonable approximation using wxMODERN/Courier
1072 return (long) (GetCharHeight() * 72.0 / 120.0);
1076 void wxPostScriptDC::SetAxisOrientation( bool xLeftRight
, bool yBottomUp
)
1078 wxCHECK_RET( m_ok
&& m_pstream
, "invalid postscript dc" );
1080 m_signX
= (xLeftRight
? 1 : -1);
1081 m_signY
= (yBottomUp
? 1 : -1);
1083 ComputeScaleAndOrigin();
1086 void wxPostScriptDC::SetDeviceOrigin( long x
, long y
)
1088 wxCHECK_RET( m_ok
&& m_pstream
, "invalid postscript dc" );
1094 wxDC::SetDeviceOrigin( x
, h
-y
);
1097 void wxPostScriptDC::GetSize(int* width
, int* height
) const
1099 const char *paperType
= wxThePrintSetupData
->GetPaperName();
1101 if (!paperType
) paperType
= _("A4 210 x 297 mm");
1103 wxPrintPaperType
*paper
= wxThePrintPaperDatabase
->FindPaperType(paperType
);
1105 if (!paper
) paper
= wxThePrintPaperDatabase
->FindPaperType(_("A4 210 x 297 mm"));
1109 if (width
) *width
= paper
->widthPixels
;
1110 if (height
) *height
= paper
->heightPixels
;
1114 if (width
) *width
= 595;
1115 if (height
) *height
= 842;
1119 bool wxPostScriptDC::StartDoc (const wxString
& message
)
1121 wxCHECK_MSG( m_ok
, FALSE
, "invalid postscript dc" );
1123 if (m_filename
== "")
1125 m_filename
= wxGetTempFileName("ps");
1126 wxThePrintSetupData
->SetPrinterFile((char *)(const char *)m_filename
);
1131 wxThePrintSetupData
->SetPrinterFile((char *)(const char *)m_filename
);
1134 m_pstream
= new ofstream (wxThePrintSetupData
->GetPrinterFile());
1136 if (!m_pstream
|| !m_pstream
->good())
1138 wxMessageBox (_("Cannot open file!"), _("Error"), wxOK
);
1145 SetBrush( *wxBLACK_BRUSH
);
1146 SetPen( *wxBLACK_PEN
);
1147 SetBackground( *wxWHITE_BRUSH
);
1148 SetTextForeground( *wxBLACK
);
1150 // set origin according to paper size
1151 SetDeviceOrigin( 0,0 );
1159 void wxPostScriptDC::EndDoc ()
1161 wxCHECK_RET( m_ok
&& m_pstream
, "invalid postscript dc" );
1166 *m_pstream
<< "grestore\n";
1172 m_pstream
= (ofstream
*) NULL
;
1175 char *header_file
= wxGetTempFileName("ps");
1177 m_pstream
= new ofstream( header_file
);
1179 *m_pstream
<< "%!PS-Adobe-2.0\n"; /* PostScript magic strings */
1180 *m_pstream
<< "%%Title: " << (const char *) m_title
<< "\n";
1181 *m_pstream
<< "%%Creator: " << wxTheApp
->argv
[0] << "\n";
1182 *m_pstream
<< "%%CreationDate: " << wxNow() << "\n";
1185 if ( wxGetEmailAddress(userID
, sizeof(userID
)) )
1187 *m_pstream
<< "%%For: " << (char *)userID
;
1189 if (wxGetUserName(userName
, sizeof(userName
)))
1190 *m_pstream
<< " (" << (char *)userName
<< ")";
1193 else if ( wxGetUserName(userID
, sizeof(userID
)) )
1195 *m_pstream
<< "%%For: " << (char *)userID
<< "\n";
1198 // THE FOLLOWING HAS BEEN CONTRIBUTED BY Andy Fyfe <andy@hyperparallel.com>
1200 long wx_printer_translate_x
, wx_printer_translate_y
;
1201 double wx_printer_scale_x
, wx_printer_scale_y
;
1202 wxThePrintSetupData
->GetPrinterTranslation(&wx_printer_translate_x
, &wx_printer_translate_y
);
1203 wxThePrintSetupData
->GetPrinterScaling(&wx_printer_scale_x
, &wx_printer_scale_y
);
1205 if (wxThePrintSetupData
->GetPrinterOrientation() == PS_LANDSCAPE
)
1207 *m_pstream
<< "%%Orientation: Landscape\n";
1211 *m_pstream
<< "%%Orientation: Portrait\n";
1214 // Compute the bounding box. Note that it is in the default user
1215 // coordinate system, thus we have to convert the values.
1216 long llx
= (long) ((XLOG2DEV(m_minX
)+wx_printer_translate_x
)*wx_printer_scale_x
);
1217 long lly
= (long) ((YLOG2DEV(m_minY
)+wx_printer_translate_y
)*wx_printer_scale_y
);
1218 long urx
= (long) ((XLOG2DEV(m_maxX
)+wx_printer_translate_x
)*wx_printer_scale_x
);
1219 long ury
= (long) ((YLOG2DEV(m_maxY
)+wx_printer_translate_y
)*wx_printer_scale_y
);
1221 // If we're landscape, our sense of "x" and "y" is reversed.
1222 if (wxThePrintSetupData
->GetPrinterOrientation() == PS_LANDSCAPE
)
1225 tmp
= llx
; llx
= lly
; lly
= tmp
;
1226 tmp
= urx
; urx
= ury
; ury
= tmp
;
1228 // We need either the two lines that follow, or we need to subtract
1229 // min_x from real_translate_y, which is commented out below.
1230 llx
= llx
- (long)(m_minX
*wx_printer_scale_y
);
1231 urx
= urx
- (long)(m_minX
*wx_printer_scale_y
);
1234 // The Adobe specifications call for integers; we round as to make
1235 // the bounding larger.
1236 *m_pstream
<< "%%BoundingBox: "
1237 << floor((double)llx
) << " " << floor((double)lly
) << " "
1238 << ceil((double)urx
) << " " << ceil((double)ury
) << "\n";
1239 *m_pstream
<< "%%Pages: " << (wxPageNumber
- 1) << "\n";
1240 *m_pstream
<< "%%EndComments\n\n";
1242 // To check the correctness of the bounding box, postscript commands
1243 // to draw a box corresponding to the bounding box are generated below.
1244 // But since we typically don't want to print such a box, the postscript
1245 // commands are generated within comments. These lines appear before any
1246 // adjustment of scale, rotation, or translation, and hence are in the
1247 // default user coordinates.
1248 *m_pstream
<< "% newpath\n";
1249 *m_pstream
<< "% " << llx
<< " " << lly
<< " moveto\n";
1250 *m_pstream
<< "% " << urx
<< " " << lly
<< " lineto\n";
1251 *m_pstream
<< "% " << urx
<< " " << ury
<< " lineto\n";
1252 *m_pstream
<< "% " << llx
<< " " << ury
<< " lineto closepath stroke\n";
1254 *m_pstream
<< "%%BeginProlog\n";
1255 *m_pstream
<< wxPostScriptHeaderEllipse
;
1256 *m_pstream
<< wxPostScriptHeaderEllipticArc
;
1257 *m_pstream
<< wxPostScriptHeaderColourImage
;
1258 *m_pstream
<< wxPostScriptHeaderReencodeISO1
;
1259 *m_pstream
<< wxPostScriptHeaderReencodeISO2
;
1261 if (wxPostScriptHeaderSpline
)
1262 *m_pstream
<< wxPostScriptHeaderSpline
;
1263 *m_pstream
<< "%%EndProlog\n";
1266 m_pstream
= (ofstream
*) NULL
;
1268 char *tmp_file
= wxGetTempFileName("ps");
1270 // Paste header Before wx_printer_file
1271 wxConcatFiles (header_file
, wxThePrintSetupData
->GetPrinterFile(), tmp_file
);
1272 wxRemoveFile (header_file
);
1273 wxRemoveFile (wxThePrintSetupData
->GetPrinterFile());
1274 wxRenameFile(tmp_file
, wxThePrintSetupData
->GetPrinterFile());
1276 #if defined(__X__) || defined(__WXGTK__)
1279 switch (wxThePrintSetupData
->GetPrinterMode()) {
1283 argv
[0] = wxThePrintSetupData
->GetPrintPreviewCommand();
1284 argv
[1] = wxThePrintSetupData
->GetPrinterFile();
1285 argv
[2] = (char *) NULL
;
1286 wxExecute (argv
, TRUE
);
1287 wxRemoveFile(wxThePrintSetupData
->GetPrinterFile());
1295 argv
[argc
++] = wxThePrintSetupData
->GetPrinterCommand();
1297 // !SM! If we simply assign to argv[1] here, if printer options
1298 // are blank, we get an annoying and confusing message from lpr.
1299 char * opts
= wxThePrintSetupData
->GetPrinterOptions();
1301 argv
[argc
++] = opts
;
1303 argv
[argc
++] = wxThePrintSetupData
->GetPrinterFile();
1304 argv
[argc
++] = (char *) NULL
;
1305 wxExecute (argv
, TRUE
);
1306 wxRemoveFile(wxThePrintSetupData
->GetPrinterFile());
1317 void wxPostScriptDC::StartPage ()
1319 wxCHECK_RET( m_ok
&& m_pstream
, "invalid postscript dc" );
1321 *m_pstream
<< "%%Page: " << (wxPageNumber
++) << "\n";
1323 // *m_pstream << "matrix currentmatrix\n";
1325 // Added by Chris Breeze
1327 // Each page starts with an "initgraphics" which resets the
1328 // transformation and so we need to reset the origin
1329 // (and rotate the page for landscape printing)
1332 m_scaleFactor = 1.0;
1333 m_logicalOriginX = 0;
1334 m_logicalOriginY = 0;
1338 long translate_x
, translate_y
;
1339 double scale_x
, scale_y
;
1340 wxThePrintSetupData
->GetPrinterTranslation(&translate_x
, &translate_y
);
1341 wxThePrintSetupData
->GetPrinterScaling(&scale_x
, &scale_y
);
1343 if (wxThePrintSetupData
->GetPrinterOrientation() == PS_LANDSCAPE
)
1345 translate_y
-= m_maxY
;
1346 *m_pstream
<< "90 rotate\n";
1349 *m_pstream
<< scale_x
<< " " << scale_y
<< " scale\n";
1350 *m_pstream
<< translate_x
<< " " << translate_y
<< " translate\n";
1353 void wxPostScriptDC::EndPage ()
1355 wxCHECK_RET( m_ok
&& m_pstream
, "invalid postscript dc" );
1357 *m_pstream
<< "showpage\n";
1360 bool wxPostScriptDC::Blit( long WXUNUSED(xdest
), long WXUNUSED(ydest
),
1361 long WXUNUSED(fwidth
), long WXUNUSED(fheight
),
1362 wxDC
*WXUNUSED(source
),
1363 long WXUNUSED(xsrc
), long WXUNUSED(ysrc
),
1364 int WXUNUSED(rop
), bool WXUNUSED(useMask
) )
1366 wxCHECK_MSG( m_ok
&& m_pstream
, FALSE
, "invalid postscript dc" );
1368 wxFAIL_MSG( "wxPostScriptDC::Blit no yet implemented." );
1373 long wxPostScriptDC::GetCharHeight ()
1376 return m_font
.GetPointSize();
1381 void wxPostScriptDC::GetTextExtent (const wxString
& string
, long *x
, long *y
,
1382 long *descent
, long *externalLeading
, wxFont
*theFont
,
1383 bool WXUNUSED(use16
))
1385 // if (!m_pstream) return;
1387 wxFont
*fontToUse
= theFont
;
1389 if (!fontToUse
) fontToUse
= (wxFont
*) &m_font
;
1391 #if !USE_AFM_FOR_POSTSCRIPT
1392 // Provide a VERY rough estimate (avoid using it)
1393 // Chris Breeze 5/11/97: produces accurate results for mono-spaced
1394 // font such as Courier (aka wxMODERN)
1398 height
= fontToUse
->GetPointSize();
1400 *x
= strlen (string
) * height
* 72 / 120;
1401 *y
= (long) (height
* 1.32); // allow for descender
1405 if (externalLeading
)
1406 *externalLeading
= 0;
1408 // +++++ start of contributed code +++++
1410 // ************************************************************
1411 // method for calculating string widths in postscript:
1412 // read in the AFM (adobe font metrics) file for the
1413 // actual font, parse it and extract the character widths
1414 // and also the descender. this may be improved, but for now
1415 // it works well. the AFM file is only read in if the
1416 // font is changed. this may be chached in the future.
1417 // calls to GetTextExtent with the font unchanged are rather
1420 // for each font and style used there is an AFM file necessary.
1421 // currently i have only files for the roman font family.
1422 // i try to get files for the other ones!
1424 // CAVE: the size of the string is currently always calculated
1425 // in 'points' (1/72 of an inch). this should later on be
1426 // changed to depend on the mapping mode.
1427 // CAVE: the path to the AFM files must be set before calling this
1428 // function. this is usually done by a call like the following:
1429 // wxSetAFMPath("d:\\wxw161\\afm\\");
1433 // wxPostScriptDC dc(NULL, TRUE);
1435 // wxSetAFMPath("d:\\wxw161\\afm\\");
1436 // dc.StartDoc("Test");
1439 // dc.SetFont(new wxFont(10, wxROMAN, wxNORMAL, wxNORMAL));
1440 // dc.GetTextExtent("Hallo",&w,&h);
1445 // by steve (stefan.hammes@urz.uni-heidelberg.de)
1446 // created: 10.09.94
1447 // updated: 14.05.95
1449 assert(fontToUse
&& "void wxPostScriptDC::GetTextExtent: no font defined");
1450 assert(x
&& "void wxPostScriptDC::GetTextExtent: x == NULL");
1451 assert(y
&& "void wxPostScriptDC::GetTextExtent: y == NULL");
1453 // these static vars are for storing the state between calls
1454 static int lastFamily
= INT_MIN
;
1455 static int lastSize
= INT_MIN
;
1456 static int lastStyle
= INT_MIN
;
1457 static int lastWeight
= INT_MIN
;
1458 static int lastDescender
= INT_MIN
;
1459 static int lastWidths
[256]; // widths of the characters
1461 // get actual parameters
1462 const int Family
= fontToUse
->GetFamily();
1463 const int Size
= fontToUse
->GetPointSize();
1464 const int Style
= fontToUse
->GetStyle();
1465 const int Weight
= fontToUse
->GetWeight();
1467 // if we have another font, read the font-metrics
1468 if(Family
!=lastFamily
||Size
!=lastSize
||Style
!=lastStyle
||Weight
!=lastWeight
){
1469 // store actual values
1470 lastFamily
= Family
;
1473 lastWeight
= Weight
;
1475 // read in new font metrics **************************************
1477 // 1. construct filename ******************************************
1478 /* MATTHEW: [2] Use wxTheFontNameDirectory */
1481 // Julian - we'll need to do this a different way now we've removed the
1482 // font directory system. Must find Stefan's original code.
1484 name
= wxTheFontNameDirectory
->GetAFMName(Family
, Weight
, Style
);
1488 // get the directory of the AFM files
1492 strcpy(afmName
,wxGetAFMPath());
1494 // 2. open and process the file **********************************
1496 // a short explanation of the AFM format:
1497 // we have for each character a line, which gives its size
1500 // C 63 ; WX 444 ; N question ; B 49 -14 395 676 ;
1502 // that means, we have a character with ascii code 63, and width
1503 // (444/1000 * fontSize) points.
1504 // the other data is ignored for now!
1506 // when the font has changed, we read in the right AFM file and store the
1507 // character widths in an array, which is processed below (see point 3.).
1509 // new elements JC Sun Aug 25 23:21:44 MET DST 1996
1512 strcat(afmName
,name
);
1513 strcat(afmName
,".afm");
1514 FILE *afmFile
= fopen(afmName
,"r");
1516 wxLogDebug("GetTextExtent: can't open AFM file '%s'\n",afmName
);
1517 wxLogDebug(" using approximate values\n");
1519 for (i
=0; i
<256; i
++) lastWidths
[i
] = 500; // an approximate value
1520 lastDescender
= -150; // dito.
1523 // init the widths array
1524 for(i
=0; i
<256; i
++) lastWidths
[i
]= INT_MIN
;
1525 // some variables for holding parts of a line
1526 char cString
[10],semiString
[10],WXString
[10],descString
[20];
1527 char upString
[30], utString
[30], encString
[50];
1530 // read in the file and parse it
1531 while(fgets(line
,sizeof(line
),afmFile
)!=NULL
){
1532 // A.) check for descender definition
1533 if(strncmp(line
,"Descender",9)==0){
1534 if((sscanf(line
,"%s%d",descString
,&lastDescender
)!=2)
1535 || (strcmp(descString
,"Descender")!=0)) {
1536 wxLogDebug("AFM-file '%s': line '%s' has error (bad descender)\n",
1540 // JC 1.) check for UnderlinePosition
1541 else if(strncmp(line
,"UnderlinePosition",17)==0){
1542 if((sscanf(line
,"%s%lf",upString
,&UnderlinePosition
)!=2)
1543 || (strcmp(upString
,"UnderlinePosition")!=0)) {
1544 wxLogDebug("AFM-file '%s': line '%s' has error (bad UnderlinePosition)\n",
1548 // JC 2.) check for UnderlineThickness
1549 else if(strncmp(line
,"UnderlineThickness",18)==0){
1550 if((sscanf(line
,"%s%lf",utString
,&UnderlineThickness
)!=2)
1551 || (strcmp(utString
,"UnderlineThickness")!=0)) {
1552 wxLogDebug("AFM-file '%s': line '%s' has error (bad UnderlineThickness)\n",
1556 // JC 3.) check for EncodingScheme
1557 else if(strncmp(line
,"EncodingScheme",14)==0){
1558 if((sscanf(line
,"%s%s",utString
,encString
)!=2)
1559 || (strcmp(utString
,"EncodingScheme")!=0)) {
1560 wxLogDebug("AFM-file '%s': line '%s' has error (bad EncodingScheme)\n",
1563 else if (strncmp(encString
, "AdobeStandardEncoding", 21))
1565 wxLogDebug("AFM-file '%s': line '%s' has error (unsupported EncodingScheme %s)\n",
1566 afmName
,line
, encString
);
1569 // B.) check for char-width
1570 else if(strncmp(line
,"C ",2)==0){
1571 if(sscanf(line
,"%s%d%s%s%d",
1572 cString
,&ascii
,semiString
,WXString
,&cWidth
)!=5){
1573 wxLogDebug("AFM-file '%s': line '%s' has an error (bad character width)\n",afmName
,line
);
1575 if(strcmp(cString
,"C")!=0 || strcmp(semiString
,";")!=0 ||
1576 strcmp(WXString
,"WX")!=0){
1577 wxLogDebug("AFM-file '%s': line '%s' has a format error\n",afmName
,line
);
1579 //printf(" char '%c'=%d has width '%d'\n",ascii,ascii,cWidth);
1580 if(ascii
>=0 && ascii
<256){
1581 lastWidths
[ascii
] = cWidth
; // store width
1583 /* MATTHEW: this happens a lot; don't print an error */
1584 // wxLogDebug("AFM-file '%s': ASCII value %d out of range\n",afmName,ascii);
1587 // C.) ignore other entries.
1591 // hack to compute correct values for german 'Umlaute'
1592 // the correct way would be to map the character names
1593 // like 'adieresis' to corresp. positions of ISOEnc and read
1594 // these values from AFM files, too. Maybe later ...
1595 lastWidths
[196] = lastWidths
['A']; // Ä
1596 lastWidths
[228] = lastWidths
['a']; // ä
1597 lastWidths
[214] = lastWidths
['O']; // Ö
1598 lastWidths
[246] = lastWidths
['o']; // ö
1599 lastWidths
[220] = lastWidths
['U']; // Ü
1600 lastWidths
[252] = lastWidths
['u']; // ü
1601 lastWidths
[223] = lastWidths
[251]; // ß
1604 // JC: calculate UnderlineThickness/UnderlinePosition
1605 m_underlinePosition
= m_underlinePosition
* fontToUse
->GetPointSize() / 1000.0f
;
1606 m_underlineThickness
= m_underlineThickness
* fontToUse
->GetPointSize() / 1000.0f
* m_scaleFactor
;
1608 // 3. now the font metrics are read in, calc size *******************
1609 // this is done by adding the widths of the characters in the
1610 // string. they are given in 1/1000 of the size!
1613 long height
=Size
; // by default
1615 for(p
=(unsigned char *)(const char *)string
; *p
; p
++){
1616 if(lastWidths
[*p
]== INT_MIN
){
1617 wxLogDebug("GetTextExtent: undefined width for character '%c' (%d)\n",
1619 widthSum
+= (long)(lastWidths
[' ']/1000.0F
* Size
); // assume space
1621 widthSum
+= (long)((lastWidths
[*p
]/1000.0F
)*Size
);
1624 // add descender to height (it is usually a negative value)
1625 if(lastDescender
!=INT_MIN
){
1626 height
+= (long)(((-lastDescender
)/1000.0F
) * Size
); /* MATTHEW: forgot scale */
1629 // return size values
1633 // return other parameters
1635 if(lastDescender
!=INT_MIN
){
1636 *descent
= (long)(((-lastDescender
)/1000.0F
) * Size
); /* MATTHEW: forgot scale */
1642 // currently no idea how to calculate this!
1643 // if (externalLeading) *externalLeading = 0;
1644 if (externalLeading
)
1645 *externalLeading
= 0;
1647 // ----- end of contributed code -----
1651 void wxPostScriptDC::GetSizeMM(long *width
, long *height
) const
1653 const char *paperType
= wxThePrintSetupData
->GetPaperName();
1655 if (!paperType
) paperType
= _("A4 210 x 297 mm");
1657 wxPrintPaperType
*paper
= wxThePrintPaperDatabase
->FindPaperType(paperType
);
1659 if (!paper
) paper
= wxThePrintPaperDatabase
->FindPaperType(_("A4 210 x 297 mm"));
1663 if (width
) *width
= paper
->widthMM
;
1664 if (height
) *height
= paper
->heightMM
;
1668 if (width
) *width
= 210;
1669 if (height
) *height
= 297;