Skip to content

Commit

Permalink
New text::Font::getGlyphAdvance() to get whitespace bounds correctly
Browse files Browse the repository at this point in the history
  • Loading branch information
dacap committed Aug 26, 2024
1 parent 06279da commit af450af
Show file tree
Hide file tree
Showing 8 changed files with 24 additions and 3 deletions.
1 change: 1 addition & 0 deletions text/empty_font_mgr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
1 change: 1 addition & 0 deletions text/font.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
5 changes: 5 additions & 0 deletions text/freetype_font.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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> FreeTypeFont::LoadFont(
ft::Lib& lib,
const char* filename,
Expand Down
1 change: 1 addition & 0 deletions text/freetype_font.h
Original file line number Diff line number Diff line change
Expand Up @@ -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; }

Expand Down
7 changes: 7 additions & 0 deletions text/skia_font.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
1 change: 1 addition & 0 deletions text/skia_font.h
Original file line number Diff line number Diff line change
Expand Up @@ -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; }

Expand Down
4 changes: 4 additions & 0 deletions text/sprite_sheet_font.h
Original file line number Diff line number Diff line change
Expand Up @@ -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];
Expand Down
7 changes: 4 additions & 3 deletions text/text_blob.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down

0 comments on commit af450af

Please sign in to comment.