Skip to content

Instantly share code, notes, and snippets.

@zeffii
Created December 4, 2020 21:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zeffii/a64c0f57ba59d22647d51e1a7d19bcb8 to your computer and use it in GitHub Desktop.
Save zeffii/a64c0f57ba59d22647d51e1a7d19bcb8 to your computer and use it in GitHub Desktop.
C++ consolas.fnt parser
#include <SDL2/SDL.h>
// #include <SDL2/SDL_image.h>
#include <iostream>
#include <vector>
#include <string>
#include <fstream>
#include <stdio.h>
#include <regex>
struct Glyph
{
int id, x, y, width, height, xoffset, yoffset, xadvance, page, chnl;
};
std::vector<std::string> elements = {"id", "x", "y", "width", "height", "xoffset", "yoffset", "xadvance", "page", "chnl"};
std::vector<Glyph> glyphlist;
void print_glyph(Glyph g){
std::cout << "g.id: " << g.id << std::endl;
std::cout << "g.width: " << g.width << std::endl;
};
int get_line_data(std::string input, std::string pattern) {
int rval = 0;
const std::regex r(pattern);
std::smatch m;
if (std::regex_search(input, m, r)) { rval = std::stoi(m[1].str()); }
return rval;
};
void load_font_file(){
auto numregex = [](std::string item) { return item + R"(="(\d*)" ")"; };
std::ifstream file ("res/consolas.fnt");
std::string s;
// skip first few lines
for (int i = 0; i <= 6; i++) getline(file, s);
getline(file, s);
int numchars = get_line_data(s, numregex("count"));
for (int i=0; i < numchars; i++){
getline(file, s);
int gp[10] = {};
for (int j=0; j < 10; j++)
gp[j] = get_line_data(s, numregex(elements[j]));
Glyph glyph_char = {gp[0], gp[1], gp[2], gp[3], gp[4], gp[5], gp[6], gp[7], gp[8], gp[9]};
glyphlist.push_back(glyph_char);
}
print_glyph(glyphlist[4]);
file.close();
};
int main(int argc, char* args[])
{
load_font_file();
while(1){}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment