Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 1 addition & 10 deletions src/odr/internal/odf/odf_style.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
#include <odr/internal/odf/odf_parser.hpp>
#include <odr/internal/xml/xml_util.hpp>

#include <algorithm>
#include <array>
#include <cstdlib>
#include <cstring>
Expand Down Expand Up @@ -801,15 +800,7 @@ pugi::xml_node properties_of(pugi::xml_node style, const char *name) {
if (pugi::xml_node existing = style.child(name)) {
return existing;
}
const auto rank = [](const std::string_view child) {
return std::ranges::find(order, child) - std::begin(order);
};
for (pugi::xml_node child : style.children()) {
if (rank(child.name()) > rank(name)) {
return style.insert_child_before(name, child);
}
}
return style.append_child(name);
return xml::insert_in_sequence(style, name, order);
}

const char *text_align_value(const HorizontalAlign align) {
Expand Down
2 changes: 1 addition & 1 deletion src/odr/internal/ooxml/AGENTS.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ a per-part `ParseContext` (path, its relations, the part cache).
|---|---|
| `ooxml_file.{hpp,cpp}` | `OfficeOpenXmlFile`: meta, encryption state, `decrypt()`, dispatch to the per-format `Document` on `file_type()` |
| `ooxml_meta.cpp` | `parse_file_meta`: type detection by sentinel path (`/word/document.xml`, `/ppt/presentation.xml`, `/xl/workbook.xml`); an encrypted package has `/EncryptionInfo` and `/EncryptedPackage` |
| `ooxml_util.{hpp,cpp}` | Stateless attribute readers: half-points, hundredth-points, EMUs, twips, percents, colours, borders, font weight and style; relationship parsing; `write_text_nodes`, `insert_in_sequence` for the writers |
| `ooxml_util.{hpp,cpp}` | Stateless attribute readers: half-points, hundredth-points, EMUs, twips, percents, colours, borders, font weight and style; relationship parsing; `write_text_nodes` for the writers |
| `ooxml_crypto.{hpp,cpp}` | Decryption |

## Encryption
Expand Down
18 changes: 0 additions & 18 deletions src/odr/internal/ooxml/ooxml_util.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -133,24 +133,6 @@ constexpr std::array<std::pair<std::string_view, std::uint32_t>, 16>

} // namespace

pugi::xml_node
ooxml::insert_in_sequence(pugi::xml_node parent, const char *name,
const std::span<const std::string_view> order) {
const auto rank = [&](const std::string_view child_name) {
const auto it = std::ranges::find(order, child_name);
return it == std::end(order)
? order.size()
: static_cast<std::size_t>(it - std::begin(order));
};
const std::size_t own_rank = rank(name);
for (const pugi::xml_node child : parent.children()) {
if (rank(child.name()) > own_rank) {
return parent.insert_child_before(name, child);
}
}
return parent.append_child(name);
}

std::string ooxml::hex_color(const Color &color) {
return fmt::format("{:06X}", color.rgb());
}
Expand Down
5 changes: 0 additions & 5 deletions src/odr/internal/ooxml/ooxml_util.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
#include <odr/internal/xml/xml_tree_edit.hpp>

#include <optional>
#include <span>
#include <string>
#include <string_view>
#include <unordered_map>
Expand Down Expand Up @@ -37,10 +36,6 @@ xml::NodeSpan write_text_nodes(pugi::xml_node parent, pugi::xml_node before,
const std::string &text,
std::string_view prefix);

/// Inserts a child @p name into @p parent at its place in the schema
/// sequence @p order; a child the sequence does not name ranks last.
pugi::xml_node insert_in_sequence(pugi::xml_node parent, const char *name,
std::span<const std::string_view> order);
/// `RRGGBB`, as `w:color/@w:val` and `a:srgbClr/@val` spell one.
std::string hex_color(const Color &color);
/// The `w:highlight` name of @p color, where it is one of the sixteen
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -211,14 +211,10 @@ constexpr std::array<const char *, 6> fill_names{"a:noFill", "a:solidFill",
/// size as attributes, the colour and the highlight as children.
void write_run_properties(pugi::xml_node properties, const TextStyle &style) {
const auto attribute = [&](const char *name, const std::string &value) {
pugi::xml_attribute attr = properties.attribute(name);
if (!attr) {
attr = properties.append_attribute(name);
}
attr.set_value(value.c_str());
xml::set_attribute(properties, name, value.c_str());
};
const auto solid = [&](const char *name, const Color &color) {
insert_in_sequence(properties, name, run_property_order)
xml::insert_in_sequence(properties, name, run_property_order)
.append_child("a:srgbClr")
.append_attribute("val")
.set_value(hex_color(color).c_str());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ pugi::xml_node ordered_child(pugi::xml_node parent, const char *name,
if (const pugi::xml_node existing = parent.child(name)) {
return existing;
}
return insert_in_sequence(parent, name, order);
return xml::insert_in_sequence(parent, name, order);
}

/// The @p name child of `styleSheet`, made in the order [ECMA-376] 18.8.39
Expand Down
2 changes: 1 addition & 1 deletion src/odr/internal/ooxml/text/ooxml_text_document.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,7 @@ constexpr std::array<std::string_view, 39> run_property_order{
/// `w:themeColor` would win over a new `w:val`.
pugi::xml_node set_run_property(pugi::xml_node properties, const char *name) {
properties.remove_child(name);
return insert_in_sequence(properties, name, run_property_order);
return xml::insert_in_sequence(properties, name, run_property_order);
}

/// Writes the set fields of @p style into a `w:rPr`, the complex-script
Expand Down
5 changes: 3 additions & 2 deletions src/odr/internal/xml/AGENTS.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@ xml does differently, and why. Open work is in [`PLAN.md`](PLAN.md).
## Three things live here

- `xml_util` is the shared plumbing: `parse`, `escape_text`,
`escape_attribute`, `read_declared_encoding`, `tokenize_text`. odf, ooxml,
svg and the html writer go through it. It depends on no other engine.
`escape_attribute`, `read_declared_encoding`, `tokenize_text`, and for the
writers `set_attribute` and `insert_in_sequence`. odf, ooxml, svg and the
html writer go through it. It depends on no other engine.
- `xml_tree_edit` is the node editing the odf and ooxml write sides share.
- `xml_file` is the format: xml opened as a file of its own and rendered as a
source view. The rest of this file is about it.
Expand Down
19 changes: 19 additions & 0 deletions src/odr/internal/xml/xml_util.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

#include <pugixml.hpp>

#include <algorithm>
#include <cstddef>
#include <cstdint>
#include <cstdlib>
Expand Down Expand Up @@ -98,6 +99,24 @@ void xml::set_attribute(pugi::xml_node node, const char *name,
attribute.set_value(value);
}

pugi::xml_node
xml::insert_in_sequence(pugi::xml_node parent, const char *name,
const std::span<const std::string_view> order) {
const auto rank = [&](const std::string_view child_name) {
const auto it = std::ranges::find(order, child_name);
return it == std::end(order)
? order.size()
: static_cast<std::size_t>(it - std::begin(order));
};
const std::size_t own_rank = rank(name);
for (const pugi::xml_node child : parent.children()) {
if (rank(child.name()) > own_rank) {
return parent.insert_child_before(name, child);
}
}
return parent.append_child(name);
}

std::string xml::read_declared_encoding(std::istream &in) {
static constexpr std::size_t probe_size = 1024;
static constexpr std::string_view space = " \t\r\n";
Expand Down
5 changes: 5 additions & 0 deletions src/odr/internal/xml/xml_util.hpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#pragma once

#include <iosfwd>
#include <span>
#include <string>
#include <string_view>
#include <vector>
Expand Down Expand Up @@ -36,6 +37,10 @@ pugi::xml_document parse(const abstract::ReadableFilesystem &, const AbsPath &);

/// Sets the attribute @p name of @p node, appending it where it is missing.
void set_attribute(pugi::xml_node node, const char *name, const char *value);
/// Inserts a child @p name into @p parent at its place in the schema
/// sequence @p order; a child the sequence does not name ranks last.
pugi::xml_node insert_in_sequence(pugi::xml_node parent, const char *name,
std::span<const std::string_view> order);

/// Throws unless @p in holds a well formed xml document.
void check_xml_file(std::istream &in);
Expand Down
Loading