/[pcsx2_0.9.7]/trunk/3rdparty/wxWidgets/src/generic/dcpsg.cpp
ViewVC logotype

Contents of /trunk/3rdparty/wxWidgets/src/generic/dcpsg.cpp

Parent Directory Parent Directory | Revision Log Revision Log


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

  ViewVC Help
Powered by ViewVC 1.1.22