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