source: git/src/printwin.c @ d275723

RELEASE/1.0RELEASE/1.1RELEASE/1.2debug-cidebug-ci-sanitisersfaster-cavernloglog-selectstereowalls-datawalls-data-hanging-as-warning
Last change on this file since d275723 was 4d3c384e, checked in by Olly Betts <olly@…>, 22 years ago

Another fix - now compiles.

git-svn-id: file:///home/survex-svn/survex/trunk@1986 4b37db11-9a0c-4f06-9ece-9ab7cdaee568

  • Property mode set to 100644
File size: 10.7 KB
Line 
1/* printwin.c */
2/* Device dependent part of Survex Win32 driver */
3/* Copyright (C) 1993-2002 Olly Betts
4 * Copyright (C) 2001 Philip Underwood
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19 */
20
21#ifdef HAVE_CONFIG_H
22# include <config.h>
23#endif
24
25#include <stdio.h>
26#include <stdlib.h>
27#include <math.h>
28#include <string.h>
29#include <time.h>
30#include <ctype.h>
31#include <float.h>
32#include <limits.h>
33
34#include "debug.h" /* for BUG and ASSERT */
35#include "filelist.h"
36#include "filename.h"
37#include "ini.h"
38#include "message.h"
39#include "prcore.h"
40#include "prio.h"
41#include "useful.h"
42
43#include <windows.h>
44
45static double MarginLeft, MarginRight, MarginTop, MarginBottom;
46static int fontsize, fontsize_labels;
47static double LineWidth;
48
49static char *fontname, *fontname_labels;
50
51static const char *win_Name(void);
52static int win_Pre(int pagesToPrint, const char *title);
53static void win_NewPage(int pg, int pass, int pagesX, int pagesY);
54static char *win_Init(FILE **fh_list, const char *pth, const char *outfnm,
55                      double *pscX, double *pscY, bool fCalibrate);
56static int  win_Charset(void);
57static void win_MoveTo(long x, long y);
58static void win_DrawTo(long x, long y);
59static void win_DrawCross(long x, long y);
60static void win_SetFont(int fontcode);
61static void win_SetColour(int colourcode);
62static void win_WriteString(const char *s);
63static void win_DrawCircle(long x, long y, long r);
64static void win_ShowPage(const char *szPageDetails);
65static void win_Quit(void);
66
67device printer = {
68   PR_FLAG_NOFILEOUTPUT, /* |PR_FLAG_NOINI - now read fontsize from ini file */
69   win_Name,
70   win_Init,
71   win_Charset,
72   win_Pre,
73   win_NewPage,
74   win_MoveTo,
75   win_DrawTo,
76   win_DrawCross,
77   win_SetFont,
78   win_SetColour,
79   win_WriteString,
80   win_DrawCircle,
81   win_ShowPage,
82   NULL, /* win_Post */
83   win_Quit
84};
85
86static HDC pd; /* printer context */
87
88static TEXTMETRIC *tm, tm_labels, tm_default; /* font info */
89
90static COLORREF colour_leg, colour_surface_leg, colour_cross, colour_frame;
91static COLORREF colour_text, colour_labels;
92static HPEN pen_leg, pen_surface_leg, pen_cross, pen_frame;
93
94static double scX, scY;
95
96static int cur_pass;
97
98static border clip;
99
100static long xpPageWidth, ypPageDepth;
101
102static long x_t, y_t;
103
104static int
105check_intersection(long x_p, long y_p)
106{
107#define U 1
108#define D 2
109#define L 4
110#define R 8
111   int mask_p = 0, mask_t = 0;
112   if (x_p < 0)
113      mask_p = L;
114   else if (x_p > xpPageWidth)
115      mask_p = R;
116
117   if (y_p < 0)
118      mask_p |= D;
119   else if (y_p > ypPageDepth)
120      mask_p |= U;
121
122   if (x_t < 0)
123      mask_t = L;
124   else if (x_t > xpPageWidth)
125      mask_t = R;
126
127   if (y_t < 0)
128      mask_t |= D;
129   else if (y_t > ypPageDepth)
130      mask_t |= U;
131
132#if 0
133   /* approximation to correct answer */
134   return !(mask_t & mask_p);
135#else
136   /* One end of the line is on the page */
137   if (!mask_t || !mask_p) return 1;
138
139   /* whole line is above, left, right, or below page */
140   if (mask_t & mask_p) return 0;
141
142   if (mask_t == 0) mask_t = mask_p;
143   if (mask_t & U) {
144      double v = (double)(y_p - ypPageDepth) / (y_p - y_t);
145      return v >= 0 && v <= 1;
146   }
147   if (mask_t & D) {
148      double v = (double)y_p / (y_p - y_t);
149      return v >= 0 && v <= 1;
150   }
151   if (mask_t & R) {
152      double v = (double)(x_p - xpPageWidth) / (x_p - x_t);
153      return v >= 0 && v <= 1;
154   }
155   ASSERT(mask_t & L);
156   {
157      double v = (double)x_p / (x_p - x_t);
158      return v >= 0 && v <= 1;
159   }
160#endif
161#undef U
162#undef D
163#undef L
164#undef R
165}
166
167static const char *
168win_Name(void)
169{
170   return "Win32 Printer";
171}
172
173static void
174win_MoveTo(long x, long y)
175{
176   x_t = x - clip.x_min;
177   y_t = clip.y_max - y;
178   if (cur_pass != -1) MoveToEx(pd, x_t, y_t, NULL);
179}
180
181static void
182win_DrawTo(long x, long y)
183{
184   long x_p = x_t, y_p = y_t;
185   x_t = x - clip.x_min;
186   y_t = clip.y_max - y;
187   if (cur_pass != -1) {
188      LineTo(pd, x_t, y_t);
189   } else {
190      if (check_intersection(x_p, y_p)) fBlankPage = fFalse;
191   }
192}
193
194#define POINTS_PER_INCH 72.0
195#define POINTS_PER_MM (POINTS_PER_INCH / MM_PER_INCH)
196#define WIN_CROSS_SIZE (2 * scX / POINTS_PER_MM)
197
198static void
199win_DrawCross(long x, long y)
200{
201   if (cur_pass != -1) {
202      win_MoveTo(x - WIN_CROSS_SIZE, y - WIN_CROSS_SIZE);
203      win_DrawTo(x + WIN_CROSS_SIZE, y + WIN_CROSS_SIZE);
204      win_MoveTo(x + WIN_CROSS_SIZE, y - WIN_CROSS_SIZE);
205      win_DrawTo(x - WIN_CROSS_SIZE, y + WIN_CROSS_SIZE);
206      win_MoveTo(x, y);
207   } else {
208      if ((x + WIN_CROSS_SIZE > clip.x_min &&
209           x - WIN_CROSS_SIZE < clip.x_max) ||
210          (y + WIN_CROSS_SIZE > clip.y_min &&
211           y - WIN_CROSS_SIZE < clip.y_max)) {
212         fBlankPage = fFalse;
213      }
214   }
215}
216
217static HFONT font_labels, font_default, font_old;
218
219static void
220win_SetFont(int fontcode)
221{
222   switch (fontcode) {
223      case PR_FONT_DEFAULT:
224         SelectObject(pd, font_default);
225         tm = &tm_default;
226         break;
227      case PR_FONT_LABELS:
228         SelectObject(pd, font_labels);
229         tm = &tm_labels;
230         break;
231      default:
232         BUG("unknown font code");
233   }
234}
235
236static void
237win_SetColour(int colourcode)
238{
239   switch (colourcode) {
240      case PR_COLOUR_TEXT:
241         SetTextColor(pd, colour_text);
242         break;
243      case PR_COLOUR_LABELS:
244         SetTextColor(pd, colour_labels);
245         break;
246      case PR_COLOUR_FRAME:
247         SelectObject(pd, pen_frame);
248         break;
249      case PR_COLOUR_LEG:
250         SelectObject(pd, pen_leg);
251         break;
252      case PR_COLOUR_CROSS:
253         SelectObject(pd, pen_cross);
254         break;
255      case PR_COLOUR_SURFACE_LEG:
256         SelectObject(pd, pen_surface_leg);
257         break;
258      default:
259         BUG("unknown colour code");
260   }
261}
262
263static void
264win_WriteString(const char *s)
265{
266   if (cur_pass != -1) {
267      TextOut(pd, x_t, y_t - tm->tmAscent, s, strlen(s));
268   } else {
269      if ((y_t + tm->tmDescent > 0 &&
270           y_t - tm->tmAscent < clip.y_max - clip.y_min) ||
271          (x_t < clip.x_max - clip.x_min &&
272           x_t + strlen(s) * tm->tmAveCharWidth > 0)) {
273         fBlankPage = fFalse;
274      }
275   }
276}
277
278static void
279win_DrawCircle(long x, long y, long r)
280{
281   /* Don't need to check in first-pass - circle is only used in title box */
282   if (cur_pass != -1) {
283      x_t = x - clip.x_min;
284      y_t = clip.y_max - y;
285      Ellipse(pd, x_t - r, y_t - r, x_t + r, y_t + r);
286   }
287}
288
289static int
290win_Charset(void)
291{
292   return CHARSET_ISO_8859_1;
293}
294
295static int
296win_Pre(int pagesToPrint, const char *title)
297{
298   PRINTDLGA psd;
299   DOCINFO info;
300   int logpixelsy;
301
302   pagesToPrint = pagesToPrint; /* suppress compiler warning */
303
304   memset(&psd, 0, sizeof(PRINTDLGA));
305   psd.lStructSize = 66;
306   psd.Flags = PD_RETURNDC | PD_RETURNDEFAULT;
307
308   if (!PrintDlgA(&psd)) exit(1);
309   pd = psd.hDC;
310
311   logpixelsy = GetDeviceCaps(pd, LOGPIXELSY);
312   font_labels = CreateFont(-MulDiv(fontsize_labels, logpixelsy, 72),
313                            0, 0, 0, FW_NORMAL, 0, 0, 0,
314                            ANSI_CHARSET, OUT_DEFAULT_PRECIS,
315                            CLIP_DEFAULT_PRECIS, DEFAULT_QUALITY,
316                            FF_DONTCARE | DEFAULT_PITCH, "Arial");
317   font_default = CreateFont(-MulDiv(fontsize, logpixelsy, 72),
318                             0, 0, 0, FW_NORMAL, 0, 0, 0,
319                             ANSI_CHARSET, OUT_DEFAULT_PRECIS,
320                             CLIP_DEFAULT_PRECIS, DEFAULT_QUALITY,
321                             FF_DONTCARE | DEFAULT_PITCH, "Arial");
322   pen_leg = CreatePen(PS_SOLID, 0, colour_leg);
323   pen_surface_leg = CreatePen(PS_SOLID, 0, colour_surface_leg);
324   pen_cross = CreatePen(PS_SOLID, 0, colour_cross);
325   pen_frame = CreatePen(PS_SOLID, 0, colour_frame);
326   font_old = SelectObject(pd, font_labels);
327   GetTextMetrics(pd, &tm_labels);
328   SelectObject(pd, font_default);
329   GetTextMetrics(pd, &tm_default);
330
331   memset(&info, 0, sizeof(DOCINFO));
332   info.cbSize = sizeof(DOCINFO);
333   info.lpszDocName = title;
334
335   StartDoc(pd, &info);
336   return 1; /* only need 1 pass */
337}
338
339static void
340win_NewPage(int pg, int pass, int pagesX, int pagesY)
341{
342   int x, y;
343
344   x = (pg - 1) % pagesX;
345   y = pagesY - 1 - ((pg - 1) / pagesX);
346
347   clip.x_min = (long)x * xpPageWidth;
348   clip.y_min = (long)y * ypPageDepth;
349   clip.x_max = clip.x_min + xpPageWidth; /* dm/pcl/ps had -1; */
350   clip.y_max = clip.y_min + ypPageDepth; /* dm/pcl/ps had -1; */
351
352   cur_pass = pass;
353   if (pass == -1) return;
354
355   StartPage(pd);
356   drawticks(clip, 9 * scX / POINTS_PER_MM, x, y);
357}
358
359static void
360win_ShowPage(const char *szPageDetails)
361{
362   win_MoveTo((long)(6 * scX) + clip.x_min, clip.y_min - (long)(7 * scY));
363   win_WriteString(szPageDetails);
364   EndPage(pd);
365}
366
367static COLORREF
368to_rgb(const char *var, char *val)
369{
370   unsigned long rgb = as_colour(var, val);
371   return RGB((rgb & 0xff0000) >> 16, (rgb & 0xff00) >> 8, rgb & 0xff);
372}
373
374/* Initialise printer routines */
375static char *
376win_Init(FILE **fh_list, const char *pth, const char *out_fnm,
377         double *pscX, double *pscY, bool fCalibrate)
378{
379   PRINTDLGA psd;
380   LPDEVNAMES dn;
381   static const char *vars[] = {
382      "like",
383      "font_size_labels",
384      "colour_text",
385      "colour_labels",
386      "colour_frame",
387      "colour_legs",
388      "colour_crosses",
389      "colour_surface_legs",
390      NULL
391   };
392   char **vals;
393
394   fCalibrate = fCalibrate; /* suppress unused argument warning */
395   out_fnm = out_fnm;
396   pth = pth;
397
398   vals = ini_read_hier(fh_list, "win", vars);
399
400   fontsize_labels = as_int(vars[1], vals[1], 1, INT_MAX);
401   fontsize = 10;
402
403   colour_text = to_rgb(vars[2], vals[2]);
404   colour_labels = to_rgb(vars[3], vals[3]);
405   colour_frame = to_rgb(vars[4], vals[4]);
406   colour_leg = to_rgb(vars[5], vals[5]);
407   colour_cross = to_rgb(vars[6], vals[6]);
408
409   memset(&psd, 0, sizeof(PRINTDLGA));
410   psd.lStructSize = 66;
411   psd.Flags = PD_RETURNDC | PD_RETURNDEFAULT;
412
413   if (!PrintDlgA(&psd)) exit(1);
414
415   PaperWidth = GetDeviceCaps(psd.hDC, HORZSIZE);
416   PaperDepth = GetDeviceCaps(psd.hDC, VERTSIZE);
417   xpPageWidth = GetDeviceCaps(psd.hDC, HORZRES);
418   ypPageDepth = GetDeviceCaps(psd.hDC, VERTRES);
419   MarginLeft = MarginBottom = 0;
420   MarginRight = PaperWidth;
421   MarginTop = PaperDepth;
422   LineWidth = 0;
423   scX = *pscX = xpPageWidth / PaperWidth;
424   scY = *pscY = ypPageDepth / PaperDepth;
425   xpPageWidth--;
426   ypPageDepth = ypPageDepth - (int)(10 * *pscY);
427   DeleteDC(psd.hDC);
428
429   dn = GlobalLock(psd.hDevNames);
430   if (dn) {
431      char *p = osstrdup((char *)dn + dn->wDeviceOffset);
432      GlobalUnlock(psd.hDevNames);
433      return p;
434   }
435   return NULL;
436}
437
438static void
439win_Quit(void)
440{
441   SelectObject(pd, font_old);
442   DeleteObject(font_labels);
443   DeleteObject(font_default);
444   EndDoc(pd);
445   DeleteDC(pd);
446}
Note: See TracBrowser for help on using the repository browser.