[Kanimaji] Add settings to stroke removal function

This commit is contained in:
TennesseeTrash 2025-06-16 20:33:22 +02:00
parent c15045562e
commit 0c0a8e346c
4 changed files with 65 additions and 19 deletions

View file

@ -13,9 +13,11 @@ namespace Kanimaji
std::string Animate(const std::string& source,
const AnimationSettings& settings = AnimationSettings::Default());
void RemoveStrokeNumbers(const std::string& source, const std::string& destination);
void RemoveStrokeNumbers(const std::string& source, const std::string& destination,
const RemovalSetttings& settings = RemovalSetttings::Default());
std::string RemoveStrokeNumbers(const std::string& source);
std::string RemoveStrokeNumbers(const std::string& source,
const RemovalSetttings& settings = RemovalSetttings::Default());
}
#endif // KANIMAJI_KANIMAJI_HPP

View file

@ -49,6 +49,13 @@ namespace Kanimaji
static AnimationSettings Default();
};
struct RemovalSetttings
{
StrokeStyle Style;
static RemovalSetttings Default();
};
}
#endif // KANIMAJI_CONFIG_HPP

View file

@ -12,42 +12,55 @@ namespace Kanimaji
{
namespace
{
using namespace std::string_view_literals;
constexpr auto StrokeProgressionFormat =
constexpr std::string_view StaticStrokeStyleFormat(
"fill:none;stroke:{};stroke-width:{};stroke-linecap:round;stroke-linejoin:round;"
);
constexpr std::string_view StrokeProgressionFormat(
" "
"@keyframes stroke-{} {{ "
"0.000% {{ stroke-dashoffset: {:.3f}; }} "
"{:.3f}% {{ stroke-dashoffset: {:.3f}; }} "
"{:.3f}% {{ stroke-dashoffset: 0; }} "
"100.000% {{ stroke-dashoffset: 0; }} }}\n"sv;
constexpr auto AnimationVisibilityFormat =
"100.000% {{ stroke-dashoffset: 0; }} }}\n"
);
constexpr std::string_view AnimationVisibilityFormat(
" "
"@keyframes display-{} {{ "
"{:.3f}% {{ visibility: hidden; }} "
"{:.3f}% {{ stroke: {}; }} }}\n"sv;
constexpr auto AnimationProgressionFormat =
"{:.3f}% {{ stroke: {}; }} }}\n"
);
constexpr std::string_view AnimationProgressionFormat(
" "
"#{} {{ "
"stroke-dasharray: {:.3f} {:.3f}; "
"stroke-dashoffset: 0; "
"animation: stroke-{} {:.3f}s {} infinite, "
"display-{} {:.3f}s step-start infinite; }}\n"sv;
constexpr auto BrushVisibilityFormat =
"display-{} {:.3f}s step-start infinite; }}\n"
);
constexpr std::string_view BrushVisibilityFormat(
" "
"@keyframes display-brush-{} {{ "
"{:.3f}% {{ visibility: hidden; }} "
"{:.3f}% {{ visibility: visible; }} "
"100.000% {{ visibility: hidden; }} }}\n"sv;
constexpr auto BrushProgressionFormat =
"100.000% {{ visibility: hidden; }} }}\n"
);
constexpr std::string_view BrushProgressionFormat(
" "
"#{}, #{} {{ "
"stroke-dasharray: 0 {:.3f}; "
"animation: stroke-{} {:.3f}s {} infinite, "
"display-brush-{} {:.3f}s step-start infinite; }}\n"sv;
"display-brush-{} {:.3f}s step-start infinite; }}\n"
);
constexpr auto StylesHeader =
constexpr std::string_view StylesHeader(
"\n "
"/* Styles generated automatically by Kanimaji, please avoid editing manually. */\n"sv;
"/* Styles generated automatically by Kanimaji, please avoid editing manually. */\n"
);
double AsSeconds(auto duration) {
return std::max(0.0, duration.count());
@ -65,8 +78,7 @@ namespace Kanimaji
pugi::xml_node newGroup = svg.append_child("g");
newGroup.append_attribute("id") = name;
newGroup.append_attribute("style") = std::format(
"fill:none;stroke:{};stroke-width:{};stroke-linecap:round;stroke-linejoin:round;",
config.Colour.ToHex(), config.Width
StaticStrokeStyleFormat, config.Colour.ToHex(), config.Width
);
return newGroup;
}
@ -270,7 +282,8 @@ namespace Kanimaji
return str.str();
}
void RemoveStrokeNumbers(const std::string& source, const std::string& destination)
void RemoveStrokeNumbers(const std::string& source, const std::string& destination,
const RemovalSetttings& settings)
{
pugi::xml_document doc;
pugi::xml_parse_result result = doc.load_file(source.c_str(), pugi::parse_full);
@ -284,6 +297,13 @@ namespace Kanimaji
throw Error("Unexpected document format: Expected to find a SVG element");
}
pugi::xml_node pathsGroup = svg.find_child([](pugi::xml_node& node) {
return std::string_view(node.attribute("id").as_string()).contains("StrokePaths");
});
pathsGroup.attribute("style") = std::format(
StaticStrokeStyleFormat, settings.Style.Colour.ToHex(), settings.Style.Width
);
RemoveStrokeNumbers(svg);
if (!doc.save_file(destination.c_str())) {
@ -291,7 +311,7 @@ namespace Kanimaji
}
}
std::string RemoveStrokeNumbers(const std::string& source)
std::string RemoveStrokeNumbers(const std::string& source, const RemovalSetttings& settings)
{
pugi::xml_document doc;
pugi::xml_parse_result result = doc.load_string(source.c_str(), pugi::parse_full);
@ -305,6 +325,13 @@ namespace Kanimaji
throw Error("Unexpected document format: Expected to find a SVG element");
}
pugi::xml_node pathsGroup = svg.find_child([](pugi::xml_node& node) {
return std::string_view(node.attribute("id").as_string()).contains("StrokePaths");
});
pathsGroup.attribute("style") = std::format(
StaticStrokeStyleFormat, settings.Style.Colour.ToHex(), settings.Style.Width
);
RemoveStrokeNumbers(svg);
std::stringstream str;

View file

@ -48,4 +48,14 @@ namespace Kanimaji
.DelayBetweenStrokes = 50ms,
};
}
RemovalSetttings RemovalSetttings::Default()
{
return RemovalSetttings {
.Style = StrokeStyle {
.Width = 3.0,
.Colour = RGB::FromHex("#000000"),
},
};
}
}