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