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 <vector>
#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 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;
auto picture = Picture::gen();
@ -49,87 +74,108 @@ bool convert(string& in, string& out)
if (Initializer::term(CanvasEngine::Sw) != Result::Success) return false;
return true;
}
}
void convert(string& svgName)
{
void convert(string& svgName)
{
//Get tvg file
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;
cout << "Generated TVG file : " << tvgName << endl;
}
else {
cout << "Failed Converting TVG file : " << svgName << endl;
}
}
}
char* getpath(const char* input)
{
static char buf[PATH_MAX];
const char* realPath(const char* path)
{
#ifdef _WIN32
return _fullpath(buf, input, PATH_MAX);
return _fullpath(full, path, PATH_MAX);
#else
return realpath(input, buf);
return realpath(path, full);
#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)
{
string extn = ".svg";
if (svgName.size() <= extn.size() || svgName.substr(svgName.size() - extn.size()) != extn) {
cout << "Error: \"" << svgName << "\" is invalid." << endl;
bool handleDirectory(const string& path)
{
#ifdef _WIN32
//open directory
WIN32_FIND_DATA fd;
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 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));
void directory(const string& path, DIR* dir)
{
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);
#ifdef _WIN32
subpath += '\\';
#else
subpath += '/';
#endif
subpath += entry->d_name;
//open directory
if (auto subdir = opendir(subpath.c_str())) {
cout << "Sub Directory: \"" << subpath << "\"" << endl;
directory(subpath, subdir);
closedir(dir);
}
if (!handleDirectory(subpath)) continue;
//file
} else {
string svgName(entry->d_name);
if (!validate(svgName)) continue;
svgName = string(path);
#ifdef _WIN32
svgName += '\\';
#else
svgName += '/';
#endif
svgName += entry->d_name;
convert(svgName);
}
}
}
#endif
return true;
}
int main(int argc, char **argv)
{
public:
int setup(int argc, char** argv)
{
//Collect input files
vector<const char*> inputs;
@ -145,23 +191,30 @@ int main(int argc, char **argv)
for (auto input : inputs) {
auto path = getpath(input);
auto path = realPath(input);
if (!path) {
cout << "Invalid file or path name: \"" << input << "\"" << endl;
continue;
}
if (auto dir = opendir(path)) {
if (isDirectory(path)) {
//load from directory
cout << "Directory: \"" << path << "\"" << endl;
directory(path, dir);
closedir(dir);
} else {
if (!handleDirectory(path)) break;
}
else {
string svgName(input);
if (!validate(svgName)) continue;
convert(svgName);
}
}
return 0;
}
};
int main(int argc, char **argv)
{
App app;
return app.setup(argc, argv);
}