ComputedValues.h 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  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 float font_size() { return 10; }
  14. static int font_weight() { return 400; }
  15. static CSS::Float float_() { return CSS::Float::None; }
  16. static CSS::Clear clear() { return CSS::Clear::None; }
  17. static CSS::Cursor cursor() { return CSS::Cursor::Auto; }
  18. static CSS::WhiteSpace white_space() { return CSS::WhiteSpace::Normal; }
  19. static CSS::TextAlign text_align() { return CSS::TextAlign::Left; }
  20. static CSS::TextJustify text_justify() { return CSS::TextJustify::Auto; }
  21. static CSS::Position position() { return CSS::Position::Static; }
  22. static CSS::TextDecorationLine text_decoration_line() { return CSS::TextDecorationLine::None; }
  23. static CSS::Length text_decoration_thickness() { return Length::make_px(1); }
  24. static CSS::TextDecorationStyle text_decoration_style() { return CSS::TextDecorationStyle::Solid; }
  25. static CSS::TextTransform text_transform() { return CSS::TextTransform::None; }
  26. static CSS::Display display() { return CSS::Display { CSS::Display::Outside::Inline, CSS::Display::Inside::Flow }; }
  27. static Color color() { return Color::Black; }
  28. static Color background_color() { return Color::Transparent; }
  29. static CSS::ListStyleType list_style_type() { return CSS::ListStyleType::Disc; }
  30. static CSS::Visibility visibility() { return CSS::Visibility::Visible; }
  31. static CSS::FlexDirection flex_direction() { return CSS::FlexDirection::Row; }
  32. static CSS::FlexWrap flex_wrap() { return CSS::FlexWrap::Nowrap; }
  33. static CSS::ImageRendering image_rendering() { return CSS::ImageRendering::Auto; }
  34. static CSS::JustifyContent justify_content() { return CSS::JustifyContent::FlexStart; }
  35. static CSS::AlignItems align_items() { return CSS::AlignItems::Stretch; }
  36. static CSS::Overflow overflow() { return CSS::Overflow::Visible; }
  37. static CSS::BoxSizing box_sizing() { return CSS::BoxSizing::ContentBox; }
  38. static CSS::PointerEvents pointer_events() { return CSS::PointerEvents::Auto; }
  39. static float flex_grow() { return 0.0f; }
  40. static float flex_shrink() { return 1.0f; }
  41. static float opacity() { return 1.0f; }
  42. static CSS::Length border_radius() { return Length::make_px(0); }
  43. static Variant<CSS::VerticalAlign, CSS::LengthPercentage> vertical_align() { return CSS::VerticalAlign::Baseline; }
  44. };
  45. struct BackgroundLayerData {
  46. RefPtr<CSS::ImageStyleValue> image { nullptr };
  47. CSS::BackgroundAttachment attachment { CSS::BackgroundAttachment::Scroll };
  48. CSS::BackgroundBox origin { CSS::BackgroundBox::PaddingBox };
  49. CSS::BackgroundBox clip { CSS::BackgroundBox::BorderBox };
  50. CSS::PositionEdge position_edge_x { CSS::PositionEdge::Left };
  51. CSS::LengthPercentage position_offset_x { CSS::Length::make_px(0) };
  52. CSS::PositionEdge position_edge_y { CSS::PositionEdge::Top };
  53. CSS::LengthPercentage position_offset_y { CSS::Length::make_px(0) };
  54. CSS::BackgroundSize size_type { CSS::BackgroundSize::LengthPercentage };
  55. CSS::LengthPercentage size_x { CSS::Length::make_auto() };
  56. CSS::LengthPercentage size_y { CSS::Length::make_auto() };
  57. CSS::Repeat repeat_x { CSS::Repeat::Repeat };
  58. CSS::Repeat repeat_y { CSS::Repeat::Repeat };
  59. };
  60. struct BorderData {
  61. public:
  62. Color color { Color::Transparent };
  63. CSS::LineStyle line_style { CSS::LineStyle::None };
  64. float width { 0 };
  65. };
  66. struct Transformation {
  67. CSS::TransformFunction function;
  68. Vector<Variant<CSS::LengthPercentage, float>> values;
  69. };
  70. struct TransformOrigin {
  71. CSS::LengthPercentage x { Percentage(50) };
  72. CSS::LengthPercentage y { Percentage(50) };
  73. };
  74. struct FlexBasisData {
  75. CSS::FlexBasis type { CSS::FlexBasis::Auto };
  76. Optional<CSS::LengthPercentage> length_percentage;
  77. bool is_definite() const { return type == CSS::FlexBasis::LengthPercentage; }
  78. };
  79. struct BoxShadowData {
  80. Color color {};
  81. CSS::Length offset_x { Length::make_px(0) };
  82. CSS::Length offset_y { Length::make_px(0) };
  83. CSS::Length blur_radius { Length::make_px(0) };
  84. CSS::Length spread_distance { Length::make_px(0) };
  85. CSS::BoxShadowPlacement placement { CSS::BoxShadowPlacement::Outer };
  86. };
  87. struct ContentData {
  88. enum class Type {
  89. Normal,
  90. None,
  91. String,
  92. } type { Type::Normal };
  93. // FIXME: Data is a list of identifiers, strings and image values.
  94. String data {};
  95. String alt_text {};
  96. };
  97. class ComputedValues {
  98. public:
  99. CSS::Float float_() const { return m_noninherited.float_; }
  100. CSS::Clear clear() const { return m_noninherited.clear; }
  101. CSS::Cursor cursor() const { return m_inherited.cursor; }
  102. CSS::ContentData content() const { return m_noninherited.content; }
  103. CSS::PointerEvents pointer_events() const { return m_inherited.pointer_events; }
  104. CSS::Display display() const { return m_noninherited.display; }
  105. Optional<int> const& z_index() const { return m_noninherited.z_index; }
  106. CSS::TextAlign text_align() const { return m_inherited.text_align; }
  107. CSS::TextJustify text_justify() const { return m_inherited.text_justify; }
  108. CSS::TextDecorationLine text_decoration_line() const { return m_noninherited.text_decoration_line; }
  109. CSS::LengthPercentage text_decoration_thickness() const { return m_noninherited.text_decoration_thickness; }
  110. CSS::TextDecorationStyle text_decoration_style() const { return m_noninherited.text_decoration_style; }
  111. Color text_decoration_color() const { return m_noninherited.text_decoration_color; }
  112. CSS::TextTransform text_transform() const { return m_inherited.text_transform; }
  113. CSS::Position position() const { return m_noninherited.position; }
  114. CSS::WhiteSpace white_space() const { return m_inherited.white_space; }
  115. CSS::FlexDirection flex_direction() const { return m_noninherited.flex_direction; }
  116. CSS::FlexWrap flex_wrap() const { return m_noninherited.flex_wrap; }
  117. FlexBasisData const& flex_basis() const { return m_noninherited.flex_basis; }
  118. float flex_grow() const { return m_noninherited.flex_grow; }
  119. float flex_shrink() const { return m_noninherited.flex_shrink; }
  120. CSS::AlignItems align_items() const { return m_noninherited.align_items; }
  121. float opacity() const { return m_noninherited.opacity; }
  122. CSS::Visibility visibility() const { return m_inherited.visibility; }
  123. CSS::ImageRendering image_rendering() const { return m_inherited.image_rendering; }
  124. CSS::JustifyContent justify_content() const { return m_noninherited.justify_content; }
  125. Vector<BoxShadowData> const& box_shadow() const { return m_noninherited.box_shadow; }
  126. CSS::BoxSizing box_sizing() const { return m_noninherited.box_sizing; }
  127. Optional<CSS::LengthPercentage> const& width() const { return m_noninherited.width; }
  128. Optional<CSS::LengthPercentage> const& min_width() const { return m_noninherited.min_width; }
  129. Optional<CSS::LengthPercentage> const& max_width() const { return m_noninherited.max_width; }
  130. Optional<CSS::LengthPercentage> const& height() const { return m_noninherited.height; }
  131. Optional<CSS::LengthPercentage> const& min_height() const { return m_noninherited.min_height; }
  132. Optional<CSS::LengthPercentage> const& max_height() const { return m_noninherited.max_height; }
  133. Variant<CSS::VerticalAlign, CSS::LengthPercentage> const& vertical_align() const { return m_noninherited.vertical_align; }
  134. const CSS::LengthBox& offset() const { return m_noninherited.offset; }
  135. const CSS::LengthBox& margin() const { return m_noninherited.margin; }
  136. const CSS::LengthBox& padding() const { return m_noninherited.padding; }
  137. const BorderData& border_left() const { return m_noninherited.border_left; }
  138. const BorderData& border_top() const { return m_noninherited.border_top; }
  139. const BorderData& border_right() const { return m_noninherited.border_right; }
  140. const BorderData& border_bottom() const { return m_noninherited.border_bottom; }
  141. const CSS::LengthPercentage& border_bottom_left_radius() const { return m_noninherited.border_bottom_left_radius; }
  142. const CSS::LengthPercentage& border_bottom_right_radius() const { return m_noninherited.border_bottom_right_radius; }
  143. const CSS::LengthPercentage& border_top_left_radius() const { return m_noninherited.border_top_left_radius; }
  144. const CSS::LengthPercentage& border_top_right_radius() const { return m_noninherited.border_top_right_radius; }
  145. CSS::Overflow overflow_x() const { return m_noninherited.overflow_x; }
  146. CSS::Overflow overflow_y() const { return m_noninherited.overflow_y; }
  147. Color color() const { return m_inherited.color; }
  148. Color background_color() const { return m_noninherited.background_color; }
  149. Vector<BackgroundLayerData> const& background_layers() const { return m_noninherited.background_layers; }
  150. CSS::ListStyleType list_style_type() const { return m_inherited.list_style_type; }
  151. Optional<Color> fill() const { return m_inherited.fill; }
  152. Optional<Color> stroke() const { return m_inherited.stroke; }
  153. Optional<LengthPercentage> const& stroke_width() const { return m_inherited.stroke_width; }
  154. Vector<CSS::Transformation> transformations() const { return m_noninherited.transformations; }
  155. CSS::TransformOrigin transform_origin() const { return m_noninherited.transform_origin; }
  156. float font_size() const { return m_inherited.font_size; }
  157. int font_weight() const { return m_inherited.font_weight; }
  158. ComputedValues clone_inherited_values() const
  159. {
  160. ComputedValues clone;
  161. clone.m_inherited = m_inherited;
  162. return clone;
  163. }
  164. protected:
  165. struct {
  166. float font_size { InitialValues::font_size() };
  167. int font_weight { InitialValues::font_weight() };
  168. Color color { InitialValues::color() };
  169. CSS::Cursor cursor { InitialValues::cursor() };
  170. CSS::ImageRendering image_rendering { InitialValues::image_rendering() };
  171. CSS::PointerEvents pointer_events { InitialValues::pointer_events() };
  172. CSS::TextAlign text_align { InitialValues::text_align() };
  173. CSS::TextJustify text_justify { InitialValues::text_justify() };
  174. CSS::TextTransform text_transform { InitialValues::text_transform() };
  175. CSS::WhiteSpace white_space { InitialValues::white_space() };
  176. CSS::ListStyleType list_style_type { InitialValues::list_style_type() };
  177. CSS::Visibility visibility { InitialValues::visibility() };
  178. Optional<Color> fill;
  179. Optional<Color> stroke;
  180. Optional<LengthPercentage> stroke_width;
  181. } m_inherited;
  182. struct {
  183. CSS::Float float_ { InitialValues::float_() };
  184. CSS::Clear clear { InitialValues::clear() };
  185. CSS::Display display { InitialValues::display() };
  186. Optional<int> z_index;
  187. CSS::TextDecorationLine text_decoration_line { InitialValues::text_decoration_line() };
  188. CSS::LengthPercentage text_decoration_thickness { InitialValues::text_decoration_thickness() };
  189. CSS::TextDecorationStyle text_decoration_style { InitialValues::text_decoration_style() };
  190. Color text_decoration_color { InitialValues::color() };
  191. CSS::Position position { InitialValues::position() };
  192. Optional<CSS::LengthPercentage> width;
  193. Optional<CSS::LengthPercentage> min_width;
  194. Optional<CSS::LengthPercentage> max_width;
  195. Optional<CSS::LengthPercentage> height;
  196. Optional<CSS::LengthPercentage> min_height;
  197. Optional<CSS::LengthPercentage> max_height;
  198. CSS::LengthBox offset;
  199. CSS::LengthBox margin;
  200. CSS::LengthBox padding;
  201. BorderData border_left;
  202. BorderData border_top;
  203. BorderData border_right;
  204. BorderData border_bottom;
  205. LengthPercentage border_bottom_left_radius { InitialValues::border_radius() };
  206. LengthPercentage border_bottom_right_radius { InitialValues::border_radius() };
  207. LengthPercentage border_top_left_radius { InitialValues::border_radius() };
  208. LengthPercentage border_top_right_radius { InitialValues::border_radius() };
  209. Color background_color { InitialValues::background_color() };
  210. Vector<BackgroundLayerData> background_layers;
  211. CSS::FlexDirection flex_direction { InitialValues::flex_direction() };
  212. CSS::FlexWrap flex_wrap { InitialValues::flex_wrap() };
  213. CSS::FlexBasisData flex_basis {};
  214. float flex_grow { InitialValues::flex_grow() };
  215. float flex_shrink { InitialValues::flex_shrink() };
  216. CSS::AlignItems align_items { InitialValues::align_items() };
  217. CSS::JustifyContent justify_content { InitialValues::justify_content() };
  218. CSS::Overflow overflow_x { InitialValues::overflow() };
  219. CSS::Overflow overflow_y { InitialValues::overflow() };
  220. float opacity { InitialValues::opacity() };
  221. Vector<BoxShadowData> box_shadow {};
  222. Vector<CSS::Transformation> transformations {};
  223. CSS::TransformOrigin transform_origin {};
  224. CSS::BoxSizing box_sizing { InitialValues::box_sizing() };
  225. CSS::ContentData content;
  226. Variant<CSS::VerticalAlign, CSS::LengthPercentage> vertical_align { InitialValues::vertical_align() };
  227. } m_noninherited;
  228. };
  229. class ImmutableComputedValues final : public ComputedValues {
  230. };
  231. class MutableComputedValues final : public ComputedValues {
  232. public:
  233. void set_font_size(float font_size) { m_inherited.font_size = font_size; }
  234. void set_font_weight(int font_weight) { m_inherited.font_weight = font_weight; }
  235. void set_color(const Color& color) { m_inherited.color = color; }
  236. void set_content(ContentData const& content) { m_noninherited.content = content; }
  237. void set_cursor(CSS::Cursor cursor) { m_inherited.cursor = cursor; }
  238. void set_image_rendering(CSS::ImageRendering value) { m_inherited.image_rendering = value; }
  239. void set_pointer_events(CSS::PointerEvents value) { m_inherited.pointer_events = value; }
  240. void set_background_color(const Color& color) { m_noninherited.background_color = color; }
  241. void set_background_layers(Vector<BackgroundLayerData>&& layers) { m_noninherited.background_layers = move(layers); }
  242. void set_float(CSS::Float value) { m_noninherited.float_ = value; }
  243. void set_clear(CSS::Clear value) { m_noninherited.clear = value; }
  244. void set_z_index(Optional<int> value) { m_noninherited.z_index = value; }
  245. void set_text_align(CSS::TextAlign text_align) { m_inherited.text_align = text_align; }
  246. void set_text_justify(CSS::TextJustify text_justify) { m_inherited.text_justify = text_justify; }
  247. void set_text_decoration_line(CSS::TextDecorationLine value) { m_noninherited.text_decoration_line = value; }
  248. void set_text_decoration_thickness(CSS::LengthPercentage value) { m_noninherited.text_decoration_thickness = value; }
  249. void set_text_decoration_style(CSS::TextDecorationStyle value) { m_noninherited.text_decoration_style = value; }
  250. void set_text_decoration_color(Color value) { m_noninherited.text_decoration_color = value; }
  251. void set_text_transform(CSS::TextTransform value) { m_inherited.text_transform = value; }
  252. void set_position(CSS::Position position) { m_noninherited.position = position; }
  253. void set_white_space(CSS::WhiteSpace value) { m_inherited.white_space = value; }
  254. void set_width(CSS::LengthPercentage const& width) { m_noninherited.width = width; }
  255. void set_min_width(CSS::LengthPercentage const& width) { m_noninherited.min_width = width; }
  256. void set_max_width(CSS::LengthPercentage const& width) { m_noninherited.max_width = width; }
  257. void set_height(CSS::LengthPercentage const& height) { m_noninherited.height = height; }
  258. void set_min_height(CSS::LengthPercentage const& height) { m_noninherited.min_height = height; }
  259. void set_max_height(CSS::LengthPercentage const& height) { m_noninherited.max_height = height; }
  260. void set_offset(const CSS::LengthBox& offset) { m_noninherited.offset = offset; }
  261. void set_margin(const CSS::LengthBox& margin) { m_noninherited.margin = margin; }
  262. void set_padding(const CSS::LengthBox& padding) { m_noninherited.padding = padding; }
  263. void set_overflow_x(CSS::Overflow value) { m_noninherited.overflow_x = value; }
  264. void set_overflow_y(CSS::Overflow value) { m_noninherited.overflow_y = value; }
  265. void set_list_style_type(CSS::ListStyleType value) { m_inherited.list_style_type = value; }
  266. void set_display(CSS::Display value) { m_noninherited.display = value; }
  267. void set_border_bottom_left_radius(CSS::LengthPercentage value) { m_noninherited.border_bottom_left_radius = value; }
  268. void set_border_bottom_right_radius(CSS::LengthPercentage value) { m_noninherited.border_bottom_right_radius = value; }
  269. void set_border_top_left_radius(CSS::LengthPercentage value) { m_noninherited.border_top_left_radius = value; }
  270. void set_border_top_right_radius(CSS::LengthPercentage value) { m_noninherited.border_top_right_radius = value; }
  271. BorderData& border_left() { return m_noninherited.border_left; }
  272. BorderData& border_top() { return m_noninherited.border_top; }
  273. BorderData& border_right() { return m_noninherited.border_right; }
  274. BorderData& border_bottom() { return m_noninherited.border_bottom; }
  275. void set_flex_direction(CSS::FlexDirection value) { m_noninherited.flex_direction = value; }
  276. void set_flex_wrap(CSS::FlexWrap value) { m_noninherited.flex_wrap = value; }
  277. void set_flex_basis(FlexBasisData value) { m_noninherited.flex_basis = value; }
  278. void set_flex_grow(float value) { m_noninherited.flex_grow = value; }
  279. void set_flex_shrink(float value) { m_noninherited.flex_shrink = value; }
  280. void set_align_items(CSS::AlignItems value) { m_noninherited.align_items = value; }
  281. void set_opacity(float value) { m_noninherited.opacity = value; }
  282. void set_justify_content(CSS::JustifyContent value) { m_noninherited.justify_content = value; }
  283. void set_box_shadow(Vector<BoxShadowData>&& value) { m_noninherited.box_shadow = move(value); }
  284. void set_transformations(Vector<CSS::Transformation> value) { m_noninherited.transformations = move(value); }
  285. void set_transform_origin(CSS::TransformOrigin value) { m_noninherited.transform_origin = value; }
  286. void set_box_sizing(CSS::BoxSizing value) { m_noninherited.box_sizing = value; }
  287. void set_vertical_align(Variant<CSS::VerticalAlign, CSS::LengthPercentage> value) { m_noninherited.vertical_align = value; }
  288. void set_visibility(CSS::Visibility value) { m_inherited.visibility = value; }
  289. void set_fill(Color value) { m_inherited.fill = value; }
  290. void set_stroke(Color value) { m_inherited.stroke = value; }
  291. void set_stroke_width(LengthPercentage value) { m_inherited.stroke_width = value; }
  292. };
  293. }