ComputeProperty.cpp 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  1. /*
  2. * This source file is part of RmlUi, the HTML/CSS Interface Middleware
  3. *
  4. * For the latest information, see http://github.com/mikke89/RmlUi
  5. *
  6. * Copyright (c) 2008-2010 CodePoint Ltd, Shift Technology Ltd
  7. * Copyright (c) 2019 The RmlUi Team, and contributors
  8. *
  9. * Permission is hereby granted, free of charge, to any person obtaining a copy
  10. * of this software and associated documentation files (the "Software"), to deal
  11. * in the Software without restriction, including without limitation the rights
  12. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  13. * copies of the Software, and to permit persons to whom the Software is
  14. * furnished to do so, subject to the following conditions:
  15. *
  16. * The above copyright notice and this permission notice shall be included in
  17. * all copies or substantial portions of the Software.
  18. *
  19. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  20. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  21. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  22. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  23. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  24. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  25. * THE SOFTWARE.
  26. *
  27. */
  28. #include "../../Include/RmlUi/Core/Property.h"
  29. #include "ComputeProperty.h"
  30. namespace Rml {
  31. const Style::ComputedValues DefaultComputedValues = Style::ComputedValues{};
  32. static constexpr float PixelsPerInch = 96.0f;
  33. float ResolveValue(Style::LengthPercentageAuto length, float base_value)
  34. {
  35. if (length.type == Style::LengthPercentageAuto::Length)
  36. return length.value;
  37. else if (length.type == Style::LengthPercentageAuto::Percentage)
  38. return length.value * 0.01f * base_value;
  39. return 0.0f;
  40. }
  41. float ResolveValue(Style::LengthPercentage length, float base_value)
  42. {
  43. if (length.type == Style::LengthPercentage::Length)
  44. return length.value;
  45. else if (length.type == Style::LengthPercentage::Percentage)
  46. return length.value * 0.01f * base_value;
  47. return 0.0f;
  48. }
  49. float ComputeLength(const Property* property, float font_size, float document_font_size, float dp_ratio, Vector2f vp_dimensions)
  50. {
  51. RMLUI_ASSERT(property);
  52. float value = property->value.Get<float>();
  53. switch (property->unit)
  54. {
  55. case Property::NUMBER:
  56. case Property::PX:
  57. case Property::RAD:
  58. return value;
  59. case Property::EM:
  60. return value * font_size;
  61. case Property::REM:
  62. return value * document_font_size;
  63. case Property::DP:
  64. return value * dp_ratio;
  65. case Property::VW:
  66. return value * vp_dimensions.x * 0.01f;
  67. case Property::VH:
  68. return value * vp_dimensions.y * 0.01f;
  69. case Property::DEG:
  70. return Math::DegreesToRadians(value);
  71. default:
  72. break;
  73. }
  74. // Values based on pixels-per-inch.
  75. if (property->unit & Property::PPI_UNIT)
  76. {
  77. float inch = value * PixelsPerInch;
  78. switch (property->unit)
  79. {
  80. case Property::INCH: // inch
  81. return inch;
  82. case Property::CM: // centimeter
  83. return inch * (1.0f / 2.54f);
  84. case Property::MM: // millimeter
  85. return inch * (1.0f / 25.4f);
  86. case Property::PT: // point
  87. return inch * (1.0f / 72.0f);
  88. case Property::PC: // pica
  89. return inch * (1.0f / 6.0f);
  90. default:
  91. break;
  92. }
  93. }
  94. // We're not a numeric property; return 0.
  95. return 0.0f;
  96. }
  97. float ComputeAbsoluteLength(const Property& property, float dp_ratio, Vector2f vp_dimensions)
  98. {
  99. RMLUI_ASSERT(property.unit & Property::ABSOLUTE_LENGTH);
  100. switch (property.unit)
  101. {
  102. case Property::PX:
  103. return property.value.Get< float >();
  104. case Property::DP:
  105. return property.value.Get< float >() * dp_ratio;
  106. case Property::VW:
  107. return property.value.Get< float >() * vp_dimensions.x * 0.01f;
  108. case Property::VH:
  109. return property.value.Get< float >() * vp_dimensions.y * 0.01f;
  110. default:
  111. // Values based on pixels-per-inch.
  112. if (property.unit & Property::PPI_UNIT)
  113. {
  114. float inch = property.value.Get< float >() * PixelsPerInch;
  115. switch (property.unit)
  116. {
  117. case Property::INCH: // inch
  118. return inch;
  119. case Property::CM: // centimeter
  120. return inch * (1.0f / 2.54f);
  121. case Property::MM: // millimeter
  122. return inch * (1.0f / 25.4f);
  123. case Property::PT: // point
  124. return inch * (1.0f / 72.0f);
  125. case Property::PC: // pica
  126. return inch * (1.0f / 6.0f);
  127. default:
  128. break;
  129. }
  130. }
  131. }
  132. RMLUI_ERROR;
  133. return 0.0f;
  134. }
  135. float ComputeAngle(const Property& property)
  136. {
  137. float value = property.value.Get<float>();
  138. switch (property.unit)
  139. {
  140. case Property::NUMBER:
  141. case Property::RAD:
  142. return value;
  143. case Property::DEG:
  144. return Math::DegreesToRadians(value);
  145. default:
  146. break;
  147. }
  148. return 0.0f;
  149. }
  150. String ComputeFontFamily(String font_family)
  151. {
  152. return StringUtilities::ToLower(std::move(font_family));
  153. }
  154. float ComputeFontsize(const Property& property, const Style::ComputedValues& values, const Style::ComputedValues* parent_values, const Style::ComputedValues* document_values, float dp_ratio, Vector2f vp_dimensions)
  155. {
  156. // The calculated value of the font-size property is inherited, so we need to check if this
  157. // is an inherited property. If so, then we return our parent's font size instead.
  158. if (property.unit & Property::RELATIVE_UNIT)
  159. {
  160. float multiplier = 1.0f;
  161. switch (property.unit)
  162. {
  163. case Property::PERCENT:
  164. multiplier = 0.01f;
  165. //-fallthrough
  166. case Property::EM:
  167. if (!parent_values)
  168. return 0;
  169. return property.value.Get< float >() * multiplier * parent_values->font_size;
  170. case Property::REM:
  171. if (!document_values)
  172. return 0;
  173. // If the current element is a document, the rem unit is relative to the default size
  174. if(&values == document_values)
  175. return property.value.Get< float >() * DefaultComputedValues.font_size;
  176. // Otherwise it is relative to the document font size
  177. return property.value.Get< float >() * document_values->font_size;
  178. default:
  179. RMLUI_ERRORMSG("A relative unit must be percentage, em or rem.");
  180. }
  181. }
  182. return ComputeAbsoluteLength(property, dp_ratio, vp_dimensions);
  183. }
  184. Style::Clip ComputeClip(const Property* property)
  185. {
  186. const int value = property->Get<int>();
  187. if (property->unit == Property::KEYWORD)
  188. return Style::Clip(static_cast<Style::Clip::Type>(value));
  189. else if (property->unit == Property::NUMBER)
  190. return Style::Clip(Style::Clip::Type::Number, static_cast<int8_t>(value));
  191. RMLUI_ERRORMSG("Invalid clip type");
  192. return Style::Clip();
  193. }
  194. Style::LineHeight ComputeLineHeight(const Property* property, float font_size, float document_font_size, float dp_ratio, Vector2f vp_dimensions)
  195. {
  196. if (property->unit & Property::LENGTH)
  197. {
  198. float value = ComputeLength(property, font_size, document_font_size, dp_ratio, vp_dimensions);
  199. return Style::LineHeight(value, Style::LineHeight::Length, value);
  200. }
  201. float scale_factor = 1.0f;
  202. switch (property->unit)
  203. {
  204. case Property::NUMBER:
  205. scale_factor = property->value.Get< float >();
  206. break;
  207. case Property::PERCENT:
  208. scale_factor = property->value.Get< float >() * 0.01f;
  209. break;
  210. default:
  211. RMLUI_ERRORMSG("Invalid unit for line-height");
  212. }
  213. float value = font_size * scale_factor;
  214. return Style::LineHeight(value, Style::LineHeight::Number, scale_factor);
  215. }
  216. Style::VerticalAlign ComputeVerticalAlign(const Property* property, float line_height, float font_size, float document_font_size, float dp_ratio, Vector2f vp_dimensions)
  217. {
  218. if (property->unit & Property::LENGTH)
  219. {
  220. float value = ComputeLength(property, font_size, document_font_size, dp_ratio, vp_dimensions);
  221. return Style::VerticalAlign(value);
  222. }
  223. else if (property->unit & Property::PERCENT)
  224. {
  225. return Style::VerticalAlign(property->Get<float>() * line_height);
  226. }
  227. RMLUI_ASSERT(property->unit & Property::KEYWORD);
  228. return Style::VerticalAlign((Style::VerticalAlign::Type)property->Get<int>());
  229. }
  230. Style::LengthPercentage ComputeLengthPercentage(const Property* property, float font_size, float document_font_size, float dp_ratio, Vector2f vp_dimensions)
  231. {
  232. using namespace Style;
  233. if (property->unit & Property::PERCENT)
  234. return LengthPercentage(LengthPercentage::Percentage, property->Get<float>());
  235. return LengthPercentage(LengthPercentage::Length, ComputeLength(property, font_size, document_font_size, dp_ratio, vp_dimensions));
  236. }
  237. Style::LengthPercentageAuto ComputeLengthPercentageAuto(const Property* property, float font_size, float document_font_size, float dp_ratio, Vector2f vp_dimensions)
  238. {
  239. using namespace Style;
  240. // Assuming here that 'auto' is the only possible keyword
  241. if (property->unit & Property::PERCENT)
  242. return LengthPercentageAuto(LengthPercentageAuto::Percentage, property->Get<float>());
  243. else if (property->unit & Property::KEYWORD)
  244. return LengthPercentageAuto(LengthPercentageAuto::Auto);
  245. return LengthPercentageAuto(LengthPercentageAuto::Length, ComputeLength(property, font_size, document_font_size, dp_ratio, vp_dimensions));
  246. }
  247. Style::LengthPercentage ComputeOrigin(const Property* property, float font_size, float document_font_size, float dp_ratio, Vector2f vp_dimensions)
  248. {
  249. using namespace Style;
  250. static_assert((int)OriginX::Left == (int)OriginY::Top && (int)OriginX::Center == (int)OriginY::Center && (int)OriginX::Right == (int)OriginY::Bottom, "");
  251. if (property->unit & Property::KEYWORD)
  252. {
  253. float percent = 0.0f;
  254. OriginX origin = (OriginX)property->Get<int>();
  255. switch (origin)
  256. {
  257. case OriginX::Left: percent = 0.0f; break;
  258. case OriginX::Center: percent = 50.0f; break;
  259. case OriginX::Right: percent = 100.f; break;
  260. }
  261. return LengthPercentage(LengthPercentage::Percentage, percent);
  262. }
  263. else if (property->unit & Property::PERCENT)
  264. return LengthPercentage(LengthPercentage::Percentage, property->Get<float>());
  265. return LengthPercentage(LengthPercentage::Length, ComputeLength(property, font_size, document_font_size, dp_ratio, vp_dimensions));
  266. }
  267. } // namespace Rml