- Timestamp:
- Oct 13, 2005, 12:00:01 AM (19 years ago)
- Location:
- trunk/src/lib/graphics/text_engine
- Files:
-
- 3 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/lib/graphics/text_engine/font.cc
r5367 r5368 36 36 * @param fontSize the Size of the Font in Pixels 37 37 */ 38 Font::Font(const char* fontFile, unsigned int fontSize)38 Font::Font(const char* fontFile, unsigned int renderSize) 39 39 { 40 40 this->init(); 41 41 42 this-> setSize(fontSize);42 this->renderSize = renderSize; 43 43 this->setStyle("c"); 44 44 45 45 if (fontFile != NULL) 46 this->loadFont (fontFile);46 this->loadFontFromTTF(fontFile); 47 47 } 48 48 … … 118 118 119 119 // erease this font out of the memory. 120 if (likely(this->font != NULL))121 TTF_CloseFont(this->font );120 if (likely(this->fontTTF != NULL)) 121 TTF_CloseFont(this->fontTTF); 122 122 } 123 123 … … 129 129 this->setClassID(CL_FONT, "Font"); 130 130 // setting default values. 131 this->font = NULL;131 this->fontTTF = NULL; 132 132 this->glyphArray = NULL; 133 133 this->fastTextureID = 0; … … 140 140 * @returns true if loaded, false if something went wrong, or if a font was loaded before. 141 141 */ 142 bool Font::loadFont (const char* fontFile)142 bool Font::loadFontFromTTF(const char* fontFile) 143 143 { 144 144 // checking for existent Font. 145 if (this->font != NULL)146 { 147 TTF_CloseFont(this->font );148 this->font = NULL;145 if (this->fontTTF != NULL) 146 { 147 TTF_CloseFont(this->fontTTF); 148 this->fontTTF = NULL; 149 149 } 150 150 if (this->fastTextureID != 0) … … 156 156 157 157 this->setName(fontFile); 158 this->font = TTF_OpenFont(this->getName(), this->fontSize);159 160 if(this->font != NULL)158 this->fontTTF = TTF_OpenFont(this->getName(), this->renderSize); 159 160 if(this->fontTTF != NULL) 161 161 { 162 162 this->fastTextureID = this->createFastTexture(); … … 184 184 return false; 185 185 186 if (this->font != NULL)187 { 188 TTF_CloseFont(this->font );189 this->font = NULL;186 if (this->fontTTF != NULL) 187 { 188 TTF_CloseFont(this->fontTTF); 189 this->fontTTF = NULL; 190 190 } 191 191 if (this->fastTextureID != 0) … … 214 214 tmpGlyph->texCoord[2] = cy+0.001f; 215 215 tmpGlyph->texCoord[3] = cy+0.0625f; 216 tmpGlyph->width = 1 6;217 tmpGlyph->bearingX = 1 6;218 tmpGlyph->bearingY = 1 6;219 tmpGlyph->height = 1 6;216 tmpGlyph->width = 1; 217 tmpGlyph->bearingX = 1; 218 tmpGlyph->bearingY = 1; 219 tmpGlyph->height = 1; 220 220 } 221 221 } … … 241 241 this->renderStyle |= TTF_STYLE_UNDERLINE; 242 242 243 if (likely(this->font != NULL))244 TTF_SetFontStyle(this->font , this->renderStyle);243 if (likely(this->fontTTF != NULL)) 244 TTF_SetFontStyle(this->fontTTF, this->renderStyle); 245 245 // else 246 246 // PRINTF(2)("Font was not initialized, please do so before setting the Font-Style.\n"); 247 247 } 248 248 249 /**250 * Sets a new Size to the font251 * @param fontSize The new Size in pixels.252 */253 void Font::setSize(unsigned int fontSize)254 {255 this->fontSize = fontSize;256 }257 258 249 Font* Font::defaultFont = NULL; 259 250 260 251 void Font::createAsciiImage(const char* fileName) 261 252 { 262 if (this->font == NULL)253 if (this->fontTTF == NULL) 263 254 return; 264 255 int height = this->getMaxHeight(); … … 294 285 { 295 286 SDL_Surface* glyphSurf = NULL; 296 if (likely(this->font != NULL))287 if (likely(this->fontTTF != NULL)) 297 288 { 298 289 SDL_Color white = {255, 255, 255}; 299 glyphSurf = TTF_RenderGlyph_Blended(this->font , posX+16*posY, white);290 glyphSurf = TTF_RenderGlyph_Blended(this->fontTTF, posX+16*posY, white); 300 291 } 301 292 if( glyphSurf != NULL ) … … 339 330 int Font::getMaxHeight() 340 331 { 341 if (likely (this->font != NULL))342 return TTF_FontHeight(this->font );332 if (likely (this->fontTTF != NULL)) 333 return TTF_FontHeight(this->fontTTF); 343 334 else 344 335 return 0; … … 352 343 int Font::getMaxAscent() 353 344 { 354 if (likely(this->font != NULL))355 return TTF_FontAscent(this->font );345 if (likely(this->fontTTF != NULL)) 346 return TTF_FontAscent(this->fontTTF); 356 347 else 357 348 return 0; … … 365 356 int Font::getMaxDescent() 366 357 { 367 if (likely(this->font != NULL))368 return TTF_FontDescent(this->font );358 if (likely(this->fontTTF != NULL)) 359 return TTF_FontDescent(this->fontTTF); 369 360 else 370 361 return 0; … … 384 375 Glyph* rg = new Glyph; 385 376 rg->character = character; 386 if (likely (this->font!= NULL)) 387 TTF_GlyphMetrics(this->font, rg->character, 388 &rg->minX, &rg->maxX, 389 &rg->minY, &rg->maxY, 390 &rg->advance); 377 if (likely (this->fontTTF!= NULL)) 378 { 379 int miX, maX, miY, maY, adv; 380 TTF_GlyphMetrics(this->fontTTF, rg->character, 381 &miX, &maX, 382 &miY, &maY, 383 &adv); 384 rg->minX = (float)miX / (float)this->renderSize; 385 rg->maxX = (float)maX / (float)this->renderSize; 386 rg->minY = (float)miY / (float)this->renderSize; 387 rg->maxY = (float)maY / (float)this->renderSize; 388 rg->advance = (float)adv / (float)this->renderSize; 389 } 391 390 rg->height = rg->maxY - rg->minY; 392 391 rg->width = rg->maxX - rg->minX; … … 412 411 413 412 this->initGlyphs(32, numberOfGlyphs); 414 this->glyphArray[32]->width = fontSize/2; //!< @todo find out the real size of a Space413 this->glyphArray[32]->width = .5f; //!< @todo find out the real size of a Space 415 414 416 415 int rectSize = this->findOptimalFastTextureSize(); … … 447 446 if (tmpGlyph = this->glyphArray[i]) 448 447 { 449 if (tmpGlyph->height > maxLineHeight)450 maxLineHeight = tmpGlyph->height;451 452 if (tmpRect.x+tmpGlyph->advance > tmpSurf->w)448 if (tmpGlyph->height*this->renderSize > maxLineHeight) 449 maxLineHeight = (int)(tmpGlyph->height*this->renderSize); 450 451 if (tmpRect.x+tmpGlyph->advance*this->renderSize > tmpSurf->w) 453 452 { 454 453 tmpRect.x = 0; … … 462 461 } 463 462 // reading in the new Glyph 464 if (likely(this->font != NULL))463 if (likely(this->fontTTF != NULL)) 465 464 { 466 465 SDL_Color white = {255, 255, 255}; 467 glyphSurf = TTF_RenderGlyph_Blended(this->font , i, white);466 glyphSurf = TTF_RenderGlyph_Blended(this->fontTTF, i, white); 468 467 } 469 468 if( glyphSurf != NULL ) … … 473 472 SDL_BlitSurface(glyphSurf, NULL, tmpSurf, &tmpRect); 474 473 tmpGlyph->texCoord[0] = (float)tmpRect.x/(float)tmpSurf->w; 475 tmpGlyph->texCoord[1] = (float)(tmpRect.x + tmpGlyph->advance )/(float)tmpSurf->w;474 tmpGlyph->texCoord[1] = (float)(tmpRect.x + tmpGlyph->advance*this->renderSize)/(float)tmpSurf->w; 476 475 tmpGlyph->texCoord[2] = (float)(tmpRect.y)/(float)tmpSurf->w; 477 tmpGlyph->texCoord[3] = (float)(tmpRect.y+tmpGlyph->height )/(float)tmpSurf->w;476 tmpGlyph->texCoord[3] = (float)(tmpRect.y+tmpGlyph->height*this->renderSize)/(float)tmpSurf->w; 478 477 SDL_FreeSurface(glyphSurf); 479 478 480 tmpRect.x += tmpGlyph->advance;479 tmpRect.x += (int)(tmpGlyph->advance*this->renderSize); 481 480 482 481 // Outputting Glyphs to BMP-files. 483 /*482 /* 484 483 char outname[512]; 485 484 if (i < 10) … … 489 488 else 490 489 sprintf( outname, "%s-glyph-%d.bmp", this->getName(), i ); 491 SDL_SaveBMP(tmpSurf, outname); */492 490 SDL_SaveBMP(tmpSurf, outname); 491 */ 493 492 } 494 493 } … … 568 567 { 569 568 // getting the height of the highest Glyph in the Line. 570 if (tmpGlyph->height > maxLineHeight)571 maxLineHeight = tmpGlyph->height;572 573 if (x + tmpGlyph->advance > size)569 if (tmpGlyph->height*this->renderSize > maxLineHeight) 570 maxLineHeight = (int)(tmpGlyph->height*this->renderSize); 571 572 if (x + tmpGlyph->advance*this->renderSize > size) 574 573 { 575 574 x = 0; … … 579 578 if (y + maxLineHeight + 1 > size) 580 579 break; 581 x += tmpGlyph->advance;580 x += (int)(tmpGlyph->advance*this->renderSize); 582 581 583 582 } … … 599 598 // print the loaded font's style 600 599 int style; 601 if (likely(this->font != NULL))602 style = TTF_GetFontStyle(this->font );600 if (likely(this->fontTTF != NULL)) 601 style = TTF_GetFontStyle(this->fontTTF); 603 602 PRINTF(0)("The font style is:"); 604 603 if(style==TTF_STYLE_NORMAL) -
trunk/src/lib/graphics/text_engine/font.h
r5367 r5368 37 37 // Glyph-specific (size and so on) 38 38 Uint16 character; //!< The character 39 intminX; //!< The minimum distance from the origin in X40 intmaxX; //!< The maximum distance from the origin in X41 intminY; //!< The minimum distance from the origin in Y42 intmaxY; //!< The maximum distance from the origin in Y43 intwidth; //!< The width of the Glyph44 intheight; //!< The height of the Glyph45 intbearingX; //!< How much is right of the Origin46 intbearingY; //!< How much is above the Origin47 intadvance; //!< How big a Glyph would be in monospace-mode39 float minX; //!< The minimum distance from the origin in X 40 float maxX; //!< The maximum distance from the origin in X 41 float minY; //!< The minimum distance from the origin in Y 42 float maxY; //!< The maximum distance from the origin in Y 43 float width; //!< The width of the Glyph 44 float height; //!< The height of the Glyph 45 float bearingX; //!< How much is right of the Origin 46 float bearingY; //!< How much is above the Origin 47 float advance; //!< How big a Glyph would be in monospace-mode 48 48 49 49 GLfloat texCoord[4]; //!< Texture coordinates: 0:left, 1:right, 2: top, 3: bottom. … … 58 58 public: 59 59 Font(const char* fontFile, 60 unsigned int fontSize);60 unsigned int renderSize); 61 61 Font(const char* imageFile); 62 62 Font(char** xpmArray); … … 66 66 67 67 // font 68 bool loadFont (const char* fontFile);68 bool loadFontFromTTF(const char* fontFile); 69 69 bool loadFontFromSDL_Surface(SDL_Surface* surface); 70 70 71 void setSize(unsigned int fontSize);72 71 void setStyle(const char* renderStyle); 73 72 … … 82 81 static void initDefaultFont(); 83 82 static void removeDefaultFont(); 83 84 84 85 85 private: … … 99 99 static Font* defaultFont; //!< a default font, that is used, if other fonts were unable to be loaded. 100 100 // information about the Font 101 TTF_Font* font; //!< The font we use for this. 102 unsigned int fontSize; //!< The size of the font in pixels. each Font has one size. 101 TTF_Font* fontTTF; //!< The font we use for this. 103 102 int renderStyle; //!< The Renderstyle 103 unsigned int renderSize; //!< How big the Font should be rendered. 104 104 105 105 Glyph** glyphArray; //!< An Array of all the Glyphs stored in the Array of Glyphs. -
trunk/src/lib/graphics/text_engine/text.cc
r5367 r5368 32 32 * @param type The renderType to display this font in 33 33 */ 34 Text::Text(const char* fontFile, unsigned int fontSize, TEXT_RENDER_TYPE type)34 Text::Text(const char* fontFile, unsigned int textSize, TEXT_RENDER_TYPE type) 35 35 { 36 36 this->init(); 37 37 38 38 if (fontFile != NULL) 39 this->setFont(fontFile, fontSize);39 this->setFont(fontFile, textSize); 40 40 this->setType(type); 41 this->size = textSize; 41 42 } 42 43 … … 109 110 void Text::setType(TEXT_RENDER_TYPE type) 110 111 { 111 if (this->font != NULL && this->font->font )112 if (this->font != NULL && this->font->fontTTF) 112 113 this->type = type; 113 114 else … … 150 151 Glyph** glyphArray = this->font->getGlyphArray(); 151 152 152 int width = 0;153 float width = 0; 153 154 const char* tmpText = this->externText; 154 155 if (this->externText == NULL) … … 184 185 { 185 186 SDL_Color theColor = { (int)(this->color.x*255), (int)(this->color.y*255), (int)(this->color.z*255) }; 186 tmpSurf = TTF_RenderText_Blended(this->font->font ,187 tmpSurf = TTF_RenderText_Blended(this->font->fontTTF, 187 188 this->text, 188 189 theColor); … … 248 249 glBegin(GL_QUADS); 249 250 glTexCoord2f(tmpGlyph->texCoord[0], tmpGlyph->texCoord[2]); 250 glVertex2d(0, - tmpGlyph->bearingY );251 glVertex2d(0, - tmpGlyph->bearingY * this->size); 251 252 glTexCoord2f(tmpGlyph->texCoord[0], tmpGlyph->texCoord[3]); 252 glVertex2d(0, tmpGlyph->height - tmpGlyph->bearingY);253 glVertex2d(0, (tmpGlyph->height - tmpGlyph->bearingY)*this->size); 253 254 glTexCoord2f(tmpGlyph->texCoord[1], tmpGlyph->texCoord[3]); 254 glVertex2d(tmpGlyph->width , tmpGlyph->height - tmpGlyph->bearingY);255 glVertex2d(tmpGlyph->width*this->size, (tmpGlyph->height - tmpGlyph->bearingY)*this->size); 255 256 glTexCoord2f(tmpGlyph->texCoord[1], tmpGlyph->texCoord[2]); 256 glVertex2d(tmpGlyph->width , - tmpGlyph->bearingY);257 glVertex2d(tmpGlyph->width*this->size, - tmpGlyph->bearingY*this->size); 257 258 glEnd(); 258 259 glEndList(); 259 glTranslatef(glyphArray[*tmpText]->width , 0, 0);260 glTranslatef(glyphArray[*tmpText]->width*this->size, 0, 0); 260 261 } 261 262 tmpText++; … … 371 372 glGenTextures(1, &texture); 372 373 glBindTexture(GL_TEXTURE_2D, texture); 373 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_ NEAREST);374 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); 374 375 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); 375 376 glTexImage2D(GL_TEXTURE_2D,
Note: See TracChangeset
for help on using the changeset viewer.