source: git/src/printing.cc @ 1afe833

stereo-2025
Last change on this file since 1afe833 was 85f7905, checked in by Olly Betts <olly@…>, 7 months ago

Add exporting to shape files

Requires GDAL.

Fixes #87

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