Add separate functions for removing stroke numbers
This commit is contained in:
parent
9a0e4d8f22
commit
4943f35d91
2 changed files with 73 additions and 19 deletions
|
@ -3,18 +3,19 @@
|
|||
|
||||
#include "Settings.hpp"
|
||||
|
||||
#include <filesystem>
|
||||
#include <string>
|
||||
|
||||
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
|
||||
|
|
|
@ -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);
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue