blob: cfaad24967aeca045dda4f6412d749f6ed84e361 (
plain) (
blame)
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
|
#include "font.h"
#include <fstream>
#include <iostream>
#include "renderer.h"
inline int charToIndex(char ch) {
return static_cast<int>(ch) - static_cast<int>('!');
}
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<numChars; i++) {
fontfile >> 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));
}
|