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"
25 //-----------------------------------------------------------------------------
26 // start and end of document/page
27 //-----------------------------------------------------------------------------
29 static const char *wxPostScriptHeaderEllipse
= "\
30 /ellipsedict 8 dict def\n\
31 ellipsedict /mtrx matrix put\n\
35 /startangle exch def\n\
40 /savematrix mtrx currentmatrix def\n\
43 0 0 1 startangle endangle arc\n\
44 savematrix setmatrix\n\
49 static const char *wxPostScriptHeaderEllipticArc
= "\
50 /ellipticarcdict 8 dict def\n\
51 ellipticarcdict /mtrx matrix put\n\
53 { ellipticarcdict begin\n\
56 /startangle exch def\n\
61 /savematrix mtrx currentmatrix def\n\
64 do_fill { 0 0 moveto } if\n\
65 0 0 1 startangle endangle arc\n\
66 savematrix setmatrix\n\
67 do_fill { fill }{ stroke } ifelse\n\
71 static const char *wxPostScriptHeaderSpline
= "\
72 /DrawSplineSection {\n\
79 /xa x1 x2 x1 sub 0.666667 mul add def\n\
80 /ya y1 y2 y1 sub 0.666667 mul add def\n\
81 /xb x3 x2 x3 sub 0.666667 mul add def\n\
82 /yb y3 y2 y3 sub 0.666667 mul add def\n\
84 xa ya xb yb x3 y3 curveto\n\
88 static const char *wxPostScriptHeaderColourImage
= "\
89 % define 'colorimage' if it isn't defined\n\
90 % ('colortogray' and 'mergeprocs' come from xwd2ps\n\
92 /colorimage where % do we know about 'colorimage'?\n\
93 { pop } % yes: pop off the 'dict' returned\n\
95 /colortogray { % define an RGB->I function\n\
96 /rgbdata exch store % call input 'rgbdata'\n\
97 rgbdata length 3 idiv\n\
100 0 1 npixls 1 sub {\n\
102 rgbdata rgbindx get 20 mul % Red\n\
103 rgbdata rgbindx 1 add get 32 mul % Green\n\
104 rgbdata rgbindx 2 add get 12 mul % Blue\n\
105 add add 64 idiv % I = .5G + .31R + .18B\n\
107 /rgbindx rgbindx 3 add store\n\
109 grays 0 npixls getinterval\n\
112 % Utility procedure for colorimage operator.\n\
113 % This procedure takes two procedures off the\n\
114 % stack and merges them into a single procedure.\n\
116 /mergeprocs { % def\n\
135 /colorimage { % def\n\
136 pop pop % remove 'false 3' operands\n\
137 {colortogray} mergeprocs\n\
140 } ifelse % end of 'false' case\n\
143 static char wxPostScriptHeaderReencodeISO1
[] =
145 "dup dup findfont dup length dict begin\n"
146 "{ 1 index /FID ne { def }{ pop pop } ifelse } forall\n"
147 "/Encoding ISOLatin1Encoding def\n"
148 "currentdict end definefont\n"
150 "/ISOLatin1Encoding [\n"
151 "/.notdef/.notdef/.notdef/.notdef/.notdef/.notdef/.notdef/.notdef\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 "/space/exclam/quotedbl/numbersign/dollar/percent/ampersand/quoteright\n"
156 "/parenleft/parenright/asterisk/plus/comma/minus/period/slash\n"
157 "/zero/one/two/three/four/five/six/seven/eight/nine/colon/semicolon\n"
158 "/less/equal/greater/question/at/A/B/C/D/E/F/G/H/I/J/K/L/M/N\n"
159 "/O/P/Q/R/S/T/U/V/W/X/Y/Z/bracketleft/backslash/bracketright\n"
160 "/asciicircum/underscore/quoteleft/a/b/c/d/e/f/g/h/i/j/k/l/m\n"
161 "/n/o/p/q/r/s/t/u/v/w/x/y/z/braceleft/bar/braceright/asciitilde\n"
162 "/.notdef/.notdef/.notdef/.notdef/.notdef/.notdef/.notdef/.notdef\n"
163 "/.notdef/.notdef/.notdef/.notdef/.notdef/.notdef/.notdef/.notdef\n"
164 "/.notdef/dotlessi/grave/acute/circumflex/tilde/macron/breve\n"
165 "/dotaccent/dieresis/.notdef/ring/cedilla/.notdef/hungarumlaut\n";
167 static char wxPostScriptHeaderReencodeISO2
[] =
168 "/ogonek/caron/space/exclamdown/cent/sterling/currency/yen/brokenbar\n"
169 "/section/dieresis/copyright/ordfeminine/guillemotleft/logicalnot\n"
170 "/hyphen/registered/macron/degree/plusminus/twosuperior/threesuperior\n"
171 "/acute/mu/paragraph/periodcentered/cedilla/onesuperior/ordmasculine\n"
172 "/guillemotright/onequarter/onehalf/threequarters/questiondown\n"
173 "/Agrave/Aacute/Acircumflex/Atilde/Adieresis/Aring/AE/Ccedilla\n"
174 "/Egrave/Eacute/Ecircumflex/Edieresis/Igrave/Iacute/Icircumflex\n"
175 "/Idieresis/Eth/Ntilde/Ograve/Oacute/Ocircumflex/Otilde/Odieresis\n"
176 "/multiply/Oslash/Ugrave/Uacute/Ucircumflex/Udieresis/Yacute\n"
177 "/Thorn/germandbls/agrave/aacute/acircumflex/atilde/adieresis\n"
178 "/aring/ae/ccedilla/egrave/eacute/ecircumflex/edieresis/igrave\n"
179 "/iacute/icircumflex/idieresis/eth/ntilde/ograve/oacute/ocircumflex\n"
180 "/otilde/odieresis/divide/oslash/ugrave/uacute/ucircumflex/udieresis\n"
181 "/yacute/thorn/ydieresis\n"
184 //-------------------------------------------------------------------------------
186 //-------------------------------------------------------------------------------
188 wxPostScriptDC::wxPostScriptDC ()
190 m_pstream
= (ofstream
*) NULL
;
200 m_underlinePosition
= 0.0;
201 m_underlineThickness
= 0.0;
203 m_signX
= 1; // default x-axis left to right
204 m_signY
= -1; // default y-axis bottom up -> top down
207 wxPostScriptDC::wxPostScriptDC (const wxString
& file
, bool interactive
, wxWindow
*parent
)
209 m_pstream
= (ofstream
*) NULL
;
219 m_underlinePosition
= 0.0;
220 m_underlineThickness
= 0.0;
222 m_signX
= 1; // default x-axis left to right
223 m_signY
= -1; // default y-axis bottom up -> top down
225 Create(file
, interactive
, parent
);
228 bool wxPostScriptDC::Create(const wxString
& file
, bool interactive
, wxWindow
*parent
)
230 m_isInteractive
= interactive
;
236 // Can only send to file in Windows
237 wxThePrintSetupData
->SetPrinterMode(PS_FILE
);
242 if ((m_ok
= PrinterDialog (parent
) ) == FALSE
) return FALSE
;
252 wxPostScriptDC::~wxPostScriptDC ()
254 if (m_pstream
) delete m_pstream
;
257 bool wxPostScriptDC::Ok() const
259 return (m_ok
&& m_pstream
);
262 bool wxPostScriptDC::PrinterDialog(wxWindow
*parent
)
264 wxPostScriptPrintDialog
dialog( parent
, _("Printer Settings"), wxPoint(150, 150), wxSize(400, 400),
265 wxDEFAULT_DIALOG_STYLE
| wxDIALOG_MODAL
);
266 m_ok
= (dialog
.ShowModal () == wxID_OK
);
268 if (!m_ok
) return FALSE
;
270 if ((m_filename
== "") &&
271 (wxThePrintSetupData
->GetPrinterMode() == PS_PREVIEW
||
272 wxThePrintSetupData
->GetPrinterMode() == PS_PRINTER
))
276 wxThePrintSetupData
->SetPrinterFile("preview");
278 // For PS_PRINTER action this depends on a Unix-style print spooler
279 // since the wx_printer_file can be destroyed during a session
280 // @@@ TODO: a Windows-style answer for non-Unix
282 wxGetUserId (userId
, sizeof (userId
) / sizeof (char));
284 strcpy (tmp
, "/tmp/preview_");
285 strcat (tmp
, userId
);
286 wxThePrintSetupData
->SetPrinterFile(tmp
);
289 strcpy(tmp2
, wxThePrintSetupData
->GetPrinterFile());
290 strcat (tmp2
, ".ps");
291 wxThePrintSetupData
->SetPrinterFile(tmp2
);
294 else if ((m_filename
== "") && (wxThePrintSetupData
->GetPrinterMode() == PS_FILE
))
296 char *file
= wxSaveFileSelector (_("PostScript"), "ps");
302 wxThePrintSetupData
->SetPrinterFile(file
);
310 void wxPostScriptDC::SetClippingRegion (long x
, long y
, long w
, long h
)
312 wxCHECK_RET( Ok(), "invalid postscript dc" );
314 if (m_clipping
) return;
316 wxDC::SetClippingRegion( x
, y
, w
, h
);
319 *m_pstream
<< "gsave\n"
321 << XLOG2DEV(x
) << " " << YLOG2DEV(y
) << " moveto\n"
322 << XLOG2DEV(x
+w
) << " " << YLOG2DEV(y
) << " lineto\n"
323 << XLOG2DEV(x
+w
) << " " << YLOG2DEV(y
+h
) << " lineto\n"
324 << XLOG2DEV(x
) << " " << YLOG2DEV(y
+h
) << " lineto\n"
325 << "closepath clip newpath\n";
328 void wxPostScriptDC::SetClippingRegion( const wxRegion
&WXUNUSED(region
) )
332 void wxPostScriptDC::DestroyClippingRegion()
334 wxCHECK_RET( Ok(), "invalid postscript dc" );
336 wxDC::DestroyClippingRegion();
341 *m_pstream
<< "grestore\n";
345 void wxPostScriptDC::Clear()
347 wxFAIL_MSG( "wxPostScriptDC::Clear not implemented." );
350 void wxPostScriptDC::FloodFill (long WXUNUSED(x
), long WXUNUSED(y
), const wxColour
&WXUNUSED(col
), int WXUNUSED(style
))
352 wxFAIL_MSG( "wxPostScriptDC::FloodFill not implemented." );
355 bool wxPostScriptDC::GetPixel (long WXUNUSED(x
), long WXUNUSED(y
), wxColour
* WXUNUSED(col
)) const
357 wxFAIL_MSG( "wxPostScriptDC::GetPixel not implemented." );
361 void wxPostScriptDC::CrossHair (long WXUNUSED(x
), long WXUNUSED(y
))
363 wxFAIL_MSG( "wxPostScriptDC::CrossHair not implemented." );
366 void wxPostScriptDC::DrawLine (long x1
, long y1
, long x2
, long y2
)
368 wxCHECK_RET( Ok(), "invalid postscript dc" );
370 if (m_pen
.GetStyle() == wxTRANSPARENT
) return;
374 *m_pstream
<< "newpath\n"
375 << XLOG2DEV(x1
) << " " << YLOG2DEV (y1
) << " moveto\n"
376 << XLOG2DEV(x2
) << " " << YLOG2DEV (y2
) << " lineto\n"
379 CalcBoundingBox( x1
, y1
);
380 CalcBoundingBox( x2
, y2
);
383 #define RAD2DEG 57.29577951308
385 void wxPostScriptDC::DrawArc (long x1
, long y1
, long x2
, long y2
, long xc
, long yc
)
387 wxCHECK_RET( Ok(), "invalid postscript dc" );
391 long radius
= (long) sqrt(dx
*dx
+dy
*dy
);
392 double alpha1
, alpha2
;
394 if (x1
== x2
&& y1
== y2
)
398 } else if (radius
== 0.0)
400 alpha1
= alpha2
= 0.0;
403 alpha1
= (x1
- xc
== 0) ?
404 (y1
- yc
< 0) ? 90.0 : -90.0 :
405 -atan2(double(y1
-yc
), double(x1
-xc
)) * RAD2DEG
;
406 alpha2
= (x2
- xc
== 0) ?
407 (y2
- yc
< 0) ? 90.0 : -90.0 :
408 -atan2(double(y2
-yc
), double(x2
-xc
)) * RAD2DEG
;
410 while (alpha1
<= 0) alpha1
+= 360;
411 while (alpha2
<= 0) alpha2
+= 360; // adjust angles to be between
412 while (alpha1
> 360) alpha1
-= 360; // 0 and 360 degree
413 while (alpha2
> 360) alpha2
-= 360;
415 if (m_brush
.GetStyle() != wxTRANSPARENT
)
418 *m_pstream
<< "newpath\n"
419 << XLOG2DEV(xc
) << " "
420 << YLOG2DEV(yc
) << " "
421 << XLOG2DEVREL(radius
) << " "
422 << YLOG2DEVREL(radius
) << " "
424 << alpha2
<< " ellipse\n"
425 << XLOG2DEV(xc
) << " "
426 << YLOG2DEV(yc
) << " lineto\n"
431 if (m_pen
.GetStyle() != wxTRANSPARENT
)
434 *m_pstream
<< "newpath\n"
435 << XLOG2DEV(xc
) << " "
436 << YLOG2DEV(yc
) << " "
437 << XLOG2DEVREL(radius
) << " "
438 << YLOG2DEVREL(radius
) << " "
440 << alpha2
<< " ellipse\n"
444 CalcBoundingBox( xc
-radius
, yc
-radius
);
445 CalcBoundingBox( xc
+radius
, yc
+radius
);
448 void wxPostScriptDC::DrawEllipticArc(long x
,long y
,long w
,long h
,double sa
,double ea
)
450 wxCHECK_RET( Ok(), "invalid postscript dc" );
452 if (sa
>=360 || sa
<=-360) sa
=sa
-int(sa
/360)*360;
453 if (ea
>=360 || ea
<=-360) ea
=ea
-int(ea
/360)*360;
459 DrawEllipse(x
,y
,w
,h
);
463 if (m_brush
.GetStyle () != wxTRANSPARENT
)
467 *m_pstream
<< "newpath\n"
468 << XLOG2DEV(x
+w
/2) << " " << YLOG2DEV(y
+h
/2) << " "
469 << XLOG2DEVREL(w
/2) << " " << YLOG2DEVREL(h
/2) << " "
470 << int(sa
) <<" "<< int(ea
) << " true ellipticarc\n";
472 CalcBoundingBox( x
,y
);
473 CalcBoundingBox( x
+w
, y
+h
);
476 if (m_pen
.GetStyle () != wxTRANSPARENT
)
480 *m_pstream
<< "newpath\n"
481 << XLOG2DEV(x
+w
/2) << " " << YLOG2DEV(y
+h
/2) << " "
482 << XLOG2DEVREL(w
/2) << " " << XLOG2DEVREL(h
/2) << " "
483 << int(sa
) <<" "<< int(ea
) << " false ellipticarc\n";
485 CalcBoundingBox( x
, y
);
486 CalcBoundingBox( x
+w
, y
+h
);
490 void wxPostScriptDC::DrawPoint (long x
, long y
)
492 wxCHECK_RET( Ok(), "invalid postscript dc" );
494 if (m_pen
.GetStyle() == wxTRANSPARENT
) return;
498 *m_pstream
<< "newpath\n"
499 << XLOG2DEV(x
) << " " << YLOG2DEV (y
) << " moveto\n"
500 << XLOG2DEV(x
+1) << " " << YLOG2DEV (y
) << " lineto\n"
503 CalcBoundingBox( x
, y
);
506 void wxPostScriptDC::DrawPolygon (int n
, wxPoint points
[], long xoffset
, long yoffset
, int WXUNUSED(fillStyle
))
508 wxCHECK_RET( Ok(), "invalid postscript dc" );
512 if (m_brush
.GetStyle () != wxTRANSPARENT
)
516 *m_pstream
<< "newpath\n";
518 long xx
= XLOG2DEV(points
[0].x
+ xoffset
);
519 long yy
= YLOG2DEV(points
[0].y
+ yoffset
);
520 *m_pstream
<< xx
<< " " << yy
<< " moveto\n";
521 CalcBoundingBox( points
[0].x
+ xoffset
, points
[0].y
+ yoffset
);
523 for (int i
= 1; i
< n
; i
++)
525 xx
= XLOG2DEV(points
[i
].x
+ xoffset
);
526 yy
= YLOG2DEV(points
[i
].y
+ yoffset
);
527 *m_pstream
<< xx
<< " " << yy
<< " lineto\n";
528 CalcBoundingBox( points
[i
].x
+ xoffset
, points
[i
].y
+ yoffset
);
530 *m_pstream
<< "fill\n";
533 if (m_pen
.GetStyle () != wxTRANSPARENT
)
537 *m_pstream
<< "newpath\n";
539 long xx
= XLOG2DEV(points
[0].x
+ xoffset
);
540 long yy
= YLOG2DEV(points
[0].y
+ yoffset
);
541 *m_pstream
<< xx
<< " " << yy
<< " moveto\n";
542 CalcBoundingBox( points
[0].x
+ xoffset
, points
[0].y
+ yoffset
);
544 for (int i
= 1; i
< n
; i
++)
546 xx
= XLOG2DEV(points
[i
].x
+ xoffset
);
547 yy
= YLOG2DEV(points
[i
].y
+ yoffset
);
548 *m_pstream
<< xx
<< " " << yy
<< " lineto\n";
549 CalcBoundingBox( points
[i
].x
+ xoffset
, points
[i
].y
+ yoffset
);
552 *m_pstream
<< "stroke\n";
556 void wxPostScriptDC::DrawLines (int n
, wxPoint points
[], long xoffset
, long yoffset
)
558 wxCHECK_RET( Ok(), "invalid postscript dc" );
560 if (m_pen
.GetStyle() == wxTRANSPARENT
) return;
565 for (int i
=0; i
<n
; ++i
)
567 CalcBoundingBox( XLOG2DEV(points
[i
].x
+xoffset
), YLOG2DEV(points
[i
].y
+yoffset
));
570 *m_pstream
<< "newpath\n"
571 << XLOG2DEV(points
[0].x
+xoffset
) << " "
572 << YLOG2DEV(points
[0].y
+yoffset
) << " moveto\n";
574 for (int i
= 1; i
< n
; i
++)
576 *m_pstream
<< XLOG2DEV(points
[i
].x
+xoffset
) << " "
577 << YLOG2DEV(points
[i
].y
+yoffset
) << " lineto\n";
580 *m_pstream
<< "stroke\n";
583 void wxPostScriptDC::DrawRectangle (long x
, long y
, long width
, long height
)
585 wxCHECK_RET( Ok(), "invalid postscript dc" );
587 if (m_brush
.GetStyle () != wxTRANSPARENT
)
591 *m_pstream
<< "newpath\n"
592 << XLOG2DEV(x
) << " " << YLOG2DEV(y
) << " moveto\n"
593 << XLOG2DEV(x
+ width
) << " " << YLOG2DEV(y
) << " lineto\n"
594 << XLOG2DEV(x
+ width
) << " " << YLOG2DEV(y
+ height
) << " lineto\n"
595 << XLOG2DEV(x
) << " " << YLOG2DEV(y
+ height
) << " lineto\n"
599 CalcBoundingBox( x
, y
);
600 CalcBoundingBox( x
+ width
, y
+ height
);
603 if (m_pen
.GetStyle () != wxTRANSPARENT
)
607 *m_pstream
<< "newpath\n"
608 << XLOG2DEV(x
) << " " << YLOG2DEV(y
) << " moveto\n"
609 << XLOG2DEV(x
+ width
) << " " << YLOG2DEV(y
) << " lineto\n"
610 << XLOG2DEV(x
+ width
) << " " << YLOG2DEV(y
+ height
) << " lineto\n"
611 << XLOG2DEV(x
) << " " << YLOG2DEV(y
+ height
) << " lineto\n"
615 CalcBoundingBox( x
, y
);
616 CalcBoundingBox( x
+ width
, y
+ height
);
620 void wxPostScriptDC::DrawRoundedRectangle (long x
, long y
, long width
, long height
, double radius
)
622 wxCHECK_RET( Ok(), "invalid postscript dc" );
626 // Now, a negative radius is interpreted to mean
627 // 'the proportion of the smallest X or Y dimension'
628 double smallest
= 0.0;
633 radius
= (-radius
* smallest
);
636 long rad
= (long) radius
;
638 if (m_brush
.GetStyle () != wxTRANSPARENT
)
642 // Draw rectangle anticlockwise
643 *m_pstream
<< "newpath\n"
644 << XLOG2DEV(x
+ rad
) << " " << YLOG2DEV(y
+ rad
) << " " << XLOG2DEVREL(rad
) << " 90 180 arc\n"
645 << XLOG2DEV(x
) << " " << YLOG2DEV(y
+ rad
) << " moveto\n"
646 << XLOG2DEV(x
+ rad
) << " " << YLOG2DEV(y
+ height
- rad
) << " " << XLOG2DEVREL(rad
) << " 180 270 arc\n"
647 << XLOG2DEV(x
+ width
- rad
) << " " << YLOG2DEV(y
+ height
) << " lineto\n"
648 << XLOG2DEV(x
+ width
- rad
) << " " << YLOG2DEV(y
+ height
- rad
) << " " << XLOG2DEVREL(rad
) << " 270 0 arc\n"
649 << XLOG2DEV(x
+ width
) << " " << YLOG2DEV(y
+ rad
) << " lineto\n"
650 << XLOG2DEV(x
+ width
- rad
) << " " << YLOG2DEV(y
+ rad
) << " " << XLOG2DEVREL(rad
) << " 0 90 arc\n"
651 << XLOG2DEV(x
+ rad
) << " " << YLOG2DEV(y
) << " lineto\n"
655 CalcBoundingBox( x
, y
);
656 CalcBoundingBox( x
+ width
, y
+ height
);
659 if (m_pen
.GetStyle () != wxTRANSPARENT
)
663 // Draw rectangle anticlockwise
664 *m_pstream
<< "newpath\n"
665 << XLOG2DEV(x
+ rad
) << " " << YLOG2DEV(y
+ rad
) << " " << XLOG2DEVREL(rad
) << " 90 180 arc\n"
666 << XLOG2DEV(x
) << " " << YLOG2DEV(y
+ rad
) << " moveto\n"
667 << XLOG2DEV(x
+ rad
) << " " << YLOG2DEV(y
+ height
- rad
) << " " << XLOG2DEVREL(rad
) << " 180 270 arc\n"
668 << XLOG2DEV(x
+ width
- rad
) << " " << YLOG2DEV(y
+ height
) << " lineto\n"
669 << XLOG2DEV(x
+ width
- rad
) << " " << YLOG2DEV(y
+ height
- rad
) << " " << XLOG2DEVREL(rad
) << " 270 0 arc\n"
670 << XLOG2DEV(x
+ width
) << " " << YLOG2DEV(y
+ rad
) << " lineto\n"
671 << XLOG2DEV(x
+ width
- rad
) << " " << YLOG2DEV(y
+ rad
) << " " << XLOG2DEVREL(rad
) << " 0 90 arc\n"
672 << XLOG2DEV(x
+ rad
) << " " << YLOG2DEV(y
) << " lineto\n"
676 CalcBoundingBox( x
, y
);
677 CalcBoundingBox( x
+ width
, y
+ height
);
681 void wxPostScriptDC::DrawEllipse (long x
, long y
, long width
, long height
)
683 wxCHECK_RET( Ok(), "invalid postscript dc" );
685 if (m_brush
.GetStyle () != wxTRANSPARENT
)
689 *m_pstream
<< "newpath\n"
690 << XLOG2DEV(x
+ width
/ 2) << " " << YLOG2DEV(y
+ height
/ 2) << " "
691 << XLOG2DEV(width
/ 2) << " " << YLOG2DEVREL(height
/ 2) << " 0 360 ellipse\n"
694 CalcBoundingBox( x
- width
, y
- height
);
695 CalcBoundingBox( x
+ width
, y
+ height
);
698 if (m_pen
.Ok() && m_pen
.GetStyle () != wxTRANSPARENT
)
702 *m_pstream
<< "newpath\n"
703 << XLOG2DEV(x
+ width
/ 2) << " " << YLOG2DEV(y
+ height
/ 2) << " "
704 << XLOG2DEV(width
/ 2) << " " << YLOG2DEVREL(height
/ 2) << " 0 360 ellipse\n"
707 CalcBoundingBox( x
- width
, y
- height
);
708 CalcBoundingBox( x
+ width
, y
+ height
);
712 void wxPostScriptDC::DrawIcon (const wxIcon
& icon
, long x
, long y
)
714 wxCHECK_RET( Ok(), "invalid postscript dc" );
717 memDC
.SelectObject( icon
);
718 Blit(x
, y
, icon
.GetWidth(), icon
.GetHeight(), &memDC
, 0, 0);
721 void wxPostScriptDC::DrawBitmap( const wxBitmap
& bitmap
, long x
, long y
, bool useMask
)
723 wxCHECK_RET( Ok(), "invalid postscript dc" );
726 memDC
.SelectObject( bitmap
);
727 Blit( x
, y
, bitmap
.GetWidth(), bitmap
.GetHeight(), &memDC
, 0, 0, useMask
);
730 void wxPostScriptDC::SetFont (const wxFont
& font
)
732 wxCHECK_RET( Ok(), "invalid postscript dc" );
734 if (!font
.Ok()) return;
738 char *name
= wxTheFontNameDirectory
->GetPostScriptName( m_font
.GetFamily(),
741 if (!name
) name
= "Times-Roman";
743 *m_pstream
<< "/" << name
<< " reencodeISO def\n"
744 << "/" << name
<< " findfont\n"
745 << YLOG2DEVREL(font
.GetPointSize())
746 << " scalefont setfont\n";
749 void wxPostScriptDC::SetPen( const wxPen
& pen
)
751 wxCHECK_RET( Ok(), "invalid postscript dc" );
753 if (!pen
.Ok()) return;
755 int oldStyle
= m_pen
.GetStyle();
759 *m_pstream
<< XLOG2DEVREL(m_pen
.GetWidth()) << " setlinewidth\n";
762 Line style - WRONG: 2nd arg is OFFSET
764 Here, I'm afraid you do not conceive meaning of parameters of 'setdash'
765 operator correctly. You should look-up this in the Red Book: the 2nd parame-
766 ter is not number of values in the array of the first one, but an offset
767 into this description of the pattern. I mean a real *offset* not index
768 into array. I.e. If the command is [3 4] 1 setdash is used, then there
769 will be first black line *2* units long, then space 4 units, then the
770 pattern of *3* units black, 4 units space will be repeated.
773 static const char *dotted
= "[2 5] 2";
774 static const char *short_dashed
= "[4 4] 2";
775 static const char *long_dashed
= "[4 8] 2";
776 static const char *dotted_dashed
= "[6 6 2 6] 4";
778 const char *psdash
= (char *) NULL
;
779 switch (m_pen
.GetStyle ())
781 case wxDOT
: psdash
= dotted
; break;
782 case wxSHORT_DASH
: psdash
= short_dashed
; break;
783 case wxLONG_DASH
: psdash
= long_dashed
; break;
784 case wxDOT_DASH
: psdash
= dotted_dashed
; break;
787 default: psdash
= "[] 0"; break;
790 if (oldStyle
!= m_pen
.GetStyle())
792 *m_pstream
<< psdash
<< " setdash\n";
796 unsigned char red
= m_pen
.GetColour().Red();
797 unsigned char blue
= m_pen
.GetColour().Blue();
798 unsigned char green
= m_pen
.GetColour().Green();
802 // Anything not white is black
803 if (!(red
== (unsigned char) 255 && blue
== (unsigned char) 255
804 && green
== (unsigned char) 255))
806 red
= (unsigned char) 0;
807 green
= (unsigned char) 0;
808 blue
= (unsigned char) 0;
814 if (!(red
== m_currentRed
&& green
== m_currentGreen
&& blue
== m_currentBlue
))
816 long redPS
= (long) (((int) red
) / 255.0);
817 long bluePS
= (long) (((int) blue
) / 255.0);
818 long greenPS
= (long) (((int) green
) / 255.0);
820 *m_pstream
<< redPS
<< " " << greenPS
<< " " << bluePS
<< " setrgbcolor\n";
823 m_currentBlue
= blue
;
824 m_currentGreen
= green
;
828 void wxPostScriptDC::SetBrush( const wxBrush
& brush
)
830 wxCHECK_RET( Ok(), "invalid postscript dc" );
832 if (!brush
.Ok()) return;
837 unsigned char red
= m_brush
.GetColour ().Red ();
838 unsigned char blue
= m_brush
.GetColour ().Blue ();
839 unsigned char green
= m_brush
.GetColour ().Green ();
843 // Anything not black is white
844 if (!(red
== (unsigned char) 0 && blue
== (unsigned char) 0
845 && green
== (unsigned char) 0))
847 red
= (unsigned char) 255;
848 green
= (unsigned char) 255;
849 blue
= (unsigned char) 255;
855 if (!(red
== m_currentRed
&& green
== m_currentGreen
&& blue
== m_currentBlue
))
857 long redPS
= (long) (((int) red
) / 255.0);
858 long bluePS
= (long) (((int) blue
) / 255.0);
859 long greenPS
= (long) (((int) green
) / 255.0);
860 *m_pstream
<< redPS
<< " " << greenPS
<< " " << bluePS
<< " setrgbcolor\n";
862 m_currentBlue
= blue
;
863 m_currentGreen
= green
;
867 void wxPostScriptDC::DrawText( const wxString
& text
, long x
, long y
, bool WXUNUSED(use16bit
) )
869 wxCHECK_RET( Ok(), "invalid postscript dc" );
873 if (m_textForegroundColour
.Ok ())
875 unsigned char red
= m_textForegroundColour
.Red ();
876 unsigned char blue
= m_textForegroundColour
.Blue ();
877 unsigned char green
= m_textForegroundColour
.Green ();
881 // Anything not white is black
882 if (!(red
== (unsigned char) 255 && blue
== (unsigned char) 255
883 && green
== (unsigned char) 255))
885 red
= (unsigned char) 0;
886 green
= (unsigned char) 0;
887 blue
= (unsigned char) 0;
891 // maybe setgray here ?
893 if (!(red
== m_currentRed
&& green
== m_currentGreen
&& blue
== m_currentBlue
))
895 long redPS
= (long) (((int) red
) / 255.0);
896 long bluePS
= (long) (((int) blue
) / 255.0);
897 long greenPS
= (long) (((int) green
) / 255.0);
898 *m_pstream
<< redPS
<< " " << greenPS
<< " " << bluePS
<< " setrgbcolor\n";
901 m_currentBlue
= blue
;
902 m_currentGreen
= green
;
906 int size
= m_font
.GetPointSize();
908 long by
= y
+ (long)floor( float(size
) * 2.0 / 3.0 ); // approximate baseline
909 *m_pstream
<< XLOG2DEV(x
) << " " << YLOG2DEV(by
) << " moveto\n";
912 int len
= strlen ((char *)(const char *)text
);
914 for (i
= 0; i
< len
; i
++)
916 int c
= (unsigned char) text
[i
];
917 if ( c
== ')' || c
== '(' || c
== '\\')
919 *m_pstream
<< "\\" << (char) c
;
923 // Cope with character codes > 127
925 sprintf(tmp
, "\\%o", c
);
929 *m_pstream
<< (char) c
;
932 *m_pstream
<< ")" << " show\n";
934 if (m_font
.GetUnderlined())
936 long uy
= (long)(y
+ size
- m_underlinePosition
);
938 GetTextExtent(text
, &w
, &h
);
940 *m_pstream
<< "gsave " << XLOG2DEV(x
) << " " << YLOG2DEV(uy
)
942 << (long)m_underlineThickness
<< " setlinewidth "
943 << XLOG2DEV(x
+ w
) << " " << YLOG2DEV(uy
)
944 << " lineto stroke grestore\n";
947 CalcBoundingBox( x
, y
);
948 CalcBoundingBox( x
+ size
* text
.Length() * 2/3 , y
);
952 void wxPostScriptDC::SetBackground (const wxBrush
& brush
)
954 m_backgroundBrush
= brush
;
957 void wxPostScriptDC::SetLogicalFunction (int WXUNUSED(function
))
959 wxFAIL_MSG( "wxPostScriptDC::SetLogicalFunction not implemented." );
962 void wxPostScriptDC::DrawSpline( wxList
*points
)
964 wxCHECK_RET( Ok(), "invalid postscript dc" );
968 double a
, b
, c
, d
, x1
, y1
, x2
, y2
, x3
, y3
;
971 wxNode
*node
= points
->First();
972 p
= (wxPoint
*)node
->Data();
977 p
= (wxPoint
*)node
->Data();
980 x3
= a
= (double)(x1
+ c
) / 2;
981 y3
= b
= (double)(y1
+ d
) / 2;
983 *m_pstream
<< "newpath "
984 << XLOG2DEV((long)x1
) << " " << YLOG2DEV((long)y1
) << " moveto "
985 << XLOG2DEV((long)x3
) << " " << YLOG2DEV((long)y3
) << " lineto\n";
987 CalcBoundingBox( (long)x1
, (long)y1
);
988 CalcBoundingBox( (long)x3
, (long)y3
);
990 while ((node
= node
->Next()) != NULL
)
992 q
= (wxPoint
*)node
->Data();
1000 x3
= (double)(x2
+ c
) / 2;
1001 y3
= (double)(y2
+ d
) / 2;
1002 *m_pstream
<< XLOG2DEV((long)x1
) << " " << YLOG2DEV((long)y1
) << " "
1003 << XLOG2DEV((long)x2
) << " " << YLOG2DEV((long)y2
) << " "
1004 << XLOG2DEV((long)x3
) << " " << YLOG2DEV((long)y3
) << " DrawSplineSection\n";
1006 CalcBoundingBox( (long)x1
, (long)y1
);
1007 CalcBoundingBox( (long)x3
, (long)y3
);
1011 At this point, (x2,y2) and (c,d) are the position of the
1012 next-to-last and last point respectively, in the point list
1015 *m_pstream
<< XLOG2DEV((long)c
) << " " << YLOG2DEV((long)d
) << " lineto stroke\n";
1018 long wxPostScriptDC::GetCharWidth ()
1020 // Chris Breeze: reasonable approximation using wxMODERN/Courier
1021 return (long) (GetCharHeight() * 72.0 / 120.0);
1025 void wxPostScriptDC::SetAxisOrientation( bool xLeftRight
, bool yBottomUp
)
1027 wxCHECK_RET( Ok(), "invalid postscript dc" );
1029 m_signX
= (xLeftRight
? 1 : -1);
1030 m_signY
= (yBottomUp
? 1 : -1);
1032 ComputeScaleAndOrigin();
1035 void wxPostScriptDC::SetDeviceOrigin( long x
, long y
)
1037 wxCHECK_RET( Ok(), "invalid postscript dc" );
1043 wxDC::SetDeviceOrigin( x
, h
-y
);
1046 void wxPostScriptDC::GetSize(int* width
, int* height
) const
1048 wxCHECK_RET( Ok(), "invalid postscript dc" );
1050 const char *paperType
= wxThePrintSetupData
->GetPaperName();
1052 if (!paperType
) paperType
= _("A4 210 x 297 mm");
1054 wxPrintPaperType
*paper
= wxThePrintPaperDatabase
->FindPaperType(paperType
);
1056 if (!paper
) paper
= wxThePrintPaperDatabase
->FindPaperType(_("A4 210 x 297 mm"));
1060 if (width
) *width
= paper
->widthPixels
;
1061 if (height
) *height
= paper
->heightPixels
;
1065 if (width
) *width
= 595;
1066 if (height
) *height
= 842;
1070 bool wxPostScriptDC::StartDoc (const wxString
& message
)
1072 wxCHECK_MSG( Ok(), FALSE
, "invalid postscript dc" );
1074 if (m_filename
== "")
1076 m_filename
= wxGetTempFileName("ps");
1077 wxThePrintSetupData
->SetPrinterFile((char *)(const char *)m_filename
);
1082 wxThePrintSetupData
->SetPrinterFile((char *)(const char *)m_filename
);
1085 m_pstream
= new ofstream (wxThePrintSetupData
->GetPrinterFile());
1087 if (!m_pstream
|| !m_pstream
->good())
1089 wxMessageBox (_("Cannot open file!"), _("Error"), wxOK
);
1096 SetBrush( *wxBLACK_BRUSH
);
1097 SetPen( *wxBLACK_PEN
);
1098 SetBackground( *wxWHITE_BRUSH
);
1099 SetTextForeground( *wxBLACK
);
1101 // set origin according to paper size
1102 SetDeviceOrigin( 0,0 );
1110 void wxPostScriptDC::EndDoc ()
1112 wxCHECK_RET( Ok(), "invalid postscript dc" );
1117 *m_pstream
<< "grestore\n";
1123 m_pstream
= (ofstream
*) NULL
;
1126 char *header_file
= wxGetTempFileName("ps");
1128 m_pstream
= new ofstream( header_file
);
1130 *m_pstream
<< "%!PS-Adobe-2.0\n"; /* PostScript magic strings */
1131 *m_pstream
<< "%%Title: " << (const char *) m_title
<< "\n";
1132 *m_pstream
<< "%%Creator: " << wxTheApp
->argv
[0] << "\n";
1133 *m_pstream
<< "%%CreationDate: " << wxNow() << "\n";
1136 if ( wxGetEmailAddress(userID
, sizeof(userID
)) )
1138 *m_pstream
<< "%%For: " << (char *)userID
;
1140 if (wxGetUserName(userName
, sizeof(userName
)))
1141 *m_pstream
<< " (" << (char *)userName
<< ")";
1144 else if ( wxGetUserName(userID
, sizeof(userID
)) )
1146 *m_pstream
<< "%%For: " << (char *)userID
<< "\n";
1149 // THE FOLLOWING HAS BEEN CONTRIBUTED BY Andy Fyfe <andy@hyperparallel.com>
1151 long wx_printer_translate_x
, wx_printer_translate_y
;
1152 double wx_printer_scale_x
, wx_printer_scale_y
;
1153 wxThePrintSetupData
->GetPrinterTranslation(&wx_printer_translate_x
, &wx_printer_translate_y
);
1154 wxThePrintSetupData
->GetPrinterScaling(&wx_printer_scale_x
, &wx_printer_scale_y
);
1156 if (wxThePrintSetupData
->GetPrinterOrientation() == PS_LANDSCAPE
)
1158 *m_pstream
<< "%%Orientation: Landscape\n";
1162 *m_pstream
<< "%%Orientation: Portrait\n";
1165 // Compute the bounding box. Note that it is in the default user
1166 // coordinate system, thus we have to convert the values.
1167 long llx
= (long) ((XLOG2DEV(m_minX
)+wx_printer_translate_x
)*wx_printer_scale_x
);
1168 long lly
= (long) ((YLOG2DEV(m_minY
)+wx_printer_translate_y
)*wx_printer_scale_y
);
1169 long urx
= (long) ((XLOG2DEV(m_maxX
)+wx_printer_translate_x
)*wx_printer_scale_x
);
1170 long ury
= (long) ((YLOG2DEV(m_maxY
)+wx_printer_translate_y
)*wx_printer_scale_y
);
1172 // If we're landscape, our sense of "x" and "y" is reversed.
1173 if (wxThePrintSetupData
->GetPrinterOrientation() == PS_LANDSCAPE
)
1176 tmp
= llx
; llx
= lly
; lly
= tmp
;
1177 tmp
= urx
; urx
= ury
; ury
= tmp
;
1179 // We need either the two lines that follow, or we need to subtract
1180 // min_x from real_translate_y, which is commented out below.
1181 llx
= llx
- (long)(m_minX
*wx_printer_scale_y
);
1182 urx
= urx
- (long)(m_minX
*wx_printer_scale_y
);
1185 // The Adobe specifications call for integers; we round as to make
1186 // the bounding larger.
1187 *m_pstream
<< "%%BoundingBox: "
1188 << floor((double)llx
) << " " << floor((double)lly
) << " "
1189 << ceil((double)urx
) << " " << ceil((double)ury
) << "\n";
1190 *m_pstream
<< "%%Pages: " << (wxPageNumber
- 1) << "\n";
1191 *m_pstream
<< "%%EndComments\n\n";
1193 // To check the correctness of the bounding box, postscript commands
1194 // to draw a box corresponding to the bounding box are generated below.
1195 // But since we typically don't want to print such a box, the postscript
1196 // commands are generated within comments. These lines appear before any
1197 // adjustment of scale, rotation, or translation, and hence are in the
1198 // default user coordinates.
1199 *m_pstream
<< "% newpath\n";
1200 *m_pstream
<< "% " << llx
<< " " << lly
<< " moveto\n";
1201 *m_pstream
<< "% " << urx
<< " " << lly
<< " lineto\n";
1202 *m_pstream
<< "% " << urx
<< " " << ury
<< " lineto\n";
1203 *m_pstream
<< "% " << llx
<< " " << ury
<< " lineto closepath stroke\n";
1205 *m_pstream
<< "%%BeginProlog\n";
1206 *m_pstream
<< wxPostScriptHeaderEllipse
;
1207 *m_pstream
<< wxPostScriptHeaderEllipticArc
;
1208 *m_pstream
<< wxPostScriptHeaderReencodeISO1
;
1209 *m_pstream
<< wxPostScriptHeaderReencodeISO2
;
1211 if (wxPostScriptHeaderSpline
)
1212 *m_pstream
<< wxPostScriptHeaderSpline
;
1213 *m_pstream
<< "%%EndProlog\n";
1216 m_pstream
= (ofstream
*) NULL
;
1218 char *tmp_file
= wxGetTempFileName("ps");
1220 // Paste header Before wx_printer_file
1221 wxConcatFiles (header_file
, wxThePrintSetupData
->GetPrinterFile(), tmp_file
);
1222 wxRemoveFile (header_file
);
1223 wxRemoveFile (wxThePrintSetupData
->GetPrinterFile());
1224 wxRenameFile(tmp_file
, wxThePrintSetupData
->GetPrinterFile());
1226 #if defined(__X__) || defined(__WXGTK__)
1229 switch (wxThePrintSetupData
->GetPrinterMode()) {
1233 argv
[0] = wxThePrintSetupData
->GetPrintPreviewCommand();
1234 argv
[1] = wxThePrintSetupData
->GetPrinterFile();
1235 argv
[2] = (char *) NULL
;
1236 wxExecute (argv
, TRUE
);
1237 wxRemoveFile(wxThePrintSetupData
->GetPrinterFile());
1245 argv
[argc
++] = wxThePrintSetupData
->GetPrinterCommand();
1247 // !SM! If we simply assign to argv[1] here, if printer options
1248 // are blank, we get an annoying and confusing message from lpr.
1249 char * opts
= wxThePrintSetupData
->GetPrinterOptions();
1251 argv
[argc
++] = opts
;
1253 argv
[argc
++] = wxThePrintSetupData
->GetPrinterFile();
1254 argv
[argc
++] = (char *) NULL
;
1255 wxExecute (argv
, TRUE
);
1256 wxRemoveFile(wxThePrintSetupData
->GetPrinterFile());
1267 void wxPostScriptDC::StartPage ()
1269 wxCHECK_RET( Ok(), "invalid postscript dc" );
1271 *m_pstream
<< "%%Page: " << (wxPageNumber
++) << "\n";
1273 // *m_pstream << "matrix currentmatrix\n";
1275 // Added by Chris Breeze
1277 // Each page starts with an "initgraphics" which resets the
1278 // transformation and so we need to reset the origin
1279 // (and rotate the page for landscape printing)
1282 m_scaleFactor = 1.0;
1283 m_logicalOriginX = 0;
1284 m_logicalOriginY = 0;
1288 long translate_x
, translate_y
;
1289 double scale_x
, scale_y
;
1290 wxThePrintSetupData
->GetPrinterTranslation(&translate_x
, &translate_y
);
1291 wxThePrintSetupData
->GetPrinterScaling(&scale_x
, &scale_y
);
1293 if (wxThePrintSetupData
->GetPrinterOrientation() == PS_LANDSCAPE
)
1295 translate_y
-= m_maxY
;
1296 *m_pstream
<< "90 rotate\n";
1299 *m_pstream
<< scale_x
<< " " << scale_y
<< " scale\n";
1300 *m_pstream
<< translate_x
<< " " << translate_y
<< " translate\n";
1303 void wxPostScriptDC::EndPage ()
1305 wxCHECK_RET( Ok(), "invalid postscript dc" );
1307 *m_pstream
<< "showpage\n";
1310 bool wxPostScriptDC::Blit (long xdest
, long ydest
, long fwidth
, long fheight
,
1311 wxDC
*source
, long xsrc
, long ysrc
, int WXUNUSED(rop
), bool WXUNUSED(useMask
))
1313 wxCHECK_MSG( Ok(), FALSE
, "invalid postscript dc" );
1318 long wxPostScriptDC::GetCharHeight ()
1321 return m_font
.GetPointSize();
1326 void wxPostScriptDC::GetTextExtent (const wxString
& string
, long *x
, long *y
,
1327 long *descent
, long *externalLeading
, wxFont
*theFont
,
1328 bool WXUNUSED(use16
))
1330 // if (!m_pstream) return;
1332 wxFont
*fontToUse
= theFont
;
1334 if (!fontToUse
) fontToUse
= (wxFont
*) &m_font
;
1336 #if !USE_AFM_FOR_POSTSCRIPT
1337 // Provide a VERY rough estimate (avoid using it)
1338 // Chris Breeze 5/11/97: produces accurate results for mono-spaced
1339 // font such as Courier (aka wxMODERN)
1343 height
= fontToUse
->GetPointSize();
1345 *x
= strlen (string
) * height
* 72 / 120;
1346 *y
= (long) (height
* 1.32); // allow for descender
1350 if (externalLeading
)
1351 *externalLeading
= 0;
1353 // +++++ start of contributed code +++++
1355 // ************************************************************
1356 // method for calculating string widths in postscript:
1357 // read in the AFM (adobe font metrics) file for the
1358 // actual font, parse it and extract the character widths
1359 // and also the descender. this may be improved, but for now
1360 // it works well. the AFM file is only read in if the
1361 // font is changed. this may be chached in the future.
1362 // calls to GetTextExtent with the font unchanged are rather
1365 // for each font and style used there is an AFM file necessary.
1366 // currently i have only files for the roman font family.
1367 // i try to get files for the other ones!
1369 // CAVE: the size of the string is currently always calculated
1370 // in 'points' (1/72 of an inch). this should later on be
1371 // changed to depend on the mapping mode.
1372 // CAVE: the path to the AFM files must be set before calling this
1373 // function. this is usually done by a call like the following:
1374 // wxSetAFMPath("d:\\wxw161\\afm\\");
1378 // wxPostScriptDC dc(NULL, TRUE);
1380 // wxSetAFMPath("d:\\wxw161\\afm\\");
1381 // dc.StartDoc("Test");
1384 // dc.SetFont(new wxFont(10, wxROMAN, wxNORMAL, wxNORMAL));
1385 // dc.GetTextExtent("Hallo",&w,&h);
1390 // by steve (stefan.hammes@urz.uni-heidelberg.de)
1391 // created: 10.09.94
1392 // updated: 14.05.95
1394 assert(fontToUse
&& "void wxPostScriptDC::GetTextExtent: no font defined");
1395 assert(x
&& "void wxPostScriptDC::GetTextExtent: x == NULL");
1396 assert(y
&& "void wxPostScriptDC::GetTextExtent: y == NULL");
1398 // these static vars are for storing the state between calls
1399 static int lastFamily
= INT_MIN
;
1400 static int lastSize
= INT_MIN
;
1401 static int lastStyle
= INT_MIN
;
1402 static int lastWeight
= INT_MIN
;
1403 static int lastDescender
= INT_MIN
;
1404 static int lastWidths
[256]; // widths of the characters
1406 // get actual parameters
1407 const int Family
= fontToUse
->GetFamily();
1408 const int Size
= fontToUse
->GetPointSize();
1409 const int Style
= fontToUse
->GetStyle();
1410 const int Weight
= fontToUse
->GetWeight();
1412 // if we have another font, read the font-metrics
1413 if(Family
!=lastFamily
||Size
!=lastSize
||Style
!=lastStyle
||Weight
!=lastWeight
){
1414 // store actual values
1415 lastFamily
= Family
;
1418 lastWeight
= Weight
;
1420 // read in new font metrics **************************************
1422 // 1. construct filename ******************************************
1423 /* MATTHEW: [2] Use wxTheFontNameDirectory */
1426 // Julian - we'll need to do this a different way now we've removed the
1427 // font directory system. Must find Stefan's original code.
1429 name
= wxTheFontNameDirectory
->GetAFMName(Family
, Weight
, Style
);
1433 // get the directory of the AFM files
1437 strcpy(afmName
,wxGetAFMPath());
1439 // 2. open and process the file **********************************
1441 // a short explanation of the AFM format:
1442 // we have for each character a line, which gives its size
1445 // C 63 ; WX 444 ; N question ; B 49 -14 395 676 ;
1447 // that means, we have a character with ascii code 63, and width
1448 // (444/1000 * fontSize) points.
1449 // the other data is ignored for now!
1451 // when the font has changed, we read in the right AFM file and store the
1452 // character widths in an array, which is processed below (see point 3.).
1454 // new elements JC Sun Aug 25 23:21:44 MET DST 1996
1457 strcat(afmName
,name
);
1458 strcat(afmName
,".afm");
1459 FILE *afmFile
= fopen(afmName
,"r");
1461 wxDebugMsg("GetTextExtent: can't open AFM file '%s'\n",afmName
);
1462 wxDebugMsg(" using approximate values\n");
1464 for (i
=0; i
<256; i
++) lastWidths
[i
] = 500; // an approximate value
1465 lastDescender
= -150; // dito.
1468 // init the widths array
1469 for(i
=0; i
<256; i
++) lastWidths
[i
]= INT_MIN
;
1470 // some variables for holding parts of a line
1471 char cString
[10],semiString
[10],WXString
[10],descString
[20];
1472 char upString
[30], utString
[30], encString
[50];
1475 // read in the file and parse it
1476 while(fgets(line
,sizeof(line
),afmFile
)!=NULL
){
1477 // A.) check for descender definition
1478 if(strncmp(line
,"Descender",9)==0){
1479 if((sscanf(line
,"%s%d",descString
,&lastDescender
)!=2)
1480 || (strcmp(descString
,"Descender")!=0)) {
1481 wxDebugMsg("AFM-file '%s': line '%s' has error (bad descender)\n",
1485 // JC 1.) check for UnderlinePosition
1486 else if(strncmp(line
,"UnderlinePosition",17)==0){
1487 if((sscanf(line
,"%s%lf",upString
,&UnderlinePosition
)!=2)
1488 || (strcmp(upString
,"UnderlinePosition")!=0)) {
1489 wxDebugMsg("AFM-file '%s': line '%s' has error (bad UnderlinePosition)\n",
1493 // JC 2.) check for UnderlineThickness
1494 else if(strncmp(line
,"UnderlineThickness",18)==0){
1495 if((sscanf(line
,"%s%lf",utString
,&UnderlineThickness
)!=2)
1496 || (strcmp(utString
,"UnderlineThickness")!=0)) {
1497 wxDebugMsg("AFM-file '%s': line '%s' has error (bad UnderlineThickness)\n",
1501 // JC 3.) check for EncodingScheme
1502 else if(strncmp(line
,"EncodingScheme",14)==0){
1503 if((sscanf(line
,"%s%s",utString
,encString
)!=2)
1504 || (strcmp(utString
,"EncodingScheme")!=0)) {
1505 wxDebugMsg("AFM-file '%s': line '%s' has error (bad EncodingScheme)\n",
1508 else if (strncmp(encString
, "AdobeStandardEncoding", 21))
1510 wxDebugMsg("AFM-file '%s': line '%s' has error (unsupported EncodingScheme %s)\n",
1511 afmName
,line
, encString
);
1514 // B.) check for char-width
1515 else if(strncmp(line
,"C ",2)==0){
1516 if(sscanf(line
,"%s%d%s%s%d",
1517 cString
,&ascii
,semiString
,WXString
,&cWidth
)!=5){
1518 wxDebugMsg("AFM-file '%s': line '%s' has an error (bad character width)\n",afmName
,line
);
1520 if(strcmp(cString
,"C")!=0 || strcmp(semiString
,";")!=0 ||
1521 strcmp(WXString
,"WX")!=0){
1522 wxDebugMsg("AFM-file '%s': line '%s' has a format error\n",afmName
,line
);
1524 //printf(" char '%c'=%d has width '%d'\n",ascii,ascii,cWidth);
1525 if(ascii
>=0 && ascii
<256){
1526 lastWidths
[ascii
] = cWidth
; // store width
1528 /* MATTHEW: this happens a lot; don't print an error */
1529 // wxDebugMsg("AFM-file '%s': ASCII value %d out of range\n",afmName,ascii);
1532 // C.) ignore other entries.
1536 // hack to compute correct values for german 'Umlaute'
1537 // the correct way would be to map the character names
1538 // like 'adieresis' to corresp. positions of ISOEnc and read
1539 // these values from AFM files, too. Maybe later ...
1540 lastWidths
[196] = lastWidths
['A']; // Ä
1541 lastWidths
[228] = lastWidths
['a']; // ä
1542 lastWidths
[214] = lastWidths
['O']; // Ö
1543 lastWidths
[246] = lastWidths
['o']; // ö
1544 lastWidths
[220] = lastWidths
['U']; // Ü
1545 lastWidths
[252] = lastWidths
['u']; // ü
1546 lastWidths
[223] = lastWidths
[251]; // ß
1549 // JC: calculate UnderlineThickness/UnderlinePosition
1550 m_underlinePosition
= m_underlinePosition
* fontToUse
->GetPointSize() / 1000.0f
;
1551 m_underlineThickness
= m_underlineThickness
* fontToUse
->GetPointSize() / 1000.0f
* m_scaleFactor
;
1553 // 3. now the font metrics are read in, calc size *******************
1554 // this is done by adding the widths of the characters in the
1555 // string. they are given in 1/1000 of the size!
1558 long height
=Size
; // by default
1560 for(p
=(unsigned char *)(const char *)string
; *p
; p
++){
1561 if(lastWidths
[*p
]== INT_MIN
){
1562 wxDebugMsg("GetTextExtent: undefined width for character '%c' (%d)\n",
1564 widthSum
+= (long)(lastWidths
[' ']/1000.0F
* Size
); // assume space
1566 widthSum
+= (long)((lastWidths
[*p
]/1000.0F
)*Size
);
1569 // add descender to height (it is usually a negative value)
1570 if(lastDescender
!=INT_MIN
){
1571 height
+= (long)(((-lastDescender
)/1000.0F
) * Size
); /* MATTHEW: forgot scale */
1574 // return size values
1578 // return other parameters
1580 if(lastDescender
!=INT_MIN
){
1581 *descent
= (long)(((-lastDescender
)/1000.0F
) * Size
); /* MATTHEW: forgot scale */
1587 // currently no idea how to calculate this!
1588 // if (externalLeading) *externalLeading = 0;
1589 if (externalLeading
)
1590 *externalLeading
= 0;
1592 // ----- end of contributed code -----
1596 void wxPostScriptDC::GetSizeMM(long *width
, long *height
) const
1598 const char *paperType
= wxThePrintSetupData
->GetPaperName();
1600 if (!paperType
) paperType
= _("A4 210 x 297 mm");
1602 wxPrintPaperType
*paper
= wxThePrintPaperDatabase
->FindPaperType(paperType
);
1604 if (!paper
) paper
= wxThePrintPaperDatabase
->FindPaperType(_("A4 210 x 297 mm"));
1608 if (width
) *width
= paper
->widthMM
;
1609 if (height
) *height
= paper
->heightMM
;
1613 if (width
) *width
= 210;
1614 if (height
) *height
= 297;