-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFont.cpp
More file actions
174 lines (132 loc) · 4.94 KB
/
Font.cpp
File metadata and controls
174 lines (132 loc) · 4.94 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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
//
// Copyright (c) 2020-present Caps Collective & contributors
// Originally authored by Jonathan Moallem (@jonjondev) & Aryeh Zinn (@Raelr)
//
// This code is released under an unmodified zlib license.
// For conditions of distribution and use, please see:
// https://opensource.org/licenses/Zlib
//
#include "Font.h"
#include <freetype/freetype.h>
#include <resources/PackFile.h>
#include <resources/PackFileData.h>
#include <resources/ResourceSystem.h>
#include <utils/Defer.h>
#include <utils/Logging.h>
#include "render/renderer/statics/Statics.h"
#include "utils/Descriptor.h"
#include "utils/TypeAdaptor.h"
#define CAST_F(val) static_cast<float>(val)
namespace Siege::Vulkan
{
Font::Font(const char* filePath)
{
PackFile* packFile = ResourceSystem::GetInstance().GetPackFile();
std::shared_ptr<PackFileData> fileData = packFile->FindData(filePath);
FT_Open_Args args;
args.flags = FT_OPEN_MEMORY;
args.memory_base = reinterpret_cast<const FT_Byte*>(fileData->data);
args.memory_size = FT_Long(fileData->dataSize);
FT_Face fontFace;
CC_ASSERT(!FT_Open_Face(Statics::GetFontLib(), &args, 0, &fontFace), "Failed to load font!")
FT_Set_Pixel_Sizes(fontFace, 0, 64);
Utils::Offset2D textPadding = {1, 2};
uint8_t* buffer = new uint8_t[MAX_ATLAS_WIDTH * MAX_ATLAS_HEIGHT];
defer([buffer]() { delete[] buffer; });
memset(buffer, 0, sizeof(uint8_t) * MAX_ATLAS_WIDTH * MAX_ATLAS_HEIGHT);
extent = PopulateGlyphs(fontFace, textPadding, buffer);
texture = Texture2D(filePath,
buffer,
sizeof(uint8_t) * extent.width * extent.height,
extent.width,
extent.height,
Utils::TEXTURE_FILTER_LINEAR,
Texture2D::Usage::TEX_USAGE_FONT);
PopulateTextureAtlas(buffer);
id = INTERN_STR(filePath);
info.textureInfo = texture.GetInfo();
}
Font::~Font()
{
Free();
}
void Font::Free()
{
texture.Free();
info = {{}};
}
void Font::Swap(Font& other)
{
auto tmpId = id;
auto tmpAtlasExtent = extent;
auto tmpGlyphs = glyphs;
auto tmpInfo = info;
auto tmpTexture = std::move(texture);
id = other.id;
extent = other.extent;
glyphs = other.glyphs;
info = other.info;
texture = std::move(other.texture);
other.id = tmpId;
other.extent = tmpAtlasExtent;
other.glyphs = tmpGlyphs;
other.info = tmpInfo;
other.texture = std::move(tmpTexture);
}
Utils::Extent2DF Font::PopulateGlyphs(FontFace fontFace, Utils::Offset2D padding, uint8_t* buffer)
{
float x {0.f}, y {0.f}, rowHeight {0.f}, maxX {0.f}, maxY {0.f};
uint32_t offset {0};
for (unsigned char c = 32; c < 126; c++)
{
auto message = String("Unable to load char: ") + String(c);
CC_ASSERT(!FT_Load_Char(fontFace, c, FT_LOAD_RENDER), message.Str())
auto glyph = fontFace->glyph;
float width {CAST_F(glyph->bitmap.width)}, height {CAST_F(glyph->bitmap.rows)};
bool exceededWidth = (width + x) >= MAX_ATLAS_WIDTH;
float atlasX = (x * !exceededWidth);
float atlasY = (y + (exceededWidth * rowHeight));
glyphs[c] = {atlasX,
atlasY,
width,
height,
width,
height,
CAST_F(glyph->bitmap_left),
CAST_F(glyph->bitmap_top),
glyph->advance.x,
offset};
rowHeight =
((height > rowHeight) * height + (height <= rowHeight) * rowHeight) + padding.height;
x = ((!exceededWidth * (width + x)) + (exceededWidth * width)) + glyph->bitmap_left +
padding.width;
y += (exceededWidth * (rowHeight)) + glyph->bitmap_top + padding.height;
maxX = ((x > maxX) * x + (x <= maxX) * maxX);
maxY = ((y > maxY) * (y + rowHeight) + (y <= maxY) * maxY);
auto size = sizeof(uint8_t) * width * height;
x += (glyph->advance.x >> 6) + padding.width;
memcpy(buffer + offset, glyph->bitmap.buffer, size);
offset += size;
}
return {maxX + padding.width, maxY + padding.height};
}
void Font::PopulateTextureAtlas(uint8_t* buffer)
{
for (unsigned char c = 32; c < 126; c++)
{
auto& glyph = glyphs[c];
float xPos {glyph.uvxMin}, yPos {glyph.uvyMin};
glyph.uvxMin /= extent.width;
glyph.uvyMin /= extent.height;
glyph.widthNormalised /= extent.width;
glyph.heightNormalised /= extent.height;
Utils::Extent3D imageExtent {(uint32_t) (glyph.width), (uint32_t) (glyph.height), 1};
size_t size = sizeof(uint8_t) * (glyph.width * glyph.height);
if (size == 0) continue;
texture.CopyToRegion(buffer + glyph.bufferOffset,
size,
imageExtent,
{int32_t(xPos), int32_t(yPos)});
}
}
} // namespace Siege::Vulkan