mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2025-04-22 04:25:13 +00:00
LibHTML: Implement CSS text-align: left/center/right
This was easier than I imagined; we just shift each line box to the left based on the alignment and the remaining space on each line. :^)
This commit is contained in:
parent
2366c330e3
commit
6dbba6ad85
Notes:
sideshowbarker
2024-07-19 11:40:28 +09:00
Author: https://github.com/awesomekling Commit: https://github.com/SerenityOS/serenity/commit/6dbba6ad857
3 changed files with 31 additions and 2 deletions
|
@ -1,5 +1,8 @@
|
|||
<html>
|
||||
<head><title>Lorem Ipsum</title></head>
|
||||
<style>
|
||||
p { text-align: right; }
|
||||
</style>
|
||||
<body>
|
||||
<h1>Lorem Ipsum</h1>
|
||||
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. In non elit dignissim, lobortis velit id, rutrum enim. Fusce urna nulla, semper in nisl consectetur, dictum dignissim felis. Vivamus mollis porttitor neque non pulvinar. Donec sollicitudin pulvinar nisi, nec vestibulum massa rutrum id. Aenean convallis tincidunt diam vel egestas. Pellentesque laoreet commodo arcu id dignissim. Etiam mattis elementum lectus, ut ultricies nibh dapibus sit amet. Curabitur sodales cursus ipsum vitae porttitor. Vestibulum ac nulla auctor, imperdiet augue accumsan, ornare eros.</p>
|
||||
|
|
|
@ -14,6 +14,9 @@ namespace CSS {
|
|||
enum class ValueID {
|
||||
Invalid,
|
||||
VendorSpecificLink,
|
||||
Center,
|
||||
Left,
|
||||
Right,
|
||||
};
|
||||
}
|
||||
|
||||
|
|
|
@ -58,18 +58,41 @@ void LayoutBlock::layout_inline_children()
|
|||
});
|
||||
|
||||
int min_line_height = style().line_height();
|
||||
|
||||
int content_height = 0;
|
||||
|
||||
// FIXME: This should be done by the CSS parser!
|
||||
CSS::ValueID text_align = CSS::ValueID::Left;
|
||||
auto text_align_string = style().string_or_fallback(CSS::PropertyID::TextAlign, "left");
|
||||
if (text_align_string == "center")
|
||||
text_align = CSS::ValueID::Center;
|
||||
else if (text_align_string == "left")
|
||||
text_align = CSS::ValueID::Left;
|
||||
else if (text_align_string == "right")
|
||||
text_align = CSS::ValueID::Right;
|
||||
|
||||
for (auto& line_box : m_line_boxes) {
|
||||
int max_height = min_line_height;
|
||||
for (auto& fragment : line_box.fragments()) {
|
||||
max_height = max(max_height, fragment.rect().height());
|
||||
}
|
||||
|
||||
int x_offset = x();
|
||||
switch (text_align) {
|
||||
case CSS::ValueID::Center:
|
||||
x_offset += (width() - line_box.width()) / 2;
|
||||
break;
|
||||
case CSS::ValueID::Right:
|
||||
x_offset += (width() - line_box.width());
|
||||
break;
|
||||
case CSS::ValueID::Left:
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
||||
for (auto& fragment : line_box.fragments()) {
|
||||
// Vertically align everyone's bottom to the line.
|
||||
// FIXME: Support other kinds of vertical alignment.
|
||||
fragment.rect().set_x(x() + fragment.rect().x());
|
||||
fragment.rect().set_x(x_offset + fragment.rect().x());
|
||||
fragment.rect().set_y(y() + content_height + (max_height - fragment.rect().height()));
|
||||
|
||||
if (is<LayoutReplaced>(fragment.layout_node()))
|
||||
|
|
Loading…
Add table
Reference in a new issue