Skip to content

Commit

Permalink
Improved text vertical alignment
Browse files Browse the repository at this point in the history
  • Loading branch information
juanjp600 committed Apr 28, 2020
1 parent 4de773a commit 443f302
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 19 deletions.
38 changes: 19 additions & 19 deletions gxruntime/gxfont.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,19 @@ gxFont::gxFont(FT_Library ftLibrary, gxGraphics *gfx, const std::string& fn, int
glyphData.clear();
atlases.clear();

renderAtlas(0);
glyphHeight = height;
renderAtlas('T');
std::map<int,GlyphData>::iterator it = glyphData.find('T');
if (it!=glyphData.end()) {
const GlyphData& gd = it->second;

glyphHeight = gd.srcRect[3];
glyphRenderOffset = -gd.drawOffset[1];
}

tCanvasHeight = (glyphHeight*16)/10;
glyphRenderBaseline = (glyphHeight*2/10);
glyphRenderOffset += glyphRenderBaseline;

tempCanvas = nullptr;
}
Expand Down Expand Up @@ -155,13 +167,13 @@ void gxFont::render(gxCanvas *dest,unsigned color_argb,int x,int y,const std::st
int width=stringWidth( text );
if( tempCanvas == nullptr || width>tempCanvas->getWidth() ){
graphics->freeCanvas( tempCanvas );
tempCanvas=graphics->createCanvas( width,height*16/10,0 );
tempCanvas=graphics->createCanvas( width,tCanvasHeight,0 );
tempCanvas->setMask(transparentPixel);
}

if( (color_argb&0xffffff)==transparentPixel ) { color_argb++; }
tempCanvas->setColor( transparentPixel );
tempCanvas->rect( 0,0,width,height*16/10,true );
tempCanvas->rect( 0,0,width,tCanvasHeight,true );
tempCanvas->setColor( color_argb );

int t_x = 0;
Expand All @@ -179,27 +191,15 @@ void gxFont::render(gxCanvas *dest,unsigned color_argb,int x,int y,const std::st
const GlyphData& gd = it->second;

if (gd.atlasIndex>=0) {
tempCanvas->rect( t_x - gd.drawOffset[0],(height*4/10)-gd.drawOffset[1],gd.srcRect[2],gd.srcRect[3],true );
tempCanvas->blit( t_x - gd.drawOffset[0],(height*4/10)-gd.drawOffset[1],atlases[gd.atlasIndex],gd.srcRect[0],gd.srcRect[1],gd.srcRect[2],gd.srcRect[3],false );
tempCanvas->rect( t_x - gd.drawOffset[0],glyphRenderBaseline-gd.drawOffset[1],gd.srcRect[2],gd.srcRect[3],true );
tempCanvas->blit( t_x - gd.drawOffset[0],glyphRenderBaseline-gd.drawOffset[1],atlases[gd.atlasIndex],gd.srcRect[0],gd.srcRect[1],gd.srcRect[2],gd.srcRect[3],false );
}
t_x += gd.horizontalAdvance;
}
i+=codepointLen;
}

std::map<int,GlyphData>::iterator it = glyphData.find('T');
if (it==glyphData.end()) {
renderAtlas('T');
it = glyphData.find('T');
}

if (it!=glyphData.end()) {
const GlyphData& gd = it->second;

y -= (height*4/10)-gd.drawOffset[1];
}

dest->blit( x,y,tempCanvas,0,0,width,height*16/10,false );
dest->blit( x,y,tempCanvas,0,0,width,tCanvasHeight,false );
}

int gxFont::charWidth(int chr) {
Expand Down Expand Up @@ -244,7 +244,7 @@ int gxFont::getWidth()const {
}

int gxFont::getHeight()const {
return height;
return glyphHeight+glyphRenderOffset*2;
}

int gxFont::getWidth( const std::string &text ) {
Expand Down
4 changes: 4 additions & 0 deletions gxruntime/gxfont.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,10 @@ class gxFont {
gxFont();

int maxWidth = 0;
int glyphHeight = 0;
int tCanvasHeight = 0;
int glyphRenderBaseline = 0;
int glyphRenderOffset = 0;

struct GlyphData {
int atlasIndex;
Expand Down

0 comments on commit 443f302

Please sign in to comment.