lmi-commits
[Top][All Lists]
Advanced

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

[lmi-commits] [lmi] master e43a485 005/156: Further improve wxPdfDocumen


From: Greg Chicares
Subject: [lmi-commits] [lmi] master e43a485 005/156: Further improve wxPdfDocument API encapsulation and reuse
Date: Tue, 30 Jan 2018 17:21:50 -0500 (EST)

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

    Further improve wxPdfDocument API encapsulation and reuse
    
    Make output_image() and output_html() functions members of pdf_writer_wx
    to avoid the need for exposing wxPdfDocument outside of pdf_writer_wx
    and to allow reusing them from elsewhere.
---
 group_quote_pdf_gen_wx.cpp | 125 +++++----------------------------------------
 pdf_writer_wx.cpp          |  96 ++++++++++++++++++++++++++++++++++
 pdf_writer_wx.hpp          |  30 ++++++++++-
 3 files changed, 137 insertions(+), 114 deletions(-)

diff --git a/group_quote_pdf_gen_wx.cpp b/group_quote_pdf_gen_wx.cpp
index c5c7167..fb94998 100644
--- a/group_quote_pdf_gen_wx.cpp
+++ b/group_quote_pdf_gen_wx.cpp
@@ -45,7 +45,6 @@
 #include <boost/filesystem/path.hpp>
 
 #include <wx/datetime.h>
-#include <wx/html/htmlcell.h>
 #include <wx/image.h>
 
 #include <cstring>                      // strstr()
@@ -60,11 +59,6 @@ LMI_FORCE_LINKING_IN_SITU(group_quote_pdf_generator_wx)
 namespace
 {
 
-enum enum_output_mode
-    {e_output_normal
-    ,e_output_measure_only
-    };
-
 /// Escape special XML characters in the given string, ensuring that it appears
 /// correctly inside HTML element contents. Notice that we don't need to escape
 /// quotes here as we never use the result of this function inside an HTML
@@ -300,101 +294,6 @@ wxImage load_image(char const* file)
     return image;
 }
 
-/// Output an image at the given scale into the PDF.
-///
-/// The scale specifies how many times the image should be shrunk:
-/// scale > 1 makes the image smaller, while scale < 1 makes it larger.
-///
-/// Updates pos_y by increasing it by the height of the specified
-/// image at the given scale.
-
-void output_image
-    (pdf_writer_wx&   pdf_writer
-    ,wxImage const&   image
-    ,char const*      image_name
-    ,double           scale
-    ,int              x
-    ,int*             pos_y
-    ,enum_output_mode output_mode = e_output_normal
-    )
-{
-    int const y = wxRound(image.GetHeight() / scale);
-
-    switch(output_mode)
-        {
-        case e_output_normal:
-            {
-            // Use wxPdfDocument API directly as wxDC doesn't provide a way to
-            // set the image scale at PDF level and also because passing via
-            // wxDC wastefully converts wxImage to wxBitmap only to convert it
-            // back to wxImage when embedding it into the PDF.
-            wxPdfDocument& pdf_doc = pdf_writer.pdf_document();
-
-            pdf_doc.SetImageScale(scale);
-            pdf_doc.Image(image_name, image, x, *pos_y);
-            pdf_doc.SetImageScale(1);
-            }
-            break;
-        case e_output_measure_only:
-            // Do nothing.
-            break;
-        default:
-            {
-            alarum() << "Case " << output_mode << " not found." << LMI_FLUSH;
-            }
-        }
-
-    *pos_y += y;
-}
-
-/// Render, or just pretend rendering in order to measure it, the given HTML
-/// contents at the specified position wrapping it at the given width.
-/// Return the height of the output (using this width).
-
-int output_html
-    (pdf_writer_wx& pdf_writer
-    ,int x
-    ,int y
-    ,int width
-    ,wxString const& html
-    ,enum_output_mode output_mode = e_output_normal
-    )
-{
-    wxHtmlWinParser& html_parser = pdf_writer.html_parser();
-
-    std::unique_ptr<wxHtmlContainerCell> const cell
-        (static_cast<wxHtmlContainerCell*>(html_parser.Parse(html))
-        );
-    LMI_ASSERT(cell);
-
-    cell->Layout(width);
-    switch(output_mode)
-        {
-        case e_output_normal:
-            {
-            wxHtmlRenderingInfo rendering_info;
-            cell->Draw
-                (*html_parser.GetDC()
-                ,x
-                ,y
-                ,0
-                ,std::numeric_limits<int>::max()
-                ,rendering_info
-                );
-            }
-            break;
-        case e_output_measure_only:
-            // Do nothing.
-            break;
-        default:
-            {
-            alarum() << "Case " << output_mode << " not found." << LMI_FLUSH;
-            }
-        }
-
-    return cell->GetHeight();
-}
-
 enum enum_group_quote_columns
     {e_col_number
     ,e_col_name
@@ -1055,7 +954,7 @@ void group_quote_pdf_generator_wx::output_image_header
     double const image_width = banner_image.GetWidth();
     double const scale = image_width / pdf_writer.get_total_width();
     int const pos_top = *pos_y;
-    output_image(pdf_writer, banner_image, "banner", scale, 0, pos_y);
+    pdf_writer.output_image(banner_image, "banner", scale, 0, pos_y);
 
     auto& pdf_dc = pdf_writer.dc();
 
@@ -1101,7 +1000,12 @@ void group_quote_pdf_generator_wx::output_document_header
         ,escape_for_html_elem(report_data_.prepared_by_)
         );
 
-    output_html(pdf_writer, pdf_writer.get_horz_margin(), *pos_y, 
pdf_writer.get_page_width() / 2, title_html);
+    pdf_writer.output_html
+        (pdf_writer.get_horz_margin()
+        ,*pos_y
+        ,pdf_writer.get_page_width() / 2
+        ,title_html
+        );
 
     // Build the summary table with all the mandatory fields.
     wxString summary_html =
@@ -1152,9 +1056,8 @@ void group_quote_pdf_generator_wx::output_document_header
     // Finally close the summary table.
     summary_html += "</table>";
 
-    int const summary_height = output_html
-        (pdf_writer
-        ,pdf_writer.get_horz_margin() + pdf_writer.get_page_width() / 2
+    int const summary_height = pdf_writer.output_html
+        (pdf_writer.get_horz_margin() + pdf_writer.get_page_width() / 2
         ,*pos_y
         ,pdf_writer.get_page_width() / 2
         ,summary_html
@@ -1320,9 +1223,8 @@ void group_quote_pdf_generator_wx::output_footer
         {
         // Arbitrarily scale down the logo by a factor of 2 to avoid making it
         // too big.
-        output_image
-            (pdf_writer
-            ,logo_image
+        pdf_writer.output_image
+            (logo_image
             ,"company_logo"
             ,2.0
             ,pdf_writer.get_horz_margin()
@@ -1335,9 +1237,8 @@ void group_quote_pdf_generator_wx::output_footer
 
     wxString const footer_html = "<p>" + report_data_.footer_html_ + "</p>";
 
-    *pos_y += output_html
-        (pdf_writer
-        ,pdf_writer.get_horz_margin()
+    *pos_y += pdf_writer.output_html
+        (pdf_writer.get_horz_margin()
         ,*pos_y
         ,pdf_writer.get_page_width()
         ,footer_html
diff --git a/pdf_writer_wx.cpp b/pdf_writer_wx.cpp
index 67d275e..72d8105 100644
--- a/pdf_writer_wx.cpp
+++ b/pdf_writer_wx.cpp
@@ -23,6 +23,10 @@
 
 #include "pdf_writer_wx.hpp"
 
+#include "alert.hpp"
+
+#include <wx/html/htmlcell.h>
+
 namespace
 {
 
@@ -81,6 +85,98 @@ pdf_writer_wx::pdf_writer_wx
         );
 }
 
+/// Output an image at the given scale into the PDF.
+///
+/// The scale specifies how many times the image should be shrunk:
+/// scale > 1 makes the image smaller, while scale < 1 makes it larger.
+///
+/// Updates pos_y by increasing it by the height of the specified
+/// image at the given scale.
+
+void pdf_writer_wx::output_image
+    (wxImage const&   image
+    ,char const*      image_name
+    ,double           scale
+    ,int              x
+    ,int*             pos_y
+    ,enum_output_mode output_mode
+    )
+{
+    int const y = wxRound(image.GetHeight() / scale);
+
+    switch(output_mode)
+        {
+        case e_output_normal:
+            {
+            // Use wxPdfDocument API directly as wxDC doesn't provide a way to
+            // set the image scale at PDF level and also because passing via
+            // wxDC wastefully converts wxImage to wxBitmap only to convert it
+            // back to wxImage when embedding it into the PDF.
+            wxPdfDocument* const pdf_doc = pdf_dc_.GetPdfDocument();
+            LMI_ASSERT(pdf_doc);
+
+            pdf_doc->SetImageScale(scale);
+            pdf_doc->Image(image_name, image, x, *pos_y);
+            pdf_doc->SetImageScale(1);
+            }
+            break;
+        case e_output_measure_only:
+            // Do nothing.
+            break;
+        default:
+            {
+            alarum() << "Case " << output_mode << " not found." << LMI_FLUSH;
+            }
+        }
+
+    *pos_y += y;
+}
+
+/// Render, or just pretend rendering in order to measure it, the given HTML
+/// contents at the specified position wrapping it at the given width.
+/// Return the height of the output (using this width).
+
+int pdf_writer_wx::output_html
+    (int x
+    ,int y
+    ,int width
+    ,wxString const& html
+    ,enum_output_mode output_mode
+    )
+{
+    std::unique_ptr<wxHtmlContainerCell> const cell
+        (static_cast<wxHtmlContainerCell*>(html_parser_.Parse(html))
+        );
+    LMI_ASSERT(cell);
+
+    cell->Layout(width);
+    switch(output_mode)
+        {
+        case e_output_normal:
+            {
+            wxHtmlRenderingInfo rendering_info;
+            cell->Draw
+                (pdf_dc_
+                ,x
+                ,y
+                ,0
+                ,std::numeric_limits<int>::max()
+                ,rendering_info
+                );
+            }
+            break;
+        case e_output_measure_only:
+            // Do nothing.
+            break;
+        default:
+            {
+            alarum() << "Case " << output_mode << " not found." << LMI_FLUSH;
+            }
+        }
+
+    return cell->GetHeight();
+}
+
 int pdf_writer_wx::get_horz_margin() const
 {
     return horz_margin;
diff --git a/pdf_writer_wx.hpp b/pdf_writer_wx.hpp
index ad786a5..fff8a53 100644
--- a/pdf_writer_wx.hpp
+++ b/pdf_writer_wx.hpp
@@ -24,10 +24,19 @@
 
 #include "config.hpp"
 
+#include "assert_lmi.hpp"
+
 #include <wx/html/winpars.h>
 
 #include <wx/pdfdc.h>
 
+#include <memory>                       // std::unique_ptr
+
+enum enum_output_mode
+    {e_output_normal
+    ,e_output_measure_only
+    };
+
 class pdf_writer_wx
 {
   public:
@@ -41,9 +50,26 @@ class pdf_writer_wx
 
     ~pdf_writer_wx();
 
+    // High level functions which should be preferably used if possible.
+    int output_html
+        (int x
+        ,int y
+        ,int width
+        ,wxString const& html
+        ,enum_output_mode output_mode = e_output_normal
+        );
+
+    void output_image
+        (wxImage const&   image
+        ,char const*      image_name
+        ,double           scale
+        ,int              x
+        ,int*             pos_y
+        ,enum_output_mode output_mode = e_output_normal
+        );
+
+    // Accessors allowing to use lower level wxDC API directly.
     wxDC& dc() { return pdf_dc_; }
-    wxPdfDocument& pdf_document() { return *pdf_dc_.GetPdfDocument(); }
-    wxHtmlWinParser& html_parser() { return html_parser_; }
 
     // Page metrics: the page width and height are the size of the page region
     // reserved for the normal contents, excluding horizontal and vertical



reply via email to

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