ComputedValues.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. /*
  2. * Copyright (c) 2020, Andreas Kling <kling@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #pragma once
  7. #include <AK/Optional.h>
  8. #include <LibWeb/CSS/LengthBox.h>
  9. #include <LibWeb/CSS/StyleValue.h>
  10. namespace Web::CSS {
  11. class InitialValues {
  12. public:
  13. static CSS::Float float_() { return CSS::Float::None; }
  14. static CSS::Clear clear() { return CSS::Clear::None; }
  15. static CSS::Cursor cursor() { return CSS::Cursor::Auto; }
  16. static CSS::WhiteSpace white_space() { return CSS::WhiteSpace::Normal; }
  17. static CSS::TextAlign text_align() { return CSS::TextAlign::Left; }
  18. static CSS::Position position() { return CSS::Position::Static; }
  19. static CSS::TextDecorationLine text_decoration_line() { return CSS::TextDecorationLine::None; }
  20. static CSS::TextTransform text_transform() { return CSS::TextTransform::None; }
  21. static CSS::Display display() { return CSS::Display { CSS::Display::Outside::Inline, CSS::Display::Inside::Flow }; }
  22. static Color color() { return Color::Black; }
  23. static Color background_color() { return Color::Transparent; }
  24. static CSS::Repeat background_repeat() { return CSS::Repeat::Repeat; }
  25. static CSS::ListStyleType list_style_type() { return CSS::ListStyleType::Disc; }
  26. static CSS::FlexDirection flex_direction() { return CSS::FlexDirection::Row; }
  27. static CSS::FlexWrap flex_wrap() { return CSS::FlexWrap::Nowrap; }
  28. static CSS::JustifyContent justify_content() { return CSS::JustifyContent::FlexStart; }
  29. static CSS::AlignItems align_items() { return CSS::AlignItems::Stretch; }
  30. static CSS::Overflow overflow() { return CSS::Overflow::Visible; }
  31. static CSS::BoxSizing box_sizing() { return CSS::BoxSizing::ContentBox; }
  32. static CSS::PointerEvents pointer_events() { return CSS::PointerEvents::Auto; }
  33. static float flex_grow() { return 0.0f; }
  34. static float flex_shrink() { return 1.0f; }
  35. static float opacity() { return 1.0f; }
  36. };
  37. struct BackgroundLayerData {
  38. RefPtr<CSS::ImageStyleValue> image;
  39. CSS::Repeat repeat_x;
  40. CSS::Repeat repeat_y;
  41. };
  42. struct BorderData {
  43. public:
  44. Color color { Color::Transparent };
  45. CSS::LineStyle line_style { CSS::LineStyle::None };
  46. float width { 0 };
  47. };
  48. struct Transformation {
  49. CSS::TransformFunction function;
  50. Vector<Variant<CSS::Length, float>> values;
  51. };
  52. struct FlexBasisData {
  53. CSS::FlexBasis type { CSS::FlexBasis::Auto };
  54. CSS::Length length {};
  55. bool is_definite() const { return type == CSS::FlexBasis::Length; }
  56. };
  57. struct BoxShadowData {
  58. CSS::Length offset_x {};
  59. CSS::Length offset_y {};
  60. CSS::Length blur_radius {};
  61. Color color {};
  62. };
  63. struct BackgroundRepeatData {
  64. CSS::Repeat repeat_x;
  65. CSS::Repeat repeat_y;
  66. };
  67. class ComputedValues {
  68. public:
  69. CSS::Float float_() const { return m_noninherited.float_; }
  70. CSS::Clear clear() const { return m_noninherited.clear; }
  71. CSS::Cursor cursor() const { return m_inherited.cursor; }
  72. CSS::PointerEvents pointer_events() const { return m_inherited.pointer_events; }
  73. CSS::Display display() const { return m_noninherited.display; }
  74. Optional<int> const& z_index() const { return m_noninherited.z_index; }
  75. CSS::TextAlign text_align() const { return m_inherited.text_align; }
  76. CSS::TextDecorationLine text_decoration_line() const { return m_noninherited.text_decoration_line; }
  77. CSS::TextTransform text_transform() const { return m_inherited.text_transform; }
  78. CSS::Position position() const { return m_noninherited.position; }
  79. CSS::WhiteSpace white_space() const { return m_inherited.white_space; }
  80. CSS::FlexDirection flex_direction() const { return m_noninherited.flex_direction; }
  81. CSS::FlexWrap flex_wrap() const { return m_noninherited.flex_wrap; }
  82. FlexBasisData const& flex_basis() const { return m_noninherited.flex_basis; }
  83. float flex_grow() const { return m_noninherited.flex_grow; }
  84. float flex_shrink() const { return m_noninherited.flex_shrink; }
  85. CSS::AlignItems align_items() const { return m_noninherited.align_items; }
  86. float opacity() const { return m_noninherited.opacity; }
  87. CSS::JustifyContent justify_content() const { return m_noninherited.justify_content; }
  88. Optional<BoxShadowData> const& box_shadow() const { return m_noninherited.box_shadow; }
  89. CSS::BoxSizing box_sizing() const { return m_noninherited.box_sizing; }
  90. const CSS::Length& width() const { return m_noninherited.width; }
  91. const CSS::Length& min_width() const { return m_noninherited.min_width; }
  92. const CSS::Length& max_width() const { return m_noninherited.max_width; }
  93. const CSS::Length& height() const { return m_noninherited.height; }
  94. const CSS::Length& min_height() const { return m_noninherited.min_height; }
  95. const CSS::Length& max_height() const { return m_noninherited.max_height; }
  96. const CSS::LengthBox& offset() const { return m_noninherited.offset; }
  97. const CSS::LengthBox& margin() const { return m_noninherited.margin; }
  98. const CSS::LengthBox& padding() const { return m_noninherited.padding; }
  99. const BorderData& border_left() const { return m_noninherited.border_left; }
  100. const BorderData& border_top() const { return m_noninherited.border_top; }
  101. const BorderData& border_right() const { return m_noninherited.border_right; }
  102. const BorderData& border_bottom() const { return m_noninherited.border_bottom; }
  103. const CSS::Length& border_bottom_left_radius() const { return m_noninherited.border_bottom_left_radius; }
  104. const CSS::Length& border_bottom_right_radius() const { return m_noninherited.border_bottom_right_radius; }
  105. const CSS::Length& border_top_left_radius() const { return m_noninherited.border_top_left_radius; }
  106. const CSS::Length& border_top_right_radius() const { return m_noninherited.border_top_right_radius; }
  107. CSS::Overflow overflow_x() const { return m_noninherited.overflow_x; }
  108. CSS::Overflow overflow_y() const { return m_noninherited.overflow_y; }
  109. Color color() const { return m_inherited.color; }
  110. Color background_color() const { return m_noninherited.background_color; }
  111. Vector<BackgroundLayerData> const& background_layers() const { return m_noninherited.background_layers; }
  112. BackgroundRepeatData background_repeat() const { return m_noninherited.background_repeat; }
  113. CSS::ListStyleType list_style_type() const { return m_inherited.list_style_type; }
  114. Optional<Color> fill() const { return m_inherited.fill; }
  115. Optional<Color> stroke() const { return m_inherited.stroke; }
  116. Optional<Length> const& stroke_width() const { return m_inherited.stroke_width; }
  117. Vector<CSS::Transformation> transformations() const { return m_noninherited.transformations; }
  118. ComputedValues clone_inherited_values() const
  119. {
  120. ComputedValues clone;
  121. clone.m_inherited = m_inherited;
  122. return clone;
  123. }
  124. protected:
  125. struct {
  126. Color color { InitialValues::color() };
  127. CSS::Cursor cursor { InitialValues::cursor() };
  128. CSS::PointerEvents pointer_events { InitialValues::pointer_events() };
  129. CSS::TextAlign text_align { InitialValues::text_align() };
  130. CSS::TextTransform text_transform { InitialValues::text_transform() };
  131. CSS::WhiteSpace white_space { InitialValues::white_space() };
  132. CSS::ListStyleType list_style_type { InitialValues::list_style_type() };
  133. Optional<Color> fill;
  134. Optional<Color> stroke;
  135. Optional<Length> stroke_width;
  136. } m_inherited;
  137. struct {
  138. CSS::Float float_ { InitialValues::float_() };
  139. CSS::Clear clear { InitialValues::clear() };
  140. CSS::Display display { InitialValues::display() };
  141. Optional<int> z_index;
  142. CSS::TextDecorationLine text_decoration_line { InitialValues::text_decoration_line() };
  143. CSS::Position position { InitialValues::position() };
  144. CSS::Length width;
  145. CSS::Length min_width;
  146. CSS::Length max_width;
  147. CSS::Length height;
  148. CSS::Length min_height;
  149. CSS::Length max_height;
  150. CSS::LengthBox offset;
  151. CSS::LengthBox margin;
  152. CSS::LengthBox padding;
  153. BorderData border_left;
  154. BorderData border_top;
  155. BorderData border_right;
  156. BorderData border_bottom;
  157. Length border_bottom_left_radius;
  158. Length border_bottom_right_radius;
  159. Length border_top_left_radius;
  160. Length border_top_right_radius;
  161. Color background_color { InitialValues::background_color() };
  162. Vector<BackgroundLayerData> background_layers;
  163. BackgroundRepeatData background_repeat { InitialValues::background_repeat(), InitialValues::background_repeat() };
  164. CSS::FlexDirection flex_direction { InitialValues::flex_direction() };
  165. CSS::FlexWrap flex_wrap { InitialValues::flex_wrap() };
  166. CSS::FlexBasisData flex_basis {};
  167. float flex_grow { InitialValues::flex_grow() };
  168. float flex_shrink { InitialValues::flex_shrink() };
  169. CSS::AlignItems align_items { InitialValues::align_items() };
  170. CSS::JustifyContent justify_content { InitialValues::justify_content() };
  171. CSS::Overflow overflow_x { InitialValues::overflow() };
  172. CSS::Overflow overflow_y { InitialValues::overflow() };
  173. float opacity { InitialValues::opacity() };
  174. Optional<BoxShadowData> box_shadow {};
  175. Vector<CSS::Transformation> transformations {};
  176. CSS::BoxSizing box_sizing { InitialValues::box_sizing() };
  177. } m_noninherited;
  178. };
  179. class ImmutableComputedValues final : public ComputedValues {
  180. };
  181. class MutableComputedValues final : public ComputedValues {
  182. public:
  183. void set_color(const Color& color) { m_inherited.color = color; }
  184. void set_cursor(CSS::Cursor cursor) { m_inherited.cursor = cursor; }
  185. void set_pointer_events(CSS::PointerEvents value) { m_inherited.pointer_events = value; }
  186. void set_background_color(const Color& color) { m_noninherited.background_color = color; }
  187. void set_background_repeat(BackgroundRepeatData repeat) { m_noninherited.background_repeat = repeat; }
  188. void set_background_layers(Vector<BackgroundLayerData>&& layers) { m_noninherited.background_layers = move(layers); }
  189. void set_float(CSS::Float value) { m_noninherited.float_ = value; }
  190. void set_clear(CSS::Clear value) { m_noninherited.clear = value; }
  191. void set_z_index(Optional<int> value) { m_noninherited.z_index = value; }
  192. void set_text_align(CSS::TextAlign text_align) { m_inherited.text_align = text_align; }
  193. void set_text_decoration_line(CSS::TextDecorationLine value) { m_noninherited.text_decoration_line = value; }
  194. void set_text_transform(CSS::TextTransform value) { m_inherited.text_transform = value; }
  195. void set_position(CSS::Position position) { m_noninherited.position = position; }
  196. void set_white_space(CSS::WhiteSpace value) { m_inherited.white_space = value; }
  197. void set_width(const CSS::Length& width) { m_noninherited.width = width; }
  198. void set_min_width(const CSS::Length& width) { m_noninherited.min_width = width; }
  199. void set_max_width(const CSS::Length& width) { m_noninherited.max_width = width; }
  200. void set_height(const CSS::Length& height) { m_noninherited.height = height; }
  201. void set_min_height(const CSS::Length& height) { m_noninherited.min_height = height; }
  202. void set_max_height(const CSS::Length& height) { m_noninherited.max_height = height; }
  203. void set_offset(const CSS::LengthBox& offset) { m_noninherited.offset = offset; }
  204. void set_margin(const CSS::LengthBox& margin) { m_noninherited.margin = margin; }
  205. void set_padding(const CSS::LengthBox& padding) { m_noninherited.padding = padding; }
  206. void set_overflow_x(CSS::Overflow value) { m_noninherited.overflow_x = value; }
  207. void set_overflow_y(CSS::Overflow value) { m_noninherited.overflow_y = value; }
  208. void set_list_style_type(CSS::ListStyleType value) { m_inherited.list_style_type = value; }
  209. void set_display(CSS::Display value) { m_noninherited.display = value; }
  210. void set_border_bottom_left_radius(CSS::Length value) { m_noninherited.border_bottom_left_radius = value; }
  211. void set_border_bottom_right_radius(CSS::Length value) { m_noninherited.border_bottom_right_radius = value; }
  212. void set_border_top_left_radius(CSS::Length value) { m_noninherited.border_top_left_radius = value; }
  213. void set_border_top_right_radius(CSS::Length value) { m_noninherited.border_top_right_radius = value; }
  214. BorderData& border_left() { return m_noninherited.border_left; }
  215. BorderData& border_top() { return m_noninherited.border_top; }
  216. BorderData& border_right() { return m_noninherited.border_right; }
  217. BorderData& border_bottom() { return m_noninherited.border_bottom; }
  218. void set_flex_direction(CSS::FlexDirection value) { m_noninherited.flex_direction = value; }
  219. void set_flex_wrap(CSS::FlexWrap value) { m_noninherited.flex_wrap = value; }
  220. void set_flex_basis(FlexBasisData value) { m_noninherited.flex_basis = value; }
  221. void set_flex_grow(float value) { m_noninherited.flex_grow = value; }
  222. void set_flex_shrink(float value) { m_noninherited.flex_shrink = value; }
  223. void set_align_items(CSS::AlignItems value) { m_noninherited.align_items = value; }
  224. void set_opacity(float value) { m_noninherited.opacity = value; }
  225. void set_justify_content(CSS::JustifyContent value) { m_noninherited.justify_content = value; }
  226. void set_box_shadow(Optional<BoxShadowData> value) { m_noninherited.box_shadow = move(value); }
  227. void set_transformations(Vector<CSS::Transformation> value) { m_noninherited.transformations = move(value); }
  228. void set_box_sizing(CSS::BoxSizing value) { m_noninherited.box_sizing = value; }
  229. void set_fill(Color value) { m_inherited.fill = value; }
  230. void set_stroke(Color value) { m_inherited.stroke = value; }
  231. void set_stroke_width(Length value) { m_inherited.stroke_width = value; }
  232. };
  233. }