ComputedValues.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  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::FlexStart; }
  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. };
  36. struct BorderData {
  37. public:
  38. Color color { Color::Transparent };
  39. CSS::LineStyle line_style { CSS::LineStyle::None };
  40. float width { 0 };
  41. };
  42. struct Transformation {
  43. CSS::TransformFunction function;
  44. Vector<Variant<CSS::Length, float>> values;
  45. };
  46. struct FlexBasisData {
  47. CSS::FlexBasis type { CSS::FlexBasis::Auto };
  48. CSS::Length length {};
  49. bool is_definite() const { return type == CSS::FlexBasis::Length; }
  50. };
  51. struct BoxShadowData {
  52. CSS::Length offset_x {};
  53. CSS::Length offset_y {};
  54. CSS::Length blur_radius {};
  55. Color color {};
  56. };
  57. class ComputedValues {
  58. public:
  59. CSS::Float float_() const { return m_noninherited.float_; }
  60. CSS::Clear clear() const { return m_noninherited.clear; }
  61. CSS::Cursor cursor() const { return m_inherited.cursor; }
  62. CSS::PointerEvents pointer_events() const { return m_inherited.pointer_events; }
  63. CSS::Display display() const { return m_noninherited.display; }
  64. Optional<int> const& z_index() const { return m_noninherited.z_index; }
  65. CSS::TextAlign text_align() const { return m_inherited.text_align; }
  66. CSS::TextDecorationLine text_decoration_line() const { return m_noninherited.text_decoration_line; }
  67. CSS::TextTransform text_transform() const { return m_inherited.text_transform; }
  68. CSS::Position position() const { return m_noninherited.position; }
  69. CSS::WhiteSpace white_space() const { return m_inherited.white_space; }
  70. CSS::FlexDirection flex_direction() const { return m_noninherited.flex_direction; }
  71. CSS::FlexWrap flex_wrap() const { return m_noninherited.flex_wrap; }
  72. FlexBasisData const& flex_basis() const { return m_noninherited.flex_basis; }
  73. float flex_grow() const { return m_noninherited.flex_grow; }
  74. float flex_shrink() const { return m_noninherited.flex_shrink; }
  75. CSS::AlignItems align_items() const { return m_noninherited.align_items; }
  76. Optional<float> const& opacity() const { return m_noninherited.opacity; }
  77. CSS::JustifyContent justify_content() const { return m_noninherited.justify_content; }
  78. Optional<BoxShadowData> const& box_shadow() const { return m_noninherited.box_shadow; }
  79. CSS::BoxSizing box_sizing() const { return m_noninherited.box_sizing; }
  80. const CSS::Length& width() const { return m_noninherited.width; }
  81. const CSS::Length& min_width() const { return m_noninherited.min_width; }
  82. const CSS::Length& max_width() const { return m_noninherited.max_width; }
  83. const CSS::Length& height() const { return m_noninherited.height; }
  84. const CSS::Length& min_height() const { return m_noninherited.min_height; }
  85. const CSS::Length& max_height() const { return m_noninherited.max_height; }
  86. const CSS::LengthBox& offset() const { return m_noninherited.offset; }
  87. const CSS::LengthBox& margin() const { return m_noninherited.margin; }
  88. const CSS::LengthBox& padding() const { return m_noninherited.padding; }
  89. const BorderData& border_left() const { return m_noninherited.border_left; }
  90. const BorderData& border_top() const { return m_noninherited.border_top; }
  91. const BorderData& border_right() const { return m_noninherited.border_right; }
  92. const BorderData& border_bottom() const { return m_noninherited.border_bottom; }
  93. const CSS::Length& border_bottom_left_radius() const { return m_noninherited.border_bottom_left_radius; }
  94. const CSS::Length& border_bottom_right_radius() const { return m_noninherited.border_bottom_right_radius; }
  95. const CSS::Length& border_top_left_radius() const { return m_noninherited.border_top_left_radius; }
  96. const CSS::Length& border_top_right_radius() const { return m_noninherited.border_top_right_radius; }
  97. CSS::Overflow overflow_x() const { return m_noninherited.overflow_x; }
  98. CSS::Overflow overflow_y() const { return m_noninherited.overflow_y; }
  99. Color color() const { return m_inherited.color; }
  100. Color background_color() const { return m_noninherited.background_color; }
  101. CSS::Repeat background_repeat_x() const { return m_noninherited.background_repeat_x; }
  102. CSS::Repeat background_repeat_y() const { return m_noninherited.background_repeat_y; }
  103. CSS::ListStyleType list_style_type() const { return m_inherited.list_style_type; }
  104. Optional<Color> fill() const { return m_inherited.fill; }
  105. Optional<Color> stroke() const { return m_inherited.stroke; }
  106. Optional<Length> const& stroke_width() const { return m_inherited.stroke_width; }
  107. Vector<CSS::Transformation> transformations() const { return m_noninherited.transformations; }
  108. ComputedValues clone_inherited_values() const
  109. {
  110. ComputedValues clone;
  111. clone.m_inherited = m_inherited;
  112. return clone;
  113. }
  114. protected:
  115. struct {
  116. Color color { InitialValues::color() };
  117. CSS::Cursor cursor { InitialValues::cursor() };
  118. CSS::PointerEvents pointer_events { InitialValues::pointer_events() };
  119. CSS::TextAlign text_align { InitialValues::text_align() };
  120. CSS::TextTransform text_transform { InitialValues::text_transform() };
  121. CSS::WhiteSpace white_space { InitialValues::white_space() };
  122. CSS::ListStyleType list_style_type { InitialValues::list_style_type() };
  123. Optional<Color> fill;
  124. Optional<Color> stroke;
  125. Optional<Length> stroke_width;
  126. } m_inherited;
  127. struct {
  128. CSS::Float float_ { InitialValues::float_() };
  129. CSS::Clear clear { InitialValues::clear() };
  130. CSS::Display display { InitialValues::display() };
  131. Optional<int> z_index;
  132. CSS::TextDecorationLine text_decoration_line { InitialValues::text_decoration_line() };
  133. CSS::Position position { InitialValues::position() };
  134. CSS::Length width;
  135. CSS::Length min_width;
  136. CSS::Length max_width;
  137. CSS::Length height;
  138. CSS::Length min_height;
  139. CSS::Length max_height;
  140. CSS::LengthBox offset;
  141. CSS::LengthBox margin;
  142. CSS::LengthBox padding;
  143. BorderData border_left;
  144. BorderData border_top;
  145. BorderData border_right;
  146. BorderData border_bottom;
  147. Length border_bottom_left_radius;
  148. Length border_bottom_right_radius;
  149. Length border_top_left_radius;
  150. Length border_top_right_radius;
  151. Color background_color { InitialValues::background_color() };
  152. CSS::Repeat background_repeat_x { InitialValues::background_repeat() };
  153. CSS::Repeat background_repeat_y { InitialValues::background_repeat() };
  154. CSS::FlexDirection flex_direction { InitialValues::flex_direction() };
  155. CSS::FlexWrap flex_wrap { InitialValues::flex_wrap() };
  156. CSS::FlexBasisData flex_basis {};
  157. float flex_grow { InitialValues::flex_grow() };
  158. float flex_shrink { InitialValues::flex_shrink() };
  159. ;
  160. CSS::AlignItems align_items { InitialValues::align_items() };
  161. CSS::JustifyContent justify_content { InitialValues::justify_content() };
  162. CSS::Overflow overflow_x { InitialValues::overflow() };
  163. CSS::Overflow overflow_y { InitialValues::overflow() };
  164. Optional<float> opacity;
  165. Optional<BoxShadowData> box_shadow {};
  166. Vector<CSS::Transformation> transformations {};
  167. CSS::BoxSizing box_sizing { InitialValues::box_sizing() };
  168. } m_noninherited;
  169. };
  170. class ImmutableComputedValues final : public ComputedValues {
  171. };
  172. class MutableComputedValues final : public ComputedValues {
  173. public:
  174. void set_color(const Color& color) { m_inherited.color = color; }
  175. void set_cursor(CSS::Cursor cursor) { m_inherited.cursor = cursor; }
  176. void set_pointer_events(CSS::PointerEvents value) { m_inherited.pointer_events = value; }
  177. void set_background_color(const Color& color) { m_noninherited.background_color = color; }
  178. void set_background_repeat_x(CSS::Repeat repeat) { m_noninherited.background_repeat_x = repeat; }
  179. void set_background_repeat_y(CSS::Repeat repeat) { m_noninherited.background_repeat_y = repeat; }
  180. void set_float(CSS::Float value) { m_noninherited.float_ = value; }
  181. void set_clear(CSS::Clear value) { m_noninherited.clear = value; }
  182. void set_z_index(Optional<int> value) { m_noninherited.z_index = value; }
  183. void set_text_align(CSS::TextAlign text_align) { m_inherited.text_align = text_align; }
  184. void set_text_decoration_line(CSS::TextDecorationLine value) { m_noninherited.text_decoration_line = value; }
  185. void set_text_transform(CSS::TextTransform value) { m_inherited.text_transform = value; }
  186. void set_position(CSS::Position position) { m_noninherited.position = position; }
  187. void set_white_space(CSS::WhiteSpace value) { m_inherited.white_space = value; }
  188. void set_width(const CSS::Length& width) { m_noninherited.width = width; }
  189. void set_min_width(const CSS::Length& width) { m_noninherited.min_width = width; }
  190. void set_max_width(const CSS::Length& width) { m_noninherited.max_width = width; }
  191. void set_height(const CSS::Length& height) { m_noninherited.height = height; }
  192. void set_min_height(const CSS::Length& height) { m_noninherited.min_height = height; }
  193. void set_max_height(const CSS::Length& height) { m_noninherited.max_height = height; }
  194. void set_offset(const CSS::LengthBox& offset) { m_noninherited.offset = offset; }
  195. void set_margin(const CSS::LengthBox& margin) { m_noninherited.margin = margin; }
  196. void set_padding(const CSS::LengthBox& padding) { m_noninherited.padding = padding; }
  197. void set_overflow_x(CSS::Overflow value) { m_noninherited.overflow_x = value; }
  198. void set_overflow_y(CSS::Overflow value) { m_noninherited.overflow_y = value; }
  199. void set_list_style_type(CSS::ListStyleType value) { m_inherited.list_style_type = value; }
  200. void set_display(CSS::Display value) { m_noninherited.display = value; }
  201. void set_border_bottom_left_radius(CSS::Length value) { m_noninherited.border_bottom_left_radius = value; }
  202. void set_border_bottom_right_radius(CSS::Length value) { m_noninherited.border_bottom_right_radius = value; }
  203. void set_border_top_left_radius(CSS::Length value) { m_noninherited.border_top_left_radius = value; }
  204. void set_border_top_right_radius(CSS::Length value) { m_noninherited.border_top_right_radius = value; }
  205. BorderData& border_left() { return m_noninherited.border_left; }
  206. BorderData& border_top() { return m_noninherited.border_top; }
  207. BorderData& border_right() { return m_noninherited.border_right; }
  208. BorderData& border_bottom() { return m_noninherited.border_bottom; }
  209. void set_flex_direction(CSS::FlexDirection value) { m_noninherited.flex_direction = value; }
  210. void set_flex_wrap(CSS::FlexWrap value) { m_noninherited.flex_wrap = value; }
  211. void set_flex_basis(FlexBasisData value) { m_noninherited.flex_basis = value; }
  212. void set_flex_grow(float value) { m_noninherited.flex_grow = value; }
  213. void set_flex_shrink(float value) { m_noninherited.flex_shrink = value; }
  214. void set_align_items(CSS::AlignItems value) { m_noninherited.align_items = value; }
  215. void set_opacity(Optional<float> value) { m_noninherited.opacity = value; }
  216. void set_justify_content(CSS::JustifyContent value) { m_noninherited.justify_content = value; }
  217. void set_box_shadow(Optional<BoxShadowData> value) { m_noninherited.box_shadow = move(value); }
  218. void set_transformations(Vector<CSS::Transformation> value) { m_noninherited.transformations = move(value); }
  219. void set_box_sizing(CSS::BoxSizing value) { m_noninherited.box_sizing = value; }
  220. void set_fill(Color value) { m_inherited.fill = value; }
  221. void set_stroke(Color value) { m_inherited.stroke = value; }
  222. void set_stroke_width(Length value) { m_inherited.stroke_width = value; }
  223. };
  224. }