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