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

LibWeb: Cache the used CSS text-align property on LayoutNodeWithStyle

This commit is contained in:
Andreas Kling 2020-06-23 23:28:40 +02:00
parent ae181e1573
commit f4ecb5362f
6 changed files with 28 additions and 16 deletions

View file

@ -229,19 +229,19 @@ bool StyleProperties::operator==(const StyleProperties& other) const
return true; return true;
} }
CSS::ValueID StyleProperties::text_align() const CSS::TextAlign StyleProperties::text_align() const
{ {
auto string = string_or_fallback(CSS::PropertyID::TextAlign, "left"); auto string = string_or_fallback(CSS::PropertyID::TextAlign, "left");
if (string == "center") if (string == "center")
return CSS::ValueID::Center; return CSS::TextAlign::Center;
if (string == "right") if (string == "right")
return CSS::ValueID::Right; return CSS::TextAlign::Right;
if (string == "justify") if (string == "justify")
return CSS::ValueID::Justify; return CSS::TextAlign::Justify;
if (string == "-libweb-center") if (string == "-libweb-center")
return CSS::ValueID::VendorSpecificCenter; return CSS::TextAlign::VendorSpecificCenter;
// Otherwise, just assume "left".. // Otherwise, just assume "left"..
return CSS::ValueID::Left; return CSS::TextAlign::Left;
} }
} }

View file

@ -59,7 +59,7 @@ public:
Length length_or_fallback(CSS::PropertyID, const Length& fallback, float reference_for_percentages) const; Length length_or_fallback(CSS::PropertyID, const Length& fallback, float reference_for_percentages) const;
String string_or_fallback(CSS::PropertyID, const StringView& fallback) const; String string_or_fallback(CSS::PropertyID, const StringView& fallback) const;
Color color_or_fallback(CSS::PropertyID, const Document&, Color fallback) const; Color color_or_fallback(CSS::PropertyID, const Document&, Color fallback) const;
CSS::ValueID text_align() const; CSS::TextAlign text_align() const;
const Gfx::Font& font() const const Gfx::Font& font() const
{ {

View file

@ -114,6 +114,15 @@ enum class Position {
Fixed, Fixed,
Sticky, Sticky,
}; };
enum class TextAlign {
Left,
Center,
Right,
Justify,
VendorSpecificCenter,
};
} }
class StyleValue : public RefCounted<StyleValue> { class StyleValue : public RefCounted<StyleValue> {

View file

@ -187,7 +187,7 @@ void LayoutBlock::layout_inline_children(LayoutMode layout_mode)
line_box.trim_trailing_whitespace(); line_box.trim_trailing_whitespace();
} }
auto text_align = style().text_align(); auto text_align = this->text_align();
float min_line_height = style().line_height(*this); float min_line_height = style().line_height(*this);
float line_spacing = min_line_height - style().font().glyph_height(); float line_spacing = min_line_height - style().font().glyph_height();
float content_height = 0; float content_height = 0;
@ -203,22 +203,22 @@ void LayoutBlock::layout_inline_children(LayoutMode layout_mode)
float excess_horizontal_space = (float)width() - line_box.width(); float excess_horizontal_space = (float)width() - line_box.width();
switch (text_align) { switch (text_align) {
case CSS::ValueID::Center: case CSS::TextAlign::Center:
case CSS::ValueID::VendorSpecificCenter: case CSS::TextAlign::VendorSpecificCenter:
x_offset += excess_horizontal_space / 2; x_offset += excess_horizontal_space / 2;
break; break;
case CSS::ValueID::Right: case CSS::TextAlign::Right:
x_offset += excess_horizontal_space; x_offset += excess_horizontal_space;
break; break;
case CSS::ValueID::Left: case CSS::TextAlign::Left:
case CSS::ValueID::Justify: case CSS::TextAlign::Justify:
default: default:
break; break;
} }
float excess_horizontal_space_including_whitespace = excess_horizontal_space; float excess_horizontal_space_including_whitespace = excess_horizontal_space;
int whitespace_count = 0; int whitespace_count = 0;
if (text_align == CSS::ValueID::Justify) { if (text_align == CSS::TextAlign::Justify) {
for (auto& fragment : line_box.fragments()) { for (auto& fragment : line_box.fragments()) {
if (fragment.is_justifiable_whitespace()) { if (fragment.is_justifiable_whitespace()) {
++whitespace_count; ++whitespace_count;
@ -236,7 +236,7 @@ void LayoutBlock::layout_inline_children(LayoutMode layout_mode)
// FIXME: Support other kinds of vertical alignment. // FIXME: Support other kinds of vertical alignment.
fragment.set_offset({ roundf(x_offset + fragment.offset().x()), content_height + (max_height - fragment.height()) - (line_spacing / 2) }); fragment.set_offset({ roundf(x_offset + fragment.offset().x()), content_height + (max_height - fragment.height()) - (line_spacing / 2) });
if (text_align == CSS::ValueID::Justify) { if (text_align == CSS::TextAlign::Justify) {
if (fragment.is_justifiable_whitespace()) { if (fragment.is_justifiable_whitespace()) {
if (fragment.width() != justified_space_width) { if (fragment.width() != justified_space_width) {
float diff = justified_space_width - fragment.width(); float diff = justified_space_width - fragment.width();
@ -633,7 +633,7 @@ void LayoutBlock::place_block_level_non_replaced_element_in_normal_flow(LayoutBl
+ box.padding().left.to_px(*this) + box.padding().left.to_px(*this)
+ box.offset().left.to_px(*this); + box.offset().left.to_px(*this);
if (this->style().text_align() == CSS::ValueID::VendorSpecificCenter) { if (text_align() == CSS::TextAlign::VendorSpecificCenter) {
x = (containing_block.width() / 2) - block.width() / 2; x = (containing_block.width() / 2) - block.width() / 2;
} }

View file

@ -217,6 +217,7 @@ LayoutNodeWithStyle::LayoutNodeWithStyle(const Node* node, NonnullRefPtr<StylePr
{ {
m_has_style = true; m_has_style = true;
m_position = m_style->position(); m_position = m_style->position();
m_text_align = m_style->text_align();
} }
} }

View file

@ -259,6 +259,7 @@ public:
void set_style(const StyleProperties& style) { m_style = style; } void set_style(const StyleProperties& style) { m_style = style; }
CSS::Position position() const { return m_position; } CSS::Position position() const { return m_position; }
CSS::TextAlign text_align() const { return m_text_align; }
protected: protected:
explicit LayoutNodeWithStyle(const Node*, NonnullRefPtr<StyleProperties>); explicit LayoutNodeWithStyle(const Node*, NonnullRefPtr<StyleProperties>);
@ -266,6 +267,7 @@ protected:
private: private:
NonnullRefPtr<StyleProperties> m_style; NonnullRefPtr<StyleProperties> m_style;
CSS::Position m_position; CSS::Position m_position;
CSS::TextAlign m_text_align;
}; };
class LayoutNodeWithStyleAndBoxModelMetrics : public LayoutNodeWithStyle { class LayoutNodeWithStyleAndBoxModelMetrics : public LayoutNodeWithStyle {