source: git/src/printing.cc @ 6ed3e17

debug-cidebug-ci-sanitisersfaster-cavernloglog-selectstereo-2025walls-datawalls-data-hanging-as-warningwarn-only-for-hanging-survey
Last change on this file since 6ed3e17 was 6ed3e17, checked in by Olly Betts <olly@…>, 2 years ago

Right align tilt spinctrl value is print/export dialog

Looks better as we already right align the bearing.

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