]> git.saurik.com Git - wxWidgets.git/blob - src/generic/dcpsg.cpp
Added support for outlines printing under wxGTK. I still
[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 "dcpsg.h"
14 #endif
15
16 #include "wx/wxprec.h"
17
18 #ifdef __BORLANDC__
19 #pragma hdrstop
20 #endif
21
22 #ifndef WX_PRECOMP
23 #endif // WX_PRECOMP
24
25 #if wxUSE_PRINTING_ARCHITECTURE
26
27 #if wxUSE_POSTSCRIPT
28
29 #include "wx/setup.h"
30
31 #include "wx/window.h"
32 #include "wx/dcmemory.h"
33 #include "wx/utils.h"
34 #include "wx/intl.h"
35 #include "wx/filedlg.h"
36 #include "wx/app.h"
37 #include "wx/msgdlg.h"
38 #include "wx/image.h"
39 #include "wx/log.h"
40 #include "wx/generic/dcpsg.h"
41 #include "wx/printdlg.h"
42 #include "wx/button.h"
43 #include "wx/stattext.h"
44 #include "wx/radiobox.h"
45 #include "wx/textctrl.h"
46 #include "wx/prntbase.h"
47 #include "wx/paper.h"
48 #include "wx/filefn.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 *wxPostScriptHeaderConicTo = "\
77 /conicto {\n\
78 /to_y exch def\n\
79 /to_x exch def\n\
80 /conic_cntrl_y exch def\n\
81 /conic_cntrl_x exch def\n\
82 currentpoint\n\
83 /p0_y exch def\n\
84 /p0_x exch def\n\
85 /p1_x p0_x conic_cntrl_x p0_x sub 2 3 div mul add def\n\
86 /p1_y p0_y conic_cntrl_y p0_y sub 2 3 div mul add def\n\
87 /p2_x p1_x to_x p0_x sub 1 3 div mul add def\n\
88 /p2_y p1_y to_y p0_y sub 1 3 div mul add def\n\
89 p1_x p1_y p2_x p2_y to_x to_y curveto\n\
90 } bind def\n\
91 /start_ol { gsave 1.0 72 div dup scale } bind def\n\
92 /end_ol { closepath fill grestore } bind def\n\
93 ";
94
95 static const char *wxPostScriptHeaderEllipse = "\
96 /ellipsedict 8 dict def\n\
97 ellipsedict /mtrx matrix put\n\
98 /ellipse {\n\
99 ellipsedict begin\n\
100 /endangle exch def\n\
101 /startangle exch def\n\
102 /yrad exch def\n\
103 /xrad exch def\n\
104 /y exch def\n\
105 /x exch def\n\
106 /savematrix mtrx currentmatrix def\n\
107 x y translate\n\
108 xrad yrad scale\n\
109 0 0 1 startangle endangle arc\n\
110 savematrix setmatrix\n\
111 end\n\
112 } def\n\
113 ";
114
115 static const char *wxPostScriptHeaderEllipticArc= "\
116 /ellipticarcdict 8 dict def\n\
117 ellipticarcdict /mtrx matrix put\n\
118 /ellipticarc\n\
119 { ellipticarcdict begin\n\
120 /do_fill exch def\n\
121 /endangle exch def\n\
122 /startangle exch def\n\
123 /yrad exch def\n\
124 /xrad exch def \n\
125 /y exch def\n\
126 /x exch def\n\
127 /savematrix mtrx currentmatrix def\n\
128 x y translate\n\
129 xrad yrad scale\n\
130 do_fill { 0 0 moveto } if\n\
131 0 0 1 startangle endangle arc\n\
132 savematrix setmatrix\n\
133 do_fill { fill }{ stroke } ifelse\n\
134 end\n\
135 } def\n";
136
137 static const char *wxPostScriptHeaderSpline = "\
138 /DrawSplineSection {\n\
139 /y3 exch def\n\
140 /x3 exch def\n\
141 /y2 exch def\n\
142 /x2 exch def\n\
143 /y1 exch def\n\
144 /x1 exch def\n\
145 /xa x1 x2 x1 sub 0.666667 mul add def\n\
146 /ya y1 y2 y1 sub 0.666667 mul add def\n\
147 /xb x3 x2 x3 sub 0.666667 mul add def\n\
148 /yb y3 y2 y3 sub 0.666667 mul add def\n\
149 x1 y1 lineto\n\
150 xa ya xb yb x3 y3 curveto\n\
151 } def\n\
152 ";
153
154 static const char *wxPostScriptHeaderColourImage = "\
155 %% define 'colorimage' if it isn't defined\n\
156 %% ('colortogray' and 'mergeprocs' come from xwd2ps\n\
157 %% via xgrab)\n\
158 /colorimage where %% do we know about 'colorimage'?\n\
159 { pop } %% yes: pop off the 'dict' returned\n\
160 { %% no: define one\n\
161 /colortogray { %% define an RGB->I function\n\
162 /rgbdata exch store %% call input 'rgbdata'\n\
163 rgbdata length 3 idiv\n\
164 /npixls exch store\n\
165 /rgbindx 0 store\n\
166 0 1 npixls 1 sub {\n\
167 grays exch\n\
168 rgbdata rgbindx get 20 mul %% Red\n\
169 rgbdata rgbindx 1 add get 32 mul %% Green\n\
170 rgbdata rgbindx 2 add get 12 mul %% Blue\n\
171 add add 64 idiv %% I = .5G + .31R + .18B\n\
172 put\n\
173 /rgbindx rgbindx 3 add store\n\
174 } for\n\
175 grays 0 npixls getinterval\n\
176 } bind def\n\
177 \n\
178 %% Utility procedure for colorimage operator.\n\
179 %% This procedure takes two procedures off the\n\
180 %% stack and merges them into a single procedure.\n\
181 \n\
182 /mergeprocs { %% def\n\
183 dup length\n\
184 3 -1 roll\n\
185 dup\n\
186 length\n\
187 dup\n\
188 5 1 roll\n\
189 3 -1 roll\n\
190 add\n\
191 array cvx\n\
192 dup\n\
193 3 -1 roll\n\
194 0 exch\n\
195 putinterval\n\
196 dup\n\
197 4 2 roll\n\
198 putinterval\n\
199 } bind def\n\
200 \n\
201 /colorimage { %% def\n\
202 pop pop %% remove 'false 3' operands\n\
203 {colortogray} mergeprocs\n\
204 image\n\
205 } bind def\n\
206 } ifelse %% end of 'false' case\n\
207 ";
208
209 #ifndef __WXGTK20__
210 static char wxPostScriptHeaderReencodeISO1[] =
211 "\n/reencodeISO {\n"
212 "dup dup findfont dup length dict begin\n"
213 "{ 1 index /FID ne { def }{ pop pop } ifelse } forall\n"
214 "/Encoding ISOLatin1Encoding def\n"
215 "currentdict end definefont\n"
216 "} def\n"
217 "/ISOLatin1Encoding [\n"
218 "/.notdef/.notdef/.notdef/.notdef/.notdef/.notdef/.notdef/.notdef\n"
219 "/.notdef/.notdef/.notdef/.notdef/.notdef/.notdef/.notdef/.notdef\n"
220 "/.notdef/.notdef/.notdef/.notdef/.notdef/.notdef/.notdef/.notdef\n"
221 "/.notdef/.notdef/.notdef/.notdef/.notdef/.notdef/.notdef/.notdef\n"
222 "/space/exclam/quotedbl/numbersign/dollar/percent/ampersand/quoteright\n"
223 "/parenleft/parenright/asterisk/plus/comma/minus/period/slash\n"
224 "/zero/one/two/three/four/five/six/seven/eight/nine/colon/semicolon\n"
225 "/less/equal/greater/question/at/A/B/C/D/E/F/G/H/I/J/K/L/M/N\n"
226 "/O/P/Q/R/S/T/U/V/W/X/Y/Z/bracketleft/backslash/bracketright\n"
227 "/asciicircum/underscore/quoteleft/a/b/c/d/e/f/g/h/i/j/k/l/m\n"
228 "/n/o/p/q/r/s/t/u/v/w/x/y/z/braceleft/bar/braceright/asciitilde\n"
229 "/.notdef/.notdef/.notdef/.notdef/.notdef/.notdef/.notdef/.notdef\n"
230 "/.notdef/.notdef/.notdef/.notdef/.notdef/.notdef/.notdef/.notdef\n"
231 "/.notdef/dotlessi/grave/acute/circumflex/tilde/macron/breve\n"
232 "/dotaccent/dieresis/.notdef/ring/cedilla/.notdef/hungarumlaut\n";
233
234 static char wxPostScriptHeaderReencodeISO2[] =
235 "/ogonek/caron/space/exclamdown/cent/sterling/currency/yen/brokenbar\n"
236 "/section/dieresis/copyright/ordfeminine/guillemotleft/logicalnot\n"
237 "/hyphen/registered/macron/degree/plusminus/twosuperior/threesuperior\n"
238 "/acute/mu/paragraph/periodcentered/cedilla/onesuperior/ordmasculine\n"
239 "/guillemotright/onequarter/onehalf/threequarters/questiondown\n"
240 "/Agrave/Aacute/Acircumflex/Atilde/Adieresis/Aring/AE/Ccedilla\n"
241 "/Egrave/Eacute/Ecircumflex/Edieresis/Igrave/Iacute/Icircumflex\n"
242 "/Idieresis/Eth/Ntilde/Ograve/Oacute/Ocircumflex/Otilde/Odieresis\n"
243 "/multiply/Oslash/Ugrave/Uacute/Ucircumflex/Udieresis/Yacute\n"
244 "/Thorn/germandbls/agrave/aacute/acircumflex/atilde/adieresis\n"
245 "/aring/ae/ccedilla/egrave/eacute/ecircumflex/edieresis/igrave\n"
246 "/iacute/icircumflex/idieresis/eth/ntilde/ograve/oacute/ocircumflex\n"
247 "/otilde/odieresis/divide/oslash/ugrave/uacute/ucircumflex/udieresis\n"
248 "/yacute/thorn/ydieresis\n"
249 "] def\n\n";
250 #endif
251
252 //-------------------------------------------------------------------------------
253 // wxPostScriptDC
254 //-------------------------------------------------------------------------------
255
256 float wxPostScriptDC::ms_PSScaleFactor = 10.0;
257
258 void wxPostScriptDC::SetResolution(int ppi)
259 {
260 ms_PSScaleFactor = (float)ppi / 72.0;
261 }
262
263 int wxPostScriptDC::GetResolution()
264 {
265 return (int)(ms_PSScaleFactor * 72.0);
266 }
267
268
269
270 wxPostScriptDC::wxPostScriptDC ()
271 {
272 m_pstream = (FILE*) NULL;
273
274 m_currentRed = 0;
275 m_currentGreen = 0;
276 m_currentBlue = 0;
277
278 m_pageNumber = 0;
279
280 m_clipping = FALSE;
281
282 m_underlinePosition = 0.0;
283 m_underlineThickness = 0.0;
284
285 m_signX = 1; // default x-axis left to right
286 m_signY = -1; // default y-axis bottom up -> top down
287
288 // Compatibility only
289 // HH: Doesn't seem to work for wxMSW...
290 #if !defined(__WXMSW__)
291 m_printData = * wxThePrintSetupData;
292 #endif
293 }
294
295 wxPostScriptDC::wxPostScriptDC (const wxString& file, bool interactive, wxWindow *parent)
296 {
297 m_pstream = (FILE*) NULL;
298
299 m_currentRed = 0;
300 m_currentGreen = 0;
301 m_currentBlue = 0;
302
303 m_pageNumber = 0;
304
305 m_clipping = FALSE;
306
307 m_underlinePosition = 0.0;
308 m_underlineThickness = 0.0;
309
310 m_signX = 1; // default x-axis left to right
311 m_signY = -1; // default y-axis bottom up -> top down
312
313 Create(file, interactive, parent);
314 }
315
316 bool wxPostScriptDC::Create(const wxString& file, bool interactive, wxWindow *parent)
317 {
318 m_isInteractive = interactive;
319
320 m_title = "";
321 m_printData.SetFilename(file);
322
323 #ifdef __WXMSW__
324 // Can only send to file in Windows
325 m_printData.SetPrintMode(wxPRINT_MODE_FILE);
326 #endif
327
328 if (m_isInteractive)
329 {
330 if ((m_ok = PrinterDialog (parent) ) == FALSE) return FALSE;
331 }
332 else
333 {
334 m_ok = TRUE;
335 }
336
337 return m_ok;
338 }
339
340 wxPostScriptDC::wxPostScriptDC (const wxPrintData& printData)
341 {
342 m_pstream = (FILE*) NULL;
343
344 m_currentRed = 0;
345 m_currentGreen = 0;
346 m_currentBlue = 0;
347
348 m_pageNumber = 0;
349
350 m_clipping = FALSE;
351
352 m_underlinePosition = 0.0;
353 m_underlineThickness = 0.0;
354
355 m_signX = 1; // default x-axis left to right
356 m_signY = -1; // default y-axis bottom up -> top down
357
358 m_printData = printData;
359
360 m_ok = TRUE;
361 }
362
363 wxPostScriptDC::~wxPostScriptDC ()
364 {
365 if (m_pstream)
366 {
367 fclose( m_pstream );
368 m_pstream = (FILE*) NULL;
369 }
370 }
371
372 bool wxPostScriptDC::Ok() const
373 {
374 return m_ok;
375 }
376
377 // This dialog is deprecated now: use wxGenericPrintDialog or the printing framework
378 bool wxPostScriptDC::PrinterDialog(wxWindow *parent)
379 {
380 wxPostScriptPrintDialog dialog( parent, _("Printer Settings"), wxPoint(150, 150), wxSize(400, 400),
381 wxDEFAULT_DIALOG_STYLE | wxDIALOG_MODAL );
382 m_ok = (dialog.ShowModal () == wxID_OK);
383
384 if (!m_ok) return FALSE;
385
386 if ((m_printData.GetFilename() == "") &&
387 (m_printData.GetPrintMode() == wxPRINT_MODE_PREVIEW ||
388 m_printData.GetPrintMode() == wxPRINT_MODE_PRINTER))
389 {
390 // steve, 05.09.94
391 #ifdef __VMS__
392 m_printData.SetFilename("preview");
393 #else
394 // For PS_PRINTER action this depends on a Unix-style print spooler
395 // since the wx_printer_file can be destroyed during a session
396 // @@@ TODO: a Windows-style answer for non-Unix
397 wxChar userId[256];
398 wxGetUserId (userId, sizeof (userId) / sizeof (char));
399 wxChar tmp[256];
400 wxStrcpy (tmp, wxT("/tmp/preview_"));
401 wxStrcat (tmp, userId);
402 m_printData.SetFilename(tmp);
403 #endif
404 wxChar tmp2[256];
405 wxStrcpy(tmp2, m_printData.GetFilename());
406 wxStrcat (tmp2, wxT(".ps"));
407 m_printData.SetFilename(tmp2);
408 }
409 else if ((m_printData.GetFilename() == wxT("")) && (m_printData.GetPrintMode() == wxPRINT_MODE_FILE))
410 {
411 wxString file = wxSaveFileSelector (_("PostScript"), wxT("ps"));
412 if ( file.IsEmpty() )
413 {
414 m_ok = FALSE;
415 return FALSE;
416 }
417
418 m_printData.SetFilename(file);
419 m_ok = TRUE;
420 }
421
422 return m_ok;
423 }
424
425 void wxPostScriptDC::DoSetClippingRegion (wxCoord x, wxCoord y, wxCoord w, wxCoord h)
426 {
427 wxCHECK_RET( m_ok && m_pstream, wxT("invalid postscript dc") );
428
429 if (m_clipping) DestroyClippingRegion();
430
431 wxDC::DoSetClippingRegion(x, y, w, h);
432
433 m_clipping = TRUE;
434 fprintf( m_pstream,
435 "gsave\n newpath\n"
436 "%d %d moveto\n"
437 "%d %d lineto\n"
438 "%d %d lineto\n"
439 "%d %d lineto\n"
440 "closepath clip newpath\n",
441 LogicalToDeviceX(x), LogicalToDeviceY(y),
442 LogicalToDeviceX(x+w), LogicalToDeviceY(y),
443 LogicalToDeviceX(x+w), LogicalToDeviceY(y+h),
444 LogicalToDeviceX(x), LogicalToDeviceY(y+h) );
445 }
446
447
448 void wxPostScriptDC::DestroyClippingRegion()
449 {
450 wxCHECK_RET( m_ok && m_pstream, wxT("invalid postscript dc") );
451
452 if (m_clipping)
453 {
454 m_clipping = FALSE;
455 fprintf( m_pstream, "grestore\n" );
456 }
457
458 wxDC::DestroyClippingRegion();
459 }
460
461 void wxPostScriptDC::Clear()
462 {
463 wxFAIL_MSG( wxT("wxPostScriptDC::Clear not implemented.") );
464 }
465
466 bool wxPostScriptDC::DoFloodFill (wxCoord WXUNUSED(x), wxCoord WXUNUSED(y), const wxColour &WXUNUSED(col), int WXUNUSED(style))
467 {
468 wxFAIL_MSG( wxT("wxPostScriptDC::FloodFill not implemented.") );
469 return FALSE;
470 }
471
472 bool wxPostScriptDC::DoGetPixel (wxCoord WXUNUSED(x), wxCoord WXUNUSED(y), wxColour * WXUNUSED(col)) const
473 {
474 wxFAIL_MSG( wxT("wxPostScriptDC::GetPixel not implemented.") );
475 return FALSE;
476 }
477
478 void wxPostScriptDC::DoCrossHair (wxCoord WXUNUSED(x), wxCoord WXUNUSED(y))
479 {
480 wxFAIL_MSG( wxT("wxPostScriptDC::CrossHair not implemented.") );
481 }
482
483 void wxPostScriptDC::DoDrawLine (wxCoord x1, wxCoord y1, wxCoord x2, wxCoord y2)
484 {
485 wxCHECK_RET( m_ok && m_pstream, wxT("invalid postscript dc") );
486
487 if (m_pen.GetStyle() == wxTRANSPARENT) return;
488
489 SetPen( m_pen );
490
491 fprintf( m_pstream,
492 "newpath\n"
493 "%d %d moveto\n"
494 "%d %d lineto\n"
495 "stroke\n",
496 LogicalToDeviceX(x1), LogicalToDeviceY(y1),
497 LogicalToDeviceX(x2), LogicalToDeviceY (y2) );
498
499 CalcBoundingBox( x1, y1 );
500 CalcBoundingBox( x2, y2 );
501 }
502
503 #define RAD2DEG 57.29577951308
504
505 void wxPostScriptDC::DoDrawArc (wxCoord x1, wxCoord y1, wxCoord x2, wxCoord y2, wxCoord xc, wxCoord yc)
506 {
507 wxCHECK_RET( m_ok && m_pstream, wxT("invalid postscript dc") );
508
509 wxCoord dx = x1 - xc;
510 wxCoord dy = y1 - yc;
511 wxCoord radius = (wxCoord) sqrt( (double)(dx*dx+dy*dy) );
512 double alpha1, alpha2;
513
514 if (x1 == x2 && y1 == y2)
515 {
516 alpha1 = 0.0;
517 alpha2 = 360.0;
518 }
519 else if (radius == 0.0)
520 {
521 alpha1 = alpha2 = 0.0;
522 }
523 else
524 {
525 alpha1 = (x1 - xc == 0) ?
526 (y1 - yc < 0) ? 90.0 : -90.0 :
527 -atan2(double(y1-yc), double(x1-xc)) * RAD2DEG;
528 alpha2 = (x2 - xc == 0) ?
529 (y2 - yc < 0) ? 90.0 : -90.0 :
530 -atan2(double(y2-yc), double(x2-xc)) * RAD2DEG;
531 }
532 while (alpha1 <= 0) alpha1 += 360;
533 while (alpha2 <= 0) alpha2 += 360; // adjust angles to be between
534 while (alpha1 > 360) alpha1 -= 360; // 0 and 360 degree
535 while (alpha2 > 360) alpha2 -= 360;
536
537 if (m_brush.GetStyle() != wxTRANSPARENT)
538 {
539 SetBrush( m_brush );
540
541 fprintf( m_pstream,
542 "newpath\n"
543 "%d %d %d %d %d %d ellipse\n"
544 "%d %d lineto\n"
545 "closepath\n"
546 "fill\n",
547 LogicalToDeviceX(xc), LogicalToDeviceY(yc), LogicalToDeviceXRel(radius), LogicalToDeviceYRel(radius), (wxCoord)alpha1, (wxCoord) alpha2,
548 LogicalToDeviceX(xc), LogicalToDeviceY(yc) );
549
550 CalcBoundingBox( xc-radius, yc-radius );
551 CalcBoundingBox( xc+radius, yc+radius );
552 }
553
554 if (m_pen.GetStyle() != wxTRANSPARENT)
555 {
556 SetPen( m_pen );
557
558 fprintf( m_pstream,
559 "newpath\n"
560 "%d %d %d %d %d %d ellipse\n"
561 "%d %d lineto\n"
562 "stroke\n"
563 "fill\n",
564 LogicalToDeviceX(xc), LogicalToDeviceY(yc), LogicalToDeviceXRel(radius), LogicalToDeviceYRel(radius), (wxCoord)alpha1, (wxCoord) alpha2,
565 LogicalToDeviceX(xc), LogicalToDeviceY(yc) );
566
567 CalcBoundingBox( xc-radius, yc-radius );
568 CalcBoundingBox( xc+radius, yc+radius );
569 }
570 }
571
572 void wxPostScriptDC::DoDrawEllipticArc(wxCoord x,wxCoord y,wxCoord w,wxCoord h,double sa,double ea)
573 {
574 wxCHECK_RET( m_ok && m_pstream, wxT("invalid postscript dc") );
575
576 if (sa>=360 || sa<=-360) sa=sa-int(sa/360)*360;
577 if (ea>=360 || ea<=-360) ea=ea-int(ea/360)*360;
578 if (sa<0) sa+=360;
579 if (ea<0) ea+=360;
580
581 if (sa==ea)
582 {
583 DrawEllipse(x,y,w,h);
584 return;
585 }
586
587 if (m_brush.GetStyle () != wxTRANSPARENT)
588 {
589 SetBrush( m_brush );
590
591 fprintf( m_pstream,
592 "newpath\n"
593 "%d %d %d %d %d %d true ellipticarc\n",
594 LogicalToDeviceX(x+w/2), LogicalToDeviceY(y+h/2), LogicalToDeviceXRel(w/2), LogicalToDeviceYRel(h/2), (wxCoord)sa, (wxCoord)ea );
595
596 CalcBoundingBox( x ,y );
597 CalcBoundingBox( x+w, y+h );
598 }
599
600 if (m_pen.GetStyle () != wxTRANSPARENT)
601 {
602 SetPen( m_pen );
603
604 fprintf(m_pstream,
605 "newpath\n"
606 "%d %d %d %d %d %d false ellipticarc\n",
607 LogicalToDeviceX(x+w/2), LogicalToDeviceY(y+h/2), LogicalToDeviceXRel(w/2), LogicalToDeviceYRel(h/2), (wxCoord)sa, (wxCoord)ea );
608
609 CalcBoundingBox( x ,y );
610 CalcBoundingBox( x+w, y+h );
611 }
612 }
613
614 void wxPostScriptDC::DoDrawPoint (wxCoord x, wxCoord y)
615 {
616 wxCHECK_RET( m_ok && m_pstream, wxT("invalid postscript dc") );
617
618 if (m_pen.GetStyle() == wxTRANSPARENT) return;
619
620 SetPen (m_pen);
621
622 fprintf( m_pstream,
623 "newpath\n"
624 "%d %d moveto\n"
625 "%d %d lineto\n"
626 "stroke\n",
627 LogicalToDeviceX(x), LogicalToDeviceY(y),
628 LogicalToDeviceX(x+1), LogicalToDeviceY(y) );
629
630 CalcBoundingBox( x, y );
631 }
632
633 void wxPostScriptDC::DoDrawPolygon (int n, wxPoint points[], wxCoord xoffset, wxCoord yoffset, int WXUNUSED(fillStyle))
634 {
635 wxCHECK_RET( m_ok && m_pstream, wxT("invalid postscript dc") );
636
637 if (n <= 0) return;
638
639 if (m_brush.GetStyle () != wxTRANSPARENT)
640 {
641 SetBrush( m_brush );
642
643 fprintf( m_pstream, "newpath\n" );
644
645 wxCoord xx = LogicalToDeviceX(points[0].x + xoffset);
646 wxCoord yy = LogicalToDeviceY(points[0].y + yoffset);
647
648 fprintf( m_pstream, "%d %d moveto\n", xx, yy );
649
650 CalcBoundingBox( points[0].x + xoffset, points[0].y + yoffset );
651
652 for (int i = 1; i < n; i++)
653 {
654 xx = LogicalToDeviceX(points[i].x + xoffset);
655 yy = LogicalToDeviceY(points[i].y + yoffset);
656
657 fprintf( m_pstream, "%d %d lineto\n", xx, yy );
658
659 CalcBoundingBox( points[i].x + xoffset, points[i].y + yoffset);
660 }
661
662 fprintf( m_pstream, "fill\n" );
663 }
664
665 if (m_pen.GetStyle () != wxTRANSPARENT)
666 {
667 SetPen( m_pen );
668
669 fprintf( m_pstream, "newpath\n" );
670
671 wxCoord xx = LogicalToDeviceX(points[0].x + xoffset);
672 wxCoord yy = LogicalToDeviceY(points[0].y + yoffset);
673
674 fprintf( m_pstream, "%d %d moveto\n", xx, yy );
675
676 CalcBoundingBox( points[0].x + xoffset, points[0].y + yoffset );
677
678 for (int i = 1; i < n; i++)
679 {
680 xx = LogicalToDeviceX(points[i].x + xoffset);
681 yy = LogicalToDeviceY(points[i].y + yoffset);
682
683 fprintf( m_pstream, "%d %d lineto\n", xx, yy );
684
685 CalcBoundingBox( points[i].x + xoffset, points[i].y + yoffset);
686 }
687
688 fprintf( m_pstream, "closepath\n" );
689 fprintf( m_pstream, "stroke\n" );
690 }
691 }
692
693 void wxPostScriptDC::DoDrawLines (int n, wxPoint points[], wxCoord xoffset, wxCoord yoffset)
694 {
695 wxCHECK_RET( m_ok && m_pstream, wxT("invalid postscript dc") );
696
697 if (m_pen.GetStyle() == wxTRANSPARENT) return;
698
699 if (n <= 0) return;
700
701 SetPen (m_pen);
702
703 int i;
704 for ( i =0; i<n ; i++ )
705 {
706 CalcBoundingBox( LogicalToDeviceX(points[i].x+xoffset), LogicalToDeviceY(points[i].y+yoffset));
707 }
708
709 fprintf( m_pstream,
710 "newpath\n"
711 "%d %d moveto\n",
712 LogicalToDeviceX(points[0].x+xoffset), LogicalToDeviceY(points[0].y+yoffset) );
713
714 for (i = 1; i < n; i++)
715 {
716 fprintf( m_pstream,
717 "%d %d lineto\n",
718 LogicalToDeviceX(points[i].x+xoffset), LogicalToDeviceY(points[i].y+yoffset) );
719 }
720
721 fprintf( m_pstream, "stroke\n" );
722 }
723
724 void wxPostScriptDC::DoDrawRectangle (wxCoord x, wxCoord y, wxCoord width, wxCoord height)
725 {
726 wxCHECK_RET( m_ok && m_pstream, wxT("invalid postscript dc") );
727
728 if (m_brush.GetStyle () != wxTRANSPARENT)
729 {
730 SetBrush( m_brush );
731
732 fprintf( m_pstream,
733 "newpath\n"
734 "%d %d moveto\n"
735 "%d %d lineto\n"
736 "%d %d lineto\n"
737 "%d %d lineto\n"
738 "closepath\n"
739 "fill\n",
740 LogicalToDeviceX(x), LogicalToDeviceY(y),
741 LogicalToDeviceX(x + width), LogicalToDeviceY(y),
742 LogicalToDeviceX(x + width), LogicalToDeviceY(y + height),
743 LogicalToDeviceX(x), LogicalToDeviceY(y + height) );
744
745 CalcBoundingBox( x, y );
746 CalcBoundingBox( x + width, y + height );
747 }
748
749 if (m_pen.GetStyle () != wxTRANSPARENT)
750 {
751 SetPen (m_pen);
752
753 fprintf( m_pstream,
754 "newpath\n"
755 "%d %d moveto\n"
756 "%d %d lineto\n"
757 "%d %d lineto\n"
758 "%d %d lineto\n"
759 "closepath\n"
760 "stroke\n",
761 LogicalToDeviceX(x), LogicalToDeviceY(y),
762 LogicalToDeviceX(x + width), LogicalToDeviceY(y),
763 LogicalToDeviceX(x + width), LogicalToDeviceY(y + height),
764 LogicalToDeviceX(x), LogicalToDeviceY(y + height) );
765
766 CalcBoundingBox( x, y );
767 CalcBoundingBox( x + width, y + height );
768 }
769 }
770
771 void wxPostScriptDC::DoDrawRoundedRectangle (wxCoord x, wxCoord y, wxCoord width, wxCoord height, double radius)
772 {
773 wxCHECK_RET( m_ok && m_pstream, wxT("invalid postscript dc") );
774
775 if (radius < 0.0)
776 {
777 // Now, a negative radius is interpreted to mean
778 // 'the proportion of the smallest X or Y dimension'
779 double smallest = 0.0;
780 if (width < height)
781 smallest = width;
782 else
783 smallest = height;
784 radius = (-radius * smallest);
785 }
786
787 wxCoord rad = (wxCoord) radius;
788
789 if (m_brush.GetStyle () != wxTRANSPARENT)
790 {
791 SetBrush( m_brush );
792
793 /* Draw rectangle anticlockwise */
794 fprintf( m_pstream,
795 "newpath\n"
796 "%d %d %d 90 180 arc\n"
797 "%d %d moveto\n"
798 "%d %d %d 180 270 arc\n"
799 "%d %d lineto\n"
800 "%d %d %d 270 0 arc\n"
801 "%d %d lineto\n"
802 "%d %d %d 0 90 arc\n"
803 "%d %d lineto\n"
804 "closepath\n"
805 "fill\n",
806 LogicalToDeviceX(x + rad), LogicalToDeviceY(y + rad), LogicalToDeviceXRel(rad),
807 LogicalToDeviceX(x), LogicalToDeviceY(y + rad),
808 LogicalToDeviceX(x + rad), LogicalToDeviceY(y + height - rad), LogicalToDeviceXRel(rad),
809 LogicalToDeviceX(x + width - rad), LogicalToDeviceY(y + height),
810 LogicalToDeviceX(x + width - rad), LogicalToDeviceY(y + height - rad), LogicalToDeviceXRel(rad),
811 LogicalToDeviceX(x + width), LogicalToDeviceY(y + rad),
812 LogicalToDeviceX(x + width - rad), LogicalToDeviceY(y + rad), LogicalToDeviceXRel(rad),
813 LogicalToDeviceX(x + rad), LogicalToDeviceY(y) );
814
815 CalcBoundingBox( x, y );
816 CalcBoundingBox( x + width, y + height );
817 }
818
819 if (m_pen.GetStyle () != wxTRANSPARENT)
820 {
821 SetPen (m_pen);
822
823 /* Draw rectangle anticlockwise */
824 fprintf( m_pstream,
825 "newpath\n"
826 "%d %d %d 90 180 arc\n"
827 "%d %d moveto\n"
828 "%d %d %d 180 270 arc\n"
829 "%d %d lineto\n"
830 "%d %d %d 270 0 arc\n"
831 "%d %d lineto\n"
832 "%d %d %d 0 90 arc\n"
833 "%d %d lineto\n"
834 "closepath\n"
835 "stroke\n",
836 LogicalToDeviceX(x + rad), LogicalToDeviceY(y + rad), LogicalToDeviceXRel(rad),
837 LogicalToDeviceX(x), LogicalToDeviceY(y + rad),
838 LogicalToDeviceX(x + rad), LogicalToDeviceY(y + height - rad), LogicalToDeviceXRel(rad),
839 LogicalToDeviceX(x + width - rad), LogicalToDeviceY(y + height),
840 LogicalToDeviceX(x + width - rad), LogicalToDeviceY(y + height - rad), LogicalToDeviceXRel(rad),
841 LogicalToDeviceX(x + width), LogicalToDeviceY(y + rad),
842 LogicalToDeviceX(x + width - rad), LogicalToDeviceY(y + rad), LogicalToDeviceXRel(rad),
843 LogicalToDeviceX(x + rad), LogicalToDeviceY(y) );
844
845 CalcBoundingBox( x, y );
846 CalcBoundingBox( x + width, y + height );
847 }
848 }
849
850 void wxPostScriptDC::DoDrawEllipse (wxCoord x, wxCoord y, wxCoord width, wxCoord height)
851 {
852 wxCHECK_RET( m_ok && m_pstream, wxT("invalid postscript dc") );
853
854 if (m_brush.GetStyle () != wxTRANSPARENT)
855 {
856 SetBrush (m_brush);
857
858 fprintf( m_pstream,
859 "newpath\n"
860 "%d %d %d %d 0 360 ellipse\n"
861 "fill\n",
862 LogicalToDeviceX(x + width / 2), LogicalToDeviceY(y + height / 2),
863 LogicalToDeviceXRel(width / 2), LogicalToDeviceYRel(height / 2) );
864
865 CalcBoundingBox( x - width, y - height );
866 CalcBoundingBox( x + width, y + height );
867 }
868
869 if (m_pen.GetStyle () != wxTRANSPARENT)
870 {
871 SetPen (m_pen);
872
873 fprintf( m_pstream,
874 "newpath\n"
875 "%d %d %d %d 0 360 ellipse\n"
876 "stroke\n",
877 LogicalToDeviceX(x + width / 2), LogicalToDeviceY(y + height / 2),
878 LogicalToDeviceXRel(width / 2), LogicalToDeviceYRel(height / 2) );
879
880 CalcBoundingBox( x - width, y - height );
881 CalcBoundingBox( x + width, y + height );
882 }
883 }
884
885 void wxPostScriptDC::DoDrawIcon( const wxIcon& icon, wxCoord x, wxCoord y )
886 {
887 DrawBitmap( icon, x, y, TRUE );
888 }
889
890 /* this has to be char, not wxChar */
891 static char hexArray[] = "0123456789ABCDEF";
892 static void LocalDecToHex( int dec, char *buf )
893 {
894 int firstDigit = (int)(dec/16.0);
895 int secondDigit = (int)(dec - (firstDigit*16.0));
896 buf[0] = hexArray[firstDigit];
897 buf[1] = hexArray[secondDigit];
898 buf[2] = 0;
899 }
900
901 void wxPostScriptDC::DoDrawBitmap( const wxBitmap& bitmap, wxCoord x, wxCoord y, bool WXUNUSED(useMask) )
902 {
903 wxCHECK_RET( m_ok && m_pstream, wxT("invalid postscript dc") );
904
905 if (!bitmap.Ok()) return;
906
907 wxImage image = bitmap.ConvertToImage();
908
909 if (!image.Ok()) return;
910
911 wxCoord w = image.GetWidth();
912 wxCoord h = image.GetHeight();
913
914 wxCoord ww = LogicalToDeviceXRel(image.GetWidth());
915 wxCoord hh = LogicalToDeviceYRel(image.GetHeight());
916
917 wxCoord xx = LogicalToDeviceX(x);
918 wxCoord yy = LogicalToDeviceY(y + bitmap.GetHeight());
919
920 fprintf( m_pstream,
921 "/origstate save def\n"
922 "20 dict begin\n"
923 "/pix %d string def\n"
924 "/grays %d string def\n"
925 "/npixels 0 def\n"
926 "/rgbindx 0 def\n"
927 "%d %d translate\n"
928 "%d %d scale\n"
929 "%d %d 8\n"
930 "[%d 0 0 %d 0 %d]\n"
931 "{currentfile pix readhexstring pop}\n"
932 "false 3 colorimage\n",
933 w, w, xx, yy, ww, hh, w, h, w, -h, h );
934
935
936 for (int j = 0; j < h; j++)
937 {
938 for (int i = 0; i < w; i++)
939 {
940 char buffer[5];
941 LocalDecToHex( image.GetRed(i,j), buffer );
942 fprintf( m_pstream, buffer );
943 LocalDecToHex( image.GetGreen(i,j), buffer );
944 fprintf( m_pstream, buffer );
945 LocalDecToHex( image.GetBlue(i,j), buffer );
946 fprintf( m_pstream, buffer );
947 }
948 fprintf( m_pstream, "\n" );
949 }
950
951 fprintf( m_pstream, "end\n" );
952 fprintf( m_pstream, "origstate restore\n" );
953 }
954
955 void wxPostScriptDC::SetFont( const wxFont& font )
956 {
957 wxCHECK_RET( m_ok && m_pstream, wxT("invalid postscript dc") );
958
959 if (!font.Ok()) return;
960
961 m_font = font;
962
963 #ifndef __WXGTK20__
964 int Style = m_font.GetStyle();
965 int Weight = m_font.GetWeight();
966
967 const char *name;
968 switch (m_font.GetFamily())
969 {
970 case wxTELETYPE:
971 case wxMODERN:
972 {
973 if (Style == wxITALIC)
974 {
975 if (Weight == wxBOLD)
976 name = "/Courier-BoldOblique";
977 else
978 name = "/Courier-Oblique";
979 }
980 else
981 {
982 if (Weight == wxBOLD)
983 name = "/Courier-Bold";
984 else
985 name = "/Courier";
986 }
987 break;
988 }
989 case wxROMAN:
990 {
991 if (Style == wxITALIC)
992 {
993 if (Weight == wxBOLD)
994 name = "/Times-BoldItalic";
995 else
996 name = "/Times-Italic";
997 }
998 else
999 {
1000 if (Weight == wxBOLD)
1001 name = "/Times-Bold";
1002 else
1003 name = "/Times-Roman";
1004 }
1005 break;
1006 }
1007 case wxSCRIPT:
1008 {
1009 name = "/ZapfChancery-MediumItalic";
1010 Style = wxNORMAL;
1011 Weight = wxNORMAL;
1012 break;
1013 }
1014 case wxSWISS:
1015 default:
1016 {
1017 if (Style == wxITALIC)
1018 {
1019 if (Weight == wxBOLD)
1020 name = "/Helvetica-BoldOblique";
1021 else
1022 name = "/Helvetica-Oblique";
1023 }
1024 else
1025 {
1026 if (Weight == wxBOLD)
1027 name = "/Helvetica-Bold";
1028 else
1029 name = "/Helvetica";
1030 }
1031 break;
1032 }
1033 }
1034
1035 fprintf( m_pstream, name );
1036 fprintf( m_pstream, " reencodeISO def\n" );
1037 fprintf( m_pstream, name );
1038 fprintf( m_pstream, " findfont\n" );
1039
1040 char buffer[100];
1041 sprintf( buffer, "%f scalefont setfont\n", LogicalToDeviceYRel(m_font.GetPointSize() * 1000) / 1000.0F);
1042 // this is a hack - we must scale font size (in pts) according to m_scaleY but
1043 // LogicalToDeviceYRel works with wxCoord type (int or longint). Se we first convert font size
1044 // to 1/1000th of pt and then back.
1045 for (int i = 0; i < 100; i++)
1046 if (buffer[i] == ',') buffer[i] = '.';
1047 fprintf( m_pstream, buffer );
1048 #endif
1049 }
1050
1051 void wxPostScriptDC::SetPen( const wxPen& pen )
1052 {
1053 wxCHECK_RET( m_ok && m_pstream, wxT("invalid postscript dc") );
1054
1055 if (!pen.Ok()) return;
1056
1057 int oldStyle = m_pen.GetStyle();
1058
1059 m_pen = pen;
1060
1061 {
1062 char buffer[100];
1063 #ifdef __WXMSW__
1064 sprintf( buffer, "%f setlinewidth\n", LogicalToDeviceXRel(1000 * m_pen.GetWidth()) / 1000.0f );
1065 #else
1066 sprintf( buffer, "%f setlinewidth\n", LogicalToDeviceXRel(1000 * m_pen.GetWidth()) / 1000.0f );
1067 #endif
1068 for (int i = 0; i < 100; i++)
1069 if (buffer[i] == ',') buffer[i] = '.';
1070 fprintf( m_pstream, buffer );
1071 }
1072
1073 /*
1074 Line style - WRONG: 2nd arg is OFFSET
1075
1076 Here, I'm afraid you do not conceive meaning of parameters of 'setdash'
1077 operator correctly. You should look-up this in the Red Book: the 2nd parame-
1078 ter is not number of values in the array of the first one, but an offset
1079 into this description of the pattern. I mean a real *offset* not index
1080 into array. I.e. If the command is [3 4] 1 setdash is used, then there
1081 will be first black line *2* units wxCoord, then space 4 units, then the
1082 pattern of *3* units black, 4 units space will be repeated.
1083 */
1084
1085 static const char *dotted = "[2 5] 2";
1086 static const char *short_dashed = "[4 4] 2";
1087 static const char *wxCoord_dashed = "[4 8] 2";
1088 static const char *dotted_dashed = "[6 6 2 6] 4";
1089
1090 const char *psdash = (char *) NULL;
1091 switch (m_pen.GetStyle())
1092 {
1093 case wxDOT: psdash = dotted; break;
1094 case wxSHORT_DASH: psdash = short_dashed; break;
1095 case wxLONG_DASH: psdash = wxCoord_dashed; break;
1096 case wxDOT_DASH: psdash = dotted_dashed; break;
1097 case wxSOLID:
1098 case wxTRANSPARENT:
1099 default: psdash = "[] 0"; break;
1100 }
1101
1102 if (oldStyle != m_pen.GetStyle())
1103 {
1104 fprintf( m_pstream, psdash );
1105 fprintf( m_pstream," setdash\n" );
1106 }
1107
1108 // Line colour
1109 unsigned char red = m_pen.GetColour().Red();
1110 unsigned char blue = m_pen.GetColour().Blue();
1111 unsigned char green = m_pen.GetColour().Green();
1112
1113 if (!m_colour)
1114 {
1115 // Anything not white is black
1116 if (! (red == (unsigned char) 255 &&
1117 blue == (unsigned char) 255 &&
1118 green == (unsigned char) 255) )
1119 {
1120 red = (unsigned char) 0;
1121 green = (unsigned char) 0;
1122 blue = (unsigned char) 0;
1123 }
1124 // setgray here ?
1125 }
1126
1127 if (!(red == m_currentRed && green == m_currentGreen && blue == m_currentBlue))
1128 {
1129 double redPS = (double)(red) / 255.0;
1130 double bluePS = (double)(blue) / 255.0;
1131 double greenPS = (double)(green) / 255.0;
1132
1133 char buffer[100];
1134 sprintf( buffer,
1135 "%.8f %.8f %.8f setrgbcolor\n",
1136 redPS, greenPS, bluePS );
1137 for (int i = 0; i < 100; i++)
1138 if (buffer[i] == ',') buffer[i] = '.';
1139 fprintf( m_pstream, buffer );
1140
1141 m_currentRed = red;
1142 m_currentBlue = blue;
1143 m_currentGreen = green;
1144 }
1145 }
1146
1147 void wxPostScriptDC::SetBrush( const wxBrush& brush )
1148 {
1149 wxCHECK_RET( m_ok && m_pstream, wxT("invalid postscript dc") );
1150
1151 if (!brush.Ok()) return;
1152
1153 m_brush = brush;
1154
1155 // Brush colour
1156 unsigned char red = m_brush.GetColour().Red();
1157 unsigned char blue = m_brush.GetColour().Blue();
1158 unsigned char green = m_brush.GetColour().Green();
1159
1160 if (!m_colour)
1161 {
1162 // Anything not white is black
1163 if (! (red == (unsigned char) 255 &&
1164 blue == (unsigned char) 255 &&
1165 green == (unsigned char) 255) )
1166 {
1167 red = (unsigned char) 0;
1168 green = (unsigned char) 0;
1169 blue = (unsigned char) 0;
1170 }
1171 // setgray here ?
1172 }
1173
1174 if (!(red == m_currentRed && green == m_currentGreen && blue == m_currentBlue))
1175 {
1176 double redPS = (double)(red) / 255.0;
1177 double bluePS = (double)(blue) / 255.0;
1178 double greenPS = (double)(green) / 255.0;
1179
1180 char buffer[100];
1181 sprintf( buffer,
1182 "%.8f %.8f %.8f setrgbcolor\n",
1183 redPS, greenPS, bluePS );
1184 for (int i = 0; i < 100; i++)
1185 if (buffer[i] == ',') buffer[i] = '.';
1186 fprintf( m_pstream, buffer );
1187
1188 m_currentRed = red;
1189 m_currentBlue = blue;
1190 m_currentGreen = green;
1191 }
1192 }
1193
1194 #ifdef __WXGTK20__
1195
1196 #include "wx/gtk/private.h"
1197 #include "wx/fontutil.h"
1198 #include "gtk/gtk.h"
1199 #include <pango/pangoft2.h>
1200 #include <freetype/ftglyph.h>
1201
1202 #ifndef FT_Outline_Decompose
1203 FT_EXPORT( FT_Error ) FT_Outline_Decompose(
1204 FT_Outline* outline,
1205 const FT_Outline_Funcs* interface,
1206 void* user );
1207 #endif
1208
1209 typedef struct _OutlineInfo OutlineInfo;
1210 struct _OutlineInfo {
1211 FILE *OUT;
1212 FT_Vector glyph_origin;
1213 int dpi;
1214 };
1215
1216 static int paps_move_to( FT_Vector* to,
1217 void *user_data)
1218 {
1219 OutlineInfo *outline_info = (OutlineInfo*)user_data;
1220 fprintf(outline_info->OUT, "%d %d moveto\n",
1221 (int)to->x ,
1222 (int)to->y );
1223 return 0;
1224 }
1225
1226 static int paps_line_to( FT_Vector* to,
1227 void *user_data)
1228 {
1229 OutlineInfo *outline_info = (OutlineInfo*)user_data;
1230 fprintf(outline_info->OUT, "%d %d lineto\n",
1231 (int)to->x ,
1232 (int)to->y );
1233 return 0;
1234 }
1235
1236 static int paps_conic_to( FT_Vector* control,
1237 FT_Vector* to,
1238 void *user_data)
1239 {
1240 OutlineInfo *outline_info = (OutlineInfo*)user_data;
1241 fprintf(outline_info->OUT, "%d %d %d %d conicto\n",
1242 (int)control->x ,
1243 (int)control->y ,
1244 (int)to->x ,
1245 (int)to->y );
1246 return 0;
1247 }
1248
1249 static int paps_cubic_to( FT_Vector* control1,
1250 FT_Vector* control2,
1251 FT_Vector* to,
1252 void *user_data)
1253 {
1254 OutlineInfo *outline_info = (OutlineInfo*)user_data;
1255 fprintf(outline_info->OUT,
1256 "%d %d %d %d %d %d curveto\n",
1257 (int)control1->x ,
1258 (int)control1->y ,
1259 (int)control2->x ,
1260 (int)control2->y ,
1261 (int)to->x ,
1262 (int)to->y );
1263 return 0;
1264 }
1265
1266 static void draw_bezier_outline(FILE *OUT,
1267 int dpi,
1268 FT_Face face,
1269 FT_UInt glyph_index,
1270 wxCoord pos_x,
1271 wxCoord pos_y
1272 )
1273 {
1274 FT_Int load_flags = FT_LOAD_DEFAULT;
1275 FT_Glyph glyph;
1276
1277 /* Output outline */
1278 FT_Outline_Funcs outlinefunc =
1279 {
1280 paps_move_to,
1281 paps_line_to,
1282 paps_conic_to,
1283 paps_cubic_to
1284 };
1285 OutlineInfo outline_info;
1286
1287 outline_info.glyph_origin.x = (FT_Pos) pos_x;
1288 outline_info.glyph_origin.y = (FT_Pos) pos_y;
1289 outline_info.dpi = dpi;
1290 outline_info.OUT = OUT;
1291
1292 fprintf(OUT, "gsave %d %d translate 0 0 0 setrgbcolor\n", pos_x, pos_y);
1293 fprintf(OUT, "start_ol\n");
1294
1295 FT_Load_Glyph(face, glyph_index, load_flags);
1296 FT_Get_Glyph (face->glyph, &glyph);
1297 FT_Outline_Decompose (&(((FT_OutlineGlyph)glyph)->outline),
1298 &outlinefunc, &outline_info);
1299 fprintf(OUT, "end_ol grestore \n");
1300
1301 FT_Done_Glyph (glyph);
1302 }
1303
1304
1305 #endif
1306
1307 void wxPostScriptDC::DoDrawText( const wxString& text, wxCoord x, wxCoord y )
1308 {
1309 wxCHECK_RET( m_ok && m_pstream, wxT("invalid postscript dc") );
1310
1311 #ifdef __WXGTK20__
1312 int dpi = GetResolution() * 2;
1313
1314 PangoContext *context = pango_ft2_get_context ( dpi, dpi );
1315
1316 // What are these for?
1317 pango_context_set_language (context, pango_language_from_string ("en_US"));
1318 pango_context_set_base_dir (context, PANGO_DIRECTION_LTR );
1319
1320 // Set the font
1321 pango_context_set_font_description (context, m_font.GetNativeFontInfo()->description );
1322
1323 // Create layout
1324 PangoLayout *layout = layout = pango_layout_new (context);
1325 #if wxUSE_UNICODE
1326 wxCharBuffer buffer = wxConvUTF8.cWC2MB( text );
1327 #else
1328 wxCharBuffer buffer = wxConvUTF8.cWC2MB( wxConvLocal.cWX2WC( text ) );
1329 #endif
1330 pango_layout_set_text( layout, (const char*) buffer, strlen(buffer) );
1331
1332 #if 1
1333 double xx = LogicalToDeviceX(x);
1334 double yy = LogicalToDeviceY(y /*+ bitmap.GetHeight()*/ );
1335
1336 // Loop over lines in layout
1337 int num_lines = pango_layout_get_line_count( layout );
1338 for (int i = 0; i < num_lines; i++)
1339 {
1340 PangoLayoutLine *line = pango_layout_get_line( layout, i );
1341
1342 // Loop over runs in line
1343 GSList *runs_list = line->runs;
1344 while (runs_list)
1345 {
1346 PangoLayoutRun *run = (PangoLayoutRun*) runs_list->data;
1347 PangoItem *item = run->item;
1348 PangoGlyphString *glyphs = run->glyphs;
1349 PangoAnalysis *analysis = &item->analysis;
1350 PangoFont *font = analysis->font;
1351 FT_Face ft_face = pango_ft2_font_get_face(font);
1352
1353 int num_glyphs = glyphs->num_glyphs;
1354 for (int glyph_idx = 0; glyph_idx < num_glyphs; glyph_idx++)
1355 {
1356 PangoGlyphGeometry geometry = glyphs->glyphs[glyph_idx].geometry;
1357 double pos_x = xx + 1.0 * geometry.x_offset / PANGO_SCALE;
1358 double pos_y = yy - 1.0 * geometry.y_offset / PANGO_SCALE;
1359 xx += 1.0 * geometry.width / PANGO_SCALE;
1360
1361 draw_bezier_outline( m_pstream, dpi, ft_face,
1362 (FT_UInt)(glyphs->glyphs[glyph_idx].glyph),
1363 (wxCoord)pos_x, (wxCoord)pos_y );
1364 }
1365 runs_list = runs_list->next;
1366 }
1367 }
1368 #else
1369 // Find out extent for the bitmap
1370 int height = 0;
1371 int width = 0;
1372 PangoRectangle logical_rect;
1373 pango_layout_get_extents (layout, NULL, &logical_rect);
1374 height = PANGO_PIXELS (logical_rect.height);
1375 width = PANGO_PIXELS (logical_rect.width);
1376
1377 // printf( "h %d w %d lh %d lw %d\n", height, width, logical_rect.height, logical_rect.width );
1378
1379 // Allocate FreeType 2 bitmap
1380 int byte_width = (width + 7)/8 * 8;
1381 FT_Bitmap bitmap;
1382 guchar *buf = (guchar*) g_malloc (byte_width * height);
1383 memset (buf, 0x00, byte_width * height);
1384 bitmap.rows = height;
1385 bitmap.width = byte_width;
1386 bitmap.pitch = byte_width;
1387 bitmap.buffer = buf;
1388 bitmap.num_grays = 256;
1389 bitmap.pixel_mode = ft_pixel_mode_grays;
1390
1391 // Render bitmap
1392 pango_ft2_render_layout (&bitmap, layout, 0, 0);
1393
1394 // Invert bitmap to get black text on white background
1395 for (int pix_idx = 0; pix_idx < width * height; pix_idx++)
1396 buf[pix_idx] = 255-buf[pix_idx];
1397
1398 // Write PS output
1399 wxCoord xx = LogicalToDeviceX(x);
1400 wxCoord yy = LogicalToDeviceY(y /*+ bitmap.GetHeight()*/ );
1401
1402 fprintf(m_pstream, "gsave\n");
1403 fprintf(m_pstream, "%d %d translate\n", xx, yy);
1404 fprintf(m_pstream, "/img_width %d def\n", bitmap.width);
1405 fprintf(m_pstream, "/img_height %d def\n", bitmap.rows);
1406 fprintf(m_pstream, "/picstr img_width 8 idiv string def\n");
1407
1408 fprintf(m_pstream,
1409 " img_width 72 15 div mul\n"
1410 " img_height 72 15 div mul scale\n"
1411 " 0 setgray\n"
1412 " img_width img_height\n"
1413 " true\n"
1414 " [img_width 0 0 img_height neg 0 img_height 0.67 mul]\n"
1415 " { currentfile\n"
1416 " picstr readhexstring pop }\n"
1417 " imagemask"
1418 );
1419
1420
1421 for (int b_idx= 0; b_idx < bitmap.width/8 * bitmap.rows; b_idx++)
1422 {
1423 guchar packed_b = 0;
1424 int bit_idx;
1425
1426 if (b_idx % (bitmap.width/8) == 0)
1427 fprintf(m_pstream, "\n");
1428
1429 for (bit_idx = 0; bit_idx < 8; bit_idx++)
1430 {
1431 guchar this_bit = bitmap.buffer[b_idx * 8+bit_idx]<128;
1432 packed_b = (packed_b << 1) + this_bit;
1433 }
1434 fprintf(m_pstream, "%02x", packed_b);
1435 }
1436
1437 fprintf(m_pstream, "\ngrestore\n" );
1438
1439 // Free memory
1440 g_free( buf );
1441 #endif
1442
1443 #else
1444 wxCoord text_w, text_h, text_descent;
1445
1446 GetTextExtent(text, &text_w, &text_h, &text_descent);
1447
1448 // VZ: this seems to be unnecessary, so taking it out for now, if it
1449 // doesn't create any problems, remove this comment entirely
1450 //SetFont( m_font );
1451
1452 if (m_textForegroundColour.Ok())
1453 {
1454 unsigned char red = m_textForegroundColour.Red();
1455 unsigned char blue = m_textForegroundColour.Blue();
1456 unsigned char green = m_textForegroundColour.Green();
1457
1458 if (!m_colour)
1459 {
1460 // Anything not white is black
1461 if (! (red == (unsigned char) 255 &&
1462 blue == (unsigned char) 255 &&
1463 green == (unsigned char) 255))
1464 {
1465 red = (unsigned char) 0;
1466 green = (unsigned char) 0;
1467 blue = (unsigned char) 0;
1468 }
1469 }
1470
1471 // maybe setgray here ?
1472 if (!(red == m_currentRed && green == m_currentGreen && blue == m_currentBlue))
1473 {
1474 double redPS = (double)(red) / 255.0;
1475 double bluePS = (double)(blue) / 255.0;
1476 double greenPS = (double)(green) / 255.0;
1477
1478 char buffer[100];
1479 sprintf( buffer,
1480 "%.8f %.8f %.8f setrgbcolor\n",
1481 redPS, greenPS, bluePS );
1482 for (int i = 0; i < 100; i++)
1483 if (buffer[i] == ',') buffer[i] = '.';
1484 fprintf( m_pstream, buffer );
1485
1486 m_currentRed = red;
1487 m_currentBlue = blue;
1488 m_currentGreen = green;
1489 }
1490 }
1491
1492 int size = m_font.GetPointSize();
1493
1494 // wxCoord by = y + (wxCoord)floor( double(size) * 2.0 / 3.0 ); // approximate baseline
1495 // commented by V. Slavik and replaced by accurate version
1496 // - note that there is still rounding error in text_descent!
1497 wxCoord by = y + size - text_descent; // baseline
1498 fprintf( m_pstream, "%d %d moveto\n", LogicalToDeviceX(x), LogicalToDeviceY(by) );
1499
1500 fprintf( m_pstream, "(" );
1501 const wxWX2MBbuf textbuf = text.mb_str();
1502 size_t len = strlen(textbuf);
1503 size_t i;
1504 for (i = 0; i < len; i++)
1505 {
1506 int c = (unsigned char) textbuf[i];
1507 if (c == ')' || c == '(' || c == '\\')
1508 {
1509 /* Cope with special characters */
1510 fprintf( m_pstream, "\\" );
1511 fputc(c, m_pstream);
1512 }
1513 else if ( c >= 128 )
1514 {
1515 /* Cope with character codes > 127 */
1516 fprintf(m_pstream, "\\%o", c);
1517 }
1518 else
1519 {
1520 fputc(c, m_pstream);
1521 }
1522 }
1523
1524 fprintf( m_pstream, ") show\n" );
1525
1526 if (m_font.GetUnderlined())
1527 {
1528 wxCoord uy = (wxCoord)(y + size - m_underlinePosition);
1529 char buffer[100];
1530
1531 sprintf( buffer,
1532 "gsave\n"
1533 "%d %d moveto\n"
1534 "%f setlinewidth\n"
1535 "%d %d lineto\n"
1536 "stroke\n"
1537 "grestore\n",
1538 LogicalToDeviceX(x), LogicalToDeviceY(uy),
1539 m_underlineThickness,
1540 LogicalToDeviceX(x + text_w), LogicalToDeviceY(uy) );
1541 for (i = 0; i < 100; i++)
1542 if (buffer[i] == ',') buffer[i] = '.';
1543 fprintf( m_pstream, buffer );
1544 }
1545
1546 CalcBoundingBox( x, y );
1547 CalcBoundingBox( x + size * text.Length() * 2/3 , y );
1548 #endif
1549 }
1550
1551 void wxPostScriptDC::DoDrawRotatedText( const wxString& text, wxCoord x, wxCoord y, double angle )
1552 {
1553 if (angle == 0.0)
1554 {
1555 DoDrawText(text, x, y);
1556 return;
1557 }
1558
1559 wxCHECK_RET( m_ok && m_pstream, wxT("invalid postscript dc") );
1560
1561 SetFont( m_font );
1562
1563 if (m_textForegroundColour.Ok())
1564 {
1565 unsigned char red = m_textForegroundColour.Red();
1566 unsigned char blue = m_textForegroundColour.Blue();
1567 unsigned char green = m_textForegroundColour.Green();
1568
1569 if (!m_colour)
1570 {
1571 // Anything not white is black
1572 if (! (red == (unsigned char) 255 &&
1573 blue == (unsigned char) 255 &&
1574 green == (unsigned char) 255))
1575 {
1576 red = (unsigned char) 0;
1577 green = (unsigned char) 0;
1578 blue = (unsigned char) 0;
1579 }
1580 }
1581
1582 // maybe setgray here ?
1583 if (!(red == m_currentRed && green == m_currentGreen && blue == m_currentBlue))
1584 {
1585 double redPS = (double)(red) / 255.0;
1586 double bluePS = (double)(blue) / 255.0;
1587 double greenPS = (double)(green) / 255.0;
1588
1589 char buffer[100];
1590 sprintf( buffer,
1591 "%.8f %.8f %.8f setrgbcolor\n",
1592 redPS, greenPS, bluePS );
1593 for (int i = 0; i < 100; i++)
1594 if (buffer[i] == ',') buffer[i] = '.';
1595 fprintf( m_pstream, buffer );
1596
1597 m_currentRed = red;
1598 m_currentBlue = blue;
1599 m_currentGreen = green;
1600 }
1601 }
1602
1603 int size = m_font.GetPointSize();
1604
1605 long by = y + (long)floor( double(size) * 2.0 / 3.0 ); // approximate baseline
1606
1607 // FIXME only correct for 90 degrees
1608 fprintf(m_pstream, "%d %d moveto\n",
1609 LogicalToDeviceX((wxCoord)(x + size)), LogicalToDeviceY((wxCoord)by) );
1610
1611 char buffer[100];
1612 sprintf(buffer, "%.8f rotate\n", angle);
1613 size_t i;
1614 for (i = 0; i < 100; i++)
1615 if (buffer[i] == ',') buffer[i] = '.';
1616 fprintf(m_pstream, buffer);
1617
1618 fprintf( m_pstream, "(" );
1619 const wxWX2MBbuf textbuf = text.mb_str();
1620 size_t len = strlen(textbuf);
1621 for (i = 0; i < len; i++)
1622 {
1623 int c = (unsigned char) textbuf[i];
1624 if (c == ')' || c == '(' || c == '\\')
1625 {
1626 /* Cope with special characters */
1627 fprintf( m_pstream, "\\" );
1628 fputc(c, m_pstream);
1629 }
1630 else if ( c >= 128 )
1631 {
1632 /* Cope with character codes > 127 */
1633 fprintf(m_pstream, "\\%o", c);
1634 }
1635 else
1636 {
1637 fputc(c, m_pstream);
1638 }
1639 }
1640
1641 fprintf( m_pstream, ") show\n" );
1642
1643 sprintf( buffer, "%.8f rotate\n", -angle );
1644 for (i = 0; i < 100; i++)
1645 if (buffer[i] == ',') buffer[i] = '.';
1646 fprintf( m_pstream, buffer );
1647
1648 if (m_font.GetUnderlined())
1649 {
1650 wxCoord uy = (wxCoord)(y + size - m_underlinePosition);
1651 wxCoord w, h;
1652 char buffer[100];
1653 GetTextExtent(text, &w, &h);
1654
1655 sprintf( buffer,
1656 "gsave\n"
1657 "%d %d moveto\n"
1658 "%f setlinewidth\n"
1659 "%d %d lineto\n"
1660 "stroke\n"
1661 "grestore\n",
1662 LogicalToDeviceX(x), LogicalToDeviceY(uy),
1663 m_underlineThickness,
1664 LogicalToDeviceX(x + w), LogicalToDeviceY(uy) );
1665 for (i = 0; i < 100; i++)
1666 if (buffer[i] == ',') buffer[i] = '.';
1667 fprintf( m_pstream, buffer );
1668 }
1669
1670 CalcBoundingBox( x, y );
1671 CalcBoundingBox( x + size * text.Length() * 2/3 , y );
1672 }
1673
1674 void wxPostScriptDC::SetBackground (const wxBrush& brush)
1675 {
1676 m_backgroundBrush = brush;
1677 }
1678
1679 void wxPostScriptDC::SetLogicalFunction (int WXUNUSED(function))
1680 {
1681 wxFAIL_MSG( wxT("wxPostScriptDC::SetLogicalFunction not implemented.") );
1682 }
1683
1684 void wxPostScriptDC::DoDrawSpline( wxList *points )
1685 {
1686 wxCHECK_RET( m_ok && m_pstream, wxT("invalid postscript dc") );
1687
1688 SetPen( m_pen );
1689
1690 double a, b, c, d, x1, y1, x2, y2, x3, y3;
1691 wxPoint *p, *q;
1692
1693 wxNode *node = points->First();
1694 p = (wxPoint *)node->Data();
1695 x1 = p->x;
1696 y1 = p->y;
1697
1698 node = node->Next();
1699 p = (wxPoint *)node->Data();
1700 c = p->x;
1701 d = p->y;
1702 x3 = a = (double)(x1 + c) / 2;
1703 y3 = b = (double)(y1 + d) / 2;
1704
1705 fprintf( m_pstream,
1706 "newpath\n"
1707 "%d %d moveto\n"
1708 "%d %d lineto\n",
1709 LogicalToDeviceX((wxCoord)x1), LogicalToDeviceY((wxCoord)y1),
1710 LogicalToDeviceX((wxCoord)x3), LogicalToDeviceY((wxCoord)y3) );
1711
1712 CalcBoundingBox( (wxCoord)x1, (wxCoord)y1 );
1713 CalcBoundingBox( (wxCoord)x3, (wxCoord)y3 );
1714
1715 while ((node = node->Next()) != NULL)
1716 {
1717 q = (wxPoint *)node->Data();
1718
1719 x1 = x3;
1720 y1 = y3;
1721 x2 = c;
1722 y2 = d;
1723 c = q->x;
1724 d = q->y;
1725 x3 = (double)(x2 + c) / 2;
1726 y3 = (double)(y2 + d) / 2;
1727
1728 fprintf( m_pstream,
1729 "%d %d %d %d %d %d DrawSplineSection\n",
1730 LogicalToDeviceX((wxCoord)x1), LogicalToDeviceY((wxCoord)y1),
1731 LogicalToDeviceX((wxCoord)x2), LogicalToDeviceY((wxCoord)y2),
1732 LogicalToDeviceX((wxCoord)x3), LogicalToDeviceY((wxCoord)y3) );
1733
1734 CalcBoundingBox( (wxCoord)x1, (wxCoord)y1 );
1735 CalcBoundingBox( (wxCoord)x3, (wxCoord)y3 );
1736 }
1737
1738 /*
1739 At this point, (x2,y2) and (c,d) are the position of the
1740 next-to-last and last point respectively, in the point list
1741 */
1742
1743 fprintf( m_pstream,
1744 "%d %d lineto\n"
1745 "stroke\n",
1746 LogicalToDeviceX((wxCoord)c), LogicalToDeviceY((wxCoord)d) );
1747 }
1748
1749 wxCoord wxPostScriptDC::GetCharWidth() const
1750 {
1751 // Chris Breeze: reasonable approximation using wxMODERN/Courier
1752 return (wxCoord) (GetCharHeight() * 72.0 / 120.0);
1753 }
1754
1755
1756 void wxPostScriptDC::SetAxisOrientation( bool xLeftRight, bool yBottomUp )
1757 {
1758 wxCHECK_RET( m_ok && m_pstream, wxT("invalid postscript dc") );
1759
1760 m_signX = (xLeftRight ? 1 : -1);
1761 m_signY = (yBottomUp ? 1 : -1);
1762
1763 // FIXME there is no such function in MSW nor in OS2/PM
1764 #if !defined(__WXMSW__) && !defined(__WXPM__)
1765 ComputeScaleAndOrigin();
1766 #endif
1767 }
1768
1769 void wxPostScriptDC::SetDeviceOrigin( wxCoord x, wxCoord y )
1770 {
1771 wxCHECK_RET( m_ok && m_pstream, wxT("invalid postscript dc") );
1772
1773 int h = 0;
1774 int w = 0;
1775 GetSize( &w, &h );
1776
1777 wxDC::SetDeviceOrigin( x, h-y );
1778 }
1779
1780 void wxPostScriptDC::DoGetSize(int* width, int* height) const
1781 {
1782 wxPaperSize id = m_printData.GetPaperId();
1783
1784 wxPrintPaperType *paper = wxThePrintPaperDatabase->FindPaperType(id);
1785
1786 if (!paper) paper = wxThePrintPaperDatabase->FindPaperType(wxPAPER_A4);
1787
1788 int w = 595;
1789 int h = 842;
1790 if (paper)
1791 {
1792 w = paper->GetSizeDeviceUnits().x;
1793 h = paper->GetSizeDeviceUnits().y;
1794 }
1795
1796 if (m_printData.GetOrientation() == wxLANDSCAPE)
1797 {
1798 int tmp = w;
1799 w = h;
1800 h = tmp;
1801 }
1802
1803 if (width) *width = (int)(w * ms_PSScaleFactor);
1804 if (height) *height = (int)(h * ms_PSScaleFactor);
1805 }
1806
1807 void wxPostScriptDC::DoGetSizeMM(int *width, int *height) const
1808 {
1809 wxPaperSize id = m_printData.GetPaperId();
1810
1811 wxPrintPaperType *paper = wxThePrintPaperDatabase->FindPaperType(id);
1812
1813 if (!paper) paper = wxThePrintPaperDatabase->FindPaperType(wxPAPER_A4);
1814
1815 int w = 210;
1816 int h = 297;
1817 if (paper)
1818 {
1819 w = paper->GetWidth() / 10;
1820 h = paper->GetHeight() / 10;
1821 }
1822
1823 if (m_printData.GetOrientation() == wxLANDSCAPE)
1824 {
1825 int tmp = w;
1826 w = h;
1827 h = tmp;
1828 }
1829
1830 if (width) *width = w;
1831 if (height) *height = h;
1832 }
1833
1834 // Resolution in pixels per logical inch
1835 wxSize wxPostScriptDC::GetPPI(void) const
1836 {
1837 return wxSize((int)(72 * ms_PSScaleFactor),
1838 (int)(72 * ms_PSScaleFactor));
1839 }
1840
1841
1842 bool wxPostScriptDC::StartDoc( const wxString& message )
1843 {
1844 wxCHECK_MSG( m_ok, FALSE, wxT("invalid postscript dc") );
1845
1846 if (m_printData.GetFilename() == "")
1847 {
1848 wxString filename = wxGetTempFileName("ps");
1849 m_printData.SetFilename(filename);
1850 }
1851
1852 m_pstream = wxFopen( m_printData.GetFilename().c_str(), wxT("w+") ); // FIXME: use fn_str() here under Unicode?
1853
1854 if (!m_pstream)
1855 {
1856 wxLogError( _("Cannot open file for PostScript printing!"));
1857 m_ok = FALSE;
1858 return FALSE;
1859 }
1860
1861 m_ok = TRUE;
1862
1863 fprintf( m_pstream, "%%%%BeginProlog\n" );
1864 fprintf( m_pstream, wxPostScriptHeaderConicTo );
1865 fprintf( m_pstream, wxPostScriptHeaderEllipse );
1866 fprintf( m_pstream, wxPostScriptHeaderEllipticArc );
1867 fprintf( m_pstream, wxPostScriptHeaderColourImage );
1868 #ifndef __WXGTK20__
1869 fprintf( m_pstream, wxPostScriptHeaderReencodeISO1 );
1870 fprintf( m_pstream, wxPostScriptHeaderReencodeISO2 );
1871 #endif
1872 if (wxPostScriptHeaderSpline)
1873 fprintf( m_pstream, wxPostScriptHeaderSpline );
1874 fprintf( m_pstream, "%%%%EndProlog\n" );
1875
1876 SetBrush( *wxBLACK_BRUSH );
1877 SetPen( *wxBLACK_PEN );
1878 SetBackground( *wxWHITE_BRUSH );
1879 SetTextForeground( *wxBLACK );
1880
1881 // set origin according to paper size
1882 SetDeviceOrigin( 0,0 );
1883
1884 wxPageNumber = 1;
1885 m_pageNumber = 1;
1886 m_title = message;
1887 return TRUE;
1888 }
1889
1890 void wxPostScriptDC::EndDoc ()
1891 {
1892 wxCHECK_RET( m_ok && m_pstream, wxT("invalid postscript dc") );
1893
1894 if (m_clipping)
1895 {
1896 m_clipping = FALSE;
1897 fprintf( m_pstream, "grestore\n" );
1898 }
1899
1900 fclose( m_pstream );
1901 m_pstream = (FILE *) NULL;
1902
1903 wxChar *header_file = wxGetTempFileName("ps");
1904
1905 m_pstream = fopen( wxConvFile.cWX2MB(header_file) , "w+" );
1906
1907 fprintf( m_pstream, "%%!PS-Adobe-2.0\n" ); // PostScript magic strings
1908 fprintf( m_pstream, "%%%%Title: %s\n", (const char *)m_title.mb_str() );
1909 fprintf( m_pstream, "%%%%Creator: %s\n", (const char*)wxConvCurrent->cWX2MB(wxTheApp->argv[0]) );
1910 fprintf( m_pstream, "%%%%CreationDate: %s\n", (const char *)wxNow().mb_str() );
1911
1912 wxChar userID[256];
1913 if ( wxGetEmailAddress(userID, sizeof(userID)) )
1914 {
1915 fprintf( m_pstream, "%%%%For: %s ", wxMBSTRINGCAST wxConvCurrent->cWX2MB(userID) );
1916 wxChar userName[245];
1917 if (wxGetUserName(userName, sizeof(userName)))
1918 fprintf( m_pstream, " (%s)", wxMBSTRINGCAST wxConvCurrent->cWX2MB(userName) );
1919 fprintf( m_pstream, "\n" );
1920 }
1921 else if ( wxGetUserName(userID, sizeof(userID)) )
1922 {
1923 fprintf( m_pstream, "%%%%For: %s\n", wxMBSTRINGCAST wxConvCurrent->cWX2MB(userID) );;
1924 }
1925
1926 // THE FOLLOWING HAS BEEN CONTRIBUTED BY Andy Fyfe <andy@hyperparallel.com>
1927
1928 wxCoord wx_printer_translate_x, wx_printer_translate_y;
1929 double wx_printer_scale_x, wx_printer_scale_y;
1930
1931 wx_printer_translate_x = (wxCoord)m_printData.GetPrinterTranslateX();
1932 wx_printer_translate_y = (wxCoord)m_printData.GetPrinterTranslateY();
1933
1934 wx_printer_scale_x = m_printData.GetPrinterScaleX();
1935 wx_printer_scale_y = m_printData.GetPrinterScaleY();
1936
1937 if (m_printData.GetOrientation() == wxLANDSCAPE)
1938 fprintf( m_pstream, "%%%%Orientation: Landscape\n" );
1939 else
1940 fprintf( m_pstream, "%%%%Orientation: Portrait\n" );
1941
1942 // Compute the bounding box. Note that it is in the default user
1943 // coordinate system, thus we have to convert the values.
1944 wxCoord minX = (wxCoord) LogicalToDeviceX(m_minX);
1945 wxCoord minY = (wxCoord) LogicalToDeviceY(m_minY);
1946 wxCoord maxX = (wxCoord) LogicalToDeviceX(m_maxX);
1947 wxCoord maxY = (wxCoord) LogicalToDeviceY(m_maxY);
1948
1949 // LOG2DEV may have changed the minimum to maximum vice versa
1950 if ( minX > maxX ) { wxCoord tmp = minX; minX = maxX; maxX = tmp; }
1951 if ( minY > maxY ) { wxCoord tmp = minY; minY = maxY; maxY = tmp; }
1952
1953 // account for used scaling (boundingbox is before scaling in ps-file)
1954 double scale_x = m_printData.GetPrinterScaleX() / ms_PSScaleFactor;
1955 double scale_y = m_printData.GetPrinterScaleY() / ms_PSScaleFactor;
1956
1957 wxCoord llx, lly, urx, ury;
1958 llx = (wxCoord) ((minX+wx_printer_translate_x)*scale_x);
1959 lly = (wxCoord) ((minY+wx_printer_translate_y)*scale_y);
1960 urx = (wxCoord) ((maxX+wx_printer_translate_x)*scale_x);
1961 ury = (wxCoord) ((maxY+wx_printer_translate_y)*scale_y);
1962 // (end of bounding box computation)
1963
1964
1965 // If we're landscape, our sense of "x" and "y" is reversed.
1966 if (m_printData.GetOrientation() == wxLANDSCAPE)
1967 {
1968 wxCoord tmp;
1969 tmp = llx; llx = lly; lly = tmp;
1970 tmp = urx; urx = ury; ury = tmp;
1971
1972 // We need either the two lines that follow, or we need to subtract
1973 // min_x from real_translate_y, which is commented out below.
1974 llx = llx - (wxCoord)(m_minX*wx_printer_scale_y);
1975 urx = urx - (wxCoord)(m_minX*wx_printer_scale_y);
1976 }
1977
1978 // The Adobe specifications call for integers; we round as to make
1979 // the bounding larger.
1980 fprintf( m_pstream,
1981 "%%%%BoundingBox: %d %d %d %d\n",
1982 (wxCoord)floor((double)llx), (wxCoord)floor((double)lly),
1983 (wxCoord)ceil((double)urx), (wxCoord)ceil((double)ury) );
1984 fprintf( m_pstream, "%%%%Pages: %d\n", (wxPageNumber - 1) );
1985 fprintf( m_pstream, "%%%%EndComments\n\n" );
1986
1987 // To check the correctness of the bounding box, postscript commands
1988 // to draw a box corresponding to the bounding box are generated below.
1989 // But since we typically don't want to print such a box, the postscript
1990 // commands are generated within comments. These lines appear before any
1991 // adjustment of scale, rotation, or translation, and hence are in the
1992 // default user coordinates.
1993 fprintf( m_pstream, "%% newpath\n" );
1994 fprintf( m_pstream, "%% %d %d moveto\n", llx, lly );
1995 fprintf( m_pstream, "%% %d %d lineto\n", urx, lly );
1996 fprintf( m_pstream, "%% %d %d lineto\n", urx, ury );
1997 fprintf( m_pstream, "%% %d %d lineto closepath stroke\n", llx, ury );
1998
1999 fclose( m_pstream );
2000 m_pstream = (FILE*) NULL;
2001
2002 wxChar *tmp_file = wxGetTempFileName("ps");
2003
2004 // Paste header Before wx_printer_file
2005 wxConcatFiles (header_file, m_printData.GetFilename(), tmp_file );
2006 wxRemoveFile( header_file );
2007 wxRemoveFile( m_printData.GetFilename() );
2008 wxRenameFile( tmp_file, m_printData.GetFilename() );
2009
2010 #if defined(__X__) || defined(__WXGTK__)
2011 if (m_ok)
2012 {
2013 wxString previewCommand(m_printData.GetPreviewCommand());
2014 wxString printerCommand(m_printData.GetPrinterCommand());
2015 wxString printerOptions(m_printData.GetPrinterOptions());
2016 wxString filename(m_printData.GetFilename());
2017
2018 switch (m_printData.GetPrintMode())
2019 {
2020 case wxPRINT_MODE_PREVIEW:
2021 {
2022 wxChar *argv[3];
2023 argv[0] = WXSTRINGCAST previewCommand;
2024 argv[1] = WXSTRINGCAST filename;
2025 argv[2] = (wxChar*) NULL;
2026 #if defined(__WXGTK20__) && wxUSE_UNICODE
2027 #else
2028 wxExecute( argv, TRUE );
2029 #endif
2030 wxRemoveFile( m_printData.GetFilename() );
2031 }
2032 break;
2033 case wxPRINT_MODE_PRINTER:
2034 {
2035 wxChar *argv[4];
2036 int argc = 0;
2037 argv[argc++] = WXSTRINGCAST printerCommand;
2038
2039 // !SM! If we simply assign to argv[1] here, if printer options
2040 // are blank, we get an annoying and confusing message from lpr.
2041 wxChar *opts = WXSTRINGCAST printerOptions;
2042 if (opts && *opts)
2043 argv[argc++] = opts;
2044
2045 argv[argc++] = WXSTRINGCAST filename;
2046 argv[argc++] = (wxChar *) NULL;
2047 #if defined(__WXGTK20__) && wxUSE_UNICODE
2048 #else
2049 wxExecute( argv, TRUE );
2050 #endif
2051 wxRemoveFile( filename );
2052 }
2053 break;
2054 case wxPRINT_MODE_FILE:
2055 case wxPRINT_MODE_NONE:
2056 break;
2057 }
2058 }
2059 #endif
2060 }
2061
2062 void wxPostScriptDC::StartPage()
2063 {
2064 wxCHECK_RET( m_ok && m_pstream, wxT("invalid postscript dc") );
2065
2066 fprintf( m_pstream, "%%%%Page: %d\n", wxPageNumber++ );
2067
2068 // What is this one supposed to do? RR.
2069 // *m_pstream << "matrix currentmatrix\n";
2070
2071 // Added by Chris Breeze
2072
2073 // Each page starts with an "initgraphics" which resets the
2074 // transformation and so we need to reset the origin
2075 // (and rotate the page for landscape printing)
2076
2077 // Output scaling
2078 wxCoord translate_x, translate_y;
2079 double scale_x, scale_y;
2080
2081 translate_x = (wxCoord)m_printData.GetPrinterTranslateX();
2082 translate_y = (wxCoord)m_printData.GetPrinterTranslateY();
2083
2084 scale_x = m_printData.GetPrinterScaleX();
2085 scale_y = m_printData.GetPrinterScaleY();
2086
2087 if (m_printData.GetOrientation() == wxLANDSCAPE)
2088 {
2089 int h;
2090 GetSize( (int*) NULL, &h );
2091 translate_y -= h;
2092 fprintf( m_pstream, "90 rotate\n" );
2093
2094 // I copied this one from a PostScript tutorial, but to no avail. RR.
2095 // fprintf( m_pstream, "90 rotate llx neg ury nef translate\n" );
2096 }
2097
2098 char buffer[100];
2099 sprintf( buffer, "%.8f %.8f scale\n", scale_x / ms_PSScaleFactor,
2100 scale_y / ms_PSScaleFactor);
2101 for (int i = 0; i < 100; i++)
2102 if (buffer[i] == ',') buffer[i] = '.';
2103 fprintf( m_pstream, buffer );
2104
2105 fprintf( m_pstream, "%d %d translate\n", translate_x, translate_y );
2106 }
2107
2108 void wxPostScriptDC::EndPage ()
2109 {
2110 wxCHECK_RET( m_ok && m_pstream, wxT("invalid postscript dc") );
2111
2112 fprintf( m_pstream, "showpage\n" );
2113 }
2114
2115 bool wxPostScriptDC::DoBlit( wxCoord xdest, wxCoord ydest,
2116 wxCoord fwidth, wxCoord fheight,
2117 wxDC *source,
2118 wxCoord xsrc, wxCoord ysrc,
2119 int rop, bool WXUNUSED(useMask), wxCoord WXUNUSED(xsrcMask), wxCoord WXUNUSED(ysrcMask) )
2120 {
2121 wxCHECK_MSG( m_ok && m_pstream, FALSE, wxT("invalid postscript dc") );
2122
2123 wxCHECK_MSG( source, FALSE, wxT("invalid source dc") );
2124
2125 /* blit into a bitmap */
2126 wxBitmap bitmap( (int)fwidth, (int)fheight );
2127 wxMemoryDC memDC;
2128 memDC.SelectObject(bitmap);
2129 memDC.Blit(0, 0, fwidth, fheight, source, xsrc, ysrc, rop); /* TODO: Blit transparently? */
2130 memDC.SelectObject(wxNullBitmap);
2131
2132 /* draw bitmap. scaling and positioning is done there */
2133 DrawBitmap( bitmap, xdest, ydest );
2134
2135 return TRUE;
2136 }
2137
2138 wxCoord wxPostScriptDC::GetCharHeight() const
2139 {
2140 if (m_font.Ok())
2141 return m_font.GetPointSize();
2142 else
2143 return 12;
2144 }
2145
2146 void wxPostScriptDC::DoGetTextExtent(const wxString& string,
2147 wxCoord *x, wxCoord *y,
2148 wxCoord *descent, wxCoord *externalLeading,
2149 wxFont *theFont ) const
2150 {
2151 wxFont *fontToUse = theFont;
2152
2153 if (!fontToUse) fontToUse = (wxFont*) &m_font;
2154
2155 wxCHECK_RET( fontToUse, wxT("GetTextExtent: no font defined") );
2156
2157 const wxWX2MBbuf strbuf = string.mb_str();
2158
2159 #if !wxUSE_AFM_FOR_POSTSCRIPT
2160 /* Provide a VERY rough estimate (avoid using it).
2161 * Produces accurate results for mono-spaced font
2162 * such as Courier (aka wxMODERN) */
2163
2164 int height = 12;
2165 if (fontToUse)
2166 {
2167 height = fontToUse->GetPointSize();
2168 }
2169 if ( x )
2170 *x = strlen (strbuf) * height * 72 / 120;
2171 if ( y )
2172 *y = (wxCoord) (height * 1.32); /* allow for descender */
2173 if (descent) *descent = 0;
2174 if (externalLeading) *externalLeading = 0;
2175 #else
2176
2177 /* method for calculating string widths in postscript:
2178 / read in the AFM (adobe font metrics) file for the
2179 / actual font, parse it and extract the character widths
2180 / and also the descender. this may be improved, but for now
2181 / it works well. the AFM file is only read in if the
2182 / font is changed. this may be chached in the future.
2183 / calls to GetTextExtent with the font unchanged are rather
2184 / efficient!!!
2185 /
2186 / for each font and style used there is an AFM file necessary.
2187 / currently i have only files for the roman font family.
2188 / I try to get files for the other ones!
2189 /
2190 / CAVE: the size of the string is currently always calculated
2191 / in 'points' (1/72 of an inch). this should later on be
2192 / changed to depend on the mapping mode.
2193 / CAVE: the path to the AFM files must be set before calling this
2194 / function. this is usually done by a call like the following:
2195 / wxSetAFMPath("d:\\wxw161\\afm\\");
2196 /
2197 / example:
2198 /
2199 / wxPostScriptDC dc(NULL, TRUE);
2200 / if (dc.Ok()){
2201 / wxSetAFMPath("d:\\wxw161\\afm\\");
2202 / dc.StartDoc("Test");
2203 / dc.StartPage();
2204 / wxCoord w,h;
2205 / dc.SetFont(new wxFont(10, wxROMAN, wxNORMAL, wxNORMAL));
2206 / dc.GetTextExtent("Hallo",&w,&h);
2207 / dc.EndPage();
2208 / dc.EndDoc();
2209 / }
2210 /
2211 / by steve (stefan.hammes@urz.uni-heidelberg.de)
2212 / created: 10.09.94
2213 / updated: 14.05.95 */
2214
2215 /* these static vars are for storing the state between calls */
2216 static int lastFamily= INT_MIN;
2217 static int lastSize= INT_MIN;
2218 static int lastStyle= INT_MIN;
2219 static int lastWeight= INT_MIN;
2220 static int lastDescender = INT_MIN;
2221 static int lastWidths[256]; /* widths of the characters */
2222
2223 double UnderlinePosition = 0.0;
2224 double UnderlineThickness = 0.0;
2225
2226 /* get actual parameters */
2227 int Family = fontToUse->GetFamily();
2228 int Size = fontToUse->GetPointSize();
2229 int Style = fontToUse->GetStyle();
2230 int Weight = fontToUse->GetWeight();
2231
2232 /* if we have another font, read the font-metrics */
2233 if (Family!=lastFamily || Size!=lastSize || Style!=lastStyle || Weight!=lastWeight)
2234 {
2235 /* store actual values */
2236 lastFamily = Family;
2237 lastSize = Size;
2238 lastStyle = Style;
2239 lastWeight = Weight;
2240
2241 const char *name = NULL;
2242
2243 switch (Family)
2244 {
2245 case wxMODERN:
2246 case wxTELETYPE:
2247 {
2248 if ((Style == wxITALIC) && (Weight == wxBOLD)) name = "CourBoO.afm";
2249 else if ((Style != wxITALIC) && (Weight == wxBOLD)) name = "CourBo.afm";
2250 else if ((Style == wxITALIC) && (Weight != wxBOLD)) name = "CourO.afm";
2251 else name = "Cour.afm";
2252 break;
2253 }
2254 case wxROMAN:
2255 {
2256 if ((Style == wxITALIC) && (Weight == wxBOLD)) name = "TimesBoO.afm";
2257 else if ((Style != wxITALIC) && (Weight == wxBOLD)) name = "TimesBo.afm";
2258 else if ((Style == wxITALIC) && (Weight != wxBOLD)) name = "TimesO.afm";
2259 else name = "TimesRo.afm";
2260 break;
2261 }
2262 case wxSCRIPT:
2263 {
2264 name = "Zapf.afm";
2265 Style = wxNORMAL;
2266 Weight = wxNORMAL;
2267 }
2268 case wxSWISS:
2269 default:
2270 {
2271 if ((Style == wxITALIC) && (Weight == wxBOLD)) name = "HelvBoO.afm";
2272 else if ((Style != wxITALIC) && (Weight == wxBOLD)) name = "HelvBo.afm";
2273 else if ((Style == wxITALIC) && (Weight != wxBOLD)) name = "HelvO.afm";
2274 else name = "Helv.afm";
2275 break;
2276 }
2277 }
2278
2279 /* get the directory of the AFM files */
2280 wxString afmName = wxEmptyString;
2281 if (!m_printData.GetFontMetricPath().IsEmpty())
2282 {
2283 afmName = m_printData.GetFontMetricPath();
2284 }
2285
2286 /* 2. open and process the file
2287 / a short explanation of the AFM format:
2288 / we have for each character a line, which gives its size
2289 / e.g.:
2290 /
2291 / C 63 ; WX 444 ; N question ; B 49 -14 395 676 ;
2292 /
2293 / that means, we have a character with ascii code 63, and width
2294 / (444/1000 * fontSize) points.
2295 / the other data is ignored for now!
2296 /
2297 / when the font has changed, we read in the right AFM file and store the
2298 / character widths in an array, which is processed below (see point 3.). */
2299
2300 /* new elements JC Sun Aug 25 23:21:44 MET DST 1996 */
2301
2302 afmName << name;
2303 FILE *afmFile = wxFopen(afmName,wxT("r"));
2304
2305 if (afmFile==NULL)
2306 {
2307 afmName = wxThePrintSetupData->GetAFMPath();
2308 afmName << wxFILE_SEP_PATH << name;
2309 afmFile = wxFopen(afmName,wxT("r"));
2310 }
2311
2312 #if defined(__UNIX__) && !defined(__VMS__)
2313 if (afmFile==NULL)
2314 /* please do NOT change the line above to "else if (afmFile==NULL)" -
2315 - afmFile = fopen() may fail and in that case the next if branch
2316 MUST be executed - and it would not if there was "else" */
2317 {
2318 afmName = wxGetDataDir();
2319 afmName << wxFILE_SEP_PATH
2320 #if defined(__LINUX__) || defined(__FREEBSD__)
2321 << wxT("gs_afm") << wxFILE_SEP_PATH
2322 #else
2323 << wxT("afm") << wxFILE_SEP_PATH
2324 #endif
2325 << name;
2326 afmFile = wxFopen(afmName,wxT("r"));
2327 }
2328 #endif
2329
2330 if (afmFile==NULL)
2331 {
2332 wxLogDebug( wxT("GetTextExtent: can't open AFM file '%s'"), afmName.c_str() );
2333 wxLogDebug( wxT(" using approximate values"));
2334 for (int i=0; i<256; i++) lastWidths[i] = 500; /* an approximate value */
2335 lastDescender = -150; /* dito. */
2336 }
2337 else
2338 {
2339 /* init the widths array */
2340 for(int i=0; i<256; i++) lastWidths[i] = INT_MIN;
2341 /* some variables for holding parts of a line */
2342 char cString[10],semiString[10],WXString[10],descString[20];
2343 char upString[30], utString[30], encString[50];
2344 char line[256];
2345 int ascii,cWidth;
2346 /* read in the file and parse it */
2347 while(fgets(line,sizeof(line),afmFile)!=NULL)
2348 {
2349 /* A.) check for descender definition */
2350 if (strncmp(line,"Descender",9)==0)
2351 {
2352 if ((sscanf(line,"%s%d",descString,&lastDescender)!=2) ||
2353 (strcmp(descString,"Descender")!=0))
2354 {
2355 wxLogDebug( wxT("AFM-file '%s': line '%s' has error (bad descender)"), afmName.c_str(),line );
2356 }
2357 }
2358 /* JC 1.) check for UnderlinePosition */
2359 else if(strncmp(line,"UnderlinePosition",17)==0)
2360 {
2361 if ((sscanf(line,"%s%lf",upString,&UnderlinePosition)!=2) ||
2362 (strcmp(upString,"UnderlinePosition")!=0))
2363 {
2364 wxLogDebug( wxT("AFM-file '%s': line '%s' has error (bad UnderlinePosition)"), afmName.c_str(), line );
2365 }
2366 }
2367 /* JC 2.) check for UnderlineThickness */
2368 else if(strncmp(line,"UnderlineThickness",18)==0)
2369 {
2370 if ((sscanf(line,"%s%lf",utString,&UnderlineThickness)!=2) ||
2371 (strcmp(utString,"UnderlineThickness")!=0))
2372 {
2373 wxLogDebug( wxT("AFM-file '%s': line '%s' has error (bad UnderlineThickness)"), afmName.c_str(), line );
2374 }
2375 }
2376 /* JC 3.) check for EncodingScheme */
2377 else if(strncmp(line,"EncodingScheme",14)==0)
2378 {
2379 if ((sscanf(line,"%s%s",utString,encString)!=2) ||
2380 (strcmp(utString,"EncodingScheme")!=0))
2381 {
2382 wxLogDebug( wxT("AFM-file '%s': line '%s' has error (bad EncodingScheme)"), afmName.c_str(), line );
2383 }
2384 else if (strncmp(encString, "AdobeStandardEncoding", 21))
2385 {
2386 wxLogDebug( wxT("AFM-file '%s': line '%s' has error (unsupported EncodingScheme %s)"),
2387 afmName.c_str(),line, encString);
2388 }
2389 }
2390 /* B.) check for char-width */
2391 else if(strncmp(line,"C ",2)==0)
2392 {
2393 if (sscanf(line,"%s%d%s%s%d",cString,&ascii,semiString,WXString,&cWidth)!=5)
2394 {
2395 wxLogDebug(wxT("AFM-file '%s': line '%s' has an error (bad character width)"),afmName.c_str(),line);
2396 }
2397 if(strcmp(cString,"C")!=0 || strcmp(semiString,";")!=0 || strcmp(WXString,"WX")!=0)
2398 {
2399 wxLogDebug(wxT("AFM-file '%s': line '%s' has a format error"),afmName.c_str(),line);
2400 }
2401 /* printf(" char '%c'=%d has width '%d'\n",ascii,ascii,cWidth); */
2402 if (ascii>=0 && ascii<256)
2403 {
2404 lastWidths[ascii] = cWidth; /* store width */
2405 }
2406 else
2407 {
2408 /* MATTHEW: this happens a lot; don't print an error */
2409 /* wxLogDebug("AFM-file '%s': ASCII value %d out of range",afmName.c_str(),ascii); */
2410 }
2411 }
2412 /* C.) ignore other entries. */
2413 }
2414 fclose(afmFile);
2415 }
2416 /* hack to compute correct values for german 'Umlaute'
2417 / the correct way would be to map the character names
2418 / like 'adieresis' to corresp. positions of ISOEnc and read
2419 / these values from AFM files, too. Maybe later ... */
2420 lastWidths[196] = lastWidths['A']; // Ä
2421 lastWidths[228] = lastWidths['a']; // ä
2422 lastWidths[214] = lastWidths['O']; // Ö
2423 lastWidths[246] = lastWidths['o']; // ö
2424 lastWidths[220] = lastWidths['U']; // Ü
2425 lastWidths[252] = lastWidths['u']; // ü
2426 lastWidths[223] = lastWidths[251]; // ß
2427
2428 /* JC: calculate UnderlineThickness/UnderlinePosition */
2429
2430 // VS: dirty, but is there any better solution?
2431 double *pt;
2432 pt = (double*) &m_underlinePosition;
2433 *pt = LogicalToDeviceYRel((wxCoord)(UnderlinePosition * fontToUse->GetPointSize())) / 1000.0f;
2434 pt = (double*) &m_underlineThickness;
2435 *pt = LogicalToDeviceYRel((wxCoord)(UnderlineThickness * fontToUse->GetPointSize())) / 1000.0f;
2436
2437 }
2438
2439
2440 /* 3. now the font metrics are read in, calc size this
2441 / is done by adding the widths of the characters in the
2442 / string. they are given in 1/1000 of the size! */
2443
2444 long sum=0;
2445 wxCoord height=Size; /* by default */
2446 unsigned char *p;
2447 for(p=(unsigned char *)wxMBSTRINGCAST strbuf; *p; p++)
2448 {
2449 if(lastWidths[*p]== INT_MIN)
2450 {
2451 wxLogDebug(wxT("GetTextExtent: undefined width for character '%c' (%d)"), *p,*p);
2452 sum += lastWidths[' ']; /* assume space */
2453 }
2454 else
2455 {
2456 sum += lastWidths[*p];
2457 }
2458 }
2459
2460 double widthSum = sum;
2461 widthSum *= Size;
2462 widthSum /= 1000.0F;
2463
2464 /* add descender to height (it is usually a negative value) */
2465 //if (lastDescender != INT_MIN)
2466 //{
2467 // height += (wxCoord)(((-lastDescender)/1000.0F) * Size); /* MATTHEW: forgot scale */
2468 //}
2469 // - commented by V. Slavik - height already contains descender in it
2470 // (judging from few experiments)
2471
2472 /* return size values */
2473 if ( x )
2474 *x = (wxCoord)widthSum;
2475 if ( y )
2476 *y = height;
2477
2478 /* return other parameters */
2479 if (descent)
2480 {
2481 if(lastDescender!=INT_MIN)
2482 {
2483 *descent = (wxCoord)(((-lastDescender)/1000.0F) * Size); /* MATTHEW: forgot scale */
2484 }
2485 else
2486 {
2487 *descent = 0;
2488 }
2489 }
2490
2491 /* currently no idea how to calculate this! */
2492 if (externalLeading) *externalLeading = 0;
2493
2494 #endif
2495 }
2496
2497 // Determine the Default Postscript Previewer
2498 // available on the platform
2499 #if defined(__SUN__) && defined(__XVIEW__)
2500 // OpenWindow/NeWS's Postscript Previewer
2501 # define PS_VIEWER_PROG "pageview"
2502 #elif defined(__VMS__)
2503 #define PS_VIEWER_PROG "view/format=ps/select=x_display"
2504 #elif defined(__SGI__)
2505 // SGI's Display Postscript Previewer
2506 //# define PS_VIEWER_PROG "dps"
2507 # define PS_VIEWER_PROG "xpsview"
2508 #elif defined(__X__) || defined(__WXGTK__)
2509 // Front-end to ghostscript
2510 # define PS_VIEWER_PROG "ghostview"
2511 #else
2512 // Windows ghostscript/ghostview
2513 # define PS_VIEWER_PROG "gsview"
2514 #endif
2515
2516 wxPrintSetupData *wxThePrintSetupData = (wxPrintSetupData *) NULL;
2517
2518 IMPLEMENT_DYNAMIC_CLASS(wxPostScriptDC, wxDC)
2519 IMPLEMENT_DYNAMIC_CLASS(wxPrintSetupData, wxObject)
2520
2521 // Redundant now I think
2522 #if 1
2523 IMPLEMENT_CLASS(wxPostScriptPrintDialog, wxDialog)
2524
2525 wxPostScriptPrintDialog::wxPostScriptPrintDialog (wxWindow *parent, const wxString& title,
2526 const wxPoint& pos, const wxSize& size, wxCoord style):
2527 wxDialog(parent, -1, title, pos, size, style)
2528 {
2529 wxBeginBusyCursor();
2530
2531 char buf[100];
2532 int yPos = 40;
2533 wxString
2534 *orientation = new wxString[2],
2535 *print_modes = new wxString[3];
2536 int features;
2537 wxCoord wx_printer_translate_x, wx_printer_translate_y;
2538 double wx_printer_scale_x, wx_printer_scale_y;
2539
2540 orientation[0] = _("Portrait");
2541 orientation[1] = _("Landscape");
2542
2543 print_modes[0] = _("Send to Printer");
2544 print_modes[1] = _("Print to File");
2545 print_modes[2] = _("Preview Only");
2546
2547
2548
2549 wxButton *okBut = new wxButton (this, wxID_OK, _("OK"), wxPoint(5, 5));
2550 (void) new wxButton (this, wxID_CANCEL, _("Cancel"), wxPoint(40, 5));
2551 okBut->SetDefault();
2552
2553
2554 #if defined(__WXGTK__) || defined (__WXMOTIF__)
2555 (void) new wxStaticText( this, -1, _("Printer Command: "),
2556 wxPoint(5, yPos) );
2557 (void) new wxTextCtrl( this, wxID_PRINTER_COMMAND, wxThePrintSetupData->GetPrinterCommand(),
2558 wxPoint(100, yPos), wxSize(100, -1) );
2559
2560 (void) new wxStaticText( this, -1, _("Printer Options: "),
2561 wxPoint(210, yPos) );
2562 (void) new wxTextCtrl( this, wxID_PRINTER_OPTIONS, wxThePrintSetupData->GetPrinterOptions(),
2563 wxPoint(305, yPos), wxSize(150, -1) );
2564
2565 yPos += 40;
2566 #endif
2567
2568
2569 wxRadioBox *radio0 = new wxRadioBox(this, wxID_PRINTER_ORIENTATION, "Orientation: ", wxPoint(5, yPos), wxSize(-1,-1),
2570 2,orientation,2,wxRA_SPECIFY_ROWS);
2571 radio0->SetSelection((int)wxThePrintSetupData->GetPrinterOrientation() - 1);
2572
2573 // @@@ Configuration hook
2574 if ( !wxThePrintSetupData->GetPrintPreviewCommand() )
2575 wxThePrintSetupData->SetPrintPreviewCommand(PS_VIEWER_PROG);
2576
2577 // wxGetResource ("wxWindows", "PSView", &wxThePrintSetupData->m_previewCommand);
2578
2579 features = (wxThePrintSetupData->GetPrintPreviewCommand() &&
2580 *wxThePrintSetupData->GetPrintPreviewCommand()) ? 3 : 2;
2581
2582 wxRadioBox *radio1 = new wxRadioBox(this, wxID_PRINTER_MODES, _("PostScript:"),
2583 wxPoint(150, yPos),
2584 wxSize(-1,-1), features,
2585 print_modes, features, wxRA_SPECIFY_ROWS);
2586
2587 #ifdef __WXMSW__
2588 radio1->Enable(0, FALSE);
2589 if (wxThePrintSetupData->GetPrintPreviewCommand() && *wxThePrintSetupData->GetPrintPreviewCommand())
2590 radio1->Enable(2, FALSE);
2591 #endif
2592
2593 radio1->SetSelection((int)wxThePrintSetupData->GetPrinterMode());
2594 wxThePrintSetupData->GetPrinterTranslation(&wx_printer_translate_x, &wx_printer_translate_y);
2595 wxThePrintSetupData->GetPrinterScaling(&wx_printer_scale_x, &wx_printer_scale_y);
2596
2597 sprintf (buf, "%.2f", wx_printer_scale_x);
2598
2599 yPos += 90;
2600 (void) new wxStaticText(this, -1, _("X Scaling"), wxPoint(5, yPos));
2601 /* wxTextCtrl *text1 = */ (void) new wxTextCtrl(this, wxID_PRINTER_X_SCALE, buf, wxPoint(100, yPos), wxSize(100, -1));
2602
2603 sprintf (buf, "%.2f", wx_printer_scale_y);
2604 (void) new wxStaticText(this, -1, _("Y Scaling"), wxPoint(220, yPos));
2605 /* wxTextCtrl *text2 = */ (void) new wxTextCtrl(this, wxID_PRINTER_Y_SCALE, buf, wxPoint(320, yPos), wxSize(100, -1));
2606
2607 yPos += 25;
2608
2609 (void) new wxStaticText(this, -1, _("X Translation"), wxPoint(5, yPos));
2610 sprintf (buf, "%.2d", wx_printer_translate_x);
2611 /* wxTextCtrl *text3 = */ (void) new wxTextCtrl(this, wxID_PRINTER_X_TRANS, buf, wxPoint(100, yPos), wxSize(100, -1));
2612
2613 (void) new wxStaticText(this, -1, _("Y Translation"), wxPoint(220, yPos));
2614 sprintf (buf, "%.2d", wx_printer_translate_y);
2615 /* wxTextCtrl *text4 = */ (void) new wxTextCtrl(this, wxID_PRINTER_Y_TRANS, buf, wxPoint(320, yPos), wxSize(100, -1));
2616
2617 Fit ();
2618
2619 delete[] orientation;
2620 delete[] print_modes;
2621
2622 wxEndBusyCursor();
2623 }
2624
2625 int wxPostScriptPrintDialog::ShowModal ()
2626 {
2627 if ( wxDialog::ShowModal() == wxID_OK )
2628 {
2629 // wxTextCtrl *text0 = (wxTextCtrl *)FindWindow(wxID_PRINTER_OPTIONS);
2630 wxTextCtrl *text1 = (wxTextCtrl *)FindWindow(wxID_PRINTER_X_SCALE);
2631 wxTextCtrl *text2 = (wxTextCtrl *)FindWindow(wxID_PRINTER_Y_SCALE);
2632 wxTextCtrl *text3 = (wxTextCtrl *)FindWindow(wxID_PRINTER_X_TRANS);
2633 wxTextCtrl *text4 = (wxTextCtrl *)FindWindow(wxID_PRINTER_Y_TRANS);
2634 // wxTextCtrl *text_prt = (wxTextCtrl *)FindWindow(wxID_PRINTER_COMMAND);
2635 wxRadioBox *radio0 = (wxRadioBox *)FindWindow(wxID_PRINTER_ORIENTATION);
2636 wxRadioBox *radio1 = (wxRadioBox *)FindWindow(wxID_PRINTER_MODES);
2637
2638 StringToDouble (WXSTRINGCAST text1->GetValue (), &wxThePrintSetupData->m_printerScaleX);
2639 StringToDouble (WXSTRINGCAST text2->GetValue (), &wxThePrintSetupData->m_printerScaleY);
2640
2641 long dummy;
2642 StringToLong (WXSTRINGCAST text3->GetValue (), &dummy);
2643 wxThePrintSetupData->m_printerTranslateX = (wxCoord)dummy;
2644 StringToLong (WXSTRINGCAST text4->GetValue (), &dummy);
2645 wxThePrintSetupData->m_printerTranslateY = (wxCoord)dummy;
2646
2647 #ifdef __X__
2648 // wxThePrintSetupData->SetPrinterOptions(WXSTRINGCAST text0->GetValue ());
2649 // wxThePrintSetupData->SetPrinterCommand(WXSTRINGCAST text_prt->GetValue ());
2650 #endif
2651
2652 wxThePrintSetupData->SetPrinterOrientation((radio0->GetSelection() == 1 ? wxLANDSCAPE : wxPORTRAIT));
2653
2654 // C++ wants this
2655 switch ( radio1->GetSelection() ) {
2656 case 0: wxThePrintSetupData->SetPrinterMode(PS_PRINTER); break;
2657 case 1: wxThePrintSetupData->SetPrinterMode(PS_FILE); break;
2658 case 2: wxThePrintSetupData->SetPrinterMode(PS_PREVIEW); break;
2659 }
2660 return wxID_OK;
2661 }
2662 return wxID_CANCEL;
2663 }
2664 #endif
2665 // 0 (redundant)
2666
2667 // PostScript printer settings
2668 // RETAINED FOR BACKWARD COMPATIBILITY
2669 void wxSetPrinterCommand(const wxString& cmd)
2670 {
2671 wxThePrintSetupData->SetPrinterCommand(cmd);
2672 }
2673
2674 void wxSetPrintPreviewCommand(const wxString& cmd)
2675 {
2676 wxThePrintSetupData->SetPrintPreviewCommand(cmd);
2677 }
2678
2679 void wxSetPrinterOptions(const wxString& flags)
2680 {
2681 wxThePrintSetupData->SetPrinterOptions(flags);
2682 }
2683
2684 void wxSetPrinterFile(const wxString& f)
2685 {
2686 wxThePrintSetupData->SetPrinterFile(f);
2687 }
2688
2689 void wxSetPrinterOrientation(int orient)
2690 {
2691 wxThePrintSetupData->SetPrinterOrientation(orient);
2692 }
2693
2694 void wxSetPrinterScaling(double x, double y)
2695 {
2696 wxThePrintSetupData->SetPrinterScaling(x, y);
2697 }
2698
2699 void wxSetPrinterTranslation(wxCoord x, wxCoord y)
2700 {
2701 wxThePrintSetupData->SetPrinterTranslation(x, y);
2702 }
2703
2704 // 1 = Preview, 2 = print to file, 3 = send to printer
2705 void wxSetPrinterMode(int mode)
2706 {
2707 wxThePrintSetupData->SetPrinterMode(mode);
2708 }
2709
2710 void wxSetAFMPath(const wxString& f)
2711 {
2712 wxThePrintSetupData->SetAFMPath(f);
2713 }
2714
2715 // Get current values
2716 wxString wxGetPrinterCommand()
2717 {
2718 return wxThePrintSetupData->GetPrinterCommand();
2719 }
2720
2721 wxString wxGetPrintPreviewCommand()
2722 {
2723 return wxThePrintSetupData->GetPrintPreviewCommand();
2724 }
2725
2726 wxString wxGetPrinterOptions()
2727 {
2728 return wxThePrintSetupData->GetPrinterOptions();
2729 }
2730
2731 wxString wxGetPrinterFile()
2732 {
2733 return wxThePrintSetupData->GetPrinterFile();
2734 }
2735
2736 int wxGetPrinterOrientation()
2737 {
2738 return wxThePrintSetupData->GetPrinterOrientation();
2739 }
2740
2741 void wxGetPrinterScaling(double* x, double* y)
2742 {
2743 wxThePrintSetupData->GetPrinterScaling(x, y);
2744 }
2745
2746 void wxGetPrinterTranslation(wxCoord *x, wxCoord *y)
2747 {
2748 wxThePrintSetupData->GetPrinterTranslation(x, y);
2749 }
2750
2751 int wxGetPrinterMode()
2752 {
2753 return wxThePrintSetupData->GetPrinterMode();
2754 }
2755
2756 wxString wxGetAFMPath()
2757 {
2758 return wxThePrintSetupData->GetAFMPath();
2759 }
2760
2761 /*
2762 * Print setup data
2763 */
2764
2765 wxPrintSetupData::wxPrintSetupData()
2766 {
2767 m_printerOrient = wxPORTRAIT;
2768 m_printerScaleX = (double)1.0;
2769 m_printerScaleY = (double)1.0;
2770 m_printerTranslateX = 0;
2771 m_printerTranslateY = 0;
2772 m_printerMode = wxPRINT_MODE_FILE;
2773 m_printColour = TRUE;
2774 }
2775
2776 wxPrintSetupData::~wxPrintSetupData()
2777 {
2778 }
2779
2780 void wxPrintSetupData::operator=(wxPrintSetupData& data)
2781 {
2782 SetPrinterCommand(data.GetPrinterCommand());
2783 SetPrintPreviewCommand(data.GetPrintPreviewCommand());
2784 SetPrinterOptions(data.GetPrinterOptions());
2785 wxCoord x, y;
2786 data.GetPrinterTranslation(&x, &y);
2787 SetPrinterTranslation(x, y);
2788
2789 double x1, y1;
2790 data.GetPrinterScaling(&x1, &y1);
2791 SetPrinterScaling(x1, y1);
2792
2793 SetPrinterOrientation(data.GetPrinterOrientation());
2794 SetPrinterMode(data.GetPrinterMode());
2795 SetAFMPath(data.GetAFMPath());
2796 SetPaperName(data.GetPaperName());
2797 SetColour(data.GetColour());
2798 }
2799
2800 // Initialize from a wxPrintData object (wxPrintData should now be used instead of wxPrintSetupData).
2801 void wxPrintSetupData::operator=(const wxPrintData& data)
2802 {
2803 SetPrinterCommand(data.GetPrinterCommand());
2804 SetPrintPreviewCommand(data.GetPreviewCommand());
2805 SetPrinterOptions(data.GetPrinterOptions());
2806 SetPrinterTranslation((wxCoord)data.GetPrinterTranslateX(),
2807 (wxCoord)data.GetPrinterTranslateY());
2808 SetPrinterScaling(data.GetPrinterScaleX(), data.GetPrinterScaleY());
2809 SetPrinterOrientation(data.GetOrientation());
2810 SetPrinterMode((int) data.GetPrintMode());
2811 SetAFMPath(data.GetFontMetricPath());
2812 SetPaperName(wxThePrintPaperDatabase->ConvertIdToName(data.GetPaperId()));
2813 SetColour(data.GetColour());
2814 SetPrinterFile(data.GetFilename());
2815 }
2816
2817 void wxInitializePrintSetupData(bool init)
2818 {
2819 if (init)
2820 {
2821 wxThePrintSetupData = new wxPrintSetupData;
2822
2823 wxThePrintSetupData->SetPrintPreviewCommand(PS_VIEWER_PROG);
2824 wxThePrintSetupData->SetPrinterOrientation(wxPORTRAIT);
2825 wxThePrintSetupData->SetPrinterMode(wxPRINT_MODE_PREVIEW);
2826 wxThePrintSetupData->SetPaperName(_("A4 sheet, 210 x 297 mm"));
2827
2828 // Could have a .ini file to read in some defaults
2829 // - and/or use environment variables, e.g. WXWIN
2830 #ifdef __VMS__
2831 wxThePrintSetupData->SetPrinterCommand("print");
2832 wxThePrintSetupData->SetPrinterOptions("/nonotify/queue=psqueue");
2833 wxThePrintSetupData->SetAFMPath("sys$ps_font_metrics:");
2834 #endif
2835 #ifdef __WXMSW__
2836 wxThePrintSetupData->SetPrinterCommand("print");
2837 wxThePrintSetupData->SetAFMPath("c:\\windows\\system\\");
2838 wxThePrintSetupData->SetPrinterOptions("");
2839 #endif
2840 #if !defined(__VMS__) && !defined(__WXMSW__)
2841 wxThePrintSetupData->SetPrinterCommand("lpr");
2842 wxThePrintSetupData->SetPrinterOptions("");
2843 wxThePrintSetupData->SetAFMPath("");
2844 #endif
2845 }
2846 else
2847 {
2848 if (wxThePrintSetupData)
2849 delete wxThePrintSetupData;
2850 wxThePrintSetupData = (wxPrintSetupData *) NULL;
2851 }
2852 }
2853
2854 // A module to allow initialization/cleanup of PostScript-related
2855 // things without calling these functions from app.cpp.
2856
2857 class WXDLLEXPORT wxPostScriptModule: public wxModule
2858 {
2859 DECLARE_DYNAMIC_CLASS(wxPostScriptModule)
2860 public:
2861 wxPostScriptModule() {}
2862 bool OnInit();
2863 void OnExit();
2864 };
2865
2866 IMPLEMENT_DYNAMIC_CLASS(wxPostScriptModule, wxModule)
2867
2868 /*
2869 * Initialization/cleanup module
2870 */
2871
2872 bool wxPostScriptModule::OnInit()
2873 {
2874 wxInitializePrintSetupData();
2875
2876 return TRUE;
2877 }
2878
2879 void wxPostScriptModule::OnExit()
2880 {
2881 wxInitializePrintSetupData(FALSE);
2882 }
2883
2884 #endif
2885 // wxUSE_POSTSCRIPT
2886
2887 #endif
2888 // wxUSE_PRINTING_ARCHITECTURE