ComputedValues.h 19 KB

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