Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Ignore:
Timestamp:
Jun 24, 2006, 3:49:33 AM (19 years ago)
Author:
bensch
Message:

trunk: much better loading of the Font

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/lib/graphics/text_engine/font_data.cc

    r8764 r8765  
    2828 */
    2929FontData::FontData()
    30   : texData(new TextureData)
    31 {
    32   printf("CREATE FONT_DATA\n");
    33   this->fontTTF = NULL;
     30    : texData(new TextureData)
     31{
    3432  this->glyphArray = NULL;
    3533  this->renderStyle = TTF_STYLE_NORMAL;
    3634  this->renderSize = FONT_DEFAULT_RENDER_SIZE;
     35
     36  this->maxHeight = 0;
     37  this->maxAscent = 0;
     38  this->maxDescent = 0;
    3739}
    3840
     
    6062  //     if(glIsTexture(this->fastTextureID))
    6163  //       glDeleteTextures(1, &this->fastTextureID);
    62 
    63   // erease this font out of the memory.
    64   if (likely(this->fontTTF != NULL))
    65     TTF_CloseFont(this->fontTTF);
    66 }
    67 
    68 /**
    69  * @returns the maximum height of the Font, if the font was initialized, 0 otherwise
    70  */
    71 int FontData::getMaxHeight() const
    72 {
    73   if (likely (this->fontTTF != NULL))
    74     return TTF_FontHeight(this->fontTTF);
    75   else
    76     return 1;
    77 }
    78 
    79 /**
    80  * @returns the maximum ascent of the Font, if the font was initialized, 0 otherwise
    81  *
    82  * the ascent is the pixels of the font above the baseline
    83  */
    84 int FontData::getMaxAscent() const
    85 {
    86   if (likely(this->fontTTF != NULL))
    87     return TTF_FontAscent(this->fontTTF);
    88   else
    89     return 0;
    90 }
    91 
    92 /**
    93  * @returns the maximum descent of the Font, if the font was initialized, 0 otherwise
    94  *
    95  * the descent is the pixels of the font below the baseline
    96  */
    97 int FontData::getMaxDescent() const
    98 {
    99   if (likely(this->fontTTF != NULL))
    100     return TTF_FontDescent(this->fontTTF);
    101   else
    102     return 0;
    103 }
    104 
     64}
    10565
    10666
     
    11272bool FontData::loadFontFromTTF(const std::string& fontFile, unsigned int renderSize)
    11373{
     74  TTF_Font* fontTTF;
    11475  //this->setName(fontFile);
    115   this->fontTTF = TTF_OpenFont(fontFile.c_str(), renderSize);
     76  fontTTF = TTF_OpenFont(fontFile.c_str(), renderSize);
    11677  this->renderSize = renderSize;
    11778
    118   if(this->fontTTF != NULL)
    119   {
    120     this->setStyle("c");
    121     if (this->createFastTexture())
     79  if(fontTTF != NULL)
     80  {
     81    this->maxHeight = TTF_FontHeight(fontTTF);
     82    this->maxAscent = TTF_FontAscent(fontTTF);
     83    this->maxDescent = TTF_FontDescent(fontTTF);
     84
     85    this->setStyle(fontTTF, "c");
     86    if (this->createFastTexture(fontTTF))
     87    {
     88      TTF_CloseFont(fontTTF);
    12289      return true;
     90    }
     91
    12392    else
    12493    {
     94      TTF_CloseFont(fontTTF);
     95      PRINTF(1)("Unable to createa a Fast Texture fot %s\n", fontFile.c_str() );
    12596      return false;
    12697    }
     
    12899  else
    129100  {
    130     PRINTF(1)("TTF_OpenFont: %s\n", TTF_GetError());
     101    PRINTF(1)("TTF_OpenFont: %s for %s\n", TTF_GetError(), fontFile.c_str());
    131102    return false;
    132103  }
     
    194165 *   i: italic, b: bold, u, underline
    195166 */
    196 void FontData::setStyle(const std::string& renderStyle)
     167void FontData::setStyle(TTF_Font* font, const std::string& renderStyle)
    197168{
    198169  this->renderStyle = TTF_STYLE_NORMAL;
     
    207178      this->renderStyle |= TTF_STYLE_UNDERLINE;
    208179  }
    209   if (likely(this->fontTTF != NULL))
    210     TTF_SetFontStyle(this->fontTTF, this->renderStyle);
     180
     181  /// !TODO REBUILD THE FONT
     182
     183  if (likely(font != NULL))
     184      TTF_SetFontStyle(font, this->renderStyle);
    211185  //  else
    212186  //    PRINTF(2)("Font was not initialized, please do so before setting the Font-Style.\n");
     
    225199 * for more info about vertical Fonts
    226200 */
    227 bool FontData::getGlyphMetrics(Glyph* glyph, Uint16 character)
     201bool FontData::getGlyphMetrics(TTF_Font* font, Glyph* glyph, Uint16 character)
    228202{
    229203  glyph->character = character;
    230   if (likely (this->fontTTF!= NULL))
     204  if (likely (font != NULL))
    231205  {
    232206    int miX, maX, miY, maY, adv;
    233     if (TTF_GlyphMetrics(this->fontTTF, glyph->character,
    234         &miX, &maX,
    235         &miY, &maY,
    236         &adv) == -1)
     207    if (TTF_GlyphMetrics(font, glyph->character,
     208                         &miX, &maX,
     209                         &miY, &maY,
     210                         &adv) == -1)
    237211      return false;
    238212    glyph->minX = (float)miX / (float)this->renderSize;
     
    259233 * @brief creates a Fast-Texture of this Font
    260234 */
    261 bool FontData::createFastTexture()
     235bool FontData::createFastTexture(TTF_Font* fontTTF)
    262236{
    263237  /* interesting GLYPHS:
     
    271245  int numberOfGlyphs = 91;
    272246
    273   this->initGlyphs(32, numberOfGlyphs);
     247  this->initGlyphs(fontTTF, 32, numberOfGlyphs);
    274248  //  this->glyphArray[32]->width = .5f; //!< @todo find out the real size of a Space
    275249
     
    281255  SDL_Rect tmpRect; // this represents a Rectangle for blitting.
    282256  SDL_Surface* tmpSurf =  SDL_CreateRGBSurface(SDL_HWSURFACE,
    283                                                rectSize, rectSize,
    284                                                32,
     257                          rectSize, rectSize,
     258                          32,
    285259#if SDL_BYTEORDER == SDL_LIL_ENDIAN /* OpenGL RGBA masks */
    286260                          0x000000FF,
     
    289263                          0xFF000000
    290264#else
    291                               0xFF000000,
     265                          0xFF000000,
    292266                          0x00FF0000,
    293267                          0x0000FF00,
     
    321295      }
    322296      // reading in the new Glyph
    323       if (likely(this->fontTTF != NULL))
     297      if (likely(fontTTF != NULL))
    324298      {
    325299        SDL_Color white = {255, 255, 255};
    326         glyphSurf = TTF_RenderGlyph_Blended(this->fontTTF, i, white);
     300        glyphSurf = TTF_RenderGlyph_Blended(fontTTF, i, white);
    327301      }
    328302      if( glyphSurf != NULL )
     
    344318  }
    345319  // outputting the GLYPH-table
    346 //       char outName[1024];
    347 //       sprintf( outName, "%s-glyphs.bmp", this->getName());
    348 //       SDL_SaveBMP(tmpSurf, outName);
     320  //       char outName[1024];
     321  //       sprintf( outName, "%s-glyphs.bmp", this->getName());
     322  //       SDL_SaveBMP(tmpSurf, outName);
    349323  this->texData->setAlpha(true);
    350324  if (this->texData->setSurface(tmpSurf))
     
    358332 * @param count The number of Glyphs to start From.
    359333 */
    360 void FontData::initGlyphs(Uint16 from, Uint16 count)
     334void FontData::initGlyphs(TTF_Font* font, Uint16 from, Uint16 count)
    361335{
    362336  /* initialize the Array, and set all its entries to NULL
     
    376350    // setting up all the Glyphs we like.
    377351    Glyph* newGlyph = new Glyph;
    378     if (getGlyphMetrics(newGlyph, i))
     352    if (getGlyphMetrics(font, newGlyph, i))
    379353      this->glyphArray[i] = newGlyph;
    380354    else
Note: See TracChangeset for help on using the changeset viewer.