]> git.saurik.com Git - wxWidgets.git/blame - src/common/postscrp.cpp
* Fixed two memory leaks.
[wxWidgets.git] / src / common / postscrp.cpp
CommitLineData
c801d85f
KB
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
2049ba38 47#ifdef __WXMSW__
c801d85f
KB
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
2049ba38 65#ifdef __WXGTK__
c801d85f
KB
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
2049ba38 74#ifdef __WXMSW__
c801d85f
KB
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"
2049ba38 108#elif defined(__X__) || defined(__WXGTK__)
c801d85f
KB
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
c67daf87 116wxPrintSetupData *wxThePrintSetupData = (wxPrintSetupData *) NULL;
c801d85f
KB
117
118// these should move into wxPostscriptDC:
119double UnderlinePosition = 0.0F;
120double UnderlineThickness = 0.0F;
121
122#define _MAXPATHLEN 500
123
124/* See "wxspline.inc" and "xfspline.inc" */
125#if USE_XFIG_SPLINE_CODE
126static 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.
c67daf87 144static const char *wxPostScriptHeaderSpline = (char *) NULL;
c801d85f
KB
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)
155static char *fileBuffer = NULL;
156#endif
157
158#if !USE_SHARED_LIBRARY
159IMPLEMENT_DYNAMIC_CLASS(wxPostScriptDC, wxDC)
160IMPLEMENT_DYNAMIC_CLASS(wxPrintSetupData, wxObject)
161IMPLEMENT_DYNAMIC_CLASS(wxPrintPaperType, wxObject)
162IMPLEMENT_DYNAMIC_CLASS(wxPrintPaperDatabase, wxList)
163#endif
164
165wxPostScriptDC::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
c67daf87 176 m_pstream = (ofstream *) NULL;
c801d85f 177
2049ba38 178#ifdef __WXMSW__
c801d85f
KB
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
188wxPostScriptDC::wxPostScriptDC (const wxString& file, bool interactive, wxWindow *parent)
189{
190 Create(file, interactive, parent);
191}
192
193bool 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;
c67daf87 206 m_pstream = (ofstream *) NULL;
c801d85f 207
2049ba38 208#ifdef __WXMSW__
c801d85f
KB
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
229wxPostScriptDC::~wxPostScriptDC (void)
230{
231 if (m_pstream)
232 delete m_pstream;
233}
234
235bool wxPostScriptDC::PrinterDialog(wxWindow *parent)
236{
1a5a8367 237 wxPostScriptPrintDialog dialog (parent, _("Printer Settings"), wxPoint(150, 150), wxSize(400, 400), wxDEFAULT_DIALOG_STYLE | wxDIALOG_MODAL);
c801d85f
KB
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 {
1a5a8367 267 char *file = wxSaveFileSelector (_("PostScript"), "ps");
c801d85f
KB
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
281void 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
298void 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
309void wxPostScriptDC::Clear (void)
310{
311}
312
313void wxPostScriptDC::FloodFill (long WXUNUSED(x), long WXUNUSED(y), wxColour * WXUNUSED(col), int WXUNUSED(style))
314{
315}
316
317bool wxPostScriptDC::GetPixel (long WXUNUSED(x), long WXUNUSED(y), wxColour * WXUNUSED(col)) const
318{
319 return FALSE;
320}
321
322void wxPostScriptDC::CrossHair (long WXUNUSED(x), long WXUNUSED(y))
323{
324}
325
326void 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
342void 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
392void 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
435void 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
448void 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
505void 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
533void 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
569void 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
640void 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
670void wxPostScriptDC::DrawIcon (const wxIcon& icon, long x, long y)
671{
2049ba38 672#if defined(__X__) || defined(__WXGTK__)
c801d85f
KB
673 wxMemoryDC memDC;
674 memDC.SelectObject(icon);
675 Blit(x, y, icon.GetWidth(), icon.GetHeight(), &memDC, 0, 0);
676#endif
677}
678
679void 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];
85ccdcce
VZ
693 const char *name;
694 const char *style = "";
c801d85f
KB
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
768void 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 */
85ccdcce
VZ
793 static const char *dotted = "[2 5] 2";
794 static const char *short_dashed = "[4 4] 2";
795 static const char *long_dashed = "[4 8] 2";
796 static const char *dotted_dashed = "[6 6 2 6] 4";
c801d85f 797
c67daf87 798 const char *psdash = (char *) NULL;
c801d85f
KB
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
853void 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
892void 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
983void wxPostScriptDC::SetBackground (const wxBrush& brush)
984{
985 m_backgroundBrush = brush;
986}
987
988void wxPostScriptDC::SetLogicalFunction (int WXUNUSED(function))
989{
990}
991
992static const char *wxPostScriptHeaderEllipse = "\
993/ellipsedict 8 dict def\n\
994ellipsedict /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
1012static const char *wxPostScriptHeaderEllipticArc= "\
1013/ellipticarcdict 8 dict def\n\
1014ellipticarcdict /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
1034bool 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
1077void 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;
c67daf87 1137 m_pstream = (ofstream *) NULL;
c801d85f
KB
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;
c67daf87 1261 m_pstream = (ofstream *) NULL;
c801d85f
KB
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
2049ba38 1275#if defined(__X__) || defined(__WXGTK__)
c801d85f
KB
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();
c67daf87 1284 argv[2] = (char *) NULL;
c801d85f
KB
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();
c67daf87 1303 argv[argc++] = (char *) NULL;
c801d85f
KB
1304 wxExecute (argv, TRUE);
1305 wxRemoveFile(wxThePrintSetupData->GetPrinterFile());
1306 }
1307 break;
1308
1309 case PS_FILE:
1310 break;
1311 }
1312 }
1313#endif
1314}
1315
1316void 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
1348void 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 */
1403bool wxPostScriptDC::
1404Blit (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
2049ba38 1409#if !defined(__X__) && !defined(__WXGTK__)
c801d85f
KB
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
2049ba38 1431#if defined(__X__) || defined(__WXGTK__)
c801d85f
KB
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
2049ba38 1440#ifdef __WXGTK__
c801d85f
KB
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
2049ba38 1449#ifdef __WXMOTIF__
c801d85f
KB
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
1537long wxPostScriptDC::GetCharHeight (void)
1538{
1539 if (m_font.Ok())
1540 return m_font.GetPointSize ();
1541 else
1542 return 12;
1543}
1544
1545void 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 */
85ccdcce 1643 const char *name;
c801d85f
KB
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
a3622daa 1648 name = wxTheFontNameDirectory->GetAFMName(Family, Weight, Style);
c801d85f
KB
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){
3fc5256f 1706 if((sscanf(line,"%s%lf",upString,&UnderlinePosition)!=2)
c801d85f
KB
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){
40a97c00 1714 if((sscanf(line,"%s%lf",utString,&UnderlineThickness)!=2)
c801d85f
KB
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