Further wxDC changes
[wxWidgets.git] / src / generic / dcpsg.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/generic/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
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
11
12 #include "wx/wxprec.h"
13
14 #ifdef __BORLANDC__
15 #pragma hdrstop
16 #endif
17
18 #if wxUSE_PRINTING_ARCHITECTURE && wxUSE_POSTSCRIPT
19
20 #include "wx/generic/dcpsg.h"
21
22 #ifndef WX_PRECOMP
23 #include "wx/intl.h"
24 #include "wx/log.h"
25 #include "wx/utils.h"
26 #include "wx/dcmemory.h"
27 #include "wx/math.h"
28 #include "wx/image.h"
29 #include "wx/icon.h"
30 #endif // WX_PRECOMP
31
32 #include "wx/prntbase.h"
33 #include "wx/generic/prntdlgg.h"
34 #include "wx/paper.h"
35 #include "wx/filefn.h"
36 #include "wx/stdpaths.h"
37
38 #ifdef __WXMSW__
39
40 #ifdef DrawText
41 #undef DrawText
42 #endif
43
44 #ifdef StartDoc
45 #undef StartDoc
46 #endif
47
48 #ifdef GetCharWidth
49 #undef GetCharWidth
50 #endif
51
52 #ifdef FindWindow
53 #undef FindWindow
54 #endif
55
56 #endif
57
58 //-----------------------------------------------------------------------------
59 // start and end of document/page
60 //-----------------------------------------------------------------------------
61
62 static const char *wxPostScriptHeaderConicTo = "\
63 /conicto {\n\
64 /to_y exch def\n\
65 /to_x exch def\n\
66 /conic_cntrl_y exch def\n\
67 /conic_cntrl_x exch def\n\
68 currentpoint\n\
69 /p0_y exch def\n\
70 /p0_x exch def\n\
71 /p1_x p0_x conic_cntrl_x p0_x sub 2 3 div mul add def\n\
72 /p1_y p0_y conic_cntrl_y p0_y sub 2 3 div mul add def\n\
73 /p2_x p1_x to_x p0_x sub 1 3 div mul add def\n\
74 /p2_y p1_y to_y p0_y sub 1 3 div mul add def\n\
75 p1_x p1_y p2_x p2_y to_x to_y curveto\n\
76 } bind def\n\
77 ";
78
79 static const char *wxPostScriptHeaderEllipse = "\
80 /ellipsedict 8 dict def\n\
81 ellipsedict /mtrx matrix put\n\
82 /ellipse {\n\
83 ellipsedict begin\n\
84 /endangle exch def\n\
85 /startangle exch def\n\
86 /yrad exch def\n\
87 /xrad exch def\n\
88 /y exch def\n\
89 /x exch def\n\
90 /savematrix mtrx currentmatrix def\n\
91 x y translate\n\
92 xrad yrad scale\n\
93 0 0 1 startangle endangle arc\n\
94 savematrix setmatrix\n\
95 end\n\
96 } def\n\
97 ";
98
99 static const char *wxPostScriptHeaderEllipticArc= "\
100 /ellipticarcdict 8 dict def\n\
101 ellipticarcdict /mtrx matrix put\n\
102 /ellipticarc\n\
103 { ellipticarcdict begin\n\
104 /do_fill exch def\n\
105 /endangle exch def\n\
106 /startangle exch def\n\
107 /yrad exch def\n\
108 /xrad exch def \n\
109 /y exch def\n\
110 /x exch def\n\
111 /savematrix mtrx currentmatrix def\n\
112 x y translate\n\
113 xrad yrad scale\n\
114 do_fill { 0 0 moveto } if\n\
115 0 0 1 startangle endangle arc\n\
116 savematrix setmatrix\n\
117 do_fill { fill }{ stroke } ifelse\n\
118 end\n\
119 } def\n";
120
121 static const char *wxPostScriptHeaderSpline = "\
122 /DrawSplineSection {\n\
123 /y3 exch def\n\
124 /x3 exch def\n\
125 /y2 exch def\n\
126 /x2 exch def\n\
127 /y1 exch def\n\
128 /x1 exch def\n\
129 /xa x1 x2 x1 sub 0.666667 mul add def\n\
130 /ya y1 y2 y1 sub 0.666667 mul add def\n\
131 /xb x3 x2 x3 sub 0.666667 mul add def\n\
132 /yb y3 y2 y3 sub 0.666667 mul add def\n\
133 x1 y1 lineto\n\
134 xa ya xb yb x3 y3 curveto\n\
135 } def\n\
136 ";
137
138 static const char *wxPostScriptHeaderColourImage = "\
139 % define 'colorimage' if it isn't defined\n\
140 % ('colortogray' and 'mergeprocs' come from xwd2ps\n\
141 % via xgrab)\n\
142 /colorimage where % do we know about 'colorimage'?\n\
143 { pop } % yes: pop off the 'dict' returned\n\
144 { % no: define one\n\
145 /colortogray { % define an RGB->I function\n\
146 /rgbdata exch store % call input 'rgbdata'\n\
147 rgbdata length 3 idiv\n\
148 /npixls exch store\n\
149 /rgbindx 0 store\n\
150 0 1 npixls 1 sub {\n\
151 grays exch\n\
152 rgbdata rgbindx get 20 mul % Red\n\
153 rgbdata rgbindx 1 add get 32 mul % Green\n\
154 rgbdata rgbindx 2 add get 12 mul % Blue\n\
155 add add 64 idiv % I = .5G + .31R + .18B\n\
156 put\n\
157 /rgbindx rgbindx 3 add store\n\
158 } for\n\
159 grays 0 npixls getinterval\n\
160 } bind def\n\
161 \n\
162 % Utility procedure for colorimage operator.\n\
163 % This procedure takes two procedures off the\n\
164 % stack and merges them into a single procedure.\n\
165 \n\
166 /mergeprocs { % def\n\
167 dup length\n\
168 3 -1 roll\n\
169 dup\n\
170 length\n\
171 dup\n\
172 5 1 roll\n\
173 3 -1 roll\n\
174 add\n\
175 array cvx\n\
176 dup\n\
177 3 -1 roll\n\
178 0 exch\n\
179 putinterval\n\
180 dup\n\
181 4 2 roll\n\
182 putinterval\n\
183 } bind def\n\
184 \n\
185 /colorimage { % def\n\
186 pop pop % remove 'false 3' operands\n\
187 {colortogray} mergeprocs\n\
188 image\n\
189 } bind def\n\
190 } ifelse % end of 'false' case\n\
191 ";
192
193 static char wxPostScriptHeaderReencodeISO1[] =
194 "\n/reencodeISO {\n"
195 "dup dup findfont dup length dict begin\n"
196 "{ 1 index /FID ne { def }{ pop pop } ifelse } forall\n"
197 "/Encoding ISOLatin1Encoding def\n"
198 "currentdict end definefont\n"
199 "} def\n"
200 "/ISOLatin1Encoding [\n"
201 "/.notdef/.notdef/.notdef/.notdef/.notdef/.notdef/.notdef/.notdef\n"
202 "/.notdef/.notdef/.notdef/.notdef/.notdef/.notdef/.notdef/.notdef\n"
203 "/.notdef/.notdef/.notdef/.notdef/.notdef/.notdef/.notdef/.notdef\n"
204 "/.notdef/.notdef/.notdef/.notdef/.notdef/.notdef/.notdef/.notdef\n"
205 "/space/exclam/quotedbl/numbersign/dollar/percent/ampersand/quoteright\n"
206 "/parenleft/parenright/asterisk/plus/comma/minus/period/slash\n"
207 "/zero/one/two/three/four/five/six/seven/eight/nine/colon/semicolon\n"
208 "/less/equal/greater/question/at/A/B/C/D/E/F/G/H/I/J/K/L/M/N\n"
209 "/O/P/Q/R/S/T/U/V/W/X/Y/Z/bracketleft/backslash/bracketright\n"
210 "/asciicircum/underscore/quoteleft/a/b/c/d/e/f/g/h/i/j/k/l/m\n"
211 "/n/o/p/q/r/s/t/u/v/w/x/y/z/braceleft/bar/braceright/asciitilde\n"
212 "/.notdef/.notdef/.notdef/.notdef/.notdef/.notdef/.notdef/.notdef\n"
213 "/.notdef/.notdef/.notdef/.notdef/.notdef/.notdef/.notdef/.notdef\n"
214 "/.notdef/dotlessi/grave/acute/circumflex/tilde/macron/breve\n"
215 "/dotaccent/dieresis/.notdef/ring/cedilla/.notdef/hungarumlaut\n";
216
217 static char wxPostScriptHeaderReencodeISO2[] =
218 "/ogonek/caron/space/exclamdown/cent/sterling/currency/yen/brokenbar\n"
219 "/section/dieresis/copyright/ordfeminine/guillemotleft/logicalnot\n"
220 "/hyphen/registered/macron/degree/plusminus/twosuperior/threesuperior\n"
221 "/acute/mu/paragraph/periodcentered/cedilla/onesuperior/ordmasculine\n"
222 "/guillemotright/onequarter/onehalf/threequarters/questiondown\n"
223 "/Agrave/Aacute/Acircumflex/Atilde/Adieresis/Aring/AE/Ccedilla\n"
224 "/Egrave/Eacute/Ecircumflex/Edieresis/Igrave/Iacute/Icircumflex\n"
225 "/Idieresis/Eth/Ntilde/Ograve/Oacute/Ocircumflex/Otilde/Odieresis\n"
226 "/multiply/Oslash/Ugrave/Uacute/Ucircumflex/Udieresis/Yacute\n"
227 "/Thorn/germandbls/agrave/aacute/acircumflex/atilde/adieresis\n"
228 "/aring/ae/ccedilla/egrave/eacute/ecircumflex/edieresis/igrave\n"
229 "/iacute/icircumflex/idieresis/eth/ntilde/ograve/oacute/ocircumflex\n"
230 "/otilde/odieresis/divide/oslash/ugrave/uacute/ucircumflex/udieresis\n"
231 "/yacute/thorn/ydieresis\n"
232 "] def\n\n";
233
234 //-------------------------------------------------------------------------------
235 // wxPostScriptDC
236 //-------------------------------------------------------------------------------
237
238
239 #if wxUSE_NEW_DC
240
241 IMPLEMENT_DYNAMIC_CLASS(wxPostScriptDC, wxDC)
242
243 wxPostScriptDC::wxPostScriptDC()
244 {
245 m_pimpl = new wxPostScriptImplDC( this );
246 }
247
248 wxPostScriptDC::wxPostScriptDC(const wxPrintData& printData)
249 {
250 m_pimpl = new wxPostScriptImplDC( this, printData );
251 }
252
253 #endif
254
255
256 // conversion
257 static const double RAD2DEG = 180.0 / M_PI;
258
259 // we don't want to use only 72 dpi from PS print
260 static const int DPI = 600;
261 static const double PS2DEV = 600.0 / 72.0;
262 static const double DEV2PS = 72.0 / 600.0;
263
264 #define XLOG2DEV(x) ((double)(LogicalToDeviceX(x)) * DEV2PS)
265 #define XLOG2DEVREL(x) ((double)(LogicalToDeviceXRel(x)) * DEV2PS)
266 #define YLOG2DEV(x) ((m_pageHeight - (double)LogicalToDeviceY(x)) * DEV2PS)
267 #define YLOG2DEVREL(x) ((double)(LogicalToDeviceYRel(x)) * DEV2PS)
268
269
270 #if wxUSE_NEW_DC
271 IMPLEMENT_ABSTRACT_CLASS(wxPostScriptImplDC, wxImplDC)
272 #else
273 IMPLEMENT_ABSTRACT_CLASS(wxPostScriptDC, wxDC)
274 #endif
275
276 //-------------------------------------------------------------------------------
277
278 #if wxUSE_NEW_DC
279 wxPostScriptImplDC::wxPostScriptImplDC( wxPostScriptDC *owner ) :
280 wxImplDC( owner )
281 {
282 Init();
283
284 m_pageHeight = 842 * PS2DEV;
285
286 m_ok = true;
287 }
288
289 wxPostScriptImplDC::wxPostScriptImplDC( wxPostScriptDC *owner, const wxPrintData& data ) :
290 wxImplDC( owner )
291 {
292 Init();
293
294 // this calculates m_pageHeight required for
295 // taking the inverted Y axis into account
296 SetPrintData( data );
297
298 m_ok = true;
299 }
300 #endif
301
302
303 #if wxUSE_NEW_DC
304 wxPostScriptImplDC::wxPostScriptImplDC( wxPrinterDC *owner ) :
305 wxImplDC( owner )
306 #else
307 wxPostScriptDC::wxPostScriptDC()
308 #endif
309 {
310 Init();
311
312 m_pageHeight = 842 * PS2DEV;
313
314 m_ok = true;
315 }
316
317 #if wxUSE_NEW_DC
318 wxPostScriptImplDC::wxPostScriptImplDC( wxPrinterDC *owner, const wxPrintData& data ) :
319 wxImplDC( owner )
320 #else
321 wxPostScriptDC::wxPostScriptDC( const wxPrintData& data )
322 #endif
323 {
324 Init();
325
326 // this calculates m_pageHeight required for
327 // taking the inverted Y axis into account
328 SetPrintData( data );
329
330 m_ok = true;
331 }
332
333 void wxPostScriptImplDC::Init()
334 {
335 m_pstream = (FILE*) NULL;
336
337 m_currentRed = 0;
338 m_currentGreen = 0;
339 m_currentBlue = 0;
340
341 m_pageNumber = 0;
342
343 m_clipping = false;
344
345 m_underlinePosition = 0.0;
346 m_underlineThickness = 0.0;
347
348 }
349
350 wxPostScriptImplDC::~wxPostScriptImplDC ()
351 {
352 if (m_pstream)
353 {
354 fclose( m_pstream );
355 m_pstream = (FILE*) NULL;
356 }
357 }
358
359 bool wxPostScriptImplDC::IsOk() const
360 {
361 return m_ok;
362 }
363
364 wxRect wxPostScriptImplDC::GetPaperRect()
365 {
366 int w = 0;
367 int h = 0;
368 DoGetSize( &w, &h );
369 return wxRect(0,0,w,h);
370 }
371
372 int wxPostScriptImplDC::GetResolution()
373 {
374 return DPI;
375 }
376
377 void wxPostScriptImplDC::DoSetClippingRegion (wxCoord x, wxCoord y, wxCoord w, wxCoord h)
378 {
379 wxCHECK_RET( m_ok , wxT("invalid postscript dc") );
380
381 if (m_clipping)
382 DestroyClippingRegion();
383
384 #if wxUSE_NEW_DC
385 m_clipX1 = x;
386 m_clipY1 = y;
387 m_clipX2 = x + w;
388 m_clipY2 = y + h;
389 #else
390 wxDC::DoSetClippingRegion(x, y, w, h);
391 #endif
392
393 m_clipping = true;
394
395 wxString buffer;
396 buffer.Printf( "gsave\n"
397 "newpath\n"
398 "%f %f moveto\n"
399 "%f %f lineto\n"
400 "%f %f lineto\n"
401 "%f %f lineto\n"
402 "closepath clip newpath\n",
403 XLOG2DEV(x), YLOG2DEV(y),
404 XLOG2DEV(x+w), YLOG2DEV(y),
405 XLOG2DEV(x+w), YLOG2DEV(y+h),
406 XLOG2DEV(x), YLOG2DEV(y+h) );
407 buffer.Replace( ",", "." );
408 PsPrint( buffer );
409 }
410
411
412 void wxPostScriptImplDC::DestroyClippingRegion()
413 {
414 wxCHECK_RET( m_ok , wxT("invalid postscript dc") );
415
416 if (m_clipping)
417 {
418 m_clipping = false;
419 PsPrint( "grestore\n" );
420 }
421
422 #if wxUSE_NEW_DC
423 wxImplDC::DestroyClippingRegion();
424 #else
425 wxDC::DestroyClippingRegion();
426 #endif
427 }
428
429 void wxPostScriptImplDC::Clear()
430 {
431 // This should fail silently to avoid unnecessary
432 // asserts
433 // wxFAIL_MSG( wxT("wxPostScriptImplDC::Clear not implemented.") );
434 }
435
436 bool wxPostScriptImplDC::DoFloodFill (wxCoord WXUNUSED(x), wxCoord WXUNUSED(y), const wxColour &WXUNUSED(col), int WXUNUSED(style))
437 {
438 wxFAIL_MSG( wxT("wxPostScriptImplDC::FloodFill not implemented.") );
439 return false;
440 }
441
442 bool wxPostScriptImplDC::DoGetPixel (wxCoord WXUNUSED(x), wxCoord WXUNUSED(y), wxColour * WXUNUSED(col)) const
443 {
444 wxFAIL_MSG( wxT("wxPostScriptImplDC::GetPixel not implemented.") );
445 return false;
446 }
447
448 void wxPostScriptImplDC::DoCrossHair (wxCoord WXUNUSED(x), wxCoord WXUNUSED(y))
449 {
450 wxFAIL_MSG( wxT("wxPostScriptImplDC::CrossHair not implemented.") );
451 }
452
453 void wxPostScriptImplDC::DoDrawLine (wxCoord x1, wxCoord y1, wxCoord x2, wxCoord y2)
454 {
455 wxCHECK_RET( m_ok, wxT("invalid postscript dc") );
456
457 if (m_pen.GetStyle() == wxTRANSPARENT) return;
458
459 SetPen( m_pen );
460
461 wxString buffer;
462 buffer.Printf( "newpath\n"
463 "%f %f moveto\n"
464 "%f %f lineto\n"
465 "stroke\n",
466 XLOG2DEV(x1), YLOG2DEV(y1),
467 XLOG2DEV(x2), YLOG2DEV(y2) );
468 buffer.Replace( ",", "." );
469 PsPrint( buffer );
470
471 CalcBoundingBox( x1, y1 );
472 CalcBoundingBox( x2, y2 );
473 }
474
475 void wxPostScriptImplDC::DoDrawArc (wxCoord x1, wxCoord y1, wxCoord x2, wxCoord y2, wxCoord xc, wxCoord yc)
476 {
477 wxCHECK_RET( m_ok, wxT("invalid postscript dc") );
478
479 wxCoord dx = x1 - xc;
480 wxCoord dy = y1 - yc;
481 double radius = sqrt( (double)(dx*dx+dy*dy) );
482 double alpha1, alpha2;
483
484 if (x1 == x2 && y1 == y2)
485 {
486 alpha1 = 0.0;
487 alpha2 = 360.0;
488 }
489 else if ( wxIsNullDouble(radius) )
490 {
491 alpha1 =
492 alpha2 = 0.0;
493 }
494 else
495 {
496 alpha1 = (x1 - xc == 0) ?
497 (y1 - yc < 0) ? 90.0 : -90.0 :
498 -atan2(double(y1-yc), double(x1-xc)) * RAD2DEG;
499 alpha2 = (x2 - xc == 0) ?
500 (y2 - yc < 0) ? 90.0 : -90.0 :
501 -atan2(double(y2-yc), double(x2-xc)) * RAD2DEG;
502 }
503 while (alpha1 <= 0) alpha1 += 360;
504 while (alpha2 <= 0) alpha2 += 360; // adjust angles to be between
505 while (alpha1 > 360) alpha1 -= 360; // 0 and 360 degree
506 while (alpha2 > 360) alpha2 -= 360;
507
508 int i_radius = wxRound( radius );
509
510 if (m_brush.GetStyle() != wxTRANSPARENT)
511 {
512 SetBrush( m_brush );
513
514 wxString buffer;
515 buffer.Printf( "newpath\n"
516 "%f %f %f %f %f %f ellipse\n"
517 "%f %f lineto\n"
518 "closepath\n"
519 "fill\n",
520 XLOG2DEV(xc), YLOG2DEV(yc),
521 XLOG2DEVREL(i_radius), YLOG2DEVREL(i_radius),
522 alpha1, alpha2,
523 XLOG2DEV(xc), YLOG2DEV(yc) );
524 buffer.Replace( ",", "." );
525 PsPrint( buffer );
526
527 CalcBoundingBox( xc-i_radius, yc-i_radius );
528 CalcBoundingBox( xc+i_radius, yc+i_radius );
529 }
530
531 if (m_pen.GetStyle() != wxTRANSPARENT)
532 {
533 SetPen( m_pen );
534
535 wxString buffer;
536 buffer.Printf( "newpath\n"
537 "%f %f %f %f %f %f ellipse\n"
538 "%f %f lineto\n"
539 "closepath\n"
540 "stroke\n",
541 XLOG2DEV(xc), YLOG2DEV(yc),
542 XLOG2DEVREL(i_radius), YLOG2DEVREL(i_radius),
543 alpha1, alpha2,
544 XLOG2DEV(xc), YLOG2DEV(yc) );
545 buffer.Replace( ",", "." );
546 PsPrint( buffer );
547
548 CalcBoundingBox( xc-i_radius, yc-i_radius );
549 CalcBoundingBox( xc+i_radius, yc+i_radius );
550 }
551 }
552
553 void wxPostScriptImplDC::DoDrawEllipticArc(wxCoord x,wxCoord y,wxCoord w,wxCoord h,double sa,double ea)
554 {
555 wxCHECK_RET( m_ok, wxT("invalid postscript dc") );
556
557 if ( sa >= 360 || sa <= -360 )
558 sa -= int(sa/360)*360;
559 if ( ea >= 360 || ea <=- 360 )
560 ea -= int(ea/360)*360;
561 if ( sa < 0 )
562 sa += 360;
563 if ( ea < 0 )
564 ea += 360;
565
566 if ( wxIsSameDouble(sa, ea) )
567 {
568 DoDrawEllipse(x,y,w,h);
569 return;
570 }
571
572 if (m_brush.GetStyle () != wxTRANSPARENT)
573 {
574 SetBrush( m_brush );
575
576 wxString buffer;
577 buffer.Printf( "newpath\n"
578 "%f %f %f %f %f %f true ellipticarc\n",
579 XLOG2DEV(x+w/2), YLOG2DEV(y+h/2),
580 XLOG2DEVREL(w/2), YLOG2DEVREL(h/2),
581 sa, ea );
582 buffer.Replace( ",", "." );
583 PsPrint( buffer );
584
585 CalcBoundingBox( x ,y );
586 CalcBoundingBox( x+w, y+h );
587 }
588
589 if (m_pen.GetStyle () != wxTRANSPARENT)
590 {
591 SetPen( m_pen );
592
593 wxString buffer;
594 buffer.Printf( "newpath\n"
595 "%f %f %f %f %f %f false ellipticarc\n",
596 XLOG2DEV(x+w/2), YLOG2DEV(y+h/2),
597 XLOG2DEVREL(w/2), YLOG2DEVREL(h/2),
598 sa, ea );
599 buffer.Replace( ",", "." );
600 PsPrint( buffer );
601
602 CalcBoundingBox( x ,y );
603 CalcBoundingBox( x+w, y+h );
604 }
605 }
606
607 void wxPostScriptImplDC::DoDrawPoint (wxCoord x, wxCoord y)
608 {
609 wxCHECK_RET( m_ok, wxT("invalid postscript dc") );
610
611 if (m_pen.GetStyle() == wxTRANSPARENT) return;
612
613 SetPen (m_pen);
614
615 wxString buffer;
616 buffer.Printf( "newpath\n"
617 "%f %f moveto\n"
618 "%f %f lineto\n"
619 "stroke\n",
620 XLOG2DEV(x), YLOG2DEV(y),
621 XLOG2DEV(x+1), YLOG2DEV(y) );
622 buffer.Replace( ",", "." );
623 PsPrint( buffer );
624
625 CalcBoundingBox( x, y );
626 }
627
628 void wxPostScriptImplDC::DoDrawPolygon (int n, wxPoint points[], wxCoord xoffset, wxCoord yoffset, int fillStyle)
629 {
630 wxCHECK_RET( m_ok, wxT("invalid postscript dc") );
631
632 if (n <= 0) return;
633
634 if (m_brush.GetStyle () != wxTRANSPARENT)
635 {
636 SetBrush( m_brush );
637
638 PsPrint( "newpath\n" );
639
640 double xx = XLOG2DEV(points[0].x + xoffset);
641 double yy = YLOG2DEV(points[0].y + yoffset);
642
643 wxString buffer;
644 buffer.Printf( "%f %f moveto\n", xx, yy );
645 buffer.Replace( ",", "." );
646 PsPrint( buffer );
647
648 CalcBoundingBox( points[0].x + xoffset, points[0].y + yoffset );
649
650 for (int i = 1; i < n; i++)
651 {
652 xx = XLOG2DEV(points[i].x + xoffset);
653 yy = YLOG2DEV(points[i].y + yoffset);
654
655 buffer.Printf( "%f %f lineto\n", xx, yy );
656 buffer.Replace( ",", "." );
657 PsPrint( buffer );
658
659 CalcBoundingBox( points[i].x + xoffset, points[i].y + yoffset);
660 }
661
662 PsPrint( (fillStyle == wxODDEVEN_RULE ? "eofill\n" : "fill\n") );
663 }
664
665 if (m_pen.GetStyle () != wxTRANSPARENT)
666 {
667 SetPen( m_pen );
668
669 PsPrint( "newpath\n" );
670
671 double xx = XLOG2DEV(points[0].x + xoffset);
672 double yy = YLOG2DEV(points[0].y + yoffset);
673
674 wxString buffer;
675 buffer.Printf( "%f %f moveto\n", xx, yy );
676 buffer.Replace( ",", "." );
677 PsPrint( buffer );
678
679 CalcBoundingBox( points[0].x + xoffset, points[0].y + yoffset );
680
681 for (int i = 1; i < n; i++)
682 {
683 xx = XLOG2DEV(points[i].x + xoffset);
684 yy = YLOG2DEV(points[i].y + yoffset);
685
686 buffer.Printf( "%f %f lineto\n", xx, yy );
687 buffer.Replace( ",", "." );
688 PsPrint( buffer );
689
690 CalcBoundingBox( points[i].x + xoffset, points[i].y + yoffset);
691 }
692
693 PsPrint( "closepath\n" );
694 PsPrint( "stroke\n" );
695 }
696 }
697
698 void wxPostScriptImplDC::DoDrawPolyPolygon (int n, int count[], wxPoint points[], wxCoord xoffset, wxCoord yoffset, int fillStyle)
699 {
700 wxCHECK_RET( m_ok, wxT("invalid postscript dc") );
701
702 if (n <= 0) return;
703
704 if (m_brush.GetStyle () != wxTRANSPARENT)
705 {
706 SetBrush( m_brush );
707
708 PsPrint( "newpath\n" );
709
710 int ofs = 0;
711 for (int i = 0; i < n; ofs += count[i++])
712 {
713 double xx = XLOG2DEV(points[ofs].x + xoffset);
714 double yy = YLOG2DEV(points[ofs].y + yoffset);
715
716 wxString buffer;
717 buffer.Printf( "%f %f moveto\n", xx, yy );
718 buffer.Replace( ",", "." );
719 PsPrint( buffer );
720
721 CalcBoundingBox( points[ofs].x + xoffset, points[ofs].y + yoffset );
722
723 for (int j = 1; j < count[i]; j++)
724 {
725 xx = XLOG2DEV(points[ofs+j].x + xoffset);
726 yy = YLOG2DEV(points[ofs+j].y + yoffset);
727
728 buffer.Printf( "%f %f lineto\n", xx, yy );
729 buffer.Replace( ",", "." );
730 PsPrint( buffer );
731
732 CalcBoundingBox( points[ofs+j].x + xoffset, points[ofs+j].y + yoffset);
733 }
734 }
735 PsPrint( (fillStyle == wxODDEVEN_RULE ? "eofill\n" : "fill\n") );
736 }
737
738 if (m_pen.GetStyle () != wxTRANSPARENT)
739 {
740 SetPen( m_pen );
741
742 PsPrint( "newpath\n" );
743
744 int ofs = 0;
745 for (int i = 0; i < n; ofs += count[i++])
746 {
747 double xx = XLOG2DEV(points[ofs].x + xoffset);
748 double yy = YLOG2DEV(points[ofs].y + yoffset);
749
750 wxString buffer;
751 buffer.Printf( "%f %f moveto\n", xx, yy );
752 buffer.Replace( ",", "." );
753 PsPrint( buffer );
754
755 CalcBoundingBox( points[ofs].x + xoffset, points[ofs].y + yoffset );
756
757 for (int j = 1; j < count[i]; j++)
758 {
759 xx = XLOG2DEV(points[ofs+j].x + xoffset);
760 yy = YLOG2DEV(points[ofs+j].y + yoffset);
761
762 buffer.Printf( "%f %f lineto\n", xx, yy );
763 buffer.Replace( ",", "." );
764 PsPrint( buffer );
765
766 CalcBoundingBox( points[ofs+j].x + xoffset, points[ofs+j].y + yoffset);
767 }
768 }
769 PsPrint( "closepath\n" );
770 PsPrint( "stroke\n" );
771 }
772 }
773
774 void wxPostScriptImplDC::DoDrawLines (int n, wxPoint points[], wxCoord xoffset, wxCoord yoffset)
775 {
776 wxCHECK_RET( m_ok, wxT("invalid postscript dc") );
777
778 if (m_pen.GetStyle() == wxTRANSPARENT) return;
779
780 if (n <= 0) return;
781
782 SetPen (m_pen);
783
784 int i;
785 for ( i =0; i<n ; i++ )
786 CalcBoundingBox( points[i].x+xoffset, points[i].y+yoffset );
787
788 wxString buffer;
789 buffer.Printf( "newpath\n"
790 "%f %f moveto\n",
791 XLOG2DEV(points[0].x+xoffset),
792 YLOG2DEV(points[0].y+yoffset) );
793 buffer.Replace( ",", "." );
794 PsPrint( buffer );
795
796 for (i = 1; i < n; i++)
797 {
798 buffer.Printf( "%f %f lineto\n",
799 XLOG2DEV(points[i].x+xoffset),
800 YLOG2DEV(points[i].y+yoffset) );
801 buffer.Replace( ",", "." );
802 PsPrint( buffer );
803 }
804
805 PsPrint( "stroke\n" );
806 }
807
808 void wxPostScriptImplDC::DoDrawRectangle (wxCoord x, wxCoord y, wxCoord width, wxCoord height)
809 {
810 wxCHECK_RET( m_ok, wxT("invalid postscript dc") );
811
812 width--;
813 height--;
814
815 if (m_brush.GetStyle () != wxTRANSPARENT)
816 {
817 SetBrush( m_brush );
818
819 wxString buffer;
820 buffer.Printf( "newpath\n"
821 "%f %f moveto\n"
822 "%f %f lineto\n"
823 "%f %f lineto\n"
824 "%f %f lineto\n"
825 "closepath\n"
826 "fill\n",
827 XLOG2DEV(x), YLOG2DEV(y),
828 XLOG2DEV(x + width), YLOG2DEV(y),
829 XLOG2DEV(x + width), YLOG2DEV(y + height),
830 XLOG2DEV(x), YLOG2DEV(y + height) );
831 buffer.Replace( ",", "." );
832 PsPrint( buffer );
833
834 CalcBoundingBox( x, y );
835 CalcBoundingBox( x + width, y + height );
836 }
837
838 if (m_pen.GetStyle () != wxTRANSPARENT)
839 {
840 SetPen (m_pen);
841
842 wxString buffer;
843 buffer.Printf( "newpath\n"
844 "%f %f moveto\n"
845 "%f %f lineto\n"
846 "%f %f lineto\n"
847 "%f %f lineto\n"
848 "closepath\n"
849 "stroke\n",
850 XLOG2DEV(x), YLOG2DEV(y),
851 XLOG2DEV(x + width), YLOG2DEV(y),
852 XLOG2DEV(x + width), YLOG2DEV(y + height),
853 XLOG2DEV(x), YLOG2DEV(y + height) );
854 buffer.Replace( ",", "." );
855 PsPrint( buffer );
856
857 CalcBoundingBox( x, y );
858 CalcBoundingBox( x + width, y + height );
859 }
860 }
861
862 void wxPostScriptImplDC::DoDrawRoundedRectangle (wxCoord x, wxCoord y, wxCoord width, wxCoord height, double radius)
863 {
864 wxCHECK_RET( m_ok, wxT("invalid postscript dc") );
865
866 width--;
867 height--;
868
869 if (radius < 0.0)
870 {
871 // Now, a negative radius is interpreted to mean
872 // 'the proportion of the smallest X or Y dimension'
873 double smallest = width < height ? width : height;
874 radius = (-radius * smallest);
875 }
876
877 wxCoord rad = (wxCoord) radius;
878
879 if (m_brush.GetStyle () != wxTRANSPARENT)
880 {
881 SetBrush( m_brush );
882
883 /* Draw rectangle anticlockwise */
884 wxString buffer;
885 buffer.Printf( "newpath\n"
886 "%f %f %f 90 180 arc\n"
887 "%f %f lineto\n"
888 "%f %f %f 180 270 arc\n"
889 "%f %f lineto\n"
890 "%f %f %f 270 0 arc\n"
891 "%f %f lineto\n"
892 "%f %f %f 0 90 arc\n"
893 "%f %f lineto\n"
894 "closepath\n"
895 "fill\n",
896 XLOG2DEV(x + rad), YLOG2DEV(y + rad), XLOG2DEVREL(rad),
897 XLOG2DEV(x), YLOG2DEV(y + height - rad),
898 XLOG2DEV(x + rad), YLOG2DEV(y + height - rad), XLOG2DEVREL(rad),
899 XLOG2DEV(x + width - rad), YLOG2DEV(y + height),
900 XLOG2DEV(x + width - rad), YLOG2DEV(y + height - rad), XLOG2DEVREL(rad),
901 XLOG2DEV(x + width), YLOG2DEV(y + rad),
902 XLOG2DEV(x + width - rad), YLOG2DEV(y + rad), XLOG2DEVREL(rad),
903 XLOG2DEV(x + rad), YLOG2DEV(y) );
904 buffer.Replace( ",", "." );
905 PsPrint( buffer );
906
907 CalcBoundingBox( x, y );
908 CalcBoundingBox( x + width, y + height );
909 }
910
911 if (m_pen.GetStyle () != wxTRANSPARENT)
912 {
913 SetPen (m_pen);
914
915 /* Draw rectangle anticlockwise */
916 wxString buffer;
917 buffer.Printf( "newpath\n"
918 "%f %f %f 90 180 arc\n"
919 "%f %f lineto\n"
920 "%f %f %f 180 270 arc\n"
921 "%f %f lineto\n"
922 "%f %f %f 270 0 arc\n"
923 "%f %f lineto\n"
924 "%f %f %f 0 90 arc\n"
925 "%f %f lineto\n"
926 "closepath\n"
927 "stroke\n",
928 XLOG2DEV(x + rad), YLOG2DEV(y + rad), XLOG2DEVREL(rad),
929 XLOG2DEV(x), YLOG2DEV(y + height - rad),
930 XLOG2DEV(x + rad), YLOG2DEV(y + height - rad), XLOG2DEVREL(rad),
931 XLOG2DEV(x + width - rad), YLOG2DEV(y + height),
932 XLOG2DEV(x + width - rad), YLOG2DEV(y + height - rad), XLOG2DEVREL(rad),
933 XLOG2DEV(x + width), YLOG2DEV(y + rad),
934 XLOG2DEV(x + width - rad), YLOG2DEV(y + rad), XLOG2DEVREL(rad),
935 XLOG2DEV(x + rad), YLOG2DEV(y) );
936 buffer.Replace( ",", "." );
937 PsPrint( buffer );
938
939 CalcBoundingBox( x, y );
940 CalcBoundingBox( x + width, y + height );
941 }
942 }
943
944 void wxPostScriptImplDC::DoDrawEllipse (wxCoord x, wxCoord y, wxCoord width, wxCoord height)
945 {
946 wxCHECK_RET( m_ok, wxT("invalid postscript dc") );
947
948 width--;
949 height--;
950
951 if (m_brush.GetStyle () != wxTRANSPARENT)
952 {
953 SetBrush (m_brush);
954
955 wxString buffer;
956 buffer.Printf( "newpath\n"
957 "%f %f %f %f 0 360 ellipse\n"
958 "fill\n",
959 XLOG2DEV(x + width / 2), YLOG2DEV(y + height / 2),
960 XLOG2DEVREL(width / 2), YLOG2DEVREL(height / 2) );
961 buffer.Replace( ",", "." );
962 PsPrint( buffer );
963
964 CalcBoundingBox( x - width, y - height );
965 CalcBoundingBox( x + width, y + height );
966 }
967
968 if (m_pen.GetStyle () != wxTRANSPARENT)
969 {
970 SetPen (m_pen);
971
972 wxString buffer;
973 buffer.Printf( "newpath\n"
974 "%f %f %f %f 0 360 ellipse\n"
975 "stroke\n",
976 XLOG2DEV(x + width / 2), YLOG2DEV(y + height / 2),
977 XLOG2DEVREL(width / 2), YLOG2DEVREL(height / 2) );
978 buffer.Replace( ",", "." );
979 PsPrint( buffer );
980
981 CalcBoundingBox( x - width, y - height );
982 CalcBoundingBox( x + width, y + height );
983 }
984 }
985
986 void wxPostScriptImplDC::DoDrawIcon( const wxIcon& icon, wxCoord x, wxCoord y )
987 {
988 DoDrawBitmap( icon, x, y, true );
989 }
990
991 /* this has to be char, not wxChar */
992 static char hexArray[] = "0123456789ABCDEF";
993
994 void wxPostScriptImplDC::DoDrawBitmap( const wxBitmap& bitmap, wxCoord x, wxCoord y, bool WXUNUSED(useMask) )
995 {
996 wxCHECK_RET( m_ok, wxT("invalid postscript dc") );
997
998 if (!bitmap.Ok()) return;
999
1000 wxImage image = bitmap.ConvertToImage();
1001
1002 if (!image.Ok()) return;
1003
1004 wxCoord w = image.GetWidth();
1005 wxCoord h = image.GetHeight();
1006
1007 double ww = XLOG2DEVREL(image.GetWidth());
1008 double hh = YLOG2DEVREL(image.GetHeight());
1009
1010 double xx = XLOG2DEV(x);
1011 double yy = YLOG2DEV(y + bitmap.GetHeight());
1012
1013 wxString buffer;
1014 buffer.Printf( "/origstate save def\n"
1015 "20 dict begin\n"
1016 "/pix %d string def\n"
1017 "/grays %d string def\n"
1018 "/npixels 0 def\n"
1019 "/rgbindx 0 def\n"
1020 "%f %f translate\n"
1021 "%f %f scale\n"
1022 "%d %d 8\n"
1023 "[%d 0 0 %d 0 %d]\n"
1024 "{currentfile pix readhexstring pop}\n"
1025 "false 3 colorimage\n",
1026 w, w, xx, yy, ww, hh, w, h, w, -h, h );
1027 buffer.Replace( ",", "." );
1028 PsPrint( buffer );
1029
1030 unsigned char* data = image.GetData();
1031
1032 // size of the buffer = width*rgb(3)*hexa(2)+'\n'
1033 wxCharBuffer charbuffer(w*6 + 1);
1034 int firstDigit, secondDigit;
1035
1036 //rows
1037 for (int j = 0; j < h; j++)
1038 {
1039 char* bufferindex = charbuffer.data();
1040
1041 //cols
1042 for (int i = 0; i < w*3; i++)
1043 {
1044 firstDigit = (int)(*data/16.0);
1045 secondDigit = (int)(*data - (firstDigit*16.0));
1046 *(bufferindex++) = hexArray[firstDigit];
1047 *(bufferindex++) = hexArray[secondDigit];
1048
1049 data++;
1050 }
1051 *(bufferindex++) = '\n';
1052 *bufferindex = 0;
1053
1054 if (m_pstream)
1055 fwrite( charbuffer, 1, strlen( charbuffer ), m_pstream );
1056 else
1057 PsPrint( charbuffer );
1058 }
1059
1060 PsPrint( "end\n" );
1061 PsPrint( "origstate restore\n" );
1062 }
1063
1064 void wxPostScriptImplDC::SetFont( const wxFont& font )
1065 {
1066 wxCHECK_RET( m_ok, wxT("invalid postscript dc") );
1067
1068 if (!font.Ok()) return;
1069
1070 m_font = font;
1071
1072 int Style = m_font.GetStyle();
1073 int Weight = m_font.GetWeight();
1074
1075 const char *name;
1076 switch (m_font.GetFamily())
1077 {
1078 case wxTELETYPE:
1079 case wxMODERN:
1080 {
1081 if (Style == wxITALIC)
1082 {
1083 if (Weight == wxBOLD)
1084 name = "/Courier-BoldOblique";
1085 else
1086 name = "/Courier-Oblique";
1087 }
1088 else
1089 {
1090 if (Weight == wxBOLD)
1091 name = "/Courier-Bold";
1092 else
1093 name = "/Courier";
1094 }
1095 break;
1096 }
1097 case wxROMAN:
1098 {
1099 if (Style == wxITALIC)
1100 {
1101 if (Weight == wxBOLD)
1102 name = "/Times-BoldItalic";
1103 else
1104 name = "/Times-Italic";
1105 }
1106 else
1107 {
1108 if (Weight == wxBOLD)
1109 name = "/Times-Bold";
1110 else
1111 name = "/Times-Roman";
1112 }
1113 break;
1114 }
1115 case wxSCRIPT:
1116 {
1117 name = "/ZapfChancery-MediumItalic";
1118 break;
1119 }
1120 case wxSWISS:
1121 default:
1122 {
1123 if (Style == wxITALIC)
1124 {
1125 if (Weight == wxBOLD)
1126 name = "/Helvetica-BoldOblique";
1127 else
1128 name = "/Helvetica-Oblique";
1129 }
1130 else
1131 {
1132 if (Weight == wxBOLD)
1133 name = "/Helvetica-Bold";
1134 else
1135 name = "/Helvetica";
1136 }
1137 break;
1138 }
1139 }
1140
1141 // We may legitimately call SetFont before BeginDoc
1142 if (!m_pstream)
1143 return;
1144
1145 PsPrint( name );
1146 PsPrint( " reencodeISO def\n" );
1147 PsPrint( name );
1148 PsPrint( " findfont\n" );
1149
1150
1151 double size = (double) m_font.GetPointSize();
1152 wxString buffer;
1153 buffer.Printf( "%f scalefont setfont\n", size * DEV2PS * m_scaleX );
1154 buffer.Replace( ",", "." );
1155 PsPrint( buffer );
1156 }
1157
1158 void wxPostScriptImplDC::SetPen( const wxPen& pen )
1159 {
1160 wxCHECK_RET( m_ok, wxT("invalid postscript dc") );
1161
1162 if (!pen.Ok()) return;
1163
1164 int oldStyle = m_pen.GetStyle();
1165
1166 m_pen = pen;
1167
1168 double width;
1169
1170 if (m_pen.GetWidth() <= 0)
1171 width = 0.1;
1172 else
1173 width = (double) m_pen.GetWidth();
1174
1175 wxString buffer;
1176 buffer.Printf( "%f setlinewidth\n", width * DEV2PS * m_scaleX );
1177 buffer.Replace( ",", "." );
1178 PsPrint( buffer );
1179
1180 /*
1181 Line style - WRONG: 2nd arg is OFFSET
1182
1183 Here, I'm afraid you do not conceive meaning of parameters of 'setdash'
1184 operator correctly. You should look-up this in the Red Book: the 2nd parame-
1185 ter is not number of values in the array of the first one, but an offset
1186 into this description of the pattern. I mean a real *offset* not index
1187 into array. I.e. If the command is [3 4] 1 setdash is used, then there
1188 will be first black line *2* units wxCoord, then space 4 units, then the
1189 pattern of *3* units black, 4 units space will be repeated.
1190 */
1191
1192 static const char *dotted = "[2 5] 2";
1193 static const char *short_dashed = "[4 4] 2";
1194 static const char *wxCoord_dashed = "[4 8] 2";
1195 static const char *dotted_dashed = "[6 6 2 6] 4";
1196
1197 const char *psdash;
1198
1199 switch (m_pen.GetStyle())
1200 {
1201 case wxDOT: psdash = dotted; break;
1202 case wxSHORT_DASH: psdash = short_dashed; break;
1203 case wxLONG_DASH: psdash = wxCoord_dashed; break;
1204 case wxDOT_DASH: psdash = dotted_dashed; break;
1205 case wxUSER_DASH:
1206 {
1207 wxDash *dashes;
1208 int nDashes = m_pen.GetDashes (&dashes);
1209 PsPrint ("[");
1210 for (int i = 0; i < nDashes; ++i)
1211 {
1212 buffer.Printf( "%d ", dashes [i] );
1213 PsPrint( buffer );
1214 }
1215 PsPrint ("] 0 setdash\n");
1216 psdash = 0;
1217 }
1218 break;
1219 case wxSOLID:
1220 case wxTRANSPARENT:
1221 default: psdash = "[] 0"; break;
1222 }
1223
1224 if ( psdash && (oldStyle != m_pen.GetStyle()) )
1225 {
1226 PsPrint( psdash );
1227 PsPrint( " setdash\n" );
1228 }
1229
1230 // Line colour
1231 unsigned char red = m_pen.GetColour().Red();
1232 unsigned char blue = m_pen.GetColour().Blue();
1233 unsigned char green = m_pen.GetColour().Green();
1234
1235 if (!m_colour)
1236 {
1237 // Anything not white is black
1238 if (! (red == (unsigned char) 255 &&
1239 blue == (unsigned char) 255 &&
1240 green == (unsigned char) 255) )
1241 {
1242 red = (unsigned char) 0;
1243 green = (unsigned char) 0;
1244 blue = (unsigned char) 0;
1245 }
1246 // setgray here ?
1247 }
1248
1249 if (!(red == m_currentRed && green == m_currentGreen && blue == m_currentBlue))
1250 {
1251 double redPS = (double)(red) / 255.0;
1252 double bluePS = (double)(blue) / 255.0;
1253 double greenPS = (double)(green) / 255.0;
1254
1255 wxString buffer;
1256 buffer.Printf( "%f %f %f setrgbcolor\n", redPS, greenPS, bluePS );
1257 buffer.Replace( ",", "." );
1258 PsPrint( buffer );
1259
1260 m_currentRed = red;
1261 m_currentBlue = blue;
1262 m_currentGreen = green;
1263 }
1264 }
1265
1266 void wxPostScriptImplDC::SetBrush( const wxBrush& brush )
1267 {
1268 wxCHECK_RET( m_ok, wxT("invalid postscript dc") );
1269
1270 if (!brush.Ok()) return;
1271
1272 m_brush = brush;
1273
1274 // Brush colour
1275 unsigned char red = m_brush.GetColour().Red();
1276 unsigned char blue = m_brush.GetColour().Blue();
1277 unsigned char green = m_brush.GetColour().Green();
1278
1279 if (!m_colour)
1280 {
1281 // Anything not white is black
1282 if (! (red == (unsigned char) 255 &&
1283 blue == (unsigned char) 255 &&
1284 green == (unsigned char) 255) )
1285 {
1286 red = (unsigned char) 0;
1287 green = (unsigned char) 0;
1288 blue = (unsigned char) 0;
1289 }
1290 // setgray here ?
1291 }
1292
1293 if (!(red == m_currentRed && green == m_currentGreen && blue == m_currentBlue))
1294 {
1295 double redPS = (double)(red) / 255.0;
1296 double bluePS = (double)(blue) / 255.0;
1297 double greenPS = (double)(green) / 255.0;
1298
1299 wxString buffer;
1300 buffer.Printf( "%f %f %f setrgbcolor\n", redPS, greenPS, bluePS );
1301 buffer.Replace( ",", "." );
1302 PsPrint( buffer );
1303
1304 m_currentRed = red;
1305 m_currentBlue = blue;
1306 m_currentGreen = green;
1307 }
1308 }
1309
1310 void wxPostScriptImplDC::DoDrawText( const wxString& text, wxCoord x, wxCoord y )
1311 {
1312 wxCHECK_RET( m_ok, wxT("invalid postscript dc") );
1313
1314 const wxWX2MBbuf textbuf = text.mb_str();
1315 if ( !textbuf )
1316 return;
1317
1318 if (m_textForegroundColour.Ok())
1319 {
1320 unsigned char red = m_textForegroundColour.Red();
1321 unsigned char blue = m_textForegroundColour.Blue();
1322 unsigned char green = m_textForegroundColour.Green();
1323
1324 if (!m_colour)
1325 {
1326 // Anything not white is black
1327 if (! (red == (unsigned char) 255 &&
1328 blue == (unsigned char) 255 &&
1329 green == (unsigned char) 255))
1330 {
1331 red = (unsigned char) 0;
1332 green = (unsigned char) 0;
1333 blue = (unsigned char) 0;
1334 }
1335 }
1336
1337 // maybe setgray here ?
1338 if (!(red == m_currentRed && green == m_currentGreen && blue == m_currentBlue))
1339 {
1340 double redPS = (double)(red) / 255.0;
1341 double bluePS = (double)(blue) / 255.0;
1342 double greenPS = (double)(green) / 255.0;
1343
1344 wxString buffer;
1345 buffer.Printf( "%f %f %f setrgbcolor\n", redPS, greenPS, bluePS );
1346 buffer.Replace( ",", "." );
1347 PsPrint( buffer );
1348
1349 m_currentRed = red;
1350 m_currentBlue = blue;
1351 m_currentGreen = green;
1352 }
1353 }
1354
1355 wxCoord text_w, text_h, text_descent;
1356
1357 GetOwner()->GetTextExtent(text, &text_w, &text_h, &text_descent);
1358
1359 int size = m_font.GetPointSize();
1360
1361 // wxCoord by = y + (wxCoord)floor( double(size) * 2.0 / 3.0 ); // approximate baseline
1362 // commented by V. Slavik and replaced by accurate version
1363 // - note that there is still rounding error in text_descent!
1364 wxCoord by = y + size - text_descent; // baseline
1365
1366 wxString buffer;
1367 buffer.Printf( "%f %f moveto\n", XLOG2DEV(x), YLOG2DEV(by) );
1368 buffer.Replace( ",", "." );
1369 PsPrint( buffer );
1370 PsPrint( "(" );
1371
1372 size_t len = strlen(textbuf);
1373 size_t i;
1374 for (i = 0; i < len; i++)
1375 {
1376 int c = (unsigned char) textbuf[i];
1377 if (c == ')' || c == '(' || c == '\\')
1378 {
1379 /* Cope with special characters */
1380 PsPrint( "\\" );
1381 PsPrint( (char) c );
1382 }
1383 else if ( c >= 128 )
1384 {
1385 /* Cope with character codes > 127 */
1386 buffer.Printf( "\\%o", c );
1387 PsPrint( buffer );
1388 }
1389 else
1390 {
1391 PsPrint( (char) c );
1392 }
1393 }
1394
1395 PsPrint( ") show\n" );
1396
1397 if (m_font.GetUnderlined())
1398 {
1399 wxCoord uy = (wxCoord)(y + size - m_underlinePosition);
1400
1401 buffer.Printf( "gsave\n"
1402 "%f %f moveto\n"
1403 "%f setlinewidth\n"
1404 "%f %f lineto\n"
1405 "stroke\n"
1406 "grestore\n",
1407 XLOG2DEV(x), YLOG2DEV(uy),
1408 m_underlineThickness,
1409 XLOG2DEV(x + text_w), YLOG2DEV(uy) );
1410 buffer.Replace( ",", "." );
1411 PsPrint( buffer );
1412 }
1413
1414 CalcBoundingBox( x, y );
1415 CalcBoundingBox( x + size * text.length() * 2/3 , y );
1416 }
1417
1418 void wxPostScriptImplDC::DoDrawRotatedText( const wxString& text, wxCoord x, wxCoord y, double angle )
1419 {
1420 if ( wxIsNullDouble(angle) )
1421 {
1422 DoDrawText(text, x, y);
1423 return;
1424 }
1425
1426 wxCHECK_RET( m_ok, wxT("invalid postscript dc") );
1427
1428 SetFont( m_font );
1429
1430 if (m_textForegroundColour.Ok())
1431 {
1432 unsigned char red = m_textForegroundColour.Red();
1433 unsigned char blue = m_textForegroundColour.Blue();
1434 unsigned char green = m_textForegroundColour.Green();
1435
1436 if (!m_colour)
1437 {
1438 // Anything not white is black
1439 if (! (red == (unsigned char) 255 &&
1440 blue == (unsigned char) 255 &&
1441 green == (unsigned char) 255))
1442 {
1443 red = (unsigned char) 0;
1444 green = (unsigned char) 0;
1445 blue = (unsigned char) 0;
1446 }
1447 }
1448
1449 // maybe setgray here ?
1450 if (!(red == m_currentRed && green == m_currentGreen && blue == m_currentBlue))
1451 {
1452 double redPS = (double)(red) / 255.0;
1453 double bluePS = (double)(blue) / 255.0;
1454 double greenPS = (double)(green) / 255.0;
1455
1456 wxString buffer;
1457 buffer.Printf( "%f %f %f setrgbcolor\n", redPS, greenPS, bluePS );
1458 buffer.Replace( ",", "." );
1459 PsPrint( buffer );
1460
1461 m_currentRed = red;
1462 m_currentBlue = blue;
1463 m_currentGreen = green;
1464 }
1465 }
1466
1467 int size = m_font.GetPointSize();
1468
1469 wxString buffer;
1470 buffer.Printf( "%f %f moveto\n", XLOG2DEV(x), YLOG2DEV(y));
1471 buffer.Replace( ",", "." );
1472 PsPrint( buffer );
1473
1474 buffer.Printf( "%f rotate\n", angle );
1475 buffer.Replace( ",", "." );
1476 PsPrint( buffer );
1477
1478 PsPrint( "(" );
1479 const wxWX2MBbuf textbuf = text.mb_str();
1480 size_t len = strlen(textbuf);
1481 size_t i;
1482 for (i = 0; i < len; i++)
1483 {
1484 int c = (unsigned char) textbuf[i];
1485 if (c == ')' || c == '(' || c == '\\')
1486 {
1487 /* Cope with special characters */
1488 PsPrint( "\\" );
1489 PsPrint( (char) c );
1490 }
1491 else if ( c >= 128 )
1492 {
1493 /* Cope with character codes > 127 */
1494 buffer.Printf( "\\%o", c);
1495 PsPrint( buffer );
1496 }
1497 else
1498 {
1499 PsPrint( (char) c );
1500 }
1501 }
1502
1503 PsPrint( ") show\n" );
1504
1505 buffer.Printf( "%f rotate\n", -angle );
1506 buffer.Replace( ",", "." );
1507 PsPrint( buffer );
1508
1509 if (m_font.GetUnderlined())
1510 {
1511 wxCoord uy = (wxCoord)(y + size - m_underlinePosition);
1512 wxCoord w, h;
1513 GetOwner()->GetTextExtent(text, &w, &h);
1514
1515 buffer.Printf(
1516 "gsave\n"
1517 "%f %f moveto\n"
1518 "%f setlinewidth\n"
1519 "%f %f lineto\n"
1520 "stroke\n"
1521 "grestore\n",
1522 XLOG2DEV(x), YLOG2DEV(uy),
1523 m_underlineThickness,
1524 XLOG2DEV(x + w), YLOG2DEV(uy) );
1525 buffer.Replace( ",", "." );
1526 PsPrint( buffer );
1527 }
1528
1529 CalcBoundingBox( x, y );
1530 CalcBoundingBox( x + size * text.length() * 2/3 , y );
1531 }
1532
1533 void wxPostScriptImplDC::SetBackground (const wxBrush& brush)
1534 {
1535 m_backgroundBrush = brush;
1536 }
1537
1538 void wxPostScriptImplDC::SetLogicalFunction (int WXUNUSED(function))
1539 {
1540 wxFAIL_MSG( wxT("wxPostScriptImplDC::SetLogicalFunction not implemented.") );
1541 }
1542
1543 #if wxUSE_SPLINES
1544 void wxPostScriptImplDC::DoDrawSpline( const wxPointList *points )
1545 {
1546 wxCHECK_RET( m_ok, wxT("invalid postscript dc") );
1547
1548 SetPen( m_pen );
1549
1550 // a and b are not used
1551 //double a, b;
1552 double c, d, x1, y1, x2, y2, x3, y3;
1553 wxPoint *p, *q;
1554
1555 wxPointList::compatibility_iterator node = points->GetFirst();
1556 p = node->GetData();
1557 x1 = p->x;
1558 y1 = p->y;
1559
1560 node = node->GetNext();
1561 p = node->GetData();
1562 c = p->x;
1563 d = p->y;
1564 x3 =
1565 #if 0
1566 a =
1567 #endif
1568 (double)(x1 + c) / 2;
1569 y3 =
1570 #if 0
1571 b =
1572 #endif
1573 (double)(y1 + d) / 2;
1574
1575 wxString buffer;
1576 buffer.Printf( "newpath\n"
1577 "%f %f moveto\n"
1578 "%f %f lineto\n",
1579 XLOG2DEV(wxRound(x1)), YLOG2DEV(wxRound(y1)),
1580 XLOG2DEV(wxRound(x3)), YLOG2DEV(wxRound(y3)) );
1581 buffer.Replace( ",", "." );
1582 PsPrint( buffer );
1583
1584 CalcBoundingBox( (wxCoord)x1, (wxCoord)y1 );
1585 CalcBoundingBox( (wxCoord)x3, (wxCoord)y3 );
1586
1587 node = node->GetNext();
1588 while (node)
1589 {
1590 q = node->GetData();
1591
1592 x1 = x3;
1593 y1 = y3;
1594 x2 = c;
1595 y2 = d;
1596 c = q->x;
1597 d = q->y;
1598 x3 = (double)(x2 + c) / 2;
1599 y3 = (double)(y2 + d) / 2;
1600
1601 buffer.Printf( "%f %f %f %f %f %f DrawSplineSection\n",
1602 XLOG2DEV(wxRound(x1)), YLOG2DEV(wxRound(y1)),
1603 XLOG2DEV(wxRound(x2)), YLOG2DEV(wxRound(y2)),
1604 XLOG2DEV(wxRound(x3)), YLOG2DEV(wxRound(y3)) );
1605 buffer.Replace( ",", "." );
1606 PsPrint( buffer );
1607
1608 CalcBoundingBox( (wxCoord)x1, (wxCoord)y1 );
1609 CalcBoundingBox( (wxCoord)x3, (wxCoord)y3 );
1610
1611 node = node->GetNext();
1612 }
1613
1614 /*
1615 At this point, (x2,y2) and (c,d) are the position of the
1616 next-to-last and last point respectively, in the point list
1617 */
1618
1619 buffer.Printf( "%f %f lineto\nstroke\n", XLOG2DEV(wxRound(c)), YLOG2DEV(wxRound(d)) );
1620 buffer.Replace( ",", "." );
1621 PsPrint( buffer );
1622 }
1623 #endif // wxUSE_SPLINES
1624
1625 wxCoord wxPostScriptImplDC::GetCharWidth() const
1626 {
1627 // Chris Breeze: reasonable approximation using wxMODERN/Courier
1628 return (wxCoord) (GetCharHeight() * 72.0 / 120.0);
1629 }
1630
1631 void wxPostScriptImplDC::SetPrintData(const wxPrintData& data)
1632 {
1633 m_printData = data;
1634
1635 wxPaperSize id = m_printData.GetPaperId();
1636 wxPrintPaperType *paper = wxThePrintPaperDatabase->FindPaperType(id);
1637 if (!paper) paper = wxThePrintPaperDatabase->FindPaperType(wxPAPER_A4);
1638 int w = 595;
1639 int h = 842;
1640 if (paper)
1641 {
1642 w = paper->GetSizeDeviceUnits().x;
1643 h = paper->GetSizeDeviceUnits().y;
1644 }
1645
1646 if (m_printData.GetOrientation() == wxLANDSCAPE)
1647 m_pageHeight = w * PS2DEV;
1648 else
1649 m_pageHeight = h * PS2DEV;
1650 }
1651
1652 #if wxUSE_NEW_DC
1653 #else
1654 void wxPostScriptImplDC::SetAxisOrientation( bool xLeftRight, bool yBottomUp )
1655 {
1656 wxDCBase::SetAxisOrientation(xLeftRight,yBottomUp);
1657 }
1658
1659 void wxPostScriptImplDC::SetMapMode(int mode)
1660 {
1661 wxDCBase::SetMapMode(mode);
1662 }
1663
1664 void wxPostScriptImplDC::SetUserScale(double x, double y)
1665 {
1666 wxDCBase::SetUserScale(x,y);
1667 }
1668
1669 void wxPostScriptImplDC::SetLogicalScale(double x, double y)
1670 {
1671 wxDCBase::SetLogicalScale(x,y);
1672 }
1673
1674 void wxPostScriptImplDC::SetLogicalOrigin(wxCoord x, wxCoord y)
1675 {
1676 wxDCBase::SetLogicalOrigin(x,y);
1677 }
1678
1679 void wxPostScriptImplDC::SetDeviceOrigin(wxCoord x, wxCoord y)
1680 {
1681 wxDCBase::SetDeviceOrigin(x,y);
1682 }
1683 #endif
1684
1685 void wxPostScriptImplDC::ComputeScaleAndOrigin()
1686 {
1687 const wxRealPoint origScale(m_scaleX, m_scaleY);
1688
1689 #if wxUSE_NEW_DC
1690 wxImplDC::ComputeScaleAndOrigin();
1691 #else
1692 wxDC::ComputeScaleAndOrigin();
1693 #endif
1694
1695 // If scale has changed call SetPen to recalulate the line width
1696 // and SetFont to recalculate font size
1697 if ( wxRealPoint(m_scaleX, m_scaleY) != origScale && m_pen.IsOk() )
1698 {
1699 SetPen( m_pen );
1700 SetFont( m_font );
1701 }
1702 }
1703
1704 void wxPostScriptImplDC::DoGetSize(int* width, int* height) const
1705 {
1706 wxPaperSize id = m_printData.GetPaperId();
1707
1708 wxPrintPaperType *paper = wxThePrintPaperDatabase->FindPaperType(id);
1709
1710 if (!paper) paper = wxThePrintPaperDatabase->FindPaperType(wxPAPER_A4);
1711
1712 int w = 595;
1713 int h = 842;
1714 if (paper)
1715 {
1716 w = paper->GetSizeDeviceUnits().x;
1717 h = paper->GetSizeDeviceUnits().y;
1718 }
1719
1720 if (m_printData.GetOrientation() == wxLANDSCAPE)
1721 {
1722 int tmp = w;
1723 w = h;
1724 h = tmp;
1725 }
1726
1727 if (width)
1728 *width = wxRound( w * PS2DEV );
1729
1730 if (height)
1731 *height = wxRound( h * PS2DEV );
1732 }
1733
1734 void wxPostScriptImplDC::DoGetSizeMM(int *width, int *height) const
1735 {
1736 wxPaperSize id = m_printData.GetPaperId();
1737
1738 wxPrintPaperType *paper = wxThePrintPaperDatabase->FindPaperType(id);
1739
1740 if (!paper) paper = wxThePrintPaperDatabase->FindPaperType(wxPAPER_A4);
1741
1742 int w = 210;
1743 int h = 297;
1744 if (paper)
1745 {
1746 w = paper->GetWidth() / 10;
1747 h = paper->GetHeight() / 10;
1748 }
1749
1750 if (m_printData.GetOrientation() == wxLANDSCAPE)
1751 {
1752 int tmp = w;
1753 w = h;
1754 h = tmp;
1755 }
1756
1757 if (width) *width = w;
1758 if (height) *height = h;
1759 }
1760
1761 // Resolution in pixels per logical inch
1762 wxSize wxPostScriptImplDC::GetPPI(void) const
1763 {
1764 return wxSize( DPI, DPI );
1765 }
1766
1767
1768 bool wxPostScriptImplDC::StartDoc( const wxString& message )
1769 {
1770 wxCHECK_MSG( m_ok, false, wxT("invalid postscript dc") );
1771
1772 if (m_printData.GetPrintMode() != wxPRINT_MODE_STREAM )
1773 {
1774 if (m_printData.GetFilename() == wxEmptyString)
1775 {
1776 wxString filename = wxGetTempFileName( wxT("ps") );
1777 m_printData.SetFilename(filename);
1778 }
1779
1780 m_pstream = wxFopen( m_printData.GetFilename(), wxT("w+") );
1781
1782 if (!m_pstream)
1783 {
1784 wxLogError( _("Cannot open file for PostScript printing!"));
1785 m_ok = false;
1786 return false;
1787 }
1788 }
1789
1790 m_ok = true;
1791 m_title = message;
1792
1793 wxString buffer;
1794
1795 PsPrint( "%!PS-Adobe-2.0\n" );
1796
1797 buffer.Printf( "%%%%Title: %s\n", m_title );
1798 PsPrint( buffer );
1799 PsPrint( "%%Creator: wxWidgets PostScript renderer\n" );
1800
1801 buffer.Printf( "%%%%CreationDate: %s\n", wxNow() );
1802 PsPrint( buffer );
1803
1804 if (m_printData.GetOrientation() == wxLANDSCAPE)
1805 PsPrint( "%%Orientation: Landscape\n" );
1806 else
1807 PsPrint( "%%Orientation: Portrait\n" );
1808
1809 const wxChar *paper;
1810 switch (m_printData.GetPaperId())
1811 {
1812 case wxPAPER_LETTER: paper = wxT("Letter"); break; // Letter: paper ""; 8 1/2 by 11 inches
1813 case wxPAPER_LEGAL: paper = wxT("Legal"); break; // Legal, 8 1/2 by 14 inches
1814 case wxPAPER_A4: paper = wxT("A4"); break; // A4 Sheet, 210 by 297 millimeters
1815 case wxPAPER_TABLOID: paper = wxT("Tabloid"); break; // Tabloid, 11 by 17 inches
1816 case wxPAPER_LEDGER: paper = wxT("Ledger"); break; // Ledger, 17 by 11 inches
1817 case wxPAPER_STATEMENT: paper = wxT("Statement"); break; // Statement, 5 1/2 by 8 1/2 inches
1818 case wxPAPER_EXECUTIVE: paper = wxT("Executive"); break; // Executive, 7 1/4 by 10 1/2 inches
1819 case wxPAPER_A3: paper = wxT("A3"); break; // A3 sheet, 297 by 420 millimeters
1820 case wxPAPER_A5: paper = wxT("A5"); break; // A5 sheet, 148 by 210 millimeters
1821 case wxPAPER_B4: paper = wxT("B4"); break; // B4 sheet, 250 by 354 millimeters
1822 case wxPAPER_B5: paper = wxT("B5"); break; // B5 sheet, 182-by-257-millimeter paper
1823 case wxPAPER_FOLIO: paper = wxT("Folio"); break; // Folio, 8-1/2-by-13-inch paper
1824 case wxPAPER_QUARTO: paper = wxT("Quaro"); break; // Quarto, 215-by-275-millimeter paper
1825 case wxPAPER_10X14: paper = wxT("10x14"); break; // 10-by-14-inch sheet
1826 default: paper = wxT("A4");
1827 }
1828
1829 buffer.Printf( "%%%%DocumentPaperSizes: %s\n", paper );
1830 PsPrint( buffer );
1831
1832 PsPrint( "%%EndComments\n\n" );
1833
1834 PsPrint( "%%BeginProlog\n" );
1835 PsPrint( wxPostScriptHeaderConicTo );
1836 PsPrint( wxPostScriptHeaderEllipse );
1837 PsPrint( wxPostScriptHeaderEllipticArc );
1838 PsPrint( wxPostScriptHeaderColourImage );
1839 PsPrint( wxPostScriptHeaderReencodeISO1 );
1840 PsPrint( wxPostScriptHeaderReencodeISO2 );
1841 if (wxPostScriptHeaderSpline)
1842 PsPrint( wxPostScriptHeaderSpline );
1843 PsPrint( "%%EndProlog\n" );
1844
1845 SetBrush( *wxBLACK_BRUSH );
1846 SetPen( *wxBLACK_PEN );
1847 SetBackground( *wxWHITE_BRUSH );
1848 SetTextForeground( *wxBLACK );
1849
1850 // set origin according to paper size
1851 SetDeviceOrigin( 0,0 );
1852
1853 m_pageNumber = 1;
1854 return true;
1855 }
1856
1857 void wxPostScriptImplDC::EndDoc ()
1858 {
1859 wxCHECK_RET( m_ok, wxT("invalid postscript dc") );
1860
1861 if (m_clipping)
1862 {
1863 m_clipping = false;
1864 PsPrint( "grestore\n" );
1865 }
1866
1867 if ( m_pstream ) {
1868 fclose( m_pstream );
1869 m_pstream = (FILE *) NULL;
1870 }
1871
1872 #if 0
1873 // THE FOLLOWING HAS BEEN CONTRIBUTED BY Andy Fyfe <andy@hyperparallel.com>
1874 wxCoord wx_printer_translate_x, wx_printer_translate_y;
1875 double wx_printer_scale_x, wx_printer_scale_y;
1876
1877 wx_printer_translate_x = (wxCoord)m_printData.GetPrinterTranslateX();
1878 wx_printer_translate_y = (wxCoord)m_printData.GetPrinterTranslateY();
1879
1880 wx_printer_scale_x = m_printData.GetPrinterScaleX();
1881 wx_printer_scale_y = m_printData.GetPrinterScaleY();
1882
1883 // Compute the bounding box. Note that it is in the default user
1884 // coordinate system, thus we have to convert the values.
1885 wxCoord minX = (wxCoord) XLOG2DEV(m_minX);
1886 wxCoord minY = (wxCoord) YLOG2DEV(m_minY);
1887 wxCoord maxX = (wxCoord) XLOG2DEV(m_maxX);
1888 wxCoord maxY = (wxCoord) YLOG2DEV(m_maxY);
1889
1890 // LOG2DEV may have changed the minimum to maximum vice versa
1891 if ( minX > maxX ) { wxCoord tmp = minX; minX = maxX; maxX = tmp; }
1892 if ( minY > maxY ) { wxCoord tmp = minY; minY = maxY; maxY = tmp; }
1893
1894 // account for used scaling (boundingbox is before scaling in ps-file)
1895 double scale_x = m_printData.GetPrinterScaleX() / ms_PSScaleFactor;
1896 double scale_y = m_printData.GetPrinterScaleY() / ms_PSScaleFactor;
1897
1898 wxCoord llx, lly, urx, ury;
1899 llx = (wxCoord) ((minX+wx_printer_translate_x)*scale_x);
1900 lly = (wxCoord) ((minY+wx_printer_translate_y)*scale_y);
1901 urx = (wxCoord) ((maxX+wx_printer_translate_x)*scale_x);
1902 ury = (wxCoord) ((maxY+wx_printer_translate_y)*scale_y);
1903 // (end of bounding box computation)
1904
1905
1906 // If we're landscape, our sense of "x" and "y" is reversed.
1907 if (m_printData.GetOrientation() == wxLANDSCAPE)
1908 {
1909 wxCoord tmp;
1910 tmp = llx; llx = lly; lly = tmp;
1911 tmp = urx; urx = ury; ury = tmp;
1912
1913 // We need either the two lines that follow, or we need to subtract
1914 // min_x from real_translate_y, which is commented out below.
1915 llx = llx - (wxCoord)(m_minX*wx_printer_scale_y);
1916 urx = urx - (wxCoord)(m_minX*wx_printer_scale_y);
1917 }
1918
1919 // The Adobe specifications call for integers; we round as to make
1920 // the bounding larger.
1921 PsPrintf( wxT("%%%%BoundingBox: %d %d %d %d\n"),
1922 (wxCoord)floor((double)llx), (wxCoord)floor((double)lly),
1923 (wxCoord)ceil((double)urx), (wxCoord)ceil((double)ury) );
1924
1925 // To check the correctness of the bounding box, postscript commands
1926 // to draw a box corresponding to the bounding box are generated below.
1927 // But since we typically don't want to print such a box, the postscript
1928 // commands are generated within comments. These lines appear before any
1929 // adjustment of scale, rotation, or translation, and hence are in the
1930 // default user coordinates.
1931 PsPrint( "% newpath\n" );
1932 PsPrintf( wxT("%% %d %d moveto\n"), llx, lly );
1933 PsPrintf( wxT("%% %d %d lineto\n"), urx, lly );
1934 PsPrintf( wxT("%% %d %d lineto\n"), urx, ury );
1935 PsPrintf( wxT("%% %d %d lineto closepath stroke\n"), llx, ury );
1936 #endif
1937
1938 #ifndef __WXMSW__
1939 wxPostScriptPrintNativeData *data =
1940 (wxPostScriptPrintNativeData *) m_printData.GetNativeData();
1941
1942 if (m_ok && (m_printData.GetPrintMode() == wxPRINT_MODE_PRINTER))
1943 {
1944 wxString command;
1945 command += data->GetPrinterCommand();
1946 command += wxT(" ");
1947 command += data->GetPrinterOptions();
1948 command += wxT(" ");
1949 command += m_printData.GetFilename();
1950
1951 wxExecute( command, true );
1952 wxRemoveFile( m_printData.GetFilename() );
1953 }
1954 #endif
1955 }
1956
1957 void wxPostScriptImplDC::StartPage()
1958 {
1959 wxCHECK_RET( m_ok, wxT("invalid postscript dc") );
1960
1961 wxString buffer;
1962 buffer.Printf( wxT("%%%%Page: %d\n"), m_pageNumber++ );
1963 PsPrint( buffer );
1964
1965 #if 0
1966 wxPostScriptPrintNativeData *data =
1967 (wxPostScriptPrintNativeData *) m_printData.GetNativeData();
1968
1969 wxCoord translate_x = (wxCoord)data->GetPrinterTranslateX();
1970 wxCoord translate_y = (wxCoord)data->GetPrinterTranslateY();
1971
1972 buffer.Printf( "%d %d translate\n", translate_x, translate_y );
1973 PsPrint( buffer );
1974
1975 double scale_x = data->GetPrinterScaleX();
1976 double scale_y = data->GetPrinterScaleY();
1977
1978 buffer.Printf( "%f %f scale\n", scale_x, scale_y );
1979 buffer.Replace( ",", "." );
1980 PsPrint( buffer );
1981
1982 #endif
1983
1984 // Each page starts with an "initgraphics" which resets the
1985 // transformation and so we need to rotate the page for
1986 // landscape printing)
1987
1988 // I copied this one from a PostScript tutorial, but to no avail. RR.
1989 // PsPrint( "90 rotate llx neg ury nef translate\n" );
1990
1991 if (m_printData.GetOrientation() == wxLANDSCAPE)
1992 PsPrint( "90 rotate\n" );
1993 }
1994
1995 void wxPostScriptImplDC::EndPage ()
1996 {
1997 wxCHECK_RET( m_ok , wxT("invalid postscript dc") );
1998
1999 PsPrint( "showpage\n" );
2000 }
2001
2002 bool wxPostScriptImplDC::DoBlit( wxCoord xdest, wxCoord ydest,
2003 wxCoord fwidth, wxCoord fheight,
2004 wxDC *source,
2005 wxCoord xsrc, wxCoord ysrc,
2006 int rop, bool WXUNUSED(useMask), wxCoord WXUNUSED(xsrcMask), wxCoord WXUNUSED(ysrcMask) )
2007 {
2008 wxCHECK_MSG( m_ok, false, wxT("invalid postscript dc") );
2009
2010 wxCHECK_MSG( source, false, wxT("invalid source dc") );
2011
2012 // blit into a bitmap
2013 wxBitmap bitmap( (int)fwidth, (int)fheight );
2014 wxMemoryDC memDC;
2015 memDC.SelectObject(bitmap);
2016 memDC.Blit(0, 0, fwidth, fheight, source, xsrc, ysrc, rop); /* TODO: Blit transparently? */
2017 memDC.SelectObject(wxNullBitmap);
2018
2019 //draw bitmap. scaling and positioning is done there
2020 GetOwner()->DrawBitmap( bitmap, xdest, ydest );
2021
2022 return true;
2023 }
2024
2025 wxCoord wxPostScriptImplDC::GetCharHeight() const
2026 {
2027 if (m_font.Ok())
2028 return m_font.GetPointSize();
2029 else
2030 return 12;
2031 }
2032
2033 void wxPostScriptImplDC::PsPrint( const wxString& str )
2034 {
2035 const wxCharBuffer psdata(str.utf8_str());
2036
2037 wxPostScriptPrintNativeData *data =
2038 (wxPostScriptPrintNativeData *) m_printData.GetNativeData();
2039
2040 switch (m_printData.GetPrintMode())
2041 {
2042 #if wxUSE_STREAMS
2043 // append to output stream
2044 case wxPRINT_MODE_STREAM:
2045 {
2046 wxOutputStream* outputstream = data->GetOutputStream();
2047 wxCHECK_RET( outputstream, wxT("invalid outputstream") );
2048 outputstream->Write( psdata, strlen( psdata ) );
2049 }
2050 break;
2051 #endif // wxUSE_STREAMS
2052
2053 // save data into file
2054 default:
2055 wxCHECK_RET( m_pstream, wxT("invalid postscript dc") );
2056 fwrite( psdata, 1, strlen( psdata ), m_pstream );
2057 }
2058 }
2059
2060 void wxPostScriptImplDC::DoGetTextExtent(const wxString& string,
2061 wxCoord *x, wxCoord *y,
2062 wxCoord *descent, wxCoord *externalLeading,
2063 const wxFont *theFont ) const
2064 {
2065 const wxFont *fontToUse = theFont;
2066
2067 if (!fontToUse) fontToUse = &m_font;
2068
2069 wxCHECK_RET( fontToUse, wxT("GetTextExtent: no font defined") );
2070
2071 if (string.empty())
2072 {
2073 if (x) (*x) = 0;
2074 if (y) (*y) = 0;
2075 if (descent) (*descent) = 0;
2076 if (externalLeading) (*externalLeading) = 0;
2077 return;
2078 }
2079
2080 // GTK 2.0
2081
2082 const wxWX2MBbuf strbuf = string.mb_str();
2083
2084 // conversion failed (non e.g. ISO characters)
2085 if ( !strbuf )
2086 return;
2087
2088 #if !wxUSE_AFM_FOR_POSTSCRIPT
2089 /* Provide a VERY rough estimate (avoid using it).
2090 * Produces accurate results for mono-spaced font
2091 * such as Courier (aka wxMODERN) */
2092
2093 int height = 12;
2094 if (fontToUse)
2095 {
2096 height = fontToUse->GetPointSize();
2097 }
2098 if ( x )
2099 *x = strlen (strbuf) * height * 72 / 120;
2100 if ( y )
2101 *y = (wxCoord) (height * 1.32); /* allow for descender */
2102 if (descent) *descent = 0;
2103 if (externalLeading) *externalLeading = 0;
2104 #else
2105
2106 /* method for calculating string widths in postscript:
2107 / read in the AFM (adobe font metrics) file for the
2108 / actual font, parse it and extract the character widths
2109 / and also the descender. this may be improved, but for now
2110 / it works well. the AFM file is only read in if the
2111 / font is changed. this may be chached in the future.
2112 / calls to GetTextExtent with the font unchanged are rather
2113 / efficient!!!
2114 /
2115 / for each font and style used there is an AFM file necessary.
2116 / currently i have only files for the roman font family.
2117 / I try to get files for the other ones!
2118 /
2119 / CAVE: the size of the string is currently always calculated
2120 / in 'points' (1/72 of an inch). this should later on be
2121 / changed to depend on the mapping mode.
2122 / CAVE: the path to the AFM files must be set before calling this
2123 / fun3B3Bction. this is usually done by a call like the following:
2124 / wxSetAFMPath("d:\\wxw161\\afm\\");
2125 /
2126 / example:
2127 /
2128 / wxPostScriptDC dc(NULL, true);
2129 / if (dc.Ok()){
2130 / wxSetAFMPath("d:\\wxw161\\afm\\");
2131 / dc.StartDoc("Test");
2132 / dc.StartPage();
2133 / wxCoord w,h;
2134 / dc.SetFont(new wxFont(10, wxROMAN, wxNORMAL, wxNORMAL));
2135 / dc.GetTextExtent("Hallo",&w,&h);
2136 / dc.EndPage();
2137 / dc.EndDoc();
2138 / }
2139 /
2140 / by steve (stefan.hammes@urz.uni-heidelberg.de)
2141 / created: 10.09.94
2142 / updated: 14.05.95 */
2143
2144 /* these static vars are for storing the state between calls */
2145 static int lastFamily= INT_MIN;
2146 static int lastSize= INT_MIN;
2147 static int lastStyle= INT_MIN;
2148 static int lastWeight= INT_MIN;
2149 static int lastDescender = INT_MIN;
2150 static int lastWidths[256]; /* widths of the characters */
2151
2152 double UnderlinePosition = 0.0;
2153 double UnderlineThickness = 0.0;
2154
2155 // Get actual parameters
2156 int Family = fontToUse->GetFamily();
2157 int Size = fontToUse->GetPointSize();
2158 int Style = fontToUse->GetStyle();
2159 int Weight = fontToUse->GetWeight();
2160
2161 // If we have another font, read the font-metrics
2162 if (Family!=lastFamily || Size!=lastSize || Style!=lastStyle || Weight!=lastWeight)
2163 {
2164 // Store actual values
2165 lastFamily = Family;
2166 lastSize = Size;
2167 lastStyle = Style;
2168 lastWeight = Weight;
2169
2170 const wxChar *name;
2171
2172 switch (Family)
2173 {
2174 case wxMODERN:
2175 case wxTELETYPE:
2176 {
2177 if ((Style == wxITALIC) && (Weight == wxBOLD)) name = wxT("CourBoO.afm");
2178 else if ((Style != wxITALIC) && (Weight == wxBOLD)) name = wxT("CourBo.afm");
2179 else if ((Style == wxITALIC) && (Weight != wxBOLD)) name = wxT("CourO.afm");
2180 else name = wxT("Cour.afm");
2181 break;
2182 }
2183 case wxROMAN:
2184 {
2185 if ((Style == wxITALIC) && (Weight == wxBOLD)) name = wxT("TimesBoO.afm");
2186 else if ((Style != wxITALIC) && (Weight == wxBOLD)) name = wxT("TimesBo.afm");
2187 else if ((Style == wxITALIC) && (Weight != wxBOLD)) name = wxT("TimesO.afm");
2188 else name = wxT("TimesRo.afm");
2189 break;
2190 }
2191 case wxSCRIPT:
2192 {
2193 name = wxT("Zapf.afm");
2194 break;
2195 }
2196 case wxSWISS:
2197 default:
2198 {
2199 if ((Style == wxITALIC) && (Weight == wxBOLD)) name = wxT("HelvBoO.afm");
2200 else if ((Style != wxITALIC) && (Weight == wxBOLD)) name = wxT("HelvBo.afm");
2201 else if ((Style == wxITALIC) && (Weight != wxBOLD)) name = wxT("HelvO.afm");
2202 else name = wxT("Helv.afm");
2203 break;
2204 }
2205 }
2206
2207 FILE *afmFile = NULL;
2208
2209 // Get the directory of the AFM files
2210 wxString afmName;
2211
2212 // VZ: I don't know if the cast always works under Unix but it clearly
2213 // never does under Windows where the pointer is
2214 // wxWindowsPrintNativeData and so calling GetFontMetricPath() on
2215 // it just crashes
2216 #ifndef __WIN32__
2217 wxPostScriptPrintNativeData *data =
2218 wxDynamicCast(m_printData.GetNativeData(), wxPostScriptPrintNativeData);
2219
2220 if (data && !data->GetFontMetricPath().empty())
2221 {
2222 afmName = data->GetFontMetricPath();
2223 afmName << wxFILE_SEP_PATH << name;
2224 }
2225 #endif // __WIN32__
2226
2227 if ( !afmName.empty() )
2228 afmFile = wxFopen(afmName, wxT("r"));
2229
2230 if ( !afmFile )
2231 {
2232 #if defined(__UNIX__) && !defined(__VMS__)
2233 afmName = wxGetDataDir();
2234 #else // !__UNIX__
2235 afmName = wxStandardPaths::Get().GetDataDir();
2236 #endif // __UNIX__/!__UNIX__
2237
2238 afmName << wxFILE_SEP_PATH
2239 #if defined(__LINUX__) || defined(__FREEBSD__)
2240 << wxT("gs_afm") << wxFILE_SEP_PATH
2241 #else
2242 << wxT("afm") << wxFILE_SEP_PATH
2243 #endif
2244 << name;
2245 afmFile = wxFopen(afmName,wxT("r"));
2246 }
2247
2248 /* 2. open and process the file
2249 / a short explanation of the AFM format:
2250 / we have for each character a line, which gives its size
2251 / e.g.:
2252 /
2253 / C 63 ; WX 444 ; N question ; B 49 -14 395 676 ;
2254 /
2255 / that means, we have a character with ascii code 63, and width
2256 / (444/1000 * fontSize) points.
2257 / the other data is ignored for now!
2258 /
2259 / when the font has changed, we read in the right AFM file and store the
2260 / character widths in an array, which is processed below (see point 3.). */
2261 if (afmFile==NULL)
2262 {
2263 wxLogDebug( wxT("GetTextExtent: can't open AFM file '%s'"), afmName.c_str() );
2264 wxLogDebug( wxT(" using approximate values"));
2265 for (int i=0; i<256; i++) lastWidths[i] = 500; /* an approximate value */
2266 lastDescender = -150; /* dito. */
2267 }
2268 else
2269 {
2270 /* init the widths array */
2271 for(int i=0; i<256; i++) lastWidths[i] = INT_MIN;
2272 /* some variables for holding parts of a line */
2273 char cString[10], semiString[10], WXString[10];
2274 char descString[20];
2275 char upString[30], utString[30];
2276 char encString[50];
2277 char line[256];
2278 int ascii,cWidth;
2279 /* read in the file and parse it */
2280 while(fgets(line,sizeof(line),afmFile)!=NULL)
2281 {
2282 /* A.) check for descender definition */
2283 if (strncmp(line,"Descender",9)==0)
2284 {
2285 if ((sscanf(line,"%s%d",descString,&lastDescender)!=2) ||
2286 (strcmp(descString,"Descender")!=0))
2287 {
2288 wxLogDebug( wxT("AFM-file '%s': line '%s' has error (bad descender)"), afmName.c_str(),line );
2289 }
2290 }
2291 /* JC 1.) check for UnderlinePosition */
2292 else if(strncmp(line,"UnderlinePosition",17)==0)
2293 {
2294 if ((sscanf(line,"%s%lf",upString,&UnderlinePosition)!=2) ||
2295 (strcmp(upString,"UnderlinePosition")!=0))
2296 {
2297 wxLogDebug( wxT("AFM-file '%s': line '%s' has error (bad UnderlinePosition)"), afmName.c_str(), line );
2298 }
2299 }
2300 /* JC 2.) check for UnderlineThickness */
2301 else if(strncmp(line,"UnderlineThickness",18)==0)
2302 {
2303 if ((sscanf(line,"%s%lf",utString,&UnderlineThickness)!=2) ||
2304 (strcmp(utString,"UnderlineThickness")!=0))
2305 {
2306 wxLogDebug( wxT("AFM-file '%s': line '%s' has error (bad UnderlineThickness)"), afmName.c_str(), line );
2307 }
2308 }
2309 /* JC 3.) check for EncodingScheme */
2310 else if(strncmp(line,"EncodingScheme",14)==0)
2311 {
2312 if ((sscanf(line,"%s%s",utString,encString)!=2) ||
2313 (strcmp(utString,"EncodingScheme")!=0))
2314 {
2315 wxLogDebug( wxT("AFM-file '%s': line '%s' has error (bad EncodingScheme)"), afmName.c_str(), line );
2316 }
2317 else if (strncmp(encString, "AdobeStandardEncoding", 21))
2318 {
2319 wxLogDebug( wxT("AFM-file '%s': line '%s' has error (unsupported EncodingScheme %s)"),
2320 afmName.c_str(),line, encString);
2321 }
2322 }
2323 /* B.) check for char-width */
2324 else if(strncmp(line,"C ",2)==0)
2325 {
2326 if (sscanf(line,"%s%d%s%s%d",cString,&ascii,semiString,WXString,&cWidth)!=5)
2327 {
2328 wxLogDebug(wxT("AFM-file '%s': line '%s' has an error (bad character width)"),afmName.c_str(),line);
2329 }
2330 if(strcmp(cString,"C")!=0 || strcmp(semiString,";")!=0 || strcmp(WXString,"WX")!=0)
2331 {
2332 wxLogDebug(wxT("AFM-file '%s': line '%s' has a format error"),afmName.c_str(),line);
2333 }
2334 /* printf(" char '%c'=%d has width '%d'\n",ascii,ascii,cWidth); */
2335 if (ascii>=0 && ascii<256)
2336 {
2337 lastWidths[ascii] = cWidth; /* store width */
2338 }
2339 else
2340 {
2341 /* MATTHEW: this happens a lot; don't print an error */
2342 /* wxLogDebug("AFM-file '%s': ASCII value %d out of range",afmName.c_str(),ascii); */
2343 }
2344 }
2345 /* C.) ignore other entries. */
2346 }
2347 fclose(afmFile);
2348 }
2349 /* hack to compute correct values for german 'Umlaute'
2350 / the correct way would be to map the character names
2351 / like 'adieresis' to corresp. positions of ISOEnc and read
2352 / these values from AFM files, too. Maybe later ... */
2353
2354 // NB: casts to int are needed to suppress gcc 3.3 warnings
2355 lastWidths[196] = lastWidths[(int)'A']; // Ä
2356 lastWidths[228] = lastWidths[(int)'a']; // ä
2357 lastWidths[214] = lastWidths[(int)'O']; // Ö
2358 lastWidths[246] = lastWidths[(int)'o']; // ö
2359 lastWidths[220] = lastWidths[(int)'U']; // Ü
2360 lastWidths[252] = lastWidths[(int)'u']; // ü
2361 lastWidths[223] = lastWidths[(int)251]; // ß
2362
2363 /* JC: calculate UnderlineThickness/UnderlinePosition */
2364
2365 // VS: dirty, but is there any better solution?
2366 double *pt;
2367 pt = (double*) &m_underlinePosition;
2368 *pt = YLOG2DEVREL((wxCoord)(UnderlinePosition * fontToUse->GetPointSize())) / 1000.0f;
2369 pt = (double*) &m_underlineThickness;
2370 *pt = YLOG2DEVREL((wxCoord)(UnderlineThickness * fontToUse->GetPointSize())) / 1000.0f;
2371
2372 }
2373
2374
2375 /* 3. now the font metrics are read in, calc size this
2376 / is done by adding the widths of the characters in the
2377 / string. they are given in 1/1000 of the size! */
2378
2379 long sum=0;
2380 wxCoord height=Size; /* by default */
2381 unsigned char *p;
2382 for(p=(unsigned char *)wxMBSTRINGCAST strbuf; *p; p++)
2383 {
2384 if(lastWidths[*p]== INT_MIN)
2385 {
2386 wxLogDebug(wxT("GetTextExtent: undefined width for character '%c' (%d)"), *p,*p);
2387 sum += lastWidths[(unsigned char)' ']; /* assume space */
2388 }
2389 else
2390 {
2391 sum += lastWidths[*p];
2392 }
2393 }
2394
2395 double widthSum = sum;
2396 widthSum *= Size;
2397 widthSum /= 1000.0F;
2398
2399 /* add descender to height (it is usually a negative value) */
2400 //if (lastDescender != INT_MIN)
2401 //{
2402 // height += (wxCoord)(((-lastDescender)/1000.0F) * Size); /* MATTHEW: forgot scale */
2403 //}
2404 // - commented by V. Slavik - height already contains descender in it
2405 // (judging from few experiments)
2406
2407 /* return size values */
2408 if ( x )
2409 *x = (wxCoord)widthSum;
2410 if ( y )
2411 *y = height;
2412
2413 /* return other parameters */
2414 if (descent)
2415 {
2416 if(lastDescender!=INT_MIN)
2417 {
2418 *descent = (wxCoord)(((-lastDescender)/1000.0F) * Size); /* MATTHEW: forgot scale */
2419 }
2420 else
2421 {
2422 *descent = 0;
2423 }
2424 }
2425
2426 /* currently no idea how to calculate this! */
2427 if (externalLeading) *externalLeading = 0;
2428 #endif
2429 // Use AFM
2430 }
2431
2432
2433 #endif // wxUSE_PRINTING_ARCHITECTURE && wxUSE_POSTSCRIPT
2434
2435 // vi:sts=4:sw=4:et