Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/branches/heathaze/src/lib/graphics/text_engine/font.cc @ 10127

Last change on this file since 10127 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
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
31ObjectListDefinition(Font);
32
33Font::Font()
34    : data(Font::defaultFontData)
35{
36  this->init();
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  this->setTexture(this->data->textureData());
121
122  return *this;
123};
124
125
126/**
127 * @brief initializes a Font (with default values)
128 */
129void Font::init()
130{
131  this->setBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
132
133  this->registerObject(this, Font::_objectList);
134  if (Font::defaultFontData.isNull())
135  {
136    Font::initDefaultFont();
137    this->data = Font::defaultFontData;
138  }
139}
140
141FontData::Pointer Font::defaultFontData(NULL);
142
143/**
144 * @brief initializes the default font
145 */
146void Font::initDefaultFont()
147{
148  // temporarily create a Font.
149  Font::defaultFontData = FontData::Pointer(new FontData);
150  // apply the Data.
151  Font::defaultFontData = Font(font_xpm).data;
152}
153
154
155/**
156 * @brief sets The Font.
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 */
160bool Font::loadFontFromTTF(const std::string& fontFile, unsigned int renderSize)
161{
162  this->data = FontData::Pointer (new FontData());
163  bool retVal = this->data->loadFontFromTTF(fontFile, renderSize);
164  if (!retVal)
165    this->data = Font::defaultFontData;
166
167  this->setTexture(this->data->textureData());
168  return retVal;
169}
170
171/**
172 * @brief loads a font From an XPM-array.
173 * @param xpmArray the array of the XPM to load the font from.
174 */
175bool Font::loadFontFromSDL_Surface(SDL_Surface* surface)
176{
177  this->data = FontData::Pointer (new FontData());
178  bool retVal = this->data->loadFontFromSDL_Surface(surface);
179  if (!retVal)
180    this->data = Font::defaultFontData;
181
182  this->setTexture(this->data->textureData());
183  return retVal;
184}
185
186
187/**
188 * @brief sets a specific data->renderStyle
189 * @param data->renderStyle the Style to render: a string (char-array) containing:
190 *   i: italic, b: bold, u, underline
191 */
192void Font::setStyle(const std::string& renderStyle)
193{
194  /// FIXME
195  //this->data->setStyle(renderStyle);
196}
197
198
199void Font::setTexture(const TextureData::Pointer& texDataPointer)
200{
201  this->setDiffuseMap(texDataPointer);
202}
203
204
205/**
206 * @brief creates and exports an Image, that has all the characters
207 * stored in a Array (as an image)
208 * @param fileName the File to write the image into.
209 */
210void Font::createAsciiImage(const std::string& ttfFile, const std::string& fileName, unsigned int size)
211{
212  TTF_Font* fontTTF = TTF_OpenFont(ttfFile.c_str(), size);
213
214  if (fontTTF == NULL)
215    return;
216  int height = TTF_FontHeight(fontTTF);
217
218  //
219  // Surface definition.
220  SDL_Rect tmpRect; // this represents a Rectangle for blitting.
221  SDL_Surface* tmpSurf =  SDL_CreateRGBSurface(SDL_SWSURFACE,
222                          height*size, height*size,
223                          32,
224#if SDL_BYTEORDER == SDL_LIL_ENDIAN /* OpenGL RGBA masks */
225                          0x000000FF,
226                          0x0000FF00,
227                          0x00FF0000,
228                          0xFF000000
229#else
230                          0xFF000000,
231                          0x00FF0000,
232                          0x0000FF00,
233                          0x000000FF
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;
246      SDL_Color white = {255, 255, 255};
247      glyphSurf = TTF_RenderGlyph_Blended(fontTTF, posX+size*posY, white);
248
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  }
260  SDL_SaveBMP(tmpSurf, fileName.c_str());
261  SDL_FreeSurface(tmpSurf);
262
263  TTF_CloseFont(fontTTF);
264}
265
266
267
268
269/**
270 * @brief a simple function to get some interesting information about this class
271 */
272void Font::debug() const
273{
274  Material::debug();
275
276  //PRINT(0)("TEST %p and %p\n", this->data.get(), this->data->textureData().get());
277  // print the loaded font's style
278/*  int style = TTF_STYLE_NORMAL;
279  if (likely(this->data->fontTTF != NULL))
280    style = TTF_GetFontStyle(this->data->fontTTF);
281  PRINTF(0)("The font style is:");
282  if(style==TTF_STYLE_NORMAL)
283    PRINTF(0)(" normal");
284  else
285  {
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)
291      PRINTF(0)(" underline");*/
292//  }
293  PRINTF(0)("\n");
294}
Note: See TracBrowser for help on using the repository browser.