[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[lmi-commits] [lmi] master 01be23a 103/156: Automatically squeeze table
From: |
Greg Chicares |
Subject: |
[lmi-commits] [lmi] master 01be23a 103/156: Automatically squeeze table columns to make them fit if necessary |
Date: |
Tue, 30 Jan 2018 17:22:22 -0500 (EST) |
branch: master
commit 01be23a639e7aca8cebc96b5a4e36e22e4c0386b
Author: Vadim Zeitlin <address@hidden>
Commit: Vadim Zeitlin <address@hidden>
Automatically squeeze table columns to make them fit if necessary
Some tables in the illustrations are so wide that they overflow the page
width if the default generous column margins (~1em) are used, however
globally decreasing the margins for all tables doesn't seem desirable as
they look better with bigger margins.
Solve this problem by making the column margin dynamic and reducing it
automatically if the columns don't fit. This results in optimally
looking tables in all cases and saves time on tweaking the columns
manually to make them fit.
---
wx_table_generator.cpp | 49 ++++++++++++++++++++++++++++++++++++++++++-------
wx_table_generator.hpp | 1 +
2 files changed, 43 insertions(+), 7 deletions(-)
diff --git a/wx_table_generator.cpp b/wx_table_generator.cpp
index 5720f5d..078c4be 100644
--- a/wx_table_generator.cpp
+++ b/wx_table_generator.cpp
@@ -53,6 +53,7 @@ wx_table_generator::wx_table_generator
,total_width_(total_width)
,char_height_(dc_.GetCharHeight())
,row_height_((4 * char_height_ + 2) / 3) // Arbitrarily use 1.333 line
spacing.
+ ,column_margin_(dc_.GetTextExtent("M").x)
,has_column_widths_(false)
,max_header_lines_(1)
{
@@ -110,8 +111,7 @@ void wx_table_generator::add_column
{
increase_to_if_smaller(width,
dc_.GetMultiLineTextExtent(header).x);
- // Add roughly 1 em margins on both sides.
- width += dc_.GetTextExtent("MM").x;
+ width += 2*column_margin_;
}
}
@@ -173,6 +173,8 @@ void
wx_table_generator::do_compute_column_widths_if_necessary()
return;
}
+ has_column_widths_ = true;
+
int num_expand = 0;
int total_fixed = 0;
@@ -195,7 +197,43 @@ void
wx_table_generator::do_compute_column_widths_if_necessary()
if(total_width_ < total_fixed)
{
- warning() << "Not enough space for all fixed columns." << LMI_FLUSH;
+ auto const overflow = total_fixed - total_width_;
+
+ // If we have only fixed columns, try to make them fit by decreasing
+ // the margins around them if this can help, assuming that we can
+ // reduce them by up to half if really needed.
+ if(!num_expand)
+ {
+ int const num_columns = columns_.size();
+ auto const overflow_per_column =
+ (overflow + num_columns - 1)/num_columns;
+ if(overflow_per_column <= column_margin_)
+ {
+ for(auto& i : columns_)
+ {
+ if(i.is_hidden())
+ {
+ continue;
+ }
+
+ i.width_ -= overflow_per_column;
+ }
+
+ column_margin_ -= (overflow_per_column + 1)/2;
+
+ // We condensed the columns enough to make them fit, so no need
+ // for the warning and we don't have any expanding columns, so
+ // we're done.
+ return;
+ }
+ }
+
+ warning()
+ << "Not enough space for all fixed columns: "
+ << overflow
+ << " more pixels needed."
+ << LMI_FLUSH
+ ;
return;
}
@@ -217,8 +255,6 @@ void
wx_table_generator::do_compute_column_widths_if_necessary()
}
}
}
-
- has_column_widths_ = true;
}
void wx_table_generator::do_output_values
@@ -266,8 +302,7 @@ void wx_table_generator::do_output_values
}
else
{
- // Otherwise just offset it by ~1 em.
- x_text += dc_.GetTextExtent("M").x;
+ x_text += column_margin_;
}
}
diff --git a/wx_table_generator.hpp b/wx_table_generator.hpp
index a71717c..5e138ff 100644
--- a/wx_table_generator.hpp
+++ b/wx_table_generator.hpp
@@ -155,6 +155,7 @@ class wx_table_generator
// These values could be recomputed, but cache them for performance.
int const char_height_;
int row_height_;
+ int column_margin_;
struct column_info
{
- [lmi-commits] [lmi] master 1272ce2 127/156: Don't include "Assumptions" page in composite NASD illustrations, (continued)
- [lmi-commits] [lmi] master 1272ce2 127/156: Don't include "Assumptions" page in composite NASD illustrations, Greg Chicares, 2018/01/30
- [lmi-commits] [lmi] master e00ab7d 080/156: Hide "attained age" column for composite illustrations, Greg Chicares, 2018/01/30
- [lmi-commits] [lmi] master 29e63d6 117/156: Add basic illustration page of group placement illustration, Greg Chicares, 2018/01/30
- [lmi-commits] [lmi] master 1a9bfb3 137/156: Factor out standard_supplemental_report class, Greg Chicares, 2018/01/30
- [lmi-commits] [lmi] master 7c0ff88 145/156: Add copyright notices to all .mst template files, Greg Chicares, 2018/01/30
- [lmi-commits] [lmi] master 2fd6a11 147/156: Use shorter and more consistent names for external templates, Greg Chicares, 2018/01/30
- [lmi-commits] [lmi] master b09a25f 026/156: Implement numbering pages in the PDF output, Greg Chicares, 2018/01/30
- [lmi-commits] [lmi] master bb75971 033/156: Implement the second part of the narrative summary page, Greg Chicares, 2018/01/30
- [lmi-commits] [lmi] master 964db82 067/156: Rename columns_headings_page to column_headings_page, Greg Chicares, 2018/01/30
- [lmi-commits] [lmi] master 2a94469 074/156: Use Mustache template files from the data directory, Greg Chicares, 2018/01/30
- [lmi-commits] [lmi] master 01be23a 103/156: Automatically squeeze table columns to make them fit if necessary,
Greg Chicares <=
- [lmi-commits] [lmi] master 46a7442 068/156: Factor out helper render_template() method, Greg Chicares, 2018/01/30
- [lmi-commits] [lmi] master d77620f 100/156: Factor out dollar_units template from the existing ones, Greg Chicares, 2018/01/30
- [lmi-commits] [lmi] master 833eef5 086/156: Use std::make_unique<> instead of raw "new", Greg Chicares, 2018/01/30
- [lmi-commits] [lmi] master 34c088c 073/156: Move footer contents into an external template too, Greg Chicares, 2018/01/30
- [lmi-commits] [lmi] master 0ba1c32 146/156: Consistently use "curr" as abbreviation of "current", Greg Chicares, 2018/01/30
- [lmi-commits] [lmi] master 29865fc 133/156: Add supplemental report to NASD illustrations too, Greg Chicares, 2018/01/30
- [lmi-commits] [lmi] master 919d796 001/156: Remove old XSL-FO-based PDF generation code, Greg Chicares, 2018/01/30
- [lmi-commits] [lmi] master cc7fc05 143/156: Change template file extension from .mustache to .mst, Greg Chicares, 2018/01/30
- [lmi-commits] [lmi] master 08984bb 148/156: Recognize Mustache files in the coding rules test, Greg Chicares, 2018/01/30
- [lmi-commits] [lmi] master de5a5c7 151/156: Fix interpolate_string_test build, Greg Chicares, 2018/01/30