diff --git a/Libraries/Kanimaji/Include/Kanimaji/Kanimaji.hpp b/Libraries/Kanimaji/Include/Kanimaji/Kanimaji.hpp index 79c6bd0..79d3d9b 100644 --- a/Libraries/Kanimaji/Include/Kanimaji/Kanimaji.hpp +++ b/Libraries/Kanimaji/Include/Kanimaji/Kanimaji.hpp @@ -3,18 +3,19 @@ #include "Settings.hpp" -#include #include namespace Kanimaji { - using Path = std::filesystem::path; - - void AnimateFile(const Path& source, const Path& destination, + void AnimateFile(const std::string& source, const std::string& destination, const AnimationSettings& settings = AnimationSettings::Default()); std::string Animate(const std::string& source, const AnimationSettings& settings = AnimationSettings::Default()); + + void RemoveStrokeNumbers(const std::string& source, const std::string& destination); + + std::string RemoveStrokeNumbers(const std::string& source); } #endif // KANIMAJI_KANIMAJI_HPP diff --git a/Libraries/Kanimaji/Source/Kanimaji.cpp b/Libraries/Kanimaji/Source/Kanimaji.cpp index 27cdadd..3bf8034 100644 --- a/Libraries/Kanimaji/Source/Kanimaji.cpp +++ b/Libraries/Kanimaji/Source/Kanimaji.cpp @@ -53,6 +53,13 @@ namespace Kanimaji return std::max(0.0, duration.count()); } + void RemoveStrokeNumbers(pugi::xml_node& svg) + { + svg.remove_child(svg.find_child([](pugi::xml_node& node) { + return std::string_view(node.attribute("id").as_string()).contains("StrokeNumbers"); + })); + } + pugi::xml_node AppendGroup(pugi::xml_node& svg, std::string_view name, StrokeStyle config) { pugi::xml_node newGroup = svg.append_child("g"); @@ -92,17 +99,8 @@ namespace Kanimaji } } - void Animate(pugi::xml_document& doc, const AnimationSettings& settings) + void Animate(pugi::xml_node& svg, const AnimationSettings& settings) { - pugi::xml_node svg = doc.child("svg"); - if (!svg) { - throw Error("Unexpected document format: Expected to find a SVG element"); - } - - svg.remove_child(svg.find_child([](pugi::xml_node& node) { - return std::string_view(node.attribute("id").as_string()).contains("StrokeNumbers"); - })); - pugi::xml_node pathsGroup = svg.find_child([](pugi::xml_node& node) { return std::string_view(node.attribute("id").as_string()).contains("StrokePaths"); }); @@ -225,20 +223,27 @@ namespace Kanimaji } } - void AnimateFile(const Path& source, const Path& destination, const AnimationSettings& settings) + void AnimateFile(const std::string& source, const std::string& destination, + const AnimationSettings& settings) { pugi::xml_document doc; pugi::xml_parse_result result = doc.load_file(source.c_str(), pugi::parse_full); if (!result) { constexpr std::string_view errorFormat = "Failed to load file from {}, due to: {}"; - throw FileError(std::format(errorFormat, source.string(), result.description())); + throw FileError(std::format(errorFormat, source, result.description())); + } + + pugi::xml_node svg = doc.child("svg"); + if (!svg) { + throw Error("Unexpected document format: Expected to find a SVG element"); } AmendComment(doc); - Animate(doc, settings); + RemoveStrokeNumbers(svg); + Animate(svg, settings); if (!doc.save_file(destination.c_str())) { - throw FileError(std::format("Failed to save file to {}", destination.string())); + throw FileError(std::format("Failed to save file to {}", destination)); } } @@ -251,8 +256,56 @@ namespace Kanimaji throw ParseError(result.offset, std::format(errorFormat, result.description())); } + pugi::xml_node svg = doc.child("svg"); + if (!svg) { + throw Error("Unexpected document format: Expected to find a SVG element"); + } + AmendComment(doc); - Animate(doc, settings); + RemoveStrokeNumbers(svg); + Animate(svg, settings); + + std::stringstream str; + doc.save(str); + return str.str(); + } + + void RemoveStrokeNumbers(const std::string& source, const std::string& destination) + { + pugi::xml_document doc; + pugi::xml_parse_result result = doc.load_file(source.c_str(), pugi::parse_full); + if (!result) { + constexpr std::string_view errorFormat = "Failed to load file from {}, due to: {}"; + throw FileError(std::format(errorFormat, source, result.description())); + } + + pugi::xml_node svg = doc.child("svg"); + if (!svg) { + throw Error("Unexpected document format: Expected to find a SVG element"); + } + + RemoveStrokeNumbers(svg); + + if (!doc.save_file(destination.c_str())) { + throw FileError(std::format("Failed to save file to {}", destination)); + } + } + + std::string RemoveStrokeNumbers(const std::string& source) + { + pugi::xml_document doc; + pugi::xml_parse_result result = doc.load_string(source.c_str(), pugi::parse_full); + if (!result) { + constexpr std::string_view errorFormat = "Failed to parse SVG document: {}"; + throw ParseError(result.offset, std::format(errorFormat, result.description())); + } + + pugi::xml_node svg = doc.child("svg"); + if (!svg) { + throw Error("Unexpected document format: Expected to find a SVG element"); + } + + RemoveStrokeNumbers(svg); std::stringstream str; doc.save(str);