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