wx/math.h integration
[wxWidgets.git] / src / generic / dcpsg.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: 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 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
13 #pragma implementation "dcpsg.h"
14 #endif
15
16 #include "wx/wxprec.h"
17
18 #ifdef __BORLANDC__
19 #pragma hdrstop
20 #endif
21
22 #ifndef WX_PRECOMP
23 #endif // WX_PRECOMP
24
25 #if wxUSE_PRINTING_ARCHITECTURE
26
27 #if wxUSE_POSTSCRIPT
28
29 #include "wx/setup.h"
30
31 #include "wx/dcmemory.h"
32 #include "wx/utils.h"
33 #include "wx/intl.h"
34 #include "wx/app.h"
35 #include "wx/image.h"
36 #include "wx/log.h"
37 #include "wx/generic/dcpsg.h"
38 #include "wx/prntbase.h"
39 #include "wx/generic/prntdlgg.h"
40 #include "wx/paper.h"
41 #include "wx/filefn.h"
42 #include "wx/math.h"
43
44 #ifdef __WXMSW__
45
46 #ifdef DrawText
47 #undef DrawText
48 #endif
49
50 #ifdef StartDoc
51 #undef StartDoc
52 #endif
53
54 #ifdef GetCharWidth
55 #undef GetCharWidth
56 #endif
57
58 #ifdef FindWindow
59 #undef FindWindow
60 #endif
61
62 #endif
63
64 //-----------------------------------------------------------------------------
65 // start and end of document/page
66 //-----------------------------------------------------------------------------
67
68 static const char *wxPostScriptHeaderConicTo = "\
69 /conicto {\n\
70 /to_y exch def\n\
71 /to_x exch def\n\
72 /conic_cntrl_y exch def\n\
73 /conic_cntrl_x exch def\n\
74 currentpoint\n\
75 /p0_y exch def\n\
76 /p0_x exch def\n\
77 /p1_x p0_x conic_cntrl_x p0_x sub 2 3 div mul add def\n\
78 /p1_y p0_y conic_cntrl_y p0_y sub 2 3 div mul add def\n\
79 /p2_x p1_x to_x p0_x sub 1 3 div mul add def\n\
80 /p2_y p1_y to_y p0_y sub 1 3 div mul add def\n\
81 p1_x p1_y p2_x p2_y to_x to_y curveto\n\
82 } bind def\n\
83 ";
84
85 static const char *wxPostScriptHeaderEllipse = "\
86 /ellipsedict 8 dict def\n\
87 ellipsedict /mtrx matrix put\n\
88 /ellipse {\n\
89 ellipsedict begin\n\
90 /endangle exch def\n\
91 /startangle exch def\n\
92 /yrad exch def\n\
93 /xrad exch def\n\
94 /y exch def\n\
95 /x exch def\n\
96 /savematrix mtrx currentmatrix def\n\
97 x y translate\n\
98 xrad yrad scale\n\
99 0 0 1 startangle endangle arc\n\
100 savematrix setmatrix\n\
101 end\n\
102 } def\n\
103 ";
104
105 static const char *wxPostScriptHeaderEllipticArc= "\
106 /ellipticarcdict 8 dict def\n\
107 ellipticarcdict /mtrx matrix put\n\
108 /ellipticarc\n\
109 { ellipticarcdict begin\n\
110 /do_fill exch def\n\
111 /endangle exch def\n\
112 /startangle exch def\n\
113 /yrad exch def\n\
114 /xrad exch def \n\
115 /y exch def\n\
116 /x exch def\n\
117 /savematrix mtrx currentmatrix def\n\
118 x y translate\n\
119 xrad yrad scale\n\
120 do_fill { 0 0 moveto } if\n\
121 0 0 1 startangle endangle arc\n\
122 savematrix setmatrix\n\
123 do_fill { fill }{ stroke } ifelse\n\
124 end\n\
125 } def\n";
126
127 static const char *wxPostScriptHeaderSpline = "\
128 /DrawSplineSection {\n\
129 /y3 exch def\n\
130 /x3 exch def\n\
131 /y2 exch def\n\
132 /x2 exch def\n\
133 /y1 exch def\n\
134 /x1 exch def\n\
135 /xa x1 x2 x1 sub 0.666667 mul add def\n\
136 /ya y1 y2 y1 sub 0.666667 mul add def\n\
137 /xb x3 x2 x3 sub 0.666667 mul add def\n\
138 /yb y3 y2 y3 sub 0.666667 mul add def\n\
139 x1 y1 lineto\n\
140 xa ya xb yb x3 y3 curveto\n\
141 } def\n\
142 ";
143
144 static const char *wxPostScriptHeaderColourImage = "\
145 % define 'colorimage' if it isn't defined\n\
146 % ('colortogray' and 'mergeprocs' come from xwd2ps\n\
147 % via xgrab)\n\
148 /colorimage where % do we know about 'colorimage'?\n\
149 { pop } % yes: pop off the 'dict' returned\n\
150 { % no: define one\n\
151 /colortogray { % define an RGB->I function\n\
152 /rgbdata exch store % call input 'rgbdata'\n\
153 rgbdata length 3 idiv\n\
154 /npixls exch store\n\
155 /rgbindx 0 store\n\
156 0 1 npixls 1 sub {\n\
157 grays exch\n\
158 rgbdata rgbindx get 20 mul % Red\n\
159 rgbdata rgbindx 1 add get 32 mul % Green\n\
160 rgbdata rgbindx 2 add get 12 mul % Blue\n\
161 add add 64 idiv % I = .5G + .31R + .18B\n\
162 put\n\
163 /rgbindx rgbindx 3 add store\n\
164 } for\n\
165 grays 0 npixls getinterval\n\
166 } bind def\n\
167 \n\
168 % Utility procedure for colorimage operator.\n\
169 % This procedure takes two procedures off the\n\
170 % stack and merges them into a single procedure.\n\
171 \n\
172 /mergeprocs { % def\n\
173 dup length\n\
174 3 -1 roll\n\
175 dup\n\
176 length\n\
177 dup\n\
178 5 1 roll\n\
179 3 -1 roll\n\
180 add\n\
181 array cvx\n\
182 dup\n\
183 3 -1 roll\n\
184 0 exch\n\
185 putinterval\n\
186 dup\n\
187 4 2 roll\n\
188 putinterval\n\
189 } bind def\n\
190 \n\
191 /colorimage { % def\n\
192 pop pop % remove 'false 3' operands\n\
193 {colortogray} mergeprocs\n\
194 image\n\
195 } bind def\n\
196 } ifelse % end of 'false' case\n\
197 ";
198
199 static char wxPostScriptHeaderReencodeISO1[] =
200 "\n/reencodeISO {\n"
201 "dup dup findfont dup length dict begin\n"
202 "{ 1 index /FID ne { def }{ pop pop } ifelse } forall\n"
203 "/Encoding ISOLatin1Encoding def\n"
204 "currentdict end definefont\n"
205 "} def\n"
206 "/ISOLatin1Encoding [\n"
207 "/.notdef/.notdef/.notdef/.notdef/.notdef/.notdef/.notdef/.notdef\n"
208 "/.notdef/.notdef/.notdef/.notdef/.notdef/.notdef/.notdef/.notdef\n"
209 "/.notdef/.notdef/.notdef/.notdef/.notdef/.notdef/.notdef/.notdef\n"
210 "/.notdef/.notdef/.notdef/.notdef/.notdef/.notdef/.notdef/.notdef\n"
211 "/space/exclam/quotedbl/numbersign/dollar/percent/ampersand/quoteright\n"
212 "/parenleft/parenright/asterisk/plus/comma/minus/period/slash\n"
213 "/zero/one/two/three/four/five/six/seven/eight/nine/colon/semicolon\n"
214 "/less/equal/greater/question/at/A/B/C/D/E/F/G/H/I/J/K/L/M/N\n"
215 "/O/P/Q/R/S/T/U/V/W/X/Y/Z/bracketleft/backslash/bracketright\n"
216 "/asciicircum/underscore/quoteleft/a/b/c/d/e/f/g/h/i/j/k/l/m\n"
217 "/n/o/p/q/r/s/t/u/v/w/x/y/z/braceleft/bar/braceright/asciitilde\n"
218 "/.notdef/.notdef/.notdef/.notdef/.notdef/.notdef/.notdef/.notdef\n"
219 "/.notdef/.notdef/.notdef/.notdef/.notdef/.notdef/.notdef/.notdef\n"
220 "/.notdef/dotlessi/grave/acute/circumflex/tilde/macron/breve\n"
221 "/dotaccent/dieresis/.notdef/ring/cedilla/.notdef/hungarumlaut\n";
222
223 static char wxPostScriptHeaderReencodeISO2[] =
224 "/ogonek/caron/space/exclamdown/cent/sterling/currency/yen/brokenbar\n"
225 "/section/dieresis/copyright/ordfeminine/guillemotleft/logicalnot\n"
226 "/hyphen/registered/macron/degree/plusminus/twosuperior/threesuperior\n"
227 "/acute/mu/paragraph/periodcentered/cedilla/onesuperior/ordmasculine\n"
228 "/guillemotright/onequarter/onehalf/threequarters/questiondown\n"
229 "/Agrave/Aacute/Acircumflex/Atilde/Adieresis/Aring/AE/Ccedilla\n"
230 "/Egrave/Eacute/Ecircumflex/Edieresis/Igrave/Iacute/Icircumflex\n"
231 "/Idieresis/Eth/Ntilde/Ograve/Oacute/Ocircumflex/Otilde/Odieresis\n"
232 "/multiply/Oslash/Ugrave/Uacute/Ucircumflex/Udieresis/Yacute\n"
233 "/Thorn/germandbls/agrave/aacute/acircumflex/atilde/adieresis\n"
234 "/aring/ae/ccedilla/egrave/eacute/ecircumflex/edieresis/igrave\n"
235 "/iacute/icircumflex/idieresis/eth/ntilde/ograve/oacute/ocircumflex\n"
236 "/otilde/odieresis/divide/oslash/ugrave/uacute/ucircumflex/udieresis\n"
237 "/yacute/thorn/ydieresis\n"
238 "] def\n\n";
239
240 //-------------------------------------------------------------------------------
241 // wxPostScriptDC
242 //-------------------------------------------------------------------------------
243
244 IMPLEMENT_DYNAMIC_CLASS(wxPostScriptDC, wxDC)
245
246 float wxPostScriptDC::ms_PSScaleFactor = 1.0;
247
248 void wxPostScriptDC::SetResolution(int ppi)
249 {
250 ms_PSScaleFactor = (float)ppi / 72.0;
251 }
252
253 int wxPostScriptDC::GetResolution()
254 {
255 return (int)(ms_PSScaleFactor * 72.0);
256 }
257
258 //-------------------------------------------------------------------------------
259
260 wxPostScriptDC::wxPostScriptDC ()
261 {
262 m_pstream = (FILE*) NULL;
263
264 m_currentRed = 0;
265 m_currentGreen = 0;
266 m_currentBlue = 0;
267
268 m_pageNumber = 0;
269
270 m_clipping = false;
271
272 m_underlinePosition = 0.0;
273 m_underlineThickness = 0.0;
274
275 m_signX = 1; // default x-axis left to right
276 m_signY = -1; // default y-axis bottom up -> top down
277 }
278
279 wxPostScriptDC::wxPostScriptDC (const wxPrintData& printData)
280 {
281 m_pstream = (FILE*) NULL;
282
283 m_currentRed = 0;
284 m_currentGreen = 0;
285 m_currentBlue = 0;
286
287 m_pageNumber = 0;
288
289 m_clipping = false;
290
291 m_underlinePosition = 0.0;
292 m_underlineThickness = 0.0;
293
294 m_signX = 1; // default x-axis left to right
295 m_signY = -1; // default y-axis bottom up -> top down
296
297 m_printData = printData;
298
299 m_ok = true;
300 }
301
302 wxPostScriptDC::~wxPostScriptDC ()
303 {
304 if (m_pstream)
305 {
306 fclose( m_pstream );
307 m_pstream = (FILE*) NULL;
308 }
309 }
310
311 bool wxPostScriptDC::Ok() const
312 {
313 return m_ok;
314 }
315
316 void wxPostScriptDC::DoSetClippingRegion (wxCoord x, wxCoord y, wxCoord w, wxCoord h)
317 {
318 wxCHECK_RET( m_ok , wxT("invalid postscript dc") );
319
320 if (m_clipping) DestroyClippingRegion();
321
322 wxDC::DoSetClippingRegion(x, y, w, h);
323
324 m_clipping = true;
325
326 PsPrintf( wxT("gsave\n newpath\n")
327 wxT("%d %d moveto\n")
328 wxT("%d %d lineto\n")
329 wxT("%d %d lineto\n")
330 wxT("%d %d lineto\n")
331 wxT("closepath clip newpath\n"),
332 LogicalToDeviceX(x), LogicalToDeviceY(y),
333 LogicalToDeviceX(x+w), LogicalToDeviceY(y),
334 LogicalToDeviceX(x+w), LogicalToDeviceY(y+h),
335 LogicalToDeviceX(x), LogicalToDeviceY(y+h) );
336 }
337
338
339 void wxPostScriptDC::DestroyClippingRegion()
340 {
341 wxCHECK_RET( m_ok , wxT("invalid postscript dc") );
342
343 if (m_clipping)
344 {
345 m_clipping = false;
346 PsPrint( "grestore\n" );
347 }
348
349 wxDC::DestroyClippingRegion();
350 }
351
352 void wxPostScriptDC::Clear()
353 {
354 // This should fail silently to avoid unnecessary
355 // asserts
356 // wxFAIL_MSG( wxT("wxPostScriptDC::Clear not implemented.") );
357 }
358
359 bool wxPostScriptDC::DoFloodFill (wxCoord WXUNUSED(x), wxCoord WXUNUSED(y), const wxColour &WXUNUSED(col), int WXUNUSED(style))
360 {
361 wxFAIL_MSG( wxT("wxPostScriptDC::FloodFill not implemented.") );
362 return false;
363 }
364
365 bool wxPostScriptDC::DoGetPixel (wxCoord WXUNUSED(x), wxCoord WXUNUSED(y), wxColour * WXUNUSED(col)) const
366 {
367 wxFAIL_MSG( wxT("wxPostScriptDC::GetPixel not implemented.") );
368 return false;
369 }
370
371 void wxPostScriptDC::DoCrossHair (wxCoord WXUNUSED(x), wxCoord WXUNUSED(y))
372 {
373 wxFAIL_MSG( wxT("wxPostScriptDC::CrossHair not implemented.") );
374 }
375
376 void wxPostScriptDC::DoDrawLine (wxCoord x1, wxCoord y1, wxCoord x2, wxCoord y2)
377 {
378 wxCHECK_RET( m_ok, wxT("invalid postscript dc") );
379
380 if (m_pen.GetStyle() == wxTRANSPARENT) return;
381
382 SetPen( m_pen );
383
384 PsPrintf( wxT("newpath\n")
385 wxT("%d %d moveto\n")
386 wxT("%d %d lineto\n")
387 wxT("stroke\n"),
388 LogicalToDeviceX(x1), LogicalToDeviceY(y1),
389 LogicalToDeviceX(x2), LogicalToDeviceY (y2) );
390
391 CalcBoundingBox( x1, y1 );
392 CalcBoundingBox( x2, y2 );
393 }
394
395 #define RAD2DEG 57.29577951308
396
397 void wxPostScriptDC::DoDrawArc (wxCoord x1, wxCoord y1, wxCoord x2, wxCoord y2, wxCoord xc, wxCoord yc)
398 {
399 wxCHECK_RET( m_ok, wxT("invalid postscript dc") );
400
401 wxCoord dx = x1 - xc;
402 wxCoord dy = y1 - yc;
403 wxCoord radius = (wxCoord) sqrt( (double)(dx*dx+dy*dy) );
404 double alpha1, alpha2;
405
406 if (x1 == x2 && y1 == y2)
407 {
408 alpha1 = 0.0;
409 alpha2 = 360.0;
410 }
411 else if (radius == 0.0)
412 {
413 alpha1 = alpha2 = 0.0;
414 }
415 else
416 {
417 alpha1 = (x1 - xc == 0) ?
418 (y1 - yc < 0) ? 90.0 : -90.0 :
419 -atan2(double(y1-yc), double(x1-xc)) * RAD2DEG;
420 alpha2 = (x2 - xc == 0) ?
421 (y2 - yc < 0) ? 90.0 : -90.0 :
422 -atan2(double(y2-yc), double(x2-xc)) * RAD2DEG;
423 }
424 while (alpha1 <= 0) alpha1 += 360;
425 while (alpha2 <= 0) alpha2 += 360; // adjust angles to be between
426 while (alpha1 > 360) alpha1 -= 360; // 0 and 360 degree
427 while (alpha2 > 360) alpha2 -= 360;
428
429 if (m_brush.GetStyle() != wxTRANSPARENT)
430 {
431 SetBrush( m_brush );
432
433 PsPrintf( wxT("newpath\n")
434 wxT("%d %d %d %d %d %d ellipse\n")
435 wxT("%d %d lineto\n")
436 wxT("closepath\n")
437 wxT("fill\n"),
438 LogicalToDeviceX(xc), LogicalToDeviceY(yc), LogicalToDeviceXRel(radius), LogicalToDeviceYRel(radius), (wxCoord)alpha1, (wxCoord) alpha2,
439 LogicalToDeviceX(xc), LogicalToDeviceY(yc) );
440
441 CalcBoundingBox( xc-radius, yc-radius );
442 CalcBoundingBox( xc+radius, yc+radius );
443 }
444
445 if (m_pen.GetStyle() != wxTRANSPARENT)
446 {
447 SetPen( m_pen );
448
449 PsPrintf( wxT("newpath\n")
450 wxT("%d %d %d %d %d %d ellipse\n")
451 wxT("%d %d lineto\n")
452 wxT("stroke\n")
453 wxT("fill\n"),
454 LogicalToDeviceX(xc), LogicalToDeviceY(yc), LogicalToDeviceXRel(radius), LogicalToDeviceYRel(radius), (wxCoord)alpha1, (wxCoord) alpha2,
455 LogicalToDeviceX(xc), LogicalToDeviceY(yc) );
456
457 CalcBoundingBox( xc-radius, yc-radius );
458 CalcBoundingBox( xc+radius, yc+radius );
459 }
460 }
461
462 void wxPostScriptDC::DoDrawEllipticArc(wxCoord x,wxCoord y,wxCoord w,wxCoord h,double sa,double ea)
463 {
464 wxCHECK_RET( m_ok, wxT("invalid postscript dc") );
465
466 if (sa>=360 || sa<=-360) sa=sa-int(sa/360)*360;
467 if (ea>=360 || ea<=-360) ea=ea-int(ea/360)*360;
468 if (sa<0) sa+=360;
469 if (ea<0) ea+=360;
470
471 if (sa==ea)
472 {
473 DrawEllipse(x,y,w,h);
474 return;
475 }
476
477 if (m_brush.GetStyle () != wxTRANSPARENT)
478 {
479 SetBrush( m_brush );
480
481 PsPrintf( wxT("newpath\n")
482 wxT("%d %d %d %d %d %d true ellipticarc\n"),
483 LogicalToDeviceX(x+w/2), LogicalToDeviceY(y+h/2),
484 LogicalToDeviceXRel(w/2), LogicalToDeviceYRel(h/2),
485 (wxCoord)sa, (wxCoord)ea );
486
487 CalcBoundingBox( x ,y );
488 CalcBoundingBox( x+w, y+h );
489 }
490
491 if (m_pen.GetStyle () != wxTRANSPARENT)
492 {
493 SetPen( m_pen );
494
495 PsPrintf( wxT("newpath\n")
496 wxT("%d %d %d %d %d %d false ellipticarc\n"),
497 LogicalToDeviceX(x+w/2), LogicalToDeviceY(y+h/2),
498 LogicalToDeviceXRel(w/2), LogicalToDeviceYRel(h/2),
499 (wxCoord)sa, (wxCoord)ea );
500
501 CalcBoundingBox( x ,y );
502 CalcBoundingBox( x+w, y+h );
503 }
504 }
505
506 void wxPostScriptDC::DoDrawPoint (wxCoord x, wxCoord y)
507 {
508 wxCHECK_RET( m_ok, wxT("invalid postscript dc") );
509
510 if (m_pen.GetStyle() == wxTRANSPARENT) return;
511
512 SetPen (m_pen);
513
514 PsPrintf( wxT("newpath\n")
515 wxT("%d %d moveto\n")
516 wxT("%d %d lineto\n")
517 wxT("stroke\n"),
518 LogicalToDeviceX(x), LogicalToDeviceY(y),
519 LogicalToDeviceX(x+1), LogicalToDeviceY(y) );
520
521 CalcBoundingBox( x, y );
522 }
523
524 void wxPostScriptDC::DoDrawPolygon (int n, wxPoint points[], wxCoord xoffset, wxCoord yoffset, int fillStyle)
525 {
526 wxCHECK_RET( m_ok, wxT("invalid postscript dc") );
527
528 if (n <= 0) return;
529
530 if (m_brush.GetStyle () != wxTRANSPARENT)
531 {
532 SetBrush( m_brush );
533
534 PsPrint( "newpath\n" );
535
536 wxCoord xx = LogicalToDeviceX(points[0].x + xoffset);
537 wxCoord yy = LogicalToDeviceY(points[0].y + yoffset);
538
539 PsPrintf( wxT("%d %d moveto\n"), xx, yy );
540
541 CalcBoundingBox( points[0].x + xoffset, points[0].y + yoffset );
542
543 for (int i = 1; i < n; i++)
544 {
545 xx = LogicalToDeviceX(points[i].x + xoffset);
546 yy = LogicalToDeviceY(points[i].y + yoffset);
547
548 PsPrintf( wxT("%d %d lineto\n"), xx, yy );
549
550 CalcBoundingBox( points[i].x + xoffset, points[i].y + yoffset);
551 }
552
553 PsPrint( (fillStyle == wxODDEVEN_RULE ? "eofill\n" : "fill\n") );
554 }
555
556 if (m_pen.GetStyle () != wxTRANSPARENT)
557 {
558 SetPen( m_pen );
559
560 PsPrint( "newpath\n" );
561
562 wxCoord xx = LogicalToDeviceX(points[0].x + xoffset);
563 wxCoord yy = LogicalToDeviceY(points[0].y + yoffset);
564
565 PsPrintf( wxT("%d %d moveto\n"), xx, yy );
566
567 CalcBoundingBox( points[0].x + xoffset, points[0].y + yoffset );
568
569 for (int i = 1; i < n; i++)
570 {
571 xx = LogicalToDeviceX(points[i].x + xoffset);
572 yy = LogicalToDeviceY(points[i].y + yoffset);
573
574 PsPrintf( wxT("%d %d lineto\n"), xx, yy );
575
576 CalcBoundingBox( points[i].x + xoffset, points[i].y + yoffset);
577 }
578
579 PsPrint( "closepath\n" );
580 PsPrint( "stroke\n" );
581 }
582 }
583
584 void wxPostScriptDC::DoDrawPolyPolygon (int n, int count[], wxPoint points[], wxCoord xoffset, wxCoord yoffset, int fillStyle)
585 {
586 wxCHECK_RET( m_ok, wxT("invalid postscript dc") );
587
588 if (n <= 0) return;
589
590 if (m_brush.GetStyle () != wxTRANSPARENT)
591 {
592 SetBrush( m_brush );
593
594 PsPrint( "newpath\n" );
595
596 int ofs = 0;
597 for (int i = 0; i < n; ofs += count[i++])
598 {
599 wxCoord xx = LogicalToDeviceX(points[ofs].x + xoffset);
600 wxCoord yy = LogicalToDeviceY(points[ofs].y + yoffset);
601
602 PsPrintf( wxT("%d %d moveto\n"), xx, yy );
603
604 CalcBoundingBox( points[ofs].x + xoffset, points[ofs].y + yoffset );
605
606 for (int j = 1; j < count[i]; j++)
607 {
608 xx = LogicalToDeviceX(points[ofs+j].x + xoffset);
609 yy = LogicalToDeviceY(points[ofs+j].y + yoffset);
610
611 PsPrintf( wxT("%d %d lineto\n"), xx, yy );
612
613 CalcBoundingBox( points[ofs+j].x + xoffset, points[ofs+j].y + yoffset);
614 }
615 }
616 PsPrint( (fillStyle == wxODDEVEN_RULE ? "eofill\n" : "fill\n") );
617 }
618
619 if (m_pen.GetStyle () != wxTRANSPARENT)
620 {
621 SetPen( m_pen );
622
623 PsPrint( "newpath\n" );
624
625 int ofs = 0;
626 for (int i = 0; i < n; ofs += count[i++])
627 {
628 wxCoord xx = LogicalToDeviceX(points[ofs].x + xoffset);
629 wxCoord yy = LogicalToDeviceY(points[ofs].y + yoffset);
630
631 PsPrintf( wxT("%d %d moveto\n"), xx, yy );
632
633 CalcBoundingBox( points[ofs].x + xoffset, points[ofs].y + yoffset );
634
635 for (int j = 1; j < count[i]; j++)
636 {
637 xx = LogicalToDeviceX(points[ofs+j].x + xoffset);
638 yy = LogicalToDeviceY(points[ofs+j].y + yoffset);
639
640 PsPrintf( wxT("%d %d lineto\n"), xx, yy );
641
642 CalcBoundingBox( points[ofs+j].x + xoffset, points[ofs+j].y + yoffset);
643 }
644 }
645 PsPrint( "closepath\n" );
646 PsPrint( "stroke\n" );
647 }
648 }
649
650 void wxPostScriptDC::DoDrawLines (int n, wxPoint points[], wxCoord xoffset, wxCoord yoffset)
651 {
652 wxCHECK_RET( m_ok, wxT("invalid postscript dc") );
653
654 if (m_pen.GetStyle() == wxTRANSPARENT) return;
655
656 if (n <= 0) return;
657
658 SetPen (m_pen);
659
660 int i;
661 for ( i =0; i<n ; i++ )
662 {
663 CalcBoundingBox( LogicalToDeviceX(points[i].x+xoffset), LogicalToDeviceY(points[i].y+yoffset));
664 }
665
666 PsPrintf( wxT("newpath\n")
667 wxT("%d %d moveto\n"),
668 LogicalToDeviceX(points[0].x+xoffset),
669 LogicalToDeviceY(points[0].y+yoffset) );
670
671 for (i = 1; i < n; i++)
672 {
673 PsPrintf( wxT("%d %d lineto\n"),
674 LogicalToDeviceX(points[i].x+xoffset),
675 LogicalToDeviceY(points[i].y+yoffset) );
676 }
677
678 PsPrint( "stroke\n" );
679 }
680
681 void wxPostScriptDC::DoDrawRectangle (wxCoord x, wxCoord y, wxCoord width, wxCoord height)
682 {
683 wxCHECK_RET( m_ok, wxT("invalid postscript dc") );
684
685 if (m_brush.GetStyle () != wxTRANSPARENT)
686 {
687 SetBrush( m_brush );
688
689 PsPrintf( wxT("newpath\n")
690 wxT("%d %d moveto\n")
691 wxT("%d %d lineto\n")
692 wxT("%d %d lineto\n")
693 wxT("%d %d lineto\n")
694 wxT("closepath\n")
695 wxT("fill\n"),
696 LogicalToDeviceX(x), LogicalToDeviceY(y),
697 LogicalToDeviceX(x + width), LogicalToDeviceY(y),
698 LogicalToDeviceX(x + width), LogicalToDeviceY(y + height),
699 LogicalToDeviceX(x), LogicalToDeviceY(y + height) );
700
701 CalcBoundingBox( x, y );
702 CalcBoundingBox( x + width, y + height );
703 }
704
705 if (m_pen.GetStyle () != wxTRANSPARENT)
706 {
707 SetPen (m_pen);
708
709 PsPrintf( wxT("newpath\n")
710 wxT("%d %d moveto\n")
711 wxT("%d %d lineto\n")
712 wxT("%d %d lineto\n")
713 wxT("%d %d lineto\n")
714 wxT("closepath\n")
715 wxT("stroke\n"),
716 LogicalToDeviceX(x), LogicalToDeviceY(y),
717 LogicalToDeviceX(x + width), LogicalToDeviceY(y),
718 LogicalToDeviceX(x + width), LogicalToDeviceY(y + height),
719 LogicalToDeviceX(x), LogicalToDeviceY(y + height) );
720
721 CalcBoundingBox( x, y );
722 CalcBoundingBox( x + width, y + height );
723 }
724 }
725
726 void wxPostScriptDC::DoDrawRoundedRectangle (wxCoord x, wxCoord y, wxCoord width, wxCoord height, double radius)
727 {
728 wxCHECK_RET( m_ok, wxT("invalid postscript dc") );
729
730 if (radius < 0.0)
731 {
732 // Now, a negative radius is interpreted to mean
733 // 'the proportion of the smallest X or Y dimension'
734 double smallest = width < height ? width : height;
735 radius = (-radius * smallest);
736 }
737
738 wxCoord rad = (wxCoord) radius;
739
740 if (m_brush.GetStyle () != wxTRANSPARENT)
741 {
742 SetBrush( m_brush );
743
744 /* Draw rectangle anticlockwise */
745 PsPrintf( wxT("newpath\n")
746 wxT("%d %d %d 90 180 arc\n")
747 wxT("%d %d moveto\n")
748 wxT("%d %d %d 180 270 arc\n")
749 wxT("%d %d lineto\n")
750 wxT("%d %d %d 270 0 arc\n")
751 wxT("%d %d lineto\n")
752 wxT("%d %d %d 0 90 arc\n")
753 wxT("%d %d lineto\n")
754 wxT("closepath\n")
755 wxT("fill\n"),
756 LogicalToDeviceX(x + rad), LogicalToDeviceY(y + rad), LogicalToDeviceXRel(rad),
757 LogicalToDeviceX(x), LogicalToDeviceY(y + rad),
758 LogicalToDeviceX(x + rad), LogicalToDeviceY(y + height - rad), LogicalToDeviceXRel(rad),
759 LogicalToDeviceX(x + width - rad), LogicalToDeviceY(y + height),
760 LogicalToDeviceX(x + width - rad), LogicalToDeviceY(y + height - rad), LogicalToDeviceXRel(rad),
761 LogicalToDeviceX(x + width), LogicalToDeviceY(y + rad),
762 LogicalToDeviceX(x + width - rad), LogicalToDeviceY(y + rad), LogicalToDeviceXRel(rad),
763 LogicalToDeviceX(x + rad), LogicalToDeviceY(y) );
764
765 CalcBoundingBox( x, y );
766 CalcBoundingBox( x + width, y + height );
767 }
768
769 if (m_pen.GetStyle () != wxTRANSPARENT)
770 {
771 SetPen (m_pen);
772
773 /* Draw rectangle anticlockwise */
774 PsPrintf( wxT("newpath\n")
775 wxT("%d %d %d 90 180 arc\n")
776 wxT("%d %d moveto\n")
777 wxT("%d %d %d 180 270 arc\n")
778 wxT("%d %d lineto\n")
779 wxT("%d %d %d 270 0 arc\n")
780 wxT("%d %d lineto\n")
781 wxT("%d %d %d 0 90 arc\n")
782 wxT("%d %d lineto\n")
783 wxT("closepath\n")
784 wxT("stroke\n"),
785 LogicalToDeviceX(x + rad), LogicalToDeviceY(y + rad), LogicalToDeviceXRel(rad),
786 LogicalToDeviceX(x), LogicalToDeviceY(y + rad),
787 LogicalToDeviceX(x + rad), LogicalToDeviceY(y + height - rad), LogicalToDeviceXRel(rad),
788 LogicalToDeviceX(x + width - rad), LogicalToDeviceY(y + height),
789 LogicalToDeviceX(x + width - rad), LogicalToDeviceY(y + height - rad), LogicalToDeviceXRel(rad),
790 LogicalToDeviceX(x + width), LogicalToDeviceY(y + rad),
791 LogicalToDeviceX(x + width - rad), LogicalToDeviceY(y + rad), LogicalToDeviceXRel(rad),
792 LogicalToDeviceX(x + rad), LogicalToDeviceY(y) );
793
794 CalcBoundingBox( x, y );
795 CalcBoundingBox( x + width, y + height );
796 }
797 }
798
799 void wxPostScriptDC::DoDrawEllipse (wxCoord x, wxCoord y, wxCoord width, wxCoord height)
800 {
801 wxCHECK_RET( m_ok, wxT("invalid postscript dc") );
802
803 if (m_brush.GetStyle () != wxTRANSPARENT)
804 {
805 SetBrush (m_brush);
806
807 PsPrintf( wxT("newpath\n")
808 wxT("%d %d %d %d 0 360 ellipse\n")
809 wxT("fill\n"),
810 LogicalToDeviceX(x + width / 2), LogicalToDeviceY(y + height / 2),
811 LogicalToDeviceXRel(width / 2), LogicalToDeviceYRel(height / 2) );
812
813 CalcBoundingBox( x - width, y - height );
814 CalcBoundingBox( x + width, y + height );
815 }
816
817 if (m_pen.GetStyle () != wxTRANSPARENT)
818 {
819 SetPen (m_pen);
820
821 PsPrintf( wxT("newpath\n")
822 wxT("%d %d %d %d 0 360 ellipse\n")
823 wxT("stroke\n"),
824 LogicalToDeviceX(x + width / 2), LogicalToDeviceY(y + height / 2),
825 LogicalToDeviceXRel(width / 2), LogicalToDeviceYRel(height / 2) );
826
827 CalcBoundingBox( x - width, y - height );
828 CalcBoundingBox( x + width, y + height );
829 }
830 }
831
832 void wxPostScriptDC::DoDrawIcon( const wxIcon& icon, wxCoord x, wxCoord y )
833 {
834 DrawBitmap( icon, x, y, true );
835 }
836
837 /* this has to be char, not wxChar */
838 static char hexArray[] = "0123456789ABCDEF";
839
840 void wxPostScriptDC::DoDrawBitmap( const wxBitmap& bitmap, wxCoord x, wxCoord y, bool WXUNUSED(useMask) )
841 {
842 wxCHECK_RET( m_ok, wxT("invalid postscript dc") );
843
844 if (!bitmap.Ok()) return;
845
846 wxImage image = bitmap.ConvertToImage();
847
848 if (!image.Ok()) return;
849
850 wxCoord w = image.GetWidth();
851 wxCoord h = image.GetHeight();
852
853 wxCoord ww = LogicalToDeviceXRel(image.GetWidth());
854 wxCoord hh = LogicalToDeviceYRel(image.GetHeight());
855
856 wxCoord xx = LogicalToDeviceX(x);
857 wxCoord yy = LogicalToDeviceY(y + bitmap.GetHeight());
858
859 PsPrintf( wxT("/origstate save def\n")
860 wxT("20 dict begin\n")
861 wxT("/pix %d string def\n")
862 wxT("/grays %d string def\n")
863 wxT("/npixels 0 def\n")
864 wxT("/rgbindx 0 def\n")
865 wxT("%d %d translate\n")
866 wxT("%d %d scale\n")
867 wxT("%d %d 8\n")
868 wxT("[%d 0 0 %d 0 %d]\n")
869 wxT("{currentfile pix readhexstring pop}\n")
870 wxT("false 3 colorimage\n"),
871 w, w, xx, yy, ww, hh, w, h, w, -h, h );
872
873 unsigned char* data = image.GetData();
874
875 /* buffer = line = width*rgb(3)*hexa(2)+'\n'(1)+null(1) */
876 char* buffer = new char[ w*6+2 ];
877 int firstDigit, secondDigit;
878
879 //rows
880 for (int j = 0; j < h; j++)
881 {
882 char* bufferindex = buffer;
883
884 //cols
885 for (int i = 0; i < w*3; i++)
886 {
887 firstDigit = (int)(*data/16.0);
888 secondDigit = (int)(*data - (firstDigit*16.0));
889 *(bufferindex++) = hexArray[firstDigit];
890 *(bufferindex++) = hexArray[secondDigit];
891
892 data++;
893 }
894 *(bufferindex++) = '\n';
895 *bufferindex = 0;
896 PsPrint( buffer );
897 }
898
899 PsPrint( "end\n" );
900 PsPrint( "origstate restore\n" );
901 }
902
903 void wxPostScriptDC::SetFont( const wxFont& font )
904 {
905 wxCHECK_RET( m_ok, wxT("invalid postscript dc") );
906
907 if (!font.Ok()) return;
908
909 m_font = font;
910
911 int Style = m_font.GetStyle();
912 int Weight = m_font.GetWeight();
913
914 const char *name;
915 switch (m_font.GetFamily())
916 {
917 case wxTELETYPE:
918 case wxMODERN:
919 {
920 if (Style == wxITALIC)
921 {
922 if (Weight == wxBOLD)
923 name = "/Courier-BoldOblique";
924 else
925 name = "/Courier-Oblique";
926 }
927 else
928 {
929 if (Weight == wxBOLD)
930 name = "/Courier-Bold";
931 else
932 name = "/Courier";
933 }
934 break;
935 }
936 case wxROMAN:
937 {
938 if (Style == wxITALIC)
939 {
940 if (Weight == wxBOLD)
941 name = "/Times-BoldItalic";
942 else
943 name = "/Times-Italic";
944 }
945 else
946 {
947 if (Weight == wxBOLD)
948 name = "/Times-Bold";
949 else
950 name = "/Times-Roman";
951 }
952 break;
953 }
954 case wxSCRIPT:
955 {
956 name = "/ZapfChancery-MediumItalic";
957 break;
958 }
959 case wxSWISS:
960 default:
961 {
962 if (Style == wxITALIC)
963 {
964 if (Weight == wxBOLD)
965 name = "/Helvetica-BoldOblique";
966 else
967 name = "/Helvetica-Oblique";
968 }
969 else
970 {
971 if (Weight == wxBOLD)
972 name = "/Helvetica-Bold";
973 else
974 name = "/Helvetica";
975 }
976 break;
977 }
978 }
979
980 // We may legitimately call SetFont before BeginDoc
981 if (!m_pstream)
982 return;
983
984 PsPrint( name );
985 PsPrint( " reencodeISO def\n" );
986 PsPrint( name );
987 PsPrint( " findfont\n" );
988
989 char buffer[100];
990 sprintf( buffer, "%f scalefont setfont\n", LogicalToDeviceYRel(m_font.GetPointSize() * 1000) / 1000.0F);
991 // this is a hack - we must scale font size (in pts) according to m_scaleY but
992 // LogicalToDeviceYRel works with wxCoord type (int or longint). Se we first convert font size
993 // to 1/1000th of pt and then back.
994 for (int i = 0; i < 100; i++)
995 if (buffer[i] == ',') buffer[i] = '.';
996 PsPrint( buffer );
997 }
998
999 void wxPostScriptDC::SetPen( const wxPen& pen )
1000 {
1001 wxCHECK_RET( m_ok, wxT("invalid postscript dc") );
1002
1003 if (!pen.Ok()) return;
1004
1005 int oldStyle = m_pen.GetStyle();
1006
1007 m_pen = pen;
1008
1009 char buffer[100];
1010 sprintf( buffer, "%f setlinewidth\n", LogicalToDeviceXRel(1000 * m_pen.GetWidth()) / 1000.0f );
1011 for (int i = 0; i < 100; i++)
1012 if (buffer[i] == ',') buffer[i] = '.';
1013 PsPrint( buffer );
1014
1015 /*
1016 Line style - WRONG: 2nd arg is OFFSET
1017
1018 Here, I'm afraid you do not conceive meaning of parameters of 'setdash'
1019 operator correctly. You should look-up this in the Red Book: the 2nd parame-
1020 ter is not number of values in the array of the first one, but an offset
1021 into this description of the pattern. I mean a real *offset* not index
1022 into array. I.e. If the command is [3 4] 1 setdash is used, then there
1023 will be first black line *2* units wxCoord, then space 4 units, then the
1024 pattern of *3* units black, 4 units space will be repeated.
1025 */
1026
1027 static const char *dotted = "[2 5] 2";
1028 static const char *short_dashed = "[4 4] 2";
1029 static const char *wxCoord_dashed = "[4 8] 2";
1030 static const char *dotted_dashed = "[6 6 2 6] 4";
1031
1032 const char *psdash;
1033
1034 switch (m_pen.GetStyle())
1035 {
1036 case wxDOT: psdash = dotted; break;
1037 case wxSHORT_DASH: psdash = short_dashed; break;
1038 case wxLONG_DASH: psdash = wxCoord_dashed; break;
1039 case wxDOT_DASH: psdash = dotted_dashed; break;
1040 case wxSOLID:
1041 case wxTRANSPARENT:
1042 default: psdash = "[] 0"; break;
1043 }
1044
1045 if ( (oldStyle != m_pen.GetStyle()) )
1046 {
1047 PsPrint( psdash );
1048 PsPrint( " setdash\n" );
1049 }
1050
1051 // Line colour
1052 unsigned char red = m_pen.GetColour().Red();
1053 unsigned char blue = m_pen.GetColour().Blue();
1054 unsigned char green = m_pen.GetColour().Green();
1055
1056 if (!m_colour)
1057 {
1058 // Anything not white is black
1059 if (! (red == (unsigned char) 255 &&
1060 blue == (unsigned char) 255 &&
1061 green == (unsigned char) 255) )
1062 {
1063 red = (unsigned char) 0;
1064 green = (unsigned char) 0;
1065 blue = (unsigned char) 0;
1066 }
1067 // setgray here ?
1068 }
1069
1070 if (!(red == m_currentRed && green == m_currentGreen && blue == m_currentBlue))
1071 {
1072 double redPS = (double)(red) / 255.0;
1073 double bluePS = (double)(blue) / 255.0;
1074 double greenPS = (double)(green) / 255.0;
1075
1076 char buffer[100];
1077 sprintf( buffer,
1078 "%.8f %.8f %.8f setrgbcolor\n",
1079 redPS, greenPS, bluePS );
1080 for (int i = 0; i < 100; i++)
1081 if (buffer[i] == ',') buffer[i] = '.';
1082
1083 PsPrint( buffer );
1084
1085 m_currentRed = red;
1086 m_currentBlue = blue;
1087 m_currentGreen = green;
1088 }
1089 }
1090
1091 void wxPostScriptDC::SetBrush( const wxBrush& brush )
1092 {
1093 wxCHECK_RET( m_ok, wxT("invalid postscript dc") );
1094
1095 if (!brush.Ok()) return;
1096
1097 m_brush = brush;
1098
1099 // Brush colour
1100 unsigned char red = m_brush.GetColour().Red();
1101 unsigned char blue = m_brush.GetColour().Blue();
1102 unsigned char green = m_brush.GetColour().Green();
1103
1104 if (!m_colour)
1105 {
1106 // Anything not white is black
1107 if (! (red == (unsigned char) 255 &&
1108 blue == (unsigned char) 255 &&
1109 green == (unsigned char) 255) )
1110 {
1111 red = (unsigned char) 0;
1112 green = (unsigned char) 0;
1113 blue = (unsigned char) 0;
1114 }
1115 // setgray here ?
1116 }
1117
1118 if (!(red == m_currentRed && green == m_currentGreen && blue == m_currentBlue))
1119 {
1120 double redPS = (double)(red) / 255.0;
1121 double bluePS = (double)(blue) / 255.0;
1122 double greenPS = (double)(green) / 255.0;
1123
1124 char buffer[100];
1125 sprintf( buffer,
1126 "%.8f %.8f %.8f setrgbcolor\n",
1127 redPS, greenPS, bluePS );
1128 for (int i = 0; i < 100; i++)
1129 if (buffer[i] == ',') buffer[i] = '.';
1130
1131 PsPrint( buffer );
1132
1133 m_currentRed = red;
1134 m_currentBlue = blue;
1135 m_currentGreen = green;
1136 }
1137 }
1138
1139 void wxPostScriptDC::DoDrawText( const wxString& text, wxCoord x, wxCoord y )
1140 {
1141 wxCHECK_RET( m_ok, wxT("invalid postscript dc") );
1142
1143 if (m_textForegroundColour.Ok())
1144 {
1145 unsigned char red = m_textForegroundColour.Red();
1146 unsigned char blue = m_textForegroundColour.Blue();
1147 unsigned char green = m_textForegroundColour.Green();
1148
1149 if (!m_colour)
1150 {
1151 // Anything not white is black
1152 if (! (red == (unsigned char) 255 &&
1153 blue == (unsigned char) 255 &&
1154 green == (unsigned char) 255))
1155 {
1156 red = (unsigned char) 0;
1157 green = (unsigned char) 0;
1158 blue = (unsigned char) 0;
1159 }
1160 }
1161
1162 // maybe setgray here ?
1163 if (!(red == m_currentRed && green == m_currentGreen && blue == m_currentBlue))
1164 {
1165 double redPS = (double)(red) / 255.0;
1166 double bluePS = (double)(blue) / 255.0;
1167 double greenPS = (double)(green) / 255.0;
1168
1169 char buffer[100];
1170 sprintf( buffer,
1171 "%.8f %.8f %.8f setrgbcolor\n",
1172 redPS, greenPS, bluePS );
1173 for (size_t i = 0; i < strlen(buffer); i++)
1174 if (buffer[i] == ',') buffer[i] = '.';
1175 PsPrint( buffer );
1176
1177 m_currentRed = red;
1178 m_currentBlue = blue;
1179 m_currentGreen = green;
1180 }
1181 }
1182
1183 wxCoord text_w, text_h, text_descent;
1184
1185 GetTextExtent(text, &text_w, &text_h, &text_descent);
1186
1187 // VZ: this seems to be unnecessary, so taking it out for now, if it
1188 // doesn't create any problems, remove this comment entirely
1189 //SetFont( m_font );
1190
1191
1192 int size = m_font.GetPointSize();
1193
1194 // wxCoord by = y + (wxCoord)floor( double(size) * 2.0 / 3.0 ); // approximate baseline
1195 // commented by V. Slavik and replaced by accurate version
1196 // - note that there is still rounding error in text_descent!
1197 wxCoord by = y + size - text_descent; // baseline
1198
1199 PsPrintf( wxT("%d %d moveto\n"), LogicalToDeviceX(x), LogicalToDeviceY(by) );
1200 PsPrint( "(" );
1201
1202 const wxWX2MBbuf textbuf = text.mb_str();
1203 size_t len = strlen(textbuf);
1204 size_t i;
1205 for (i = 0; i < len; i++)
1206 {
1207 int c = (unsigned char) textbuf[i];
1208 if (c == ')' || c == '(' || c == '\\')
1209 {
1210 /* Cope with special characters */
1211 PsPrint( "\\" );
1212 PsPrint(c);
1213 }
1214 else if ( c >= 128 )
1215 {
1216 /* Cope with character codes > 127 */
1217 PsPrintf( wxT("\\%o"), c);
1218 }
1219 else
1220 {
1221 PsPrint(c);
1222 }
1223 }
1224
1225 PsPrint( ") show\n" );
1226
1227 if (m_font.GetUnderlined())
1228 {
1229 wxCoord uy = (wxCoord)(y + size - m_underlinePosition);
1230 char buffer[100];
1231
1232 sprintf( buffer,
1233 "gsave\n"
1234 "%d %d moveto\n"
1235 "%f setlinewidth\n"
1236 "%d %d lineto\n"
1237 "stroke\n"
1238 "grestore\n",
1239 LogicalToDeviceX(x), LogicalToDeviceY(uy),
1240 m_underlineThickness,
1241 LogicalToDeviceX(x + text_w), LogicalToDeviceY(uy) );
1242 for (i = 0; i < 100; i++)
1243 if (buffer[i] == ',') buffer[i] = '.';
1244 PsPrint( buffer );
1245 }
1246
1247 CalcBoundingBox( x, y );
1248 CalcBoundingBox( x + size * text.Length() * 2/3 , y );
1249 }
1250
1251 void wxPostScriptDC::DoDrawRotatedText( const wxString& text, wxCoord x, wxCoord y, double angle )
1252 {
1253 if (angle == 0.0)
1254 {
1255 DoDrawText(text, x, y);
1256 return;
1257 }
1258
1259 wxCHECK_RET( m_ok, wxT("invalid postscript dc") );
1260
1261 SetFont( m_font );
1262
1263 if (m_textForegroundColour.Ok())
1264 {
1265 unsigned char red = m_textForegroundColour.Red();
1266 unsigned char blue = m_textForegroundColour.Blue();
1267 unsigned char green = m_textForegroundColour.Green();
1268
1269 if (!m_colour)
1270 {
1271 // Anything not white is black
1272 if (! (red == (unsigned char) 255 &&
1273 blue == (unsigned char) 255 &&
1274 green == (unsigned char) 255))
1275 {
1276 red = (unsigned char) 0;
1277 green = (unsigned char) 0;
1278 blue = (unsigned char) 0;
1279 }
1280 }
1281
1282 // maybe setgray here ?
1283 if (!(red == m_currentRed && green == m_currentGreen && blue == m_currentBlue))
1284 {
1285 double redPS = (double)(red) / 255.0;
1286 double bluePS = (double)(blue) / 255.0;
1287 double greenPS = (double)(green) / 255.0;
1288
1289 char buffer[100];
1290 sprintf( buffer,
1291 "%.8f %.8f %.8f setrgbcolor\n",
1292 redPS, greenPS, bluePS );
1293 for (int i = 0; i < 100; i++)
1294 if (buffer[i] == ',') buffer[i] = '.';
1295 PsPrint( buffer );
1296
1297 m_currentRed = red;
1298 m_currentBlue = blue;
1299 m_currentGreen = green;
1300 }
1301 }
1302
1303 int size = m_font.GetPointSize();
1304
1305 PsPrintf( wxT("%d %d moveto\n"),
1306 LogicalToDeviceX(x), LogicalToDeviceY(y));
1307
1308 char buffer[100];
1309 sprintf(buffer, "%.8f rotate\n", angle);
1310 size_t i;
1311 for (i = 0; i < 100; i++)
1312 {
1313 if (buffer[i] == ',') buffer[i] = '.';
1314 }
1315 PsPrint( buffer);
1316
1317 PsPrint( "(" );
1318 const wxWX2MBbuf textbuf = text.mb_str();
1319 size_t len = strlen(textbuf);
1320 for (i = 0; i < len; i++)
1321 {
1322 int c = (unsigned char) textbuf[i];
1323 if (c == ')' || c == '(' || c == '\\')
1324 {
1325 /* Cope with special characters */
1326 PsPrint( "\\" );
1327 PsPrint(c);
1328 }
1329 else if ( c >= 128 )
1330 {
1331 /* Cope with character codes > 127 */
1332 PsPrintf( wxT("\\%o"), c);
1333 }
1334 else
1335 {
1336 PsPrint(c);
1337 }
1338 }
1339
1340 PsPrint( ") show\n" );
1341
1342 sprintf( buffer, "%.8f rotate\n", -angle );
1343 for (i = 0; i < 100; i++)
1344 {
1345 if (buffer[i] == ',') buffer[i] = '.';
1346 }
1347 PsPrint( buffer );
1348
1349 if (m_font.GetUnderlined())
1350 {
1351 wxCoord uy = (wxCoord)(y + size - m_underlinePosition);
1352 wxCoord w, h;
1353 char buffer[100];
1354 GetTextExtent(text, &w, &h);
1355
1356 sprintf( buffer,
1357 "gsave\n"
1358 "%d %d moveto\n"
1359 "%f setlinewidth\n"
1360 "%d %d lineto\n"
1361 "stroke\n"
1362 "grestore\n",
1363 LogicalToDeviceX(x), LogicalToDeviceY(uy),
1364 m_underlineThickness,
1365 LogicalToDeviceX(x + w), LogicalToDeviceY(uy) );
1366 for (i = 0; i < 100; i++)
1367 {
1368 if (buffer[i] == ',') buffer[i] = '.';
1369 }
1370 PsPrint( buffer );
1371 }
1372
1373 CalcBoundingBox( x, y );
1374 CalcBoundingBox( x + size * text.Length() * 2/3 , y );
1375 }
1376
1377 void wxPostScriptDC::SetBackground (const wxBrush& brush)
1378 {
1379 m_backgroundBrush = brush;
1380 }
1381
1382 void wxPostScriptDC::SetLogicalFunction (int WXUNUSED(function))
1383 {
1384 wxFAIL_MSG( wxT("wxPostScriptDC::SetLogicalFunction not implemented.") );
1385 }
1386
1387 void wxPostScriptDC::DoDrawSpline( wxList *points )
1388 {
1389 wxCHECK_RET( m_ok, wxT("invalid postscript dc") );
1390
1391 SetPen( m_pen );
1392
1393 // a and b are not used
1394 //double a, b;
1395 double c, d, x1, y1, x2, y2, x3, y3;
1396 wxPoint *p, *q;
1397
1398 wxList::compatibility_iterator node = points->GetFirst();
1399 p = (wxPoint *)node->GetData();
1400 x1 = p->x;
1401 y1 = p->y;
1402
1403 node = node->GetNext();
1404 p = (wxPoint *)node->GetData();
1405 c = p->x;
1406 d = p->y;
1407 x3 =
1408 #if 0
1409 a =
1410 #endif
1411 (double)(x1 + c) / 2;
1412 y3 =
1413 #if 0
1414 b =
1415 #endif
1416 (double)(y1 + d) / 2;
1417
1418 PsPrintf( wxT("newpath\n")
1419 wxT("%d %d moveto\n")
1420 wxT("%d %d lineto\n"),
1421 LogicalToDeviceX((wxCoord)x1), LogicalToDeviceY((wxCoord)y1),
1422 LogicalToDeviceX((wxCoord)x3), LogicalToDeviceY((wxCoord)y3) );
1423
1424 CalcBoundingBox( (wxCoord)x1, (wxCoord)y1 );
1425 CalcBoundingBox( (wxCoord)x3, (wxCoord)y3 );
1426
1427 node = node->GetNext();
1428 while (node)
1429 {
1430 q = (wxPoint *)node->GetData();
1431
1432 x1 = x3;
1433 y1 = y3;
1434 x2 = c;
1435 y2 = d;
1436 c = q->x;
1437 d = q->y;
1438 x3 = (double)(x2 + c) / 2;
1439 y3 = (double)(y2 + d) / 2;
1440
1441 PsPrintf( wxT("%d %d %d %d %d %d DrawSplineSection\n"),
1442 LogicalToDeviceX((wxCoord)x1), LogicalToDeviceY((wxCoord)y1),
1443 LogicalToDeviceX((wxCoord)x2), LogicalToDeviceY((wxCoord)y2),
1444 LogicalToDeviceX((wxCoord)x3), LogicalToDeviceY((wxCoord)y3) );
1445
1446 CalcBoundingBox( (wxCoord)x1, (wxCoord)y1 );
1447 CalcBoundingBox( (wxCoord)x3, (wxCoord)y3 );
1448
1449 node = node->GetNext();
1450 }
1451
1452 /*
1453 At this point, (x2,y2) and (c,d) are the position of the
1454 next-to-last and last point respectively, in the point list
1455 */
1456
1457 PsPrintf( wxT("%d %d lineto\n")
1458 wxT("stroke\n"),
1459 LogicalToDeviceX((wxCoord)c), LogicalToDeviceY((wxCoord)d) );
1460 }
1461
1462 wxCoord wxPostScriptDC::GetCharWidth() const
1463 {
1464 // Chris Breeze: reasonable approximation using wxMODERN/Courier
1465 return (wxCoord) (GetCharHeight() * 72.0 / 120.0);
1466 }
1467
1468
1469 void wxPostScriptDC::SetAxisOrientation( bool xLeftRight, bool yBottomUp )
1470 {
1471 wxCHECK_RET( m_ok, wxT("invalid postscript dc") );
1472
1473 m_signX = (xLeftRight ? 1 : -1);
1474 m_signY = (yBottomUp ? 1 : -1);
1475
1476 // FIXME there is no such function in MSW nor in OS2/PM
1477 #if !defined(__WXMSW__) && !defined(__WXPM__)
1478 ComputeScaleAndOrigin();
1479 #endif
1480 }
1481
1482 void wxPostScriptDC::SetDeviceOrigin( wxCoord x, wxCoord y )
1483 {
1484 wxCHECK_RET( m_ok, wxT("invalid postscript dc") );
1485
1486 int h = 0;
1487 int w = 0;
1488 GetSize( &w, &h );
1489
1490 wxDC::SetDeviceOrigin( x, h-y );
1491 }
1492
1493 void wxPostScriptDC::DoGetSize(int* width, int* height) const
1494 {
1495 wxPaperSize id = m_printData.GetPaperId();
1496
1497 wxPrintPaperType *paper = wxThePrintPaperDatabase->FindPaperType(id);
1498
1499 if (!paper) paper = wxThePrintPaperDatabase->FindPaperType(wxPAPER_A4);
1500
1501 int w = 595;
1502 int h = 842;
1503 if (paper)
1504 {
1505 w = paper->GetSizeDeviceUnits().x;
1506 h = paper->GetSizeDeviceUnits().y;
1507 }
1508
1509 if (m_printData.GetOrientation() == wxLANDSCAPE)
1510 {
1511 int tmp = w;
1512 w = h;
1513 h = tmp;
1514 }
1515
1516 if (width) *width = (int)(w * ms_PSScaleFactor);
1517 if (height) *height = (int)(h * ms_PSScaleFactor);
1518 }
1519
1520 void wxPostScriptDC::DoGetSizeMM(int *width, int *height) const
1521 {
1522 wxPaperSize id = m_printData.GetPaperId();
1523
1524 wxPrintPaperType *paper = wxThePrintPaperDatabase->FindPaperType(id);
1525
1526 if (!paper) paper = wxThePrintPaperDatabase->FindPaperType(wxPAPER_A4);
1527
1528 int w = 210;
1529 int h = 297;
1530 if (paper)
1531 {
1532 w = paper->GetWidth() / 10;
1533 h = paper->GetHeight() / 10;
1534 }
1535
1536 if (m_printData.GetOrientation() == wxLANDSCAPE)
1537 {
1538 int tmp = w;
1539 w = h;
1540 h = tmp;
1541 }
1542
1543 if (width) *width = w;
1544 if (height) *height = h;
1545 }
1546
1547 // Resolution in pixels per logical inch
1548 wxSize wxPostScriptDC::GetPPI(void) const
1549 {
1550 return wxSize((int)(72 * ms_PSScaleFactor),
1551 (int)(72 * ms_PSScaleFactor));
1552 }
1553
1554
1555 bool wxPostScriptDC::StartDoc( const wxString& message )
1556 {
1557 wxCHECK_MSG( m_ok, false, wxT("invalid postscript dc") );
1558
1559 if (m_printData.GetPrintMode() != wxPRINT_MODE_STREAM )
1560 {
1561 if (m_printData.GetFilename() == wxEmptyString)
1562 {
1563 wxString filename = wxGetTempFileName( wxT("ps") );
1564 m_printData.SetFilename(filename);
1565 }
1566
1567 // FIXME: use fn_str() here under Unicode?
1568 m_pstream = wxFopen( m_printData.GetFilename().c_str(), 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 wxPostScriptPrintNativeData *data =
1970 (wxPostScriptPrintNativeData *) m_printData.GetNativeData();
1971
1972 // Get the directory of the AFM files
1973 wxString afmName;
1974 if (!data->GetFontMetricPath().empty())
1975 {
1976 afmName = data->GetFontMetricPath();
1977 afmName << wxFILE_SEP_PATH << name;
1978 afmFile = wxFopen(afmName,wxT("r"));
1979 }
1980
1981 #if defined(__UNIX__) && !defined(__VMS__)
1982 if (afmFile==NULL)
1983 {
1984 afmName = wxGetDataDir();
1985 afmName << wxFILE_SEP_PATH
1986 #if defined(__LINUX__) || defined(__FREEBSD__)
1987 << wxT("gs_afm") << wxFILE_SEP_PATH
1988 #else
1989 << wxT("afm") << wxFILE_SEP_PATH
1990 #endif
1991 << name;
1992 afmFile = wxFopen(afmName,wxT("r"));
1993 }
1994 #endif
1995
1996 /* 2. open and process the file
1997 / a short explanation of the AFM format:
1998 / we have for each character a line, which gives its size
1999 / e.g.:
2000 /
2001 / C 63 ; WX 444 ; N question ; B 49 -14 395 676 ;
2002 /
2003 / that means, we have a character with ascii code 63, and width
2004 / (444/1000 * fontSize) points.
2005 / the other data is ignored for now!
2006 /
2007 / when the font has changed, we read in the right AFM file and store the
2008 / character widths in an array, which is processed below (see point 3.). */
2009 if (afmFile==NULL)
2010 {
2011 wxLogDebug( wxT("GetTextExtent: can't open AFM file '%s'"), afmName.c_str() );
2012 wxLogDebug( wxT(" using approximate values"));
2013 for (int i=0; i<256; i++) lastWidths[i] = 500; /* an approximate value */
2014 lastDescender = -150; /* dito. */
2015 }
2016 else
2017 {
2018 /* init the widths array */
2019 for(int i=0; i<256; i++) lastWidths[i] = INT_MIN;
2020 /* some variables for holding parts of a line */
2021 char cString[10], semiString[10], WXString[10];
2022 char descString[20];
2023 char upString[30], utString[30];
2024 char encString[50];
2025 char line[256];
2026 int ascii,cWidth;
2027 /* read in the file and parse it */
2028 while(fgets(line,sizeof(line),afmFile)!=NULL)
2029 {
2030 /* A.) check for descender definition */
2031 if (strncmp(line,"Descender",9)==0)
2032 {
2033 if ((sscanf(line,"%s%d",descString,&lastDescender)!=2) ||
2034 (strcmp(descString,"Descender")!=0))
2035 {
2036 wxLogDebug( wxT("AFM-file '%s': line '%s' has error (bad descender)"), afmName.c_str(),line );
2037 }
2038 }
2039 /* JC 1.) check for UnderlinePosition */
2040 else if(strncmp(line,"UnderlinePosition",17)==0)
2041 {
2042 if ((sscanf(line,"%s%lf",upString,&UnderlinePosition)!=2) ||
2043 (strcmp(upString,"UnderlinePosition")!=0))
2044 {
2045 wxLogDebug( wxT("AFM-file '%s': line '%s' has error (bad UnderlinePosition)"), afmName.c_str(), line );
2046 }
2047 }
2048 /* JC 2.) check for UnderlineThickness */
2049 else if(strncmp(line,"UnderlineThickness",18)==0)
2050 {
2051 if ((sscanf(line,"%s%lf",utString,&UnderlineThickness)!=2) ||
2052 (strcmp(utString,"UnderlineThickness")!=0))
2053 {
2054 wxLogDebug( wxT("AFM-file '%s': line '%s' has error (bad UnderlineThickness)"), afmName.c_str(), line );
2055 }
2056 }
2057 /* JC 3.) check for EncodingScheme */
2058 else if(strncmp(line,"EncodingScheme",14)==0)
2059 {
2060 if ((sscanf(line,"%s%s",utString,encString)!=2) ||
2061 (strcmp(utString,"EncodingScheme")!=0))
2062 {
2063 wxLogDebug( wxT("AFM-file '%s': line '%s' has error (bad EncodingScheme)"), afmName.c_str(), line );
2064 }
2065 else if (strncmp(encString, "AdobeStandardEncoding", 21))
2066 {
2067 wxLogDebug( wxT("AFM-file '%s': line '%s' has error (unsupported EncodingScheme %s)"),
2068 afmName.c_str(),line, encString);
2069 }
2070 }
2071 /* B.) check for char-width */
2072 else if(strncmp(line,"C ",2)==0)
2073 {
2074 if (sscanf(line,"%s%d%s%s%d",cString,&ascii,semiString,WXString,&cWidth)!=5)
2075 {
2076 wxLogDebug(wxT("AFM-file '%s': line '%s' has an error (bad character width)"),afmName.c_str(),line);
2077 }
2078 if(strcmp(cString,"C")!=0 || strcmp(semiString,";")!=0 || strcmp(WXString,"WX")!=0)
2079 {
2080 wxLogDebug(wxT("AFM-file '%s': line '%s' has a format error"),afmName.c_str(),line);
2081 }
2082 /* printf(" char '%c'=%d has width '%d'\n",ascii,ascii,cWidth); */
2083 if (ascii>=0 && ascii<256)
2084 {
2085 lastWidths[ascii] = cWidth; /* store width */
2086 }
2087 else
2088 {
2089 /* MATTHEW: this happens a lot; don't print an error */
2090 /* wxLogDebug("AFM-file '%s': ASCII value %d out of range",afmName.c_str(),ascii); */
2091 }
2092 }
2093 /* C.) ignore other entries. */
2094 }
2095 fclose(afmFile);
2096 }
2097 /* hack to compute correct values for german 'Umlaute'
2098 / the correct way would be to map the character names
2099 / like 'adieresis' to corresp. positions of ISOEnc and read
2100 / these values from AFM files, too. Maybe later ... */
2101
2102 // NB: casts to int are needed to suppress gcc 3.3 warnings
2103 lastWidths[196] = lastWidths[(int)'A']; // Ä
2104 lastWidths[228] = lastWidths[(int)'a']; // ä
2105 lastWidths[214] = lastWidths[(int)'O']; // Ö
2106 lastWidths[246] = lastWidths[(int)'o']; // ö
2107 lastWidths[220] = lastWidths[(int)'U']; // Ü
2108 lastWidths[252] = lastWidths[(int)'u']; // ü
2109 lastWidths[223] = lastWidths[(int)251]; // ß
2110
2111 /* JC: calculate UnderlineThickness/UnderlinePosition */
2112
2113 // VS: dirty, but is there any better solution?
2114 double *pt;
2115 pt = (double*) &m_underlinePosition;
2116 *pt = LogicalToDeviceYRel((wxCoord)(UnderlinePosition * fontToUse->GetPointSize())) / 1000.0f;
2117 pt = (double*) &m_underlineThickness;
2118 *pt = LogicalToDeviceYRel((wxCoord)(UnderlineThickness * fontToUse->GetPointSize())) / 1000.0f;
2119
2120 }
2121
2122
2123 /* 3. now the font metrics are read in, calc size this
2124 / is done by adding the widths of the characters in the
2125 / string. they are given in 1/1000 of the size! */
2126
2127 long sum=0;
2128 wxCoord height=Size; /* by default */
2129 unsigned char *p;
2130 for(p=(unsigned char *)wxMBSTRINGCAST strbuf; *p; p++)
2131 {
2132 if(lastWidths[*p]== INT_MIN)
2133 {
2134 wxLogDebug(wxT("GetTextExtent: undefined width for character '%c' (%d)"), *p,*p);
2135 sum += lastWidths[(unsigned char)' ']; /* assume space */
2136 }
2137 else
2138 {
2139 sum += lastWidths[*p];
2140 }
2141 }
2142
2143 double widthSum = sum;
2144 widthSum *= Size;
2145 widthSum /= 1000.0F;
2146
2147 /* add descender to height (it is usually a negative value) */
2148 //if (lastDescender != INT_MIN)
2149 //{
2150 // height += (wxCoord)(((-lastDescender)/1000.0F) * Size); /* MATTHEW: forgot scale */
2151 //}
2152 // - commented by V. Slavik - height already contains descender in it
2153 // (judging from few experiments)
2154
2155 /* return size values */
2156 if ( x )
2157 *x = (wxCoord)widthSum;
2158 if ( y )
2159 *y = height;
2160
2161 /* return other parameters */
2162 if (descent)
2163 {
2164 if(lastDescender!=INT_MIN)
2165 {
2166 *descent = (wxCoord)(((-lastDescender)/1000.0F) * Size); /* MATTHEW: forgot scale */
2167 }
2168 else
2169 {
2170 *descent = 0;
2171 }
2172 }
2173
2174 /* currently no idea how to calculate this! */
2175 if (externalLeading) *externalLeading = 0;
2176 #endif
2177 // Use AFM
2178 }
2179
2180 // print postscript datas via required method (file, stream)
2181 void wxPostScriptDC::PsPrintf( const wxChar* fmt, ... )
2182 {
2183 va_list argptr;
2184 va_start(argptr, fmt);
2185
2186 PsPrint( wxString::FormatV( fmt, argptr ).c_str() );
2187 }
2188
2189 void wxPostScriptDC::PsPrint( const char* psdata )
2190 {
2191 wxPostScriptPrintNativeData *data =
2192 (wxPostScriptPrintNativeData *) m_printData.GetNativeData();
2193
2194 switch (m_printData.GetPrintMode())
2195 {
2196 #if wxUSE_STREAMS
2197 // append to output stream
2198 case wxPRINT_MODE_STREAM:
2199 {
2200 wxOutputStream* outputstream = data->GetOutputStream();
2201 wxCHECK_RET( outputstream, wxT("invalid outputstream") );
2202 outputstream->Write( psdata, strlen( psdata ) );
2203 }
2204 break;
2205 #endif // wxUSE_STREAMS
2206
2207 // save data into file
2208 default:
2209 wxCHECK_RET( m_pstream, wxT("invalid postscript dc") );
2210 fwrite( psdata, 1, strlen( psdata ), m_pstream );
2211 }
2212 }
2213
2214 void wxPostScriptDC::PsPrint( int ch )
2215 {
2216 wxPostScriptPrintNativeData *data =
2217 (wxPostScriptPrintNativeData *) m_printData.GetNativeData();
2218
2219 switch (m_printData.GetPrintMode())
2220 {
2221 #if wxUSE_STREAMS
2222 // append to output stream
2223 case wxPRINT_MODE_STREAM:
2224 {
2225 wxOutputStream* outputstream = data->GetOutputStream();
2226 wxCHECK_RET( outputstream, wxT("invalid outputstream") );
2227 outputstream->PutC( (char)ch );
2228 }
2229 break;
2230 #endif // wxUSE_STREAMS
2231
2232 // save data into file
2233 default:
2234 wxCHECK_RET( m_pstream, wxT("invalid postscript dc") );
2235 fputc( ch, m_pstream );
2236 }
2237 }
2238
2239 #endif
2240 // wxUSE_POSTSCRIPT
2241
2242 #endif
2243 // wxUSE_PRINTING_ARCHITECTURE
2244
2245
2246 // vi:sts=4:sw=4:et