summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorKohei Yoshida <kohei.yoshida@gmail.com>2022-09-07 22:25:43 -0400
committerKohei Yoshida <kohei.yoshida@gmail.com>2022-09-07 22:41:58 -0400
commit81f3cd6cdb98ecbf340423c8a6a3ece3d81c7ce8 (patch)
treeaf0e0daea02577f9f29200de8e21bc48843e97ca
parentfd92263b35df127ec4d6b8a4fde255dab491d512 (diff)
downloadorcus-feature/157-cell-properties.tar.gz
Make wrap-text and shrink-to-fit optional feature/157-cell-properties
ODF styles need this behavior for style hierarchy to work correctly.
-rw-r--r--include/orcus/spreadsheet/styles.hpp5
-rw-r--r--src/liborcus/odf_style_context.cpp4
-rw-r--r--src/liborcus/odf_styles.hpp5
-rw-r--r--src/liborcus/odf_styles_context.cpp28
-rw-r--r--src/orcus_test_ods.cpp2
-rw-r--r--src/orcus_test_xls_xml.cpp9
-rw-r--r--src/orcus_test_xlsx.cpp8
-rw-r--r--src/spreadsheet/debug_state_dumper.cpp14
-rw-r--r--src/spreadsheet/styles.cpp4
9 files changed, 55 insertions, 24 deletions
diff --git a/include/orcus/spreadsheet/styles.hpp b/include/orcus/spreadsheet/styles.hpp
index 8aafe46d..4ee34233 100644
--- a/include/orcus/spreadsheet/styles.hpp
+++ b/include/orcus/spreadsheet/styles.hpp
@@ -14,6 +14,7 @@
#include <memory>
#include <string_view>
+#include <optional>
namespace orcus { namespace spreadsheet {
@@ -219,14 +220,14 @@ struct ORCUS_SPM_DLLPUBLIC cell_format_t
size_t style_xf; /// style XF ID (used only for cell format)
hor_alignment_t hor_align;
ver_alignment_t ver_align;
+ std::optional<bool> wrap_text;
+ std::optional<bool> shrink_to_fit;
bool apply_num_format:1;
bool apply_font:1;
bool apply_fill:1;
bool apply_border:1;
bool apply_alignment:1;
bool apply_protection:1;
- bool wrap_text:1;
- bool shrink_to_fit:1;
cell_format_t();
void reset();
diff --git a/src/liborcus/odf_style_context.cpp b/src/liborcus/odf_style_context.cpp
index c1032ca9..68d2f9bc 100644
--- a/src/liborcus/odf_style_context.cpp
+++ b/src/liborcus/odf_style_context.cpp
@@ -518,8 +518,8 @@ void style_context::start_table_cell_properties(const xml_token_pair_t& parent,
border_map_type border_styles;
ss::ver_alignment_t ver_alignment = ss::ver_alignment_t::unknown;
- bool wrap_text = false;
- bool shrink_to_fit = false;
+ std::optional<bool> wrap_text;
+ std::optional<bool> shrink_to_fit;
for (const xml_token_attr_t& attr : attrs)
{
diff --git a/src/liborcus/odf_styles.hpp b/src/liborcus/odf_styles.hpp
index 5da14b54..861d330e 100644
--- a/src/liborcus/odf_styles.hpp
+++ b/src/liborcus/odf_styles.hpp
@@ -15,6 +15,7 @@
#include <memory>
#include <variant>
#include <ostream>
+#include <optional>
namespace orcus {
@@ -56,8 +57,8 @@ struct odf_style
std::size_t number_format = 0;
spreadsheet::hor_alignment_t hor_align = spreadsheet::hor_alignment_t::unknown;
spreadsheet::ver_alignment_t ver_align = spreadsheet::ver_alignment_t::unknown;
- bool wrap_text = false;
- bool shrink_to_fit = false;
+ std::optional<bool> wrap_text;
+ std::optional<bool> shrink_to_fit;
};
struct table
diff --git a/src/liborcus/odf_styles_context.cpp b/src/liborcus/odf_styles_context.cpp
index 7e587962..36a486ec 100644
--- a/src/liborcus/odf_styles_context.cpp
+++ b/src/liborcus/odf_styles_context.cpp
@@ -167,11 +167,17 @@ void styles_context::end_child_context(xmlns_id_t ns, xml_token_t name, xml_cont
xf->set_fill(cell.fill);
xf->set_border(cell.border);
xf->set_protection(cell.protection);
- xf->set_horizontal_alignment(cell.hor_align);
- xf->set_vertical_alignment(cell.ver_align);
xf->set_number_format(cell.number_format);
- xf->set_wrap_text(cell.wrap_text);
- xf->set_shrink_to_fit(cell.shrink_to_fit);
+
+ if (cell.hor_align != ss::hor_alignment_t::unknown)
+ xf->set_horizontal_alignment(cell.hor_align);
+ if (cell.ver_align != ss::ver_alignment_t::unknown)
+ xf->set_vertical_alignment(cell.ver_align);
+ if (cell.wrap_text)
+ xf->set_wrap_text(*cell.wrap_text);
+ if (cell.shrink_to_fit)
+ xf->set_shrink_to_fit(*cell.shrink_to_fit);
+
cell.xf = xf->commit();
}
else
@@ -184,11 +190,17 @@ void styles_context::end_child_context(xmlns_id_t ns, xml_token_t name, xml_cont
xf->set_fill(cell.fill);
xf->set_border(cell.border);
xf->set_protection(cell.protection);
- xf->set_horizontal_alignment(cell.hor_align);
- xf->set_vertical_alignment(cell.ver_align);
xf->set_number_format(cell.number_format);
- xf->set_wrap_text(cell.wrap_text);
- xf->set_shrink_to_fit(cell.shrink_to_fit);
+
+ if (cell.hor_align != ss::hor_alignment_t::unknown)
+ xf->set_horizontal_alignment(cell.hor_align);
+ if (cell.ver_align != ss::ver_alignment_t::unknown)
+ xf->set_vertical_alignment(cell.ver_align);
+ if (cell.wrap_text)
+ xf->set_wrap_text(*cell.wrap_text);
+ if (cell.shrink_to_fit)
+ xf->set_shrink_to_fit(*cell.shrink_to_fit);
+
size_t style_xf_id = xf->commit();
auto* cell_style = mp_styles->get_cell_style();
diff --git a/src/orcus_test_ods.cpp b/src/orcus_test_ods.cpp
index 9b8bc85d..cc5e6714 100644
--- a/src/orcus_test_ods.cpp
+++ b/src/orcus_test_ods.cpp
@@ -332,6 +332,7 @@ void test_ods_import_cell_properties()
xf = styles.get_cell_format(xfid);
assert(xf);
assert(xf->wrap_text);
+ assert(*xf->wrap_text);
assert(!xf->shrink_to_fit);
xfid = sh->get_cell_format(2, 1); // B3
@@ -339,6 +340,7 @@ void test_ods_import_cell_properties()
assert(xf);
assert(!xf->wrap_text);
assert(xf->shrink_to_fit);
+ assert(*xf->shrink_to_fit);
}
} // anonymous namespace
diff --git a/src/orcus_test_xls_xml.cpp b/src/orcus_test_xls_xml.cpp
index 0dfc27ce..376b0456 100644
--- a/src/orcus_test_xls_xml.cpp
+++ b/src/orcus_test_xls_xml.cpp
@@ -1041,6 +1041,7 @@ void test_xls_xml_cell_properties()
std::size_t xfid = sh->get_cell_format(0, 1); // B1
const ss::cell_format_t* xf = styles.get_cell_format(xfid);
assert(xf);
+ // TODO: These results may not be correct. We need to properly import the "Default" style.
assert(!xf->wrap_text);
assert(!xf->shrink_to_fit);
@@ -1048,13 +1049,17 @@ void test_xls_xml_cell_properties()
xf = styles.get_cell_format(xfid);
assert(xf);
assert(xf->wrap_text);
- assert(!xf->shrink_to_fit);
+ assert(*xf->wrap_text);
+ assert(xf->shrink_to_fit);
+ assert(!*xf->shrink_to_fit);
xfid = sh->get_cell_format(2, 1); // B3
xf = styles.get_cell_format(xfid);
assert(xf);
- assert(!xf->wrap_text);
+ assert(xf->wrap_text);
+ assert(!*xf->wrap_text);
assert(xf->shrink_to_fit);
+ assert(*xf->shrink_to_fit);
}
void test_xls_xml_view_cursor_per_sheet()
diff --git a/src/orcus_test_xlsx.cpp b/src/orcus_test_xlsx.cpp
index ea6ef8a6..8c871419 100644
--- a/src/orcus_test_xlsx.cpp
+++ b/src/orcus_test_xlsx.cpp
@@ -807,13 +807,17 @@ void test_xlsx_cell_properties()
xf = styles.get_cell_format(xfid);
assert(xf);
assert(xf->wrap_text);
- assert(!xf->shrink_to_fit);
+ assert(*xf->wrap_text);
+ assert(xf->shrink_to_fit);
+ assert(!*xf->shrink_to_fit);
xfid = sh->get_cell_format(2, 1); // B3
xf = styles.get_cell_format(xfid);
assert(xf);
- assert(!xf->wrap_text);
+ assert(xf->wrap_text);
+ assert(!*xf->wrap_text);
assert(xf->shrink_to_fit);
+ assert(*xf->shrink_to_fit);
}
void test_xlsx_pivot_two_pivot_caches()
diff --git a/src/spreadsheet/debug_state_dumper.cpp b/src/spreadsheet/debug_state_dumper.cpp
index 70ad2c85..cdf076be 100644
--- a/src/spreadsheet/debug_state_dumper.cpp
+++ b/src/spreadsheet/debug_state_dumper.cpp
@@ -48,7 +48,15 @@ void doc_debug_state_dumper::dump_styles(const fs::path& outdir) const
of << std::boolalpha;
- auto dump_xf = [&of](std::size_t i, const cell_format_t& xf)
+ auto to_string = [](std::optional<bool> v) -> std::string
+ {
+ if (!v)
+ return "(unset)";
+
+ return *v ? "true" : "false";
+ };
+
+ auto dump_xf = [&of,to_string](std::size_t i, const cell_format_t& xf)
{
of << " - id: " << i << std::endl
<< " font: " << xf.font << std::endl
@@ -65,8 +73,8 @@ void doc_debug_state_dumper::dump_styles(const fs::path& outdir) const
<< " apply-border: " << xf.apply_border << std::endl
<< " apply-alignment: " << xf.apply_alignment << std::endl
<< " apply-protection: " << xf.apply_protection << std::endl
- << " wrap-text: " << xf.wrap_text << std::endl
- << " shrink-to-fit: " << xf.shrink_to_fit << std::endl;
+ << " wrap-text: " << to_string(xf.wrap_text) << std::endl
+ << " shrink-to-fit: " << to_string(xf.shrink_to_fit) << std::endl;
};
auto active_value = [&of](std::string_view name, const auto& v, bool active, int level=2)
diff --git a/src/spreadsheet/styles.cpp b/src/spreadsheet/styles.cpp
index 085c3652..93546c44 100644
--- a/src/spreadsheet/styles.cpp
+++ b/src/spreadsheet/styles.cpp
@@ -276,9 +276,7 @@ cell_format_t::cell_format_t() :
apply_fill(false),
apply_border(false),
apply_alignment(false),
- apply_protection(false),
- wrap_text(false),
- shrink_to_fit(false)
+ apply_protection(false)
{
}