source: git/src/printwin.c @ a11230b

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

Fix various MSVC warnings.

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

  • Property mode set to 100644
File size: 11.0 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 SVX_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   SVX_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 (int)(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         SetBkMode(pd, TRANSPARENT);
246         break;
247      case PR_COLOUR_FRAME:
248         SelectObject(pd, pen_frame);
249         break;
250      case PR_COLOUR_LEG:
251         SelectObject(pd, pen_leg);
252         break;
253      case PR_COLOUR_CROSS:
254         SelectObject(pd, pen_cross);
255         break;
256      case PR_COLOUR_SURFACE_LEG:
257         SelectObject(pd, pen_surface_leg);
258         break;
259      default:
260         BUG("unknown colour code");
261   }
262}
263
264static void
265win_WriteString(const char *s)
266{
267   if (cur_pass != -1) {
268      TextOut(pd, x_t, y_t - tm->tmAscent, s, strlen(s));
269   } else {
270      if ((y_t + tm->tmDescent > 0 &&
271           y_t - tm->tmAscent < clip.y_max - clip.y_min) ||
272          (x_t < clip.x_max - clip.x_min &&
273           x_t + strlen(s) * tm->tmAveCharWidth > 0)) {
274         fBlankPage = fFalse;
275      }
276   }
277}
278
279static void
280win_DrawCircle(long x, long y, long r)
281{
282   /* Don't need to check in first-pass - circle is only used in title box */
283   if (cur_pass != -1) {
284      x_t = x - clip.x_min;
285      y_t = clip.y_max - y;
286      Ellipse(pd, x_t - r, y_t - r, x_t + r, y_t + r);
287   }
288}
289
290static int
291win_Charset(void)
292{
293   return CHARSET_ISO_8859_1;
294}
295
296static int
297win_Pre(int pagesToPrint, const char *title)
298{
299   PRINTDLGA psd;
300   DOCINFO info;
301   int logpixelsy;
302
303   pagesToPrint = pagesToPrint; /* suppress compiler warning */
304
305   memset(&psd, 0, sizeof(PRINTDLGA));
306   psd.lStructSize = 66;
307   psd.Flags = PD_RETURNDC | PD_RETURNDEFAULT;
308
309   if (!PrintDlgA(&psd)) exit(1);
310   pd = psd.hDC;
311
312   logpixelsy = GetDeviceCaps(pd, LOGPIXELSY);
313   font_labels = CreateFont(-MulDiv(fontsize_labels, logpixelsy, 72),
314                            0, 0, 0, FW_NORMAL, 0, 0, 0,
315                            ANSI_CHARSET, OUT_DEFAULT_PRECIS,
316                            CLIP_DEFAULT_PRECIS, DEFAULT_QUALITY,
317                            FF_DONTCARE | DEFAULT_PITCH, "Arial");
318   font_default = CreateFont(-MulDiv(fontsize, logpixelsy, 72),
319                             0, 0, 0, FW_NORMAL, 0, 0, 0,
320                             ANSI_CHARSET, OUT_DEFAULT_PRECIS,
321                             CLIP_DEFAULT_PRECIS, DEFAULT_QUALITY,
322                             FF_DONTCARE | DEFAULT_PITCH, "Arial");
323   /* Pen width of 0 should work, but seemed to give problems, so we'll
324    * try 1 instead... */
325   pen_leg = CreatePen(PS_SOLID, 1, colour_leg);
326   pen_surface_leg = CreatePen(PS_SOLID, 1, colour_surface_leg);
327   pen_cross = CreatePen(PS_SOLID, 1, colour_cross);
328   pen_frame = CreatePen(PS_SOLID, 1, colour_frame);
329   font_old = SelectObject(pd, font_labels);
330   GetTextMetrics(pd, &tm_labels);
331   SelectObject(pd, font_default);
332   GetTextMetrics(pd, &tm_default);
333
334   memset(&info, 0, sizeof(DOCINFO));
335   info.cbSize = sizeof(DOCINFO);
336   info.lpszDocName = title;
337
338   StartDoc(pd, &info);
339   return 1; /* only need 1 pass */
340}
341
342static void
343win_NewPage(int pg, int pass, int pagesX, int pagesY)
344{
345   int x, y;
346
347   x = (pg - 1) % pagesX;
348   y = pagesY - 1 - ((pg - 1) / pagesX);
349
350   clip.x_min = (long)x * xpPageWidth;
351   clip.y_min = (long)y * ypPageDepth;
352   clip.x_max = clip.x_min + xpPageWidth; /* dm/pcl/ps had -1; */
353   clip.y_max = clip.y_min + ypPageDepth; /* dm/pcl/ps had -1; */
354
355   cur_pass = pass;
356   if (pass == -1) return;
357
358   StartPage(pd);
359   drawticks(clip, int(9 * scX / POINTS_PER_MM), x, y);
360}
361
362static void
363win_ShowPage(const char *szPageDetails)
364{
365   win_MoveTo((long)(6 * scX) + clip.x_min, clip.y_min - (long)(7 * scY));
366   win_WriteString(szPageDetails);
367   EndPage(pd);
368}
369
370static COLORREF
371to_rgb(const char *var, char *val)
372{
373   unsigned long rgb;
374   if (!val) return RGB(0, 0, 0);
375   rgb = as_colour(var, val);
376   return RGB((rgb & 0xff0000) >> 16, (rgb & 0xff00) >> 8, rgb & 0xff);
377}
378
379/* Initialise printer routines */
380static char *
381win_Init(FILE **fh_list, const char *pth, const char *out_fnm,
382         double *pscX, double *pscY, bool fCalibrate)
383{
384   PRINTDLGA psd;
385   LPDEVNAMES dn;
386   static const char *vars[] = {
387      "like",
388      "font_size_labels",
389      "colour_text",
390      "colour_labels",
391      "colour_frame",
392      "colour_legs",
393      "colour_crosses",
394      "colour_surface_legs",
395      NULL
396   };
397   char **vals;
398
399   fCalibrate = fCalibrate; /* suppress unused argument warning */
400   out_fnm = out_fnm;
401   pth = pth;
402
403   vals = ini_read_hier(fh_list, "win", vars);
404
405   fontsize_labels = as_int(vars[1], vals[1], 1, INT_MAX);
406   fontsize = 10;
407
408   colour_text = to_rgb(vars[2], vals[2]);
409   colour_labels = to_rgb(vars[3], vals[3]);
410   colour_frame = to_rgb(vars[4], vals[4]);
411   colour_leg = to_rgb(vars[5], vals[5]);
412   colour_cross = to_rgb(vars[6], vals[6]);
413   colour_surface_leg = to_rgb(vars[7], vals[7]);
414
415   memset(&psd, 0, sizeof(PRINTDLGA));
416   psd.lStructSize = 66;
417   psd.Flags = PD_RETURNDC | PD_RETURNDEFAULT;
418
419   if (!PrintDlgA(&psd)) exit(1);
420
421   PaperWidth = GetDeviceCaps(psd.hDC, HORZSIZE);
422   PaperDepth = GetDeviceCaps(psd.hDC, VERTSIZE);
423   xpPageWidth = GetDeviceCaps(psd.hDC, HORZRES);
424   ypPageDepth = GetDeviceCaps(psd.hDC, VERTRES);
425   MarginLeft = MarginBottom = 0;
426   MarginRight = PaperWidth;
427   MarginTop = PaperDepth;
428   LineWidth = 0;
429   scX = *pscX = xpPageWidth / PaperWidth;
430   scY = *pscY = ypPageDepth / PaperDepth;
431   xpPageWidth--;
432   ypPageDepth = ypPageDepth - (int)(10 * *pscY);
433   DeleteDC(psd.hDC);
434
435   dn = GlobalLock(psd.hDevNames);
436   if (dn) {
437      char *p = osstrdup((char *)dn + dn->wDeviceOffset);
438      GlobalUnlock(psd.hDevNames);
439      return p;
440   }
441   return NULL;
442}
443
444static void
445win_Quit(void)
446{
447   SelectObject(pd, font_old);
448   DeleteObject(font_labels);
449   DeleteObject(font_default);
450   EndDoc(pd);
451   DeleteDC(pd);
452}
Note: See TracBrowser for help on using the repository browser.