tools svg2tvg: ++Windows port

Complete the Windows port build that is currently missing.

@Issue: https://github.com/thorvg/thorvg/issues/882
This commit is contained in:
Hermet Park 2023-04-19 13:55:15 +09:00
parent 46147dcec7
commit fc196b0965

View file

@ -23,145 +23,198 @@
#include <iostream> #include <iostream>
#include <vector> #include <vector>
#include <thorvg.h> #include <thorvg.h>
#include <dirent.h> #ifdef _WIN32
#include <windows.h>
#ifndef PATH_MAX
#define PATH_MAX MAX_PATH
#endif
#else
#include <dirent.h>
#include <unistd.h>
#include <limits.h>
#include <sys/stat.h>
#endif
using namespace std; using namespace std;
using namespace tvg; using namespace tvg;
void helpMsg() struct App
{ {
cout<<"Usage: \n svg2tvg [SVG file] or [SVG folder]\n\nExamples: \n $ svg2tvg input.svg\n $ svg2tvg svgfolder\n\n"; private:
} char full[PATH_MAX]; //full path
void helpMsg()
{
cout << "Usage: \n svg2tvg [SVG file] or [SVG folder]\n\nExamples: \n $ svg2tvg input.svg\n $ svg2tvg svgfolder\n\n";
}
bool convert(string& in, string& out) bool validate(string& svgName)
{ {
if (Initializer::init(CanvasEngine::Sw, 0) != Result::Success) return false; string extn = ".svg";
auto picture = Picture::gen(); if (svgName.size() <= extn.size() || svgName.substr(svgName.size() - extn.size()) != extn) {
if (picture->load(in) != Result::Success) return false; cout << "Error: \"" << svgName << "\" is invalid." << endl;
return false;
}
return true;
}
auto saver = Saver::gen(); bool convert(string& in, string& out)
if (saver->save(move(picture), out) != Result::Success) return false; {
if (saver->sync() != Result::Success) return false; if (Initializer::init(CanvasEngine::Sw, 0) != Result::Success) return false;
if (Initializer::term(CanvasEngine::Sw) != Result::Success) return false; auto picture = Picture::gen();
if (picture->load(in) != Result::Success) return false;
return true; auto saver = Saver::gen();
} if (saver->save(move(picture), out) != Result::Success) return false;
if (saver->sync() != Result::Success) return false;
if (Initializer::term(CanvasEngine::Sw) != Result::Success) return false;
void convert(string& svgName) return true;
{ }
//Get tvg file
auto tvgName = svgName;
tvgName.replace(tvgName.length() - 3, 3, "tvg");
if (convert(svgName, tvgName)) { void convert(string& svgName)
cout<<"Generated TVG file : "<< tvgName << endl; {
} else { //Get tvg file
cout<<"Failed Converting TVG file : "<< svgName << endl; auto tvgName = svgName;
} tvgName.replace(tvgName.length() - 3, 3, "tvg");
}
if (convert(svgName, tvgName)) {
cout << "Generated TVG file : " << tvgName << endl;
}
else {
cout << "Failed Converting TVG file : " << svgName << endl;
}
}
char* getpath(const char* input) const char* realPath(const char* path)
{ {
static char buf[PATH_MAX];
#ifdef _WIN32 #ifdef _WIN32
return _fullpath(buf, input, PATH_MAX); return _fullpath(full, path, PATH_MAX);
#else #else
return realpath(input, buf); return realpath(path, full);
#endif #endif
} }
bool isDirectory(const char* path)
bool validate(string& svgName) {
{
string extn = ".svg";
if (svgName.size() <= extn.size() || svgName.substr(svgName.size() - extn.size()) != extn) {
cout << "Error: \"" << svgName << "\" is invalid." << endl;
return false;
}
return true;
}
void directory(const string& path, DIR* dir)
{
//List directories
while (auto entry = readdir(dir)) {
if (*entry->d_name == '.' || *entry->d_name == '$') continue;
if (entry->d_type == DT_DIR) {
string subpath = string(path);
#ifdef _WIN32 #ifdef _WIN32
subpath += '\\'; DWORD attr = GetFileAttributes(path);
if (attr == INVALID_FILE_ATTRIBUTES) return false;
return attr & FILE_ATTRIBUTE_DIRECTORY;
#else #else
subpath += '/'; struct stat buf;
if (stat(path, &buf) != 0) return false;
return S_ISDIR(buf.st_mode);
#endif #endif
subpath += entry->d_name; }
//open directory bool handleDirectory(const string& path)
if (auto subdir = opendir(subpath.c_str())) { {
cout << "Sub Directory: \"" << subpath << "\"" << endl;
directory(subpath, subdir);
closedir(dir);
}
} else {
string svgName(entry->d_name);
if (!validate(svgName)) continue;
svgName = string(path);
#ifdef _WIN32 #ifdef _WIN32
svgName += '\\'; //open directory
#else WIN32_FIND_DATA fd;
svgName += '/'; HANDLE h = FindFirstFileEx((path + "\\*").c_str(), FindExInfoBasic, &fd, FindExSearchNameMatch, NULL, 0);
#endif if (h == INVALID_HANDLE_VALUE) {
svgName += entry->d_name; cout << "Couldn't open directory \"" << path.c_str() << "\"." << endl;
return false;
convert(svgName);
} }
} //List directories
} do {
if (*fd.cFileName == '.' || *fd.cFileName == '$') continue;
//sub directory
if (fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) {
string subpath = string(path);
subpath += '\\';
subpath += fd.cFileName;
if (!handleDirectory(subpath)) continue;
//file
} else {
string svgName(fd.cFileName);
if (!validate(svgName)) continue;
svgName = string(path);
svgName += '\\';
svgName += fd.cFileName;
convert(svgName);
}
} while (FindNextFile(h, &fd));
FindClose(h);
#else
//open directory
auto dir = opendir(path.c_str());
if (!dir) {
cout << "Couldn't open directory \"" << path.c_str() << "\"." << endl;
return false;
}
//List directories
while (auto entry = readdir(dir)) {
if (*entry->d_name == '.' || *entry->d_name == '$') continue;
//sub directory
if (entry->d_type == DT_DIR) {
string subpath = string(path);
subpath += '/';
subpath += entry->d_name;
if (!handleDirectory(subpath)) continue;
//file
} else {
string svgName(entry->d_name);
if (!validate(svgName)) continue;
svgName = string(path);
svgName += '/';
svgName += entry->d_name;
convert(svgName);
}
}
#endif
return true;
}
public:
int setup(int argc, char** argv)
{
//Collect input files
vector<const char*> inputs;
for (int i = 1; i < argc; ++i) {
inputs.push_back(argv[i]);
}
//No Input SVG
if (inputs.empty()) {
helpMsg();
return 0;
}
for (auto input : inputs) {
auto path = realPath(input);
if (!path) {
cout << "Invalid file or path name: \"" << input << "\"" << endl;
continue;
}
if (isDirectory(path)) {
//load from directory
cout << "Directory: \"" << path << "\"" << endl;
if (!handleDirectory(path)) break;
}
else {
string svgName(input);
if (!validate(svgName)) continue;
convert(svgName);
}
}
return 0;
}
};
int main(int argc, char **argv) int main(int argc, char **argv)
{ {
//Collect input files App app;
vector<const char*> inputs; return app.setup(argc, argv);
}
for (int i = 1; i < argc; ++i) {
inputs.push_back(argv[i]);
}
//No Input SVG
if (inputs.empty()) {
helpMsg();
return 0;
}
for (auto input : inputs) {
auto path = getpath(input);
if (!path) {
cout << "Invalid file or path name: \"" << input << "\"" << endl;
continue;
}
if (auto dir = opendir(path)) {
//load from directory
cout << "Directory: \"" << path << "\"" << endl;
directory(path, dir);
closedir(dir);
} else {
string svgName(input);
if (!validate(svgName)) continue;
convert(svgName);
}
}
return 0;
}