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

LibGfx: Add support for TextAlignment::TopCenter / BottomCenter

Now supports TextAlignment::TopCenter and TextAlignment::BottomCenter
for the Painter::draw_text.

Also patched this in Spreadsheet/CellTypeDialog.cpp
This commit is contained in:
Vrins 2022-02-27 01:33:48 +01:00 committed by Andreas Kling
parent 73ade62d4f
commit 3b22fd9a9f
5 changed files with 33 additions and 2 deletions

View file

@ -74,10 +74,12 @@ constexpr static CellTypeDialog::VerticalAlignment vertical_alignment_from(Gfx::
case Gfx::TextAlignment::Center: case Gfx::TextAlignment::Center:
return CellTypeDialog::VerticalAlignment::Center; return CellTypeDialog::VerticalAlignment::Center;
case Gfx::TextAlignment::TopCenter:
case Gfx::TextAlignment::TopRight: case Gfx::TextAlignment::TopRight:
case Gfx::TextAlignment::TopLeft: case Gfx::TextAlignment::TopLeft:
return CellTypeDialog::VerticalAlignment::Top; return CellTypeDialog::VerticalAlignment::Top;
case Gfx::TextAlignment::BottomCenter:
case Gfx::TextAlignment::BottomLeft: case Gfx::TextAlignment::BottomLeft:
case Gfx::TextAlignment::BottomRight: case Gfx::TextAlignment::BottomRight:
return CellTypeDialog::VerticalAlignment::Bottom; return CellTypeDialog::VerticalAlignment::Bottom;
@ -89,7 +91,9 @@ constexpr static CellTypeDialog::VerticalAlignment vertical_alignment_from(Gfx::
constexpr static CellTypeDialog::HorizontalAlignment horizontal_alignment_from(Gfx::TextAlignment alignment) constexpr static CellTypeDialog::HorizontalAlignment horizontal_alignment_from(Gfx::TextAlignment alignment)
{ {
switch (alignment) { switch (alignment) {
case Gfx::TextAlignment::BottomCenter:
case Gfx::TextAlignment::Center: case Gfx::TextAlignment::Center:
case Gfx::TextAlignment::TopCenter:
return CellTypeDialog::HorizontalAlignment::Center; return CellTypeDialog::HorizontalAlignment::Center;
case Gfx::TextAlignment::TopRight: case Gfx::TextAlignment::TopRight:

View file

@ -376,8 +376,10 @@ T* Object::find_descendant_of_type_named(String const& name) requires IsBaseOf<O
{ Gfx::TextAlignment::Center, "Center" }, \ { Gfx::TextAlignment::Center, "Center" }, \
{ Gfx::TextAlignment::CenterLeft, "CenterLeft" }, \ { Gfx::TextAlignment::CenterLeft, "CenterLeft" }, \
{ Gfx::TextAlignment::CenterRight, "CenterRight" }, \ { Gfx::TextAlignment::CenterRight, "CenterRight" }, \
{ Gfx::TextAlignment::TopLeft, "TopCenter" }, \
{ Gfx::TextAlignment::TopLeft, "TopLeft" }, \ { Gfx::TextAlignment::TopLeft, "TopLeft" }, \
{ Gfx::TextAlignment::TopRight, "TopRight" }, \ { Gfx::TextAlignment::TopRight, "TopRight" }, \
{ Gfx::TextAlignment::TopLeft, "BottomCenter" }, \
{ Gfx::TextAlignment::BottomLeft, "BottomLeft" }, \ { Gfx::TextAlignment::BottomLeft, "BottomLeft" }, \
{ Gfx::TextAlignment::BottomRight, "BottomRight" }) { Gfx::TextAlignment::BottomRight, "BottomRight" })

View file

@ -1336,6 +1336,8 @@ void draw_text_line(IntRect const& a_rect, Utf8View const& text, Font const& fon
case TextAlignment::BottomRight: case TextAlignment::BottomRight:
rect.set_x(rect.right() - font.width(text)); rect.set_x(rect.right() - font.width(text));
break; break;
case TextAlignment::TopCenter:
case TextAlignment::BottomCenter:
case TextAlignment::Center: { case TextAlignment::Center: {
auto shrunken_rect = rect; auto shrunken_rect = rect;
shrunken_rect.set_width(font.width(text)); shrunken_rect.set_width(font.width(text));
@ -1554,6 +1556,10 @@ void Painter::do_draw_text(IntRect const& rect, Utf8View const& text, Font const
auto bounding_rect = layout.bounding_rect(wrapping, line_spacing); auto bounding_rect = layout.bounding_rect(wrapping, line_spacing);
switch (alignment) { switch (alignment) {
case TextAlignment::TopCenter:
bounding_rect.set_y(rect.y());
bounding_rect.center_horizontally_within(rect);
break;
case TextAlignment::TopLeft: case TextAlignment::TopLeft:
bounding_rect.set_location(rect.location()); bounding_rect.set_location(rect.location());
break; break;
@ -1569,6 +1575,10 @@ void Painter::do_draw_text(IntRect const& rect, Utf8View const& text, Font const
case TextAlignment::Center: case TextAlignment::Center:
bounding_rect.center_within(rect); bounding_rect.center_within(rect);
break; break;
case TextAlignment::BottomCenter:
bounding_rect.set_y((rect.bottom() + 1) - bounding_rect.height());
bounding_rect.center_horizontally_within(rect);
break;
case TextAlignment::BottomLeft: case TextAlignment::BottomLeft:
bounding_rect.set_location({ rect.x(), (rect.bottom() + 1) - bounding_rect.height() }); bounding_rect.set_location({ rect.x(), (rect.bottom() + 1) - bounding_rect.height() });
break; break;

View file

@ -132,9 +132,15 @@ Rect<T> Rect<T>::aligned_within(Size<T> const& rect_size, Point<T> const& align_
Rect<T> rect; Rect<T> rect;
switch (alignment) { switch (alignment) {
case TextAlignment::TopCenter:
rect = { { align_at.x() - rect_size.width() / 2, align_at.y() }, rect_size };
break;
case TextAlignment::TopLeft: case TextAlignment::TopLeft:
rect = { align_at, rect_size }; rect = { align_at, rect_size };
break; break;
case TextAlignment::TopRight:
rect = { { align_at.x() - rect_size.width(), align_at.y() }, rect_size };
break;
case TextAlignment::CenterLeft: case TextAlignment::CenterLeft:
rect = { { align_at.x(), align_at.y() - rect_size.height() / 2 }, rect_size }; rect = { { align_at.x(), align_at.y() - rect_size.height() / 2 }, rect_size };
break; break;
@ -144,8 +150,8 @@ Rect<T> Rect<T>::aligned_within(Size<T> const& rect_size, Point<T> const& align_
case TextAlignment::CenterRight: case TextAlignment::CenterRight:
rect = { { align_at.x() - rect_size.width() / 2, align_at.y() }, rect_size }; rect = { { align_at.x() - rect_size.width() / 2, align_at.y() }, rect_size };
break; break;
case TextAlignment::TopRight: case TextAlignment::BottomCenter:
rect = { { align_at.x() - rect_size.width(), align_at.y() }, rect_size }; rect = { { align_at.x() - rect_size.width() / 2, align_at.y() - rect_size.width() }, rect_size };
break; break;
case TextAlignment::BottomLeft: case TextAlignment::BottomLeft:
rect = { { align_at.x(), align_at.y() - rect_size.width() }, rect_size }; rect = { { align_at.x(), align_at.y() - rect_size.width() }, rect_size };
@ -270,6 +276,9 @@ void Rect<T>::align_within(Rect<T> const& other, TextAlignment alignment)
case TextAlignment::Center: case TextAlignment::Center:
center_within(other); center_within(other);
return; return;
case TextAlignment::TopCenter:
set_x(other.x() + other.width() / 2);
return;
case TextAlignment::TopLeft: case TextAlignment::TopLeft:
set_location(other.location()); set_location(other.location());
return; return;
@ -285,6 +294,10 @@ void Rect<T>::align_within(Rect<T> const& other, TextAlignment alignment)
set_x(other.x() + other.width() - width()); set_x(other.x() + other.width() - width());
center_vertically_within(other); center_vertically_within(other);
return; return;
case TextAlignment::BottomCenter:
set_x(other.x() + other.width() / 2);
set_y(other.y() + other.height() - height());
return;
case TextAlignment::BottomLeft: case TextAlignment::BottomLeft:
set_x(other.x()); set_x(other.x());
set_y(other.y() + other.height() - height()); set_y(other.y() + other.height() - height());

View file

@ -15,8 +15,10 @@ namespace Gfx {
M(Center) \ M(Center) \
M(CenterLeft) \ M(CenterLeft) \
M(CenterRight) \ M(CenterRight) \
M(TopCenter) \
M(TopLeft) \ M(TopLeft) \
M(TopRight) \ M(TopRight) \
M(BottomCenter) \
M(BottomLeft) \ M(BottomLeft) \
M(BottomRight) M(BottomRight)