]> git.saurik.com Git - wxWidgets.git/blob - src/generic/dcpsg.cpp
1. wxApp::ProcessPendingEvents() is now common, added appcmn.cpp and
[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, wxT("/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, wxT(".ps"));
372 m_printData.SetFilename(tmp2);
373 }
374 else if ((m_printData.GetFilename() == wxT("")) && (m_printData.GetPrintMode() == wxPRINT_MODE_FILE))
375 {
376 wxString file = wxSaveFileSelector (_("PostScript"), wxT("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::DoSetClippingRegion (wxCoord x, wxCoord y, wxCoord w, wxCoord h)
391 {
392 wxCHECK_RET( m_ok && m_pstream, wxT("invalid postscript dc") );
393
394 if (m_clipping) DestroyClippingRegion();
395
396 wxDC::DoSetClippingRegion(x, y, w, h);
397
398 m_clipping = TRUE;
399 fprintf( m_pstream,
400 "gsave\n newpath\n"
401 "%d %d moveto\n"
402 "%d %d lineto\n"
403 "%d %d lineto\n"
404 "%d %d 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
413 void wxPostScriptDC::DestroyClippingRegion()
414 {
415 wxCHECK_RET( m_ok && m_pstream, wxT("invalid postscript dc") );
416
417 if (m_clipping)
418 {
419 m_clipping = FALSE;
420 fprintf( m_pstream, "grestore\n" );
421 }
422
423 wxDC::DestroyClippingRegion();
424 }
425
426 void wxPostScriptDC::Clear()
427 {
428 wxFAIL_MSG( wxT("wxPostScriptDC::Clear not implemented.") );
429 }
430
431 void wxPostScriptDC::DoFloodFill (wxCoord WXUNUSED(x), wxCoord WXUNUSED(y), const wxColour &WXUNUSED(col), int WXUNUSED(style))
432 {
433 wxFAIL_MSG( wxT("wxPostScriptDC::FloodFill not implemented.") );
434 }
435
436 bool wxPostScriptDC::DoGetPixel (wxCoord WXUNUSED(x), wxCoord WXUNUSED(y), wxColour * WXUNUSED(col)) const
437 {
438 wxFAIL_MSG( wxT("wxPostScriptDC::GetPixel not implemented.") );
439 return FALSE;
440 }
441
442 void wxPostScriptDC::DoCrossHair (wxCoord WXUNUSED(x), wxCoord WXUNUSED(y))
443 {
444 wxFAIL_MSG( wxT("wxPostScriptDC::CrossHair not implemented.") );
445 }
446
447 void wxPostScriptDC::DoDrawLine (wxCoord x1, wxCoord y1, wxCoord x2, wxCoord y2)
448 {
449 wxCHECK_RET( m_ok && m_pstream, wxT("invalid postscript dc") );
450
451 if (m_pen.GetStyle() == wxTRANSPARENT) return;
452
453 SetPen( m_pen );
454
455 fprintf( m_pstream,
456 "newpath\n"
457 "%d %d moveto\n"
458 "%d %d lineto\n"
459 "stroke\n",
460 XLOG2DEV(x1), YLOG2DEV(y1),
461 XLOG2DEV(x2), YLOG2DEV (y2) );
462
463 CalcBoundingBox( x1, y1 );
464 CalcBoundingBox( x2, y2 );
465 }
466
467 #define RAD2DEG 57.29577951308
468
469 void wxPostScriptDC::DoDrawArc (wxCoord x1, wxCoord y1, wxCoord x2, wxCoord y2, wxCoord xc, wxCoord yc)
470 {
471 wxCHECK_RET( m_ok && m_pstream, wxT("invalid postscript dc") );
472
473 wxCoord dx = x1 - xc;
474 wxCoord dy = y1 - yc;
475 wxCoord radius = (wxCoord) sqrt( (double)(dx*dx+dy*dy) );
476 double alpha1, alpha2;
477
478 if (x1 == x2 && y1 == y2)
479 {
480 alpha1 = 0.0;
481 alpha2 = 360.0;
482 }
483 else if (radius == 0.0)
484 {
485 alpha1 = alpha2 = 0.0;
486 }
487 else
488 {
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;
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
501 if (m_brush.GetStyle() != wxTRANSPARENT)
502 {
503 SetBrush( m_brush );
504
505 fprintf( m_pstream,
506 "newpath\n"
507 "%d %d %d %d %d %d ellipse\n"
508 "%d %d lineto\n"
509 "closepath\n"
510 "fill\n",
511 XLOG2DEV(xc), YLOG2DEV(yc), XLOG2DEVREL(radius), YLOG2DEVREL(radius), (wxCoord)alpha1, (wxCoord) alpha2,
512 XLOG2DEV(xc), YLOG2DEV(yc) );
513
514 CalcBoundingBox( xc-radius, yc-radius );
515 CalcBoundingBox( xc+radius, yc+radius );
516 }
517
518 if (m_pen.GetStyle() != wxTRANSPARENT)
519 {
520 SetPen( m_pen );
521
522 fprintf( m_pstream,
523 "newpath\n"
524 "%d %d %d %d %d %d ellipse\n"
525 "%d %d lineto\n"
526 "stroke\n"
527 "fill\n",
528 XLOG2DEV(xc), YLOG2DEV(yc), XLOG2DEVREL(radius), YLOG2DEVREL(radius), (wxCoord)alpha1, (wxCoord) alpha2,
529 XLOG2DEV(xc), YLOG2DEV(yc) );
530
531 CalcBoundingBox( xc-radius, yc-radius );
532 CalcBoundingBox( xc+radius, yc+radius );
533 }
534 }
535
536 void wxPostScriptDC::DoDrawEllipticArc(wxCoord x,wxCoord y,wxCoord w,wxCoord h,double sa,double ea)
537 {
538 wxCHECK_RET( m_ok && m_pstream, wxT("invalid postscript dc") );
539
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;
544
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 );
554
555 fprintf( m_pstream,
556 "newpath\n"
557 "%d %d %d %d %d %d true ellipticarc\n",
558 XLOG2DEV(x+w/2), YLOG2DEV(y+h/2), XLOG2DEVREL(w/2), YLOG2DEVREL(h/2), (wxCoord)sa, (wxCoord)ea );
559
560 CalcBoundingBox( x ,y );
561 CalcBoundingBox( x+w, y+h );
562 }
563
564 if (m_pen.GetStyle () != wxTRANSPARENT)
565 {
566 SetPen( m_pen );
567
568 fprintf(m_pstream,
569 "newpath\n"
570 "%d %d %d %d %d %d false ellipticarc\n",
571 XLOG2DEV(x+w/2), YLOG2DEV(y+h/2), XLOG2DEVREL(w/2), YLOG2DEVREL(h/2), (wxCoord)sa, (wxCoord)ea );
572
573 CalcBoundingBox( x ,y );
574 CalcBoundingBox( x+w, y+h );
575 }
576 }
577
578 void wxPostScriptDC::DoDrawPoint (wxCoord x, wxCoord y)
579 {
580 wxCHECK_RET( m_ok && m_pstream, wxT("invalid postscript dc") );
581
582 if (m_pen.GetStyle() == wxTRANSPARENT) return;
583
584 SetPen (m_pen);
585
586 fprintf( m_pstream,
587 "newpath\n"
588 "%d %d moveto\n"
589 "%d %d lineto\n"
590 "stroke\n",
591 XLOG2DEV(x), YLOG2DEV(y),
592 XLOG2DEV(x+1), YLOG2DEV(y) );
593
594 CalcBoundingBox( x, y );
595 }
596
597 void wxPostScriptDC::DoDrawPolygon (int n, wxPoint points[], wxCoord xoffset, wxCoord yoffset, int WXUNUSED(fillStyle))
598 {
599 wxCHECK_RET( m_ok && m_pstream, wxT("invalid postscript dc") );
600
601 if (n <= 0) return;
602
603 if (m_brush.GetStyle () != wxTRANSPARENT)
604 {
605 SetBrush( m_brush );
606
607 fprintf( m_pstream, "newpath\n" );
608
609 wxCoord xx = XLOG2DEV(points[0].x + xoffset);
610 wxCoord yy = YLOG2DEV(points[0].y + yoffset);
611
612 fprintf( m_pstream, "%d %d moveto\n", xx, yy );
613
614 CalcBoundingBox( points[0].x + xoffset, points[0].y + yoffset );
615
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, "%d %d lineto\n", xx, yy );
622
623 CalcBoundingBox( points[i].x + xoffset, points[i].y + yoffset);
624 }
625
626 fprintf( m_pstream, "fill\n" );
627 }
628
629 if (m_pen.GetStyle () != wxTRANSPARENT)
630 {
631 SetPen( m_pen );
632
633 fprintf( m_pstream, "newpath\n" );
634
635 wxCoord xx = XLOG2DEV(points[0].x + xoffset);
636 wxCoord yy = YLOG2DEV(points[0].y + yoffset);
637
638 fprintf( m_pstream, "%d %d moveto\n", xx, yy );
639
640 CalcBoundingBox( points[0].x + xoffset, points[0].y + yoffset );
641
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, "%d %d lineto\n", xx, yy );
648
649 CalcBoundingBox( points[i].x + xoffset, points[i].y + yoffset);
650 }
651
652 fprintf( m_pstream, "fill\n" );
653 }
654 }
655
656 void wxPostScriptDC::DoDrawLines (int n, wxPoint points[], wxCoord xoffset, wxCoord yoffset)
657 {
658 wxCHECK_RET( m_ok && m_pstream, wxT("invalid postscript dc") );
659
660 if (m_pen.GetStyle() == wxTRANSPARENT) return;
661
662 if (n <= 0) return;
663
664 SetPen (m_pen);
665
666 int i;
667 for ( i =0; i<n ; i++ )
668 {
669 CalcBoundingBox( XLOG2DEV(points[i].x+xoffset), YLOG2DEV(points[i].y+yoffset));
670 }
671
672 fprintf( m_pstream,
673 "newpath\n"
674 "%d %d moveto\n",
675 XLOG2DEV(points[0].x+xoffset), YLOG2DEV(points[0].y+yoffset) );
676
677 for (i = 1; i < n; i++)
678 {
679 fprintf( m_pstream,
680 "%d %d lineto\n",
681 XLOG2DEV(points[i].x+xoffset), YLOG2DEV(points[i].y+yoffset) );
682 }
683
684 fprintf( m_pstream, "stroke\n" );
685 }
686
687 void wxPostScriptDC::DoDrawRectangle (wxCoord x, wxCoord y, wxCoord width, wxCoord height)
688 {
689 wxCHECK_RET( m_ok && m_pstream, wxT("invalid postscript dc") );
690
691 if (m_brush.GetStyle () != wxTRANSPARENT)
692 {
693 SetBrush( m_brush );
694
695 fprintf( m_pstream,
696 "newpath\n"
697 "%d %d moveto\n"
698 "%d %d lineto\n"
699 "%d %d lineto\n"
700 "%d %d 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) );
707
708 CalcBoundingBox( x, y );
709 CalcBoundingBox( x + width, y + height );
710 }
711
712 if (m_pen.GetStyle () != wxTRANSPARENT)
713 {
714 SetPen (m_pen);
715
716 fprintf( m_pstream,
717 "newpath\n"
718 "%d %d moveto\n"
719 "%d %d lineto\n"
720 "%d %d lineto\n"
721 "%d %d 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
729 CalcBoundingBox( x, y );
730 CalcBoundingBox( x + width, y + height );
731 }
732 }
733
734 void wxPostScriptDC::DoDrawRoundedRectangle (wxCoord x, wxCoord y, wxCoord width, wxCoord height, double radius)
735 {
736 wxCHECK_RET( m_ok && m_pstream, wxT("invalid postscript dc") );
737
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)
744 smallest = width;
745 else
746 smallest = height;
747 radius = (-radius * smallest);
748 }
749
750 wxCoord rad = (wxCoord) radius;
751
752 if (m_brush.GetStyle () != wxTRANSPARENT)
753 {
754 SetBrush( m_brush );
755
756 /* Draw rectangle anticlockwise */
757 fprintf( m_pstream,
758 "newpath\n"
759 "%d %d %d 90 180 arc\n"
760 "%d %d moveto\n"
761 "%d %d %d 180 270 arc\n"
762 "%d %d lineto\n"
763 "%d %d %d 270 0 arc\n"
764 "%d %d lineto\n"
765 "%d %d %d 0 90 arc\n"
766 "%d %d 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) );
777
778 CalcBoundingBox( x, y );
779 CalcBoundingBox( x + width, y + height );
780 }
781
782 if (m_pen.GetStyle () != wxTRANSPARENT)
783 {
784 SetPen (m_pen);
785
786 /* Draw rectangle anticlockwise */
787 fprintf( m_pstream,
788 "newpath\n"
789 "%d %d %d 90 180 arc\n"
790 "%d %d moveto\n"
791 "%d %d %d 180 270 arc\n"
792 "%d %d lineto\n"
793 "%d %d %d 270 0 arc\n"
794 "%d %d lineto\n"
795 "%d %d %d 0 90 arc\n"
796 "%d %d 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) );
807
808 CalcBoundingBox( x, y );
809 CalcBoundingBox( x + width, y + height );
810 }
811 }
812
813 void wxPostScriptDC::DoDrawEllipse (wxCoord x, wxCoord y, wxCoord width, wxCoord height)
814 {
815 wxCHECK_RET( m_ok && m_pstream, wxT("invalid postscript dc") );
816
817 if (m_brush.GetStyle () != wxTRANSPARENT)
818 {
819 SetBrush (m_brush);
820
821 fprintf( m_pstream,
822 "newpath\n"
823 "%d %d %d %d 0 360 ellipse\n"
824 "fill\n",
825 XLOG2DEV(x + width / 2), YLOG2DEV(y + height / 2),
826 XLOG2DEV(width / 2), YLOG2DEVREL(height / 2) );
827
828 CalcBoundingBox( x - width, y - height );
829 CalcBoundingBox( x + width, y + height );
830 }
831
832 if (m_pen.GetStyle () != wxTRANSPARENT)
833 {
834 SetPen (m_pen);
835
836 fprintf( m_pstream,
837 "newpath\n"
838 "%d %d %d %d 0 360 ellipse\n"
839 "stroke\n",
840 XLOG2DEV(x + width / 2), YLOG2DEV(y + height / 2),
841 XLOG2DEV(width / 2), YLOG2DEVREL(height / 2) );
842
843 CalcBoundingBox( x - width, y - height );
844 CalcBoundingBox( x + width, y + height );
845 }
846 }
847
848 void wxPostScriptDC::DoDrawIcon( const wxIcon& icon, wxCoord x, wxCoord y )
849 {
850 DrawBitmap( icon, x, y, TRUE );
851 }
852
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
864 void wxPostScriptDC::DoDrawBitmap( const wxBitmap& bitmap, wxCoord x, wxCoord y, bool WXUNUSED(useMask) )
865 {
866 wxCHECK_RET( m_ok && m_pstream, wxT("invalid postscript dc") );
867
868 if (!bitmap.Ok()) return;
869
870 wxImage image( bitmap );
871
872 if (!image.Ok()) return;
873
874 wxCoord ww = XLOG2DEVREL(image.GetWidth());
875 wxCoord hh = YLOG2DEVREL(image.GetHeight());
876
877 image = image.Scale( ww, hh );
878
879 if (!image.Ok()) return;
880
881 wxCoord xx = XLOG2DEV(x);
882 wxCoord yy = YLOG2DEV(y + bitmap.GetHeight());
883
884 fprintf( m_pstream,
885 "/origstate save def\n"
886 "20 dict begin\n"
887 "/pix %d string def\n"
888 "/grays %d string def\n"
889 "/npixels 0 def\n"
890 "/rgbindx 0 def\n"
891 "%d %d translate\n"
892 "%d %d scale\n"
893 "%d %d 8\n"
894 "[%d 0 0 %d 0 %d]\n"
895 "{currentfile pix readhexstring pop}\n"
896 "false 3 colorimage\n",
897 ww, ww, xx, yy, ww, hh, ww, hh, ww, -hh, hh );
898
899 for (int j = 0; j < hh; j++)
900 {
901 for (int i = 0; i < ww; i++)
902 {
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" );
912 }
913
914 fprintf( m_pstream, "end\n" );
915 fprintf( m_pstream, "origstate restore\n" );
916 }
917
918 void wxPostScriptDC::SetFont( const wxFont& font )
919 {
920 wxCHECK_RET( m_ok && m_pstream, wxT("invalid postscript dc") );
921
922 if (!font.Ok()) return;
923
924 m_font = font;
925
926 const char *name;
927 const char *style = "";
928 int Style = m_font.GetStyle();
929 int Weight = m_font.GetWeight();
930
931 switch (m_font.GetFamily())
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))
955 {
956 if (m_font.GetFamily () == wxROMAN)
957 style = "-Roman";
958 else
959 style = "";
960 }
961 else if (Style == wxNORMAL && Weight == wxBOLD)
962 {
963 style = "-Bold";
964 }
965 else if (Style == wxITALIC && (Weight == wxNORMAL || Weight == wxLIGHT))
966 {
967 if (m_font.GetFamily () == wxROMAN)
968 style = "-Italic";
969 else
970 style = "-Oblique";
971 }
972 else if (Style == wxITALIC && Weight == wxBOLD)
973 {
974 if (m_font.GetFamily () == wxROMAN)
975 style = "-BoldItalic";
976 else
977 style = "-BoldOblique";
978 }
979 else if (Style == wxSLANT && (Weight == wxNORMAL || Weight == wxLIGHT))
980 {
981 if (m_font.GetFamily () == wxROMAN)
982 style = "-Italic";
983 else
984 style = "-Oblique";
985 }
986 else if (Style == wxSLANT && Weight == wxBOLD)
987 {
988 if (m_font.GetFamily () == wxROMAN)
989 style = "-BoldItalic";
990 else
991 style = "-BoldOblique";
992 }
993 else
994 {
995 style = "";
996 }
997
998 char buffer[100];
999 strcpy( buffer, name );
1000 strcat( buffer, style );
1001
1002 fprintf( m_pstream, buffer );
1003 fprintf( m_pstream, " reencodeISO def\n" );
1004 fprintf( m_pstream, buffer );
1005 fprintf( m_pstream, " findfont\n" );
1006 #ifdef __WXMSW__
1007 fprintf( m_pstream, "%d scalefont setfont\n", YLOG2DEVREL(m_font.GetPointSize()) );
1008 #else
1009 fprintf( m_pstream, "%d scalefont setfont\n", YLOG2DEVREL(m_font.GetPointSize()) );
1010 #endif
1011 }
1012
1013 void wxPostScriptDC::SetPen( const wxPen& pen )
1014 {
1015 wxCHECK_RET( m_ok && m_pstream, wxT("invalid postscript dc") );
1016
1017 if (!pen.Ok()) return;
1018
1019 int oldStyle = m_pen.GetStyle();
1020
1021 m_pen = pen;
1022
1023 #ifdef __WXMSW__
1024 fprintf( m_pstream, "%d setlinewidth\n", XLOG2DEVREL(m_pen.GetWidth()) );
1025 #else
1026 fprintf( m_pstream, "%d setlinewidth\n", XLOG2DEVREL(m_pen.GetWidth()) );
1027 #endif
1028 /*
1029 Line style - WRONG: 2nd arg is OFFSET
1030
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 wxCoord, 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 *wxCoord_dashed = "[4 8] 2";
1043 static const char *dotted_dashed = "[6 6 2 6] 4";
1044
1045 const char *psdash = (char *) NULL;
1046 switch (m_pen.GetStyle())
1047 {
1048 case wxDOT: psdash = dotted; break;
1049 case wxSHORT_DASH: psdash = short_dashed; break;
1050 case wxLONG_DASH: psdash = wxCoord_dashed; break;
1051 case wxDOT_DASH: psdash = dotted_dashed; break;
1052 case wxSOLID:
1053 case wxTRANSPARENT:
1054 default: psdash = "[] 0"; break;
1055 }
1056
1057 if (oldStyle != m_pen.GetStyle())
1058 {
1059 fprintf( m_pstream, psdash );
1060 fprintf( m_pstream," setdash\n" );
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
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 ?
1080 }
1081
1082 if (!(red == m_currentRed && green == m_currentGreen && blue == m_currentBlue))
1083 {
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;
1095 }
1096 }
1097
1098 void wxPostScriptDC::SetBrush( const wxBrush& brush )
1099 {
1100 wxCHECK_RET( m_ok && m_pstream, wxT("invalid postscript dc") );
1101
1102 if (!brush.Ok()) return;
1103
1104 m_brush = brush;
1105
1106 // Brush colour
1107 unsigned char red = m_brush.GetColour().Red();
1108 unsigned char blue = m_brush.GetColour().Blue();
1109 unsigned char green = m_brush.GetColour().Green();
1110
1111 if (!m_colour)
1112 {
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 ?
1123 }
1124
1125 if (!(red == m_currentRed && green == m_currentGreen && blue == m_currentBlue))
1126 {
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;
1138 }
1139 }
1140
1141 void wxPostScriptDC::DoDrawText( const wxString& text, wxCoord x, wxCoord y )
1142 {
1143 wxCHECK_RET( m_ok && m_pstream, wxT("invalid postscript dc") );
1144
1145 SetFont( m_font );
1146
1147 if (m_textForegroundColour.Ok())
1148 {
1149 unsigned char red = m_textForegroundColour.Red();
1150 unsigned char blue = m_textForegroundColour.Blue();
1151 unsigned char green = m_textForegroundColour.Green();
1152
1153 if (!m_colour)
1154 {
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 }
1164 }
1165
1166 // maybe setgray here ?
1167 if (!(red == m_currentRed && green == m_currentGreen && blue == m_currentBlue))
1168 {
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
1177 m_currentRed = red;
1178 m_currentBlue = blue;
1179 m_currentGreen = green;
1180 }
1181 }
1182
1183 int size = m_font.GetPointSize();
1184
1185 wxCoord by = y + (wxCoord)floor( double(size) * 2.0 / 3.0 ); // approximate baseline
1186 fprintf( m_pstream, "%d %d moveto\n", XLOG2DEV(x), YLOG2DEV(by) );
1187
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, "(" );
1193 const wxWX2MBbuf textbuf = text.mb_str();
1194 int len = strlen(textbuf);
1195 int i;
1196 for (i = 0; i < len; i++)
1197 {
1198 int c = (unsigned char) textbuf[i];
1199 if (c == ')' || c == '(' || c == '\\')
1200 {
1201 /* Cope with special characters */
1202 fprintf( m_pstream, "\\" );
1203 tmpbuf[0] = (char) c;
1204 fprintf( m_pstream, tmpbuf );
1205 }
1206 else if ( c >= 128 )
1207 {
1208 /* Cope with character codes > 127 */
1209 fprintf(m_pstream, "\\%o", c);
1210 }
1211 else
1212 {
1213 tmpbuf[0] = (char) c;
1214 fprintf( m_pstream, tmpbuf );
1215 }
1216 }
1217
1218 fprintf( m_pstream, ") show\n" );
1219
1220 if (m_font.GetUnderlined())
1221 {
1222 wxCoord uy = (wxCoord)(y + size - m_underlinePosition);
1223 wxCoord w, h;
1224 GetTextExtent(text, &w, &h);
1225
1226 fprintf( m_pstream,
1227 "gsave\n"
1228 "%d %d moveto\n"
1229 "%d setlinewidth\n"
1230 "%d %d lineto\n"
1231 "stroke\n"
1232 "grestore\n",
1233 XLOG2DEV(x), YLOG2DEV(uy),
1234 (wxCoord)m_underlineThickness,
1235 XLOG2DEV(x + w), YLOG2DEV(uy) );
1236 }
1237
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 {
1250 wxFAIL_MSG( wxT("wxPostScriptDC::SetLogicalFunction not implemented.") );
1251 }
1252
1253 void wxPostScriptDC::DoDrawSpline( wxList *points )
1254 {
1255 wxCHECK_RET( m_ok && m_pstream, wxT("invalid postscript dc") );
1256
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();
1264 x1 = p->x;
1265 y1 = p->y;
1266
1267 node = node->Next();
1268 p = (wxPoint *)node->Data();
1269 c = p->x;
1270 d = p->y;
1271 x3 = a = (double)(x1 + c) / 2;
1272 y3 = b = (double)(y1 + d) / 2;
1273
1274 fprintf( m_pstream,
1275 "newpath\n"
1276 "%d %d moveto\n"
1277 "%d %d lineto\n",
1278 XLOG2DEV((wxCoord)x1), YLOG2DEV((wxCoord)y1),
1279 XLOG2DEV((wxCoord)x3), YLOG2DEV((wxCoord)y3) );
1280
1281 CalcBoundingBox( (wxCoord)x1, (wxCoord)y1 );
1282 CalcBoundingBox( (wxCoord)x3, (wxCoord)y3 );
1283
1284 while ((node = node->Next()) != NULL)
1285 {
1286 q = (wxPoint *)node->Data();
1287
1288 x1 = x3;
1289 y1 = y3;
1290 x2 = c;
1291 y2 = d;
1292 c = q->x;
1293 d = q->y;
1294 x3 = (double)(x2 + c) / 2;
1295 y3 = (double)(y2 + d) / 2;
1296
1297 fprintf( m_pstream,
1298 "%d %d %d %d %d %d DrawSplineSection\n",
1299 XLOG2DEV((wxCoord)x1), YLOG2DEV((wxCoord)y1),
1300 XLOG2DEV((wxCoord)x2), YLOG2DEV((wxCoord)y2),
1301 XLOG2DEV((wxCoord)x3), YLOG2DEV((wxCoord)y3) );
1302
1303 CalcBoundingBox( (wxCoord)x1, (wxCoord)y1 );
1304 CalcBoundingBox( (wxCoord)x3, (wxCoord)y3 );
1305 }
1306
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
1312 fprintf( m_pstream,
1313 "%d %d lineto\n"
1314 "stroke\n",
1315 XLOG2DEV((wxCoord)c), YLOG2DEV((wxCoord)d) );
1316 }
1317
1318 wxCoord wxPostScriptDC::GetCharWidth() const
1319 {
1320 // Chris Breeze: reasonable approximation using wxMODERN/Courier
1321 return (wxCoord) (GetCharHeight() * 72.0 / 120.0);
1322 }
1323
1324
1325 void wxPostScriptDC::SetAxisOrientation( bool xLeftRight, bool yBottomUp )
1326 {
1327 wxCHECK_RET( m_ok && m_pstream, wxT("invalid postscript dc") );
1328
1329 m_signX = (xLeftRight ? 1 : -1);
1330 m_signY = (yBottomUp ? 1 : -1);
1331
1332 // FIXME there is no such function in MSW
1333 #ifndef __WXMSW__
1334 ComputeScaleAndOrigin();
1335 #endif
1336 }
1337
1338 void wxPostScriptDC::SetDeviceOrigin( wxCoord x, wxCoord y )
1339 {
1340 wxCHECK_RET( m_ok && m_pstream, wxT("invalid postscript dc") );
1341
1342 int h = 0;
1343 int w = 0;
1344 GetSize( &w, &h );
1345
1346 wxDC::SetDeviceOrigin( x, h-y );
1347 }
1348
1349 void wxPostScriptDC::DoGetSize(int* width, int* height) const
1350 {
1351 wxPaperSize id = m_printData.GetPaperId();
1352
1353 wxPrintPaperType *paper = wxThePrintPaperDatabase->FindPaperType(id);
1354
1355 if (!paper) paper = wxThePrintPaperDatabase->FindPaperType(wxPAPER_A4);
1356
1357 int w = 595;
1358 int h = 842;
1359 if (paper)
1360 {
1361 w = paper->GetSizeDeviceUnits().x;
1362 h = paper->GetSizeDeviceUnits().y;
1363 }
1364
1365 if (m_printData.GetOrientation() == wxLANDSCAPE)
1366 {
1367 int tmp = w;
1368 w = h;
1369 h = tmp;
1370 }
1371
1372 if (width) *width = w;
1373 if (height) *height = h;
1374 }
1375
1376 void wxPostScriptDC::DoGetSizeMM(int *width, int *height) const
1377 {
1378 wxPaperSize id = m_printData.GetPaperId();
1379
1380 wxPrintPaperType *paper = wxThePrintPaperDatabase->FindPaperType(id);
1381
1382 if (!paper) paper = wxThePrintPaperDatabase->FindPaperType(wxPAPER_A4);
1383
1384 int w = 210;
1385 int h = 297;
1386 if (paper)
1387 {
1388 w = paper->GetWidth() / 10;
1389 h = paper->GetHeight() / 10;
1390 }
1391
1392 if (m_printData.GetOrientation() == wxLANDSCAPE)
1393 {
1394 int tmp = w;
1395 w = h;
1396 h = tmp;
1397 }
1398
1399 if (width) *width = w;
1400 if (height) *height = h;
1401 }
1402
1403 // Resolution in pixels per logical inch
1404 wxSize wxPostScriptDC::GetPPI(void) const
1405 {
1406 return wxSize(72, 72);
1407 }
1408
1409
1410 bool wxPostScriptDC::StartDoc( const wxString& message )
1411 {
1412 wxCHECK_MSG( m_ok, FALSE, wxT("invalid postscript dc") );
1413
1414 if (m_printData.GetFilename() == "")
1415 {
1416 wxString filename = wxGetTempFileName("ps");
1417 m_printData.SetFilename(filename);
1418 }
1419
1420 m_pstream = fopen( m_printData.GetFilename().fn_str(), "w+" );
1421
1422 if (!m_pstream)
1423 {
1424 wxMessageBox( _("Cannot open file for PostScript printing!"), _("Error"), wxOK );
1425 m_ok = FALSE;
1426 return FALSE;
1427 }
1428
1429 m_ok = TRUE;
1430
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
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 );
1448
1449 wxPageNumber = 1;
1450 m_pageNumber = 1;
1451 m_title = message;
1452 return TRUE;
1453 }
1454
1455 void wxPostScriptDC::EndDoc ()
1456 {
1457 wxCHECK_RET( m_ok && m_pstream, wxT("invalid postscript dc") );
1458
1459 if (m_clipping)
1460 {
1461 m_clipping = FALSE;
1462 fprintf( m_pstream, "grestore\n" );
1463 }
1464
1465 fclose( m_pstream );
1466 m_pstream = (FILE *) NULL;
1467
1468 wxChar *header_file = wxGetTempFileName("ps");
1469
1470 m_pstream = fopen( wxConvFile.cWX2MB(header_file) , "w+" );
1471
1472 fprintf( m_pstream, "%%!PS-Adobe-2.0\n" ); // PostScript magic strings
1473 fprintf( m_pstream, "%%%%Title: %s\n", (const char *)m_title.mb_str() );
1474 fprintf( m_pstream, "%%%%Creator: %s\n", (const char*)wxConvCurrent->cWX2MB(wxTheApp->argv[0]) );
1475 fprintf( m_pstream, "%%%%CreationDate: %s\n", (const char *)wxNow().mb_str() );
1476
1477 wxChar userID[256];
1478 if ( wxGetEmailAddress(userID, sizeof(userID)) )
1479 {
1480 fprintf( m_pstream, "%%%%For: %s ", wxMBSTRINGCAST wxConvCurrent->cWX2MB(userID) );
1481 wxChar userName[245];
1482 if (wxGetUserName(userName, sizeof(userName)))
1483 fprintf( m_pstream, " (%s)", wxMBSTRINGCAST wxConvCurrent->cWX2MB(userName) );
1484 fprintf( m_pstream, "\n" );
1485 }
1486 else if ( wxGetUserName(userID, sizeof(userID)) )
1487 {
1488 fprintf( m_pstream, "%%%%For: %s\n", wxMBSTRINGCAST wxConvCurrent->cWX2MB(userID) );;
1489 }
1490
1491 // THE FOLLOWING HAS BEEN CONTRIBUTED BY Andy Fyfe <andy@hyperparallel.com>
1492
1493 wxCoord wx_printer_translate_x, wx_printer_translate_y;
1494 double wx_printer_scale_x, wx_printer_scale_y;
1495
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)
1503 fprintf( m_pstream, "%%%%Orientation: Landscape\n" );
1504 else
1505 fprintf( m_pstream, "%%%%Orientation: Portrait\n" );
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 wxCoord llx = (wxCoord) ((XLOG2DEV(m_minX)+wx_printer_translate_x)*wx_printer_scale_x);
1510 wxCoord lly = (wxCoord) ((YLOG2DEV(m_minY)+wx_printer_translate_y)*wx_printer_scale_y);
1511 wxCoord urx = (wxCoord) ((XLOG2DEV(m_maxX)+wx_printer_translate_x)*wx_printer_scale_x);
1512 wxCoord ury = (wxCoord) ((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.
1515 if (m_printData.GetOrientation() == wxLANDSCAPE)
1516 {
1517 wxCoord 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 - (wxCoord)(m_minX*wx_printer_scale_y);
1524 urx = urx - (wxCoord)(m_minX*wx_printer_scale_y);
1525 }
1526
1527 // The Adobe specifications call for integers; we round as to make
1528 // the bounding larger.
1529 fprintf( m_pstream,
1530 "%%%%BoundingBox: %d %d %d %d\n",
1531 (wxCoord)floor((double)llx), (wxCoord)floor((double)lly),
1532 (wxCoord)ceil((double)urx), (wxCoord)ceil((double)ury) );
1533 fprintf( m_pstream, "%%%%Pages: %d\n", (wxPageNumber - 1) );
1534 fprintf( m_pstream, "%%%%EndComments\n\n" );
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.
1542 fprintf( m_pstream, "%% newpath\n" );
1543 fprintf( m_pstream, "%% %d %d moveto\n", llx, lly );
1544 fprintf( m_pstream, "%% %d %d lineto\n", urx, lly );
1545 fprintf( m_pstream, "%% %d %d lineto\n", urx, ury );
1546 fprintf( m_pstream, "%% %d %d lineto closepath stroke\n", llx, ury );
1547
1548 fclose( m_pstream );
1549 m_pstream = (FILE*) NULL;
1550
1551 wxChar *tmp_file = wxGetTempFileName("ps");
1552
1553 // Paste header Before wx_printer_file
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() );
1558
1559 #if defined(__X__) || defined(__WXGTK__)
1560 if (m_ok)
1561 {
1562 wxString previewCommand(m_printData.GetPreviewCommand());
1563 wxString printerCommand(m_printData.GetPrinterCommand());
1564 wxString printerOptions(m_printData.GetPrinterOptions());
1565 wxString filename(m_printData.GetFilename());
1566
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 }
1601 }
1602 #endif
1603 }
1604
1605 void wxPostScriptDC::StartPage()
1606 {
1607 wxCHECK_RET( m_ok && m_pstream, wxT("invalid postscript dc") );
1608
1609 fprintf( m_pstream, "%%%%Page: %d\n", wxPageNumber++ );
1610
1611 // What is this one supposed to do? RR.
1612 // *m_pstream << "matrix currentmatrix\n";
1613
1614 // Added by Chris Breeze
1615
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)
1619
1620 // Output scaling
1621 wxCoord translate_x, translate_y;
1622 double scale_x, scale_y;
1623
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)
1631 {
1632 int h;
1633 GetSize( (int*) NULL, &h );
1634 translate_y -= h;
1635 fprintf( m_pstream, "90 rotate\n" );
1636
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" );
1639 }
1640
1641 fprintf( m_pstream, "%.8f %.8f scale\n", scale_x, scale_y );
1642 fprintf( m_pstream, "%d %d translate\n", translate_x, translate_y );
1643 }
1644
1645 void wxPostScriptDC::EndPage ()
1646 {
1647 wxCHECK_RET( m_ok && m_pstream, wxT("invalid postscript dc") );
1648
1649 fprintf( m_pstream, "showpage\n" );
1650 }
1651
1652 bool wxPostScriptDC::DoBlit( wxCoord xdest, wxCoord ydest,
1653 wxCoord fwidth, wxCoord fheight,
1654 wxDC *source,
1655 wxCoord xsrc, wxCoord ysrc,
1656 int rop, bool WXUNUSED(useMask) )
1657 {
1658 wxCHECK_MSG( m_ok && m_pstream, FALSE, wxT("invalid postscript dc") );
1659
1660 wxCHECK_MSG( source, FALSE, wxT("invalid source dc") );
1661
1662 /* blit into a bitmap */
1663 wxBitmap bitmap( (int)fwidth, (int)fheight );
1664 wxMemoryDC memDC;
1665 memDC.SelectObject(bitmap);
1666 memDC.Blit(0, 0, fwidth, fheight, source, xsrc, ysrc, rop); /* TODO: Blit transparently? */
1667 memDC.SelectObject(wxNullBitmap);
1668
1669 /* draw bitmap. scaling and positioning is done there */
1670 DrawBitmap( bitmap, xdest, ydest );
1671
1672 return TRUE;
1673 }
1674
1675 wxCoord wxPostScriptDC::GetCharHeight() const
1676 {
1677 if (m_font.Ok())
1678 return m_font.GetPointSize();
1679 else
1680 return 12;
1681 }
1682
1683 void wxPostScriptDC::DoGetTextExtent(const wxString& string,
1684 wxCoord *x, wxCoord *y,
1685 wxCoord *descent, wxCoord *externalLeading,
1686 wxFont *theFont ) const
1687 {
1688 wxFont *fontToUse = theFont;
1689
1690 if (!fontToUse) fontToUse = (wxFont*) &m_font;
1691
1692 wxCHECK_RET( fontToUse, wxT("GetTextExtent: no font defined") );
1693 wxCHECK_RET( x, wxT("GetTextExtent: x == NULL") );
1694 wxCHECK_RET( y, wxT("GetTextExtent: y == NULL") );
1695
1696 const wxWX2MBbuf strbuf = string.mb_str();
1697
1698 #if !wxUSE_AFM_FOR_POSTSCRIPT
1699 /* Provide a VERY rough estimate (avoid using it).
1700 * Produces accurate results for mono-spaced font
1701 * such as Courier (aka wxMODERN) */
1702
1703 int height = 12;
1704 if (fontToUse)
1705 {
1706 height = fontToUse->GetPointSize();
1707 }
1708 if ( x )
1709 *x = strlen (strbuf) * height * 72 / 120;
1710 if ( y )
1711 *y = (wxCoord) (height * 1.32); /* allow for descender */
1712 if (descent) *descent = 0;
1713 if (externalLeading) *externalLeading = 0;
1714 #else
1715
1716 /* method for calculating string widths in postscript:
1717 / read in the AFM (adobe font metrics) file for the
1718 / actual font, parse it and extract the character widths
1719 / and also the descender. this may be improved, but for now
1720 / it works well. the AFM file is only read in if the
1721 / font is changed. this may be chached in the future.
1722 / calls to GetTextExtent with the font unchanged are rather
1723 / efficient!!!
1724 /
1725 / for each font and style used there is an AFM file necessary.
1726 / currently i have only files for the roman font family.
1727 / I try to get files for the other ones!
1728 /
1729 / CAVE: the size of the string is currently always calculated
1730 / in 'points' (1/72 of an inch). this should later on be
1731 / changed to depend on the mapping mode.
1732 / CAVE: the path to the AFM files must be set before calling this
1733 / function. this is usually done by a call like the following:
1734 / wxSetAFMPath("d:\\wxw161\\afm\\");
1735 /
1736 / example:
1737 /
1738 / wxPostScriptDC dc(NULL, TRUE);
1739 / if (dc.Ok()){
1740 / wxSetAFMPath("d:\\wxw161\\afm\\");
1741 / dc.StartDoc("Test");
1742 / dc.StartPage();
1743 / wxCoord w,h;
1744 / dc.SetFont(new wxFont(10, wxROMAN, wxNORMAL, wxNORMAL));
1745 / dc.GetTextExtent("Hallo",&w,&h);
1746 / dc.EndPage();
1747 / dc.EndDoc();
1748 / }
1749 /
1750 / by steve (stefan.hammes@urz.uni-heidelberg.de)
1751 / created: 10.09.94
1752 / updated: 14.05.95 */
1753
1754 /* these static vars are for storing the state between calls */
1755 static int lastFamily= INT_MIN;
1756 static int lastSize= INT_MIN;
1757 static int lastStyle= INT_MIN;
1758 static int lastWeight= INT_MIN;
1759 static int lastDescender = INT_MIN;
1760 static int lastWidths[256]; /* widths of the characters */
1761
1762 double UnderlinePosition = 0.0;
1763 double UnderlineThickness = 0.0;
1764
1765 /* get actual parameters */
1766 const int Family = fontToUse->GetFamily();
1767 const int Size = fontToUse->GetPointSize();
1768 const int Style = fontToUse->GetStyle();
1769 const int Weight = fontToUse->GetWeight();
1770
1771 /* if we have another font, read the font-metrics */
1772 if (Family!=lastFamily || Size!=lastSize || Style!=lastStyle || Weight!=lastWeight)
1773 {
1774 /* store actual values */
1775 lastFamily = Family;
1776 lastSize = Size;
1777 lastStyle = Style;
1778 lastWeight = Weight;
1779
1780 char *name = (char*) NULL;
1781
1782 switch (Family)
1783 {
1784 case wxMODERN:
1785 {
1786 if ((Style == wxITALIC) && (Weight == wxBOLD)) name = "CourBoO";
1787 else if ((Style != wxITALIC) && (Weight == wxBOLD)) name = "CourBo";
1788 else if ((Style == wxITALIC) && (Weight != wxBOLD)) name = "Cour0";
1789 else name = "Cour";
1790 }
1791 break;
1792 case wxROMAN:
1793 {
1794 if ((Style == wxITALIC) && (Weight == wxBOLD)) name = "TimesBoO";
1795 else if ((Style != wxITALIC) && (Weight == wxBOLD)) name = "TimesBo";
1796 else if ((Style == wxITALIC) && (Weight != wxBOLD)) name = "TimesO";
1797 else name = "TimesRo";
1798 }
1799 break;
1800 default:
1801 {
1802 if ((Style == wxITALIC) && (Weight == wxBOLD)) name = "HelvBoO";
1803 else if ((Style != wxITALIC) && (Weight == wxBOLD)) name = "HelvBo";
1804 else if ((Style == wxITALIC) && (Weight != wxBOLD)) name = "Helv0";
1805 else name = "Helv";
1806 }
1807 break;
1808 }
1809
1810 /* get the directory of the AFM files */
1811 char afmName[256];
1812 afmName[0] = 0;
1813
1814 if (!m_printData.GetFontMetricPath().IsEmpty())
1815 {
1816 strcpy( afmName, m_printData.GetFontMetricPath().fn_str() );
1817 }
1818
1819 /* 2. open and process the file
1820 / a short explanation of the AFM format:
1821 / we have for each character a line, which gives its size
1822 / e.g.:
1823 /
1824 / C 63 ; WX 444 ; N question ; B 49 -14 395 676 ;
1825 /
1826 / that means, we have a character with ascii code 63, and width
1827 / (444/1000 * fontSize) points.
1828 / the other data is ignored for now!
1829 /
1830 / when the font has changed, we read in the right AFM file and store the
1831 / character widths in an array, which is processed below (see point 3.). */
1832
1833 /* new elements JC Sun Aug 25 23:21:44 MET DST 1996 */
1834
1835 strcat(afmName,name);
1836 strcat(afmName,".afm");
1837 FILE *afmFile = fopen(afmName,"r");
1838
1839 #ifdef __UNIX__
1840 if (afmFile==NULL)
1841 {
1842 strcpy( afmName, "/usr/local/share/wx/afm/" );
1843 strcat(afmName,name);
1844 strcat(afmName,".afm");
1845 afmFile = fopen(afmName,"r");
1846 }
1847
1848 if (afmFile==NULL)
1849 {
1850 strcpy( afmName, "/usr/share/wx/afm/" );
1851 strcat(afmName,name);
1852 strcat(afmName,".afm");
1853 afmFile = fopen(afmName,"r");
1854 }
1855 #endif
1856
1857 if (afmFile==NULL)
1858 {
1859 wxLogDebug( "GetTextExtent: can't open AFM file '%s'\n", afmName );
1860 wxLogDebug( " using approximate values\n");
1861 for (int i=0; i<256; i++) lastWidths[i] = 500; /* an approximate value */
1862 lastDescender = -150; /* dito. */
1863 }
1864 else
1865 {
1866 /* init the widths array */
1867 for(int i=0; i<256; i++) lastWidths[i] = INT_MIN;
1868 /* some variables for holding parts of a line */
1869 char cString[10],semiString[10],WXString[10],descString[20];
1870 char upString[30], utString[30], encString[50];
1871 char line[256];
1872 int ascii,cWidth;
1873 /* read in the file and parse it */
1874 while(fgets(line,sizeof(line),afmFile)!=NULL)
1875 {
1876 /* A.) check for descender definition */
1877 if (strncmp(line,"Descender",9)==0)
1878 {
1879 if ((sscanf(line,"%s%d",descString,&lastDescender)!=2) ||
1880 (strcmp(descString,"Descender")!=0))
1881 {
1882 wxLogDebug( "AFM-file '%s': line '%s' has error (bad descender)\n", afmName,line );
1883 }
1884 }
1885 /* JC 1.) check for UnderlinePosition */
1886 else if(strncmp(line,"UnderlinePosition",17)==0)
1887 {
1888 if ((sscanf(line,"%s%lf",upString,&UnderlinePosition)!=2) ||
1889 (strcmp(upString,"UnderlinePosition")!=0))
1890 {
1891 wxLogDebug( "AFM-file '%s': line '%s' has error (bad UnderlinePosition)\n", afmName, line );
1892 }
1893 }
1894 /* JC 2.) check for UnderlineThickness */
1895 else if(strncmp(line,"UnderlineThickness",18)==0)
1896 {
1897 if ((sscanf(line,"%s%lf",utString,&UnderlineThickness)!=2) ||
1898 (strcmp(utString,"UnderlineThickness")!=0))
1899 {
1900 wxLogDebug( "AFM-file '%s': line '%s' has error (bad UnderlineThickness)\n", afmName, line );
1901 }
1902 }
1903 /* JC 3.) check for EncodingScheme */
1904 else if(strncmp(line,"EncodingScheme",14)==0)
1905 {
1906 if ((sscanf(line,"%s%s",utString,encString)!=2) ||
1907 (strcmp(utString,"EncodingScheme")!=0))
1908 {
1909 wxLogDebug("AFM-file '%s': line '%s' has error (bad EncodingScheme)\n", afmName, line );
1910 }
1911 else if (strncmp(encString, "AdobeStandardEncoding", 21))
1912 {
1913 wxLogDebug( "AFM-file '%s': line '%s' has error (unsupported EncodingScheme %s)\n",
1914 afmName,line, encString);
1915 }
1916 }
1917 /* B.) check for char-width */
1918 else if(strncmp(line,"C ",2)==0)
1919 {
1920 if (sscanf(line,"%s%d%s%s%d",cString,&ascii,semiString,WXString,&cWidth)!=5)
1921 {
1922 wxLogDebug("AFM-file '%s': line '%s' has an error (bad character width)\n",afmName,line);
1923 }
1924 if(strcmp(cString,"C")!=0 || strcmp(semiString,";")!=0 || strcmp(WXString,"WX")!=0)
1925 {
1926 wxLogDebug("AFM-file '%s': line '%s' has a format error\n",afmName,line);
1927 }
1928 /* printf(" char '%c'=%d has width '%d'\n",ascii,ascii,cWidth); */
1929 if (ascii>=0 && ascii<256)
1930 {
1931 lastWidths[ascii] = cWidth; /* store width */
1932 }
1933 else
1934 {
1935 /* MATTHEW: this happens a lot; don't print an error */
1936 /* wxLogDebug("AFM-file '%s': ASCII value %d out of range\n",afmName,ascii); */
1937 }
1938 }
1939 /* C.) ignore other entries. */
1940 }
1941 fclose(afmFile);
1942 }
1943 /* hack to compute correct values for german 'Umlaute'
1944 / the correct way would be to map the character names
1945 / like 'adieresis' to corresp. positions of ISOEnc and read
1946 / these values from AFM files, too. Maybe later ... */
1947 lastWidths[196] = lastWidths['A']; // Ä
1948 lastWidths[228] = lastWidths['a']; // ä
1949 lastWidths[214] = lastWidths['O']; // Ö
1950 lastWidths[246] = lastWidths['o']; // ö
1951 lastWidths[220] = lastWidths['U']; // Ü
1952 lastWidths[252] = lastWidths['u']; // ü
1953 lastWidths[223] = lastWidths[251]; // ß
1954 }
1955
1956 /* JC: calculate UnderlineThickness/UnderlinePosition */
1957 {
1958 // VS: dirty, but is there any better solution?
1959 double *pt;
1960 pt = (double*) &m_underlinePosition;
1961 *pt = UnderlinePosition * fontToUse->GetPointSize() / 1000.0f;
1962 pt = (double*) &m_underlineThickness;
1963 *pt = UnderlineThickness * fontToUse->GetPointSize() / 1000.0f * m_scaleFactor;
1964 }
1965
1966 /* 3. now the font metrics are read in, calc size this
1967 / is done by adding the widths of the characters in the
1968 / string. they are given in 1/1000 of the size! */
1969
1970 double widthSum=0;
1971 wxCoord height=Size; /* by default */
1972 unsigned char *p;
1973 for(p=(unsigned char *)strbuf; *p; p++)
1974 {
1975 if(lastWidths[*p]== INT_MIN)
1976 {
1977 wxLogDebug("GetTextExtent: undefined width for character '%c' (%d)\n", *p,*p);
1978 widthSum += /*(wxCoord)*/(lastWidths[' ']/1000.0F * Size); /* assume space */
1979 }
1980 else
1981 {
1982 widthSum += /*(wxCoord)*/((lastWidths[*p]/1000.0F)*Size);
1983 }
1984 }
1985
1986 /* add descender to height (it is usually a negative value) */
1987 if (lastDescender != INT_MIN)
1988 {
1989 height += (wxCoord)(((-lastDescender)/1000.0F) * Size); /* MATTHEW: forgot scale */
1990 }
1991
1992 /* return size values */
1993 if ( x )
1994 *x = (wxCoord)widthSum;
1995 if ( y )
1996 *y = height;
1997
1998 /* return other parameters */
1999 if (descent)
2000 {
2001 if(lastDescender!=INT_MIN)
2002 {
2003 *descent = (wxCoord)(((-lastDescender)/1000.0F) * Size); /* MATTHEW: forgot scale */
2004 }
2005 else
2006 {
2007 *descent = 0;
2008 }
2009 }
2010
2011 /* currently no idea how to calculate this! */
2012 if (externalLeading) *externalLeading = 0;
2013
2014 #endif
2015 }
2016
2017 // Determine the Default Postscript Previewer
2018 // available on the platform
2019 #if defined(__SUN__) && defined(__XVIEW__)
2020 // OpenWindow/NeWS's Postscript Previewer
2021 # define PS_VIEWER_PROG "pageview"
2022 #elif defined(__VMS__)
2023 #define PS_VIEWER_PROG "view/format=ps/select=x_display"
2024 #elif defined(__SGI__)
2025 // SGI's Display Postscript Previewer
2026 //# define PS_VIEWER_PROG "dps"
2027 # define PS_VIEWER_PROG "xpsview"
2028 #elif defined(__X__) || defined(__WXGTK__)
2029 // Front-end to ghostscript
2030 # define PS_VIEWER_PROG "ghostview"
2031 #else
2032 // Windows ghostscript/ghostview
2033 # define PS_VIEWER_PROG "gsview"
2034 #endif
2035
2036 wxPrintSetupData *wxThePrintSetupData = (wxPrintSetupData *) NULL;
2037
2038 #if !USE_SHARED_LIBRARY
2039 IMPLEMENT_DYNAMIC_CLASS(wxPostScriptDC, wxDC)
2040 IMPLEMENT_DYNAMIC_CLASS(wxPrintSetupData, wxObject)
2041 #endif
2042
2043 // Redundant now I think
2044 #if 1
2045 IMPLEMENT_CLASS(wxPostScriptPrintDialog, wxDialog)
2046
2047 wxPostScriptPrintDialog::wxPostScriptPrintDialog (wxWindow *parent, const wxString& title,
2048 const wxPoint& pos, const wxSize& size, wxCoord style):
2049 wxDialog(parent, -1, title, pos, size, style)
2050 {
2051 wxBeginBusyCursor();
2052
2053 char buf[100];
2054 int yPos = 40;
2055 wxString
2056 *orientation = new wxString[2],
2057 *print_modes = new wxString[3];
2058 int features;
2059 wxCoord wx_printer_translate_x, wx_printer_translate_y;
2060 double wx_printer_scale_x, wx_printer_scale_y;
2061
2062 orientation[0] = _("Portrait");
2063 orientation[1] = _("Landscape");
2064
2065 print_modes[0] = _("Send to Printer");
2066 print_modes[1] = _("Print to File");
2067 print_modes[2] = _("Preview Only");
2068
2069
2070
2071 wxButton *okBut = new wxButton (this, wxID_OK, _("OK"), wxPoint(5, 5));
2072 (void) new wxButton (this, wxID_CANCEL, _("Cancel"), wxPoint(40, 5));
2073 okBut->SetDefault();
2074
2075
2076 #if defined(__WXGTK__) || defined (__WXMOTIF__)
2077 (void) new wxStaticText( this, -1, _("Printer Command: "),
2078 wxPoint(5, yPos) );
2079 (void) new wxTextCtrl( this, wxID_PRINTER_COMMAND, wxThePrintSetupData->GetPrinterCommand(),
2080 wxPoint(100, yPos), wxSize(100, -1) );
2081
2082 (void) new wxStaticText( this, -1, _("Printer Options: "),
2083 wxPoint(210, yPos) );
2084 (void) new wxTextCtrl( this, wxID_PRINTER_OPTIONS, wxThePrintSetupData->GetPrinterOptions(),
2085 wxPoint(305, yPos), wxSize(150, -1) );
2086
2087 yPos += 40;
2088 #endif
2089
2090
2091 wxRadioBox *radio0 = new wxRadioBox(this, wxID_PRINTER_ORIENTATION, "Orientation: ", wxPoint(5, yPos), wxSize(-1,-1),
2092 2,orientation,2,wxRA_SPECIFY_ROWS);
2093 radio0->SetSelection((int)wxThePrintSetupData->GetPrinterOrientation() - 1);
2094
2095 // @@@ Configuration hook
2096 if ( !wxThePrintSetupData->GetPrintPreviewCommand() )
2097 wxThePrintSetupData->SetPrintPreviewCommand(PS_VIEWER_PROG);
2098
2099 // wxGetResource ("wxWindows", "PSView", &wxThePrintSetupData->m_previewCommand);
2100
2101 features = (wxThePrintSetupData->GetPrintPreviewCommand() &&
2102 *wxThePrintSetupData->GetPrintPreviewCommand()) ? 3 : 2;
2103
2104 wxRadioBox *radio1 = new wxRadioBox(this, wxID_PRINTER_MODES, _("PostScript:"),
2105 wxPoint(150, yPos),
2106 wxSize(-1,-1), features,
2107 print_modes, features, wxRA_SPECIFY_ROWS);
2108
2109 #ifdef __WXMSW__
2110 radio1->Enable(0, FALSE);
2111 if (wxThePrintSetupData->GetPrintPreviewCommand() && *wxThePrintSetupData->GetPrintPreviewCommand())
2112 radio1->Enable(2, FALSE);
2113 #endif
2114
2115 radio1->SetSelection((int)wxThePrintSetupData->GetPrinterMode());
2116 wxThePrintSetupData->GetPrinterTranslation(&wx_printer_translate_x, &wx_printer_translate_y);
2117 wxThePrintSetupData->GetPrinterScaling(&wx_printer_scale_x, &wx_printer_scale_y);
2118
2119 sprintf (buf, "%.2f", wx_printer_scale_x);
2120
2121 yPos += 90;
2122 (void) new wxStaticText(this, -1, _("X Scaling"), wxPoint(5, yPos));
2123 /* wxTextCtrl *text1 = */ (void) new wxTextCtrl(this, wxID_PRINTER_X_SCALE, buf, wxPoint(100, yPos), wxSize(100, -1));
2124
2125 sprintf (buf, "%.2f", wx_printer_scale_y);
2126 (void) new wxStaticText(this, -1, _("Y Scaling"), wxPoint(220, yPos));
2127 /* wxTextCtrl *text2 = */ (void) new wxTextCtrl(this, wxID_PRINTER_Y_SCALE, buf, wxPoint(320, yPos), wxSize(100, -1));
2128
2129 yPos += 25;
2130
2131 (void) new wxStaticText(this, -1, _("X Translation"), wxPoint(5, yPos));
2132 sprintf (buf, "%.2d", wx_printer_translate_x);
2133 /* wxTextCtrl *text3 = */ (void) new wxTextCtrl(this, wxID_PRINTER_X_TRANS, buf, wxPoint(100, yPos), wxSize(100, -1));
2134
2135 (void) new wxStaticText(this, -1, _("Y Translation"), wxPoint(220, yPos));
2136 sprintf (buf, "%.2d", wx_printer_translate_y);
2137 /* wxTextCtrl *text4 = */ (void) new wxTextCtrl(this, wxID_PRINTER_Y_TRANS, buf, wxPoint(320, yPos), wxSize(100, -1));
2138
2139 Fit ();
2140
2141 delete[] orientation;
2142 delete[] print_modes;
2143
2144 wxEndBusyCursor();
2145 }
2146
2147 int wxPostScriptPrintDialog::ShowModal ()
2148 {
2149 if ( wxDialog::ShowModal() == wxID_OK )
2150 {
2151 // wxTextCtrl *text0 = (wxTextCtrl *)FindWindow(wxID_PRINTER_OPTIONS);
2152 wxTextCtrl *text1 = (wxTextCtrl *)FindWindow(wxID_PRINTER_X_SCALE);
2153 wxTextCtrl *text2 = (wxTextCtrl *)FindWindow(wxID_PRINTER_Y_SCALE);
2154 wxTextCtrl *text3 = (wxTextCtrl *)FindWindow(wxID_PRINTER_X_TRANS);
2155 wxTextCtrl *text4 = (wxTextCtrl *)FindWindow(wxID_PRINTER_Y_TRANS);
2156 // wxTextCtrl *text_prt = (wxTextCtrl *)FindWindow(wxID_PRINTER_COMMAND);
2157 wxRadioBox *radio0 = (wxRadioBox *)FindWindow(wxID_PRINTER_ORIENTATION);
2158 wxRadioBox *radio1 = (wxRadioBox *)FindWindow(wxID_PRINTER_MODES);
2159
2160 StringToDouble (WXSTRINGCAST text1->GetValue (), &wxThePrintSetupData->m_printerScaleX);
2161 StringToDouble (WXSTRINGCAST text2->GetValue (), &wxThePrintSetupData->m_printerScaleY);
2162
2163 long dummy;
2164 StringToLong (WXSTRINGCAST text3->GetValue (), &dummy);
2165 wxThePrintSetupData->m_printerTranslateX = (wxCoord)dummy;
2166 StringToLong (WXSTRINGCAST text4->GetValue (), &dummy);
2167 wxThePrintSetupData->m_printerTranslateY = (wxCoord)dummy;
2168
2169 #ifdef __X__
2170 // wxThePrintSetupData->SetPrinterOptions(WXSTRINGCAST text0->GetValue ());
2171 // wxThePrintSetupData->SetPrinterCommand(WXSTRINGCAST text_prt->GetValue ());
2172 #endif
2173
2174 wxThePrintSetupData->SetPrinterOrientation((radio0->GetSelection() == 1 ? wxLANDSCAPE : wxPORTRAIT));
2175
2176 // C++ wants this
2177 switch ( radio1->GetSelection() ) {
2178 case 0: wxThePrintSetupData->SetPrinterMode(PS_PRINTER); break;
2179 case 1: wxThePrintSetupData->SetPrinterMode(PS_FILE); break;
2180 case 2: wxThePrintSetupData->SetPrinterMode(PS_PREVIEW); break;
2181 }
2182 return wxID_OK;
2183 }
2184 return wxID_CANCEL;
2185 }
2186 #endif
2187 // 0 (redundant)
2188
2189 // PostScript printer settings
2190 // RETAINED FOR BACKWARD COMPATIBILITY
2191 void wxSetPrinterCommand(const wxString& cmd)
2192 {
2193 wxThePrintSetupData->SetPrinterCommand(cmd);
2194 }
2195
2196 void wxSetPrintPreviewCommand(const wxString& cmd)
2197 {
2198 wxThePrintSetupData->SetPrintPreviewCommand(cmd);
2199 }
2200
2201 void wxSetPrinterOptions(const wxString& flags)
2202 {
2203 wxThePrintSetupData->SetPrinterOptions(flags);
2204 }
2205
2206 void wxSetPrinterFile(const wxString& f)
2207 {
2208 wxThePrintSetupData->SetPrinterFile(f);
2209 }
2210
2211 void wxSetPrinterOrientation(int orient)
2212 {
2213 wxThePrintSetupData->SetPrinterOrientation(orient);
2214 }
2215
2216 void wxSetPrinterScaling(double x, double y)
2217 {
2218 wxThePrintSetupData->SetPrinterScaling(x, y);
2219 }
2220
2221 void wxSetPrinterTranslation(wxCoord x, wxCoord y)
2222 {
2223 wxThePrintSetupData->SetPrinterTranslation(x, y);
2224 }
2225
2226 // 1 = Preview, 2 = print to file, 3 = send to printer
2227 void wxSetPrinterMode(int mode)
2228 {
2229 wxThePrintSetupData->SetPrinterMode(mode);
2230 }
2231
2232 void wxSetAFMPath(const wxString& f)
2233 {
2234 wxThePrintSetupData->SetAFMPath(f);
2235 }
2236
2237 // Get current values
2238 wxString wxGetPrinterCommand()
2239 {
2240 return wxThePrintSetupData->GetPrinterCommand();
2241 }
2242
2243 wxString wxGetPrintPreviewCommand()
2244 {
2245 return wxThePrintSetupData->GetPrintPreviewCommand();
2246 }
2247
2248 wxString wxGetPrinterOptions()
2249 {
2250 return wxThePrintSetupData->GetPrinterOptions();
2251 }
2252
2253 wxString wxGetPrinterFile()
2254 {
2255 return wxThePrintSetupData->GetPrinterFile();
2256 }
2257
2258 int wxGetPrinterOrientation()
2259 {
2260 return wxThePrintSetupData->GetPrinterOrientation();
2261 }
2262
2263 void wxGetPrinterScaling(double* x, double* y)
2264 {
2265 wxThePrintSetupData->GetPrinterScaling(x, y);
2266 }
2267
2268 void wxGetPrinterTranslation(wxCoord *x, wxCoord *y)
2269 {
2270 wxThePrintSetupData->GetPrinterTranslation(x, y);
2271 }
2272
2273 int wxGetPrinterMode()
2274 {
2275 return wxThePrintSetupData->GetPrinterMode();
2276 }
2277
2278 wxString wxGetAFMPath()
2279 {
2280 return wxThePrintSetupData->GetAFMPath();
2281 }
2282
2283 /*
2284 * Print setup data
2285 */
2286
2287 wxPrintSetupData::wxPrintSetupData()
2288 {
2289 m_printerOrient = wxPORTRAIT;
2290 m_printerScaleX = (double)1.0;
2291 m_printerScaleY = (double)1.0;
2292 m_printerTranslateX = 0;
2293 m_printerTranslateY = 0;
2294 m_printerMode = wxPRINT_MODE_FILE;
2295 m_printColour = TRUE;
2296 }
2297
2298 wxPrintSetupData::~wxPrintSetupData()
2299 {
2300 }
2301
2302 void wxPrintSetupData::operator=(wxPrintSetupData& data)
2303 {
2304 SetPrinterCommand(data.GetPrinterCommand());
2305 SetPrintPreviewCommand(data.GetPrintPreviewCommand());
2306 SetPrinterOptions(data.GetPrinterOptions());
2307 wxCoord x, y;
2308 data.GetPrinterTranslation(&x, &y);
2309 SetPrinterTranslation(x, y);
2310
2311 double x1, y1;
2312 data.GetPrinterScaling(&x1, &y1);
2313 SetPrinterScaling(x1, y1);
2314
2315 SetPrinterOrientation(data.GetPrinterOrientation());
2316 SetPrinterMode(data.GetPrinterMode());
2317 SetAFMPath(data.GetAFMPath());
2318 SetPaperName(data.GetPaperName());
2319 SetColour(data.GetColour());
2320 }
2321
2322 // Initialize from a wxPrintData object (wxPrintData should now be used instead of wxPrintSetupData).
2323 void wxPrintSetupData::operator=(const wxPrintData& data)
2324 {
2325 SetPrinterCommand(data.GetPrinterCommand());
2326 SetPrintPreviewCommand(data.GetPreviewCommand());
2327 SetPrinterOptions(data.GetPrinterOptions());
2328 SetPrinterTranslation(data.GetPrinterTranslateX(), data.GetPrinterTranslateY());
2329 SetPrinterScaling(data.GetPrinterScaleX(), data.GetPrinterScaleY());
2330 SetPrinterOrientation(data.GetOrientation());
2331 SetPrinterMode((int) data.GetPrintMode());
2332 SetAFMPath(data.GetFontMetricPath());
2333 SetPaperName(wxThePrintPaperDatabase->ConvertIdToName(data.GetPaperId()));
2334 SetColour(data.GetColour());
2335 SetPrinterFile(data.GetFilename());
2336 }
2337
2338 void wxInitializePrintSetupData(bool init)
2339 {
2340 if (init)
2341 {
2342 wxThePrintSetupData = new wxPrintSetupData;
2343
2344 wxThePrintSetupData->SetPrintPreviewCommand(PS_VIEWER_PROG);
2345 wxThePrintSetupData->SetPrinterOrientation(wxPORTRAIT);
2346 wxThePrintSetupData->SetPrinterMode(wxPRINT_MODE_PREVIEW);
2347 wxThePrintSetupData->SetPaperName(_("A4 sheet, 210 x 297 mm"));
2348
2349 // Could have a .ini file to read in some defaults
2350 // - and/or use environment variables, e.g. WXWIN
2351 #ifdef __VMS__
2352 wxThePrintSetupData->SetPrinterCommand("print");
2353 wxThePrintSetupData->SetPrinterOptions("/nonotify/queue=psqueue");
2354 wxThePrintSetupData->SetAFMPath("sys$ps_font_metrics:");
2355 #endif
2356 #ifdef __WXMSW__
2357 wxThePrintSetupData->SetPrinterCommand("print");
2358 wxThePrintSetupData->SetAFMPath("c:\\windows\\system\\");
2359 wxThePrintSetupData->SetPrinterOptions("");
2360 #endif
2361 #if !defined(__VMS__) && !defined(__WXMSW__)
2362 wxThePrintSetupData->SetPrinterCommand("lpr");
2363 wxThePrintSetupData->SetPrinterOptions("");
2364 wxThePrintSetupData->SetAFMPath("");
2365 #endif
2366 }
2367 else
2368 {
2369 if (wxThePrintSetupData)
2370 delete wxThePrintSetupData;
2371 wxThePrintSetupData = (wxPrintSetupData *) NULL;
2372 }
2373 }
2374
2375 // A module to allow initialization/cleanup of PostScript-related
2376 // things without calling these functions from app.cpp.
2377
2378 class WXDLLEXPORT wxPostScriptModule: public wxModule
2379 {
2380 DECLARE_DYNAMIC_CLASS(wxPostScriptModule)
2381 public:
2382 wxPostScriptModule() {}
2383 bool OnInit();
2384 void OnExit();
2385 };
2386
2387 IMPLEMENT_DYNAMIC_CLASS(wxPostScriptModule, wxModule)
2388
2389 /*
2390 * Initialization/cleanup module
2391 */
2392
2393 bool wxPostScriptModule::OnInit()
2394 {
2395 wxInitializePrintSetupData();
2396
2397 return TRUE;
2398 }
2399
2400 void wxPostScriptModule::OnExit()
2401 {
2402 wxInitializePrintSetupData(FALSE);
2403 }
2404
2405 #endif
2406 // wxUSE_POSTSCRIPT
2407
2408 #endif
2409 // wxUSE_PRINTING_ARCHITECTURE