source: git/src/printing.cc @ bfdf561

stereo-2025 debian/1.4.10-2
Last change on this file since bfdf561 was 90b2149, checked in by Olly Betts <olly@…>, 11 months ago

aven: Add imperial scales for export and printing

Fixes #132, reported by Eric C. Landgraf.

  • Property mode set to 100644
File size: 67.8 KB
RevLine 
[4283d6f]1/* printing.cc */
2/* Aven printing code */
[1a46879]3/* Copyright (C) 1993-2003,2004,2005,2006,2010,2011,2012,2013,2014,2015,2016,2017,2018 Olly Betts
[79c239e]4 * Copyright (C) 2001,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
[9f6ea6c]18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
[79c239e]19 */
20
[4c83f84]21#include <config.h>
[79c239e]22
[255f3269]23#include <wx/confbase.h>
24#include <wx/filename.h>
25#include <wx/print.h>
26#include <wx/printdlg.h>
27#include <wx/spinctrl.h>
28#include <wx/radiobox.h>
29#include <wx/statbox.h>
30#include <wx/valgen.h>
[741d94f]31
[255f3269]32#include <vector>
[741d94f]33
[79c239e]34#include <stdio.h>
35#include <stdlib.h>
36#include <math.h>
37#include <string.h>
38#include <ctype.h>
39#include <float.h>
40#include <limits.h>
41
[5940815]42#include "export.h"
[79c239e]43#include "filelist.h"
44#include "filename.h"
45#include "message.h"
46#include "useful.h"
47
[e0ffc2c]48#include "aven.h"
[79c239e]49#include "avenprcore.h"
50#include "mainfrm.h"
[4283d6f]51#include "printing.h"
[79c239e]52
[255f3269]53using namespace std;
54
[4a66219]55// How many decimal points to show on angles:
56#define ANGLE_DP 1
57
58#if ANGLE_DP == 0
59# define ANGLE_FMT wxT("%03.f")
60# define ANGLE2_FMT wxT("%.f")
61#elif ANGLE_DP == 1
62# define ANGLE_FMT wxT("%05.1f")
63# define ANGLE2_FMT wxT("%.1f")
64#elif ANGLE_DP == 2
65# define ANGLE_FMT wxT("%06.2f")
66# define ANGLE2_FMT wxT("%.2f")
67#else
68# error Need to add ANGLE_FMT and ANGLE2_FMT for the currently set ANGLE_DP
69#endif
70
71static wxString
72format_angle(const wxChar * fmt, double angle)
73{
74    wxString s;
75    s.Printf(fmt, angle);
76    size_t dot = s.find('.');
77    size_t i = s.size();
78    while (i > dot) {
79        --i;
80        if (s[i] != '0') {
81            if (i != dot) ++i;
82            s.resize(i);
83            break;
84        }
85    }
86    s += wmsg(/*°*/344);
87    return s;
88}
89
[d64e7df]90enum {
[de8488a6]91        svx_EXPORT = 1200,
[583c17d]92        svx_FORMAT,
[d64e7df]93        svx_SCALE,
94        svx_BEARING,
95        svx_TILT,
96        svx_LEGS,
97        svx_STATIONS,
98        svx_NAMES,
[e90a41e]99        svx_XSECT,
100        svx_WALLS,
101        svx_PASSAGES,
[d64e7df]102        svx_BORDERS,
103        svx_BLANKS,
[08e858b]104        svx_LEGEND,
[d180604]105        svx_SURFACE,
[ddcf585]106        svx_SPLAYS,
[d64e7df]107        svx_PLAN,
[583c17d]108        svx_ELEV,
[7b55ac2]109        svx_ENTS,
110        svx_FIXES,
111        svx_EXPORTS,
112        svx_GRID,
113        svx_TEXT_HEIGHT,
[bc1fac5]114        svx_MARKER_SIZE,
115        svx_CENTRED,
[32a040e]116        svx_FULLCOORDS,
117        svx_CLAMP_TO_GROUND
[d64e7df]118};
119
[256c4c8]120class BitValidator : public wxValidator {
121    // Disallow assignment.
122    BitValidator & operator=(const BitValidator&);
123
124  protected:
125    int * val;
126
127    int mask;
128
129  public:
130    BitValidator(int * val_, int mask_)
131        : val(val_), mask(mask_) { }
132
[38500b0]133    BitValidator(const BitValidator &o) : wxValidator() {
134        Copy(o);
135    }
136
[256c4c8]137    ~BitValidator() { }
138
[1298787]139    wxObject *Clone() const { return new BitValidator(val, mask); }
[256c4c8]140
[38500b0]141    bool Copy(const BitValidator& o) {
142        wxValidator::Copy(o);
143        val = o.val;
144        mask = o.mask;
145        return true;
146    }
147
[256c4c8]148    bool Validate(wxWindow *) { return true; }
149
150    bool TransferToWindow() {
151        if (!m_validatorWindow->IsKindOf(CLASSINFO(wxCheckBox)))
152            return false;
153        ((wxCheckBox*)m_validatorWindow)->SetValue(*val & mask);
154        return true;
155    }
156
157    bool TransferFromWindow() {
158        if (!m_validatorWindow->IsKindOf(CLASSINFO(wxCheckBox)))
159            return false;
160        if (((wxCheckBox*)m_validatorWindow)->IsChecked())
161            *val |= mask;
162        else
163            *val &= ~mask;
164        return true;
165    }
166};
167
[79c239e]168class svxPrintout : public wxPrintout {
[ce403f1]169    MainFrm *mainfrm;
[79c239e]170    layout *m_layout;
[5a36f76]171    wxPageSetupDialogData* m_data;
[79c239e]172    wxDC* pdc;
[c3e81cf]173    wxFont *font_labels, *font_default;
[3d3a91c]174    // Currently unused, but "skip blank pages" would use it.
[7087afb]175    bool scan_for_blank_pages;
[79c239e]176
[585e9e0]177    wxPen *pen_frame, *pen_cross, *pen_leg, *pen_surface_leg, *pen_splay;
[f0e6d5c]178    wxColour colour_text, colour_labels;
[79c239e]179
180    long x_t, y_t;
181    double font_scaling_x, font_scaling_y;
182
[dc7898c]183    struct {
184        long x_min, y_min, x_max, y_max;
185    } clip;
186
[7087afb]187    bool fBlankPage;
188
[79c239e]189    int check_intersection(long x_p, long y_p);
190    void draw_info_box();
191    void draw_scale_bar(double x, double y, double MaxLength);
192    int next_page(int *pstate, char **q, int pageLim);
[dc7898c]193    void drawticks(int tsize, int x, int y);
[79c239e]194
195    void MOVEMM(double X, double Y) {
196        MoveTo((long)(X * m_layout->scX), (long)(Y * m_layout->scY));
197    }
198    void DRAWMM(double X, double Y) {
199        DrawTo((long)(X * m_layout->scX), (long)(Y * m_layout->scY));
200    }
201    void MoveTo(long x, long y);
202    void DrawTo(long x, long y);
203    void DrawCross(long x, long y);
[c3e81cf]204    void SetFont(wxFont * font) {
205        pdc->SetFont(*font);
206    }
[5627cbb]207    void WriteString(const wxString & s);
[79c239e]208    void DrawEllipse(long x, long y, long r, long R);
209    void SolidRectangle(long x, long y, long w, long h);
210    void NewPage(int pg, int pagesX, int pagesY);
[ee05463]211    void PlotLR(const vector<XSect> & centreline);
212    void PlotUD(const vector<XSect> & centreline);
[79c239e]213  public:
[5a36f76]214    svxPrintout(MainFrm *mainfrm, layout *l, wxPageSetupDialogData *data, const wxString & title);
[79c239e]215    bool OnPrintPage(int pageNum);
[13da582]216    void GetPageInfo(int *minPage, int *maxPage,
217                     int *pageFrom, int *pageTo);
218    bool HasPage(int pageNum);
[79c239e]219    void OnBeginPrinting();
220    void OnEndPrinting();
221};
222
223BEGIN_EVENT_TABLE(svxPrintDlg, wxDialog)
[583c17d]224    EVT_CHOICE(svx_FORMAT, svxPrintDlg::OnChange)
[23a2fe9]225    EVT_TEXT(svx_SCALE, svxPrintDlg::OnChangeScale)
226    EVT_COMBOBOX(svx_SCALE, svxPrintDlg::OnChangeScale)
[cbc9c5e7]227    EVT_SPINCTRLDOUBLE(svx_BEARING, svxPrintDlg::OnChangeSpin)
228    EVT_SPINCTRLDOUBLE(svx_TILT, svxPrintDlg::OnChangeSpin)
[de8488a6]229    EVT_BUTTON(wxID_PRINT, svxPrintDlg::OnPrint)
[5940815]230    EVT_BUTTON(svx_EXPORT, svxPrintDlg::OnExport)
[4ed8154]231    EVT_BUTTON(wxID_CANCEL, svxPrintDlg::OnCancel)
[cca2ce1]232#ifdef AVEN_PRINT_PREVIEW
[de8488a6]233    EVT_BUTTON(wxID_PREVIEW, svxPrintDlg::OnPreview)
[cca2ce1]234#endif
[79c239e]235    EVT_BUTTON(svx_PLAN, svxPrintDlg::OnPlan)
236    EVT_BUTTON(svx_ELEV, svxPrintDlg::OnElevation)
[102ba1d]237    EVT_UPDATE_UI(svx_PLAN, svxPrintDlg::OnPlanUpdate)
238    EVT_UPDATE_UI(svx_ELEV, svxPrintDlg::OnElevationUpdate)
[6b2113d]239    EVT_CHECKBOX(svx_LEGS, svxPrintDlg::OnChange)
240    EVT_CHECKBOX(svx_STATIONS, svxPrintDlg::OnChange)
241    EVT_CHECKBOX(svx_NAMES, svxPrintDlg::OnChange)
242    EVT_CHECKBOX(svx_SURFACE, svxPrintDlg::OnChange)
[ddcf585]243    EVT_CHECKBOX(svx_SPLAYS, svxPrintDlg::OnChange)
[7b55ac2]244    EVT_CHECKBOX(svx_ENTS, svxPrintDlg::OnChange)
245    EVT_CHECKBOX(svx_FIXES, svxPrintDlg::OnChange)
246    EVT_CHECKBOX(svx_EXPORTS, svxPrintDlg::OnChange)
[79c239e]247END_EVENT_TABLE()
248
[4c46260]249static wxString scales[] = {
[5627cbb]250    wxT(""),
251    wxT("25"),
252    wxT("50"),
253    wxT("100"),
254    wxT("250"),
255    wxT("500"),
256    wxT("1000"),
257    wxT("2500"),
258    wxT("5000"),
259    wxT("10000"),
260    wxT("25000"),
261    wxT("50000"),
[23a2fe9]262    wxT("100000"),
[90b2149]263    wxT("240 (1\":20')"),
264    wxT("300 (1\":25')"),
265    // This entry will be "304.8 (1mm:1ft)" but we need to use the
266    // locale-specific decimal point so this gets filled in on first
267    // use, after the locale is initialised.
268#define SCALES_INDEX_MM_TO_FEET 15
269    wxT(""),
270    wxT("480 (1\":40')"),
271    wxT("600 (1\":50')"),
[23a2fe9]272    wxT("...")
[79c239e]273};
274
[59d4fbc7]275// The order of these arrays must match export_format in export.h.
276
[583c17d]277static wxString formats[] = {
[355df41]278    wxT("Survex 3d"),
[13bfd7b]279    wxT("CSV"),
[583c17d]280    wxT("DXF"),
281    wxT("EPS"),
282    wxT("GPX"),
283    wxT("HPGL"),
[1fe107a]284    wxT("JSON"),
[1534ed9]285    wxT("KML"),
[583c17d]286    wxT("Plot"),
[c3f954b]287    wxT("Survex pos"),
[583c17d]288    wxT("SVG")
289};
290
[4d3c8915]291static_assert(sizeof(formats) == FMT_MAX_PLUS_ONE_ * sizeof(formats[0]),
292              "formats[] matches enum export_format");
[583c17d]293
[23a2fe9]294// We discriminate as "One page" isn't valid for exporting.
[f044e2f]295static wxString default_scale_print;
296static wxString default_scale_export;
297
[ce403f1]298svxPrintDlg::svxPrintDlg(MainFrm* mainfrm_, const wxString & filename,
[60d7755]299                         const wxString & title,
[1798716]300                         const wxString & datestamp,
[79c239e]301                         double angle, double tilt_angle,
[5940815]302                         bool labels, bool crosses, bool legs, bool surf,
[ddcf585]303                         bool splays, bool tubes, bool ents, bool fixes,
304                         bool exports, bool printing, bool close_after_)
[6969b17]305        : wxDialog(mainfrm_, wxID_ANY,
306                   wxString(printing ? /* TRANSLATORS: Title of the print
307                                        * dialog */
308                                       wmsg(/*Print*/399) :
309                                       /* TRANSLATORS: Title of the export
310                                        * dialog */
311                                       wmsg(/*Export*/383))),
[ea69247]312          m_layout(printing ? wxGetApp().GetPageSetupDialogData() : NULL),
[4ed8154]313          m_File(filename), mainfrm(mainfrm_), close_after(close_after_)
[79c239e]314{
[5940815]315    m_scale = NULL;
316    m_printSize = NULL;
317    m_bearing = NULL;
318    m_tilt = NULL;
[583c17d]319    m_format = NULL;
[d713e5d]320    int show_mask = 0;
321    if (labels)
322        show_mask |= LABELS;
323    if (crosses)
324        show_mask |= STNS;
325    if (legs)
326        show_mask |= LEGS;
327    if (surf)
328        show_mask |= SURF;
[ddcf585]329    if (splays)
330        show_mask |= SPLAYS;
[fdea415]331    if (tubes)
[5624403]332        show_mask |= XSECT|WALLS|PASG;
[fdea415]333    if (ents)
334        show_mask |= ENTS;
335    if (fixes)
336        show_mask |= FIXES;
337    if (exports)
338        show_mask |= EXPORTS;
[d713e5d]339    m_layout.show_mask = show_mask;
[5627cbb]340    m_layout.datestamp = datestamp;
[4a66219]341    m_layout.rot = angle;
[5627cbb]342    m_layout.title = title;
[eef68f9]343    if (mainfrm->IsExtendedElevation()) {
[79c239e]344        m_layout.view = layout::EXTELEV;
[4a66219]345        if (m_layout.rot != 0.0 && m_layout.rot != 180.0) m_layout.rot = 0;
[79c239e]346        m_layout.tilt = 0;
347    } else {
[4a66219]348        m_layout.tilt = tilt_angle;
349        if (m_layout.tilt == -90.0) {
[79c239e]350            m_layout.view = layout::PLAN;
[4a66219]351        } else if (m_layout.tilt == 0.0) {
[79c239e]352            m_layout.view = layout::ELEV;
353        } else {
354            m_layout.view = layout::TILT;
355        }
356    }
357
358    /* setup our print dialog*/
359    wxBoxSizer* v1 = new wxBoxSizer(wxVERTICAL);
360    wxBoxSizer* h1 = new wxBoxSizer(wxHORIZONTAL); // holds controls
[736f7df]361    /* TRANSLATORS: Used as a label for the surrounding box for the "Bearing"
362     * and "Tilt angle" fields, and the "Plan view" and "Elevation" buttons in
363     * the "what to print/export" dialog. */
[6969b17]364    m_viewbox = new wxStaticBoxSizer(new wxStaticBox(this, wxID_ANY, wmsg(/*View*/283)), wxVERTICAL);
[736f7df]365    /* TRANSLATORS: Used as a label for the surrounding box for the "survey
366     * legs" "stations" "names" etc checkboxes in the "what to print" dialog.
367     * "Elements" isn’t a good name for this but nothing better has yet come to
368     * mind! */
[6969b17]369    wxBoxSizer* v2 = new wxStaticBoxSizer(new wxStaticBox(this, wxID_ANY, wmsg(/*Elements*/256)), wxVERTICAL);
[60d7755]370    wxBoxSizer* h2 = new wxBoxSizer(wxHORIZONTAL); // holds buttons
[79c239e]371
[583c17d]372    if (!printing) {
373        wxStaticText* label;
[6969b17]374        label = new wxStaticText(this, wxID_ANY, wxString(wmsg(/*Export format*/410)));
[9feb252]375        const size_t n_formats = sizeof(formats) / sizeof(formats[0]);
376        m_format = new wxChoice(this, svx_FORMAT,
377                                wxDefaultPosition, wxDefaultSize,
378                                n_formats, formats);
[a322a09]379        unsigned current_format = 0;
[3972b26]380        wxConfigBase * cfg = wxConfigBase::Get();
381        wxString s;
382        if (cfg->Read(wxT("export_format"), &s, wxString())) {
[9feb252]383            for (unsigned i = 0; i != n_formats; ++i) {
[3972b26]384                if (s == formats[i]) {
[a322a09]385                    current_format = i;
[3972b26]386                    break;
387                }
388            }
389        }
[a322a09]390        m_format->SetSelection(current_format);
[583c17d]391        wxBoxSizer* formatbox = new wxBoxSizer(wxHORIZONTAL);
[32bd91b0]392        formatbox->Add(label, 0, wxALIGN_CENTRE_VERTICAL|wxALL, 5);
393        formatbox->Add(m_format, 0, wxALIGN_CENTRE_VERTICAL|wxALL, 5);
[583c17d]394
395        v1->Add(formatbox, 0, wxALIGN_LEFT|wxALL, 0);
396    }
[5940815]397
[90b2149]398    if (scales[SCALES_INDEX_MM_TO_FEET][0] == '\0') {
399        scales[SCALES_INDEX_MM_TO_FEET] = wxString::FromDouble(304.8) + wxT(" (1mm:1ft)");
400    }
[7b55ac2]401    wxStaticText* label;
[6969b17]402    label = new wxStaticText(this, wxID_ANY, wxString(wmsg(/*Scale*/154)) + wxT(" 1:"));
[ad55a7a]403    if (printing && scales[0].empty()) {
404        /* TRANSLATORS: used in the scale drop down selector in the print
405         * dialog the implicit meaning is "choose a suitable scale to fit
406         * the plot on a single page", but we need something shorter */
407        scales[0].assign(wmsg(/*One page*/258));
408    }
[f044e2f]409    wxString default_scale;
410    if (printing) {
411        default_scale = default_scale_print;
412        if (default_scale.empty()) default_scale = scales[0];
413    } else {
414        default_scale = default_scale_export;
415        if (default_scale.empty()) default_scale = wxT("1000");
416    }
[ad55a7a]417    const wxString* scale_list = scales;
418    size_t n_scales = sizeof(scales) / sizeof(scales[0]);
419    if (!printing) {
420        ++scale_list;
421        --n_scales;
[7b55ac2]422    }
[ad55a7a]423    m_scale = new wxComboBox(this, svx_SCALE, default_scale, wxDefaultPosition,
424                             wxDefaultSize, n_scales, scale_list);
[7b55ac2]425    m_scalebox = new wxBoxSizer(wxHORIZONTAL);
[32bd91b0]426    m_scalebox->Add(label, 0, wxALIGN_CENTRE_VERTICAL|wxALL, 5);
427    m_scalebox->Add(m_scale, 0, wxALIGN_CENTRE_VERTICAL|wxALL, 5);
[7b55ac2]428
429    m_viewbox->Add(m_scalebox, 0, wxALIGN_LEFT|wxALL, 0);
[5940815]430
[7b55ac2]431    if (printing) {
[5940815]432        // Make the dummy string wider than any sane value and use that to
433        // fix the width of the control so the sizers allow space for bigger
434        // page layouts.
[6969b17]435        m_printSize = new wxStaticText(this, wxID_ANY, wxString::Format(wmsg(/*%d pages (%dx%d)*/257), 9604, 98, 98));
[7b55ac2]436        m_viewbox->Add(m_printSize, 0, wxALIGN_LEFT|wxALL, 5);
[79c239e]437    }
438
439    if (m_layout.view != layout::EXTELEV) {
[eef68f9]440        wxFlexGridSizer* anglebox = new wxFlexGridSizer(2);
441        wxStaticText * brg_label, * tilt_label;
[6969b17]442        brg_label = new wxStaticText(this, wxID_ANY, wmsg(/*Bearing*/259));
[32bd91b0]443        anglebox->Add(brg_label, 0, wxALIGN_CENTRE_VERTICAL|wxALIGN_LEFT|wxALL, 5);
[3794f78]444        // wSP_WRAP means that you can scroll past 360 to 0, and vice versa.
445        m_bearing = new wxSpinCtrlDouble(this, svx_BEARING, wxEmptyString,
[25aa2bf]446                wxDefaultPosition, wxDefaultSize,
447                wxSP_ARROW_KEYS|wxALIGN_RIGHT|wxSP_WRAP);
[4a66219]448        m_bearing->SetRange(0.0, 360.0);
449        m_bearing->SetDigits(ANGLE_DP);
[32bd91b0]450        anglebox->Add(m_bearing, 0, wxALIGN_CENTRE|wxALL, 5);
[736f7df]451        /* TRANSLATORS: Used in the print dialog: */
[6969b17]452        tilt_label = new wxStaticText(this, wxID_ANY, wmsg(/*Tilt angle*/263));
[32bd91b0]453        anglebox->Add(tilt_label, 0, wxALIGN_CENTRE_VERTICAL|wxALIGN_LEFT|wxALL, 5);
[6ed3e17]454        m_tilt = new wxSpinCtrlDouble(this, svx_TILT, wxEmptyString,
455                wxDefaultPosition, wxDefaultSize,
456                wxSP_ARROW_KEYS|wxALIGN_RIGHT);
[4a66219]457        m_tilt->SetRange(-90.0, 90.0);
458        m_tilt->SetDigits(ANGLE_DP);
[32bd91b0]459        anglebox->Add(m_tilt, 0, wxALIGN_CENTRE|wxALL, 5);
[eef68f9]460
[7b55ac2]461        m_viewbox->Add(anglebox, 0, wxALIGN_LEFT|wxALL, 0);
[eef68f9]462
[79c239e]463        wxBoxSizer * planelevsizer = new wxBoxSizer(wxHORIZONTAL);
[8a78ca1]464        planelevsizer->Add(new wxButton(this, svx_PLAN, wmsg(/*P&lan view*/117)),
[79c239e]465                           0, wxALIGN_CENTRE_VERTICAL|wxALL, 5);
[8a78ca1]466        planelevsizer->Add(new wxButton(this, svx_ELEV, wmsg(/*&Elevation*/285)),
[79c239e]467                           0, wxALIGN_CENTRE_VERTICAL|wxALL, 5);
468
[7b55ac2]469        m_viewbox->Add(planelevsizer, 0, wxALIGN_LEFT|wxALL, 5);
[79c239e]470    }
471
[736f7df]472    /* TRANSLATORS: Here a "survey leg" is a set of measurements between two
473     * "survey stations". */
[60d7755]474    v2->Add(new wxCheckBox(this, svx_LEGS, wmsg(/*Underground Survey Legs*/262),
[256c4c8]475                           wxDefaultPosition, wxDefaultSize, 0,
476                           BitValidator(&m_layout.show_mask, LEGS)),
477            0, wxALIGN_LEFT|wxALL, 2);
[736f7df]478    /* TRANSLATORS: Here a "survey leg" is a set of measurements between two
479     * "survey stations". */
[60d7755]480    v2->Add(new wxCheckBox(this, svx_SURFACE, wmsg(/*Sur&face Survey Legs*/403),
[256c4c8]481                           wxDefaultPosition, wxDefaultSize, 0,
482                           BitValidator(&m_layout.show_mask, SURF)),
483            0, wxALIGN_LEFT|wxALL, 2);
[60d7755]484    v2->Add(new wxCheckBox(this, svx_SPLAYS, wmsg(/*Spla&y Legs*/406),
[ddcf585]485                           wxDefaultPosition, wxDefaultSize, 0,
486                           BitValidator(&m_layout.show_mask, SPLAYS)),
487            0, wxALIGN_LEFT|wxALL, 2);
[60d7755]488    v2->Add(new wxCheckBox(this, svx_STATIONS, wmsg(/*Crosses*/261),
[256c4c8]489                           wxDefaultPosition, wxDefaultSize, 0,
490                           BitValidator(&m_layout.show_mask, STNS)),
491            0, wxALIGN_LEFT|wxALL, 2);
[60d7755]492    v2->Add(new wxCheckBox(this, svx_NAMES, wmsg(/*Station Names*/260),
[256c4c8]493                           wxDefaultPosition, wxDefaultSize, 0,
494                           BitValidator(&m_layout.show_mask, LABELS)),
495            0, wxALIGN_LEFT|wxALL, 2);
[60d7755]496    v2->Add(new wxCheckBox(this, svx_ENTS, wmsg(/*Entrances*/418),
[7b55ac2]497                           wxDefaultPosition, wxDefaultSize, 0,
498                           BitValidator(&m_layout.show_mask, ENTS)),
499            0, wxALIGN_LEFT|wxALL, 2);
[60d7755]500    v2->Add(new wxCheckBox(this, svx_FIXES, wmsg(/*Fixed Points*/419),
[7b55ac2]501                           wxDefaultPosition, wxDefaultSize, 0,
502                           BitValidator(&m_layout.show_mask, FIXES)),
503            0, wxALIGN_LEFT|wxALL, 2);
[60d7755]504    v2->Add(new wxCheckBox(this, svx_EXPORTS, wmsg(/*Exported Stations*/420),
[7b55ac2]505                           wxDefaultPosition, wxDefaultSize, 0,
506                           BitValidator(&m_layout.show_mask, EXPORTS)),
507            0, wxALIGN_LEFT|wxALL, 2);
[60d7755]508    v2->Add(new wxCheckBox(this, svx_XSECT, wmsg(/*Cross-sections*/393),
[256c4c8]509                           wxDefaultPosition, wxDefaultSize, 0,
510                           BitValidator(&m_layout.show_mask, XSECT)),
511            0, wxALIGN_LEFT|wxALL, 2);
512    if (!printing) {
[60d7755]513        v2->Add(new wxCheckBox(this, svx_WALLS, wmsg(/*Walls*/394),
[256c4c8]514                               wxDefaultPosition, wxDefaultSize, 0,
515                               BitValidator(&m_layout.show_mask, WALLS)),
516                0, wxALIGN_LEFT|wxALL, 2);
[36efb03]517        // TRANSLATORS: Label for checkbox which controls whether there's a
518        // layer in the exported file (for formats such as DXF and SVG)
519        // containing polygons for the inside of cave passages).
[60d7755]520        v2->Add(new wxCheckBox(this, svx_PASSAGES, wmsg(/*Passages*/395),
[256c4c8]521                               wxDefaultPosition, wxDefaultSize, 0,
522                               BitValidator(&m_layout.show_mask, PASG)),
523                0, wxALIGN_LEFT|wxALL, 2);
[60d7755]524        v2->Add(new wxCheckBox(this, svx_CENTRED, wmsg(/*Origin in centre*/421),
[bc1fac5]525                               wxDefaultPosition, wxDefaultSize, 0,
526                               BitValidator(&m_layout.show_mask, CENTRED)),
527                0, wxALIGN_LEFT|wxALL, 2);
[60d7755]528        v2->Add(new wxCheckBox(this, svx_FULLCOORDS, wmsg(/*Full coordinates*/422),
[bc1fac5]529                               wxDefaultPosition, wxDefaultSize, 0,
530                               BitValidator(&m_layout.show_mask, FULL_COORDS)),
531                0, wxALIGN_LEFT|wxALL, 2);
[32a040e]532        v2->Add(new wxCheckBox(this, svx_CLAMP_TO_GROUND, wmsg(/*Clamp to ground*/477),
533                               wxDefaultPosition, wxDefaultSize, 0,
534                               BitValidator(&m_layout.show_mask, CLAMP_TO_GROUND)),
535                0, wxALIGN_LEFT|wxALL, 2);
[256c4c8]536    }
[5940815]537    if (printing) {
[736f7df]538        /* TRANSLATORS: used in the print dialog - controls drawing lines
539         * around each page */
[60d7755]540        v2->Add(new wxCheckBox(this, svx_BORDERS, wmsg(/*Page Borders*/264),
[256c4c8]541                               wxDefaultPosition, wxDefaultSize, 0,
542                               wxGenericValidator(&m_layout.Border)),
543                0, wxALIGN_LEFT|wxALL, 2);
[736f7df]544        /* TRANSLATORS: will be used in the print dialog - check this to print
545         * blank pages (otherwise they’ll be skipped to save paper) */
[5627cbb]546//      m_blanks = new wxCheckBox(this, svx_BLANKS, wmsg(/*Blank Pages*/266));
[60d7755]547//      v2->Add(m_blanks, 0, wxALIGN_LEFT|wxALL, 2);
[736f7df]548        /* TRANSLATORS: As in the legend on a map.  Used in the print dialog -
549         * controls drawing the box at the lower left with survey name, view
550         * angles, etc */
[60d7755]551        v2->Add(new wxCheckBox(this, svx_LEGEND, wmsg(/*Legend*/265),
[256c4c8]552                               wxDefaultPosition, wxDefaultSize, 0,
553                               wxGenericValidator(&m_layout.Legend)),
554                0, wxALIGN_LEFT|wxALL, 2);
[5940815]555    }
[ee05463]556
[60d7755]557    h1->Add(v2, 0, wxALIGN_LEFT|wxALL, 5);
[6d3938b]558    h1->Add(m_viewbox, 0, wxALIGN_LEFT|wxLEFT, 5);
[79c239e]559
[6d3938b]560    v1->Add(h1, 0, wxALIGN_LEFT|wxALL, 5);
[9b5a5fd]561
[583c17d]562    // When we enable/disable checkboxes in the export dialog, ideally we'd
563    // like the dialog to resize, but not sure how to achieve that, so we
564    // add a stretchable spacer here so at least the buttons stay in the
565    // lower right corner.
566    v1->AddStretchSpacer();
567
[79c239e]568    wxButton * but;
[73b3388]569    but = new wxButton(this, wxID_CANCEL);
[60d7755]570    h2->Add(but, 0, wxALL, 5);
[5940815]571    if (printing) {
[cca2ce1]572#ifdef AVEN_PRINT_PREVIEW
[de8488a6]573        but = new wxButton(this, wxID_PREVIEW);
[60d7755]574        h2->Add(but, 0, wxALL, 5);
[de8488a6]575        but = new wxButton(this, wxID_PRINT);
[b72f4b5]576#else
[7f928d3]577        but = new wxButton(this, wxID_PRINT, wmsg(/*&Print...*/400));
[b72f4b5]578#endif
[5940815]579    } else {
[736f7df]580        /* TRANSLATORS: The text on the action button in the "Export" settings
581         * dialog */
[7f928d3]582        but = new wxButton(this, svx_EXPORT, wmsg(/*&Export...*/230));
[5940815]583    }
[79c239e]584    but->SetDefault();
[60d7755]585    h2->Add(but, 0, wxALL, 5);
586    v1->Add(h2, 0, wxALIGN_RIGHT|wxALL, 5);
[ee05463]587
[79c239e]588    SetAutoLayout(true);
589    SetSizer(v1);
590    v1->SetSizeHints(this);
591
592    LayoutToUI();
[583c17d]593    SomethingChanged(0);
[79c239e]594}
595
[ee05463]596void
[79c239e]597svxPrintDlg::OnPrint(wxCommandEvent&) {
[583c17d]598    SomethingChanged(0);
[0056ee1]599    TransferDataFromWindow();
[e0ffc2c]600    wxPageSetupDialogData * psdd = wxGetApp().GetPageSetupDialogData();
601    wxPrintDialogData pd(psdd->GetPrintData());
[79c239e]602    wxPrinter pr(&pd);
[e0ffc2c]603    svxPrintout po(mainfrm, &m_layout, psdd, m_File);
[7087afb]604    if (m_layout.SkipBlank) {
605        // FIXME: wx's printing requires a contiguous range of valid page
606        // numbers.  To achieve that, we need to run a scan for blank pages
607        // here, so that GetPageInfo() knows what range to return, and so
608        // that OnPrintPage() can map a page number back to where in the
609        // MxN multi-page layout.
610#if 0
611        po.scan_for_blank_pages = true;
612        for (int page = 1; page <= m_layout->pages; ++page) {
[63d4f07]613            po.fBlankPage = true;
[7087afb]614            po.OnPrintPage(page);
615            // FIXME: Do something with po.fBlankPage
616        }
617        po.scan_for_blank_pages = false;
618#endif
619    }
[79c239e]620    if (pr.Print(this, &po, true)) {
621        // Close the print dialog if printing succeeded.
622        Destroy();
623    }
624}
625
[5940815]626void
627svxPrintDlg::OnExport(wxCommandEvent&) {
628    UIToLayout();
[54b7650]629    TransferDataFromWindow();
[583c17d]630    wxString leaf;
631    wxFileName::SplitPath(m_File, NULL, NULL, &leaf, NULL, wxPATH_NATIVE);
632    unsigned format_idx = ((wxChoice*)FindWindow(svx_FORMAT))->GetSelection();
[4d3c8915]633    const auto& info = export_format_info[format_idx];
634    leaf += wxString::FromUTF8(info.extension);
[583c17d]635
[4d3c8915]636    wxString filespec = wmsg(info.msg_filetype);
[583c17d]637    filespec += wxT("|*");
[4d3c8915]638    filespec += wxString::FromUTF8(info.extension);
[583c17d]639    filespec += wxT("|");
640    filespec += wmsg(/*All files*/208);
641    filespec += wxT("|");
642    filespec += wxFileSelectorDefaultWildcardStr;
643
[736f7df]644    /* TRANSLATORS: Title of file dialog to choose name and type of exported
645     * file. */
[583c17d]646    wxFileDialog dlg(this, wmsg(/*Export as:*/401), wxString(), leaf,
647                     filespec, wxFD_SAVE|wxFD_OVERWRITE_PROMPT);
[5940815]648    if (dlg.ShowModal() == wxID_OK) {
[01c70fc]649        /* FIXME: Set up a way for the user to specify these: */
650        double grid = DEFAULT_GRID_SPACING; // metres
651        double text_height = DEFAULT_TEXT_HEIGHT;
652        double marker_size = DEFAULT_MARKER_SIZE;
[7b55ac2]653
[6d3938b]654        try {
[5c621d3]655            const wxString& export_fnm = dlg.GetPath();
[4d3c8915]656            unsigned mask = info.mask;
[ea93ada]657            double rot, tilt;
[40623c5]658            if (mask & ORIENTABLE) {
[ea93ada]659                rot = m_layout.rot;
660                tilt = m_layout.tilt;
[40623c5]661            } else {
662                rot = 0.0;
663                tilt = -90.0;
[ea93ada]664            }
[5c621d3]665            if (!Export(export_fnm, m_layout.title,
[1a46879]666                        m_layout.datestamp, *mainfrm, mainfrm->GetTreeFilter(),
[ea93ada]667                        rot, tilt, m_layout.get_effective_show_mask(),
[60d7755]668                        export_format(format_idx),
[18ff765]669                        grid, text_height, marker_size, m_layout.Scale)) {
[6d3938b]670                wxString m = wxString::Format(wmsg(/*Couldn’t write file “%s”*/402).c_str(),
[5c621d3]671                                              export_fnm.c_str());
[6d3938b]672                wxGetApp().ReportError(m);
673            }
674        } catch (const wxString & m) {
[6e63fd3]675            wxGetApp().ReportError(m);
[5940815]676        }
677    }
678    Destroy();
679}
680
[cca2ce1]681#ifdef AVEN_PRINT_PREVIEW
[ee05463]682void
[79c239e]683svxPrintDlg::OnPreview(wxCommandEvent&) {
[583c17d]684    SomethingChanged(0);
[54b7650]685    TransferDataFromWindow();
[e0ffc2c]686    wxPageSetupDialogData * psdd = wxGetApp().GetPageSetupDialogData();
687    wxPrintDialogData pd(psdd->GetPrintData());
[79c239e]688    wxPrintPreview* pv;
[e0ffc2c]689    pv = new wxPrintPreview(new svxPrintout(mainfrm, &m_layout, psdd, m_File),
690                            new svxPrintout(mainfrm, &m_layout, psdd, m_File),
[79c239e]691                            &pd);
[736f7df]692    // TRANSLATORS: Title of the print preview dialog
[5627cbb]693    wxPreviewFrame *frame = new wxPreviewFrame(pv, mainfrm, wmsg(/*Print Preview*/398));
[79c239e]694    frame->Initialize();
695
696    // Size preview frame so that all of the controlbar and canvas can be seen
697    // if possible.
698    int w, h;
699    // GetBestSize gives us the width needed to show the whole controlbar.
700    frame->GetBestSize(&w, &h);
701    if (h < w) {
702        // On wxGTK at least, GetBestSize() returns much too small a height.
703        h = w * 6 / 5;
704    }
705    // Ensure that we don't make the window bigger than the screen.
706    // Use wxGetClientDisplayRect() so we don't cover the MS Windows
707    // task bar either.
708    wxRect disp = wxGetClientDisplayRect();
709    if (w > disp.GetWidth()) w = disp.GetWidth();
710    if (h > disp.GetHeight()) h = disp.GetHeight();
711    // Centre the window within the "ClientDisplayRect".
712    int x = disp.GetLeft() + (disp.GetWidth() - w) / 2;
713    int y = disp.GetTop() + (disp.GetHeight() - h) / 2;
714    frame->SetSize(x, y, w, h);
715
716    frame->Show();
717}
[cca2ce1]718#endif
[79c239e]719
[ee05463]720void
[79c239e]721svxPrintDlg::OnPlan(wxCommandEvent&) {
[4a66219]722    m_tilt->SetValue(-90.0);
[583c17d]723    SomethingChanged(svx_TILT);
[79c239e]724}
725
[ee05463]726void
[79c239e]727svxPrintDlg::OnElevation(wxCommandEvent&) {
[4a66219]728    m_tilt->SetValue(0.0);
[583c17d]729    SomethingChanged(svx_TILT);
[79c239e]730}
731
[102ba1d]732void
733svxPrintDlg::OnPlanUpdate(wxUpdateUIEvent& e) {
[4a66219]734    e.Enable(m_tilt->GetValue() != -90.0);
[102ba1d]735}
736
737void
738svxPrintDlg::OnElevationUpdate(wxUpdateUIEvent& e) {
[4a66219]739    e.Enable(m_tilt->GetValue() != 0.0);
[102ba1d]740}
741
[ee05463]742void
[cbc9c5e7]743svxPrintDlg::OnChangeSpin(wxSpinDoubleEvent& e) {
[583c17d]744    SomethingChanged(e.GetId());
[79c239e]745}
746
[ee05463]747void
[583c17d]748svxPrintDlg::OnChange(wxCommandEvent& e) {
[23a2fe9]749    SomethingChanged(e.GetId());
750}
751
752void
753svxPrintDlg::OnChangeScale(wxCommandEvent& e) {
[9311999]754    // Seems to be needed on macOS.
755    if (!m_scale) return;
[23a2fe9]756    wxString value = m_scale->GetValue();
757    if (value == "...") {
758        m_scale->SetValue("");
759        m_scale->SetFocus();
760    } else {
761        default_scale_print = value;
[f044e2f]762        if (default_scale_print != scales[0]) {
[23a2fe9]763            // Don't store "One page" for use when exporting.
[f044e2f]764            default_scale_export = default_scale_print;
765        }
766    }
[583c17d]767    SomethingChanged(e.GetId());
[79c239e]768}
769
[4ed8154]770void
771svxPrintDlg::OnCancel(wxCommandEvent&) {
772    if (close_after)
773        mainfrm->Close();
[d4885c3]774    Destroy();
[4ed8154]775}
776
[79c239e]777void
[583c17d]778svxPrintDlg::SomethingChanged(int control_id) {
[7b55ac2]779    if ((control_id == 0 || control_id == svx_FORMAT) && m_format) {
[583c17d]780        // Update the shown/hidden fields for the newly selected export filter.
[a322a09]781        int new_filter_idx = m_format->GetSelection();
782        if (new_filter_idx != wxNOT_FOUND) {
[4d3c8915]783            unsigned mask = export_format_info[new_filter_idx].mask;
[9feb252]784            static const struct { int id; unsigned mask; } controls[] = {
785                { svx_LEGS, LEGS },
786                { svx_SURFACE, SURF },
[ddcf585]787                { svx_SPLAYS, SPLAYS },
[9feb252]788                { svx_STATIONS, STNS },
789                { svx_NAMES, LABELS },
790                { svx_XSECT, XSECT },
791                { svx_WALLS, WALLS },
792                { svx_PASSAGES, PASG },
793                { svx_ENTS, ENTS },
794                { svx_FIXES, FIXES },
795                { svx_EXPORTS, EXPORTS },
796                { svx_CENTRED, CENTRED },
797                { svx_FULLCOORDS, FULL_COORDS },
[32a040e]798                { svx_CLAMP_TO_GROUND, CLAMP_TO_GROUND },
[9feb252]799            };
800            static unsigned n_controls = sizeof(controls) / sizeof(controls[0]);
801            for (unsigned i = 0; i != n_controls; ++i) {
802                wxWindow * control = FindWindow(controls[i].id);
803                if (control) control->Show(mask & controls[i].mask);
804            }
[7b55ac2]805            m_scalebox->Show(bool(mask & SCALE));
[40623c5]806            m_viewbox->Show(bool(mask & ORIENTABLE));
[a322a09]807            GetSizer()->Layout();
[dbd6e18a]808            // Force the window to resize to match the updated layout.
[4f968e5]809            if (control_id) SetSizerAndFit(GetSizer());
[a322a09]810            if (control_id == svx_FORMAT) {
811                wxConfigBase * cfg = wxConfigBase::Get();
812                cfg->Write(wxT("export_format"), formats[new_filter_idx]);
813            }
814        }
[583c17d]815    }
816
[79c239e]817    UIToLayout();
[18ff765]818
819    if (m_printSize || m_scale) {
820        // Update the bounding box.
821        RecalcBounds();
822
823        if (m_scale) {
[90b2149]824            // Remove the comment part (e.g. `(1":20')`).
825            wxString value = m_scale->GetValue();
826            auto comment = value.find('(');
827            if (comment != value.npos) value.resize(comment);
828            // Strip spaces as trailing spaces cause wxWidgets to fail to
829            // parse.
830            value.Replace(" ", "");
831            // Convert `,` to `.` and parse with ToCDouble() so either decimal
832            // separator works regardless of locale settings.
833            value.Replace(",", ".");
834            if (!value.ToCDouble(&(m_layout.Scale)) ||
[bfa86e2]835                m_layout.Scale == 0.0) {
[18ff765]836                m_layout.pick_scale(1, 1);
837            }
[6ca9f08]838        }
839    }
840
[18ff765]841    if (m_printSize && m_layout.xMax >= m_layout.xMin) {
[79c239e]842        m_layout.pages_required();
[5627cbb]843        m_printSize->SetLabel(wxString::Format(wmsg(/*%d pages (%dx%d)*/257), m_layout.pages, m_layout.pagesX, m_layout.pagesY));
[79c239e]844    }
845}
846
[ee05463]847void
[277a545]848svxPrintDlg::LayoutToUI()
849{
[79c239e]850//    m_blanks->SetValue(m_layout.SkipBlank);
[eef68f9]851    if (m_layout.view != layout::EXTELEV) {
852        m_tilt->SetValue(m_layout.tilt);
853        m_bearing->SetValue(m_layout.rot);
[79c239e]854    }
855
[ad55a7a]856    if (m_scale && m_layout.Scale != 0) {
857        // Do this last as it causes an OnChange message which calls UIToLayout
858        wxString temp;
859        temp << m_layout.Scale;
860        m_scale->SetValue(temp);
[79c239e]861    }
862}
863
[ee05463]864void
[277a545]865svxPrintDlg::UIToLayout()
866{
[79c239e]867//    m_layout.SkipBlank = m_blanks->IsChecked();
868
[de9aa88]869    if (m_layout.view != layout::EXTELEV && m_tilt) {
[79c239e]870        m_layout.tilt = m_tilt->GetValue();
[4a66219]871        if (m_layout.tilt == -90.0) {
[79c239e]872            m_layout.view = layout::PLAN;
[4a66219]873        } else if (m_layout.tilt == 0.0) {
[79c239e]874            m_layout.view = layout::ELEV;
875        } else {
876            m_layout.view = layout::TILT;
877        }
[256c4c8]878
879        bool enable_passage_opts = (m_layout.view != layout::TILT);
880        wxWindow * win;
881        win = FindWindow(svx_XSECT);
882        if (win) win->Enable(enable_passage_opts);
883        win = FindWindow(svx_WALLS);
884        if (win) win->Enable(enable_passage_opts);
885        win = FindWindow(svx_PASSAGES);
886        if (win) win->Enable(enable_passage_opts);
887
[79c239e]888        m_layout.rot = m_bearing->GetValue();
889    }
890}
891
892void
893svxPrintDlg::RecalcBounds()
894{
895    m_layout.yMax = m_layout.xMax = -DBL_MAX;
896    m_layout.yMin = m_layout.xMin = DBL_MAX;
897
[5940815]898    double SIN = sin(rad(m_layout.rot));
899    double COS = cos(rad(m_layout.rot));
900    double SINT = sin(rad(m_layout.tilt));
901    double COST = cos(rad(m_layout.tilt));
[79c239e]902
[1a46879]903    const SurveyFilter* filter = mainfrm->GetTreeFilter();
[4049a36]904    int show_mask = m_layout.get_effective_show_mask();
905    if (show_mask & LEGS) {
[b96edeb]906        for (int f = 0; f != 8; ++f) {
907            if ((show_mask & (f & img_FLAG_SURFACE) ? SURF : LEGS) == 0) {
908                // Not showing traverse because of surface/underground status.
909                continue;
910            }
911            if ((f & img_FLAG_SPLAY) && (show_mask & SPLAYS) == 0) {
912                // Not showing because it's a splay.
[b022b57]913                continue;
[b96edeb]914            }
[1a46879]915            list<traverse>::const_iterator trav = mainfrm->traverses_begin(f, filter);
[b96edeb]916            list<traverse>::const_iterator tend = mainfrm->traverses_end(f);
[1a46879]917            for ( ; trav != tend; trav = mainfrm->traverses_next(f, filter, trav)) {
[b96edeb]918                vector<PointInfo>::const_iterator pos = trav->begin();
919                vector<PointInfo>::const_iterator end = trav->end();
920                for ( ; pos != end; ++pos) {
921                    double x = pos->GetX();
922                    double y = pos->GetY();
923                    double z = pos->GetZ();
924                    double X = x * COS - y * SIN;
925                    if (X > m_layout.xMax) m_layout.xMax = X;
926                    if (X < m_layout.xMin) m_layout.xMin = X;
927                    double Y = z * COST - (x * SIN + y * COS) * SINT;
928                    if (Y > m_layout.yMax) m_layout.yMax = Y;
929                    if (Y < m_layout.yMin) m_layout.yMin = Y;
930                }
[ce403f1]931            }
932        }
933    }
[fdbeeebb]934
[4049a36]935    if ((show_mask & XSECT) &&
[fdbeeebb]936        (m_layout.tilt == 0.0 || m_layout.tilt == 90.0 || m_layout.tilt == -90.0)) {
[d7078b4]937        list<vector<XSect>>::const_iterator trav = mainfrm->tubes_begin();
938        list<vector<XSect>>::const_iterator tend = mainfrm->tubes_end();
[fdbeeebb]939        for ( ; trav != tend; ++trav) {
[672459c]940            const XSect* prev_pt_v = NULL;
[fdbeeebb]941            Vector3 last_right(1.0, 0.0, 0.0);
942
943            vector<XSect>::const_iterator i = trav->begin();
944            vector<XSect>::size_type segment = 0;
945            while (i != trav->end()) {
946                // get the coordinates of this vertex
947                const XSect & pt_v = *i++;
948                if (m_layout.tilt == 0.0) {
[147847c]949                    double u = pt_v.GetU();
950                    double d = pt_v.GetD();
[fdbeeebb]951
952                    if (u >= 0 || d >= 0) {
[672459c]953                        if (filter && !filter->CheckVisible(pt_v.GetLabel()))
954                            continue;
955
[fdbeeebb]956                        double x = pt_v.GetX();
957                        double y = pt_v.GetY();
958                        double z = pt_v.GetZ();
959                        double X = x * COS - y * SIN;
960                        double Y = z * COST - (x * SIN + y * COS) * SINT;
961
962                        if (X > m_layout.xMax) m_layout.xMax = X;
963                        if (X < m_layout.xMin) m_layout.xMin = X;
964                        double U = Y + max(0.0, pt_v.GetU());
965                        if (U > m_layout.yMax) m_layout.yMax = U;
966                        double D = Y - max(0.0, pt_v.GetD());
967                        if (D < m_layout.yMin) m_layout.yMin = D;
968                    }
969                } else {
970                    // More complex, and this duplicates the algorithm from
971                    // PlotLR() - we should try to share that, maybe via a
972                    // template.
973                    Vector3 right;
974
975                    const Vector3 up_v(0.0, 0.0, 1.0);
976
977                    if (segment == 0) {
978                        assert(i != trav->end());
979                        // first segment
980
981                        // get the coordinates of the next vertex
982                        const XSect & next_pt_v = *i;
983
984                        // calculate vector from this pt to the next one
985                        Vector3 leg_v = next_pt_v - pt_v;
986
987                        // obtain a vector in the LRUD plane
988                        right = leg_v * up_v;
989                        if (right.magnitude() == 0) {
990                            right = last_right;
991                        } else {
992                            last_right = right;
993                        }
994                    } else if (segment + 1 == trav->size()) {
995                        // last segment
996
997                        // Calculate vector from the previous pt to this one.
[672459c]998                        Vector3 leg_v = pt_v - *prev_pt_v;
[fdbeeebb]999
1000                        // Obtain a horizontal vector in the LRUD plane.
1001                        right = leg_v * up_v;
1002                        if (right.magnitude() == 0) {
1003                            right = Vector3(last_right.GetX(), last_right.GetY(), 0.0);
1004                        } else {
1005                            last_right = right;
1006                        }
1007                    } else {
1008                        assert(i != trav->end());
1009                        // Intermediate segment.
1010
1011                        // Get the coordinates of the next vertex.
1012                        const XSect & next_pt_v = *i;
1013
1014                        // Calculate vectors from this vertex to the
1015                        // next vertex, and from the previous vertex to
1016                        // this one.
[672459c]1017                        Vector3 leg1_v = pt_v - *prev_pt_v;
[fdbeeebb]1018                        Vector3 leg2_v = next_pt_v - pt_v;
1019
1020                        // Obtain horizontal vectors perpendicular to
1021                        // both legs, then normalise and average to get
1022                        // a horizontal bisector.
1023                        Vector3 r1 = leg1_v * up_v;
1024                        Vector3 r2 = leg2_v * up_v;
1025                        r1.normalise();
1026                        r2.normalise();
1027                        right = r1 + r2;
1028                        if (right.magnitude() == 0) {
1029                            // This is the "mid-pitch" case...
1030                            right = last_right;
1031                        }
1032                        last_right = right;
1033                    }
1034
1035                    // Scale to unit vectors in the LRUD plane.
1036                    right.normalise();
1037
[147847c]1038                    double l = pt_v.GetL();
1039                    double r = pt_v.GetR();
[fdbeeebb]1040
1041                    if (l >= 0 || r >= 0) {
[672459c]1042                        if (!filter || filter->CheckVisible(pt_v.GetLabel())) {
1043                            // Get the x and y coordinates of the survey station
1044                            double pt_X = pt_v.GetX() * COS - pt_v.GetY() * SIN;
1045                            double pt_Y = pt_v.GetX() * SIN + pt_v.GetY() * COS;
1046
1047                            double X, Y;
1048                            if (l >= 0) {
1049                                // Get the x and y coordinates of the end of the left arrow
1050                                Vector3 p = pt_v.GetPoint() - right * l;
1051                                X = p.GetX() * COS - p.GetY() * SIN;
1052                                Y = (p.GetX() * SIN + p.GetY() * COS);
1053                            } else {
1054                                X = pt_X;
1055                                Y = pt_Y;
1056                            }
1057                            if (X > m_layout.xMax) m_layout.xMax = X;
1058                            if (X < m_layout.xMin) m_layout.xMin = X;
1059                            if (Y > m_layout.yMax) m_layout.yMax = Y;
1060                            if (Y < m_layout.yMin) m_layout.yMin = Y;
1061
1062                            if (r >= 0) {
1063                                // Get the x and y coordinates of the end of the right arrow
1064                                Vector3 p = pt_v.GetPoint() + right * r;
1065                                X = p.GetX() * COS - p.GetY() * SIN;
1066                                Y = (p.GetX() * SIN + p.GetY() * COS);
1067                            } else {
1068                                X = pt_X;
1069                                Y = pt_Y;
1070                            }
1071                            if (X > m_layout.xMax) m_layout.xMax = X;
1072                            if (X < m_layout.xMin) m_layout.xMin = X;
1073                            if (Y > m_layout.yMax) m_layout.yMax = Y;
1074                            if (Y < m_layout.yMin) m_layout.yMin = Y;
[fdbeeebb]1075                        }
1076                    }
1077
[672459c]1078                    prev_pt_v = &pt_v;
[fdbeeebb]1079
1080                    ++segment;
1081                }
1082            }
1083        }
1084    }
1085
[4049a36]1086    if (show_mask & (LABELS|STNS)) {
[672459c]1087        for (auto label = mainfrm->GetLabels();
1088             label != mainfrm->GetLabelsEnd();
1089             ++label) {
1090            if (filter && !filter->CheckVisible((*label)->GetText()))
1091                continue;
[79c239e]1092            double x = (*label)->GetX();
1093            double y = (*label)->GetY();
1094            double z = (*label)->GetZ();
[4049a36]1095            if ((show_mask & SURF) || (*label)->IsUnderground()) {
[79c239e]1096                double X = x * COS - y * SIN;
1097                if (X > m_layout.xMax) m_layout.xMax = X;
1098                if (X < m_layout.xMin) m_layout.xMin = X;
[7a57dc7]1099                double Y = z * COST - (x * SIN + y * COS) * SINT;
[79c239e]1100                if (Y > m_layout.yMax) m_layout.yMax = Y;
1101                if (Y < m_layout.yMin) m_layout.yMin = Y;
1102            }
1103        }
1104    }
1105}
1106
1107static int xpPageWidth, ypPageDepth;
1108static long x_offset, y_offset;
1109static int fontsize, fontsize_labels;
1110
1111/* FIXME: allow the font to be set */
1112
1113static const char *fontname = "Arial", *fontname_labels = "Arial";
1114
[5627cbb]1115svxPrintout::svxPrintout(MainFrm *mainfrm_, layout *l,
1116                         wxPageSetupDialogData *data, const wxString & title)
[7087afb]1117    : wxPrintout(title), font_labels(NULL), font_default(NULL),
1118      scan_for_blank_pages(false)
[79c239e]1119{
[ce403f1]1120    mainfrm = mainfrm_;
[79c239e]1121    m_layout = l;
1122    m_data = data;
1123}
1124
1125void
1126svxPrintout::draw_info_box()
1127{
1128   layout *l = m_layout;
[995cf6a]1129   int boxwidth = 70;
[79c239e]1130   int boxheight = 30;
1131
[f0e6d5c]1132   pdc->SetPen(*pen_frame);
[79c239e]1133
[995cf6a]1134   int div = boxwidth;
[79c239e]1135   if (l->view != layout::EXTELEV) {
[995cf6a]1136      boxwidth += boxheight;
1137      MOVEMM(div, boxheight);
1138      DRAWMM(div, 0);
1139      MOVEMM(0, 30); DRAWMM(div, 30);
[79c239e]1140   }
1141
1142   MOVEMM(0, boxheight);
1143   DRAWMM(boxwidth, boxheight);
1144   DRAWMM(boxwidth, 0);
1145   if (!l->Border) {
1146      DRAWMM(0, 0);
1147      DRAWMM(0, boxheight);
1148   }
1149
[995cf6a]1150   MOVEMM(0, 20); DRAWMM(div, 20);
1151   MOVEMM(0, 10); DRAWMM(div, 10);
[79c239e]1152
1153   switch (l->view) {
1154    case layout::PLAN: {
1155      long ax, ay, bx, by, cx, cy, dx, dy;
1156
[995cf6a]1157      long xc = boxwidth - boxheight / 2;
1158      long yc = boxheight / 2;
[725cd74d]1159      const double RADIUS = boxheight / 3;
[995cf6a]1160      DrawEllipse(long(xc * l->scX), long(yc * l->scY),
1161                  long(RADIUS * l->scX), long(RADIUS * l->scY));
1162
1163      ax = (long)((xc - (RADIUS - 1) * sin(rad(000.0 + l->rot))) * l->scX);
1164      ay = (long)((yc + (RADIUS - 1) * cos(rad(000.0 + l->rot))) * l->scY);
1165      bx = (long)((xc - RADIUS * 0.5 * sin(rad(180.0 + l->rot))) * l->scX);
1166      by = (long)((yc + RADIUS * 0.5 * cos(rad(180.0 + l->rot))) * l->scY);
1167      cx = (long)((xc - (RADIUS - 1) * sin(rad(160.0 + l->rot))) * l->scX);
1168      cy = (long)((yc + (RADIUS - 1) * cos(rad(160.0 + l->rot))) * l->scY);
1169      dx = (long)((xc - (RADIUS - 1) * sin(rad(200.0 + l->rot))) * l->scX);
1170      dy = (long)((yc + (RADIUS - 1) * cos(rad(200.0 + l->rot))) * l->scY);
[79c239e]1171
1172      MoveTo(ax, ay);
1173      DrawTo(bx, by);
1174      DrawTo(cx, cy);
1175      DrawTo(ax, ay);
1176      DrawTo(dx, dy);
1177      DrawTo(bx, by);
1178
[04c9a6d]1179      pdc->SetTextForeground(colour_text);
[725cd74d]1180      MOVEMM(div + 0.5, boxheight - 5.5);
[5627cbb]1181      WriteString(wmsg(/*North*/115));
[79c239e]1182
[4a66219]1183      wxString angle = format_angle(ANGLE_FMT, l->rot);
[995cf6a]1184      wxString s;
[736f7df]1185      /* TRANSLATORS: This is used on printouts of plans, with %s replaced by
1186       * something like "123°".  The bearing is up the page. */
[995cf6a]1187      s.Printf(wmsg(/*Plan view, %s up page*/168), angle.c_str());
1188      MOVEMM(2, 12); WriteString(s);
[79c239e]1189      break;
1190    }
[995cf6a]1191    case layout::ELEV: case layout::TILT: {
1192      const int L = div + 2;
1193      const int R = boxwidth - 2;
[725cd74d]1194      const int H = boxheight / 2;
[995cf6a]1195      MOVEMM(L, H); DRAWMM(L + 5, H - 3); DRAWMM(L + 3, H); DRAWMM(L + 5, H + 3);
[79c239e]1196
[995cf6a]1197      DRAWMM(L, H); DRAWMM(R, H);
[79c239e]1198
[995cf6a]1199      DRAWMM(R - 5, H + 3); DRAWMM(R - 3, H); DRAWMM(R - 5, H - 3); DRAWMM(R, H);
[79c239e]1200
[995cf6a]1201      MOVEMM((L + R) / 2, H - 2); DRAWMM((L + R) / 2, H + 2);
[79c239e]1202
[04c9a6d]1203      pdc->SetTextForeground(colour_text);
[725cd74d]1204      MOVEMM(div + 2, boxheight - 8);
[736f7df]1205      /* TRANSLATORS: "Elevation on" 020 <-> 200 degrees */
[5627cbb]1206      WriteString(wmsg(/*Elevation on*/116));
[b49ac56]1207
[725cd74d]1208      MOVEMM(L, 2);
[4a66219]1209      WriteString(format_angle(ANGLE_FMT, fmod(l->rot + 270.0, 360.0)));
[725cd74d]1210      MOVEMM(R - 10, 2);
[4a66219]1211      WriteString(format_angle(ANGLE_FMT, fmod(l->rot + 90.0, 360.0)));
[995cf6a]1212
[4a66219]1213      wxString angle = format_angle(ANGLE_FMT, l->rot);
[995cf6a]1214      wxString s;
1215      if (l->view == layout::ELEV) {
[736f7df]1216          /* TRANSLATORS: This is used on printouts of elevations, with %s
1217           * replaced by something like "123°".  The bearing is the direction
1218           * we’re looking. */
[995cf6a]1219          s.Printf(wmsg(/*Elevation facing %s*/169), angle.c_str());
1220      } else {
[4a66219]1221          wxString a2 = format_angle(ANGLE2_FMT, l->tilt);
[736f7df]1222          /* TRANSLATORS: This is used on printouts of tilted elevations, with
1223           * the first %s replaced by something like "123°", and the second by
1224           * something like "-45°".  The bearing is the direction we’re
1225           * looking. */
[995cf6a]1226          s.Printf(wmsg(/*Elevation facing %s, tilted %s*/284), angle.c_str(), a2.c_str());
1227      }
1228      MOVEMM(2, 12); WriteString(s);
[79c239e]1229      break;
[995cf6a]1230    }
[79c239e]1231    case layout::EXTELEV:
[04c9a6d]1232      pdc->SetTextForeground(colour_text);
[f6dff8b]1233      MOVEMM(2, 12);
[736f7df]1234      /* TRANSLATORS: This is used on printouts of extended elevations. */
[5627cbb]1235      WriteString(wmsg(/*Extended elevation*/191));
[79c239e]1236      break;
1237   }
1238
[f6dff8b]1239   MOVEMM(2, boxheight - 8); WriteString(l->title);
[79c239e]1240
[995cf6a]1241   MOVEMM(2, 2);
1242   // FIXME: "Original Scale" better?
[5627cbb]1243   WriteString(wxString::Format(wmsg(/*Scale*/154) + wxT(" 1:%.0f"),
1244                                l->Scale));
[79c239e]1245
1246   /* This used to be a copyright line, but it was occasionally
1247    * mis-interpreted as us claiming copyright on the survey, so let's
1248    * give the website URL instead */
1249   MOVEMM(boxwidth + 2, 2);
[d417499]1250   WriteString(wxT("Survex " VERSION " - https://survex.com/"));
[79c239e]1251
1252   draw_scale_bar(boxwidth + 10.0, 17.0, l->PaperWidth - boxwidth - 18.0);
1253}
1254
1255/* Draw fancy scale bar with bottom left at (x,y) (both in mm) and at most */
1256/* MaxLength mm long. The scaling in use is 1:scale */
1257void
1258svxPrintout::draw_scale_bar(double x, double y, double MaxLength)
1259{
1260   double StepEst, d;
[5627cbb]1261   int E, Step, n, c;
1262   wxString buf;
[79c239e]1263   /* Limit scalebar to 20cm to stop people with A0 plotters complaining */
1264   if (MaxLength > 200.0) MaxLength = 200.0;
1265
1266#define dmin 10.0      /* each division >= dmin mm long */
1267#define StepMax 5      /* number in steps of at most StepMax (x 10^N) */
1268#define epsilon (1e-4) /* fudge factor to prevent rounding problems */
1269
1270   E = (int)ceil(log10((dmin * 0.001 * m_layout->Scale) / StepMax));
1271   StepEst = pow(10.0, -(double)E) * (dmin * 0.001) * m_layout->Scale - epsilon;
1272
1273   /* Force labelling to be in multiples of 1, 2, or 5 */
1274   Step = (StepEst <= 1.0 ? 1 : (StepEst <= 2.0 ? 2 : 5));
1275
1276   /* Work out actual length of each scale bar division */
1277   d = Step * pow(10.0, (double)E) / m_layout->Scale * 1000.0;
1278
[ccb83b7]1279   /* FIXME: Non-metric units here... */
[79c239e]1280   /* Choose appropriate units, s.t. if possible E is >=0 and minimized */
[ccb83b7]1281   int units;
1282   if (E >= 3) {
1283      E -= 3;
1284      units = /*km*/423;
1285   } else if (E >= 0) {
1286      units = /*m*/424;
1287   } else {
1288      E += 2;
1289      units = /*cm*/425;
1290   }
[79c239e]1291
[5627cbb]1292   buf = wmsg(/*Scale*/154);
[79c239e]1293
1294   /* Add units used - eg. "Scale (10m)" */
[ccb83b7]1295   double pow10_E = pow(10.0, (double)E);
1296   if (E >= 0) {
1297      buf += wxString::Format(wxT(" (%.f%s)"), pow10_E, wmsg(units).c_str());
1298   } else {
1299      int sf = -(int)floor(E);
1300      buf += wxString::Format(wxT(" (%.*f%s)"), sf, pow10_E, wmsg(units).c_str());
1301   }
[04c9a6d]1302   pdc->SetTextForeground(colour_text);
[79c239e]1303   MOVEMM(x, y + 4); WriteString(buf);
1304
1305   /* Work out how many divisions there will be */
1306   n = (int)(MaxLength / d);
1307
[f0e6d5c]1308   pdc->SetPen(*pen_frame);
[79c239e]1309
1310   long Y = long(y * m_layout->scY);
1311   long Y2 = long((y + 3) * m_layout->scY);
1312   long X = long(x * m_layout->scX);
1313   long X2 = long((x + n * d) * m_layout->scX);
1314
1315   /* Draw top of scale bar */
1316   MoveTo(X2, Y2);
1317   DrawTo(X, Y2);
1318#if 0
1319   DrawTo(X2, Y);
1320   DrawTo(X, Y);
1321   MOVEMM(x + n * d, y); DRAWMM(x, y);
1322#endif
1323   /* Draw divisions and label them */
1324   for (c = 0; c <= n; c++) {
[f0e6d5c]1325      pdc->SetPen(*pen_frame);
[79c239e]1326      X = long((x + c * d) * m_layout->scX);
1327      MoveTo(X, Y);
1328      DrawTo(X, Y2);
1329#if 0 // Don't waste toner!
1330      /* Draw a "zebra crossing" scale bar. */
1331      if (c < n && (c & 1) == 0) {
1332          X2 = long((x + (c + 1) * d) * m_layout->scX);
1333          SolidRectangle(X, Y, X2 - X, Y2 - Y);
1334      }
1335#endif
[5627cbb]1336      buf.Printf(wxT("%d"), c * Step);
[04c9a6d]1337      pdc->SetTextForeground(colour_text);
[30f1caa]1338      MOVEMM(x + c * d - buf.length(), y - 5);
[79c239e]1339      WriteString(buf);
1340   }
1341}
1342
1343#if 0
[ee05463]1344void
[79c239e]1345make_calibration(layout *l) {
1346      img_point pt = { 0.0, 0.0, 0.0 };
1347      l->xMax = l->yMax = 0.1;
1348      l->xMin = l->yMin = 0;
1349
[f6c92f1]1350      stack(l, img_MOVE, NULL, &pt);
[79c239e]1351      pt.x = 0.1;
[f6c92f1]1352      stack(l, img_LINE, NULL, &pt);
[79c239e]1353      pt.y = 0.1;
[f6c92f1]1354      stack(l, img_LINE, NULL, &pt);
[79c239e]1355      pt.x = 0.0;
[f6c92f1]1356      stack(l, img_LINE, NULL, &pt);
[79c239e]1357      pt.y = 0.0;
[f6c92f1]1358      stack(l, img_LINE, NULL, &pt);
[79c239e]1359      pt.x = 0.05;
1360      pt.y = 0.001;
[f6c92f1]1361      stack(l, img_LABEL, "10cm", &pt);
[79c239e]1362      pt.x = 0.001;
1363      pt.y = 0.05;
[f6c92f1]1364      stack(l, img_LABEL, "10cm", &pt);
[79c239e]1365      l->Scale = 1.0;
1366}
1367#endif
1368
1369int
1370svxPrintout::next_page(int *pstate, char **q, int pageLim)
1371{
1372   char *p;
1373   int page;
1374   int c;
1375   p = *q;
1376   if (*pstate > 0) {
1377      /* doing a range */
1378      (*pstate)++;
[bcbd681]1379      wxASSERT(*p == '-');
[79c239e]1380      p++;
1381      while (isspace((unsigned char)*p)) p++;
1382      if (sscanf(p, "%u%n", &page, &c) > 0) {
1383         p += c;
1384      } else {
1385         page = pageLim;
1386      }
1387      if (*pstate > page) goto err;
1388      if (*pstate < page) return *pstate;
1389      *q = p;
1390      *pstate = 0;
1391      return page;
1392   }
1393
1394   while (isspace((unsigned char)*p) || *p == ',') p++;
1395
1396   if (!*p) return 0; /* done */
1397
1398   if (*p == '-') {
1399      *q = p;
1400      *pstate = 1;
1401      return 1; /* range with initial parameter omitted */
1402   }
1403   if (sscanf(p, "%u%n", &page, &c) > 0) {
1404      p += c;
1405      while (isspace((unsigned char)*p)) p++;
1406      *q = p;
1407      if (0 < page && page <= pageLim) {
1408         if (*p == '-') *pstate = page; /* range with start */
1409         return page;
1410      }
1411   }
1412   err:
1413   *pstate = -1;
1414   return 0;
1415}
1416
1417/* Draws in alignment marks on each page or borders on edge pages */
1418void
[dc7898c]1419svxPrintout::drawticks(int tsize, int x, int y)
[79c239e]1420{
1421   long i;
1422   int s = tsize * 4;
1423   int o = s / 8;
[63d4f07]1424   bool fAtCorner = false;
[f0e6d5c]1425   pdc->SetPen(*pen_frame);
[79c239e]1426   if (x == 0 && m_layout->Border) {
1427      /* solid left border */
1428      MoveTo(clip.x_min, clip.y_min);
1429      DrawTo(clip.x_min, clip.y_max);
[63d4f07]1430      fAtCorner = true;
[79c239e]1431   } else {
1432      if (x > 0 || y > 0) {
1433         MoveTo(clip.x_min, clip.y_min);
1434         DrawTo(clip.x_min, clip.y_min + tsize);
1435      }
1436      if (s && x > 0 && m_layout->Cutlines) {
1437         /* dashed left border */
1438         i = (clip.y_max - clip.y_min) -
1439             (tsize + ((clip.y_max - clip.y_min - tsize * 2L) % s) / 2);
1440         for ( ; i > tsize; i -= s) {
1441            MoveTo(clip.x_min, clip.y_max - (i + o));
1442            DrawTo(clip.x_min, clip.y_max - (i - o));
1443         }
1444      }
1445      if (x > 0 || y < m_layout->pagesY - 1) {
1446         MoveTo(clip.x_min, clip.y_max - tsize);
1447         DrawTo(clip.x_min, clip.y_max);
[63d4f07]1448         fAtCorner = true;
[79c239e]1449      }
1450   }
1451
1452   if (y == m_layout->pagesY - 1 && m_layout->Border) {
1453      /* solid top border */
1454      if (!fAtCorner) MoveTo(clip.x_min, clip.y_max);
1455      DrawTo(clip.x_max, clip.y_max);
[63d4f07]1456      fAtCorner = true;
[79c239e]1457   } else {
1458      if (y < m_layout->pagesY - 1 || x > 0) {
1459         if (!fAtCorner) MoveTo(clip.x_min, clip.y_max);
1460         DrawTo(clip.x_min + tsize, clip.y_max);
1461      }
1462      if (s && y < m_layout->pagesY - 1 && m_layout->Cutlines) {
1463         /* dashed top border */
1464         i = (clip.x_max - clip.x_min) -
1465             (tsize + ((clip.x_max - clip.x_min - tsize * 2L) % s) / 2);
1466         for ( ; i > tsize; i -= s) {
1467            MoveTo(clip.x_max - (i + o), clip.y_max);
1468            DrawTo(clip.x_max - (i - o), clip.y_max);
1469         }
1470      }
1471      if (y < m_layout->pagesY - 1 || x < m_layout->pagesX - 1) {
1472         MoveTo(clip.x_max - tsize, clip.y_max);
1473         DrawTo(clip.x_max, clip.y_max);
[63d4f07]1474         fAtCorner = true;
[79c239e]1475      } else {
[63d4f07]1476         fAtCorner = false;
[79c239e]1477      }
1478   }
1479
1480   if (x == m_layout->pagesX - 1 && m_layout->Border) {
1481      /* solid right border */
1482      if (!fAtCorner) MoveTo(clip.x_max, clip.y_max);
1483      DrawTo(clip.x_max, clip.y_min);
[63d4f07]1484      fAtCorner = true;
[79c239e]1485   } else {
1486      if (x < m_layout->pagesX - 1 || y < m_layout->pagesY - 1) {
1487         if (!fAtCorner) MoveTo(clip.x_max, clip.y_max);
1488         DrawTo(clip.x_max, clip.y_max - tsize);
1489      }
1490      if (s && x < m_layout->pagesX - 1 && m_layout->Cutlines) {
1491         /* dashed right border */
1492         i = (clip.y_max - clip.y_min) -
1493             (tsize + ((clip.y_max - clip.y_min - tsize * 2L) % s) / 2);
1494         for ( ; i > tsize; i -= s) {
1495            MoveTo(clip.x_max, clip.y_min + (i + o));
1496            DrawTo(clip.x_max, clip.y_min + (i - o));
1497         }
1498      }
1499      if (x < m_layout->pagesX - 1 || y > 0) {
1500         MoveTo(clip.x_max, clip.y_min + tsize);
1501         DrawTo(clip.x_max, clip.y_min);
[63d4f07]1502         fAtCorner = true;
[79c239e]1503      } else {
[63d4f07]1504         fAtCorner = false;
[79c239e]1505      }
1506   }
1507
1508   if (y == 0 && m_layout->Border) {
1509      /* solid bottom border */
1510      if (!fAtCorner) MoveTo(clip.x_max, clip.y_min);
1511      DrawTo(clip.x_min, clip.y_min);
1512   } else {
1513      if (y > 0 || x < m_layout->pagesX - 1) {
1514         if (!fAtCorner) MoveTo(clip.x_max, clip.y_min);
1515         DrawTo(clip.x_max - tsize, clip.y_min);
1516      }
1517      if (s && y > 0 && m_layout->Cutlines) {
1518         /* dashed bottom border */
1519         i = (clip.x_max - clip.x_min) -
1520             (tsize + ((clip.x_max - clip.x_min - tsize * 2L) % s) / 2);
1521         for ( ; i > tsize; i -= s) {
1522            MoveTo(clip.x_min + (i + o), clip.y_min);
1523            DrawTo(clip.x_min + (i - o), clip.y_min);
1524         }
1525      }
1526      if (y > 0 || x > 0) {
1527         MoveTo(clip.x_min + tsize, clip.y_min);
1528         DrawTo(clip.x_min, clip.y_min);
1529      }
1530   }
1531}
1532
[ee05463]1533bool
[79c239e]1534svxPrintout::OnPrintPage(int pageNum) {
1535    GetPageSizePixels(&xpPageWidth, &ypPageDepth);
1536    pdc = GetDC();
[04c9a6d]1537    pdc->SetBackgroundMode(wxTRANSPARENT);
[cca2ce1]1538#ifdef AVEN_PRINT_PREVIEW
[79c239e]1539    if (IsPreview()) {
1540        int dcx, dcy;
1541        pdc->GetSize(&dcx, &dcy);
1542        pdc->SetUserScale((double)dcx / xpPageWidth, (double)dcy / ypPageDepth);
1543    }
[cca2ce1]1544#endif
[79c239e]1545
1546    layout * l = m_layout;
1547    {
1548        int pwidth, pdepth;
1549        GetPageSizeMM(&pwidth, &pdepth);
1550        l->scX = (double)xpPageWidth / pwidth;
1551        l->scY = (double)ypPageDepth / pdepth;
1552        font_scaling_x = l->scX * (25.4 / 72.0);
1553        font_scaling_y = l->scY * (25.4 / 72.0);
[3f43e47]1554        long MarginLeft = m_data->GetMarginTopLeft().x;
1555        long MarginTop = m_data->GetMarginTopLeft().y;
1556        long MarginBottom = m_data->GetMarginBottomRight().y;
1557        long MarginRight = m_data->GetMarginBottomRight().x;
[79c239e]1558        xpPageWidth -= (int)(l->scX * (MarginLeft + MarginRight));
[a31e3fd]1559        ypPageDepth -= (int)(l->scY * (FOOTER_HEIGHT_MM + MarginBottom + MarginTop));
[79c239e]1560        // xpPageWidth -= 1;
[44272ef]1561        pdepth -= FOOTER_HEIGHT_MM;
[79c239e]1562        x_offset = (long)(l->scX * MarginLeft);
1563        y_offset = (long)(l->scY * MarginTop);
1564        l->PaperWidth = pwidth -= MarginLeft + MarginRight;
1565        l->PaperDepth = pdepth -= MarginTop + MarginBottom;
1566    }
1567
[5940815]1568    double SIN = sin(rad(l->rot));
1569    double COS = cos(rad(l->rot));
1570    double SINT = sin(rad(l->tilt));
1571    double COST = cos(rad(l->tilt));
[79c239e]1572
1573    NewPage(pageNum, l->pagesX, l->pagesY);
1574
[256c4c8]1575    if (l->Legend && pageNum == (l->pagesY - 1) * l->pagesX + 1) {
[c3e81cf]1576        SetFont(font_default);
[79c239e]1577        draw_info_box();
1578    }
1579
[f6c92f1]1580    pdc->SetClippingRegion(x_offset, y_offset, xpPageWidth + 1, ypPageDepth + 1);
[55918ca]1581
[741d94f]1582    const double Sc = 1000 / l->Scale;
[79c239e]1583
[1a46879]1584    const SurveyFilter* filter = mainfrm->GetTreeFilter();
[4049a36]1585    int show_mask = l->get_effective_show_mask();
[b96edeb]1586    if (show_mask & (LEGS|SURF)) {
1587        for (int f = 0; f != 8; ++f) {
1588            if ((show_mask & (f & img_FLAG_SURFACE) ? SURF : LEGS) == 0) {
1589                // Not showing traverse because of surface/underground status.
1590                continue;
1591            }
1592            if ((f & img_FLAG_SPLAY) && (show_mask & SPLAYS) == 0) {
1593                // Not showing because it's a splay.
1594                continue;
1595            }
1596            if (f & img_FLAG_SPLAY) {
[ddcf585]1597                pdc->SetPen(*pen_splay);
[b96edeb]1598            } else if (f & img_FLAG_SURFACE) {
1599                pdc->SetPen(*pen_surface_leg);
[ddcf585]1600            } else {
1601                pdc->SetPen(*pen_leg);
1602            }
[1a46879]1603            list<traverse>::const_iterator trav = mainfrm->traverses_begin(f, filter);
[b96edeb]1604            list<traverse>::const_iterator tend = mainfrm->traverses_end(f);
[1a46879]1605            for ( ; trav != tend; trav = mainfrm->traverses_next(f, filter, trav)) {
[b96edeb]1606                vector<PointInfo>::const_iterator pos = trav->begin();
1607                vector<PointInfo>::const_iterator end = trav->end();
1608                for ( ; pos != end; ++pos) {
1609                    double x = pos->GetX();
1610                    double y = pos->GetY();
1611                    double z = pos->GetZ();
1612                    double X = x * COS - y * SIN;
1613                    double Y = z * COST - (x * SIN + y * COS) * SINT;
1614                    long px = (long)((X * Sc + l->xOrg) * l->scX);
1615                    long py = (long)((Y * Sc + l->yOrg) * l->scY);
1616                    if (pos == trav->begin()) {
1617                        MoveTo(px, py);
1618                    } else {
1619                        DrawTo(px, py);
1620                    }
[ce403f1]1621                }
1622            }
[ee05463]1623        }
1624    }
1625
[4049a36]1626    if ((show_mask & XSECT) &&
[d713e5d]1627        (l->tilt == 0.0 || l->tilt == 90.0 || l->tilt == -90.0)) {
[585e9e0]1628        pdc->SetPen(*pen_splay);
[d7078b4]1629        list<vector<XSect>>::const_iterator trav = mainfrm->tubes_begin();
1630        list<vector<XSect>>::const_iterator tend = mainfrm->tubes_end();
[ee05463]1631        for ( ; trav != tend; ++trav) {
[677fac2]1632            if (l->tilt == 0.0) {
1633                PlotUD(*trav);
1634            } else {
1635                // m_layout.tilt is 90.0 or -90.0 due to check above.
1636                PlotLR(*trav);
1637            }
[ce403f1]1638        }
1639    }
1640
[4049a36]1641    if (show_mask & (LABELS|STNS)) {
1642        if (show_mask & LABELS) SetFont(font_labels);
[672459c]1643        for (auto label = mainfrm->GetLabels();
1644             label != mainfrm->GetLabelsEnd();
1645             ++label) {
1646            if (filter && !filter->CheckVisible((*label)->GetText()))
1647                continue;
[79c239e]1648            double px = (*label)->GetX();
1649            double py = (*label)->GetY();
1650            double pz = (*label)->GetZ();
[4049a36]1651            if ((show_mask & SURF) || (*label)->IsUnderground()) {
[79c239e]1652                double X = px * COS - py * SIN;
[7a57dc7]1653                double Y = pz * COST - (px * SIN + py * COS) * SINT;
[79c239e]1654                long xnew, ynew;
1655                xnew = (long)((X * Sc + l->xOrg) * l->scX);
1656                ynew = (long)((Y * Sc + l->yOrg) * l->scY);
[4049a36]1657                if (show_mask & STNS) {
[f0e6d5c]1658                    pdc->SetPen(*pen_cross);
[79c239e]1659                    DrawCross(xnew, ynew);
1660                }
[4049a36]1661                if (show_mask & LABELS) {
[04c9a6d]1662                    pdc->SetTextForeground(colour_labels);
[79c239e]1663                    MoveTo(xnew, ynew);
1664                    WriteString((*label)->GetText());
1665                }
1666            }
1667        }
1668    }
1669
1670    return true;
1671}
1672
[13da582]1673void
1674svxPrintout::GetPageInfo(int *minPage, int *maxPage,
1675                         int *pageFrom, int *pageTo)
1676{
1677    *minPage = *pageFrom = 1;
1678    *maxPage = *pageTo = m_layout->pages;
1679}
1680
1681bool
1682svxPrintout::HasPage(int pageNum) {
1683    return (pageNum <= m_layout->pages);
1684}
1685
[ee05463]1686void
[79c239e]1687svxPrintout::OnBeginPrinting() {
[a5b7959]1688    /* Initialise printer routines */
1689    fontsize_labels = 10;
1690    fontsize = 10;
1691
1692    colour_text = colour_labels = *wxBLACK;
1693
1694    wxColour colour_frame, colour_cross, colour_leg, colour_surface_leg;
1695    colour_frame = colour_cross = colour_leg = colour_surface_leg = *wxBLACK;
1696
1697    pen_frame = new wxPen(colour_frame);
1698    pen_cross = new wxPen(colour_cross);
1699    pen_leg = new wxPen(colour_leg);
1700    pen_surface_leg = new wxPen(colour_surface_leg);
[985c5d9]1701    pen_splay = new wxPen(wxColour(128, 128, 128));
[a5b7959]1702
1703    m_layout->scX = 1;
1704    m_layout->scY = 1;
1705
[9cdd6c7]1706    font_labels = new wxFont(fontsize_labels, wxFONTFAMILY_DEFAULT,
1707                             wxFONTSTYLE_NORMAL, wxFONTWEIGHT_NORMAL,
[a5b7959]1708                             false, wxString(fontname_labels, wxConvUTF8),
1709                             wxFONTENCODING_ISO8859_1);
[9cdd6c7]1710    font_default = new wxFont(fontsize, wxFONTFAMILY_DEFAULT,
1711                              wxFONTSTYLE_NORMAL, wxFONTWEIGHT_NORMAL,
[a5b7959]1712                              false, wxString(fontname, wxConvUTF8),
1713                              wxFONTENCODING_ISO8859_1);
[79c239e]1714}
1715
1716void
1717svxPrintout::OnEndPrinting() {
[03e2031]1718    delete font_labels;
1719    delete font_default;
1720    delete pen_frame;
[f0e6d5c]1721    delete pen_cross;
[03e2031]1722    delete pen_leg;
1723    delete pen_surface_leg;
[585e9e0]1724    delete pen_splay;
[79c239e]1725}
1726
1727int
1728svxPrintout::check_intersection(long x_p, long y_p)
1729{
1730#define U 1
1731#define D 2
1732#define L 4
1733#define R 8
1734   int mask_p = 0, mask_t = 0;
1735   if (x_p < 0)
1736      mask_p = L;
1737   else if (x_p > xpPageWidth)
1738      mask_p = R;
1739
1740   if (y_p < 0)
1741      mask_p |= D;
1742   else if (y_p > ypPageDepth)
1743      mask_p |= U;
1744
1745   if (x_t < 0)
1746      mask_t = L;
1747   else if (x_t > xpPageWidth)
1748      mask_t = R;
1749
1750   if (y_t < 0)
1751      mask_t |= D;
1752   else if (y_t > ypPageDepth)
1753      mask_t |= U;
1754
1755#if 0
1756   /* approximation to correct answer */
1757   return !(mask_t & mask_p);
1758#else
1759   /* One end of the line is on the page */
1760   if (!mask_t || !mask_p) return 1;
1761
1762   /* whole line is above, left, right, or below page */
1763   if (mask_t & mask_p) return 0;
1764
1765   if (mask_t == 0) mask_t = mask_p;
1766   if (mask_t & U) {
1767      double v = (double)(y_p - ypPageDepth) / (y_p - y_t);
1768      return v >= 0 && v <= 1;
1769   }
1770   if (mask_t & D) {
1771      double v = (double)y_p / (y_p - y_t);
1772      return v >= 0 && v <= 1;
1773   }
1774   if (mask_t & R) {
1775      double v = (double)(x_p - xpPageWidth) / (x_p - x_t);
1776      return v >= 0 && v <= 1;
1777   }
[bcbd681]1778   wxASSERT(mask_t & L);
[79c239e]1779   {
1780      double v = (double)x_p / (x_p - x_t);
1781      return v >= 0 && v <= 1;
1782   }
1783#endif
1784#undef U
1785#undef D
1786#undef L
1787#undef R
1788}
1789
1790void
1791svxPrintout::MoveTo(long x, long y)
1792{
1793    x_t = x_offset + x - clip.x_min;
1794    y_t = y_offset + clip.y_max - y;
1795}
1796
1797void
1798svxPrintout::DrawTo(long x, long y)
1799{
1800    long x_p = x_t, y_p = y_t;
1801    x_t = x_offset + x - clip.x_min;
1802    y_t = y_offset + clip.y_max - y;
[7087afb]1803    if (!scan_for_blank_pages) {
[5940815]1804        pdc->DrawLine(x_p, y_p, x_t, y_t);
[79c239e]1805    } else {
[63d4f07]1806        if (check_intersection(x_p, y_p)) fBlankPage = false;
[79c239e]1807    }
1808}
1809
1810#define POINTS_PER_INCH 72.0
1811#define POINTS_PER_MM (POINTS_PER_INCH / MM_PER_INCH)
1812#define PWX_CROSS_SIZE (int)(2 * m_layout->scX / POINTS_PER_MM)
1813
1814void
1815svxPrintout::DrawCross(long x, long y)
1816{
[7087afb]1817   if (!scan_for_blank_pages) {
[79c239e]1818      MoveTo(x - PWX_CROSS_SIZE, y - PWX_CROSS_SIZE);
1819      DrawTo(x + PWX_CROSS_SIZE, y + PWX_CROSS_SIZE);
1820      MoveTo(x + PWX_CROSS_SIZE, y - PWX_CROSS_SIZE);
1821      DrawTo(x - PWX_CROSS_SIZE, y + PWX_CROSS_SIZE);
1822      MoveTo(x, y);
1823   } else {
1824      if ((x + PWX_CROSS_SIZE > clip.x_min &&
1825           x - PWX_CROSS_SIZE < clip.x_max) ||
1826          (y + PWX_CROSS_SIZE > clip.y_min &&
1827           y - PWX_CROSS_SIZE < clip.y_max)) {
[63d4f07]1828         fBlankPage = false;
[79c239e]1829      }
1830   }
1831}
1832
1833void
[5627cbb]1834svxPrintout::WriteString(const wxString & s)
[79c239e]1835{
1836    double xsc, ysc;
1837    pdc->GetUserScale(&xsc, &ysc);
1838    pdc->SetUserScale(xsc * font_scaling_x, ysc * font_scaling_y);
[7087afb]1839    if (!scan_for_blank_pages) {
[ee05463]1840        pdc->DrawText(s,
[79c239e]1841                      long(x_t / font_scaling_x),
[79c78f7]1842                      long(y_t / font_scaling_y) - pdc->GetCharHeight());
[79c239e]1843    } else {
[79c78f7]1844        int w, h;
[79c239e]1845        pdc->GetTextExtent(s, &w, &h);
1846        if ((y_t + h > 0 && y_t - h < clip.y_max - clip.y_min) ||
1847            (x_t < clip.x_max - clip.x_min && x_t + w > 0)) {
[63d4f07]1848            fBlankPage = false;
[79c239e]1849        }
1850    }
1851    pdc->SetUserScale(xsc, ysc);
1852}
1853
1854void
1855svxPrintout::DrawEllipse(long x, long y, long r, long R)
1856{
[7087afb]1857    if (!scan_for_blank_pages) {
[79c239e]1858        x_t = x_offset + x - clip.x_min;
1859        y_t = y_offset + clip.y_max - y;
[2bf75f3]1860        const wxBrush & save_brush = pdc->GetBrush();
[79c239e]1861        pdc->SetBrush(*wxTRANSPARENT_BRUSH);
1862        pdc->DrawEllipse(x_t - r, y_t - R, 2 * r, 2 * R);
[2bf75f3]1863        pdc->SetBrush(save_brush);
[7087afb]1864    } else {
1865        /* No need to check - this is only used in the legend. */
[79c239e]1866    }
1867}
1868
1869void
1870svxPrintout::SolidRectangle(long x, long y, long w, long h)
1871{
1872    long X = x_offset + x - clip.x_min;
1873    long Y = y_offset + clip.y_max - y;
1874    pdc->SetBrush(*wxBLACK_BRUSH);
1875    pdc->DrawRectangle(X, Y - h, w, h);
1876}
1877
1878void
1879svxPrintout::NewPage(int pg, int pagesX, int pagesY)
1880{
[6b2384e]1881    pdc->DestroyClippingRegion();
1882
[79c239e]1883    int x, y;
1884    x = (pg - 1) % pagesX;
1885    y = pagesY - 1 - ((pg - 1) / pagesX);
1886
1887    clip.x_min = (long)x * xpPageWidth;
1888    clip.y_min = (long)y * ypPageDepth;
1889    clip.x_max = clip.x_min + xpPageWidth; /* dm/pcl/ps had -1; */
1890    clip.y_max = clip.y_min + ypPageDepth; /* dm/pcl/ps had -1; */
1891
[203b480]1892    const int FOOTERS = 4;
[ef1c501]1893    wxString footer[FOOTERS];
1894    footer[0] = m_layout->title;
[3a567c7]1895
[203b480]1896    double rot = m_layout->rot;
1897    double tilt = m_layout->tilt;
[47ad66f]1898    double scale = m_layout->Scale;
[1d5c08a]1899    switch (m_layout->view) {
1900        case layout::PLAN:
1901            // TRANSLATORS: Used in the footer of printouts to compactly
1902            // indicate this is a plan view and what the viewing angle is.
1903            // Aven will replace %s with the bearing, and %.0f with the scale.
1904            //
1905            // This message probably doesn't need translating for most languages.
1906            footer[1].Printf(wmsg(/*↑%s 1:%.0f*/233),
1907                    format_angle(ANGLE_FMT, rot).c_str(),
1908                    scale);
1909            break;
1910        case layout::ELEV:
1911            // TRANSLATORS: Used in the footer of printouts to compactly
1912            // indicate this is an elevation view and what the viewing angle
1913            // is.  Aven will replace the %s codes with the bearings to the
1914            // left and right of the viewer, and %.0f with the scale.
1915            //
1916            // This message probably doesn't need translating for most languages.
1917            footer[1].Printf(wmsg(/*%s↔%s 1:%.0f*/235),
1918                    format_angle(ANGLE_FMT, fmod(rot + 270.0, 360.0)).c_str(),
1919                    format_angle(ANGLE_FMT, fmod(rot + 90.0, 360.0)).c_str(),
1920                    scale);
1921            break;
1922        case layout::TILT:
1923            // TRANSLATORS: Used in the footer of printouts to compactly
1924            // indicate this is a tilted elevation view and what the viewing
1925            // angles are.  Aven will replace the %s codes with the bearings to
1926            // the left and right of the viewer and the angle the view is
1927            // tilted at, and %.0f with the scale.
1928            //
1929            // This message probably doesn't need translating for most languages.
1930            footer[1].Printf(wmsg(/*%s↔%s ∡%s 1:%.0f*/236),
1931                    format_angle(ANGLE_FMT, fmod(rot + 270.0, 360.0)).c_str(),
1932                    format_angle(ANGLE_FMT, fmod(rot + 90.0, 360.0)).c_str(),
1933                    format_angle(ANGLE2_FMT, tilt).c_str(),
1934                    scale);
1935            break;
1936        case layout::EXTELEV:
1937            // TRANSLATORS: Used in the footer of printouts to compactly
1938            // indicate this is an extended elevation view.  Aven will replace
1939            // %.0f with the scale.
1940            //
1941            // Try to keep the translation short (for example, in English we
1942            // use "Extended" not "Extended elevation") - there is limited room
1943            // in the footer, and the details there are mostly to make it easy
1944            // to check that you have corresponding pages from a multiple page
1945            // printout.
1946            footer[1].Printf(wmsg(/*Extended 1:%.0f*/244), scale);
1947            break;
[203b480]1948    }
[3a567c7]1949
[ef1c501]1950    // TRANSLATORS: N/M meaning page N of M in the page footer of a printout.
[203b480]1951    footer[2].Printf(wmsg(/*%d/%d*/232), pg, m_layout->pagesX * m_layout->pagesY);
[3a567c7]1952
1953    wxString datestamp = m_layout->datestamp;
1954    if (!datestamp.empty()) {
1955        // Remove any timezone suffix (e.g. " UTC" or " +1200").
1956        wxChar ch = datestamp[datestamp.size() - 1];
1957        if (ch >= 'A' && ch <= 'Z') {
1958            for (size_t i = datestamp.size() - 1; i; --i) {
1959                ch = datestamp[i];
1960                if (ch < 'A' || ch > 'Z') {
1961                    if (ch == ' ') datestamp.resize(i);
1962                    break;
1963                }
1964            }
1965        } else if (ch >= '0' && ch <= '9') {
1966            for (size_t i = datestamp.size() - 1; i; --i) {
1967                ch = datestamp[i];
1968                if (ch < '0' || ch > '9') {
1969                    if ((ch == '-' || ch == '+') && datestamp[--i] == ' ')
1970                        datestamp.resize(i);
1971                    break;
1972                }
1973            }
1974        }
1975
1976        // Remove any day prefix (e.g. "Mon,").
1977        for (size_t i = 0; i != datestamp.size(); ++i) {
1978            if (datestamp[i] == ',' && i + 1 != datestamp.size()) {
1979                // Also skip a space after the comma.
1980                if (datestamp[i + 1] == ' ') ++i;
1981                datestamp.erase(0, i + 1);
1982                break;
1983            }
1984        }
1985    }
1986
1987    // TRANSLATORS: Used in the footer of printouts to compactly indicate that
1988    // the date which follows is the date that the survey data was processed.
1989    //
1990    // Aven will replace %s with a string giving the date and time (e.g.
1991    // "2015-06-09 12:40:44").
1992    footer[3].Printf(wmsg(/*Processed: %s*/167), datestamp.c_str());
1993
[ef1c501]1994    const wxChar * footer_sep = wxT("    ");
1995    int fontsize_footer = fontsize_labels;
1996    wxFont * font_footer;
[9cdd6c7]1997    font_footer = new wxFont(fontsize_footer, wxFONTFAMILY_DEFAULT,
1998                             wxFONTSTYLE_NORMAL, wxFONTWEIGHT_NORMAL,
[ef1c501]1999                             false, wxString(fontname_labels, wxConvUTF8),
2000                             wxFONTENCODING_UTF8);
2001    font_footer->Scale(font_scaling_x);
2002    SetFont(font_footer);
2003    int w[FOOTERS], ws, h;
2004    pdc->GetTextExtent(footer_sep, &ws, &h);
2005    int wtotal = ws * (FOOTERS - 1);
2006    for (int i = 0; i < FOOTERS; ++i) {
2007        pdc->GetTextExtent(footer[i], &w[i], &h);
2008        wtotal += w[i];
2009    }
2010
2011    long X = x_offset;
2012    long Y = y_offset + ypPageDepth + (long)(7 * m_layout->scY) - pdc->GetCharHeight();
2013
2014    if (wtotal > xpPageWidth) {
[b94af2c]2015        // Rescale the footer so it fits.
2016        double rescale = double(wtotal) / xpPageWidth;
2017        double xsc, ysc;
2018        pdc->GetUserScale(&xsc, &ysc);
2019        pdc->SetUserScale(xsc / rescale, ysc / rescale);
[ef1c501]2020        SetFont(font_footer);
2021        wxString fullfooter = footer[0];
[b94af2c]2022        for (int i = 1; i < FOOTERS - 1; ++i) {
[ef1c501]2023            fullfooter += footer_sep;
2024            fullfooter += footer[i];
2025        }
[b94af2c]2026        pdc->DrawText(fullfooter, X * rescale, Y * rescale);
2027        // Draw final item right aligned to avoid misaligning.
2028        wxRect rect(x_offset * rescale, Y * rescale,
2029                    xpPageWidth * rescale, pdc->GetCharHeight() * rescale);
2030        pdc->DrawLabel(footer[FOOTERS - 1], rect, wxALIGN_RIGHT|wxALIGN_TOP);
2031        pdc->SetUserScale(xsc, ysc);
[ef1c501]2032    } else {
2033        // Space out the elements of the footer to fill the line.
[203b480]2034        double extra = double(xpPageWidth - wtotal) / (FOOTERS - 1);
2035        for (int i = 0; i < FOOTERS - 1; ++i) {
[ef1c501]2036            pdc->DrawText(footer[i], X + extra * i, Y);
[203b480]2037            X += ws + w[i];
[ef1c501]2038        }
2039        // Draw final item right aligned to avoid misaligning.
2040        wxRect rect(x_offset, Y, xpPageWidth, pdc->GetCharHeight());
[203b480]2041        pdc->DrawLabel(footer[FOOTERS - 1], rect, wxALIGN_RIGHT|wxALIGN_TOP);
[ef1c501]2042    }
[dc7898c]2043    drawticks((int)(9 * m_layout->scX / POINTS_PER_MM), x, y);
[79c239e]2044}
2045
[741d94f]2046void
[ee05463]2047svxPrintout::PlotLR(const vector<XSect> & centreline)
[741d94f]2048{
[1a46879]2049    const SurveyFilter* filter = mainfrm->GetTreeFilter();
[741d94f]2050    assert(centreline.size() > 1);
[672459c]2051    const XSect* prev_pt_v = NULL;
[741d94f]2052    Vector3 last_right(1.0, 0.0, 0.0);
2053
2054    const double Sc = 1000 / m_layout->Scale;
[0fdd3aa]2055    const double SIN = sin(rad(m_layout->rot));
2056    const double COS = cos(rad(m_layout->rot));
[741d94f]2057
[ee05463]2058    vector<XSect>::const_iterator i = centreline.begin();
2059    vector<XSect>::size_type segment = 0;
[741d94f]2060    while (i != centreline.end()) {
2061        // get the coordinates of this vertex
[ee05463]2062        const XSect & pt_v = *i++;
[741d94f]2063
2064        Vector3 right;
2065
2066        const Vector3 up_v(0.0, 0.0, 1.0);
2067
2068        if (segment == 0) {
2069            assert(i != centreline.end());
2070            // first segment
2071
2072            // get the coordinates of the next vertex
[ee05463]2073            const XSect & next_pt_v = *i;
[741d94f]2074
2075            // calculate vector from this pt to the next one
[d67450e]2076            Vector3 leg_v = next_pt_v - pt_v;
[741d94f]2077
2078            // obtain a vector in the LRUD plane
2079            right = leg_v * up_v;
2080            if (right.magnitude() == 0) {
2081                right = last_right;
2082            } else {
2083                last_right = right;
2084            }
2085        } else if (segment + 1 == centreline.size()) {
2086            // last segment
2087
2088            // Calculate vector from the previous pt to this one.
[672459c]2089            Vector3 leg_v = pt_v - *prev_pt_v;
[741d94f]2090
2091            // Obtain a horizontal vector in the LRUD plane.
2092            right = leg_v * up_v;
2093            if (right.magnitude() == 0) {
[d67450e]2094                right = Vector3(last_right.GetX(), last_right.GetY(), 0.0);
[741d94f]2095            } else {
2096                last_right = right;
2097            }
2098        } else {
2099            assert(i != centreline.end());
2100            // Intermediate segment.
2101
2102            // Get the coordinates of the next vertex.
[ee05463]2103            const XSect & next_pt_v = *i;
[741d94f]2104
2105            // Calculate vectors from this vertex to the
2106            // next vertex, and from the previous vertex to
2107            // this one.
[672459c]2108            Vector3 leg1_v = pt_v - *prev_pt_v;
[d67450e]2109            Vector3 leg2_v = next_pt_v - pt_v;
[741d94f]2110
2111            // Obtain horizontal vectors perpendicular to
2112            // both legs, then normalise and average to get
2113            // a horizontal bisector.
2114            Vector3 r1 = leg1_v * up_v;
2115            Vector3 r2 = leg2_v * up_v;
2116            r1.normalise();
2117            r2.normalise();
2118            right = r1 + r2;
2119            if (right.magnitude() == 0) {
2120                // This is the "mid-pitch" case...
2121                right = last_right;
2122            }
2123            last_right = right;
2124        }
2125
2126        // Scale to unit vectors in the LRUD plane.
2127        right.normalise();
2128
[147847c]2129        double l = pt_v.GetL();
2130        double r = pt_v.GetR();
[741d94f]2131
[e27750b]2132        if (l >= 0 || r >= 0) {
[672459c]2133            if (!filter || filter->CheckVisible(pt_v.GetLabel())) {
2134                // Get the x and y coordinates of the survey station
2135                double pt_X = pt_v.GetX() * COS - pt_v.GetY() * SIN;
2136                double pt_Y = pt_v.GetX() * SIN + pt_v.GetY() * COS;
2137                long pt_x = (long)((pt_X * Sc + m_layout->xOrg) * m_layout->scX);
2138                long pt_y = (long)((pt_Y * Sc + m_layout->yOrg) * m_layout->scY);
2139
2140                // Calculate dimensions for the right arrow
2141                double COSR = right.GetX();
2142                double SINR = right.GetY();
2143                long CROSS_MAJOR = (COSR + SINR) * PWX_CROSS_SIZE;
2144                long CROSS_MINOR = (COSR - SINR) * PWX_CROSS_SIZE;
2145
2146                if (l >= 0) {
2147                    // Get the x and y coordinates of the end of the left arrow
2148                    Vector3 p = pt_v.GetPoint() - right * l;
2149                    double X = p.GetX() * COS - p.GetY() * SIN;
2150                    double Y = (p.GetX() * SIN + p.GetY() * COS);
2151                    long x = (long)((X * Sc + m_layout->xOrg) * m_layout->scX);
2152                    long y = (long)((Y * Sc + m_layout->yOrg) * m_layout->scY);
2153
2154                    // Draw the arrow stem
2155                    MoveTo(pt_x, pt_y);
2156                    DrawTo(x, y);
2157
2158                    // Rotate the arrow by the page rotation
2159                    long dx1 = (+CROSS_MINOR) * COS - (+CROSS_MAJOR) * SIN;
2160                    long dy1 = (+CROSS_MINOR) * SIN + (+CROSS_MAJOR) * COS;
2161                    long dx2 = (+CROSS_MAJOR) * COS - (-CROSS_MINOR) * SIN;
2162                    long dy2 = (+CROSS_MAJOR) * SIN + (-CROSS_MINOR) * COS;
2163
2164                    // Draw the arrow
2165                    MoveTo(x + dx1, y + dy1);
2166                    DrawTo(x, y);
2167                    DrawTo(x + dx2, y + dy2);
2168                }
[e27750b]2169
[672459c]2170                if (r >= 0) {
2171                    // Get the x and y coordinates of the end of the right arrow
2172                    Vector3 p = pt_v.GetPoint() + right * r;
2173                    double X = p.GetX() * COS - p.GetY() * SIN;
2174                    double Y = (p.GetX() * SIN + p.GetY() * COS);
2175                    long x = (long)((X * Sc + m_layout->xOrg) * m_layout->scX);
2176                    long y = (long)((Y * Sc + m_layout->yOrg) * m_layout->scY);
2177
2178                    // Draw the arrow stem
2179                    MoveTo(pt_x, pt_y);
2180                    DrawTo(x, y);
2181
2182                    // Rotate the arrow by the page rotation
2183                    long dx1 = (-CROSS_MINOR) * COS - (-CROSS_MAJOR) * SIN;
2184                    long dy1 = (-CROSS_MINOR) * SIN + (-CROSS_MAJOR) * COS;
2185                    long dx2 = (-CROSS_MAJOR) * COS - (+CROSS_MINOR) * SIN;
2186                    long dy2 = (-CROSS_MAJOR) * SIN + (+CROSS_MINOR) * COS;
2187
2188                    // Draw the arrow
2189                    MoveTo(x + dx1, y + dy1);
2190                    DrawTo(x, y);
2191                    DrawTo(x + dx2, y + dy2);
2192                }
[e27750b]2193            }
[741d94f]2194        }
2195
[672459c]2196        prev_pt_v = &pt_v;
[741d94f]2197
2198        ++segment;
2199    }
2200}
2201
[0fdd3aa]2202void
[ee05463]2203svxPrintout::PlotUD(const vector<XSect> & centreline)
[0fdd3aa]2204{
[1a46879]2205    const SurveyFilter* filter = mainfrm->GetTreeFilter();
[0fdd3aa]2206    assert(centreline.size() > 1);
2207    const double Sc = 1000 / m_layout->Scale;
2208
[ee05463]2209    vector<XSect>::const_iterator i = centreline.begin();
[0fdd3aa]2210    while (i != centreline.end()) {
2211        // get the coordinates of this vertex
[ee05463]2212        const XSect & pt_v = *i++;
[0fdd3aa]2213
[147847c]2214        double u = pt_v.GetU();
2215        double d = pt_v.GetD();
[0fdd3aa]2216
2217        if (u >= 0 || d >= 0) {
[672459c]2218            if (filter && !filter->CheckVisible(pt_v.GetLabel()))
2219                continue;
2220
[e27750b]2221            // Get the coordinates of the survey point
[672459c]2222            Vector3 p = pt_v.GetPoint();
[0fdd3aa]2223            double SIN = sin(rad(m_layout->rot));
2224            double COS = cos(rad(m_layout->rot));
[d67450e]2225            double X = p.GetX() * COS - p.GetY() * SIN;
2226            double Y = p.GetZ();
[0fdd3aa]2227            long x = (long)((X * Sc + m_layout->xOrg) * m_layout->scX);
[e27750b]2228            long pt_y = (long)((Y * Sc + m_layout->yOrg) * m_layout->scX);
2229
[0fdd3aa]2230            if (u >= 0) {
[e27750b]2231                // Get the y coordinate of the up arrow
[0fdd3aa]2232                long y = (long)(((Y + u) * Sc + m_layout->yOrg) * m_layout->scY);
[e27750b]2233
[15c6a4e]2234                // Draw the arrow stem
2235                MoveTo(x, pt_y);
[0fdd3aa]2236                DrawTo(x, y);
[e27750b]2237
2238                // Draw the up arrow
2239                MoveTo(x - PWX_CROSS_SIZE, y - PWX_CROSS_SIZE);
2240                DrawTo(x, y);
2241                DrawTo(x + PWX_CROSS_SIZE, y - PWX_CROSS_SIZE);
[0fdd3aa]2242            }
[e27750b]2243
[0fdd3aa]2244            if (d >= 0) {
[e27750b]2245                // Get the y coordinate of the down arrow
[0fdd3aa]2246                long y = (long)(((Y - d) * Sc + m_layout->yOrg) * m_layout->scY);
[e27750b]2247
[15c6a4e]2248                // Draw the arrow stem
2249                MoveTo(x, pt_y);
[0fdd3aa]2250                DrawTo(x, y);
[e27750b]2251
2252                // Draw the down arrow
2253                MoveTo(x - PWX_CROSS_SIZE, y + PWX_CROSS_SIZE);
2254                DrawTo(x, y);
2255                DrawTo(x + PWX_CROSS_SIZE, y + PWX_CROSS_SIZE);
[0fdd3aa]2256            }
2257        }
2258    }
2259}
Note: See TracBrowser for help on using the repository browser.