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