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