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