diff options
author | Andreas Kling <kling@serenityos.org> | 2023-01-05 20:41:19 +0100 |
---|---|---|
committer | Andreas Kling <kling@serenityos.org> | 2023-01-06 12:02:20 +0100 |
commit | 6b421fb5214d4ea3646ddd230a895c8a57453dc2 (patch) | |
tree | ad892a353e5c8666ba43eb68981d346de0272642 /Userland | |
parent | 65c8cd37e3188f85ea5a76e19502c3a27599ac40 (diff) | |
download | serenity-6b421fb5214d4ea3646ddd230a895c8a57453dc2.zip |
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.
Diffstat (limited to 'Userland')
-rw-r--r-- | Userland/Libraries/LibGUI/Label.cpp | 2 | ||||
-rw-r--r-- | Userland/Libraries/LibGfx/Painter.cpp | 2 | ||||
-rw-r--r-- | Userland/Libraries/LibGfx/TextLayout.cpp | 20 | ||||
-rw-r--r-- | Userland/Libraries/LibGfx/TextLayout.h | 13 |
4 files changed, 14 insertions, 23 deletions
diff --git a/Userland/Libraries/LibGUI/Label.cpp b/Userland/Libraries/LibGUI/Label.cpp index 06388284ec..eb9bd8ae92 100644 --- a/Userland/Libraries/LibGUI/Label.cpp +++ b/Userland/Libraries/LibGUI/Label.cpp @@ -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 diff --git a/Userland/Libraries/LibGfx/Painter.cpp b/Userland/Libraries/LibGfx/Painter.cpp index 519ae136e9..efa88f7772 100644 --- a/Userland/Libraries/LibGfx/Painter.cpp +++ b/Userland/Libraries/LibGfx/Painter.cpp @@ -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; diff --git a/Userland/Libraries/LibGfx/TextLayout.cpp b/Userland/Libraries/LibGfx/TextLayout.cpp index 4f63e06ce7..76c9ba4212 100644 --- a/Userland/Libraries/LibGfx/TextLayout.cpp +++ b/Userland/Libraries/LibGfx/TextLayout.cpp @@ -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. diff --git a/Userland/Libraries/LibGfx/TextLayout.h b/Userland/Libraries/LibGfx/TextLayout.h index c67375d43f..17a44757d2 100644 --- a/Userland/Libraries/LibGfx/TextLayout.h +++ b/Userland/Libraries/LibGfx/TextLayout.h @@ -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; }; |