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