Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/branches/proxy/src/lib/graphics/text_engine/font.cc @ 9349

Last change on this file since 9349 was 8989, checked in by bensch, 18 years ago

orxonox/trunk: notifier notifies

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