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
27 #include "avenprcore.h"
29 #if defined __WXMSW__ || defined __WXMAC__
30 # include <wx/dcprint.h>
36 layout::layout(wxPageSetupDialogData
* data
)
41 // Create a temporary wxPrinterDC/wxPostScriptDC so we can get access
42 // to the size of the printable area in mm to allow us to calculate how
43 // many pages will be needed.
45 // It may seem like data->GetPaperSize() would tell us this page size
46 // without having to construct a temporary DC, but that just returns
47 // (0, 0) for the size, at least with wxGTK 3.0.2.
48 #if defined __WXMSW__ || defined __WXMAC__
49 wxPrinterDC
pdc(data
->GetPrintData());
51 wxPostScriptDC
pdc(data
->GetPrintData());
53 // Calculate the size of the printable area in mm to allow us to work
54 // out how many pages will be needed for a given scale.
55 wxSize size
= pdc
.GetSizeMM();
56 size
.DecBy(data
->GetMarginBottomRight());
57 size
.DecBy(data
->GetMarginTopLeft());
59 // Allow for our footer.
60 PaperDepth
= size
.y
- FOOTER_HEIGHT_MM
;
63 PaperWidth
= PaperDepth
= 0;
68 layout::pages_required() {
69 double image_dx
, image_dy
;
70 double image_centre_x
, image_centre_y
;
71 double paper_centre_x
, paper_centre_y
;
74 if (Legend
) allow
+= 30.0;
75 double Sc
= 1000 / Scale
;
76 image_dx
= (xMax
- xMin
) * Sc
;
77 if (PaperWidth
> 0.0) {
78 pagesX
= (int)ceil((image_dx
+ 19.0) / PaperWidth
);
80 /* paperwidth not fixed (eg window or roll printer/plotter) */
82 PaperWidth
= image_dx
+ 19.0;
84 paper_centre_x
= (pagesX
* PaperWidth
) / 2;
85 image_centre_x
= Sc
* (xMax
+ xMin
) / 2;
86 xOrg
= paper_centre_x
- image_centre_x
;
88 image_dy
= (yMax
- yMin
) * Sc
;
89 if (PaperDepth
> 0.0) {
90 pagesY
= (int)ceil((image_dy
+ allow
) / PaperDepth
);
92 /* paperdepth not fixed (eg window or roll printer/plotter) */
94 PaperDepth
= image_dy
+ allow
;
96 paper_centre_y
= 20 + (pagesY
* PaperDepth
) / 2;
97 image_centre_y
= Sc
* (yMax
+ yMin
) / 2;
98 yOrg
= paper_centre_y
- image_centre_y
;
100 pages
= pagesX
* pagesY
;
103 #define DEF_RATIO (1.0/(double)DEFAULT_SCALE)
105 /* pick a scale which will make it fit in the desired size */
107 layout::pick_scale(int x
, int y
)
114 /* pagesY = ceil((image_dy+allow)/PaperDepth)
115 * so (image_dy+allow)/PaperDepth <= pagesY < (image_dy+allow)/PaperDepth+1
116 * so image_dy <= pagesY*PaperDepth-allow < image_dy+PaperDepth
117 * and Sc = image_dy / (yMax-yMin)
118 * so Sc <= (pagesY*PaperDepth-allow)/(yMax-yMin) < Sc+PaperDepth/(yMax-yMin)
120 Sc_x
= Sc_y
= DEF_RATIO
;
121 if (PaperWidth
> 0.0 && xMax
> xMin
)
122 Sc_x
= (x
* PaperWidth
- 19.0) / (xMax
- xMin
);
123 if (PaperDepth
> 0.0 && yMax
> yMin
) {
125 if (Legend
) allow
+= 30.0;
126 Sc_y
= (y
* PaperDepth
- allow
) / (yMax
- yMin
);
129 Sc_x
= std::min(Sc_x
, Sc_y
) * 0.99; /* shrink by 1% so we don't cock up */
130 #if 0 /* this picks a nice (in some sense) ratio, but is too stingy */
131 double E
= pow(10.0, floor(log10(Sc_x
)));
132 Sc_x
= floor(Sc_x
/ E
) * E
;
135 double Scale_exact
= 1000.0 / Sc_x
;
137 /* trim to 2 s.f. (rounding up) */
138 double w
= pow(10.0, floor(log10(Scale_exact
) - 1.0));
139 Scale
= ceil(Scale_exact
/ w
) * w
;