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