]> git.saurik.com Git - wxWidgets.git/blob - src/common/postscrp.cpp
Removed streams from VC++ makefile for now; corrected typo in log.h
[wxWidgets.git] / src / common / postscrp.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: postscrp.cpp
3 // Purpose: wxPostScriptDC implementation
4 // Author: Julian Smart
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 "postscrp.h"
14 #pragma implementation
15 #pragma interface
16 #endif
17
18 // For compilers that support precompilation, includes "wx.h".
19 #include "wx/wxprec.h"
20
21 #ifdef __BORLANDC__
22 #pragma hdrstop
23 #endif
24
25 #include "wx/defs.h"
26
27 #if USE_POSTSCRIPT
28
29 #ifndef WX_PRECOMP
30 #include "wx/intl.h"
31 #include "wx/frame.h"
32 #include "wx/postscrp.h"
33 #include "wx/utils.h"
34 #include "wx/filedlg.h"
35 #include "wx/msgdlg.h"
36 #include "wx/app.h"
37 #include "wx/button.h"
38 #include "wx/radiobox.h"
39 #include "wx/textctrl.h"
40 #include "wx/stattext.h"
41 #include "wx/icon.h"
42 #include "wx/list.h"
43 #endif
44
45 #include "wx/dcmemory.h"
46
47 #ifdef __WXMSW__
48 #include "wx/msw/private.h"
49 #endif
50
51 #if USE_IOSTREAMH
52 #include <iostream.h>
53 #else
54 #include <iostream>
55 #endif
56
57 #include <fstream.h>
58 #include <math.h>
59 #include <stdio.h>
60 #include <stdlib.h>
61 #include <string.h>
62 #include <limits.h>
63 #include <assert.h>
64
65 #ifdef __WXGTK__
66
67 #include "gdk/gdkx.h" // GDK_DISPLAY
68 #include "gdk/gdkprivate.h" // XImage
69 #include <X11/Xlib.h>
70 #include <X11/Xutil.h>
71
72 #endif
73
74 #ifdef __WXMSW__
75
76 #ifdef DrawText
77 #undef DrawText
78 #endif
79
80 #ifdef StartDoc
81 #undef StartDoc
82 #endif
83
84 #ifdef GetCharWidth
85 #undef GetCharWidth
86 #endif
87
88 #ifdef FindWindow
89 #undef FindWindow
90 #endif
91
92 #endif
93
94 // Declarations local to this file
95 #define YSCALE(y) (m_yOrigin / m_scaleFactor - (y))
96
97 // Determine the Default Postscript Previewer
98 // available on the platform
99 #if defined(__SUN__) && defined(__XVIEW__)
100 // OpenWindow/NeWS's Postscript Previewer
101 # define PS_VIEWER_PROG "pageview"
102 #elif defined(__VMS__)
103 #define PS_VIEWER_PROG "view/format=ps/select=x_display"
104 #elif defined(__SGI__)
105 // SGI's Display Postscript Previewer
106 //# define PS_VIEWER_PROG "dps"
107 # define PS_VIEWER_PROG "xpsview"
108 #elif defined(__X__) || defined(__WXGTK__)
109 // Front-end to ghostscript
110 # define PS_VIEWER_PROG "ghostview"
111 #else
112 // Windows ghostscript/ghostview
113 # define PS_VIEWER_PROG NULL
114 #endif
115
116 wxPrintSetupData *wxThePrintSetupData = NULL;
117
118 // these should move into wxPostscriptDC:
119 double UnderlinePosition = 0.0F;
120 double UnderlineThickness = 0.0F;
121
122 #define _MAXPATHLEN 500
123
124 /* See "wxspline.inc" and "xfspline.inc" */
125 #if USE_XFIG_SPLINE_CODE
126 static const char *wxPostScriptHeaderSpline = " \
127 /DrawSplineSection {\n\
128 /y3 exch def\n\
129 /x3 exch def\n\
130 /y2 exch def\n\
131 /x2 exch def\n\
132 /y1 exch def\n\
133 /x1 exch def\n\
134 /xa x1 x2 x1 sub 0.666667 mul add def\n\
135 /ya y1 y2 y1 sub 0.666667 mul add def\n\
136 /xb x3 x2 x3 sub 0.666667 mul add def\n\
137 /yb y3 y2 y3 sub 0.666667 mul add def\n\
138 x1 y1 lineto\n\
139 xa ya xb yb x3 y3 curveto\n\
140 } def\n\
141 ";
142 #else
143 // No extra PS header for this spline implementation.
144 static const char *wxPostScriptHeaderSpline = NULL;
145
146 #endif /* USE_XFIG_SPLINE_CODE */
147
148 // steve, 05.09.94
149 // VMS has a bug in the ofstream class.
150 // the buffering doesn't work correctly. therefore
151 // we will allocate (temporarily) a very big buffer (1MB), so
152 // that a buffer overflow will not occur.
153 #ifdef __VMS__
154 #define VMS_BUFSIZ (1024L*1024L)
155 static char *fileBuffer = NULL;
156 #endif
157
158 #if !USE_SHARED_LIBRARY
159 IMPLEMENT_DYNAMIC_CLASS(wxPostScriptDC, wxDC)
160 IMPLEMENT_DYNAMIC_CLASS(wxPrintSetupData, wxObject)
161 IMPLEMENT_DYNAMIC_CLASS(wxPrintPaperType, wxObject)
162 IMPLEMENT_DYNAMIC_CLASS(wxPrintPaperDatabase, wxList)
163 #endif
164
165 wxPostScriptDC::wxPostScriptDC (void)
166 {
167 // m_yOrigin = 792; // For EPS output
168 m_yOrigin = 842; // For A4 output
169
170 m_minX = 1000;
171 m_minY = 1000;
172 m_maxX = -1000;
173 m_maxY = -1000;
174 m_title = "";
175
176 m_pstream = NULL;
177
178 #ifdef __WXMSW__
179 // Can only send to file in Windows
180 wxThePrintSetupData->SetPrinterMode(PS_FILE);
181 #endif
182
183 m_currentRed = 255;
184 m_currentGreen = 255;
185 m_currentBlue = 0;
186 }
187
188 wxPostScriptDC::wxPostScriptDC (const wxString& file, bool interactive, wxWindow *parent)
189 {
190 Create(file, interactive, parent);
191 }
192
193 bool wxPostScriptDC::Create(const wxString& file, bool interactive, wxWindow *parent)
194 {
195 m_isInteractive = interactive;
196
197 m_yOrigin = 792; // For EPS output
198 // m_yOrigin = 842; // For A4 output
199
200 m_minX = 1000;
201 m_minY = 1000;
202 m_maxX = -1000;
203 m_maxY = -1000;
204 m_title = "";
205 m_filename = file;
206 m_pstream = NULL;
207
208 #ifdef __WXMSW__
209 // Can only send to file in Windows
210 wxThePrintSetupData->SetPrinterMode(PS_FILE);
211 #endif
212
213 if (m_isInteractive)
214 {
215 if ((m_ok = PrinterDialog (parent) ) == FALSE) return FALSE;
216 }
217 else
218 m_ok = TRUE;
219
220 m_currentRed = 255;
221 m_currentGreen = 255;
222 m_currentBlue = 0;
223
224 m_scaleFactor = 1.0;
225
226 return m_ok;
227 }
228
229 wxPostScriptDC::~wxPostScriptDC (void)
230 {
231 if (m_pstream)
232 delete m_pstream;
233 }
234
235 bool wxPostScriptDC::PrinterDialog(wxWindow *parent)
236 {
237 wxPostScriptPrintDialog dialog (parent, "Printer Settings", wxPoint(150, 150), wxSize(400, 400), wxDEFAULT_DIALOG_STYLE | wxDIALOG_MODAL);
238 m_ok = (dialog.ShowModal () == wxID_OK) ;
239
240 if (!m_ok)
241 return FALSE;
242
243 if ((m_filename == "") && (wxThePrintSetupData->GetPrinterMode() == PS_PREVIEW || wxThePrintSetupData->GetPrinterMode() == PS_PRINTER))
244 {
245 // steve, 05.09.94
246 #ifdef __VMS__
247 wxThePrintSetupData->SetPrinterFile("preview");
248 #else
249 // For PS_PRINTER action this depends on a Unix-style print spooler
250 // since the wx_printer_file can be destroyed during a session
251 // @@@ TODO: a Windows-style answer for non-Unix
252 char userId[256];
253 wxGetUserId (userId, sizeof (userId) / sizeof (char));
254 char tmp[256];
255 strcpy (tmp, "/tmp/preview_");
256 strcat (tmp, userId);
257 wxThePrintSetupData->SetPrinterFile(tmp);
258 #endif
259 char tmp2[256];
260 strcpy(tmp2, wxThePrintSetupData->GetPrinterFile());
261 strcat (tmp2, ".ps");
262 wxThePrintSetupData->SetPrinterFile(tmp2);
263 m_filename = tmp2;
264 }
265 else if ((m_filename == "") && (wxThePrintSetupData->GetPrinterMode() == PS_FILE))
266 {
267 char *file = wxSaveFileSelector ("PostScript", "ps");
268 if (!file)
269 {
270 m_ok = FALSE;
271 return FALSE;
272 }
273 wxThePrintSetupData->SetPrinterFile(file);
274 m_filename = file;
275 m_ok = TRUE;
276 }
277
278 return m_ok;
279 }
280
281 void wxPostScriptDC::SetClippingRegion (long cx, long cy, long cw, long ch)
282 {
283 if (m_clipping)
284 return;
285 if (!m_pstream)
286 return;
287
288 m_clipping = TRUE;
289 *m_pstream << "gsave\n";
290 *m_pstream << "newpath\n";
291 *m_pstream << cx << " " << YSCALE (cy) << " moveto\n";
292 *m_pstream << cx + cw << " " << YSCALE (cy) << " lineto\n";
293 *m_pstream << cx + cw << " " << YSCALE (cy + ch) << " lineto\n";
294 *m_pstream << cx << " " << YSCALE (cy + ch) << " lineto\n";
295 *m_pstream << "closepath clip newpath\n";
296 }
297
298 void wxPostScriptDC::DestroyClippingRegion (void)
299 {
300 if (!m_pstream)
301 return;
302 if (m_clipping)
303 {
304 m_clipping = FALSE;
305 *m_pstream << "grestore\n";
306 }
307 }
308
309 void wxPostScriptDC::Clear (void)
310 {
311 }
312
313 void wxPostScriptDC::FloodFill (long WXUNUSED(x), long WXUNUSED(y), wxColour * WXUNUSED(col), int WXUNUSED(style))
314 {
315 }
316
317 bool wxPostScriptDC::GetPixel (long WXUNUSED(x), long WXUNUSED(y), wxColour * WXUNUSED(col)) const
318 {
319 return FALSE;
320 }
321
322 void wxPostScriptDC::CrossHair (long WXUNUSED(x), long WXUNUSED(y))
323 {
324 }
325
326 void wxPostScriptDC::DrawLine (long x1, long y1, long x2, long y2)
327 {
328 if (!m_pstream)
329 return;
330 if (m_pen.Ok())
331 SetPen (m_pen);
332 *m_pstream << "newpath\n";
333 *m_pstream << x1 << " " << YSCALE (y1) << " moveto\n";
334 *m_pstream << x2 << " " << YSCALE (y2) << " lineto\n";
335 *m_pstream << "stroke\n";
336 CalcBoundingBox (x1, (long)YSCALE (y1));
337 CalcBoundingBox (x2, (long)YSCALE (y2));
338 }
339
340 #define RAD2DEG 57.29577951308
341
342 void wxPostScriptDC::DrawArc (long x1, long y1, long x2, long y2, long xc, long yc)
343 {
344 if (!m_pstream)
345 return;
346
347 long dx = x1 - xc;
348 long dy = y1 - yc;
349 long radius = (long) sqrt(dx*dx+dy*dy);
350 double alpha1, alpha2;
351
352 if (x1 == x2 && y1 == y2) {
353 alpha1 = 0.0;
354 alpha2 = 360.0;
355 } else if (radius == 0.0) {
356 alpha1 = alpha2 = 0.0;
357 } else {
358 alpha1 = (x1 - xc == 0) ?
359 (y1 - yc < 0) ? 90.0 : -90.0 :
360 -atan2(double(y1-yc), double(x1-xc)) * RAD2DEG;
361 alpha2 = (x2 - xc == 0) ?
362 (y2 - yc < 0) ? 90.0 : -90.0 :
363 -atan2(double(y2-yc), double(x2-xc)) * RAD2DEG;
364 }
365 while (alpha1 <= 0) alpha1 += 360;
366 while (alpha2 <= 0) alpha2 += 360; // adjust angles to be between
367 while (alpha1 > 360) alpha1 -= 360; // 0 and 360 degree
368 while (alpha2 > 360) alpha2 -= 360;
369
370 if (m_brush.Ok() && m_brush.GetStyle() != wxTRANSPARENT) {
371 SetBrush(m_brush);
372 *m_pstream << "newpath\n"
373 << xc << " " << YSCALE(yc) << " "
374 << radius << " " << radius << " "
375 << alpha1 << " " << alpha2 << " ellipse\n"
376 << xc << " " << YSCALE(yc) << " lineto\n"
377 << "closepath\n"
378 << "fill\n";
379 }
380 if (m_pen.Ok() && m_pen.GetStyle() != wxTRANSPARENT) {
381 SetPen(m_pen);
382 *m_pstream << "newpath\n"
383 << xc << " " << YSCALE(yc) << " "
384 << radius << " " << radius << " "
385 << alpha1 << " " << alpha2 << " ellipse\n"
386 << "stroke\n";
387 }
388 CalcBoundingBox(x1, (long)YSCALE(y1));
389 CalcBoundingBox(x2, (long)YSCALE(y2));
390 }
391
392 void wxPostScriptDC::DrawEllipticArc(long x,long y,long w,long h,double sa,double ea)
393 {
394 if (!m_pstream)
395 return;
396
397 if (sa>=360 || sa<=-360) sa=sa-int(sa/360)*360;
398 if (ea>=360 || ea<=-360) ea=ea-int(ea/360)*360;
399 if (sa<0) sa+=360;
400 if (ea<0) ea+=360;
401 if (sa==ea)
402 {
403 DrawEllipse(x,y,w,h);
404 return;
405 }
406
407 if (m_brush.Ok() && m_brush.GetStyle () != wxTRANSPARENT)
408 {
409 SetBrush (m_brush);
410
411 *m_pstream <<
412 "newpath\n" <<
413 x+w/2 << " " << YSCALE (y+h/2) << " " <<
414 w/2 << " " << h/2 << " " <<
415 int(sa) <<" "<< int(ea)<<" true ellipticarc\n";
416
417 CalcBoundingBox (x , (long)YSCALE (y ));
418 CalcBoundingBox (x+w,(long)YSCALE(y+h));
419 }
420 if (m_pen.Ok() && m_pen.GetStyle () != wxTRANSPARENT)
421 {
422 SetPen (m_pen);
423
424 *m_pstream <<
425 "newpath\n" <<
426 x+w/2 << " " << YSCALE (y+h/2) << " " <<
427 w/2 << " " << h/2 << " " <<
428 int(sa) <<" "<< int(ea)<<" false ellipticarc\n";
429
430 CalcBoundingBox (x , (long)YSCALE (y ));
431 CalcBoundingBox (x+w,(long)YSCALE(y+h));
432 }
433 }
434
435 void wxPostScriptDC::DrawPoint (long x, long y)
436 {
437 if (!m_pstream)
438 return;
439 if (m_pen.Ok())
440 SetPen (m_pen);
441 *m_pstream << "newpath\n";
442 *m_pstream << x << " " << YSCALE (y) << " moveto\n";
443 *m_pstream << (x+1) << " " << YSCALE (y) << " lineto\n";
444 *m_pstream << "stroke\n";
445 CalcBoundingBox (x, (long)YSCALE (y));
446 }
447
448 void wxPostScriptDC::DrawPolygon (int n, wxPoint points[], long xoffset, long yoffset, int WXUNUSED(fillStyle))
449 {
450 if (!m_pstream)
451 return;
452 if (n > 0)
453 {
454 if (m_brush.Ok() && m_brush.GetStyle () != wxTRANSPARENT)
455 {
456 SetBrush (m_brush);
457 *m_pstream << "newpath\n";
458
459 long xx = points[0].x + xoffset;
460 long yy = (long) YSCALE (points[0].y + yoffset);
461 *m_pstream << xx << " " << yy << " moveto\n";
462 CalcBoundingBox (xx, yy);
463
464 int i;
465 for (i = 1; i < n; i++)
466 {
467 xx = points[i].x + xoffset;
468 yy = (long) YSCALE (points[i].y + yoffset);
469 *m_pstream << xx << " " << yy << " lineto\n";
470 CalcBoundingBox (xx, yy);
471 }
472 *m_pstream << "fill\n";
473 }
474
475 if (m_pen.Ok() && m_pen.GetStyle () != wxTRANSPARENT)
476 {
477 SetPen (m_pen);
478 *m_pstream << "newpath\n";
479
480 long xx = points[0].x + xoffset;
481 long yy = (long) YSCALE (points[0].y + yoffset);
482 *m_pstream << xx << " " << yy << " moveto\n";
483 CalcBoundingBox (xx, yy);
484
485 int i;
486 for (i = 1; i < n; i++)
487 {
488 xx = points[i].x + xoffset;
489 yy = (long) YSCALE (points[i].y + yoffset);
490 *m_pstream << xx << " " << yy << " lineto\n";
491 CalcBoundingBox (xx, yy);
492 }
493
494 // Close the polygon
495 xx = points[0].x + xoffset;
496 yy = (long) YSCALE (points[0].y + yoffset);
497 *m_pstream << xx << " " << yy << " lineto\n";
498
499 // Output the line
500 *m_pstream << "stroke\n";
501 }
502 }
503 }
504
505 void wxPostScriptDC::DrawLines (int n, wxPoint points[], long xoffset, long yoffset)
506 {
507 if (!m_pstream)
508 return;
509 if (n > 0)
510 {
511 if (m_pen.Ok())
512 SetPen (m_pen);
513
514 *m_pstream << "newpath\n";
515
516 long xx = points[0].x + xoffset;
517 long yy = (long) YSCALE (points[0].y + yoffset);
518 *m_pstream << xx << " " << yy << " moveto\n";
519 CalcBoundingBox (xx, yy);
520
521 int i;
522 for (i = 1; i < n; i++)
523 {
524 xx = points[i].x + xoffset;
525 yy = (long) YSCALE (points[i].y + yoffset);
526 *m_pstream << xx << " " << yy << " lineto\n";
527 CalcBoundingBox (xx, yy);
528 }
529 *m_pstream << "stroke\n";
530 }
531 }
532
533 void wxPostScriptDC::DrawRectangle (long x, long y, long width, long height)
534 {
535 if (!m_pstream)
536 return;
537 if (m_brush.Ok() && m_brush.GetStyle () != wxTRANSPARENT)
538 {
539 SetBrush (m_brush);
540
541 *m_pstream << "newpath\n";
542 *m_pstream << x << " " << YSCALE (y) << " moveto\n";
543 *m_pstream << x + width << " " << YSCALE (y) << " lineto\n";
544 *m_pstream << x + width << " " << YSCALE (y + height) << " lineto\n";
545 *m_pstream << x << " " << YSCALE (y + height) << " lineto\n";
546 *m_pstream << "closepath\n";
547 *m_pstream << "fill\n";
548
549 CalcBoundingBox (x, (long)YSCALE (y));
550 CalcBoundingBox (x + width, (long)YSCALE (y + height));
551 }
552 if (m_pen.Ok() && m_pen.GetStyle () != wxTRANSPARENT)
553 {
554 SetPen (m_pen);
555
556 *m_pstream << "newpath\n";
557 *m_pstream << x << " " << YSCALE (y) << " moveto\n";
558 *m_pstream << x + width << " " << YSCALE (y) << " lineto\n";
559 *m_pstream << x + width << " " << YSCALE (y + height) << " lineto\n";
560 *m_pstream << x << " " << YSCALE (y + height) << " lineto\n";
561 *m_pstream << "closepath\n";
562 *m_pstream << "stroke\n";
563
564 CalcBoundingBox (x, (long)YSCALE (y));
565 CalcBoundingBox (x + width, (long)YSCALE (y + height));
566 }
567 }
568
569 void wxPostScriptDC::DrawRoundedRectangle (long x, long y, long width, long height, double radius)
570 {
571 if (!m_pstream)
572 return;
573
574 if (radius < 0.0)
575 {
576 // Now, a negative radius is interpreted to mean
577 // 'the proportion of the smallest X or Y dimension'
578 double smallest = 0.0;
579 if (width < height)
580 smallest = width;
581 else
582 smallest = height;
583 radius = (-radius * smallest);
584 }
585
586 if (m_brush.Ok() && m_brush.GetStyle () != wxTRANSPARENT)
587 {
588 SetBrush (m_brush);
589 // Draw rectangle anticlockwise
590 *m_pstream << "newpath\n";
591 *m_pstream << x + radius << " " << YSCALE (y + radius) << " " << radius << " 90 180 arc\n";
592
593 *m_pstream << x << " " << YSCALE (y + radius) << " moveto\n";
594
595 *m_pstream << x + radius << " " << YSCALE (y + height - radius) << " " << radius << " 180 270 arc\n";
596 *m_pstream << x + width - radius << " " << YSCALE (y + height) << " lineto\n";
597
598 *m_pstream << x + width - radius << " " << YSCALE (y + height - radius) << " " << radius << " 270 0 arc\n";
599 *m_pstream << x + width << " " << YSCALE (y + radius) << " lineto\n";
600
601 *m_pstream << x + width - radius << " " << YSCALE (y + radius) << " " << radius << " 0 90 arc\n";
602
603 *m_pstream << x + radius << " " << YSCALE (y) << " lineto\n";
604
605 *m_pstream << "closepath\n";
606
607 *m_pstream << "fill\n";
608
609 CalcBoundingBox (x, (long)YSCALE (y));
610 CalcBoundingBox (x + width, (long)YSCALE (y + height));
611 }
612 if (m_pen.Ok() && m_pen.GetStyle () != wxTRANSPARENT)
613 {
614 SetPen (m_pen);
615 // Draw rectangle anticlockwise
616 *m_pstream << "newpath\n";
617 *m_pstream << x + radius << " " << YSCALE (y + radius) << " " << radius << " 90 180 arc\n";
618
619 *m_pstream << x << " " << YSCALE (y + height - radius) << " lineto\n";
620
621 *m_pstream << x + radius << " " << YSCALE (y + height - radius) << " " << radius << " 180 270 arc\n";
622 *m_pstream << x + width - radius << " " << YSCALE (y + height) << " lineto\n";
623
624 *m_pstream << x + width - radius << " " << YSCALE (y + height - radius) << " " << radius << " 270 0 arc\n";
625 *m_pstream << x + width << " " << YSCALE (y + radius) << " lineto\n";
626
627 *m_pstream << x + width - radius << " " << YSCALE (y + radius) << " " << radius << " 0 90 arc\n";
628
629 *m_pstream << x + radius << " " << YSCALE (y) << " lineto\n";
630
631 *m_pstream << "closepath\n";
632
633 *m_pstream << "stroke\n";
634
635 CalcBoundingBox (x, (long)YSCALE (y));
636 CalcBoundingBox (x + width, (long)YSCALE (y + height));
637 }
638 }
639
640 void wxPostScriptDC::DrawEllipse (long x, long y, long width, long height)
641 {
642 if (!m_pstream)
643 return;
644 if (m_brush.Ok() && m_brush.GetStyle () != wxTRANSPARENT)
645 {
646 SetBrush (m_brush);
647
648 *m_pstream << "newpath\n";
649 *m_pstream << x + width / 2 << " " << YSCALE (y + height / 2) << " ";
650 *m_pstream << width / 2 << " " << height / 2 << " 0 360 ellipse\n";
651 *m_pstream << "fill\n";
652
653 CalcBoundingBox (x - width, (long)YSCALE (y - height));
654 CalcBoundingBox (x + width, (long)YSCALE (y + height));
655 }
656 if (m_pen.Ok() && m_pen.GetStyle () != wxTRANSPARENT)
657 {
658 SetPen (m_pen);
659
660 *m_pstream << "newpath\n";
661 *m_pstream << x + width / 2 << " " << YSCALE (y + height / 2) << " ";
662 *m_pstream << width / 2 << " " << height / 2 << " 0 360 ellipse\n";
663 *m_pstream << "stroke\n";
664
665 CalcBoundingBox (x - width, (long)YSCALE (y - height));
666 CalcBoundingBox (x + width, (long)YSCALE (y + height));
667 }
668 }
669
670 void wxPostScriptDC::DrawIcon (const wxIcon& icon, long x, long y)
671 {
672 #if defined(__X__) || defined(__WXGTK__)
673 wxMemoryDC memDC;
674 memDC.SelectObject(icon);
675 Blit(x, y, icon.GetWidth(), icon.GetHeight(), &memDC, 0, 0);
676 #endif
677 }
678
679 void wxPostScriptDC::SetFont (const wxFont& the_font)
680 {
681 if (!m_pstream)
682 return;
683
684 if (m_font == the_font)
685 return;
686
687 m_font = the_font;
688
689 if ( !m_font.Ok() )
690 return;
691
692 char buf[100];
693 char *name;
694 char *style = "";
695 int Style = m_font.GetStyle ();
696 int Weight = m_font.GetWeight ();
697
698 switch (m_font.GetFamily ())
699 {
700 case wxTELETYPE:
701 case wxMODERN:
702 name = "/Courier";
703 break;
704 case wxSWISS:
705 name = "/Helvetica";
706 break;
707 case wxROMAN:
708 // name = "/Times-Roman";
709 name = "/Times"; // Altered by EDZ
710 break;
711 case wxSCRIPT:
712 name = "/Zapf-Chancery-MediumItalic";
713 Style = wxNORMAL;
714 Weight = wxNORMAL;
715 break;
716 default:
717 case wxDEFAULT: // Sans Serif Font
718 name = "/LucidaSans";
719 }
720
721 if (Style == wxNORMAL && (Weight == wxNORMAL || Weight == wxLIGHT))
722 {
723 if (m_font.GetFamily () == wxROMAN)
724 style = "-Roman";
725 else
726 style = "";
727 }
728 else if (Style == wxNORMAL && Weight == wxBOLD)
729 style = "-Bold";
730
731 else if (Style == wxITALIC && (Weight == wxNORMAL || Weight == wxLIGHT))
732 {
733 if (m_font.GetFamily () == wxROMAN)
734 style = "-Italic";
735 else
736 style = "-Oblique";
737 }
738 else if (Style == wxITALIC && Weight == wxBOLD)
739 {
740 if (m_font.GetFamily () == wxROMAN)
741 style = "-BoldItalic";
742 else
743 style = "-BoldOblique";
744 }
745 else if (Style == wxSLANT && (Weight == wxNORMAL || Weight == wxLIGHT))
746 {
747 if (m_font.GetFamily () == wxROMAN)
748 style = "-Italic";
749 else
750 style = "-Oblique";
751 }
752 else if (Style == wxSLANT && Weight == wxBOLD)
753 {
754 if (m_font.GetFamily () == wxROMAN)
755 style = "-BoldItalic";
756 else
757 style = "-BoldOblique";
758 }
759 else
760 style = "";
761
762 strcpy (buf, name);
763 strcat (buf, style);
764 *m_pstream << buf << " findfont\n";
765 *m_pstream << m_font.GetPointSize() * m_scaleFactor << " scalefont setfont\n";
766 }
767
768 void wxPostScriptDC::SetPen (const wxPen& pen)
769 {
770 if (!m_pstream)
771 return;
772
773 int oldStyle = m_pen.GetStyle();
774
775 m_pen = pen;
776
777 if (!m_pen.Ok())
778 return;
779
780 // Line width
781 *m_pstream << m_pen.GetWidth () << " setlinewidth\n";
782
783 // Line style - WRONG: 2nd arg is OFFSET
784 /*
785 Here, I'm afraid you do not conceive meaning of parameters of 'setdash'
786 operator correctly. You should look-up this in the Red Book: the 2nd parame-
787 ter is not number of values in the array of the first one, but an offset
788 into this description of the pattern. I mean a real *offset* not index
789 into array. I.e. If the command is [3 4] 1 setdash is used, then there
790 will be first black line *2* units long, then space 4 units, then the
791 pattern of *3* units black, 4 units space will be repeated.
792 */
793 static char *dotted = "[2 5] 2";
794 static char *short_dashed = "[4 4] 2";
795 static char *long_dashed = "[4 8] 2";
796 static char *dotted_dashed = "[6 6 2 6] 4";
797
798 char *psdash = NULL;
799 switch (m_pen.GetStyle ())
800 {
801 case wxDOT:
802 psdash = dotted;
803 break;
804 case wxSHORT_DASH:
805 psdash = short_dashed;
806 break;
807 case wxLONG_DASH:
808 psdash = long_dashed;
809 break;
810 case wxDOT_DASH:
811 psdash = dotted_dashed;
812 break;
813 case wxSOLID:
814 case wxTRANSPARENT:
815 default:
816 psdash = "[] 0";
817 break;
818 }
819 if (oldStyle != m_pen.GetStyle())
820 *m_pstream << psdash << " setdash\n";
821
822 // Line colour
823 unsigned char red = m_pen.GetColour ().Red ();
824 unsigned char blue = m_pen.GetColour ().Blue ();
825 unsigned char green = m_pen.GetColour ().Green ();
826
827 if (!m_colour)
828 {
829 // Anything not white is black
830 if (!(red == (unsigned char) 255 && blue == (unsigned char) 255
831 && green == (unsigned char) 255))
832 {
833 red = (unsigned char) 0;
834 green = (unsigned char) 0;
835 blue = (unsigned char) 0;
836 }
837 }
838
839 if (!(red == m_currentRed && green == m_currentGreen && blue == m_currentBlue))
840 {
841 long redPS = (long) (((int) red) / 255.0);
842 long bluePS = (long) (((int) blue) / 255.0);
843 long greenPS = (long) (((int) green) / 255.0);
844
845 *m_pstream << redPS << " " << greenPS << " " << bluePS << " setrgbcolor\n";
846
847 m_currentRed = red;
848 m_currentBlue = blue;
849 m_currentGreen = green;
850 }
851 }
852
853 void wxPostScriptDC::SetBrush (const wxBrush& brush)
854 {
855 if (!m_pstream)
856 return;
857
858 m_brush = brush;
859
860 if ( !m_brush.Ok() )
861 return;
862
863 // Brush colour
864 unsigned char red = m_brush.GetColour ().Red ();
865 unsigned char blue = m_brush.GetColour ().Blue ();
866 unsigned char green = m_brush.GetColour ().Green ();
867
868 if (!m_colour)
869 {
870 // Anything not black is white
871 if (!(red == (unsigned char) 0 && blue == (unsigned char) 0
872 && green == (unsigned char) 0))
873 {
874 red = (unsigned char) 255;
875 green = (unsigned char) 255;
876 blue = (unsigned char) 255;
877 }
878 }
879
880 if (!(red == m_currentRed && green == m_currentGreen && blue == m_currentBlue))
881 {
882 long redPS = (long) (((int) red) / 255.0);
883 long bluePS = (long) (((int) blue) / 255.0);
884 long greenPS = (long) (((int) green) / 255.0);
885 *m_pstream << redPS << " " << greenPS << " " << bluePS << " setrgbcolor\n";
886 m_currentRed = red;
887 m_currentBlue = blue;
888 m_currentGreen = green;
889 }
890 }
891
892 void wxPostScriptDC::DrawText (const wxString& text, long x, long y, bool WXUNUSED(use16bit))
893 {
894 if (!m_pstream)
895 return;
896
897 // TODO: SetFont checks for identity so this will always be a NULL operation
898 if (m_font.Ok())
899 SetFont (m_font);
900
901 if (m_textForegroundColour.Ok ())
902 {
903 unsigned char red = m_textForegroundColour.Red ();
904 unsigned char blue = m_textForegroundColour.Blue ();
905 unsigned char green = m_textForegroundColour.Green ();
906
907 if (!m_colour)
908 {
909 // Anything not white is black
910 if (!(red == (unsigned char) 255 && blue == (unsigned char) 255
911 && green == (unsigned char) 255))
912 {
913 red = (unsigned char) 0;
914 green = (unsigned char) 0;
915 blue = (unsigned char) 0;
916 }
917 }
918 if (!(red == m_currentRed && green == m_currentGreen && blue == m_currentBlue))
919 {
920 long redPS = (long) (((int) red) / 255.0);
921 long bluePS = (long) (((int) blue) / 255.0);
922 long greenPS = (long) (((int) green) / 255.0);
923 *m_pstream << redPS << " " << greenPS << " " << bluePS << " setrgbcolor\n";
924
925 m_currentRed = red;
926 m_currentBlue = blue;
927 m_currentGreen = green;
928 }
929 }
930
931 int size = 10;
932 if (m_font.Ok())
933 size = m_font.GetPointSize ();
934
935 *m_pstream << x << " " << YSCALE (y + size) << " moveto\n";
936
937 // *m_pstream << "(" << text << ")" << " show\n";
938 *m_pstream << "(";
939 int len = strlen ((char *)(const char *)text);
940 int i;
941 for (i = 0; i < len; i++)
942 {
943 /*
944 char ch = text[i];
945 if (ch == ')' || ch == '(' || ch == '\\')
946 *m_pstream << "\\";
947 *m_pstream << ch;
948 */
949 int c = (unsigned char) text[i];
950 if ( c == ')' || c == '(' || c == '\\')
951 {
952 *m_pstream << "\\" << (char) c;
953 }
954 else if ( c >= 128 )
955 {
956 // Cope with character codes > 127
957 char tmp[5];
958 sprintf(tmp, "\\%o", c);
959 *m_pstream << tmp;
960 }
961 else
962 *m_pstream << (char) c;
963 }
964
965 *m_pstream << ")" << " show\n";
966
967 if (m_font.GetUnderlined())
968 {
969 long w, h;
970 GetTextExtent(text, &w, &h);
971 *m_pstream << "gsave " << x << " " << YSCALE (y + size - UnderlinePosition)
972 << " moveto\n"
973 << UnderlineThickness << " setlinewidth "
974 << x + w << " " << YSCALE (y + size - UnderlinePosition)
975 << " lineto stroke grestore\n";
976 }
977
978 CalcBoundingBox (x, (long)YSCALE (y + size));
979 CalcBoundingBox (x + size * strlen ((char *)(const char *)text), (long)YSCALE (y));
980 }
981
982
983 void wxPostScriptDC::SetBackground (const wxBrush& brush)
984 {
985 m_backgroundBrush = brush;
986 }
987
988 void wxPostScriptDC::SetLogicalFunction (int WXUNUSED(function))
989 {
990 }
991
992 static const char *wxPostScriptHeaderEllipse = "\
993 /ellipsedict 8 dict def\n\
994 ellipsedict /mtrx matrix put\n\
995 /ellipse\n\
996 { ellipsedict begin\n\
997 /endangle exch def\n\
998 /startangle exch def\n\
999 /yrad exch def\n\
1000 /xrad exch def\n\
1001 /y exch def\n\
1002 /x exch def\n\
1003 /savematrix mtrx currentmatrix def\n\
1004 x y translate\n\
1005 xrad yrad scale\n\
1006 0 0 1 startangle endangle arc\n\
1007 savematrix setmatrix\n\
1008 end\n\
1009 } def\n\
1010 ";
1011
1012 static const char *wxPostScriptHeaderEllipticArc= "\
1013 /ellipticarcdict 8 dict def\n\
1014 ellipticarcdict /mtrx matrix put\n\
1015 /ellipticarc\n\
1016 { ellipticarcdict begin\n\
1017 /do_fill exch def\n\
1018 /endangle exch def\n\
1019 /startangle exch def\n\
1020 /yrad exch def\n\
1021 /xrad exch def \n\
1022 /y exch def\n\
1023 /x exch def\n\
1024 /savematrix mtrx currentmatrix def\n\
1025 x y translate\n\
1026 xrad yrad scale\n\
1027 do_fill { 0 0 moveto } if\n\
1028 0 0 1 startangle endangle arc\n\
1029 savematrix setmatrix\n\
1030 do_fill { fill }{ stroke } ifelse\n\
1031 end\n\
1032 } def\n";
1033
1034 bool wxPostScriptDC::StartDoc (const wxString& message)
1035 {
1036 if (m_filename == "")
1037 {
1038 #ifdef __VMS__
1039 m_filename = "wxtmp.ps";
1040 #else
1041 m_filename = wxGetTempFileName("ps");
1042 #endif
1043 wxThePrintSetupData->SetPrinterFile((char *)(const char *)m_filename);
1044 m_ok = TRUE;
1045 }
1046 else
1047 wxThePrintSetupData->SetPrinterFile((char *)(const char *)m_filename);
1048
1049 #ifdef __VMS__
1050 // steve, 05.09.94
1051 // VMS is sh*t!
1052 m_pstream = new ofstream;
1053 if(fileBuffer) delete[] fileBuffer;
1054 fileBuffer = new char[VMS_BUFSIZ];
1055 m_pstream->setbuf(fileBuffer,VMS_BUFSIZ);
1056 m_pstream->open(wxThePrintSetupData->GetPrinterFile());
1057 #else
1058 m_pstream = new ofstream (wxThePrintSetupData->GetPrinterFile());
1059 #endif
1060 if (!m_pstream || !m_pstream->good())
1061 {
1062 wxMessageBox (_("Cannot open file!"), _("Error"), wxOK);
1063 m_ok = FALSE;
1064 return FALSE;
1065 }
1066 m_ok = TRUE;
1067
1068 SetBrush (*wxBLACK_BRUSH);
1069 SetPen (*wxBLACK_PEN);
1070
1071 wxPageNumber = 1;
1072 m_title = message;
1073 return TRUE;
1074 }
1075
1076
1077 void wxPostScriptDC::EndDoc (void)
1078 {
1079 static char wxPostScriptHeaderReencodeISO1[] =
1080 "\n/reencodeISO {\n"
1081 "dup dup findfont dup length dict begin\n"
1082 "{ 1 index /FID ne { def }{ pop pop } ifelse } forall\n"
1083 "/Encoding ISOLatin1Encoding def\n"
1084 "currentdict end definefont\n"
1085 "} def\n"
1086 "/ISOLatin1Encoding [\n"
1087 "/.notdef/.notdef/.notdef/.notdef/.notdef/.notdef/.notdef/.notdef\n"
1088 "/.notdef/.notdef/.notdef/.notdef/.notdef/.notdef/.notdef/.notdef\n"
1089 "/.notdef/.notdef/.notdef/.notdef/.notdef/.notdef/.notdef/.notdef\n"
1090 "/.notdef/.notdef/.notdef/.notdef/.notdef/.notdef/.notdef/.notdef\n"
1091 "/space/exclam/quotedbl/numbersign/dollar/percent/ampersand/quoteright\n"
1092 "/parenleft/parenright/asterisk/plus/comma/minus/period/slash\n"
1093 "/zero/one/two/three/four/five/six/seven/eight/nine/colon/semicolon\n"
1094 "/less/equal/greater/question/at/A/B/C/D/E/F/G/H/I/J/K/L/M/N\n"
1095 "/O/P/Q/R/S/T/U/V/W/X/Y/Z/bracketleft/backslash/bracketright\n"
1096 "/asciicircum/underscore/quoteleft/a/b/c/d/e/f/g/h/i/j/k/l/m\n"
1097 "/n/o/p/q/r/s/t/u/v/w/x/y/z/braceleft/bar/braceright/asciitilde\n"
1098 "/.notdef/.notdef/.notdef/.notdef/.notdef/.notdef/.notdef/.notdef\n"
1099 "/.notdef/.notdef/.notdef/.notdef/.notdef/.notdef/.notdef/.notdef\n"
1100 "/.notdef/dotlessi/grave/acute/circumflex/tilde/macron/breve\n"
1101 "/dotaccent/dieresis/.notdef/ring/cedilla/.notdef/hungarumlaut\n";
1102
1103 static char wxPostScriptHeaderReencodeISO2[] =
1104 "/ogonek/caron/space/exclamdown/cent/sterling/currency/yen/brokenbar\n"
1105 "/section/dieresis/copyright/ordfeminine/guillemotleft/logicalnot\n"
1106 "/hyphen/registered/macron/degree/plusminus/twosuperior/threesuperior\n"
1107 "/acute/mu/paragraph/periodcentered/cedilla/onesuperior/ordmasculine\n"
1108 "/guillemotright/onequarter/onehalf/threequarters/questiondown\n"
1109 "/Agrave/Aacute/Acircumflex/Atilde/Adieresis/Aring/AE/Ccedilla\n"
1110 "/Egrave/Eacute/Ecircumflex/Edieresis/Igrave/Iacute/Icircumflex\n"
1111 "/Idieresis/Eth/Ntilde/Ograve/Oacute/Ocircumflex/Otilde/Odieresis\n"
1112 "/multiply/Oslash/Ugrave/Uacute/Ucircumflex/Udieresis/Yacute\n"
1113 "/Thorn/germandbls/agrave/aacute/acircumflex/atilde/adieresis\n"
1114 "/aring/ae/ccedilla/egrave/eacute/ecircumflex/edieresis/igrave\n"
1115 "/iacute/icircumflex/idieresis/eth/ntilde/ograve/oacute/ocircumflex\n"
1116 "/otilde/odieresis/divide/oslash/ugrave/uacute/ucircumflex/udieresis\n"
1117 "/yacute/thorn/ydieresis\n"
1118 "] def\n\n";
1119
1120 if (!m_pstream)
1121 return;
1122 if (m_clipping)
1123 {
1124 m_clipping = FALSE;
1125 *m_pstream << "grestore\n";
1126 }
1127
1128 // Will reuse m_pstream for header
1129 #ifdef __VMS__
1130 // see the definition of fileBuffer for explanation
1131 m_pstream->close(); // steve, 05.09.94
1132 if(fileBuffer) delete[] fileBuffer;
1133 #endif
1134 if (m_pstream)
1135 {
1136 delete m_pstream;
1137 m_pstream = NULL;
1138 }
1139
1140 // Write header now
1141 // steve, 05.09.94
1142 #ifdef __VMS__
1143 char *header_file = "header.ps";
1144 #else
1145 char *header_file = wxGetTempFileName("ps");
1146 #endif
1147 m_pstream = new ofstream (header_file);
1148
1149 *m_pstream << "%!PS-Adobe-2.0\n"; /* PostScript magic strings */
1150 *m_pstream << "%%Title: " << (const char *) m_title << "\n";
1151 *m_pstream << "%%Creator: " << wxTheApp->argv[0] << "\n";
1152 // time_t when; time (&when);
1153 // *m_pstream << "%%CreationDate: " << ctime (&when);
1154 *m_pstream << "%%CreationDate: " << wxNow() << "\n";
1155
1156 // User Id information
1157 char userID[256];
1158 if ( wxGetEmailAddress(userID, sizeof(userID)) )
1159 {
1160 *m_pstream << "%%For: " << (char *)userID;
1161 char userName[245];
1162 if (wxGetUserName(userName, sizeof(userName)))
1163 *m_pstream << " (" << (char *)userName << ")";
1164 *m_pstream << "\n";
1165 }
1166 else if ( wxGetUserName(userID, sizeof(userID)) )
1167 {
1168 *m_pstream << "%%For: " << (char *)userID << "\n";
1169 }
1170
1171 // THE FOLLOWING HAS BEEN CONTRIBUTED BY Andy Fyfe <andy@hyperparallel.com>
1172
1173 long wx_printer_translate_x, wx_printer_translate_y;
1174 double wx_printer_scale_x, wx_printer_scale_y;
1175 wxThePrintSetupData->GetPrinterTranslation(&wx_printer_translate_x, &wx_printer_translate_y);
1176 wxThePrintSetupData->GetPrinterScaling(&wx_printer_scale_x, &wx_printer_scale_y);
1177
1178 if (wxThePrintSetupData->GetPrinterOrientation() == PS_LANDSCAPE)
1179 {
1180 *m_pstream << "%%Orientation: Landscape\n";
1181 }
1182 else
1183 {
1184 *m_pstream << "%%Orientation: Portrait\n";
1185 }
1186
1187 // Compute the bounding box. Note that it is in the default user
1188 // coordinate system, thus we have to convert the values.
1189 long llx = (long) ((m_minX+wx_printer_translate_x)*wx_printer_scale_x);
1190 long lly = (long) ((m_minY+wx_printer_translate_y)*wx_printer_scale_y);
1191 long urx = (long) ((m_maxX+wx_printer_translate_x)*wx_printer_scale_x);
1192 long ury = (long) ((m_maxY+wx_printer_translate_y)*wx_printer_scale_y);
1193
1194 #if 0
1195 // If we're landscape, our sense of "x" and "y" is reversed.
1196 if (wxThePrintSetupData->GetPrinterOrientation() == PS_LANDSCAPE)
1197 {
1198 double tmp;
1199 tmp = llx; llx = lly; lly = tmp;
1200 tmp = urx; urx = ury; ury = tmp;
1201
1202 // We need either the two lines that follow, or we need to subtract
1203 // min_x from real_translate_y, which is commented out below.
1204 llx = llx - m_minX*wx_printer_scale_y;
1205 urx = urx - m_minX*wx_printer_scale_y;
1206 }
1207 #endif
1208
1209 // The Adobe specifications call for integers; we round as to make
1210 // the bounding larger.
1211 *m_pstream << "%%BoundingBox: "
1212 << floor(llx) << " " << floor(lly) << " "
1213 << ceil(urx) << " " << ceil(ury) << "\n";
1214 *m_pstream << "%%Pages: " << wxPageNumber - 1 << "\n";
1215 *m_pstream << "%%EndComments\n\n";
1216
1217 // To check the correctness of the bounding box, postscript commands
1218 // to draw a box corresponding to the bounding box are generated below.
1219 // But since we typically don't want to print such a box, the postscript
1220 // commands are generated within comments. These lines appear before any
1221 // adjustment of scale, rotation, or translation, and hence are in the
1222 // default user coordinates.
1223 *m_pstream << "% newpath\n";
1224 *m_pstream << "% " << llx << " " << lly << " moveto\n";
1225 *m_pstream << "% " << urx << " " << lly << " lineto\n";
1226 *m_pstream << "% " << urx << " " << ury << " lineto\n";
1227 *m_pstream << "% " << llx << " " << ury << " lineto closepath stroke\n";
1228
1229 #if 0
1230 // Output scaling
1231 long real_translate_y = wx_printer_translate_y;
1232 if (wxThePrintSetupData->GetPrinterOrientation() == PS_LANDSCAPE)
1233 {
1234 real_translate_y -= m_maxY;
1235 // The following line can be used instead of the adjustment to
1236 // llx and urx above.
1237 // real_translate_y -= m_minX;
1238 *m_pstream << "90 rotate\n";
1239 }
1240
1241 /* Probably don't want this now we have it in EndPage, below.
1242 * We should rationalise the scaling code to one place. JACS, October 1995
1243 * Do we take the next 2 lines out or not?
1244 */
1245
1246 *m_pstream << wx_printer_scale_x << " " << wx_printer_scale_y << " scale\n";
1247 *m_pstream << wx_printer_translate_x << " " << real_translate_y << " translate\n";
1248 #endif
1249
1250 *m_pstream << "%%BeginProlog\n";
1251 *m_pstream << wxPostScriptHeaderEllipse;
1252 *m_pstream << wxPostScriptHeaderEllipticArc;
1253 *m_pstream << wxPostScriptHeaderReencodeISO1;
1254 *m_pstream << wxPostScriptHeaderReencodeISO2;
1255
1256 if (wxPostScriptHeaderSpline)
1257 *m_pstream << wxPostScriptHeaderSpline;
1258 *m_pstream << "%%EndProlog\n";
1259
1260 delete m_pstream;
1261 m_pstream = NULL;
1262
1263 #ifdef __VMS__
1264 char *tmp_file = "tmp.ps";
1265 #else
1266 char *tmp_file = wxGetTempFileName("ps");
1267 #endif
1268
1269 // Paste header Before wx_printer_file
1270 wxConcatFiles (header_file, wxThePrintSetupData->GetPrinterFile(), tmp_file);
1271 wxRemoveFile (header_file);
1272 wxRemoveFile (wxThePrintSetupData->GetPrinterFile());
1273 wxRenameFile(tmp_file, wxThePrintSetupData->GetPrinterFile());
1274
1275 #if defined(__X__) || defined(__WXGTK__)
1276 if (m_ok)
1277 {
1278 switch (wxThePrintSetupData->GetPrinterMode()) {
1279 case PS_PREVIEW:
1280 {
1281 char *argv[3];
1282 argv[0] = wxThePrintSetupData->GetPrintPreviewCommand();
1283 argv[1] = wxThePrintSetupData->GetPrinterFile();
1284 argv[2] = NULL;
1285 wxExecute (argv, TRUE);
1286 wxRemoveFile(wxThePrintSetupData->GetPrinterFile());
1287 }
1288 break;
1289
1290 case PS_PRINTER:
1291 {
1292 char *argv[4];
1293 int argc = 0;
1294 argv[argc++] = wxThePrintSetupData->GetPrinterCommand();
1295
1296 // !SM! If we simply assign to argv[1] here, if printer options
1297 // are blank, we get an annoying and confusing message from lpr.
1298 char * opts = wxThePrintSetupData->GetPrinterOptions();
1299 if (opts && *opts)
1300 argv[argc++] = opts;
1301
1302 argv[argc++] = wxThePrintSetupData->GetPrinterFile();
1303 argv[argc++] = NULL;
1304 wxExecute (argv, TRUE);
1305 wxRemoveFile(wxThePrintSetupData->GetPrinterFile());
1306 }
1307 break;
1308
1309 case PS_FILE:
1310 break;
1311 }
1312 }
1313 #endif
1314 }
1315
1316 void wxPostScriptDC::StartPage (void)
1317 {
1318 if (!m_pstream)
1319 return;
1320 *m_pstream << "%%Page: " << wxPageNumber++ << "\n";
1321 // *m_pstream << "matrix currentmatrix\n";
1322
1323 // Added by Chris Breeze
1324
1325 // Each page starts with an "initgraphics" which resets the
1326 // transformation and so we need to reset the origin
1327 // (and rotate the page for landscape printing)
1328 m_scaleFactor = 1.0;
1329 m_logicalOriginX = 0;
1330 m_logicalOriginY = 0;
1331
1332 // Output scaling
1333 long translate_x, translate_y;
1334 double scale_x, scale_y;
1335 wxThePrintSetupData->GetPrinterTranslation(&translate_x, &translate_y);
1336 wxThePrintSetupData->GetPrinterScaling(&scale_x, &scale_y);
1337
1338 if (wxThePrintSetupData->GetPrinterOrientation() == PS_LANDSCAPE)
1339 {
1340 translate_y -= GetYOrigin();
1341 *m_pstream << "90 rotate\n";
1342 }
1343
1344 *m_pstream << scale_x << " " << scale_y << " scale\n";
1345 *m_pstream << translate_x << " " << translate_y << " translate\n";
1346 }
1347
1348 void wxPostScriptDC::EndPage (void)
1349 {
1350 if (!m_pstream)
1351 return;
1352 *m_pstream << "showpage\n";
1353
1354 // Removed by Chris Breeze
1355 #if 0
1356 *m_pstream << "setmatrix\n";
1357
1358 // THE FOLLOWING HAS BEEN CONTRIBUTED BY Andy Fyfe <andy@hyperparallel.com>
1359
1360 long wx_printer_translate_x, wx_printer_translate_y;
1361 double wx_printer_scale_x, wx_printer_scale_y;
1362 wxThePrintSetupData->GetPrinterTranslation(&wx_printer_translate_x, &wx_printer_translate_y);
1363 wxThePrintSetupData->GetPrinterScaling(&wx_printer_scale_x, &wx_printer_scale_y);
1364
1365 // Compute the bounding box. Note that it is in the default user
1366 // coordinate system, thus we have to convert the values.
1367 long llx = (m_minX+wx_printer_translate_x)*wx_printer_scale_x;
1368 long lly = (m_minY+wx_printer_translate_y)*wx_printer_scale_y;
1369 long urx = (m_maxX+wx_printer_translate_x)*wx_printer_scale_x;
1370 long ury = (m_maxY+wx_printer_translate_y)*wx_printer_scale_y;
1371
1372 // If we're landscape, our sense of "x" and "y" is reversed.
1373 if (wxThePrintSetupData->GetPrinterOrientation() == PS_LANDSCAPE)
1374 {
1375 long tmp;
1376 tmp = llx; llx = lly; lly = tmp;
1377 tmp = urx; urx = ury; ury = tmp;
1378
1379 // We need either the two lines that follow, or we need to subtract
1380 // m_minX from real_translate_y, which is commented out below.
1381 llx = llx - m_minX*wx_printer_scale_y;
1382 urx = urx - m_minX*wx_printer_scale_y;
1383 }
1384
1385 // Output scaling
1386 long real_translate_y = wx_printer_translate_y;
1387 if (wxThePrintSetupData->GetPrinterOrientation() == PS_LANDSCAPE)
1388 {
1389 real_translate_y -= m_maxY;
1390 // The following line can be used instead of the adjustment to
1391 // llx and urx above.
1392 // real_translate_y -= m_minX;
1393 *m_pstream << "90 rotate\n";
1394 }
1395
1396 *m_pstream << wx_printer_scale_x << " " << wx_printer_scale_y << " scale\n";
1397 *m_pstream << wx_printer_translate_x << " " << real_translate_y << " translate\n";
1398 #endif
1399 }
1400
1401 /* MATTHEW: Implement Blit: */
1402 /* MATTHEW: [4] Re-wrote to use colormap */
1403 bool wxPostScriptDC::
1404 Blit (long xdest, long ydest, long fwidth, long fheight,
1405 wxDC *source, long xsrc, long ysrc, int WXUNUSED(rop), bool WXUNUSED(useMask))
1406 {
1407 long width, height, x, y;
1408
1409 #if !defined(__X__) && !defined(__WXGTK__)
1410 return FALSE;
1411 #endif
1412
1413 if (!source->IsKindOf(CLASSINFO(wxPaintDC))) return FALSE;
1414
1415 width = (long)floor(fwidth);
1416 height = (long)floor(fheight);
1417 x = (long)floor(xsrc);
1418 y = (long)floor(ysrc);
1419
1420 /* PostScript setup: */
1421 *m_pstream << "gsave\n";
1422 *m_pstream << xdest << " " << YSCALE(ydest + fheight) << " translate\n";
1423 *m_pstream << fwidth << " " << fheight << " scale\n";
1424 *m_pstream << "/DataString " << width << " string def\n";
1425 *m_pstream << width << " " << height << " 8 [ ";
1426 *m_pstream << width << " 0 0 " << (-height) << " 0 " << height;
1427 *m_pstream << " ]\n{\n";
1428 *m_pstream << " currentfile DataString readhexstring pop\n";
1429 *m_pstream << "} bind image\n";
1430
1431 #if defined(__X__) || defined(__WXGTK__)
1432
1433 /* Output data as hex digits: */
1434 Display *d;
1435 Colormap cm;
1436 XImage *image;
1437 long j, i;
1438 char s[3];
1439
1440 #ifdef __WXGTK__
1441
1442 d = gdk_display;
1443 cm = ((GdkColormapPrivate*)gdk_colormap_get_system())->xcolormap;
1444 GdkWindow *gwin = ((wxClientDC*)source)->GetWindow();
1445 image = XGetImage(d, ((GdkWindowPrivate*)gwin)->xwindow, x, y, width, height, AllPlanes, ZPixmap);
1446
1447 #else
1448
1449 #ifdef __WXMOTIF__
1450 d = source->display;
1451 #else
1452 d = wxGetDisplay();
1453 #endif
1454
1455 cm = wxGetMainColormap(d);
1456 image = XGetImage(d, source->pixmap, x, y, width, height, AllPlanes, ZPixmap);
1457
1458 #endif
1459
1460
1461 s[2] = 0;
1462
1463 #define CM_CACHE_SIZE 256
1464 unsigned long cachesrc[CM_CACHE_SIZE];
1465 int cachedest[CM_CACHE_SIZE], cache_pos = 0, all_cache = FALSE;
1466
1467 for (j = 0; j < height; j++) {
1468 for (i = 0; i < width; i++) {
1469 XColor xcol;
1470 unsigned long spixel;
1471 int pixel, k;
1472 const unsigned short MAX_COLOR = 0xFFFF;
1473
1474 spixel = XGetPixel(image, i, j);
1475
1476 for (k = cache_pos; k--; )
1477 if (cachesrc[k] == spixel) {
1478 pixel = cachedest[k];
1479 goto install;
1480 }
1481 if (all_cache)
1482 for (k = CM_CACHE_SIZE; k-- > cache_pos; )
1483 if (cachesrc[k] == spixel) {
1484 pixel = cachedest[k];
1485 goto install;
1486 }
1487
1488 cachesrc[cache_pos] = xcol.pixel = spixel;
1489 XQueryColor(d, cm, &xcol);
1490
1491 long r, g, b;
1492
1493 r = (long)((double)(xcol.red) / MAX_COLOR);
1494 g = (long)((double)(xcol.green) / MAX_COLOR);
1495 b = (long)((double)(xcol.blue) / MAX_COLOR);
1496
1497 pixel = (int)(255 * sqrt(((r * r) + (g * g) + (b * b)) / 3));
1498
1499 cachedest[cache_pos] = pixel;
1500
1501 if (++cache_pos >= CM_CACHE_SIZE) {
1502 cache_pos = 0;
1503 all_cache = TRUE;
1504 }
1505
1506 install:
1507 int h, l;
1508
1509 h = (pixel >> 4) & 0xF;
1510 l = pixel & 0xF;
1511
1512 if (h <= 9)
1513 s[0] = '0' + h;
1514 else
1515 s[0] = 'a' + (h - 10);
1516 if (l <= 9)
1517 s[1] = '0' + l;
1518 else
1519 s[1] = 'a' + (l - 10);
1520
1521 *m_pstream << s;
1522 }
1523 *m_pstream << "\n";
1524 }
1525
1526 XDestroyImage(image);
1527 #endif
1528
1529 *m_pstream << "grestore\n";
1530
1531 CalcBoundingBox(xdest, (long)YSCALE(ydest));
1532 CalcBoundingBox(xdest + fwidth, (long)YSCALE(ydest + fheight));
1533
1534 return TRUE;
1535 }
1536
1537 long wxPostScriptDC::GetCharHeight (void)
1538 {
1539 if (m_font.Ok())
1540 return m_font.GetPointSize ();
1541 else
1542 return 12;
1543 }
1544
1545 void wxPostScriptDC::GetTextExtent (const wxString& string, long *x, long *y,
1546 long *descent, long *externalLeading, wxFont *theFont,
1547 bool WXUNUSED(use16))
1548 {
1549 wxFont *fontToUse = theFont;
1550 if (!fontToUse)
1551 fontToUse = (wxFont*) &m_font;
1552
1553 if (!m_pstream)
1554 return;
1555 #if !USE_AFM_FOR_POSTSCRIPT
1556 // Provide a VERY rough estimate (avoid using it)
1557 // Chris Breeze 5/11/97: produces accurate results for mono-spaced
1558 // font such as Courier (aka wxMODERN)
1559 int height = 12;
1560 if (fontToUse)
1561 {
1562 height = fontToUse->GetPointSize();
1563 }
1564 *x = strlen (string) * height * 72 / 120;
1565 *y = (long) (height * 1.32); // allow for descender
1566
1567 if (descent)
1568 *descent = 0;
1569 if (externalLeading)
1570 *externalLeading = 0;
1571 #else
1572 // +++++ start of contributed code +++++
1573
1574 // ************************************************************
1575 // method for calculating string widths in postscript:
1576 // read in the AFM (adobe font metrics) file for the
1577 // actual font, parse it and extract the character widths
1578 // and also the descender. this may be improved, but for now
1579 // it works well. the AFM file is only read in if the
1580 // font is changed. this may be chached in the future.
1581 // calls to GetTextExtent with the font unchanged are rather
1582 // efficient!!!
1583 //
1584 // for each font and style used there is an AFM file necessary.
1585 // currently i have only files for the roman font family.
1586 // i try to get files for the other ones!
1587 //
1588 // CAVE: the size of the string is currently always calculated
1589 // in 'points' (1/72 of an inch). this should later on be
1590 // changed to depend on the mapping mode.
1591 // CAVE: the path to the AFM files must be set before calling this
1592 // function. this is usually done by a call like the following:
1593 // wxSetAFMPath("d:\\wxw161\\afm\\");
1594 //
1595 // example:
1596 //
1597 // wxPostScriptDC dc(NULL, TRUE);
1598 // if (dc.Ok()){
1599 // wxSetAFMPath("d:\\wxw161\\afm\\");
1600 // dc.StartDoc("Test");
1601 // dc.StartPage();
1602 // long w,h;
1603 // dc.SetFont(new wxFont(10, wxROMAN, wxNORMAL, wxNORMAL));
1604 // dc.GetTextExtent("Hallo",&w,&h);
1605 // dc.EndPage();
1606 // dc.EndDoc();
1607 // }
1608 //
1609 // by steve (stefan.hammes@urz.uni-heidelberg.de)
1610 // created: 10.09.94
1611 // updated: 14.05.95
1612
1613 assert(fontToUse && "void wxPostScriptDC::GetTextExtent: no font defined");
1614 assert(x && "void wxPostScriptDC::GetTextExtent: x == NULL");
1615 assert(y && "void wxPostScriptDC::GetTextExtent: y == NULL");
1616
1617 // these static vars are for storing the state between calls
1618 static int lastFamily= INT_MIN;
1619 static int lastSize= INT_MIN;
1620 static int lastStyle= INT_MIN;
1621 static int lastWeight= INT_MIN;
1622 static int lastDescender = INT_MIN;
1623 static int lastWidths[256]; // widths of the characters
1624
1625 // get actual parameters
1626 const int Family = fontToUse->GetFamily();
1627 const int Size = fontToUse->GetPointSize();
1628 const int Style = fontToUse->GetStyle();
1629 const int Weight = fontToUse->GetWeight();
1630
1631 // if we have another font, read the font-metrics
1632 if(Family!=lastFamily||Size!=lastSize||Style!=lastStyle||Weight!=lastWeight){
1633 // store actual values
1634 lastFamily = Family;
1635 lastSize = Size;
1636 lastStyle = Style;
1637 lastWeight = Weight;
1638
1639 // read in new font metrics **************************************
1640
1641 // 1. construct filename ******************************************
1642 /* MATTHEW: [2] Use wxTheFontNameDirectory */
1643 char *name;
1644
1645 // Julian - we'll need to do this a different way now we've removed the
1646 // font directory system. Must find Stefan's original code.
1647
1648 name = wxTheFontNameDirectory.GetAFMName(Family, Weight, Style);
1649 if (!name)
1650 name = "unknown";
1651
1652 // get the directory of the AFM files
1653 char afmName[256];
1654 afmName[0] = 0;
1655 if (wxGetAFMPath())
1656 strcpy(afmName,wxGetAFMPath());
1657
1658 // 2. open and process the file **********************************
1659
1660 // a short explanation of the AFM format:
1661 // we have for each character a line, which gives its size
1662 // e.g.:
1663 //
1664 // C 63 ; WX 444 ; N question ; B 49 -14 395 676 ;
1665 //
1666 // that means, we have a character with ascii code 63, and width
1667 // (444/1000 * fontSize) points.
1668 // the other data is ignored for now!
1669 //
1670 // when the font has changed, we read in the right AFM file and store the
1671 // character widths in an array, which is processed below (see point 3.).
1672
1673 // new elements JC Sun Aug 25 23:21:44 MET DST 1996
1674
1675
1676 strcat(afmName,name);
1677 strcat(afmName,".afm");
1678 FILE *afmFile = fopen(afmName,"r");
1679 if(afmFile==NULL){
1680 wxDebugMsg("GetTextExtent: can't open AFM file '%s'\n",afmName);
1681 wxDebugMsg(" using approximate values\n");
1682 int i;
1683 for (i=0; i<256; i++) lastWidths[i] = 500; // an approximate value
1684 lastDescender = -150; // dito.
1685 }else{
1686 int i;
1687 // init the widths array
1688 for(i=0; i<256; i++) lastWidths[i]= INT_MIN;
1689 // some variables for holding parts of a line
1690 char cString[10],semiString[10],WXString[10],descString[20];
1691 char upString[30], utString[30], encString[50];
1692 char line[256];
1693 int ascii,cWidth;
1694 // read in the file and parse it
1695 while(fgets(line,sizeof(line),afmFile)!=NULL){
1696 // A.) check for descender definition
1697 if(strncmp(line,"Descender",9)==0){
1698 if((sscanf(line,"%s%d",descString,&lastDescender)!=2)
1699 || (strcmp(descString,"Descender")!=0)) {
1700 wxDebugMsg("AFM-file '%s': line '%s' has error (bad descender)\n",
1701 afmName,line);
1702 }
1703 }
1704 // JC 1.) check for UnderlinePosition
1705 else if(strncmp(line,"UnderlinePosition",17)==0){
1706 if((sscanf(line,"%s%lf",upString,&UnderlinePosition)!=2)
1707 || (strcmp(upString,"UnderlinePosition")!=0)) {
1708 wxDebugMsg("AFM-file '%s': line '%s' has error (bad UnderlinePosition)\n",
1709 afmName,line);
1710 }
1711 }
1712 // JC 2.) check for UnderlineThickness
1713 else if(strncmp(line,"UnderlineThickness",18)==0){
1714 if((sscanf(line,"%s%lf",utString,&UnderlineThickness)!=2)
1715 || (strcmp(utString,"UnderlineThickness")!=0)) {
1716 wxDebugMsg("AFM-file '%s': line '%s' has error (bad UnderlineThickness)\n",
1717 afmName,line);
1718 }
1719 }
1720 // JC 3.) check for EncodingScheme
1721 else if(strncmp(line,"EncodingScheme",14)==0){
1722 if((sscanf(line,"%s%s",utString,encString)!=2)
1723 || (strcmp(utString,"EncodingScheme")!=0)) {
1724 wxDebugMsg("AFM-file '%s': line '%s' has error (bad EncodingScheme)\n",
1725 afmName,line);
1726 }
1727 else if (strncmp(encString, "AdobeStandardEncoding", 21))
1728 {
1729 wxDebugMsg("AFM-file '%s': line '%s' has error (unsupported EncodingScheme %s)\n",
1730 afmName,line, encString);
1731 }
1732 }
1733 // B.) check for char-width
1734 else if(strncmp(line,"C ",2)==0){
1735 if(sscanf(line,"%s%d%s%s%d",
1736 cString,&ascii,semiString,WXString,&cWidth)!=5){
1737 wxDebugMsg("AFM-file '%s': line '%s' has an error (bad character width)\n",afmName,line);
1738 }
1739 if(strcmp(cString,"C")!=0 || strcmp(semiString,";")!=0 ||
1740 strcmp(WXString,"WX")!=0){
1741 wxDebugMsg("AFM-file '%s': line '%s' has a format error\n",afmName,line);
1742 }
1743 //printf(" char '%c'=%d has width '%d'\n",ascii,ascii,cWidth);
1744 if(ascii>=0 && ascii<256){
1745 lastWidths[ascii] = cWidth; // store width
1746 }else{
1747 /* MATTHEW: this happens a lot; don't print an error */
1748 // wxDebugMsg("AFM-file '%s': ASCII value %d out of range\n",afmName,ascii);
1749 }
1750 }
1751 // C.) ignore other entries.
1752 }
1753 fclose(afmFile);
1754 }
1755 // hack to compute correct values for german 'Umlaute'
1756 // the correct way would be to map the character names
1757 // like 'adieresis' to corresp. positions of ISOEnc and read
1758 // these values from AFM files, too. Maybe later ...
1759 lastWidths[196] = lastWidths['A']; // Ä
1760 lastWidths[228] = lastWidths['a']; // ä
1761 lastWidths[214] = lastWidths['O']; // Ö
1762 lastWidths[246] = lastWidths['o']; // ö
1763 lastWidths[220] = lastWidths['U']; // Ü
1764 lastWidths[252] = lastWidths['u']; // ü
1765 lastWidths[223] = lastWidths[251]; // ß
1766 }
1767
1768 // JC: calculate UnderlineThickness/UnderlinePosition
1769 UnderlinePosition = UnderlinePosition * fontToUse->GetPointSize() / 1000.0f;
1770 UnderlineThickness = UnderlineThickness * fontToUse->GetPointSize() / 1000.0f * m_scaleFactor;
1771
1772 // 3. now the font metrics are read in, calc size *******************
1773 // this is done by adding the widths of the characters in the
1774 // string. they are given in 1/1000 of the size!
1775
1776 long widthSum=0;
1777 long height=Size; // by default
1778 unsigned char *p;
1779 for(p=(unsigned char *)(const char *)string; *p; p++){
1780 if(lastWidths[*p]== INT_MIN){
1781 wxDebugMsg("GetTextExtent: undefined width for character '%c' (%d)\n",
1782 *p,*p);
1783 widthSum += (long)(lastWidths[' ']/1000.0F * Size); // assume space
1784 }else{
1785 widthSum += (long)((lastWidths[*p]/1000.0F)*Size);
1786 }
1787 }
1788 // add descender to height (it is usually a negative value)
1789 if(lastDescender!=INT_MIN){
1790 height += (long)(((-lastDescender)/1000.0F) * Size); /* MATTHEW: forgot scale */
1791 }
1792
1793 // return size values
1794 *x = widthSum;
1795 *y = height;
1796
1797 // return other parameters
1798 if (descent){
1799 if(lastDescender!=INT_MIN){
1800 *descent = (long)(((-lastDescender)/1000.0F) * Size); /* MATTHEW: forgot scale */
1801 }else{
1802 *descent = 0;
1803 }
1804 }
1805
1806 // currently no idea how to calculate this!
1807 // if (externalLeading) *externalLeading = 0;
1808 if (externalLeading)
1809 *externalLeading = 0;
1810
1811 // ----- end of contributed code -----
1812 #endif
1813 }
1814
1815 void wxPostScriptDC::DrawOpenSpline( wxList *points )
1816 {
1817 double a, b, c, d, x1, y1, x2, y2, x3, y3;
1818 wxPoint *p, *q;
1819
1820 wxNode *node = points->First();
1821 p = (wxPoint *)node->Data();
1822
1823 x1 = p->x; y1 = p->y;
1824
1825 node = node->Next();
1826 p = (wxPoint *)node->Data();
1827 c = p->x; d = p->y;
1828 x3 = a = (double)(x1 + c) / 2;
1829 y3 = b = (double)(y1 + d) / 2;
1830
1831 *(GetStream()) << "newpath " << x1 << " " << GetYOrigin() - y1 << " moveto " << x3 << " " << GetYOrigin() - y3;
1832 *(GetStream()) << " lineto\n";
1833 CalcBoundingBox( (long)x1, (long)(GetYOrigin() - y1));
1834 CalcBoundingBox( (long)x3, (long)(GetYOrigin() - y3));
1835
1836 while ((node = node->Next()) != NULL)
1837 {
1838 q = (wxPoint *)node->Data();
1839
1840 x1 = x3; y1 = y3;
1841 x2 = c; y2 = d;
1842 c = q->x; d = q->y;
1843 x3 = (double)(x2 + c) / 2;
1844 y3 = (double)(y2 + d) / 2;
1845 *(GetStream()) << x1 << " " << GetYOrigin() - y1 << " " << x2 << " " << GetYOrigin() - y2 << " ";
1846 *(GetStream()) << x3 << " " << GetYOrigin() - y3 << " DrawSplineSection\n";
1847
1848 CalcBoundingBox( (long)x1, (long)(GetYOrigin() - y1));
1849 CalcBoundingBox( (long)x3, (long)(GetYOrigin() - y3));
1850 }
1851 /*
1852 * At this point, (x2,y2) and (c,d) are the position of the
1853 * next-to-last and last point respectively, in the point list
1854 */
1855 *(GetStream()) << c << " " << GetYOrigin() - d << " lineto stroke\n";
1856 }
1857
1858 long wxPostScriptDC::GetCharWidth (void)
1859 {
1860 // Chris Breeze: reasonable approximation using wxMODERN/Courier
1861 return (long) (GetCharHeight() * 72.0 / 120.0);
1862 }
1863
1864
1865 void wxPostScriptDC::SetMapMode (int mode)
1866 {
1867 m_mappingMode = mode;
1868 SetLogicalOrigin(0, 0);
1869 if(m_mappingMode == MM_METRIC)
1870 {
1871 SetUserScale(72.0f / 25.4f, 72.0f / 25.4f);
1872 }
1873 else
1874 {
1875 SetUserScale(1.0, 1.0);
1876 }
1877 return;
1878 }
1879
1880 /*
1881 * Set the logical origin.
1882 * Actually we are setting the printer's origin and since
1883 * postscript transformations are cumulative we need to reset
1884 * the previous origin. We also need to allow for the user scale
1885 * factor (which affects printer coords but not logical)
1886 */
1887 void wxPostScriptDC::SetLogicalOrigin(long x, long y)
1888 {
1889 if (m_scaleFactor)
1890 {
1891 long xOffset = (long) ((m_logicalOriginX - x) / m_scaleFactor);
1892 long yOffset = (long) ((y - m_logicalOriginY) / m_scaleFactor);
1893
1894 if (m_pstream)
1895 {
1896 *m_pstream << xOffset << " " << yOffset << " translate\n";
1897 }
1898 }
1899 m_logicalOriginX = x;
1900 m_logicalOriginY = y;
1901 }
1902
1903 /*
1904 * Obsolete - now performed by SetUserScale() and SetLogicalOrigin()
1905 */
1906 void
1907 wxPostScriptDC::SetupCTM()
1908 {
1909 }
1910
1911 /*
1912 * Set the user scale.
1913 * NB: Postscript transformations are cumulative and so we
1914 * need to take into account the current scale. Also, this
1915 * affects the logical origin
1916 */
1917 void wxPostScriptDC::SetUserScale (double x, double y)
1918 {
1919 // reset logical origin
1920 long xOrg = m_logicalOriginX;
1921 long yOrg = m_logicalOriginY;
1922 SetLogicalOrigin(0, 0);
1923
1924 // set new scale factor
1925 double factor = x;
1926 if (m_scaleFactor)
1927 {
1928 factor /= m_scaleFactor;
1929 }
1930 if (m_pstream)
1931 {
1932 *m_pstream << factor << " " << factor << " scale\n";
1933 }
1934
1935 // set logical origin at new scale
1936 SetLogicalOrigin(xOrg, yOrg);
1937
1938 m_userScaleX = x;
1939 m_userScaleY = y;
1940
1941 m_scaleFactor = m_userScaleX;
1942 }
1943
1944 long wxPostScriptDC::DeviceToLogicalX (int x) const
1945 {
1946 return x;
1947 }
1948
1949 long wxPostScriptDC::DeviceToLogicalXRel (int x) const
1950 {
1951 return x;
1952 }
1953
1954 long wxPostScriptDC::DeviceToLogicalY (int y) const
1955 {
1956 return y;
1957 }
1958
1959 long wxPostScriptDC::DeviceToLogicalYRel (int y) const
1960 {
1961 return y;
1962 }
1963
1964 long wxPostScriptDC::LogicalToDeviceX (long x) const
1965 {
1966 return x;
1967 }
1968
1969 long wxPostScriptDC::LogicalToDeviceXRel (long x) const
1970 {
1971 return x;
1972 }
1973
1974 long wxPostScriptDC::LogicalToDeviceY (long y) const
1975 {
1976 return y;
1977 }
1978
1979 long wxPostScriptDC::LogicalToDeviceYRel (long y) const
1980 {
1981 return y;
1982 }
1983
1984 void wxPostScriptDC::GetSize(int* width, int* height) const
1985 {
1986 char *paperType = wxThePrintSetupData->GetPaperName();
1987 if (!paperType)
1988 paperType = "A4 210 x 297 mm";
1989
1990 wxPrintPaperType *paper = wxThePrintPaperDatabase->FindPaperType(paperType);
1991 if (!paper)
1992 paper = wxThePrintPaperDatabase->FindPaperType("A4 210 x 297 mm");
1993 if (paper)
1994 {
1995 *width = paper->widthPixels;
1996 *height = paper->heightPixels;
1997 }
1998 else
1999 {
2000 *width = 1000;
2001 *height = 1000;
2002 }
2003 }
2004
2005 void wxPostScriptDC::GetSizeMM(long *width, long *height) const
2006 {
2007 char *paperType = wxThePrintSetupData->GetPaperName();
2008 if (!paperType)
2009 paperType = "A4 210 x 297 mm";
2010
2011 wxPrintPaperType *paper = wxThePrintPaperDatabase->FindPaperType(paperType);
2012 if (!paper)
2013 paper = wxThePrintPaperDatabase->FindPaperType("A4 210 x 297 mm");
2014 if (paper)
2015 {
2016 *width = paper->widthMM;
2017 *height = paper->heightMM;
2018 }
2019 else
2020 {
2021 *width = 1000;
2022 *height = 1000;
2023 }
2024 }
2025
2026 void wxPostScriptDC::CalcBoundingBox(long x, long y)
2027 {
2028 long device_x = (long) (x - m_logicalOriginX * m_scaleFactor);
2029 long device_y = (long) (y + m_logicalOriginY * m_scaleFactor);
2030
2031 if (device_x < m_minX) m_minX = device_x;
2032 if (device_y < m_minY) m_minY = device_y;
2033 if (device_x > m_maxX) m_maxX = device_x;
2034 if (device_y > m_maxY) m_maxY = device_y;
2035 }
2036
2037 IMPLEMENT_CLASS(wxPostScriptPrintDialog, wxDialog)
2038
2039 wxPostScriptPrintDialog::wxPostScriptPrintDialog (wxWindow *parent, const wxString& title,
2040 const wxPoint& pos, const wxSize& size, long style):
2041 wxDialog(parent, -1, title, pos, size, style)
2042 {
2043 wxBeginBusyCursor();
2044 char buf[100];
2045
2046 wxButton *okBut = new wxButton (this, wxID_OK, _("OK"), wxPoint(5, 5));
2047 (void) new wxButton (this, wxID_CANCEL, _("Cancel"), wxPoint(40, 5));
2048 okBut->SetDefault();
2049
2050 int yPos = 30;
2051
2052 #ifdef __X__
2053 (void) new wxStaticText(this, -1, "Printer Command: ", wxPoint(5, yPos));
2054 wxTextCtrl *text_prt = new wxTextCtrl(this, wxID_PRINTER_COMMAND, wxThePrintSetupData->GetPrinterCommand(), wxPoint(100, yPos), wxSize(100, -1));
2055
2056 (void) new wxStaticText(this, -1, "Printer Options: ", wxPoint(210, yPos));
2057 wxTextCtrl *text0 = new wxTextCtrl(this, wxID_PRINTER_OPTIONS, wxThePrintSetupData->GetPrinterOptions(), wxPoint(305, -1), wxSize(150, -1));
2058
2059 yPos += 25;
2060 #endif
2061
2062 wxString orientation[2];
2063 orientation[0] = "Portrait";
2064 orientation[1] = "Landscape";
2065
2066 wxRadioBox *radio0 = new wxRadioBox(this, wxID_PRINTER_ORIENTATION, "Orientation: ", wxPoint(5, yPos), wxSize(-1,-1),
2067 2,orientation,2,0);
2068 radio0->SetSelection((int)wxThePrintSetupData->GetPrinterOrientation() - 1);
2069
2070 // @@@ Configuration hook
2071 if (wxThePrintSetupData->GetPrintPreviewCommand() == NULL)
2072 wxThePrintSetupData->SetPrintPreviewCommand(PS_VIEWER_PROG);
2073
2074 wxGetResource ("wxWindows", "PSView", &wxThePrintSetupData->previewCommand);
2075
2076 wxString print_modes[3];
2077 print_modes[0] = "Send to Printer";
2078 print_modes[1] = "Print to File";
2079 print_modes[2] = "Preview Only";
2080
2081 int features = (wxThePrintSetupData->GetPrintPreviewCommand() && *wxThePrintSetupData->GetPrintPreviewCommand()) ? 3 : 2;
2082 wxRadioBox *radio1 = new wxRadioBox(this, wxID_PRINTER_MODES, "PostScript:",
2083 wxPoint(150, yPos), wxSize(-1,-1), features, print_modes, features, 0);
2084
2085 #ifdef __WXMSW__
2086 radio1->Enable(0, FALSE);
2087 if (wxThePrintSetupData->GetPrintPreviewCommand() && *wxThePrintSetupData->GetPrintPreviewCommand())
2088 radio1->Enable(2, FALSE);
2089 #endif
2090
2091 radio1->SetSelection((int)wxThePrintSetupData->GetPrinterMode());
2092
2093 long wx_printer_translate_x, wx_printer_translate_y;
2094 double wx_printer_scale_x, wx_printer_scale_y;
2095 wxThePrintSetupData->GetPrinterTranslation(&wx_printer_translate_x, &wx_printer_translate_y);
2096 wxThePrintSetupData->GetPrinterScaling(&wx_printer_scale_x, &wx_printer_scale_y);
2097
2098 sprintf (buf, "%.2f", wx_printer_scale_x);
2099
2100 yPos += 60;
2101
2102 (void) new wxStaticText(this, -1, "X Scaling", wxPoint(5, yPos));
2103 /* wxTextCtrl *text1 = */ (void) new wxTextCtrl(this, wxID_PRINTER_X_SCALE, buf, wxPoint(100, -1), wxSize(100, -1));
2104
2105 sprintf (buf, "%.2f", wx_printer_scale_y);
2106 (void) new wxStaticText(this, -1, "Y Scaling", wxPoint(120, yPos));
2107 /* wxTextCtrl *text2 = */ (void) new wxTextCtrl(this, wxID_PRINTER_Y_SCALE, buf, wxPoint(230, -1), wxSize(100, -1));
2108
2109 yPos += 25;
2110
2111 (void) new wxStaticText(this, -1, "X Translation", wxPoint(5, yPos));
2112 sprintf (buf, "%.2ld", wx_printer_translate_x);
2113 /* wxTextCtrl *text3 = */ (void) new wxTextCtrl(this, wxID_PRINTER_X_TRANS, buf, wxPoint(100, -1), wxSize(100, -1));
2114
2115 (void) new wxStaticText(this, -1, "Y Translation", wxPoint(120, yPos));
2116 sprintf (buf, "%.2ld", wx_printer_translate_y);
2117 /* wxTextCtrl *text4 = */ (void) new wxTextCtrl(this, wxID_PRINTER_Y_TRANS, buf, wxPoint(230, -1), wxSize(100, -1));
2118
2119 Fit ();
2120
2121 wxEndBusyCursor();
2122 }
2123
2124 int wxPostScriptPrintDialog::ShowModal (void)
2125 {
2126 if ( wxDialog::ShowModal() == wxID_OK )
2127 {
2128 // wxTextCtrl *text0 = (wxTextCtrl *)FindWindow(wxID_PRINTER_OPTIONS);
2129 wxTextCtrl *text1 = (wxTextCtrl *)FindWindow(wxID_PRINTER_X_SCALE);
2130 wxTextCtrl *text2 = (wxTextCtrl *)FindWindow(wxID_PRINTER_Y_SCALE);
2131 wxTextCtrl *text3 = (wxTextCtrl *)FindWindow(wxID_PRINTER_X_TRANS);
2132 wxTextCtrl *text4 = (wxTextCtrl *)FindWindow(wxID_PRINTER_Y_TRANS);
2133 // wxTextCtrl *text_prt = (wxTextCtrl *)FindWindow(wxID_PRINTER_COMMAND);
2134 wxRadioBox *radio0 = (wxRadioBox *)FindWindow(wxID_PRINTER_ORIENTATION);
2135 wxRadioBox *radio1 = (wxRadioBox *)FindWindow(wxID_PRINTER_MODES);
2136
2137 StringToDouble (WXSTRINGCAST text1->GetValue (), &wxThePrintSetupData->printerScaleX);
2138 StringToDouble (WXSTRINGCAST text2->GetValue (), &wxThePrintSetupData->printerScaleY);
2139 StringToLong (WXSTRINGCAST text3->GetValue (), &wxThePrintSetupData->printerTranslateX);
2140 StringToLong (WXSTRINGCAST text4->GetValue (), &wxThePrintSetupData->printerTranslateY);
2141
2142 #ifdef __X__
2143 wxThePrintSetupData->SetPrinterOptions(WXSTRINGCAST text0->GetValue ());
2144 wxThePrintSetupData->SetPrinterCommand(WXSTRINGCAST text_prt->GetValue ());
2145 #endif
2146
2147 wxThePrintSetupData->SetPrinterOrientation((radio0->GetSelection() == 1 ? PS_LANDSCAPE : PS_PORTRAIT));
2148
2149 // C++ wants this
2150 switch ( radio1->GetSelection() ) {
2151 case PS_PREVIEW: wxThePrintSetupData->SetPrinterMode(PS_PREVIEW); break;
2152 case PS_FILE: wxThePrintSetupData->SetPrinterMode(PS_FILE); break;
2153 case PS_PRINTER: wxThePrintSetupData->SetPrinterMode(PS_PRINTER); break;
2154 }
2155 return wxID_OK;
2156 }
2157 return wxID_CANCEL;
2158 }
2159
2160 // PostScript printer settings
2161 // RETAINED FOR BACKWARD COMPATIBILITY
2162 void wxSetPrinterCommand(char *cmd)
2163 {
2164 wxThePrintSetupData->SetPrinterCommand(cmd);
2165 }
2166
2167 void wxSetPrintPreviewCommand(char *cmd)
2168 {
2169 wxThePrintSetupData->SetPrintPreviewCommand(cmd);
2170 }
2171
2172 void wxSetPrinterOptions(char *flags)
2173 {
2174 wxThePrintSetupData->SetPrinterOptions(flags);
2175 }
2176
2177 void wxSetPrinterFile(char *f)
2178 {
2179 wxThePrintSetupData->SetPrinterFile(f);
2180 }
2181
2182 void wxSetPrinterOrientation(int orient)
2183 {
2184 wxThePrintSetupData->SetPrinterOrientation(orient);
2185 }
2186
2187 void wxSetPrinterScaling(double x, double y)
2188 {
2189 wxThePrintSetupData->SetPrinterScaling(x, y);
2190 }
2191
2192 void wxSetPrinterTranslation(long x, long y)
2193 {
2194 wxThePrintSetupData->SetPrinterTranslation(x, y);
2195 }
2196
2197 // 1 = Preview, 2 = print to file, 3 = send to printer
2198 void wxSetPrinterMode(int mode)
2199 {
2200 wxThePrintSetupData->SetPrinterMode(mode);
2201 }
2202
2203 void wxSetAFMPath(char *f)
2204 {
2205 wxThePrintSetupData->SetAFMPath(f);
2206 }
2207
2208 // Get current values
2209 char *wxGetPrinterCommand(void)
2210 {
2211 return wxThePrintSetupData->GetPrinterCommand();
2212 }
2213
2214 char *wxGetPrintPreviewCommand(void)
2215 {
2216 return wxThePrintSetupData->GetPrintPreviewCommand();
2217 }
2218
2219 char *wxGetPrinterOptions(void)
2220 {
2221 return wxThePrintSetupData->GetPrinterOptions();
2222 }
2223
2224 char *wxGetPrinterFile(void)
2225 {
2226 return wxThePrintSetupData->GetPrinterFile();
2227 }
2228
2229 int wxGetPrinterOrientation(void)
2230 {
2231 return wxThePrintSetupData->GetPrinterOrientation();
2232 }
2233
2234 void wxGetPrinterScaling(double* x, double* y)
2235 {
2236 wxThePrintSetupData->GetPrinterScaling(x, y);
2237 }
2238
2239 void wxGetPrinterTranslation(long *x, long *y)
2240 {
2241 wxThePrintSetupData->GetPrinterTranslation(x, y);
2242 }
2243
2244 int wxGetPrinterMode(void)
2245 {
2246 return wxThePrintSetupData->GetPrinterMode();
2247 }
2248
2249 char *wxGetAFMPath(void)
2250 {
2251 return wxThePrintSetupData->GetAFMPath();
2252 }
2253
2254 /*
2255 * Print setup data
2256 */
2257
2258 wxPrintSetupData::wxPrintSetupData(void)
2259 {
2260 printerCommand = NULL;
2261 previewCommand = NULL;
2262 printerFlags = NULL;
2263 printerOrient = PS_PORTRAIT;
2264 printerScaleX = (double)1.0;
2265 printerScaleY = (double)1.0;
2266 printerTranslateX = 0;
2267 printerTranslateY = 0;
2268 // 1 = Preview, 2 = print to file, 3 = send to printer
2269 printerMode = 3;
2270 afmPath = NULL;
2271 paperName = NULL;
2272 printColour = TRUE;
2273 printerFile = NULL;
2274 }
2275
2276 wxPrintSetupData::~wxPrintSetupData(void)
2277 {
2278 if (printerCommand)
2279 delete[] printerCommand;
2280 if (previewCommand)
2281 delete[] previewCommand;
2282 if (printerFlags)
2283 delete[] printerFlags;
2284 if (afmPath)
2285 delete[] afmPath;
2286 if (paperName)
2287 delete[] paperName;
2288 if (printerFile)
2289 delete[] printerFile;
2290 }
2291
2292 void wxPrintSetupData::SetPrinterCommand(char *cmd)
2293 {
2294 if (cmd == printerCommand)
2295 return;
2296
2297 if (printerCommand)
2298 delete[] printerCommand;
2299 if (cmd)
2300 printerCommand = copystring(cmd);
2301 else
2302 printerCommand = NULL;
2303 }
2304
2305 void wxPrintSetupData::SetPrintPreviewCommand(char *cmd)
2306 {
2307 if (cmd == previewCommand)
2308 return;
2309
2310 if (previewCommand)
2311 delete[] previewCommand;
2312 if (cmd)
2313 previewCommand = copystring(cmd);
2314 else
2315 previewCommand = NULL;
2316 }
2317
2318 void wxPrintSetupData::SetPaperName(char *name)
2319 {
2320 if (name == paperName)
2321 return;
2322
2323 if (paperName)
2324 delete[] paperName;
2325 if (name)
2326 paperName = copystring(name);
2327 else
2328 paperName = NULL;
2329 }
2330
2331 void wxPrintSetupData::SetPrinterOptions(char *flags)
2332 {
2333 if (printerFlags == flags)
2334 return;
2335
2336 if (printerFlags)
2337 delete[] printerFlags;
2338 if (flags)
2339 printerFlags = copystring(flags);
2340 else
2341 printerFlags = NULL;
2342 }
2343
2344 void wxPrintSetupData::SetPrinterFile(char *f)
2345 {
2346 if (f == printerFile)
2347 return;
2348
2349 if (printerFile)
2350 delete[] printerFile;
2351 if (f)
2352 printerFile = copystring(f);
2353 else
2354 printerFile = NULL;
2355 }
2356
2357 void wxPrintSetupData::SetPrinterOrientation(int orient)
2358 {
2359 printerOrient = orient;
2360 }
2361
2362 void wxPrintSetupData::SetPrinterScaling(double x, double y)
2363 {
2364 printerScaleX = x;
2365 printerScaleY = y;
2366 }
2367
2368 void wxPrintSetupData::SetPrinterTranslation(long x, long y)
2369 {
2370 printerTranslateX = x;
2371 printerTranslateY = y;
2372 }
2373
2374 // 1 = Preview, 2 = print to file, 3 = send to printer
2375 void wxPrintSetupData::SetPrinterMode(int mode)
2376 {
2377 printerMode = mode;
2378 }
2379
2380 void wxPrintSetupData::SetAFMPath(char *f)
2381 {
2382 if (f == afmPath)
2383 return;
2384
2385 if (afmPath)
2386 delete[] afmPath;
2387 if (f)
2388 afmPath = copystring(f);
2389 else
2390 afmPath = NULL;
2391 }
2392
2393 void wxPrintSetupData::SetColour(bool col)
2394 {
2395 printColour = col;
2396 }
2397
2398 // Get current values
2399 char *wxPrintSetupData::GetPrinterCommand(void)
2400 {
2401 return printerCommand;
2402 }
2403
2404 char *wxPrintSetupData::GetPrintPreviewCommand(void)
2405 {
2406 return previewCommand;
2407 }
2408
2409 char *wxPrintSetupData::GetPrinterOptions(void)
2410 {
2411 return printerFlags;
2412 }
2413
2414 char *wxPrintSetupData::GetPrinterFile(void)
2415 {
2416 return printerFile;
2417 }
2418
2419 char *wxPrintSetupData::GetPaperName(void)
2420 {
2421 return paperName;
2422 }
2423
2424 int wxPrintSetupData::GetPrinterOrientation(void)
2425 {
2426 return printerOrient;
2427 }
2428
2429 void wxPrintSetupData::GetPrinterScaling(double *x, double *y)
2430 {
2431 *x = printerScaleX;
2432 *y = printerScaleY;
2433 }
2434
2435 void wxPrintSetupData::GetPrinterTranslation(long *x, long *y)
2436 {
2437 *x = printerTranslateX;
2438 *y = printerTranslateY;
2439 }
2440
2441 int wxPrintSetupData::GetPrinterMode(void)
2442 {
2443 return printerMode;
2444 }
2445
2446 char *wxPrintSetupData::GetAFMPath(void)
2447 {
2448 return afmPath;
2449 }
2450
2451 bool wxPrintSetupData::GetColour(void)
2452 {
2453 return printColour;
2454 }
2455
2456 void wxPrintSetupData::operator=(wxPrintSetupData& data)
2457 {
2458 SetPrinterCommand(data.GetPrinterCommand());
2459 SetPrintPreviewCommand(data.GetPrintPreviewCommand());
2460 SetPrinterOptions(data.GetPrinterOptions());
2461 long x, y;
2462 data.GetPrinterTranslation(&x, &y);
2463 SetPrinterTranslation(x, y);
2464
2465 double x1, y1;
2466 data.GetPrinterScaling(&x1, &y1);
2467 SetPrinterScaling(x1, y1);
2468
2469 SetPrinterOrientation(data.GetPrinterOrientation());
2470 SetPrinterMode(data.GetPrinterMode());
2471 SetAFMPath(data.GetAFMPath());
2472 SetPaperName(data.GetPaperName());
2473 SetColour(data.GetColour());
2474 }
2475
2476 void wxInitializePrintSetupData(bool init)
2477 {
2478 if (init)
2479 {
2480 wxThePrintSetupData = new wxPrintSetupData;
2481
2482 wxThePrintSetupData->SetPrintPreviewCommand(PS_VIEWER_PROG);
2483 wxThePrintSetupData->SetPrinterOrientation(PS_PORTRAIT);
2484 wxThePrintSetupData->SetPrinterMode(PS_PREVIEW);
2485 wxThePrintSetupData->SetPaperName("A4 210 x 297 mm");
2486
2487 // Could have a .ini file to read in some defaults
2488 // - and/or use environment variables, e.g. WXWIN
2489 #ifdef __VMS__
2490 wxThePrintSetupData->SetPrinterCommand("print");
2491 wxThePrintSetupData->SetPrinterOptions("/nonotify/queue=psqueue");
2492 wxThePrintSetupData->SetAFMPath("sys$ps_font_metrics:");
2493 #endif
2494 #ifdef __WXMSW__
2495 wxThePrintSetupData->SetPrinterCommand("print");
2496 wxThePrintSetupData->SetAFMPath("c:\\windows\\system\\");
2497 wxThePrintSetupData->SetPrinterOptions(NULL);
2498 #endif
2499 #if !defined(__VMS__) && !defined(__WXMSW__)
2500 wxThePrintSetupData->SetPrinterCommand("lpr");
2501 wxThePrintSetupData->SetPrinterOptions(NULL);
2502 wxThePrintSetupData->SetAFMPath(NULL);
2503 #endif
2504 }
2505 else
2506 {
2507 if (wxThePrintSetupData)
2508 delete wxThePrintSetupData;
2509 wxThePrintSetupData = NULL;
2510 }
2511 }
2512
2513 /*
2514 * Paper size database for PostScript
2515 */
2516
2517 wxPrintPaperType::wxPrintPaperType(char *name, int wmm, int hmm, int wp, int hp)
2518 {
2519 widthMM = wmm;
2520 heightMM = hmm;
2521 widthPixels = wp;
2522 heightPixels = hp;
2523 pageName = copystring(name);
2524 }
2525
2526 wxPrintPaperType::~wxPrintPaperType(void)
2527 {
2528 delete[] pageName;
2529 }
2530
2531 wxPrintPaperDatabase::wxPrintPaperDatabase(void):wxList(wxKEY_STRING)
2532 {
2533 DeleteContents(TRUE);
2534 }
2535
2536 wxPrintPaperDatabase::~wxPrintPaperDatabase(void)
2537 {
2538 }
2539
2540 void wxPrintPaperDatabase::CreateDatabase(void)
2541 {
2542 // Need correct values for page size in pixels.
2543 // Each unit is one 'point' = 1/72 of an inch.
2544 // NOTE: WE NEED ALSO TO MAKE ADJUSTMENTS WHEN TRANSLATING
2545 // in wxPostScriptDC code, so we can start from top left.
2546 // So access this database and translate by appropriate number
2547 // of points for this paper size. OR IS IT OK ALREADY?
2548 // Can't remember where the PostScript origin is by default.
2549 // Heck, someone will know how to make it hunky-dory...
2550 // JACS 25/5/95
2551
2552 AddPaperType("A4 210 x 297 mm", 210, 297, 595, 842);
2553 AddPaperType("A3 297 x 420 mm", 297, 420, 842, 1191);
2554 AddPaperType("Letter 8 1/2 x 11 in", 216, 279, 612, 791);
2555 AddPaperType("Legal 8 1/2 x 14 in", 216, 356, 612, 1009);
2556 }
2557
2558 void wxPrintPaperDatabase::ClearDatabase(void)
2559 {
2560 Clear();
2561 }
2562
2563 void wxPrintPaperDatabase::AddPaperType(char *name, int wmm, int hmm, int wp, int hp)
2564 {
2565 Append(name, new wxPrintPaperType(name, wmm, hmm, wp, hp));
2566 }
2567
2568 wxPrintPaperType *wxPrintPaperDatabase::FindPaperType(char *name)
2569 {
2570 wxNode *node = Find(name);
2571 if (node)
2572 return (wxPrintPaperType *)node->Data();
2573 else
2574 return NULL;
2575 }
2576
2577 #endif