[396b9dd] | 1 | /* printwin.c */ |
---|
[55b7334] | 2 | /* Device dependent part of Survex Win32 driver */ |
---|
[b7b666d] | 3 | /* Copyright (C) 1993-2002 Olly Betts |
---|
[4888f06] | 4 | * Copyright (C) 2001 Philip Underwood |
---|
[55b7334] | 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 | |
---|
[c34796a] | 34 | #include "debug.h" /* for BUG and ASSERT */ |
---|
| 35 | #include "filelist.h" |
---|
[55b7334] | 36 | #include "filename.h" |
---|
[c34796a] | 37 | #include "ini.h" |
---|
[55b7334] | 38 | #include "message.h" |
---|
| 39 | #include "prcore.h" |
---|
[c34796a] | 40 | #include "prio.h" |
---|
| 41 | #include "useful.h" |
---|
| 42 | |
---|
[55b7334] | 43 | #include <windows.h> |
---|
| 44 | |
---|
[6974a34] | 45 | static double MarginLeft, MarginRight, MarginTop, MarginBottom; |
---|
[55b7334] | 46 | static int fontsize, fontsize_labels; |
---|
[6974a34] | 47 | static double LineWidth; |
---|
[55b7334] | 48 | |
---|
| 49 | static char *fontname, *fontname_labels; |
---|
| 50 | |
---|
| 51 | static const char *win_Name(void); |
---|
| 52 | static int win_Pre(int pagesToPrint, const char *title); |
---|
| 53 | static void win_NewPage(int pg, int pass, int pagesX, int pagesY); |
---|
[e1fb3cb] | 54 | static void win_Init(FILE **fh_list, const char *pth, const char *outfnm, |
---|
[b7b666d] | 55 | double *pscX, double *pscY, bool fCalibrate); |
---|
[08e95aa] | 56 | static int win_Charset(void); |
---|
[55b7334] | 57 | static void win_MoveTo(long x, long y); |
---|
| 58 | static void win_DrawTo(long x, long y); |
---|
| 59 | static void win_DrawCross(long x, long y); |
---|
[abb82a4] | 60 | static void win_SetFont(int fontcode); |
---|
[55b7334] | 61 | static void win_WriteString(const char *s); |
---|
| 62 | static void win_DrawCircle(long x, long y, long r); |
---|
| 63 | static void win_ShowPage(const char *szPageDetails); |
---|
| 64 | static void win_Quit(void); |
---|
| 65 | |
---|
| 66 | device printer = { |
---|
[bd171e1a] | 67 | PR_FLAG_NOFILEOUTPUT|PR_FLAG_NOINI, |
---|
[55b7334] | 68 | win_Name, |
---|
| 69 | win_Init, |
---|
[08e95aa] | 70 | win_Charset, |
---|
[55b7334] | 71 | win_Pre, |
---|
| 72 | win_NewPage, |
---|
| 73 | win_MoveTo, |
---|
| 74 | win_DrawTo, |
---|
| 75 | win_DrawCross, |
---|
[abb82a4] | 76 | win_SetFont, |
---|
[55b7334] | 77 | win_WriteString, |
---|
| 78 | win_DrawCircle, |
---|
| 79 | win_ShowPage, |
---|
[9d46af2] | 80 | NULL, /* win_Post */ |
---|
[55b7334] | 81 | win_Quit |
---|
| 82 | }; |
---|
| 83 | |
---|
[d0ffc05] | 84 | static HDC pd; /* printer context */ |
---|
[55b7334] | 85 | |
---|
[abb82a4] | 86 | static TEXTMETRIC tm, tm_labels, tm_default; /* font info */ |
---|
[55b7334] | 87 | |
---|
[6974a34] | 88 | static double scX, scY; |
---|
[55b7334] | 89 | |
---|
[396b9dd] | 90 | static int cur_pass; |
---|
| 91 | |
---|
[55b7334] | 92 | static border clip; |
---|
| 93 | |
---|
| 94 | static long xpPageWidth, ypPageDepth; |
---|
| 95 | |
---|
[0658e7b] | 96 | static long x_t, y_t; |
---|
| 97 | |
---|
[396b9dd] | 98 | static int |
---|
| 99 | check_intersection(long x_p, long y_p) |
---|
| 100 | { |
---|
| 101 | #define U 1 |
---|
| 102 | #define D 2 |
---|
| 103 | #define L 4 |
---|
[421b7d2] | 104 | #define R 8 |
---|
[f7cdeb2] | 105 | int mask_p = 0, mask_t = 0; |
---|
[f17dbe2] | 106 | if (x_p < 0) |
---|
[396b9dd] | 107 | mask_p = L; |
---|
[f17dbe2] | 108 | else if (x_p > xpPageWidth) |
---|
[396b9dd] | 109 | mask_p = R; |
---|
| 110 | |
---|
[f17dbe2] | 111 | if (y_p < 0) |
---|
[396b9dd] | 112 | mask_p |= D; |
---|
[f17dbe2] | 113 | else if (y_p > ypPageDepth) |
---|
[396b9dd] | 114 | mask_p |= U; |
---|
| 115 | |
---|
[f17dbe2] | 116 | if (x_t < 0) |
---|
[396b9dd] | 117 | mask_t = L; |
---|
[f17dbe2] | 118 | else if (x_t > xpPageWidth) |
---|
[396b9dd] | 119 | mask_t = R; |
---|
| 120 | |
---|
[f17dbe2] | 121 | if (y_t < 0) |
---|
[396b9dd] | 122 | mask_t |= D; |
---|
[f17dbe2] | 123 | else if (y_t > ypPageDepth) |
---|
[396b9dd] | 124 | mask_t |= U; |
---|
| 125 | |
---|
| 126 | #if 0 |
---|
| 127 | /* approximation to correct answer */ |
---|
| 128 | return !(mask_t & mask_p); |
---|
[69fde77] | 129 | #else |
---|
[396b9dd] | 130 | /* One end of the line is on the page */ |
---|
| 131 | if (!mask_t || !mask_p) return 1; |
---|
| 132 | |
---|
| 133 | /* whole line is above, left, right, or below page */ |
---|
| 134 | if (mask_t & mask_p) return 0; |
---|
| 135 | |
---|
| 136 | if (mask_t == 0) mask_t = mask_p; |
---|
| 137 | if (mask_t & U) { |
---|
[f17dbe2] | 138 | double v = (double)(y_p - ypPageDepth) / (y_p - y_t); |
---|
[396b9dd] | 139 | return v >= 0 && v <= 1; |
---|
| 140 | } |
---|
| 141 | if (mask_t & D) { |
---|
[f17dbe2] | 142 | double v = (double)y_p / (y_p - y_t); |
---|
[396b9dd] | 143 | return v >= 0 && v <= 1; |
---|
| 144 | } |
---|
| 145 | if (mask_t & R) { |
---|
[f17dbe2] | 146 | double v = (double)(x_p - xpPageWidth) / (x_p - x_t); |
---|
[396b9dd] | 147 | return v >= 0 && v <= 1; |
---|
| 148 | } |
---|
| 149 | ASSERT(mask_t & L); |
---|
| 150 | { |
---|
[f17dbe2] | 151 | double v = (double)x_p / (x_p - x_t); |
---|
[396b9dd] | 152 | return v >= 0 && v <= 1; |
---|
| 153 | } |
---|
| 154 | #endif |
---|
| 155 | #undef U |
---|
| 156 | #undef D |
---|
| 157 | #undef L |
---|
| 158 | #undef R |
---|
| 159 | } |
---|
| 160 | |
---|
[55b7334] | 161 | static const char * |
---|
| 162 | win_Name(void) |
---|
| 163 | { |
---|
| 164 | return "Win32 Printer"; |
---|
| 165 | } |
---|
| 166 | |
---|
| 167 | static void |
---|
| 168 | win_MoveTo(long x, long y) |
---|
| 169 | { |
---|
| 170 | x_t = x - clip.x_min; |
---|
| 171 | y_t = clip.y_max - y; |
---|
[396b9dd] | 172 | if (cur_pass != -1) MoveToEx(pd, x_t, y_t, NULL); |
---|
[55b7334] | 173 | } |
---|
| 174 | |
---|
| 175 | static void |
---|
| 176 | win_DrawTo(long x, long y) |
---|
| 177 | { |
---|
[396b9dd] | 178 | long x_p = x_t, y_p = y_t; |
---|
[55b7334] | 179 | x_t = x - clip.x_min; |
---|
| 180 | y_t = clip.y_max - y; |
---|
[396b9dd] | 181 | if (cur_pass != -1) { |
---|
| 182 | LineTo(pd, x_t, y_t); |
---|
| 183 | } else { |
---|
| 184 | if (check_intersection(x_p, y_p)) fBlankPage = fFalse; |
---|
| 185 | } |
---|
[55b7334] | 186 | } |
---|
| 187 | |
---|
[7be0ba8] | 188 | #define POINTS_PER_INCH 72.0 |
---|
| 189 | #define POINTS_PER_MM (POINTS_PER_INCH / MM_PER_INCH) |
---|
| 190 | #define WIN_CROSS_SIZE (2 * scX / POINTS_PER_MM) |
---|
| 191 | |
---|
[55b7334] | 192 | static void |
---|
| 193 | win_DrawCross(long x, long y) |
---|
| 194 | { |
---|
[396b9dd] | 195 | if (cur_pass != -1) { |
---|
[d3d9230] | 196 | win_MoveTo(x - WIN_CROSS_SIZE, y - WIN_CROSS_SIZE); |
---|
| 197 | win_DrawTo(x + WIN_CROSS_SIZE, y + WIN_CROSS_SIZE); |
---|
| 198 | win_MoveTo(x + WIN_CROSS_SIZE, y - WIN_CROSS_SIZE); |
---|
| 199 | win_DrawTo(x - WIN_CROSS_SIZE, y + WIN_CROSS_SIZE); |
---|
[396b9dd] | 200 | win_MoveTo(x, y); |
---|
| 201 | } else { |
---|
[39e4399a] | 202 | if ((x + WIN_CROSS_SIZE > clip.x_min && |
---|
| 203 | x - WIN_CROSS_SIZE < clip.x_max) || |
---|
| 204 | (y + WIN_CROSS_SIZE > clip.y_min && |
---|
| 205 | y - WIN_CROSS_SIZE < clip.y_max)) { |
---|
[396b9dd] | 206 | fBlankPage = fFalse; |
---|
| 207 | } |
---|
| 208 | } |
---|
[55b7334] | 209 | } |
---|
| 210 | |
---|
[b7b666d] | 211 | static HFONT font_labels, font_default, font_old; |
---|
| 212 | |
---|
[abb82a4] | 213 | static void |
---|
| 214 | win_SetFont(int fontcode) |
---|
| 215 | { |
---|
| 216 | switch (fontcode) { |
---|
| 217 | case PR_FONT_DEFAULT: |
---|
[b7b666d] | 218 | SelectObject(pd, font_default); |
---|
[abb82a4] | 219 | tm = tm_default; |
---|
| 220 | break; |
---|
| 221 | case PR_FONT_LABELS: |
---|
[b7b666d] | 222 | SelectObject(pd, font_labels); |
---|
[abb82a4] | 223 | tm = tm_labels; |
---|
| 224 | break; |
---|
| 225 | default: |
---|
| 226 | BUG("unknown font code"); |
---|
| 227 | } |
---|
| 228 | } |
---|
| 229 | |
---|
[55b7334] | 230 | static void |
---|
| 231 | win_WriteString(const char *s) |
---|
| 232 | { |
---|
[396b9dd] | 233 | if (cur_pass != -1) { |
---|
| 234 | TextOut(pd, x_t, y_t - tm.tmAscent, s, strlen(s)); |
---|
| 235 | } else { |
---|
| 236 | if ((y_t + tm.tmDescent > 0 && |
---|
| 237 | y_t - tm.tmAscent < clip.y_max - clip.y_min) || |
---|
| 238 | (x_t < clip.x_max - clip.x_min && |
---|
| 239 | x_t + strlen(s) * tm.tmAveCharWidth > 0)) { |
---|
| 240 | fBlankPage = fFalse; |
---|
| 241 | } |
---|
| 242 | } |
---|
[55b7334] | 243 | } |
---|
| 244 | |
---|
| 245 | static void |
---|
| 246 | win_DrawCircle(long x, long y, long r) |
---|
| 247 | { |
---|
[396b9dd] | 248 | /* Don't need to check in first-pass - circle is only used in title box */ |
---|
| 249 | if (cur_pass != -1) { |
---|
| 250 | x_t = x - clip.x_min; |
---|
| 251 | y_t = clip.y_max - y; |
---|
| 252 | Ellipse(pd, x_t - r, y_t - r, x_t + r, y_t + r); |
---|
| 253 | } |
---|
[55b7334] | 254 | } |
---|
| 255 | |
---|
[08e95aa] | 256 | static int |
---|
| 257 | win_Charset(void) |
---|
| 258 | { |
---|
| 259 | return CHARSET_ISO_8859_1; |
---|
| 260 | } |
---|
| 261 | |
---|
[55b7334] | 262 | static int |
---|
| 263 | win_Pre(int pagesToPrint, const char *title) |
---|
| 264 | { |
---|
[5824d76] | 265 | PRINTDLGA psd; |
---|
| 266 | DOCINFO info; |
---|
| 267 | |
---|
| 268 | pagesToPrint = pagesToPrint; /* suppress compiler warning */ |
---|
[9d46af2] | 269 | |
---|
[5824d76] | 270 | memset(&psd, 0, sizeof(PRINTDLGA)); |
---|
[d0ffc05] | 271 | psd.lStructSize = 66; |
---|
[5824d76] | 272 | psd.Flags = PD_RETURNDC | PD_RETURNDEFAULT; |
---|
[8bc2567] | 273 | |
---|
[5824d76] | 274 | if (!PrintDlgA(&psd)) exit(1); |
---|
[d0ffc05] | 275 | pd = psd.hDC; |
---|
[5824d76] | 276 | |
---|
[abb82a4] | 277 | font_labels = CreateFont(fontsize_labels, 0, 0, 0, FW_NORMAL, 0, 0, 0, |
---|
| 278 | ANSI_CHARSET, OUT_DEFAULT_PRECIS, |
---|
| 279 | CLIP_DEFAULT_PRECIS, DEFAULT_QUALITY, |
---|
| 280 | FF_DONTCARE | DEFAULT_PITCH, "Arial"); |
---|
| 281 | font_default = CreateFont(fontsize, 0, 0, 0, FW_NORMAL, 0, 0, 0, |
---|
| 282 | ANSI_CHARSET, OUT_DEFAULT_PRECIS, |
---|
| 283 | CLIP_DEFAULT_PRECIS, DEFAULT_QUALITY, |
---|
| 284 | FF_DONTCARE | DEFAULT_PITCH, "Arial"); |
---|
| 285 | font_old = SelectObject(pd, font_labels); |
---|
| 286 | GetTextMetrics(pd, &tm_labels); |
---|
| 287 | SelectObject(pd, font_default); |
---|
| 288 | GetTextMetrics(pd, &tm_default); |
---|
| 289 | |
---|
[8bc2567] | 290 | memset(&info, 0, sizeof(DOCINFO)); |
---|
[d0ffc05] | 291 | info.cbSize = sizeof(DOCINFO); |
---|
[5824d76] | 292 | info.lpszDocName = title; |
---|
| 293 | |
---|
[d0ffc05] | 294 | StartDoc(pd, &info); |
---|
| 295 | return 1; /* only need 1 pass */ |
---|
[55b7334] | 296 | } |
---|
| 297 | |
---|
| 298 | static void |
---|
| 299 | win_NewPage(int pg, int pass, int pagesX, int pagesY) |
---|
| 300 | { |
---|
| 301 | int x, y; |
---|
| 302 | |
---|
[396b9dd] | 303 | x = (pg - 1) % pagesX; |
---|
| 304 | y = pagesY - 1 - ((pg - 1) / pagesX); |
---|
| 305 | |
---|
[f17dbe2] | 306 | clip.x_min = (long)x * xpPageWidth; |
---|
| 307 | clip.y_min = (long)y * ypPageDepth; |
---|
| 308 | clip.x_max = clip.x_min + xpPageWidth; /* dm/pcl/ps had -1; */ |
---|
| 309 | clip.y_max = clip.y_min + ypPageDepth; /* dm/pcl/ps had -1; */ |
---|
| 310 | |
---|
[396b9dd] | 311 | cur_pass = pass; |
---|
[55b7334] | 312 | if (pass == -1) { |
---|
[396b9dd] | 313 | /* Don't count alignment marks, but do count borders */ |
---|
| 314 | fBlankPage = fNoBorder |
---|
| 315 | || (x > 0 && y > 0 && x < pagesX - 1 && y < pagesY - 1); |
---|
[55b7334] | 316 | return; |
---|
| 317 | } |
---|
| 318 | |
---|
| 319 | StartPage(pd); |
---|
[7be0ba8] | 320 | drawticks(clip, 9 * scX / POINTS_PER_MM, x, y); |
---|
[55b7334] | 321 | } |
---|
| 322 | |
---|
| 323 | static void |
---|
| 324 | win_ShowPage(const char *szPageDetails) |
---|
| 325 | { |
---|
[d0ffc05] | 326 | win_MoveTo((long)(6 * scX) + clip.x_min, clip.y_min - (long)(7 * scY)); |
---|
| 327 | win_WriteString(szPageDetails); |
---|
| 328 | EndPage(pd); |
---|
[55b7334] | 329 | } |
---|
| 330 | |
---|
[9d46af2] | 331 | /* Initialise printer routines */ |
---|
[55b7334] | 332 | static void |
---|
[e1fb3cb] | 333 | win_Init(FILE **fh_list, const char *pth, const char *out_fnm, |
---|
[b7b666d] | 334 | double *pscX, double *pscY, bool fCalibrate) |
---|
[55b7334] | 335 | { |
---|
[5824d76] | 336 | PRINTDLGA psd; |
---|
[abb82a4] | 337 | static const char *vars[] = { |
---|
| 338 | "like", |
---|
| 339 | "font_size_labels", |
---|
| 340 | NULL |
---|
| 341 | }; |
---|
| 342 | char **vals; |
---|
| 343 | |
---|
| 344 | fCalibrate = fCalibrate; /* suppress unused argument warning */ |
---|
[e1fb3cb] | 345 | out_fnm = out_fnm; |
---|
[c34796a] | 346 | pth = pth; |
---|
[9d46af2] | 347 | |
---|
[abb82a4] | 348 | vals = ini_read_hier(fh_list, "win", vars); |
---|
| 349 | |
---|
| 350 | fontsize_labels = as_int(vars[2], vals[2], 1, INT_MAX); |
---|
| 351 | fontsize = 10; |
---|
| 352 | |
---|
[5824d76] | 353 | memset(&psd, 0, sizeof(PRINTDLGA)); |
---|
[d0ffc05] | 354 | psd.lStructSize = 66; |
---|
[5824d76] | 355 | psd.Flags = PD_RETURNDC | PD_RETURNDEFAULT; |
---|
| 356 | |
---|
[421b7d2] | 357 | if (!PrintDlgA(&psd)) exit(1); |
---|
[5824d76] | 358 | |
---|
[d0ffc05] | 359 | PaperWidth = GetDeviceCaps(psd.hDC, HORZSIZE); |
---|
| 360 | PaperDepth = GetDeviceCaps(psd.hDC, VERTSIZE); |
---|
| 361 | xpPageWidth = GetDeviceCaps(psd.hDC, HORZRES); |
---|
| 362 | ypPageDepth = GetDeviceCaps(psd.hDC, VERTRES); |
---|
| 363 | MarginLeft = MarginBottom = 0; |
---|
| 364 | MarginRight = PaperWidth; |
---|
| 365 | MarginTop = PaperDepth; |
---|
| 366 | LineWidth = 0; |
---|
| 367 | scX = *pscX = xpPageWidth / PaperWidth; |
---|
| 368 | scY = *pscY = ypPageDepth / PaperDepth; |
---|
| 369 | xpPageWidth--; |
---|
| 370 | ypPageDepth = ypPageDepth - (int)(10 * *pscY); |
---|
| 371 | DeleteDC(psd.hDC); |
---|
[55b7334] | 372 | } |
---|
| 373 | |
---|
| 374 | static void |
---|
| 375 | win_Quit(void) |
---|
| 376 | { |
---|
[abb82a4] | 377 | SelectObject(pd, font_old); |
---|
| 378 | DeleteObject(font_labels); |
---|
| 379 | DeleteObject(font_default); |
---|
[d0ffc05] | 380 | EndDoc(pd); |
---|
| 381 | DeleteDC(pd); |
---|
[55b7334] | 382 | } |
---|