Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 10144 was 9869, checked in by bensch, 18 years ago

orxonox/trunk: merged the new_class_id branche back to the trunk.
merged with command:
svn merge https://svn.orxonox.net/orxonox/branches/new_class_id trunk -r9683:HEAD
no conflicts… puh..

File size: 6.8 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"
[1853]19
[5343]20#ifdef HAVE_SDL_IMAGE_H
21#include <SDL_image.h>
22#else
23#include <SDL/SDL_image.h>
24#endif
[8761]25
[5347]26#include "default_font.xpm"
[5343]27
28#include "debug.h"
29#include "compiler.h"
[1853]30
[9869]31ObjectListDefinition(Font);
[8761]32
33Font::Font()
[8764]34    : data(Font::defaultFontData)
[8761]35{
36  this->init();
37}
38
[5343]39/**
[7449]40 * @brief constructs a Font out of a TTF-FIle
[5343]41 * @param fontFile the File to load the font from
42 * @param fontSize the Size of the Font in Pixels
43 */
[7221]44Font::Font(const std::string& fontFile, unsigned int renderSize)
[8764]45    : data(Font::defaultFontData)
[5343]46{
47  this->init();
[1856]48
[7221]49  if (!fontFile.empty())
[8761]50    this->loadFontFromTTF(fontFile, renderSize);
[5347]51}
[5343]52
[8761]53
[5347]54/**
[7449]55 * @brief constructs a Font out of an ImageFile
[5347]56 * @param imageFile the ImageFile to load the Font From.
57 */
[7221]58Font::Font(const std::string& imageFile)
[8764]59    : data(Font::defaultFontData)
[5347]60{
61  this->init();
[8761]62
[5347]63  this->setName(imageFile);
64  //  this->setSize(fontSize);
65  SDL_Surface* image = NULL;
[7221]66  if (!imageFile.empty())
67    image = IMG_Load(imageFile.c_str());
[5347]68  else
69    return;
70  if (image != NULL)
71  {
72    this->loadFontFromSDL_Surface(image);
73    SDL_FreeSurface(image);
74  }
75  else
[7221]76    PRINTF(1)("loading from surface %s failed: %s\n", imageFile.c_str(), IMG_GetError());
[5343]77}
78
[3245]79/**
[7449]80 * @brief constructs a Font
[5347]81 * @param xpmArray the xpm-ARRAY to load the font from
[5343]82 */
83Font::Font(char** xpmArray)
[8764]84    : data(Font::defaultFontData)
[3365]85{
[5343]86  this->init();
[5347]87  this->setName("XPM-array-font");
[5343]88  //  this->setSize(fontSize);
89  SDL_Surface* image = NULL;
90  if (xpmArray != NULL)
91    image = IMG_ReadXPMFromArray(xpmArray);
92  if (image != NULL)
93  {
94    this->loadFontFromSDL_Surface(image);
95    SDL_FreeSurface(image);
96  }
97  else
[8761]98    PRINTF(1)("Loading from XPM-array failed: %s\n", IMG_GetError());
[3365]99}
[1853]100
[8763]101Font::Font(const Font& font)
102{
103  this->init();
104  *this = font;
105}
[1853]106
[3245]107/**
[7449]108 * @brief destructs a font
109 *
[5343]110 * this releases the memory a font uses to be opened.
111 * deletes the glLists, and the TTF-handler, if present.
112 */
113Font::~Font()
[8751]114{ }
[5343]115
[8761]116Font& Font::operator=(const Font& font)
117{
118  Material::operator=(font);
119  this->data = font.data;
[9869]120  this->setTexture(this->data->textureData());
[8761]121
122  return *this;
123};
124
125
[5343]126/**
[7449]127 * @brief initializes a Font (with default values)
[5343]128 */
129void Font::init()
130{
[8761]131  this->setBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
132
[9869]133  this->registerObject(this, Font::_objectList);
134  if (Font::defaultFontData.isNull())
[8761]135  {
[8764]136    Font::initDefaultFont();
[8761]137    this->data = Font::defaultFontData;
138  }
[5343]139}
140
[9869]141FontData::Pointer Font::defaultFontData(NULL);
[5343]142
143/**
[8761]144 * @brief initializes the default font
145 */
146void Font::initDefaultFont()
147{
148  // temporarily create a Font.
[9869]149  Font::defaultFontData = FontData::Pointer(new FontData);
[8761]150  // apply the Data.
151  Font::defaultFontData = Font(font_xpm).data;
152}
153
[8768]154
[8761]155/**
[8751]156 * @brief sets The Font.
[5343]157 * @param fontFile The file containing the font.
158 * @returns true if loaded, false if something went wrong, or if a font was loaded before.
159 */
[8761]160bool Font::loadFontFromTTF(const std::string& fontFile, unsigned int renderSize)
[5343]161{
[9869]162  this->data = FontData::Pointer (new FontData());
[8764]163  bool retVal = this->data->loadFontFromTTF(fontFile, renderSize);
164  if (!retVal)
[8761]165    this->data = Font::defaultFontData;
[5347]166
[8766]167  this->setTexture(this->data->textureData());
[8764]168  return retVal;
[5343]169}
170
171/**
[7449]172 * @brief loads a font From an XPM-array.
[5343]173 * @param xpmArray the array of the XPM to load the font from.
174 */
175bool Font::loadFontFromSDL_Surface(SDL_Surface* surface)
176{
[9869]177  this->data = FontData::Pointer (new FontData());
[8764]178  bool retVal = this->data->loadFontFromSDL_Surface(surface);
179  if (!retVal)
[8761]180    this->data = Font::defaultFontData;
[5347]181
[8766]182  this->setTexture(this->data->textureData());
[8764]183  return retVal;
[5343]184}
185
186
187/**
[8751]188 * @brief sets a specific data->renderStyle
189 * @param data->renderStyle the Style to render: a string (char-array) containing:
[5347]190 *   i: italic, b: bold, u, underline
[5343]191 */
[7221]192void Font::setStyle(const std::string& renderStyle)
[5343]193{
[8765]194  /// FIXME
195  //this->data->setStyle(renderStyle);
[8764]196}
[5343]197
[8764]198
[9869]199void Font::setTexture(const TextureData::Pointer& texDataPointer)
[8764]200{
201  this->setDiffuseMap(texDataPointer);
[5343]202}
203
[8764]204
[5768]205/**
[8316]206 * @brief creates and exports an Image, that has all the characters
[5768]207 * stored in a Array (as an image)
208 * @param fileName the File to write the image into.
209 */
[8765]210void Font::createAsciiImage(const std::string& ttfFile, const std::string& fileName, unsigned int size)
[5343]211{
[8765]212  TTF_Font* fontTTF = TTF_OpenFont(ttfFile.c_str(), size);
213
214  if (fontTTF == NULL)
[5343]215    return;
[8765]216  int height = TTF_FontHeight(fontTTF);
[5343]217
218  //
219  // Surface definition.
220  SDL_Rect tmpRect; // this represents a Rectangle for blitting.
221  SDL_Surface* tmpSurf =  SDL_CreateRGBSurface(SDL_SWSURFACE,
[7221]222                          height*size, height*size,
223                          32,
[5343]224#if SDL_BYTEORDER == SDL_LIL_ENDIAN /* OpenGL RGBA masks */
[7221]225                          0x000000FF,
226                          0x0000FF00,
227                          0x00FF0000,
228                          0xFF000000
[5343]229#else
[7221]230                          0xFF000000,
231                          0x00FF0000,
232                          0x0000FF00,
233                          0x000000FF
[5343]234#endif
235                                              );
236  tmpRect.x = 0; tmpRect.y = 0; tmpRect.w = tmpSurf->w; tmpRect.h = tmpSurf->h;
237  SDL_SetClipRect(tmpSurf, &tmpRect);
238
239  int posX, posY;
240  // all the interessting Glyphs
241  for (posY = 0; posY < 16; posY++)
242  {
243    for (posX = 0; posX < 16; posX++)
244    {
245      SDL_Surface* glyphSurf = NULL;
[8765]246      SDL_Color white = {255, 255, 255};
247      glyphSurf = TTF_RenderGlyph_Blended(fontTTF, posX+size*posY, white);
248
[5343]249      if( glyphSurf != NULL )
250      {
251        tmpRect.x = height*posX;
252        tmpRect.y = height*posY;
253        SDL_SetAlpha(glyphSurf, 0, 0);
254
255        SDL_BlitSurface(glyphSurf, NULL, tmpSurf, &tmpRect);
256        SDL_FreeSurface(glyphSurf);
257      }
258    }
259  }
[7221]260  SDL_SaveBMP(tmpSurf, fileName.c_str());
[5343]261  SDL_FreeSurface(tmpSurf);
[8765]262
263  TTF_CloseFont(fontTTF);
[5343]264}
265
266
267
268
269/**
[8316]270 * @brief a simple function to get some interesting information about this class
[5343]271 */
[8761]272void Font::debug() const
[5343]273{
[8761]274  Material::debug();
275
[8989]276  //PRINT(0)("TEST %p and %p\n", this->data.get(), this->data->textureData().get());
[5343]277  // print the loaded font's style
[8765]278/*  int style = TTF_STYLE_NORMAL;
[8751]279  if (likely(this->data->fontTTF != NULL))
280    style = TTF_GetFontStyle(this->data->fontTTF);
[5343]281  PRINTF(0)("The font style is:");
282  if(style==TTF_STYLE_NORMAL)
283    PRINTF(0)(" normal");
[7221]284  else
285  {
[5343]286    if(style&TTF_STYLE_BOLD)
287      PRINTF(0)(" bold");
288    if(style&TTF_STYLE_ITALIC)
289      PRINTF(0)(" italic");
290    if(style&TTF_STYLE_UNDERLINE)
[8765]291      PRINTF(0)(" underline");*/
292//  }
[5343]293  PRINTF(0)("\n");
294}
Note: See TracBrowser for help on using the repository browser.