Skip to content

Commit

Permalink
fix docx regression
Browse files Browse the repository at this point in the history
  • Loading branch information
andiwand committed Dec 23, 2023
1 parent 522efc1 commit 63284d9
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 36 deletions.
82 changes: 61 additions & 21 deletions src/odr/internal/ooxml/ooxml_util.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,15 @@
namespace odr::internal {

std::optional<std::string>
ooxml::read_string_attribute(const pugi::xml_attribute attribute) {
ooxml::read_string_attribute(pugi::xml_attribute attribute) {
if (!attribute) {
return {};
}
return attribute.value();
}

std::optional<Color>
ooxml::read_color_attribute(const pugi::xml_attribute attribute) {
ooxml::read_color_attribute(pugi::xml_attribute attribute) {
static const std::unordered_map<std::string, Color> color_map{
{"red", {255, 0, 0}},
{"green", {0, 255, 0}},
Expand All @@ -46,38 +46,38 @@ ooxml::read_color_attribute(const pugi::xml_attribute attribute) {
}

std::optional<Measure>
ooxml::read_half_point_attribute(const pugi::xml_attribute attribute) {
ooxml::read_half_point_attribute(pugi::xml_attribute attribute) {
if (!attribute) {
return {};
}
return Measure(attribute.as_float() * 0.5f, DynamicUnit("pt"));
}

std::optional<Measure>
ooxml::read_hundredth_point_attribute(const pugi::xml_attribute attribute) {
ooxml::read_hundredth_point_attribute(pugi::xml_attribute attribute) {
if (!attribute) {
return {};
}
return Measure(attribute.as_float() * 0.01f, DynamicUnit("pt"));
}

std::optional<Measure>
ooxml::read_emus_attribute(const pugi::xml_attribute attribute) {
ooxml::read_emus_attribute(pugi::xml_attribute attribute) {
if (!attribute) {
return {};
}
return Measure(attribute.as_float() / 914400.0f, DynamicUnit("in"));
}

std::optional<Measure>
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<Measure> ooxml::read_width_attribute(const pugi::xml_node node) {
std::optional<Measure> ooxml::read_width_attribute(pugi::xml_node node) {
if (!node) {
return {};
}
Expand All @@ -97,7 +97,19 @@ std::optional<Measure> 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;
}
Expand All @@ -109,28 +121,58 @@ bool ooxml::read_line_attribute(const pugi::xml_attribute attribute) {
return true;
}

std::optional<std::string> ooxml::read_shadow_attribute(pugi::xml_node node) {
if (!node) {
return {};
}
return "1pt 1pt";
}

std::optional<std::string>
ooxml::read_shadow_attribute(const pugi::xml_attribute attribute) {
ooxml::read_shadow_attribute(pugi::xml_attribute attribute) {
if (!attribute) {
return {};
}
return "1pt 1pt";
}

std::optional<FontWeight>
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<FontWeight>
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<FontStyle> 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<FontStyle>
ooxml::read_font_style_attribute(const pugi::xml_attribute attribute) {
ooxml::read_font_style_attribute(pugi::xml_attribute attribute) {
if (!attribute) {
return {};
}
Expand All @@ -142,7 +184,7 @@ ooxml::read_font_style_attribute(const pugi::xml_attribute attribute) {
}

std::optional<TextAlign>
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;
Expand All @@ -160,7 +202,7 @@ ooxml::read_text_align_attribute(const pugi::xml_attribute attribute) {
}

std::optional<VerticalAlign>
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;
Expand All @@ -174,8 +216,7 @@ ooxml::read_vertical_align_attribute(const pugi::xml_attribute attribute) {
return {};
}

std::optional<std::string>
ooxml::read_border_attribute(const pugi::xml_node node) {
std::optional<std::string> ooxml::read_border_node(pugi::xml_node node) {
if (!node) {
return {};
}
Expand Down Expand Up @@ -203,8 +244,8 @@ std::unordered_map<std::string, std::string>
ooxml::parse_relationships(const pugi::xml_document &rels) {
std::unordered_map<std::string, std::string> 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;
Expand All @@ -213,13 +254,12 @@ ooxml::parse_relationships(const pugi::xml_document &rels) {
std::unordered_map<std::string, std::string>
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);
}

Expand Down
6 changes: 5 additions & 1 deletion src/odr/internal/ooxml/ooxml_util.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,16 @@ std::optional<Measure> read_emus_attribute(pugi::xml_attribute);
std::optional<Measure> read_twips_attribute(pugi::xml_attribute);
std::optional<Measure> read_width_attribute(pugi::xml_node);
bool read_line_attribute(pugi::xml_attribute);
bool read_line_attribute(pugi::xml_node);
std::optional<std::string> read_shadow_attribute(pugi::xml_attribute);
std::optional<std::string> read_shadow_attribute(pugi::xml_node);
std::optional<FontWeight> read_font_weight_attribute(pugi::xml_attribute);
std::optional<FontWeight> read_font_weight_attribute(pugi::xml_node);
std::optional<FontStyle> read_font_style_attribute(pugi::xml_attribute);
std::optional<FontStyle> read_font_style_attribute(pugi::xml_node);
std::optional<TextAlign> read_text_align_attribute(pugi::xml_attribute);
std::optional<VerticalAlign> read_vertical_align_attribute(pugi::xml_attribute);
std::optional<std::string> read_border_attribute(pugi::xml_node);
std::optional<std::string> read_border_node(pugi::xml_node);

std::string read_text_property(pugi::xml_node);

Expand Down
27 changes: 13 additions & 14 deletions src/odr/internal/ooxml/text/ooxml_text_style.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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;
}
Expand Down

0 comments on commit 63284d9

Please sign in to comment.