Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion src/graphics/font.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ void Font::PreparedFontString::append(std::string_view s, float text_size, glm::
/*.string_offset =*/ int(string_offset + index),
/*.size =*/ text_size,
/*.color =*/ color,
/*.descender =*/ 0.0f,
});
previous_char_code = char_info.code;
index += char_info.consumed_bytes;
Expand All @@ -74,8 +75,10 @@ void Font::PreparedFontString::append(std::string_view s, float text_size, glm::
{
glyph.advance = 0;
glyph.bounds.size.x = 0;
glyph.descender = 0.0f;
}

data.back().descender = glyph.descender * text_size / float(pixel_size);
next_position_x += glyph.advance * text_size / float(pixel_size);
if ((flags & FlagLineWrap) && next_position_x > area_size.x)
{
Expand Down Expand Up @@ -126,6 +129,7 @@ void Font::PreparedFontString::finish()
/*.string_offset =*/ 0,
/*.size =*/ 0.0f,
/*.color =*/ {255,255,255,255},
/*.descender =*/ 0.0f,
});
} else {
data.push_back({
Expand All @@ -134,6 +138,7 @@ void Font::PreparedFontString::finish()
/*.string_offset =*/ data.back().string_offset + 1,
/*.size =*/ data.back().size,
/*.color =*/ data.back().color,
/*.descender =*/ data.back().descender,
});
}

Expand Down Expand Up @@ -215,7 +220,7 @@ glm::vec2 Font::PreparedFontString::getUsedAreaSize() const
float max_y = 0.0f;
for(auto& d : data) {
max_x = std::max(max_x, d.position.x);
max_y = std::max(max_y, d.position.y);
max_y = std::max(max_y, d.position.y + d.descender);
}

glm::vec2 result(max_x, max_y);
Expand Down
2 changes: 2 additions & 0 deletions src/graphics/font.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ class Font : sp::NonCopyable
int string_offset;
float size;
glm::u8vec4 color;
float descender;
};
std::vector<GlyphData> data;

Expand Down Expand Up @@ -65,6 +66,7 @@ class Font : sp::NonCopyable
public:
Rect bounds;
float advance;
float descender;
};
virtual CharacterInfo getCharacterInfo(const char* str) = 0;
virtual bool getGlyphInfo(int char_code, int pixel_size, GlyphInfo& info) = 0;
Expand Down
2 changes: 2 additions & 0 deletions src/graphics/freetypefont.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,7 @@ bool FreetypeFont::getGlyphInfo(int char_code, int pixel_size, Font::GlyphInfo&

info.bounds = Rect({0, 0}, {0, 0});
info.advance = 0;
info.descender = 0.0f;

if (static_cast<FT_Face>(ft_face)->size->metrics.x_ppem != pixel_size)
FT_Set_Pixel_Sizes(static_cast<FT_Face>(ft_face), 0, pixel_size);
Expand All @@ -153,6 +154,7 @@ bool FreetypeFont::getGlyphInfo(int char_code, int pixel_size, Font::GlyphInfo&
info.bounds.position.y = float(face->glyph->metrics.horiBearingY) / float(1 << 6);
info.bounds.size.x = float(face->glyph->metrics.width) / float(1 << 6);
info.bounds.size.y = float(face->glyph->metrics.height) / float(1 << 6);
info.descender = float(face->glyph->metrics.height - face->glyph->metrics.horiBearingY) / float(1 << 6);

FT_Done_Glyph(glyph);
}
Expand Down
1 change: 1 addition & 0 deletions src/graphics/renderTarget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -811,6 +811,7 @@ void RenderTarget::drawRotatedText(glm::vec2 center, float rotation, std::string
{
glyph.advance = 0.0f;
glyph.bounds.size.x = 0.0f;
glyph.descender = 0.0f;
}

if (glyph.bounds.size.x > 0.0f)
Expand Down
Loading