1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: Generic wxPostScriptDC implementation
4 // Author: Julian Smart, Robert Roebling, Markus Holzhem
8 // Copyright: (c) Julian Smart and Markus Holzem
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
13 #pragma implementation "dcpsg.h"
16 #include "wx/wxprec.h"
25 #if wxUSE_PRINTING_ARCHITECTURE
31 #include "wx/window.h"
32 #include "wx/dcmemory.h"
35 #include "wx/filedlg.h"
37 #include "wx/msgdlg.h"
40 #include "wx/generic/dcpsg.h"
41 #include "wx/printdlg.h"
42 #include "wx/button.h"
43 #include "wx/stattext.h"
44 #include "wx/radiobox.h"
45 #include "wx/textctrl.h"
46 #include "wx/prntbase.h"
48 #include "wx/filefn.h"
72 //-----------------------------------------------------------------------------
73 // start and end of document/page
74 //-----------------------------------------------------------------------------
76 static const char *wxPostScriptHeaderConicTo
= "\
80 /conic_cntrl_y exch def\n\
81 /conic_cntrl_x exch def\n\
85 /p1_x p0_x conic_cntrl_x p0_x sub 2 3 div mul add def\n\
86 /p1_y p0_y conic_cntrl_y p0_y sub 2 3 div mul add def\n\
87 /p2_x p1_x to_x p0_x sub 1 3 div mul add def\n\
88 /p2_y p1_y to_y p0_y sub 1 3 div mul add def\n\
89 p1_x p1_y p2_x p2_y to_x to_y curveto\n\
91 /start_ol { gsave 1.0 72 div dup scale } bind def\n\
92 /end_ol { closepath fill grestore } bind def\n\
95 static const char *wxPostScriptHeaderEllipse
= "\
96 /ellipsedict 8 dict def\n\
97 ellipsedict /mtrx matrix put\n\
100 /endangle exch def\n\
101 /startangle exch def\n\
106 /savematrix mtrx currentmatrix def\n\
109 0 0 1 startangle endangle arc\n\
110 savematrix setmatrix\n\
115 static const char *wxPostScriptHeaderEllipticArc
= "\
116 /ellipticarcdict 8 dict def\n\
117 ellipticarcdict /mtrx matrix put\n\
119 { ellipticarcdict begin\n\
121 /endangle exch def\n\
122 /startangle exch def\n\
127 /savematrix mtrx currentmatrix def\n\
130 do_fill { 0 0 moveto } if\n\
131 0 0 1 startangle endangle arc\n\
132 savematrix setmatrix\n\
133 do_fill { fill }{ stroke } ifelse\n\
137 static const char *wxPostScriptHeaderSpline
= "\
138 /DrawSplineSection {\n\
145 /xa x1 x2 x1 sub 0.666667 mul add def\n\
146 /ya y1 y2 y1 sub 0.666667 mul add def\n\
147 /xb x3 x2 x3 sub 0.666667 mul add def\n\
148 /yb y3 y2 y3 sub 0.666667 mul add def\n\
150 xa ya xb yb x3 y3 curveto\n\
154 static const char *wxPostScriptHeaderColourImage
= "\
155 %% define 'colorimage' if it isn't defined\n\
156 %% ('colortogray' and 'mergeprocs' come from xwd2ps\n\
158 /colorimage where %% do we know about 'colorimage'?\n\
159 { pop } %% yes: pop off the 'dict' returned\n\
160 { %% no: define one\n\
161 /colortogray { %% define an RGB->I function\n\
162 /rgbdata exch store %% call input 'rgbdata'\n\
163 rgbdata length 3 idiv\n\
164 /npixls exch store\n\
166 0 1 npixls 1 sub {\n\
168 rgbdata rgbindx get 20 mul %% Red\n\
169 rgbdata rgbindx 1 add get 32 mul %% Green\n\
170 rgbdata rgbindx 2 add get 12 mul %% Blue\n\
171 add add 64 idiv %% I = .5G + .31R + .18B\n\
173 /rgbindx rgbindx 3 add store\n\
175 grays 0 npixls getinterval\n\
178 %% Utility procedure for colorimage operator.\n\
179 %% This procedure takes two procedures off the\n\
180 %% stack and merges them into a single procedure.\n\
182 /mergeprocs { %% def\n\
201 /colorimage { %% def\n\
202 pop pop %% remove 'false 3' operands\n\
203 {colortogray} mergeprocs\n\
206 } ifelse %% end of 'false' case\n\
210 static char wxPostScriptHeaderReencodeISO1
[] =
212 "dup dup findfont dup length dict begin\n"
213 "{ 1 index /FID ne { def }{ pop pop } ifelse } forall\n"
214 "/Encoding ISOLatin1Encoding def\n"
215 "currentdict end definefont\n"
217 "/ISOLatin1Encoding [\n"
218 "/.notdef/.notdef/.notdef/.notdef/.notdef/.notdef/.notdef/.notdef\n"
219 "/.notdef/.notdef/.notdef/.notdef/.notdef/.notdef/.notdef/.notdef\n"
220 "/.notdef/.notdef/.notdef/.notdef/.notdef/.notdef/.notdef/.notdef\n"
221 "/.notdef/.notdef/.notdef/.notdef/.notdef/.notdef/.notdef/.notdef\n"
222 "/space/exclam/quotedbl/numbersign/dollar/percent/ampersand/quoteright\n"
223 "/parenleft/parenright/asterisk/plus/comma/minus/period/slash\n"
224 "/zero/one/two/three/four/five/six/seven/eight/nine/colon/semicolon\n"
225 "/less/equal/greater/question/at/A/B/C/D/E/F/G/H/I/J/K/L/M/N\n"
226 "/O/P/Q/R/S/T/U/V/W/X/Y/Z/bracketleft/backslash/bracketright\n"
227 "/asciicircum/underscore/quoteleft/a/b/c/d/e/f/g/h/i/j/k/l/m\n"
228 "/n/o/p/q/r/s/t/u/v/w/x/y/z/braceleft/bar/braceright/asciitilde\n"
229 "/.notdef/.notdef/.notdef/.notdef/.notdef/.notdef/.notdef/.notdef\n"
230 "/.notdef/.notdef/.notdef/.notdef/.notdef/.notdef/.notdef/.notdef\n"
231 "/.notdef/dotlessi/grave/acute/circumflex/tilde/macron/breve\n"
232 "/dotaccent/dieresis/.notdef/ring/cedilla/.notdef/hungarumlaut\n";
234 static char wxPostScriptHeaderReencodeISO2
[] =
235 "/ogonek/caron/space/exclamdown/cent/sterling/currency/yen/brokenbar\n"
236 "/section/dieresis/copyright/ordfeminine/guillemotleft/logicalnot\n"
237 "/hyphen/registered/macron/degree/plusminus/twosuperior/threesuperior\n"
238 "/acute/mu/paragraph/periodcentered/cedilla/onesuperior/ordmasculine\n"
239 "/guillemotright/onequarter/onehalf/threequarters/questiondown\n"
240 "/Agrave/Aacute/Acircumflex/Atilde/Adieresis/Aring/AE/Ccedilla\n"
241 "/Egrave/Eacute/Ecircumflex/Edieresis/Igrave/Iacute/Icircumflex\n"
242 "/Idieresis/Eth/Ntilde/Ograve/Oacute/Ocircumflex/Otilde/Odieresis\n"
243 "/multiply/Oslash/Ugrave/Uacute/Ucircumflex/Udieresis/Yacute\n"
244 "/Thorn/germandbls/agrave/aacute/acircumflex/atilde/adieresis\n"
245 "/aring/ae/ccedilla/egrave/eacute/ecircumflex/edieresis/igrave\n"
246 "/iacute/icircumflex/idieresis/eth/ntilde/ograve/oacute/ocircumflex\n"
247 "/otilde/odieresis/divide/oslash/ugrave/uacute/ucircumflex/udieresis\n"
248 "/yacute/thorn/ydieresis\n"
252 //-------------------------------------------------------------------------------
254 //-------------------------------------------------------------------------------
256 float wxPostScriptDC::ms_PSScaleFactor
= 10.0;
258 void wxPostScriptDC::SetResolution(int ppi
)
260 ms_PSScaleFactor
= (float)ppi
/ 72.0;
263 int wxPostScriptDC::GetResolution()
265 return (int)(ms_PSScaleFactor
* 72.0);
270 wxPostScriptDC::wxPostScriptDC ()
272 m_pstream
= (FILE*) NULL
;
282 m_underlinePosition
= 0.0;
283 m_underlineThickness
= 0.0;
285 m_signX
= 1; // default x-axis left to right
286 m_signY
= -1; // default y-axis bottom up -> top down
288 // Compatibility only
289 // HH: Doesn't seem to work for wxMSW...
290 #if !defined(__WXMSW__)
291 m_printData
= * wxThePrintSetupData
;
295 wxPostScriptDC::wxPostScriptDC (const wxString
& file
, bool interactive
, wxWindow
*parent
)
297 m_pstream
= (FILE*) NULL
;
307 m_underlinePosition
= 0.0;
308 m_underlineThickness
= 0.0;
310 m_signX
= 1; // default x-axis left to right
311 m_signY
= -1; // default y-axis bottom up -> top down
313 Create(file
, interactive
, parent
);
316 bool wxPostScriptDC::Create(const wxString
& file
, bool interactive
, wxWindow
*parent
)
318 m_isInteractive
= interactive
;
321 m_printData
.SetFilename(file
);
324 // Can only send to file in Windows
325 m_printData
.SetPrintMode(wxPRINT_MODE_FILE
);
330 if ((m_ok
= PrinterDialog (parent
) ) == FALSE
) return FALSE
;
340 wxPostScriptDC::wxPostScriptDC (const wxPrintData
& printData
)
342 m_pstream
= (FILE*) NULL
;
352 m_underlinePosition
= 0.0;
353 m_underlineThickness
= 0.0;
355 m_signX
= 1; // default x-axis left to right
356 m_signY
= -1; // default y-axis bottom up -> top down
358 m_printData
= printData
;
363 wxPostScriptDC::~wxPostScriptDC ()
368 m_pstream
= (FILE*) NULL
;
372 bool wxPostScriptDC::Ok() const
377 // This dialog is deprecated now: use wxGenericPrintDialog or the printing framework
378 bool wxPostScriptDC::PrinterDialog(wxWindow
*parent
)
380 wxPostScriptPrintDialog
dialog( parent
, _("Printer Settings"), wxPoint(150, 150), wxSize(400, 400),
381 wxDEFAULT_DIALOG_STYLE
| wxDIALOG_MODAL
);
382 m_ok
= (dialog
.ShowModal () == wxID_OK
);
384 if (!m_ok
) return FALSE
;
386 if ((m_printData
.GetFilename() == "") &&
387 (m_printData
.GetPrintMode() == wxPRINT_MODE_PREVIEW
||
388 m_printData
.GetPrintMode() == wxPRINT_MODE_PRINTER
))
392 m_printData
.SetFilename("preview");
394 // For PS_PRINTER action this depends on a Unix-style print spooler
395 // since the wx_printer_file can be destroyed during a session
396 // @@@ TODO: a Windows-style answer for non-Unix
398 wxGetUserId (userId
, sizeof (userId
) / sizeof (char));
400 wxStrcpy (tmp
, wxT("/tmp/preview_"));
401 wxStrcat (tmp
, userId
);
402 m_printData
.SetFilename(tmp
);
405 wxStrcpy(tmp2
, m_printData
.GetFilename());
406 wxStrcat (tmp2
, wxT(".ps"));
407 m_printData
.SetFilename(tmp2
);
409 else if ((m_printData
.GetFilename() == wxT("")) && (m_printData
.GetPrintMode() == wxPRINT_MODE_FILE
))
411 wxString file
= wxSaveFileSelector (_("PostScript"), wxT("ps"));
412 if ( file
.IsEmpty() )
418 m_printData
.SetFilename(file
);
425 void wxPostScriptDC::DoSetClippingRegion (wxCoord x
, wxCoord y
, wxCoord w
, wxCoord h
)
427 wxCHECK_RET( m_ok
&& m_pstream
, wxT("invalid postscript dc") );
429 if (m_clipping
) DestroyClippingRegion();
431 wxDC::DoSetClippingRegion(x
, y
, w
, h
);
440 "closepath clip newpath\n",
441 LogicalToDeviceX(x
), LogicalToDeviceY(y
),
442 LogicalToDeviceX(x
+w
), LogicalToDeviceY(y
),
443 LogicalToDeviceX(x
+w
), LogicalToDeviceY(y
+h
),
444 LogicalToDeviceX(x
), LogicalToDeviceY(y
+h
) );
448 void wxPostScriptDC::DestroyClippingRegion()
450 wxCHECK_RET( m_ok
&& m_pstream
, wxT("invalid postscript dc") );
455 fprintf( m_pstream
, "grestore\n" );
458 wxDC::DestroyClippingRegion();
461 void wxPostScriptDC::Clear()
463 wxFAIL_MSG( wxT("wxPostScriptDC::Clear not implemented.") );
466 bool wxPostScriptDC::DoFloodFill (wxCoord
WXUNUSED(x
), wxCoord
WXUNUSED(y
), const wxColour
&WXUNUSED(col
), int WXUNUSED(style
))
468 wxFAIL_MSG( wxT("wxPostScriptDC::FloodFill not implemented.") );
472 bool wxPostScriptDC::DoGetPixel (wxCoord
WXUNUSED(x
), wxCoord
WXUNUSED(y
), wxColour
* WXUNUSED(col
)) const
474 wxFAIL_MSG( wxT("wxPostScriptDC::GetPixel not implemented.") );
478 void wxPostScriptDC::DoCrossHair (wxCoord
WXUNUSED(x
), wxCoord
WXUNUSED(y
))
480 wxFAIL_MSG( wxT("wxPostScriptDC::CrossHair not implemented.") );
483 void wxPostScriptDC::DoDrawLine (wxCoord x1
, wxCoord y1
, wxCoord x2
, wxCoord y2
)
485 wxCHECK_RET( m_ok
&& m_pstream
, wxT("invalid postscript dc") );
487 if (m_pen
.GetStyle() == wxTRANSPARENT
) return;
496 LogicalToDeviceX(x1
), LogicalToDeviceY(y1
),
497 LogicalToDeviceX(x2
), LogicalToDeviceY (y2
) );
499 CalcBoundingBox( x1
, y1
);
500 CalcBoundingBox( x2
, y2
);
503 #define RAD2DEG 57.29577951308
505 void wxPostScriptDC::DoDrawArc (wxCoord x1
, wxCoord y1
, wxCoord x2
, wxCoord y2
, wxCoord xc
, wxCoord yc
)
507 wxCHECK_RET( m_ok
&& m_pstream
, wxT("invalid postscript dc") );
509 wxCoord dx
= x1
- xc
;
510 wxCoord dy
= y1
- yc
;
511 wxCoord radius
= (wxCoord
) sqrt( (double)(dx
*dx
+dy
*dy
) );
512 double alpha1
, alpha2
;
514 if (x1
== x2
&& y1
== y2
)
519 else if (radius
== 0.0)
521 alpha1
= alpha2
= 0.0;
525 alpha1
= (x1
- xc
== 0) ?
526 (y1
- yc
< 0) ? 90.0 : -90.0 :
527 -atan2(double(y1
-yc
), double(x1
-xc
)) * RAD2DEG
;
528 alpha2
= (x2
- xc
== 0) ?
529 (y2
- yc
< 0) ? 90.0 : -90.0 :
530 -atan2(double(y2
-yc
), double(x2
-xc
)) * RAD2DEG
;
532 while (alpha1
<= 0) alpha1
+= 360;
533 while (alpha2
<= 0) alpha2
+= 360; // adjust angles to be between
534 while (alpha1
> 360) alpha1
-= 360; // 0 and 360 degree
535 while (alpha2
> 360) alpha2
-= 360;
537 if (m_brush
.GetStyle() != wxTRANSPARENT
)
543 "%d %d %d %d %d %d ellipse\n"
547 LogicalToDeviceX(xc
), LogicalToDeviceY(yc
), LogicalToDeviceXRel(radius
), LogicalToDeviceYRel(radius
), (wxCoord
)alpha1
, (wxCoord
) alpha2
,
548 LogicalToDeviceX(xc
), LogicalToDeviceY(yc
) );
550 CalcBoundingBox( xc
-radius
, yc
-radius
);
551 CalcBoundingBox( xc
+radius
, yc
+radius
);
554 if (m_pen
.GetStyle() != wxTRANSPARENT
)
560 "%d %d %d %d %d %d ellipse\n"
564 LogicalToDeviceX(xc
), LogicalToDeviceY(yc
), LogicalToDeviceXRel(radius
), LogicalToDeviceYRel(radius
), (wxCoord
)alpha1
, (wxCoord
) alpha2
,
565 LogicalToDeviceX(xc
), LogicalToDeviceY(yc
) );
567 CalcBoundingBox( xc
-radius
, yc
-radius
);
568 CalcBoundingBox( xc
+radius
, yc
+radius
);
572 void wxPostScriptDC::DoDrawEllipticArc(wxCoord x
,wxCoord y
,wxCoord w
,wxCoord h
,double sa
,double ea
)
574 wxCHECK_RET( m_ok
&& m_pstream
, wxT("invalid postscript dc") );
576 if (sa
>=360 || sa
<=-360) sa
=sa
-int(sa
/360)*360;
577 if (ea
>=360 || ea
<=-360) ea
=ea
-int(ea
/360)*360;
583 DrawEllipse(x
,y
,w
,h
);
587 if (m_brush
.GetStyle () != wxTRANSPARENT
)
593 "%d %d %d %d %d %d true ellipticarc\n",
594 LogicalToDeviceX(x
+w
/2), LogicalToDeviceY(y
+h
/2), LogicalToDeviceXRel(w
/2), LogicalToDeviceYRel(h
/2), (wxCoord
)sa
, (wxCoord
)ea
);
596 CalcBoundingBox( x
,y
);
597 CalcBoundingBox( x
+w
, y
+h
);
600 if (m_pen
.GetStyle () != wxTRANSPARENT
)
606 "%d %d %d %d %d %d false ellipticarc\n",
607 LogicalToDeviceX(x
+w
/2), LogicalToDeviceY(y
+h
/2), LogicalToDeviceXRel(w
/2), LogicalToDeviceYRel(h
/2), (wxCoord
)sa
, (wxCoord
)ea
);
609 CalcBoundingBox( x
,y
);
610 CalcBoundingBox( x
+w
, y
+h
);
614 void wxPostScriptDC::DoDrawPoint (wxCoord x
, wxCoord y
)
616 wxCHECK_RET( m_ok
&& m_pstream
, wxT("invalid postscript dc") );
618 if (m_pen
.GetStyle() == wxTRANSPARENT
) return;
627 LogicalToDeviceX(x
), LogicalToDeviceY(y
),
628 LogicalToDeviceX(x
+1), LogicalToDeviceY(y
) );
630 CalcBoundingBox( x
, y
);
633 void wxPostScriptDC::DoDrawPolygon (int n
, wxPoint points
[], wxCoord xoffset
, wxCoord yoffset
, int WXUNUSED(fillStyle
))
635 wxCHECK_RET( m_ok
&& m_pstream
, wxT("invalid postscript dc") );
639 if (m_brush
.GetStyle () != wxTRANSPARENT
)
643 fprintf( m_pstream
, "newpath\n" );
645 wxCoord xx
= LogicalToDeviceX(points
[0].x
+ xoffset
);
646 wxCoord yy
= LogicalToDeviceY(points
[0].y
+ yoffset
);
648 fprintf( m_pstream
, "%d %d moveto\n", xx
, yy
);
650 CalcBoundingBox( points
[0].x
+ xoffset
, points
[0].y
+ yoffset
);
652 for (int i
= 1; i
< n
; i
++)
654 xx
= LogicalToDeviceX(points
[i
].x
+ xoffset
);
655 yy
= LogicalToDeviceY(points
[i
].y
+ yoffset
);
657 fprintf( m_pstream
, "%d %d lineto\n", xx
, yy
);
659 CalcBoundingBox( points
[i
].x
+ xoffset
, points
[i
].y
+ yoffset
);
662 fprintf( m_pstream
, "fill\n" );
665 if (m_pen
.GetStyle () != wxTRANSPARENT
)
669 fprintf( m_pstream
, "newpath\n" );
671 wxCoord xx
= LogicalToDeviceX(points
[0].x
+ xoffset
);
672 wxCoord yy
= LogicalToDeviceY(points
[0].y
+ yoffset
);
674 fprintf( m_pstream
, "%d %d moveto\n", xx
, yy
);
676 CalcBoundingBox( points
[0].x
+ xoffset
, points
[0].y
+ yoffset
);
678 for (int i
= 1; i
< n
; i
++)
680 xx
= LogicalToDeviceX(points
[i
].x
+ xoffset
);
681 yy
= LogicalToDeviceY(points
[i
].y
+ yoffset
);
683 fprintf( m_pstream
, "%d %d lineto\n", xx
, yy
);
685 CalcBoundingBox( points
[i
].x
+ xoffset
, points
[i
].y
+ yoffset
);
688 fprintf( m_pstream
, "closepath\n" );
689 fprintf( m_pstream
, "stroke\n" );
693 void wxPostScriptDC::DoDrawLines (int n
, wxPoint points
[], wxCoord xoffset
, wxCoord yoffset
)
695 wxCHECK_RET( m_ok
&& m_pstream
, wxT("invalid postscript dc") );
697 if (m_pen
.GetStyle() == wxTRANSPARENT
) return;
704 for ( i
=0; i
<n
; i
++ )
706 CalcBoundingBox( LogicalToDeviceX(points
[i
].x
+xoffset
), LogicalToDeviceY(points
[i
].y
+yoffset
));
712 LogicalToDeviceX(points
[0].x
+xoffset
), LogicalToDeviceY(points
[0].y
+yoffset
) );
714 for (i
= 1; i
< n
; i
++)
718 LogicalToDeviceX(points
[i
].x
+xoffset
), LogicalToDeviceY(points
[i
].y
+yoffset
) );
721 fprintf( m_pstream
, "stroke\n" );
724 void wxPostScriptDC::DoDrawRectangle (wxCoord x
, wxCoord y
, wxCoord width
, wxCoord height
)
726 wxCHECK_RET( m_ok
&& m_pstream
, wxT("invalid postscript dc") );
728 if (m_brush
.GetStyle () != wxTRANSPARENT
)
740 LogicalToDeviceX(x
), LogicalToDeviceY(y
),
741 LogicalToDeviceX(x
+ width
), LogicalToDeviceY(y
),
742 LogicalToDeviceX(x
+ width
), LogicalToDeviceY(y
+ height
),
743 LogicalToDeviceX(x
), LogicalToDeviceY(y
+ height
) );
745 CalcBoundingBox( x
, y
);
746 CalcBoundingBox( x
+ width
, y
+ height
);
749 if (m_pen
.GetStyle () != wxTRANSPARENT
)
761 LogicalToDeviceX(x
), LogicalToDeviceY(y
),
762 LogicalToDeviceX(x
+ width
), LogicalToDeviceY(y
),
763 LogicalToDeviceX(x
+ width
), LogicalToDeviceY(y
+ height
),
764 LogicalToDeviceX(x
), LogicalToDeviceY(y
+ height
) );
766 CalcBoundingBox( x
, y
);
767 CalcBoundingBox( x
+ width
, y
+ height
);
771 void wxPostScriptDC::DoDrawRoundedRectangle (wxCoord x
, wxCoord y
, wxCoord width
, wxCoord height
, double radius
)
773 wxCHECK_RET( m_ok
&& m_pstream
, wxT("invalid postscript dc") );
777 // Now, a negative radius is interpreted to mean
778 // 'the proportion of the smallest X or Y dimension'
779 double smallest
= 0.0;
784 radius
= (-radius
* smallest
);
787 wxCoord rad
= (wxCoord
) radius
;
789 if (m_brush
.GetStyle () != wxTRANSPARENT
)
793 /* Draw rectangle anticlockwise */
796 "%d %d %d 90 180 arc\n"
798 "%d %d %d 180 270 arc\n"
800 "%d %d %d 270 0 arc\n"
802 "%d %d %d 0 90 arc\n"
806 LogicalToDeviceX(x
+ rad
), LogicalToDeviceY(y
+ rad
), LogicalToDeviceXRel(rad
),
807 LogicalToDeviceX(x
), LogicalToDeviceY(y
+ rad
),
808 LogicalToDeviceX(x
+ rad
), LogicalToDeviceY(y
+ height
- rad
), LogicalToDeviceXRel(rad
),
809 LogicalToDeviceX(x
+ width
- rad
), LogicalToDeviceY(y
+ height
),
810 LogicalToDeviceX(x
+ width
- rad
), LogicalToDeviceY(y
+ height
- rad
), LogicalToDeviceXRel(rad
),
811 LogicalToDeviceX(x
+ width
), LogicalToDeviceY(y
+ rad
),
812 LogicalToDeviceX(x
+ width
- rad
), LogicalToDeviceY(y
+ rad
), LogicalToDeviceXRel(rad
),
813 LogicalToDeviceX(x
+ rad
), LogicalToDeviceY(y
) );
815 CalcBoundingBox( x
, y
);
816 CalcBoundingBox( x
+ width
, y
+ height
);
819 if (m_pen
.GetStyle () != wxTRANSPARENT
)
823 /* Draw rectangle anticlockwise */
826 "%d %d %d 90 180 arc\n"
828 "%d %d %d 180 270 arc\n"
830 "%d %d %d 270 0 arc\n"
832 "%d %d %d 0 90 arc\n"
836 LogicalToDeviceX(x
+ rad
), LogicalToDeviceY(y
+ rad
), LogicalToDeviceXRel(rad
),
837 LogicalToDeviceX(x
), LogicalToDeviceY(y
+ rad
),
838 LogicalToDeviceX(x
+ rad
), LogicalToDeviceY(y
+ height
- rad
), LogicalToDeviceXRel(rad
),
839 LogicalToDeviceX(x
+ width
- rad
), LogicalToDeviceY(y
+ height
),
840 LogicalToDeviceX(x
+ width
- rad
), LogicalToDeviceY(y
+ height
- rad
), LogicalToDeviceXRel(rad
),
841 LogicalToDeviceX(x
+ width
), LogicalToDeviceY(y
+ rad
),
842 LogicalToDeviceX(x
+ width
- rad
), LogicalToDeviceY(y
+ rad
), LogicalToDeviceXRel(rad
),
843 LogicalToDeviceX(x
+ rad
), LogicalToDeviceY(y
) );
845 CalcBoundingBox( x
, y
);
846 CalcBoundingBox( x
+ width
, y
+ height
);
850 void wxPostScriptDC::DoDrawEllipse (wxCoord x
, wxCoord y
, wxCoord width
, wxCoord height
)
852 wxCHECK_RET( m_ok
&& m_pstream
, wxT("invalid postscript dc") );
854 if (m_brush
.GetStyle () != wxTRANSPARENT
)
860 "%d %d %d %d 0 360 ellipse\n"
862 LogicalToDeviceX(x
+ width
/ 2), LogicalToDeviceY(y
+ height
/ 2),
863 LogicalToDeviceXRel(width
/ 2), LogicalToDeviceYRel(height
/ 2) );
865 CalcBoundingBox( x
- width
, y
- height
);
866 CalcBoundingBox( x
+ width
, y
+ height
);
869 if (m_pen
.GetStyle () != wxTRANSPARENT
)
875 "%d %d %d %d 0 360 ellipse\n"
877 LogicalToDeviceX(x
+ width
/ 2), LogicalToDeviceY(y
+ height
/ 2),
878 LogicalToDeviceXRel(width
/ 2), LogicalToDeviceYRel(height
/ 2) );
880 CalcBoundingBox( x
- width
, y
- height
);
881 CalcBoundingBox( x
+ width
, y
+ height
);
885 void wxPostScriptDC::DoDrawIcon( const wxIcon
& icon
, wxCoord x
, wxCoord y
)
887 DrawBitmap( icon
, x
, y
, TRUE
);
890 /* this has to be char, not wxChar */
891 static char hexArray
[] = "0123456789ABCDEF";
892 static void LocalDecToHex( int dec
, char *buf
)
894 int firstDigit
= (int)(dec
/16.0);
895 int secondDigit
= (int)(dec
- (firstDigit
*16.0));
896 buf
[0] = hexArray
[firstDigit
];
897 buf
[1] = hexArray
[secondDigit
];
901 void wxPostScriptDC::DoDrawBitmap( const wxBitmap
& bitmap
, wxCoord x
, wxCoord y
, bool WXUNUSED(useMask
) )
903 wxCHECK_RET( m_ok
&& m_pstream
, wxT("invalid postscript dc") );
905 if (!bitmap
.Ok()) return;
907 wxImage image
= bitmap
.ConvertToImage();
909 if (!image
.Ok()) return;
911 wxCoord w
= image
.GetWidth();
912 wxCoord h
= image
.GetHeight();
914 wxCoord ww
= LogicalToDeviceXRel(image
.GetWidth());
915 wxCoord hh
= LogicalToDeviceYRel(image
.GetHeight());
917 wxCoord xx
= LogicalToDeviceX(x
);
918 wxCoord yy
= LogicalToDeviceY(y
+ bitmap
.GetHeight());
921 "/origstate save def\n"
923 "/pix %d string def\n"
924 "/grays %d string def\n"
931 "{currentfile pix readhexstring pop}\n"
932 "false 3 colorimage\n",
933 w
, w
, xx
, yy
, ww
, hh
, w
, h
, w
, -h
, h
);
936 for (int j
= 0; j
< h
; j
++)
938 for (int i
= 0; i
< w
; i
++)
941 LocalDecToHex( image
.GetRed(i
,j
), buffer
);
942 fprintf( m_pstream
, buffer
);
943 LocalDecToHex( image
.GetGreen(i
,j
), buffer
);
944 fprintf( m_pstream
, buffer
);
945 LocalDecToHex( image
.GetBlue(i
,j
), buffer
);
946 fprintf( m_pstream
, buffer
);
948 fprintf( m_pstream
, "\n" );
951 fprintf( m_pstream
, "end\n" );
952 fprintf( m_pstream
, "origstate restore\n" );
955 void wxPostScriptDC::SetFont( const wxFont
& font
)
957 wxCHECK_RET( m_ok
&& m_pstream
, wxT("invalid postscript dc") );
959 if (!font
.Ok()) return;
964 int Style
= m_font
.GetStyle();
965 int Weight
= m_font
.GetWeight();
968 switch (m_font
.GetFamily())
973 if (Style
== wxITALIC
)
975 if (Weight
== wxBOLD
)
976 name
= "/Courier-BoldOblique";
978 name
= "/Courier-Oblique";
982 if (Weight
== wxBOLD
)
983 name
= "/Courier-Bold";
991 if (Style
== wxITALIC
)
993 if (Weight
== wxBOLD
)
994 name
= "/Times-BoldItalic";
996 name
= "/Times-Italic";
1000 if (Weight
== wxBOLD
)
1001 name
= "/Times-Bold";
1003 name
= "/Times-Roman";
1009 name
= "/ZapfChancery-MediumItalic";
1017 if (Style
== wxITALIC
)
1019 if (Weight
== wxBOLD
)
1020 name
= "/Helvetica-BoldOblique";
1022 name
= "/Helvetica-Oblique";
1026 if (Weight
== wxBOLD
)
1027 name
= "/Helvetica-Bold";
1029 name
= "/Helvetica";
1035 fprintf( m_pstream
, name
);
1036 fprintf( m_pstream
, " reencodeISO def\n" );
1037 fprintf( m_pstream
, name
);
1038 fprintf( m_pstream
, " findfont\n" );
1041 sprintf( buffer
, "%f scalefont setfont\n", LogicalToDeviceYRel(m_font
.GetPointSize() * 1000) / 1000.0F
);
1042 // this is a hack - we must scale font size (in pts) according to m_scaleY but
1043 // LogicalToDeviceYRel works with wxCoord type (int or longint). Se we first convert font size
1044 // to 1/1000th of pt and then back.
1045 for (int i
= 0; i
< 100; i
++)
1046 if (buffer
[i
] == ',') buffer
[i
] = '.';
1047 fprintf( m_pstream
, buffer
);
1051 void wxPostScriptDC::SetPen( const wxPen
& pen
)
1053 wxCHECK_RET( m_ok
&& m_pstream
, wxT("invalid postscript dc") );
1055 if (!pen
.Ok()) return;
1057 int oldStyle
= m_pen
.GetStyle();
1064 sprintf( buffer
, "%f setlinewidth\n", LogicalToDeviceXRel(1000 * m_pen
.GetWidth()) / 1000.0f
);
1066 sprintf( buffer
, "%f setlinewidth\n", LogicalToDeviceXRel(1000 * m_pen
.GetWidth()) / 1000.0f
);
1068 for (int i
= 0; i
< 100; i
++)
1069 if (buffer
[i
] == ',') buffer
[i
] = '.';
1070 fprintf( m_pstream
, buffer
);
1074 Line style - WRONG: 2nd arg is OFFSET
1076 Here, I'm afraid you do not conceive meaning of parameters of 'setdash'
1077 operator correctly. You should look-up this in the Red Book: the 2nd parame-
1078 ter is not number of values in the array of the first one, but an offset
1079 into this description of the pattern. I mean a real *offset* not index
1080 into array. I.e. If the command is [3 4] 1 setdash is used, then there
1081 will be first black line *2* units wxCoord, then space 4 units, then the
1082 pattern of *3* units black, 4 units space will be repeated.
1085 static const char *dotted
= "[2 5] 2";
1086 static const char *short_dashed
= "[4 4] 2";
1087 static const char *wxCoord_dashed
= "[4 8] 2";
1088 static const char *dotted_dashed
= "[6 6 2 6] 4";
1090 const char *psdash
= (char *) NULL
;
1091 switch (m_pen
.GetStyle())
1093 case wxDOT
: psdash
= dotted
; break;
1094 case wxSHORT_DASH
: psdash
= short_dashed
; break;
1095 case wxLONG_DASH
: psdash
= wxCoord_dashed
; break;
1096 case wxDOT_DASH
: psdash
= dotted_dashed
; break;
1099 default: psdash
= "[] 0"; break;
1102 if (oldStyle
!= m_pen
.GetStyle())
1104 fprintf( m_pstream
, psdash
);
1105 fprintf( m_pstream
," setdash\n" );
1109 unsigned char red
= m_pen
.GetColour().Red();
1110 unsigned char blue
= m_pen
.GetColour().Blue();
1111 unsigned char green
= m_pen
.GetColour().Green();
1115 // Anything not white is black
1116 if (! (red
== (unsigned char) 255 &&
1117 blue
== (unsigned char) 255 &&
1118 green
== (unsigned char) 255) )
1120 red
= (unsigned char) 0;
1121 green
= (unsigned char) 0;
1122 blue
= (unsigned char) 0;
1127 if (!(red
== m_currentRed
&& green
== m_currentGreen
&& blue
== m_currentBlue
))
1129 double redPS
= (double)(red
) / 255.0;
1130 double bluePS
= (double)(blue
) / 255.0;
1131 double greenPS
= (double)(green
) / 255.0;
1135 "%.8f %.8f %.8f setrgbcolor\n",
1136 redPS
, greenPS
, bluePS
);
1137 for (int i
= 0; i
< 100; i
++)
1138 if (buffer
[i
] == ',') buffer
[i
] = '.';
1139 fprintf( m_pstream
, buffer
);
1142 m_currentBlue
= blue
;
1143 m_currentGreen
= green
;
1147 void wxPostScriptDC::SetBrush( const wxBrush
& brush
)
1149 wxCHECK_RET( m_ok
&& m_pstream
, wxT("invalid postscript dc") );
1151 if (!brush
.Ok()) return;
1156 unsigned char red
= m_brush
.GetColour().Red();
1157 unsigned char blue
= m_brush
.GetColour().Blue();
1158 unsigned char green
= m_brush
.GetColour().Green();
1162 // Anything not white is black
1163 if (! (red
== (unsigned char) 255 &&
1164 blue
== (unsigned char) 255 &&
1165 green
== (unsigned char) 255) )
1167 red
= (unsigned char) 0;
1168 green
= (unsigned char) 0;
1169 blue
= (unsigned char) 0;
1174 if (!(red
== m_currentRed
&& green
== m_currentGreen
&& blue
== m_currentBlue
))
1176 double redPS
= (double)(red
) / 255.0;
1177 double bluePS
= (double)(blue
) / 255.0;
1178 double greenPS
= (double)(green
) / 255.0;
1182 "%.8f %.8f %.8f setrgbcolor\n",
1183 redPS
, greenPS
, bluePS
);
1184 for (int i
= 0; i
< 100; i
++)
1185 if (buffer
[i
] == ',') buffer
[i
] = '.';
1186 fprintf( m_pstream
, buffer
);
1189 m_currentBlue
= blue
;
1190 m_currentGreen
= green
;
1196 #include "wx/gtk/private.h"
1197 #include "wx/fontutil.h"
1198 #include "gtk/gtk.h"
1199 #include <pango/pangoft2.h>
1200 #include <freetype/ftglyph.h>
1202 #ifndef FT_Outline_Decompose
1203 FT_EXPORT( FT_Error
) FT_Outline_Decompose(
1204 FT_Outline
* outline
,
1205 const FT_Outline_Funcs
* interface
,
1209 typedef struct _OutlineInfo OutlineInfo
;
1210 struct _OutlineInfo
{
1212 FT_Vector glyph_origin
;
1216 static int paps_move_to( FT_Vector
* to
,
1219 OutlineInfo
*outline_info
= (OutlineInfo
*)user_data
;
1220 fprintf(outline_info
->OUT
, "%d %d moveto\n",
1226 static int paps_line_to( FT_Vector
* to
,
1229 OutlineInfo
*outline_info
= (OutlineInfo
*)user_data
;
1230 fprintf(outline_info
->OUT
, "%d %d lineto\n",
1236 static int paps_conic_to( FT_Vector
* control
,
1240 OutlineInfo
*outline_info
= (OutlineInfo
*)user_data
;
1241 fprintf(outline_info
->OUT
, "%d %d %d %d conicto\n",
1249 static int paps_cubic_to( FT_Vector
* control1
,
1250 FT_Vector
* control2
,
1254 OutlineInfo
*outline_info
= (OutlineInfo
*)user_data
;
1255 fprintf(outline_info
->OUT
,
1256 "%d %d %d %d %d %d curveto\n",
1266 static void draw_bezier_outline(FILE *OUT
,
1269 FT_UInt glyph_index
,
1274 FT_Int load_flags
= FT_LOAD_DEFAULT
;
1277 /* Output outline */
1278 FT_Outline_Funcs outlinefunc
=
1285 OutlineInfo outline_info
;
1287 outline_info
.glyph_origin
.x
= (FT_Pos
) pos_x
;
1288 outline_info
.glyph_origin
.y
= (FT_Pos
) pos_y
;
1289 outline_info
.dpi
= dpi
;
1290 outline_info
.OUT
= OUT
;
1292 fprintf(OUT
, "gsave %d %d translate 0 0 0 setrgbcolor\n", pos_x
, pos_y
);
1293 fprintf(OUT
, "start_ol\n");
1295 FT_Load_Glyph(face
, glyph_index
, load_flags
);
1296 FT_Get_Glyph (face
->glyph
, &glyph
);
1297 FT_Outline_Decompose (&(((FT_OutlineGlyph
)glyph
)->outline
),
1298 &outlinefunc
, &outline_info
);
1299 fprintf(OUT
, "end_ol grestore \n");
1301 FT_Done_Glyph (glyph
);
1307 void wxPostScriptDC::DoDrawText( const wxString
& text
, wxCoord x
, wxCoord y
)
1309 wxCHECK_RET( m_ok
&& m_pstream
, wxT("invalid postscript dc") );
1313 int dpi
= GetResolution() * 2;
1316 PangoContext
*context
= pango_ft2_get_context ( dpi
, dpi
);
1319 // What are these for?
1320 pango_context_set_language (context
, pango_language_from_string ("en_US"));
1321 pango_context_set_base_dir (context
, PANGO_DIRECTION_LTR
);
1324 pango_context_set_font_description (context
, m_font
.GetNativeFontInfo()->description
);
1327 PangoLayout
*layout
= layout
= pango_layout_new (context
);
1329 wxCharBuffer buffer
= wxConvUTF8
.cWC2MB( text
);
1331 wxCharBuffer buffer
= wxConvUTF8
.cWC2MB( wxConvLocal
.cWX2WC( text
) );
1333 pango_layout_set_text( layout
, (const char*) buffer
, strlen(buffer
) );
1336 double xx
= LogicalToDeviceX(x
);
1337 double yy
= LogicalToDeviceY(y
/*+ bitmap.GetHeight()*/ );
1339 // Loop over lines in layout
1340 int num_lines
= pango_layout_get_line_count( layout
);
1341 for (int i
= 0; i
< num_lines
; i
++)
1343 PangoLayoutLine
*line
= pango_layout_get_line( layout
, i
);
1345 // Loop over runs in line
1346 GSList
*runs_list
= line
->runs
;
1349 PangoLayoutRun
*run
= (PangoLayoutRun
*) runs_list
->data
;
1350 PangoItem
*item
= run
->item
;
1351 PangoGlyphString
*glyphs
= run
->glyphs
;
1352 PangoAnalysis
*analysis
= &item
->analysis
;
1353 PangoFont
*font
= analysis
->font
;
1354 FT_Face ft_face
= pango_ft2_font_get_face(font
);
1356 int num_glyphs
= glyphs
->num_glyphs
;
1357 for (int glyph_idx
= 0; glyph_idx
< num_glyphs
; glyph_idx
++)
1359 PangoGlyphGeometry geometry
= glyphs
->glyphs
[glyph_idx
].geometry
;
1360 double pos_x
= xx
+ 1.0 * geometry
.x_offset
/ PANGO_SCALE
;
1361 double pos_y
= yy
- 1.0 * geometry
.y_offset
/ PANGO_SCALE
;
1362 xx
+= 1.0 * geometry
.width
/ PANGO_SCALE
;
1364 draw_bezier_outline( m_pstream
, dpi
, ft_face
,
1365 (FT_UInt
)(glyphs
->glyphs
[glyph_idx
].glyph
),
1366 (wxCoord
)pos_x
, (wxCoord
)pos_y
);
1368 runs_list
= runs_list
->next
;
1372 // Find out extent for the bitmap
1375 PangoRectangle logical_rect
;
1376 pango_layout_get_extents (layout
, NULL
, &logical_rect
);
1377 height
= PANGO_PIXELS (logical_rect
.height
);
1378 width
= PANGO_PIXELS (logical_rect
.width
);
1380 // printf( "h %d w %d lh %d lw %d\n", height, width, logical_rect.height, logical_rect.width );
1382 // Allocate FreeType 2 bitmap
1383 int byte_width
= (width
+ 7)/8 * 8;
1385 guchar
*buf
= (guchar
*) g_malloc (byte_width
* height
);
1386 memset (buf
, 0x00, byte_width
* height
);
1387 bitmap
.rows
= height
;
1388 bitmap
.width
= byte_width
;
1389 bitmap
.pitch
= byte_width
;
1390 bitmap
.buffer
= buf
;
1391 bitmap
.num_grays
= 256;
1392 bitmap
.pixel_mode
= ft_pixel_mode_grays
;
1395 pango_ft2_render_layout (&bitmap
, layout
, 0, 0);
1397 // Invert bitmap to get black text on white background
1398 for (int pix_idx
= 0; pix_idx
< width
* height
; pix_idx
++)
1399 buf
[pix_idx
] = 255-buf
[pix_idx
];
1402 wxCoord xx
= LogicalToDeviceX(x
);
1403 wxCoord yy
= LogicalToDeviceY(y
/*+ bitmap.GetHeight()*/ );
1405 fprintf(m_pstream
, "gsave\n");
1406 fprintf(m_pstream
, "%d %d translate\n", xx
, yy
);
1407 fprintf(m_pstream
, "/img_width %d def\n", bitmap
.width
);
1408 fprintf(m_pstream
, "/img_height %d def\n", bitmap
.rows
);
1409 fprintf(m_pstream
, "/picstr img_width 8 idiv string def\n");
1412 " img_width 72 15 div mul\n"
1413 " img_height 72 15 div mul scale\n"
1415 " img_width img_height\n"
1417 " [img_width 0 0 img_height neg 0 img_height 0.67 mul]\n"
1419 " picstr readhexstring pop }\n"
1424 for (int b_idx
= 0; b_idx
< bitmap
.width
/8 * bitmap
.rows
; b_idx
++)
1426 guchar packed_b
= 0;
1429 if (b_idx
% (bitmap
.width
/8) == 0)
1430 fprintf(m_pstream
, "\n");
1432 for (bit_idx
= 0; bit_idx
< 8; bit_idx
++)
1434 guchar this_bit
= bitmap
.buffer
[b_idx
* 8+bit_idx
]<128;
1435 packed_b
= (packed_b
<< 1) + this_bit
;
1437 fprintf(m_pstream
, "%02x", packed_b
);
1440 fprintf(m_pstream
, "\ngrestore\n" );
1447 wxCoord text_w
, text_h
, text_descent
;
1449 GetTextExtent(text
, &text_w
, &text_h
, &text_descent
);
1451 // VZ: this seems to be unnecessary, so taking it out for now, if it
1452 // doesn't create any problems, remove this comment entirely
1453 //SetFont( m_font );
1455 if (m_textForegroundColour
.Ok())
1457 unsigned char red
= m_textForegroundColour
.Red();
1458 unsigned char blue
= m_textForegroundColour
.Blue();
1459 unsigned char green
= m_textForegroundColour
.Green();
1463 // Anything not white is black
1464 if (! (red
== (unsigned char) 255 &&
1465 blue
== (unsigned char) 255 &&
1466 green
== (unsigned char) 255))
1468 red
= (unsigned char) 0;
1469 green
= (unsigned char) 0;
1470 blue
= (unsigned char) 0;
1474 // maybe setgray here ?
1475 if (!(red
== m_currentRed
&& green
== m_currentGreen
&& blue
== m_currentBlue
))
1477 double redPS
= (double)(red
) / 255.0;
1478 double bluePS
= (double)(blue
) / 255.0;
1479 double greenPS
= (double)(green
) / 255.0;
1483 "%.8f %.8f %.8f setrgbcolor\n",
1484 redPS
, greenPS
, bluePS
);
1485 for (int i
= 0; i
< 100; i
++)
1486 if (buffer
[i
] == ',') buffer
[i
] = '.';
1487 fprintf( m_pstream
, buffer
);
1490 m_currentBlue
= blue
;
1491 m_currentGreen
= green
;
1495 int size
= m_font
.GetPointSize();
1497 // wxCoord by = y + (wxCoord)floor( double(size) * 2.0 / 3.0 ); // approximate baseline
1498 // commented by V. Slavik and replaced by accurate version
1499 // - note that there is still rounding error in text_descent!
1500 wxCoord by
= y
+ size
- text_descent
; // baseline
1501 fprintf( m_pstream
, "%d %d moveto\n", LogicalToDeviceX(x
), LogicalToDeviceY(by
) );
1503 fprintf( m_pstream
, "(" );
1504 const wxWX2MBbuf textbuf
= text
.mb_str();
1505 size_t len
= strlen(textbuf
);
1507 for (i
= 0; i
< len
; i
++)
1509 int c
= (unsigned char) textbuf
[i
];
1510 if (c
== ')' || c
== '(' || c
== '\\')
1512 /* Cope with special characters */
1513 fprintf( m_pstream
, "\\" );
1514 fputc(c
, m_pstream
);
1516 else if ( c
>= 128 )
1518 /* Cope with character codes > 127 */
1519 fprintf(m_pstream
, "\\%o", c
);
1523 fputc(c
, m_pstream
);
1527 fprintf( m_pstream
, ") show\n" );
1529 if (m_font
.GetUnderlined())
1531 wxCoord uy
= (wxCoord
)(y
+ size
- m_underlinePosition
);
1541 LogicalToDeviceX(x
), LogicalToDeviceY(uy
),
1542 m_underlineThickness
,
1543 LogicalToDeviceX(x
+ text_w
), LogicalToDeviceY(uy
) );
1544 for (i
= 0; i
< 100; i
++)
1545 if (buffer
[i
] == ',') buffer
[i
] = '.';
1546 fprintf( m_pstream
, buffer
);
1549 CalcBoundingBox( x
, y
);
1550 CalcBoundingBox( x
+ size
* text
.Length() * 2/3 , y
);
1554 void wxPostScriptDC::DoDrawRotatedText( const wxString
& text
, wxCoord x
, wxCoord y
, double angle
)
1558 DoDrawText(text
, x
, y
);
1562 wxCHECK_RET( m_ok
&& m_pstream
, wxT("invalid postscript dc") );
1566 if (m_textForegroundColour
.Ok())
1568 unsigned char red
= m_textForegroundColour
.Red();
1569 unsigned char blue
= m_textForegroundColour
.Blue();
1570 unsigned char green
= m_textForegroundColour
.Green();
1574 // Anything not white is black
1575 if (! (red
== (unsigned char) 255 &&
1576 blue
== (unsigned char) 255 &&
1577 green
== (unsigned char) 255))
1579 red
= (unsigned char) 0;
1580 green
= (unsigned char) 0;
1581 blue
= (unsigned char) 0;
1585 // maybe setgray here ?
1586 if (!(red
== m_currentRed
&& green
== m_currentGreen
&& blue
== m_currentBlue
))
1588 double redPS
= (double)(red
) / 255.0;
1589 double bluePS
= (double)(blue
) / 255.0;
1590 double greenPS
= (double)(green
) / 255.0;
1594 "%.8f %.8f %.8f setrgbcolor\n",
1595 redPS
, greenPS
, bluePS
);
1596 for (int i
= 0; i
< 100; i
++)
1597 if (buffer
[i
] == ',') buffer
[i
] = '.';
1598 fprintf( m_pstream
, buffer
);
1601 m_currentBlue
= blue
;
1602 m_currentGreen
= green
;
1606 int size
= m_font
.GetPointSize();
1608 long by
= y
+ (long)floor( double(size
) * 2.0 / 3.0 ); // approximate baseline
1610 // FIXME only correct for 90 degrees
1611 fprintf(m_pstream
, "%d %d moveto\n",
1612 LogicalToDeviceX((wxCoord
)(x
+ size
)), LogicalToDeviceY((wxCoord
)by
) );
1615 sprintf(buffer
, "%.8f rotate\n", angle
);
1617 for (i
= 0; i
< 100; i
++)
1618 if (buffer
[i
] == ',') buffer
[i
] = '.';
1619 fprintf(m_pstream
, buffer
);
1621 fprintf( m_pstream
, "(" );
1622 const wxWX2MBbuf textbuf
= text
.mb_str();
1623 size_t len
= strlen(textbuf
);
1624 for (i
= 0; i
< len
; i
++)
1626 int c
= (unsigned char) textbuf
[i
];
1627 if (c
== ')' || c
== '(' || c
== '\\')
1629 /* Cope with special characters */
1630 fprintf( m_pstream
, "\\" );
1631 fputc(c
, m_pstream
);
1633 else if ( c
>= 128 )
1635 /* Cope with character codes > 127 */
1636 fprintf(m_pstream
, "\\%o", c
);
1640 fputc(c
, m_pstream
);
1644 fprintf( m_pstream
, ") show\n" );
1646 sprintf( buffer
, "%.8f rotate\n", -angle
);
1647 for (i
= 0; i
< 100; i
++)
1648 if (buffer
[i
] == ',') buffer
[i
] = '.';
1649 fprintf( m_pstream
, buffer
);
1651 if (m_font
.GetUnderlined())
1653 wxCoord uy
= (wxCoord
)(y
+ size
- m_underlinePosition
);
1656 GetTextExtent(text
, &w
, &h
);
1665 LogicalToDeviceX(x
), LogicalToDeviceY(uy
),
1666 m_underlineThickness
,
1667 LogicalToDeviceX(x
+ w
), LogicalToDeviceY(uy
) );
1668 for (i
= 0; i
< 100; i
++)
1669 if (buffer
[i
] == ',') buffer
[i
] = '.';
1670 fprintf( m_pstream
, buffer
);
1673 CalcBoundingBox( x
, y
);
1674 CalcBoundingBox( x
+ size
* text
.Length() * 2/3 , y
);
1677 void wxPostScriptDC::SetBackground (const wxBrush
& brush
)
1679 m_backgroundBrush
= brush
;
1682 void wxPostScriptDC::SetLogicalFunction (int WXUNUSED(function
))
1684 wxFAIL_MSG( wxT("wxPostScriptDC::SetLogicalFunction not implemented.") );
1687 void wxPostScriptDC::DoDrawSpline( wxList
*points
)
1689 wxCHECK_RET( m_ok
&& m_pstream
, wxT("invalid postscript dc") );
1693 double a
, b
, c
, d
, x1
, y1
, x2
, y2
, x3
, y3
;
1696 wxNode
*node
= points
->First();
1697 p
= (wxPoint
*)node
->Data();
1701 node
= node
->Next();
1702 p
= (wxPoint
*)node
->Data();
1705 x3
= a
= (double)(x1
+ c
) / 2;
1706 y3
= b
= (double)(y1
+ d
) / 2;
1712 LogicalToDeviceX((wxCoord
)x1
), LogicalToDeviceY((wxCoord
)y1
),
1713 LogicalToDeviceX((wxCoord
)x3
), LogicalToDeviceY((wxCoord
)y3
) );
1715 CalcBoundingBox( (wxCoord
)x1
, (wxCoord
)y1
);
1716 CalcBoundingBox( (wxCoord
)x3
, (wxCoord
)y3
);
1718 while ((node
= node
->Next()) != NULL
)
1720 q
= (wxPoint
*)node
->Data();
1728 x3
= (double)(x2
+ c
) / 2;
1729 y3
= (double)(y2
+ d
) / 2;
1732 "%d %d %d %d %d %d DrawSplineSection\n",
1733 LogicalToDeviceX((wxCoord
)x1
), LogicalToDeviceY((wxCoord
)y1
),
1734 LogicalToDeviceX((wxCoord
)x2
), LogicalToDeviceY((wxCoord
)y2
),
1735 LogicalToDeviceX((wxCoord
)x3
), LogicalToDeviceY((wxCoord
)y3
) );
1737 CalcBoundingBox( (wxCoord
)x1
, (wxCoord
)y1
);
1738 CalcBoundingBox( (wxCoord
)x3
, (wxCoord
)y3
);
1742 At this point, (x2,y2) and (c,d) are the position of the
1743 next-to-last and last point respectively, in the point list
1749 LogicalToDeviceX((wxCoord
)c
), LogicalToDeviceY((wxCoord
)d
) );
1752 wxCoord
wxPostScriptDC::GetCharWidth() const
1754 // Chris Breeze: reasonable approximation using wxMODERN/Courier
1755 return (wxCoord
) (GetCharHeight() * 72.0 / 120.0);
1759 void wxPostScriptDC::SetAxisOrientation( bool xLeftRight
, bool yBottomUp
)
1761 wxCHECK_RET( m_ok
&& m_pstream
, wxT("invalid postscript dc") );
1763 m_signX
= (xLeftRight
? 1 : -1);
1764 m_signY
= (yBottomUp
? 1 : -1);
1766 // FIXME there is no such function in MSW nor in OS2/PM
1767 #if !defined(__WXMSW__) && !defined(__WXPM__)
1768 ComputeScaleAndOrigin();
1772 void wxPostScriptDC::SetDeviceOrigin( wxCoord x
, wxCoord y
)
1774 wxCHECK_RET( m_ok
&& m_pstream
, wxT("invalid postscript dc") );
1780 wxDC::SetDeviceOrigin( x
, h
-y
);
1783 void wxPostScriptDC::DoGetSize(int* width
, int* height
) const
1785 wxPaperSize id
= m_printData
.GetPaperId();
1787 wxPrintPaperType
*paper
= wxThePrintPaperDatabase
->FindPaperType(id
);
1789 if (!paper
) paper
= wxThePrintPaperDatabase
->FindPaperType(wxPAPER_A4
);
1795 w
= paper
->GetSizeDeviceUnits().x
;
1796 h
= paper
->GetSizeDeviceUnits().y
;
1799 if (m_printData
.GetOrientation() == wxLANDSCAPE
)
1806 if (width
) *width
= (int)(w
* ms_PSScaleFactor
);
1807 if (height
) *height
= (int)(h
* ms_PSScaleFactor
);
1810 void wxPostScriptDC::DoGetSizeMM(int *width
, int *height
) const
1812 wxPaperSize id
= m_printData
.GetPaperId();
1814 wxPrintPaperType
*paper
= wxThePrintPaperDatabase
->FindPaperType(id
);
1816 if (!paper
) paper
= wxThePrintPaperDatabase
->FindPaperType(wxPAPER_A4
);
1822 w
= paper
->GetWidth() / 10;
1823 h
= paper
->GetHeight() / 10;
1826 if (m_printData
.GetOrientation() == wxLANDSCAPE
)
1833 if (width
) *width
= w
;
1834 if (height
) *height
= h
;
1837 // Resolution in pixels per logical inch
1838 wxSize
wxPostScriptDC::GetPPI(void) const
1840 return wxSize((int)(72 * ms_PSScaleFactor
),
1841 (int)(72 * ms_PSScaleFactor
));
1845 bool wxPostScriptDC::StartDoc( const wxString
& message
)
1847 wxCHECK_MSG( m_ok
, FALSE
, wxT("invalid postscript dc") );
1849 if (m_printData
.GetFilename() == "")
1851 wxString filename
= wxGetTempFileName("ps");
1852 m_printData
.SetFilename(filename
);
1855 m_pstream
= wxFopen( m_printData
.GetFilename().c_str(), wxT("w+") ); // FIXME: use fn_str() here under Unicode?
1859 wxLogError( _("Cannot open file for PostScript printing!"));
1866 fprintf( m_pstream
, "%%%%BeginProlog\n" );
1867 fprintf( m_pstream
, wxPostScriptHeaderConicTo
);
1868 fprintf( m_pstream
, wxPostScriptHeaderEllipse
);
1869 fprintf( m_pstream
, wxPostScriptHeaderEllipticArc
);
1870 fprintf( m_pstream
, wxPostScriptHeaderColourImage
);
1872 fprintf( m_pstream
, wxPostScriptHeaderReencodeISO1
);
1873 fprintf( m_pstream
, wxPostScriptHeaderReencodeISO2
);
1875 if (wxPostScriptHeaderSpline
)
1876 fprintf( m_pstream
, wxPostScriptHeaderSpline
);
1877 fprintf( m_pstream
, "%%%%EndProlog\n" );
1879 SetBrush( *wxBLACK_BRUSH
);
1880 SetPen( *wxBLACK_PEN
);
1881 SetBackground( *wxWHITE_BRUSH
);
1882 SetTextForeground( *wxBLACK
);
1884 // set origin according to paper size
1885 SetDeviceOrigin( 0,0 );
1893 void wxPostScriptDC::EndDoc ()
1895 wxCHECK_RET( m_ok
&& m_pstream
, wxT("invalid postscript dc") );
1900 fprintf( m_pstream
, "grestore\n" );
1903 fclose( m_pstream
);
1904 m_pstream
= (FILE *) NULL
;
1906 wxChar
*header_file
= wxGetTempFileName("ps");
1908 m_pstream
= wxFopen( header_file
, wxT("w+") );
1910 fprintf( m_pstream
, "%%!PS-Adobe-2.0\n" ); // PostScript magic strings
1911 fprintf( m_pstream
, "%%%%Title: %s\n", (const char *)m_title
.mb_str() );
1912 fprintf( m_pstream
, "%%%%Creator: wxWindows PostScript renderer\n" );
1913 fprintf( m_pstream
, "%%%%CreationDate: %s\n", (const char *)wxNow().mb_str() );
1916 // THE FOLLOWING HAS BEEN CONTRIBUTED BY Andy Fyfe <andy@hyperparallel.com>
1918 wxCoord wx_printer_translate_x
, wx_printer_translate_y
;
1919 double wx_printer_scale_x
, wx_printer_scale_y
;
1921 wx_printer_translate_x
= (wxCoord
)m_printData
.GetPrinterTranslateX();
1922 wx_printer_translate_y
= (wxCoord
)m_printData
.GetPrinterTranslateY();
1924 wx_printer_scale_x
= m_printData
.GetPrinterScaleX();
1925 wx_printer_scale_y
= m_printData
.GetPrinterScaleY();
1927 if (m_printData
.GetOrientation() == wxLANDSCAPE
)
1928 fprintf( m_pstream
, "%%%%Orientation: Landscape\n" );
1930 fprintf( m_pstream
, "%%%%Orientation: Portrait\n" );
1932 // Compute the bounding box. Note that it is in the default user
1933 // coordinate system, thus we have to convert the values.
1934 wxCoord minX
= (wxCoord
) LogicalToDeviceX(m_minX
);
1935 wxCoord minY
= (wxCoord
) LogicalToDeviceY(m_minY
);
1936 wxCoord maxX
= (wxCoord
) LogicalToDeviceX(m_maxX
);
1937 wxCoord maxY
= (wxCoord
) LogicalToDeviceY(m_maxY
);
1939 // LOG2DEV may have changed the minimum to maximum vice versa
1940 if ( minX
> maxX
) { wxCoord tmp
= minX
; minX
= maxX
; maxX
= tmp
; }
1941 if ( minY
> maxY
) { wxCoord tmp
= minY
; minY
= maxY
; maxY
= tmp
; }
1943 // account for used scaling (boundingbox is before scaling in ps-file)
1944 double scale_x
= m_printData
.GetPrinterScaleX() / ms_PSScaleFactor
;
1945 double scale_y
= m_printData
.GetPrinterScaleY() / ms_PSScaleFactor
;
1947 wxCoord llx
, lly
, urx
, ury
;
1948 llx
= (wxCoord
) ((minX
+wx_printer_translate_x
)*scale_x
);
1949 lly
= (wxCoord
) ((minY
+wx_printer_translate_y
)*scale_y
);
1950 urx
= (wxCoord
) ((maxX
+wx_printer_translate_x
)*scale_x
);
1951 ury
= (wxCoord
) ((maxY
+wx_printer_translate_y
)*scale_y
);
1952 // (end of bounding box computation)
1955 // If we're landscape, our sense of "x" and "y" is reversed.
1956 if (m_printData
.GetOrientation() == wxLANDSCAPE
)
1959 tmp
= llx
; llx
= lly
; lly
= tmp
;
1960 tmp
= urx
; urx
= ury
; ury
= tmp
;
1962 // We need either the two lines that follow, or we need to subtract
1963 // min_x from real_translate_y, which is commented out below.
1964 llx
= llx
- (wxCoord
)(m_minX
*wx_printer_scale_y
);
1965 urx
= urx
- (wxCoord
)(m_minX
*wx_printer_scale_y
);
1968 // The Adobe specifications call for integers; we round as to make
1969 // the bounding larger.
1971 "%%%%BoundingBox: %d %d %d %d\n",
1972 (wxCoord
)floor((double)llx
), (wxCoord
)floor((double)lly
),
1973 (wxCoord
)ceil((double)urx
), (wxCoord
)ceil((double)ury
) );
1974 fprintf( m_pstream
, "%%%%Pages: %d\n", (wxPageNumber
- 1) );
1975 fprintf( m_pstream
, "%%%%EndComments\n\n" );
1977 // To check the correctness of the bounding box, postscript commands
1978 // to draw a box corresponding to the bounding box are generated below.
1979 // But since we typically don't want to print such a box, the postscript
1980 // commands are generated within comments. These lines appear before any
1981 // adjustment of scale, rotation, or translation, and hence are in the
1982 // default user coordinates.
1983 fprintf( m_pstream
, "%% newpath\n" );
1984 fprintf( m_pstream
, "%% %d %d moveto\n", llx
, lly
);
1985 fprintf( m_pstream
, "%% %d %d lineto\n", urx
, lly
);
1986 fprintf( m_pstream
, "%% %d %d lineto\n", urx
, ury
);
1987 fprintf( m_pstream
, "%% %d %d lineto closepath stroke\n", llx
, ury
);
1989 fclose( m_pstream
);
1990 m_pstream
= (FILE*) NULL
;
1992 wxChar
*tmp_file
= wxGetTempFileName("ps");
1994 // Paste header Before wx_printer_file
1995 wxConcatFiles (header_file
, m_printData
.GetFilename(), tmp_file
);
1996 wxRemoveFile( header_file
);
1997 wxRemoveFile( m_printData
.GetFilename() );
1998 wxRenameFile( tmp_file
, m_printData
.GetFilename() );
2000 #if defined(__X__) || defined(__WXGTK__)
2003 wxString
previewCommand(m_printData
.GetPreviewCommand());
2004 wxString
printerCommand(m_printData
.GetPrinterCommand());
2005 wxString
printerOptions(m_printData
.GetPrinterOptions());
2006 wxString
filename(m_printData
.GetFilename());
2008 switch (m_printData
.GetPrintMode())
2010 case wxPRINT_MODE_PREVIEW
:
2013 argv
[0] = WXSTRINGCAST previewCommand
;
2014 argv
[1] = WXSTRINGCAST filename
;
2015 argv
[2] = (wxChar
*) NULL
;
2016 #if defined(__WXGTK20__) && wxUSE_UNICODE
2018 wxExecute( argv
, TRUE
);
2020 wxRemoveFile( m_printData
.GetFilename() );
2023 case wxPRINT_MODE_PRINTER
:
2027 argv
[argc
++] = WXSTRINGCAST printerCommand
;
2029 // !SM! If we simply assign to argv[1] here, if printer options
2030 // are blank, we get an annoying and confusing message from lpr.
2031 wxChar
*opts
= WXSTRINGCAST printerOptions
;
2033 argv
[argc
++] = opts
;
2035 argv
[argc
++] = WXSTRINGCAST filename
;
2036 argv
[argc
++] = (wxChar
*) NULL
;
2037 #if defined(__WXGTK20__) && wxUSE_UNICODE
2039 wxExecute( argv
, TRUE
);
2041 wxRemoveFile( filename
);
2044 case wxPRINT_MODE_FILE
:
2045 case wxPRINT_MODE_NONE
:
2052 void wxPostScriptDC::StartPage()
2054 wxCHECK_RET( m_ok
&& m_pstream
, wxT("invalid postscript dc") );
2056 fprintf( m_pstream
, "%%%%Page: %d\n", wxPageNumber
++ );
2058 // What is this one supposed to do? RR.
2059 // *m_pstream << "matrix currentmatrix\n";
2061 // Added by Chris Breeze
2063 // Each page starts with an "initgraphics" which resets the
2064 // transformation and so we need to reset the origin
2065 // (and rotate the page for landscape printing)
2068 wxCoord translate_x
, translate_y
;
2069 double scale_x
, scale_y
;
2071 translate_x
= (wxCoord
)m_printData
.GetPrinterTranslateX();
2072 translate_y
= (wxCoord
)m_printData
.GetPrinterTranslateY();
2074 scale_x
= m_printData
.GetPrinterScaleX();
2075 scale_y
= m_printData
.GetPrinterScaleY();
2077 if (m_printData
.GetOrientation() == wxLANDSCAPE
)
2080 GetSize( (int*) NULL
, &h
);
2082 fprintf( m_pstream
, "90 rotate\n" );
2084 // I copied this one from a PostScript tutorial, but to no avail. RR.
2085 // fprintf( m_pstream, "90 rotate llx neg ury nef translate\n" );
2089 sprintf( buffer
, "%.8f %.8f scale\n", scale_x
/ ms_PSScaleFactor
,
2090 scale_y
/ ms_PSScaleFactor
);
2091 for (int i
= 0; i
< 100; i
++)
2092 if (buffer
[i
] == ',') buffer
[i
] = '.';
2093 fprintf( m_pstream
, buffer
);
2095 fprintf( m_pstream
, "%d %d translate\n", translate_x
, translate_y
);
2098 void wxPostScriptDC::EndPage ()
2100 wxCHECK_RET( m_ok
&& m_pstream
, wxT("invalid postscript dc") );
2102 fprintf( m_pstream
, "showpage\n" );
2105 bool wxPostScriptDC::DoBlit( wxCoord xdest
, wxCoord ydest
,
2106 wxCoord fwidth
, wxCoord fheight
,
2108 wxCoord xsrc
, wxCoord ysrc
,
2109 int rop
, bool WXUNUSED(useMask
), wxCoord
WXUNUSED(xsrcMask
), wxCoord
WXUNUSED(ysrcMask
) )
2111 wxCHECK_MSG( m_ok
&& m_pstream
, FALSE
, wxT("invalid postscript dc") );
2113 wxCHECK_MSG( source
, FALSE
, wxT("invalid source dc") );
2115 /* blit into a bitmap */
2116 wxBitmap
bitmap( (int)fwidth
, (int)fheight
);
2118 memDC
.SelectObject(bitmap
);
2119 memDC
.Blit(0, 0, fwidth
, fheight
, source
, xsrc
, ysrc
, rop
); /* TODO: Blit transparently? */
2120 memDC
.SelectObject(wxNullBitmap
);
2122 /* draw bitmap. scaling and positioning is done there */
2123 DrawBitmap( bitmap
, xdest
, ydest
);
2128 wxCoord
wxPostScriptDC::GetCharHeight() const
2131 return m_font
.GetPointSize();
2136 void wxPostScriptDC::DoGetTextExtent(const wxString
& string
,
2137 wxCoord
*x
, wxCoord
*y
,
2138 wxCoord
*descent
, wxCoord
*externalLeading
,
2139 wxFont
*theFont
) const
2141 wxFont
*fontToUse
= theFont
;
2143 if (!fontToUse
) fontToUse
= (wxFont
*) &m_font
;
2145 wxCHECK_RET( fontToUse
, wxT("GetTextExtent: no font defined") );
2147 const wxWX2MBbuf strbuf
= string
.mb_str();
2149 #if !wxUSE_AFM_FOR_POSTSCRIPT
2150 /* Provide a VERY rough estimate (avoid using it).
2151 * Produces accurate results for mono-spaced font
2152 * such as Courier (aka wxMODERN) */
2157 height
= fontToUse
->GetPointSize();
2160 *x
= strlen (strbuf
) * height
* 72 / 120;
2162 *y
= (wxCoord
) (height
* 1.32); /* allow for descender */
2163 if (descent
) *descent
= 0;
2164 if (externalLeading
) *externalLeading
= 0;
2167 /* method for calculating string widths in postscript:
2168 / read in the AFM (adobe font metrics) file for the
2169 / actual font, parse it and extract the character widths
2170 / and also the descender. this may be improved, but for now
2171 / it works well. the AFM file is only read in if the
2172 / font is changed. this may be chached in the future.
2173 / calls to GetTextExtent with the font unchanged are rather
2176 / for each font and style used there is an AFM file necessary.
2177 / currently i have only files for the roman font family.
2178 / I try to get files for the other ones!
2180 / CAVE: the size of the string is currently always calculated
2181 / in 'points' (1/72 of an inch). this should later on be
2182 / changed to depend on the mapping mode.
2183 / CAVE: the path to the AFM files must be set before calling this
2184 / function. this is usually done by a call like the following:
2185 / wxSetAFMPath("d:\\wxw161\\afm\\");
2189 / wxPostScriptDC dc(NULL, TRUE);
2191 / wxSetAFMPath("d:\\wxw161\\afm\\");
2192 / dc.StartDoc("Test");
2195 / dc.SetFont(new wxFont(10, wxROMAN, wxNORMAL, wxNORMAL));
2196 / dc.GetTextExtent("Hallo",&w,&h);
2201 / by steve (stefan.hammes@urz.uni-heidelberg.de)
2203 / updated: 14.05.95 */
2205 /* these static vars are for storing the state between calls */
2206 static int lastFamily
= INT_MIN
;
2207 static int lastSize
= INT_MIN
;
2208 static int lastStyle
= INT_MIN
;
2209 static int lastWeight
= INT_MIN
;
2210 static int lastDescender
= INT_MIN
;
2211 static int lastWidths
[256]; /* widths of the characters */
2213 double UnderlinePosition
= 0.0;
2214 double UnderlineThickness
= 0.0;
2216 /* get actual parameters */
2217 int Family
= fontToUse
->GetFamily();
2218 int Size
= fontToUse
->GetPointSize();
2219 int Style
= fontToUse
->GetStyle();
2220 int Weight
= fontToUse
->GetWeight();
2222 /* if we have another font, read the font-metrics */
2223 if (Family
!=lastFamily
|| Size
!=lastSize
|| Style
!=lastStyle
|| Weight
!=lastWeight
)
2225 /* store actual values */
2226 lastFamily
= Family
;
2229 lastWeight
= Weight
;
2231 const char *name
= NULL
;
2238 if ((Style
== wxITALIC
) && (Weight
== wxBOLD
)) name
= "CourBoO.afm";
2239 else if ((Style
!= wxITALIC
) && (Weight
== wxBOLD
)) name
= "CourBo.afm";
2240 else if ((Style
== wxITALIC
) && (Weight
!= wxBOLD
)) name
= "CourO.afm";
2241 else name
= "Cour.afm";
2246 if ((Style
== wxITALIC
) && (Weight
== wxBOLD
)) name
= "TimesBoO.afm";
2247 else if ((Style
!= wxITALIC
) && (Weight
== wxBOLD
)) name
= "TimesBo.afm";
2248 else if ((Style
== wxITALIC
) && (Weight
!= wxBOLD
)) name
= "TimesO.afm";
2249 else name
= "TimesRo.afm";
2261 if ((Style
== wxITALIC
) && (Weight
== wxBOLD
)) name
= "HelvBoO.afm";
2262 else if ((Style
!= wxITALIC
) && (Weight
== wxBOLD
)) name
= "HelvBo.afm";
2263 else if ((Style
== wxITALIC
) && (Weight
!= wxBOLD
)) name
= "HelvO.afm";
2264 else name
= "Helv.afm";
2269 /* get the directory of the AFM files */
2270 wxString afmName
= wxEmptyString
;
2271 if (!m_printData
.GetFontMetricPath().IsEmpty())
2273 afmName
= m_printData
.GetFontMetricPath();
2276 /* 2. open and process the file
2277 / a short explanation of the AFM format:
2278 / we have for each character a line, which gives its size
2281 / C 63 ; WX 444 ; N question ; B 49 -14 395 676 ;
2283 / that means, we have a character with ascii code 63, and width
2284 / (444/1000 * fontSize) points.
2285 / the other data is ignored for now!
2287 / when the font has changed, we read in the right AFM file and store the
2288 / character widths in an array, which is processed below (see point 3.). */
2290 /* new elements JC Sun Aug 25 23:21:44 MET DST 1996 */
2293 FILE *afmFile
= wxFopen(afmName
,wxT("r"));
2297 afmName
= wxThePrintSetupData
->GetAFMPath();
2298 afmName
<< wxFILE_SEP_PATH
<< name
;
2299 afmFile
= wxFopen(afmName
,wxT("r"));
2302 #if defined(__UNIX__) && !defined(__VMS__)
2304 /* please do NOT change the line above to "else if (afmFile==NULL)" -
2305 - afmFile = fopen() may fail and in that case the next if branch
2306 MUST be executed - and it would not if there was "else" */
2308 afmName
= wxGetDataDir();
2309 afmName
<< wxFILE_SEP_PATH
2310 #if defined(__LINUX__) || defined(__FREEBSD__)
2311 << wxT("gs_afm") << wxFILE_SEP_PATH
2313 << wxT("afm") << wxFILE_SEP_PATH
2316 afmFile
= wxFopen(afmName
,wxT("r"));
2322 wxLogDebug( wxT("GetTextExtent: can't open AFM file '%s'"), afmName
.c_str() );
2323 wxLogDebug( wxT(" using approximate values"));
2324 for (int i
=0; i
<256; i
++) lastWidths
[i
] = 500; /* an approximate value */
2325 lastDescender
= -150; /* dito. */
2329 /* init the widths array */
2330 for(int i
=0; i
<256; i
++) lastWidths
[i
] = INT_MIN
;
2331 /* some variables for holding parts of a line */
2332 char cString
[10],semiString
[10],WXString
[10],descString
[20];
2333 char upString
[30], utString
[30], encString
[50];
2336 /* read in the file and parse it */
2337 while(fgets(line
,sizeof(line
),afmFile
)!=NULL
)
2339 /* A.) check for descender definition */
2340 if (strncmp(line
,"Descender",9)==0)
2342 if ((sscanf(line
,"%s%d",descString
,&lastDescender
)!=2) ||
2343 (strcmp(descString
,"Descender")!=0))
2345 wxLogDebug( wxT("AFM-file '%s': line '%s' has error (bad descender)"), afmName
.c_str(),line
);
2348 /* JC 1.) check for UnderlinePosition */
2349 else if(strncmp(line
,"UnderlinePosition",17)==0)
2351 if ((sscanf(line
,"%s%lf",upString
,&UnderlinePosition
)!=2) ||
2352 (strcmp(upString
,"UnderlinePosition")!=0))
2354 wxLogDebug( wxT("AFM-file '%s': line '%s' has error (bad UnderlinePosition)"), afmName
.c_str(), line
);
2357 /* JC 2.) check for UnderlineThickness */
2358 else if(strncmp(line
,"UnderlineThickness",18)==0)
2360 if ((sscanf(line
,"%s%lf",utString
,&UnderlineThickness
)!=2) ||
2361 (strcmp(utString
,"UnderlineThickness")!=0))
2363 wxLogDebug( wxT("AFM-file '%s': line '%s' has error (bad UnderlineThickness)"), afmName
.c_str(), line
);
2366 /* JC 3.) check for EncodingScheme */
2367 else if(strncmp(line
,"EncodingScheme",14)==0)
2369 if ((sscanf(line
,"%s%s",utString
,encString
)!=2) ||
2370 (strcmp(utString
,"EncodingScheme")!=0))
2372 wxLogDebug( wxT("AFM-file '%s': line '%s' has error (bad EncodingScheme)"), afmName
.c_str(), line
);
2374 else if (strncmp(encString
, "AdobeStandardEncoding", 21))
2376 wxLogDebug( wxT("AFM-file '%s': line '%s' has error (unsupported EncodingScheme %s)"),
2377 afmName
.c_str(),line
, encString
);
2380 /* B.) check for char-width */
2381 else if(strncmp(line
,"C ",2)==0)
2383 if (sscanf(line
,"%s%d%s%s%d",cString
,&ascii
,semiString
,WXString
,&cWidth
)!=5)
2385 wxLogDebug(wxT("AFM-file '%s': line '%s' has an error (bad character width)"),afmName
.c_str(),line
);
2387 if(strcmp(cString
,"C")!=0 || strcmp(semiString
,";")!=0 || strcmp(WXString
,"WX")!=0)
2389 wxLogDebug(wxT("AFM-file '%s': line '%s' has a format error"),afmName
.c_str(),line
);
2391 /* printf(" char '%c'=%d has width '%d'\n",ascii,ascii,cWidth); */
2392 if (ascii
>=0 && ascii
<256)
2394 lastWidths
[ascii
] = cWidth
; /* store width */
2398 /* MATTHEW: this happens a lot; don't print an error */
2399 /* wxLogDebug("AFM-file '%s': ASCII value %d out of range",afmName.c_str(),ascii); */
2402 /* C.) ignore other entries. */
2406 /* hack to compute correct values for german 'Umlaute'
2407 / the correct way would be to map the character names
2408 / like 'adieresis' to corresp. positions of ISOEnc and read
2409 / these values from AFM files, too. Maybe later ... */
2410 lastWidths
[196] = lastWidths
['A']; // Ä
2411 lastWidths
[228] = lastWidths
['a']; // ä
2412 lastWidths
[214] = lastWidths
['O']; // Ö
2413 lastWidths
[246] = lastWidths
['o']; // ö
2414 lastWidths
[220] = lastWidths
['U']; // Ü
2415 lastWidths
[252] = lastWidths
['u']; // ü
2416 lastWidths
[223] = lastWidths
[251]; // ß
2418 /* JC: calculate UnderlineThickness/UnderlinePosition */
2420 // VS: dirty, but is there any better solution?
2422 pt
= (double*) &m_underlinePosition
;
2423 *pt
= LogicalToDeviceYRel((wxCoord
)(UnderlinePosition
* fontToUse
->GetPointSize())) / 1000.0f
;
2424 pt
= (double*) &m_underlineThickness
;
2425 *pt
= LogicalToDeviceYRel((wxCoord
)(UnderlineThickness
* fontToUse
->GetPointSize())) / 1000.0f
;
2430 /* 3. now the font metrics are read in, calc size this
2431 / is done by adding the widths of the characters in the
2432 / string. they are given in 1/1000 of the size! */
2435 wxCoord height
=Size
; /* by default */
2437 for(p
=(unsigned char *)wxMBSTRINGCAST strbuf
; *p
; p
++)
2439 if(lastWidths
[*p
]== INT_MIN
)
2441 wxLogDebug(wxT("GetTextExtent: undefined width for character '%c' (%d)"), *p
,*p
);
2442 sum
+= lastWidths
[' ']; /* assume space */
2446 sum
+= lastWidths
[*p
];
2450 double widthSum
= sum
;
2452 widthSum
/= 1000.0F
;
2454 /* add descender to height (it is usually a negative value) */
2455 //if (lastDescender != INT_MIN)
2457 // height += (wxCoord)(((-lastDescender)/1000.0F) * Size); /* MATTHEW: forgot scale */
2459 // - commented by V. Slavik - height already contains descender in it
2460 // (judging from few experiments)
2462 /* return size values */
2464 *x
= (wxCoord
)widthSum
;
2468 /* return other parameters */
2471 if(lastDescender
!=INT_MIN
)
2473 *descent
= (wxCoord
)(((-lastDescender
)/1000.0F
) * Size
); /* MATTHEW: forgot scale */
2481 /* currently no idea how to calculate this! */
2482 if (externalLeading
) *externalLeading
= 0;
2487 // Determine the Default Postscript Previewer
2488 // available on the platform
2489 #if defined(__SUN__) && defined(__XVIEW__)
2490 // OpenWindow/NeWS's Postscript Previewer
2491 # define PS_VIEWER_PROG "pageview"
2492 #elif defined(__VMS__)
2493 #define PS_VIEWER_PROG "view/format=ps/select=x_display"
2494 #elif defined(__SGI__)
2495 // SGI's Display Postscript Previewer
2496 //# define PS_VIEWER_PROG "dps"
2497 # define PS_VIEWER_PROG "xpsview"
2498 #elif defined(__X__) || defined(__WXGTK__)
2499 // Front-end to ghostscript
2500 # define PS_VIEWER_PROG "ghostview"
2502 // Windows ghostscript/ghostview
2503 # define PS_VIEWER_PROG "gsview"
2506 wxPrintSetupData
*wxThePrintSetupData
= (wxPrintSetupData
*) NULL
;
2508 IMPLEMENT_DYNAMIC_CLASS(wxPostScriptDC
, wxDC
)
2509 IMPLEMENT_DYNAMIC_CLASS(wxPrintSetupData
, wxObject
)
2511 // Redundant now I think
2513 IMPLEMENT_CLASS(wxPostScriptPrintDialog
, wxDialog
)
2515 wxPostScriptPrintDialog::wxPostScriptPrintDialog (wxWindow
*parent
, const wxString
& title
,
2516 const wxPoint
& pos
, const wxSize
& size
, wxCoord style
):
2517 wxDialog(parent
, -1, title
, pos
, size
, style
)
2519 wxBeginBusyCursor();
2524 *orientation
= new wxString
[2],
2525 *print_modes
= new wxString
[3];
2527 wxCoord wx_printer_translate_x
, wx_printer_translate_y
;
2528 double wx_printer_scale_x
, wx_printer_scale_y
;
2530 orientation
[0] = _("Portrait");
2531 orientation
[1] = _("Landscape");
2533 print_modes
[0] = _("Send to Printer");
2534 print_modes
[1] = _("Print to File");
2535 print_modes
[2] = _("Preview Only");
2539 wxButton
*okBut
= new wxButton (this, wxID_OK
, _("OK"), wxPoint(5, 5));
2540 (void) new wxButton (this, wxID_CANCEL
, _("Cancel"), wxPoint(40, 5));
2541 okBut
->SetDefault();
2544 #if defined(__WXGTK__) || defined (__WXMOTIF__)
2545 (void) new wxStaticText( this, -1, _("Printer Command: "),
2547 (void) new wxTextCtrl( this, wxID_PRINTER_COMMAND
, wxThePrintSetupData
->GetPrinterCommand(),
2548 wxPoint(100, yPos
), wxSize(100, -1) );
2550 (void) new wxStaticText( this, -1, _("Printer Options: "),
2551 wxPoint(210, yPos
) );
2552 (void) new wxTextCtrl( this, wxID_PRINTER_OPTIONS
, wxThePrintSetupData
->GetPrinterOptions(),
2553 wxPoint(305, yPos
), wxSize(150, -1) );
2559 wxRadioBox
*radio0
= new wxRadioBox(this, wxID_PRINTER_ORIENTATION
, "Orientation: ", wxPoint(5, yPos
), wxSize(-1,-1),
2560 2,orientation
,2,wxRA_SPECIFY_ROWS
);
2561 radio0
->SetSelection((int)wxThePrintSetupData
->GetPrinterOrientation() - 1);
2563 // @@@ Configuration hook
2564 if ( !wxThePrintSetupData
->GetPrintPreviewCommand() )
2565 wxThePrintSetupData
->SetPrintPreviewCommand(PS_VIEWER_PROG
);
2567 // wxGetResource ("wxWindows", "PSView", &wxThePrintSetupData->m_previewCommand);
2569 features
= (wxThePrintSetupData
->GetPrintPreviewCommand() &&
2570 *wxThePrintSetupData
->GetPrintPreviewCommand()) ? 3 : 2;
2572 wxRadioBox
*radio1
= new wxRadioBox(this, wxID_PRINTER_MODES
, _("PostScript:"),
2574 wxSize(-1,-1), features
,
2575 print_modes
, features
, wxRA_SPECIFY_ROWS
);
2578 radio1
->Enable(0, FALSE
);
2579 if (wxThePrintSetupData
->GetPrintPreviewCommand() && *wxThePrintSetupData
->GetPrintPreviewCommand())
2580 radio1
->Enable(2, FALSE
);
2583 radio1
->SetSelection((int)wxThePrintSetupData
->GetPrinterMode());
2584 wxThePrintSetupData
->GetPrinterTranslation(&wx_printer_translate_x
, &wx_printer_translate_y
);
2585 wxThePrintSetupData
->GetPrinterScaling(&wx_printer_scale_x
, &wx_printer_scale_y
);
2587 sprintf (buf
, "%.2f", wx_printer_scale_x
);
2590 (void) new wxStaticText(this, -1, _("X Scaling"), wxPoint(5, yPos
));
2591 /* wxTextCtrl *text1 = */ (void) new wxTextCtrl(this, wxID_PRINTER_X_SCALE
, buf
, wxPoint(100, yPos
), wxSize(100, -1));
2593 sprintf (buf
, "%.2f", wx_printer_scale_y
);
2594 (void) new wxStaticText(this, -1, _("Y Scaling"), wxPoint(220, yPos
));
2595 /* wxTextCtrl *text2 = */ (void) new wxTextCtrl(this, wxID_PRINTER_Y_SCALE
, buf
, wxPoint(320, yPos
), wxSize(100, -1));
2599 (void) new wxStaticText(this, -1, _("X Translation"), wxPoint(5, yPos
));
2600 sprintf (buf
, "%.2d", wx_printer_translate_x
);
2601 /* wxTextCtrl *text3 = */ (void) new wxTextCtrl(this, wxID_PRINTER_X_TRANS
, buf
, wxPoint(100, yPos
), wxSize(100, -1));
2603 (void) new wxStaticText(this, -1, _("Y Translation"), wxPoint(220, yPos
));
2604 sprintf (buf
, "%.2d", wx_printer_translate_y
);
2605 /* wxTextCtrl *text4 = */ (void) new wxTextCtrl(this, wxID_PRINTER_Y_TRANS
, buf
, wxPoint(320, yPos
), wxSize(100, -1));
2609 delete[] orientation
;
2610 delete[] print_modes
;
2615 int wxPostScriptPrintDialog::ShowModal ()
2617 if ( wxDialog::ShowModal() == wxID_OK
)
2619 // wxTextCtrl *text0 = (wxTextCtrl *)FindWindow(wxID_PRINTER_OPTIONS);
2620 wxTextCtrl
*text1
= (wxTextCtrl
*)FindWindow(wxID_PRINTER_X_SCALE
);
2621 wxTextCtrl
*text2
= (wxTextCtrl
*)FindWindow(wxID_PRINTER_Y_SCALE
);
2622 wxTextCtrl
*text3
= (wxTextCtrl
*)FindWindow(wxID_PRINTER_X_TRANS
);
2623 wxTextCtrl
*text4
= (wxTextCtrl
*)FindWindow(wxID_PRINTER_Y_TRANS
);
2624 // wxTextCtrl *text_prt = (wxTextCtrl *)FindWindow(wxID_PRINTER_COMMAND);
2625 wxRadioBox
*radio0
= (wxRadioBox
*)FindWindow(wxID_PRINTER_ORIENTATION
);
2626 wxRadioBox
*radio1
= (wxRadioBox
*)FindWindow(wxID_PRINTER_MODES
);
2628 StringToDouble (WXSTRINGCAST text1
->GetValue (), &wxThePrintSetupData
->m_printerScaleX
);
2629 StringToDouble (WXSTRINGCAST text2
->GetValue (), &wxThePrintSetupData
->m_printerScaleY
);
2632 StringToLong (WXSTRINGCAST text3
->GetValue (), &dummy
);
2633 wxThePrintSetupData
->m_printerTranslateX
= (wxCoord
)dummy
;
2634 StringToLong (WXSTRINGCAST text4
->GetValue (), &dummy
);
2635 wxThePrintSetupData
->m_printerTranslateY
= (wxCoord
)dummy
;
2638 // wxThePrintSetupData->SetPrinterOptions(WXSTRINGCAST text0->GetValue ());
2639 // wxThePrintSetupData->SetPrinterCommand(WXSTRINGCAST text_prt->GetValue ());
2642 wxThePrintSetupData
->SetPrinterOrientation((radio0
->GetSelection() == 1 ? wxLANDSCAPE
: wxPORTRAIT
));
2645 switch ( radio1
->GetSelection() ) {
2646 case 0: wxThePrintSetupData
->SetPrinterMode(PS_PRINTER
); break;
2647 case 1: wxThePrintSetupData
->SetPrinterMode(PS_FILE
); break;
2648 case 2: wxThePrintSetupData
->SetPrinterMode(PS_PREVIEW
); break;
2657 // PostScript printer settings
2658 // RETAINED FOR BACKWARD COMPATIBILITY
2659 void wxSetPrinterCommand(const wxString
& cmd
)
2661 wxThePrintSetupData
->SetPrinterCommand(cmd
);
2664 void wxSetPrintPreviewCommand(const wxString
& cmd
)
2666 wxThePrintSetupData
->SetPrintPreviewCommand(cmd
);
2669 void wxSetPrinterOptions(const wxString
& flags
)
2671 wxThePrintSetupData
->SetPrinterOptions(flags
);
2674 void wxSetPrinterFile(const wxString
& f
)
2676 wxThePrintSetupData
->SetPrinterFile(f
);
2679 void wxSetPrinterOrientation(int orient
)
2681 wxThePrintSetupData
->SetPrinterOrientation(orient
);
2684 void wxSetPrinterScaling(double x
, double y
)
2686 wxThePrintSetupData
->SetPrinterScaling(x
, y
);
2689 void wxSetPrinterTranslation(wxCoord x
, wxCoord y
)
2691 wxThePrintSetupData
->SetPrinterTranslation(x
, y
);
2694 // 1 = Preview, 2 = print to file, 3 = send to printer
2695 void wxSetPrinterMode(int mode
)
2697 wxThePrintSetupData
->SetPrinterMode(mode
);
2700 void wxSetAFMPath(const wxString
& f
)
2702 wxThePrintSetupData
->SetAFMPath(f
);
2705 // Get current values
2706 wxString
wxGetPrinterCommand()
2708 return wxThePrintSetupData
->GetPrinterCommand();
2711 wxString
wxGetPrintPreviewCommand()
2713 return wxThePrintSetupData
->GetPrintPreviewCommand();
2716 wxString
wxGetPrinterOptions()
2718 return wxThePrintSetupData
->GetPrinterOptions();
2721 wxString
wxGetPrinterFile()
2723 return wxThePrintSetupData
->GetPrinterFile();
2726 int wxGetPrinterOrientation()
2728 return wxThePrintSetupData
->GetPrinterOrientation();
2731 void wxGetPrinterScaling(double* x
, double* y
)
2733 wxThePrintSetupData
->GetPrinterScaling(x
, y
);
2736 void wxGetPrinterTranslation(wxCoord
*x
, wxCoord
*y
)
2738 wxThePrintSetupData
->GetPrinterTranslation(x
, y
);
2741 int wxGetPrinterMode()
2743 return wxThePrintSetupData
->GetPrinterMode();
2746 wxString
wxGetAFMPath()
2748 return wxThePrintSetupData
->GetAFMPath();
2755 wxPrintSetupData::wxPrintSetupData()
2757 m_printerOrient
= wxPORTRAIT
;
2758 m_printerScaleX
= (double)1.0;
2759 m_printerScaleY
= (double)1.0;
2760 m_printerTranslateX
= 0;
2761 m_printerTranslateY
= 0;
2762 m_printerMode
= wxPRINT_MODE_FILE
;
2763 m_printColour
= TRUE
;
2766 wxPrintSetupData::~wxPrintSetupData()
2770 void wxPrintSetupData::operator=(wxPrintSetupData
& data
)
2772 SetPrinterCommand(data
.GetPrinterCommand());
2773 SetPrintPreviewCommand(data
.GetPrintPreviewCommand());
2774 SetPrinterOptions(data
.GetPrinterOptions());
2776 data
.GetPrinterTranslation(&x
, &y
);
2777 SetPrinterTranslation(x
, y
);
2780 data
.GetPrinterScaling(&x1
, &y1
);
2781 SetPrinterScaling(x1
, y1
);
2783 SetPrinterOrientation(data
.GetPrinterOrientation());
2784 SetPrinterMode(data
.GetPrinterMode());
2785 SetAFMPath(data
.GetAFMPath());
2786 SetPaperName(data
.GetPaperName());
2787 SetColour(data
.GetColour());
2790 // Initialize from a wxPrintData object (wxPrintData should now be used instead of wxPrintSetupData).
2791 void wxPrintSetupData::operator=(const wxPrintData
& data
)
2793 SetPrinterCommand(data
.GetPrinterCommand());
2794 SetPrintPreviewCommand(data
.GetPreviewCommand());
2795 SetPrinterOptions(data
.GetPrinterOptions());
2796 SetPrinterTranslation((wxCoord
)data
.GetPrinterTranslateX(),
2797 (wxCoord
)data
.GetPrinterTranslateY());
2798 SetPrinterScaling(data
.GetPrinterScaleX(), data
.GetPrinterScaleY());
2799 SetPrinterOrientation(data
.GetOrientation());
2800 SetPrinterMode((int) data
.GetPrintMode());
2801 SetAFMPath(data
.GetFontMetricPath());
2802 SetPaperName(wxThePrintPaperDatabase
->ConvertIdToName(data
.GetPaperId()));
2803 SetColour(data
.GetColour());
2804 SetPrinterFile(data
.GetFilename());
2807 void wxInitializePrintSetupData(bool init
)
2811 wxThePrintSetupData
= new wxPrintSetupData
;
2813 wxThePrintSetupData
->SetPrintPreviewCommand(PS_VIEWER_PROG
);
2814 wxThePrintSetupData
->SetPrinterOrientation(wxPORTRAIT
);
2815 wxThePrintSetupData
->SetPrinterMode(wxPRINT_MODE_PREVIEW
);
2816 wxThePrintSetupData
->SetPaperName(_("A4 sheet, 210 x 297 mm"));
2818 // Could have a .ini file to read in some defaults
2819 // - and/or use environment variables, e.g. WXWIN
2821 wxThePrintSetupData
->SetPrinterCommand("print");
2822 wxThePrintSetupData
->SetPrinterOptions("/nonotify/queue=psqueue");
2823 wxThePrintSetupData
->SetAFMPath("sys$ps_font_metrics:");
2826 wxThePrintSetupData
->SetPrinterCommand("print");
2827 wxThePrintSetupData
->SetAFMPath("c:\\windows\\system\\");
2828 wxThePrintSetupData
->SetPrinterOptions("");
2830 #if !defined(__VMS__) && !defined(__WXMSW__)
2831 wxThePrintSetupData
->SetPrinterCommand("lpr");
2832 wxThePrintSetupData
->SetPrinterOptions("");
2833 wxThePrintSetupData
->SetAFMPath("");
2838 if (wxThePrintSetupData
)
2839 delete wxThePrintSetupData
;
2840 wxThePrintSetupData
= (wxPrintSetupData
*) NULL
;
2844 // A module to allow initialization/cleanup of PostScript-related
2845 // things without calling these functions from app.cpp.
2847 class WXDLLEXPORT wxPostScriptModule
: public wxModule
2849 DECLARE_DYNAMIC_CLASS(wxPostScriptModule
)
2851 wxPostScriptModule() {}
2856 IMPLEMENT_DYNAMIC_CLASS(wxPostScriptModule
, wxModule
)
2859 * Initialization/cleanup module
2862 bool wxPostScriptModule::OnInit()
2864 wxInitializePrintSetupData();
2869 void wxPostScriptModule::OnExit()
2871 wxInitializePrintSetupData(FALSE
);
2878 // wxUSE_PRINTING_ARCHITECTURE