From 1fea15ac7bb4533bc2e6a90b5a9661ce56430bb0 Mon Sep 17 00:00:00 2001 From: Eugene Sandulenko Date: Tue, 17 Jan 2017 18:44:27 +0100 Subject: GRAPHICS: Implementing kerning calculation for MacFonts --- graphics/fonts/macfont.cpp | 86 ++++++++++++++++++++++++++++++++-------------- graphics/fonts/macfont.h | 27 ++++++++++----- 2 files changed, 80 insertions(+), 33 deletions(-) diff --git a/graphics/fonts/macfont.cpp b/graphics/fonts/macfont.cpp index 0df5fa7aa0..92b68b4f6d 100644 --- a/graphics/fonts/macfont.cpp +++ b/graphics/fonts/macfont.cpp @@ -62,27 +62,6 @@ static int getDepth(uint16 _fontType) { return 1 << ((_fontType >> 2) & 3); } -MacFont::MacFont() { - _fontType = 0; - _firstChar = 0; - _lastChar = 0; - _maxWidth = 0; - _kernMax = 0; - _nDescent = 0; - _fRectWidth = 0; - _fRectHeight = 0; - _owTLoc = 0; - _ascent = 0; - _descent = 0; - _leading = 0; - _rowWords = 0; - _bitImage = nullptr; -} - -MacFont::~MacFont() { - free(_bitImage); -} - MacFontFamily::MacFontFamily() { _ffFlags = 0; _ffFamID = 0; @@ -209,9 +188,13 @@ bool MacFontFamily::load(Common::SeekableReadStream &stream) { debug(10, " style: %x kernpairs: %d", _ffKernEntries[i]._style, _ffKernEntries[i]._entryLength); for (uint j = 0; j < _ffKernEntries[i]._entryLength; j++) { - _ffKernEntries[i]._kernPairs[j]._firstChar = stream.readByte(); - _ffKernEntries[i]._kernPairs[j]._secondChar = stream.readByte(); - _ffKernEntries[i]._kernPairs[j]._distance = stream.readSint16BE(); + byte f, s; + int16 d; + f = _ffKernEntries[i]._kernPairs[j]._firstChar = stream.readByte(); + s = _ffKernEntries[i]._kernPairs[j]._secondChar = stream.readByte(); + d = _ffKernEntries[i]._kernPairs[j]._distance = stream.readSint16BE(); + + _ffKernEntries[i]._kernTable[(f << 8) | s] = d; } } } @@ -219,7 +202,49 @@ bool MacFontFamily::load(Common::SeekableReadStream &stream) { return true; } -bool MacFont::loadFont(Common::SeekableReadStream &stream) { + int MacFontFamily::getKerningOffset(uint style, int32 left, uint32 right) const { + uint16 idx = ((left & 0xff) << 8) | (right & 0xff); + + for (uint i = 0; i < _ffKernEntries.size(); i++) { + if (_ffKernEntries[i]._style == style) + if (_ffKernEntries[i]._kernTable.contains(idx)) + return _ffKernEntries[i]._kernTable[idx]; + } + + return 0; + } + + + MacFont::MacFont() { + _fontType = 0; + _firstChar = 0; + _lastChar = 0; + _maxWidth = 0; + _kernMax = 0; + _nDescent = 0; + _fRectWidth = 0; + _fRectHeight = 0; + _owTLoc = 0; + _ascent = 0; + _descent = 0; + _leading = 0; + _rowWords = 0; + _bitImage = nullptr; + + _family = nullptr; + _size = 12; + _style = 0; + } + + MacFont::~MacFont() { + free(_bitImage); + } + +bool MacFont::loadFont(Common::SeekableReadStream &stream, MacFontFamily *family, int size, int style) { + _family = family; + _size = size; + _style = style; + _fontType = stream.readUint16BE(); // font type _firstChar = stream.readUint16BE(); // character code of first glyph _lastChar = stream.readUint16BE(); // character code of last glyph @@ -360,4 +385,15 @@ const MacFont::Glyph *MacFont::findGlyph(uint32 c) const { return &_glyphs[c - _firstChar]; } +int MacFont::getKerningOffset(uint32 left, uint32 right) const { + if (_family) { + int kerning = _family->getKerningOffset(_style, left, right); + kerning *= _size; + + return (int)(kerning / (double)(1 << 12)); + } + + return 0; +} + } // End of namespace Graphics diff --git a/graphics/fonts/macfont.h b/graphics/fonts/macfont.h index 935fbb2e98..ca1eb72abf 100644 --- a/graphics/fonts/macfont.h +++ b/graphics/fonts/macfont.h @@ -24,6 +24,7 @@ #define GRAPHICS_FONTS_MACFONT_H #include "common/array.h" +#include "common/hashmap.h" #include "graphics/font.h" namespace Graphics { @@ -34,6 +35,15 @@ public: ~MacFontFamily(); bool load(Common::SeekableReadStream &stream); + int getKerningOffset(uint style, int32 left, uint32 right) const; + + struct AsscEntry { + uint16 _fontSize; + uint16 _fontStyle; + uint16 _fontID; + }; + + Common::Array *getAssocTable() { return &_ffAssocEntries; } private: // FOND @@ -52,12 +62,6 @@ private: uint16 _ffIntl[2]; uint16 _ffVersion; - struct AsscEntry { - uint16 _fontSize; - uint16 _fontStyle; - uint16 _fontID; - }; - uint16 _ffNumAssoc; Common::Array _ffAssocEntries; @@ -85,6 +89,8 @@ private: uint16 _style; uint16 _entryLength; Common::Array _kernPairs; + + Common::HashMap _kernTable; }; uint16 _ffNumKerns; @@ -104,10 +110,11 @@ public: virtual int getCharWidth(uint32 chr) const; virtual void drawChar(Surface *dst, uint32 chr, int x, int y, uint32 color) const; - bool loadFont(Common::SeekableReadStream &stream); + bool loadFont(Common::SeekableReadStream &stream, MacFontFamily *family = nullptr, int size = 12, int style = 0); + + virtual int getKerningOffset(uint32 left, uint32 right) const; private: - // FONT/NFNT uint16 _fontType; uint16 _firstChar; uint16 _lastChar; @@ -141,6 +148,10 @@ private: Common::Array _glyphs; Glyph _defaultChar; const Glyph *findGlyph(uint32 c) const; + + MacFontFamily *_family; + int _size; + int _style; }; } // End of namespace Graphics -- cgit v1.2.3