diff --git a/Libraries/LibDraw/Painter.cpp b/Libraries/LibDraw/Painter.cpp index a3e974f77e..b3b0043b0e 100644 --- a/Libraries/LibDraw/Painter.cpp +++ b/Libraries/LibDraw/Painter.cpp @@ -1,15 +1,15 @@ #include "Painter.h" +#include "Emoji.h" #include "Font.h" #include "GraphicsBitmap.h" -#include "Emoji.h" #include #include #include +#include #include #include #include #include -#include #pragma GCC optimize("O3") @@ -623,18 +623,22 @@ void Painter::draw_text_line(const Rect& a_rect, const Utf8View& text, const Fon } } - if (alignment == TextAlignment::TopLeft) { - // No-op. - } else if (alignment == TextAlignment::CenterLeft) { - // No-op. - } else if (alignment == TextAlignment::CenterRight) { + switch (alignment) { + case TextAlignment::TopLeft: + case TextAlignment::CenterLeft: + break; + case TextAlignment::TopRight: + case TextAlignment::CenterRight: rect.set_x(rect.right() - font.width(final_text)); - } else if (alignment == TextAlignment::Center) { + break; + case TextAlignment::Center: { auto shrunken_rect = rect; shrunken_rect.set_width(font.width(final_text)); shrunken_rect.center_within(rect); rect = shrunken_rect; - } else { + break; + } + default: ASSERT_NOT_REACHED(); } @@ -687,15 +691,23 @@ void Painter::draw_text(const Rect& rect, const StringView& raw_text, const Font bounding_rect.set_width(line_width); } - if (alignment == TextAlignment::TopLeft) { + switch (alignment) { + case TextAlignment::TopLeft: bounding_rect.set_location(rect.location()); - } else if (alignment == TextAlignment::CenterLeft) { + break; + case TextAlignment::TopRight: + bounding_rect.set_location({ (rect.right() + 1) - bounding_rect.width(), rect.y() }); + break; + case TextAlignment::CenterLeft: bounding_rect.set_location({ rect.x(), rect.center().y() - (bounding_rect.height() / 2) }); - } else if (alignment == TextAlignment::CenterRight) { + break; + case TextAlignment::CenterRight: bounding_rect.set_location({ (rect.right() + 1) - bounding_rect.width(), rect.center().y() - (bounding_rect.height() / 2) }); - } else if (alignment == TextAlignment::Center) { + break; + case TextAlignment::Center: bounding_rect.center_within(rect); - } else { + break; + default: ASSERT_NOT_REACHED(); } diff --git a/Libraries/LibDraw/Rect.cpp b/Libraries/LibDraw/Rect.cpp index 213d18ead9..1a5ccdc73f 100644 --- a/Libraries/LibDraw/Rect.cpp +++ b/Libraries/LibDraw/Rect.cpp @@ -86,6 +86,10 @@ void Rect::align_within(const Rect& other, TextAlignment alignment) case TextAlignment::TopLeft: set_location(other.location()); return; + case TextAlignment::TopRight: + set_x(other.x() + other.width() - width()); + set_y(other.y()); + return; case TextAlignment::CenterLeft: set_x(other.x()); center_vertically_within(other); diff --git a/Libraries/LibDraw/TextAlignment.h b/Libraries/LibDraw/TextAlignment.h index bd9d61ac73..b16987cfc7 100644 --- a/Libraries/LibDraw/TextAlignment.h +++ b/Libraries/LibDraw/TextAlignment.h @@ -4,13 +4,15 @@ enum class TextAlignment { TopLeft, CenterLeft, Center, - CenterRight + CenterRight, + TopRight, }; inline bool is_right_text_alignment(TextAlignment alignment) { switch (alignment) { case TextAlignment::CenterRight: + case TextAlignment::TopRight: return true; default: return false;