ComputedValues.h 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. /*
  2. * This source file is part of libRocket, the HTML/CSS Interface Middleware
  3. *
  4. * For the latest information, see http://www.librocket.com
  5. *
  6. * Copyright (c) 2019 Michael R. P. Ragazzon
  7. *
  8. * Permission is hereby granted, free of charge, to any person obtaining a copy
  9. * of this software and associated documentation files (the "Software"), to deal
  10. * in the Software without restriction, including without limitation the rights
  11. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  12. * copies of the Software, and to permit persons to whom the Software is
  13. * furnished to do so, subject to the following conditions:
  14. *
  15. * The above copyright notice and this permission notice shall be included in
  16. * all copies or substantial portions of the Software.
  17. *
  18. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  19. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  20. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  21. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  22. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  23. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  24. * THE SOFTWARE.
  25. *
  26. */
  27. #ifndef ROCKETCORERCSS_H
  28. #define ROCKETCORERCSS_H
  29. namespace Rocket {
  30. namespace Core {
  31. #include "Types.h"
  32. struct LengthPercentageAuto {
  33. enum Type { Length, Percentage, Auto } type = Length;
  34. float value = 0;
  35. LengthPercentageAuto() {}
  36. LengthPercentageAuto(Type type, float value = 0) : type(type), value(value) {}
  37. };
  38. struct LengthPercentage {
  39. enum Type { Length, Percentage } type = Length;
  40. float value = 0;
  41. LengthPercentage() {}
  42. LengthPercentage(Type type, float value = 0) : type(type), value(value) {}
  43. };
  44. struct NumberAuto {
  45. enum Type { Number, Auto } type = Number;
  46. float value = 0;
  47. NumberAuto() {}
  48. NumberAuto(Type type, float value = 0) : type(type), value(value) {}
  49. };
  50. namespace Style
  51. {
  52. enum class Display { None, Block, Inline, InlineBlock };
  53. enum class Position { Static, Relative, Absolute, Fixed };
  54. enum class Float { None, Left, Right };
  55. enum class Clear { None, Left, Right, Both };
  56. struct VerticalAlign {
  57. enum Type { Baseline, Middle, Sub, Super, TextTop, TextBottom, Top, Bottom, Length } type;
  58. float value; // For length type
  59. VerticalAlign(Type type = Baseline) : type(type), value(0) {}
  60. VerticalAlign(float value) : type(Length), value(value) {}
  61. };
  62. enum class Overflow { Visible, Hidden, Auto, Scroll };
  63. enum class Clip { None = -1, Auto = 0, NumberStart = 1}; // Can contain any positive value as number
  64. enum class Visibility { Visible, Hidden };
  65. enum class FontStyle { Normal, Italic };
  66. enum class FontWeight { Normal, Bold };
  67. enum class TextAlign { Left, Right, Center, Justify };
  68. enum class TextDecoration { None, Underline };
  69. enum class TextTransform { None, Capitalize, Uppercase, Lowercase };
  70. enum class WhiteSpace { Normal, Pre, Nowrap, Prewrap, Preline };
  71. enum class Drag { None, Drag, DragDrop, Block, Clone };
  72. enum class TabIndex { None, Auto };
  73. enum class Focus { None, Auto };
  74. enum class PointerEvents { None, Auto };
  75. enum class OriginX { Left, Center, Right };
  76. enum class OriginY { Top, Center, Bottom };
  77. struct LineHeight {
  78. float value = 12.f*1.2f; // The computed value (length)
  79. enum InheritType { Number, Length } inherit_type = Number;
  80. float inherit_value = 1.2f;
  81. LineHeight() {}
  82. LineHeight(float value, InheritType inherit_type, float inherit_value) : value(value), inherit_type(inherit_type), inherit_value(inherit_value) {}
  83. };
  84. // A computed value is a value resolved as far as possible :before: updating layout. See CSS specs for details of each property.
  85. struct ComputedValues
  86. {
  87. LengthPercentageAuto margin_top, margin_right, margin_bottom, margin_left;
  88. LengthPercentage padding_top, padding_right, padding_bottom, padding_left;
  89. float border_top_width = 0, border_right_width = 0, border_bottom_width = 0, border_left_width = 0;
  90. Colourb border_top_color{ 255, 255, 255 }, border_right_color{ 255, 255, 255 }, border_bottom_color{ 255, 255, 255 }, border_left_color{ 255, 255, 255 };
  91. Display display = Display::Inline;
  92. Position position = Position::Static;
  93. LengthPercentageAuto top{ LengthPercentageAuto::Auto };
  94. LengthPercentageAuto right{ LengthPercentageAuto::Auto };
  95. LengthPercentageAuto bottom{ LengthPercentageAuto::Auto };
  96. LengthPercentageAuto left{ LengthPercentageAuto::Auto };
  97. Float float_ = Float::None;
  98. Clear clear = Clear::None;
  99. NumberAuto z_index = { NumberAuto::Auto };
  100. LengthPercentageAuto width = { LengthPercentageAuto::Auto };
  101. LengthPercentage min_width, max_width;
  102. LengthPercentageAuto height = { LengthPercentageAuto::Auto };
  103. LengthPercentage min_height, max_height;
  104. LineHeight line_height;
  105. VerticalAlign vertical_align;
  106. Overflow overflow_x = Overflow::Visible, overflow_y = Overflow::Visible;
  107. Clip clip = Clip::Auto;
  108. Visibility visibility = Visibility::Visible;
  109. Colourb background_color = Colourb(255, 255, 255, 0);
  110. Colourb color = Colourb(255, 255, 255);
  111. Colourb image_color = Colourb(255, 255, 255);
  112. float opacity = 1;
  113. String font_family;
  114. String font_charset; // empty is same as "U+0020-007E"
  115. FontStyle font_style = FontStyle::Normal;
  116. FontWeight font_weight = FontWeight::Normal;
  117. float font_size = 12.f;
  118. TextAlign text_align = TextAlign::Left;
  119. TextDecoration text_decoration = TextDecoration::None;
  120. TextTransform text_transform = TextTransform::None;
  121. WhiteSpace white_space = WhiteSpace::Normal;
  122. String cursor;
  123. Drag drag = Drag::None;
  124. TabIndex tab_index = TabIndex::None;
  125. Focus focus = Focus::Auto;
  126. float scrollbar_margin = 0;
  127. PointerEvents pointer_events = PointerEvents::Auto;
  128. float perspective = 0;
  129. LengthPercentage perspective_origin_x = { LengthPercentage::Percentage, 50.f };
  130. LengthPercentage perspective_origin_y = { LengthPercentage::Percentage, 50.f };
  131. TransformRef transform;
  132. LengthPercentage transform_origin_x = { LengthPercentage::Percentage, 50.f };
  133. LengthPercentage transform_origin_y = { LengthPercentage::Percentage, 50.f };
  134. float transform_origin_z = 0.0f;
  135. TransitionList transition;
  136. AnimationList animation;
  137. };
  138. }
  139. // Note: Auto must be manually handled during layout, here it returns zero.
  140. inline float ResolveProperty(LengthPercentageAuto length, float base_value) {
  141. if (length.type == LengthPercentageAuto::Length)
  142. return length.value;
  143. else if (length.type == LengthPercentageAuto::Percentage)
  144. return length.value * 0.01f * base_value;
  145. return 0.0f;
  146. }
  147. inline float ResolveProperty(LengthPercentage length, float base_value) {
  148. if (length.type == LengthPercentage::Length)
  149. return length.value;
  150. else if (length.type == LengthPercentage::Percentage)
  151. return length.value * 0.01f * base_value;
  152. return 0.0f;
  153. }
  154. using ComputedValues = Style::ComputedValues;
  155. }
  156. }
  157. #endif