source: git/src/avenprcore.cc @ e90a41e

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

lib/survex.pot,src/: Add options to export dialog to control showing
cross-sections, walls and passages.

  • 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 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.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        : Labels(false), Crosses(false), Shots(true), Surface(false),
53          Xsect(false), Walls(false), Passages(false),
54          SkipBlank(false), Border(true), Cutlines(true), Raw(false),
55          title(), datestamp(), 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()
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
76void
77layout::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 += 30.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
112static void setting_missing(const char *v)
113{
114   fatalerror(/*Parameter “%s” missing in printer configuration file*/85, v);
115}
116
117static 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
123int
124as_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
140unsigned long
141as_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 */
213void
214layout::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 += 30.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
246bool fBlankPage = fFalse;
247
248void 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    old_charset = select_charset(CHARSET_ISO_8859_1);
265
266    l->footer = msgPerm(/*Survey “%s”   Page %d (of %d)   Processed on %s*/167);
267
268    cPagesPrinted = 0;
269    page = state = 0;
270    p = l->szPages;
271    while (1) {
272        if (pageLim == 1) {
273            if (page == 0)
274                page = 1;
275            else
276                page = 0; /* we've already printed the only page */
277        } else if (!*l->szPages) {
278            page++;
279            if (page > pageLim) page = 0; /* all pages printed */
280        } else {
281            page = next_page(&state, &p, pageLim);
282        }
283        SVX_ASSERT(state >= 0); /* errors should have been caught above */
284        if (page == 0) break;
285        cPagesPrinted++;
286        if (l->pages > 1) {
287            putchar('\r');
288            printf(msg166, (int)cPagesPrinted, l->pages);
289        }
290        /* don't skip the page with the info box on */
291        if (l->SkipBlank && (int)page != (l->pagesY - 1) * l->pagesX + 1) {
292            pass = -1;
293            fBlankPage = fTrue;
294        } else {
295            pass = 0;
296            fBlankPage = fFalse;
297        }
298        print_page(m_parent, l, page, pass, cPasses);
299    }
300
301    Quit();
302    select_charset(old_charset);
303}
304#endif
Note: See TracBrowser for help on using the repository browser.