| 1 |
/* |
| 2 |
* Copyright (C) 2004 Ivo Danihelka ([email protected]) |
| 3 |
* |
| 4 |
* This program is free software; you can redistribute it and/or modify |
| 5 |
* it under the terms of the GNU General Public License as published by |
| 6 |
* the Free Software Foundation; either version 2 of the License, or |
| 7 |
* (at your option) any later version. |
| 8 |
*/ |
| 9 |
#include "Font.h" |
| 10 |
|
| 11 |
#include "Log.h" |
| 12 |
#include "Path.h" |
| 13 |
#include "TTFException.h" |
| 14 |
#include "SDLException.h" |
| 15 |
#include "Outline.h" |
| 16 |
|
| 17 |
#ifdef HAVE_FRIBIDI |
| 18 |
#include "fribidi.h" |
| 19 |
#endif |
| 20 |
|
| 21 |
std::string |
| 22 |
Font::biditize(const std::string &text) |
| 23 |
{ |
| 24 |
#ifdef HAVE_FRIBIDI |
| 25 |
FriBidiCharType base = FRIBIDI_TYPE_ON; |
| 26 |
FriBidiChar *logicalString = new FriBidiChar[text.length() + 1]; |
| 27 |
FriBidiChar *visualString = new FriBidiChar[text.length() + 1]; |
| 28 |
|
| 29 |
int ucsLength = fribidi_charset_to_unicode(FRIBIDI_CHAR_SET_UTF8, |
| 30 |
const_cast<char*>(text.c_str()), |
| 31 |
text.length(), logicalString); |
| 32 |
fribidi_boolean ok = fribidi_log2vis(logicalString, ucsLength, &base, |
| 33 |
visualString, NULL, NULL, NULL); |
| 34 |
if (!ok) { |
| 35 |
LOG_WARNING(ExInfo("cannot biditize text") |
| 36 |
.addInfo("text", text)); |
| 37 |
return text; |
| 38 |
} |
| 39 |
|
| 40 |
char *buffer = new char[text.length() + 1]; |
| 41 |
int length = fribidi_unicode_to_charset(FRIBIDI_CHAR_SET_UTF8, |
| 42 |
visualString, ucsLength, buffer); |
| 43 |
std::string result = std::string(buffer, length); |
| 44 |
delete[] buffer; |
| 45 |
delete[] visualString; |
| 46 |
delete[] logicalString; |
| 47 |
return result; |
| 48 |
#else |
| 49 |
return text; |
| 50 |
#endif |
| 51 |
} |
| 52 |
|
| 53 |
//----------------------------------------------------------------- |
| 54 |
/** |
| 55 |
* Create new font from file. |
| 56 |
* Initialized TTF_Init when necessary. |
| 57 |
* @param file_ttf path to ttf file |
| 58 |
* @param height font height |
| 59 |
* @throws TTFException when cannot open font |
| 60 |
*/ |
| 61 |
Font::Font(const Path &file_ttf, int height) |
| 62 |
{ |
| 63 |
m_ttfont = TTF_OpenFont(file_ttf.getNative().c_str(), height); |
| 64 |
if (!m_ttfont) { |
| 65 |
throw TTFException(ExInfo("OpenFont") |
| 66 |
.addInfo("file", file_ttf.getNative())); |
| 67 |
} |
| 68 |
|
| 69 |
//NOTE: bg color will be set to be transparent |
| 70 |
SDL_Color bg = {10, 10, 10, 0}; |
| 71 |
m_bg = bg; |
| 72 |
} |
| 73 |
//----------------------------------------------------------------- |
| 74 |
Font::~Font() |
| 75 |
{ |
| 76 |
TTF_CloseFont(m_ttfont); |
| 77 |
} |
| 78 |
//----------------------------------------------------------------- |
| 79 |
/** |
| 80 |
* Prepare font rendering. |
| 81 |
* @throws TTFException when cannot init SDL_ttf. |
| 82 |
*/ |
| 83 |
void |
| 84 |
Font::init() |
| 85 |
{ |
| 86 |
if (TTF_Init() < 0) { |
| 87 |
throw TTFException(ExInfo("Init")); |
| 88 |
} |
| 89 |
} |
| 90 |
//----------------------------------------------------------------- |
| 91 |
/** |
| 92 |
* Deinit font subsystem. |
| 93 |
*/ |
| 94 |
void |
| 95 |
Font::shutdown() |
| 96 |
{ |
| 97 |
TTF_Quit(); |
| 98 |
} |
| 99 |
|
| 100 |
//----------------------------------------------------------------- |
| 101 |
int |
| 102 |
Font::calcTextWidth(const std::string &text) |
| 103 |
{ |
| 104 |
int w; |
| 105 |
TTF_SizeUTF8(m_ttfont, text.c_str(), &w, NULL); |
| 106 |
return w; |
| 107 |
} |
| 108 |
//----------------------------------------------------------------- |
| 109 |
/** |
| 110 |
* Render text with this color. |
| 111 |
* @param text utf-8 encoded text |
| 112 |
* @param color text color |
| 113 |
* @return new rendered surface |
| 114 |
* @throws TTFException when render fails |
| 115 |
* @throws SDLException when converting fails |
| 116 |
*/ |
| 117 |
SDL_Surface * |
| 118 |
Font::renderText(const std::string &text, const SDL_Color &color) const |
| 119 |
{ |
| 120 |
std::string content = biditize(text); |
| 121 |
if (text.empty()) { |
| 122 |
content = " "; |
| 123 |
LOG_WARNING(ExInfo("empty text to render") |
| 124 |
.addInfo("r", color.r) |
| 125 |
.addInfo("g", color.g) |
| 126 |
.addInfo("b", color.b)); |
| 127 |
} |
| 128 |
|
| 129 |
SDL_Surface *raw_surface = TTF_RenderUTF8_Shaded(m_ttfont, content.c_str(), |
| 130 |
color, m_bg); |
| 131 |
if (!raw_surface) { |
| 132 |
throw TTFException(ExInfo("RenderUTF8") |
| 133 |
.addInfo("text", text)); |
| 134 |
} |
| 135 |
|
| 136 |
//NOTE: at index 0 is bg color |
| 137 |
if (SDL_SetColorKey(raw_surface, SDL_SRCCOLORKEY, 0) < 0) { |
| 138 |
throw SDLException(ExInfo("SetColorKey")); |
| 139 |
} |
| 140 |
|
| 141 |
SDL_Surface *surface = SDL_DisplayFormat(raw_surface); |
| 142 |
if (!surface) { |
| 143 |
throw SDLException(ExInfo("DisplayFormat")); |
| 144 |
} |
| 145 |
SDL_FreeSurface(raw_surface); |
| 146 |
|
| 147 |
return surface; |
| 148 |
} |
| 149 |
//----------------------------------------------------------------- |
| 150 |
/** |
| 151 |
* Render text with black outline around font. |
| 152 |
* @param text utf-8 encoded text |
| 153 |
* @param color text color |
| 154 |
* @param outlineWidth outline width |
| 155 |
* @return new rendered surface |
| 156 |
*/ |
| 157 |
SDL_Surface * |
| 158 |
Font::renderTextOutlined(const std::string &text, |
| 159 |
const SDL_Color &color, int outlineWidth) const |
| 160 |
{ |
| 161 |
static const SDL_Color BLACK = {0, 0, 0, 255}; |
| 162 |
//NOTE: uses spaces to ensure space for outline |
| 163 |
SDL_Surface *surface = renderText(" " + text + " ", color); |
| 164 |
Outline outline(BLACK, outlineWidth); |
| 165 |
|
| 166 |
outline.drawOnColorKey(surface); |
| 167 |
return surface; |
| 168 |
} |
| 169 |
|