This commit is contained in:
bkaradzic 2013-05-16 20:54:25 -07:00
parent 274a5f2321
commit 2ab5b6e166
3 changed files with 73 additions and 62 deletions

View File

@ -56,7 +56,7 @@ struct FTHolder
FT_Face face; FT_Face face;
}; };
class FontManager::TrueTypeFont class TrueTypeFont
{ {
public: public:
TrueTypeFont(); TrueTypeFont();
@ -84,15 +84,16 @@ public:
/// update the GlyphInfo according to the raster strategy /// update the GlyphInfo according to the raster strategy
/// @ remark buffer min size: glyphInfo.m_width * glyphInfo * height * sizeof(char) /// @ remark buffer min size: glyphInfo.m_width * glyphInfo * height * sizeof(char)
bool bakeGlyphDistance(CodePoint_t _codePoint, GlyphInfo& _outGlyphInfo, uint8_t* _outBuffer); bool bakeGlyphDistance(CodePoint_t _codePoint, GlyphInfo& _outGlyphInfo, uint8_t* _outBuffer);
private: private:
FTHolder* m_font; FTHolder* m_font;
}; };
FontManager::TrueTypeFont::TrueTypeFont() : m_font(NULL) TrueTypeFont::TrueTypeFont() : m_font(NULL)
{ {
} }
FontManager::TrueTypeFont::~TrueTypeFont() TrueTypeFont::~TrueTypeFont()
{ {
if (m_font != NULL) if (m_font != NULL)
{ {
@ -104,7 +105,7 @@ FontManager::TrueTypeFont::~TrueTypeFont()
} }
} }
bool FontManager::TrueTypeFont::init(const uint8_t* _buffer, uint32_t _bufferSize, int32_t _fontIndex, uint32_t _pixelHeight) bool TrueTypeFont::init(const uint8_t* _buffer, uint32_t _bufferSize, int32_t _fontIndex, uint32_t _pixelHeight)
{ {
BX_CHECK( (_bufferSize > 256 BX_CHECK( (_bufferSize > 256
&& _bufferSize < 100000000), "TrueType buffer size is suspicious"); && _bufferSize < 100000000), "TrueType buffer size is suspicious");
@ -163,7 +164,7 @@ bool FontManager::TrueTypeFont::init(const uint8_t* _buffer, uint32_t _bufferSiz
return true; return true;
} }
FontInfo FontManager::TrueTypeFont::getFontInfo() FontInfo TrueTypeFont::getFontInfo()
{ {
BX_CHECK(m_font != NULL, "TrueTypeFont not initialized"); BX_CHECK(m_font != NULL, "TrueTypeFont not initialized");
FTHolder* holder = (FTHolder*) m_font; FTHolder* holder = (FTHolder*) m_font;
@ -179,12 +180,12 @@ FontInfo FontManager::TrueTypeFont::getFontInfo()
outFontInfo.descender = metrics.descender / 64.0f; outFontInfo.descender = metrics.descender / 64.0f;
outFontInfo.lineGap = (metrics.height - metrics.ascender + metrics.descender) / 64.0f; outFontInfo.lineGap = (metrics.height - metrics.ascender + metrics.descender) / 64.0f;
outFontInfo.underline_position = FT_MulFix(holder->face->underline_position, metrics.y_scale) / 64.0f; outFontInfo.underlinePosition = FT_MulFix(holder->face->underline_position, metrics.y_scale) / 64.0f;
outFontInfo.underline_thickness = FT_MulFix(holder->face->underline_thickness, metrics.y_scale) / 64.0f; outFontInfo.underlineThickness = FT_MulFix(holder->face->underline_thickness, metrics.y_scale) / 64.0f;
return outFontInfo; return outFontInfo;
} }
bool FontManager::TrueTypeFont::bakeGlyphAlpha(CodePoint_t _codePoint, GlyphInfo& _glyphInfo, uint8_t* _outBuffer) bool TrueTypeFont::bakeGlyphAlpha(CodePoint_t _codePoint, GlyphInfo& _glyphInfo, uint8_t* _outBuffer)
{ {
BX_CHECK(m_font != NULL, "TrueTypeFont not initialized"); BX_CHECK(m_font != NULL, "TrueTypeFont not initialized");
FTHolder* holder = (FTHolder*) m_font; FTHolder* holder = (FTHolder*) m_font;
@ -238,7 +239,7 @@ bool FontManager::TrueTypeFont::bakeGlyphAlpha(CodePoint_t _codePoint, GlyphInfo
return true; return true;
} }
bool FontManager::TrueTypeFont::bakeGlyphSubpixel(CodePoint_t _codePoint, GlyphInfo& _glyphInfo, uint8_t* _outBuffer) bool TrueTypeFont::bakeGlyphSubpixel(CodePoint_t _codePoint, GlyphInfo& _glyphInfo, uint8_t* _outBuffer)
{ {
BX_CHECK(m_font != NULL, "TrueTypeFont not initialized"); BX_CHECK(m_font != NULL, "TrueTypeFont not initialized");
FTHolder* holder = (FTHolder*) m_font; FTHolder* holder = (FTHolder*) m_font;
@ -394,7 +395,7 @@ void make_distance_map(unsigned char* img, unsigned char* outImg, unsigned int w
free(inside); free(inside);
} }
bool FontManager::TrueTypeFont::bakeGlyphDistance(CodePoint_t _codePoint, GlyphInfo& _glyphInfo, uint8_t* _outBuffer) bool TrueTypeFont::bakeGlyphDistance(CodePoint_t _codePoint, GlyphInfo& _glyphInfo, uint8_t* _outBuffer)
{ {
BX_CHECK(m_font != NULL, "TrueTypeFont not initialized"); BX_CHECK(m_font != NULL, "TrueTypeFont not initialized");
FTHolder* holder = (FTHolder*) m_font; FTHolder* holder = (FTHolder*) m_font;
@ -500,7 +501,7 @@ struct FontManager::CachedFont
} }
FontInfo fontInfo; FontInfo fontInfo;
GlyphHash_t cachedGlyphs; GlyphHash_t cachedGlyphs;
FontManager::TrueTypeFont* trueTypeFont; TrueTypeFont* trueTypeFont;
// an handle to a master font in case of sub distance field font // an handle to a master font in case of sub distance field font
FontHandle masterFontHandle; FontHandle masterFontHandle;
int16_t padding; int16_t padding;
@ -673,8 +674,8 @@ FontHandle FontManager::createScaledFontToPixelSize(FontHandle _baseFontHandle,
newFontInfo.ascender = (newFontInfo.ascender * newFontInfo.scale); newFontInfo.ascender = (newFontInfo.ascender * newFontInfo.scale);
newFontInfo.descender = (newFontInfo.descender * newFontInfo.scale); newFontInfo.descender = (newFontInfo.descender * newFontInfo.scale);
newFontInfo.lineGap = (newFontInfo.lineGap * newFontInfo.scale); newFontInfo.lineGap = (newFontInfo.lineGap * newFontInfo.scale);
newFontInfo.underline_thickness = (newFontInfo.underline_thickness * newFontInfo.scale); newFontInfo.underlineThickness = (newFontInfo.underlineThickness * newFontInfo.scale);
newFontInfo.underline_position = (newFontInfo.underline_position * newFontInfo.scale); newFontInfo.underlinePosition = (newFontInfo.underlinePosition * newFontInfo.scale);
uint16_t fontIdx = m_fontHandles.alloc(); uint16_t fontIdx = m_fontHandles.alloc();
BX_CHECK(fontIdx != bx::HandleAlloc::invalid, "Invalid handle used"); BX_CHECK(fontIdx != bx::HandleAlloc::invalid, "Invalid handle used");

View File

@ -21,23 +21,23 @@ enum FontType
struct FontInfo struct FontInfo
{ {
//the font height in pixel /// The font height in pixel.
uint16_t pixelSize; uint16_t pixelSize;
/// Rendering type used for the font /// Rendering type used for the font.
int16_t fontType; int16_t fontType;
/// The pixel extents above the baseline in pixels (typically positive) /// The pixel extents above the baseline in pixels (typically positive).
float ascender; float ascender;
/// The extents below the baseline in pixels (typically negative) /// The extents below the baseline in pixels (typically negative).
float descender; float descender;
/// The spacing in pixels between one row's descent and the next row's ascent /// The spacing in pixels between one row's descent and the next row's ascent.
float lineGap; float lineGap;
/// The thickness of the under/hover/striketrough line in pixels /// The thickness of the under/hover/strike-trough line in pixels.
float underline_thickness; float underlineThickness;
/// The position of the underline relatively to the baseline /// The position of the underline relatively to the baseline.
float underline_position; float underlinePosition;
//scale to apply to glyph data /// Scale to apply to glyph data.
float scale; float scale;
}; };
@ -78,7 +78,7 @@ typedef int32_t CodePoint_t;
/// A structure that describe a glyph. /// A structure that describe a glyph.
struct GlyphInfo struct GlyphInfo
{ {
/// Index for faster retrieval /// Index for faster retrieval.
int32_t glyphIndex; int32_t glyphIndex;
/// Glyph's width in pixels. /// Glyph's width in pixels.
@ -90,23 +90,24 @@ struct GlyphInfo
/// Glyph's left offset in pixels /// Glyph's left offset in pixels
float offset_x; float offset_x;
/// Glyph's top offset in pixels /// Glyph's top offset in pixels.
/// Remember that this is the distance from the baseline to the top-most ///
/// glyph scan line, upwards y coordinates being positive. /// @remark This is the distance from the baseline to the top-most glyph
/// scan line, upwards y coordinates being positive.
float offset_y; float offset_y;
/// For horizontal text layouts, this is the unscaled horizontal distance in pixels /// For horizontal text layouts, this is the unscaled horizontal
/// used to increment the pen position when the glyph is drawn as part of a string of text. /// distance in pixels used to increment the pen position when the
/// glyph is drawn as part of a string of text.
float advance_x; float advance_x;
/// For vertical text layouts, this is the unscaled vertical distance in pixels /// For vertical text layouts, this is the unscaled vertical distance
/// used to increment the pen position when the glyph is drawn as part of a string of text. /// in pixels used to increment the pen position when the glyph is
/// drawn as part of a string of text.
float advance_y; float advance_y;
/// region index in the atlas storing textures /// Region index in the atlas storing textures.
uint16_t regionIndex; uint16_t regionIndex;
///32 bits alignment
int16_t padding;
}; };
BGFX_HANDLE(TrueTypeHandle); BGFX_HANDLE(TrueTypeHandle);
@ -115,67 +116,78 @@ BGFX_HANDLE(FontHandle);
class FontManager class FontManager
{ {
public: public:
/// create the font manager using an external cube atlas (doesn't take ownership of the atlas) /// Create the font manager using an external cube atlas (doesn't take
/// ownership of the atlas).
FontManager(Atlas* _atlas); FontManager(Atlas* _atlas);
/// create the font manager and create the texture cube as BGRA8 with linear filtering
/// Create the font manager and create the texture cube as BGRA8 with
/// linear filtering.
FontManager(uint32_t _textureSideWidth = 512); FontManager(uint32_t _textureSideWidth = 512);
~FontManager(); ~FontManager();
/// retrieve the atlas used by the font manager (e.g. to add stuff to it) /// Retrieve the atlas used by the font manager (e.g. to add stuff to it)
Atlas* getAtlas() Atlas* getAtlas()
{ {
return m_atlas; return m_atlas;
} }
/// load a TrueType font from a file path /// Load a TrueType font from a file path.
/// @return invalid handle if the loading fail ///
/// @return INVALID_HANDLE if the loading fail.
TrueTypeHandle loadTrueTypeFromFile(const char* _fontPath); TrueTypeHandle loadTrueTypeFromFile(const char* _fontPath);
/// load a TrueType font from a given buffer. /// Load a TrueType font from a given buffer. The buffer is copied and
/// the buffer is copied and thus can be freed or reused after this call /// thus can be freed or reused after this call.
///
/// @return invalid handle if the loading fail /// @return invalid handle if the loading fail
TrueTypeHandle loadTrueTypeFromMemory(const uint8_t* _buffer, uint32_t _size); TrueTypeHandle loadTrueTypeFromMemory(const uint8_t* _buffer, uint32_t _size);
/// unload a TrueType font (free font memory) but keep loaded glyphs /// Unload a TrueType font (free font memory) but keep loaded glyphs.
void unloadTrueType(TrueTypeHandle _handle); void unloadTrueType(TrueTypeHandle _handle);
/// return a font whose height is a fixed pixel size /// Return a font whose height is a fixed pixel size.
FontHandle createFontByPixelSize(TrueTypeHandle _handle, uint32_t _typefaceIndex, uint32_t _pixelSize, FontType _fontType = FONT_TYPE_ALPHA); FontHandle createFontByPixelSize(TrueTypeHandle _handle, uint32_t _typefaceIndex, uint32_t _pixelSize, FontType _fontType = FONT_TYPE_ALPHA);
/// return a scaled child font whose height is a fixed pixel size /// Return a scaled child font whose height is a fixed pixel size.
FontHandle createScaledFontToPixelSize(FontHandle _baseFontHandle, uint32_t _pixelSize); FontHandle createScaledFontToPixelSize(FontHandle _baseFontHandle, uint32_t _pixelSize);
/// load a baked font (the set of glyph is fixed) /// Load a baked font (the set of glyph is fixed).
/// @return INVALID_HANDLE if the loading fail ///
/// @return INVALID_HANDLE if the loading fail.
FontHandle loadBakedFontFromFile(const char* _imagePath, const char* _descriptorPath); FontHandle loadBakedFontFromFile(const char* _imagePath, const char* _descriptorPath);
/// load a baked font (the set of glyph is fixed) /// Load a baked font (the set of glyph is fixed).
/// @return INVALID_HANDLE if the loading fail ///
/// @return INVALID_HANDLE if the loading fail.
FontHandle loadBakedFontFromMemory(const uint8_t* _imageBuffer, uint32_t _imageSize, const uint8_t* _descriptorBuffer, uint32_t _descriptorSize); FontHandle loadBakedFontFromMemory(const uint8_t* _imageBuffer, uint32_t _imageSize, const uint8_t* _descriptorBuffer, uint32_t _descriptorSize);
/// destroy a font (truetype or baked) /// destroy a font (truetype or baked)
void destroyFont(FontHandle _handle); void destroyFont(FontHandle _handle);
/// Preload a set of glyphs from a TrueType file /// Preload a set of glyphs from a TrueType file.
/// @return true if every glyph could be preloaded, false otherwise ///
/// if the Font is a baked font, this only do validation on the characters /// @return True if every glyph could be preloaded, false otherwise if
/// the Font is a baked font, this only do validation on the characters.
bool preloadGlyph(FontHandle _handle, const wchar_t* _string); bool preloadGlyph(FontHandle _handle, const wchar_t* _string);
/// Preload a single glyph, return true on success /// Preload a single glyph, return true on success.
bool preloadGlyph(FontHandle _handle, CodePoint_t _character); bool preloadGlyph(FontHandle _handle, CodePoint_t _character);
/// bake a font to disk (the set of preloaded glyph) /// Bake a font to disk (the set of preloaded glyph).
///
/// @return true if the baking succeed, false otherwise /// @return true if the baking succeed, false otherwise
bool saveBakedFont(FontHandle _handle, const char* _fontDirectory, const char* _fontName); bool saveBakedFont(FontHandle _handle, const char* _fontDirectory, const char* _fontName);
/// return the font descriptor of a font /// Return the font descriptor of a font.
///
/// @remark the handle is required to be valid /// @remark the handle is required to be valid
const FontInfo& getFontInfo(FontHandle _handle); const FontInfo& getFontInfo(FontHandle _handle);
/// Return the rendering informations about the glyph region /// Return the rendering informations about the glyph region. Load the
/// Load the glyph from a TrueType font if possible /// glyph from a TrueType font if possible
/// @return true if the Glyph is available ///
/// @return True if the Glyph is available.
bool getGlyphInfo(FontHandle _handle, CodePoint_t _codePoint, GlyphInfo& _outInfo); bool getGlyphInfo(FontHandle _handle, CodePoint_t _codePoint, GlyphInfo& _outInfo);
GlyphInfo& getBlackGlyph() GlyphInfo& getBlackGlyph()
@ -183,9 +195,7 @@ public:
return m_blackGlyph; return m_blackGlyph;
} }
class TrueTypeFont; //public to shut off Intellisense warning
private: private:
struct CachedFont; struct CachedFont;
struct CachedFile struct CachedFile
{ {

View File

@ -428,7 +428,7 @@ void TextBuffer::appendGlyph(CodePoint_t _codePoint, const FontInfo& _font, cons
float x0 = (m_penX - kerning); float x0 = (m_penX - kerning);
float y0 = (m_penY - m_lineDescender / 2); float y0 = (m_penY - m_lineDescender / 2);
float x1 = ( (float)x0 + (_glyphInfo.advance_x) ); float x1 = ( (float)x0 + (_glyphInfo.advance_x) );
float y1 = y0 + _font.underline_thickness; float y1 = y0 + _font.underlineThickness;
m_fontManager->getAtlas()->packUV(blackGlyph.regionIndex, (uint8_t*)m_vertexBuffer, sizeof(TextVertex) * m_vertexCount + offsetof(TextVertex, u), sizeof(TextVertex) ); m_fontManager->getAtlas()->packUV(blackGlyph.regionIndex, (uint8_t*)m_vertexBuffer, sizeof(TextVertex) * m_vertexCount + offsetof(TextVertex, u), sizeof(TextVertex) );
@ -453,7 +453,7 @@ void TextBuffer::appendGlyph(CodePoint_t _codePoint, const FontInfo& _font, cons
float x0 = (m_penX - kerning); float x0 = (m_penX - kerning);
float y0 = (m_penY - _font.ascender); float y0 = (m_penY - _font.ascender);
float x1 = ( (float)x0 + (_glyphInfo.advance_x) ); float x1 = ( (float)x0 + (_glyphInfo.advance_x) );
float y1 = y0 + _font.underline_thickness; float y1 = y0 + _font.underlineThickness;
m_fontManager->getAtlas()->packUV(blackGlyph.regionIndex, (uint8_t*)m_vertexBuffer, sizeof(TextVertex) * m_vertexCount + offsetof(TextVertex, u), sizeof(TextVertex) ); m_fontManager->getAtlas()->packUV(blackGlyph.regionIndex, (uint8_t*)m_vertexBuffer, sizeof(TextVertex) * m_vertexCount + offsetof(TextVertex, u), sizeof(TextVertex) );
@ -478,7 +478,7 @@ void TextBuffer::appendGlyph(CodePoint_t _codePoint, const FontInfo& _font, cons
float x0 = (m_penX - kerning); float x0 = (m_penX - kerning);
float y0 = (m_penY - _font.ascender / 3); float y0 = (m_penY - _font.ascender / 3);
float x1 = ( (float)x0 + (_glyphInfo.advance_x) ); float x1 = ( (float)x0 + (_glyphInfo.advance_x) );
float y1 = y0 + _font.underline_thickness; float y1 = y0 + _font.underlineThickness;
m_fontManager->getAtlas()->packUV(blackGlyph.regionIndex, (uint8_t*)m_vertexBuffer, sizeof(TextVertex) * m_vertexCount + offsetof(TextVertex, u), sizeof(TextVertex) ); m_fontManager->getAtlas()->packUV(blackGlyph.regionIndex, (uint8_t*)m_vertexBuffer, sizeof(TextVertex) * m_vertexCount + offsetof(TextVertex, u), sizeof(TextVertex) );