Add paint stroke script automation
This commit is contained in:
@@ -5,6 +5,7 @@
|
||||
#include "foundation/parse.h"
|
||||
#include "foundation/result.h"
|
||||
#include "paint/stroke.h"
|
||||
#include "paint/stroke_script.h"
|
||||
#include "ui_core/layout_xml.h"
|
||||
|
||||
#include <cstdint>
|
||||
@@ -46,6 +47,10 @@ struct SimulateStrokeArgs {
|
||||
std::uint32_t spacing = 1;
|
||||
};
|
||||
|
||||
struct SimulateStrokeScriptArgs {
|
||||
std::string path;
|
||||
};
|
||||
|
||||
void print_error(std::string_view command, std::string_view message)
|
||||
{
|
||||
std::cout << "{\"ok\":false,\"command\":\"" << command
|
||||
@@ -61,6 +66,7 @@ void print_help()
|
||||
<< " inspect-project --path FILE\n"
|
||||
<< " parse-layout --path FILE\n"
|
||||
<< " simulate-stroke --x1 N --y1 N --x2 N --y2 N [--spacing N]\n"
|
||||
<< " simulate-stroke-script --path FILE\n"
|
||||
<< " --help\n";
|
||||
}
|
||||
|
||||
@@ -379,6 +385,78 @@ int simulate_stroke(int argc, char** argv)
|
||||
return 0;
|
||||
}
|
||||
|
||||
pp::foundation::Status parse_simulate_stroke_script_args(int argc, char** argv, SimulateStrokeScriptArgs& args)
|
||||
{
|
||||
for (int i = 2; i < argc; ++i) {
|
||||
const std::string_view key(argv[i]);
|
||||
if (key == "--path") {
|
||||
if (i + 1 >= argc) {
|
||||
return pp::foundation::Status::invalid_argument("missing value for option");
|
||||
}
|
||||
|
||||
args.path = argv[++i];
|
||||
} else {
|
||||
return pp::foundation::Status::invalid_argument("unknown option");
|
||||
}
|
||||
}
|
||||
|
||||
if (args.path.empty()) {
|
||||
return pp::foundation::Status::invalid_argument("path must not be empty");
|
||||
}
|
||||
|
||||
return pp::foundation::Status::success();
|
||||
}
|
||||
|
||||
int simulate_stroke_script(int argc, char** argv)
|
||||
{
|
||||
SimulateStrokeScriptArgs args;
|
||||
const auto status = parse_simulate_stroke_script_args(argc, argv, args);
|
||||
if (!status.ok()) {
|
||||
print_error("simulate-stroke-script", status.message);
|
||||
return 2;
|
||||
}
|
||||
|
||||
std::ifstream stream(args.path, std::ios::binary);
|
||||
if (!stream) {
|
||||
print_error("simulate-stroke-script", "stroke script file could not be opened");
|
||||
return 2;
|
||||
}
|
||||
|
||||
const std::string text {
|
||||
std::istreambuf_iterator<char>(stream),
|
||||
std::istreambuf_iterator<char>()
|
||||
};
|
||||
const auto script = pp::paint::parse_stroke_script(text);
|
||||
if (!script) {
|
||||
print_error("simulate-stroke-script", script.status().message);
|
||||
return 2;
|
||||
}
|
||||
|
||||
std::size_t total_samples = 0;
|
||||
float total_distance = 0.0F;
|
||||
for (const auto& stroke : script.value().strokes) {
|
||||
const pp::paint::StrokePoint points[] { stroke.start, stroke.end };
|
||||
const auto samples = pp::paint::sample_stroke(
|
||||
points,
|
||||
pp::paint::StrokeSamplingConfig {
|
||||
.spacing = stroke.spacing,
|
||||
});
|
||||
if (!samples) {
|
||||
print_error("simulate-stroke-script", samples.status().message);
|
||||
return 2;
|
||||
}
|
||||
|
||||
total_samples += samples.value().size();
|
||||
total_distance += samples.value().back().distance;
|
||||
}
|
||||
|
||||
std::cout << "{\"ok\":true,\"command\":\"simulate-stroke-script\""
|
||||
<< ",\"strokes\":" << script.value().strokes.size()
|
||||
<< ",\"samples\":" << total_samples
|
||||
<< ",\"distance\":" << total_distance << "}\n";
|
||||
return 0;
|
||||
}
|
||||
|
||||
pp::foundation::Status parse_layout_args(int argc, char** argv, ParseLayoutArgs& args)
|
||||
{
|
||||
for (int i = 2; i < argc; ++i) {
|
||||
@@ -464,6 +542,10 @@ int main(int argc, char** argv)
|
||||
return simulate_stroke(argc, argv);
|
||||
}
|
||||
|
||||
if (command == "simulate-stroke-script") {
|
||||
return simulate_stroke_script(argc, argv);
|
||||
}
|
||||
|
||||
if (command == "parse-layout") {
|
||||
return parse_layout(argc, argv);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user