]>
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]; |
223d09f6 | 365 | wxStrcpy (tmp, wxT("/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()); | |
223d09f6 | 371 | wxStrcat (tmp2, wxT(".ps")); |
7bcb11d3 | 372 | m_printData.SetFilename(tmp2); |
ed880dd4 | 373 | } |
223d09f6 | 374 | else if ((m_printData.GetFilename() == wxT("")) && (m_printData.GetPrintMode() == wxPRINT_MODE_FILE)) |
ed880dd4 | 375 | { |
223d09f6 | 376 | wxString file = wxSaveFileSelector (_("PostScript"), wxT("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 | ||
7087444f | 390 | void wxPostScriptDC::DoSetClippingRegion (long x, long y, long w, long h) |
ed880dd4 | 391 | { |
223d09f6 | 392 | wxCHECK_RET( m_ok && m_pstream, wxT("invalid postscript dc") ); |
afce4c03 | 393 | |
7087444f | 394 | if (m_clipping) DestroyClippingRegion(); |
ed880dd4 | 395 | |
7087444f | 396 | wxDC::DoSetClippingRegion(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 | ||
ed880dd4 RR |
412 | |
413 | void wxPostScriptDC::DestroyClippingRegion() | |
414 | { | |
223d09f6 | 415 | wxCHECK_RET( m_ok && m_pstream, wxT("invalid postscript dc") ); |
afce4c03 | 416 | |
ed880dd4 RR |
417 | if (m_clipping) |
418 | { | |
419 | m_clipping = FALSE; | |
d7657f75 | 420 | fprintf( m_pstream, "grestore\n" ); |
ed880dd4 | 421 | } |
7087444f VS |
422 | |
423 | wxDC::DestroyClippingRegion(); | |
ed880dd4 RR |
424 | } |
425 | ||
426 | void wxPostScriptDC::Clear() | |
427 | { | |
223d09f6 | 428 | wxFAIL_MSG( wxT("wxPostScriptDC::Clear not implemented.") ); |
ed880dd4 RR |
429 | } |
430 | ||
64698f9a | 431 | void wxPostScriptDC::DoFloodFill (long WXUNUSED(x), long WXUNUSED(y), const wxColour &WXUNUSED(col), int WXUNUSED(style)) |
ed880dd4 | 432 | { |
223d09f6 | 433 | wxFAIL_MSG( wxT("wxPostScriptDC::FloodFill not implemented.") ); |
ed880dd4 RR |
434 | } |
435 | ||
64698f9a | 436 | bool wxPostScriptDC::DoGetPixel (long WXUNUSED(x), long WXUNUSED(y), wxColour * WXUNUSED(col)) const |
ed880dd4 | 437 | { |
223d09f6 | 438 | wxFAIL_MSG( wxT("wxPostScriptDC::GetPixel not implemented.") ); |
ed880dd4 RR |
439 | return FALSE; |
440 | } | |
441 | ||
64698f9a | 442 | void wxPostScriptDC::DoCrossHair (long WXUNUSED(x), long WXUNUSED(y)) |
ed880dd4 | 443 | { |
223d09f6 | 444 | wxFAIL_MSG( wxT("wxPostScriptDC::CrossHair not implemented.") ); |
ed880dd4 RR |
445 | } |
446 | ||
64698f9a | 447 | void wxPostScriptDC::DoDrawLine (long x1, long y1, long x2, long y2) |
ed880dd4 | 448 | { |
223d09f6 | 449 | wxCHECK_RET( m_ok && m_pstream, wxT("invalid postscript dc") ); |
afce4c03 | 450 | |
ed880dd4 | 451 | if (m_pen.GetStyle() == wxTRANSPARENT) return; |
afce4c03 | 452 | |
ed880dd4 | 453 | SetPen( m_pen ); |
afce4c03 | 454 | |
d7657f75 RR |
455 | fprintf( m_pstream, |
456 | "newpath\n" | |
457 | "%ld %ld moveto\n" | |
458 | "%ld %ld lineto\n" | |
459 | "stroke\n", | |
460 | XLOG2DEV(x1), YLOG2DEV(y1), | |
461 | XLOG2DEV(x2), YLOG2DEV (y2) ); | |
afce4c03 | 462 | |
ed880dd4 RR |
463 | CalcBoundingBox( x1, y1 ); |
464 | CalcBoundingBox( x2, y2 ); | |
465 | } | |
466 | ||
467 | #define RAD2DEG 57.29577951308 | |
468 | ||
64698f9a | 469 | void wxPostScriptDC::DoDrawArc (long x1, long y1, long x2, long y2, long xc, long yc) |
ed880dd4 | 470 | { |
223d09f6 | 471 | wxCHECK_RET( m_ok && m_pstream, wxT("invalid postscript dc") ); |
afce4c03 | 472 | |
ed880dd4 RR |
473 | long dx = x1 - xc; |
474 | long dy = y1 - yc; | |
be087207 | 475 | long radius = (long) sqrt( (double)(dx*dx+dy*dy) ); |
ed880dd4 RR |
476 | double alpha1, alpha2; |
477 | ||
afce4c03 | 478 | if (x1 == x2 && y1 == y2) |
ed880dd4 | 479 | { |
d7657f75 RR |
480 | alpha1 = 0.0; |
481 | alpha2 = 360.0; | |
482 | } | |
483 | else if (radius == 0.0) | |
ed880dd4 | 484 | { |
d7657f75 RR |
485 | alpha1 = alpha2 = 0.0; |
486 | } | |
487 | else | |
ed880dd4 | 488 | { |
d7657f75 RR |
489 | alpha1 = (x1 - xc == 0) ? |
490 | (y1 - yc < 0) ? 90.0 : -90.0 : | |
491 | -atan2(double(y1-yc), double(x1-xc)) * RAD2DEG; | |
492 | alpha2 = (x2 - xc == 0) ? | |
493 | (y2 - yc < 0) ? 90.0 : -90.0 : | |
494 | -atan2(double(y2-yc), double(x2-xc)) * RAD2DEG; | |
ed880dd4 RR |
495 | } |
496 | while (alpha1 <= 0) alpha1 += 360; | |
497 | while (alpha2 <= 0) alpha2 += 360; // adjust angles to be between | |
498 | while (alpha1 > 360) alpha1 -= 360; // 0 and 360 degree | |
499 | while (alpha2 > 360) alpha2 -= 360; | |
500 | ||
afce4c03 | 501 | if (m_brush.GetStyle() != wxTRANSPARENT) |
ed880dd4 | 502 | { |
d7657f75 RR |
503 | SetBrush( m_brush ); |
504 | ||
505 | fprintf( m_pstream, | |
506 | "newpath\n" | |
507 | "%ld %ld %ld %ld %ld %ld ellipse\n" | |
508 | "%ld %ld lineto\n" | |
509 | "closepath\n" | |
510 | "fill\n", | |
511 | XLOG2DEV(xc), YLOG2DEV(yc), XLOG2DEVREL(radius), YLOG2DEVREL(radius), (long)alpha1, (long) alpha2, | |
512 | XLOG2DEV(xc), YLOG2DEV(yc) ); | |
513 | ||
514 | CalcBoundingBox( xc-radius, yc-radius ); | |
515 | CalcBoundingBox( xc+radius, yc+radius ); | |
ed880dd4 | 516 | } |
afce4c03 VZ |
517 | |
518 | if (m_pen.GetStyle() != wxTRANSPARENT) | |
ed880dd4 | 519 | { |
d7657f75 RR |
520 | SetPen( m_pen ); |
521 | ||
522 | fprintf( m_pstream, | |
523 | "newpath\n" | |
524 | "%ld %ld %ld %ld %ld %ld ellipse\n" | |
525 | "%ld %ld lineto\n" | |
526 | "stroke\n" | |
527 | "fill\n", | |
528 | XLOG2DEV(xc), YLOG2DEV(yc), XLOG2DEVREL(radius), YLOG2DEVREL(radius), (long)alpha1, (long) alpha2, | |
529 | XLOG2DEV(xc), YLOG2DEV(yc) ); | |
530 | ||
531 | CalcBoundingBox( xc-radius, yc-radius ); | |
532 | CalcBoundingBox( xc+radius, yc+radius ); | |
ed880dd4 | 533 | } |
ed880dd4 RR |
534 | } |
535 | ||
64698f9a | 536 | void wxPostScriptDC::DoDrawEllipticArc(long x,long y,long w,long h,double sa,double ea) |
ed880dd4 | 537 | { |
223d09f6 | 538 | wxCHECK_RET( m_ok && m_pstream, wxT("invalid postscript dc") ); |
afce4c03 | 539 | |
ed880dd4 RR |
540 | if (sa>=360 || sa<=-360) sa=sa-int(sa/360)*360; |
541 | if (ea>=360 || ea<=-360) ea=ea-int(ea/360)*360; | |
542 | if (sa<0) sa+=360; | |
543 | if (ea<0) ea+=360; | |
afce4c03 | 544 | |
ed880dd4 RR |
545 | if (sa==ea) |
546 | { | |
547 | DrawEllipse(x,y,w,h); | |
548 | return; | |
549 | } | |
550 | ||
551 | if (m_brush.GetStyle () != wxTRANSPARENT) | |
552 | { | |
553 | SetBrush( m_brush ); | |
d7657f75 RR |
554 | |
555 | fprintf( m_pstream, | |
556 | "newpath\n" | |
557 | "%ld %ld %ld %ld %ld %ld true ellipticarc\n", | |
558 | XLOG2DEV(x+w/2), YLOG2DEV(y+h/2), XLOG2DEVREL(w/2), YLOG2DEVREL(h/2), (long)sa, (long)ea ); | |
559 | ||
ed880dd4 RR |
560 | CalcBoundingBox( x ,y ); |
561 | CalcBoundingBox( x+w, y+h ); | |
d7657f75 RR |
562 | } |
563 | ||
ed880dd4 RR |
564 | if (m_pen.GetStyle () != wxTRANSPARENT) |
565 | { | |
566 | SetPen( m_pen ); | |
567 | ||
d7657f75 RR |
568 | fprintf( m_pstream, |
569 | "newpath\n" | |
570 | "%ld %ld %ld %ld %ld %ld false ellipticarc\n", | |
571 | XLOG2DEV(x+w/2), YLOG2DEV(y+h/2), XLOG2DEVREL(w/2), YLOG2DEVREL(h/2), (long)sa, (long)ea ); | |
572 | ||
573 | CalcBoundingBox( x ,y ); | |
ed880dd4 RR |
574 | CalcBoundingBox( x+w, y+h ); |
575 | } | |
576 | } | |
577 | ||
64698f9a | 578 | void wxPostScriptDC::DoDrawPoint (long x, long y) |
ed880dd4 | 579 | { |
223d09f6 | 580 | wxCHECK_RET( m_ok && m_pstream, wxT("invalid postscript dc") ); |
afce4c03 | 581 | |
ed880dd4 | 582 | if (m_pen.GetStyle() == wxTRANSPARENT) return; |
afce4c03 | 583 | |
ed880dd4 | 584 | SetPen (m_pen); |
afce4c03 | 585 | |
d7657f75 RR |
586 | fprintf( m_pstream, |
587 | "newpath\n" | |
588 | "%ld %ld moveto\n" | |
589 | "%ld %ld lineto\n" | |
590 | "stroke\n", | |
591 | XLOG2DEV(x), YLOG2DEV(y), | |
592 | XLOG2DEV(x+1), YLOG2DEV(y) ); | |
afce4c03 | 593 | |
ed880dd4 RR |
594 | CalcBoundingBox( x, y ); |
595 | } | |
596 | ||
64698f9a | 597 | void wxPostScriptDC::DoDrawPolygon (int n, wxPoint points[], long xoffset, long yoffset, int WXUNUSED(fillStyle)) |
ed880dd4 | 598 | { |
223d09f6 | 599 | wxCHECK_RET( m_ok && m_pstream, wxT("invalid postscript dc") ); |
afce4c03 | 600 | |
ed880dd4 | 601 | if (n <= 0) return; |
afce4c03 | 602 | |
ed880dd4 RR |
603 | if (m_brush.GetStyle () != wxTRANSPARENT) |
604 | { | |
d7657f75 RR |
605 | SetBrush( m_brush ); |
606 | ||
607 | fprintf( m_pstream, "newpath\n" ); | |
afce4c03 | 608 | |
d7657f75 RR |
609 | long xx = XLOG2DEV(points[0].x + xoffset); |
610 | long yy = YLOG2DEV(points[0].y + yoffset); | |
611 | ||
612 | fprintf( m_pstream, "%ld %ld moveto\n", xx, yy ); | |
613 | ||
614 | CalcBoundingBox( points[0].x + xoffset, points[0].y + yoffset ); | |
ed880dd4 | 615 | |
d7657f75 RR |
616 | for (int i = 1; i < n; i++) |
617 | { | |
618 | xx = XLOG2DEV(points[i].x + xoffset); | |
619 | yy = YLOG2DEV(points[i].y + yoffset); | |
620 | ||
621 | fprintf( m_pstream, "%ld %ld lineto\n", xx, yy ); | |
ed880dd4 | 622 | |
d7657f75 RR |
623 | CalcBoundingBox( points[i].x + xoffset, points[i].y + yoffset); |
624 | } | |
625 | ||
626 | fprintf( m_pstream, "fill\n" ); | |
ed880dd4 RR |
627 | } |
628 | ||
629 | if (m_pen.GetStyle () != wxTRANSPARENT) | |
630 | { | |
d7657f75 RR |
631 | SetPen( m_pen ); |
632 | ||
633 | fprintf( m_pstream, "newpath\n" ); | |
ed880dd4 | 634 | |
d7657f75 RR |
635 | long xx = XLOG2DEV(points[0].x + xoffset); |
636 | long yy = YLOG2DEV(points[0].y + yoffset); | |
637 | ||
638 | fprintf( m_pstream, "%ld %ld moveto\n", xx, yy ); | |
639 | ||
640 | CalcBoundingBox( points[0].x + xoffset, points[0].y + yoffset ); | |
ed880dd4 | 641 | |
d7657f75 RR |
642 | for (int i = 1; i < n; i++) |
643 | { | |
644 | xx = XLOG2DEV(points[i].x + xoffset); | |
645 | yy = YLOG2DEV(points[i].y + yoffset); | |
646 | ||
647 | fprintf( m_pstream, "%ld %ld lineto\n", xx, yy ); | |
648 | ||
649 | CalcBoundingBox( points[i].x + xoffset, points[i].y + yoffset); | |
650 | } | |
ed880dd4 | 651 | |
d7657f75 | 652 | fprintf( m_pstream, "fill\n" ); |
ed880dd4 RR |
653 | } |
654 | } | |
655 | ||
64698f9a | 656 | void wxPostScriptDC::DoDrawLines (int n, wxPoint points[], long xoffset, long yoffset) |
ed880dd4 | 657 | { |
223d09f6 | 658 | wxCHECK_RET( m_ok && m_pstream, wxT("invalid postscript dc") ); |
afce4c03 | 659 | |
d7657f75 RR |
660 | if (m_pen.GetStyle() == wxTRANSPARENT) return; |
661 | ||
662 | if (n <= 0) return; | |
afce4c03 | 663 | |
ed880dd4 | 664 | SetPen (m_pen); |
afce4c03 | 665 | |
9a6be59a VZ |
666 | int i; |
667 | for ( i =0; i<n ; i++ ) | |
ed880dd4 | 668 | { |
9a6be59a | 669 | CalcBoundingBox( XLOG2DEV(points[i].x+xoffset), YLOG2DEV(points[i].y+yoffset)); |
ed880dd4 RR |
670 | } |
671 | ||
d7657f75 RR |
672 | fprintf( m_pstream, |
673 | "newpath\n" | |
674 | "%ld %ld moveto\n", | |
675 | XLOG2DEV(points[0].x+xoffset), YLOG2DEV(points[0].y+yoffset) ); | |
afce4c03 VZ |
676 | |
677 | for (i = 1; i < n; i++) | |
ed880dd4 | 678 | { |
d7657f75 RR |
679 | fprintf( m_pstream, |
680 | "%ld %ld lineto\n", | |
681 | XLOG2DEV(points[i].x+xoffset), YLOG2DEV(points[i].y+yoffset) ); | |
ed880dd4 | 682 | } |
afce4c03 | 683 | |
d7657f75 | 684 | fprintf( m_pstream, "stroke\n" ); |
ed880dd4 RR |
685 | } |
686 | ||
64698f9a | 687 | void wxPostScriptDC::DoDrawRectangle (long x, long y, long width, long height) |
ed880dd4 | 688 | { |
223d09f6 | 689 | wxCHECK_RET( m_ok && m_pstream, wxT("invalid postscript dc") ); |
afce4c03 | 690 | |
ed880dd4 RR |
691 | if (m_brush.GetStyle () != wxTRANSPARENT) |
692 | { | |
693 | SetBrush( m_brush ); | |
d7657f75 RR |
694 | |
695 | fprintf( m_pstream, | |
696 | "newpath\n" | |
697 | "%ld %ld moveto\n" | |
698 | "%ld %ld lineto\n" | |
699 | "%ld %ld lineto\n" | |
700 | "%ld %ld lineto\n" | |
701 | "closepath\n" | |
702 | "fill\n", | |
703 | XLOG2DEV(x), YLOG2DEV(y), | |
704 | XLOG2DEV(x + width), YLOG2DEV(y), | |
705 | XLOG2DEV(x + width), YLOG2DEV(y + height), | |
706 | XLOG2DEV(x), YLOG2DEV(y + height) ); | |
ed880dd4 RR |
707 | |
708 | CalcBoundingBox( x, y ); | |
709 | CalcBoundingBox( x + width, y + height ); | |
710 | } | |
afce4c03 | 711 | |
ed880dd4 RR |
712 | if (m_pen.GetStyle () != wxTRANSPARENT) |
713 | { | |
714 | SetPen (m_pen); | |
715 | ||
d7657f75 RR |
716 | fprintf( m_pstream, |
717 | "newpath\n" | |
718 | "%ld %ld moveto\n" | |
719 | "%ld %ld lineto\n" | |
720 | "%ld %ld lineto\n" | |
721 | "%ld %ld lineto\n" | |
722 | "closepath\n" | |
723 | "stroke\n", | |
724 | XLOG2DEV(x), YLOG2DEV(y), | |
725 | XLOG2DEV(x + width), YLOG2DEV(y), | |
726 | XLOG2DEV(x + width), YLOG2DEV(y + height), | |
727 | XLOG2DEV(x), YLOG2DEV(y + height) ); | |
728 | ||
ed880dd4 RR |
729 | CalcBoundingBox( x, y ); |
730 | CalcBoundingBox( x + width, y + height ); | |
731 | } | |
732 | } | |
733 | ||
64698f9a | 734 | void wxPostScriptDC::DoDrawRoundedRectangle (long x, long y, long width, long height, double radius) |
ed880dd4 | 735 | { |
223d09f6 | 736 | wxCHECK_RET( m_ok && m_pstream, wxT("invalid postscript dc") ); |
afce4c03 | 737 | |
ed880dd4 RR |
738 | if (radius < 0.0) |
739 | { | |
740 | // Now, a negative radius is interpreted to mean | |
741 | // 'the proportion of the smallest X or Y dimension' | |
742 | double smallest = 0.0; | |
743 | if (width < height) | |
9a6be59a | 744 | smallest = width; |
ed880dd4 | 745 | else |
9a6be59a | 746 | smallest = height; |
ed880dd4 RR |
747 | radius = (-radius * smallest); |
748 | } | |
afce4c03 | 749 | |
ed880dd4 RR |
750 | long rad = (long) radius; |
751 | ||
752 | if (m_brush.GetStyle () != wxTRANSPARENT) | |
753 | { | |
754 | SetBrush( m_brush ); | |
afce4c03 | 755 | |
d7657f75 RR |
756 | /* Draw rectangle anticlockwise */ |
757 | fprintf( m_pstream, | |
758 | "newpath\n" | |
759 | "%ld %ld %ld 90 180 arc\n" | |
760 | "%ld %ld moveto\n" | |
761 | "%ld %ld %ld 180 270 arc\n" | |
762 | "%ld %ld lineto\n" | |
763 | "%ld %ld %ld 270 0 arc\n" | |
764 | "%ld %ld lineto\n" | |
765 | "%ld %ld %ld 0 90 arc\n" | |
766 | "%ld %ld lineto\n" | |
767 | "closepath\n" | |
768 | "fill\n", | |
769 | XLOG2DEV(x + rad), YLOG2DEV(y + rad), XLOG2DEVREL(rad), | |
770 | XLOG2DEV(x), YLOG2DEV(y + rad), | |
771 | XLOG2DEV(x + rad), YLOG2DEV(y + height - rad), XLOG2DEVREL(rad), | |
772 | XLOG2DEV(x + width - rad), YLOG2DEV(y + height), | |
773 | XLOG2DEV(x + width - rad), YLOG2DEV(y + height - rad), XLOG2DEVREL(rad), | |
774 | XLOG2DEV(x + width), YLOG2DEV(y + rad), | |
775 | XLOG2DEV(x + width - rad), YLOG2DEV(y + rad), XLOG2DEVREL(rad), | |
776 | XLOG2DEV(x + rad), YLOG2DEV(y) ); | |
ed880dd4 RR |
777 | |
778 | CalcBoundingBox( x, y ); | |
779 | CalcBoundingBox( x + width, y + height ); | |
780 | } | |
afce4c03 | 781 | |
ed880dd4 RR |
782 | if (m_pen.GetStyle () != wxTRANSPARENT) |
783 | { | |
784 | SetPen (m_pen); | |
afce4c03 | 785 | |
d7657f75 RR |
786 | /* Draw rectangle anticlockwise */ |
787 | fprintf( m_pstream, | |
788 | "newpath\n" | |
789 | "%ld %ld %ld 90 180 arc\n" | |
790 | "%ld %ld moveto\n" | |
791 | "%ld %ld %ld 180 270 arc\n" | |
792 | "%ld %ld lineto\n" | |
793 | "%ld %ld %ld 270 0 arc\n" | |
794 | "%ld %ld lineto\n" | |
795 | "%ld %ld %ld 0 90 arc\n" | |
796 | "%ld %ld lineto\n" | |
797 | "closepath\n" | |
798 | "stroke\n", | |
799 | XLOG2DEV(x + rad), YLOG2DEV(y + rad), XLOG2DEVREL(rad), | |
800 | XLOG2DEV(x), YLOG2DEV(y + rad), | |
801 | XLOG2DEV(x + rad), YLOG2DEV(y + height - rad), XLOG2DEVREL(rad), | |
802 | XLOG2DEV(x + width - rad), YLOG2DEV(y + height), | |
803 | XLOG2DEV(x + width - rad), YLOG2DEV(y + height - rad), XLOG2DEVREL(rad), | |
804 | XLOG2DEV(x + width), YLOG2DEV(y + rad), | |
805 | XLOG2DEV(x + width - rad), YLOG2DEV(y + rad), XLOG2DEVREL(rad), | |
806 | XLOG2DEV(x + rad), YLOG2DEV(y) ); | |
ed880dd4 RR |
807 | |
808 | CalcBoundingBox( x, y ); | |
809 | CalcBoundingBox( x + width, y + height ); | |
810 | } | |
811 | } | |
812 | ||
64698f9a | 813 | void wxPostScriptDC::DoDrawEllipse (long x, long y, long width, long height) |
ed880dd4 | 814 | { |
223d09f6 | 815 | wxCHECK_RET( m_ok && m_pstream, wxT("invalid postscript dc") ); |
afce4c03 | 816 | |
ed880dd4 RR |
817 | if (m_brush.GetStyle () != wxTRANSPARENT) |
818 | { | |
819 | SetBrush (m_brush); | |
820 | ||
d7657f75 RR |
821 | fprintf( m_pstream, |
822 | "newpath\n" | |
823 | "%ld %ld %ld %ld 0 360 ellipse\n" | |
824 | "fill\n", | |
825 | XLOG2DEV(x + width / 2), YLOG2DEV(y + height / 2), | |
826 | XLOG2DEV(width / 2), YLOG2DEVREL(height / 2) ); | |
ed880dd4 RR |
827 | |
828 | CalcBoundingBox( x - width, y - height ); | |
829 | CalcBoundingBox( x + width, y + height ); | |
830 | } | |
afce4c03 | 831 | |
d7657f75 | 832 | if (m_pen.GetStyle () != wxTRANSPARENT) |
ed880dd4 RR |
833 | { |
834 | SetPen (m_pen); | |
835 | ||
d7657f75 RR |
836 | fprintf( m_pstream, |
837 | "newpath\n" | |
838 | "%ld %ld %ld %ld 0 360 ellipse\n" | |
839 | "stroke\n", | |
840 | XLOG2DEV(x + width / 2), YLOG2DEV(y + height / 2), | |
841 | XLOG2DEV(width / 2), YLOG2DEVREL(height / 2) ); | |
ed880dd4 RR |
842 | |
843 | CalcBoundingBox( x - width, y - height ); | |
844 | CalcBoundingBox( x + width, y + height ); | |
845 | } | |
846 | } | |
847 | ||
64698f9a | 848 | void wxPostScriptDC::DoDrawIcon( const wxIcon& icon, long x, long y ) |
ed880dd4 | 849 | { |
ef539066 | 850 | DrawBitmap( icon, x, y, TRUE ); |
ed880dd4 RR |
851 | } |
852 | ||
d7657f75 RR |
853 | /* this has to be char, not wxChar */ |
854 | static char hexArray[] = "0123456789ABCDEF"; | |
855 | static void LocalDecToHex( int dec, char *buf ) | |
856 | { | |
857 | int firstDigit = (int)(dec/16.0); | |
858 | int secondDigit = (int)(dec - (firstDigit*16.0)); | |
859 | buf[0] = hexArray[firstDigit]; | |
860 | buf[1] = hexArray[secondDigit]; | |
861 | buf[2] = 0; | |
862 | } | |
863 | ||
64698f9a | 864 | void wxPostScriptDC::DoDrawBitmap( const wxBitmap& bitmap, long x, long y, bool WXUNUSED(useMask) ) |
ed880dd4 | 865 | { |
223d09f6 | 866 | wxCHECK_RET( m_ok && m_pstream, wxT("invalid postscript dc") ); |
ef539066 RR |
867 | |
868 | if (!bitmap.Ok()) return; | |
afce4c03 | 869 | |
ef539066 | 870 | wxImage image( bitmap ); |
afce4c03 | 871 | |
ef539066 | 872 | if (!image.Ok()) return; |
afce4c03 | 873 | |
d7657f75 RR |
874 | long ww = XLOG2DEVREL(image.GetWidth()); |
875 | long hh = YLOG2DEVREL(image.GetHeight()); | |
afce4c03 | 876 | |
ef539066 | 877 | image = image.Scale( ww, hh ); |
afce4c03 | 878 | |
ef539066 | 879 | if (!image.Ok()) return; |
afce4c03 | 880 | |
d7657f75 RR |
881 | long xx = XLOG2DEV(x); |
882 | long yy = YLOG2DEV(y + bitmap.GetHeight()); | |
883 | ||
884 | fprintf( m_pstream, | |
885 | "/origstate save def\n" | |
886 | "20 dict begin\n" | |
887 | "/pix %ld string def\n" | |
888 | "/grays %ld string def\n" | |
889 | "/npixels 0 def\n" | |
890 | "/rgbindx 0 def\n" | |
891 | "%ld %ld translate\n" | |
892 | "%ld %ld scale\n" | |
893 | "%ld %ld 8\n" | |
894 | "[%ld 0 0 %ld 0 %ld]\n" | |
895 | "{currentfile pix readhexstring pop}\n" | |
896 | "false 3 colorimage\n", | |
897 | ww, ww, xx, yy, ww, hh, ww, hh, ww, -hh, hh ); | |
afce4c03 | 898 | |
ef539066 RR |
899 | for (int j = 0; j < hh; j++) |
900 | { | |
901 | for (int i = 0; i < ww; i++) | |
902 | { | |
d7657f75 RR |
903 | char buffer[5]; |
904 | LocalDecToHex( image.GetRed(i,j), buffer ); | |
905 | fprintf( m_pstream, buffer ); | |
906 | LocalDecToHex( image.GetGreen(i,j), buffer ); | |
907 | fprintf( m_pstream, buffer ); | |
908 | LocalDecToHex( image.GetBlue(i,j), buffer ); | |
909 | fprintf( m_pstream, buffer ); | |
910 | } | |
911 | fprintf( m_pstream, "\n" ); | |
ef539066 RR |
912 | } |
913 | ||
d7657f75 RR |
914 | fprintf( m_pstream, "end\n" ); |
915 | fprintf( m_pstream, "origstate restore\n" ); | |
ed880dd4 RR |
916 | } |
917 | ||
36b3b54a | 918 | void wxPostScriptDC::SetFont( const wxFont& font ) |
ed880dd4 | 919 | { |
223d09f6 | 920 | wxCHECK_RET( m_ok && m_pstream, wxT("invalid postscript dc") ); |
afce4c03 | 921 | |
ed880dd4 | 922 | if (!font.Ok()) return; |
afce4c03 | 923 | |
ed880dd4 | 924 | m_font = font; |
57c208c5 | 925 | |
36b3b54a RR |
926 | const char *name; |
927 | const char *style = ""; | |
928 | int Style = m_font.GetStyle(); | |
929 | int Weight = m_font.GetWeight(); | |
930 | ||
d7657f75 | 931 | switch (m_font.GetFamily()) |
36b3b54a RR |
932 | { |
933 | case wxTELETYPE: | |
934 | case wxMODERN: | |
935 | name = "/Courier"; | |
936 | break; | |
937 | case wxSWISS: | |
938 | name = "/Helvetica"; | |
939 | break; | |
940 | case wxROMAN: | |
941 | // name = "/Times-Roman"; | |
942 | name = "/Times"; // Altered by EDZ | |
943 | break; | |
944 | case wxSCRIPT: | |
945 | name = "/Zapf-Chancery-MediumItalic"; | |
946 | Style = wxNORMAL; | |
947 | Weight = wxNORMAL; | |
948 | break; | |
949 | default: | |
950 | case wxDEFAULT: // Sans Serif Font | |
951 | name = "/LucidaSans"; | |
952 | } | |
953 | ||
954 | if (Style == wxNORMAL && (Weight == wxNORMAL || Weight == wxLIGHT)) | |
57c208c5 | 955 | { |
d7657f75 RR |
956 | if (m_font.GetFamily () == wxROMAN) |
957 | style = "-Roman"; | |
958 | else | |
959 | style = ""; | |
57c208c5 | 960 | } |
36b3b54a RR |
961 | else if (Style == wxNORMAL && Weight == wxBOLD) |
962 | { | |
d7657f75 | 963 | style = "-Bold"; |
36b3b54a RR |
964 | } |
965 | else if (Style == wxITALIC && (Weight == wxNORMAL || Weight == wxLIGHT)) | |
57c208c5 | 966 | { |
d7657f75 RR |
967 | if (m_font.GetFamily () == wxROMAN) |
968 | style = "-Italic"; | |
969 | else | |
970 | style = "-Oblique"; | |
57c208c5 | 971 | } |
36b3b54a | 972 | else if (Style == wxITALIC && Weight == wxBOLD) |
57c208c5 | 973 | { |
d7657f75 RR |
974 | if (m_font.GetFamily () == wxROMAN) |
975 | style = "-BoldItalic"; | |
976 | else | |
977 | style = "-BoldOblique"; | |
57c208c5 | 978 | } |
36b3b54a | 979 | else if (Style == wxSLANT && (Weight == wxNORMAL || Weight == wxLIGHT)) |
57c208c5 | 980 | { |
d7657f75 RR |
981 | if (m_font.GetFamily () == wxROMAN) |
982 | style = "-Italic"; | |
983 | else | |
984 | style = "-Oblique"; | |
57c208c5 | 985 | } |
36b3b54a | 986 | else if (Style == wxSLANT && Weight == wxBOLD) |
57c208c5 | 987 | { |
d7657f75 RR |
988 | if (m_font.GetFamily () == wxROMAN) |
989 | style = "-BoldItalic"; | |
990 | else | |
991 | style = "-BoldOblique"; | |
57c208c5 | 992 | } |
36b3b54a RR |
993 | else |
994 | { | |
d7657f75 | 995 | style = ""; |
36b3b54a | 996 | } |
57c208c5 | 997 | |
d7657f75 RR |
998 | char buffer[100]; |
999 | strcpy( buffer, name ); | |
1000 | strcat( buffer, style ); | |
afce4c03 | 1001 | |
d7657f75 RR |
1002 | fprintf( m_pstream, buffer ); |
1003 | fprintf( m_pstream, " reencodeISO def\n" ); | |
1004 | fprintf( m_pstream, buffer ); | |
1005 | fprintf( m_pstream, " findfont\n" ); | |
e6f1ad22 HH |
1006 | #ifdef __WXMSW__ |
1007 | fprintf( m_pstream, "%d scalefont setfont\n", YLOG2DEVREL(m_font.GetPointSize()) ); | |
1008 | #else | |
d7657f75 | 1009 | fprintf( m_pstream, "%ld scalefont setfont\n", YLOG2DEVREL(m_font.GetPointSize()) ); |
e6f1ad22 | 1010 | #endif |
ed880dd4 RR |
1011 | } |
1012 | ||
1013 | void wxPostScriptDC::SetPen( const wxPen& pen ) | |
1014 | { | |
223d09f6 | 1015 | wxCHECK_RET( m_ok && m_pstream, wxT("invalid postscript dc") ); |
afce4c03 | 1016 | |
ed880dd4 | 1017 | if (!pen.Ok()) return; |
afce4c03 | 1018 | |
ed880dd4 RR |
1019 | int oldStyle = m_pen.GetStyle(); |
1020 | ||
1021 | m_pen = pen; | |
1022 | ||
e6f1ad22 HH |
1023 | #ifdef __WXMSW__ |
1024 | fprintf( m_pstream, "%d setlinewidth\n", XLOG2DEVREL(m_pen.GetWidth()) ); | |
1025 | #else | |
d7657f75 | 1026 | fprintf( m_pstream, "%ld setlinewidth\n", XLOG2DEVREL(m_pen.GetWidth()) ); |
e6f1ad22 | 1027 | #endif |
ed880dd4 RR |
1028 | /* |
1029 | Line style - WRONG: 2nd arg is OFFSET | |
afce4c03 | 1030 | |
ed880dd4 RR |
1031 | Here, I'm afraid you do not conceive meaning of parameters of 'setdash' |
1032 | operator correctly. You should look-up this in the Red Book: the 2nd parame- | |
1033 | ter is not number of values in the array of the first one, but an offset | |
1034 | into this description of the pattern. I mean a real *offset* not index | |
1035 | into array. I.e. If the command is [3 4] 1 setdash is used, then there | |
1036 | will be first black line *2* units long, then space 4 units, then the | |
1037 | pattern of *3* units black, 4 units space will be repeated. | |
1038 | */ | |
1039 | ||
1040 | static const char *dotted = "[2 5] 2"; | |
1041 | static const char *short_dashed = "[4 4] 2"; | |
1042 | static const char *long_dashed = "[4 8] 2"; | |
1043 | static const char *dotted_dashed = "[6 6 2 6] 4"; | |
1044 | ||
1045 | const char *psdash = (char *) NULL; | |
d7657f75 | 1046 | switch (m_pen.GetStyle()) |
ed880dd4 RR |
1047 | { |
1048 | case wxDOT: psdash = dotted; break; | |
1049 | case wxSHORT_DASH: psdash = short_dashed; break; | |
1050 | case wxLONG_DASH: psdash = long_dashed; break; | |
1051 | case wxDOT_DASH: psdash = dotted_dashed; break; | |
1052 | case wxSOLID: | |
1053 | case wxTRANSPARENT: | |
1054 | default: psdash = "[] 0"; break; | |
1055 | } | |
afce4c03 | 1056 | |
ed880dd4 RR |
1057 | if (oldStyle != m_pen.GetStyle()) |
1058 | { | |
d7657f75 RR |
1059 | fprintf( m_pstream, psdash ); |
1060 | fprintf( m_pstream," setdash\n" ); | |
ed880dd4 RR |
1061 | } |
1062 | ||
1063 | // Line colour | |
1064 | unsigned char red = m_pen.GetColour().Red(); | |
1065 | unsigned char blue = m_pen.GetColour().Blue(); | |
1066 | unsigned char green = m_pen.GetColour().Green(); | |
1067 | ||
1068 | if (!m_colour) | |
1069 | { | |
1070 | // Anything not white is black | |
d7657f75 RR |
1071 | if (! (red == (unsigned char) 255 && |
1072 | blue == (unsigned char) 255 && | |
1073 | green == (unsigned char) 255) ) | |
1074 | { | |
1075 | red = (unsigned char) 0; | |
1076 | green = (unsigned char) 0; | |
1077 | blue = (unsigned char) 0; | |
1078 | } | |
1079 | // setgray here ? | |
ed880dd4 RR |
1080 | } |
1081 | ||
1082 | if (!(red == m_currentRed && green == m_currentGreen && blue == m_currentBlue)) | |
1083 | { | |
d7657f75 RR |
1084 | double redPS = (double)(red) / 255.0; |
1085 | double bluePS = (double)(blue) / 255.0; | |
1086 | double greenPS = (double)(green) / 255.0; | |
1087 | ||
1088 | fprintf( m_pstream, | |
1089 | "%.8f %.8f %.8f setrgbcolor\n", | |
1090 | redPS, greenPS, bluePS ); | |
1091 | ||
1092 | m_currentRed = red; | |
1093 | m_currentBlue = blue; | |
1094 | m_currentGreen = green; | |
ed880dd4 RR |
1095 | } |
1096 | } | |
1097 | ||
1098 | void wxPostScriptDC::SetBrush( const wxBrush& brush ) | |
1099 | { | |
223d09f6 | 1100 | wxCHECK_RET( m_ok && m_pstream, wxT("invalid postscript dc") ); |
afce4c03 | 1101 | |
ed880dd4 | 1102 | if (!brush.Ok()) return; |
afce4c03 | 1103 | |
ed880dd4 RR |
1104 | m_brush = brush; |
1105 | ||
1106 | // Brush colour | |
3c1b65a4 RR |
1107 | unsigned char red = m_brush.GetColour().Red(); |
1108 | unsigned char blue = m_brush.GetColour().Blue(); | |
1109 | unsigned char green = m_brush.GetColour().Green(); | |
ed880dd4 RR |
1110 | |
1111 | if (!m_colour) | |
1112 | { | |
d7657f75 RR |
1113 | // Anything not white is black |
1114 | if (! (red == (unsigned char) 255 && | |
1115 | blue == (unsigned char) 255 && | |
1116 | green == (unsigned char) 255) ) | |
1117 | { | |
1118 | red = (unsigned char) 0; | |
1119 | green = (unsigned char) 0; | |
1120 | blue = (unsigned char) 0; | |
1121 | } | |
1122 | // setgray here ? | |
ed880dd4 | 1123 | } |
d7657f75 | 1124 | |
ed880dd4 RR |
1125 | if (!(red == m_currentRed && green == m_currentGreen && blue == m_currentBlue)) |
1126 | { | |
d7657f75 RR |
1127 | double redPS = (double)(red) / 255.0; |
1128 | double bluePS = (double)(blue) / 255.0; | |
1129 | double greenPS = (double)(green) / 255.0; | |
1130 | ||
1131 | fprintf( m_pstream, | |
1132 | "%.8f %.8f %.8f setrgbcolor\n", | |
1133 | redPS, greenPS, bluePS ); | |
1134 | ||
1135 | m_currentRed = red; | |
1136 | m_currentBlue = blue; | |
1137 | m_currentGreen = green; | |
ed880dd4 RR |
1138 | } |
1139 | } | |
1140 | ||
64698f9a | 1141 | void wxPostScriptDC::DoDrawText( const wxString& text, long x, long y ) |
ed880dd4 | 1142 | { |
223d09f6 | 1143 | wxCHECK_RET( m_ok && m_pstream, wxT("invalid postscript dc") ); |
afce4c03 | 1144 | |
ed880dd4 RR |
1145 | SetFont( m_font ); |
1146 | ||
d7657f75 | 1147 | if (m_textForegroundColour.Ok()) |
ed880dd4 | 1148 | { |
d7657f75 RR |
1149 | unsigned char red = m_textForegroundColour.Red(); |
1150 | unsigned char blue = m_textForegroundColour.Blue(); | |
1151 | unsigned char green = m_textForegroundColour.Green(); | |
ed880dd4 RR |
1152 | |
1153 | if (!m_colour) | |
9a6be59a | 1154 | { |
d7657f75 RR |
1155 | // Anything not white is black |
1156 | if (! (red == (unsigned char) 255 && | |
1157 | blue == (unsigned char) 255 && | |
1158 | green == (unsigned char) 255)) | |
1159 | { | |
1160 | red = (unsigned char) 0; | |
1161 | green = (unsigned char) 0; | |
1162 | blue = (unsigned char) 0; | |
1163 | } | |
9a6be59a | 1164 | } |
afce4c03 | 1165 | |
d7657f75 | 1166 | // maybe setgray here ? |
ed880dd4 RR |
1167 | if (!(red == m_currentRed && green == m_currentGreen && blue == m_currentBlue)) |
1168 | { | |
d7657f75 RR |
1169 | double redPS = (double)(red) / 255.0; |
1170 | double bluePS = (double)(blue) / 255.0; | |
1171 | double greenPS = (double)(green) / 255.0; | |
1172 | ||
1173 | fprintf( m_pstream, | |
1174 | "%.8f %.8f %.8f setrgbcolor\n", | |
1175 | redPS, greenPS, bluePS ); | |
1176 | ||
ed880dd4 RR |
1177 | m_currentRed = red; |
1178 | m_currentBlue = blue; | |
1179 | m_currentGreen = green; | |
1180 | } | |
1181 | } | |
1182 | ||
1183 | int size = m_font.GetPointSize(); | |
1184 | ||
d7657f75 RR |
1185 | long by = y + (long)floor( double(size) * 2.0 / 3.0 ); // approximate baseline |
1186 | fprintf( m_pstream, "%ld %ld moveto\n", XLOG2DEV(x), YLOG2DEV(by) ); | |
afce4c03 | 1187 | |
d7657f75 RR |
1188 | /* I don't know how to write char to a stream, so I use a mini string */ |
1189 | char tmpbuf[2]; | |
1190 | tmpbuf[1] = 0; | |
1191 | ||
1192 | fprintf( m_pstream, "(" ); | |
9626e0bf | 1193 | const wxWX2MBbuf textbuf = text.mb_str(); |
d7657f75 | 1194 | int len = strlen(textbuf); |
ed880dd4 RR |
1195 | int i; |
1196 | for (i = 0; i < len; i++) | |
1197 | { | |
87138c52 | 1198 | int c = (unsigned char) textbuf[i]; |
d7657f75 | 1199 | if (c == ')' || c == '(' || c == '\\') |
ed880dd4 | 1200 | { |
d7657f75 RR |
1201 | /* Cope with special characters */ |
1202 | fprintf( m_pstream, "\\" ); | |
1203 | tmpbuf[0] = (char) c; | |
1204 | fprintf( m_pstream, tmpbuf ); | |
ed880dd4 RR |
1205 | } |
1206 | else if ( c >= 128 ) | |
1207 | { | |
d7657f75 RR |
1208 | /* Cope with character codes > 127 */ |
1209 | fprintf(m_pstream, "\\%o", c); | |
ed880dd4 RR |
1210 | } |
1211 | else | |
d7657f75 RR |
1212 | { |
1213 | tmpbuf[0] = (char) c; | |
1214 | fprintf( m_pstream, tmpbuf ); | |
1215 | } | |
ed880dd4 | 1216 | } |
d7657f75 RR |
1217 | |
1218 | fprintf( m_pstream, ") show\n" ); | |
ed880dd4 RR |
1219 | |
1220 | if (m_font.GetUnderlined()) | |
1221 | { | |
1222 | long uy = (long)(y + size - m_underlinePosition); | |
1223 | long w, h; | |
1224 | GetTextExtent(text, &w, &h); | |
afce4c03 | 1225 | |
d7657f75 RR |
1226 | fprintf( m_pstream, |
1227 | "gsave\n" | |
1228 | "%ld %ld moveto\n" | |
1229 | "%ld setlinewidth\n" | |
1230 | "%ld %ld lineto\n" | |
1231 | "stroke\n" | |
1232 | "grestore\n", | |
1233 | XLOG2DEV(x), YLOG2DEV(uy), | |
1234 | (long)m_underlineThickness, | |
1235 | XLOG2DEV(x + w), YLOG2DEV(uy) ); | |
ed880dd4 | 1236 | } |
afce4c03 | 1237 | |
ed880dd4 RR |
1238 | CalcBoundingBox( x, y ); |
1239 | CalcBoundingBox( x + size * text.Length() * 2/3 , y ); | |
1240 | } | |
1241 | ||
1242 | ||
1243 | void wxPostScriptDC::SetBackground (const wxBrush& brush) | |
1244 | { | |
1245 | m_backgroundBrush = brush; | |
1246 | } | |
1247 | ||
1248 | void wxPostScriptDC::SetLogicalFunction (int WXUNUSED(function)) | |
1249 | { | |
223d09f6 | 1250 | wxFAIL_MSG( wxT("wxPostScriptDC::SetLogicalFunction not implemented.") ); |
ed880dd4 RR |
1251 | } |
1252 | ||
64698f9a | 1253 | void wxPostScriptDC::DoDrawSpline( wxList *points ) |
ed880dd4 | 1254 | { |
223d09f6 | 1255 | wxCHECK_RET( m_ok && m_pstream, wxT("invalid postscript dc") ); |
afce4c03 | 1256 | |
ed880dd4 RR |
1257 | SetPen( m_pen ); |
1258 | ||
1259 | double a, b, c, d, x1, y1, x2, y2, x3, y3; | |
1260 | wxPoint *p, *q; | |
1261 | ||
1262 | wxNode *node = points->First(); | |
1263 | p = (wxPoint *)node->Data(); | |
afce4c03 | 1264 | x1 = p->x; |
ed880dd4 RR |
1265 | y1 = p->y; |
1266 | ||
1267 | node = node->Next(); | |
1268 | p = (wxPoint *)node->Data(); | |
afce4c03 | 1269 | c = p->x; |
ed880dd4 RR |
1270 | d = p->y; |
1271 | x3 = a = (double)(x1 + c) / 2; | |
1272 | y3 = b = (double)(y1 + d) / 2; | |
1273 | ||
d7657f75 RR |
1274 | fprintf( m_pstream, |
1275 | "newpath\n" | |
1276 | "%ld %ld moveto\n" | |
1277 | "%ld %ld lineto\n", | |
1278 | XLOG2DEV((long)x1), YLOG2DEV((long)y1), | |
1279 | XLOG2DEV((long)x3), YLOG2DEV((long)y3) ); | |
afce4c03 | 1280 | |
ed880dd4 RR |
1281 | CalcBoundingBox( (long)x1, (long)y1 ); |
1282 | CalcBoundingBox( (long)x3, (long)y3 ); | |
1283 | ||
1284 | while ((node = node->Next()) != NULL) | |
1285 | { | |
1286 | q = (wxPoint *)node->Data(); | |
1287 | ||
d7657f75 RR |
1288 | x1 = x3; |
1289 | y1 = y3; | |
1290 | x2 = c; | |
1291 | y2 = d; | |
1292 | c = q->x; | |
1293 | d = q->y; | |
ed880dd4 RR |
1294 | x3 = (double)(x2 + c) / 2; |
1295 | y3 = (double)(y2 + d) / 2; | |
d7657f75 RR |
1296 | |
1297 | fprintf( m_pstream, | |
1298 | "%ld %ld %ld %ld %ld %ld DrawSplineSection\n", | |
1299 | XLOG2DEV((long)x1), YLOG2DEV((long)y1), | |
1300 | XLOG2DEV((long)x2), YLOG2DEV((long)y2), | |
1301 | XLOG2DEV((long)x3), YLOG2DEV((long)y3) ); | |
ed880dd4 RR |
1302 | |
1303 | CalcBoundingBox( (long)x1, (long)y1 ); | |
1304 | CalcBoundingBox( (long)x3, (long)y3 ); | |
1305 | } | |
afce4c03 | 1306 | |
ed880dd4 RR |
1307 | /* |
1308 | At this point, (x2,y2) and (c,d) are the position of the | |
1309 | next-to-last and last point respectively, in the point list | |
1310 | */ | |
1311 | ||
d7657f75 RR |
1312 | fprintf( m_pstream, |
1313 | "%ld %ld lineto\n" | |
1314 | "stroke\n", | |
1315 | XLOG2DEV((long)c), YLOG2DEV((long)d) ); | |
ed880dd4 RR |
1316 | } |
1317 | ||
64698f9a | 1318 | long wxPostScriptDC::GetCharWidth() const |
ed880dd4 RR |
1319 | { |
1320 | // Chris Breeze: reasonable approximation using wxMODERN/Courier | |
1321 | return (long) (GetCharHeight() * 72.0 / 120.0); | |
1322 | } | |
1323 | ||
1324 | ||
1325 | void wxPostScriptDC::SetAxisOrientation( bool xLeftRight, bool yBottomUp ) | |
1326 | { | |
223d09f6 | 1327 | wxCHECK_RET( m_ok && m_pstream, wxT("invalid postscript dc") ); |
afce4c03 | 1328 | |
ed880dd4 RR |
1329 | m_signX = (xLeftRight ? 1 : -1); |
1330 | m_signY = (yBottomUp ? 1 : -1); | |
afce4c03 | 1331 | |
9a6be59a VZ |
1332 | // FIXME there is no such function in MSW |
1333 | #ifndef __WXMSW__ | |
ed880dd4 | 1334 | ComputeScaleAndOrigin(); |
9a6be59a | 1335 | #endif |
ed880dd4 RR |
1336 | } |
1337 | ||
1338 | void wxPostScriptDC::SetDeviceOrigin( long x, long y ) | |
1339 | { | |
223d09f6 | 1340 | wxCHECK_RET( m_ok && m_pstream, wxT("invalid postscript dc") ); |
afce4c03 | 1341 | |
4bc67cc5 RR |
1342 | int h = 0; |
1343 | int w = 0; | |
1344 | GetSize( &w, &h ); | |
afce4c03 | 1345 | |
4bc67cc5 | 1346 | wxDC::SetDeviceOrigin( x, h-y ); |
ed880dd4 RR |
1347 | } |
1348 | ||
4e4ea166 | 1349 | void wxPostScriptDC::DoGetSize(int* width, int* height) const |
ed880dd4 | 1350 | { |
7bcb11d3 | 1351 | wxPaperSize id = m_printData.GetPaperId(); |
ed880dd4 | 1352 | |
7bcb11d3 | 1353 | wxPrintPaperType *paper = wxThePrintPaperDatabase->FindPaperType(id); |
afce4c03 | 1354 | |
7bcb11d3 | 1355 | if (!paper) paper = wxThePrintPaperDatabase->FindPaperType(wxPAPER_A4); |
afce4c03 | 1356 | |
ac2def68 RR |
1357 | int w = 595; |
1358 | int h = 842; | |
ed880dd4 RR |
1359 | if (paper) |
1360 | { | |
ac2def68 RR |
1361 | w = paper->GetSizeDeviceUnits().x; |
1362 | h = paper->GetSizeDeviceUnits().y; | |
ed880dd4 | 1363 | } |
ac2def68 RR |
1364 | |
1365 | if (m_printData.GetOrientation() == wxLANDSCAPE) | |
ed880dd4 | 1366 | { |
ac2def68 RR |
1367 | int tmp = w; |
1368 | w = h; | |
1369 | h = tmp; | |
ed880dd4 | 1370 | } |
ac2def68 RR |
1371 | |
1372 | if (width) *width = w; | |
1373 | if (height) *height = h; | |
ed880dd4 RR |
1374 | } |
1375 | ||
4e4ea166 | 1376 | void wxPostScriptDC::DoGetSizeMM(int *width, int *height) const |
ed880dd4 | 1377 | { |
7bcb11d3 JS |
1378 | wxPaperSize id = m_printData.GetPaperId(); |
1379 | ||
1380 | wxPrintPaperType *paper = wxThePrintPaperDatabase->FindPaperType(id); | |
afce4c03 | 1381 | |
7bcb11d3 JS |
1382 | if (!paper) paper = wxThePrintPaperDatabase->FindPaperType(wxPAPER_A4); |
1383 | ||
ac2def68 RR |
1384 | int w = 210; |
1385 | int h = 297; | |
7bcb11d3 | 1386 | if (paper) |
ed880dd4 | 1387 | { |
ac2def68 RR |
1388 | w = paper->GetWidth() / 10; |
1389 | h = paper->GetHeight() / 10; | |
ed880dd4 | 1390 | } |
ac2def68 RR |
1391 | |
1392 | if (m_printData.GetOrientation() == wxLANDSCAPE) | |
ed880dd4 | 1393 | { |
ac2def68 RR |
1394 | int tmp = w; |
1395 | w = h; | |
1396 | h = tmp; | |
ed880dd4 | 1397 | } |
ac2def68 RR |
1398 | |
1399 | if (width) *width = w; | |
1400 | if (height) *height = h; | |
7bcb11d3 JS |
1401 | } |
1402 | ||
1403 | // Resolution in pixels per logical inch | |
1404 | wxSize wxPostScriptDC::GetPPI(void) const | |
1405 | { | |
1406 | return wxSize(72, 72); | |
1407 | } | |
ed880dd4 | 1408 | |
7bcb11d3 | 1409 | |
d7657f75 | 1410 | bool wxPostScriptDC::StartDoc( const wxString& message ) |
7bcb11d3 | 1411 | { |
223d09f6 | 1412 | wxCHECK_MSG( m_ok, FALSE, wxT("invalid postscript dc") ); |
7bcb11d3 JS |
1413 | |
1414 | if (m_printData.GetFilename() == "") | |
1415 | { | |
1416 | wxString filename = wxGetTempFileName("ps"); | |
1417 | m_printData.SetFilename(filename); | |
7bcb11d3 | 1418 | } |
d7657f75 RR |
1419 | |
1420 | m_pstream = fopen( m_printData.GetFilename().fn_str(), "w+" ); | |
7bcb11d3 | 1421 | |
d7657f75 | 1422 | if (!m_pstream) |
ed880dd4 | 1423 | { |
d7657f75 | 1424 | wxMessageBox( _("Cannot open file for PostScript printing!"), _("Error"), wxOK ); |
7bcb11d3 JS |
1425 | m_ok = FALSE; |
1426 | return FALSE; | |
ed880dd4 | 1427 | } |
afce4c03 | 1428 | |
ed880dd4 RR |
1429 | m_ok = TRUE; |
1430 | ||
d7657f75 RR |
1431 | fprintf( m_pstream, "%%%%BeginProlog\n" ); |
1432 | fprintf( m_pstream, wxPostScriptHeaderEllipse ); | |
1433 | fprintf( m_pstream, wxPostScriptHeaderEllipticArc ); | |
1434 | fprintf( m_pstream, wxPostScriptHeaderColourImage ); | |
1435 | fprintf( m_pstream, wxPostScriptHeaderReencodeISO1 ); | |
1436 | fprintf( m_pstream, wxPostScriptHeaderReencodeISO2 ); | |
1437 | if (wxPostScriptHeaderSpline) | |
1438 | fprintf( m_pstream, wxPostScriptHeaderSpline ); | |
1439 | fprintf( m_pstream, "%%%%EndProlog\n" ); | |
1440 | ||
ed880dd4 RR |
1441 | SetBrush( *wxBLACK_BRUSH ); |
1442 | SetPen( *wxBLACK_PEN ); | |
1443 | SetBackground( *wxWHITE_BRUSH ); | |
1444 | SetTextForeground( *wxBLACK ); | |
1445 | ||
1446 | // set origin according to paper size | |
1447 | SetDeviceOrigin( 0,0 ); | |
afce4c03 | 1448 | |
ed880dd4 RR |
1449 | wxPageNumber = 1; |
1450 | m_pageNumber = 1; | |
1451 | m_title = message; | |
1452 | return TRUE; | |
1453 | } | |
1454 | ||
1455 | void wxPostScriptDC::EndDoc () | |
1456 | { | |
223d09f6 | 1457 | wxCHECK_RET( m_ok && m_pstream, wxT("invalid postscript dc") ); |
afce4c03 | 1458 | |
ed880dd4 RR |
1459 | if (m_clipping) |
1460 | { | |
1461 | m_clipping = FALSE; | |
d7657f75 | 1462 | fprintf( m_pstream, "grestore\n" ); |
ed880dd4 RR |
1463 | } |
1464 | ||
d7657f75 RR |
1465 | fclose( m_pstream ); |
1466 | m_pstream = (FILE *) NULL; | |
ed880dd4 | 1467 | |
87138c52 | 1468 | wxChar *header_file = wxGetTempFileName("ps"); |
afce4c03 | 1469 | |
d7657f75 | 1470 | m_pstream = fopen( wxConvFile.cWX2MB(header_file) , "w+" ); |
ed880dd4 | 1471 | |
d7657f75 | 1472 | fprintf( m_pstream, "%%!PS-Adobe-2.0\n" ); // PostScript magic strings |
aece0491 | 1473 | fprintf( m_pstream, "%%%%Title: %s\n", (const char *)m_title.mb_str() ); |
ac2def68 | 1474 | fprintf( m_pstream, "%%%%Creator: %s\n", (const char*)wxConvCurrent->cWX2MB(wxTheApp->argv[0]) ); |
aece0491 | 1475 | fprintf( m_pstream, "%%%%CreationDate: %s\n", (const char *)wxNow().mb_str() ); |
ed880dd4 | 1476 | |
87138c52 | 1477 | wxChar userID[256]; |
ed880dd4 RR |
1478 | if ( wxGetEmailAddress(userID, sizeof(userID)) ) |
1479 | { | |
e90c1d2a | 1480 | fprintf( m_pstream, "%%%%For: %s ", wxMBSTRINGCAST wxConvCurrent->cWX2MB(userID) ); |
87138c52 | 1481 | wxChar userName[245]; |
ed880dd4 | 1482 | if (wxGetUserName(userName, sizeof(userName))) |
e90c1d2a | 1483 | fprintf( m_pstream, " (%s)", wxMBSTRINGCAST wxConvCurrent->cWX2MB(userName) ); |
d7657f75 | 1484 | fprintf( m_pstream, "\n" ); |
ed880dd4 RR |
1485 | } |
1486 | else if ( wxGetUserName(userID, sizeof(userID)) ) | |
1487 | { | |
e90c1d2a | 1488 | fprintf( m_pstream, "%%%%For: %s\n", wxMBSTRINGCAST wxConvCurrent->cWX2MB(userID) );; |
ed880dd4 RR |
1489 | } |
1490 | ||
1491 | // THE FOLLOWING HAS BEEN CONTRIBUTED BY Andy Fyfe <andy@hyperparallel.com> | |
1492 | ||
1493 | long wx_printer_translate_x, wx_printer_translate_y; | |
1494 | double wx_printer_scale_x, wx_printer_scale_y; | |
ed880dd4 | 1495 | |
7bcb11d3 JS |
1496 | wx_printer_translate_x = m_printData.GetPrinterTranslateX(); |
1497 | wx_printer_translate_y = m_printData.GetPrinterTranslateY(); | |
1498 | ||
1499 | wx_printer_scale_x = m_printData.GetPrinterScaleX(); | |
1500 | wx_printer_scale_y = m_printData.GetPrinterScaleY(); | |
1501 | ||
1502 | if (m_printData.GetOrientation() == wxLANDSCAPE) | |
d7657f75 | 1503 | fprintf( m_pstream, "%%%%Orientation: Landscape\n" ); |
ed880dd4 | 1504 | else |
d7657f75 | 1505 | fprintf( m_pstream, "%%%%Orientation: Portrait\n" ); |
ed880dd4 RR |
1506 | |
1507 | // Compute the bounding box. Note that it is in the default user | |
1508 | // coordinate system, thus we have to convert the values. | |
1509 | long llx = (long) ((XLOG2DEV(m_minX)+wx_printer_translate_x)*wx_printer_scale_x); | |
1510 | long lly = (long) ((YLOG2DEV(m_minY)+wx_printer_translate_y)*wx_printer_scale_y); | |
1511 | long urx = (long) ((XLOG2DEV(m_maxX)+wx_printer_translate_x)*wx_printer_scale_x); | |
1512 | long ury = (long) ((YLOG2DEV(m_maxY)+wx_printer_translate_y)*wx_printer_scale_y); | |
1513 | ||
1514 | // If we're landscape, our sense of "x" and "y" is reversed. | |
7bcb11d3 | 1515 | if (m_printData.GetOrientation() == wxLANDSCAPE) |
ed880dd4 RR |
1516 | { |
1517 | long tmp; | |
1518 | tmp = llx; llx = lly; lly = tmp; | |
1519 | tmp = urx; urx = ury; ury = tmp; | |
1520 | ||
1521 | // We need either the two lines that follow, or we need to subtract | |
1522 | // min_x from real_translate_y, which is commented out below. | |
1523 | llx = llx - (long)(m_minX*wx_printer_scale_y); | |
1524 | urx = urx - (long)(m_minX*wx_printer_scale_y); | |
1525 | } | |
1526 | ||
1527 | // The Adobe specifications call for integers; we round as to make | |
1528 | // the bounding larger. | |
d7657f75 RR |
1529 | fprintf( m_pstream, |
1530 | "%%%%BoundingBox: %ld %ld %ld %ld\n", | |
1531 | (long)floor((double)llx), (long)floor((double)lly), | |
1532 | (long)ceil((double)urx), (long)ceil((double)ury) ); | |
1533 | fprintf( m_pstream, "%%%%Pages: %d\n", (wxPageNumber - 1) ); | |
1534 | fprintf( m_pstream, "%%%%EndComments\n\n" ); | |
ed880dd4 RR |
1535 | |
1536 | // To check the correctness of the bounding box, postscript commands | |
1537 | // to draw a box corresponding to the bounding box are generated below. | |
1538 | // But since we typically don't want to print such a box, the postscript | |
1539 | // commands are generated within comments. These lines appear before any | |
1540 | // adjustment of scale, rotation, or translation, and hence are in the | |
1541 | // default user coordinates. | |
d7657f75 RR |
1542 | fprintf( m_pstream, "%% newpath\n" ); |
1543 | fprintf( m_pstream, "%% %ld %ld moveto\n", llx, lly ); | |
1544 | fprintf( m_pstream, "%% %ld %ld lineto\n", urx, lly ); | |
1545 | fprintf( m_pstream, "%% %ld %ld lineto\n", urx, ury ); | |
1546 | fprintf( m_pstream, "%% %ld %ld lineto closepath stroke\n", llx, ury ); | |
ed880dd4 | 1547 | |
d7657f75 RR |
1548 | fclose( m_pstream ); |
1549 | m_pstream = (FILE*) NULL; | |
ed880dd4 | 1550 | |
87138c52 | 1551 | wxChar *tmp_file = wxGetTempFileName("ps"); |
ed880dd4 RR |
1552 | |
1553 | // Paste header Before wx_printer_file | |
d7657f75 RR |
1554 | wxConcatFiles (header_file, m_printData.GetFilename(), tmp_file ); |
1555 | wxRemoveFile( header_file ); | |
1556 | wxRemoveFile( m_printData.GetFilename() ); | |
1557 | wxRenameFile( tmp_file, m_printData.GetFilename() ); | |
ed880dd4 RR |
1558 | |
1559 | #if defined(__X__) || defined(__WXGTK__) | |
d7657f75 | 1560 | if (m_ok) |
ed880dd4 | 1561 | { |
d7657f75 RR |
1562 | wxString previewCommand(m_printData.GetPreviewCommand()); |
1563 | wxString printerCommand(m_printData.GetPrinterCommand()); | |
1564 | wxString printerOptions(m_printData.GetPrinterOptions()); | |
1565 | wxString filename(m_printData.GetFilename()); | |
ed880dd4 | 1566 | |
d7657f75 RR |
1567 | switch (m_printData.GetPrintMode()) |
1568 | { | |
1569 | case wxPRINT_MODE_PREVIEW: | |
1570 | { | |
1571 | wxChar *argv[3]; | |
1572 | argv[0] = WXSTRINGCAST previewCommand; | |
1573 | argv[1] = WXSTRINGCAST filename; | |
1574 | argv[2] = (wxChar*) NULL; | |
1575 | wxExecute( argv, TRUE ); | |
1576 | wxRemoveFile( m_printData.GetFilename() ); | |
1577 | } | |
1578 | break; | |
1579 | case wxPRINT_MODE_PRINTER: | |
1580 | { | |
1581 | wxChar *argv[4]; | |
1582 | int argc = 0; | |
1583 | argv[argc++] = WXSTRINGCAST printerCommand; | |
1584 | ||
1585 | // !SM! If we simply assign to argv[1] here, if printer options | |
1586 | // are blank, we get an annoying and confusing message from lpr. | |
1587 | wxChar *opts = WXSTRINGCAST printerOptions; | |
1588 | if (opts && *opts) | |
1589 | argv[argc++] = opts; | |
1590 | ||
1591 | argv[argc++] = WXSTRINGCAST filename; | |
1592 | argv[argc++] = (wxChar *) NULL; | |
1593 | wxExecute( argv, TRUE ); | |
1594 | wxRemoveFile( filename ); | |
1595 | } | |
1596 | break; | |
1597 | case wxPRINT_MODE_FILE: | |
1598 | case wxPRINT_MODE_NONE: | |
1599 | break; | |
1600 | } | |
ed880dd4 RR |
1601 | } |
1602 | #endif | |
1603 | } | |
1604 | ||
d7657f75 | 1605 | void wxPostScriptDC::StartPage() |
ed880dd4 | 1606 | { |
223d09f6 | 1607 | wxCHECK_RET( m_ok && m_pstream, wxT("invalid postscript dc") ); |
afce4c03 | 1608 | |
d7657f75 | 1609 | fprintf( m_pstream, "%%%%Page: %d\n", wxPageNumber++ ); |
afce4c03 | 1610 | |
ac2def68 | 1611 | // What is this one supposed to do? RR. |
ed880dd4 RR |
1612 | // *m_pstream << "matrix currentmatrix\n"; |
1613 | ||
1614 | // Added by Chris Breeze | |
1615 | ||
9a6be59a VZ |
1616 | // Each page starts with an "initgraphics" which resets the |
1617 | // transformation and so we need to reset the origin | |
1618 | // (and rotate the page for landscape printing) | |
afce4c03 | 1619 | |
afce4c03 VZ |
1620 | // Output scaling |
1621 | long translate_x, translate_y; | |
1622 | double scale_x, scale_y; | |
afce4c03 | 1623 | |
7bcb11d3 JS |
1624 | translate_x = m_printData.GetPrinterTranslateX(); |
1625 | translate_y = m_printData.GetPrinterTranslateY(); | |
1626 | ||
1627 | scale_x = m_printData.GetPrinterScaleX(); | |
1628 | scale_y = m_printData.GetPrinterScaleY(); | |
1629 | ||
1630 | if (m_printData.GetOrientation() == wxLANDSCAPE) | |
afce4c03 | 1631 | { |
ac2def68 RR |
1632 | int h; |
1633 | GetSize( (int*) NULL, &h ); | |
1634 | translate_y -= h; | |
1635 | fprintf( m_pstream, "90 rotate\n" ); | |
d7657f75 | 1636 | |
ac2def68 RR |
1637 | // I copied this one from a PostScript tutorial, but to no avail. RR. |
1638 | // fprintf( m_pstream, "90 rotate llx neg ury nef translate\n" ); | |
afce4c03 VZ |
1639 | } |
1640 | ||
ac2def68 RR |
1641 | fprintf( m_pstream, "%.8f %.8f scale\n", scale_x, scale_y ); |
1642 | fprintf( m_pstream, "%ld %ld translate\n", translate_x, translate_y ); | |
ed880dd4 RR |
1643 | } |
1644 | ||
1645 | void wxPostScriptDC::EndPage () | |
1646 | { | |
223d09f6 | 1647 | wxCHECK_RET( m_ok && m_pstream, wxT("invalid postscript dc") ); |
afce4c03 | 1648 | |
d7657f75 | 1649 | fprintf( m_pstream, "showpage\n" ); |
ed880dd4 RR |
1650 | } |
1651 | ||
64698f9a | 1652 | bool wxPostScriptDC::DoBlit( long xdest, long ydest, |
ea6f44b5 | 1653 | long fwidth, long fheight, |
afce4c03 VZ |
1654 | wxDC *source, |
1655 | long xsrc, long ysrc, | |
45b776d4 | 1656 | int rop, bool WXUNUSED(useMask) ) |
ed880dd4 | 1657 | { |
223d09f6 | 1658 | wxCHECK_MSG( m_ok && m_pstream, FALSE, wxT("invalid postscript dc") ); |
afce4c03 | 1659 | |
223d09f6 | 1660 | wxCHECK_MSG( source, FALSE, wxT("invalid source dc") ); |
afce4c03 | 1661 | |
342b6a2f | 1662 | /* blit into a bitmap */ |
afce4c03 | 1663 | wxBitmap bitmap( (int)fwidth, (int)fheight ); |
45b776d4 JS |
1664 | wxMemoryDC memDC; |
1665 | memDC.SelectObject(bitmap); | |
342b6a2f | 1666 | memDC.Blit(0, 0, fwidth, fheight, source, xsrc, ysrc, rop); /* TODO: Blit transparently? */ |
45b776d4 | 1667 | memDC.SelectObject(wxNullBitmap); |
ea6f44b5 RR |
1668 | |
1669 | /* draw bitmap. scaling and positioning is done there */ | |
ea6f44b5 | 1670 | DrawBitmap( bitmap, xdest, ydest ); |
afce4c03 | 1671 | |
ed880dd4 RR |
1672 | return TRUE; |
1673 | } | |
1674 | ||
64698f9a | 1675 | long wxPostScriptDC::GetCharHeight() const |
ed880dd4 RR |
1676 | { |
1677 | if (m_font.Ok()) | |
1678 | return m_font.GetPointSize(); | |
1679 | else | |
1680 | return 12; | |
1681 | } | |
1682 | ||
342b6a2f | 1683 | void wxPostScriptDC::GetTextExtent( const wxString& string, long *x, long *y, |
64698f9a | 1684 | long *descent, long *externalLeading, wxFont *theFont ) const |
ed880dd4 | 1685 | { |
36b3b54a | 1686 | wxFont *fontToUse = theFont; |
afce4c03 | 1687 | |
36b3b54a RR |
1688 | if (!fontToUse) fontToUse = (wxFont*) &m_font; |
1689 | ||
223d09f6 KB |
1690 | wxCHECK_RET( fontToUse, wxT("GetTextExtent: no font defined") ); |
1691 | wxCHECK_RET( x, wxT("GetTextExtent: x == NULL") ); | |
1692 | wxCHECK_RET( y, wxT("GetTextExtent: y == NULL") ); | |
87138c52 | 1693 | |
9626e0bf | 1694 | const wxWX2MBbuf strbuf = string.mb_str(); |
ed880dd4 | 1695 | |
f036b31c | 1696 | #if !wxUSE_AFM_FOR_POSTSCRIPT |
36b3b54a RR |
1697 | /* Provide a VERY rough estimate (avoid using it). |
1698 | * Produces accurate results for mono-spaced font | |
1699 | * such as Courier (aka wxMODERN) */ | |
afce4c03 | 1700 | |
9a6be59a VZ |
1701 | int height = 12; |
1702 | if (fontToUse) | |
1703 | { | |
afce4c03 | 1704 | height = fontToUse->GetPointSize(); |
9a6be59a | 1705 | } |
87138c52 | 1706 | *x = strlen (strbuf) * height * 72 / 120; |
afce4c03 | 1707 | *y = (long) (height * 1.32); /* allow for descender */ |
36b3b54a RR |
1708 | if (descent) *descent = 0; |
1709 | if (externalLeading) *externalLeading = 0; | |
ed880dd4 | 1710 | #else |
ed880dd4 | 1711 | |
afce4c03 VZ |
1712 | /* method for calculating string widths in postscript: |
1713 | / read in the AFM (adobe font metrics) file for the | |
1714 | / actual font, parse it and extract the character widths | |
1715 | / and also the descender. this may be improved, but for now | |
1716 | / it works well. the AFM file is only read in if the | |
1717 | / font is changed. this may be chached in the future. | |
1718 | / calls to GetTextExtent with the font unchanged are rather | |
1719 | / efficient!!! | |
1720 | / | |
1721 | / for each font and style used there is an AFM file necessary. | |
1722 | / currently i have only files for the roman font family. | |
1723 | / I try to get files for the other ones! | |
1724 | / | |
1725 | / CAVE: the size of the string is currently always calculated | |
1726 | / in 'points' (1/72 of an inch). this should later on be | |
1727 | / changed to depend on the mapping mode. | |
1728 | / CAVE: the path to the AFM files must be set before calling this | |
1729 | / function. this is usually done by a call like the following: | |
1730 | / wxSetAFMPath("d:\\wxw161\\afm\\"); | |
1731 | / | |
1732 | / example: | |
1733 | / | |
1734 | / wxPostScriptDC dc(NULL, TRUE); | |
1735 | / if (dc.Ok()){ | |
1736 | / wxSetAFMPath("d:\\wxw161\\afm\\"); | |
1737 | / dc.StartDoc("Test"); | |
1738 | / dc.StartPage(); | |
1739 | / long w,h; | |
1740 | / dc.SetFont(new wxFont(10, wxROMAN, wxNORMAL, wxNORMAL)); | |
1741 | / dc.GetTextExtent("Hallo",&w,&h); | |
1742 | / dc.EndPage(); | |
1743 | / dc.EndDoc(); | |
1744 | / } | |
1745 | / | |
1746 | / by steve (stefan.hammes@urz.uni-heidelberg.de) | |
1747 | / created: 10.09.94 | |
1748 | / updated: 14.05.95 */ | |
36b3b54a RR |
1749 | |
1750 | /* these static vars are for storing the state between calls */ | |
1751 | static int lastFamily= INT_MIN; | |
1752 | static int lastSize= INT_MIN; | |
1753 | static int lastStyle= INT_MIN; | |
1754 | static int lastWeight= INT_MIN; | |
1755 | static int lastDescender = INT_MIN; | |
1756 | static int lastWidths[256]; /* widths of the characters */ | |
f036b31c | 1757 | |
a439ecef VS |
1758 | double UnderlinePosition = 0.0; |
1759 | double UnderlineThickness = 0.0; | |
36b3b54a RR |
1760 | |
1761 | /* get actual parameters */ | |
1762 | const int Family = fontToUse->GetFamily(); | |
1763 | const int Size = fontToUse->GetPointSize(); | |
1764 | const int Style = fontToUse->GetStyle(); | |
1765 | const int Weight = fontToUse->GetWeight(); | |
1766 | ||
1767 | /* if we have another font, read the font-metrics */ | |
1768 | if (Family!=lastFamily || Size!=lastSize || Style!=lastStyle || Weight!=lastWeight) | |
1769 | { | |
1770 | /* store actual values */ | |
1771 | lastFamily = Family; | |
1772 | lastSize = Size; | |
1773 | lastStyle = Style; | |
1774 | lastWeight = Weight; | |
1775 | ||
1776 | char *name = (char*) NULL; | |
1777 | ||
1778 | switch (Family) | |
afce4c03 VZ |
1779 | { |
1780 | case wxMODERN: | |
1781 | { | |
1782 | if ((Style == wxITALIC) && (Weight == wxBOLD)) name = "CourBoO"; | |
1783 | else if ((Style != wxITALIC) && (Weight == wxBOLD)) name = "CourBo"; | |
1784 | else if ((Style == wxITALIC) && (Weight != wxBOLD)) name = "Cour0"; | |
1785 | else name = "Cour"; | |
1786 | } | |
1787 | break; | |
1788 | case wxROMAN: | |
1789 | { | |
1790 | if ((Style == wxITALIC) && (Weight == wxBOLD)) name = "TimesBoO"; | |
1791 | else if ((Style != wxITALIC) && (Weight == wxBOLD)) name = "TimesBo"; | |
1792 | else if ((Style == wxITALIC) && (Weight != wxBOLD)) name = "TimesO"; | |
a439ecef | 1793 | else name = "TimesRo"; |
afce4c03 VZ |
1794 | } |
1795 | break; | |
1796 | default: | |
1797 | { | |
1798 | if ((Style == wxITALIC) && (Weight == wxBOLD)) name = "HelvBoO"; | |
1799 | else if ((Style != wxITALIC) && (Weight == wxBOLD)) name = "HelvBo"; | |
1800 | else if ((Style == wxITALIC) && (Weight != wxBOLD)) name = "Helv0"; | |
1801 | else name = "Helv"; | |
1802 | } | |
1803 | break; | |
1804 | } | |
36b3b54a RR |
1805 | |
1806 | /* get the directory of the AFM files */ | |
1807 | char afmName[256]; | |
1808 | afmName[0] = 0; | |
385bcb35 RR |
1809 | |
1810 | if (!m_printData.GetFontMetricPath().IsEmpty()) | |
1811 | { | |
f036b31c | 1812 | strcpy( afmName, m_printData.GetFontMetricPath().fn_str() ); |
385bcb35 | 1813 | } |
36b3b54a | 1814 | |
afce4c03 VZ |
1815 | /* 2. open and process the file |
1816 | / a short explanation of the AFM format: | |
1817 | / we have for each character a line, which gives its size | |
1818 | / e.g.: | |
1819 | / | |
1820 | / C 63 ; WX 444 ; N question ; B 49 -14 395 676 ; | |
1821 | / | |
1822 | / that means, we have a character with ascii code 63, and width | |
1823 | / (444/1000 * fontSize) points. | |
1824 | / the other data is ignored for now! | |
1825 | / | |
1826 | / when the font has changed, we read in the right AFM file and store the | |
1827 | / character widths in an array, which is processed below (see point 3.). */ | |
36b3b54a RR |
1828 | |
1829 | /* new elements JC Sun Aug 25 23:21:44 MET DST 1996 */ | |
1830 | ||
1831 | strcat(afmName,name); | |
1832 | strcat(afmName,".afm"); | |
1833 | FILE *afmFile = fopen(afmName,"r"); | |
385bcb35 RR |
1834 | |
1835 | #ifdef __UNIX__ | |
a439ecef VS |
1836 | if (afmFile==NULL) |
1837 | { | |
704d25e2 | 1838 | strcpy( afmName, "/usr/local/share/wx/afm/" ); |
a439ecef VS |
1839 | strcat(afmName,name); |
1840 | strcat(afmName,".afm"); | |
1841 | afmFile = fopen(afmName,"r"); | |
1842 | } | |
1843 | ||
1844 | if (afmFile==NULL) | |
1845 | { | |
704d25e2 | 1846 | strcpy( afmName, "/usr/share/wx/afm/" ); |
a439ecef VS |
1847 | strcat(afmName,name); |
1848 | strcat(afmName,".afm"); | |
1849 | afmFile = fopen(afmName,"r"); | |
1850 | } | |
385bcb35 RR |
1851 | #endif |
1852 | ||
1853 | if (afmFile==NULL) | |
36b3b54a RR |
1854 | { |
1855 | wxLogDebug( "GetTextExtent: can't open AFM file '%s'\n", afmName ); | |
1856 | wxLogDebug( " using approximate values\n"); | |
1857 | for (int i=0; i<256; i++) lastWidths[i] = 500; /* an approximate value */ | |
1858 | lastDescender = -150; /* dito. */ | |
ed880dd4 | 1859 | } |
afce4c03 VZ |
1860 | else |
1861 | { | |
36b3b54a RR |
1862 | /* init the widths array */ |
1863 | for(int i=0; i<256; i++) lastWidths[i] = INT_MIN; | |
1864 | /* some variables for holding parts of a line */ | |
1865 | char cString[10],semiString[10],WXString[10],descString[20]; | |
1866 | char upString[30], utString[30], encString[50]; | |
1867 | char line[256]; | |
1868 | int ascii,cWidth; | |
1869 | /* read in the file and parse it */ | |
1870 | while(fgets(line,sizeof(line),afmFile)!=NULL) | |
afce4c03 | 1871 | { |
36b3b54a RR |
1872 | /* A.) check for descender definition */ |
1873 | if (strncmp(line,"Descender",9)==0) | |
afce4c03 | 1874 | { |
36b3b54a | 1875 | if ((sscanf(line,"%s%d",descString,&lastDescender)!=2) || |
afce4c03 VZ |
1876 | (strcmp(descString,"Descender")!=0)) |
1877 | { | |
1878 | wxLogDebug( "AFM-file '%s': line '%s' has error (bad descender)\n", afmName,line ); | |
36b3b54a RR |
1879 | } |
1880 | } | |
1881 | /* JC 1.) check for UnderlinePosition */ | |
1882 | else if(strncmp(line,"UnderlinePosition",17)==0) | |
afce4c03 | 1883 | { |
36b3b54a | 1884 | if ((sscanf(line,"%s%lf",upString,&UnderlinePosition)!=2) || |
afce4c03 VZ |
1885 | (strcmp(upString,"UnderlinePosition")!=0)) |
1886 | { | |
1887 | wxLogDebug( "AFM-file '%s': line '%s' has error (bad UnderlinePosition)\n", afmName, line ); | |
36b3b54a RR |
1888 | } |
1889 | } | |
afce4c03 | 1890 | /* JC 2.) check for UnderlineThickness */ |
36b3b54a | 1891 | else if(strncmp(line,"UnderlineThickness",18)==0) |
afce4c03 | 1892 | { |
36b3b54a | 1893 | if ((sscanf(line,"%s%lf",utString,&UnderlineThickness)!=2) || |
afce4c03 VZ |
1894 | (strcmp(utString,"UnderlineThickness")!=0)) |
1895 | { | |
1896 | wxLogDebug( "AFM-file '%s': line '%s' has error (bad UnderlineThickness)\n", afmName, line ); | |
36b3b54a RR |
1897 | } |
1898 | } | |
afce4c03 | 1899 | /* JC 3.) check for EncodingScheme */ |
36b3b54a | 1900 | else if(strncmp(line,"EncodingScheme",14)==0) |
afce4c03 | 1901 | { |
36b3b54a | 1902 | if ((sscanf(line,"%s%s",utString,encString)!=2) || |
afce4c03 VZ |
1903 | (strcmp(utString,"EncodingScheme")!=0)) |
1904 | { | |
1905 | wxLogDebug("AFM-file '%s': line '%s' has error (bad EncodingScheme)\n", afmName, line ); | |
36b3b54a RR |
1906 | } |
1907 | else if (strncmp(encString, "AdobeStandardEncoding", 21)) | |
1908 | { | |
afce4c03 VZ |
1909 | wxLogDebug( "AFM-file '%s': line '%s' has error (unsupported EncodingScheme %s)\n", |
1910 | afmName,line, encString); | |
36b3b54a RR |
1911 | } |
1912 | } | |
1913 | /* B.) check for char-width */ | |
1914 | else if(strncmp(line,"C ",2)==0) | |
afce4c03 | 1915 | { |
36b3b54a | 1916 | if (sscanf(line,"%s%d%s%s%d",cString,&ascii,semiString,WXString,&cWidth)!=5) |
afce4c03 | 1917 | { |
36b3b54a RR |
1918 | wxLogDebug("AFM-file '%s': line '%s' has an error (bad character width)\n",afmName,line); |
1919 | } | |
1920 | if(strcmp(cString,"C")!=0 || strcmp(semiString,";")!=0 || strcmp(WXString,"WX")!=0) | |
afce4c03 | 1921 | { |
36b3b54a RR |
1922 | wxLogDebug("AFM-file '%s': line '%s' has a format error\n",afmName,line); |
1923 | } | |
1924 | /* printf(" char '%c'=%d has width '%d'\n",ascii,ascii,cWidth); */ | |
1925 | if (ascii>=0 && ascii<256) | |
afce4c03 | 1926 | { |
36b3b54a RR |
1927 | lastWidths[ascii] = cWidth; /* store width */ |
1928 | } | |
afce4c03 VZ |
1929 | else |
1930 | { | |
1931 | /* MATTHEW: this happens a lot; don't print an error */ | |
36b3b54a RR |
1932 | /* wxLogDebug("AFM-file '%s': ASCII value %d out of range\n",afmName,ascii); */ |
1933 | } | |
1934 | } | |
1935 | /* C.) ignore other entries. */ | |
1936 | } | |
1937 | fclose(afmFile); | |
ed880dd4 | 1938 | } |
36b3b54a | 1939 | /* hack to compute correct values for german 'Umlaute' |
afce4c03 VZ |
1940 | / the correct way would be to map the character names |
1941 | / like 'adieresis' to corresp. positions of ISOEnc and read | |
1942 | / these values from AFM files, too. Maybe later ... */ | |
36b3b54a RR |
1943 |