void font_data::init(const char * fname, unsigned int size, ushort low, ushort high) { initflag = true; this->h = (float) size; // Create and initilize a freetype font library. library = 0; if (FT_Init_FreeType( &library )) throw std::runtime_error("FT_Init_FreeType failed"); // The object in which Freetype holds information on a given // font is called a "face". // This is where we load in the font information from the file. // Of all the places where the code might die, this is the most likely, // as FT_New_Face will die if the font file does not exist or is somehow broken. face = 0; if (FT_New_Face( library, fname, 0, &face )) throw std::runtime_error("FT_New_Face failed (there is probably a problem with your font file)"); // For some twisted reason, Freetype measures font size // in terms of 1/64ths of pixels. Thus, to make a font // h pixels high, we need to request a size of h*64. // (h << 6 is just a prettier way of writting h*64) FT_Set_Char_Size(face, size << 6, size << 6, 96, 96); // Here we ask opengl to allocate resources for // all the display lists which we are about to create. list_base=glGenLists(LIST_LEN); // This is where we actually create each of the fonts display lists. for (ushort i = low; i <= high; ++i) { make_dlist(i); } }
Fonte *carregar_fonte(const char *fname, unsigned int tamanho) { TTF_Font *fontSDL=TTF_OpenFont(fname,tamanho); if (!fontSDL) { printf("Erro ao abrir fonte\n"); return NULL; } Fonte *fonte=(Fonte *) calloc(1,sizeof(Fonte)); fonte->chars = (FonteChar *) calloc(256,sizeof(FonteChar)); TTF_SizeText(fontSDL,"A",NULL,&fonte->altura); fonte->list_base=glGenLists(255); unsigned char i; for (i=0; i<255 ;i++) { glGenTextures( 1, &fonte->chars[i].textura ); make_dlist(fontSDL,i,fonte->list_base,&fonte->chars[i]); } TTF_CloseFont(fontSDL); return fonte; }
// get width and dynamically load texture if necessary inline int font_data::load_and_get_width(ushort us) { int w = m.get_w(us); if (w != -1) return w; else { make_dlist(us); w = m.get_w(us); return w; } return 0; }
// load glyphs into arbitrary positions int font_data::load_as(uint glyph_id, ushort char_id, uint num) { int successes = 0; for ( uint pos = 0; pos < num; ++pos ) { int w = m.get_w(char_id + pos); if (w == -1) { make_dlist(glyph_id + pos, char_id + pos); ++successes; } } return successes; }
void Font::init(string fname, int h) { // Allocate Some Memory To Store The Texture Ids. m_pTextures = new GLuint[128]; m_iSize = h; // Create And Initilize A FreeType Font Library. FT_Library library; if (FT_Init_FreeType( &library )) throw std::runtime_error("FT_Init_FreeType failed"); // The Object In Which FreeType Holds Information On A Given // Font Is Called A "face". FT_Face face; // This Is Where We Load In The Font Information From The File. // Of All The Places Where The Code Might Die, This Is The Most Likely, // As FT_New_Face Will Fail If The Font File Does Not Exist Or Is Somehow Broken. if (FT_New_Face( library, fname.c_str(), 0, &face )) throw std::runtime_error("FT_New_Face failed (there is probably a problem with your font file)"); // For Some Twisted Reason, FreeType Measures Font Size // In Terms Of 1/64ths Of Pixels. Thus, To Make A Font // h Pixels High, We Need To Request A Size Of h*64. // (h << 6 Is Just A Prettier Way Of Writing h*64) FT_Set_Char_Size( face, h << 6, h << 6, 96, 96); // Here We Ask OpenGL To Allocate Resources For // All The Textures And Display Lists Which We // Are About To Create. m_iListBase=glGenLists(128); glGenTextures( 128, m_pTextures ); // This Is Where We Actually Create Each Of The Fonts Display Lists. for(unsigned char i=0;i<128;i++) make_dlist(face,i,m_iListBase,m_pTextures); // We Don't Need The Face Information Now That The Display // Lists Have Been Created, So We Free The Assosiated Resources. FT_Done_Face(face); // Ditto For The Font Library. FT_Done_FreeType(library); }
void font_data::init(const char * fname, unsigned int h) { //Allocate some memory to store the texture ids. textures = new GLuint[128]; this->h=h; //Create and initilize a freetype font library. FT_Library library; if (FT_Init_FreeType( &library )) throw std::runtime_error("FT_Init_FreeType failed"); //The object in which Freetype holds information on a given //font is called a "face". FT_Face face; //This is where we load in the font information from the file. //Of all the places where the code might die, this is the most likely, //as FT_New_Face will die if the font file does not exist or is somehow broken. if (FT_New_Face( library, fname, 0, &face )) throw std::runtime_error("FT_New_Face failed (there is probably a problem with your font file)"); //For some twisted reason, Freetype measures font size //in terms of 1/64ths of pixels. Thus, to make a font //h pixels high, we need to request a size of h*64. //(h << 6 is just a prettier way of writting h*64) FT_Set_Char_Size( face, h << 6, h << 6, 96, 96); //Here we ask opengl to allocate resources for //all the textures and displays lists which we //are about to create. list_base=glGenLists(128); glGenTextures( 128, textures ); //This is where we actually create each of the fonts display lists. for(unsigned char i=0; i<128; i++) make_dlist(face,i,list_base,textures); //We don't need the face information now that the display //lists have been created, so we free the assosiated resources. FT_Done_Face(face); //Ditto for the library. FT_Done_FreeType(library); }
void font_data::make_dlist(ushort ch) { make_dlist(ch,ch); }