diff --git a/text/empty_font_mgr.cpp b/text/empty_font_mgr.cpp index 3948b91bb..59c08d74b 100644 --- a/text/empty_font_mgr.cpp +++ b/text/empty_font_mgr.cpp @@ -39,6 +39,7 @@ class EmptyFont : public Font { glyph_t codePointToGlyph(codepoint_t) const override { return false; } gfx::RectF getGlyphBounds(glyph_t) const override { return gfx::RectF(); } + float getGlyphAdvance(glyph_t glyph) const override { return 0; } }; class EmptyFontStyleSet : public FontStyleSet { diff --git a/text/font.h b/text/font.h index 252446088..fbc499d72 100644 --- a/text/font.h +++ b/text/font.h @@ -52,6 +52,7 @@ namespace text { virtual glyph_t codePointToGlyph(codepoint_t cp) const = 0; virtual gfx::RectF getGlyphBounds(glyph_t glyph) const = 0; + virtual float getGlyphAdvance(glyph_t glyph) const = 0; Font* fallback() const { return m_fallback; diff --git a/text/freetype_font.cpp b/text/freetype_font.cpp index b7bac82b3..2ea4fb666 100644 --- a/text/freetype_font.cpp +++ b/text/freetype_font.cpp @@ -119,6 +119,11 @@ gfx::RectF FreeTypeFont::getGlyphBounds(glyph_t glyph) const return gfx::RectF(); // TODO impl } +float FreeTypeFont::getGlyphAdvance(glyph_t glyph) const +{ + return 0; // TODO impl +} + base::Ref FreeTypeFont::LoadFont( ft::Lib& lib, const char* filename, diff --git a/text/freetype_font.h b/text/freetype_font.h index 3a0f7faea..b502d9ead 100644 --- a/text/freetype_font.h +++ b/text/freetype_font.h @@ -41,6 +41,7 @@ namespace text { glyph_t codePointToGlyph(codepoint_t cp) const override; gfx::RectF getGlyphBounds(glyph_t glyph) const override; + float getGlyphAdvance(glyph_t glyph) const override; Face& face() { return m_face; } diff --git a/text/skia_font.cpp b/text/skia_font.cpp index ee93cf87f..5191a9664 100644 --- a/text/skia_font.cpp +++ b/text/skia_font.cpp @@ -129,4 +129,11 @@ gfx::RectF SkiaFont::getGlyphBounds(glyph_t glyph) const return os::from_skia(bounds); } +float SkiaFont::getGlyphAdvance(glyph_t glyph) const +{ + float widths = 0.0f; + m_skFont.getWidthsBounds(&glyph, 1, &widths, nullptr, nullptr); + return widths; +} + } // namespace text diff --git a/text/skia_font.h b/text/skia_font.h index 71dfcb8e0..c0cbaa444 100644 --- a/text/skia_font.h +++ b/text/skia_font.h @@ -35,6 +35,7 @@ namespace text { glyph_t codePointToGlyph(codepoint_t codepoint) const override; gfx::RectF getGlyphBounds(glyph_t glyph) const override; + float getGlyphAdvance(glyph_t glyph) const override; SkFont& skFont() { return m_skFont; } diff --git a/text/sprite_sheet_font.h b/text/sprite_sheet_font.h index 749126c8a..c1ab763eb 100644 --- a/text/sprite_sheet_font.h +++ b/text/sprite_sheet_font.h @@ -105,6 +105,10 @@ class SpriteSheetFont : public Font { return getCharBounds(128); } + float getGlyphAdvance(glyph_t glyph) const override { + return getGlyphBounds(glyph).w; + } + gfx::RectF getGlyphBoundsOnSheet(glyph_t glyph) const { if (glyph >= 0 && glyph < (int)m_glyphs.size()) return m_glyphs[glyph]; diff --git a/text/text_blob.cpp b/text/text_blob.cpp index 9e0b9672d..5b50f0d84 100644 --- a/text/text_blob.cpp +++ b/text/text_blob.cpp @@ -68,9 +68,10 @@ gfx::RectF TextBlob::RunInfo::getGlyphBounds(const size_t i) const // Get bounds of whitespace from a space glyph. if (bounds.isEmpty()) { - auto glyph = font->getGlyphBounds(' '); - bounds.w = glyph.w - glyph.x; - bounds.h = glyph.h - glyph.y; + FontMetrics metrics; + font->metrics(&metrics); + bounds.w = font->getGlyphAdvance(font->codePointToGlyph(' ')); + bounds.h = std::abs(metrics.capHeight); } ASSERT(!bounds.isEmpty());