]> git.saurik.com Git - wxWidgets.git/blob - src/generic/dcpsg.cpp
wxString(file.fn_str()) doesn't make sense. If just file.fn_str() gives
[wxWidgets.git] / src / generic / dcpsg.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: dcpsg.cpp
3 // Purpose: Generic wxPostScriptDC implementation
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
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
11
12 #ifdef __GNUG__
13 #pragma implementation
14 #pragma interface
15 #endif
16
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
27
28 #if wxUSE_PRINTING_ARCHITECTURE
29
30 #if wxUSE_POSTSCRIPT
31
32 #include "wx/window.h"
33 #include "wx/dcmemory.h"
34 #include "wx/utils.h"
35 #include "wx/intl.h"
36 #include "wx/filedlg.h"
37 #include "wx/app.h"
38 #include "wx/msgdlg.h"
39 #include "wx/image.h"
40 #include "wx/log.h"
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"
47 #include "wx/prntbase.h"
48 #include "wx/paper.h"
49
50 #include <math.h>
51
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
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\
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\
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\
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\
133 ";
134
135 static const char *wxPostScriptHeaderColourImage = "\
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\
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\
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\
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\
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\
162 \n\
163 /mergeprocs { %% def\n\
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\
182 /colorimage { %% def\n\
183 pop pop %% remove 'false 3' operands\n\
184 {colortogray} mergeprocs\n\
185 image\n\
186 } bind def\n\
187 } ifelse %% end of 'false' case\n\
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
231 //-------------------------------------------------------------------------------
232 // wxPostScriptDC
233 //-------------------------------------------------------------------------------
234
235 wxPostScriptDC::wxPostScriptDC ()
236 {
237 m_pstream = (FILE*) NULL;
238
239 m_currentRed = 0;
240 m_currentGreen = 0;
241 m_currentBlue = 0;
242
243 m_pageNumber = 0;
244
245 m_clipping = FALSE;
246
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
252
253 // Compatibility only
254 // HH: Doesn't seem to work for wxMSW...
255 #ifndef __WXMSW__
256 m_printData = * wxThePrintSetupData;
257 #endif
258 }
259
260 wxPostScriptDC::wxPostScriptDC (const wxString& file, bool interactive, wxWindow *parent)
261 {
262 m_pstream = (FILE*) NULL;
263
264 m_currentRed = 0;
265 m_currentGreen = 0;
266 m_currentBlue = 0;
267
268 m_pageNumber = 0;
269
270 m_clipping = FALSE;
271
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 = "";
286 m_printData.SetFilename(file);
287
288 #ifdef __WXMSW__
289 // Can only send to file in Windows
290 m_printData.SetPrintMode(wxPRINT_MODE_FILE);
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 }
301
302 return m_ok;
303 }
304
305 wxPostScriptDC::wxPostScriptDC (const wxPrintData& printData)
306 {
307 m_pstream = (FILE*) NULL;
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;
324
325 m_ok = TRUE;
326 }
327
328 wxPostScriptDC::~wxPostScriptDC ()
329 {
330 if (m_pstream)
331 {
332 fclose( m_pstream );
333 m_pstream = (FILE*) NULL;
334 }
335 }
336
337 bool wxPostScriptDC::Ok() const
338 {
339 return m_ok;
340 }
341
342 // This dialog is deprecated now: use wxGenericPrintDialog or the printing framework
343 bool wxPostScriptDC::PrinterDialog(wxWindow *parent)
344 {
345 wxPostScriptPrintDialog dialog( parent, _("Printer Settings"), wxPoint(150, 150), wxSize(400, 400),
346 wxDEFAULT_DIALOG_STYLE | wxDIALOG_MODAL );
347 m_ok = (dialog.ShowModal () == wxID_OK);
348
349 if (!m_ok) return FALSE;
350
351 if ((m_printData.GetFilename() == "") &&
352 (m_printData.GetPrintMode() == wxPRINT_MODE_PREVIEW ||
353 m_printData.GetPrintMode() == wxPRINT_MODE_PRINTER))
354 {
355 // steve, 05.09.94
356 #ifdef __VMS__
357 m_printData.SetFilename("preview");
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
362 wxChar userId[256];
363 wxGetUserId (userId, sizeof (userId) / sizeof (char));
364 wxChar tmp[256];
365 wxStrcpy (tmp, _T("/tmp/preview_"));
366 wxStrcat (tmp, userId);
367 m_printData.SetFilename(tmp);
368 #endif
369 wxChar tmp2[256];
370 wxStrcpy(tmp2, m_printData.GetFilename());
371 wxStrcat (tmp2, _T(".ps"));
372 m_printData.SetFilename(tmp2);
373 }
374 else if ((m_printData.GetFilename() == _T("")) && (m_printData.GetPrintMode() == wxPRINT_MODE_FILE))
375 {
376 wxString file = wxSaveFileSelector (_("PostScript"), _T("ps"));
377 if ( file.IsEmpty() )
378 {
379 m_ok = FALSE;
380 return FALSE;
381 }
382
383 m_printData.SetFilename(file);
384 m_ok = TRUE;
385 }
386
387 return m_ok;
388 }
389
390 void wxPostScriptDC::SetClippingRegion (long x, long y, long w, long h)
391 {
392 wxCHECK_RET( m_ok && m_pstream, _T("invalid postscript dc") );
393
394 if (m_clipping) return;
395
396 wxDC::SetClippingRegion( x, y, w, h );
397
398 m_clipping = TRUE;
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) );
410 }
411
412 void wxPostScriptDC::SetClippingRegion( const wxRegion &WXUNUSED(region) )
413 {
414 wxFAIL_MSG( _T("wxPostScriptDC::SetClippingRegion not implemented.") );
415 }
416
417 void wxPostScriptDC::DestroyClippingRegion()
418 {
419 wxCHECK_RET( m_ok && m_pstream, _T("invalid postscript dc") );
420
421 wxDC::DestroyClippingRegion();
422
423 if (m_clipping)
424 {
425 m_clipping = FALSE;
426 fprintf( m_pstream, "grestore\n" );
427 }
428 }
429
430 void wxPostScriptDC::Clear()
431 {
432 wxFAIL_MSG( _T("wxPostScriptDC::Clear not implemented.") );
433 }
434
435 void wxPostScriptDC::DoFloodFill (long WXUNUSED(x), long WXUNUSED(y), const wxColour &WXUNUSED(col), int WXUNUSED(style))
436 {
437 wxFAIL_MSG( _T("wxPostScriptDC::FloodFill not implemented.") );
438 }
439
440 bool wxPostScriptDC::DoGetPixel (long WXUNUSED(x), long WXUNUSED(y), wxColour * WXUNUSED(col)) const
441 {
442 wxFAIL_MSG( _T("wxPostScriptDC::GetPixel not implemented.") );
443 return FALSE;
444 }
445
446 void wxPostScriptDC::DoCrossHair (long WXUNUSED(x), long WXUNUSED(y))
447 {
448 wxFAIL_MSG( _T("wxPostScriptDC::CrossHair not implemented.") );
449 }
450
451 void wxPostScriptDC::DoDrawLine (long x1, long y1, long x2, long y2)
452 {
453 wxCHECK_RET( m_ok && m_pstream, _T("invalid postscript dc") );
454
455 if (m_pen.GetStyle() == wxTRANSPARENT) return;
456
457 SetPen( m_pen );
458
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) );
466
467 CalcBoundingBox( x1, y1 );
468 CalcBoundingBox( x2, y2 );
469 }
470
471 #define RAD2DEG 57.29577951308
472
473 void wxPostScriptDC::DoDrawArc (long x1, long y1, long x2, long y2, long xc, long yc)
474 {
475 wxCHECK_RET( m_ok && m_pstream, _T("invalid postscript dc") );
476
477 long dx = x1 - xc;
478 long dy = y1 - yc;
479 long radius = (long) sqrt(dx*dx+dy*dy);
480 double alpha1, alpha2;
481
482 if (x1 == x2 && y1 == y2)
483 {
484 alpha1 = 0.0;
485 alpha2 = 360.0;
486 }
487 else if (radius == 0.0)
488 {
489 alpha1 = alpha2 = 0.0;
490 }
491 else
492 {
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;
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
505 if (m_brush.GetStyle() != wxTRANSPARENT)
506 {
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 );
520 }
521
522 if (m_pen.GetStyle() != wxTRANSPARENT)
523 {
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 );
537 }
538 }
539
540 void wxPostScriptDC::DoDrawEllipticArc(long x,long y,long w,long h,double sa,double ea)
541 {
542 wxCHECK_RET( m_ok && m_pstream, _T("invalid postscript dc") );
543
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;
548
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 );
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
564 CalcBoundingBox( x ,y );
565 CalcBoundingBox( x+w, y+h );
566 }
567
568 if (m_pen.GetStyle () != wxTRANSPARENT)
569 {
570 SetPen( m_pen );
571
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 );
578 CalcBoundingBox( x+w, y+h );
579 }
580 }
581
582 void wxPostScriptDC::DoDrawPoint (long x, long y)
583 {
584 wxCHECK_RET( m_ok && m_pstream, _T("invalid postscript dc") );
585
586 if (m_pen.GetStyle() == wxTRANSPARENT) return;
587
588 SetPen (m_pen);
589
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) );
597
598 CalcBoundingBox( x, y );
599 }
600
601 void wxPostScriptDC::DoDrawPolygon (int n, wxPoint points[], long xoffset, long yoffset, int WXUNUSED(fillStyle))
602 {
603 wxCHECK_RET( m_ok && m_pstream, _T("invalid postscript dc") );
604
605 if (n <= 0) return;
606
607 if (m_brush.GetStyle () != wxTRANSPARENT)
608 {
609 SetBrush( m_brush );
610
611 fprintf( m_pstream, "newpath\n" );
612
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 );
619
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 );
626
627 CalcBoundingBox( points[i].x + xoffset, points[i].y + yoffset);
628 }
629
630 fprintf( m_pstream, "fill\n" );
631 }
632
633 if (m_pen.GetStyle () != wxTRANSPARENT)
634 {
635 SetPen( m_pen );
636
637 fprintf( m_pstream, "newpath\n" );
638
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 );
645
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 }
655
656 fprintf( m_pstream, "fill\n" );
657 }
658 }
659
660 void wxPostScriptDC::DoDrawLines (int n, wxPoint points[], long xoffset, long yoffset)
661 {
662 wxCHECK_RET( m_ok && m_pstream, _T("invalid postscript dc") );
663
664 if (m_pen.GetStyle() == wxTRANSPARENT) return;
665
666 if (n <= 0) return;
667
668 SetPen (m_pen);
669
670 int i;
671 for ( i =0; i<n ; i++ )
672 {
673 CalcBoundingBox( XLOG2DEV(points[i].x+xoffset), YLOG2DEV(points[i].y+yoffset));
674 }
675
676 fprintf( m_pstream,
677 "newpath\n"
678 "%ld %ld moveto\n",
679 XLOG2DEV(points[0].x+xoffset), YLOG2DEV(points[0].y+yoffset) );
680
681 for (i = 1; i < n; i++)
682 {
683 fprintf( m_pstream,
684 "%ld %ld lineto\n",
685 XLOG2DEV(points[i].x+xoffset), YLOG2DEV(points[i].y+yoffset) );
686 }
687
688 fprintf( m_pstream, "stroke\n" );
689 }
690
691 void wxPostScriptDC::DoDrawRectangle (long x, long y, long width, long height)
692 {
693 wxCHECK_RET( m_ok && m_pstream, _T("invalid postscript dc") );
694
695 if (m_brush.GetStyle () != wxTRANSPARENT)
696 {
697 SetBrush( m_brush );
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) );
711
712 CalcBoundingBox( x, y );
713 CalcBoundingBox( x + width, y + height );
714 }
715
716 if (m_pen.GetStyle () != wxTRANSPARENT)
717 {
718 SetPen (m_pen);
719
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
733 CalcBoundingBox( x, y );
734 CalcBoundingBox( x + width, y + height );
735 }
736 }
737
738 void wxPostScriptDC::DoDrawRoundedRectangle (long x, long y, long width, long height, double radius)
739 {
740 wxCHECK_RET( m_ok && m_pstream, _T("invalid postscript dc") );
741
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)
748 smallest = width;
749 else
750 smallest = height;
751 radius = (-radius * smallest);
752 }
753
754 long rad = (long) radius;
755
756 if (m_brush.GetStyle () != wxTRANSPARENT)
757 {
758 SetBrush( m_brush );
759
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) );
781
782 CalcBoundingBox( x, y );
783 CalcBoundingBox( x + width, y + height );
784 }
785
786 if (m_pen.GetStyle () != wxTRANSPARENT)
787 {
788 SetPen (m_pen);
789
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) );
811
812 CalcBoundingBox( x, y );
813 CalcBoundingBox( x + width, y + height );
814 }
815 }
816
817 void wxPostScriptDC::DoDrawEllipse (long x, long y, long width, long height)
818 {
819 wxCHECK_RET( m_ok && m_pstream, _T("invalid postscript dc") );
820
821 if (m_brush.GetStyle () != wxTRANSPARENT)
822 {
823 SetBrush (m_brush);
824
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) );
831
832 CalcBoundingBox( x - width, y - height );
833 CalcBoundingBox( x + width, y + height );
834 }
835
836 if (m_pen.GetStyle () != wxTRANSPARENT)
837 {
838 SetPen (m_pen);
839
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) );
846
847 CalcBoundingBox( x - width, y - height );
848 CalcBoundingBox( x + width, y + height );
849 }
850 }
851
852 void wxPostScriptDC::DoDrawIcon( const wxIcon& icon, long x, long y )
853 {
854 DrawBitmap( icon, x, y, TRUE );
855 }
856
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
868 void wxPostScriptDC::DoDrawBitmap( const wxBitmap& bitmap, long x, long y, bool WXUNUSED(useMask) )
869 {
870 wxCHECK_RET( m_ok && m_pstream, _T("invalid postscript dc") );
871
872 if (!bitmap.Ok()) return;
873
874 wxImage image( bitmap );
875
876 if (!image.Ok()) return;
877
878 long ww = XLOG2DEVREL(image.GetWidth());
879 long hh = YLOG2DEVREL(image.GetHeight());
880
881 image = image.Scale( ww, hh );
882
883 if (!image.Ok()) return;
884
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 );
902
903 for (int j = 0; j < hh; j++)
904 {
905 for (int i = 0; i < ww; i++)
906 {
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" );
916 }
917
918 fprintf( m_pstream, "end\n" );
919 fprintf( m_pstream, "origstate restore\n" );
920 }
921
922 void wxPostScriptDC::SetFont( const wxFont& font )
923 {
924 wxCHECK_RET( m_ok && m_pstream, _T("invalid postscript dc") );
925
926 if (!font.Ok()) return;
927
928 m_font = font;
929
930 const char *name;
931 const char *style = "";
932 int Style = m_font.GetStyle();
933 int Weight = m_font.GetWeight();
934
935 switch (m_font.GetFamily())
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))
959 {
960 if (m_font.GetFamily () == wxROMAN)
961 style = "-Roman";
962 else
963 style = "";
964 }
965 else if (Style == wxNORMAL && Weight == wxBOLD)
966 {
967 style = "-Bold";
968 }
969 else if (Style == wxITALIC && (Weight == wxNORMAL || Weight == wxLIGHT))
970 {
971 if (m_font.GetFamily () == wxROMAN)
972 style = "-Italic";
973 else
974 style = "-Oblique";
975 }
976 else if (Style == wxITALIC && Weight == wxBOLD)
977 {
978 if (m_font.GetFamily () == wxROMAN)
979 style = "-BoldItalic";
980 else
981 style = "-BoldOblique";
982 }
983 else if (Style == wxSLANT && (Weight == wxNORMAL || Weight == wxLIGHT))
984 {
985 if (m_font.GetFamily () == wxROMAN)
986 style = "-Italic";
987 else
988 style = "-Oblique";
989 }
990 else if (Style == wxSLANT && Weight == wxBOLD)
991 {
992 if (m_font.GetFamily () == wxROMAN)
993 style = "-BoldItalic";
994 else
995 style = "-BoldOblique";
996 }
997 else
998 {
999 style = "";
1000 }
1001
1002 char buffer[100];
1003 strcpy( buffer, name );
1004 strcat( buffer, style );
1005
1006 fprintf( m_pstream, buffer );
1007 fprintf( m_pstream, " reencodeISO def\n" );
1008 fprintf( m_pstream, buffer );
1009 fprintf( m_pstream, " findfont\n" );
1010 #ifdef __WXMSW__
1011 fprintf( m_pstream, "%d scalefont setfont\n", YLOG2DEVREL(m_font.GetPointSize()) );
1012 #else
1013 fprintf( m_pstream, "%ld scalefont setfont\n", YLOG2DEVREL(m_font.GetPointSize()) );
1014 #endif
1015 }
1016
1017 void wxPostScriptDC::SetPen( const wxPen& pen )
1018 {
1019 wxCHECK_RET( m_ok && m_pstream, _T("invalid postscript dc") );
1020
1021 if (!pen.Ok()) return;
1022
1023 int oldStyle = m_pen.GetStyle();
1024
1025 m_pen = pen;
1026
1027 #ifdef __WXMSW__
1028 fprintf( m_pstream, "%d setlinewidth\n", XLOG2DEVREL(m_pen.GetWidth()) );
1029 #else
1030 fprintf( m_pstream, "%ld setlinewidth\n", XLOG2DEVREL(m_pen.GetWidth()) );
1031 #endif
1032 /*
1033 Line style - WRONG: 2nd arg is OFFSET
1034
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;
1050 switch (m_pen.GetStyle())
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 }
1060
1061 if (oldStyle != m_pen.GetStyle())
1062 {
1063 fprintf( m_pstream, psdash );
1064 fprintf( m_pstream," setdash\n" );
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
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 ?
1084 }
1085
1086 if (!(red == m_currentRed && green == m_currentGreen && blue == m_currentBlue))
1087 {
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;
1099 }
1100 }
1101
1102 void wxPostScriptDC::SetBrush( const wxBrush& brush )
1103 {
1104 wxCHECK_RET( m_ok && m_pstream, _T("invalid postscript dc") );
1105
1106 if (!brush.Ok()) return;
1107
1108 m_brush = brush;
1109
1110 // Brush colour
1111 unsigned char red = m_brush.GetColour().Red();
1112 unsigned char blue = m_brush.GetColour().Blue();
1113 unsigned char green = m_brush.GetColour().Green();
1114
1115 if (!m_colour)
1116 {
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 ?
1127 }
1128
1129 if (!(red == m_currentRed && green == m_currentGreen && blue == m_currentBlue))
1130 {
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;
1142 }
1143 }
1144
1145 void wxPostScriptDC::DoDrawText( const wxString& text, long x, long y )
1146 {
1147 wxCHECK_RET( m_ok && m_pstream, _T("invalid postscript dc") );
1148
1149 SetFont( m_font );
1150
1151 if (m_textForegroundColour.Ok())
1152 {
1153 unsigned char red = m_textForegroundColour.Red();
1154 unsigned char blue = m_textForegroundColour.Blue();
1155 unsigned char green = m_textForegroundColour.Green();
1156
1157 if (!m_colour)
1158 {
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 }
1168 }
1169
1170 // maybe setgray here ?
1171 if (!(red == m_currentRed && green == m_currentGreen && blue == m_currentBlue))
1172 {
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
1181 m_currentRed = red;
1182 m_currentBlue = blue;
1183 m_currentGreen = green;
1184 }
1185 }
1186
1187 int size = m_font.GetPointSize();
1188
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) );
1191
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, "(" );
1197 const wxWX2MBbuf textbuf = text.mb_str();
1198 int len = strlen(textbuf);
1199 int i;
1200 for (i = 0; i < len; i++)
1201 {
1202 int c = (unsigned char) textbuf[i];
1203 if (c == ')' || c == '(' || c == '\\')
1204 {
1205 /* Cope with special characters */
1206 fprintf( m_pstream, "\\" );
1207 tmpbuf[0] = (char) c;
1208 fprintf( m_pstream, tmpbuf );
1209 }
1210 else if ( c >= 128 )
1211 {
1212 /* Cope with character codes > 127 */
1213 fprintf(m_pstream, "\\%o", c);
1214 }
1215 else
1216 {
1217 tmpbuf[0] = (char) c;
1218 fprintf( m_pstream, tmpbuf );
1219 }
1220 }
1221
1222 fprintf( m_pstream, ") show\n" );
1223
1224 if (m_font.GetUnderlined())
1225 {
1226 long uy = (long)(y + size - m_underlinePosition);
1227 long w, h;
1228 GetTextExtent(text, &w, &h);
1229
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) );
1240 }
1241
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 {
1254 wxFAIL_MSG( _T("wxPostScriptDC::SetLogicalFunction not implemented.") );
1255 }
1256
1257 void wxPostScriptDC::DoDrawSpline( wxList *points )
1258 {
1259 wxCHECK_RET( m_ok && m_pstream, _T("invalid postscript dc") );
1260
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();
1268 x1 = p->x;
1269 y1 = p->y;
1270
1271 node = node->Next();
1272 p = (wxPoint *)node->Data();
1273 c = p->x;
1274 d = p->y;
1275 x3 = a = (double)(x1 + c) / 2;
1276 y3 = b = (double)(y1 + d) / 2;
1277
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) );
1284
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
1292 x1 = x3;
1293 y1 = y3;
1294 x2 = c;
1295 y2 = d;
1296 c = q->x;
1297 d = q->y;
1298 x3 = (double)(x2 + c) / 2;
1299 y3 = (double)(y2 + d) / 2;
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) );
1306
1307 CalcBoundingBox( (long)x1, (long)y1 );
1308 CalcBoundingBox( (long)x3, (long)y3 );
1309 }
1310
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
1316 fprintf( m_pstream,
1317 "%ld %ld lineto\n"
1318 "stroke\n",
1319 XLOG2DEV((long)c), YLOG2DEV((long)d) );
1320 }
1321
1322 long wxPostScriptDC::GetCharWidth() const
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 {
1331 wxCHECK_RET( m_ok && m_pstream, _T("invalid postscript dc") );
1332
1333 m_signX = (xLeftRight ? 1 : -1);
1334 m_signY = (yBottomUp ? 1 : -1);
1335
1336 // FIXME there is no such function in MSW
1337 #ifndef __WXMSW__
1338 ComputeScaleAndOrigin();
1339 #endif
1340 }
1341
1342 void wxPostScriptDC::SetDeviceOrigin( long x, long y )
1343 {
1344 wxCHECK_RET( m_ok && m_pstream, _T("invalid postscript dc") );
1345
1346 int h = 0;
1347 int w = 0;
1348 GetSize( &w, &h );
1349
1350 wxDC::SetDeviceOrigin( x, h-y );
1351 }
1352
1353 void wxPostScriptDC::DoGetSize(int* width, int* height) const
1354 {
1355 wxPaperSize id = m_printData.GetPaperId();
1356
1357 wxPrintPaperType *paper = wxThePrintPaperDatabase->FindPaperType(id);
1358
1359 if (!paper) paper = wxThePrintPaperDatabase->FindPaperType(wxPAPER_A4);
1360
1361 if (paper)
1362 {
1363 if (width) *width = paper->GetSizeDeviceUnits().x;
1364 if (height) *height = paper->GetSizeDeviceUnits().y;
1365 }
1366 else
1367 {
1368 if (width) *width = 595;
1369 if (height) *height = 842;
1370 }
1371 }
1372
1373 void wxPostScriptDC::DoGetSizeMM(int *width, int *height) const
1374 {
1375 wxPaperSize id = m_printData.GetPaperId();
1376
1377 wxPrintPaperType *paper = wxThePrintPaperDatabase->FindPaperType(id);
1378
1379 if (!paper) paper = wxThePrintPaperDatabase->FindPaperType(wxPAPER_A4);
1380
1381 if (paper)
1382 {
1383 if (width) *width = paper->GetWidth() / 10;
1384 if (height) *height = paper->GetHeight() / 10;
1385 }
1386 else
1387 {
1388 if (width) *width = 210;
1389 if (height) *height = 297;
1390 }
1391 }
1392
1393 // Resolution in pixels per logical inch
1394 wxSize wxPostScriptDC::GetPPI(void) const
1395 {
1396 return wxSize(72, 72);
1397 }
1398
1399
1400 bool wxPostScriptDC::StartDoc( const wxString& message )
1401 {
1402 wxCHECK_MSG( m_ok, FALSE, _T("invalid postscript dc") );
1403
1404 if (m_printData.GetFilename() == "")
1405 {
1406 wxString filename = wxGetTempFileName("ps");
1407 m_printData.SetFilename(filename);
1408 }
1409
1410 m_pstream = fopen( m_printData.GetFilename().fn_str(), "w+" );
1411
1412 if (!m_pstream)
1413 {
1414 wxMessageBox( _("Cannot open file for PostScript printing!"), _("Error"), wxOK );
1415 m_ok = FALSE;
1416 return FALSE;
1417 }
1418
1419 m_ok = TRUE;
1420
1421 fprintf( m_pstream, "%%%%BeginProlog\n" );
1422 fprintf( m_pstream, wxPostScriptHeaderEllipse );
1423 fprintf( m_pstream, wxPostScriptHeaderEllipticArc );
1424 fprintf( m_pstream, wxPostScriptHeaderColourImage );
1425 fprintf( m_pstream, wxPostScriptHeaderReencodeISO1 );
1426 fprintf( m_pstream, wxPostScriptHeaderReencodeISO2 );
1427 if (wxPostScriptHeaderSpline)
1428 fprintf( m_pstream, wxPostScriptHeaderSpline );
1429 fprintf( m_pstream, "%%%%EndProlog\n" );
1430
1431 SetBrush( *wxBLACK_BRUSH );
1432 SetPen( *wxBLACK_PEN );
1433 SetBackground( *wxWHITE_BRUSH );
1434 SetTextForeground( *wxBLACK );
1435
1436 // set origin according to paper size
1437 SetDeviceOrigin( 0,0 );
1438
1439 wxPageNumber = 1;
1440 m_pageNumber = 1;
1441 m_title = message;
1442 return TRUE;
1443 }
1444
1445 void wxPostScriptDC::EndDoc ()
1446 {
1447 wxCHECK_RET( m_ok && m_pstream, _T("invalid postscript dc") );
1448
1449 if (m_clipping)
1450 {
1451 m_clipping = FALSE;
1452 fprintf( m_pstream, "grestore\n" );
1453 }
1454
1455 fclose( m_pstream );
1456 m_pstream = (FILE *) NULL;
1457
1458 wxChar *header_file = wxGetTempFileName("ps");
1459
1460 m_pstream = fopen( wxConvFile.cWX2MB(header_file) , "w+" );
1461
1462 fprintf( m_pstream, "%%!PS-Adobe-2.0\n" ); // PostScript magic strings
1463 fprintf( m_pstream, "%%%%Title: %s\n", (const char *)m_title.mb_str() );
1464 fprintf( m_pstream, "%%%%Creator: %s\n", (const char*)wxConvLibc.cWX2MB(wxTheApp->argv[0]) );
1465 fprintf( m_pstream, "%%%%CreationDate: %s\n", (const char *)wxNow().mb_str() );
1466
1467 wxChar userID[256];
1468 if ( wxGetEmailAddress(userID, sizeof(userID)) )
1469 {
1470 fprintf( m_pstream, "%%%%For: %s ", MBSTRINGCAST wxConvCurrent->cWX2MB(userID) );
1471 wxChar userName[245];
1472 if (wxGetUserName(userName, sizeof(userName)))
1473 fprintf( m_pstream, " (%s)", MBSTRINGCAST wxConvCurrent->cWX2MB(userName) );
1474 fprintf( m_pstream, "\n" );
1475 }
1476 else if ( wxGetUserName(userID, sizeof(userID)) )
1477 {
1478 fprintf( m_pstream, "%%%%For: %s\n", MBSTRINGCAST wxConvCurrent->cWX2MB(userID) );;
1479 }
1480
1481 // THE FOLLOWING HAS BEEN CONTRIBUTED BY Andy Fyfe <andy@hyperparallel.com>
1482
1483 long wx_printer_translate_x, wx_printer_translate_y;
1484 double wx_printer_scale_x, wx_printer_scale_y;
1485
1486 wx_printer_translate_x = m_printData.GetPrinterTranslateX();
1487 wx_printer_translate_y = m_printData.GetPrinterTranslateY();
1488
1489 wx_printer_scale_x = m_printData.GetPrinterScaleX();
1490 wx_printer_scale_y = m_printData.GetPrinterScaleY();
1491
1492 if (m_printData.GetOrientation() == wxLANDSCAPE)
1493 fprintf( m_pstream, "%%%%Orientation: Landscape\n" );
1494 else
1495 fprintf( m_pstream, "%%%%Orientation: Portrait\n" );
1496
1497 // Compute the bounding box. Note that it is in the default user
1498 // coordinate system, thus we have to convert the values.
1499 long llx = (long) ((XLOG2DEV(m_minX)+wx_printer_translate_x)*wx_printer_scale_x);
1500 long lly = (long) ((YLOG2DEV(m_minY)+wx_printer_translate_y)*wx_printer_scale_y);
1501 long urx = (long) ((XLOG2DEV(m_maxX)+wx_printer_translate_x)*wx_printer_scale_x);
1502 long ury = (long) ((YLOG2DEV(m_maxY)+wx_printer_translate_y)*wx_printer_scale_y);
1503
1504 // If we're landscape, our sense of "x" and "y" is reversed.
1505 if (m_printData.GetOrientation() == wxLANDSCAPE)
1506 {
1507 long tmp;
1508 tmp = llx; llx = lly; lly = tmp;
1509 tmp = urx; urx = ury; ury = tmp;
1510
1511 // We need either the two lines that follow, or we need to subtract
1512 // min_x from real_translate_y, which is commented out below.
1513 llx = llx - (long)(m_minX*wx_printer_scale_y);
1514 urx = urx - (long)(m_minX*wx_printer_scale_y);
1515 }
1516
1517 // The Adobe specifications call for integers; we round as to make
1518 // the bounding larger.
1519 fprintf( m_pstream,
1520 "%%%%BoundingBox: %ld %ld %ld %ld\n",
1521 (long)floor((double)llx), (long)floor((double)lly),
1522 (long)ceil((double)urx), (long)ceil((double)ury) );
1523 fprintf( m_pstream, "%%%%Pages: %d\n", (wxPageNumber - 1) );
1524 fprintf( m_pstream, "%%%%EndComments\n\n" );
1525
1526 // To check the correctness of the bounding box, postscript commands
1527 // to draw a box corresponding to the bounding box are generated below.
1528 // But since we typically don't want to print such a box, the postscript
1529 // commands are generated within comments. These lines appear before any
1530 // adjustment of scale, rotation, or translation, and hence are in the
1531 // default user coordinates.
1532 fprintf( m_pstream, "%% newpath\n" );
1533 fprintf( m_pstream, "%% %ld %ld moveto\n", llx, lly );
1534 fprintf( m_pstream, "%% %ld %ld lineto\n", urx, lly );
1535 fprintf( m_pstream, "%% %ld %ld lineto\n", urx, ury );
1536 fprintf( m_pstream, "%% %ld %ld lineto closepath stroke\n", llx, ury );
1537
1538 fclose( m_pstream );
1539 m_pstream = (FILE*) NULL;
1540
1541 wxChar *tmp_file = wxGetTempFileName("ps");
1542
1543 // Paste header Before wx_printer_file
1544 wxConcatFiles (header_file, m_printData.GetFilename(), tmp_file );
1545 wxRemoveFile( header_file );
1546 wxRemoveFile( m_printData.GetFilename() );
1547 wxRenameFile( tmp_file, m_printData.GetFilename() );
1548
1549 #if defined(__X__) || defined(__WXGTK__)
1550 if (m_ok)
1551 {
1552 wxString previewCommand(m_printData.GetPreviewCommand());
1553 wxString printerCommand(m_printData.GetPrinterCommand());
1554 wxString printerOptions(m_printData.GetPrinterOptions());
1555 wxString filename(m_printData.GetFilename());
1556
1557 switch (m_printData.GetPrintMode())
1558 {
1559 case wxPRINT_MODE_PREVIEW:
1560 {
1561 wxChar *argv[3];
1562 argv[0] = WXSTRINGCAST previewCommand;
1563 argv[1] = WXSTRINGCAST filename;
1564 argv[2] = (wxChar*) NULL;
1565 wxExecute( argv, TRUE );
1566 wxRemoveFile( m_printData.GetFilename() );
1567 }
1568 break;
1569 case wxPRINT_MODE_PRINTER:
1570 {
1571 wxChar *argv[4];
1572 int argc = 0;
1573 argv[argc++] = WXSTRINGCAST printerCommand;
1574
1575 // !SM! If we simply assign to argv[1] here, if printer options
1576 // are blank, we get an annoying and confusing message from lpr.
1577 wxChar *opts = WXSTRINGCAST printerOptions;
1578 if (opts && *opts)
1579 argv[argc++] = opts;
1580
1581 argv[argc++] = WXSTRINGCAST filename;
1582 argv[argc++] = (wxChar *) NULL;
1583 wxExecute( argv, TRUE );
1584 wxRemoveFile( filename );
1585 }
1586 break;
1587 case wxPRINT_MODE_FILE:
1588 case wxPRINT_MODE_NONE:
1589 break;
1590 }
1591 }
1592 #endif
1593 }
1594
1595 void wxPostScriptDC::StartPage()
1596 {
1597 wxCHECK_RET( m_ok && m_pstream, _T("invalid postscript dc") );
1598
1599 fprintf( m_pstream, "%%%%Page: %d\n", wxPageNumber++ );
1600
1601 // *m_pstream << "matrix currentmatrix\n";
1602
1603 // Added by Chris Breeze
1604
1605 // Each page starts with an "initgraphics" which resets the
1606 // transformation and so we need to reset the origin
1607 // (and rotate the page for landscape printing)
1608
1609 /*
1610 m_scaleFactor = 1.0;
1611 m_logicalOriginX = 0;
1612 m_logicalOriginY = 0;
1613 */
1614 // Output scaling
1615 long translate_x, translate_y;
1616 double scale_x, scale_y;
1617
1618 translate_x = m_printData.GetPrinterTranslateX();
1619 translate_y = m_printData.GetPrinterTranslateY();
1620
1621 scale_x = m_printData.GetPrinterScaleX();
1622 scale_y = m_printData.GetPrinterScaleY();
1623
1624 if (m_printData.GetOrientation() == wxLANDSCAPE)
1625 {
1626 // translate_y -= m_maxY;
1627 // fprintf( m_pstream, "90 rotate\n" );
1628
1629 printf( "Hi.\n" );
1630
1631 fprintf( m_pstream, "90 rotate llx neg ury nef translate\n" );
1632 }
1633
1634 // fprintf( m_pstream, "%.8f %.8f scale\n", scale_x, scale_y );
1635 // fprintf( m_pstream, "%ld %ld translate\n", translate_x, translate_y );
1636 }
1637
1638 void wxPostScriptDC::EndPage ()
1639 {
1640 wxCHECK_RET( m_ok && m_pstream, _T("invalid postscript dc") );
1641
1642 fprintf( m_pstream, "showpage\n" );
1643 }
1644
1645 bool wxPostScriptDC::DoBlit( long xdest, long ydest,
1646 long fwidth, long fheight,
1647 wxDC *source,
1648 long xsrc, long ysrc,
1649 int rop, bool WXUNUSED(useMask) )
1650 {
1651 wxCHECK_MSG( m_ok && m_pstream, FALSE, _T("invalid postscript dc") );
1652
1653 wxCHECK_MSG( source, FALSE, _T("invalid source dc") );
1654
1655 /* blit into a bitmap */
1656 wxBitmap bitmap( (int)fwidth, (int)fheight );
1657 wxMemoryDC memDC;
1658 memDC.SelectObject(bitmap);
1659 memDC.Blit(0, 0, fwidth, fheight, source, xsrc, ysrc, rop); /* TODO: Blit transparently? */
1660 memDC.SelectObject(wxNullBitmap);
1661
1662 /* draw bitmap. scaling and positioning is done there */
1663 DrawBitmap( bitmap, xdest, ydest );
1664
1665 return TRUE;
1666 }
1667
1668 long wxPostScriptDC::GetCharHeight() const
1669 {
1670 if (m_font.Ok())
1671 return m_font.GetPointSize();
1672 else
1673 return 12;
1674 }
1675
1676 void wxPostScriptDC::GetTextExtent( const wxString& string, long *x, long *y,
1677 long *descent, long *externalLeading, wxFont *theFont ) const
1678 {
1679 wxFont *fontToUse = theFont;
1680
1681 if (!fontToUse) fontToUse = (wxFont*) &m_font;
1682
1683 wxCHECK_RET( fontToUse, _T("GetTextExtent: no font defined") );
1684 wxCHECK_RET( x, _T("GetTextExtent: x == NULL") );
1685 wxCHECK_RET( y, _T("GetTextExtent: y == NULL") );
1686
1687 const wxWX2MBbuf strbuf = string.mb_str();
1688
1689 #if !USE_AFM_FOR_POSTSCRIPT
1690 /* Provide a VERY rough estimate (avoid using it).
1691 * Produces accurate results for mono-spaced font
1692 * such as Courier (aka wxMODERN) */
1693
1694 int height = 12;
1695 if (fontToUse)
1696 {
1697 height = fontToUse->GetPointSize();
1698 }
1699 *x = strlen (strbuf) * height * 72 / 120;
1700 *y = (long) (height * 1.32); /* allow for descender */
1701 if (descent) *descent = 0;
1702 if (externalLeading) *externalLeading = 0;
1703 #else
1704
1705 /* method for calculating string widths in postscript:
1706 / read in the AFM (adobe font metrics) file for the
1707 / actual font, parse it and extract the character widths
1708 / and also the descender. this may be improved, but for now
1709 / it works well. the AFM file is only read in if the
1710 / font is changed. this may be chached in the future.
1711 / calls to GetTextExtent with the font unchanged are rather
1712 / efficient!!!
1713 /
1714 / for each font and style used there is an AFM file necessary.
1715 / currently i have only files for the roman font family.
1716 / I try to get files for the other ones!
1717 /
1718 / CAVE: the size of the string is currently always calculated
1719 / in 'points' (1/72 of an inch). this should later on be
1720 / changed to depend on the mapping mode.
1721 / CAVE: the path to the AFM files must be set before calling this
1722 / function. this is usually done by a call like the following:
1723 / wxSetAFMPath("d:\\wxw161\\afm\\");
1724 /
1725 / example:
1726 /
1727 / wxPostScriptDC dc(NULL, TRUE);
1728 / if (dc.Ok()){
1729 / wxSetAFMPath("d:\\wxw161\\afm\\");
1730 / dc.StartDoc("Test");
1731 / dc.StartPage();
1732 / long w,h;
1733 / dc.SetFont(new wxFont(10, wxROMAN, wxNORMAL, wxNORMAL));
1734 / dc.GetTextExtent("Hallo",&w,&h);
1735 / dc.EndPage();
1736 / dc.EndDoc();
1737 / }
1738 /
1739 / by steve (stefan.hammes@urz.uni-heidelberg.de)
1740 / created: 10.09.94
1741 / updated: 14.05.95 */
1742
1743 /* these static vars are for storing the state between calls */
1744 static int lastFamily= INT_MIN;
1745 static int lastSize= INT_MIN;
1746 static int lastStyle= INT_MIN;
1747 static int lastWeight= INT_MIN;
1748 static int lastDescender = INT_MIN;
1749 static int lastWidths[256]; /* widths of the characters */
1750
1751 /* get actual parameters */
1752 const int Family = fontToUse->GetFamily();
1753 const int Size = fontToUse->GetPointSize();
1754 const int Style = fontToUse->GetStyle();
1755 const int Weight = fontToUse->GetWeight();
1756
1757 /* if we have another font, read the font-metrics */
1758 if (Family!=lastFamily || Size!=lastSize || Style!=lastStyle || Weight!=lastWeight)
1759 {
1760 /* store actual values */
1761 lastFamily = Family;
1762 lastSize = Size;
1763 lastStyle = Style;
1764 lastWeight = Weight;
1765
1766 char *name = (char*) NULL;
1767
1768 switch (Family)
1769 {
1770 case wxMODERN:
1771 {
1772 if ((Style == wxITALIC) && (Weight == wxBOLD)) name = "CourBoO";
1773 else if ((Style != wxITALIC) && (Weight == wxBOLD)) name = "CourBo";
1774 else if ((Style == wxITALIC) && (Weight != wxBOLD)) name = "Cour0";
1775 else name = "Cour";
1776 }
1777 break;
1778 case wxROMAN:
1779 {
1780 if ((Style == wxITALIC) && (Weight == wxBOLD)) name = "TimesBoO";
1781 else if ((Style != wxITALIC) && (Weight == wxBOLD)) name = "TimesBo";
1782 else if ((Style == wxITALIC) && (Weight != wxBOLD)) name = "TimesO";
1783 else if name = "TimesRo"; /* no typo */
1784 }
1785 break;
1786 default:
1787 {
1788 if ((Style == wxITALIC) && (Weight == wxBOLD)) name = "HelvBoO";
1789 else if ((Style != wxITALIC) && (Weight == wxBOLD)) name = "HelvBo";
1790 else if ((Style == wxITALIC) && (Weight != wxBOLD)) name = "Helv0";
1791 else name = "Helv";
1792 }
1793 break;
1794 }
1795
1796 /* get the directory of the AFM files */
1797 char afmName[256];
1798 afmName[0] = 0;
1799
1800 if (!m_printData.GetFontMetricPath().IsEmpty())
1801 {
1802 strcpy( afmName, m_printData.GetFontMetricPath().fn_str() )
1803 }
1804
1805 /* 2. open and process the file
1806 / a short explanation of the AFM format:
1807 / we have for each character a line, which gives its size
1808 / e.g.:
1809 /
1810 / C 63 ; WX 444 ; N question ; B 49 -14 395 676 ;
1811 /
1812 / that means, we have a character with ascii code 63, and width
1813 / (444/1000 * fontSize) points.
1814 / the other data is ignored for now!
1815 /
1816 / when the font has changed, we read in the right AFM file and store the
1817 / character widths in an array, which is processed below (see point 3.). */
1818
1819 /* new elements JC Sun Aug 25 23:21:44 MET DST 1996 */
1820
1821 strcat(afmName,name);
1822 strcat(afmName,".afm");
1823 FILE *afmFile = fopen(afmName,"r");
1824
1825 #ifdef __UNIX__
1826 if (afmFile==NULL)
1827 {
1828 strcpy( afmName, "/usr/local/share/wx/gs_afm/" );
1829 strcat(afmName,name);
1830 strcat(afmName,".afm");
1831 FILE *afmFile = fopen(afmName,"r");
1832 }
1833
1834 if (afmFile==NULL)
1835 {
1836 strcpy( afmName, "/usr/share/wx/gs_afm/" );
1837 strcat(afmName,name);
1838 strcat(afmName,".afm");
1839 FILE *afmFile = fopen(afmName,"r");
1840 }
1841 #endif
1842
1843 if (afmFile==NULL)
1844 {
1845 wxLogDebug( "GetTextExtent: can't open AFM file '%s'\n", afmName );
1846 wxLogDebug( " using approximate values\n");
1847 for (int i=0; i<256; i++) lastWidths[i] = 500; /* an approximate value */
1848 lastDescender = -150; /* dito. */
1849 }
1850 else
1851 {
1852 /* init the widths array */
1853 for(int i=0; i<256; i++) lastWidths[i] = INT_MIN;
1854 /* some variables for holding parts of a line */
1855 char cString[10],semiString[10],WXString[10],descString[20];
1856 char upString[30], utString[30], encString[50];
1857 char line[256];
1858 int ascii,cWidth;
1859 /* read in the file and parse it */
1860 while(fgets(line,sizeof(line),afmFile)!=NULL)
1861 {
1862 /* A.) check for descender definition */
1863 if (strncmp(line,"Descender",9)==0)
1864 {
1865 if ((sscanf(line,"%s%d",descString,&lastDescender)!=2) ||
1866 (strcmp(descString,"Descender")!=0))
1867 {
1868 wxLogDebug( "AFM-file '%s': line '%s' has error (bad descender)\n", afmName,line );
1869 }
1870 }
1871 /* JC 1.) check for UnderlinePosition */
1872 else if(strncmp(line,"UnderlinePosition",17)==0)
1873 {
1874 if ((sscanf(line,"%s%lf",upString,&UnderlinePosition)!=2) ||
1875 (strcmp(upString,"UnderlinePosition")!=0))
1876 {
1877 wxLogDebug( "AFM-file '%s': line '%s' has error (bad UnderlinePosition)\n", afmName, line );
1878 }
1879 }
1880 /* JC 2.) check for UnderlineThickness */
1881 else if(strncmp(line,"UnderlineThickness",18)==0)
1882 {
1883 if ((sscanf(line,"%s%lf",utString,&UnderlineThickness)!=2) ||
1884 (strcmp(utString,"UnderlineThickness")!=0))
1885 {
1886 wxLogDebug( "AFM-file '%s': line '%s' has error (bad UnderlineThickness)\n", afmName, line );
1887 }
1888 }
1889 /* JC 3.) check for EncodingScheme */
1890 else if(strncmp(line,"EncodingScheme",14)==0)
1891 {
1892 if ((sscanf(line,"%s%s",utString,encString)!=2) ||
1893 (strcmp(utString,"EncodingScheme")!=0))
1894 {
1895 wxLogDebug("AFM-file '%s': line '%s' has error (bad EncodingScheme)\n", afmName, line );
1896 }
1897 else if (strncmp(encString, "AdobeStandardEncoding", 21))
1898 {
1899 wxLogDebug( "AFM-file '%s': line '%s' has error (unsupported EncodingScheme %s)\n",
1900 afmName,line, encString);
1901 }
1902 }
1903 /* B.) check for char-width */
1904 else if(strncmp(line,"C ",2)==0)
1905 {
1906 if (sscanf(line,"%s%d%s%s%d",cString,&ascii,semiString,WXString,&cWidth)!=5)
1907 {
1908 wxLogDebug("AFM-file '%s': line '%s' has an error (bad character width)\n",afmName,line);
1909 }
1910 if(strcmp(cString,"C")!=0 || strcmp(semiString,";")!=0 || strcmp(WXString,"WX")!=0)
1911 {
1912 wxLogDebug("AFM-file '%s': line '%s' has a format error\n",afmName,line);
1913 }
1914 /* printf(" char '%c'=%d has width '%d'\n",ascii,ascii,cWidth); */
1915 if (ascii>=0 && ascii<256)
1916 {
1917 lastWidths[ascii] = cWidth; /* store width */
1918 }
1919 else
1920 {
1921 /* MATTHEW: this happens a lot; don't print an error */
1922 /* wxLogDebug("AFM-file '%s': ASCII value %d out of range\n",afmName,ascii); */
1923 }
1924 }
1925 /* C.) ignore other entries. */
1926 }
1927 fclose(afmFile);
1928 }
1929 /* hack to compute correct values for german 'Umlaute'
1930 / the correct way would be to map the character names
1931 / like 'adieresis' to corresp. positions of ISOEnc and read
1932 / these values from AFM files, too. Maybe later ... */
1933 lastWidths[196] = lastWidths['A']; // Ä
1934 lastWidths[228] = lastWidths['a']; // ä
1935 lastWidths[214] = lastWidths['O']; // Ö
1936 lastWidths[246] = lastWidths['o']; // ö
1937 lastWidths[220] = lastWidths['U']; // Ü
1938 lastWidths[252] = lastWidths['u']; // ü
1939 lastWidths[223] = lastWidths[251]; // ß
1940 }
1941
1942 /* JC: calculate UnderlineThickness/UnderlinePosition */
1943 m_underlinePosition = m_underlinePosition * fontToUse->GetPointSize() / 1000.0f;
1944 m_underlineThickness = m_underlineThickness * fontToUse->GetPointSize() / 1000.0f * m_scaleFactor;
1945
1946 /* 3. now the font metrics are read in, calc size this
1947 / is done by adding the widths of the characters in the
1948 / string. they are given in 1/1000 of the size! */
1949
1950 long widthSum=0;
1951 long height=Size; /* by default */
1952 unsigned char *p;
1953 for(p=(unsigned char *)strbuf; *p; p++)
1954 {
1955 if(lastWidths[*p]== INT_MIN)
1956 {
1957 wxLogDebug("GetTextExtent: undefined width for character '%c' (%d)\n", *p,*p);
1958 widthSum += (long)(lastWidths[' ']/1000.0F * Size); /* assume space */
1959 }
1960 else
1961 {
1962 widthSum += (long)((lastWidths[*p]/1000.0F)*Size);
1963 }
1964 }
1965
1966 /* add descender to height (it is usually a negative value) */
1967 if (lastDescender!=INT_MIN)
1968 {
1969 height += (long)(((-lastDescender)/1000.0F) * Size); /* MATTHEW: forgot scale */
1970 }
1971
1972 /* return size values */
1973 *x = widthSum;
1974 *y = height;
1975
1976 /* return other parameters */
1977 if (descent)
1978 {
1979 if(lastDescender!=INT_MIN)
1980 {
1981 *descent = (long)(((-lastDescender)/1000.0F) * Size); /* MATTHEW: forgot scale */
1982 }
1983 else
1984 {
1985 *descent = 0;
1986 }
1987 }
1988
1989 /* currently no idea how to calculate this! */
1990 if (externalLeading) *externalLeading = 0;
1991
1992 #endif
1993 }
1994
1995 // Determine the Default Postscript Previewer
1996 // available on the platform
1997 #if defined(__SUN__) && defined(__XVIEW__)
1998 // OpenWindow/NeWS's Postscript Previewer
1999 # define PS_VIEWER_PROG "pageview"
2000 #elif defined(__VMS__)
2001 #define PS_VIEWER_PROG "view/format=ps/select=x_display"
2002 #elif defined(__SGI__)
2003 // SGI's Display Postscript Previewer
2004 //# define PS_VIEWER_PROG "dps"
2005 # define PS_VIEWER_PROG "xpsview"
2006 #elif defined(__X__) || defined(__WXGTK__)
2007 // Front-end to ghostscript
2008 # define PS_VIEWER_PROG "ghostview"
2009 #else
2010 // Windows ghostscript/ghostview
2011 # define PS_VIEWER_PROG "gsview"
2012 #endif
2013
2014 wxPrintSetupData *wxThePrintSetupData = (wxPrintSetupData *) NULL;
2015
2016 #if !USE_SHARED_LIBRARY
2017 IMPLEMENT_DYNAMIC_CLASS(wxPostScriptDC, wxDC)
2018 IMPLEMENT_DYNAMIC_CLASS(wxPrintSetupData, wxObject)
2019 #endif
2020
2021 // Redundant now I think
2022 #if 1
2023 IMPLEMENT_CLASS(wxPostScriptPrintDialog, wxDialog)
2024
2025 wxPostScriptPrintDialog::wxPostScriptPrintDialog (wxWindow *parent, const wxString& title,
2026 const wxPoint& pos, const wxSize& size, long style):
2027 wxDialog(parent, -1, title, pos, size, style)
2028 {
2029 wxBeginBusyCursor();
2030
2031 char buf[100];
2032 int yPos = 40;
2033 wxString
2034 *orientation = new wxString[2],
2035 *print_modes = new wxString[3];
2036 int features;
2037 long wx_printer_translate_x, wx_printer_translate_y;
2038 double wx_printer_scale_x, wx_printer_scale_y;
2039
2040 orientation[0] = _("Portrait");
2041 orientation[1] = _("Landscape");
2042
2043 print_modes[0] = _("Send to Printer");
2044 print_modes[1] = _("Print to File");
2045 print_modes[2] = _("Preview Only");
2046
2047
2048
2049 wxButton *okBut = new wxButton (this, wxID_OK, _("OK"), wxPoint(5, 5));
2050 (void) new wxButton (this, wxID_CANCEL, _("Cancel"), wxPoint(40, 5));
2051 okBut->SetDefault();
2052
2053
2054 #if defined(__WXGTK__) || defined (__WXMOTIF__)
2055 (void) new wxStaticText( this, -1, _("Printer Command: "),
2056 wxPoint(5, yPos) );
2057 (void) new wxTextCtrl( this, wxID_PRINTER_COMMAND, wxThePrintSetupData->GetPrinterCommand(),
2058 wxPoint(100, yPos), wxSize(100, -1) );
2059
2060 (void) new wxStaticText( this, -1, _("Printer Options: "),
2061 wxPoint(210, yPos) );
2062 (void) new wxTextCtrl( this, wxID_PRINTER_OPTIONS, wxThePrintSetupData->GetPrinterOptions(),
2063 wxPoint(305, yPos), wxSize(150, -1) );
2064
2065 yPos += 40;
2066 #endif
2067
2068
2069 wxRadioBox *radio0 = new wxRadioBox(this, wxID_PRINTER_ORIENTATION, "Orientation: ", wxPoint(5, yPos), wxSize(-1,-1),
2070 2,orientation,2,wxRA_SPECIFY_ROWS);
2071 radio0->SetSelection((int)wxThePrintSetupData->GetPrinterOrientation() - 1);
2072
2073 // @@@ Configuration hook
2074 if ( !wxThePrintSetupData->GetPrintPreviewCommand() )
2075 wxThePrintSetupData->SetPrintPreviewCommand(PS_VIEWER_PROG);
2076
2077 // wxGetResource ("wxWindows", "PSView", &wxThePrintSetupData->m_previewCommand);
2078
2079 features = (wxThePrintSetupData->GetPrintPreviewCommand() &&
2080 *wxThePrintSetupData->GetPrintPreviewCommand()) ? 3 : 2;
2081
2082 wxRadioBox *radio1 = new wxRadioBox(this, wxID_PRINTER_MODES, _("PostScript:"),
2083 wxPoint(150, yPos),
2084 wxSize(-1,-1), features,
2085 print_modes, features, wxRA_SPECIFY_ROWS);
2086
2087 #ifdef __WXMSW__
2088 radio1->Enable(0, FALSE);
2089 if (wxThePrintSetupData->GetPrintPreviewCommand() && *wxThePrintSetupData->GetPrintPreviewCommand())
2090 radio1->Enable(2, FALSE);
2091 #endif
2092
2093 radio1->SetSelection((int)wxThePrintSetupData->GetPrinterMode());
2094 wxThePrintSetupData->GetPrinterTranslation(&wx_printer_translate_x, &wx_printer_translate_y);
2095 wxThePrintSetupData->GetPrinterScaling(&wx_printer_scale_x, &wx_printer_scale_y);
2096
2097 sprintf (buf, "%.2f", wx_printer_scale_x);
2098
2099 yPos += 90;
2100 (void) new wxStaticText(this, -1, _("X Scaling"), wxPoint(5, yPos));
2101 /* wxTextCtrl *text1 = */ (void) new wxTextCtrl(this, wxID_PRINTER_X_SCALE, buf, wxPoint(100, yPos), wxSize(100, -1));
2102
2103 sprintf (buf, "%.2f", wx_printer_scale_y);
2104 (void) new wxStaticText(this, -1, _("Y Scaling"), wxPoint(220, yPos));
2105 /* wxTextCtrl *text2 = */ (void) new wxTextCtrl(this, wxID_PRINTER_Y_SCALE, buf, wxPoint(320, yPos), wxSize(100, -1));
2106
2107 yPos += 25;
2108
2109 (void) new wxStaticText(this, -1, _("X Translation"), wxPoint(5, yPos));
2110 sprintf (buf, "%.2ld", wx_printer_translate_x);
2111 /* wxTextCtrl *text3 = */ (void) new wxTextCtrl(this, wxID_PRINTER_X_TRANS, buf, wxPoint(100, yPos), wxSize(100, -1));
2112
2113 (void) new wxStaticText(this, -1, _("Y Translation"), wxPoint(220, yPos));
2114 sprintf (buf, "%.2ld", wx_printer_translate_y);
2115 /* wxTextCtrl *text4 = */ (void) new wxTextCtrl(this, wxID_PRINTER_Y_TRANS, buf, wxPoint(320, yPos), wxSize(100, -1));
2116
2117 Fit ();
2118
2119 delete[] orientation;
2120 delete[] print_modes;
2121
2122 wxEndBusyCursor();
2123 }
2124
2125 int wxPostScriptPrintDialog::ShowModal ()
2126 {
2127 if ( wxDialog::ShowModal() == wxID_OK )
2128 {
2129 // wxTextCtrl *text0 = (wxTextCtrl *)FindWindow(wxID_PRINTER_OPTIONS);
2130 wxTextCtrl *text1 = (wxTextCtrl *)FindWindow(wxID_PRINTER_X_SCALE);
2131 wxTextCtrl *text2 = (wxTextCtrl *)FindWindow(wxID_PRINTER_Y_SCALE);
2132 wxTextCtrl *text3 = (wxTextCtrl *)FindWindow(wxID_PRINTER_X_TRANS);
2133 wxTextCtrl *text4 = (wxTextCtrl *)FindWindow(wxID_PRINTER_Y_TRANS);
2134 // wxTextCtrl *text_prt = (wxTextCtrl *)FindWindow(wxID_PRINTER_COMMAND);
2135 wxRadioBox *radio0 = (wxRadioBox *)FindWindow(wxID_PRINTER_ORIENTATION);
2136 wxRadioBox *radio1 = (wxRadioBox *)FindWindow(wxID_PRINTER_MODES);
2137
2138 StringToDouble (WXSTRINGCAST text1->GetValue (), &wxThePrintSetupData->m_printerScaleX);
2139 StringToDouble (WXSTRINGCAST text2->GetValue (), &wxThePrintSetupData->m_printerScaleY);
2140 StringToLong (WXSTRINGCAST text3->GetValue (), &wxThePrintSetupData->m_printerTranslateX);
2141 StringToLong (WXSTRINGCAST text4->GetValue (), &wxThePrintSetupData->m_printerTranslateY);
2142
2143 #ifdef __X__
2144 // wxThePrintSetupData->SetPrinterOptions(WXSTRINGCAST text0->GetValue ());
2145 // wxThePrintSetupData->SetPrinterCommand(WXSTRINGCAST text_prt->GetValue ());
2146 #endif
2147
2148 wxThePrintSetupData->SetPrinterOrientation((radio0->GetSelection() == 1 ? wxLANDSCAPE : wxPORTRAIT));
2149
2150 // C++ wants this
2151 switch ( radio1->GetSelection() ) {
2152 case 0: wxThePrintSetupData->SetPrinterMode(PS_PRINTER); break;
2153 case 1: wxThePrintSetupData->SetPrinterMode(PS_FILE); break;
2154 case 2: wxThePrintSetupData->SetPrinterMode(PS_PREVIEW); break;
2155 }
2156 return wxID_OK;
2157 }
2158 return wxID_CANCEL;
2159 }
2160 #endif
2161 // 0 (redundant)
2162
2163 // PostScript printer settings
2164 // RETAINED FOR BACKWARD COMPATIBILITY
2165 void wxSetPrinterCommand(const wxString& cmd)
2166 {
2167 wxThePrintSetupData->SetPrinterCommand(cmd);
2168 }
2169
2170 void wxSetPrintPreviewCommand(const wxString& cmd)
2171 {
2172 wxThePrintSetupData->SetPrintPreviewCommand(cmd);
2173 }
2174
2175 void wxSetPrinterOptions(const wxString& flags)
2176 {
2177 wxThePrintSetupData->SetPrinterOptions(flags);
2178 }
2179
2180 void wxSetPrinterFile(const wxString& f)
2181 {
2182 wxThePrintSetupData->SetPrinterFile(f);
2183 }
2184
2185 void wxSetPrinterOrientation(int orient)
2186 {
2187 wxThePrintSetupData->SetPrinterOrientation(orient);
2188 }
2189
2190 void wxSetPrinterScaling(double x, double y)
2191 {
2192 wxThePrintSetupData->SetPrinterScaling(x, y);
2193 }
2194
2195 void wxSetPrinterTranslation(long x, long y)
2196 {
2197 wxThePrintSetupData->SetPrinterTranslation(x, y);
2198 }
2199
2200 // 1 = Preview, 2 = print to file, 3 = send to printer
2201 void wxSetPrinterMode(int mode)
2202 {
2203 wxThePrintSetupData->SetPrinterMode(mode);
2204 }
2205
2206 void wxSetAFMPath(const wxString& f)
2207 {
2208 wxThePrintSetupData->SetAFMPath(f);
2209 }
2210
2211 // Get current values
2212 wxString wxGetPrinterCommand()
2213 {
2214 return wxThePrintSetupData->GetPrinterCommand();
2215 }
2216
2217 wxString wxGetPrintPreviewCommand()
2218 {
2219 return wxThePrintSetupData->GetPrintPreviewCommand();
2220 }
2221
2222 wxString wxGetPrinterOptions()
2223 {
2224 return wxThePrintSetupData->GetPrinterOptions();
2225 }
2226
2227 wxString wxGetPrinterFile()
2228 {
2229 return wxThePrintSetupData->GetPrinterFile();
2230 }
2231
2232 int wxGetPrinterOrientation()
2233 {
2234 return wxThePrintSetupData->GetPrinterOrientation();
2235 }
2236
2237 void wxGetPrinterScaling(double* x, double* y)
2238 {
2239 wxThePrintSetupData->GetPrinterScaling(x, y);
2240 }
2241
2242 void wxGetPrinterTranslation(long *x, long *y)
2243 {
2244 wxThePrintSetupData->GetPrinterTranslation(x, y);
2245 }
2246
2247 int wxGetPrinterMode()
2248 {
2249 return wxThePrintSetupData->GetPrinterMode();
2250 }
2251
2252 wxString wxGetAFMPath()
2253 {
2254 return wxThePrintSetupData->GetAFMPath();
2255 }
2256
2257 /*
2258 * Print setup data
2259 */
2260
2261 wxPrintSetupData::wxPrintSetupData()
2262 {
2263 m_printerOrient = wxPORTRAIT;
2264 m_printerScaleX = (double)1.0;
2265 m_printerScaleY = (double)1.0;
2266 m_printerTranslateX = 0;
2267 m_printerTranslateY = 0;
2268 m_printerMode = wxPRINT_MODE_FILE;
2269 m_printColour = TRUE;
2270 }
2271
2272 wxPrintSetupData::~wxPrintSetupData()
2273 {
2274 }
2275
2276 void wxPrintSetupData::operator=(wxPrintSetupData& data)
2277 {
2278 SetPrinterCommand(data.GetPrinterCommand());
2279 SetPrintPreviewCommand(data.GetPrintPreviewCommand());
2280 SetPrinterOptions(data.GetPrinterOptions());
2281 long x, y;
2282 data.GetPrinterTranslation(&x, &y);
2283 SetPrinterTranslation(x, y);
2284
2285 double x1, y1;
2286 data.GetPrinterScaling(&x1, &y1);
2287 SetPrinterScaling(x1, y1);
2288
2289 SetPrinterOrientation(data.GetPrinterOrientation());
2290 SetPrinterMode(data.GetPrinterMode());
2291 SetAFMPath(data.GetAFMPath());
2292 SetPaperName(data.GetPaperName());
2293 SetColour(data.GetColour());
2294 }
2295
2296 // Initialize from a wxPrintData object (wxPrintData should now be used instead of wxPrintSetupData).
2297 void wxPrintSetupData::operator=(const wxPrintData& data)
2298 {
2299 SetPrinterCommand(data.GetPrinterCommand());
2300 SetPrintPreviewCommand(data.GetPreviewCommand());
2301 SetPrinterOptions(data.GetPrinterOptions());
2302 SetPrinterTranslation(data.GetPrinterTranslateX(), data.GetPrinterTranslateY());
2303 SetPrinterScaling(data.GetPrinterScaleX(), data.GetPrinterScaleY());
2304 SetPrinterOrientation(data.GetOrientation());
2305 SetPrinterMode((int) data.GetPrintMode());
2306 SetAFMPath(data.GetFontMetricPath());
2307 SetPaperName(wxThePrintPaperDatabase->ConvertIdToName(data.GetPaperId()));
2308 SetColour(data.GetColour());
2309 SetPrinterFile(data.GetFilename());
2310 }
2311
2312 void wxInitializePrintSetupData(bool init)
2313 {
2314 if (init)
2315 {
2316 wxThePrintSetupData = new wxPrintSetupData;
2317
2318 wxThePrintSetupData->SetPrintPreviewCommand(PS_VIEWER_PROG);
2319 wxThePrintSetupData->SetPrinterOrientation(wxPORTRAIT);
2320 wxThePrintSetupData->SetPrinterMode(wxPRINT_MODE_PREVIEW);
2321 wxThePrintSetupData->SetPaperName(_("A4 sheet, 210 x 297 mm"));
2322
2323 // Could have a .ini file to read in some defaults
2324 // - and/or use environment variables, e.g. WXWIN
2325 #ifdef __VMS__
2326 wxThePrintSetupData->SetPrinterCommand("print");
2327 wxThePrintSetupData->SetPrinterOptions("/nonotify/queue=psqueue");
2328 wxThePrintSetupData->SetAFMPath("sys$ps_font_metrics:");
2329 #endif
2330 #ifdef __WXMSW__
2331 wxThePrintSetupData->SetPrinterCommand("print");
2332 wxThePrintSetupData->SetAFMPath("c:\\windows\\system\\");
2333 wxThePrintSetupData->SetPrinterOptions("");
2334 #endif
2335 #if !defined(__VMS__) && !defined(__WXMSW__)
2336 wxThePrintSetupData->SetPrinterCommand("lpr");
2337 wxThePrintSetupData->SetPrinterOptions("");
2338 wxThePrintSetupData->SetAFMPath("");
2339 #endif
2340 }
2341 else
2342 {
2343 if (wxThePrintSetupData)
2344 delete wxThePrintSetupData;
2345 wxThePrintSetupData = (wxPrintSetupData *) NULL;
2346 }
2347 }
2348
2349 // A module to allow initialization/cleanup of PostScript-related
2350 // things without calling these functions from app.cpp.
2351
2352 class WXDLLEXPORT wxPostScriptModule: public wxModule
2353 {
2354 DECLARE_DYNAMIC_CLASS(wxPostScriptModule)
2355 public:
2356 wxPostScriptModule() {}
2357 bool OnInit();
2358 void OnExit();
2359 };
2360
2361 IMPLEMENT_DYNAMIC_CLASS(wxPostScriptModule, wxModule)
2362
2363 /*
2364 * Initialization/cleanup module
2365 */
2366
2367 bool wxPostScriptModule::OnInit()
2368 {
2369 wxInitializePrintSetupData();
2370
2371 return TRUE;
2372 }
2373
2374 void wxPostScriptModule::OnExit()
2375 {
2376 wxInitializePrintSetupData(FALSE);
2377 }
2378
2379 #endif
2380 // wxUSE_POSTSCRIPT
2381
2382 #endif
2383 // wxUSE_PRINTING_ARCHITECTURE