More text changes

This commit is contained in:
Dynamitos
2025-01-19 16:07:38 +01:00
parent 2915db8898
commit 4e6eb02e74
23 changed files with 390 additions and 110 deletions
+55 -29
View File
@@ -6,35 +6,61 @@ using namespace Seele;
FontAsset::FontAsset() {}
FontAsset::FontAsset(std::string_view folderPath, std::string_view name) : Asset(folderPath, name) {}
FontAsset::~FontAsset() {}
void FontAsset::save(ArchiveBuffer& buffer) const { Serialization::save(buffer, glyphs); }
void FontAsset::load(ArchiveBuffer& buffer) { Serialization::load(buffer, glyphs); }
void FontAsset::Glyph::save(ArchiveBuffer& buffer) const {
Serialization::save(buffer, size);
Serialization::save(buffer, bearing);
Serialization::save(buffer, advance);
Serialization::save(buffer, textureData);
FontAsset::FontAsset(std::string_view folderPath, std::string_view name) : Asset(folderPath, name) {
FT_Error error = FT_Init_FreeType(&ft);
assert(!error);
}
void FontAsset::Glyph::load(ArchiveBuffer& buffer) {
Serialization::load(buffer, size);
Serialization::load(buffer, bearing);
Serialization::load(buffer, advance);
Serialization::load(buffer, textureData);
texture = buffer.getGraphics()->createTexture2D(TextureCreateInfo{
.sourceData =
{
.size = textureData.size(),
.data = textureData.data(),
},
.format = Gfx::SE_FORMAT_R8_UNORM,
.width = size.x,
.height = size.y,
.name = "FontGlyph",
});
FontAsset::~FontAsset() {
FT_Done_Face(face);
FT_Done_FreeType(ft);
}
void FontAsset::save(ArchiveBuffer& buffer) const { Serialization::save(buffer, ttfFile); }
void FontAsset::load(ArchiveBuffer& buffer) {
Serialization::load(buffer, ttfFile);
graphics = buffer.getGraphics();
loadFace();
}
void FontAsset::loadFace() {
FT_Error error = FT_New_Memory_Face(ft, ttfFile.data(), ttfFile.size(), 0, &face);
assert(!error);
}
void FontAsset::loadFontSize(uint32 fontSize) {
FT_Set_Pixel_Sizes(face, 0, fontSize);
fontSizes[fontSize].ascent = face->ascender;
fontSizes[fontSize].descent = face->descender;
fontSizes[fontSize].linegap = 0;
for (uint32 c = 0; c < 256; ++c) {
if (FT_Load_Char(face, c, FT_LOAD_RENDER | FT_LOAD_COLOR)) {
continue;
}
FontAsset::Glyph& glyph = fontSizes[fontSize].glyphs[c];
glyph.size = IVector2(face->glyph->bitmap.width, face->glyph->bitmap.rows);
glyph.bearing = IVector2(face->glyph->bitmap_left, face->glyph->bitmap_top);
glyph.advance = face->glyph->advance.x;
glyph.textureData.resize(glyph.size.x * glyph.size.y);
if (glyph.textureData.size() == 0) {
glyph.size.x = 1;
glyph.size.y = 1;
glyph.textureData.add(0);
// load a single transparent pixel, so that we dont have to handle empty textures later
} else {
std::memcpy(glyph.textureData.data(), face->glyph->bitmap.buffer, glyph.textureData.size());
}
glyph.texture = graphics->createTexture2D(TextureCreateInfo{
.sourceData =
{
.size = glyph.textureData.size(),
.data = glyph.textureData.data(),
},
.format = Gfx::SE_FORMAT_R8_UNORM,
.width = glyph.size.x,
.height = glyph.size.y,
.name = "FontGlyph",
});
}
}