]>
Commit | Line | Data |
---|---|---|
ed880dd4 | 1 | ///////////////////////////////////////////////////////////////////////////// |
bf38cbff JS |
2 | // Name: dcpsg.cpp |
3 | // Purpose: Generic wxPostScriptDC implementation | |
ed880dd4 RR |
4 | // Author: Julian Smart, Robert Roebling, Markus Holzhem |
5 | // Modified by: | |
6 | // Created: 04/01/98 | |
7 | // RCS-ID: $Id$ | |
8 | // Copyright: (c) Julian Smart and Markus Holzem | |
9a6be59a | 9 | // Licence: wxWindows licence |
ed880dd4 RR |
10 | ///////////////////////////////////////////////////////////////////////////// |
11 | ||
12 | #ifdef __GNUG__ | |
ed880dd4 RR |
13 | #pragma implementation |
14 | #pragma interface | |
15 | #endif | |
16 | ||
9a6be59a VZ |
17 | #include "wx/wxprec.h" |
18 | ||
19 | #ifdef __BORLANDC__ | |
20 | #pragma hdrstop | |
21 | #endif | |
22 | ||
23 | #ifndef WX_PRECOMP | |
24 | #include "wx/setup.h" | |
25 | #include "wx/defs.h" | |
26 | #endif // WX_PRECOMP | |
06cfab17 | 27 | |
ce4169a4 RR |
28 | #if wxUSE_PRINTING_ARCHITECTURE |
29 | ||
06cfab17 RR |
30 | #if wxUSE_POSTSCRIPT |
31 | ||
e6f1ad22 | 32 | #include "wx/window.h" |
f04371f0 | 33 | #include "wx/dcmemory.h" |
ed880dd4 | 34 | #include "wx/utils.h" |
f04371f0 | 35 | #include "wx/intl.h" |
ed880dd4 | 36 | #include "wx/filedlg.h" |
ed880dd4 | 37 | #include "wx/app.h" |
f04371f0 | 38 | #include "wx/msgdlg.h" |
ef539066 | 39 | #include "wx/image.h" |
3069ac4e | 40 | #include "wx/log.h" |
bf38cbff JS |
41 | #include "wx/generic/dcpsg.h" |
42 | #include "wx/generic/prntdlgg.h" | |
43 | #include "wx/button.h" | |
44 | #include "wx/stattext.h" | |
45 | #include "wx/radiobox.h" | |
46 | #include "wx/textctrl.h" | |
9a05fd8d | 47 | #include "wx/prntbase.h" |
7bcb11d3 | 48 | #include "wx/paper.h" |
ed880dd4 | 49 | |
57c208c5 JS |
50 | #include <math.h> |
51 | ||
bf38cbff JS |
52 | #ifdef __WXMSW__ |
53 | ||
54 | #ifdef DrawText | |
55 | #undef DrawText | |
56 | #endif | |
57 | ||
58 | #ifdef StartDoc | |
59 | #undef StartDoc | |
60 | #endif | |
61 | ||
62 | #ifdef GetCharWidth | |
63 | #undef GetCharWidth | |
64 | #endif | |
65 | ||
66 | #ifdef FindWindow | |
67 | #undef FindWindow | |
68 | #endif | |
69 | ||
70 | #endif | |
71 | ||
ed880dd4 RR |
72 | //----------------------------------------------------------------------------- |
73 | // start and end of document/page | |
74 | //----------------------------------------------------------------------------- | |
75 | ||
76 | static const char *wxPostScriptHeaderEllipse = "\ | |
77 | /ellipsedict 8 dict def\n\ | |
78 | ellipsedict /mtrx matrix put\n\ | |
79 | /ellipse {\n\ | |
9a6be59a VZ |
80 | ellipsedict begin\n\ |
81 | /endangle exch def\n\ | |
82 | /startangle exch def\n\ | |
83 | /yrad exch def\n\ | |
84 | /xrad exch def\n\ | |
85 | /y exch def\n\ | |
86 | /x exch def\n\ | |
87 | /savematrix mtrx currentmatrix def\n\ | |
88 | x y translate\n\ | |
89 | xrad yrad scale\n\ | |
90 | 0 0 1 startangle endangle arc\n\ | |
91 | savematrix setmatrix\n\ | |
92 | end\n\ | |
93 | } def\n\ | |
ed880dd4 RR |
94 | "; |
95 | ||
96 | static const char *wxPostScriptHeaderEllipticArc= "\ | |
97 | /ellipticarcdict 8 dict def\n\ | |
98 | ellipticarcdict /mtrx matrix put\n\ | |
99 | /ellipticarc\n\ | |
100 | { ellipticarcdict begin\n\ | |
101 | /do_fill exch def\n\ | |
102 | /endangle exch def\n\ | |
103 | /startangle exch def\n\ | |
104 | /yrad exch def\n\ | |
105 | /xrad exch def \n\ | |
106 | /y exch def\n\ | |
107 | /x exch def\n\ | |
108 | /savematrix mtrx currentmatrix def\n\ | |
109 | x y translate\n\ | |
110 | xrad yrad scale\n\ | |
111 | do_fill { 0 0 moveto } if\n\ | |
112 | 0 0 1 startangle endangle arc\n\ | |
113 | savematrix setmatrix\n\ | |
114 | do_fill { fill }{ stroke } ifelse\n\ | |
115 | end\n\ | |
116 | } def\n"; | |
117 | ||
118 | static const char *wxPostScriptHeaderSpline = "\ | |
119 | /DrawSplineSection {\n\ | |
9a6be59a VZ |
120 | /y3 exch def\n\ |
121 | /x3 exch def\n\ | |
122 | /y2 exch def\n\ | |
123 | /x2 exch def\n\ | |
124 | /y1 exch def\n\ | |
125 | /x1 exch def\n\ | |
126 | /xa x1 x2 x1 sub 0.666667 mul add def\n\ | |
127 | /ya y1 y2 y1 sub 0.666667 mul add def\n\ | |
128 | /xb x3 x2 x3 sub 0.666667 mul add def\n\ | |
129 | /yb y3 y2 y3 sub 0.666667 mul add def\n\ | |
130 | x1 y1 lineto\n\ | |
131 | xa ya xb yb x3 y3 curveto\n\ | |
132 | } def\n\ | |
ed880dd4 RR |
133 | "; |
134 | ||
135 | static const char *wxPostScriptHeaderColourImage = "\ | |
d7657f75 RR |
136 | %% define 'colorimage' if it isn't defined\n\ |
137 | %% ('colortogray' and 'mergeprocs' come from xwd2ps\n\ | |
138 | %% via xgrab)\n\ | |
139 | /colorimage where %% do we know about 'colorimage'?\n\ | |
140 | { pop } %% yes: pop off the 'dict' returned\n\ | |
141 | { %% no: define one\n\ | |
142 | /colortogray { %% define an RGB->I function\n\ | |
143 | /rgbdata exch store %% call input 'rgbdata'\n\ | |
ed880dd4 RR |
144 | rgbdata length 3 idiv\n\ |
145 | /npixls exch store\n\ | |
146 | /rgbindx 0 store\n\ | |
147 | 0 1 npixls 1 sub {\n\ | |
148 | grays exch\n\ | |
d7657f75 RR |
149 | rgbdata rgbindx get 20 mul %% Red\n\ |
150 | rgbdata rgbindx 1 add get 32 mul %% Green\n\ | |
151 | rgbdata rgbindx 2 add get 12 mul %% Blue\n\ | |
152 | add add 64 idiv %% I = .5G + .31R + .18B\n\ | |
ed880dd4 RR |
153 | put\n\ |
154 | /rgbindx rgbindx 3 add store\n\ | |
155 | } for\n\ | |
156 | grays 0 npixls getinterval\n\ | |
157 | } bind def\n\ | |
158 | \n\ | |
d7657f75 RR |
159 | %% Utility procedure for colorimage operator.\n\ |
160 | %% This procedure takes two procedures off the\n\ | |
161 | %% stack and merges them into a single procedure.\n\ | |
ed880dd4 | 162 | \n\ |
d7657f75 | 163 | /mergeprocs { %% def\n\ |
ed880dd4 RR |
164 | dup length\n\ |
165 | 3 -1 roll\n\ | |
166 | dup\n\ | |
167 | length\n\ | |
168 | dup\n\ | |
169 | 5 1 roll\n\ | |
170 | 3 -1 roll\n\ | |
171 | add\n\ | |
172 | array cvx\n\ | |
173 | dup\n\ | |
174 | 3 -1 roll\n\ | |
175 | 0 exch\n\ | |
176 | putinterval\n\ | |
177 | dup\n\ | |
178 | 4 2 roll\n\ | |
179 | putinterval\n\ | |
180 | } bind def\n\ | |
181 | \n\ | |
d7657f75 RR |
182 | /colorimage { %% def\n\ |
183 | pop pop %% remove 'false 3' operands\n\ | |
ed880dd4 RR |
184 | {colortogray} mergeprocs\n\ |
185 | image\n\ | |
186 | } bind def\n\ | |
d7657f75 | 187 | } ifelse %% end of 'false' case\n\ |
ed880dd4 RR |
188 | "; |
189 | ||
190 | static char wxPostScriptHeaderReencodeISO1[] = | |
191 | "\n/reencodeISO {\n" | |
192 | "dup dup findfont dup length dict begin\n" | |
193 | "{ 1 index /FID ne { def }{ pop pop } ifelse } forall\n" | |
194 | "/Encoding ISOLatin1Encoding def\n" | |
195 | "currentdict end definefont\n" | |
196 | "} def\n" | |
197 | "/ISOLatin1Encoding [\n" | |
198 | "/.notdef/.notdef/.notdef/.notdef/.notdef/.notdef/.notdef/.notdef\n" | |
199 | "/.notdef/.notdef/.notdef/.notdef/.notdef/.notdef/.notdef/.notdef\n" | |
200 | "/.notdef/.notdef/.notdef/.notdef/.notdef/.notdef/.notdef/.notdef\n" | |
201 | "/.notdef/.notdef/.notdef/.notdef/.notdef/.notdef/.notdef/.notdef\n" | |
202 | "/space/exclam/quotedbl/numbersign/dollar/percent/ampersand/quoteright\n" | |
203 | "/parenleft/parenright/asterisk/plus/comma/minus/period/slash\n" | |
204 | "/zero/one/two/three/four/five/six/seven/eight/nine/colon/semicolon\n" | |
205 | "/less/equal/greater/question/at/A/B/C/D/E/F/G/H/I/J/K/L/M/N\n" | |
206 | "/O/P/Q/R/S/T/U/V/W/X/Y/Z/bracketleft/backslash/bracketright\n" | |
207 | "/asciicircum/underscore/quoteleft/a/b/c/d/e/f/g/h/i/j/k/l/m\n" | |
208 | "/n/o/p/q/r/s/t/u/v/w/x/y/z/braceleft/bar/braceright/asciitilde\n" | |
209 | "/.notdef/.notdef/.notdef/.notdef/.notdef/.notdef/.notdef/.notdef\n" | |
210 | "/.notdef/.notdef/.notdef/.notdef/.notdef/.notdef/.notdef/.notdef\n" | |
211 | "/.notdef/dotlessi/grave/acute/circumflex/tilde/macron/breve\n" | |
212 | "/dotaccent/dieresis/.notdef/ring/cedilla/.notdef/hungarumlaut\n"; | |
213 | ||
214 | static char wxPostScriptHeaderReencodeISO2[] = | |
215 | "/ogonek/caron/space/exclamdown/cent/sterling/currency/yen/brokenbar\n" | |
216 | "/section/dieresis/copyright/ordfeminine/guillemotleft/logicalnot\n" | |
217 | "/hyphen/registered/macron/degree/plusminus/twosuperior/threesuperior\n" | |
218 | "/acute/mu/paragraph/periodcentered/cedilla/onesuperior/ordmasculine\n" | |
219 | "/guillemotright/onequarter/onehalf/threequarters/questiondown\n" | |
220 | "/Agrave/Aacute/Acircumflex/Atilde/Adieresis/Aring/AE/Ccedilla\n" | |
221 | "/Egrave/Eacute/Ecircumflex/Edieresis/Igrave/Iacute/Icircumflex\n" | |
222 | "/Idieresis/Eth/Ntilde/Ograve/Oacute/Ocircumflex/Otilde/Odieresis\n" | |
223 | "/multiply/Oslash/Ugrave/Uacute/Ucircumflex/Udieresis/Yacute\n" | |
224 | "/Thorn/germandbls/agrave/aacute/acircumflex/atilde/adieresis\n" | |
225 | "/aring/ae/ccedilla/egrave/eacute/ecircumflex/edieresis/igrave\n" | |
226 | "/iacute/icircumflex/idieresis/eth/ntilde/ograve/oacute/ocircumflex\n" | |
227 | "/otilde/odieresis/divide/oslash/ugrave/uacute/ucircumflex/udieresis\n" | |
228 | "/yacute/thorn/ydieresis\n" | |
229 | "] def\n\n"; | |
230 | ||
ed880dd4 RR |
231 | //------------------------------------------------------------------------------- |
232 | // wxPostScriptDC | |
233 | //------------------------------------------------------------------------------- | |
234 | ||
235 | wxPostScriptDC::wxPostScriptDC () | |
236 | { | |
d7657f75 | 237 | m_pstream = (FILE*) NULL; |
afce4c03 | 238 | |
ed880dd4 RR |
239 | m_currentRed = 0; |
240 | m_currentGreen = 0; | |
241 | m_currentBlue = 0; | |
afce4c03 | 242 | |
ed880dd4 | 243 | m_pageNumber = 0; |
afce4c03 | 244 | |
ed880dd4 | 245 | m_clipping = FALSE; |
afce4c03 | 246 | |
ed880dd4 RR |
247 | m_underlinePosition = 0.0; |
248 | m_underlineThickness = 0.0; | |
249 | ||
250 | m_signX = 1; // default x-axis left to right | |
251 | m_signY = -1; // default y-axis bottom up -> top down | |
7bcb11d3 JS |
252 | |
253 | // Compatibility only | |
e6f1ad22 HH |
254 | // HH: Doesn't seem to work for wxMSW... |
255 | #ifndef __WXMSW__ | |
7bcb11d3 | 256 | m_printData = * wxThePrintSetupData; |
e6f1ad22 | 257 | #endif |
ed880dd4 RR |
258 | } |
259 | ||
260 | wxPostScriptDC::wxPostScriptDC (const wxString& file, bool interactive, wxWindow *parent) | |
261 | { | |
d7657f75 | 262 | m_pstream = (FILE*) NULL; |
afce4c03 | 263 | |
ed880dd4 RR |
264 | m_currentRed = 0; |
265 | m_currentGreen = 0; | |
266 | m_currentBlue = 0; | |
afce4c03 | 267 | |
ed880dd4 | 268 | m_pageNumber = 0; |
afce4c03 | 269 | |
ed880dd4 | 270 | m_clipping = FALSE; |
afce4c03 | 271 | |
ed880dd4 RR |
272 | m_underlinePosition = 0.0; |
273 | m_underlineThickness = 0.0; | |
274 | ||
275 | m_signX = 1; // default x-axis left to right | |
276 | m_signY = -1; // default y-axis bottom up -> top down | |
277 | ||
278 | Create(file, interactive, parent); | |
279 | } | |
280 | ||
281 | bool wxPostScriptDC::Create(const wxString& file, bool interactive, wxWindow *parent) | |
282 | { | |
283 | m_isInteractive = interactive; | |
284 | ||
285 | m_title = ""; | |
7bcb11d3 | 286 | m_printData.SetFilename(file); |
ed880dd4 RR |
287 | |
288 | #ifdef __WXMSW__ | |
289 | // Can only send to file in Windows | |
7bcb11d3 | 290 | m_printData.SetPrintMode(wxPRINT_MODE_FILE); |
ed880dd4 RR |
291 | #endif |
292 | ||
293 | if (m_isInteractive) | |
294 | { | |
295 | if ((m_ok = PrinterDialog (parent) ) == FALSE) return FALSE; | |
296 | } | |
297 | else | |
298 | { | |
299 | m_ok = TRUE; | |
300 | } | |
afce4c03 | 301 | |
ed880dd4 RR |
302 | return m_ok; |
303 | } | |
304 | ||
7bcb11d3 JS |
305 | wxPostScriptDC::wxPostScriptDC (const wxPrintData& printData) |
306 | { | |
d7657f75 | 307 | m_pstream = (FILE*) NULL; |
7bcb11d3 JS |
308 | |
309 | m_currentRed = 0; | |
310 | m_currentGreen = 0; | |
311 | m_currentBlue = 0; | |
312 | ||
313 | m_pageNumber = 0; | |
314 | ||
315 | m_clipping = FALSE; | |
316 | ||
317 | m_underlinePosition = 0.0; | |
318 | m_underlineThickness = 0.0; | |
319 | ||
320 | m_signX = 1; // default x-axis left to right | |
321 | m_signY = -1; // default y-axis bottom up -> top down | |
322 | ||
323 | m_printData = printData; | |
75737d05 JS |
324 | |
325 | m_ok = TRUE; | |
7bcb11d3 JS |
326 | } |
327 | ||
ed880dd4 RR |
328 | wxPostScriptDC::~wxPostScriptDC () |
329 | { | |
d7657f75 RR |
330 | if (m_pstream) |
331 | { | |
332 | fclose( m_pstream ); | |
333 | m_pstream = (FILE*) NULL; | |
334 | } | |
ed880dd4 RR |
335 | } |
336 | ||
4bc67cc5 RR |
337 | bool wxPostScriptDC::Ok() const |
338 | { | |
ef539066 | 339 | return m_ok; |
4bc67cc5 | 340 | } |
afce4c03 | 341 | |
7bcb11d3 | 342 | // This dialog is deprecated now: use wxGenericPrintDialog or the printing framework |
ed880dd4 RR |
343 | bool wxPostScriptDC::PrinterDialog(wxWindow *parent) |
344 | { | |
afce4c03 | 345 | wxPostScriptPrintDialog dialog( parent, _("Printer Settings"), wxPoint(150, 150), wxSize(400, 400), |
ed880dd4 RR |
346 | wxDEFAULT_DIALOG_STYLE | wxDIALOG_MODAL ); |
347 | m_ok = (dialog.ShowModal () == wxID_OK); | |
348 | ||
349 | if (!m_ok) return FALSE; | |
350 | ||
7bcb11d3 JS |
351 | if ((m_printData.GetFilename() == "") && |
352 | (m_printData.GetPrintMode() == wxPRINT_MODE_PREVIEW || | |
353 | m_printData.GetPrintMode() == wxPRINT_MODE_PRINTER)) | |
ed880dd4 RR |
354 | { |
355 | // steve, 05.09.94 | |
356 | #ifdef __VMS__ | |
7bcb11d3 | 357 | m_printData.SetFilename("preview"); |
ed880dd4 RR |
358 | #else |
359 | // For PS_PRINTER action this depends on a Unix-style print spooler | |
360 | // since the wx_printer_file can be destroyed during a session | |
361 | // @@@ TODO: a Windows-style answer for non-Unix | |
87138c52 | 362 | wxChar userId[256]; |
ed880dd4 | 363 | wxGetUserId (userId, sizeof (userId) / sizeof (char)); |
87138c52 | 364 | wxChar tmp[256]; |
e90c1d2a | 365 | wxStrcpy (tmp, T("/tmp/preview_")); |
87138c52 | 366 | wxStrcat (tmp, userId); |
7bcb11d3 | 367 | m_printData.SetFilename(tmp); |
ed880dd4 | 368 | #endif |
87138c52 OK |
369 | wxChar tmp2[256]; |
370 | wxStrcpy(tmp2, m_printData.GetFilename()); | |
e90c1d2a | 371 | wxStrcat (tmp2, T(".ps")); |
7bcb11d3 | 372 | m_printData.SetFilename(tmp2); |
ed880dd4 | 373 | } |
e90c1d2a | 374 | else if ((m_printData.GetFilename() == T("")) && (m_printData.GetPrintMode() == wxPRINT_MODE_FILE)) |
ed880dd4 | 375 | { |
e90c1d2a | 376 | wxString file = wxSaveFileSelector (_("PostScript"), T("ps")); |
ba681060 | 377 | if ( file.IsEmpty() ) |
ed880dd4 RR |
378 | { |
379 | m_ok = FALSE; | |
380 | return FALSE; | |
381 | } | |
ba681060 | 382 | |
7bcb11d3 | 383 | m_printData.SetFilename(file); |
ed880dd4 RR |
384 | m_ok = TRUE; |
385 | } | |
386 | ||
4bc67cc5 | 387 | return m_ok; |
ed880dd4 RR |
388 | } |
389 | ||
390 | void wxPostScriptDC::SetClippingRegion (long x, long y, long w, long h) | |
391 | { | |
e90c1d2a | 392 | wxCHECK_RET( m_ok && m_pstream, T("invalid postscript dc") ); |
afce4c03 | 393 | |
4bc67cc5 | 394 | if (m_clipping) return; |
ed880dd4 RR |
395 | |
396 | wxDC::SetClippingRegion( x, y, w, h ); | |
afce4c03 | 397 | |
ed880dd4 | 398 | m_clipping = TRUE; |
d7657f75 RR |
399 | fprintf( m_pstream, |
400 | "gsave\n newpath\n" | |
401 | "%ld %ld moveto\n" | |
402 | "%ld %ld lineto\n" | |
403 | "%ld %ld lineto\n" | |
404 | "%ld %ld lineto\n" | |
405 | "closepath clip newpath\n", | |
406 | XLOG2DEV(x), YLOG2DEV(y), | |
407 | XLOG2DEV(x+w), YLOG2DEV(y), | |
408 | XLOG2DEV(x+w), YLOG2DEV(y+h), | |
409 | XLOG2DEV(x), YLOG2DEV(y+h) ); | |
ed880dd4 RR |
410 | } |
411 | ||
412 | void wxPostScriptDC::SetClippingRegion( const wxRegion &WXUNUSED(region) ) | |
413 | { | |
e90c1d2a | 414 | wxFAIL_MSG( T("wxPostScriptDC::SetClippingRegion not implemented.") ); |
ed880dd4 RR |
415 | } |
416 | ||
417 | void wxPostScriptDC::DestroyClippingRegion() | |
418 | { | |
e90c1d2a | 419 | wxCHECK_RET( m_ok && m_pstream, T("invalid postscript dc") ); |
afce4c03 | 420 | |
ed880dd4 | 421 | wxDC::DestroyClippingRegion(); |
afce4c03 | 422 | |
ed880dd4 RR |
423 | if (m_clipping) |
424 | { | |
425 | m_clipping = FALSE; | |
d7657f75 | 426 | fprintf( m_pstream, "grestore\n" ); |
ed880dd4 RR |
427 | } |
428 | } | |
429 | ||
430 | void wxPostScriptDC::Clear() | |
431 | { | |
e90c1d2a | 432 | wxFAIL_MSG( T("wxPostScriptDC::Clear not implemented.") ); |
ed880dd4 RR |
433 | } |
434 | ||
64698f9a | 435 | void wxPostScriptDC::DoFloodFill (long WXUNUSED(x), long WXUNUSED(y), const wxColour &WXUNUSED(col), int WXUNUSED(style)) |
ed880dd4 | 436 | { |
e90c1d2a | 437 | wxFAIL_MSG( T("wxPostScriptDC::FloodFill not implemented.") ); |
ed880dd4 RR |
438 | } |
439 | ||
64698f9a | 440 | bool wxPostScriptDC::DoGetPixel (long WXUNUSED(x), long WXUNUSED(y), wxColour * WXUNUSED(col)) const |
ed880dd4 | 441 | { |
e90c1d2a | 442 | wxFAIL_MSG( T("wxPostScriptDC::GetPixel not implemented.") ); |
ed880dd4 RR |
443 | return FALSE; |
444 | } | |
445 | ||
64698f9a | 446 | void wxPostScriptDC::DoCrossHair (long WXUNUSED(x), long WXUNUSED(y)) |
ed880dd4 | 447 | { |
e90c1d2a | 448 | wxFAIL_MSG( T("wxPostScriptDC::CrossHair not implemented.") ); |
ed880dd4 RR |
449 | } |
450 | ||
64698f9a | 451 | void wxPostScriptDC::DoDrawLine (long x1, long y1, long x2, long y2) |
ed880dd4 | 452 | { |
e90c1d2a | 453 | wxCHECK_RET( m_ok && m_pstream, T("invalid postscript dc") ); |
afce4c03 | 454 | |
ed880dd4 | 455 | if (m_pen.GetStyle() == wxTRANSPARENT) return; |
afce4c03 | 456 | |
ed880dd4 | 457 | SetPen( m_pen ); |
afce4c03 | 458 | |
d7657f75 RR |
459 | fprintf( m_pstream, |
460 | "newpath\n" | |
461 | "%ld %ld moveto\n" | |
462 | "%ld %ld lineto\n" | |
463 | "stroke\n", | |
464 | XLOG2DEV(x1), YLOG2DEV(y1), | |
465 | XLOG2DEV(x2), YLOG2DEV (y2) ); | |
afce4c03 | 466 | |
ed880dd4 RR |
467 | CalcBoundingBox( x1, y1 ); |
468 | CalcBoundingBox( x2, y2 ); | |
469 | } | |
470 | ||
471 | #define RAD2DEG 57.29577951308 | |
472 | ||
64698f9a | 473 | void wxPostScriptDC::DoDrawArc (long x1, long y1, long x2, long y2, long xc, long yc) |
ed880dd4 | 474 | { |
e90c1d2a | 475 | wxCHECK_RET( m_ok && m_pstream, T("invalid postscript dc") ); |
afce4c03 | 476 | |
ed880dd4 RR |
477 | long dx = x1 - xc; |
478 | long dy = y1 - yc; | |
479 | long radius = (long) sqrt(dx*dx+dy*dy); | |
480 | double alpha1, alpha2; | |
481 | ||
afce4c03 | 482 | if (x1 == x2 && y1 == y2) |
ed880dd4 | 483 | { |
d7657f75 RR |
484 | alpha1 = 0.0; |
485 | alpha2 = 360.0; | |
486 | } | |
487 | else if (radius == 0.0) | |
ed880dd4 | 488 | { |
d7657f75 RR |
489 | alpha1 = alpha2 = 0.0; |
490 | } | |
491 | else | |
ed880dd4 | 492 | { |
d7657f75 RR |
493 | alpha1 = (x1 - xc == 0) ? |
494 | (y1 - yc < 0) ? 90.0 : -90.0 : | |
495 | -atan2(double(y1-yc), double(x1-xc)) * RAD2DEG; | |
496 | alpha2 = (x2 - xc == 0) ? | |
497 | (y2 - yc < 0) ? 90.0 : -90.0 : | |
498 | -atan2(double(y2-yc), double(x2-xc)) * RAD2DEG; | |
ed880dd4 RR |
499 | } |
500 | while (alpha1 <= 0) alpha1 += 360; | |
501 | while (alpha2 <= 0) alpha2 += 360; // adjust angles to be between | |
502 | while (alpha1 > 360) alpha1 -= 360; // 0 and 360 degree | |
503 | while (alpha2 > 360) alpha2 -= 360; | |
504 | ||
afce4c03 | 505 | if (m_brush.GetStyle() != wxTRANSPARENT) |
ed880dd4 | 506 | { |
d7657f75 RR |
507 | SetBrush( m_brush ); |
508 | ||
509 | fprintf( m_pstream, | |
510 | "newpath\n" | |
511 | "%ld %ld %ld %ld %ld %ld ellipse\n" | |
512 | "%ld %ld lineto\n" | |
513 | "closepath\n" | |
514 | "fill\n", | |
515 | XLOG2DEV(xc), YLOG2DEV(yc), XLOG2DEVREL(radius), YLOG2DEVREL(radius), (long)alpha1, (long) alpha2, | |
516 | XLOG2DEV(xc), YLOG2DEV(yc) ); | |
517 | ||
518 | CalcBoundingBox( xc-radius, yc-radius ); | |
519 | CalcBoundingBox( xc+radius, yc+radius ); | |
ed880dd4 | 520 | } |
afce4c03 VZ |
521 | |
522 | if (m_pen.GetStyle() != wxTRANSPARENT) | |
ed880dd4 | 523 | { |
d7657f75 RR |
524 | SetPen( m_pen ); |
525 | ||
526 | fprintf( m_pstream, | |
527 | "newpath\n" | |
528 | "%ld %ld %ld %ld %ld %ld ellipse\n" | |
529 | "%ld %ld lineto\n" | |
530 | "stroke\n" | |
531 | "fill\n", | |
532 | XLOG2DEV(xc), YLOG2DEV(yc), XLOG2DEVREL(radius), YLOG2DEVREL(radius), (long)alpha1, (long) alpha2, | |
533 | XLOG2DEV(xc), YLOG2DEV(yc) ); | |
534 | ||
535 | CalcBoundingBox( xc-radius, yc-radius ); | |
536 | CalcBoundingBox( xc+radius, yc+radius ); | |
ed880dd4 | 537 | } |
ed880dd4 RR |
538 | } |
539 | ||
64698f9a | 540 | void wxPostScriptDC::DoDrawEllipticArc(long x,long y,long w,long h,double sa,double ea) |
ed880dd4 | 541 | { |
e90c1d2a | 542 | wxCHECK_RET( m_ok && m_pstream, T("invalid postscript dc") ); |
afce4c03 | 543 | |
ed880dd4 RR |
544 | if (sa>=360 || sa<=-360) sa=sa-int(sa/360)*360; |
545 | if (ea>=360 || ea<=-360) ea=ea-int(ea/360)*360; | |
546 | if (sa<0) sa+=360; | |
547 | if (ea<0) ea+=360; | |
afce4c03 | 548 | |
ed880dd4 RR |
549 | if (sa==ea) |
550 | { | |
551 | DrawEllipse(x,y,w,h); | |
552 | return; | |
553 | } | |
554 | ||
555 | if (m_brush.GetStyle () != wxTRANSPARENT) | |
556 | { | |
557 | SetBrush( m_brush ); | |
d7657f75 RR |
558 | |
559 | fprintf( m_pstream, | |
560 | "newpath\n" | |
561 | "%ld %ld %ld %ld %ld %ld true ellipticarc\n", | |
562 | XLOG2DEV(x+w/2), YLOG2DEV(y+h/2), XLOG2DEVREL(w/2), YLOG2DEVREL(h/2), (long)sa, (long)ea ); | |
563 | ||
ed880dd4 RR |
564 | CalcBoundingBox( x ,y ); |
565 | CalcBoundingBox( x+w, y+h ); | |
d7657f75 RR |
566 | } |
567 | ||
ed880dd4 RR |
568 | if (m_pen.GetStyle () != wxTRANSPARENT) |
569 | { | |
570 | SetPen( m_pen ); | |
571 | ||
d7657f75 RR |
572 | fprintf( m_pstream, |
573 | "newpath\n" | |
574 | "%ld %ld %ld %ld %ld %ld false ellipticarc\n", | |
575 | XLOG2DEV(x+w/2), YLOG2DEV(y+h/2), XLOG2DEVREL(w/2), YLOG2DEVREL(h/2), (long)sa, (long)ea ); | |
576 | ||
577 | CalcBoundingBox( x ,y ); | |
ed880dd4 RR |
578 | CalcBoundingBox( x+w, y+h ); |
579 | } | |
580 | } | |
581 | ||
64698f9a | 582 | void wxPostScriptDC::DoDrawPoint (long x, long y) |
ed880dd4 | 583 | { |
e90c1d2a | 584 | wxCHECK_RET( m_ok && m_pstream, T("invalid postscript dc") ); |
afce4c03 | 585 | |
ed880dd4 | 586 | if (m_pen.GetStyle() == wxTRANSPARENT) return; |
afce4c03 | 587 | |
ed880dd4 | 588 | SetPen (m_pen); |
afce4c03 | 589 | |
d7657f75 RR |
590 | fprintf( m_pstream, |
591 | "newpath\n" | |
592 | "%ld %ld moveto\n" | |
593 | "%ld %ld lineto\n" | |
594 | "stroke\n", | |
595 | XLOG2DEV(x), YLOG2DEV(y), | |
596 | XLOG2DEV(x+1), YLOG2DEV(y) ); | |
afce4c03 | 597 | |
ed880dd4 RR |
598 | CalcBoundingBox( x, y ); |
599 | } | |
600 | ||
64698f9a | 601 | void wxPostScriptDC::DoDrawPolygon (int n, wxPoint points[], long xoffset, long yoffset, int WXUNUSED(fillStyle)) |
ed880dd4 | 602 | { |
e90c1d2a | 603 | wxCHECK_RET( m_ok && m_pstream, T("invalid postscript dc") ); |
afce4c03 | 604 | |
ed880dd4 | 605 | if (n <= 0) return; |
afce4c03 | 606 | |
ed880dd4 RR |
607 | if (m_brush.GetStyle () != wxTRANSPARENT) |
608 | { | |
d7657f75 RR |
609 | SetBrush( m_brush ); |
610 | ||
611 | fprintf( m_pstream, "newpath\n" ); | |
afce4c03 | 612 | |
d7657f75 RR |
613 | long xx = XLOG2DEV(points[0].x + xoffset); |
614 | long yy = YLOG2DEV(points[0].y + yoffset); | |
615 | ||
616 | fprintf( m_pstream, "%ld %ld moveto\n", xx, yy ); | |
617 | ||
618 | CalcBoundingBox( points[0].x + xoffset, points[0].y + yoffset ); | |
ed880dd4 | 619 | |
d7657f75 RR |
620 | for (int i = 1; i < n; i++) |
621 | { | |
622 | xx = XLOG2DEV(points[i].x + xoffset); | |
623 | yy = YLOG2DEV(points[i].y + yoffset); | |
624 | ||
625 | fprintf( m_pstream, "%ld %ld lineto\n", xx, yy ); | |
ed880dd4 | 626 | |
d7657f75 RR |
627 | CalcBoundingBox( points[i].x + xoffset, points[i].y + yoffset); |
628 | } | |
629 | ||
630 | fprintf( m_pstream, "fill\n" ); | |
ed880dd4 RR |
631 | } |
632 | ||
633 | if (m_pen.GetStyle () != wxTRANSPARENT) | |
634 | { | |
d7657f75 RR |
635 | SetPen( m_pen ); |
636 | ||
637 | fprintf( m_pstream, "newpath\n" ); | |
ed880dd4 | 638 | |
d7657f75 RR |
639 | long xx = XLOG2DEV(points[0].x + xoffset); |
640 | long yy = YLOG2DEV(points[0].y + yoffset); | |
641 | ||
642 | fprintf( m_pstream, "%ld %ld moveto\n", xx, yy ); | |
643 | ||
644 | CalcBoundingBox( points[0].x + xoffset, points[0].y + yoffset ); | |
ed880dd4 | 645 | |
d7657f75 RR |
646 | for (int i = 1; i < n; i++) |
647 | { | |
648 | xx = XLOG2DEV(points[i].x + xoffset); | |
649 | yy = YLOG2DEV(points[i].y + yoffset); | |
650 | ||
651 | fprintf( m_pstream, "%ld %ld lineto\n", xx, yy ); | |
652 | ||
653 | CalcBoundingBox( points[i].x + xoffset, points[i].y + yoffset); | |
654 | } | |
ed880dd4 | 655 | |
d7657f75 | 656 | fprintf( m_pstream, "fill\n" ); |
ed880dd4 RR |
657 | } |
658 | } | |
659 | ||
64698f9a | 660 | void wxPostScriptDC::DoDrawLines (int n, wxPoint points[], long xoffset, long yoffset) |
ed880dd4 | 661 | { |
e90c1d2a | 662 | wxCHECK_RET( m_ok && m_pstream, T("invalid postscript dc") ); |
afce4c03 | 663 | |
d7657f75 RR |
664 | if (m_pen.GetStyle() == wxTRANSPARENT) return; |
665 | ||
666 | if (n <= 0) return; | |
afce4c03 | 667 | |
ed880dd4 | 668 | SetPen (m_pen); |
afce4c03 | 669 | |
9a6be59a VZ |
670 | int i; |
671 | for ( i =0; i<n ; i++ ) | |
ed880dd4 | 672 | { |
9a6be59a | 673 | CalcBoundingBox( XLOG2DEV(points[i].x+xoffset), YLOG2DEV(points[i].y+yoffset)); |
ed880dd4 RR |
674 | } |
675 | ||
d7657f75 RR |
676 | fprintf( m_pstream, |
677 | "newpath\n" | |
678 | "%ld %ld moveto\n", | |
679 | XLOG2DEV(points[0].x+xoffset), YLOG2DEV(points[0].y+yoffset) ); | |
afce4c03 VZ |
680 | |
681 | for (i = 1; i < n; i++) | |
ed880dd4 | 682 | { |
d7657f75 RR |
683 | fprintf( m_pstream, |
684 | "%ld %ld lineto\n", | |
685 | XLOG2DEV(points[i].x+xoffset), YLOG2DEV(points[i].y+yoffset) ); | |
ed880dd4 | 686 | } |
afce4c03 | 687 | |
d7657f75 | 688 | fprintf( m_pstream, "stroke\n" ); |
ed880dd4 RR |
689 | } |
690 | ||
64698f9a | 691 | void wxPostScriptDC::DoDrawRectangle (long x, long y, long width, long height) |
ed880dd4 | 692 | { |
e90c1d2a | 693 | wxCHECK_RET( m_ok && m_pstream, T("invalid postscript dc") ); |
afce4c03 | 694 | |
ed880dd4 RR |
695 | if (m_brush.GetStyle () != wxTRANSPARENT) |
696 | { | |
697 | SetBrush( m_brush ); | |
d7657f75 RR |
698 | |
699 | fprintf( m_pstream, | |
700 | "newpath\n" | |
701 | "%ld %ld moveto\n" | |
702 | "%ld %ld lineto\n" | |
703 | "%ld %ld lineto\n" | |
704 | "%ld %ld lineto\n" | |
705 | "closepath\n" | |
706 | "fill\n", | |
707 | XLOG2DEV(x), YLOG2DEV(y), | |
708 | XLOG2DEV(x + width), YLOG2DEV(y), | |
709 | XLOG2DEV(x + width), YLOG2DEV(y + height), | |
710 | XLOG2DEV(x), YLOG2DEV(y + height) ); | |
ed880dd4 RR |
711 | |
712 | CalcBoundingBox( x, y ); | |
713 | CalcBoundingBox( x + width, y + height ); | |
714 | } | |
afce4c03 | 715 | |
ed880dd4 RR |
716 | if (m_pen.GetStyle () != wxTRANSPARENT) |
717 | { | |
718 | SetPen (m_pen); | |
719 | ||
d7657f75 RR |
720 | fprintf( m_pstream, |
721 | "newpath\n" | |
722 | "%ld %ld moveto\n" | |
723 | "%ld %ld lineto\n" | |
724 | "%ld %ld lineto\n" | |
725 | "%ld %ld lineto\n" | |
726 | "closepath\n" | |
727 | "stroke\n", | |
728 | XLOG2DEV(x), YLOG2DEV(y), | |
729 | XLOG2DEV(x + width), YLOG2DEV(y), | |
730 | XLOG2DEV(x + width), YLOG2DEV(y + height), | |
731 | XLOG2DEV(x), YLOG2DEV(y + height) ); | |
732 | ||
ed880dd4 RR |
733 | CalcBoundingBox( x, y ); |
734 | CalcBoundingBox( x + width, y + height ); | |
735 | } | |
736 | } | |
737 | ||
64698f9a | 738 | void wxPostScriptDC::DoDrawRoundedRectangle (long x, long y, long width, long height, double radius) |
ed880dd4 | 739 | { |
e90c1d2a | 740 | wxCHECK_RET( m_ok && m_pstream, T("invalid postscript dc") ); |
afce4c03 | 741 | |
ed880dd4 RR |
742 | if (radius < 0.0) |
743 | { | |
744 | // Now, a negative radius is interpreted to mean | |
745 | // 'the proportion of the smallest X or Y dimension' | |
746 | double smallest = 0.0; | |
747 | if (width < height) | |
9a6be59a | 748 | smallest = width; |
ed880dd4 | 749 | else |
9a6be59a | 750 | smallest = height; |
ed880dd4 RR |
751 | radius = (-radius * smallest); |
752 | } | |
afce4c03 | 753 | |
ed880dd4 RR |
754 | long rad = (long) radius; |
755 | ||
756 | if (m_brush.GetStyle () != wxTRANSPARENT) | |
757 | { | |
758 | SetBrush( m_brush ); | |
afce4c03 | 759 | |
d7657f75 RR |
760 | /* Draw rectangle anticlockwise */ |
761 | fprintf( m_pstream, | |
762 | "newpath\n" | |
763 | "%ld %ld %ld 90 180 arc\n" | |
764 | "%ld %ld moveto\n" | |
765 | "%ld %ld %ld 180 270 arc\n" | |
766 | "%ld %ld lineto\n" | |
767 | "%ld %ld %ld 270 0 arc\n" | |
768 | "%ld %ld lineto\n" | |
769 | "%ld %ld %ld 0 90 arc\n" | |
770 | "%ld %ld lineto\n" | |
771 | "closepath\n" | |
772 | "fill\n", | |
773 | XLOG2DEV(x + rad), YLOG2DEV(y + rad), XLOG2DEVREL(rad), | |
774 | XLOG2DEV(x), YLOG2DEV(y + rad), | |
775 | XLOG2DEV(x + rad), YLOG2DEV(y + height - rad), XLOG2DEVREL(rad), | |
776 | XLOG2DEV(x + width - rad), YLOG2DEV(y + height), | |
777 | XLOG2DEV(x + width - rad), YLOG2DEV(y + height - rad), XLOG2DEVREL(rad), | |
778 | XLOG2DEV(x + width), YLOG2DEV(y + rad), | |
779 | XLOG2DEV(x + width - rad), YLOG2DEV(y + rad), XLOG2DEVREL(rad), | |
780 | XLOG2DEV(x + rad), YLOG2DEV(y) ); | |
ed880dd4 RR |
781 | |
782 | CalcBoundingBox( x, y ); | |
783 | CalcBoundingBox( x + width, y + height ); | |
784 | } | |
afce4c03 | 785 | |
ed880dd4 RR |
786 | if (m_pen.GetStyle () != wxTRANSPARENT) |
787 | { | |
788 | SetPen (m_pen); | |
afce4c03 | 789 | |
d7657f75 RR |
790 | /* Draw rectangle anticlockwise */ |
791 | fprintf( m_pstream, | |
792 | "newpath\n" | |
793 | "%ld %ld %ld 90 180 arc\n" | |
794 | "%ld %ld moveto\n" | |
795 | "%ld %ld %ld 180 270 arc\n" | |
796 | "%ld %ld lineto\n" | |
797 | "%ld %ld %ld 270 0 arc\n" | |
798 | "%ld %ld lineto\n" | |
799 | "%ld %ld %ld 0 90 arc\n" | |
800 | "%ld %ld lineto\n" | |
801 | "closepath\n" | |
802 | "stroke\n", | |
803 | XLOG2DEV(x + rad), YLOG2DEV(y + rad), XLOG2DEVREL(rad), | |
804 | XLOG2DEV(x), YLOG2DEV(y + rad), | |
805 | XLOG2DEV(x + rad), YLOG2DEV(y + height - rad), XLOG2DEVREL(rad), | |
806 | XLOG2DEV(x + width - rad), YLOG2DEV(y + height), | |
807 | XLOG2DEV(x + width - rad), YLOG2DEV(y + height - rad), XLOG2DEVREL(rad), | |
808 | XLOG2DEV(x + width), YLOG2DEV(y + rad), | |
809 | XLOG2DEV(x + width - rad), YLOG2DEV(y + rad), XLOG2DEVREL(rad), | |
810 | XLOG2DEV(x + rad), YLOG2DEV(y) ); | |
ed880dd4 RR |
811 | |
812 | CalcBoundingBox( x, y ); | |
813 | CalcBoundingBox( x + width, y + height ); | |
814 | } | |
815 | } | |
816 | ||
64698f9a | 817 | void wxPostScriptDC::DoDrawEllipse (long x, long y, long width, long height) |
ed880dd4 | 818 | { |
e90c1d2a | 819 | wxCHECK_RET( m_ok && m_pstream, T("invalid postscript dc") ); |
afce4c03 | 820 | |
ed880dd4 RR |
821 | if (m_brush.GetStyle () != wxTRANSPARENT) |
822 | { | |
823 | SetBrush (m_brush); | |
824 | ||
d7657f75 RR |
825 | fprintf( m_pstream, |
826 | "newpath\n" | |
827 | "%ld %ld %ld %ld 0 360 ellipse\n" | |
828 | "fill\n", | |
829 | XLOG2DEV(x + width / 2), YLOG2DEV(y + height / 2), | |
830 | XLOG2DEV(width / 2), YLOG2DEVREL(height / 2) ); | |
ed880dd4 RR |
831 | |
832 | CalcBoundingBox( x - width, y - height ); | |
833 | CalcBoundingBox( x + width, y + height ); | |
834 | } | |
afce4c03 | 835 | |
d7657f75 | 836 | if (m_pen.GetStyle () != wxTRANSPARENT) |
ed880dd4 RR |
837 | { |
838 | SetPen (m_pen); | |
839 | ||
d7657f75 RR |
840 | fprintf( m_pstream, |
841 | "newpath\n" | |
842 | "%ld %ld %ld %ld 0 360 ellipse\n" | |
843 | "stroke\n", | |
844 | XLOG2DEV(x + width / 2), YLOG2DEV(y + height / 2), | |
845 | XLOG2DEV(width / 2), YLOG2DEVREL(height / 2) ); | |
ed880dd4 RR |
846 | |
847 | CalcBoundingBox( x - width, y - height ); | |
848 | CalcBoundingBox( x + width, y + height ); | |
849 | } | |
850 | } | |
851 | ||
64698f9a | 852 | void wxPostScriptDC::DoDrawIcon( const wxIcon& icon, long x, long y ) |
ed880dd4 | 853 | { |
ef539066 | 854 | DrawBitmap( icon, x, y, TRUE ); |
ed880dd4 RR |
855 | } |
856 | ||
d7657f75 RR |
857 | /* this has to be char, not wxChar */ |
858 | static char hexArray[] = "0123456789ABCDEF"; | |
859 | static void LocalDecToHex( int dec, char *buf ) | |
860 | { | |
861 | int firstDigit = (int)(dec/16.0); | |
862 | int secondDigit = (int)(dec - (firstDigit*16.0)); | |
863 | buf[0] = hexArray[firstDigit]; | |
864 | buf[1] = hexArray[secondDigit]; | |
865 | buf[2] = 0; | |
866 | } | |
867 | ||
64698f9a | 868 | void wxPostScriptDC::DoDrawBitmap( const wxBitmap& bitmap, long x, long y, bool WXUNUSED(useMask) ) |
ed880dd4 | 869 | { |
e90c1d2a | 870 | wxCHECK_RET( m_ok && m_pstream, T("invalid postscript dc") ); |
ef539066 RR |
871 | |
872 | if (!bitmap.Ok()) return; | |
afce4c03 | 873 | |
ef539066 | 874 | wxImage image( bitmap ); |
afce4c03 | 875 | |
ef539066 | 876 | if (!image.Ok()) return; |
afce4c03 | 877 | |
d7657f75 RR |
878 | long ww = XLOG2DEVREL(image.GetWidth()); |
879 | long hh = YLOG2DEVREL(image.GetHeight()); | |
afce4c03 | 880 | |
ef539066 | 881 | image = image.Scale( ww, hh ); |
afce4c03 | 882 | |
ef539066 | 883 | if (!image.Ok()) return; |
afce4c03 | 884 | |
d7657f75 RR |
885 | long xx = XLOG2DEV(x); |
886 | long yy = YLOG2DEV(y + bitmap.GetHeight()); | |
887 | ||
888 | fprintf( m_pstream, | |
889 | "/origstate save def\n" | |
890 | "20 dict begin\n" | |
891 | "/pix %ld string def\n" | |
892 | "/grays %ld string def\n" | |
893 | "/npixels 0 def\n" | |
894 | "/rgbindx 0 def\n" | |
895 | "%ld %ld translate\n" | |
896 | "%ld %ld scale\n" | |
897 | "%ld %ld 8\n" | |
898 | "[%ld 0 0 %ld 0 %ld]\n" | |
899 | "{currentfile pix readhexstring pop}\n" | |
900 | "false 3 colorimage\n", | |
901 | ww, ww, xx, yy, ww, hh, ww, hh, ww, -hh, hh ); | |
afce4c03 | 902 | |
ef539066 RR |
903 | for (int j = 0; j < hh; j++) |
904 | { | |
905 | for (int i = 0; i < ww; i++) | |
906 | { | |
d7657f75 RR |
907 | char buffer[5]; |
908 | LocalDecToHex( image.GetRed(i,j), buffer ); | |
909 | fprintf( m_pstream, buffer ); | |
910 | LocalDecToHex( image.GetGreen(i,j), buffer ); | |
911 | fprintf( m_pstream, buffer ); | |
912 | LocalDecToHex( image.GetBlue(i,j), buffer ); | |
913 | fprintf( m_pstream, buffer ); | |
914 | } | |
915 | fprintf( m_pstream, "\n" ); | |
ef539066 RR |
916 | } |
917 | ||
d7657f75 RR |
918 | fprintf( m_pstream, "end\n" ); |
919 | fprintf( m_pstream, "origstate restore\n" ); | |
ed880dd4 RR |
920 | } |
921 | ||
36b3b54a | 922 | void wxPostScriptDC::SetFont( const wxFont& font ) |
ed880dd4 | 923 | { |
e90c1d2a | 924 | wxCHECK_RET( m_ok && m_pstream, T("invalid postscript dc") ); |
afce4c03 | 925 | |
ed880dd4 | 926 | if (!font.Ok()) return; |
afce4c03 | 927 | |
ed880dd4 | 928 | m_font = font; |
57c208c5 | 929 | |
36b3b54a RR |
930 | const char *name; |
931 | const char *style = ""; | |
932 | int Style = m_font.GetStyle(); | |
933 | int Weight = m_font.GetWeight(); | |
934 | ||
d7657f75 | 935 | switch (m_font.GetFamily()) |
36b3b54a RR |
936 | { |
937 | case wxTELETYPE: | |
938 | case wxMODERN: | |
939 | name = "/Courier"; | |
940 | break; | |
941 | case wxSWISS: | |
942 | name = "/Helvetica"; | |
943 | break; | |
944 | case wxROMAN: | |
945 | // name = "/Times-Roman"; | |
946 | name = "/Times"; // Altered by EDZ | |
947 | break; | |
948 | case wxSCRIPT: | |
949 | name = "/Zapf-Chancery-MediumItalic"; | |
950 | Style = wxNORMAL; | |
951 | Weight = wxNORMAL; | |
952 | break; | |
953 | default: | |
954 | case wxDEFAULT: // Sans Serif Font | |
955 | name = "/LucidaSans"; | |
956 | } | |
957 | ||
958 | if (Style == wxNORMAL && (Weight == wxNORMAL || Weight == wxLIGHT)) | |
57c208c5 | 959 | { |
d7657f75 RR |
960 | if (m_font.GetFamily () == wxROMAN) |
961 | style = "-Roman"; | |
962 | else | |
963 | style = ""; | |
57c208c5 | 964 | } |
36b3b54a RR |
965 | else if (Style == wxNORMAL && Weight == wxBOLD) |
966 | { | |
d7657f75 | 967 | style = "-Bold"; |
36b3b54a RR |
968 | } |
969 | else if (Style == wxITALIC && (Weight == wxNORMAL || Weight == wxLIGHT)) | |
57c208c5 | 970 | { |
d7657f75 RR |
971 | if (m_font.GetFamily () == wxROMAN) |
972 | style = "-Italic"; | |
973 | else | |
974 | style = "-Oblique"; | |
57c208c5 | 975 | } |
36b3b54a | 976 | else if (Style == wxITALIC && Weight == wxBOLD) |
57c208c5 | 977 | { |
d7657f75 RR |
978 | if (m_font.GetFamily () == wxROMAN) |
979 | style = "-BoldItalic"; | |
980 | else | |
981 | style = "-BoldOblique"; | |
57c208c5 | 982 | } |
36b3b54a | 983 | else if (Style == wxSLANT && (Weight == wxNORMAL || Weight == wxLIGHT)) |
57c208c5 | 984 | { |
d7657f75 RR |
985 | if (m_font.GetFamily () == wxROMAN) |
986 | style = "-Italic"; | |
987 | else | |
988 | style = "-Oblique"; | |
57c208c5 | 989 | } |
36b3b54a | 990 | else if (Style == wxSLANT && Weight == wxBOLD) |
57c208c5 | 991 | { |
d7657f75 RR |
992 | if (m_font.GetFamily () == wxROMAN) |
993 | style = "-BoldItalic"; | |
994 | else | |
995 | style = "-BoldOblique"; | |
57c208c5 | 996 | } |
36b3b54a RR |
997 | else |
998 | { | |
d7657f75 | 999 | style = ""; |
36b3b54a | 1000 | } |
57c208c5 | 1001 | |
d7657f75 RR |
1002 | char buffer[100]; |
1003 | strcpy( buffer, name ); | |
1004 | strcat( buffer, style ); | |
afce4c03 | 1005 | |
d7657f75 RR |
1006 | fprintf( m_pstream, buffer ); |
1007 | fprintf( m_pstream, " reencodeISO def\n" ); | |
1008 | fprintf( m_pstream, buffer ); | |
1009 | fprintf( m_pstream, " findfont\n" ); | |
e6f1ad22 HH |
1010 | #ifdef __WXMSW__ |
1011 | fprintf( m_pstream, "%d scalefont setfont\n", YLOG2DEVREL(m_font.GetPointSize()) ); | |
1012 | #else | |
d7657f75 | 1013 | fprintf( m_pstream, "%ld scalefont setfont\n", YLOG2DEVREL(m_font.GetPointSize()) ); |
e6f1ad22 | 1014 | #endif |
ed880dd4 RR |
1015 | } |
1016 | ||
1017 | void wxPostScriptDC::SetPen( const wxPen& pen ) | |
1018 | { | |
e90c1d2a | 1019 | wxCHECK_RET( m_ok && m_pstream, T("invalid postscript dc") ); |
afce4c03 | 1020 | |
ed880dd4 | 1021 | if (!pen.Ok()) return; |
afce4c03 | 1022 | |
ed880dd4 RR |
1023 | int oldStyle = m_pen.GetStyle(); |
1024 | ||
1025 | m_pen = pen; | |
1026 | ||
e6f1ad22 HH |
1027 | #ifdef __WXMSW__ |
1028 | fprintf( m_pstream, "%d setlinewidth\n", XLOG2DEVREL(m_pen.GetWidth()) ); | |
1029 | #else | |
d7657f75 | 1030 | fprintf( m_pstream, "%ld setlinewidth\n", XLOG2DEVREL(m_pen.GetWidth()) ); |
e6f1ad22 | 1031 | #endif |
ed880dd4 RR |
1032 | /* |
1033 | Line style - WRONG: 2nd arg is OFFSET | |
afce4c03 | 1034 | |
ed880dd4 RR |
1035 | Here, I'm afraid you do not conceive meaning of parameters of 'setdash' |
1036 | operator correctly. You should look-up this in the Red Book: the 2nd parame- | |
1037 | ter is not number of values in the array of the first one, but an offset | |
1038 | into this description of the pattern. I mean a real *offset* not index | |
1039 | into array. I.e. If the command is [3 4] 1 setdash is used, then there | |
1040 | will be first black line *2* units long, then space 4 units, then the | |
1041 | pattern of *3* units black, 4 units space will be repeated. | |
1042 | */ | |
1043 | ||
1044 | static const char *dotted = "[2 5] 2"; | |
1045 | static const char *short_dashed = "[4 4] 2"; | |
1046 | static const char *long_dashed = "[4 8] 2"; | |
1047 | static const char *dotted_dashed = "[6 6 2 6] 4"; | |
1048 | ||
1049 | const char *psdash = (char *) NULL; | |
d7657f75 | 1050 | switch (m_pen.GetStyle()) |
ed880dd4 RR |
1051 | { |
1052 | case wxDOT: psdash = dotted; break; | |
1053 | case wxSHORT_DASH: psdash = short_dashed; break; | |
1054 | case wxLONG_DASH: psdash = long_dashed; break; | |
1055 | case wxDOT_DASH: psdash = dotted_dashed; break; | |
1056 | case wxSOLID: | |
1057 | case wxTRANSPARENT: | |
1058 | default: psdash = "[] 0"; break; | |
1059 | } | |
afce4c03 | 1060 | |
ed880dd4 RR |
1061 | if (oldStyle != m_pen.GetStyle()) |
1062 | { | |
d7657f75 RR |
1063 | fprintf( m_pstream, psdash ); |
1064 | fprintf( m_pstream," setdash\n" ); | |
ed880dd4 RR |
1065 | } |
1066 | ||
1067 | // Line colour | |
1068 | unsigned char red = m_pen.GetColour().Red(); | |
1069 | unsigned char blue = m_pen.GetColour().Blue(); | |
1070 | unsigned char green = m_pen.GetColour().Green(); | |
1071 | ||
1072 | if (!m_colour) | |
1073 | { | |
1074 | // Anything not white is black | |
d7657f75 RR |
1075 | if (! (red == (unsigned char) 255 && |
1076 | blue == (unsigned char) 255 && | |
1077 | green == (unsigned char) 255) ) | |
1078 | { | |
1079 | red = (unsigned char) 0; | |
1080 | green = (unsigned char) 0; | |
1081 | blue = (unsigned char) 0; | |
1082 | } | |
1083 | // setgray here ? | |
ed880dd4 RR |
1084 | } |
1085 | ||
1086 | if (!(red == m_currentRed && green == m_currentGreen && blue == m_currentBlue)) | |
1087 | { | |
d7657f75 RR |
1088 | double redPS = (double)(red) / 255.0; |
1089 | double bluePS = (double)(blue) / 255.0; | |
1090 | double greenPS = (double)(green) / 255.0; | |
1091 | ||
1092 | fprintf( m_pstream, | |
1093 | "%.8f %.8f %.8f setrgbcolor\n", | |
1094 | redPS, greenPS, bluePS ); | |
1095 | ||
1096 | m_currentRed = red; | |
1097 | m_currentBlue = blue; | |
1098 | m_currentGreen = green; | |
ed880dd4 RR |
1099 | } |
1100 | } | |
1101 | ||
1102 | void wxPostScriptDC::SetBrush( const wxBrush& brush ) | |
1103 | { | |
e90c1d2a | 1104 | wxCHECK_RET( m_ok && m_pstream, T("invalid postscript dc") ); |
afce4c03 | 1105 | |
ed880dd4 | 1106 | if (!brush.Ok()) return; |
afce4c03 | 1107 | |
ed880dd4 RR |
1108 | m_brush = brush; |
1109 | ||
1110 | // Brush colour | |
3c1b65a4 RR |
1111 | unsigned char red = m_brush.GetColour().Red(); |
1112 | unsigned char blue = m_brush.GetColour().Blue(); | |
1113 | unsigned char green = m_brush.GetColour().Green(); | |
ed880dd4 RR |
1114 | |
1115 | if (!m_colour) | |
1116 | { | |
d7657f75 RR |
1117 | // Anything not white is black |
1118 | if (! (red == (unsigned char) 255 && | |
1119 | blue == (unsigned char) 255 && | |
1120 | green == (unsigned char) 255) ) | |
1121 | { | |
1122 | red = (unsigned char) 0; | |
1123 | green = (unsigned char) 0; | |
1124 | blue = (unsigned char) 0; | |
1125 | } | |
1126 | // setgray here ? | |
ed880dd4 | 1127 | } |
d7657f75 | 1128 | |
ed880dd4 RR |
1129 | if (!(red == m_currentRed && green == m_currentGreen && blue == m_currentBlue)) |
1130 | { | |
d7657f75 RR |
1131 | double redPS = (double)(red) / 255.0; |
1132 | double bluePS = (double)(blue) / 255.0; | |
1133 | double greenPS = (double)(green) / 255.0; | |
1134 | ||
1135 | fprintf( m_pstream, | |
1136 | "%.8f %.8f %.8f setrgbcolor\n", | |
1137 | redPS, greenPS, bluePS ); | |
1138 | ||
1139 | m_currentRed = red; | |
1140 | m_currentBlue = blue; | |
1141 | m_currentGreen = green; | |
ed880dd4 RR |
1142 | } |
1143 | } | |
1144 | ||
64698f9a | 1145 | void wxPostScriptDC::DoDrawText( const wxString& text, long x, long y ) |
ed880dd4 | 1146 | { |
e90c1d2a | 1147 | wxCHECK_RET( m_ok && m_pstream, T("invalid postscript dc") ); |
afce4c03 | 1148 | |
ed880dd4 RR |
1149 | SetFont( m_font ); |
1150 | ||
d7657f75 | 1151 | if (m_textForegroundColour.Ok()) |
ed880dd4 | 1152 | { |
d7657f75 RR |
1153 | unsigned char red = m_textForegroundColour.Red(); |
1154 | unsigned char blue = m_textForegroundColour.Blue(); | |
1155 | unsigned char green = m_textForegroundColour.Green(); | |
ed880dd4 RR |
1156 | |
1157 | if (!m_colour) | |
9a6be59a | 1158 | { |
d7657f75 RR |
1159 | // Anything not white is black |
1160 | if (! (red == (unsigned char) 255 && | |
1161 | blue == (unsigned char) 255 && | |
1162 | green == (unsigned char) 255)) | |
1163 | { | |
1164 | red = (unsigned char) 0; | |
1165 | green = (unsigned char) 0; | |
1166 | blue = (unsigned char) 0; | |
1167 | } | |
9a6be59a | 1168 | } |
afce4c03 | 1169 | |
d7657f75 | 1170 | // maybe setgray here ? |
ed880dd4 RR |
1171 | if (!(red == m_currentRed && green == m_currentGreen && blue == m_currentBlue)) |
1172 | { | |
d7657f75 RR |
1173 | double redPS = (double)(red) / 255.0; |
1174 | double bluePS = (double)(blue) / 255.0; | |
1175 | double greenPS = (double)(green) / 255.0; | |
1176 | ||
1177 | fprintf( m_pstream, | |
1178 | "%.8f %.8f %.8f setrgbcolor\n", | |
1179 | redPS, greenPS, bluePS ); | |
1180 | ||
ed880dd4 RR |
1181 | m_currentRed = red; |
1182 | m_currentBlue = blue; | |
1183 | m_currentGreen = green; | |
1184 | } | |
1185 | } | |
1186 | ||
1187 | int size = m_font.GetPointSize(); | |
1188 | ||
d7657f75 RR |
1189 | long by = y + (long)floor( double(size) * 2.0 / 3.0 ); // approximate baseline |
1190 | fprintf( m_pstream, "%ld %ld moveto\n", XLOG2DEV(x), YLOG2DEV(by) ); | |
afce4c03 | 1191 | |
d7657f75 RR |
1192 | /* I don't know how to write char to a stream, so I use a mini string */ |
1193 | char tmpbuf[2]; | |
1194 | tmpbuf[1] = 0; | |
1195 | ||
1196 | fprintf( m_pstream, "(" ); | |
9626e0bf | 1197 | const wxWX2MBbuf textbuf = text.mb_str(); |
d7657f75 | 1198 | int len = strlen(textbuf); |
ed880dd4 RR |
1199 | int i; |
1200 | for (i = 0; i < len; i++) | |
1201 | { | |
87138c52 | 1202 | int c = (unsigned char) textbuf[i]; |
d7657f75 | 1203 | if (c == ')' || c == '(' || c == '\\') |
ed880dd4 | 1204 | { |
d7657f75 RR |
1205 | /* Cope with special characters */ |
1206 | fprintf( m_pstream, "\\" ); | |
1207 | tmpbuf[0] = (char) c; | |
1208 | fprintf( m_pstream, tmpbuf ); | |
ed880dd4 RR |
1209 | } |
1210 | else if ( c >= 128 ) | |
1211 | { | |
d7657f75 RR |
1212 | /* Cope with character codes > 127 */ |
1213 | fprintf(m_pstream, "\\%o", c); | |
ed880dd4 RR |
1214 | } |
1215 | else | |
d7657f75 RR |
1216 | { |
1217 | tmpbuf[0] = (char) c; | |
1218 | fprintf( m_pstream, tmpbuf ); | |
1219 | } | |
ed880dd4 | 1220 | } |
d7657f75 RR |
1221 | |
1222 | fprintf( m_pstream, ") show\n" ); | |
ed880dd4 RR |
1223 | |
1224 | if (m_font.GetUnderlined()) | |
1225 | { | |
1226 | long uy = (long)(y + size - m_underlinePosition); | |
1227 | long w, h; | |
1228 | GetTextExtent(text, &w, &h); | |
afce4c03 | 1229 | |
d7657f75 RR |
1230 | fprintf( m_pstream, |
1231 | "gsave\n" | |
1232 | "%ld %ld moveto\n" | |
1233 | "%ld setlinewidth\n" | |
1234 | "%ld %ld lineto\n" | |
1235 | "stroke\n" | |
1236 | "grestore\n", | |
1237 | XLOG2DEV(x), YLOG2DEV(uy), | |
1238 | (long)m_underlineThickness, | |
1239 | XLOG2DEV(x + w), YLOG2DEV(uy) ); | |
ed880dd4 | 1240 | } |
afce4c03 | 1241 | |
ed880dd4 RR |
1242 | CalcBoundingBox( x, y ); |
1243 | CalcBoundingBox( x + size * text.Length() * 2/3 , y ); | |
1244 | } | |
1245 | ||
1246 | ||
1247 | void wxPostScriptDC::SetBackground (const wxBrush& brush) | |
1248 | { | |
1249 | m_backgroundBrush = brush; | |
1250 | } | |
1251 | ||
1252 | void wxPostScriptDC::SetLogicalFunction (int WXUNUSED(function)) | |
1253 | { | |
e90c1d2a | 1254 | wxFAIL_MSG( T("wxPostScriptDC::SetLogicalFunction not implemented.") ); |
ed880dd4 RR |
1255 | } |
1256 | ||
64698f9a | 1257 | void wxPostScriptDC::DoDrawSpline( wxList *points ) |
ed880dd4 | 1258 | { |
e90c1d2a | 1259 | wxCHECK_RET( m_ok && m_pstream, T("invalid postscript dc") ); |
afce4c03 | 1260 | |
ed880dd4 RR |
1261 | SetPen( m_pen ); |
1262 | ||
1263 | double a, b, c, d, x1, y1, x2, y2, x3, y3; | |
1264 | wxPoint *p, *q; | |
1265 | ||
1266 | wxNode *node = points->First(); | |
1267 | p = (wxPoint *)node->Data(); | |
afce4c03 | 1268 | x1 = p->x; |
ed880dd4 RR |
1269 | y1 = p->y; |
1270 | ||
1271 | node = node->Next(); | |
1272 | p = (wxPoint *)node->Data(); | |
afce4c03 | 1273 | c = p->x; |
ed880dd4 RR |
1274 | d = p->y; |
1275 | x3 = a = (double)(x1 + c) / 2; | |
1276 | y3 = b = (double)(y1 + d) / 2; | |
1277 | ||
d7657f75 RR |
1278 | fprintf( m_pstream, |
1279 | "newpath\n" | |
1280 | "%ld %ld moveto\n" | |
1281 | "%ld %ld lineto\n", | |
1282 | XLOG2DEV((long)x1), YLOG2DEV((long)y1), | |
1283 | XLOG2DEV((long)x3), YLOG2DEV((long)y3) ); | |
afce4c03 | 1284 | |
ed880dd4 RR |
1285 | CalcBoundingBox( (long)x1, (long)y1 ); |
1286 | CalcBoundingBox( (long)x3, (long)y3 ); | |
1287 | ||
1288 | while ((node = node->Next()) != NULL) | |
1289 | { | |
1290 | q = (wxPoint *)node->Data(); | |
1291 | ||
d7657f75 RR |
1292 | x1 = x3; |
1293 | y1 = y3; | |
1294 | x2 = c; | |
1295 | y2 = d; | |
1296 | c = q->x; | |
1297 | d = q->y; | |
ed880dd4 RR |
1298 | x3 = (double)(x2 + c) / 2; |
1299 | y3 = (double)(y2 + d) / 2; | |
d7657f75 RR |
1300 | |
1301 | fprintf( m_pstream, | |
1302 | "%ld %ld %ld %ld %ld %ld DrawSplineSection\n", | |
1303 | XLOG2DEV((long)x1), YLOG2DEV((long)y1), | |
1304 | XLOG2DEV((long)x2), YLOG2DEV((long)y2), | |
1305 | XLOG2DEV((long)x3), YLOG2DEV((long)y3) ); | |
ed880dd4 RR |
1306 | |
1307 | CalcBoundingBox( (long)x1, (long)y1 ); | |
1308 | CalcBoundingBox( (long)x3, (long)y3 ); | |
1309 | } | |
afce4c03 | 1310 | |
ed880dd4 RR |
1311 | /* |
1312 | At this point, (x2,y2) and (c,d) are the position of the | |
1313 | next-to-last and last point respectively, in the point list | |
1314 | */ | |
1315 | ||
d7657f75 RR |
1316 | fprintf( m_pstream, |
1317 | "%ld %ld lineto\n" | |
1318 | "stroke\n", | |
1319 | XLOG2DEV((long)c), YLOG2DEV((long)d) ); | |
ed880dd4 RR |
1320 | } |
1321 | ||
64698f9a | 1322 | long wxPostScriptDC::GetCharWidth() const |
ed880dd4 RR |
1323 | { |
1324 | // Chris Breeze: reasonable approximation using wxMODERN/Courier | |
1325 | return (long) (GetCharHeight() * 72.0 / 120.0); | |
1326 | } | |
1327 | ||
1328 | ||
1329 | void wxPostScriptDC::SetAxisOrientation( bool xLeftRight, bool yBottomUp ) | |
1330 | { | |
e90c1d2a | 1331 | wxCHECK_RET( m_ok && m_pstream, T("invalid postscript dc") ); |
afce4c03 | 1332 | |
ed880dd4 RR |
1333 | m_signX = (xLeftRight ? 1 : -1); |
1334 | m_signY = (yBottomUp ? 1 : -1); | |
afce4c03 | 1335 | |
9a6be59a VZ |
1336 | // FIXME there is no such function in MSW |
1337 | #ifndef __WXMSW__ | |
ed880dd4 | 1338 | ComputeScaleAndOrigin(); |
9a6be59a | 1339 | #endif |
ed880dd4 RR |
1340 | } |
1341 | ||
1342 | void wxPostScriptDC::SetDeviceOrigin( long x, long y ) | |
1343 | { | |
e90c1d2a | 1344 | wxCHECK_RET( m_ok && m_pstream, T("invalid postscript dc") ); |
afce4c03 | 1345 | |
4bc67cc5 RR |
1346 | int h = 0; |
1347 | int w = 0; | |
1348 | GetSize( &w, &h ); | |
afce4c03 | 1349 | |
4bc67cc5 | 1350 | wxDC::SetDeviceOrigin( x, h-y ); |
ed880dd4 RR |
1351 | } |
1352 | ||
4e4ea166 | 1353 | void wxPostScriptDC::DoGetSize(int* width, int* height) const |
ed880dd4 | 1354 | { |
7bcb11d3 | 1355 | wxPaperSize id = m_printData.GetPaperId(); |
ed880dd4 | 1356 | |
7bcb11d3 | 1357 | wxPrintPaperType *paper = wxThePrintPaperDatabase->FindPaperType(id); |
afce4c03 | 1358 | |
7bcb11d3 | 1359 | if (!paper) paper = wxThePrintPaperDatabase->FindPaperType(wxPAPER_A4); |
afce4c03 | 1360 | |
ac2def68 RR |
1361 | int w = 595; |
1362 | int h = 842; | |
ed880dd4 RR |
1363 | if (paper) |
1364 | { | |
ac2def68 RR |
1365 | w = paper->GetSizeDeviceUnits().x; |
1366 | h = paper->GetSizeDeviceUnits().y; | |
ed880dd4 | 1367 | } |
ac2def68 RR |
1368 | |
1369 | if (m_printData.GetOrientation() == wxLANDSCAPE) | |
ed880dd4 | 1370 | { |
ac2def68 RR |
1371 | int tmp = w; |
1372 | w = h; | |
1373 | h = tmp; | |
ed880dd4 | 1374 | } |
ac2def68 RR |
1375 | |
1376 | if (width) *width = w; | |
1377 | if (height) *height = h; | |
ed880dd4 RR |
1378 | } |
1379 | ||
4e4ea166 | 1380 | void wxPostScriptDC::DoGetSizeMM(int *width, int *height) const |
ed880dd4 | 1381 | { |
7bcb11d3 JS |
1382 | wxPaperSize id = m_printData.GetPaperId(); |
1383 | ||
1384 | wxPrintPaperType *paper = wxThePrintPaperDatabase->FindPaperType(id); | |
afce4c03 | 1385 | |
7bcb11d3 JS |
1386 | if (!paper) paper = wxThePrintPaperDatabase->FindPaperType(wxPAPER_A4); |
1387 | ||
ac2def68 RR |
1388 | int w = 210; |
1389 | int h = 297; | |
7bcb11d3 | 1390 | if (paper) |
ed880dd4 | 1391 | { |
ac2def68 RR |
1392 | w = paper->GetWidth() / 10; |
1393 | h = paper->GetHeight() / 10; | |
ed880dd4 | 1394 | } |
ac2def68 RR |
1395 | |
1396 | if (m_printData.GetOrientation() == wxLANDSCAPE) | |
ed880dd4 | 1397 | { |
ac2def68 RR |
1398 | int tmp = w; |
1399 | w = h; | |
1400 | h = tmp; | |
ed880dd4 | 1401 | } |
ac2def68 RR |
1402 | |
1403 | if (width) *width = w; | |
1404 | if (height) *height = h; | |
7bcb11d3 JS |
1405 | } |
1406 | ||
1407 | // Resolution in pixels per logical inch | |
1408 | wxSize wxPostScriptDC::GetPPI(void) const | |
1409 | { | |
1410 | return wxSize(72, 72); | |
1411 | } | |
ed880dd4 | 1412 | |
7bcb11d3 | 1413 | |
d7657f75 | 1414 | bool wxPostScriptDC::StartDoc( const wxString& message ) |
7bcb11d3 | 1415 | { |
e90c1d2a | 1416 | wxCHECK_MSG( m_ok, FALSE, T("invalid postscript dc") ); |
7bcb11d3 JS |
1417 | |
1418 | if (m_printData.GetFilename() == "") | |
1419 | { | |
1420 | wxString filename = wxGetTempFileName("ps"); | |
1421 | m_printData.SetFilename(filename); | |
7bcb11d3 | 1422 | } |
d7657f75 RR |
1423 | |
1424 | m_pstream = fopen( m_printData.GetFilename().fn_str(), "w+" ); | |
7bcb11d3 | 1425 | |
d7657f75 | 1426 | if (!m_pstream) |
ed880dd4 | 1427 | { |
d7657f75 | 1428 | wxMessageBox( _("Cannot open file for PostScript printing!"), _("Error"), wxOK ); |
7bcb11d3 JS |
1429 | m_ok = FALSE; |
1430 | return FALSE; | |
ed880dd4 | 1431 | } |
afce4c03 | 1432 | |
ed880dd4 RR |
1433 | m_ok = TRUE; |
1434 | ||
d7657f75 RR |
1435 | fprintf( m_pstream, "%%%%BeginProlog\n" ); |
1436 | fprintf( m_pstream, wxPostScriptHeaderEllipse ); | |
1437 | fprintf( m_pstream, wxPostScriptHeaderEllipticArc ); | |
1438 | fprintf( m_pstream, wxPostScriptHeaderColourImage ); | |
1439 | fprintf( m_pstream, wxPostScriptHeaderReencodeISO1 ); | |
1440 | fprintf( m_pstream, wxPostScriptHeaderReencodeISO2 ); | |
1441 | if (wxPostScriptHeaderSpline) | |
1442 | fprintf( m_pstream, wxPostScriptHeaderSpline ); | |
1443 | fprintf( m_pstream, "%%%%EndProlog\n" ); | |
1444 | ||
ed880dd4 RR |
1445 | SetBrush( *wxBLACK_BRUSH ); |
1446 | SetPen( *wxBLACK_PEN ); | |
1447 | SetBackground( *wxWHITE_BRUSH ); | |
1448 | SetTextForeground( *wxBLACK ); | |
1449 | ||
1450 | // set origin according to paper size | |
1451 | SetDeviceOrigin( 0,0 ); | |
afce4c03 | 1452 | |
ed880dd4 RR |
1453 | wxPageNumber = 1; |
1454 | m_pageNumber = 1; | |
1455 | m_title = message; | |
1456 | return TRUE; | |
1457 | } | |
1458 | ||
1459 | void wxPostScriptDC::EndDoc () | |
1460 | { | |
e90c1d2a | 1461 | wxCHECK_RET( m_ok && m_pstream, T("invalid postscript dc") ); |
afce4c03 | 1462 | |
ed880dd4 RR |
1463 | if (m_clipping) |
1464 | { | |
1465 | m_clipping = FALSE; | |
d7657f75 | 1466 | fprintf( m_pstream, "grestore\n" ); |
ed880dd4 RR |
1467 | } |
1468 | ||
d7657f75 RR |
1469 | fclose( m_pstream ); |
1470 | m_pstream = (FILE *) NULL; | |
ed880dd4 | 1471 | |
87138c52 | 1472 | wxChar *header_file = wxGetTempFileName("ps"); |
afce4c03 | 1473 | |
d7657f75 | 1474 | m_pstream = fopen( wxConvFile.cWX2MB(header_file) , "w+" ); |
ed880dd4 | 1475 | |
d7657f75 | 1476 | fprintf( m_pstream, "%%!PS-Adobe-2.0\n" ); // PostScript magic strings |
aece0491 | 1477 | fprintf( m_pstream, "%%%%Title: %s\n", (const char *)m_title.mb_str() ); |
ac2def68 | 1478 | fprintf( m_pstream, "%%%%Creator: %s\n", (const char*)wxConvCurrent->cWX2MB(wxTheApp->argv[0]) ); |
aece0491 | 1479 | fprintf( m_pstream, "%%%%CreationDate: %s\n", (const char *)wxNow().mb_str() ); |
ed880dd4 | 1480 | |
87138c52 | 1481 | wxChar userID[256]; |
ed880dd4 RR |
1482 | if ( wxGetEmailAddress(userID, sizeof(userID)) ) |
1483 | { | |
e90c1d2a | 1484 | fprintf( m_pstream, "%%%%For: %s ", wxMBSTRINGCAST wxConvCurrent->cWX2MB(userID) ); |
87138c52 | 1485 | wxChar userName[245]; |
ed880dd4 | 1486 | if (wxGetUserName(userName, sizeof(userName))) |
e90c1d2a | 1487 | fprintf( m_pstream, " (%s)", wxMBSTRINGCAST wxConvCurrent->cWX2MB(userName) ); |
d7657f75 | 1488 | fprintf( m_pstream, "\n" ); |
ed880dd4 RR |
1489 | } |
1490 | else if ( wxGetUserName(userID, sizeof(userID)) ) | |
1491 | { | |
e90c1d2a | 1492 | fprintf( m_pstream, "%%%%For: %s\n", wxMBSTRINGCAST wxConvCurrent->cWX2MB(userID) );; |
ed880dd4 RR |
1493 | } |
1494 | ||
1495 | // THE FOLLOWING HAS BEEN CONTRIBUTED BY Andy Fyfe <andy@hyperparallel.com> | |
1496 | ||
1497 | long wx_printer_translate_x, wx_printer_translate_y; | |
1498 | double wx_printer_scale_x, wx_printer_scale_y; | |
ed880dd4 | 1499 | |
7bcb11d3 JS |
1500 | wx_printer_translate_x = m_printData.GetPrinterTranslateX(); |
1501 | wx_printer_translate_y = m_printData.GetPrinterTranslateY(); | |
1502 | ||
1503 | wx_printer_scale_x = m_printData.GetPrinterScaleX(); | |
1504 | wx_printer_scale_y = m_printData.GetPrinterScaleY(); | |
1505 | ||
1506 | if (m_printData.GetOrientation() == wxLANDSCAPE) | |
d7657f75 | 1507 | fprintf( m_pstream, "%%%%Orientation: Landscape\n" ); |
ed880dd4 | 1508 | else |
d7657f75 | 1509 | fprintf( m_pstream, "%%%%Orientation: Portrait\n" ); |
ed880dd4 RR |
1510 | |
1511 | // Compute the bounding box. Note that it is in the default user | |
1512 | // coordinate system, thus we have to convert the values. | |
1513 | long llx = (long) ((XLOG2DEV(m_minX)+wx_printer_translate_x)*wx_printer_scale_x); | |
1514 | long lly = (long) ((YLOG2DEV(m_minY)+wx_printer_translate_y)*wx_printer_scale_y); | |
1515 | long urx = (long) ((XLOG2DEV(m_maxX)+wx_printer_translate_x)*wx_printer_scale_x); | |
1516 | long ury = (long) ((YLOG2DEV(m_maxY)+wx_printer_translate_y)*wx_printer_scale_y); | |
1517 | ||
1518 | // If we're landscape, our sense of "x" and "y" is reversed. | |
7bcb11d3 | 1519 | if (m_printData.GetOrientation() == wxLANDSCAPE) |
ed880dd4 RR |
1520 | { |
1521 | long tmp; | |
1522 | tmp = llx; llx = lly; lly = tmp; | |
1523 | tmp = urx; urx = ury; ury = tmp; | |
1524 | ||
1525 | // We need either the two lines that follow, or we need to subtract | |
1526 | // min_x from real_translate_y, which is commented out below. | |
1527 | llx = llx - (long)(m_minX*wx_printer_scale_y); | |
1528 | urx = urx - (long)(m_minX*wx_printer_scale_y); | |
1529 | } | |
1530 | ||
1531 | // The Adobe specifications call for integers; we round as to make | |
1532 | // the bounding larger. | |
d7657f75 RR |
1533 | fprintf( m_pstream, |
1534 | "%%%%BoundingBox: %ld %ld %ld %ld\n", | |
1535 | (long)floor((double)llx), (long)floor((double)lly), | |
1536 | (long)ceil((double)urx), (long)ceil((double)ury) ); | |
1537 | fprintf( m_pstream, "%%%%Pages: %d\n", (wxPageNumber - 1) ); | |
1538 | fprintf( m_pstream, "%%%%EndComments\n\n" ); | |
ed880dd4 RR |
1539 | |
1540 | // To check the correctness of the bounding box, postscript commands | |
1541 | // to draw a box corresponding to the bounding box are generated below. | |
1542 | // But since we typically don't want to print such a box, the postscript | |
1543 | // commands are generated within comments. These lines appear before any | |
1544 | // adjustment of scale, rotation, or translation, and hence are in the | |
1545 | // default user coordinates. | |
d7657f75 RR |
1546 | fprintf( m_pstream, "%% newpath\n" ); |
1547 | fprintf( m_pstream, "%% %ld %ld moveto\n", llx, lly ); | |
1548 | fprintf( m_pstream, "%% %ld %ld lineto\n", urx, lly ); | |
1549 | fprintf( m_pstream, "%% %ld %ld lineto\n", urx, ury ); | |
1550 | fprintf( m_pstream, "%% %ld %ld lineto closepath stroke\n", llx, ury ); | |
ed880dd4 | 1551 | |
d7657f75 RR |
1552 | fclose( m_pstream ); |
1553 | m_pstream = (FILE*) NULL; | |
ed880dd4 | 1554 | |
87138c52 | 1555 | wxChar *tmp_file = wxGetTempFileName("ps"); |
ed880dd4 RR |
1556 | |
1557 | // Paste header Before wx_printer_file | |
d7657f75 RR |
1558 | wxConcatFiles (header_file, m_printData.GetFilename(), tmp_file ); |
1559 | wxRemoveFile( header_file ); | |
1560 | wxRemoveFile( m_printData.GetFilename() ); | |
1561 | wxRenameFile( tmp_file, m_printData.GetFilename() ); | |
ed880dd4 RR |
1562 | |
1563 | #if defined(__X__) || defined(__WXGTK__) | |
d7657f75 | 1564 | if (m_ok) |
ed880dd4 | 1565 | { |
d7657f75 RR |
1566 | wxString previewCommand(m_printData.GetPreviewCommand()); |
1567 | wxString printerCommand(m_printData.GetPrinterCommand()); | |
1568 | wxString printerOptions(m_printData.GetPrinterOptions()); | |
1569 | wxString filename(m_printData.GetFilename()); | |
ed880dd4 | 1570 | |
d7657f75 RR |
1571 | switch (m_printData.GetPrintMode()) |
1572 | { | |
1573 | case wxPRINT_MODE_PREVIEW: | |
1574 | { | |
1575 | wxChar *argv[3]; | |
1576 | argv[0] = WXSTRINGCAST previewCommand; | |
1577 | argv[1] = WXSTRINGCAST filename; | |
1578 | argv[2] = (wxChar*) NULL; | |
1579 | wxExecute( argv, TRUE ); | |
1580 | wxRemoveFile( m_printData.GetFilename() ); | |
1581 | } | |
1582 | break; | |
1583 | case wxPRINT_MODE_PRINTER: | |
1584 | { | |
1585 | wxChar *argv[4]; | |
1586 | int argc = 0; | |
1587 | argv[argc++] = WXSTRINGCAST printerCommand; | |
1588 | ||
1589 | // !SM! If we simply assign to argv[1] here, if printer options | |
1590 | // are blank, we get an annoying and confusing message from lpr. | |
1591 | wxChar *opts = WXSTRINGCAST printerOptions; | |
1592 | if (opts && *opts) | |
1593 | argv[argc++] = opts; | |
1594 | ||
1595 | argv[argc++] = WXSTRINGCAST filename; | |
1596 | argv[argc++] = (wxChar *) NULL; | |
1597 | wxExecute( argv, TRUE ); | |
1598 | wxRemoveFile( filename ); | |
1599 | } | |
1600 | break; | |
1601 | case wxPRINT_MODE_FILE: | |
1602 | case wxPRINT_MODE_NONE: | |
1603 | break; | |
1604 | } | |
ed880dd4 RR |
1605 | } |
1606 | #endif | |
1607 | } | |
1608 | ||
d7657f75 | 1609 | void wxPostScriptDC::StartPage() |
ed880dd4 | 1610 | { |
e90c1d2a | 1611 | wxCHECK_RET( m_ok && m_pstream, T("invalid postscript dc") ); |
afce4c03 | 1612 | |
d7657f75 | 1613 | fprintf( m_pstream, "%%%%Page: %d\n", wxPageNumber++ ); |
afce4c03 | 1614 | |
ac2def68 | 1615 | // What is this one supposed to do? RR. |
ed880dd4 RR |
1616 | // *m_pstream << "matrix currentmatrix\n"; |
1617 | ||
1618 | // Added by Chris Breeze | |
1619 | ||
9a6be59a VZ |
1620 | // Each page starts with an "initgraphics" which resets the |
1621 | // transformation and so we need to reset the origin | |
1622 | // (and rotate the page for landscape printing) | |
afce4c03 | 1623 | |
afce4c03 VZ |
1624 | // Output scaling |
1625 | long translate_x, translate_y; | |
1626 | double scale_x, scale_y; | |
afce4c03 | 1627 | |
7bcb11d3 JS |
1628 | translate_x = m_printData.GetPrinterTranslateX(); |
1629 | translate_y = m_printData.GetPrinterTranslateY(); | |
1630 | ||
1631 | scale_x = m_printData.GetPrinterScaleX(); | |
1632 | scale_y = m_printData.GetPrinterScaleY(); | |
1633 | ||
1634 | if (m_printData.GetOrientation() == wxLANDSCAPE) | |
afce4c03 | 1635 | { |
ac2def68 RR |
1636 | int h; |
1637 | GetSize( (int*) NULL, &h ); | |
1638 | translate_y -= h; | |
1639 | fprintf( m_pstream, "90 rotate\n" ); | |
d7657f75 | 1640 | |
ac2def68 RR |
1641 | // I copied this one from a PostScript tutorial, but to no avail. RR. |
1642 | // fprintf( m_pstream, "90 rotate llx neg ury nef translate\n" ); | |
afce4c03 VZ |
1643 | } |
1644 | ||
ac2def68 RR |
1645 | fprintf( m_pstream, "%.8f %.8f scale\n", scale_x, scale_y ); |
1646 | fprintf( m_pstream, "%ld %ld translate\n", translate_x, translate_y ); | |
ed880dd4 RR |
1647 | } |
1648 | ||
1649 | void wxPostScriptDC::EndPage () | |
1650 | { | |
e90c1d2a | 1651 | wxCHECK_RET( m_ok && m_pstream, T("invalid postscript dc") ); |
afce4c03 | 1652 | |
d7657f75 | 1653 | fprintf( m_pstream, "showpage\n" ); |
ed880dd4 RR |
1654 | } |
1655 | ||
64698f9a | 1656 | bool wxPostScriptDC::DoBlit( long xdest, long ydest, |
ea6f44b5 | 1657 | long fwidth, long fheight, |
afce4c03 VZ |
1658 | wxDC *source, |
1659 | long xsrc, long ysrc, | |
45b776d4 | 1660 | int rop, bool WXUNUSED(useMask) ) |
ed880dd4 | 1661 | { |
e90c1d2a | 1662 | wxCHECK_MSG( m_ok && m_pstream, FALSE, T("invalid postscript dc") ); |
afce4c03 | 1663 | |
e90c1d2a | 1664 | wxCHECK_MSG( source, FALSE, T("invalid source dc") ); |
afce4c03 | 1665 | |
342b6a2f | 1666 | /* blit into a bitmap */ |
afce4c03 | 1667 | wxBitmap bitmap( (int)fwidth, (int)fheight ); |
45b776d4 JS |
1668 | wxMemoryDC memDC; |
1669 | memDC.SelectObject(bitmap); | |
342b6a2f | 1670 | memDC.Blit(0, 0, fwidth, fheight, source, xsrc, ysrc, rop); /* TODO: Blit transparently? */ |
45b776d4 | 1671 | memDC.SelectObject(wxNullBitmap); |
ea6f44b5 RR |
1672 | |
1673 | /* draw bitmap. scaling and positioning is done there */ | |
ea6f44b5 | 1674 | DrawBitmap( bitmap, xdest, ydest ); |
afce4c03 | 1675 | |
ed880dd4 RR |
1676 | return TRUE; |
1677 | } | |
1678 | ||
64698f9a | 1679 | long wxPostScriptDC::GetCharHeight() const |
ed880dd4 RR |
1680 | { |
1681 | if (m_font.Ok()) | |
1682 | return m_font.GetPointSize(); | |
1683 | else | |
1684 | return 12; | |
1685 | } | |
1686 | ||
342b6a2f | 1687 | void wxPostScriptDC::GetTextExtent( const wxString& string, long *x, long *y, |
64698f9a | 1688 | long *descent, long *externalLeading, wxFont *theFont ) const |
ed880dd4 | 1689 | { |
36b3b54a | 1690 | wxFont *fontToUse = theFont; |
afce4c03 | 1691 | |
36b3b54a RR |
1692 | if (!fontToUse) fontToUse = (wxFont*) &m_font; |
1693 | ||
e90c1d2a VZ |
1694 | wxCHECK_RET( fontToUse, T("GetTextExtent: no font defined") ); |
1695 | wxCHECK_RET( x, T("GetTextExtent: x == NULL") ); | |
1696 | wxCHECK_RET( y, T("GetTextExtent: y == NULL") ); | |
87138c52 | 1697 | |
9626e0bf | 1698 | const wxWX2MBbuf strbuf = string.mb_str(); |
ed880dd4 | 1699 | |
f036b31c | 1700 | #if !wxUSE_AFM_FOR_POSTSCRIPT |
36b3b54a RR |
1701 | /* Provide a VERY rough estimate (avoid using it). |
1702 | * Produces accurate results for mono-spaced font | |
1703 | * such as Courier (aka wxMODERN) */ | |
afce4c03 | 1704 | |
9a6be59a VZ |
1705 | int height = 12; |
1706 | if (fontToUse) | |
1707 | { | |
afce4c03 | 1708 | height = fontToUse->GetPointSize(); |
9a6be59a | 1709 | } |
87138c52 | 1710 | *x = strlen (strbuf) * height * 72 / 120; |
afce4c03 | 1711 | *y = (long) (height * 1.32); /* allow for descender */ |
36b3b54a RR |
1712 | if (descent) *descent = 0; |
1713 | if (externalLeading) *externalLeading = 0; | |
ed880dd4 | 1714 | #else |
ed880dd4 | 1715 | |
afce4c03 VZ |
1716 | /* method for calculating string widths in postscript: |
1717 | / read in the AFM (adobe font metrics) file for the | |
1718 | / actual font, parse it and extract the character widths | |
1719 | / and also the descender. this may be improved, but for now | |
1720 | / it works well. the AFM file is only read in if the | |
1721 | / font is changed. this may be chached in the future. | |
1722 | / calls to GetTextExtent with the font unchanged are rather | |
1723 | / efficient!!! | |
1724 | / | |
1725 | / for each font and style used there is an AFM file necessary. | |
1726 | / currently i have only files for the roman font family. | |
1727 | / I try to get files for the other ones! | |
1728 | / | |
1729 | / CAVE: the size of the string is currently always calculated | |
1730 | / in 'points' (1/72 of an inch). this should later on be | |
1731 | / changed to depend on the mapping mode. | |
1732 | / CAVE: the path to the AFM files must be set before calling this | |
1733 | / function. this is usually done by a call like the following: | |
1734 | / wxSetAFMPath("d:\\wxw161\\afm\\"); | |
1735 | / | |
1736 | / example: | |
1737 | / | |
1738 | / wxPostScriptDC dc(NULL, TRUE); | |
1739 | / if (dc.Ok()){ | |
1740 | / wxSetAFMPath("d:\\wxw161\\afm\\"); | |
1741 | / dc.StartDoc("Test"); | |
1742 | / dc.StartPage(); | |
1743 | / long w,h; | |
1744 | / dc.SetFont(new wxFont(10, wxROMAN, wxNORMAL, wxNORMAL)); | |
1745 | / dc.GetTextExtent("Hallo",&w,&h); | |
1746 | / dc.EndPage(); | |
1747 | / dc.EndDoc(); | |
1748 | / } | |
1749 | / | |
1750 | / by steve (stefan.hammes@urz.uni-heidelberg.de) | |
1751 | / created: 10.09.94 | |
1752 | / updated: 14.05.95 */ | |
36b3b54a RR |
1753 | |
1754 | /* these static vars are for storing the state between calls */ | |
1755 | static int lastFamily= INT_MIN; | |
1756 | static int lastSize= INT_MIN; | |
1757 | static int lastStyle= INT_MIN; | |
1758 | static int lastWeight= INT_MIN; | |
1759 | static int lastDescender = INT_MIN; | |
1760 | static int lastWidths[256]; /* widths of the characters */ | |
f036b31c | 1761 | |
a439ecef VS |
1762 | double UnderlinePosition = 0.0; |
1763 | double UnderlineThickness = 0.0; | |
36b3b54a RR |
1764 | |
1765 | /* get actual parameters */ | |
1766 | const int Family = fontToUse->GetFamily(); | |
1767 | const int Size = fontToUse->GetPointSize(); | |
1768 | const int Style = fontToUse->GetStyle(); | |
1769 | const int Weight = fontToUse->GetWeight(); | |
1770 | ||
1771 | /* if we have another font, read the font-metrics */ | |
1772 | if (Family!=lastFamily || Size!=lastSize || Style!=lastStyle || Weight!=lastWeight) | |
1773 | { | |
1774 | /* store actual values */ | |
1775 | lastFamily = Family; | |
1776 | lastSize = Size; | |
1777 | lastStyle = Style; | |
1778 | lastWeight = Weight; | |
1779 | ||
1780 | char *name = (char*) NULL; | |
1781 | ||
1782 | switch (Family) | |
afce4c03 VZ |
1783 | { |
1784 | case wxMODERN: | |
1785 | { | |
1786 | if ((Style == wxITALIC) && (Weight == wxBOLD)) name = "CourBoO"; | |
1787 | else if ((Style != wxITALIC) && (Weight == wxBOLD)) name = "CourBo"; | |
1788 | else if ((Style == wxITALIC) && (Weight != wxBOLD)) name = "Cour0"; | |
1789 | else name = "Cour"; | |
1790 | } | |
1791 | break; | |
1792 | case wxROMAN: | |
1793 | { | |
1794 | if ((Style == wxITALIC) && (Weight == wxBOLD)) name = "TimesBoO"; | |
1795 | else if ((Style != wxITALIC) && (Weight == wxBOLD)) name = "TimesBo"; | |
1796 | else if ((Style == wxITALIC) && (Weight != wxBOLD)) name = "TimesO"; | |
a439ecef | 1797 | else name = "TimesRo"; |
afce4c03 VZ |
1798 | } |
1799 | break; | |
1800 | default: | |
1801 | { | |
1802 | if ((Style == wxITALIC) && (Weight == wxBOLD)) name = "HelvBoO"; | |
1803 | else if ((Style != wxITALIC) && (Weight == wxBOLD)) name = "HelvBo"; | |
1804 | else if ((Style == wxITALIC) && (Weight != wxBOLD)) name = "Helv0"; | |
1805 | else name = "Helv"; | |
1806 | } | |
1807 | break; | |
1808 | } | |
36b3b54a RR |
1809 | |
1810 | /* get the directory of the AFM files */ | |
1811 | char afmName[256]; | |
1812 | afmName[0] = 0; | |
385bcb35 RR |
1813 | |
1814 | if (!m_printData.GetFontMetricPath().IsEmpty()) | |
1815 | { | |
f036b31c | 1816 | strcpy( afmName, m_printData.GetFontMetricPath().fn_str() ); |
385bcb35 | 1817 | } |
36b3b54a | 1818 | |
afce4c03 VZ |
1819 | /* 2. open and process the file |
1820 | / a short explanation of the AFM format: | |
1821 | / we have for each character a line, which gives its size | |
1822 | / e.g.: | |
1823 | / | |
1824 | / C 63 ; WX 444 ; N question ; B 49 -14 395 676 ; | |
1825 | / | |
1826 | / that means, we have a character with ascii code 63, and width | |
1827 | / (444/1000 * fontSize) points. | |
1828 | / the other data is ignored for now! | |
1829 | / | |
1830 | / when the font has changed, we read in the right AFM file and store the | |
1831 | / character widths in an array, which is processed below (see point 3.). */ | |
36b3b54a RR |
1832 | |
1833 | /* new elements JC Sun Aug 25 23:21:44 MET DST 1996 */ | |
1834 | ||
1835 | strcat(afmName,name); | |
1836 | strcat(afmName,".afm"); | |
1837 | FILE *afmFile = fopen(afmName,"r"); | |
385bcb35 RR |
1838 | |
1839 | #ifdef __UNIX__ | |
a439ecef VS |
1840 | if (afmFile==NULL) |
1841 | { | |
704d25e2 | 1842 | strcpy( afmName, "/usr/local/share/wx/afm/" ); |
a439ecef VS |
1843 | strcat(afmName,name); |
1844 | strcat(afmName,".afm"); | |
1845 | afmFile = fopen(afmName,"r"); | |
1846 | } | |
1847 | ||
1848 | if (afmFile==NULL) | |
1849 | { | |
704d25e2 | 1850 | strcpy( afmName, "/usr/share/wx/afm/" ); |
a439ecef VS |
1851 | strcat(afmName,name); |
1852 | strcat(afmName,".afm"); | |
1853 | afmFile = fopen(afmName,"r"); | |
1854 | } | |
385bcb35 RR |
1855 | #endif |
1856 | ||
1857 | if (afmFile==NULL) | |
36b3b54a RR |
1858 | { |
1859 | wxLogDebug( "GetTextExtent: can't open AFM file '%s'\n", afmName ); | |
1860 | wxLogDebug( " using approximate values\n"); | |
1861 | for (int i=0; i<256; i++) lastWidths[i] = 500; /* an approximate value */ | |
1862 | lastDescender = -150; /* dito. */ | |
ed880dd4 | 1863 | } |
afce4c03 VZ |
1864 | else |
1865 | { | |
36b3b54a RR |
1866 | /* init the widths array */ |
1867 | for(int i=0; i<256; i++) lastWidths[i] = INT_MIN; | |
1868 | /* some variables for holding parts of a line */ | |
1869 | char cString[10],semiString[10],WXString[10],descString[20]; | |
1870 | char upString[30], utString[30], encString[50]; | |
1871 | char line[256]; | |
1872 | int ascii,cWidth; | |
1873 | /* read in the file and parse it */ | |
1874 | while(fgets(line,sizeof(line),afmFile)!=NULL) | |
afce4c03 | 1875 | { |
36b3b54a RR |
1876 | /* A.) check for descender definition */ |
1877 | if (strncmp(line,"Descender",9)==0) | |
afce4c03 | 1878 | { |
36b3b54a | 1879 | if ((sscanf(line,"%s%d",descString,&lastDescender)!=2) || |
afce4c03 VZ |
1880 | (strcmp(descString,"Descender")!=0)) |
1881 | { | |
1882 | wxLogDebug( "AFM-file '%s': line '%s' has error (bad descender)\n", afmName,line ); | |
36b3b54a RR |
1883 | } |
1884 | } | |
1885 | /* JC 1.) check for UnderlinePosition */ | |
1886 | else if(strncmp(line,"UnderlinePosition",17)==0) | |
afce4c03 | 1887 | { |
36b3b54a | 1888 | if ((sscanf(line,"%s%lf",upString,&UnderlinePosition)!=2) || |
afce4c03 VZ |
1889 | (strcmp(upString,"UnderlinePosition")!=0)) |
1890 | { | |
1891 | wxLogDebug( "AFM-file '%s': line '%s' has error (bad UnderlinePosition)\n", afmName, line ); | |
36b3b54a RR |
1892 | } |
1893 | } | |
afce4c03 | 1894 | /* JC 2.) check for UnderlineThickness */ |
36b3b54a | 1895 | else if(strncmp(line,"UnderlineThickness",18)==0) |
afce4c03 | 1896 | { |
36b3b54a | 1897 | if ((sscanf(line,"%s%lf",utString,&UnderlineThickness)!=2) || |
afce4c03 VZ |
1898 | (strcmp(utString,"UnderlineThickness")!=0)) |
1899 | { | |
1900 | wxLogDebug( "AFM-file '%s': line '%s' has error (bad UnderlineThickness)\n", afmName, line ); | |
36b3b54a RR |
1901 | } |
1902 | } | |
afce4c03 | 1903 | /* JC 3.) check for EncodingScheme */ |
36b3b54a | 1904 | else if(strncmp(line,"EncodingScheme",14)==0) |
afce4c03 | 1905 | { |
36b3b54a | 1906 | if ((sscanf(line,"%s%s",utString,encString)!=2) || |
afce4c03 VZ |
1907 | (strcmp(utString,"EncodingScheme")!=0)) |
1908 | { | |
1909 | wxLogDebug("AFM-file '%s': line '%s' has error (bad EncodingScheme)\n", afmName, line ); | |
36b3b54a RR |
1910 | } |
1911 | else if (strncmp(encString, "AdobeStandardEncoding", 21)) | |
1912 | { | |
afce4c03 VZ |
1913 | wxLogDebug( "AFM-file '%s': line '%s' has error (unsupported EncodingScheme %s)\n", |
1914 | afmName,line, encString); | |
36b3b54a RR |
1915 | } |
1916 | } | |
1917 | /* B.) check for char-width */ | |
1918 | else if(strncmp(line,"C ",2)==0) | |
afce4c03 | 1919 | { |
36b3b54a | 1920 | if (sscanf(line,"%s%d%s%s%d",cString,&ascii,semiString,WXString,&cWidth)!=5) |
afce4c03 | 1921 | { |
36b3b54a RR |
1922 | wxLogDebug("AFM-file '%s': line '%s' has an error (bad character width)\n",afmName,line); |
1923 | } | |
1924 | if(strcmp(cString,"C")!=0 || strcmp(semiString,";")!=0 || strcmp(WXString,"WX")!=0) | |
afce4c03 | 1925 | { |
36b3b54a RR |
1926 | wxLogDebug("AFM-file '%s': line '%s' has a format error\n",afmName,line); |
1927 | } | |
1928 | /* printf(" char '%c'=%d has width '%d'\n",ascii,ascii,cWidth); */ | |
1929 | if (ascii>=0 && ascii<256) | |
afce4c03 | 1930 | { |
36b3b54a RR |
1931 | lastWidths[ascii] = cWidth; /* store width */ |
1932 | } | |
afce4c03 VZ |
1933 | else |
1934 | { | |
1935 | /* MATTHEW: this happens a lot; don't print an error */ | |
36b3b54a RR |
1936 | /* wxLogDebug("AFM-file '%s': ASCII value %d out of range\n",afmName,ascii); */ |
1937 | } | |
1938 | } | |
1939 | /* C.) ignore other entries. */ | |
1940 | } | |
1941 | fclose(afmFile); | |
ed880dd4 | 1942 | } |
36b3b54a | 1943 | /* hack to compute correct values for german 'Umlaute' |
afce4c03 VZ |
1944 | / the correct way would be to map the character names |
1945 | / like 'adieresis' to corresp. positions of ISOEnc and read | |
1946 | / these values from AFM files, too. Maybe later ... */ | |
36b3b54a RR |
1947 |