Add separate functions for removing stroke numbers

This commit is contained in:
TennesseeTrash 2025-06-10 22:40:46 +02:00
parent 9a0e4d8f22
commit 4943f35d91
2 changed files with 73 additions and 19 deletions

View file

@ -3,18 +3,19 @@
#include "Settings.hpp" #include "Settings.hpp"
#include <filesystem>
#include <string> #include <string>
namespace Kanimaji namespace Kanimaji
{ {
using Path = std::filesystem::path; void AnimateFile(const std::string& source, const std::string& destination,
void AnimateFile(const Path& source, const Path& destination,
const AnimationSettings& settings = AnimationSettings::Default()); const AnimationSettings& settings = AnimationSettings::Default());
std::string Animate(const std::string& source, std::string Animate(const std::string& source,
const AnimationSettings& settings = AnimationSettings::Default()); 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 #endif // KANIMAJI_KANIMAJI_HPP

View file

@ -53,6 +53,13 @@ namespace Kanimaji
return std::max(0.0, duration.count()); 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 AppendGroup(pugi::xml_node& svg, std::string_view name, StrokeStyle config)
{ {
pugi::xml_node newGroup = svg.append_child("g"); 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) { pugi::xml_node pathsGroup = svg.find_child([](pugi::xml_node& node) {
return std::string_view(node.attribute("id").as_string()).contains("StrokePaths"); 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_document doc;
pugi::xml_parse_result result = doc.load_file(source.c_str(), pugi::parse_full); pugi::xml_parse_result result = doc.load_file(source.c_str(), pugi::parse_full);
if (!result) { if (!result) {
constexpr std::string_view errorFormat = "Failed to load file from {}, due to: {}"; 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); AmendComment(doc);
Animate(doc, settings); RemoveStrokeNumbers(svg);
Animate(svg, settings);
if (!doc.save_file(destination.c_str())) { 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())); 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); 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; std::stringstream str;
doc.save(str); doc.save(str);