Files
vb01/fontReader.cpp
2026-07-11 11:02:57 +03:00

74 lines
1.6 KiB
C++

#include "fontReader.h"
#include "fontAsset.h"
#include "root.h"
#include "stb_image.h"
#include <iostream>
#include <glad.h>
#include <glfw3.h>
using namespace std;
namespace vb01{
FontReader *fontReader = nullptr;
FontReader* FontReader::getSingleton(){
if(!fontReader)
fontReader = new FontReader();
return fontReader;
}
FontReader::FontReader() : AbstractAssetReader(){
if(FT_Init_FreeType(&ft))
cout << "Couldn't init Freetype\n";
}
Asset* FontReader::readAsset(string path){
if(FT_New_Face(ft, path.c_str(), 0, &face))
cout << "Could not load font\n";
FT_Set_Pixel_Sizes(face, pixelWidth, pixelHeight);
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
vector<FontAsset::Glyph> glyphs;
Root *root = Root::getSingleton();
for(u16 i = firstChar; i < lastChar; i++){
if(FT_Load_Char(face, i, FT_LOAD_RENDER)){
cout << "Could not load glyph\n";
continue;
}
const int numPixels = face->glyph->bitmap.width * face->glyph->bitmap.rows;
u8 *imageData = new u8[numPixels];
for(int j = 0; j < numPixels; j++)
imageData[j] = face->glyph->bitmap.buffer[j];
FontAsset::Glyph glyph = FontAsset::Glyph(
i,
imageData,
Vector2(face->glyph->bitmap.width, face->glyph->bitmap.rows),
Vector2(face->glyph->bitmap_left, face->glyph->bitmap_top),
face->glyph->advance.x,
-1,
-1
);
root->initTextureDataOnGpu(glyph.data, pixelHeight, pixelHeight, glyph.bufferId, glyph.layerId, false, true, glyph.size.x, glyph.size.y);
glyphs.push_back(glyph);
}
FontAsset *font = new FontAsset;
font->path = path;
font->face = face;
font->glyphs = glyphs;
return font;
}
}