lmi-commits
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[lmi-commits] [lmi] master 5421d85 002/156: Factor out pdf_writer_wx fro


From: Greg Chicares
Subject: [lmi-commits] [lmi] master 5421d85 002/156: Factor out pdf_writer_wx from group_quote_pdf_gen_wx
Date: Tue, 30 Jan 2018 17:21:49 -0500 (EST)

branch: master
commit 5421d854d96eb671be0c3817c71895bde67011d2
Author: Vadim Zeitlin <address@hidden>
Commit: Vadim Zeitlin <address@hidden>

    Factor out pdf_writer_wx from group_quote_pdf_gen_wx
    
    Allow reusing helpers for PDF generation from elsewhere.
    
    No real changes, this is a pure refactoring.
---
 Makefile.am                |  1 +
 group_quote_pdf_gen_wx.cpp | 47 +++-----------------------
 objects.make               |  1 +
 pdf_writer_wx.cpp          | 83 ++++++++++++++++++++++++++++++++++++++++++++++
 pdf_writer_wx.hpp          | 51 ++++++++++++++++++++++++++++
 5 files changed, 140 insertions(+), 43 deletions(-)

diff --git a/Makefile.am b/Makefile.am
index b47d240..a7dca3f 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -180,6 +180,7 @@ libskeleton_la_SOURCES = \
     multidimgrid_tools.cpp \
     mvc_controller.cpp \
     mvc_view.cpp \
+    pdf_writer_wx.cpp \
     policy_document.cpp \
     policy_view.cpp \
     preferences_view.cpp \
diff --git a/group_quote_pdf_gen_wx.cpp b/group_quote_pdf_gen_wx.cpp
index 4043a99..af1fe9f 100644
--- a/group_quote_pdf_gen_wx.cpp
+++ b/group_quote_pdf_gen_wx.cpp
@@ -36,6 +36,7 @@
 #include "miscellany.hpp"               // split_into_lines()
 #include "oecumenic_enumerations.hpp"   // oenum_format_style
 #include "path_utility.hpp"             // fs::path inserter
+#include "pdf_writer_wx.hpp"
 #include "version.hpp"
 #include "wx_table_generator.hpp"
 #include "wx_utility.hpp"               // ConvertDateToWx()
@@ -45,9 +46,7 @@
 
 #include <wx/datetime.h>
 #include <wx/html/htmlcell.h>
-#include <wx/html/winpars.h>
 #include <wx/image.h>
-#include <wx/pdfdc.h>
 
 #include <cstring>                      // strstr()
 #include <limits>
@@ -459,9 +458,6 @@ class group_quote_pdf_generator_wx
     // Ctor is private as it is only used by do_create().
     group_quote_pdf_generator_wx() = default;
 
-    // Generate the PDF once we have all the data.
-    void do_generate_pdf(wxPdfDC& pdf_dc);
-
     // Compute the number of pages needed by the table rows in the output given
     // the space remaining on the first page, the heights of the header, one
     // table row and the footer and the last row position.
@@ -858,46 +854,11 @@ void group_quote_pdf_generator_wx::add_ledger(Ledger 
const& ledger)
 
 void group_quote_pdf_generator_wx::save(std::string const& output_filename)
 {
-    // Create a wxPrintData object just to describe the paper to use.
-    wxPrintData print_data;
-    print_data.SetOrientation(wxLANDSCAPE);
-    print_data.SetPaperId(wxPAPER_LETTER);
-    print_data.SetFilename(output_filename);
+    pdf_writer_wx pdf_writer(output_filename, wxLANDSCAPE);
+    wxPdfDC& pdf_dc = pdf_writer.dc();
+    wxHtmlWinParser& html_parser = pdf_writer.html_parser();
 
-    wxPdfDC pdf_dc(print_data);
     page_.initialize(pdf_dc);
-    do_generate_pdf(pdf_dc);
-    pdf_dc.EndDoc();
-}
-
-void group_quote_pdf_generator_wx::do_generate_pdf(wxPdfDC& pdf_dc)
-{
-    // Ensure that the output is independent of the current display resolution:
-    // it seems that this is only the case with the PDF map mode and wxDC mode
-    // different from wxMM_TEXT.
-    pdf_dc.SetMapModeStyle(wxPDF_MAPMODESTYLE_PDF);
-
-    // For simplicity, use points for everything: font sizers are expressed in
-    // them anyhow, so it's convenient to use them for everything else too.
-    pdf_dc.SetMapMode(wxMM_POINTS);
-
-    pdf_dc.StartDoc(wxString()); // Argument is not used.
-    pdf_dc.StartPage();
-
-    // Use a standard PDF Helvetica font (without embedding any custom fonts in
-    // the generated file, the only other realistic choice is Times New Roman).
-    pdf_dc.SetFont
-        (wxFontInfo(8).Family(wxFONTFAMILY_SWISS).FaceName("Helvetica")
-        );
-
-    // Create an HTML parser to allow easily adding HTML contents to the 
output.
-    wxHtmlWinParser html_parser(nullptr);
-    html_parser.SetDC(&pdf_dc);
-    html_parser.SetStandardFonts
-        (pdf_dc.GetFont().GetPointSize()
-        ,"Helvetica"
-        ,"Courier"
-        );
 
     int pos_y = 0;
 
diff --git a/objects.make b/objects.make
index 1e2dff5..70c2649 100644
--- a/objects.make
+++ b/objects.make
@@ -330,6 +330,7 @@ skeleton_objects := \
   multidimgrid_tools.o \
   mvc_controller.o \
   mvc_view.o \
+  pdf_writer_wx.o \
   policy_document.o \
   policy_view.o \
   preferences_view.o \
diff --git a/pdf_writer_wx.cpp b/pdf_writer_wx.cpp
new file mode 100644
index 0000000..f9f822b
--- /dev/null
+++ b/pdf_writer_wx.cpp
@@ -0,0 +1,83 @@
+// PDF generation helpers.
+//
+// Copyright (C) 2017 Gregory W. Chicares.
+//
+// This program is free software; you can redistribute it and/or modify
+// it under the terms of the GNU General Public License version 2 as
+// published by the Free Software Foundation.
+//
+// This program is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+// GNU General Public License for more details.
+//
+// You should have received a copy of the GNU General Public License
+// along with this program; if not, write to the Free Software Foundation,
+// Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
+//
+// http://savannah.nongnu.org/projects/lmi
+// email: <address@hidden>
+// snail: Chicares, 186 Belle Woods Drive, Glastonbury CT 06033, USA
+
+#include "pchfile_wx.hpp"
+
+#include "pdf_writer_wx.hpp"
+
+namespace
+{
+
+wxPrintData make_print_data
+    (wxString const&    output_filename
+    ,wxPrintOrientation orientation
+    )
+{
+    wxPrintData print_data;
+    print_data.SetPaperId(wxPAPER_LETTER);
+    print_data.SetFilename(output_filename);
+    print_data.SetOrientation(orientation);
+    return print_data;
+}
+
+} // Unnamed namespace.
+
+pdf_writer_wx::pdf_writer_wx
+    (wxString const&    output_filename
+    ,wxPrintOrientation orientation
+    )
+    :print_data_    {make_print_data(output_filename, orientation)}
+    ,pdf_dc_        {print_data_}
+    ,html_parser_   {nullptr}
+{
+    // Ensure that the output is independent of the current display resolution:
+    // it seems that this is only the case with the PDF map mode and wxDC mode
+    // different from wxMM_TEXT.
+    pdf_dc_.SetMapModeStyle(wxPDF_MAPMODESTYLE_PDF);
+
+    // For simplicity, use points for everything: font sizers are expressed in
+    // them anyhow, so it's convenient to use them for everything else too.
+    pdf_dc_.SetMapMode(wxMM_POINTS);
+
+    pdf_dc_.StartDoc(wxString()); // Argument is not used.
+    pdf_dc_.StartPage();
+
+    // Use a standard PDF Helvetica font (without embedding any custom fonts in
+    // the generated file, the only other realistic choice is Times New Roman).
+    pdf_dc_.SetFont
+        (wxFontInfo(8).Family(wxFONTFAMILY_SWISS).FaceName("Helvetica")
+        );
+
+    // Create an HTML parser to allow easily adding HTML contents to the 
output.
+    html_parser_.SetDC(&pdf_dc_);
+    html_parser_.SetStandardFonts
+        (pdf_dc_.GetFont().GetPointSize()
+        ,"Helvetica"
+        ,"Courier"
+        );
+
+}
+
+pdf_writer_wx::~pdf_writer_wx()
+{
+    // This will finally generate the PDF file.
+    pdf_dc_.EndDoc();
+}
diff --git a/pdf_writer_wx.hpp b/pdf_writer_wx.hpp
new file mode 100644
index 0000000..0e8ae7b
--- /dev/null
+++ b/pdf_writer_wx.hpp
@@ -0,0 +1,51 @@
+// PDF generation helpers.
+//
+// Copyright (C) 2017 Gregory W. Chicares.
+//
+// This program is free software; you can redistribute it and/or modify
+// it under the terms of the GNU General Public License version 2 as
+// published by the Free Software Foundation.
+//
+// This program is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+// GNU General Public License for more details.
+//
+// You should have received a copy of the GNU General Public License
+// along with this program; if not, write to the Free Software Foundation,
+// Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
+//
+// http://savannah.nongnu.org/projects/lmi
+// email: <address@hidden>
+// snail: Chicares, 186 Belle Woods Drive, Glastonbury CT 06033, USA
+
+#ifndef pdf_writer_wx_hpp
+#define pdf_writer_wx_hpp
+
+#include "config.hpp"
+
+#include <wx/html/winpars.h>
+
+#include <wx/pdfdc.h>
+
+class pdf_writer_wx
+{
+  public:
+    pdf_writer_wx
+        (wxString const&    output_filename
+        ,wxPrintOrientation orientation
+        );
+
+    ~pdf_writer_wx();
+
+    wxPdfDC& dc() { return pdf_dc_; }
+
+    wxHtmlWinParser& html_parser() { return html_parser_; }
+
+  private:
+    wxPrintData print_data_;
+    wxPdfDC pdf_dc_;
+    wxHtmlWinParser html_parser_;
+};
+
+#endif // pdf_writer_wx_hpp



reply via email to

[Prev in Thread] Current Thread [Next in Thread]