-
Notifications
You must be signed in to change notification settings - Fork 523
Expand file tree
/
Copy pathCommandConvert.cpp
More file actions
292 lines (261 loc) · 9.87 KB
/
CommandConvert.cpp
File metadata and controls
292 lines (261 loc) · 9.87 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
/////////////////////////////////////////////////////////////////////////////////////////////////
//
// Tencent is pleased to support the open source community by making libpag available.
//
// Copyright (C) 2026 Tencent. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file
// except in compliance with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// unless required by applicable law or agreed to in writing, software distributed under the
// license is distributed on an "as is" basis, without warranties or conditions of any kind,
// either express or implied. see the license for the specific language governing permissions
// and limitations under the license.
//
/////////////////////////////////////////////////////////////////////////////////////////////////
#include "cli/CommandConvert.h"
#include <fstream>
#include <iostream>
#include <string>
#include <vector>
#include "pagx/PAGXExporter.h"
#include "pagx/PAGXImporter.h"
#include "pagx/PDFExporter.h"
#include "pagx/SVGExporter.h"
#include "pagx/SVGImporter.h"
namespace pagx::cli {
struct SVGExportOptions {
int indent = 2;
bool noXmlDeclaration = false;
bool noConvertTextToPath = false;
};
struct SVGImportOptions {
bool expandUse = true;
bool flattenTransforms = false;
bool preserveUnknown = false;
};
struct PDFExportOpts {
bool noConvertTextToPath = false;
};
struct ConvertOptions {
std::string inputFile = {};
std::string outputFile = {};
std::string outputFormat = {};
SVGExportOptions svgExport = {};
SVGImportOptions svgImport = {};
PDFExportOpts pdfExport = {};
};
static void PrintUsage() {
std::cout << "Usage: pagx convert [options] <input> <output>\n"
<< "\n"
<< "Convert between PAGX and other formats. The conversion direction\n"
<< "is inferred from file extensions (e.g. .pagx -> .svg or .svg -> .pagx).\n"
<< "\n"
<< "Options:\n"
<< " --format <format> Override output format (svg, pdf, pagx)\n"
<< "\n"
<< "SVG output options:\n"
<< " --indent <n> Indentation spaces (default: 2, valid range: 0-16)\n"
<< " --no-xml-declaration Omit the <?xml ...?> declaration\n"
<< " --no-convert-text-to-path Keep text as <text>/<text operators> instead of paths\n"
<< "\n"
<< "PDF output options:\n"
<< " --no-convert-text-to-path Keep text as PDF text operators instead of paths\n"
<< "\n"
<< "SVG input options:\n"
<< " --no-expand-use Do not expand <use> references\n"
<< " --flatten-transforms Flatten nested transforms into single matrices\n"
<< " --preserve-unknown Preserve unsupported SVG elements as Unknown nodes\n"
<< "\n"
<< "Examples:\n"
<< " pagx convert input.pagx output.svg # PAGX to SVG\n"
<< " pagx convert input.pagx output.pdf # PAGX to PDF\n"
<< " pagx convert input.svg output.pagx # SVG to PAGX\n"
<< " pagx convert --indent 4 input.pagx out.svg # PAGX to SVG with 4-space indent\n"
<< " pagx convert --format pdf input.pagx output # specify PDF output format\n";
}
static std::string InferFormat(const std::string& path) {
auto dot = path.rfind('.');
if (dot != std::string::npos) {
auto ext = path.substr(dot + 1);
if (ext == "svg") {
return "svg";
}
if (ext == "pdf") {
return "pdf";
}
if (ext == "pagx") {
return "pagx";
}
}
return {};
}
static int ParseOptions(int argc, char* argv[], ConvertOptions* options) {
std::vector<std::string> positional = {};
int i = 1;
while (i < argc) {
std::string arg = argv[i];
if (arg == "--format" && i + 1 < argc) {
options->outputFormat = argv[++i];
} else if (arg == "--indent" && i + 1 < argc) {
char* endPtr = nullptr;
long value = strtol(argv[++i], &endPtr, 10);
if (endPtr == argv[i] || *endPtr != '\0' || value < 0 || value > 16) {
std::cerr << "pagx convert: error: invalid indent '" << argv[i] << "' (must be 0-16)\n";
return 1;
}
options->svgExport.indent = static_cast<int>(value);
} else if (arg == "--no-xml-declaration") {
options->svgExport.noXmlDeclaration = true;
} else if (arg == "--no-convert-text-to-path") {
options->svgExport.noConvertTextToPath = true;
options->pdfExport.noConvertTextToPath = true;
} else if (arg == "--no-expand-use") {
options->svgImport.expandUse = false;
} else if (arg == "--flatten-transforms") {
options->svgImport.flattenTransforms = true;
} else if (arg == "--preserve-unknown") {
options->svgImport.preserveUnknown = true;
} else if (arg == "--help" || arg == "-h") {
PrintUsage();
return -1;
} else if (arg[0] == '-') {
std::cerr << "pagx convert: error: unknown option '" << arg << "'\n";
return 1;
} else {
positional.push_back(arg);
}
i++;
}
if (positional.empty()) {
std::cerr << "pagx convert: error: missing input and output files\n";
return 1;
}
if (positional.size() < 2) {
std::cerr << "pagx convert: error: missing output file\n";
return 1;
}
if (positional.size() > 2) {
std::cerr << "pagx convert: error: too many positional arguments\n";
return 1;
}
options->inputFile = positional[0];
options->outputFile = positional[1];
if (options->outputFormat.empty()) {
options->outputFormat = InferFormat(options->outputFile);
}
if (options->outputFormat.empty()) {
std::cerr << "pagx convert: error: cannot infer output format from '" << options->outputFile
<< "', use --format to specify\n";
return 1;
}
return 0;
}
static int ConvertToSVG(const ConvertOptions& options) {
auto document = PAGXImporter::FromFile(options.inputFile);
if (document == nullptr) {
std::cerr << "pagx convert: error: failed to load '" << options.inputFile << "'\n";
return 1;
}
if (!document->errors.empty()) {
for (auto& error : document->errors) {
std::cerr << "pagx convert: warning: " << error << "\n";
}
}
SVGExporter::Options svgOptions = {};
svgOptions.indent = options.svgExport.indent;
svgOptions.xmlDeclaration = !options.svgExport.noXmlDeclaration;
svgOptions.convertTextToPath = !options.svgExport.noConvertTextToPath;
if (!SVGExporter::ToFile(*document, options.outputFile, svgOptions)) {
std::cerr << "pagx convert: error: failed to write '" << options.outputFile << "'\n";
return 1;
}
std::cout << "pagx convert: wrote " << options.outputFile << "\n";
return 0;
}
static int ConvertToPDF(const ConvertOptions& options) {
auto inputFormat = InferFormat(options.inputFile);
std::shared_ptr<PAGXDocument> document;
if (inputFormat == "pagx") {
document = PAGXImporter::FromFile(options.inputFile);
} else if (inputFormat == "svg") {
SVGImporter::Options svgOptions = {};
svgOptions.expandUseReferences = options.svgImport.expandUse;
svgOptions.flattenTransforms = options.svgImport.flattenTransforms;
svgOptions.preserveUnknownElements = options.svgImport.preserveUnknown;
document = SVGImporter::Parse(options.inputFile, svgOptions);
} else {
std::cerr << "pagx convert: error: unsupported input format for '" << options.inputFile
<< "'\n";
return 1;
}
if (document == nullptr) {
std::cerr << "pagx convert: error: failed to load '" << options.inputFile << "'\n";
return 1;
}
if (!document->errors.empty()) {
for (auto& error : document->errors) {
std::cerr << "pagx convert: warning: " << error << "\n";
}
}
PDFExporter::Options pdfOptions = {};
pdfOptions.convertTextToPath = !options.pdfExport.noConvertTextToPath;
if (!PDFExporter::ToFile(*document, options.outputFile, pdfOptions)) {
std::cerr << "pagx convert: error: failed to write '" << options.outputFile << "'\n";
return 1;
}
std::cout << "pagx convert: wrote " << options.outputFile << "\n";
return 0;
}
static int ConvertToPAGX(const ConvertOptions& options) {
auto inputFormat = InferFormat(options.inputFile);
if (inputFormat != "svg") {
std::cerr << "pagx convert: error: unsupported input format for '" << options.inputFile
<< "'\n";
return 1;
}
SVGImporter::Options svgOptions = {};
svgOptions.expandUseReferences = options.svgImport.expandUse;
svgOptions.flattenTransforms = options.svgImport.flattenTransforms;
svgOptions.preserveUnknownElements = options.svgImport.preserveUnknown;
auto document = SVGImporter::Parse(options.inputFile, svgOptions);
if (document == nullptr) {
std::cerr << "pagx convert: error: failed to parse '" << options.inputFile << "'\n";
return 1;
}
if (!document->errors.empty()) {
for (auto& error : document->errors) {
std::cerr << "pagx convert: warning: " << error << "\n";
}
}
auto xml = PAGXExporter::ToXML(*document);
std::ofstream out(options.outputFile);
if (!out.is_open()) {
std::cerr << "pagx convert: error: failed to write '" << options.outputFile << "'\n";
return 1;
}
out << xml;
std::cout << "pagx convert: wrote " << options.outputFile << "\n";
return 0;
}
int RunConvert(int argc, char* argv[]) {
ConvertOptions options = {};
auto parseResult = ParseOptions(argc, argv, &options);
if (parseResult != 0) {
return parseResult == -1 ? 0 : parseResult;
}
if (options.outputFormat == "svg") {
return ConvertToSVG(options);
}
if (options.outputFormat == "pdf") {
return ConvertToPDF(options);
}
if (options.outputFormat == "pagx") {
return ConvertToPAGX(options);
}
std::cerr << "pagx convert: error: unsupported output format '" << options.outputFormat << "'\n";
return 1;
}
} // namespace pagx::cli