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