#include "font.h" #include #include #include "renderer.h" inline int charToIndex(char ch) { return static_cast(ch) - static_cast('!'); } Font::Font(std::string_view filename, Renderer& renderer) { std::ifstream fontfile(filename.data()); if (!fontfile.is_open()) { throw std::invalid_argument("Can't find font file: " + std::string(filename)); } std::string imagefilename; fontfile >> imagefilename; textureId_ = renderer.loadImageFromFile(imagefilename); std::string line; char ch; int numChars; fontfile >> numChars; std::getline(fontfile, line); // characters fontfile >> columns_; std::getline(fontfile, line); // characters per row fontfile >> tileSize_.x(); fontfile >> ch; // x fontfile >> tileSize_.y(); std::getline(fontfile, line); // tile size std::getline(fontfile, line); // ignore the baseline location std::getline(fontfile, line); // blank for (int i=0; i> ch; // the character int width; fontfile >> width; widths_.push_back(width); } } vec2i Font::getCharacterLocation(char ch) const { int index = charToIndex(ch); return (vec2i { index % columns_, index / columns_ }) * tileSize_; } vec2i Font::getCharacterSize(char ch) const { int index = charToIndex(ch); return { widths_.at(index), tileSize_.h() }; } int Font::getCharacterWidth(char ch) const { if (ch == ' ') { return 3; } return widths_.at(charToIndex(ch)); }