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