source: git/src/avenprcore.cc @ 53496ab3

RELEASE/1.2debug-cidebug-ci-sanitisersstereowalls-data
Last change on this file since 53496ab3 was 08e858b, checked in by Olly Betts <olly@…>, 10 years ago

lib/,src/avenprcore.cc,src/printwx.cc: Change term "info box" to
"legend".

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