1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-25 16:07:47 +00:00

LibDraw: Add TextAlignment::TopRight

Also tidy up the alignment code to use switch statements.
This commit is contained in:
Andreas Kling 2019-09-06 19:23:36 +02:00
parent cb62890f8e
commit a791b86afa
3 changed files with 33 additions and 15 deletions

View file

@ -1,15 +1,15 @@
#include "Painter.h" #include "Painter.h"
#include "Emoji.h"
#include "Font.h" #include "Font.h"
#include "GraphicsBitmap.h" #include "GraphicsBitmap.h"
#include "Emoji.h"
#include <AK/Assertions.h> #include <AK/Assertions.h>
#include <AK/StdLibExtras.h> #include <AK/StdLibExtras.h>
#include <AK/StringBuilder.h> #include <AK/StringBuilder.h>
#include <AK/Utf8View.h>
#include <LibDraw/CharacterBitmap.h> #include <LibDraw/CharacterBitmap.h>
#include <math.h> #include <math.h>
#include <stdio.h> #include <stdio.h>
#include <unistd.h> #include <unistd.h>
#include <AK/Utf8View.h>
#pragma GCC optimize("O3") #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) { switch (alignment) {
// No-op. case TextAlignment::TopLeft:
} else if (alignment == TextAlignment::CenterLeft) { case TextAlignment::CenterLeft:
// No-op. break;
} else if (alignment == TextAlignment::CenterRight) { case TextAlignment::TopRight:
case TextAlignment::CenterRight:
rect.set_x(rect.right() - font.width(final_text)); rect.set_x(rect.right() - font.width(final_text));
} else if (alignment == TextAlignment::Center) { break;
case TextAlignment::Center: {
auto shrunken_rect = rect; auto shrunken_rect = rect;
shrunken_rect.set_width(font.width(final_text)); shrunken_rect.set_width(font.width(final_text));
shrunken_rect.center_within(rect); shrunken_rect.center_within(rect);
rect = shrunken_rect; rect = shrunken_rect;
} else { break;
}
default:
ASSERT_NOT_REACHED(); 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); bounding_rect.set_width(line_width);
} }
if (alignment == TextAlignment::TopLeft) { switch (alignment) {
case TextAlignment::TopLeft:
bounding_rect.set_location(rect.location()); 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) }); 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) }); 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); bounding_rect.center_within(rect);
} else { break;
default:
ASSERT_NOT_REACHED(); ASSERT_NOT_REACHED();
} }

View file

@ -86,6 +86,10 @@ void Rect::align_within(const Rect& other, TextAlignment alignment)
case TextAlignment::TopLeft: case TextAlignment::TopLeft:
set_location(other.location()); set_location(other.location());
return; return;
case TextAlignment::TopRight:
set_x(other.x() + other.width() - width());
set_y(other.y());
return;
case TextAlignment::CenterLeft: case TextAlignment::CenterLeft:
set_x(other.x()); set_x(other.x());
center_vertically_within(other); center_vertically_within(other);

View file

@ -4,13 +4,15 @@ enum class TextAlignment {
TopLeft, TopLeft,
CenterLeft, CenterLeft,
Center, Center,
CenterRight CenterRight,
TopRight,
}; };
inline bool is_right_text_alignment(TextAlignment alignment) inline bool is_right_text_alignment(TextAlignment alignment)
{ {
switch (alignment) { switch (alignment) {
case TextAlignment::CenterRight: case TextAlignment::CenterRight:
case TextAlignment::TopRight:
return true; return true;
default: default:
return false; return false;