diff --git a/src/odr/internal/ooxml/ooxml_util.cpp b/src/odr/internal/ooxml/ooxml_util.cpp index fdb4abb0..15152ca5 100644 --- a/src/odr/internal/ooxml/ooxml_util.cpp +++ b/src/odr/internal/ooxml/ooxml_util.cpp @@ -12,7 +12,7 @@ namespace odr::internal { std::optional -ooxml::read_string_attribute(const pugi::xml_attribute attribute) { +ooxml::read_string_attribute(pugi::xml_attribute attribute) { if (!attribute) { return {}; } @@ -20,7 +20,7 @@ ooxml::read_string_attribute(const pugi::xml_attribute attribute) { } std::optional -ooxml::read_color_attribute(const pugi::xml_attribute attribute) { +ooxml::read_color_attribute(pugi::xml_attribute attribute) { static const std::unordered_map color_map{ {"red", {255, 0, 0}}, {"green", {0, 255, 0}}, @@ -46,7 +46,7 @@ ooxml::read_color_attribute(const pugi::xml_attribute attribute) { } std::optional -ooxml::read_half_point_attribute(const pugi::xml_attribute attribute) { +ooxml::read_half_point_attribute(pugi::xml_attribute attribute) { if (!attribute) { return {}; } @@ -54,7 +54,7 @@ ooxml::read_half_point_attribute(const pugi::xml_attribute attribute) { } std::optional -ooxml::read_hundredth_point_attribute(const pugi::xml_attribute attribute) { +ooxml::read_hundredth_point_attribute(pugi::xml_attribute attribute) { if (!attribute) { return {}; } @@ -62,7 +62,7 @@ ooxml::read_hundredth_point_attribute(const pugi::xml_attribute attribute) { } std::optional -ooxml::read_emus_attribute(const pugi::xml_attribute attribute) { +ooxml::read_emus_attribute(pugi::xml_attribute attribute) { if (!attribute) { return {}; } @@ -70,14 +70,14 @@ ooxml::read_emus_attribute(const pugi::xml_attribute attribute) { } std::optional -ooxml::read_twips_attribute(const pugi::xml_attribute attribute) { +ooxml::read_twips_attribute(pugi::xml_attribute attribute) { if (!attribute) { return {}; } return Measure(attribute.as_float() / 1440.0f, DynamicUnit("in")); } -std::optional ooxml::read_width_attribute(const pugi::xml_node node) { +std::optional ooxml::read_width_attribute(pugi::xml_node node) { if (!node) { return {}; } @@ -97,7 +97,19 @@ std::optional ooxml::read_width_attribute(const pugi::xml_node node) { return {}; } -bool ooxml::read_line_attribute(const pugi::xml_attribute attribute) { +bool ooxml::read_line_attribute(pugi::xml_node node) { + if (!node) { + return false; + } + auto val = node.attribute("w:val").value(); + if (std::strcmp("none", val) == 0 || std::strcmp("false", val) == 0 || + std::strcmp("noStrike", val) == 0) { + return false; + } + return true; +} + +bool ooxml::read_line_attribute(pugi::xml_attribute attribute) { if (!attribute) { return false; } @@ -109,8 +121,15 @@ bool ooxml::read_line_attribute(const pugi::xml_attribute attribute) { return true; } +std::optional ooxml::read_shadow_attribute(pugi::xml_node node) { + if (!node) { + return {}; + } + return "1pt 1pt"; +} + std::optional -ooxml::read_shadow_attribute(const pugi::xml_attribute attribute) { +ooxml::read_shadow_attribute(pugi::xml_attribute attribute) { if (!attribute) { return {}; } @@ -118,19 +137,42 @@ ooxml::read_shadow_attribute(const pugi::xml_attribute attribute) { } std::optional -ooxml::read_font_weight_attribute(const pugi::xml_attribute attribute) { +ooxml::read_font_weight_attribute(pugi::xml_node node) { + if (!node) { + return {}; + } + auto val = node.attribute("w:val").value(); + if (std::strcmp("false", val) == 0 || std::strcmp("0", val) == 0) { + return FontWeight::normal; + } + return FontWeight::bold; +} + +std::optional +ooxml::read_font_weight_attribute(pugi::xml_attribute attribute) { if (!attribute) { return {}; } auto val = attribute.value(); - if ((std::strcmp("false", val) == 0) || (std::strcmp("0", val) == 0)) { + if (std::strcmp("false", val) == 0 || std::strcmp("0", val) == 0) { return FontWeight::normal; } return FontWeight::bold; } +std::optional ooxml::read_font_style_attribute(pugi::xml_node node) { + if (!node) { + return {}; + } + auto val = node.attribute("w:val").value(); + if (std::strcmp("false", val) == 0) { + return {}; + } + return FontStyle::italic; +} + std::optional -ooxml::read_font_style_attribute(const pugi::xml_attribute attribute) { +ooxml::read_font_style_attribute(pugi::xml_attribute attribute) { if (!attribute) { return {}; } @@ -142,7 +184,7 @@ ooxml::read_font_style_attribute(const pugi::xml_attribute attribute) { } std::optional -ooxml::read_text_align_attribute(const pugi::xml_attribute attribute) { +ooxml::read_text_align_attribute(pugi::xml_attribute attribute) { auto val = attribute.value(); if ((std::strcmp("left", val) == 0) || (std::strcmp("start", val) == 0)) { return TextAlign::left; @@ -160,7 +202,7 @@ ooxml::read_text_align_attribute(const pugi::xml_attribute attribute) { } std::optional -ooxml::read_vertical_align_attribute(const pugi::xml_attribute attribute) { +ooxml::read_vertical_align_attribute(pugi::xml_attribute attribute) { auto val = attribute.value(); if (std::strcmp("top", val) == 0) { return VerticalAlign::top; @@ -174,8 +216,7 @@ ooxml::read_vertical_align_attribute(const pugi::xml_attribute attribute) { return {}; } -std::optional -ooxml::read_border_attribute(const pugi::xml_node node) { +std::optional ooxml::read_border_node(pugi::xml_node node) { if (!node) { return {}; } @@ -203,8 +244,8 @@ std::unordered_map ooxml::parse_relationships(const pugi::xml_document &rels) { std::unordered_map result; for (auto &&e : rels.select_nodes("//Relationship")) { - const std::string r_id = e.node().attribute("Id").as_string(); - const std::string p = e.node().attribute("Target").as_string(); + std::string r_id = e.node().attribute("Id").as_string(); + std::string p = e.node().attribute("Target").as_string(); result.insert({r_id, p}); } return result; @@ -213,13 +254,12 @@ ooxml::parse_relationships(const pugi::xml_document &rels) { std::unordered_map ooxml::parse_relationships(const abstract::ReadableFilesystem &filesystem, const common::Path &path) { - const auto rel_path = - path.parent().join("_rels").join(path.basename() + ".rels"); + auto rel_path = path.parent().join("_rels").join(path.basename() + ".rels"); if (!filesystem.is_file(rel_path)) { return {}; } - const auto relationships = util::xml::parse(filesystem, rel_path); + auto relationships = util::xml::parse(filesystem, rel_path); return parse_relationships(relationships); } diff --git a/src/odr/internal/ooxml/ooxml_util.hpp b/src/odr/internal/ooxml/ooxml_util.hpp index d5746c95..265281e8 100644 --- a/src/odr/internal/ooxml/ooxml_util.hpp +++ b/src/odr/internal/ooxml/ooxml_util.hpp @@ -35,12 +35,16 @@ std::optional read_emus_attribute(pugi::xml_attribute); std::optional read_twips_attribute(pugi::xml_attribute); std::optional read_width_attribute(pugi::xml_node); bool read_line_attribute(pugi::xml_attribute); +bool read_line_attribute(pugi::xml_node); std::optional read_shadow_attribute(pugi::xml_attribute); +std::optional read_shadow_attribute(pugi::xml_node); std::optional read_font_weight_attribute(pugi::xml_attribute); +std::optional read_font_weight_attribute(pugi::xml_node); std::optional read_font_style_attribute(pugi::xml_attribute); +std::optional read_font_style_attribute(pugi::xml_node); std::optional read_text_align_attribute(pugi::xml_attribute); std::optional read_vertical_align_attribute(pugi::xml_attribute); -std::optional read_border_attribute(pugi::xml_node); +std::optional read_border_node(pugi::xml_node); std::string read_text_property(pugi::xml_node); diff --git a/src/odr/internal/ooxml/text/ooxml_text_style.cpp b/src/odr/internal/ooxml/text/ooxml_text_style.cpp index 25ed9659..a5b86bc1 100644 --- a/src/odr/internal/ooxml/text/ooxml_text_style.cpp +++ b/src/odr/internal/ooxml/text/ooxml_text_style.cpp @@ -17,24 +17,23 @@ void resolve_text_style_(pugi::xml_node node, TextStyle &result) { run_properties.child("w:sz").attribute("w:val"))) { result.font_size = font_size; } - if (auto font_weight = read_font_weight_attribute( - run_properties.child("w:b").attribute("w:val"))) { + if (auto font_weight = + read_font_weight_attribute(run_properties.child("w:b"))) { result.font_weight = font_weight; } - if (auto font_style = read_font_style_attribute( - run_properties.child("w:i").attribute("w:val"))) { + if (auto font_style = + read_font_style_attribute(run_properties.child("w:i"))) { result.font_style = font_style; } - if (auto font_underline = - read_line_attribute(run_properties.child("w:u").attribute("w:val"))) { + if (auto font_underline = read_line_attribute(run_properties.child("w:u"))) { result.font_underline = font_underline; } - if (auto font_line_through = read_line_attribute( - run_properties.child("w:strike").attribute("w:val"))) { + if (auto font_line_through = + read_line_attribute(run_properties.child("w:strike"))) { result.font_line_through = font_line_through; } - if (auto font_shadow = read_shadow_attribute( - run_properties.child("w:shadow").attribute("w:val"))) { + if (auto font_shadow = + read_shadow_attribute(run_properties.child("w:shadow"))) { result.font_shadow = font_shadow; } if (auto font_color = read_color_attribute( @@ -96,19 +95,19 @@ void resolve_table_cell_style_(pugi::xml_node node, TableCellStyle &result) { table_cell_properties.child("w:vAlign").attribute("w:val"))) { result.vertical_align = vertical_align; } - if (auto border_right = read_border_attribute( + if (auto border_right = read_border_node( table_cell_properties.child("w:tcBorders").child("w:right"))) { result.border.right = border_right; } - if (auto border_top = read_border_attribute( + if (auto border_top = read_border_node( table_cell_properties.child("w:tcBorders").child("w:top"))) { result.border.top = border_top; } - if (auto border_left = read_border_attribute( + if (auto border_left = read_border_node( table_cell_properties.child("w:tcBorders").child("w:left"))) { result.border.left = border_left; } - if (auto border_bottom = read_border_attribute( + if (auto border_bottom = read_border_node( table_cell_properties.child("w:tcBorders").child("w:bottom"))) { result.border.bottom = border_bottom; }