ComputedValues.h 12 KB

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