From 3fe800c1c2244efeda323e4a5eea8597666be180 Mon Sep 17 00:00:00 2001 From: Christoph Schaefer Date: Thu, 19 Nov 2026 22:26:47 +0100 Subject: [PATCH] fix(OdfFile): cycle the default chart series colours When a chart from an OOXML file is written to ODF, every series gets a default colour from a fixed table of 66 entries, picked by the series number. The number was not checked against the table size, so a chart with more than 66 series read past the end of the table: the extra series got an empty colour, and with a few hundred series the conversion stopped. The series number now wraps around the table, so series 67 and later start over at the first colour, as LibreOffice does with its default colour palette. Charts with fewer than 66 series are written as before. The new test converts an XLSX with a 70-series line chart to ODS and checks that every series has a colour and that series 67-70 continue the colour cycle. Assisted-by: ClaudeCode:claude-opus-5-5 Co-Authored-By: Claude Opus 5.5 Signed-off-by: Christoph Schaefer --- OdfFile/Test/test_odf/CMakeLists.txt | 1 + .../ExampleFiles/chart_line_70_series.xlsx | Bin 0 -> 5309 bytes .../test_odf/xlsx2ods_chart_many_series.cpp | 107 ++++++++++++++++++ OdfFile/Writer/Format/odf_chart_context.cpp | 4 +- REUSE.toml | 5 + 5 files changed, 116 insertions(+), 1 deletion(-) create mode 100644 OdfFile/Test/test_odf/ExampleFiles/chart_line_70_series.xlsx create mode 100644 OdfFile/Test/test_odf/xlsx2ods_chart_many_series.cpp diff --git a/OdfFile/Test/test_odf/CMakeLists.txt b/OdfFile/Test/test_odf/CMakeLists.txt index a0de9011ca..c0dc40e37e 100644 --- a/OdfFile/Test/test_odf/CMakeLists.txt +++ b/OdfFile/Test/test_odf/CMakeLists.txt @@ -138,6 +138,7 @@ add_core_gtest( xlsx2ods_fit_to_pages.cpp ods2xlsx_styles_auto_data_style.cpp odt2docx_tracked_deleted_table.cpp + xlsx2ods_chart_many_series.cpp ${CORE_ROOT_DIR}/Common/OfficeFileFormatChecker2.cpp WORKING_DIRECTORY "${_test_odf_run_dir}" ) diff --git a/OdfFile/Test/test_odf/ExampleFiles/chart_line_70_series.xlsx b/OdfFile/Test/test_odf/ExampleFiles/chart_line_70_series.xlsx new file mode 100644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dfFile/Test/test_odf/xlsx2ods_chart_many_series.cpp new file mode 100644 index 0000000000..63bf0d0360 --- /dev/null +++ b/OdfFile/Test/test_odf/xlsx2ods_chart_many_series.cpp @@ -0,0 +1,107 @@ +// SPDX-FileCopyrightText: 2026 Euro-Office contributors +// SPDX-License-Identifier: AGPL-3.0-only + +// XLSX -> ODS: a chart series without its own colour gets one from a fixed table +// of default colours. A chart can have more series than the table has entries; +// those series start over at the first colour, as in LibreOffice. + +#include "gtest/gtest.h" + +#include "common.h" + +#include +#include +#include + +namespace +{ + std::wstring attribute(const std::wstring& tag, const std::wstring& name) + { + const std::wstring key = L" " + name + L"=\""; + const size_t pos = tag.find(key); + if (pos == std::wstring::npos) + return L""; + const size_t start = pos + key.size(); + const size_t end = tag.find(L'"', start); + if (end == std::wstring::npos) + return L""; + return tag.substr(start, end - start); + } + + // Start tags of all elements with the given name, in document order. + std::vector start_tags(const std::wstring& xml, const std::wstring& name) + { + std::vector tags; + const std::wstring open = L"<" + name + L" "; + size_t pos = 0; + while ((pos = xml.find(open, pos)) != std::wstring::npos) + { + const size_t end = xml.find(L'>', pos); + if (end == std::wstring::npos) + break; + tags.push_back(xml.substr(pos, end - pos + 1)); + pos = end; + } + return tags; + } + + // Stroke colour of each chart style, by style name. + std::map stroke_colors(const std::wstring& xml) + { + std::map colors; + size_t pos = 0; + while ((pos = xml.find(L"", pos); + if (end == std::wstring::npos) + break; + const std::wstring style = xml.substr(pos, end - pos); + const std::wstring name = attribute(style.substr(0, style.find(L'>') + 1), L"style:name"); + const std::vector graphic = start_tags(style, L"style:graphic-properties"); + if (!name.empty() && !graphic.empty()) + colors[name] = attribute(graphic.front(), L"svg:stroke-color"); + pos = end; + } + return colors; + } + + bool is_hex_color(const std::wstring& value) + { + if (value.size() != 7 || value[0] != L'#') + return false; + for (size_t i = 1; i < value.size(); ++i) + { + const wchar_t c = value[i]; + if (!((c >= L'0' && c <= L'9') || (c >= L'a' && c <= L'f') || (c >= L'A' && c <= L'F'))) + return false; + } + return true; + } +} // namespace + +TEST(XlsxToOdsChartManySeries, SeriesBeyondTheDefaultColorsStartOver) +{ + const std::wstring dir = convert_ooxml_to_odf_dir(L"ExampleFiles/chart_line_70_series.xlsx"); + ASSERT_FALSE(dir.empty()) << "conversion failed"; + const std::wstring xml = read_part(dir, L"Object 1/content.xml"); + ASSERT_FALSE(xml.empty()) << "Object 1/content.xml is missing"; + + const std::vector series = start_tags(xml, L"chart:series"); + ASSERT_EQ(series.size(), 70u); + + const std::map colors = stroke_colors(xml); + std::vector series_colors; + for (const std::wstring& tag : series) + { + const auto it = colors.find(attribute(tag, L"chart:style-name")); + ASSERT_NE(it, colors.end()) << "no style for " << std::string(tag.begin(), tag.end()); + EXPECT_TRUE(is_hex_color(it->second)) + << "series " << series_colors.size() + 1 << " has stroke colour '" + << std::string(it->second.begin(), it->second.end()) << "'"; + series_colors.push_back(it->second); + } + + // The default colours repeat every 11 series; series 67-70 continue that cycle. + for (size_t i = 66; i < series_colors.size(); ++i) + EXPECT_EQ(series_colors[i], series_colors[i - 11]) << "series " << i + 1; +} diff --git a/OdfFile/Writer/Format/odf_chart_context.cpp b/OdfFile/Writer/Format/odf_chart_context.cpp index 0bef02986c..b33cfbeef0 100644 --- a/OdfFile/Writer/Format/odf_chart_context.cpp +++ b/OdfFile/Writer/Format/odf_chart_context.cpp @@ -334,7 +334,9 @@ void odf_chart_context::Impl::set_default_series_color() if (current_level_.empty()) return; if (!current_level_.back().graphic_properties) return; - color col = color(default_MS_series_colors[current_series_count_]); + // The table has a fixed size, a chart can have more series: start over, as LibreOffice does. + const int colors_count = static_cast(sizeof(default_MS_series_colors) / sizeof(default_MS_series_colors[0])); + color col = color(default_MS_series_colors[current_series_count_ % colors_count]); current_level_.back().graphic_properties->common_draw_fill_attlist_.draw_fill_color_ = col; current_level_.back().graphic_properties->svg_stroke_color_ = col; diff --git a/REUSE.toml b/REUSE.toml index 198c7e1620..d3deb1f329 100644 --- a/REUSE.toml +++ b/REUSE.toml @@ -339,6 +339,11 @@ path = [ SPDX-FileCopyrightText = "2026 Euro-Office contributors" SPDX-License-Identifier = "AGPL-3.0-only" +[[annotations]] +path = "OdfFile/Test/test_odf/ExampleFiles/chart_line_70_series.xlsx" +SPDX-FileCopyrightText = "2026 Euro-Office contributors" +SPDX-License-Identifier = "AGPL-3.0-only" + [[annotations]] path = [ "OdfFile/Test/test_odf/ExampleFiles/numbered_paragraph_first.odt",