Skip to content

Commit

Permalink
GRAPHICS: Get rid of _glyphSlots in TTFFont.
Browse files Browse the repository at this point in the history
  • Loading branch information
Johannes Schickel committed Nov 23, 2013
1 parent afa3f50 commit 13d470d
Showing 1 changed file with 26 additions and 13 deletions.
39 changes: 26 additions & 13 deletions graphics/fonts/ttf.cpp
Expand Up @@ -126,21 +126,20 @@ class TTFFont : public Font {
Surface image;
int xOffset, yOffset;
int advance;
FT_UInt slot;
};

bool cacheGlyph(Glyph &glyph, FT_UInt &slot, uint chr);
bool cacheGlyph(Glyph &glyph, uint chr);
typedef Common::HashMap<uint32, Glyph> GlyphCache;
GlyphCache _glyphs;

FT_UInt _glyphSlots[256];

bool _monochrome;
bool _hasKerning;
};

TTFFont::TTFFont()
: _initialized(false), _face(), _ttfFile(0), _size(0), _width(0), _height(0), _ascent(0),
_descent(0), _glyphs(), _glyphSlots(), _monochrome(false), _hasKerning(false) {
_descent(0), _glyphs(), _monochrome(false), _hasKerning(false) {
}

TTFFont::~TTFFont() {
Expand Down Expand Up @@ -214,17 +213,18 @@ bool TTFFont::load(Common::SeekableReadStream &stream, int size, uint dpi, bool
if (!mapping) {
// Load all ISO-8859-1 characters.
for (uint i = 0; i < 256; ++i) {
if (!cacheGlyph(_glyphs[i], _glyphSlots[i], i))
_glyphSlots[i] = 0;
if (!cacheGlyph(_glyphs[i], i)) {
_glyphs.erase(i);
}
}
} else {
for (uint i = 0; i < 256; ++i) {
const uint32 unicode = mapping[i] & 0x7FFFFFFF;
const bool isRequired = (mapping[i] & 0x80000000) != 0;
// Check whether loading an important glyph fails and error out if
// that is the case.
if (!cacheGlyph(_glyphs[i], _glyphSlots[i], unicode)) {
_glyphSlots[i] = 0;
if (!cacheGlyph(_glyphs[i], unicode)) {
_glyphs.erase(i);
if (isRequired)
return false;
}
Expand Down Expand Up @@ -255,12 +255,22 @@ int TTFFont::getKerningOffset(uint32 left, uint32 right) const {
if (!_hasKerning)
return 0;

if (left > 255 || right > 255) {
FT_UInt leftGlyph, rightGlyph;
GlyphCache::const_iterator glyphEntry;

glyphEntry = _glyphs.find(left);
if (glyphEntry != _glyphs.end()) {
leftGlyph = glyphEntry->_value.slot;
} else {
return 0;
}

FT_UInt leftGlyph = _glyphSlots[left];
FT_UInt rightGlyph = _glyphSlots[right];
glyphEntry = _glyphs.find(right);
if (glyphEntry != _glyphs.end()) {
rightGlyph = glyphEntry->_value.slot;
} else {
return 0;
}

if (!leftGlyph || !rightGlyph)
return 0;
Expand Down Expand Up @@ -380,11 +390,13 @@ void TTFFont::drawChar(Surface *dst, uint32 chr, int x, int y, uint32 color) con
}
}

bool TTFFont::cacheGlyph(Glyph &glyph, FT_UInt &slot, uint chr) {
slot = FT_Get_Char_Index(_face, chr);
bool TTFFont::cacheGlyph(Glyph &glyph, uint chr) {
FT_UInt slot = FT_Get_Char_Index(_face, chr);
if (!slot)
return false;

glyph.slot = slot;

// We use the light target and render mode to improve the looks of the
// glyphs. It is most noticable in FreeSansBold.ttf, where otherwise the
// 't' glyph looks like it is cut off on the right side.
Expand Down Expand Up @@ -460,6 +472,7 @@ bool TTFFont::cacheGlyph(Glyph &glyph, FT_UInt &slot, uint chr) {

default:
warning("TTFFont::cacheGlyph: Unsupported pixel mode %d", bitmap.pixel_mode);
glyph.image.free();
return false;
}

Expand Down

0 comments on commit 13d470d

Please sign in to comment.