[a80e6f3] | 1 | /* avenprcore.cc |
---|
[79c239e] | 2 | * Printer independent parts of Survex printer drivers |
---|
[a4fd28d] | 3 | * Copyright (C) 1993-2002,2004,2005,2006 Olly Betts |
---|
[79c239e] | 4 | * Copyright (C) 2004 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 | /* FIXME provide more explanation when reporting errors in print.ini */ |
---|
| 22 | |
---|
| 23 | #ifdef HAVE_CONFIG_H |
---|
| 24 | # include <config.h> |
---|
| 25 | #endif |
---|
| 26 | |
---|
| 27 | #include <stdio.h> |
---|
| 28 | #include <stdlib.h> |
---|
| 29 | #include <limits.h> |
---|
| 30 | #include <math.h> |
---|
| 31 | #include <string.h> |
---|
| 32 | #include <ctype.h> |
---|
| 33 | #include <time.h> |
---|
| 34 | #include <float.h> |
---|
| 35 | |
---|
| 36 | #include "mainfrm.h" |
---|
| 37 | |
---|
| 38 | #include "useful.h" |
---|
| 39 | #include "filename.h" |
---|
| 40 | #include "message.h" |
---|
| 41 | #include "filelist.h" |
---|
| 42 | #include "img.h" |
---|
| 43 | #include "avenprcore.h" |
---|
| 44 | #include "debug.h" |
---|
| 45 | |
---|
| 46 | #if defined __WXMSW__ || defined __WXMAC__ |
---|
| 47 | # include <wx/dcprint.h> |
---|
| 48 | #else |
---|
| 49 | # include <wx/dcps.h> |
---|
| 50 | #endif |
---|
| 51 | |
---|
| 52 | layout::layout(wxPageSetupDialogData* data) |
---|
| 53 | : Labels(false), Crosses(false), Shots(true), Surface(false), |
---|
| 54 | SkipBlank(false), Border(true), Cutlines(true), Raw(false), |
---|
| 55 | title(NULL), datestamp(NULL), Scale(0), rot(0), tilt(0), |
---|
| 56 | view(PLAN), scX(1), scY(1), xMin(0), xMax(-1), yMin(0), yMax(-1), |
---|
| 57 | pagesX(1), pagesY(1), pages(1), xOrg(0), yOrg(0), footer(NULL) |
---|
| 58 | { |
---|
| 59 | // Create a temporary wxPrinterDC/wxPostScriptDC so we can get access to |
---|
| 60 | // the size of the printable area in mm to allow us to calculate how many |
---|
| 61 | // pages will be needed. |
---|
| 62 | #if defined __WXMSW__ || defined __WXMAC__ |
---|
| 63 | wxPrinterDC pdc(data->GetPrintData()); |
---|
| 64 | #else |
---|
| 65 | wxPostScriptDC pdc(data->GetPrintData()); |
---|
| 66 | #endif |
---|
| 67 | int width, depth; |
---|
| 68 | pdc.GetSizeMM(&width, &depth); |
---|
| 69 | width -= data->GetMarginBottomRight().x + data->GetMarginTopLeft().x; |
---|
| 70 | PaperWidth = width; |
---|
| 71 | depth -= data->GetMarginBottomRight().y + data->GetMarginTopLeft().y; |
---|
| 72 | // Allow for the 10mm footer. |
---|
| 73 | PaperDepth = depth - 10; |
---|
| 74 | } |
---|
| 75 | |
---|
| 76 | void |
---|
| 77 | layout::pages_required() { |
---|
| 78 | double image_dx, image_dy; |
---|
| 79 | double image_centre_x, image_centre_y; |
---|
| 80 | double paper_centre_x, paper_centre_y; |
---|
| 81 | |
---|
| 82 | double allow = 21.0; |
---|
| 83 | if (!Raw) allow += (view == EXTELEV ? 30.0 : 40.0); |
---|
| 84 | double Sc = 1000 / Scale; |
---|
| 85 | image_dx = (xMax - xMin) * Sc; |
---|
| 86 | if (PaperWidth > 0.0) { |
---|
| 87 | pagesX = (int)ceil((image_dx + 19.0) / PaperWidth); |
---|
| 88 | } else { |
---|
| 89 | /* paperwidth not fixed (eg window or roll printer/plotter) */ |
---|
| 90 | pagesX = 1; |
---|
| 91 | PaperWidth = image_dx + 19.0; |
---|
| 92 | } |
---|
| 93 | paper_centre_x = (pagesX * PaperWidth) / 2; |
---|
| 94 | image_centre_x = Sc * (xMax + xMin) / 2; |
---|
| 95 | xOrg = paper_centre_x - image_centre_x; |
---|
| 96 | |
---|
| 97 | image_dy = (yMax - yMin) * Sc; |
---|
| 98 | if (PaperDepth > 0.0) { |
---|
| 99 | pagesY = (int)ceil((image_dy + allow) / PaperDepth); |
---|
| 100 | } else { |
---|
| 101 | /* paperdepth not fixed (eg window or roll printer/plotter) */ |
---|
| 102 | pagesY = 1; |
---|
| 103 | PaperDepth = image_dy + allow; |
---|
| 104 | } |
---|
| 105 | paper_centre_y = 20 + (pagesY * PaperDepth) / 2; |
---|
| 106 | image_centre_y = Sc * (yMax + yMin) / 2; |
---|
| 107 | yOrg = paper_centre_y - image_centre_y; |
---|
| 108 | |
---|
| 109 | pages = pagesX * pagesY; |
---|
| 110 | } |
---|
| 111 | |
---|
| 112 | static void setting_missing(const char *v) |
---|
| 113 | { |
---|
| 114 | fatalerror(/*Parameter `%s' missing in printer configuration file*/85, v); |
---|
| 115 | } |
---|
| 116 | |
---|
| 117 | static void setting_bad_value(const char *v, const char *p) |
---|
| 118 | { |
---|
| 119 | fatalerror(/*Parameter `%s' has invalid value `%s' in printer configuration file*/82, |
---|
| 120 | v, p); |
---|
| 121 | } |
---|
| 122 | |
---|
| 123 | int |
---|
| 124 | as_int(const char *v, char *p, int min_val, int max_val) |
---|
| 125 | { |
---|
| 126 | long val; |
---|
| 127 | char *pEnd; |
---|
| 128 | if (!p) setting_missing(v); |
---|
| 129 | val = strtol(p, &pEnd, 10); |
---|
| 130 | if (pEnd == p || val < (long)min_val || val > (long)max_val) |
---|
| 131 | setting_bad_value(v, p); |
---|
| 132 | osfree(p); |
---|
| 133 | return (int)val; |
---|
| 134 | } |
---|
| 135 | |
---|
| 136 | /* Converts '0'-'9' to 0-9, 'A'-'F' to 10-15 and 'a'-'f' to 10-15. |
---|
| 137 | * Undefined on other values */ |
---|
| 138 | #define CHAR2HEX(C) (((C)+((C)>64?9:0))&15) |
---|
| 139 | |
---|
| 140 | unsigned long |
---|
| 141 | as_colour(const char *v, char *p) |
---|
| 142 | { |
---|
| 143 | unsigned long val = 0xffffffff; |
---|
| 144 | if (!p) setting_missing(v); |
---|
| 145 | switch (tolower(*p)) { |
---|
| 146 | case '#': { |
---|
| 147 | char *q = p + 1; |
---|
| 148 | while (isxdigit((unsigned char)*q)) q++; |
---|
| 149 | if (q - p == 4) { |
---|
| 150 | val = CHAR2HEX(p[1]) * 0x110000; |
---|
| 151 | val |= CHAR2HEX(p[2]) * 0x1100; |
---|
| 152 | val |= CHAR2HEX(p[3]) * 0x11; |
---|
| 153 | } else if (q - p == 7) { |
---|
| 154 | val = ((CHAR2HEX(p[1]) << 4) | CHAR2HEX(p[2])) << 16; |
---|
| 155 | val |= ((CHAR2HEX(p[3]) << 4) | CHAR2HEX(p[4])) << 8; |
---|
| 156 | val |= (CHAR2HEX(p[5]) << 4) | CHAR2HEX(p[6]); |
---|
| 157 | } |
---|
| 158 | break; |
---|
| 159 | } |
---|
| 160 | case 'a': |
---|
| 161 | if (strcasecmp(p, "aqua") == 0) val = 0x00fffful; |
---|
| 162 | break; |
---|
| 163 | case 'b': |
---|
| 164 | if (strcasecmp(p, "black") == 0) val = 0x000000ul; |
---|
| 165 | else if (strcasecmp(p, "blue") == 0) val = 0x0000fful; |
---|
| 166 | break; |
---|
| 167 | case 'f': |
---|
| 168 | if (strcasecmp(p, "fuchsia") == 0) val = 0xff00fful; |
---|
| 169 | break; |
---|
| 170 | case 'g': |
---|
| 171 | if (strcasecmp(p, "gray") == 0) val = 0x808080ul; |
---|
| 172 | else if (strcasecmp(p, "green") == 0) val = 0x008000ul; |
---|
| 173 | break; |
---|
| 174 | case 'l': |
---|
| 175 | if (strcasecmp(p, "lime") == 0) val = 0x00ff00ul; |
---|
| 176 | break; |
---|
| 177 | case 'm': |
---|
| 178 | if (strcasecmp(p, "maroon") == 0) val = 0x800000ul; |
---|
| 179 | break; |
---|
| 180 | case 'n': |
---|
| 181 | if (strcasecmp(p, "navy") == 0) val = 0x000080ul; |
---|
| 182 | break; |
---|
| 183 | case 'o': |
---|
| 184 | if (strcasecmp(p, "olive") == 0) val = 0x808000ul; |
---|
| 185 | break; |
---|
| 186 | case 'p': |
---|
| 187 | if (strcasecmp(p, "purple") == 0) val = 0x800080ul; |
---|
| 188 | break; |
---|
| 189 | case 'r': |
---|
| 190 | if (strcasecmp(p, "red") == 0) val = 0xff0000ul; |
---|
| 191 | break; |
---|
| 192 | case 's': |
---|
| 193 | if (strcasecmp(p, "silver") == 0) val = 0xc0c0c0ul; |
---|
| 194 | break; |
---|
| 195 | case 't': |
---|
| 196 | if (strcasecmp(p, "teal") == 0) val = 0x008080ul; |
---|
| 197 | break; |
---|
| 198 | case 'w': |
---|
| 199 | if (strcasecmp(p, "white") == 0) val = 0xfffffful; |
---|
| 200 | break; |
---|
| 201 | case 'y': |
---|
| 202 | if (strcasecmp(p, "yellow") == 0) val = 0xffff00ul; |
---|
| 203 | break; |
---|
| 204 | } |
---|
| 205 | if (val == 0xffffffff) setting_bad_value(v, p); |
---|
| 206 | osfree(p); |
---|
| 207 | return val; |
---|
| 208 | } |
---|
| 209 | |
---|
| 210 | #define DEF_RATIO (1.0/(double)DEFAULT_SCALE) |
---|
| 211 | |
---|
| 212 | /* pick a scale which will make it fit in the desired size */ |
---|
| 213 | void |
---|
| 214 | layout::pick_scale(int x, int y) |
---|
| 215 | { |
---|
| 216 | double Sc_x, Sc_y; |
---|
| 217 | /* pagesY = ceil((image_dy+allow)/PaperDepth) |
---|
| 218 | * so (image_dy+allow)/PaperDepth <= pagesY < (image_dy+allow)/PaperDepth+1 |
---|
| 219 | * so image_dy <= pagesY*PaperDepth-allow < image_dy+PaperDepth |
---|
| 220 | * and Sc = image_dy / (yMax-yMin) |
---|
| 221 | * so Sc <= (pagesY*PaperDepth-allow)/(yMax-yMin) < Sc+PaperDepth/(yMax-yMin) |
---|
| 222 | */ |
---|
| 223 | Sc_x = Sc_y = DEF_RATIO; |
---|
| 224 | if (PaperWidth > 0.0 && xMax > xMin) |
---|
| 225 | Sc_x = (x * PaperWidth - 19.0) / (xMax - xMin); |
---|
| 226 | if (PaperDepth > 0.0 && yMax > yMin) { |
---|
| 227 | double allow = 21.0; |
---|
| 228 | if (!Raw) allow += (view == EXTELEV ? 30.0 : 40.0); |
---|
| 229 | Sc_y = (y * PaperDepth - allow) / (yMax - yMin); |
---|
| 230 | } |
---|
| 231 | |
---|
| 232 | Sc_x = min(Sc_x, Sc_y) * 0.99; /* shrink by 1% so we don't cock up */ |
---|
| 233 | #if 0 /* this picks a nice (in some sense) ratio, but is too stingy */ |
---|
| 234 | double E = pow(10.0, floor(log10(Sc_x))); |
---|
| 235 | Sc_x = floor(Sc_x / E) * E; |
---|
| 236 | #endif |
---|
| 237 | |
---|
| 238 | double Scale_exact = 1000.0 / Sc_x; |
---|
| 239 | |
---|
| 240 | /* trim to 2 s.f. (rounding up) */ |
---|
| 241 | double w = pow(10.0, floor(log10(Scale_exact) - 1.0)); |
---|
| 242 | Scale = ceil(Scale_exact / w) * w; |
---|
| 243 | } |
---|
| 244 | |
---|
| 245 | #if 0 |
---|
| 246 | bool fBlankPage = fFalse; |
---|
| 247 | |
---|
| 248 | void print_all(MainFrm *m_parent, layout *l, device *pri) { |
---|
| 249 | int cPasses, pass; |
---|
| 250 | unsigned int cPagesPrinted; |
---|
| 251 | const char *msg166; |
---|
| 252 | int state; |
---|
| 253 | char *p; |
---|
| 254 | int old_charset; |
---|
| 255 | int page, pageLim; |
---|
| 256 | pageLim = l->pagesX*l->pagesY; |
---|
| 257 | PaperWidth = l->PaperWidth; |
---|
| 258 | PaperDepth = l->PaperDepth; |
---|
| 259 | /* if no explicit Alloc, default to one pass */ |
---|
| 260 | cPasses = Pre(l->pages, l->title); |
---|
| 261 | |
---|
| 262 | /* note down so we can switch to printer charset */ |
---|
| 263 | msg166 = msgPerm(/*Page %d of %d*/166); |
---|
| 264 | select_charset(Charset()); |
---|
| 265 | |
---|
| 266 | /* used in printer's native charset in footer */ |
---|
| 267 | l->footer = msgPerm(/*Survey `%s' Page %d (of %d) Processed on %s*/167); |
---|
| 268 | |
---|
| 269 | old_charset = select_charset(CHARSET_ISO_8859_1); |
---|
| 270 | cPagesPrinted = 0; |
---|
| 271 | page = state = 0; |
---|
| 272 | p = l->szPages; |
---|
| 273 | while (1) { |
---|
| 274 | if (pageLim == 1) { |
---|
| 275 | if (page == 0) |
---|
| 276 | page = 1; |
---|
| 277 | else |
---|
| 278 | page = 0; /* we've already printed the only page */ |
---|
| 279 | } else if (!*l->szPages) { |
---|
| 280 | page++; |
---|
| 281 | if (page > pageLim) page = 0; /* all pages printed */ |
---|
| 282 | } else { |
---|
| 283 | page = next_page(&state, &p, pageLim); |
---|
| 284 | } |
---|
| 285 | SVX_ASSERT(state >= 0); /* errors should have been caught above */ |
---|
| 286 | if (page == 0) break; |
---|
| 287 | cPagesPrinted++; |
---|
| 288 | if (l->pages > 1) { |
---|
| 289 | putchar('\r'); |
---|
| 290 | printf(msg166, (int)cPagesPrinted, l->pages); |
---|
| 291 | } |
---|
| 292 | /* don't skip the page with the info box on */ |
---|
| 293 | if (l->SkipBlank && (int)page != (l->pagesY - 1) * l->pagesX + 1) { |
---|
| 294 | pass = -1; |
---|
| 295 | fBlankPage = fTrue; |
---|
| 296 | } else { |
---|
| 297 | pass = 0; |
---|
| 298 | fBlankPage = fFalse; |
---|
| 299 | } |
---|
| 300 | print_page(m_parent, l, page, pass, cPasses); |
---|
| 301 | } |
---|
| 302 | |
---|
| 303 | Quit(); |
---|
| 304 | select_charset(old_charset); |
---|
| 305 | } |
---|
| 306 | #endif |
---|