-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
75 lines (59 loc) · 1.77 KB
/
main.cpp
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
//
// main.cpp - TTF SVG Extractor
// Gooborg Studios (www.gooborg.com) © 2018-2020, BSD-3-Clause License.
// See LICENSE file for more information
//
// This program takes a given TTF font file and generates SVGs for every
// available glyph.
//
#include <filesystem>
#include <fstream>
#include <iostream>
#include <nlohmann/json.hpp>
#include "font_to_svg.hpp"
using json = nlohmann::json;
void genSvg(std::string output, std::string fontname, std::string charCode,
std::string name) {
// Obtain the outline of the given glyph
font2svg::glyph g(fontname.c_str(), charCode);
// Create the file if the glyph exists
if (!g.isempty()) {
std::filesystem::create_directories(output);
std::string fname = output + charCode + " - " + name + ".svg";
std::ofstream file(fname.c_str());
file << g.svg();
file.close();
}
// Cleanup
g.free();
}
void create_svgs(std::string output, std::string fontname) {
json blocks;
std::ifstream jsonfile("unicode.json");
jsonfile >> blocks;
int blockcount = blocks.size();
for (auto &[block, glyphs] : blocks.items()) {
std::string i = block.substr(0, block.find(": "));
std::string blockname = block.substr(block.find(": ") + 2);
std::cout << "Generating SVG for block: " << blockname << " ("
<< i << "/" << blockcount << ")..." << std::endl;
for (auto &[code, glyphname] : glyphs.items()) {
genSvg(
output + std::string("/" + i + "-" + blockname + "/"),
fontname, code, glyphname
);
}
}
}
int main(int argc, char *argv[]) {
if (argc < 2) {
std::cerr << "Usage: " << argv[0] << " font.ttf\n"
<< std::endl
<< "Output: ./Output/<fontname>/*.svg" << std::endl;
exit(1);
}
std::stringstream output;
output << "./Output/" << argv[1] << "/";
create_svgs(output.str(), argv[1]);
return 0;
}