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