wxGTK: Fixed printing to use fonts sizes adjustment consistent with wxMSW. In
[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 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 = (FILE*) 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 = (FILE*) NULL;
337 }
338 }
339
340 bool wxPostScriptDCImpl::IsOk() const
341 {
342 return m_ok;
343 }
344
345 wxRect wxPostScriptDCImpl::GetPaperRect()
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()
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), int 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.GetStyle() == wxPENSTYLE_TRANSPARENT) return;
431
432 SetPen( m_pen );
433
434 wxString buffer;
435 buffer.Printf( "newpath\n"
436 "%f %f moveto\n"
437 "%f %f lineto\n"
438 "stroke\n",
439 XLOG2DEV(x1), YLOG2DEV(y1),
440 XLOG2DEV(x2), YLOG2DEV(y2) );
441 buffer.Replace( ",", "." );
442 PsPrint( buffer );
443
444 CalcBoundingBox( x1, y1 );
445 CalcBoundingBox( x2, y2 );
446 }
447
448 void wxPostScriptDCImpl::DoDrawArc (wxCoord x1, wxCoord y1, wxCoord x2, wxCoord y2, wxCoord xc, wxCoord yc)
449 {
450 wxCHECK_RET( m_ok, wxT("invalid postscript dc") );
451
452 wxCoord dx = x1 - xc;
453 wxCoord dy = y1 - yc;
454 double radius = sqrt( (double)(dx*dx+dy*dy) );
455 double alpha1, alpha2;
456
457 if (x1 == x2 && y1 == y2)
458 {
459 alpha1 = 0.0;
460 alpha2 = 360.0;
461 }
462 else if ( wxIsNullDouble(radius) )
463 {
464 alpha1 =
465 alpha2 = 0.0;
466 }
467 else
468 {
469 alpha1 = (x1 - xc == 0) ?
470 (y1 - yc < 0) ? 90.0 : -90.0 :
471 -atan2(double(y1-yc), double(x1-xc)) * RAD2DEG;
472 alpha2 = (x2 - xc == 0) ?
473 (y2 - yc < 0) ? 90.0 : -90.0 :
474 -atan2(double(y2-yc), double(x2-xc)) * RAD2DEG;
475 }
476 while (alpha1 <= 0) alpha1 += 360;
477 while (alpha2 <= 0) alpha2 += 360; // adjust angles to be between
478 while (alpha1 > 360) alpha1 -= 360; // 0 and 360 degree
479 while (alpha2 > 360) alpha2 -= 360;
480
481 int i_radius = wxRound( radius );
482
483 if (m_brush.GetStyle() != wxBRUSHSTYLE_TRANSPARENT)
484 {
485 SetBrush( m_brush );
486
487 wxString buffer;
488 buffer.Printf( "newpath\n"
489 "%f %f %f %f %f %f ellipse\n"
490 "%f %f lineto\n"
491 "closepath\n"
492 "fill\n",
493 XLOG2DEV(xc), YLOG2DEV(yc),
494 XLOG2DEVREL(i_radius), YLOG2DEVREL(i_radius),
495 alpha1, alpha2,
496 XLOG2DEV(xc), YLOG2DEV(yc) );
497 buffer.Replace( ",", "." );
498 PsPrint( buffer );
499
500 CalcBoundingBox( xc-i_radius, yc-i_radius );
501 CalcBoundingBox( xc+i_radius, yc+i_radius );
502 }
503
504 if (m_pen.GetStyle() != wxPENSTYLE_TRANSPARENT)
505 {
506 SetPen( m_pen );
507
508 wxString buffer;
509 buffer.Printf( "newpath\n"
510 "%f %f %f %f %f %f ellipse\n"
511 "%f %f lineto\n"
512 "closepath\n"
513 "stroke\n",
514 XLOG2DEV(xc), YLOG2DEV(yc),
515 XLOG2DEVREL(i_radius), YLOG2DEVREL(i_radius),
516 alpha1, alpha2,
517 XLOG2DEV(xc), YLOG2DEV(yc) );
518 buffer.Replace( ",", "." );
519 PsPrint( buffer );
520
521 CalcBoundingBox( xc-i_radius, yc-i_radius );
522 CalcBoundingBox( xc+i_radius, yc+i_radius );
523 }
524 }
525
526 void wxPostScriptDCImpl::DoDrawEllipticArc(wxCoord x,wxCoord y,wxCoord w,wxCoord h,double sa,double ea)
527 {
528 wxCHECK_RET( m_ok, wxT("invalid postscript dc") );
529
530 if ( sa >= 360 || sa <= -360 )
531 sa -= int(sa/360)*360;
532 if ( ea >= 360 || ea <=- 360 )
533 ea -= int(ea/360)*360;
534 if ( sa < 0 )
535 sa += 360;
536 if ( ea < 0 )
537 ea += 360;
538
539 if ( wxIsSameDouble(sa, ea) )
540 {
541 DoDrawEllipse(x,y,w,h);
542 return;
543 }
544
545 if (m_brush.GetStyle () != wxBRUSHSTYLE_TRANSPARENT)
546 {
547 SetBrush( m_brush );
548
549 wxString buffer;
550 buffer.Printf( "newpath\n"
551 "%f %f %f %f %f %f true ellipticarc\n",
552 XLOG2DEV(x+w/2), YLOG2DEV(y+h/2),
553 XLOG2DEVREL(w/2), YLOG2DEVREL(h/2),
554 sa, ea );
555 buffer.Replace( ",", "." );
556 PsPrint( buffer );
557
558 CalcBoundingBox( x ,y );
559 CalcBoundingBox( x+w, y+h );
560 }
561
562 if (m_pen.GetStyle () != wxPENSTYLE_TRANSPARENT)
563 {
564 SetPen( m_pen );
565
566 wxString buffer;
567 buffer.Printf( "newpath\n"
568 "%f %f %f %f %f %f false ellipticarc\n",
569 XLOG2DEV(x+w/2), YLOG2DEV(y+h/2),
570 XLOG2DEVREL(w/2), YLOG2DEVREL(h/2),
571 sa, ea );
572 buffer.Replace( ",", "." );
573 PsPrint( buffer );
574
575 CalcBoundingBox( x ,y );
576 CalcBoundingBox( x+w, y+h );
577 }
578 }
579
580 void wxPostScriptDCImpl::DoDrawPoint (wxCoord x, wxCoord y)
581 {
582 wxCHECK_RET( m_ok, wxT("invalid postscript dc") );
583
584 if (m_pen.GetStyle() == wxPENSTYLE_TRANSPARENT) return;
585
586 SetPen (m_pen);
587
588 wxString buffer;
589 buffer.Printf( "newpath\n"
590 "%f %f moveto\n"
591 "%f %f lineto\n"
592 "stroke\n",
593 XLOG2DEV(x), YLOG2DEV(y),
594 XLOG2DEV(x+1), YLOG2DEV(y) );
595 buffer.Replace( ",", "." );
596 PsPrint( buffer );
597
598 CalcBoundingBox( x, y );
599 }
600
601 void wxPostScriptDCImpl::DoDrawPolygon (int n, wxPoint points[], wxCoord xoffset, wxCoord yoffset, int fillStyle)
602 {
603 wxCHECK_RET( m_ok, wxT("invalid postscript dc") );
604
605 if (n <= 0) return;
606
607 if (m_brush.GetStyle () != wxBRUSHSTYLE_TRANSPARENT)
608 {
609 SetBrush( m_brush );
610
611 PsPrint( "newpath\n" );
612
613 double xx = XLOG2DEV(points[0].x + xoffset);
614 double yy = YLOG2DEV(points[0].y + yoffset);
615
616 wxString buffer;
617 buffer.Printf( "%f %f moveto\n", xx, yy );
618 buffer.Replace( ",", "." );
619 PsPrint( buffer );
620
621 CalcBoundingBox( points[0].x + xoffset, points[0].y + yoffset );
622
623 for (int i = 1; i < n; i++)
624 {
625 xx = XLOG2DEV(points[i].x + xoffset);
626 yy = YLOG2DEV(points[i].y + yoffset);
627
628 buffer.Printf( "%f %f lineto\n", xx, yy );
629 buffer.Replace( ",", "." );
630 PsPrint( buffer );
631
632 CalcBoundingBox( points[i].x + xoffset, points[i].y + yoffset);
633 }
634
635 PsPrint( (fillStyle == wxODDEVEN_RULE ? "eofill\n" : "fill\n") );
636 }
637
638 if (m_pen.GetStyle () != wxPENSTYLE_TRANSPARENT)
639 {
640 SetPen( m_pen );
641
642 PsPrint( "newpath\n" );
643
644 double xx = XLOG2DEV(points[0].x + xoffset);
645 double yy = YLOG2DEV(points[0].y + yoffset);
646
647 wxString buffer;
648 buffer.Printf( "%f %f moveto\n", xx, yy );
649 buffer.Replace( ",", "." );
650 PsPrint( buffer );
651
652 CalcBoundingBox( points[0].x + xoffset, points[0].y + yoffset );
653
654 for (int i = 1; i < n; i++)
655 {
656 xx = XLOG2DEV(points[i].x + xoffset);
657 yy = YLOG2DEV(points[i].y + yoffset);
658
659 buffer.Printf( "%f %f lineto\n", xx, yy );
660 buffer.Replace( ",", "." );
661 PsPrint( buffer );
662
663 CalcBoundingBox( points[i].x + xoffset, points[i].y + yoffset);
664 }
665
666 PsPrint( "closepath\n" );
667 PsPrint( "stroke\n" );
668 }
669 }
670
671 void wxPostScriptDCImpl::DoDrawPolyPolygon (int n, int count[], wxPoint points[], wxCoord xoffset, wxCoord yoffset, int fillStyle)
672 {
673 wxCHECK_RET( m_ok, wxT("invalid postscript dc") );
674
675 if (n <= 0) return;
676
677 if (m_brush.GetStyle () != wxBRUSHSTYLE_TRANSPARENT)
678 {
679 SetBrush( m_brush );
680
681 PsPrint( "newpath\n" );
682
683 int ofs = 0;
684 for (int i = 0; i < n; ofs += count[i++])
685 {
686 double xx = XLOG2DEV(points[ofs].x + xoffset);
687 double yy = YLOG2DEV(points[ofs].y + yoffset);
688
689 wxString buffer;
690 buffer.Printf( "%f %f moveto\n", xx, yy );
691 buffer.Replace( ",", "." );
692 PsPrint( buffer );
693
694 CalcBoundingBox( points[ofs].x + xoffset, points[ofs].y + yoffset );
695
696 for (int j = 1; j < count[i]; j++)
697 {
698 xx = XLOG2DEV(points[ofs+j].x + xoffset);
699 yy = YLOG2DEV(points[ofs+j].y + yoffset);
700
701 buffer.Printf( "%f %f lineto\n", xx, yy );
702 buffer.Replace( ",", "." );
703 PsPrint( buffer );
704
705 CalcBoundingBox( points[ofs+j].x + xoffset, points[ofs+j].y + yoffset);
706 }
707 }
708 PsPrint( (fillStyle == wxODDEVEN_RULE ? "eofill\n" : "fill\n") );
709 }
710
711 if (m_pen.GetStyle () != wxPENSTYLE_TRANSPARENT)
712 {
713 SetPen( m_pen );
714
715 PsPrint( "newpath\n" );
716
717 int ofs = 0;
718 for (int i = 0; i < n; ofs += count[i++])
719 {
720 double xx = XLOG2DEV(points[ofs].x + xoffset);
721 double yy = YLOG2DEV(points[ofs].y + yoffset);
722
723 wxString buffer;
724 buffer.Printf( "%f %f moveto\n", xx, yy );
725 buffer.Replace( ",", "." );
726 PsPrint( buffer );
727
728 CalcBoundingBox( points[ofs].x + xoffset, points[ofs].y + yoffset );
729
730 for (int j = 1; j < count[i]; j++)
731 {
732 xx = XLOG2DEV(points[ofs+j].x + xoffset);
733 yy = YLOG2DEV(points[ofs+j].y + yoffset);
734
735 buffer.Printf( "%f %f lineto\n", xx, yy );
736 buffer.Replace( ",", "." );
737 PsPrint( buffer );
738
739 CalcBoundingBox( points[ofs+j].x + xoffset, points[ofs+j].y + yoffset);
740 }
741 }
742 PsPrint( "closepath\n" );
743 PsPrint( "stroke\n" );
744 }
745 }
746
747 void wxPostScriptDCImpl::DoDrawLines (int n, wxPoint points[], wxCoord xoffset, wxCoord yoffset)
748 {
749 wxCHECK_RET( m_ok, wxT("invalid postscript dc") );
750
751 if (m_pen.GetStyle() == wxPENSTYLE_TRANSPARENT) 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.GetStyle () != wxBRUSHSTYLE_TRANSPARENT)
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.GetStyle () != wxPENSTYLE_TRANSPARENT)
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.GetStyle () != wxBRUSHSTYLE_TRANSPARENT)
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.GetStyle () != wxPENSTYLE_TRANSPARENT)
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.GetStyle () != wxBRUSHSTYLE_TRANSPARENT)
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.GetStyle () != wxPENSTYLE_TRANSPARENT)
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 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.Ok()) return;
972
973 wxImage image = bitmap.ConvertToImage();
974
975 if (!image.Ok()) 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.Ok()) 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.Ok()) return;
1137
1138 int oldStyle = m_pen.GetStyle();
1139
1140 m_pen = pen;
1141
1142 double width;
1143
1144 if (m_pen.GetWidth() <= 0)
1145 width = 0.1;
1146 else
1147 width = (double) m_pen.GetWidth();
1148
1149 wxString buffer;
1150 buffer.Printf( "%f setlinewidth\n", width * DEV2PS * m_scaleX );
1151 buffer.Replace( ",", "." );
1152 PsPrint( buffer );
1153
1154 /*
1155 Line style - WRONG: 2nd arg is OFFSET
1156
1157 Here, I'm afraid you do not conceive meaning of parameters of 'setdash'
1158 operator correctly. You should look-up this in the Red Book: the 2nd parame-
1159 ter is not number of values in the array of the first one, but an offset
1160 into this description of the pattern. I mean a real *offset* not index
1161 into array. I.e. If the command is [3 4] 1 setdash is used, then there
1162 will be first black line *2* units wxCoord, then space 4 units, then the
1163 pattern of *3* units black, 4 units space will be repeated.
1164 */
1165
1166 static const char *dotted = "[2 5] 2";
1167 static const char *short_dashed = "[4 4] 2";
1168 static const char *wxCoord_dashed = "[4 8] 2";
1169 static const char *dotted_dashed = "[6 6 2 6] 4";
1170
1171 const char *psdash;
1172
1173 switch (m_pen.GetStyle())
1174 {
1175 case wxPENSTYLE_DOT: psdash = dotted; break;
1176 case wxPENSTYLE_SHORT_DASH: psdash = short_dashed; break;
1177 case wxPENSTYLE_LONG_DASH: psdash = wxCoord_dashed; break;
1178 case wxPENSTYLE_DOT_DASH: psdash = dotted_dashed; break;
1179 case wxPENSTYLE_USER_DASH:
1180 {
1181 wxDash *dashes;
1182 int nDashes = m_pen.GetDashes (&dashes);
1183 PsPrint ("[");
1184 for (int i = 0; i < nDashes; ++i)
1185 {
1186 buffer.Printf( "%d ", dashes [i] );
1187 PsPrint( buffer );
1188 }
1189 PsPrint ("] 0 setdash\n");
1190 psdash = 0;
1191 }
1192 break;
1193 case wxPENSTYLE_SOLID:
1194 case wxPENSTYLE_TRANSPARENT:
1195 default: psdash = "[] 0"; break;
1196 }
1197
1198 if ( psdash && (oldStyle != m_pen.GetStyle()) )
1199 {
1200 PsPrint( psdash );
1201 PsPrint( " setdash\n" );
1202 }
1203
1204 // Line colour
1205 unsigned char red = m_pen.GetColour().Red();
1206 unsigned char blue = m_pen.GetColour().Blue();
1207 unsigned char green = m_pen.GetColour().Green();
1208
1209 if (!m_colour)
1210 {
1211 // Anything not white is black
1212 if (! (red == (unsigned char) 255 &&
1213 blue == (unsigned char) 255 &&
1214 green == (unsigned char) 255) )
1215 {
1216 red = (unsigned char) 0;
1217 green = (unsigned char) 0;
1218 blue = (unsigned char) 0;
1219 }
1220 // setgray here ?
1221 }
1222
1223 if (!(red == m_currentRed && green == m_currentGreen && blue == m_currentBlue))
1224 {
1225 double redPS = (double)(red) / 255.0;
1226 double bluePS = (double)(blue) / 255.0;
1227 double greenPS = (double)(green) / 255.0;
1228
1229 wxString buffer;
1230 buffer.Printf( "%f %f %f setrgbcolor\n", redPS, greenPS, bluePS );
1231 buffer.Replace( ",", "." );
1232 PsPrint( buffer );
1233
1234 m_currentRed = red;
1235 m_currentBlue = blue;
1236 m_currentGreen = green;
1237 }
1238 }
1239
1240 void wxPostScriptDCImpl::SetBrush( const wxBrush& brush )
1241 {
1242 wxCHECK_RET( m_ok, wxT("invalid postscript dc") );
1243
1244 if (!brush.Ok()) return;
1245
1246 m_brush = brush;
1247
1248 // Brush colour
1249 unsigned char red = m_brush.GetColour().Red();
1250 unsigned char blue = m_brush.GetColour().Blue();
1251 unsigned char green = m_brush.GetColour().Green();
1252
1253 if (!m_colour)
1254 {
1255 // Anything not white is black
1256 if (! (red == (unsigned char) 255 &&
1257 blue == (unsigned char) 255 &&
1258 green == (unsigned char) 255) )
1259 {
1260 red = (unsigned char) 0;
1261 green = (unsigned char) 0;
1262 blue = (unsigned char) 0;
1263 }
1264 // setgray here ?
1265 }
1266
1267 if (!(red == m_currentRed && green == m_currentGreen && blue == m_currentBlue))
1268 {
1269 double redPS = (double)(red) / 255.0;
1270 double bluePS = (double)(blue) / 255.0;
1271 double greenPS = (double)(green) / 255.0;
1272
1273 wxString buffer;
1274 buffer.Printf( "%f %f %f setrgbcolor\n", redPS, greenPS, bluePS );
1275 buffer.Replace( ",", "." );
1276 PsPrint( buffer );
1277
1278 m_currentRed = red;
1279 m_currentBlue = blue;
1280 m_currentGreen = green;
1281 }
1282 }
1283
1284 void wxPostScriptDCImpl::DoDrawText( const wxString& text, wxCoord x, wxCoord y )
1285 {
1286 wxCHECK_RET( m_ok, wxT("invalid postscript dc") );
1287
1288 const wxWX2MBbuf textbuf = text.mb_str();
1289 if ( !textbuf )
1290 return;
1291
1292 if (m_textForegroundColour.Ok())
1293 {
1294 unsigned char red = m_textForegroundColour.Red();
1295 unsigned char blue = m_textForegroundColour.Blue();
1296 unsigned char green = m_textForegroundColour.Green();
1297
1298 if (!m_colour)
1299 {
1300 // Anything not white is black
1301 if (! (red == (unsigned char) 255 &&
1302 blue == (unsigned char) 255 &&
1303 green == (unsigned char) 255))
1304 {
1305 red = (unsigned char) 0;
1306 green = (unsigned char) 0;
1307 blue = (unsigned char) 0;
1308 }
1309 }
1310
1311 // maybe setgray here ?
1312 if (!(red == m_currentRed && green == m_currentGreen && blue == m_currentBlue))
1313 {
1314 double redPS = (double)(red) / 255.0;
1315 double bluePS = (double)(blue) / 255.0;
1316 double greenPS = (double)(green) / 255.0;
1317
1318 wxString buffer;
1319 buffer.Printf( "%f %f %f setrgbcolor\n", redPS, greenPS, bluePS );
1320 buffer.Replace( ",", "." );
1321 PsPrint( buffer );
1322
1323 m_currentRed = red;
1324 m_currentBlue = blue;
1325 m_currentGreen = green;
1326 }
1327 }
1328
1329 wxCoord text_w, text_h, text_descent;
1330
1331 GetOwner()->GetTextExtent(text, &text_w, &text_h, &text_descent);
1332
1333 int size = m_font.GetPointSize();
1334
1335 // wxCoord by = y + (wxCoord)floor( double(size) * 2.0 / 3.0 ); // approximate baseline
1336 // commented by V. Slavik and replaced by accurate version
1337 // - note that there is still rounding error in text_descent!
1338 wxCoord by = y + size - text_descent; // baseline
1339
1340 wxString buffer;
1341 buffer.Printf( "%f %f moveto\n", XLOG2DEV(x), YLOG2DEV(by) );
1342 buffer.Replace( ",", "." );
1343 PsPrint( buffer );
1344 PsPrint( "(" );
1345
1346 for ( const char *p = textbuf; *p != '\0'; p++ )
1347 {
1348 int c = (unsigned char)*p;
1349 if (c == ')' || c == '(' || c == '\\')
1350 {
1351 /* Cope with special characters */
1352 PsPrint( "\\" );
1353 PsPrint( (char) c );
1354 }
1355 else if ( c >= 128 )
1356 {
1357 /* Cope with character codes > 127 */
1358 buffer.Printf( "\\%o", c );
1359 PsPrint( buffer );
1360 }
1361 else
1362 {
1363 PsPrint( (char) c );
1364 }
1365 }
1366
1367 PsPrint( ") show\n" );
1368
1369 if (m_font.GetUnderlined())
1370 {
1371 wxCoord uy = (wxCoord)(y + size - m_underlinePosition);
1372
1373 buffer.Printf( "gsave\n"
1374 "%f %f moveto\n"
1375 "%f setlinewidth\n"
1376 "%f %f lineto\n"
1377 "stroke\n"
1378 "grestore\n",
1379 XLOG2DEV(x), YLOG2DEV(uy),
1380 m_underlineThickness,
1381 XLOG2DEV(x + text_w), YLOG2DEV(uy) );
1382 buffer.Replace( ",", "." );
1383 PsPrint( buffer );
1384 }
1385
1386 CalcBoundingBox( x, y );
1387 CalcBoundingBox( x + size * text.length() * 2/3 , y );
1388 }
1389
1390 void wxPostScriptDCImpl::DoDrawRotatedText( const wxString& text, wxCoord x, wxCoord y, double angle )
1391 {
1392 if ( wxIsNullDouble(angle) )
1393 {
1394 DoDrawText(text, x, y);
1395 return;
1396 }
1397
1398 wxCHECK_RET( m_ok, wxT("invalid postscript dc") );
1399
1400 SetFont( m_font );
1401
1402 if (m_textForegroundColour.Ok())
1403 {
1404 unsigned char red = m_textForegroundColour.Red();
1405 unsigned char blue = m_textForegroundColour.Blue();
1406 unsigned char green = m_textForegroundColour.Green();
1407
1408 if (!m_colour)
1409 {
1410 // Anything not white is black
1411 if (! (red == (unsigned char) 255 &&
1412 blue == (unsigned char) 255 &&
1413 green == (unsigned char) 255))
1414 {
1415 red = (unsigned char) 0;
1416 green = (unsigned char) 0;
1417 blue = (unsigned char) 0;
1418 }
1419 }
1420
1421 // maybe setgray here ?
1422 if (!(red == m_currentRed && green == m_currentGreen && blue == m_currentBlue))
1423 {
1424 double redPS = (double)(red) / 255.0;
1425 double bluePS = (double)(blue) / 255.0;
1426 double greenPS = (double)(green) / 255.0;
1427
1428 wxString buffer;
1429 buffer.Printf( "%f %f %f setrgbcolor\n", redPS, greenPS, bluePS );
1430 buffer.Replace( ",", "." );
1431 PsPrint( buffer );
1432
1433 m_currentRed = red;
1434 m_currentBlue = blue;
1435 m_currentGreen = green;
1436 }
1437 }
1438
1439 int size = m_font.GetPointSize();
1440
1441 wxString buffer;
1442 buffer.Printf( "%f %f moveto\n", XLOG2DEV(x), YLOG2DEV(y));
1443 buffer.Replace( ",", "." );
1444 PsPrint( buffer );
1445
1446 buffer.Printf( "%f rotate\n", angle );
1447 buffer.Replace( ",", "." );
1448 PsPrint( buffer );
1449
1450 PsPrint( "(" );
1451 const wxWX2MBbuf textbuf = text.mb_str();
1452 if ( textbuf )
1453 {
1454 for ( const char *p = textbuf; *p != '\0'; p++ )
1455 {
1456 int c = (unsigned char)*p;
1457 if (c == ')' || c == '(' || c == '\\')
1458 {
1459 /* Cope with special characters */
1460 PsPrint( "\\" );
1461 PsPrint( (char) c );
1462 }
1463 else if ( c >= 128 )
1464 {
1465 /* Cope with character codes > 127 */
1466 buffer.Printf( "\\%o", c);
1467 PsPrint( buffer );
1468 }
1469 else
1470 {
1471 PsPrint( (char) c );
1472 }
1473 }
1474 }
1475
1476 PsPrint( ") show\n" );
1477
1478 buffer.Printf( "%f rotate\n", -angle );
1479 buffer.Replace( ",", "." );
1480 PsPrint( buffer );
1481
1482 if (m_font.GetUnderlined())
1483 {
1484 wxCoord uy = (wxCoord)(y + size - m_underlinePosition);
1485 wxCoord w, h;
1486 GetOwner()->GetTextExtent(text, &w, &h);
1487
1488 buffer.Printf(
1489 "gsave\n"
1490 "%f %f moveto\n"
1491 "%f setlinewidth\n"
1492 "%f %f lineto\n"
1493 "stroke\n"
1494 "grestore\n",
1495 XLOG2DEV(x), YLOG2DEV(uy),
1496 m_underlineThickness,
1497 XLOG2DEV(x + w), YLOG2DEV(uy) );
1498 buffer.Replace( ",", "." );
1499 PsPrint( buffer );
1500 }
1501
1502 CalcBoundingBox( x, y );
1503 CalcBoundingBox( x + size * text.length() * 2/3 , y );
1504 }
1505
1506 void wxPostScriptDCImpl::SetBackground (const wxBrush& brush)
1507 {
1508 m_backgroundBrush = brush;
1509 }
1510
1511 void wxPostScriptDCImpl::SetLogicalFunction (int WXUNUSED(function))
1512 {
1513 wxFAIL_MSG( wxT("wxPostScriptDCImpl::SetLogicalFunction not implemented.") );
1514 }
1515
1516 #if wxUSE_SPLINES
1517 void wxPostScriptDCImpl::DoDrawSpline( const wxPointList *points )
1518 {
1519 wxCHECK_RET( m_ok, wxT("invalid postscript dc") );
1520
1521 SetPen( m_pen );
1522
1523 // a and b are not used
1524 //double a, b;
1525 double c, d, x1, y1, x2, y2, x3, y3;
1526 wxPoint *p, *q;
1527
1528 wxPointList::compatibility_iterator node = points->GetFirst();
1529 p = node->GetData();
1530 x1 = p->x;
1531 y1 = p->y;
1532
1533 node = node->GetNext();
1534 p = node->GetData();
1535 c = p->x;
1536 d = p->y;
1537 x3 =
1538 #if 0
1539 a =
1540 #endif
1541 (double)(x1 + c) / 2;
1542 y3 =
1543 #if 0
1544 b =
1545 #endif
1546 (double)(y1 + d) / 2;
1547
1548 wxString buffer;
1549 buffer.Printf( "newpath\n"
1550 "%f %f moveto\n"
1551 "%f %f lineto\n",
1552 XLOG2DEV(wxRound(x1)), YLOG2DEV(wxRound(y1)),
1553 XLOG2DEV(wxRound(x3)), YLOG2DEV(wxRound(y3)) );
1554 buffer.Replace( ",", "." );
1555 PsPrint( buffer );
1556
1557 CalcBoundingBox( (wxCoord)x1, (wxCoord)y1 );
1558 CalcBoundingBox( (wxCoord)x3, (wxCoord)y3 );
1559
1560 node = node->GetNext();
1561 while (node)
1562 {
1563 q = node->GetData();
1564
1565 x1 = x3;
1566 y1 = y3;
1567 x2 = c;
1568 y2 = d;
1569 c = q->x;
1570 d = q->y;
1571 x3 = (double)(x2 + c) / 2;
1572 y3 = (double)(y2 + d) / 2;
1573
1574 buffer.Printf( "%f %f %f %f %f %f DrawSplineSection\n",
1575 XLOG2DEV(wxRound(x1)), YLOG2DEV(wxRound(y1)),
1576 XLOG2DEV(wxRound(x2)), YLOG2DEV(wxRound(y2)),
1577 XLOG2DEV(wxRound(x3)), YLOG2DEV(wxRound(y3)) );
1578 buffer.Replace( ",", "." );
1579 PsPrint( buffer );
1580
1581 CalcBoundingBox( (wxCoord)x1, (wxCoord)y1 );
1582 CalcBoundingBox( (wxCoord)x3, (wxCoord)y3 );
1583
1584 node = node->GetNext();
1585 }
1586
1587 /*
1588 At this point, (x2,y2) and (c,d) are the position of the
1589 next-to-last and last point respectively, in the point list
1590 */
1591
1592 buffer.Printf( "%f %f lineto\nstroke\n", XLOG2DEV(wxRound(c)), YLOG2DEV(wxRound(d)) );
1593 buffer.Replace( ",", "." );
1594 PsPrint( buffer );
1595 }
1596 #endif // wxUSE_SPLINES
1597
1598 wxCoord wxPostScriptDCImpl::GetCharWidth() const
1599 {
1600 // Chris Breeze: reasonable approximation using wxMODERN/Courier
1601 return (wxCoord) (GetCharHeight() * 72.0 / 120.0);
1602 }
1603
1604 void wxPostScriptDCImpl::SetPrintData(const wxPrintData& data)
1605 {
1606 m_printData = data;
1607
1608 wxPaperSize id = m_printData.GetPaperId();
1609 wxPrintPaperType *paper = wxThePrintPaperDatabase->FindPaperType(id);
1610 if (!paper) paper = wxThePrintPaperDatabase->FindPaperType(wxPAPER_A4);
1611 int w = 595;
1612 int h = 842;
1613 if (paper)
1614 {
1615 w = paper->GetSizeDeviceUnits().x;
1616 h = paper->GetSizeDeviceUnits().y;
1617 }
1618
1619 if (m_printData.GetOrientation() == wxLANDSCAPE)
1620 m_pageHeight = w * PS2DEV;
1621 else
1622 m_pageHeight = h * PS2DEV;
1623 }
1624
1625 void wxPostScriptDCImpl::ComputeScaleAndOrigin()
1626 {
1627 const wxRealPoint origScale(m_scaleX, m_scaleY);
1628
1629 wxDCImpl::ComputeScaleAndOrigin();
1630
1631 // If scale has changed call SetPen to recalulate the line width
1632 // and SetFont to recalculate font size
1633 if ( wxRealPoint(m_scaleX, m_scaleY) != origScale && m_pen.IsOk() )
1634 {
1635 SetPen( m_pen );
1636 SetFont( m_font );
1637 }
1638 }
1639
1640 void wxPostScriptDCImpl::DoGetSize(int* width, int* height) const
1641 {
1642 wxPaperSize id = m_printData.GetPaperId();
1643
1644 wxPrintPaperType *paper = wxThePrintPaperDatabase->FindPaperType(id);
1645
1646 if (!paper) paper = wxThePrintPaperDatabase->FindPaperType(wxPAPER_A4);
1647
1648 int w = 595;
1649 int h = 842;
1650 if (paper)
1651 {
1652 w = paper->GetSizeDeviceUnits().x;
1653 h = paper->GetSizeDeviceUnits().y;
1654 }
1655
1656 if (m_printData.GetOrientation() == wxLANDSCAPE)
1657 {
1658 int tmp = w;
1659 w = h;
1660 h = tmp;
1661 }
1662
1663 if (width)
1664 *width = wxRound( w * PS2DEV );
1665
1666 if (height)
1667 *height = wxRound( h * PS2DEV );
1668 }
1669
1670 void wxPostScriptDCImpl::DoGetSizeMM(int *width, int *height) const
1671 {
1672 wxPaperSize id = m_printData.GetPaperId();
1673
1674 wxPrintPaperType *paper = wxThePrintPaperDatabase->FindPaperType(id);
1675
1676 if (!paper) paper = wxThePrintPaperDatabase->FindPaperType(wxPAPER_A4);
1677
1678 int w = 210;
1679 int h = 297;
1680 if (paper)
1681 {
1682 w = paper->GetWidth() / 10;
1683 h = paper->GetHeight() / 10;
1684 }
1685
1686 if (m_printData.GetOrientation() == wxLANDSCAPE)
1687 {
1688 int tmp = w;
1689 w = h;
1690 h = tmp;
1691 }
1692
1693 if (width) *width = w;
1694 if (height) *height = h;
1695 }
1696
1697 // Resolution in pixels per logical inch
1698 wxSize wxPostScriptDCImpl::GetPPI(void) const
1699 {
1700 return wxSize( DPI, DPI );
1701 }
1702
1703
1704 bool wxPostScriptDCImpl::StartDoc( const wxString& WXUNUSED(message) )
1705 {
1706 wxCHECK_MSG( m_ok, false, wxT("invalid postscript dc") );
1707
1708 if (m_printData.GetPrintMode() != wxPRINT_MODE_STREAM )
1709 {
1710 if (m_printData.GetFilename() == wxEmptyString)
1711 {
1712 wxString filename = wxGetTempFileName( wxT("ps") );
1713 m_printData.SetFilename(filename);
1714 }
1715
1716 m_pstream = wxFopen( m_printData.GetFilename(), wxT("w+") );
1717
1718 if (!m_pstream)
1719 {
1720 wxLogError( _("Cannot open file for PostScript printing!"));
1721 m_ok = false;
1722 return false;
1723 }
1724 }
1725
1726 m_ok = true;
1727
1728 wxString buffer;
1729
1730 PsPrint( "%!PS-Adobe-2.0\n" );
1731
1732 PsPrint( "%%Creator: wxWidgets PostScript renderer\n" );
1733
1734 buffer.Printf( "%%%%CreationDate: %s\n", wxNow() );
1735 PsPrint( buffer );
1736
1737 if (m_printData.GetOrientation() == wxLANDSCAPE)
1738 PsPrint( "%%Orientation: Landscape\n" );
1739 else
1740 PsPrint( "%%Orientation: Portrait\n" );
1741
1742 const wxChar *paper;
1743 switch (m_printData.GetPaperId())
1744 {
1745 case wxPAPER_LETTER: paper = wxT("Letter"); break; // Letter: paper ""; 8 1/2 by 11 inches
1746 case wxPAPER_LEGAL: paper = wxT("Legal"); break; // Legal, 8 1/2 by 14 inches
1747 case wxPAPER_A4: paper = wxT("A4"); break; // A4 Sheet, 210 by 297 millimeters
1748 case wxPAPER_TABLOID: paper = wxT("Tabloid"); break; // Tabloid, 11 by 17 inches
1749 case wxPAPER_LEDGER: paper = wxT("Ledger"); break; // Ledger, 17 by 11 inches
1750 case wxPAPER_STATEMENT: paper = wxT("Statement"); break; // Statement, 5 1/2 by 8 1/2 inches
1751 case wxPAPER_EXECUTIVE: paper = wxT("Executive"); break; // Executive, 7 1/4 by 10 1/2 inches
1752 case wxPAPER_A3: paper = wxT("A3"); break; // A3 sheet, 297 by 420 millimeters
1753 case wxPAPER_A5: paper = wxT("A5"); break; // A5 sheet, 148 by 210 millimeters
1754 case wxPAPER_B4: paper = wxT("B4"); break; // B4 sheet, 250 by 354 millimeters
1755 case wxPAPER_B5: paper = wxT("B5"); break; // B5 sheet, 182-by-257-millimeter paper
1756 case wxPAPER_FOLIO: paper = wxT("Folio"); break; // Folio, 8-1/2-by-13-inch paper
1757 case wxPAPER_QUARTO: paper = wxT("Quaro"); break; // Quarto, 215-by-275-millimeter paper
1758 case wxPAPER_10X14: paper = wxT("10x14"); break; // 10-by-14-inch sheet
1759 default: paper = wxT("A4");
1760 }
1761
1762 buffer.Printf( "%%%%DocumentPaperSizes: %s\n", paper );
1763 PsPrint( buffer );
1764
1765 PsPrint( "%%EndComments\n\n" );
1766
1767 PsPrint( "%%BeginProlog\n" );
1768 PsPrint( wxPostScriptHeaderConicTo );
1769 PsPrint( wxPostScriptHeaderEllipse );
1770 PsPrint( wxPostScriptHeaderEllipticArc );
1771 PsPrint( wxPostScriptHeaderColourImage );
1772 PsPrint( wxPostScriptHeaderReencodeISO1 );
1773 PsPrint( wxPostScriptHeaderReencodeISO2 );
1774 if (wxPostScriptHeaderSpline)
1775 PsPrint( wxPostScriptHeaderSpline );
1776 PsPrint( "%%EndProlog\n" );
1777
1778 SetBrush( *wxBLACK_BRUSH );
1779 SetPen( *wxBLACK_PEN );
1780 SetBackground( *wxWHITE_BRUSH );
1781 SetTextForeground( *wxBLACK );
1782
1783 // set origin according to paper size
1784 SetDeviceOrigin( 0,0 );
1785
1786 m_pageNumber = 1;
1787 return true;
1788 }
1789
1790 void wxPostScriptDCImpl::EndDoc ()
1791 {
1792 wxCHECK_RET( m_ok, wxT("invalid postscript dc") );
1793
1794 if (m_clipping)
1795 {
1796 m_clipping = false;
1797 PsPrint( "grestore\n" );
1798 }
1799
1800 if ( m_pstream ) {
1801 fclose( m_pstream );
1802 m_pstream = (FILE *) NULL;
1803 }
1804
1805 #if 0
1806 // THE FOLLOWING HAS BEEN CONTRIBUTED BY Andy Fyfe <andy@hyperparallel.com>
1807 wxCoord wx_printer_translate_x, wx_printer_translate_y;
1808 double wx_printer_scale_x, wx_printer_scale_y;
1809
1810 wx_printer_translate_x = (wxCoord)m_printData.GetPrinterTranslateX();
1811 wx_printer_translate_y = (wxCoord)m_printData.GetPrinterTranslateY();
1812
1813 wx_printer_scale_x = m_printData.GetPrinterScaleX();
1814 wx_printer_scale_y = m_printData.GetPrinterScaleY();
1815
1816 // Compute the bounding box. Note that it is in the default user
1817 // coordinate system, thus we have to convert the values.
1818 wxCoord minX = (wxCoord) XLOG2DEV(m_minX);
1819 wxCoord minY = (wxCoord) YLOG2DEV(m_minY);
1820 wxCoord maxX = (wxCoord) XLOG2DEV(m_maxX);
1821 wxCoord maxY = (wxCoord) YLOG2DEV(m_maxY);
1822
1823 // LOG2DEV may have changed the minimum to maximum vice versa
1824 if ( minX > maxX ) { wxCoord tmp = minX; minX = maxX; maxX = tmp; }
1825 if ( minY > maxY ) { wxCoord tmp = minY; minY = maxY; maxY = tmp; }
1826
1827 // account for used scaling (boundingbox is before scaling in ps-file)
1828 double scale_x = m_printData.GetPrinterScaleX() / ms_PSScaleFactor;
1829 double scale_y = m_printData.GetPrinterScaleY() / ms_PSScaleFactor;
1830
1831 wxCoord llx, lly, urx, ury;
1832 llx = (wxCoord) ((minX+wx_printer_translate_x)*scale_x);
1833 lly = (wxCoord) ((minY+wx_printer_translate_y)*scale_y);
1834 urx = (wxCoord) ((maxX+wx_printer_translate_x)*scale_x);
1835 ury = (wxCoord) ((maxY+wx_printer_translate_y)*scale_y);
1836 // (end of bounding box computation)
1837
1838
1839 // If we're landscape, our sense of "x" and "y" is reversed.
1840 if (m_printData.GetOrientation() == wxLANDSCAPE)
1841 {
1842 wxCoord tmp;
1843 tmp = llx; llx = lly; lly = tmp;
1844 tmp = urx; urx = ury; ury = tmp;
1845
1846 // We need either the two lines that follow, or we need to subtract
1847 // min_x from real_translate_y, which is commented out below.
1848 llx = llx - (wxCoord)(m_minX*wx_printer_scale_y);
1849 urx = urx - (wxCoord)(m_minX*wx_printer_scale_y);
1850 }
1851
1852 // The Adobe specifications call for integers; we round as to make
1853 // the bounding larger.
1854 PsPrintf( wxT("%%%%BoundingBox: %d %d %d %d\n"),
1855 (wxCoord)floor((double)llx), (wxCoord)floor((double)lly),
1856 (wxCoord)ceil((double)urx), (wxCoord)ceil((double)ury) );
1857
1858 // To check the correctness of the bounding box, postscript commands
1859 // to draw a box corresponding to the bounding box are generated below.
1860 // But since we typically don't want to print such a box, the postscript
1861 // commands are generated within comments. These lines appear before any
1862 // adjustment of scale, rotation, or translation, and hence are in the
1863 // default user coordinates.
1864 PsPrint( "% newpath\n" );
1865 PsPrintf( wxT("%% %d %d moveto\n"), llx, lly );
1866 PsPrintf( wxT("%% %d %d lineto\n"), urx, lly );
1867 PsPrintf( wxT("%% %d %d lineto\n"), urx, ury );
1868 PsPrintf( wxT("%% %d %d lineto closepath stroke\n"), llx, ury );
1869 #endif
1870
1871 #ifndef __WXMSW__
1872 wxPostScriptPrintNativeData *data =
1873 (wxPostScriptPrintNativeData *) m_printData.GetNativeData();
1874
1875 if (m_ok && (m_printData.GetPrintMode() == wxPRINT_MODE_PRINTER))
1876 {
1877 wxString command;
1878 command += data->GetPrinterCommand();
1879 command += wxT(" ");
1880 command += data->GetPrinterOptions();
1881 command += wxT(" ");
1882 command += m_printData.GetFilename();
1883
1884 wxExecute( command, true );
1885 wxRemoveFile( m_printData.GetFilename() );
1886 }
1887 #endif
1888 }
1889
1890 void wxPostScriptDCImpl::StartPage()
1891 {
1892 wxCHECK_RET( m_ok, wxT("invalid postscript dc") );
1893
1894 wxString buffer;
1895 buffer.Printf( wxT("%%%%Page: %d\n"), m_pageNumber++ );
1896 PsPrint( buffer );
1897
1898 #if 0
1899 wxPostScriptPrintNativeData *data =
1900 (wxPostScriptPrintNativeData *) m_printData.GetNativeData();
1901
1902 wxCoord translate_x = (wxCoord)data->GetPrinterTranslateX();
1903 wxCoord translate_y = (wxCoord)data->GetPrinterTranslateY();
1904
1905 buffer.Printf( "%d %d translate\n", translate_x, translate_y );
1906 PsPrint( buffer );
1907
1908 double scale_x = data->GetPrinterScaleX();
1909 double scale_y = data->GetPrinterScaleY();
1910
1911 buffer.Printf( "%f %f scale\n", scale_x, scale_y );
1912 buffer.Replace( ",", "." );
1913 PsPrint( buffer );
1914
1915 #endif
1916
1917 // Each page starts with an "initgraphics" which resets the
1918 // transformation and so we need to rotate the page for
1919 // landscape printing)
1920
1921 // I copied this one from a PostScript tutorial, but to no avail. RR.
1922 // PsPrint( "90 rotate llx neg ury nef translate\n" );
1923
1924 if (m_printData.GetOrientation() == wxLANDSCAPE)
1925 PsPrint( "90 rotate\n" );
1926 }
1927
1928 void wxPostScriptDCImpl::EndPage ()
1929 {
1930 wxCHECK_RET( m_ok , wxT("invalid postscript dc") );
1931
1932 PsPrint( "showpage\n" );
1933 }
1934
1935 bool wxPostScriptDCImpl::DoBlit( wxCoord xdest, wxCoord ydest,
1936 wxCoord fwidth, wxCoord fheight,
1937 wxDC *source,
1938 wxCoord xsrc, wxCoord ysrc,
1939 int rop, bool WXUNUSED(useMask), wxCoord WXUNUSED(xsrcMask), wxCoord WXUNUSED(ysrcMask) )
1940 {
1941 wxCHECK_MSG( m_ok, false, wxT("invalid postscript dc") );
1942
1943 wxCHECK_MSG( source, false, wxT("invalid source dc") );
1944
1945 // blit into a bitmap
1946 wxBitmap bitmap( (int)fwidth, (int)fheight );
1947 wxMemoryDC memDC;
1948 memDC.SelectObject(bitmap);
1949 memDC.Blit(0, 0, fwidth, fheight, source, xsrc, ysrc, rop); /* TODO: Blit transparently? */
1950 memDC.SelectObject(wxNullBitmap);
1951
1952 //draw bitmap. scaling and positioning is done there
1953 GetOwner()->DrawBitmap( bitmap, xdest, ydest );
1954
1955 return true;
1956 }
1957
1958 wxCoord wxPostScriptDCImpl::GetCharHeight() const
1959 {
1960 if (m_font.Ok())
1961 return m_font.GetPointSize();
1962 else
1963 return 12;
1964 }
1965
1966 void wxPostScriptDCImpl::PsPrint( const wxString& str )
1967 {
1968 const wxCharBuffer psdata(str.utf8_str());
1969
1970 wxPostScriptPrintNativeData *data =
1971 (wxPostScriptPrintNativeData *) m_printData.GetNativeData();
1972
1973 switch (m_printData.GetPrintMode())
1974 {
1975 #if wxUSE_STREAMS
1976 // append to output stream
1977 case wxPRINT_MODE_STREAM:
1978 {
1979 wxOutputStream* outputstream = data->GetOutputStream();
1980 wxCHECK_RET( outputstream, wxT("invalid outputstream") );
1981 outputstream->Write( psdata, strlen( psdata ) );
1982 }
1983 break;
1984 #endif // wxUSE_STREAMS
1985
1986 // save data into file
1987 default:
1988 wxCHECK_RET( m_pstream, wxT("invalid postscript dc") );
1989 fwrite( psdata, 1, strlen( psdata ), m_pstream );
1990 }
1991 }
1992
1993 void wxPostScriptDCImpl::DoGetTextExtent(const wxString& string,
1994 wxCoord *x, wxCoord *y,
1995 wxCoord *descent, wxCoord *externalLeading,
1996 const wxFont *theFont ) const
1997 {
1998 const wxFont *fontToUse = theFont;
1999
2000 if (!fontToUse) fontToUse = &m_font;
2001
2002 const float fontSize =
2003 fontToUse->GetPointSize() * GetFontPointSizeAdjustment(72.0);
2004
2005 if (string.empty())
2006 {
2007 if (x) (*x) = 0;
2008 if (y) (*y) = 0;
2009 if (descent) (*descent) = 0;
2010 if (externalLeading) (*externalLeading) = 0;
2011 return;
2012 }
2013
2014 // GTK 2.0
2015
2016 const wxWX2MBbuf strbuf = string.mb_str();
2017
2018 // conversion failed (non e.g. ISO characters)
2019 if ( !strbuf )
2020 return;
2021
2022 #if !wxUSE_AFM_FOR_POSTSCRIPT
2023 /* Provide a VERY rough estimate (avoid using it).
2024 * Produces accurate results for mono-spaced font
2025 * such as Courier (aka wxMODERN) */
2026
2027 if ( x )
2028 *x = strlen (strbuf) * fontSize * 72.0 / 120.0;
2029 if ( y )
2030 *y = (wxCoord) (fontSize * 1.32); /* allow for descender */
2031 if (descent) *descent = 0;
2032 if (externalLeading) *externalLeading = 0;
2033 #else
2034
2035 /* method for calculating string widths in postscript:
2036 / read in the AFM (adobe font metrics) file for the
2037 / actual font, parse it and extract the character widths
2038 / and also the descender. this may be improved, but for now
2039 / it works well. the AFM file is only read in if the
2040 / font is changed. this may be chached in the future.
2041 / calls to GetTextExtent with the font unchanged are rather
2042 / efficient!!!
2043 /
2044 / for each font and style used there is an AFM file necessary.
2045 / currently i have only files for the roman font family.
2046 / I try to get files for the other ones!
2047 /
2048 / CAVE: the size of the string is currently always calculated
2049 / in 'points' (1/72 of an inch). this should later on be
2050 / changed to depend on the mapping mode.
2051 / CAVE: the path to the AFM files must be set before calling this
2052 / fun3B3Bction. this is usually done by a call like the following:
2053 / wxSetAFMPath("d:\\wxw161\\afm\\");
2054 /
2055 / example:
2056 /
2057 / wxPostScriptDC dc(NULL, true);
2058 / if (dc.Ok()){
2059 / wxSetAFMPath("d:\\wxw161\\afm\\");
2060 / dc.StartDoc("Test");
2061 / dc.StartPage();
2062 / wxCoord w,h;
2063 / dc.SetFont(new wxFont(10, wxROMAN, wxNORMAL, wxNORMAL));
2064 / dc.GetTextExtent("Hallo",&w,&h);
2065 / dc.EndPage();
2066 / dc.EndDoc();
2067 / }
2068 /
2069 / by steve (stefan.hammes@urz.uni-heidelberg.de)
2070 / created: 10.09.94
2071 / updated: 14.05.95 */
2072
2073 /* these static vars are for storing the state between calls */
2074 static int lastFamily= INT_MIN;
2075 static int lastSize= INT_MIN;
2076 static int lastStyle= INT_MIN;
2077 static int lastWeight= INT_MIN;
2078 static int lastDescender = INT_MIN;
2079 static int lastWidths[256]; /* widths of the characters */
2080
2081 double UnderlinePosition = 0.0;
2082 double UnderlineThickness = 0.0;
2083
2084 // Get actual parameters
2085 int Family = fontToUse->GetFamily();
2086 int Size = fontToUse->GetPointSize();
2087 int Style = fontToUse->GetStyle();
2088 int Weight = fontToUse->GetWeight();
2089
2090 // If we have another font, read the font-metrics
2091 if (Family!=lastFamily || Size!=lastSize || Style!=lastStyle || Weight!=lastWeight)
2092 {
2093 // Store actual values
2094 lastFamily = Family;
2095 lastSize = Size;
2096 lastStyle = Style;
2097 lastWeight = Weight;
2098
2099 const wxChar *name;
2100
2101 switch (Family)
2102 {
2103 case wxMODERN:
2104 case wxTELETYPE:
2105 {
2106 if ((Style == wxITALIC) && (Weight == wxBOLD)) name = wxT("CourBoO.afm");
2107 else if ((Style != wxITALIC) && (Weight == wxBOLD)) name = wxT("CourBo.afm");
2108 else if ((Style == wxITALIC) && (Weight != wxBOLD)) name = wxT("CourO.afm");
2109 else name = wxT("Cour.afm");
2110 break;
2111 }
2112 case wxROMAN:
2113 {
2114 if ((Style == wxITALIC) && (Weight == wxBOLD)) name = wxT("TimesBoO.afm");
2115 else if ((Style != wxITALIC) && (Weight == wxBOLD)) name = wxT("TimesBo.afm");
2116 else if ((Style == wxITALIC) && (Weight != wxBOLD)) name = wxT("TimesO.afm");
2117 else name = wxT("TimesRo.afm");
2118 break;
2119 }
2120 case wxSCRIPT:
2121 {
2122 name = wxT("Zapf.afm");
2123 break;
2124 }
2125 case wxSWISS:
2126 default:
2127 {
2128 if ((Style == wxITALIC) && (Weight == wxBOLD)) name = wxT("HelvBoO.afm");
2129 else if ((Style != wxITALIC) && (Weight == wxBOLD)) name = wxT("HelvBo.afm");
2130 else if ((Style == wxITALIC) && (Weight != wxBOLD)) name = wxT("HelvO.afm");
2131 else name = wxT("Helv.afm");
2132 break;
2133 }
2134 }
2135
2136 FILE *afmFile = NULL;
2137
2138 // Get the directory of the AFM files
2139 wxString afmName;
2140
2141 // VZ: I don't know if the cast always works under Unix but it clearly
2142 // never does under Windows where the pointer is
2143 // wxWindowsPrintNativeData and so calling GetFontMetricPath() on
2144 // it just crashes
2145 #ifndef __WIN32__
2146 wxPostScriptPrintNativeData *data =
2147 wxDynamicCast(m_printData.GetNativeData(), wxPostScriptPrintNativeData);
2148
2149 if (data && !data->GetFontMetricPath().empty())
2150 {
2151 afmName = data->GetFontMetricPath();
2152 afmName << wxFILE_SEP_PATH << name;
2153 }
2154 #endif // __WIN32__
2155
2156 if ( !afmName.empty() )
2157 afmFile = wxFopen(afmName, wxT("r"));
2158
2159 if ( !afmFile )
2160 {
2161 #if defined(__UNIX__) && !defined(__VMS__)
2162 afmName = wxGetDataDir();
2163 #else // !__UNIX__
2164 afmName = wxStandardPaths::Get().GetDataDir();
2165 #endif // __UNIX__/!__UNIX__
2166
2167 afmName << wxFILE_SEP_PATH
2168 #if defined(__LINUX__) || defined(__FREEBSD__)
2169 << wxT("gs_afm") << wxFILE_SEP_PATH
2170 #else
2171 << wxT("afm") << wxFILE_SEP_PATH
2172 #endif
2173 << name;
2174 afmFile = wxFopen(afmName,wxT("r"));
2175 }
2176
2177 /* 2. open and process the file
2178 / a short explanation of the AFM format:
2179 / we have for each character a line, which gives its size
2180 / e.g.:
2181 /
2182 / C 63 ; WX 444 ; N question ; B 49 -14 395 676 ;
2183 /
2184 / that means, we have a character with ascii code 63, and width
2185 / (444/1000 * fontSize) points.
2186 / the other data is ignored for now!
2187 /
2188 / when the font has changed, we read in the right AFM file and store the
2189 / character widths in an array, which is processed below (see point 3.). */
2190 if (afmFile==NULL)
2191 {
2192 wxLogDebug( wxT("GetTextExtent: can't open AFM file '%s'"), afmName.c_str() );
2193 wxLogDebug( wxT(" using approximate values"));
2194 for (int i=0; i<256; i++) lastWidths[i] = 500; /* an approximate value */
2195 lastDescender = -150; /* dito. */
2196 }
2197 else
2198 {
2199 /* init the widths array */
2200 for(int i=0; i<256; i++) lastWidths[i] = INT_MIN;
2201 /* some variables for holding parts of a line */
2202 char cString[10], semiString[10], WXString[10];
2203 char descString[20];
2204 char upString[30], utString[30];
2205 char encString[50];
2206 char line[256];
2207 int ascii,cWidth;
2208 /* read in the file and parse it */
2209 while(fgets(line,sizeof(line),afmFile)!=NULL)
2210 {
2211 /* A.) check for descender definition */
2212 if (strncmp(line,"Descender",9)==0)
2213 {
2214 if ((sscanf(line,"%s%d",descString,&lastDescender)!=2) ||
2215 (strcmp(descString,"Descender")!=0))
2216 {
2217 wxLogDebug( wxT("AFM-file '%s': line '%s' has error (bad descender)"), afmName.c_str(),line );
2218 }
2219 }
2220 /* JC 1.) check for UnderlinePosition */
2221 else if(strncmp(line,"UnderlinePosition",17)==0)
2222 {
2223 if ((sscanf(line,"%s%lf",upString,&UnderlinePosition)!=2) ||
2224 (strcmp(upString,"UnderlinePosition")!=0))
2225 {
2226 wxLogDebug( wxT("AFM-file '%s': line '%s' has error (bad UnderlinePosition)"), afmName.c_str(), line );
2227 }
2228 }
2229 /* JC 2.) check for UnderlineThickness */
2230 else if(strncmp(line,"UnderlineThickness",18)==0)
2231 {
2232 if ((sscanf(line,"%s%lf",utString,&UnderlineThickness)!=2) ||
2233 (strcmp(utString,"UnderlineThickness")!=0))
2234 {
2235 wxLogDebug( wxT("AFM-file '%s': line '%s' has error (bad UnderlineThickness)"), afmName.c_str(), line );
2236 }
2237 }
2238 /* JC 3.) check for EncodingScheme */
2239 else if(strncmp(line,"EncodingScheme",14)==0)
2240 {
2241 if ((sscanf(line,"%s%s",utString,encString)!=2) ||
2242 (strcmp(utString,"EncodingScheme")!=0))
2243 {
2244 wxLogDebug( wxT("AFM-file '%s': line '%s' has error (bad EncodingScheme)"), afmName.c_str(), line );
2245 }
2246 else if (strncmp(encString, "AdobeStandardEncoding", 21))
2247 {
2248 wxLogDebug( wxT("AFM-file '%s': line '%s' has error (unsupported EncodingScheme %s)"),
2249 afmName.c_str(),line, encString);
2250 }
2251 }
2252 /* B.) check for char-width */
2253 else if(strncmp(line,"C ",2)==0)
2254 {
2255 if (sscanf(line,"%s%d%s%s%d",cString,&ascii,semiString,WXString,&cWidth)!=5)
2256 {
2257 wxLogDebug(wxT("AFM-file '%s': line '%s' has an error (bad character width)"),afmName.c_str(),line);
2258 }
2259 if(strcmp(cString,"C")!=0 || strcmp(semiString,";")!=0 || strcmp(WXString,"WX")!=0)
2260 {
2261 wxLogDebug(wxT("AFM-file '%s': line '%s' has a format error"),afmName.c_str(),line);
2262 }
2263 /* printf(" char '%c'=%d has width '%d'\n",ascii,ascii,cWidth); */
2264 if (ascii>=0 && ascii<256)
2265 {
2266 lastWidths[ascii] = cWidth; /* store width */
2267 }
2268 else
2269 {
2270 /* MATTHEW: this happens a lot; don't print an error */
2271 /* wxLogDebug("AFM-file '%s': ASCII value %d out of range",afmName.c_str(),ascii); */
2272 }
2273 }
2274 /* C.) ignore other entries. */
2275 }
2276 fclose(afmFile);
2277 }
2278 /* hack to compute correct values for german 'Umlaute'
2279 / the correct way would be to map the character names
2280 / like 'adieresis' to corresp. positions of ISOEnc and read
2281 / these values from AFM files, too. Maybe later ... */
2282
2283 // NB: casts to int are needed to suppress gcc 3.3 warnings
2284 lastWidths[196] = lastWidths[(int)'A']; // U+00C4 A Umlaute
2285 lastWidths[228] = lastWidths[(int)'a']; // U+00E4 a Umlaute
2286 lastWidths[214] = lastWidths[(int)'O']; // U+00D6 O Umlaute
2287 lastWidths[246] = lastWidths[(int)'o']; // U+00F6 o Umlaute
2288 lastWidths[220] = lastWidths[(int)'U']; // U+00DC U Umlaute
2289 lastWidths[252] = lastWidths[(int)'u']; // U+00FC u Umlaute
2290 lastWidths[223] = lastWidths[(int)251]; // U+00DF eszett (scharfes s)
2291
2292 /* JC: calculate UnderlineThickness/UnderlinePosition */
2293
2294 // VS: dirty, but is there any better solution?
2295 double *pt;
2296 pt = (double*) &m_underlinePosition;
2297 *pt = YLOG2DEVREL((wxCoord)(UnderlinePosition * fontSize)) / 1000.0f;
2298 pt = (double*) &m_underlineThickness;
2299 *pt = YLOG2DEVREL((wxCoord)(UnderlineThickness * fontSize)) / 1000.0f;
2300
2301 }
2302
2303
2304 /* 3. now the font metrics are read in, calc size this
2305 / is done by adding the widths of the characters in the
2306 / string. they are given in 1/1000 of the size! */
2307
2308 long sum=0;
2309 float height=fontSize; /* by default */
2310 unsigned char *p;
2311 for(p=(unsigned char *)wxMBSTRINGCAST strbuf; *p; p++)
2312 {
2313 if(lastWidths[*p]== INT_MIN)
2314 {
2315 wxLogDebug(wxT("GetTextExtent: undefined width for character '%c' (%d)"), *p,*p);
2316 sum += lastWidths[(unsigned char)' ']; /* assume space */
2317 }
2318 else
2319 {
2320 sum += lastWidths[*p];
2321 }
2322 }
2323
2324 double widthSum = sum;
2325 widthSum *= fontSize;
2326 widthSum /= 1000.0F;
2327
2328 /* add descender to height (it is usually a negative value) */
2329 //if (lastDescender != INT_MIN)
2330 //{
2331 // height += (wxCoord)(((-lastDescender)/1000.0F) * Size); /* MATTHEW: forgot scale */
2332 //}
2333 // - commented by V. Slavik - height already contains descender in it
2334 // (judging from few experiments)
2335
2336 /* return size values */
2337 if ( x )
2338 *x = (wxCoord)widthSum;
2339 if ( y )
2340 *y = (wxCoord)height;
2341
2342 /* return other parameters */
2343 if (descent)
2344 {
2345 if(lastDescender!=INT_MIN)
2346 {
2347 *descent = (wxCoord)(((-lastDescender)/1000.0F) * fontSize); /* MATTHEW: forgot scale */
2348 }
2349 else
2350 {
2351 *descent = 0;
2352 }
2353 }
2354
2355 /* currently no idea how to calculate this! */
2356 if (externalLeading) *externalLeading = 0;
2357 #endif
2358 // Use AFM
2359 }
2360
2361
2362 #endif // wxUSE_PRINTING_ARCHITECTURE && wxUSE_POSTSCRIPT
2363
2364 // vi:sts=4:sw=4:et