Browse Source

LibWeb: Add support for the text-justify property

This commit adds the text-justify property as defined in:
https://drafts.csswg.org/css-text/#propdef-text-justify
sin-ack 3 years ago
parent
commit
0679eadd62

+ 4 - 0
Userland/Libraries/LibWeb/CSS/ComputedValues.h

@@ -21,6 +21,7 @@ public:
     static CSS::Cursor cursor() { return CSS::Cursor::Auto; }
     static CSS::Cursor cursor() { return CSS::Cursor::Auto; }
     static CSS::WhiteSpace white_space() { return CSS::WhiteSpace::Normal; }
     static CSS::WhiteSpace white_space() { return CSS::WhiteSpace::Normal; }
     static CSS::TextAlign text_align() { return CSS::TextAlign::Left; }
     static CSS::TextAlign text_align() { return CSS::TextAlign::Left; }
+    static CSS::TextJustify text_justify() { return CSS::TextJustify::Auto; }
     static CSS::Position position() { return CSS::Position::Static; }
     static CSS::Position position() { return CSS::Position::Static; }
     static CSS::TextDecorationLine text_decoration_line() { return CSS::TextDecorationLine::None; }
     static CSS::TextDecorationLine text_decoration_line() { return CSS::TextDecorationLine::None; }
     static CSS::Length text_decoration_thickness() { return Length::make_px(1); }
     static CSS::Length text_decoration_thickness() { return Length::make_px(1); }
@@ -111,6 +112,7 @@ public:
     CSS::Display display() const { return m_noninherited.display; }
     CSS::Display display() const { return m_noninherited.display; }
     Optional<int> const& z_index() const { return m_noninherited.z_index; }
     Optional<int> const& z_index() const { return m_noninherited.z_index; }
     CSS::TextAlign text_align() const { return m_inherited.text_align; }
     CSS::TextAlign text_align() const { return m_inherited.text_align; }
+    CSS::TextJustify text_justify() const { return m_inherited.text_justify; }
     CSS::TextDecorationLine text_decoration_line() const { return m_noninherited.text_decoration_line; }
     CSS::TextDecorationLine text_decoration_line() const { return m_noninherited.text_decoration_line; }
     CSS::LengthPercentage text_decoration_thickness() const { return m_noninherited.text_decoration_thickness; }
     CSS::LengthPercentage text_decoration_thickness() const { return m_noninherited.text_decoration_thickness; }
     CSS::TextDecorationStyle text_decoration_style() const { return m_noninherited.text_decoration_style; }
     CSS::TextDecorationStyle text_decoration_style() const { return m_noninherited.text_decoration_style; }
@@ -185,6 +187,7 @@ protected:
         CSS::ImageRendering image_rendering { InitialValues::image_rendering() };
         CSS::ImageRendering image_rendering { InitialValues::image_rendering() };
         CSS::PointerEvents pointer_events { InitialValues::pointer_events() };
         CSS::PointerEvents pointer_events { InitialValues::pointer_events() };
         CSS::TextAlign text_align { InitialValues::text_align() };
         CSS::TextAlign text_align { InitialValues::text_align() };
+        CSS::TextJustify text_justify { InitialValues::text_justify() };
         CSS::TextTransform text_transform { InitialValues::text_transform() };
         CSS::TextTransform text_transform { InitialValues::text_transform() };
         CSS::WhiteSpace white_space { InitialValues::white_space() };
         CSS::WhiteSpace white_space { InitialValues::white_space() };
         CSS::ListStyleType list_style_type { InitialValues::list_style_type() };
         CSS::ListStyleType list_style_type { InitialValues::list_style_type() };
@@ -259,6 +262,7 @@ public:
     void set_clear(CSS::Clear value) { m_noninherited.clear = value; }
     void set_clear(CSS::Clear value) { m_noninherited.clear = value; }
     void set_z_index(Optional<int> value) { m_noninherited.z_index = value; }
     void set_z_index(Optional<int> value) { m_noninherited.z_index = value; }
     void set_text_align(CSS::TextAlign text_align) { m_inherited.text_align = text_align; }
     void set_text_align(CSS::TextAlign text_align) { m_inherited.text_align = text_align; }
+    void set_text_justify(CSS::TextJustify text_justify) { m_inherited.text_justify = text_justify; }
     void set_text_decoration_line(CSS::TextDecorationLine value) { m_noninherited.text_decoration_line = value; }
     void set_text_decoration_line(CSS::TextDecorationLine value) { m_noninherited.text_decoration_line = value; }
     void set_text_decoration_thickness(CSS::LengthPercentage value) { m_noninherited.text_decoration_thickness = value; }
     void set_text_decoration_thickness(CSS::LengthPercentage value) { m_noninherited.text_decoration_thickness = value; }
     void set_text_decoration_style(CSS::TextDecorationStyle value) { m_noninherited.text_decoration_style = value; }
     void set_text_decoration_style(CSS::TextDecorationStyle value) { m_noninherited.text_decoration_style = value; }

+ 3 - 0
Userland/Libraries/LibWeb/CSS/Identifiers.json

@@ -97,6 +97,7 @@
   "decimal-leading-zero",
   "decimal-leading-zero",
   "default",
   "default",
   "disc",
   "disc",
+  "distribute",
   "dotted",
   "dotted",
   "double",
   "double",
   "e-resize",
   "e-resize",
@@ -129,6 +130,8 @@
   "inset",
   "inset",
   "inside",
   "inside",
   "interlace",
   "interlace",
+  "inter-character",
+  "inter-word",
   "invert",
   "invert",
   "italic",
   "italic",
   "justify",
   "justify",

+ 11 - 0
Userland/Libraries/LibWeb/CSS/Properties.json

@@ -1287,6 +1287,17 @@
       "unitless-length"
       "unitless-length"
     ]
     ]
   },
   },
+  "text-justify": {
+    "inherited": true,
+    "initial": "auto",
+    "valid-identifiers": [
+      "auto",
+      "none",
+      "inter-word",
+      "inter-character",
+      "distribute"
+    ]
+  },
   "text-transform": {
   "text-transform": {
     "inherited": true,
     "inherited": true,
     "initial": "none",
     "initial": "none",

+ 20 - 0
Userland/Libraries/LibWeb/CSS/StyleProperties.cpp

@@ -438,6 +438,26 @@ Optional<CSS::TextAlign> StyleProperties::text_align() const
     }
     }
 }
 }
 
 
+Optional<CSS::TextJustify> StyleProperties::text_justify() const
+{
+    auto value = property(CSS::PropertyID::TextJustify);
+    if (!value.has_value())
+        return {};
+    switch (value.value()->to_identifier()) {
+    case CSS::ValueID::Auto:
+        return CSS::TextJustify::Auto;
+    case CSS::ValueID::None:
+        return CSS::TextJustify::None;
+    case CSS::ValueID::InterWord:
+        return CSS::TextJustify::InterWord;
+    case CSS::ValueID::Distribute:
+    case CSS::ValueID::InterCharacter:
+        return CSS::TextJustify::InterCharacter;
+    default:
+        return {};
+    }
+}
+
 Optional<CSS::PointerEvents> StyleProperties::pointer_events() const
 Optional<CSS::PointerEvents> StyleProperties::pointer_events() const
 {
 {
     auto value = property(CSS::PropertyID::PointerEvents);
     auto value = property(CSS::PropertyID::PointerEvents);

+ 1 - 0
Userland/Libraries/LibWeb/CSS/StyleProperties.h

@@ -47,6 +47,7 @@ public:
     LengthBox length_box(CSS::PropertyID left_id, CSS::PropertyID top_id, CSS::PropertyID right_id, CSS::PropertyID bottom_id, const CSS::Length& default_value) const;
     LengthBox length_box(CSS::PropertyID left_id, CSS::PropertyID top_id, CSS::PropertyID right_id, CSS::PropertyID bottom_id, const CSS::Length& default_value) const;
     Color color_or_fallback(CSS::PropertyID, Layout::NodeWithStyle const&, Color fallback) const;
     Color color_or_fallback(CSS::PropertyID, Layout::NodeWithStyle const&, Color fallback) const;
     Optional<CSS::TextAlign> text_align() const;
     Optional<CSS::TextAlign> text_align() const;
+    Optional<CSS::TextJustify> text_justify() const;
     CSS::Display display() const;
     CSS::Display display() const;
     Optional<CSS::Float> float_() const;
     Optional<CSS::Float> float_() const;
     Optional<CSS::Clear> clear() const;
     Optional<CSS::Clear> clear() const;

+ 7 - 0
Userland/Libraries/LibWeb/CSS/StyleValue.h

@@ -274,6 +274,13 @@ enum class TextDecorationStyle {
     Wavy,
     Wavy,
 };
 };
 
 
+enum class TextJustify {
+    Auto,
+    None,
+    InterWord,
+    InterCharacter,
+};
+
 enum class TextTransform {
 enum class TextTransform {
     None,
     None,
     Capitalize,
     Capitalize,

+ 4 - 0
Userland/Libraries/LibWeb/Layout/Node.cpp

@@ -365,6 +365,10 @@ void NodeWithStyle::apply_style(const CSS::StyleProperties& specified_style)
     if (text_align.has_value())
     if (text_align.has_value())
         computed_values.set_text_align(text_align.value());
         computed_values.set_text_align(text_align.value());
 
 
+    auto text_justify = specified_style.text_justify();
+    if (text_align.has_value())
+        computed_values.set_text_justify(text_justify.value());
+
     auto white_space = specified_style.white_space();
     auto white_space = specified_style.white_space();
     if (white_space.has_value())
     if (white_space.has_value())
         computed_values.set_white_space(white_space.value());
         computed_values.set_white_space(white_space.value());