Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members

TextureFont Class Reference

#include <texturefont.h>

Collaboration diagram for TextureFont:

Collaboration graph
List of all members.

Public Types

enum  { TxfByte = 0, TxfBitmap = 1 }

Public Member Functions

void bind ()
bool buildTexture ()
int getHeight () const
int getMaxAscent () const
int getMaxDescent () const
int getMaxWidth () const
int getTextureName () const
int getWidth (int c) const
int getWidth (const std::string &) const
void render (const std::string &) const
void render (wchar_t) const
void setMaxAscent (int)
void setMaxDescent (int)
 TextureFont ()
 ~TextureFont ()

Static Public Member Functions

static TextureFontload (std::istream &in)

Private Member Functions

void addGlyph (const Glyph &)
const TextureFont::GlyphgetGlyph (wchar_t) const
void rebuildGlyphLookupTable ()

Private Attributes

unsigned char * fontImage
const Glyph ** glyphLookup
unsigned int glyphLookupTableSize
std::vector< Glyphglyphs
int maxAscent
int maxDescent
int maxWidth
int texHeight
unsigned int texName
int texWidth

Classes

struct  Glyph
struct  TexCoord

Member Enumeration Documentation

anonymous enum
 

Enumeration values:
TxfByte 
TxfBitmap 

Definition at line 65 of file texturefont.h.

00065          {
00066         TxfByte = 0,
00067         TxfBitmap = 1,
00068     };


Constructor & Destructor Documentation

TextureFont::TextureFont  ) 
 

Definition at line 29 of file texturefont.cpp.

Referenced by load().

00029                          :
00030     maxAscent(0),
00031     maxDescent(0),
00032     maxWidth(0),
00033     texWidth(0),
00034     texHeight(0),
00035     fontImage(NULL),
00036     texName(0),
00037     glyphLookup(NULL),
00038     glyphLookupTableSize(0)
00039 {
00040 }

TextureFont::~TextureFont  ) 
 

Definition at line 43 of file texturefont.cpp.

References fontImage, glyphLookup, and texName.

00044 {
00045     if (texName != 0)
00046         glDeleteTextures(1, (const GLuint*) &texName);
00047     if (fontImage != NULL)
00048         delete[] fontImage;
00049     if (glyphLookup != NULL)
00050         delete[] glyphLookup;
00051 }


Member Function Documentation

void TextureFont::addGlyph const Glyph  )  [private]
 

Definition at line 156 of file texturefont.cpp.

References glyphs, maxWidth, and TextureFont::Glyph::width.

Referenced by load().

00157 {
00158     glyphs.insert(glyphs.end(), g);
00159     if (g.width > maxWidth)
00160         maxWidth = g.width;
00161 }

void TextureFont::bind  ) 
 

Definition at line 149 of file texturefont.cpp.

References texName.

Referenced by Overlay::print(), Console::render(), Renderer::renderLabels(), Renderer::renderLocations(), and Renderer::renderSortedLabels().

00150 {
00151     if (texName != 0)
00152         glBindTexture(GL_TEXTURE_2D, texName);
00153 }

bool TextureFont::buildTexture  ) 
 

Definition at line 173 of file texturefont.cpp.

References DPRINTF, fontImage, texHeight, texName, and texWidth.

Referenced by CelestiaCore::initRenderer().

00174 {
00175     assert(fontImage != NULL);
00176 
00177     if (texName != 0)
00178         glDeleteTextures(1, (const GLuint*) &texName);
00179     glGenTextures(1, (GLuint*) &texName);
00180     if (texName == 0)
00181     {
00182         DPRINTF(0, "Failed to allocate texture object for font.\n");
00183         return false;
00184     }
00185 
00186     glBindTexture(GL_TEXTURE_2D, texName);
00187 
00188     // Don't build mipmaps . . . should really make them an option.
00189     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
00190     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
00191     glTexImage2D(GL_TEXTURE_2D, 0, GL_ALPHA,
00192                  texWidth, texHeight,
00193                  0,
00194                  GL_ALPHA, GL_UNSIGNED_BYTE,
00195                  fontImage);
00196 
00197     return true;
00198 }

const TextureFont::Glyph * TextureFont::getGlyph wchar_t   )  const [private]
 

Definition at line 164 of file texturefont.cpp.

References glyphLookup, and glyphLookupTableSize.

Referenced by getWidth(), and render().

00165 {
00166     if (ch >= (wchar_t)glyphLookupTableSize)
00167         return NULL;
00168     else
00169         return glyphLookup[ch];
00170 }

int TextureFont::getHeight  )  const
 

Definition at line 112 of file texturefont.cpp.

References maxAscent, and maxDescent.

Referenced by Overlay::print(), Console::render(), and CelestiaCore::renderOverlay().

00113 {
00114     return maxAscent + maxDescent;
00115 }

int TextureFont::getMaxAscent  )  const
 

Definition at line 122 of file texturefont.cpp.

References maxAscent.

00123 {
00124     return maxAscent;
00125 }

int TextureFont::getMaxDescent  )  const
 

Definition at line 132 of file texturefont.cpp.

References maxDescent.

00133 {
00134     return maxDescent;
00135 }

int TextureFont::getMaxWidth  )  const
 

Definition at line 117 of file texturefont.cpp.

References maxWidth.

00118 {
00119     return maxWidth;
00120 }

int TextureFont::getTextureName  )  const
 

Definition at line 143 of file texturefont.cpp.

References texName.

00144 {
00145     return texName;
00146 }

int TextureFont::getWidth int  c  )  const
 

int TextureFont::getWidth const std::string  )  const
 

Definition at line 90 of file texturefont.cpp.

References TextureFont::Glyph::advance, getGlyph(), UTF8Decode(), and UTF8EncodedSize().

Referenced by CelestiaCore::getTextWidth(), and CelestiaCore::renderOverlay().

00091 {
00092     int width = 0;
00093     int len = s.length();
00094     bool validChar = true;
00095         int i = 0;
00096 
00097     while (i < len && validChar)
00098     {
00099         wchar_t ch = 0;
00100         validChar = UTF8Decode(s, i, ch);
00101         i += UTF8EncodedSize(ch);
00102 
00103         const Glyph* g = getGlyph(ch);
00104         if (g != NULL)
00105             width += g->advance;
00106     }
00107 
00108     return width;
00109 }

TextureFont * TextureFont::load std::istream &  in  )  [static]
 

Definition at line 276 of file texturefont.cpp.

References addGlyph(), TextureFont::Glyph::advance, DPRINTF, fontImage, TextureFont::Glyph::height, maxAscent, maxDescent, readInt16(), readInt8(), readUint16(), readUint32(), readUint8(), rebuildGlyphLookupTable(), setMaxAscent(), setMaxDescent(), TextureFont::Glyph::texCoords, texHeight, TextureFont(), texWidth, TxfByte, TextureFont::TexCoord::u, TextureFont::TexCoord::v, TextureFont::Glyph::width, TextureFont::Glyph::x, TextureFont::Glyph::xoff, TextureFont::Glyph::y, and TextureFont::Glyph::yoff.

Referenced by LoadTextureFont().

00277 {
00278     char header[4];
00279 
00280     in.read(header, sizeof header);
00281     if (!in.good() || strncmp(header, "\377txf", 4) != 0)
00282     {
00283         DPRINTF(0, "Stream is not a texture font!.\n");
00284         return NULL;
00285     }
00286 
00287     uint32 endiannessTest = 0;
00288     in.read(reinterpret_cast<char*>(&endiannessTest), sizeof endiannessTest);
00289     if (!in.good())
00290     {
00291         DPRINTF(0, "Error reading endianness bytes in txf header.\n");
00292         return NULL;
00293     }
00294 
00295     bool byteSwap;
00296     if (endiannessTest == 0x78563412)
00297         byteSwap = true;
00298     else if (endiannessTest == 0x12345678)
00299         byteSwap = false;
00300     else
00301     {
00302         DPRINTF(0, "Stream is not a texture font!.\n");
00303         return NULL;
00304     }
00305 
00306     int format = readUint32(in, byteSwap);
00307     unsigned int texWidth = readUint32(in, byteSwap);
00308     unsigned int texHeight = readUint32(in, byteSwap);
00309     unsigned int maxAscent = readUint32(in, byteSwap);
00310     unsigned int maxDescent = readUint32(in, byteSwap);
00311     unsigned int nGlyphs = readUint32(in, byteSwap);
00312 
00313     if (!in)
00314     {
00315         DPRINTF(0, "Texture font stream is incomplete");
00316         return NULL;
00317     }
00318 
00319     DPRINTF(1, "Font contains %d glyphs.\n", nGlyphs);
00320 
00321     TextureFont* font = new TextureFont();
00322     assert(font != NULL);
00323 
00324     font->setMaxAscent(maxAscent);
00325     font->setMaxDescent(maxDescent);
00326 
00327     float dx = 0.5f / texWidth;
00328     float dy = 0.5f / texHeight;
00329 
00330     for (unsigned int i = 0; i < nGlyphs; i++)
00331     {
00332         uint16 __id = readUint16(in, byteSwap);
00333         TextureFont::Glyph glyph(__id);
00334 
00335         glyph.width = readUint8(in);
00336         glyph.height = readUint8(in);
00337         glyph.xoff = readInt8(in);
00338         glyph.yoff = readInt8(in);
00339         glyph.advance = readInt8(in);
00340         readInt8(in);
00341         glyph.x = readInt16(in, byteSwap);
00342         glyph.y = readInt16(in, byteSwap);
00343         
00344         if (!in)
00345         {
00346             DPRINTF(0, "Error reading glyph %ud from texture font stream.\n", i);
00347             delete font;
00348             return NULL;
00349         }
00350 
00351         float fWidth = texWidth;
00352         float fHeight = texHeight;
00353         glyph.texCoords[0].u = glyph.x / fWidth + dx;
00354         glyph.texCoords[0].v = glyph.y / fHeight + dy;
00355         glyph.texCoords[1].u = (glyph.x + glyph.width) / fWidth + dx;
00356         glyph.texCoords[1].v = glyph.y / fHeight + dy;
00357         glyph.texCoords[2].u = (glyph.x + glyph.width) / fWidth + dx;
00358         glyph.texCoords[2].v = (glyph.y + glyph.height) / fHeight + dy;
00359         glyph.texCoords[3].u = glyph.x / fWidth + dx;
00360         glyph.texCoords[3].v = (glyph.y + glyph.height) / fHeight + dy;
00361 
00362         font->addGlyph(glyph);
00363     }
00364 
00365     font->texWidth = texWidth;
00366     font->texHeight = texHeight;
00367     if (format == TxfByte)
00368     {
00369         unsigned char* fontImage = new unsigned char[texWidth * texHeight];
00370         if (fontImage == NULL)
00371         {
00372             DPRINTF(0, "Not enough memory for font bitmap.\n");
00373             delete font;
00374             return NULL;
00375         }
00376 
00377         DPRINTF(1, "Reading %d x %d 8-bit font image.\n", texWidth, texHeight);
00378 
00379         in.read(reinterpret_cast<char*>(fontImage), texWidth * texHeight);
00380         if (in.gcount() != (signed)(texWidth * texHeight))
00381         {
00382             DPRINTF(0, "Missing bitmap data in font stream.\n");
00383             delete font;
00384             delete[] fontImage;
00385             return NULL;
00386         }
00387 
00388         font->fontImage = fontImage;
00389     }
00390     else
00391     {
00392         int rowBytes = (texWidth + 7) >> 3;
00393         unsigned char* fontBits = new unsigned char[rowBytes * texHeight];
00394         unsigned char* fontImage = new unsigned char[texWidth * texHeight];
00395         if (fontImage == NULL || fontBits == NULL)
00396         {
00397             DPRINTF(0, "Not enough memory for font bitmap.\n");
00398             delete font;
00399             if (fontBits != NULL)
00400                 delete[] fontBits;
00401             if (fontImage != NULL)
00402                 delete[] fontImage;
00403             return NULL;
00404         }
00405 
00406         DPRINTF(1, "Reading %d x %d 1-bit font image.\n", texWidth, texHeight);
00407 
00408         in.read(reinterpret_cast<char*>(fontBits), rowBytes * texHeight);
00409         if (in.gcount() != (signed)(rowBytes * texHeight))
00410         {
00411             DPRINTF(0, "Missing bitmap data in font stream.\n");
00412             delete font;
00413             return NULL;
00414         }
00415 
00416         for (unsigned int y = 0; y < texHeight; y++)
00417         {
00418             for (unsigned int x = 0; x < texWidth; x++)
00419             {
00420                 if ((fontBits[y * rowBytes + (x >> 3)] & (1 << (x & 0x7))) != 0)
00421                     fontImage[y * texWidth + x] = 0xff;
00422                 else
00423                     fontImage[y * texWidth + x] = 0x0;
00424             }
00425         }
00426 
00427         font->fontImage = fontImage;
00428         delete[] fontBits;
00429     }
00430 
00431     font->rebuildGlyphLookupTable();
00432 
00433     return font;
00434 }

void TextureFont::rebuildGlyphLookupTable  )  [private]
 

Definition at line 201 of file texturefont.cpp.

References TextureFont::Glyph::__id, DPRINTF, glyphLookup, glyphLookupTableSize, and glyphs.

Referenced by load().

00202 {
00203     if (glyphs.size() == 0)
00204         return;
00205 
00206     // Find the largest glyph id
00207     int maxID = glyphs[0].__id;
00208     vector<Glyph>::const_iterator iter;
00209     for (iter = glyphs.begin(); iter != glyphs.end(); iter++)
00210     {
00211         if (iter->__id > maxID)
00212             maxID = iter->__id;
00213     }
00214 
00215     // If there was already a lookup table, delete it.
00216     if (glyphLookup != NULL)
00217         delete[] glyphLookup;
00218 
00219     DPRINTF(1, "texturefont: allocating glyph lookup table with %d entries.\n",
00220             maxID + 1);
00221     glyphLookup = new const Glyph*[maxID + 1];
00222     for (int i = 0; i <= maxID; i++)
00223         glyphLookup[i] = NULL;
00224 
00225     // Fill the table with glyph pointers
00226     for (iter = glyphs.begin(); iter != glyphs.end(); iter++)
00227         glyphLookup[iter->__id] = &(*iter);
00228     glyphLookupTableSize = (unsigned int) maxID + 1;
00229 }

void TextureFont::render const std::string  )  const
 

Definition at line 74 of file texturefont.cpp.

References render(), UTF8Decode(), and UTF8EncodedSize().

00075 {
00076     int len = s.length();
00077     bool validChar = true;
00078     int i = 0;
00079         
00080         while (i < len && validChar) {
00081         wchar_t ch = 0;
00082         validChar = UTF8Decode(s, i, ch);
00083         i += UTF8EncodedSize(ch);
00084         
00085         render(ch);
00086     }
00087 }

void TextureFont::render wchar_t   )  const
 

Definition at line 54 of file texturefont.cpp.

References TextureFont::Glyph::advance, getGlyph(), TextureFont::Glyph::height, TextureFont::Glyph::texCoords, TextureFont::TexCoord::u, TextureFont::TexCoord::v, TextureFont::Glyph::width, TextureFont::Glyph::xoff, and TextureFont::Glyph::yoff.

Referenced by Overlay::print(), render(), Console::render(), Renderer::renderLabels(), Renderer::renderLocations(), and Renderer::renderSortedLabels().

00055 {
00056     const Glyph* glyph = getGlyph(ch);
00057     if (glyph != NULL)
00058     {
00059         glBegin(GL_QUADS);
00060         glTexCoord2f(glyph->texCoords[0].u, glyph->texCoords[0].v);
00061         glVertex2f(glyph->xoff, glyph->yoff);
00062         glTexCoord2f(glyph->texCoords[1].u, glyph->texCoords[1].v);
00063         glVertex2f(glyph->xoff + glyph->width, glyph->yoff);
00064         glTexCoord2f(glyph->texCoords[2].u, glyph->texCoords[2].v);
00065         glVertex2f(glyph->xoff + glyph->width, glyph->yoff + glyph->height);
00066         glTexCoord2f(glyph->texCoords[3].u, glyph->texCoords[3].v);
00067         glVertex2f(glyph->xoff, glyph->yoff + glyph->height);
00068         glEnd();
00069         glTranslatef(glyph->advance, 0.0f, 0.0f);
00070     }
00071 }

void TextureFont::setMaxAscent int   ) 
 

Definition at line 127 of file texturefont.cpp.

References maxAscent.

Referenced by load().

00128 {
00129     maxAscent = _maxAscent;
00130 }

void TextureFont::setMaxDescent int   ) 
 

Definition at line 137 of file texturefont.cpp.

References maxDescent.

Referenced by load().

00138 {
00139     maxDescent = _maxDescent;
00140 }


Member Data Documentation

unsigned char* TextureFont::fontImage [private]
 

Definition at line 82 of file texturefont.h.

Referenced by buildTexture(), load(), and ~TextureFont().

const Glyph** TextureFont::glyphLookup [private]
 

Definition at line 87 of file texturefont.h.

Referenced by getGlyph(), rebuildGlyphLookupTable(), and ~TextureFont().

unsigned int TextureFont::glyphLookupTableSize [private]
 

Definition at line 88 of file texturefont.h.

Referenced by getGlyph(), and rebuildGlyphLookupTable().

std::vector<Glyph> TextureFont::glyphs [private]
 

Definition at line 85 of file texturefont.h.

Referenced by addGlyph(), and rebuildGlyphLookupTable().

int TextureFont::maxAscent [private]
 

Definition at line 76 of file texturefont.h.

Referenced by getHeight(), getMaxAscent(), load(), and setMaxAscent().

int TextureFont::maxDescent [private]
 

Definition at line 77 of file texturefont.h.

Referenced by getHeight(), getMaxDescent(), load(), and setMaxDescent().

int TextureFont::maxWidth [private]
 

Definition at line 78 of file texturefont.h.

Referenced by addGlyph(), and getMaxWidth().

int TextureFont::texHeight [private]
 

Definition at line 81 of file texturefont.h.

Referenced by buildTexture(), and load().

unsigned int TextureFont::texName [private]
 

Definition at line 83 of file texturefont.h.

Referenced by bind(), buildTexture(), getTextureName(), and ~TextureFont().

int TextureFont::texWidth [private]
 

Definition at line 80 of file texturefont.h.

Referenced by buildTexture(), and load().


The documentation for this class was generated from the following files:
Generated on Sat Jan 14 22:33:39 2006 for Celestia by  doxygen 1.4.1