Skip to content

Commit a791b86

Browse files
committed
LibDraw: Add TextAlignment::TopRight
Also tidy up the alignment code to use switch statements.
1 parent cb62890 commit a791b86

File tree

3 files changed

+33
-15
lines changed

3 files changed

+33
-15
lines changed

Libraries/LibDraw/Painter.cpp

Lines changed: 26 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
#include "Painter.h"
2+
#include "Emoji.h"
23
#include "Font.h"
34
#include "GraphicsBitmap.h"
4-
#include "Emoji.h"
55
#include <AK/Assertions.h>
66
#include <AK/StdLibExtras.h>
77
#include <AK/StringBuilder.h>
8+
#include <AK/Utf8View.h>
89
#include <LibDraw/CharacterBitmap.h>
910
#include <math.h>
1011
#include <stdio.h>
1112
#include <unistd.h>
12-
#include <AK/Utf8View.h>
1313

1414
#pragma GCC optimize("O3")
1515

@@ -623,18 +623,22 @@ void Painter::draw_text_line(const Rect& a_rect, const Utf8View& text, const Fon
623623
}
624624
}
625625

626-
if (alignment == TextAlignment::TopLeft) {
627-
// No-op.
628-
} else if (alignment == TextAlignment::CenterLeft) {
629-
// No-op.
630-
} else if (alignment == TextAlignment::CenterRight) {
626+
switch (alignment) {
627+
case TextAlignment::TopLeft:
628+
case TextAlignment::CenterLeft:
629+
break;
630+
case TextAlignment::TopRight:
631+
case TextAlignment::CenterRight:
631632
rect.set_x(rect.right() - font.width(final_text));
632-
} else if (alignment == TextAlignment::Center) {
633+
break;
634+
case TextAlignment::Center: {
633635
auto shrunken_rect = rect;
634636
shrunken_rect.set_width(font.width(final_text));
635637
shrunken_rect.center_within(rect);
636638
rect = shrunken_rect;
637-
} else {
639+
break;
640+
}
641+
default:
638642
ASSERT_NOT_REACHED();
639643
}
640644

@@ -687,15 +691,23 @@ void Painter::draw_text(const Rect& rect, const StringView& raw_text, const Font
687691
bounding_rect.set_width(line_width);
688692
}
689693

690-
if (alignment == TextAlignment::TopLeft) {
694+
switch (alignment) {
695+
case TextAlignment::TopLeft:
691696
bounding_rect.set_location(rect.location());
692-
} else if (alignment == TextAlignment::CenterLeft) {
697+
break;
698+
case TextAlignment::TopRight:
699+
bounding_rect.set_location({ (rect.right() + 1) - bounding_rect.width(), rect.y() });
700+
break;
701+
case TextAlignment::CenterLeft:
693702
bounding_rect.set_location({ rect.x(), rect.center().y() - (bounding_rect.height() / 2) });
694-
} else if (alignment == TextAlignment::CenterRight) {
703+
break;
704+
case TextAlignment::CenterRight:
695705
bounding_rect.set_location({ (rect.right() + 1) - bounding_rect.width(), rect.center().y() - (bounding_rect.height() / 2) });
696-
} else if (alignment == TextAlignment::Center) {
706+
break;
707+
case TextAlignment::Center:
697708
bounding_rect.center_within(rect);
698-
} else {
709+
break;
710+
default:
699711
ASSERT_NOT_REACHED();
700712
}
701713

Libraries/LibDraw/Rect.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,10 @@ void Rect::align_within(const Rect& other, TextAlignment alignment)
8686
case TextAlignment::TopLeft:
8787
set_location(other.location());
8888
return;
89+
case TextAlignment::TopRight:
90+
set_x(other.x() + other.width() - width());
91+
set_y(other.y());
92+
return;
8993
case TextAlignment::CenterLeft:
9094
set_x(other.x());
9195
center_vertically_within(other);

Libraries/LibDraw/TextAlignment.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,15 @@ enum class TextAlignment {
44
TopLeft,
55
CenterLeft,
66
Center,
7-
CenterRight
7+
CenterRight,
8+
TopRight,
89
};
910

1011
inline bool is_right_text_alignment(TextAlignment alignment)
1112
{
1213
switch (alignment) {
1314
case TextAlignment::CenterRight:
15+
case TextAlignment::TopRight:
1416
return true;
1517
default:
1618
return false;

0 commit comments

Comments
 (0)