-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfonts.cpp
More file actions
66 lines (59 loc) · 1.51 KB
/
Copy pathfonts.cpp
File metadata and controls
66 lines (59 loc) · 1.51 KB
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
#include "fonts.h"
// Collection of c-header fonts
// https://github.com/dhepper/font8x8
#include "font8x8_basic.h"
namespace fonts
{
inline bool isValidLetter(char letter)
{
return (letter > 0) &&
(letter < 128); // Null character always rendered empty
}
std::uint8_t getLetterScanLine(char letter, int line)
{
if (isValidLetter(letter) && (line >= 0) && (line < lettersHeight))
{
return font8x8_basic[letter][line];
}
return 0;
}
void renderLetter(char letter, std::function<void(int x, int y)> pixelsCallback)
{
// Iterate each line of the letter
for (int y = 0; y < lettersHeight; ++y)
{
char line = getLetterScanLine(letter, y);
// And iterate on each pixel of the line
for (int x = 0; x < lettersWidth; ++x)
{
if (line & 1)
{
pixelsCallback(x, y);
}
line >>= 1;
}
}
}
void renderText(const std::string& text,
std::function<void(float x, float y)> positionsCallback)
{
const float pixelSize = 1.f / fonts::lettersWidth;
float cursorX = 0, cursorY = 0;
for (auto& c : text)
{
if (c == '\n')
{
cursorX = 0;
++cursorY;
}
else
{
fonts::renderLetter(c, [&](int x, int y) {
positionsCallback(cursorX + x * pixelSize,
cursorY + y * pixelSize);
});
++cursorX;
}
}
}
} // namespace fonts