stc
Loading...
Searching...
No Matches
FntParser.hpp
Go to the documentation of this file.
1
10#pragma once
11
12#include "StdFix.hpp"
13
14#include <any>
15#include <filesystem>
16#include <fstream>
17#include <sstream>
18#include <vector>
19#include <map>
20
21namespace stc {
22
23namespace FntParser {
24
25[[deprecated("FntParser is no longer maintained, and should not be used.")]]
26typedef struct {
27 int id;
28 int x,
37
38 std::vector<float> uvCoordinates;
40
41[[deprecated("FntParser is no longer maintained, and should not be used.")]]
42typedef struct {
43 std::string faceName;
44 int size; // In what unit?
45 size_t scaleW, scaleH;
46
47 bool bold,
52
53 size_t padLeft, padTop, padRight, padBottom;
54 size_t spaceLeft, spaceTop;
55
56 int lineHeight, base;
57
58 // Loading the files is left as an exercise to the reader.
59 std::vector<std::string> pages;
60
61 std::map<int, FntCharInfo> characters;
62
63} FntInfo;
64
65[[deprecated("FntParser is no longer maintained, and should not be used.")]]
66inline std::vector<float> generateUVCoords(int atlasWidth, int atlasHeight, const FntCharInfo& chr) {
67 auto x = (float) chr.x;
68 auto y = (float) chr.y;
69 auto width = (float) chr.width;
70 auto height = (float) chr.height;
71
72 float reX = ((float) x) / atlasWidth;
73 float reY = ((float) y) / atlasHeight;
74 float newX = ((float) x + width) / atlasWidth;
75 float newY = ((float) y + height) / atlasHeight;
76 // reX += 1.0 / DIMENSIONS;
77 // reY += 1.0 / DIMENSIONS;
78 // newX -= 1.0 / DIMENSIONS;
79 // newY -= 1.0 / DIMENSIONS;
80
81 return {
82 reX, reY, // 0
83 reX, newY, // 1
84 newX, reY, // 3
85 newX, reY, // 3
86 reX, newY, // 1
87 newX, newY, // 2
88 };
89}
90
91[[deprecated("FntParser is no longer maintained, and should not be used.")]]
92inline std::map<std::string, std::any> parseLine(const std::string& line) {
93 std::map<std::string, std::any> keys;
94
95 size_t i = line.find(' ') + 1;
96 do {
97 // le sigh
98 if (line[i] == ' ') {
99 ++i;
100 continue;
101 }
102 auto eq = line.find('=', i),
103 sp = line.find(' ', i);
104 if (sp == std::string::npos) sp = line.size();
105
106
107 auto fragment = line.substr(i, sp - i);
108 auto k = fragment.substr(0, eq - i);
109 auto v = fragment.substr(eq - i + 1, sp - eq);
110
111 std::any value;
112 if (v.find('"') != std::string::npos) {
113 if (v == "\"\"") value = std::string("");
114 else value = v.substr(1, v.size() - 2);
115 } else if (v.find(',') != std::string::npos) {
116 std::vector<int> tmp;
117
118 std::string buff;
119 std::stringstream ss(v);
120 while (std::getline(ss, buff, ',')) {
121 tmp.push_back(std::stoi(buff));
122 }
123
124 value = tmp;
125 } else {
126 value = std::stoi(v);
127 }
128 keys[k] = value;
129 i = sp + 1;
130 } while (i < line.size());
131 return keys;
132}
133
134[[deprecated("FntParser is no longer maintained, and should not be used.")]]
135inline FntInfo loadAndParseFnt(const std::string& fileName) {
136 if (!std::filesystem::exists(fileName)) {
137 throw std::runtime_error("File doesn't exist");
138 }
139
140 std::filesystem::path p(fileName);
141 std::ifstream f{p};
142
143 if (!f.is_open()) {
144 throw std::runtime_error("File not found");
145 }
146
147 FntInfo info;
148
149 std::string buff;
150
151 while (stc::StdFix::getline(f, buff)) {
152 auto vars = parseLine(buff);
153
154 if (buff.starts_with("info")) {
155 info.faceName = std::any_cast<std::string>(vars.at("face"));
156 info.size = std::any_cast<int>(vars.at("size"));
157 info.bold = std::any_cast<int>(vars.at("bold"));
158 info.italic = std::any_cast<int>(vars.at("italic"));
159 info.unicode = std::any_cast<int>(vars.at("unicode"));
160 info.smooth = std::any_cast<int>(vars.at("smooth"));
161 info.antiAliasing = std::any_cast<int>(vars.at("aa"));
162 auto padding = std::any_cast<std::vector<int>>(vars.at("padding"));
163
164 info.padTop = padding.at(0);
165 info.padRight = padding.at(1);
166 info.padBottom = padding.at(2);
167 info.padLeft = padding.at(3);
168
169 auto spacing = std::any_cast<std::vector<int>>(vars.at("spacing"));
170 info.spaceLeft = spacing.at(0);
171 info.spaceTop = spacing.at(1);
172 } else if (buff.starts_with("common")) {
173 info.lineHeight = std::any_cast<int>(vars.at("lineHeight"));
174 info.base = std::any_cast<int>(vars.at("base"));
175 info.scaleW = std::any_cast<int>(vars.at("scaleW"));
176 info.scaleH = std::any_cast<int>(vars.at("scaleH"));
177
178 } else if (buff.starts_with("page")) {
179 info.pages.push_back(std::any_cast<std::string>(vars.at("file")));
180 } else if (buff.starts_with("chars")) {
181 // ignore
182 } else if (buff.starts_with("char")) {
183 FntCharInfo chr;
184 chr.id = std::any_cast<int>(vars.at("id"));
185 chr.x = std::any_cast<int>(vars.at("x"));
186 chr.y = std::any_cast<int>(vars.at("y"));
187 chr.width = std::any_cast<int>(vars.at("width"));
188 chr.height = std::any_cast<int>(vars.at("height"));
189 chr.xOffset = std::any_cast<int>(vars.at("xoffset"));
190 chr.yOffset = std::any_cast<int>(vars.at("yoffset"));
191 chr.xAdvance = std::any_cast<int>(vars.at("xadvance"));
192 chr.page = std::any_cast<int>(vars.at("page"));
193
195 (int) info.scaleW,
196 (int) info.scaleH,
197 chr
198 );
199
200 info.characters[chr.id] = chr;
201 }
202
203 }
204
205 return info;
206}
207
208}
209
210}
FntInfo loadAndParseFnt(const std::string &fileName)
Definition FntParser.hpp:135
std::map< std::string, std::any > parseLine(const std::string &line)
Definition FntParser.hpp:92
std::vector< float > generateUVCoords(int atlasWidth, int atlasHeight, const FntCharInfo &chr)
Definition FntParser.hpp:66
std::istream & getline(std::istream &is, std::string &str)
Definition StdFix.hpp:21
Definition CaptureStream.hpp:6
Definition FntParser.hpp:26
int x
Definition FntParser.hpp:28
int page
Definition FntParser.hpp:35
int id
Definition FntParser.hpp:27
std::vector< float > uvCoordinates
Definition FntParser.hpp:38
int width
Definition FntParser.hpp:30
int channel
Definition FntParser.hpp:36
int height
Definition FntParser.hpp:31
int y
Definition FntParser.hpp:29
int yOffset
Definition FntParser.hpp:33
int xAdvance
Definition FntParser.hpp:34
int xOffset
Definition FntParser.hpp:32
Definition FntParser.hpp:42
size_t padTop
Definition FntParser.hpp:53
size_t padLeft
Definition FntParser.hpp:53
size_t scaleH
Definition FntParser.hpp:45
bool antiAliasing
Definition FntParser.hpp:51
std::map< int, FntCharInfo > characters
Definition FntParser.hpp:61
std::vector< std::string > pages
Definition FntParser.hpp:59
int lineHeight
Definition FntParser.hpp:56
size_t spaceLeft
Definition FntParser.hpp:54
size_t padBottom
Definition FntParser.hpp:53
int base
Definition FntParser.hpp:56
bool smooth
Definition FntParser.hpp:50
int size
Definition FntParser.hpp:44
bool bold
Definition FntParser.hpp:47
size_t scaleW
Definition FntParser.hpp:45
std::string faceName
Definition FntParser.hpp:43
bool italic
Definition FntParser.hpp:48
bool unicode
Definition FntParser.hpp:49
size_t padRight
Definition FntParser.hpp:53
size_t spaceTop
Definition FntParser.hpp:54