]> git.saurik.com Git - wxWidgets.git/blob - src/gtk1/dcps.cpp
Update for bitmap, image on scaling, transparancy,
[wxWidgets.git] / src / gtk1 / dcps.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: dcps.cpp
3 // Purpose: wxPostScriptDC implementation
4 // Author: Julian Smart, Robert Roebling, Markus Holzhem
5 // Modified by:
6 // Created: 04/01/98
7 // RCS-ID: $Id$
8 // Copyright: (c) Julian Smart and Markus Holzem
9 // Licence: wxWindows license
10 /////////////////////////////////////////////////////////////////////////////
11
12 #ifdef __GNUG__
13 #pragma implementation
14 #pragma interface
15 #endif
16
17 #include "wx/postscrp.h"
18 #include "wx/dcmemory.h"
19 #include "wx/utils.h"
20 #include "wx/intl.h"
21 #include "wx/filedlg.h"
22 #include "wx/app.h"
23 #include "wx/msgdlg.h"
24
25 //-----------------------------------------------------------------------------
26 // start and end of document/page
27 //-----------------------------------------------------------------------------
28
29 static const char *wxPostScriptHeaderEllipse = "\
30 /ellipsedict 8 dict def\n\
31 ellipsedict /mtrx matrix put\n\
32 /ellipse {\n\
33 ellipsedict begin\n\
34 /endangle exch def\n\
35 /startangle exch def\n\
36 /yrad exch def\n\
37 /xrad exch def\n\
38 /y exch def\n\
39 /x exch def\n\
40 /savematrix mtrx currentmatrix def\n\
41 x y translate\n\
42 xrad yrad scale\n\
43 0 0 1 startangle endangle arc\n\
44 savematrix setmatrix\n\
45 end\n\
46 } def\n\
47 ";
48
49 static const char *wxPostScriptHeaderEllipticArc= "\
50 /ellipticarcdict 8 dict def\n\
51 ellipticarcdict /mtrx matrix put\n\
52 /ellipticarc\n\
53 { ellipticarcdict begin\n\
54 /do_fill exch def\n\
55 /endangle exch def\n\
56 /startangle exch def\n\
57 /yrad exch def\n\
58 /xrad exch def \n\
59 /y exch def\n\
60 /x exch def\n\
61 /savematrix mtrx currentmatrix def\n\
62 x y translate\n\
63 xrad yrad scale\n\
64 do_fill { 0 0 moveto } if\n\
65 0 0 1 startangle endangle arc\n\
66 savematrix setmatrix\n\
67 do_fill { fill }{ stroke } ifelse\n\
68 end\n\
69 } def\n";
70
71 static const char *wxPostScriptHeaderSpline = "\
72 /DrawSplineSection {\n\
73 /y3 exch def\n\
74 /x3 exch def\n\
75 /y2 exch def\n\
76 /x2 exch def\n\
77 /y1 exch def\n\
78 /x1 exch def\n\
79 /xa x1 x2 x1 sub 0.666667 mul add def\n\
80 /ya y1 y2 y1 sub 0.666667 mul add def\n\
81 /xb x3 x2 x3 sub 0.666667 mul add def\n\
82 /yb y3 y2 y3 sub 0.666667 mul add def\n\
83 x1 y1 lineto\n\
84 xa ya xb yb x3 y3 curveto\n\
85 } def\n\
86 ";
87
88 static const char *wxPostScriptHeaderColourImage = "\
89 % define 'colorimage' if it isn't defined\n\
90 % ('colortogray' and 'mergeprocs' come from xwd2ps\n\
91 % via xgrab)\n\
92 /colorimage where % do we know about 'colorimage'?\n\
93 { pop } % yes: pop off the 'dict' returned\n\
94 { % no: define one\n\
95 /colortogray { % define an RGB->I function\n\
96 /rgbdata exch store % call input 'rgbdata'\n\
97 rgbdata length 3 idiv\n\
98 /npixls exch store\n\
99 /rgbindx 0 store\n\
100 0 1 npixls 1 sub {\n\
101 grays exch\n\
102 rgbdata rgbindx get 20 mul % Red\n\
103 rgbdata rgbindx 1 add get 32 mul % Green\n\
104 rgbdata rgbindx 2 add get 12 mul % Blue\n\
105 add add 64 idiv % I = .5G + .31R + .18B\n\
106 put\n\
107 /rgbindx rgbindx 3 add store\n\
108 } for\n\
109 grays 0 npixls getinterval\n\
110 } bind def\n\
111 \n\
112 % Utility procedure for colorimage operator.\n\
113 % This procedure takes two procedures off the\n\
114 % stack and merges them into a single procedure.\n\
115 \n\
116 /mergeprocs { % def\n\
117 dup length\n\
118 3 -1 roll\n\
119 dup\n\
120 length\n\
121 dup\n\
122 5 1 roll\n\
123 3 -1 roll\n\
124 add\n\
125 array cvx\n\
126 dup\n\
127 3 -1 roll\n\
128 0 exch\n\
129 putinterval\n\
130 dup\n\
131 4 2 roll\n\
132 putinterval\n\
133 } bind def\n\
134 \n\
135 /colorimage { % def\n\
136 pop pop % remove 'false 3' operands\n\
137 {colortogray} mergeprocs\n\
138 image\n\
139 } bind def\n\
140 } ifelse % end of 'false' case\n\
141 ";
142
143 static char wxPostScriptHeaderReencodeISO1[] =
144 "\n/reencodeISO {\n"
145 "dup dup findfont dup length dict begin\n"
146 "{ 1 index /FID ne { def }{ pop pop } ifelse } forall\n"
147 "/Encoding ISOLatin1Encoding def\n"
148 "currentdict end definefont\n"
149 "} def\n"
150 "/ISOLatin1Encoding [\n"
151 "/.notdef/.notdef/.notdef/.notdef/.notdef/.notdef/.notdef/.notdef\n"
152 "/.notdef/.notdef/.notdef/.notdef/.notdef/.notdef/.notdef/.notdef\n"
153 "/.notdef/.notdef/.notdef/.notdef/.notdef/.notdef/.notdef/.notdef\n"
154 "/.notdef/.notdef/.notdef/.notdef/.notdef/.notdef/.notdef/.notdef\n"
155 "/space/exclam/quotedbl/numbersign/dollar/percent/ampersand/quoteright\n"
156 "/parenleft/parenright/asterisk/plus/comma/minus/period/slash\n"
157 "/zero/one/two/three/four/five/six/seven/eight/nine/colon/semicolon\n"
158 "/less/equal/greater/question/at/A/B/C/D/E/F/G/H/I/J/K/L/M/N\n"
159 "/O/P/Q/R/S/T/U/V/W/X/Y/Z/bracketleft/backslash/bracketright\n"
160 "/asciicircum/underscore/quoteleft/a/b/c/d/e/f/g/h/i/j/k/l/m\n"
161 "/n/o/p/q/r/s/t/u/v/w/x/y/z/braceleft/bar/braceright/asciitilde\n"
162 "/.notdef/.notdef/.notdef/.notdef/.notdef/.notdef/.notdef/.notdef\n"
163 "/.notdef/.notdef/.notdef/.notdef/.notdef/.notdef/.notdef/.notdef\n"
164 "/.notdef/dotlessi/grave/acute/circumflex/tilde/macron/breve\n"
165 "/dotaccent/dieresis/.notdef/ring/cedilla/.notdef/hungarumlaut\n";
166
167 static char wxPostScriptHeaderReencodeISO2[] =
168 "/ogonek/caron/space/exclamdown/cent/sterling/currency/yen/brokenbar\n"
169 "/section/dieresis/copyright/ordfeminine/guillemotleft/logicalnot\n"
170 "/hyphen/registered/macron/degree/plusminus/twosuperior/threesuperior\n"
171 "/acute/mu/paragraph/periodcentered/cedilla/onesuperior/ordmasculine\n"
172 "/guillemotright/onequarter/onehalf/threequarters/questiondown\n"
173 "/Agrave/Aacute/Acircumflex/Atilde/Adieresis/Aring/AE/Ccedilla\n"
174 "/Egrave/Eacute/Ecircumflex/Edieresis/Igrave/Iacute/Icircumflex\n"
175 "/Idieresis/Eth/Ntilde/Ograve/Oacute/Ocircumflex/Otilde/Odieresis\n"
176 "/multiply/Oslash/Ugrave/Uacute/Ucircumflex/Udieresis/Yacute\n"
177 "/Thorn/germandbls/agrave/aacute/acircumflex/atilde/adieresis\n"
178 "/aring/ae/ccedilla/egrave/eacute/ecircumflex/edieresis/igrave\n"
179 "/iacute/icircumflex/idieresis/eth/ntilde/ograve/oacute/ocircumflex\n"
180 "/otilde/odieresis/divide/oslash/ugrave/uacute/ucircumflex/udieresis\n"
181 "/yacute/thorn/ydieresis\n"
182 "] def\n\n";
183
184 //-------------------------------------------------------------------------------
185 // wxPostScriptDC
186 //-------------------------------------------------------------------------------
187
188 wxPostScriptDC::wxPostScriptDC ()
189 {
190 m_pstream = (ofstream*) NULL;
191
192 m_currentRed = 0;
193 m_currentGreen = 0;
194 m_currentBlue = 0;
195
196 m_pageNumber = 0;
197
198 m_clipping = FALSE;
199
200 m_underlinePosition = 0.0;
201 m_underlineThickness = 0.0;
202
203 m_signX = 1; // default x-axis left to right
204 m_signY = -1; // default y-axis bottom up -> top down
205 }
206
207 wxPostScriptDC::wxPostScriptDC (const wxString& file, bool interactive, wxWindow *parent)
208 {
209 m_pstream = (ofstream*) NULL;
210
211 m_currentRed = 0;
212 m_currentGreen = 0;
213 m_currentBlue = 0;
214
215 m_pageNumber = 0;
216
217 m_clipping = FALSE;
218
219 m_underlinePosition = 0.0;
220 m_underlineThickness = 0.0;
221
222 m_signX = 1; // default x-axis left to right
223 m_signY = -1; // default y-axis bottom up -> top down
224
225 Create(file, interactive, parent);
226 }
227
228 bool wxPostScriptDC::Create(const wxString& file, bool interactive, wxWindow *parent)
229 {
230 m_isInteractive = interactive;
231
232 m_title = "";
233 m_filename = file;
234
235 #ifdef __WXMSW__
236 // Can only send to file in Windows
237 wxThePrintSetupData->SetPrinterMode(PS_FILE);
238 #endif
239
240 if (m_isInteractive)
241 {
242 if ((m_ok = PrinterDialog (parent) ) == FALSE) return FALSE;
243 }
244 else
245 {
246 m_ok = TRUE;
247 }
248
249 return m_ok;
250 }
251
252 wxPostScriptDC::~wxPostScriptDC ()
253 {
254 if (m_pstream) delete m_pstream;
255 }
256
257 bool wxPostScriptDC::Ok() const
258 {
259 return (m_ok && m_pstream);
260 }
261
262 bool wxPostScriptDC::PrinterDialog(wxWindow *parent)
263 {
264 wxPostScriptPrintDialog dialog( parent, _("Printer Settings"), wxPoint(150, 150), wxSize(400, 400),
265 wxDEFAULT_DIALOG_STYLE | wxDIALOG_MODAL );
266 m_ok = (dialog.ShowModal () == wxID_OK);
267
268 if (!m_ok) return FALSE;
269
270 if ((m_filename == "") &&
271 (wxThePrintSetupData->GetPrinterMode() == PS_PREVIEW ||
272 wxThePrintSetupData->GetPrinterMode() == PS_PRINTER))
273 {
274 // steve, 05.09.94
275 #ifdef __VMS__
276 wxThePrintSetupData->SetPrinterFile("preview");
277 #else
278 // For PS_PRINTER action this depends on a Unix-style print spooler
279 // since the wx_printer_file can be destroyed during a session
280 // @@@ TODO: a Windows-style answer for non-Unix
281 char userId[256];
282 wxGetUserId (userId, sizeof (userId) / sizeof (char));
283 char tmp[256];
284 strcpy (tmp, "/tmp/preview_");
285 strcat (tmp, userId);
286 wxThePrintSetupData->SetPrinterFile(tmp);
287 #endif
288 char tmp2[256];
289 strcpy(tmp2, wxThePrintSetupData->GetPrinterFile());
290 strcat (tmp2, ".ps");
291 wxThePrintSetupData->SetPrinterFile(tmp2);
292 m_filename = tmp2;
293 }
294 else if ((m_filename == "") && (wxThePrintSetupData->GetPrinterMode() == PS_FILE))
295 {
296 char *file = wxSaveFileSelector (_("PostScript"), "ps");
297 if (!file)
298 {
299 m_ok = FALSE;
300 return FALSE;
301 }
302 wxThePrintSetupData->SetPrinterFile(file);
303 m_filename = file;
304 m_ok = TRUE;
305 }
306
307 return m_ok;
308 }
309
310 void wxPostScriptDC::SetClippingRegion (long x, long y, long w, long h)
311 {
312 wxCHECK_RET( Ok(), "invalid postscript dc" );
313
314 if (m_clipping) return;
315
316 wxDC::SetClippingRegion( x, y, w, h );
317
318 m_clipping = TRUE;
319 *m_pstream << "gsave\n"
320 << "newpath\n"
321 << XLOG2DEV(x) << " " << YLOG2DEV(y) << " moveto\n"
322 << XLOG2DEV(x+w) << " " << YLOG2DEV(y) << " lineto\n"
323 << XLOG2DEV(x+w) << " " << YLOG2DEV(y+h) << " lineto\n"
324 << XLOG2DEV(x) << " " << YLOG2DEV(y+h) << " lineto\n"
325 << "closepath clip newpath\n";
326 }
327
328 void wxPostScriptDC::SetClippingRegion( const wxRegion &WXUNUSED(region) )
329 {
330 }
331
332 void wxPostScriptDC::DestroyClippingRegion()
333 {
334 wxCHECK_RET( Ok(), "invalid postscript dc" );
335
336 wxDC::DestroyClippingRegion();
337
338 if (m_clipping)
339 {
340 m_clipping = FALSE;
341 *m_pstream << "grestore\n";
342 }
343 }
344
345 void wxPostScriptDC::Clear()
346 {
347 wxFAIL_MSG( "wxPostScriptDC::Clear not implemented." );
348 }
349
350 void wxPostScriptDC::FloodFill (long WXUNUSED(x), long WXUNUSED(y), const wxColour &WXUNUSED(col), int WXUNUSED(style))
351 {
352 wxFAIL_MSG( "wxPostScriptDC::FloodFill not implemented." );
353 }
354
355 bool wxPostScriptDC::GetPixel (long WXUNUSED(x), long WXUNUSED(y), wxColour * WXUNUSED(col)) const
356 {
357 wxFAIL_MSG( "wxPostScriptDC::GetPixel not implemented." );
358 return FALSE;
359 }
360
361 void wxPostScriptDC::CrossHair (long WXUNUSED(x), long WXUNUSED(y))
362 {
363 wxFAIL_MSG( "wxPostScriptDC::CrossHair not implemented." );
364 }
365
366 void wxPostScriptDC::DrawLine (long x1, long y1, long x2, long y2)
367 {
368 wxCHECK_RET( Ok(), "invalid postscript dc" );
369
370 if (m_pen.GetStyle() == wxTRANSPARENT) return;
371
372 SetPen( m_pen );
373
374 *m_pstream << "newpath\n"
375 << XLOG2DEV(x1) << " " << YLOG2DEV (y1) << " moveto\n"
376 << XLOG2DEV(x2) << " " << YLOG2DEV (y2) << " lineto\n"
377 << "stroke\n";
378
379 CalcBoundingBox( x1, y1 );
380 CalcBoundingBox( x2, y2 );
381 }
382
383 #define RAD2DEG 57.29577951308
384
385 void wxPostScriptDC::DrawArc (long x1, long y1, long x2, long y2, long xc, long yc)
386 {
387 wxCHECK_RET( Ok(), "invalid postscript dc" );
388
389 long dx = x1 - xc;
390 long dy = y1 - yc;
391 long radius = (long) sqrt(dx*dx+dy*dy);
392 double alpha1, alpha2;
393
394 if (x1 == x2 && y1 == y2)
395 {
396 alpha1 = 0.0;
397 alpha2 = 360.0;
398 } else if (radius == 0.0)
399 {
400 alpha1 = alpha2 = 0.0;
401 } else
402 {
403 alpha1 = (x1 - xc == 0) ?
404 (y1 - yc < 0) ? 90.0 : -90.0 :
405 -atan2(double(y1-yc), double(x1-xc)) * RAD2DEG;
406 alpha2 = (x2 - xc == 0) ?
407 (y2 - yc < 0) ? 90.0 : -90.0 :
408 -atan2(double(y2-yc), double(x2-xc)) * RAD2DEG;
409 }
410 while (alpha1 <= 0) alpha1 += 360;
411 while (alpha2 <= 0) alpha2 += 360; // adjust angles to be between
412 while (alpha1 > 360) alpha1 -= 360; // 0 and 360 degree
413 while (alpha2 > 360) alpha2 -= 360;
414
415 if (m_brush.GetStyle() != wxTRANSPARENT)
416 {
417 SetBrush( m_brush );
418 *m_pstream << "newpath\n"
419 << XLOG2DEV(xc) << " "
420 << YLOG2DEV(yc) << " "
421 << XLOG2DEVREL(radius) << " "
422 << YLOG2DEVREL(radius) << " "
423 << alpha1 << " "
424 << alpha2 << " ellipse\n"
425 << XLOG2DEV(xc) << " "
426 << YLOG2DEV(yc) << " lineto\n"
427 << "closepath\n"
428 << "fill\n";
429 }
430
431 if (m_pen.GetStyle() != wxTRANSPARENT)
432 {
433 SetPen( m_pen );
434 *m_pstream << "newpath\n"
435 << XLOG2DEV(xc) << " "
436 << YLOG2DEV(yc) << " "
437 << XLOG2DEVREL(radius) << " "
438 << YLOG2DEVREL(radius) << " "
439 << alpha1 << " "
440 << alpha2 << " ellipse\n"
441 << "stroke\n";
442 }
443
444 CalcBoundingBox( xc-radius, yc-radius );
445 CalcBoundingBox( xc+radius, yc+radius );
446 }
447
448 void wxPostScriptDC::DrawEllipticArc(long x,long y,long w,long h,double sa,double ea)
449 {
450 wxCHECK_RET( Ok(), "invalid postscript dc" );
451
452 if (sa>=360 || sa<=-360) sa=sa-int(sa/360)*360;
453 if (ea>=360 || ea<=-360) ea=ea-int(ea/360)*360;
454 if (sa<0) sa+=360;
455 if (ea<0) ea+=360;
456
457 if (sa==ea)
458 {
459 DrawEllipse(x,y,w,h);
460 return;
461 }
462
463 if (m_brush.GetStyle () != wxTRANSPARENT)
464 {
465 SetBrush( m_brush );
466
467 *m_pstream << "newpath\n"
468 << XLOG2DEV(x+w/2) << " " << YLOG2DEV(y+h/2) << " "
469 << XLOG2DEVREL(w/2) << " " << YLOG2DEVREL(h/2) << " "
470 << int(sa) <<" "<< int(ea) << " true ellipticarc\n";
471
472 CalcBoundingBox( x ,y );
473 CalcBoundingBox( x+w, y+h );
474 }
475
476 if (m_pen.GetStyle () != wxTRANSPARENT)
477 {
478 SetPen( m_pen );
479
480 *m_pstream << "newpath\n"
481 << XLOG2DEV(x+w/2) << " " << YLOG2DEV(y+h/2) << " "
482 << XLOG2DEVREL(w/2) << " " << XLOG2DEVREL(h/2) << " "
483 << int(sa) <<" "<< int(ea) << " false ellipticarc\n";
484
485 CalcBoundingBox( x, y );
486 CalcBoundingBox( x+w, y+h );
487 }
488 }
489
490 void wxPostScriptDC::DrawPoint (long x, long y)
491 {
492 wxCHECK_RET( Ok(), "invalid postscript dc" );
493
494 if (m_pen.GetStyle() == wxTRANSPARENT) return;
495
496 SetPen (m_pen);
497
498 *m_pstream << "newpath\n"
499 << XLOG2DEV(x) << " " << YLOG2DEV (y) << " moveto\n"
500 << XLOG2DEV(x+1) << " " << YLOG2DEV (y) << " lineto\n"
501 << "stroke\n";
502
503 CalcBoundingBox( x, y );
504 }
505
506 void wxPostScriptDC::DrawPolygon (int n, wxPoint points[], long xoffset, long yoffset, int WXUNUSED(fillStyle))
507 {
508 wxCHECK_RET( Ok(), "invalid postscript dc" );
509
510 if (n <= 0) return;
511
512 if (m_brush.GetStyle () != wxTRANSPARENT)
513 {
514 SetBrush( m_brush );
515
516 *m_pstream << "newpath\n";
517
518 long xx = XLOG2DEV(points[0].x + xoffset);
519 long yy = YLOG2DEV(points[0].y + yoffset);
520 *m_pstream << xx << " " << yy << " moveto\n";
521 CalcBoundingBox( points[0].x + xoffset, points[0].y + yoffset );
522
523 for (int i = 1; i < n; i++)
524 {
525 xx = XLOG2DEV(points[i].x + xoffset);
526 yy = YLOG2DEV(points[i].y + yoffset);
527 *m_pstream << xx << " " << yy << " lineto\n";
528 CalcBoundingBox( points[i].x + xoffset, points[i].y + yoffset);
529 }
530 *m_pstream << "fill\n";
531 }
532
533 if (m_pen.GetStyle () != wxTRANSPARENT)
534 {
535 SetPen( m_pen );
536
537 *m_pstream << "newpath\n";
538
539 long xx = XLOG2DEV(points[0].x + xoffset);
540 long yy = YLOG2DEV(points[0].y + yoffset);
541 *m_pstream << xx << " " << yy << " moveto\n";
542 CalcBoundingBox( points[0].x + xoffset, points[0].y + yoffset );
543
544 for (int i = 1; i < n; i++)
545 {
546 xx = XLOG2DEV(points[i].x + xoffset);
547 yy = YLOG2DEV(points[i].y + yoffset);
548 *m_pstream << xx << " " << yy << " lineto\n";
549 CalcBoundingBox( points[i].x + xoffset, points[i].y + yoffset);
550 }
551
552 *m_pstream << "stroke\n";
553 }
554 }
555
556 void wxPostScriptDC::DrawLines (int n, wxPoint points[], long xoffset, long yoffset)
557 {
558 wxCHECK_RET( Ok(), "invalid postscript dc" );
559
560 if (m_pen.GetStyle() == wxTRANSPARENT) return;
561 if (n <= 0) return;
562
563 SetPen (m_pen);
564
565 for (int i=0; i<n ; ++i)
566 {
567 CalcBoundingBox( XLOG2DEV(points[i].x+xoffset), YLOG2DEV(points[i].y+yoffset));
568 }
569
570 *m_pstream << "newpath\n"
571 << XLOG2DEV(points[0].x+xoffset) << " "
572 << YLOG2DEV(points[0].y+yoffset) << " moveto\n";
573
574 for (int i = 1; i < n; i++)
575 {
576 *m_pstream << XLOG2DEV(points[i].x+xoffset) << " "
577 << YLOG2DEV(points[i].y+yoffset) << " lineto\n";
578 }
579
580 *m_pstream << "stroke\n";
581 }
582
583 void wxPostScriptDC::DrawRectangle (long x, long y, long width, long height)
584 {
585 wxCHECK_RET( Ok(), "invalid postscript dc" );
586
587 if (m_brush.GetStyle () != wxTRANSPARENT)
588 {
589 SetBrush( m_brush );
590
591 *m_pstream << "newpath\n"
592 << XLOG2DEV(x) << " " << YLOG2DEV(y) << " moveto\n"
593 << XLOG2DEV(x + width) << " " << YLOG2DEV(y) << " lineto\n"
594 << XLOG2DEV(x + width) << " " << YLOG2DEV(y + height) << " lineto\n"
595 << XLOG2DEV(x) << " " << YLOG2DEV(y + height) << " lineto\n"
596 << "closepath\n"
597 << "fill\n";
598
599 CalcBoundingBox( x, y );
600 CalcBoundingBox( x + width, y + height );
601 }
602
603 if (m_pen.GetStyle () != wxTRANSPARENT)
604 {
605 SetPen (m_pen);
606
607 *m_pstream << "newpath\n"
608 << XLOG2DEV(x) << " " << YLOG2DEV(y) << " moveto\n"
609 << XLOG2DEV(x + width) << " " << YLOG2DEV(y) << " lineto\n"
610 << XLOG2DEV(x + width) << " " << YLOG2DEV(y + height) << " lineto\n"
611 << XLOG2DEV(x) << " " << YLOG2DEV(y + height) << " lineto\n"
612 << "closepath\n"
613 << "stroke\n";
614
615 CalcBoundingBox( x, y );
616 CalcBoundingBox( x + width, y + height );
617 }
618 }
619
620 void wxPostScriptDC::DrawRoundedRectangle (long x, long y, long width, long height, double radius)
621 {
622 wxCHECK_RET( Ok(), "invalid postscript dc" );
623
624 if (radius < 0.0)
625 {
626 // Now, a negative radius is interpreted to mean
627 // 'the proportion of the smallest X or Y dimension'
628 double smallest = 0.0;
629 if (width < height)
630 smallest = width;
631 else
632 smallest = height;
633 radius = (-radius * smallest);
634 }
635
636 long rad = (long) radius;
637
638 if (m_brush.GetStyle () != wxTRANSPARENT)
639 {
640 SetBrush( m_brush );
641
642 // Draw rectangle anticlockwise
643 *m_pstream << "newpath\n"
644 << XLOG2DEV(x + rad) << " " << YLOG2DEV(y + rad) << " " << XLOG2DEVREL(rad) << " 90 180 arc\n"
645 << XLOG2DEV(x) << " " << YLOG2DEV(y + rad) << " moveto\n"
646 << XLOG2DEV(x + rad) << " " << YLOG2DEV(y + height - rad) << " " << XLOG2DEVREL(rad) << " 180 270 arc\n"
647 << XLOG2DEV(x + width - rad) << " " << YLOG2DEV(y + height) << " lineto\n"
648 << XLOG2DEV(x + width - rad) << " " << YLOG2DEV(y + height - rad) << " " << XLOG2DEVREL(rad) << " 270 0 arc\n"
649 << XLOG2DEV(x + width) << " " << YLOG2DEV(y + rad) << " lineto\n"
650 << XLOG2DEV(x + width - rad) << " " << YLOG2DEV(y + rad) << " " << XLOG2DEVREL(rad) << " 0 90 arc\n"
651 << XLOG2DEV(x + rad) << " " << YLOG2DEV(y) << " lineto\n"
652 << "closepath\n"
653 << "fill\n";
654
655 CalcBoundingBox( x, y );
656 CalcBoundingBox( x + width, y + height );
657 }
658
659 if (m_pen.GetStyle () != wxTRANSPARENT)
660 {
661 SetPen (m_pen);
662
663 // Draw rectangle anticlockwise
664 *m_pstream << "newpath\n"
665 << XLOG2DEV(x + rad) << " " << YLOG2DEV(y + rad) << " " << XLOG2DEVREL(rad) << " 90 180 arc\n"
666 << XLOG2DEV(x) << " " << YLOG2DEV(y + rad) << " moveto\n"
667 << XLOG2DEV(x + rad) << " " << YLOG2DEV(y + height - rad) << " " << XLOG2DEVREL(rad) << " 180 270 arc\n"
668 << XLOG2DEV(x + width - rad) << " " << YLOG2DEV(y + height) << " lineto\n"
669 << XLOG2DEV(x + width - rad) << " " << YLOG2DEV(y + height - rad) << " " << XLOG2DEVREL(rad) << " 270 0 arc\n"
670 << XLOG2DEV(x + width) << " " << YLOG2DEV(y + rad) << " lineto\n"
671 << XLOG2DEV(x + width - rad) << " " << YLOG2DEV(y + rad) << " " << XLOG2DEVREL(rad) << " 0 90 arc\n"
672 << XLOG2DEV(x + rad) << " " << YLOG2DEV(y) << " lineto\n"
673 << "closepath\n"
674 << "stroke\n";
675
676 CalcBoundingBox( x, y );
677 CalcBoundingBox( x + width, y + height );
678 }
679 }
680
681 void wxPostScriptDC::DrawEllipse (long x, long y, long width, long height)
682 {
683 wxCHECK_RET( Ok(), "invalid postscript dc" );
684
685 if (m_brush.GetStyle () != wxTRANSPARENT)
686 {
687 SetBrush (m_brush);
688
689 *m_pstream << "newpath\n"
690 << XLOG2DEV(x + width / 2) << " " << YLOG2DEV(y + height / 2) << " "
691 << XLOG2DEV(width / 2) << " " << YLOG2DEVREL(height / 2) << " 0 360 ellipse\n"
692 << "fill\n";
693
694 CalcBoundingBox( x - width, y - height );
695 CalcBoundingBox( x + width, y + height );
696 }
697
698 if (m_pen.Ok() && m_pen.GetStyle () != wxTRANSPARENT)
699 {
700 SetPen (m_pen);
701
702 *m_pstream << "newpath\n"
703 << XLOG2DEV(x + width / 2) << " " << YLOG2DEV(y + height / 2) << " "
704 << XLOG2DEV(width / 2) << " " << YLOG2DEVREL(height / 2) << " 0 360 ellipse\n"
705 << "stroke\n";
706
707 CalcBoundingBox( x - width, y - height );
708 CalcBoundingBox( x + width, y + height );
709 }
710 }
711
712 void wxPostScriptDC::DrawIcon (const wxIcon& icon, long x, long y)
713 {
714 wxCHECK_RET( Ok(), "invalid postscript dc" );
715
716 wxMemoryDC memDC;
717 memDC.SelectObject( icon );
718 Blit(x, y, icon.GetWidth(), icon.GetHeight(), &memDC, 0, 0);
719 }
720
721 void wxPostScriptDC::DrawBitmap( const wxBitmap& bitmap, long x, long y, bool useMask )
722 {
723 wxCHECK_RET( Ok(), "invalid postscript dc" );
724
725 wxMemoryDC memDC;
726 memDC.SelectObject( bitmap );
727 Blit( x, y, bitmap.GetWidth(), bitmap.GetHeight(), &memDC, 0, 0, useMask );
728 }
729
730 void wxPostScriptDC::SetFont (const wxFont& font)
731 {
732 wxCHECK_RET( Ok(), "invalid postscript dc" );
733
734 if (!font.Ok()) return;
735
736 m_font = font;
737
738 char *name = wxTheFontNameDirectory->GetPostScriptName( m_font.GetFamily(),
739 m_font.GetWeight(),
740 m_font.GetStyle() );
741 if (!name) name = "Times-Roman";
742
743 *m_pstream << "/" << name << " reencodeISO def\n"
744 << "/" << name << " findfont\n"
745 << YLOG2DEVREL(font.GetPointSize())
746 << " scalefont setfont\n";
747 }
748
749 void wxPostScriptDC::SetPen( const wxPen& pen )
750 {
751 wxCHECK_RET( Ok(), "invalid postscript dc" );
752
753 if (!pen.Ok()) return;
754
755 int oldStyle = m_pen.GetStyle();
756
757 m_pen = pen;
758
759 *m_pstream << XLOG2DEVREL(m_pen.GetWidth()) << " setlinewidth\n";
760
761 /*
762 Line style - WRONG: 2nd arg is OFFSET
763
764 Here, I'm afraid you do not conceive meaning of parameters of 'setdash'
765 operator correctly. You should look-up this in the Red Book: the 2nd parame-
766 ter is not number of values in the array of the first one, but an offset
767 into this description of the pattern. I mean a real *offset* not index
768 into array. I.e. If the command is [3 4] 1 setdash is used, then there
769 will be first black line *2* units long, then space 4 units, then the
770 pattern of *3* units black, 4 units space will be repeated.
771 */
772
773 static const char *dotted = "[2 5] 2";
774 static const char *short_dashed = "[4 4] 2";
775 static const char *long_dashed = "[4 8] 2";
776 static const char *dotted_dashed = "[6 6 2 6] 4";
777
778 const char *psdash = (char *) NULL;
779 switch (m_pen.GetStyle ())
780 {
781 case wxDOT: psdash = dotted; break;
782 case wxSHORT_DASH: psdash = short_dashed; break;
783 case wxLONG_DASH: psdash = long_dashed; break;
784 case wxDOT_DASH: psdash = dotted_dashed; break;
785 case wxSOLID:
786 case wxTRANSPARENT:
787 default: psdash = "[] 0"; break;
788 }
789
790 if (oldStyle != m_pen.GetStyle())
791 {
792 *m_pstream << psdash << " setdash\n";
793 }
794
795 // Line colour
796 unsigned char red = m_pen.GetColour().Red();
797 unsigned char blue = m_pen.GetColour().Blue();
798 unsigned char green = m_pen.GetColour().Green();
799
800 if (!m_colour)
801 {
802 // Anything not white is black
803 if (!(red == (unsigned char) 255 && blue == (unsigned char) 255
804 && green == (unsigned char) 255))
805 {
806 red = (unsigned char) 0;
807 green = (unsigned char) 0;
808 blue = (unsigned char) 0;
809 }
810
811 // setgray here ?
812 }
813
814 if (!(red == m_currentRed && green == m_currentGreen && blue == m_currentBlue))
815 {
816 long redPS = (long) (((int) red) / 255.0);
817 long bluePS = (long) (((int) blue) / 255.0);
818 long greenPS = (long) (((int) green) / 255.0);
819
820 *m_pstream << redPS << " " << greenPS << " " << bluePS << " setrgbcolor\n";
821
822 m_currentRed = red;
823 m_currentBlue = blue;
824 m_currentGreen = green;
825 }
826 }
827
828 void wxPostScriptDC::SetBrush( const wxBrush& brush )
829 {
830 wxCHECK_RET( Ok(), "invalid postscript dc" );
831
832 if (!brush.Ok()) return;
833
834 m_brush = brush;
835
836 // Brush colour
837 unsigned char red = m_brush.GetColour ().Red ();
838 unsigned char blue = m_brush.GetColour ().Blue ();
839 unsigned char green = m_brush.GetColour ().Green ();
840
841 if (!m_colour)
842 {
843 // Anything not black is white
844 if (!(red == (unsigned char) 0 && blue == (unsigned char) 0
845 && green == (unsigned char) 0))
846 {
847 red = (unsigned char) 255;
848 green = (unsigned char) 255;
849 blue = (unsigned char) 255;
850 }
851
852 // setgray here ?
853 }
854
855 if (!(red == m_currentRed && green == m_currentGreen && blue == m_currentBlue))
856 {
857 long redPS = (long) (((int) red) / 255.0);
858 long bluePS = (long) (((int) blue) / 255.0);
859 long greenPS = (long) (((int) green) / 255.0);
860 *m_pstream << redPS << " " << greenPS << " " << bluePS << " setrgbcolor\n";
861 m_currentRed = red;
862 m_currentBlue = blue;
863 m_currentGreen = green;
864 }
865 }
866
867 void wxPostScriptDC::DrawText( const wxString& text, long x, long y, bool WXUNUSED(use16bit) )
868 {
869 wxCHECK_RET( Ok(), "invalid postscript dc" );
870
871 SetFont( m_font );
872
873 if (m_textForegroundColour.Ok ())
874 {
875 unsigned char red = m_textForegroundColour.Red ();
876 unsigned char blue = m_textForegroundColour.Blue ();
877 unsigned char green = m_textForegroundColour.Green ();
878
879 if (!m_colour)
880 {
881 // Anything not white is black
882 if (!(red == (unsigned char) 255 && blue == (unsigned char) 255
883 && green == (unsigned char) 255))
884 {
885 red = (unsigned char) 0;
886 green = (unsigned char) 0;
887 blue = (unsigned char) 0;
888 }
889 }
890
891 // maybe setgray here ?
892
893 if (!(red == m_currentRed && green == m_currentGreen && blue == m_currentBlue))
894 {
895 long redPS = (long) (((int) red) / 255.0);
896 long bluePS = (long) (((int) blue) / 255.0);
897 long greenPS = (long) (((int) green) / 255.0);
898 *m_pstream << redPS << " " << greenPS << " " << bluePS << " setrgbcolor\n";
899
900 m_currentRed = red;
901 m_currentBlue = blue;
902 m_currentGreen = green;
903 }
904 }
905
906 int size = m_font.GetPointSize();
907
908 long by = y + (long)floor( float(size) * 2.0 / 3.0 ); // approximate baseline
909 *m_pstream << XLOG2DEV(x) << " " << YLOG2DEV(by) << " moveto\n";
910
911 *m_pstream << "(";
912 int len = strlen ((char *)(const char *)text);
913 int i;
914 for (i = 0; i < len; i++)
915 {
916 int c = (unsigned char) text[i];
917 if ( c == ')' || c == '(' || c == '\\')
918 {
919 *m_pstream << "\\" << (char) c;
920 }
921 else if ( c >= 128 )
922 {
923 // Cope with character codes > 127
924 char tmp[5];
925 sprintf(tmp, "\\%o", c);
926 *m_pstream << tmp;
927 }
928 else
929 *m_pstream << (char) c;
930 }
931
932 *m_pstream << ")" << " show\n";
933
934 if (m_font.GetUnderlined())
935 {
936 long uy = (long)(y + size - m_underlinePosition);
937 long w, h;
938 GetTextExtent(text, &w, &h);
939
940 *m_pstream << "gsave " << XLOG2DEV(x) << " " << YLOG2DEV(uy)
941 << " moveto\n"
942 << (long)m_underlineThickness << " setlinewidth "
943 << XLOG2DEV(x + w) << " " << YLOG2DEV(uy)
944 << " lineto stroke grestore\n";
945 }
946
947 CalcBoundingBox( x, y );
948 CalcBoundingBox( x + size * text.Length() * 2/3 , y );
949 }
950
951
952 void wxPostScriptDC::SetBackground (const wxBrush& brush)
953 {
954 m_backgroundBrush = brush;
955 }
956
957 void wxPostScriptDC::SetLogicalFunction (int WXUNUSED(function))
958 {
959 wxFAIL_MSG( "wxPostScriptDC::SetLogicalFunction not implemented." );
960 }
961
962 void wxPostScriptDC::DrawSpline( wxList *points )
963 {
964 wxCHECK_RET( Ok(), "invalid postscript dc" );
965
966 SetPen( m_pen );
967
968 double a, b, c, d, x1, y1, x2, y2, x3, y3;
969 wxPoint *p, *q;
970
971 wxNode *node = points->First();
972 p = (wxPoint *)node->Data();
973 x1 = p->x;
974 y1 = p->y;
975
976 node = node->Next();
977 p = (wxPoint *)node->Data();
978 c = p->x;
979 d = p->y;
980 x3 = a = (double)(x1 + c) / 2;
981 y3 = b = (double)(y1 + d) / 2;
982
983 *m_pstream << "newpath "
984 << XLOG2DEV((long)x1) << " " << YLOG2DEV((long)y1) << " moveto "
985 << XLOG2DEV((long)x3) << " " << YLOG2DEV((long)y3) << " lineto\n";
986
987 CalcBoundingBox( (long)x1, (long)y1 );
988 CalcBoundingBox( (long)x3, (long)y3 );
989
990 while ((node = node->Next()) != NULL)
991 {
992 q = (wxPoint *)node->Data();
993
994 x1 = x3;
995 y1 = y3;
996 x2 = c;
997 y2 = d;
998 c = q->x;
999 d = q->y;
1000 x3 = (double)(x2 + c) / 2;
1001 y3 = (double)(y2 + d) / 2;
1002 *m_pstream << XLOG2DEV((long)x1) << " " << YLOG2DEV((long)y1) << " "
1003 << XLOG2DEV((long)x2) << " " << YLOG2DEV((long)y2) << " "
1004 << XLOG2DEV((long)x3) << " " << YLOG2DEV((long)y3) << " DrawSplineSection\n";
1005
1006 CalcBoundingBox( (long)x1, (long)y1 );
1007 CalcBoundingBox( (long)x3, (long)y3 );
1008 }
1009
1010 /*
1011 At this point, (x2,y2) and (c,d) are the position of the
1012 next-to-last and last point respectively, in the point list
1013 */
1014
1015 *m_pstream << XLOG2DEV((long)c) << " " << YLOG2DEV((long)d) << " lineto stroke\n";
1016 }
1017
1018 long wxPostScriptDC::GetCharWidth ()
1019 {
1020 // Chris Breeze: reasonable approximation using wxMODERN/Courier
1021 return (long) (GetCharHeight() * 72.0 / 120.0);
1022 }
1023
1024
1025 void wxPostScriptDC::SetAxisOrientation( bool xLeftRight, bool yBottomUp )
1026 {
1027 wxCHECK_RET( Ok(), "invalid postscript dc" );
1028
1029 m_signX = (xLeftRight ? 1 : -1);
1030 m_signY = (yBottomUp ? 1 : -1);
1031
1032 ComputeScaleAndOrigin();
1033 }
1034
1035 void wxPostScriptDC::SetDeviceOrigin( long x, long y )
1036 {
1037 wxCHECK_RET( Ok(), "invalid postscript dc" );
1038
1039 int h = 0;
1040 int w = 0;
1041 GetSize( &w, &h );
1042
1043 wxDC::SetDeviceOrigin( x, h-y );
1044 }
1045
1046 void wxPostScriptDC::GetSize(int* width, int* height) const
1047 {
1048 wxCHECK_RET( Ok(), "invalid postscript dc" );
1049
1050 const char *paperType = wxThePrintSetupData->GetPaperName();
1051
1052 if (!paperType) paperType = _("A4 210 x 297 mm");
1053
1054 wxPrintPaperType *paper = wxThePrintPaperDatabase->FindPaperType(paperType);
1055
1056 if (!paper) paper = wxThePrintPaperDatabase->FindPaperType(_("A4 210 x 297 mm"));
1057
1058 if (paper)
1059 {
1060 if (width) *width = paper->widthPixels;
1061 if (height) *height = paper->heightPixels;
1062 }
1063 else
1064 {
1065 if (width) *width = 595;
1066 if (height) *height = 842;
1067 }
1068 }
1069
1070 bool wxPostScriptDC::StartDoc (const wxString& message)
1071 {
1072 wxCHECK_MSG( Ok(), FALSE, "invalid postscript dc" );
1073
1074 if (m_filename == "")
1075 {
1076 m_filename = wxGetTempFileName("ps");
1077 wxThePrintSetupData->SetPrinterFile((char *)(const char *)m_filename);
1078 m_ok = TRUE;
1079 }
1080 else
1081 {
1082 wxThePrintSetupData->SetPrinterFile((char *)(const char *)m_filename);
1083 }
1084
1085 m_pstream = new ofstream (wxThePrintSetupData->GetPrinterFile());
1086
1087 if (!m_pstream || !m_pstream->good())
1088 {
1089 wxMessageBox (_("Cannot open file!"), _("Error"), wxOK);
1090 m_ok = FALSE;
1091 return FALSE;
1092 }
1093
1094 m_ok = TRUE;
1095
1096 SetBrush( *wxBLACK_BRUSH );
1097 SetPen( *wxBLACK_PEN );
1098 SetBackground( *wxWHITE_BRUSH );
1099 SetTextForeground( *wxBLACK );
1100
1101 // set origin according to paper size
1102 SetDeviceOrigin( 0,0 );
1103
1104 wxPageNumber = 1;
1105 m_pageNumber = 1;
1106 m_title = message;
1107 return TRUE;
1108 }
1109
1110 void wxPostScriptDC::EndDoc ()
1111 {
1112 wxCHECK_RET( Ok(), "invalid postscript dc" );
1113
1114 if (m_clipping)
1115 {
1116 m_clipping = FALSE;
1117 *m_pstream << "grestore\n";
1118 }
1119
1120 if (m_pstream)
1121 {
1122 delete m_pstream;
1123 m_pstream = (ofstream *) NULL;
1124 }
1125
1126 char *header_file = wxGetTempFileName("ps");
1127
1128 m_pstream = new ofstream( header_file );
1129
1130 *m_pstream << "%!PS-Adobe-2.0\n"; /* PostScript magic strings */
1131 *m_pstream << "%%Title: " << (const char *) m_title << "\n";
1132 *m_pstream << "%%Creator: " << wxTheApp->argv[0] << "\n";
1133 *m_pstream << "%%CreationDate: " << wxNow() << "\n";
1134
1135 char userID[256];
1136 if ( wxGetEmailAddress(userID, sizeof(userID)) )
1137 {
1138 *m_pstream << "%%For: " << (char *)userID;
1139 char userName[245];
1140 if (wxGetUserName(userName, sizeof(userName)))
1141 *m_pstream << " (" << (char *)userName << ")";
1142 *m_pstream << "\n";
1143 }
1144 else if ( wxGetUserName(userID, sizeof(userID)) )
1145 {
1146 *m_pstream << "%%For: " << (char *)userID << "\n";
1147 }
1148
1149 // THE FOLLOWING HAS BEEN CONTRIBUTED BY Andy Fyfe <andy@hyperparallel.com>
1150
1151 long wx_printer_translate_x, wx_printer_translate_y;
1152 double wx_printer_scale_x, wx_printer_scale_y;
1153 wxThePrintSetupData->GetPrinterTranslation(&wx_printer_translate_x, &wx_printer_translate_y);
1154 wxThePrintSetupData->GetPrinterScaling(&wx_printer_scale_x, &wx_printer_scale_y);
1155
1156 if (wxThePrintSetupData->GetPrinterOrientation() == PS_LANDSCAPE)
1157 {
1158 *m_pstream << "%%Orientation: Landscape\n";
1159 }
1160 else
1161 {
1162 *m_pstream << "%%Orientation: Portrait\n";
1163 }
1164
1165 // Compute the bounding box. Note that it is in the default user
1166 // coordinate system, thus we have to convert the values.
1167 long llx = (long) ((XLOG2DEV(m_minX)+wx_printer_translate_x)*wx_printer_scale_x);
1168 long lly = (long) ((YLOG2DEV(m_minY)+wx_printer_translate_y)*wx_printer_scale_y);
1169 long urx = (long) ((XLOG2DEV(m_maxX)+wx_printer_translate_x)*wx_printer_scale_x);
1170 long ury = (long) ((YLOG2DEV(m_maxY)+wx_printer_translate_y)*wx_printer_scale_y);
1171
1172 // If we're landscape, our sense of "x" and "y" is reversed.
1173 if (wxThePrintSetupData->GetPrinterOrientation() == PS_LANDSCAPE)
1174 {
1175 long tmp;
1176 tmp = llx; llx = lly; lly = tmp;
1177 tmp = urx; urx = ury; ury = tmp;
1178
1179 // We need either the two lines that follow, or we need to subtract
1180 // min_x from real_translate_y, which is commented out below.
1181 llx = llx - (long)(m_minX*wx_printer_scale_y);
1182 urx = urx - (long)(m_minX*wx_printer_scale_y);
1183 }
1184
1185 // The Adobe specifications call for integers; we round as to make
1186 // the bounding larger.
1187 *m_pstream << "%%BoundingBox: "
1188 << floor((double)llx) << " " << floor((double)lly) << " "
1189 << ceil((double)urx) << " " << ceil((double)ury) << "\n";
1190 *m_pstream << "%%Pages: " << (wxPageNumber - 1) << "\n";
1191 *m_pstream << "%%EndComments\n\n";
1192
1193 // To check the correctness of the bounding box, postscript commands
1194 // to draw a box corresponding to the bounding box are generated below.
1195 // But since we typically don't want to print such a box, the postscript
1196 // commands are generated within comments. These lines appear before any
1197 // adjustment of scale, rotation, or translation, and hence are in the
1198 // default user coordinates.
1199 *m_pstream << "% newpath\n";
1200 *m_pstream << "% " << llx << " " << lly << " moveto\n";
1201 *m_pstream << "% " << urx << " " << lly << " lineto\n";
1202 *m_pstream << "% " << urx << " " << ury << " lineto\n";
1203 *m_pstream << "% " << llx << " " << ury << " lineto closepath stroke\n";
1204
1205 *m_pstream << "%%BeginProlog\n";
1206 *m_pstream << wxPostScriptHeaderEllipse;
1207 *m_pstream << wxPostScriptHeaderEllipticArc;
1208 *m_pstream << wxPostScriptHeaderReencodeISO1;
1209 *m_pstream << wxPostScriptHeaderReencodeISO2;
1210
1211 if (wxPostScriptHeaderSpline)
1212 *m_pstream << wxPostScriptHeaderSpline;
1213 *m_pstream << "%%EndProlog\n";
1214
1215 delete m_pstream;
1216 m_pstream = (ofstream *) NULL;
1217
1218 char *tmp_file = wxGetTempFileName("ps");
1219
1220 // Paste header Before wx_printer_file
1221 wxConcatFiles (header_file, wxThePrintSetupData->GetPrinterFile(), tmp_file);
1222 wxRemoveFile (header_file);
1223 wxRemoveFile (wxThePrintSetupData->GetPrinterFile());
1224 wxRenameFile(tmp_file, wxThePrintSetupData->GetPrinterFile());
1225
1226 #if defined(__X__) || defined(__WXGTK__)
1227 if (m_ok)
1228 {
1229 switch (wxThePrintSetupData->GetPrinterMode()) {
1230 case PS_PREVIEW:
1231 {
1232 char *argv[3];
1233 argv[0] = wxThePrintSetupData->GetPrintPreviewCommand();
1234 argv[1] = wxThePrintSetupData->GetPrinterFile();
1235 argv[2] = (char *) NULL;
1236 wxExecute (argv, TRUE);
1237 wxRemoveFile(wxThePrintSetupData->GetPrinterFile());
1238 }
1239 break;
1240
1241 case PS_PRINTER:
1242 {
1243 char *argv[4];
1244 int argc = 0;
1245 argv[argc++] = wxThePrintSetupData->GetPrinterCommand();
1246
1247 // !SM! If we simply assign to argv[1] here, if printer options
1248 // are blank, we get an annoying and confusing message from lpr.
1249 char * opts = wxThePrintSetupData->GetPrinterOptions();
1250 if (opts && *opts)
1251 argv[argc++] = opts;
1252
1253 argv[argc++] = wxThePrintSetupData->GetPrinterFile();
1254 argv[argc++] = (char *) NULL;
1255 wxExecute (argv, TRUE);
1256 wxRemoveFile(wxThePrintSetupData->GetPrinterFile());
1257 }
1258 break;
1259
1260 case PS_FILE:
1261 break;
1262 }
1263 }
1264 #endif
1265 }
1266
1267 void wxPostScriptDC::StartPage ()
1268 {
1269 wxCHECK_RET( Ok(), "invalid postscript dc" );
1270
1271 *m_pstream << "%%Page: " << (wxPageNumber++) << "\n";
1272
1273 // *m_pstream << "matrix currentmatrix\n";
1274
1275 // Added by Chris Breeze
1276
1277 // Each page starts with an "initgraphics" which resets the
1278 // transformation and so we need to reset the origin
1279 // (and rotate the page for landscape printing)
1280
1281 /*
1282 m_scaleFactor = 1.0;
1283 m_logicalOriginX = 0;
1284 m_logicalOriginY = 0;
1285 */
1286
1287 // Output scaling
1288 long translate_x, translate_y;
1289 double scale_x, scale_y;
1290 wxThePrintSetupData->GetPrinterTranslation(&translate_x, &translate_y);
1291 wxThePrintSetupData->GetPrinterScaling(&scale_x, &scale_y);
1292
1293 if (wxThePrintSetupData->GetPrinterOrientation() == PS_LANDSCAPE)
1294 {
1295 translate_y -= m_maxY;
1296 *m_pstream << "90 rotate\n";
1297 }
1298
1299 *m_pstream << scale_x << " " << scale_y << " scale\n";
1300 *m_pstream << translate_x << " " << translate_y << " translate\n";
1301 }
1302
1303 void wxPostScriptDC::EndPage ()
1304 {
1305 wxCHECK_RET( Ok(), "invalid postscript dc" );
1306
1307 *m_pstream << "showpage\n";
1308 }
1309
1310 bool wxPostScriptDC::Blit (long xdest, long ydest, long fwidth, long fheight,
1311 wxDC *source, long xsrc, long ysrc, int WXUNUSED(rop), bool WXUNUSED(useMask))
1312 {
1313 wxCHECK_MSG( Ok(), FALSE, "invalid postscript dc" );
1314
1315 return TRUE;
1316 }
1317
1318 long wxPostScriptDC::GetCharHeight ()
1319 {
1320 if (m_font.Ok())
1321 return m_font.GetPointSize();
1322 else
1323 return 12;
1324 }
1325
1326 void wxPostScriptDC::GetTextExtent (const wxString& string, long *x, long *y,
1327 long *descent, long *externalLeading, wxFont *theFont,
1328 bool WXUNUSED(use16))
1329 {
1330 // if (!m_pstream) return;
1331
1332 wxFont *fontToUse = theFont;
1333
1334 if (!fontToUse) fontToUse = (wxFont*) &m_font;
1335
1336 #if !USE_AFM_FOR_POSTSCRIPT
1337 // Provide a VERY rough estimate (avoid using it)
1338 // Chris Breeze 5/11/97: produces accurate results for mono-spaced
1339 // font such as Courier (aka wxMODERN)
1340 int height = 12;
1341 if (fontToUse)
1342 {
1343 height = fontToUse->GetPointSize();
1344 }
1345 *x = strlen (string) * height * 72 / 120;
1346 *y = (long) (height * 1.32); // allow for descender
1347
1348 if (descent)
1349 *descent = 0;
1350 if (externalLeading)
1351 *externalLeading = 0;
1352 #else
1353 // +++++ start of contributed code +++++
1354
1355 // ************************************************************
1356 // method for calculating string widths in postscript:
1357 // read in the AFM (adobe font metrics) file for the
1358 // actual font, parse it and extract the character widths
1359 // and also the descender. this may be improved, but for now
1360 // it works well. the AFM file is only read in if the
1361 // font is changed. this may be chached in the future.
1362 // calls to GetTextExtent with the font unchanged are rather
1363 // efficient!!!
1364 //
1365 // for each font and style used there is an AFM file necessary.
1366 // currently i have only files for the roman font family.
1367 // i try to get files for the other ones!
1368 //
1369 // CAVE: the size of the string is currently always calculated
1370 // in 'points' (1/72 of an inch). this should later on be
1371 // changed to depend on the mapping mode.
1372 // CAVE: the path to the AFM files must be set before calling this
1373 // function. this is usually done by a call like the following:
1374 // wxSetAFMPath("d:\\wxw161\\afm\\");
1375 //
1376 // example:
1377 //
1378 // wxPostScriptDC dc(NULL, TRUE);
1379 // if (dc.Ok()){
1380 // wxSetAFMPath("d:\\wxw161\\afm\\");
1381 // dc.StartDoc("Test");
1382 // dc.StartPage();
1383 // long w,h;
1384 // dc.SetFont(new wxFont(10, wxROMAN, wxNORMAL, wxNORMAL));
1385 // dc.GetTextExtent("Hallo",&w,&h);
1386 // dc.EndPage();
1387 // dc.EndDoc();
1388 // }
1389 //
1390 // by steve (stefan.hammes@urz.uni-heidelberg.de)
1391 // created: 10.09.94
1392 // updated: 14.05.95
1393
1394 assert(fontToUse && "void wxPostScriptDC::GetTextExtent: no font defined");
1395 assert(x && "void wxPostScriptDC::GetTextExtent: x == NULL");
1396 assert(y && "void wxPostScriptDC::GetTextExtent: y == NULL");
1397
1398 // these static vars are for storing the state between calls
1399 static int lastFamily= INT_MIN;
1400 static int lastSize= INT_MIN;
1401 static int lastStyle= INT_MIN;
1402 static int lastWeight= INT_MIN;
1403 static int lastDescender = INT_MIN;
1404 static int lastWidths[256]; // widths of the characters
1405
1406 // get actual parameters
1407 const int Family = fontToUse->GetFamily();
1408 const int Size = fontToUse->GetPointSize();
1409 const int Style = fontToUse->GetStyle();
1410 const int Weight = fontToUse->GetWeight();
1411
1412 // if we have another font, read the font-metrics
1413 if(Family!=lastFamily||Size!=lastSize||Style!=lastStyle||Weight!=lastWeight){
1414 // store actual values
1415 lastFamily = Family;
1416 lastSize = Size;
1417 lastStyle = Style;
1418 lastWeight = Weight;
1419
1420 // read in new font metrics **************************************
1421
1422 // 1. construct filename ******************************************
1423 /* MATTHEW: [2] Use wxTheFontNameDirectory */
1424 const char *name;
1425
1426 // Julian - we'll need to do this a different way now we've removed the
1427 // font directory system. Must find Stefan's original code.
1428
1429 name = wxTheFontNameDirectory->GetAFMName(Family, Weight, Style);
1430 if (!name)
1431 name = "unknown";
1432
1433 // get the directory of the AFM files
1434 char afmName[256];
1435 afmName[0] = 0;
1436 if (wxGetAFMPath())
1437 strcpy(afmName,wxGetAFMPath());
1438
1439 // 2. open and process the file **********************************
1440
1441 // a short explanation of the AFM format:
1442 // we have for each character a line, which gives its size
1443 // e.g.:
1444 //
1445 // C 63 ; WX 444 ; N question ; B 49 -14 395 676 ;
1446 //
1447 // that means, we have a character with ascii code 63, and width
1448 // (444/1000 * fontSize) points.
1449 // the other data is ignored for now!
1450 //
1451 // when the font has changed, we read in the right AFM file and store the
1452 // character widths in an array, which is processed below (see point 3.).
1453
1454 // new elements JC Sun Aug 25 23:21:44 MET DST 1996
1455
1456
1457 strcat(afmName,name);
1458 strcat(afmName,".afm");
1459 FILE *afmFile = fopen(afmName,"r");
1460 if(afmFile==NULL){
1461 wxDebugMsg("GetTextExtent: can't open AFM file '%s'\n",afmName);
1462 wxDebugMsg(" using approximate values\n");
1463 int i;
1464 for (i=0; i<256; i++) lastWidths[i] = 500; // an approximate value
1465 lastDescender = -150; // dito.
1466 }else{
1467 int i;
1468 // init the widths array
1469 for(i=0; i<256; i++) lastWidths[i]= INT_MIN;
1470 // some variables for holding parts of a line
1471 char cString[10],semiString[10],WXString[10],descString[20];
1472 char upString[30], utString[30], encString[50];
1473 char line[256];
1474 int ascii,cWidth;
1475 // read in the file and parse it
1476 while(fgets(line,sizeof(line),afmFile)!=NULL){
1477 // A.) check for descender definition
1478 if(strncmp(line,"Descender",9)==0){
1479 if((sscanf(line,"%s%d",descString,&lastDescender)!=2)
1480 || (strcmp(descString,"Descender")!=0)) {
1481 wxDebugMsg("AFM-file '%s': line '%s' has error (bad descender)\n",
1482 afmName,line);
1483 }
1484 }
1485 // JC 1.) check for UnderlinePosition
1486 else if(strncmp(line,"UnderlinePosition",17)==0){
1487 if((sscanf(line,"%s%lf",upString,&UnderlinePosition)!=2)
1488 || (strcmp(upString,"UnderlinePosition")!=0)) {
1489 wxDebugMsg("AFM-file '%s': line '%s' has error (bad UnderlinePosition)\n",
1490 afmName,line);
1491 }
1492 }
1493 // JC 2.) check for UnderlineThickness
1494 else if(strncmp(line,"UnderlineThickness",18)==0){
1495 if((sscanf(line,"%s%lf",utString,&UnderlineThickness)!=2)
1496 || (strcmp(utString,"UnderlineThickness")!=0)) {
1497 wxDebugMsg("AFM-file '%s': line '%s' has error (bad UnderlineThickness)\n",
1498 afmName,line);
1499 }
1500 }
1501 // JC 3.) check for EncodingScheme
1502 else if(strncmp(line,"EncodingScheme",14)==0){
1503 if((sscanf(line,"%s%s",utString,encString)!=2)
1504 || (strcmp(utString,"EncodingScheme")!=0)) {
1505 wxDebugMsg("AFM-file '%s': line '%s' has error (bad EncodingScheme)\n",
1506 afmName,line);
1507 }
1508 else if (strncmp(encString, "AdobeStandardEncoding", 21))
1509 {
1510 wxDebugMsg("AFM-file '%s': line '%s' has error (unsupported EncodingScheme %s)\n",
1511 afmName,line, encString);
1512 }
1513 }
1514 // B.) check for char-width
1515 else if(strncmp(line,"C ",2)==0){
1516 if(sscanf(line,"%s%d%s%s%d",
1517 cString,&ascii,semiString,WXString,&cWidth)!=5){
1518 wxDebugMsg("AFM-file '%s': line '%s' has an error (bad character width)\n",afmName,line);
1519 }
1520 if(strcmp(cString,"C")!=0 || strcmp(semiString,";")!=0 ||
1521 strcmp(WXString,"WX")!=0){
1522 wxDebugMsg("AFM-file '%s': line '%s' has a format error\n",afmName,line);
1523 }
1524 //printf(" char '%c'=%d has width '%d'\n",ascii,ascii,cWidth);
1525 if(ascii>=0 && ascii<256){
1526 lastWidths[ascii] = cWidth; // store width
1527 }else{
1528 /* MATTHEW: this happens a lot; don't print an error */
1529 // wxDebugMsg("AFM-file '%s': ASCII value %d out of range\n",afmName,ascii);
1530 }
1531 }
1532 // C.) ignore other entries.
1533 }
1534 fclose(afmFile);
1535 }
1536 // hack to compute correct values for german 'Umlaute'
1537 // the correct way would be to map the character names
1538 // like 'adieresis' to corresp. positions of ISOEnc and read
1539 // these values from AFM files, too. Maybe later ...
1540 lastWidths[196] = lastWidths['A']; // Ä
1541 lastWidths[228] = lastWidths['a']; // ä
1542 lastWidths[214] = lastWidths['O']; // Ö
1543 lastWidths[246] = lastWidths['o']; // ö
1544 lastWidths[220] = lastWidths['U']; // Ü
1545 lastWidths[252] = lastWidths['u']; // ü
1546 lastWidths[223] = lastWidths[251]; // ß
1547 }
1548
1549 // JC: calculate UnderlineThickness/UnderlinePosition
1550 m_underlinePosition = m_underlinePosition * fontToUse->GetPointSize() / 1000.0f;
1551 m_underlineThickness = m_underlineThickness * fontToUse->GetPointSize() / 1000.0f * m_scaleFactor;
1552
1553 // 3. now the font metrics are read in, calc size *******************
1554 // this is done by adding the widths of the characters in the
1555 // string. they are given in 1/1000 of the size!
1556
1557 long widthSum=0;
1558 long height=Size; // by default
1559 unsigned char *p;
1560 for(p=(unsigned char *)(const char *)string; *p; p++){
1561 if(lastWidths[*p]== INT_MIN){
1562 wxDebugMsg("GetTextExtent: undefined width for character '%c' (%d)\n",
1563 *p,*p);
1564 widthSum += (long)(lastWidths[' ']/1000.0F * Size); // assume space
1565 }else{
1566 widthSum += (long)((lastWidths[*p]/1000.0F)*Size);
1567 }
1568 }
1569 // add descender to height (it is usually a negative value)
1570 if(lastDescender!=INT_MIN){
1571 height += (long)(((-lastDescender)/1000.0F) * Size); /* MATTHEW: forgot scale */
1572 }
1573
1574 // return size values
1575 *x = widthSum;
1576 *y = height;
1577
1578 // return other parameters
1579 if (descent){
1580 if(lastDescender!=INT_MIN){
1581 *descent = (long)(((-lastDescender)/1000.0F) * Size); /* MATTHEW: forgot scale */
1582 }else{
1583 *descent = 0;
1584 }
1585 }
1586
1587 // currently no idea how to calculate this!
1588 // if (externalLeading) *externalLeading = 0;
1589 if (externalLeading)
1590 *externalLeading = 0;
1591
1592 // ----- end of contributed code -----
1593 #endif
1594 }
1595
1596 void wxPostScriptDC::GetSizeMM(long *width, long *height) const
1597 {
1598 const char *paperType = wxThePrintSetupData->GetPaperName();
1599
1600 if (!paperType) paperType = _("A4 210 x 297 mm");
1601
1602 wxPrintPaperType *paper = wxThePrintPaperDatabase->FindPaperType(paperType);
1603
1604 if (!paper) paper = wxThePrintPaperDatabase->FindPaperType(_("A4 210 x 297 mm"));
1605
1606 if (paper)
1607 {
1608 if (width) *width = paper->widthMM;
1609 if (height) *height = paper->heightMM;
1610 }
1611 else
1612 {
1613 if (width) *width = 210;
1614 if (height) *height = 297;
1615 }
1616 }
1617