Fix coding style
[survex.git] / src / avenprcore.cc
blob9daf622d3a9f04e21a297952e7ca61953213c2b8
1 /* avenprcore.cc
2 * Printer independent parts of Survex printer drivers
3 * Copyright (C) 1993-2002,2004,2005,2006,2010,2011,2012,2013,2014,2015,2016 Olly Betts
4 * Copyright (C) 2004 Philip Underwood
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.
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.
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
21 #ifdef HAVE_CONFIG_H
22 # include <config.h>
23 #endif
25 #include <algorithm>
26 #include <math.h>
28 #include "useful.h"
29 #include "avenprcore.h"
31 #if defined __WXMSW__ || defined __WXMAC__
32 # include <wx/dcprint.h>
33 #else
34 # include <wx/dcps.h>
35 #endif
36 #include <wx/print.h>
38 layout::layout(wxPageSetupDialogData* data)
39 : show_mask(0),
40 SkipBlank(false), Border(true), Cutlines(true), Legend(true),
41 title(), datestamp(), Scale(0), rot(0), tilt(0),
42 view(PLAN), scX(1), scY(1), xMin(0), xMax(-1), yMin(0), yMax(-1),
43 pagesX(1), pagesY(1), pages(1), xOrg(0), yOrg(0)
45 if (data) {
46 // Printing.
48 // Create a temporary wxPrinterDC/wxPostScriptDC so we can get access
49 // to the size of the printable area in mm to allow us to calculate how
50 // many pages will be needed.
52 // It may seem like data->GetPaperSize() would tell us this page size
53 // without having to construct a temporary DC, but that just returns
54 // (0, 0) for the size, at least with wxGTK 3.0.2.
55 #if defined __WXMSW__ || defined __WXMAC__
56 wxPrinterDC pdc(data->GetPrintData());
57 #else
58 wxPostScriptDC pdc(data->GetPrintData());
59 #endif
60 // Calculate the size of the printable area in mm to allow us to work
61 // out how many pages will be needed for a given scale.
62 wxSize size = pdc.GetSizeMM();
63 size.DecBy(data->GetMarginBottomRight());
64 size.DecBy(data->GetMarginTopLeft());
65 PaperWidth = size.x;
66 // Allow for our footer.
67 PaperDepth = size.y - FOOTER_HEIGHT_MM;
68 } else {
69 // Exporting.
70 PaperWidth = PaperDepth = 0;
74 void
75 layout::pages_required() {
76 double image_dx, image_dy;
77 double image_centre_x, image_centre_y;
78 double paper_centre_x, paper_centre_y;
80 double allow = 21.0;
81 if (Legend) allow += 30.0;
82 double Sc = 1000 / Scale;
83 image_dx = (xMax - xMin) * Sc;
84 if (PaperWidth > 0.0) {
85 pagesX = (int)ceil((image_dx + 19.0) / PaperWidth);
86 } else {
87 /* paperwidth not fixed (eg window or roll printer/plotter) */
88 pagesX = 1;
89 PaperWidth = image_dx + 19.0;
91 paper_centre_x = (pagesX * PaperWidth) / 2;
92 image_centre_x = Sc * (xMax + xMin) / 2;
93 xOrg = paper_centre_x - image_centre_x;
95 image_dy = (yMax - yMin) * Sc;
96 if (PaperDepth > 0.0) {
97 pagesY = (int)ceil((image_dy + allow) / PaperDepth);
98 } else {
99 /* paperdepth not fixed (eg window or roll printer/plotter) */
100 pagesY = 1;
101 PaperDepth = image_dy + allow;
103 paper_centre_y = 20 + (pagesY * PaperDepth) / 2;
104 image_centre_y = Sc * (yMax + yMin) / 2;
105 yOrg = paper_centre_y - image_centre_y;
107 pages = pagesX * pagesY;
110 #define DEF_RATIO (1.0/(double)DEFAULT_SCALE)
112 /* pick a scale which will make it fit in the desired size */
113 void
114 layout::pick_scale(int x, int y)
116 pagesX = x;
117 pagesY = y;
118 pages = x * y;
120 double Sc_x, Sc_y;
121 /* pagesY = ceil((image_dy+allow)/PaperDepth)
122 * so (image_dy+allow)/PaperDepth <= pagesY < (image_dy+allow)/PaperDepth+1
123 * so image_dy <= pagesY*PaperDepth-allow < image_dy+PaperDepth
124 * and Sc = image_dy / (yMax-yMin)
125 * so Sc <= (pagesY*PaperDepth-allow)/(yMax-yMin) < Sc+PaperDepth/(yMax-yMin)
127 Sc_x = Sc_y = DEF_RATIO;
128 if (PaperWidth > 0.0 && xMax > xMin)
129 Sc_x = (x * PaperWidth - 19.0) / (xMax - xMin);
130 if (PaperDepth > 0.0 && yMax > yMin) {
131 double allow = 21.0;
132 if (Legend) allow += 30.0;
133 Sc_y = (y * PaperDepth - allow) / (yMax - yMin);
136 Sc_x = std::min(Sc_x, Sc_y) * 0.99; /* shrink by 1% so we don't cock up */
137 #if 0 /* this picks a nice (in some sense) ratio, but is too stingy */
138 double E = pow(10.0, floor(log10(Sc_x)));
139 Sc_x = floor(Sc_x / E) * E;
140 #endif
142 double Scale_exact = 1000.0 / Sc_x;
144 /* trim to 2 s.f. (rounding up) */
145 double w = pow(10.0, floor(log10(Scale_exact) - 1.0));
146 Scale = ceil(Scale_exact / w) * w;