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