lmi-commits
[Top][All Lists]
Advanced

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

[lmi-commits] [lmi] master 11da7b5 1/9: Extract get_needed_pages_count()


From: Greg Chicares
Subject: [lmi-commits] [lmi] master 11da7b5 1/9: Extract get_needed_pages_count() from PDF generation code
Date: Fri, 9 Feb 2018 17:39:15 -0500 (EST)

branch: master
commit 11da7b51bca1bbef87fea962bc8f6c89827f1cfc
Author: Vadim Zeitlin <address@hidden>
Commit: Gregory W. Chicares <address@hidden>

    Extract get_needed_pages_count() from PDF generation code
    
    No real changes, this is a pure refactoring done in order to allow
    testing this function more easily.
---
 ledger_pdf_generator_wx.cpp | 35 ++++++-----------------------------
 miscellany.cpp              | 38 ++++++++++++++++++++++++++++++++++++++
 miscellany.hpp              | 11 +++++++++++
 3 files changed, 55 insertions(+), 29 deletions(-)

diff --git a/ledger_pdf_generator_wx.cpp b/ledger_pdf_generator_wx.cpp
index a3a1f4c..f9ac78e 100644
--- a/ledger_pdf_generator_wx.cpp
+++ b/ledger_pdf_generator_wx.cpp
@@ -35,7 +35,7 @@
 #include "ledger_evaluator.hpp"
 #include "ledger_invariant.hpp"
 #include "ledger_variant.hpp"
-#include "miscellany.hpp"               // lmi_tolower()
+#include "miscellany.hpp"               // lmi_tolower(), 
get_needed_pages_count()
 #include "pdf_writer_wx.hpp"
 #include "wx_table_generator.hpp"
 
@@ -1765,35 +1765,12 @@ class page_with_tabular_report
             throw std::runtime_error("no space left for tabular report");
             }
 
-        // Each group actually takes rows_per_group+1 rows because of the
-        // separator row between groups, hence the second +1, but there is no
-        // need for the separator after the last group, hence the first +1.
-        int const groups_per_page = (rows_per_page + 1) / (rows_per_group + 1);
-
-        // But we are actually interested in the number of years per page and
-        // not the number of groups.
-        int const years_per_page = groups_per_page * rows_per_group;
-
-        int const total_years = ledger.GetMaxLength();
-
-        // Finally determine how many pages are needed to show all the years.
-        int num_pages = (total_years + years_per_page - 1) / years_per_page;
-
-        // The last page may not be needed if all the rows on it can fit into 
the
-        // remaining space, too small for a full group, but perhaps sufficient 
for
-        // these rows, in the last by one page.
-        if (num_pages > 1)
-            {
-            auto const rows_on_last_page = total_years - (num_pages - 
1)*years_per_page;
-            auto const free_rows = rows_per_page - 
groups_per_page*(rows_per_group + 1);
-            if(rows_on_last_page <= free_rows)
-                {
-                num_pages--;
-                }
-            }
-
         // We return the number of extra pages only, hence -1.
-        return num_pages - 1;
+        return get_needed_pages_count
+            (ledger.GetMaxLength()
+            ,rows_per_page
+            ,rows_per_group
+            ) - 1;
     }
 };
 
diff --git a/miscellany.cpp b/miscellany.cpp
index c0cbdb9..a300adb 100644
--- a/miscellany.cpp
+++ b/miscellany.cpp
@@ -190,3 +190,41 @@ std::string iso_8601_datestamp_terse()
     return s;
 }
 
+int get_needed_pages_count
+    (int total_rows
+    ,int rows_per_page
+    ,int rows_per_group
+    )
+{
+    // The caller must check for this precondition because this function is too
+    // low-level to be able to handle it correctly, e.g. it can't even use the
+    // appropriate error message.
+    LMI_ASSERT(rows_per_page >= rows_per_group);
+
+    // Each group actually takes rows_per_group+1 rows because of the
+    // separator row between groups, hence the second +1, but there is no
+    // need for the separator after the last group, hence the first +1.
+    int const groups_per_page = (rows_per_page + 1) / (rows_per_group + 1);
+
+    // But we are actually interested in the number of rows we can fit per page
+    // and not the number of groups.
+    int const used_per_page = groups_per_page * rows_per_group;
+
+    // Finally determine how many pages are needed to show all the rows.
+    int num_pages = (total_rows + used_per_page - 1) / used_per_page;
+
+    // The last page may not be needed if all the rows on it can fit into the
+    // remaining space, too small for a full group, but perhaps sufficient for
+    // these rows, in the last by one page.
+    if (num_pages > 1)
+        {
+        auto const rows_on_last_page = total_rows - (num_pages - 
1)*used_per_page;
+        auto const free_rows = rows_per_page - groups_per_page*(rows_per_group 
+ 1);
+        if(rows_on_last_page <= free_rows)
+            {
+            num_pages--;
+            }
+        }
+
+    return num_pages;
+}
diff --git a/miscellany.hpp b/miscellany.hpp
index 3cdab9d..722683f 100644
--- a/miscellany.hpp
+++ b/miscellany.hpp
@@ -213,5 +213,16 @@ inline void stifle_warning_for_unused_value(T const& t)
     (void)&t;
 }
 
+/// Compute the number of pages needed to display the given number of non-blank
+/// rows in groups of the specified size separated by blank rows.
+///
+/// Preconditions: total_rows > 0 && rows_per_page >= rows_per_group > 0
+
+int LMI_SO get_needed_pages_count
+    (int total_rows
+    ,int rows_per_page
+    ,int rows_per_group
+    );
+
 #endif // miscellany_hpp
 



reply via email to

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