mirror of
https://github.com/RGBCube/serenity
synced 2025-07-27 10:07:44 +00:00
LibGfx: Slim down Gfx::TextLayout API by removing unused accessors
Also store the Font as a const reference instead of a raw pointer, since we don't allow a null Font here.
This commit is contained in:
parent
65c8cd37e3
commit
6b421fb521
4 changed files with 14 additions and 23 deletions
|
@ -117,7 +117,7 @@ void Label::size_to_fit()
|
|||
|
||||
int Label::text_calculated_preferred_height() const
|
||||
{
|
||||
return int(AK::ceil(Gfx::TextLayout(&font(), Utf8View { m_text }, text_rect().to_type<float>()).bounding_rect(Gfx::TextWrapping::Wrap, Gfx::Painter::LINE_SPACING).height()));
|
||||
return int(AK::ceil(Gfx::TextLayout(font(), Utf8View { m_text }, text_rect().to_type<float>()).bounding_rect(Gfx::TextWrapping::Wrap, Gfx::Painter::LINE_SPACING).height()));
|
||||
}
|
||||
|
||||
Optional<UISize> Label::calculated_preferred_size() const
|
||||
|
|
|
@ -1724,7 +1724,7 @@ void Painter::do_draw_text(FloatRect const& rect, Utf8View const& text, Font con
|
|||
if (draw_text_get_length(text) == 0)
|
||||
return;
|
||||
|
||||
TextLayout layout(&font, text, rect);
|
||||
TextLayout layout(font, text, rect);
|
||||
|
||||
auto line_height = font.pixel_size() + LINE_SPACING;
|
||||
|
||||
|
|
|
@ -28,11 +28,11 @@ FloatRect TextLayout::bounding_rect(TextWrapping wrapping, int line_spacing) con
|
|||
}
|
||||
|
||||
FloatRect bounding_rect = {
|
||||
0, 0, 0, (lines.size() * (m_font->pixel_size() + line_spacing)) - line_spacing
|
||||
0, 0, 0, (lines.size() * (m_font.pixel_size() + line_spacing)) - line_spacing
|
||||
};
|
||||
|
||||
for (auto& line : lines) {
|
||||
auto line_width = m_font->width(line);
|
||||
auto line_width = m_font.width(line);
|
||||
if (line_width > bounding_rect.width())
|
||||
bounding_rect.set_width(line_width);
|
||||
}
|
||||
|
@ -107,11 +107,11 @@ Vector<DeprecatedString, 32> TextLayout::wrap_lines(TextElision elision, TextWra
|
|||
}
|
||||
|
||||
size_t max_lines_that_can_fit = 0;
|
||||
if (m_rect.height() >= m_font->glyph_height()) {
|
||||
if (m_rect.height() >= m_font.glyph_height()) {
|
||||
// NOTE: If glyph height is 10 and line spacing is 1, we can fit a
|
||||
// single line into a 10px rect and a 20px rect, but 2 lines into a
|
||||
// 21px rect.
|
||||
max_lines_that_can_fit = 1 + (m_rect.height() - m_font->glyph_height()) / (m_font->glyph_height() + line_spacing);
|
||||
max_lines_that_can_fit = 1 + (m_rect.height() - m_font.glyph_height()) / (m_font.glyph_height() + line_spacing);
|
||||
}
|
||||
|
||||
if (max_lines_that_can_fit == 0)
|
||||
|
@ -139,11 +139,11 @@ Vector<DeprecatedString, 32> TextLayout::wrap_lines(TextElision elision, TextWra
|
|||
}
|
||||
case BlockType::Whitespace:
|
||||
case BlockType::Word: {
|
||||
float block_width = font().width(block.characters);
|
||||
float block_width = m_font.width(block.characters);
|
||||
// FIXME: This should look at the specific advance amount of the
|
||||
// last character, but we don't support that yet.
|
||||
if (current_block != blocks.size() - 1) {
|
||||
block_width += font().glyph_spacing();
|
||||
block_width += m_font.glyph_spacing();
|
||||
}
|
||||
|
||||
if (wrapping == TextWrapping::Wrap && line_width + block_width > m_rect.width()) {
|
||||
|
@ -185,11 +185,11 @@ blocks_processed:
|
|||
|
||||
DeprecatedString TextLayout::elide_text_from_right(Utf8View text, bool force_elision) const
|
||||
{
|
||||
float text_width = m_font->width(text);
|
||||
float text_width = m_font.width(text);
|
||||
if (force_elision || text_width > static_cast<unsigned>(m_rect.width())) {
|
||||
float ellipsis_width = m_font->width("..."sv);
|
||||
float ellipsis_width = m_font.width("..."sv);
|
||||
float current_width = ellipsis_width;
|
||||
size_t glyph_spacing = m_font->glyph_spacing();
|
||||
size_t glyph_spacing = m_font.glyph_spacing();
|
||||
|
||||
// FIXME: This code will break when the font has glyphs with advance
|
||||
// amounts different from the actual width of the glyph
|
||||
|
@ -198,7 +198,7 @@ DeprecatedString TextLayout::elide_text_from_right(Utf8View text, bool force_eli
|
|||
size_t offset = 0;
|
||||
for (auto it = text.begin(); !it.done(); ++it) {
|
||||
auto code_point = *it;
|
||||
auto glyph_width = m_font->glyph_or_emoji_width(code_point);
|
||||
auto glyph_width = m_font.glyph_or_emoji_width(code_point);
|
||||
// NOTE: Glyph spacing should not be added after the last glyph on the line,
|
||||
// but since we are here because the last glyph does not actually fit on the line,
|
||||
// we don't have to worry about spacing.
|
||||
|
|
|
@ -44,22 +44,13 @@ enum class FitWithinRect {
|
|||
// b) Taking the Lines from TextLayout and painting each glyph.
|
||||
class TextLayout {
|
||||
public:
|
||||
TextLayout(Gfx::Font const* font, Utf8View const& text, FloatRect const& rect)
|
||||
TextLayout(Gfx::Font const& font, Utf8View const& text, FloatRect const& rect)
|
||||
: m_font(font)
|
||||
, m_text(text)
|
||||
, m_rect(rect)
|
||||
{
|
||||
}
|
||||
|
||||
Font const& font() const { return *m_font; }
|
||||
void set_font(Font const* font) { m_font = font; }
|
||||
|
||||
Utf8View const& text() const { return m_text; }
|
||||
void set_text(Utf8View const& text) { m_text = text; }
|
||||
|
||||
FloatRect const& rect() const { return m_rect; }
|
||||
void set_rect(FloatRect const& rect) { m_rect = rect; }
|
||||
|
||||
Vector<DeprecatedString, 32> lines(TextElision elision, TextWrapping wrapping, int line_spacing) const
|
||||
{
|
||||
return wrap_lines(elision, wrapping, line_spacing, FitWithinRect::Yes);
|
||||
|
@ -71,7 +62,7 @@ private:
|
|||
Vector<DeprecatedString, 32> wrap_lines(TextElision, TextWrapping, int line_spacing, FitWithinRect) const;
|
||||
DeprecatedString elide_text_from_right(Utf8View, bool force_elision) const;
|
||||
|
||||
Font const* m_font;
|
||||
Font const& m_font;
|
||||
Utf8View m_text;
|
||||
FloatRect m_rect;
|
||||
};
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue