Add PPI layer metadata save automation
This commit is contained in:
@@ -12,10 +12,12 @@
|
||||
#include "ui_core/layout_xml.h"
|
||||
|
||||
#include <array>
|
||||
#include <charconv>
|
||||
#include <cstdint>
|
||||
#include <fstream>
|
||||
#include <iostream>
|
||||
#include <iterator>
|
||||
#include <system_error>
|
||||
#include <span>
|
||||
#include <string>
|
||||
#include <string_view>
|
||||
@@ -36,6 +38,10 @@ struct SaveProjectArgs {
|
||||
std::uint32_t width = 0;
|
||||
std::uint32_t height = 0;
|
||||
std::string layer_name = "Ink";
|
||||
float layer_opacity = 1.0F;
|
||||
std::uint32_t blend_mode = 0;
|
||||
bool alpha_locked = false;
|
||||
bool visible = true;
|
||||
std::uint32_t layers = 1;
|
||||
std::uint32_t frames = 1;
|
||||
std::uint32_t frame_duration_ms = 100;
|
||||
@@ -174,6 +180,20 @@ std::string json_escape(std::string_view value)
|
||||
return escaped;
|
||||
}
|
||||
|
||||
pp::foundation::Result<float> parse_float_arg(std::string_view text)
|
||||
{
|
||||
float value = 0.0F;
|
||||
const auto* begin = text.data();
|
||||
const auto* end = begin + text.size();
|
||||
const auto [ptr, ec] = std::from_chars(begin, end, value);
|
||||
if (ec != std::errc {} || ptr != end) {
|
||||
return pp::foundation::Result<float>::failure(
|
||||
pp::foundation::Status::invalid_argument("invalid floating-point value"));
|
||||
}
|
||||
|
||||
return pp::foundation::Result<float>::success(value);
|
||||
}
|
||||
|
||||
void print_help()
|
||||
{
|
||||
std::cout
|
||||
@@ -186,7 +206,7 @@ void print_help()
|
||||
<< " load-project --path FILE\n"
|
||||
<< " parse-layout --path FILE\n"
|
||||
<< " record-render [--width N] [--height N]\n"
|
||||
<< " save-project --path FILE --width N --height N [--layer-name NAME] [--layers N] [--frames N] [--frame-duration-ms N] [--include-test-face-payload]\n"
|
||||
<< " save-project --path FILE --width N --height N [--layer-name NAME] [--layer-opacity N] [--blend-mode N] [--alpha-locked] [--hidden] [--layers N] [--frames N] [--frame-duration-ms N] [--include-test-face-payload]\n"
|
||||
<< " simulate-document-edits [--width N] [--height N]\n"
|
||||
<< " simulate-document-history [--width N] [--height N] [--history N]\n"
|
||||
<< " simulate-image-import [--width N] [--height N]\n"
|
||||
@@ -306,8 +326,22 @@ pp::foundation::Status parse_save_project_args(int argc, char** argv, SaveProjec
|
||||
} else {
|
||||
args.layer_name = argv[++i];
|
||||
}
|
||||
} else if (key == "--layer-opacity") {
|
||||
if (i + 1 >= argc) {
|
||||
return pp::foundation::Status::invalid_argument("missing value for option");
|
||||
}
|
||||
|
||||
const auto value = parse_float_arg(argv[++i]);
|
||||
if (!value) {
|
||||
return value.status();
|
||||
}
|
||||
args.layer_opacity = value.value();
|
||||
} else if (key == "--alpha-locked") {
|
||||
args.alpha_locked = true;
|
||||
} else if (key == "--hidden") {
|
||||
args.visible = false;
|
||||
} else if (key == "--width" || key == "--height" || key == "--layers" || key == "--frames"
|
||||
|| key == "--frame-duration-ms") {
|
||||
|| key == "--blend-mode" || key == "--frame-duration-ms") {
|
||||
if (i + 1 >= argc) {
|
||||
return pp::foundation::Status::invalid_argument("missing value for option");
|
||||
}
|
||||
@@ -325,6 +359,8 @@ pp::foundation::Status parse_save_project_args(int argc, char** argv, SaveProjec
|
||||
args.layers = value.value();
|
||||
} else if (key == "--frames") {
|
||||
args.frames = value.value();
|
||||
} else if (key == "--blend-mode") {
|
||||
args.blend_mode = value.value();
|
||||
} else {
|
||||
args.frame_duration_ms = value.value();
|
||||
}
|
||||
@@ -347,6 +383,14 @@ pp::foundation::Status parse_save_project_args(int argc, char** argv, SaveProjec
|
||||
return pp::foundation::Status::invalid_argument("layer name must not be empty");
|
||||
}
|
||||
|
||||
if (args.layer_opacity < 0.0F || args.layer_opacity > 1.0F) {
|
||||
return pp::foundation::Status::out_of_range("layer opacity must be in the range [0, 1]");
|
||||
}
|
||||
|
||||
if (args.blend_mode > 4U) {
|
||||
return pp::foundation::Status::out_of_range("blend mode must be in the range [0, 4]");
|
||||
}
|
||||
|
||||
if (args.layers == 0) {
|
||||
return pp::foundation::Status::invalid_argument("layer count must be greater than zero");
|
||||
}
|
||||
@@ -386,6 +430,12 @@ int save_project(int argc, char** argv)
|
||||
.width = args.width,
|
||||
.height = args.height,
|
||||
.layer_name = args.layer_name,
|
||||
.layer_metadata = pp::assets::PpiLayerMetadataConfig {
|
||||
.opacity = args.layer_opacity,
|
||||
.blend_mode = args.blend_mode,
|
||||
.alpha_locked = args.alpha_locked,
|
||||
.visible = args.visible,
|
||||
},
|
||||
.layer_count = args.layers,
|
||||
.frame_count = args.frames,
|
||||
.frame_duration_ms = args.frame_duration_ms,
|
||||
@@ -420,6 +470,10 @@ int save_project(int argc, char** argv)
|
||||
<< ",\"layers\":" << args.layers
|
||||
<< ",\"frames\":" << args.frames
|
||||
<< ",\"layerName\":\"" << json_escape(args.layer_name) << "\""
|
||||
<< ",\"layerOpacity\":" << args.layer_opacity
|
||||
<< ",\"blendMode\":" << args.blend_mode
|
||||
<< ",\"alphaLocked\":" << (args.alpha_locked ? "true" : "false")
|
||||
<< ",\"visible\":" << (args.visible ? "true" : "false")
|
||||
<< ",\"frameDurationMs\":" << args.frame_duration_ms
|
||||
<< ",\"facePayloads\":" << (args.include_test_face_payload ? 1 : 0)
|
||||
<< "}}\n";
|
||||
@@ -907,6 +961,34 @@ int load_project(int argc, char** argv)
|
||||
const auto duration = document.layer_animation_duration_ms(layer_index);
|
||||
std::cout << (duration ? duration.value() : 0U);
|
||||
}
|
||||
std::cout << "],\"layerOpacities\":[";
|
||||
for (std::size_t layer_index = 0; layer_index < document.layers().size(); ++layer_index) {
|
||||
if (layer_index != 0U) {
|
||||
std::cout << ",";
|
||||
}
|
||||
std::cout << document.layers()[layer_index].opacity;
|
||||
}
|
||||
std::cout << "],\"layerBlendModes\":[";
|
||||
for (std::size_t layer_index = 0; layer_index < document.layers().size(); ++layer_index) {
|
||||
if (layer_index != 0U) {
|
||||
std::cout << ",";
|
||||
}
|
||||
std::cout << "\"" << pp::paint::blend_mode_name(document.layers()[layer_index].blend_mode) << "\"";
|
||||
}
|
||||
std::cout << "],\"layerAlphaLocked\":[";
|
||||
for (std::size_t layer_index = 0; layer_index < document.layers().size(); ++layer_index) {
|
||||
if (layer_index != 0U) {
|
||||
std::cout << ",";
|
||||
}
|
||||
std::cout << (document.layers()[layer_index].alpha_locked ? "true" : "false");
|
||||
}
|
||||
std::cout << "],\"layerVisible\":[";
|
||||
for (std::size_t layer_index = 0; layer_index < document.layers().size(); ++layer_index) {
|
||||
if (layer_index != 0U) {
|
||||
std::cout << ",";
|
||||
}
|
||||
std::cout << (document.layers()[layer_index].visible ? "true" : "false");
|
||||
}
|
||||
std::cout << "]}}\n";
|
||||
return 0;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user