1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 |
#include "TextImage.h"
//! Initialisation des membres statiques
FT_Library TextImage::mLibrary = NULL;
u32 TextImage::mNbInstances = 0;
IVideoDriver* TextImage::mDriver=NULL;
//! constructeur
//! ATTENTION : Il faut absolument donner un pointeur sur le video
//! driver avant toute création d'images. Cela peut être fait avec
//! setVideoDriver().
TextImage::TextImage(IVideoDriver *driver)
: mText(""), mUTF8Text(true), mImage(NULL), mFont(NULL), mTextColor(SColor(0))
{
// on copie le pointeur sur IVideoDriver
if(driver) mDriver = driver;
}
TextImage::TextImage(stringw text, stringc pathfont, u32 fontsize, SColor textcolor, bool isUTF8text)
: mText(text), mUTF8Text(isUTF8text), mImage(NULL), mFont(NULL), mTextColor(textcolor)
{
// Si c'est la première instance, on initialise la librairie.
if(!mNbInstances) FT_Init_FreeType(&mLibrary);
// Une instance est créé donc on incrémente le compteur.
mNbInstances++;
// Si un texte est valide
if(text.size())
{
// on enregistre le texte
mText = text;
// Si un pathfont est présent
if(pathfont.size())
{
// chargement de la font
FT_New_Face( mLibrary, pathfont.c_str(), 0, &mFont );
// taille de police
if(mFont) FT_Set_Char_Size(mFont, fontsize*64, 0, 96, 0);
}
}
}
//! destructeur
TextImage::~TextImage()
{
// détruit les ressources
if(mImage) mImage->drop();
// On détruit une instance donc on décrémente le compteur.
mNbInstances--;
// S'il n'y a plus d'instance en cours
if(!mNbInstances)
{
// on libère la librairie.
FT_Done_FreeType(mLibrary);
// on libère la font si elle existe
if(mFont) FT_Done_Face(mFont);
}
}
/** Accesseurs **/
//! modifie le texte
void TextImage::setText(stringw text, bool isUTF8text)
{
mText = text;
mUTF8Text = isUTF8Text();
}
//! renvoie le texte
stringw TextImage::getText()
{
return mText;
}
//! renvoie true si le texte à été renseigné comme étant au format UTF-8
bool TextImage::isUTF8Text()
{
return mUTF8Text;
}
//! demande la création d'une nouvelle font
void TextImage::setFont(stringc pathfont, u32 fontsize)
{
// création de la nouvelle font
FT_Face newfont = NULL;
FT_New_Face( mLibrary, pathfont.c_str(), 0, &newfont);
// si la creation à réussie
if(newfont)
{
// on détruit l'ancienne font
FT_Done_Face(mFont);
// on applique la nouvelle
mFont = newfont;
}
}
//! demande la création d'une nouvelle font et change la couleur
void TextImage::setFontAndColor(stringc pathfont, u32 fontsize, SColor textcolor)
{
mTextColor = textcolor;
setFont(pathfont, fontsize);
}
//! renvoie la taille de la police actuelle (0 si pas de font)
u32 TextImage::getFontSize()
{
if(mFont) return (mFont->size->metrics.height/96);
else return 0;
}
//! modifie la couleur du texte
void TextImage::setTextColor(SColor textcolor)
{
mTextColor = textcolor;
}
//! renvoie la couleur du texte
SColor TextImage::getTextColor()
{
return mTextColor;
}
//! modifie tout les paramètres
void TextImage::setAll(stringw text, stringc pathfont, u32 fontsize, SColor textcolor, bool isUTF8text)
{
mText = text;
setFont(pathfont, fontsize);
mTextColor = textcolor;
mUTF8Text = isUTF8Text();
}
//! renvoie l'image originale
IImage* TextImage::getImage()
{
return mImage;
}
//! renvoie une copie de l'image
IImage* TextImage::getCopyImage()
{
IImage *copy = NULL;
if(mDriver)
{
// création de l'image "copie"
copy = mDriver->createImage(mImage->getColorFormat(), mImage->getDimension());
// copie l'originale dans la "copie"
mImage->copyTo(copy);
}
// renvoie un pointeur sur la copie
return copy;
}
//! donne un pointeur sur le videodriver à la classe
void TextImage::setVideoDriver(IVideoDriver *driver)
{
mDriver = driver;
}
/** création graphique **/
//! (1 LIGNE) créé l'image contenant le texte
void TextImage::createImageFromTextLine()
{
if(mDriver && mFont && mText.size())
{
u32 hauteurimage = 1.5*getFontSize();
// créé une image temporaire d'une longueur de 6000px x taille de police
IImage *tmp = mDriver->createImage(ECF_A8R8G8B8, dimension2du(6000, hauteurimage));
if(tmp)
{
/// vide l'image
///tmp->fill(SColor(0));
stringw text="";
// conversion du texte si besoin
if(mUTF8Text) text = stringUTF8toUNICODE(mText);
else text = mText;
// boucle de dessin des caractères
position2di position(0,0);
for(u32 a=0; a < text.size(); ++a) position.X = drawChar(tmp, text[a], position).X;
// recréé l'image finale
if(mImage) mImage->drop();
mImage = mDriver->createImage(ECF_A8R8G8B8, dimension2du(position.X, hauteurimage));
// Copie la zone de texte dans l'image finale
tmp->copyTo( mImage, position2di(0,0), recti(position2di(0,0), mImage->getDimension()) );
// libère l'image temporaire
tmp->drop();
}
}
}
//! Détruit l'image contenant le texte
void TextImage::destroyTextImage()
{
// si l'image existe
if(mImage)
{
// détruit l'image
mImage->drop();
// empêche un segfault
mImage = NULL;
}
}
/** IImage **/
//! (1 LIGNE) incruste le texte dans une image
//! Si l'image du texte n'a pas été créé avant, elle le fait.
void TextImage::blendTextLineOnImage(IImage *image, position2di position)
{
// si l'image du texte n'a pas encore été, on le fait
if(!mImage) createImageFromTextLine();
if(mImage && image)
{
// rectangle de l'image destinataire
recti zonedest(position2di(0,0), mDriver->getScreenSize());
// variables
SColor color, color1, color2;
s32 X, Y;
f32 coef1, coef2;
for(u32 y=0; y < mImage->getDimension().Height; y++)
for(u32 x=0; x < mImage->getDimension().Width; x++)
{
// calcul la position du pixel à l'écran
X = position.X + x;
Y = position.Y + y;
// récupère la couleur de l'image texte
color1 = mImage->getPixel(x,y);
// récupère la couleur de l'image destinataire
color2 = image->getPixel(X,Y);
// calcul des coef
coef1 = f32(color1.getAlpha())/255.f;
coef2 = 1.f-coef1;
// calcul de la couleur finale
color = SColor(color2.getAlpha(), color1.getRed()*coef1+color2.getRed()*coef2, color1.getGreen()*coef1+color2.getGreen()*coef2, color1.getBlue()*coef1+color2.getBlue()*coef2);
// si le pixel est dans l'écran
if( zonedest.isPointInside(position2di(X, Y)) ) image->setPixel(X,Y,color);
}
}
}
//! (1 LIGNE) créé une copie de l'image puis incruste le texte dans l'image copiée (permet de garder l'image originale intacte)
//! Si l'image du texte n'a pas été créé avant, elle le fait.
IImage* TextImage::blendTextLineOnCopiedImage(IImage *original, position2di position)
{
// si l'image du texte n'a pas encore été, on le fait
if(!mImage) createImageFromTextLine();
if(mImage && mDriver)
{
IImage *copy = NULL;
// création de l'image "copie"
copy = mDriver->createImage(original->getColorFormat(), original->getDimension());
// copie l'image originale dans la "copie"
original->copyTo(copy);
// blit le texte dans la copie
blendTextLineOnImage(copy, position);
// retourne un pointeur sur la "copie"
return copy;
}
// le texte n'a pas pu être créé
return NULL;
}
/** Ecran **/
//! (1 LIGNE) incruste le texte dans l'écran
//! Si l'image du texte n'a pas été créé avant, elle le fait.
void TextImage::blendTextLineOnScreen(position2di position)
{
// si l'image du texte n'a pas encore été, on le fait
if(!mImage) createImageFromTextLine();
if(mImage)
{
// rectangle de l'écran
recti zoneecran(position2di(0,0), mDriver->getScreenSize());
// variables
SColor color;
s32 X, Y;
for(u32 y=0; y < mImage->getDimension().Height; y++)
for(u32 x=0; x < mImage->getDimension().Width; x++)
{
// récupère la couleur
color = mImage->getPixel(x,y);
// calcul la position du pixel à l'écran
X = position.X + x;
Y = position.Y + y;
// si le pixel est dans l'écran
if(zoneecran.isPointInside(position2di(X, Y))) mDriver->drawPixel(X,Y,color);
}
}
}
//! convertit un stringw UTF-8 en stringw au format unicode
stringw TextImage::stringUTF8toUNICODE(stringw texte)
{
stringw texteUNICODE;
// pour chaque caractère
u32 cpt = 0;
while(cpt < texte.size())
{
// variables
wchar_t charUNICODE = 0;
u32 nb_octet = 1;
u8 mask = 0;
// récupère l'octet n°1
bool octetbin[8] = {0,0,0,0,0,0,0,0};
for(u32 bit=0; bit < 8; bit++)
{
u8 tmp = texte[cpt] >> bit;
octetbin[bit] = tmp&0x01;
}
// déduit le nombre d'octet pour le caractère en cours
if(octetbin[7] && octetbin[6])
{
if(octetbin[5])
{
if(octetbin[4]) nb_octet = 4;
else nb_octet = 3;
}
else nb_octet = 2;
}
// traitement du caractère suivant le nombre d'octet
u8 mot1tmp[4]={0,0,0,0}; u32 *mot1=NULL;
u8 mot2tmp[4]={0,0,0,0}; u32 *mot2=NULL;
u8 mot3tmp[4]={0,0,0,0}; u32 *mot3=NULL;
u8 mot4tmp[4]={0,0,0,0}; u32 *mot4=NULL;
switch(nb_octet)
{
default :
case 1:
// on déduit le caractère UNICODE
charUNICODE = texte[cpt];
break;
case 2:
// on mets l'octet 1 à la 2ème place avec le masque et un décalage de 2
mot1tmp[1] = texte[cpt]&0x3F;
mot1 = (u32*)mot1tmp;
*mot1 >>= 2;
// on passe à l'octet 2
cpt++;
// on mets l'octet 2 à la 1ère place avec le masque
mot2tmp[0] = texte[cpt]&0x3F;
mot2 = (u32*)mot2tmp;
// on combine les mots pour obtenir le caractère UNICODE
charUNICODE = *mot1|*mot2;
break;
case 3:
// on mets l'octet 1 à la 3ème place avec le masque et un décalage de 2
mot1tmp[2] = texte[cpt]&0x1F;
mot1 = (u32*)mot1tmp;
*mot1 >>= 4;
// on passe à l'octet 2
cpt++;
// on mets l'octet 2 à la 2ème place avec le masque
mot2tmp[1] = texte[cpt]&0x3F;
mot2 = (u32*)mot2tmp;
*mot2 >>= 2;
// on passe à l'octet 3
cpt++;
// on mets l'octet 2 à la 2ème place avec le masque
mot3tmp[0] = texte[cpt]&0x3F;
mot3 = (u32*)mot3tmp;
// on combine les mots pour obtenir le caractère UNICODE
charUNICODE = *mot1|*mot2|*mot3;
break;
case 4:
// on mets l'octet 1 à la 4ème place avec le masque et un décalage de 2
mot1tmp[3] = texte[cpt]&0x0F;
mot1 = (u32*)mot1tmp;
*mot1 >>= 6;
// on passe à l'octet 2
cpt++;
// on mets l'octet 2 à la 3ème place avec le masque
mot2tmp[2] = texte[cpt]&0x3F;
mot2 = (u32*)mot2tmp;
*mot2 >>= 4;
// on passe à l'octet 3
cpt++;
// on mets l'octet 3 à la 2ème place avec le masque
mot3tmp[1] = texte[cpt]&0x3F;
mot3 = (u32*)mot3tmp;
*mot3 >>= 2;
// on passe à l'octet 4
cpt++;
// on mets l'octet 4 à la 1ère place avec le masque
mot4tmp[0] = texte[cpt]&0x3F;
mot4 = (u32*)mot4tmp;
// on combine les mots pour obtenir le caractère UNICODE
charUNICODE = *mot1|*mot2|*mot3|*mot4;
break;
}
// insertion du caractère dans la liste
texteUNICODE += charUNICODE;
// on présente le caractère suivant
cpt++;
}
return texteUNICODE;
}
/** fonctions protégées **/
//! dessine un caractère (le caractère doit être au format UNICODE)
//! et renvoie la position du prochain caractère
//! ATTENTION : la position sur Y est modifié lors d'un saut de ligne '\
'
position2di TextImage::drawChar(IImage *dest, wchar_t caractere, position2di position)
{
// si c'est un espace
if(caractere == ' ') return position + position2di(0.3*getFontSize(), 0);
// si c'est un caractère "fonction"
if(caractere == '\
') return position + position2di(0, 1.4*getFontSize());
if(caractere == '\ ') return position + position2di(2*getFontSize(), 0);
// rendu du bitmap
FT_Load_Glyph( mFont, FT_Get_Char_Index(mFont, caractere), FT_LOAD_DEFAULT );
FT_Render_Glyph( mFont->glyph, FT_RENDER_MODE_NORMAL );
// raccourci vers le bitmap
FT_Bitmap* bitmap = &mFont->glyph->bitmap;
// taille du bitmap contenant le char
s32 x_max=bitmap->width, y_max=bitmap->rows;
// déclaration de variables
s32 Xpx, Ypx;
SColor color = getTextColor();
// rectangle de l'image
recti zoneimage(position2di(0,0), dest->getDimension());
// dessine le caractère
for(s32 j=0; j < y_max; j++)
for(s32 i=0; i < x_max; i++)
{
// position du pixel
Xpx=position.X+i;
Ypx=position.Y+j+getFontSize()-mFont->glyph->bitmap_top;
// si le pixel est dans l'image
if( zoneimage.isPointInside(position2di(Xpx, Ypx)) )
{
// alpha du pixel
color.setAlpha( bitmap->buffer[j * bitmap->width + i] );
// dessine le pixel
dest->setPixel(Xpx, Ypx, color);
}
}
// retourne la position du prochain caractère
return position + position2di(x_max+mFont->glyph->bitmap_left, 0);
}
//! renvoie la largeur d'un caractère (le caractère doit être au format UNICODE)
//! renvoie -1 si il rencontre un saut de ligne
s32 TextImage::getCharWidth(wchar_t caractere)
{
// si c'est un caractère de mise en forme
if(caractere == ' ') return s32(0.3*getFontSize());
if(caractere == '\
') return -1;
if(caractere == '\ ') return s32(2*getFontSize());
// rendu du bitmap
FT_Load_Glyph( mFont, FT_Get_Char_Index(mFont, caractere), FT_LOAD_DEFAULT );
FT_Render_Glyph( mFont->glyph, FT_RENDER_MODE_NORMAL );
// renvoie la taille du caractère
return s32(mFont->glyph->bitmap.width + mFont->glyph->bitmap_left);
}
//! renvoie la largeur d'une chaîne de caractères (la chaîne doit être au format UNICODE)
u32 TextImage::getStringWidth(stringw string, bool stop_if_return)
{
u32 width = 0;
// on cumule la taille de chaque caractère
for(u32 a=0; a < string.size(); ++a)
{
// récupère la taille du caractère
s32 taille = getCharWidth(string[a]);
// si c'est un caractère autre que retour chariot
if(taille > -1) width += taille;
// si c'est un retour chariot et que l'on doit s'arrêter de compter
else if(stop_if_return) break;
}
// retourne la largeur de la chaîne
return width;
}
|