Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/trunk/src/lib/graphics/text_engine/font.cc @ 8295

Last change on this file since 8295 was 7727, checked in by bensch, 19 years ago

trunk: some GL-properties for the Textures

File size: 15.9 KB
RevLine 
[4744]1/*
[1853]2   orxonox - the future of 3D-vertical-scrollers
3
4   Copyright (C) 2004 orx
5
6   This program is free software; you can redistribute it and/or modify
7   it under the terms of the GNU General Public License as published by
8   the Free Software Foundation; either version 2, or (at your option)
9   any later version.
[1855]10
11   ### File Specific:
[5343]12   main-programmer: Benjamin Grauer
[1855]13   co-programmer: ...
[1853]14*/
15
[5357]16#define DEBUG_SPECIAL_MODULE DEBUG_MODULE_GRAPHICS
[1853]17
[5343]18#include "font.h"
19#include "text.h"
[1853]20
[5343]21#ifdef HAVE_SDL_IMAGE_H
22#include <SDL_image.h>
23#else
24#include <SDL/SDL_image.h>
25#endif
[5347]26#include "default_font.xpm"
[5343]27
28#include "debug.h"
[5427]29#include "stdlibincl.h"
[5343]30#include "compiler.h"
[1856]31using namespace std;
[1853]32
[5343]33/**
[7449]34 * @brief constructs a Font out of a TTF-FIle
[5343]35 * @param fontFile the File to load the font from
36 * @param fontSize the Size of the Font in Pixels
37 */
[7221]38Font::Font(const std::string& fontFile, unsigned int renderSize)
[5343]39{
40  this->init();
[1856]41
[5368]42  this->renderSize = renderSize;
[5347]43  this->setStyle("c");
[5343]44
[7221]45  if (!fontFile.empty())
[5368]46    this->loadFontFromTTF(fontFile);
[5347]47}
[5343]48
[5347]49/**
[7449]50 * @brief constructs a Font out of an ImageFile
[5347]51 * @param imageFile the ImageFile to load the Font From.
52 */
[7221]53Font::Font(const std::string& imageFile)
[5347]54{
55  this->init();
56  this->setName(imageFile);
57  //  this->setSize(fontSize);
58  SDL_Surface* image = NULL;
[7221]59  if (!imageFile.empty())
60    image = IMG_Load(imageFile.c_str());
[5347]61  else
62    return;
63  if (image != NULL)
64  {
65    this->loadFontFromSDL_Surface(image);
66    SDL_FreeSurface(image);
67  }
68  else
[7221]69    PRINTF(1)("loading from surface %s failed: %s\n", imageFile.c_str(), IMG_GetError());
[5343]70}
71
[3245]72/**
[7449]73 * @brief constructs a Font
[5347]74 * @param xpmArray the xpm-ARRAY to load the font from
[5343]75 */
76Font::Font(char** xpmArray)
[3365]77{
[5343]78  this->init();
[5347]79  this->setName("XPM-array-font");
[5343]80  //  this->setSize(fontSize);
81  SDL_Surface* image = NULL;
82  if (xpmArray != NULL)
83    image = IMG_ReadXPMFromArray(xpmArray);
84  if (image != NULL)
85  {
86    this->loadFontFromSDL_Surface(image);
87    SDL_FreeSurface(image);
88  }
89  else
90    PRINTF(1)("loading from surface failed: %s\n", IMG_GetError());
[3365]91}
[1853]92
93
[3245]94/**
[7449]95 * @brief destructs a font
96 *
[5343]97 * this releases the memory a font uses to be opened.
98 * deletes the glLists, and the TTF-handler, if present.
99 */
100Font::~Font()
[3543]101{
[5343]102  // deleting all Glyphs
103  if (this->glyphArray != NULL)
104  {
105    for (int i = 0; i < FONT_HIGHEST_KNOWN_CHAR; i++)
106    {
107      if (this->glyphArray[i] != NULL)
108        delete this->glyphArray[i];
109    }
110    delete[] this->glyphArray;
111  }
112
[5347]113  //! @todo check if we really do not need to delete the fastTextureID here.
[7221]114  //   if (this->fastTextureID != 0)
115  //     if(glIsTexture(this->fastTextureID))
116  //       glDeleteTextures(1, &this->fastTextureID);
[5347]117
[5343]118  // erease this font out of the memory.
[5368]119  if (likely(this->fontTTF != NULL))
120    TTF_CloseFont(this->fontTTF);
[3543]121}
[5343]122
123/**
[7449]124 * @brief initializes a Font (with default values)
[5343]125 */
126void Font::init()
127{
128  this->setClassID(CL_FONT, "Font");
129  // setting default values.
[5368]130  this->fontTTF = NULL;
[5343]131  this->glyphArray = NULL;
132}
133
134
135/**
136 * sets The Font.
137 * @param fontFile The file containing the font.
138 * @returns true if loaded, false if something went wrong, or if a font was loaded before.
139 */
[7221]140bool Font::loadFontFromTTF(const std::string& fontFile)
[5343]141{
[5347]142  // checking for existent Font.
[5368]143  if (this->fontTTF != NULL)
[5343]144  {
[5368]145    TTF_CloseFont(this->fontTTF);
146    this->fontTTF = NULL;
[5347]147  }
[5343]148
[5768]149
[5347]150  this->setName(fontFile);
[5368]151  this->fontTTF = TTF_OpenFont(this->getName(), this->renderSize);
[5347]152
[5368]153  if(this->fontTTF != NULL)
[5347]154  {
[5768]155    this->createFastTexture();
156    return (this->getTexture() != 0);
[5343]157  }
158  else
159  {
[5347]160    PRINTF(1)("TTF_OpenFont: %s\n", TTF_GetError());
[5343]161    return false;
162  }
[5347]163
[5343]164}
165
166/**
[7449]167 * @brief loads a font From an XPM-array.
[5343]168 * @param xpmArray the array of the XPM to load the font from.
169 */
170bool Font::loadFontFromSDL_Surface(SDL_Surface* surface)
171{
172  // loading to a texture.
173  if(surface == NULL)
174    return false;
[5347]175
[5368]176  if (this->fontTTF != NULL)
[5347]177  {
[5368]178    TTF_CloseFont(this->fontTTF);
179    this->fontTTF = NULL;
[5347]180  }
[5859]181  bool hasAlpha;
182  SDL_Surface* newSurf = this->prepareSurface(surface, hasAlpha);
183  if (newSurf != NULL)
[5856]184  {
[5859]185    this->setSurface(newSurf);
186    this->setAlpha(hasAlpha);
187    this->setTexture(Texture::loadTexToGL(newSurf));
[5856]188  }
[5347]189
[5343]190  // initializing the Glyphs.
191  if (this->glyphArray == NULL)
192  {
193    float cx,cy;
[5367]194    Glyph* tmpGlyph;
[5343]195    this->glyphArray = new Glyph*[FONT_HIGHEST_KNOWN_CHAR];
196    for (int i = 0; i < FONT_HIGHEST_KNOWN_CHAR; i++)
197    {
[5367]198      tmpGlyph = this->glyphArray[i] = new Glyph;
[5343]199      cx=(float)(i%16)/16.0f;                  // X Position Of Current Character
200      cy=(float)(i/16)/16.0f;                  // Y Position Of Current Character
[5367]201
202      tmpGlyph->texCoord[0] = cx;
203      tmpGlyph->texCoord[1] = cx+0.0625f;
[7449]204      tmpGlyph->texCoord[2] = cy;
[5367]205      tmpGlyph->texCoord[3] = cy+0.0625f;
[7449]206      tmpGlyph->minX = 0.0f;
207      tmpGlyph->maxX = 1.0f;
208      tmpGlyph->minY = 0.0f;
209      tmpGlyph->maxY = 1.0f;
210      tmpGlyph->width = 1.0f;
211      tmpGlyph->advance = 1.0f;
212      tmpGlyph->bearingX = 0.0f;
213      tmpGlyph->bearingY = 0.0f;
214      tmpGlyph->height = 1.0f;
[5343]215    }
216  }
217  return true;
218}
219
220
221/**
222 *  sets a specific renderStyle
[5347]223 * @param renderStyle the Style to render: a string (char-array) containing:
224 *   i: italic, b: bold, u, underline
[5343]225 */
[7221]226void Font::setStyle(const std::string& renderStyle)
[5343]227{
228  this->renderStyle = TTF_STYLE_NORMAL;
229
[7221]230  for (int i = 0; i < renderStyle.size(); i++)
231  {
232    if (renderStyle[i] == 'b')
[5343]233      this->renderStyle |= TTF_STYLE_BOLD;
[7221]234    else if (renderStyle[i] == 'i')
235      this->renderStyle |= TTF_STYLE_ITALIC;
236    else if (renderStyle[i] == 'u')
237      this->renderStyle |= TTF_STYLE_UNDERLINE;
238  }
[5368]239  if (likely(this->fontTTF != NULL))
240    TTF_SetFontStyle(this->fontTTF, this->renderStyle);
[7221]241  //  else
242  //    PRINTF(2)("Font was not initialized, please do so before setting the Font-Style.\n");
[5343]243}
244
245Font* Font::defaultFont = NULL;
246
[5768]247/**
248 * creates and exports an Image, that has all the characters
249 * stored in a Array (as an image)
250 * @param fileName the File to write the image into.
251 */
[7221]252void Font::createAsciiImage(const std::string& fileName, unsigned int size) const
[5343]253{
[5368]254  if (this->fontTTF == NULL)
[5343]255    return;
256  int height = this->getMaxHeight();
257
258  //
259  SDL_Color tmpColor = {0, 0, 0};
260  // Surface definition.
261  SDL_Rect tmpRect; // this represents a Rectangle for blitting.
262  SDL_Surface* tmpSurf =  SDL_CreateRGBSurface(SDL_SWSURFACE,
[7221]263                          height*size, height*size,
264                          32,
[5343]265#if SDL_BYTEORDER == SDL_LIL_ENDIAN /* OpenGL RGBA masks */
[7221]266                          0x000000FF,
267                          0x0000FF00,
268                          0x00FF0000,
269                          0xFF000000
[5343]270#else
[7221]271                          0xFF000000,
272                          0x00FF0000,
273                          0x0000FF00,
274                          0x000000FF
[5343]275#endif
276                                              );
277  tmpRect.x = 0; tmpRect.y = 0; tmpRect.w = tmpSurf->w; tmpRect.h = tmpSurf->h;
278  SDL_SetClipRect(tmpSurf, &tmpRect);
279  int maxLineHeight = 0;
280
281  int posX, posY;
282  // all the interessting Glyphs
283  for (posY = 0; posY < 16; posY++)
284  {
285    for (posX = 0; posX < 16; posX++)
286    {
287      SDL_Surface* glyphSurf = NULL;
[5368]288      if (likely(this->fontTTF != NULL))
[5343]289      {
290        SDL_Color white = {255, 255, 255};
[6349]291        glyphSurf = TTF_RenderGlyph_Blended(this->fontTTF, posX+size*posY, white);
[5343]292      }
293      if( glyphSurf != NULL )
294      {
295        tmpRect.x = height*posX;
296        tmpRect.y = height*posY;
297        SDL_SetAlpha(glyphSurf, 0, 0);
298
299        SDL_BlitSurface(glyphSurf, NULL, tmpSurf, &tmpRect);
300        SDL_FreeSurface(glyphSurf);
301      }
302    }
303  }
[7221]304  SDL_SaveBMP(tmpSurf, fileName.c_str());
[5343]305  SDL_FreeSurface(tmpSurf);
306}
307
308/**
[7450]309 * @brief initializes the default font
[5343]310 */
311void Font::initDefaultFont()
312{
313  if (Font::defaultFont == NULL)
314    Font::defaultFont = new Font(font_xpm);
315}
316
317/**
[7450]318 * @brief deletes the default font
[5343]319 */
320void Font::removeDefaultFont()
321{
322  if (Font::defaultFont != NULL)
323    delete Font::defaultFont;
324  Font::defaultFont = NULL;
325}
326
327
328/**
329 * @returns the maximum height of the Font, if the font was initialized, 0 otherwise
330 */
[6349]331int Font::getMaxHeight() const
[5343]332{
[5368]333  if (likely (this->fontTTF != NULL))
334    return TTF_FontHeight(this->fontTTF);
[5343]335  else
[7450]336    return 1;
[5343]337}
338
339/**
340 * @returns the maximum ascent of the Font, if the font was initialized, 0 otherwise
341
342   the ascent is the pixels of the font above the baseline
343 */
[6349]344int Font::getMaxAscent() const
[5343]345{
[5368]346  if (likely(this->fontTTF != NULL))
347    return TTF_FontAscent(this->fontTTF);
[5343]348  else
349    return 0;
350}
351
352/**
353 * @returns the maximum descent of the Font, if the font was initialized, 0 otherwise
354
355   the descent is the pixels of the font below the baseline
356 */
[6349]357int Font::getMaxDescent() const
[5343]358{
[5368]359  if (likely(this->fontTTF != NULL))
360    return TTF_FontDescent(this->fontTTF);
[5343]361  else
362    return 0;
363}
364
365/**
[7430]366 * @param glyph: The Glyph to set the Parameters to.
367 * @param character: The character to get info about.
[5343]368 * @returns a Glyph struct of a character. This Glyph is a pointer,
[7430]369 * and MUST be deleted by the user..
370 *
371 * This only works for horizontal fonts. see
372 * http://freetype.sourceforge.net/freetype2/docs/tutorial/step2.html
373 * for more info about vertical Fonts
[5343]374 */
[7430]375bool Font::getGlyphMetrics(Glyph* glyph, Uint16 character)
[5343]376{
[7430]377  glyph->character = character;
[5368]378  if (likely (this->fontTTF!= NULL))
379  {
380    int miX, maX, miY, maY, adv;
[7430]381    if (TTF_GlyphMetrics(this->fontTTF, glyph->character,
[5368]382                     &miX, &maX,
383                     &miY, &maY,
[7430]384                     &adv) == -1)
385      return false;
386    glyph->minX = (float)miX / (float)this->renderSize;
387    glyph->maxX = (float)maX / (float)this->renderSize;
388    glyph->minY = (float)miY / (float)this->renderSize;
389    glyph->maxY = (float)maY / (float)this->renderSize;
390    glyph->advance = (float)adv / (float)this->renderSize;
391
392    // Calculate the Rest.
393    glyph->height = glyph->maxY - glyph->minY;
394    glyph->width = glyph->maxX - glyph->minX;
395    glyph->bearingX = (glyph->advance - glyph->width) / 2;
396    glyph->bearingY = glyph->maxY;
[7431]397
398    //printf("%c:: %d %d %d %d %d\n", character, miX, maX, miY, maY, adv);
399
[7430]400    return true;
[5368]401  }
[7430]402  return false;
[5343]403}
404
405/**
406 * creates a Fast-Texture of this Font
407 */
[5768]408bool Font::createFastTexture()
[5343]409{
410  /* interesting GLYPHS:
411  *  32: space
412  *  33-47: Special Characters.
413  *  48-57: 0-9
414  *  58-63: some more special chars (minor)
415  *  65-90: A-Z
416  *  97-122: a-z
417  */
418  int numberOfGlyphs = 91;
419
420  this->initGlyphs(32, numberOfGlyphs);
[7221]421  //  this->glyphArray[32]->width = .5f; //!< @todo find out the real size of a Space
[5343]422
423  int rectSize = this->findOptimalFastTextureSize();
424
425  // setting default values. (maybe not needed afterwards)
426  SDL_Color tmpColor;  tmpColor.r = tmpColor.g = tmpColor.b = 0;
427  // Surface definition.
428  SDL_Rect tmpRect; // this represents a Rectangle for blitting.
[7727]429  SDL_Surface* tmpSurf =  SDL_CreateRGBSurface(SDL_HWSURFACE,
[7221]430                          rectSize, rectSize,
431                          32,
[5343]432#if SDL_BYTEORDER == SDL_LIL_ENDIAN /* OpenGL RGBA masks */
[7221]433                          0x000000FF,
434                          0x0000FF00,
435                          0x00FF0000,
436                          0xFF000000
[5343]437#else
[7221]438                          0xFF000000,
439                          0x00FF0000,
440                          0x0000FF00,
441                          0x000000FF
[5343]442#endif
443                                              );
444  tmpRect.x = 0; tmpRect.y = 0; tmpRect.w = tmpSurf->w; tmpRect.h = tmpSurf->h;
445  SDL_SetClipRect(tmpSurf, &tmpRect);
[5420]446  int maxLineHeight = this->getMaxHeight();
[5343]447
448  // all the interessting Glyphs
449  for (int i = 0; i < 128; i++)
450  {
451    SDL_Surface* glyphSurf = NULL;
452    Glyph* tmpGlyph;
453
454    if (tmpGlyph = this->glyphArray[i])
455    {
[5368]456      if (tmpGlyph->height*this->renderSize > maxLineHeight)
457        maxLineHeight = (int)(tmpGlyph->height*this->renderSize);
[5343]458
[5368]459      if (tmpRect.x+tmpGlyph->advance*this->renderSize > tmpSurf->w)
[5343]460      {
461        tmpRect.x = 0;
[5420]462        tmpRect.y = tmpRect.y + maxLineHeight;
[5343]463      }
464      if (tmpRect.y + maxLineHeight > tmpSurf->h)
465      {
[5367]466        PRINTF(1)("Protection, so font cannot write over the boundraries (!!this should not heappen!!)\n");
[5343]467        break;
468      }
[7221]469      // reading in the new Glyph
[5368]470      if (likely(this->fontTTF != NULL))
[5343]471      {
472        SDL_Color white = {255, 255, 255};
[5368]473        glyphSurf = TTF_RenderGlyph_Blended(this->fontTTF, i, white);
[5343]474      }
475      if( glyphSurf != NULL )
476      {
477        SDL_SetAlpha(glyphSurf, 0, 0);
[7431]478        int tmpY = tmpRect.y;
[5420]479        tmpRect.y += this->getMaxAscent()-(int)((float)tmpGlyph->bearingY*this->renderSize);
480        SDL_BlitSurface(glyphSurf, NULL, tmpSurf, &tmpRect);
[7431]481        tmpRect.y = tmpY;
[5343]482
[7431]483        tmpGlyph->texCoord[0] = (float)((float)tmpRect.x )/(float)tmpSurf->w;
484        tmpGlyph->texCoord[1] = (float)((float)tmpRect.x + tmpGlyph->width*(float)this->renderSize)/(float)tmpSurf->w;
[5367]485        tmpGlyph->texCoord[2] = (float)(tmpRect.y)/(float)tmpSurf->w;
[7429]486        tmpGlyph->texCoord[3] = (float)((float)tmpRect.y+(float)this->getMaxHeight())/(float)tmpSurf->w;
[5343]487        SDL_FreeSurface(glyphSurf);
[7431]488        tmpRect.x += (int)(tmpGlyph->width * this->renderSize) + 1;
[5343]489      }
490    }
491  }
[5420]492  // outputting the GLYPH-table
[7432]493//       char outName[1024];
494//       sprintf( outName, "%s-glyphs.bmp", this->getName());
495//       SDL_SaveBMP(tmpSurf, outName);
[7727]496  this->setAlpha(true);
[5768]497  if (this->setSurface(tmpSurf))
[5856]498    (this->setTexture(Texture::loadTexToGL(tmpSurf)));
[5343]499}
500
501/**
[7727]502 * @brief stores Glyph Metrics in an Array.
[5343]503 * @param from The Glyph to start from.
504 * @param count The number of Glyphs to start From.
505 */
506void Font::initGlyphs(Uint16 from, Uint16 count)
507{
508  /* initialize the Array, and set all its entries to NULL
509  *  only if the Glyph-array has not been initialized
510  */
511  if (!this->glyphArray)
512  {
513    this->glyphArray = new Glyph*[FONT_HIGHEST_KNOWN_CHAR];
514    for (int i = 0; i < FONT_HIGHEST_KNOWN_CHAR; i++)
515      this->glyphArray[i] = NULL;
516  }
517
518  Uint16 lastGlyph = from + count;
519
520  for (int i = from; i <= lastGlyph; i++)
521  {
[7221]522    // setting up all the Glyphs we like.
[7430]523    Glyph* newGlyph = new Glyph;
524    if (getGlyphMetrics(newGlyph, i))
525      glyphArray[i] = newGlyph;
526    else
527    {
528      delete newGlyph;
529      glyphArray[i] = NULL;
530    }
[5343]531  }
532  return;
533}
534
535/**
536 * @returns the optimal size to use as the texture size
[7727]537 *
538 * @todo: this algorithm can be a lot faster, althought it does
539 * not really matter within the init-context, and 128 glyphs.
540 *
541 * This function searches for a 2^n sizes texture-size, this is for
542 * openGL-version < 1.2 compatibility ( and because it is realy easy like this :))
[5343]543 */
544int Font::findOptimalFastTextureSize()
545{
[5347]546  if (this->glyphArray == NULL)
547    return 0;
548
[5343]549  int i;
550  int x,y; // the counters
551  int maxLineHeight = this->getMaxHeight();
552  unsigned int size = 32;  // starting Value, we have to start somewhere 32 seems reasonable. (take any small enough 2^i number)
553  bool sizeOK = false;
554  Glyph* tmpGlyph;
555
556  while (!sizeOK)
557  {
558    x = 0; y = 0;
[5347]559    for (i = 0; i < FONT_HIGHEST_KNOWN_CHAR; i++)
[5343]560    {
561      if((tmpGlyph = this->glyphArray[i]) != NULL)
562      {
[7221]563        // getting the height of the highest Glyph in the Line.
[5368]564        if (tmpGlyph->height*this->renderSize > maxLineHeight)
565          maxLineHeight = (int)(tmpGlyph->height*this->renderSize);
[5343]566
[5368]567        if (x + tmpGlyph->advance*this->renderSize > size)
[5343]568        {
569          x = 0;
570          y = y + maxLineHeight;
[7221]571          //maxLineHeight = 0;
[5343]572        }
573        if (y + maxLineHeight + 1 > size)
574          break;
[5370]575        x += (int)(tmpGlyph->advance*this->renderSize)+1;
[5343]576
577      }
578    }
579    if (i >= FONT_HIGHEST_KNOWN_CHAR-1 || size > 8192)
580      sizeOK = true;
581    else
582      size *= 2;
583  }
584  return size;
585}
586
587
588/**
589 *  a simple function to get some interesting information about this class
590 */
591void Font::debug()
592{
593  // print the loaded font's style
594  int style;
[5368]595  if (likely(this->fontTTF != NULL))
596    style = TTF_GetFontStyle(this->fontTTF);
[5343]597  PRINTF(0)("The font style is:");
598  if(style==TTF_STYLE_NORMAL)
599    PRINTF(0)(" normal");
[7221]600  else
601  {
[5343]602    if(style&TTF_STYLE_BOLD)
603      PRINTF(0)(" bold");
604    if(style&TTF_STYLE_ITALIC)
605      PRINTF(0)(" italic");
606    if(style&TTF_STYLE_UNDERLINE)
607      PRINTF(0)(" underline");
608  }
609  PRINTF(0)("\n");
610}
Note: See TracBrowser for help on using the repository browser.