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,20 +23,45 @@
#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)
{ {
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;
}
bool convert(string& in, string& out)
{
if (Initializer::init(CanvasEngine::Sw, 0) != Result::Success) return false; if (Initializer::init(CanvasEngine::Sw, 0) != Result::Success) return false;
auto picture = Picture::gen(); auto picture = Picture::gen();
@ -49,87 +74,108 @@ bool convert(string& in, string& out)
if (Initializer::term(CanvasEngine::Sw) != Result::Success) return false; if (Initializer::term(CanvasEngine::Sw) != Result::Success) return false;
return true; return true;
} }
void convert(string& svgName)
void convert(string& svgName) {
{
//Get tvg file //Get tvg file
auto tvgName = svgName; auto tvgName = svgName;
tvgName.replace(tvgName.length() - 3, 3, "tvg"); tvgName.replace(tvgName.length() - 3, 3, "tvg");
if (convert(svgName, tvgName)) { if (convert(svgName, tvgName)) {
cout<<"Generated TVG file : "<< tvgName << endl; cout << "Generated TVG file : " << tvgName << endl;
} else { }
cout<<"Failed Converting TVG file : "<< svgName << endl; else {
cout << "Failed Converting TVG file : " << svgName << endl;
}
} }
}
const char* realPath(const char* path)
char* getpath(const char* input) {
{
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)
{
#ifdef _WIN32
DWORD attr = GetFileAttributes(path);
if (attr == INVALID_FILE_ATTRIBUTES) return false;
return attr & FILE_ATTRIBUTE_DIRECTORY;
#else
struct stat buf;
if (stat(path, &buf) != 0) return false;
return S_ISDIR(buf.st_mode);
#endif
}
bool validate(string& svgName) bool handleDirectory(const string& path)
{ {
string extn = ".svg"; #ifdef _WIN32
//open directory
if (svgName.size() <= extn.size() || svgName.substr(svgName.size() - extn.size()) != extn) { WIN32_FIND_DATA fd;
cout << "Error: \"" << svgName << "\" is invalid." << endl; HANDLE h = FindFirstFileEx((path + "\\*").c_str(), FindExInfoBasic, &fd, FindExSearchNameMatch, NULL, 0);
if (h == INVALID_HANDLE_VALUE) {
cout << "Couldn't open directory \"" << path.c_str() << "\"." << endl;
return false; return false;
} }
return true; //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);
void directory(const string& path, DIR* dir) #else
{ //open directory
auto dir = opendir(path.c_str());
if (!dir) {
cout << "Couldn't open directory \"" << path.c_str() << "\"." << endl;
return false;
}
//List directories //List directories
while (auto entry = readdir(dir)) { while (auto entry = readdir(dir)) {
if (*entry->d_name == '.' || *entry->d_name == '$') continue; if (*entry->d_name == '.' || *entry->d_name == '$') continue;
//sub directory
if (entry->d_type == DT_DIR) { if (entry->d_type == DT_DIR) {
string subpath = string(path); string subpath = string(path);
#ifdef _WIN32
subpath += '\\';
#else
subpath += '/'; subpath += '/';
#endif
subpath += entry->d_name; subpath += entry->d_name;
if (!handleDirectory(subpath)) continue;
//open directory //file
if (auto subdir = opendir(subpath.c_str())) {
cout << "Sub Directory: \"" << subpath << "\"" << endl;
directory(subpath, subdir);
closedir(dir);
}
} else { } else {
string svgName(entry->d_name); string svgName(entry->d_name);
if (!validate(svgName)) continue; if (!validate(svgName)) continue;
svgName = string(path); svgName = string(path);
#ifdef _WIN32
svgName += '\\';
#else
svgName += '/'; svgName += '/';
#endif
svgName += entry->d_name; svgName += entry->d_name;
convert(svgName); convert(svgName);
} }
} }
} #endif
return true;
}
public:
int setup(int argc, char** argv)
int main(int argc, char **argv) {
{
//Collect input files //Collect input files
vector<const char*> inputs; vector<const char*> inputs;
@ -145,23 +191,30 @@ int main(int argc, char **argv)
for (auto input : inputs) { for (auto input : inputs) {
auto path = getpath(input); auto path = realPath(input);
if (!path) { if (!path) {
cout << "Invalid file or path name: \"" << input << "\"" << endl; cout << "Invalid file or path name: \"" << input << "\"" << endl;
continue; continue;
} }
if (auto dir = opendir(path)) { if (isDirectory(path)) {
//load from directory //load from directory
cout << "Directory: \"" << path << "\"" << endl; cout << "Directory: \"" << path << "\"" << endl;
directory(path, dir); if (!handleDirectory(path)) break;
closedir(dir); }
} else { else {
string svgName(input); string svgName(input);
if (!validate(svgName)) continue; if (!validate(svgName)) continue;
convert(svgName); convert(svgName);
} }
} }
return 0; return 0;
}
};
int main(int argc, char **argv)
{
App app;
return app.setup(argc, argv);
} }