25 #include <FontManager.h> 33 #include <glutil/glutil.h> 35 #include FT_FREETYPE_H 45 if(!force && !initiated)
46 throw logic_error(
"Bad state: FontManager hasn't loaded any fonts yet.");
52 bool FontManager::FontDefinition::operator== (FontDefinition fd)
const {
53 return fd.face == face && fd.pixelSize == pixelSize;
56 std::string FontManager::FontDefinition::getFace()
const {
60 int FontManager::FontDefinition::getPixelSize()
const {
72 FontManager::FontDefinition::FontDefinition(std::string face,
int pixelSize) : face(face), pixelSize(pixelSize){ }
75 FontDefinition locate = FontDefinition(fontFace,pixelSize);
76 auto it = loadedFonts.find(locate);
77 if(it == loadedFonts.end()) {
78 loadFont(fontFace, pixelSize);
79 return loadedFonts.find(locate)->second;
86 FontDefinition newFont = FontDefinition(fontFace,pixelSize);
87 loadedFonts.insert(std::pair<FontDefinition,Font*>(newFont,
new Font(pixelSize)));
89 unsigned int *width = (
unsigned int*)calloc(
sizeof(
unsigned int),1);
90 unsigned int *height = (
unsigned int*)calloc(
sizeof(
unsigned int),1);
92 *height = atlasHeight;
94 iterateGlyphs(newFont,width,height);
97 atlasHeight = *height;
103 Globals::set(Globals::FONT_TEXTURE_WIDTH,*width);
104 Globals::set(Globals::FONT_TEXTURE_HEIGHT,*height);
108 void FontManager::initTexture() {
109 GLuint* tex = getTex(
true);
112 glDeleteTextures(1,tex);
116 glActiveTexture(GL_TEXTURE4);
117 glGenTextures(1, tex);
118 glBindTexture(GL_TEXTURE_2D, *tex);
119 glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
121 glTexStorage2D(GL_TEXTURE_2D,1,GL_R8,atlasWidth,atlasHeight);
124 void FontManager::iterateGlyphs(FontDefinition def,
unsigned int* width,
unsigned int* height) {
126 FT_Face face = (FT_Face)malloc(
sizeof(FT_Face));
128 if(
int error = FT_New_Face(*ft_library,def.face.c_str(),0,&face)){
129 Logger::logError(
"Failed loading face '" + def.face +
"'. Error code: " + std::to_string(error));
132 FT_Set_Pixel_Sizes(face,0,def.pixelSize);
133 FT_GlyphSlot glyph = face->glyph;
135 for(
unsigned char c = 32; c < 128; c++){
137 if(
int error = FT_Load_Char(face,c,FT_LOAD_RENDER)){
138 Logger::logError(
"Failed loading char '" + std::to_string(c) +
"'. Error code: " + std::to_string(error));
142 *height = max(*height,glyph->bitmap.rows);
143 *width += glyph->bitmap.width;
148 void FontManager::drawGlyphs() {
152 for(
auto fontIt : loadedFonts){
154 Font* font = fontIt.second;
155 FontDefinition fDef = fontIt.first;
156 if(
int error = FT_New_Face(*ft_library,fDef.face.c_str(),0,&face)){
157 Logger::logError(
"Failed loading face '" + fDef.face
158 +
"'. Error code: " + std::to_string(error));
161 FT_Set_Pixel_Sizes(face,0,fDef.pixelSize);
162 FT_GlyphSlot glyph = face->glyph;
164 for(
unsigned char c = 32; c < 128; c++){
166 if(
int error = FT_Load_Char(face,c,FT_LOAD_RENDER)){
167 Logger::logError(
"Failed loading char '" + std::to_string(c)
168 +
"'. Error code: " + std::to_string(error));
172 if (glyph->bitmap.width > 0 && glyph->bitmap.rows > 0) {
173 glTexSubImage2D(GL_TEXTURE_2D, 0, x, 0, glyph->bitmap.width, glyph->bitmap.rows,
174 GL_RED, GL_UNSIGNED_BYTE, glyph->bitmap.buffer);
178 font->addGlyph(glyph,x,c);
179 x += glyph->bitmap.width;
185 FontManager::FontManager() {
187 ft_library = (FT_Library*)malloc(
sizeof(FT_Library));
188 const int error = FT_Init_FreeType(ft_library);
190 throw new runtime_error(
"Could not initiate FreeType. Error code: " + std::to_string(error));
194 FontManager::~FontManager() {
195 if (ft_library !=
nullptr) {
Font * loadAndFetchFont(std::string fontFace, int pixelSize)
virtual void loadFont(std::string fontFace, int pixelSize)
static FontManager * getInstance()