1
0
mirror of https://github.com/vcmi/vcmi.git synced 2025-07-07 00:58:39 +02:00

- all fonts handling is now in new file, UIFramework/Fonts.cpp/h

- common base class for H3 bmp and ttf fonts
- replaced fonts.txt with fonts.json
This commit is contained in:
Ivan Savenko
2012-12-19 17:24:53 +00:00
parent 0e06ac15ad
commit b5ebf443fc
30 changed files with 1726 additions and 1917 deletions

View File

@ -11,6 +11,7 @@
const SDL_Color Colors::YELLOW = { 229, 215, 123, 0 };
const SDL_Color Colors::WHITE = { 255, 243, 222, 0 };
const SDL_Color Colors::METALLIC_GOLD = { 173, 142, 66, 0 };
const SDL_Color Colors::GREEN = { 0, 255, 0, 0 };
SDL_Surface * CSDL_Ext::newSurface(int w, int h, SDL_Surface * mod) //creates new surface, with flags/format same as in surface given
{
@ -76,311 +77,6 @@ void updateRect (SDL_Rect * rect, SDL_Surface * scr)
SDL_UpdateRect(scr,rect->x,rect->y,rect->w,rect->h);
}
void printAtMiddleWB(const std::string & text, int x, int y, TTF_Font * font, int charpr, SDL_Color kolor, SDL_Surface * dst)
{
std::vector<std::string> ws = CMessage::breakText(text,charpr);
std::vector<SDL_Surface*> wesu;
wesu.resize(ws.size());
for (size_t i=0; i < wesu.size(); ++i)
{
wesu[i]=TTF_RenderText_Blended(font,ws[i].c_str(),kolor);
}
int tox=0, toy=0;
for (size_t i=0; i < wesu.size(); ++i)
{
toy+=wesu[i]->h;
if (tox < wesu[i]->w)
tox=wesu[i]->w;
}
int evx, evy = y - (toy/2);
for (size_t i=0; i < wesu.size(); ++i)
{
evx = (x - (tox/2)) + ((tox-wesu[i]->w)/2);
blitAt(wesu[i],evx,evy,dst);
evy+=wesu[i]->h;
}
for (size_t i=0; i < wesu.size(); ++i)
SDL_FreeSurface(wesu[i]);
}
void printAtWB(const std::string & text, int x, int y, TTF_Font * font, int charpr, SDL_Color kolor, SDL_Surface * dst)
{
std::vector<std::string> ws = CMessage::breakText(text,charpr);
std::vector<SDL_Surface*> wesu;
wesu.resize(ws.size());
for (size_t i=0; i < wesu.size(); ++i)
wesu[i]=TTF_RenderText_Blended(font,ws[i].c_str(),kolor);
int evy = y;
for (size_t i=0; i < wesu.size(); ++i)
{
blitAt(wesu[i],x,evy,dst);
evy+=wesu[i]->h;
}
for (size_t i=0; i < wesu.size(); ++i)
SDL_FreeSurface(wesu[i]);
}
void CSDL_Ext::printAtWB(const std::string & text, int x, int y, EFonts font, int charpr, SDL_Color kolor, SDL_Surface * dst)
{
if (graphics->fontsTrueType[font])
{
printAtWB(text,x, y, graphics->fontsTrueType[font], charpr, kolor, dst);
return;
}
const Font *f = graphics->fonts[font];
std::vector<std::string> ws = CMessage::breakText(text,charpr);
int cury = y;
for (size_t i=0; i < ws.size(); ++i)
{
printAt(ws[i], x, cury, font, kolor, dst);
cury += f->height;
}
}
void CSDL_Ext::printAtMiddleWB( const std::string & text, int x, int y, EFonts font, int charpr, SDL_Color kolor/*=Colors::YELLOW*/, SDL_Surface * dst/*=screen*/ )
{
if (graphics->fontsTrueType[font])
{
printAtMiddleWB(text,x, y, graphics->fontsTrueType[font], charpr, kolor, dst);
return;
}
const Font *f = graphics->fonts[font];
std::vector<std::string> ws = CMessage::breakText(text,charpr);
int totalHeight = ws.size() * f->height;
int cury = y - totalHeight/2;
for (size_t i=0; i < ws.size(); ++i)
{
printAt(ws[i], x - f->getWidth(ws[i].c_str())/2, cury, font, kolor, dst);
cury += f->height;
}
}
void printAtMiddle(const std::string & text, int x, int y, TTF_Font * font, SDL_Color kolor, SDL_Surface * dst, ui8 quality=2)
{
if(text.length()==0) return;
SDL_Surface * temp;
switch (quality)
{
case 0:
temp = TTF_RenderText_Solid(font,text.c_str(),kolor);
break;
case 1:
SDL_Color tem;
tem.b = 0xff-kolor.b;
tem.g = 0xff-kolor.g;
tem.r = 0xff-kolor.r;
tem.unused = 0xff-kolor.unused;
temp = TTF_RenderText_Shaded(font,text.c_str(),kolor,tem);
break;
case 2:
temp = TTF_RenderText_Blended(font,text.c_str(),kolor);
break;
default:
temp = TTF_RenderText_Blended(font,text.c_str(),kolor);
break;
}
SDL_Rect dstRect = genRect(temp->h, temp->w, x-(temp->w/2), y-(temp->h/2));
CSDL_Ext::blitSurface(temp, NULL, dst, &dstRect);
SDL_FreeSurface(temp);
}
void CSDL_Ext::printAtMiddle( const std::string & text, int x, int y, EFonts font, SDL_Color kolor/*=Colors::WHITE*/, SDL_Surface * dst/*=screen*/ )
{
if (graphics->fontsTrueType[font])
{
printAtMiddle(text,x, y, graphics->fontsTrueType[font], kolor, dst);
return;
}
const Font *f = graphics->fonts[font];
int nx = x - f->getWidth(text.c_str())/2,
ny = y - f->height/2;
printAt(text, nx, ny, font, kolor, dst);
}
void printAt(const std::string & text, int x, int y, TTF_Font * font, SDL_Color kolor, SDL_Surface * dst, ui8 quality=2, bool refresh=false)
{
if (text.length()==0)
return;
SDL_Surface * temp;
switch (quality)
{
case 0:
temp = TTF_RenderText_Solid(font,text.c_str(),kolor);
break;
case 1:
SDL_Color tem;
tem.b = 0xff-kolor.b;
tem.g = 0xff-kolor.g;
tem.r = 0xff-kolor.r;
tem.unused = 0xff-kolor.unused;
temp = TTF_RenderText_Shaded(font,text.c_str(),kolor,tem);
break;
case 2:
temp = TTF_RenderText_Blended(font,text.c_str(),kolor);
break;
default:
temp = TTF_RenderText_Blended(font,text.c_str(),kolor);
break;
}
SDL_Rect dstRect = genRect(temp->h,temp->w,x,y);
CSDL_Ext::blitSurface(temp,NULL,dst,&dstRect);
if(refresh)
SDL_UpdateRect(dst,x,y,temp->w,temp->h);
SDL_FreeSurface(temp);
}
void CSDL_Ext::printAt( const std::string & text, int dstX, int dstY, EFonts font, SDL_Color color, SDL_Surface * dst)
{
if(!text.size())
return;
if (graphics->fontsTrueType[font])
{
printAt(text,dstX, dstY, graphics->fontsTrueType[font], color, dst);
return;
}
assert(dst);
assert(font < Graphics::FONTS_NUMBER);
Rect clipRect;
SDL_GetClipRect(dst, &clipRect);
const Font *f = graphics->fonts[font];
const Uint8 bpp = dst->format->BytesPerPixel;
TColorPutter colorPutter = getPutterFor(dst, 0);
//if text is in {} braces, we'll ommit them
const int textBegin = (text[0] == '{' ? 1 : 0);
const int textEnd = (text[text.size()-1] == '}' ? text.size()-1 : text.size());
SDL_LockSurface(dst);
// for each symbol
for(int index = textBegin; index < textEnd; index++)
{
const ui8 symbol = text[index];
dstX += f->chars[symbol].leftOffset;
int lineBegin = std::max<int>(0, clipRect.y - dstY);
int lineEnd = std::min<int>(f->height, clipRect.y + clipRect.h - dstY - 1);
int rowBegin = std::max(0, clipRect.x - dstX);
int rowEnd = std::min(f->chars[symbol].width, clipRect.x + clipRect.w - dstX - 1);
//for each line in symbol
for(int dy = lineBegin; dy <lineEnd; dy++)
{
Uint8 *dstLine = (Uint8*)dst->pixels;
Uint8 *srcLine = f->chars[symbol].pixels;
dstLine += (dstY+dy) * dst->pitch + dstX * bpp;
srcLine += dy * f->chars[symbol].width;
//for each column in line
for(int dx = rowBegin; dx < rowEnd; dx++)
{
Uint8* dstPixel = dstLine + dx*bpp;
switch(*(srcLine + dx))
{
case 1: //black "shadow"
memset(dstPixel, 0, bpp);
break;
case 255: //text colour
colorPutter(dstPixel, color.r, color.g, color.b);
break;
}
}
}
dstX += f->chars[symbol].width;
dstX += f->chars[symbol].rightOffset;
}
SDL_UnlockSurface(dst);
}
void printTo(const std::string & text, int x, int y, TTF_Font * font, SDL_Color kolor, SDL_Surface * dst, ui8 quality=2)
{
if (text.length()==0)
return;
SDL_Surface * temp;
switch (quality)
{
case 0:
temp = TTF_RenderText_Solid(font,text.c_str(),kolor);
break;
case 1:
SDL_Color tem;
tem.b = 0xff-kolor.b;
tem.g = 0xff-kolor.g;
tem.r = 0xff-kolor.r;
tem.unused = 0xff-kolor.unused;
temp = TTF_RenderText_Shaded(font,text.c_str(),kolor,tem);
break;
case 2:
temp = TTF_RenderText_Blended(font,text.c_str(),kolor);
break;
default:
temp = TTF_RenderText_Blended(font,text.c_str(),kolor);
break;
}
SDL_Rect dstRect = genRect(temp->h,temp->w,x-temp->w,y-temp->h);
CSDL_Ext::blitSurface(temp,NULL,dst,&dstRect);
SDL_UpdateRect(dst,x-temp->w,y-temp->h,temp->w,temp->h);
SDL_FreeSurface(temp);
}
void CSDL_Ext::printTo( const std::string & text, int x, int y, EFonts font, SDL_Color kolor/*=Colors::WHITE*/, SDL_Surface * dst/*=screen*/ )
{
if (graphics->fontsTrueType[font])
{
printTo(text,x, y, graphics->fontsTrueType[font], kolor, dst);
return;
}
const Font *f = graphics->fonts[font];
printAt(text, x - f->getWidth(text.c_str()), y - f->height, font, kolor, dst);
}
void printToWR(const std::string & text, int x, int y, TTF_Font * font, SDL_Color kolor, SDL_Surface * dst, ui8 quality=2)
{
if (text.length()==0)
return;
SDL_Surface * temp;
switch (quality)
{
case 0:
temp = TTF_RenderText_Solid(font,text.c_str(),kolor);
break;
case 1:
SDL_Color tem;
tem.b = 0xff-kolor.b;
tem.g = 0xff-kolor.g;
tem.r = 0xff-kolor.r;
tem.unused = 0xff-kolor.unused;
temp = TTF_RenderText_Shaded(font,text.c_str(),kolor,tem);
break;
case 2:
temp = TTF_RenderText_Blended(font,text.c_str(),kolor);
break;
default:
temp = TTF_RenderText_Blended(font,text.c_str(),kolor);
break;
}
SDL_Rect dstRect = genRect(temp->h,temp->w,x-temp->w,y-temp->h);
CSDL_Ext::blitSurface(temp,NULL,dst,&dstRect);
SDL_FreeSurface(temp);
}
// Vertical flip
SDL_Surface * CSDL_Ext::rotate01(SDL_Surface * toRot)
{
@ -1282,23 +978,6 @@ void CSDL_Ext::fillTexture(SDL_Surface *dst, SDL_Surface * src)
}
}
std::string CSDL_Ext::trimToFit(std::string text, int widthLimit, EFonts font)
{
int widthSoFar = 0;
for(auto i = text.begin(); i != text.end(); i++)
{
widthSoFar += graphics->fonts[font]->getCharWidth(*i);
if(widthSoFar > widthLimit)
{
//remove all characteres past limit
text.erase(i, text.end());
break;
}
}
return text;
}
template SDL_Surface * CSDL_Ext::createSurfaceWithBpp<2>(int, int);
template SDL_Surface * CSDL_Ext::createSurfaceWithBpp<3>(int, int);
template SDL_Surface * CSDL_Ext::createSurfaceWithBpp<4>(int, int);