253 lines
7.0 KiB
C++
253 lines
7.0 KiB
C++
#include "assets/image_format.h"
|
|
#include "document/document.h"
|
|
#include "foundation/parse.h"
|
|
#include "foundation/result.h"
|
|
#include "ui_core/layout_xml.h"
|
|
|
|
#include <cstdint>
|
|
#include <fstream>
|
|
#include <iostream>
|
|
#include <iterator>
|
|
#include <string>
|
|
#include <string_view>
|
|
#include <vector>
|
|
|
|
namespace {
|
|
|
|
struct DocumentArgs {
|
|
std::uint32_t width = 0;
|
|
std::uint32_t height = 0;
|
|
std::uint32_t layers = 1;
|
|
};
|
|
|
|
struct InspectImageArgs {
|
|
std::string path;
|
|
};
|
|
|
|
struct ParseLayoutArgs {
|
|
std::string path;
|
|
};
|
|
|
|
void print_error(std::string_view command, std::string_view message)
|
|
{
|
|
std::cout << "{\"ok\":false,\"command\":\"" << command
|
|
<< "\",\"error\":\"" << message << "\"}\n";
|
|
}
|
|
|
|
void print_help()
|
|
{
|
|
std::cout
|
|
<< "pano_cli commands:\n"
|
|
<< " create-document --width N --height N [--layers N]\n"
|
|
<< " inspect-image --path FILE\n"
|
|
<< " parse-layout --path FILE\n"
|
|
<< " --help\n";
|
|
}
|
|
|
|
pp::foundation::Status parse_document_args(int argc, char** argv, DocumentArgs& args)
|
|
{
|
|
for (int i = 2; i < argc; ++i) {
|
|
const std::string_view key(argv[i]);
|
|
if (key == "--width" || key == "--height" || key == "--layers") {
|
|
if (i + 1 >= argc) {
|
|
return pp::foundation::Status::invalid_argument("missing value for option");
|
|
}
|
|
|
|
const auto value = pp::foundation::parse_u32(argv[++i]);
|
|
if (!value) {
|
|
return value.status();
|
|
}
|
|
|
|
if (key == "--width") {
|
|
args.width = value.value();
|
|
} else if (key == "--height") {
|
|
args.height = value.value();
|
|
} else {
|
|
args.layers = value.value();
|
|
}
|
|
} else {
|
|
return pp::foundation::Status::invalid_argument("unknown option");
|
|
}
|
|
}
|
|
|
|
if (args.width == 0 || args.height == 0) {
|
|
return pp::foundation::Status::invalid_argument("width and height must be greater than zero");
|
|
}
|
|
|
|
if (args.layers == 0) {
|
|
return pp::foundation::Status::invalid_argument("layer count must be greater than zero");
|
|
}
|
|
|
|
return pp::foundation::Status::success();
|
|
}
|
|
|
|
int create_document(int argc, char** argv)
|
|
{
|
|
DocumentArgs args;
|
|
const auto status = parse_document_args(argc, argv, args);
|
|
if (!status.ok()) {
|
|
print_error("create-document", status.message);
|
|
return 2;
|
|
}
|
|
|
|
const auto document = pp::document::CanvasDocument::create(
|
|
pp::document::DocumentConfig {
|
|
.width = args.width,
|
|
.height = args.height,
|
|
.layer_count = args.layers,
|
|
});
|
|
if (!document) {
|
|
print_error("create-document", document.status().message);
|
|
return 2;
|
|
}
|
|
|
|
std::cout << "{\"ok\":true,\"command\":\"create-document\",\"document\":{"
|
|
<< "\"width\":" << document.value().width()
|
|
<< ",\"height\":" << document.value().height()
|
|
<< ",\"layers\":" << document.value().layers().size()
|
|
<< ",\"activeLayer\":" << document.value().active_layer_index()
|
|
<< ",\"frames\":" << document.value().frames().size()
|
|
<< ",\"activeFrame\":" << document.value().active_frame_index()
|
|
<< "}}\n";
|
|
return 0;
|
|
}
|
|
|
|
pp::foundation::Status parse_inspect_image_args(int argc, char** argv, InspectImageArgs& 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 inspect_image(int argc, char** argv)
|
|
{
|
|
InspectImageArgs args;
|
|
const auto status = parse_inspect_image_args(argc, argv, args);
|
|
if (!status.ok()) {
|
|
print_error("inspect-image", status.message);
|
|
return 2;
|
|
}
|
|
|
|
std::ifstream stream(args.path, std::ios::binary);
|
|
if (!stream) {
|
|
print_error("inspect-image", "image file could not be opened");
|
|
return 2;
|
|
}
|
|
|
|
const std::vector<char> chars {
|
|
std::istreambuf_iterator<char>(stream),
|
|
std::istreambuf_iterator<char>()
|
|
};
|
|
const auto* data = reinterpret_cast<const std::byte*>(chars.data());
|
|
const auto format = pp::assets::detect_image_format(std::span<const std::byte>(data, chars.size()));
|
|
if (!format) {
|
|
print_error("inspect-image", format.status().message);
|
|
return 2;
|
|
}
|
|
|
|
std::cout << "{\"ok\":true,\"command\":\"inspect-image\",\"format\":\""
|
|
<< pp::assets::image_format_name(format.value())
|
|
<< "\",\"bytes\":" << chars.size() << "}\n";
|
|
return 0;
|
|
}
|
|
|
|
pp::foundation::Status parse_layout_args(int argc, char** argv, ParseLayoutArgs& 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 parse_layout(int argc, char** argv)
|
|
{
|
|
ParseLayoutArgs args;
|
|
const auto status = parse_layout_args(argc, argv, args);
|
|
if (!status.ok()) {
|
|
print_error("parse-layout", status.message);
|
|
return 2;
|
|
}
|
|
|
|
std::ifstream stream(args.path, std::ios::binary);
|
|
if (!stream) {
|
|
print_error("parse-layout", "layout file could not be opened");
|
|
return 2;
|
|
}
|
|
|
|
const std::string xml {
|
|
std::istreambuf_iterator<char>(stream),
|
|
std::istreambuf_iterator<char>()
|
|
};
|
|
const auto summary = pp::ui::parse_layout_xml(xml);
|
|
if (!summary) {
|
|
print_error("parse-layout", summary.status().message);
|
|
return 2;
|
|
}
|
|
|
|
std::cout << "{\"ok\":true,\"command\":\"parse-layout\""
|
|
<< ",\"nodes\":" << summary.value().node_count
|
|
<< ",\"lengthAttributes\":" << summary.value().length_attribute_count
|
|
<< "}\n";
|
|
return 0;
|
|
}
|
|
|
|
}
|
|
|
|
int main(int argc, char** argv)
|
|
{
|
|
if (argc < 2) {
|
|
print_help();
|
|
return 1;
|
|
}
|
|
|
|
const std::string_view command(argv[1]);
|
|
if (command == "--help" || command == "-h") {
|
|
print_help();
|
|
return 0;
|
|
}
|
|
|
|
if (command == "create-document") {
|
|
return create_document(argc, argv);
|
|
}
|
|
|
|
if (command == "inspect-image") {
|
|
return inspect_image(argc, argv);
|
|
}
|
|
|
|
if (command == "parse-layout") {
|
|
return parse_layout(argc, argv);
|
|
}
|
|
|
|
print_error(command, "unknown command");
|
|
return 2;
|
|
}
|