ComputeProperty.cpp 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  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 "ComputeProperty.h"
  29. #include "../../Include/RmlUi/Core/ComputedValues.h"
  30. #include "../../Include/RmlUi/Core/Property.h"
  31. namespace Rml {
  32. const Style::ComputedValues DefaultComputedValues{nullptr};
  33. static constexpr float PixelsPerInch = 96.0f;
  34. float ComputeLength(const Property* property, float font_size, float document_font_size, float dp_ratio, Vector2f vp_dimensions)
  35. {
  36. RMLUI_ASSERT(property);
  37. float value = property->value.Get<float>();
  38. switch (property->unit)
  39. {
  40. case Property::NUMBER:
  41. case Property::PX:
  42. case Property::RAD:
  43. return value;
  44. case Property::EM:
  45. return value * font_size;
  46. case Property::REM:
  47. return value * document_font_size;
  48. case Property::DP:
  49. return value * dp_ratio;
  50. case Property::VW:
  51. return value * vp_dimensions.x * 0.01f;
  52. case Property::VH:
  53. return value * vp_dimensions.y * 0.01f;
  54. case Property::DEG:
  55. return Math::DegreesToRadians(value);
  56. default:
  57. break;
  58. }
  59. // Values based on pixels-per-inch.
  60. if (property->unit & Property::PPI_UNIT)
  61. {
  62. float inch = value * PixelsPerInch;
  63. switch (property->unit)
  64. {
  65. case Property::INCH: // inch
  66. return inch;
  67. case Property::CM: // centimeter
  68. return inch * (1.0f / 2.54f);
  69. case Property::MM: // millimeter
  70. return inch * (1.0f / 25.4f);
  71. case Property::PT: // point
  72. return inch * (1.0f / 72.0f);
  73. case Property::PC: // pica
  74. return inch * (1.0f / 6.0f);
  75. default:
  76. break;
  77. }
  78. }
  79. // We're not a numeric property; return 0.
  80. return 0.0f;
  81. }
  82. float ComputeAbsoluteLength(const Property& property, float dp_ratio, Vector2f vp_dimensions)
  83. {
  84. RMLUI_ASSERT(property.unit & Property::ABSOLUTE_LENGTH);
  85. switch (property.unit)
  86. {
  87. case Property::PX:
  88. return property.value.Get< float >();
  89. case Property::DP:
  90. return property.value.Get< float >() * dp_ratio;
  91. case Property::VW:
  92. return property.value.Get< float >() * vp_dimensions.x * 0.01f;
  93. case Property::VH:
  94. return property.value.Get< float >() * vp_dimensions.y * 0.01f;
  95. default:
  96. // Values based on pixels-per-inch.
  97. if (property.unit & Property::PPI_UNIT)
  98. {
  99. float inch = property.value.Get< float >() * PixelsPerInch;
  100. switch (property.unit)
  101. {
  102. case Property::INCH: // inch
  103. return inch;
  104. case Property::CM: // centimeter
  105. return inch * (1.0f / 2.54f);
  106. case Property::MM: // millimeter
  107. return inch * (1.0f / 25.4f);
  108. case Property::PT: // point
  109. return inch * (1.0f / 72.0f);
  110. case Property::PC: // pica
  111. return inch * (1.0f / 6.0f);
  112. default:
  113. break;
  114. }
  115. }
  116. }
  117. RMLUI_ERROR;
  118. return 0.0f;
  119. }
  120. float ComputeAngle(const Property& property)
  121. {
  122. float value = property.value.Get<float>();
  123. switch (property.unit)
  124. {
  125. case Property::NUMBER:
  126. case Property::RAD:
  127. return value;
  128. case Property::DEG:
  129. return Math::DegreesToRadians(value);
  130. default:
  131. break;
  132. }
  133. return 0.0f;
  134. }
  135. String ComputeFontFamily(String font_family)
  136. {
  137. return StringUtilities::ToLower(std::move(font_family));
  138. }
  139. 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)
  140. {
  141. // The calculated value of the font-size property is inherited, so we need to check if this
  142. // is an inherited property. If so, then we return our parent's font size instead.
  143. if (property.unit & Property::RELATIVE_UNIT)
  144. {
  145. float multiplier = 1.0f;
  146. switch (property.unit)
  147. {
  148. case Property::PERCENT:
  149. multiplier = 0.01f;
  150. //-fallthrough
  151. case Property::EM:
  152. if (!parent_values)
  153. return 0;
  154. return property.value.Get<float>() * multiplier * parent_values->font_size();
  155. case Property::REM:
  156. if (!document_values)
  157. return 0;
  158. // If the current element is a document, the rem unit is relative to the default size
  159. if(&values == document_values)
  160. return property.value.Get<float>() * DefaultComputedValues.font_size();
  161. // Otherwise it is relative to the document font size
  162. return property.value.Get<float>() * document_values->font_size();
  163. default:
  164. RMLUI_ERRORMSG("A relative unit must be percentage, em or rem.");
  165. }
  166. }
  167. return ComputeAbsoluteLength(property, dp_ratio, vp_dimensions);
  168. }
  169. Style::Clip ComputeClip(const Property* property)
  170. {
  171. const int value = property->Get<int>();
  172. if (property->unit == Property::KEYWORD)
  173. return Style::Clip(static_cast<Style::Clip::Type>(value));
  174. else if (property->unit == Property::NUMBER)
  175. return Style::Clip(Style::Clip::Type::Number, static_cast<int8_t>(value));
  176. RMLUI_ERRORMSG("Invalid clip type");
  177. return Style::Clip();
  178. }
  179. Style::LineHeight ComputeLineHeight(const Property* property, float font_size, float document_font_size, float dp_ratio, Vector2f vp_dimensions)
  180. {
  181. if (property->unit & Property::LENGTH)
  182. {
  183. float value = ComputeLength(property, font_size, document_font_size, dp_ratio, vp_dimensions);
  184. return Style::LineHeight(value, Style::LineHeight::Length, value);
  185. }
  186. float scale_factor = 1.0f;
  187. switch (property->unit)
  188. {
  189. case Property::NUMBER:
  190. scale_factor = property->value.Get< float >();
  191. break;
  192. case Property::PERCENT:
  193. scale_factor = property->value.Get< float >() * 0.01f;
  194. break;
  195. default:
  196. RMLUI_ERRORMSG("Invalid unit for line-height");
  197. }
  198. float value = font_size * scale_factor;
  199. return Style::LineHeight(value, Style::LineHeight::Number, scale_factor);
  200. }
  201. Style::VerticalAlign ComputeVerticalAlign(const Property* property, float line_height, float font_size, float document_font_size, float dp_ratio, Vector2f vp_dimensions)
  202. {
  203. if (property->unit & Property::LENGTH)
  204. {
  205. float value = ComputeLength(property, font_size, document_font_size, dp_ratio, vp_dimensions);
  206. return Style::VerticalAlign(value);
  207. }
  208. else if (property->unit & Property::PERCENT)
  209. {
  210. return Style::VerticalAlign(property->Get<float>() * line_height);
  211. }
  212. RMLUI_ASSERT(property->unit & Property::KEYWORD);
  213. return Style::VerticalAlign((Style::VerticalAlign::Type)property->Get<int>());
  214. }
  215. Style::LengthPercentage ComputeLengthPercentage(const Property* property, float font_size, float document_font_size, float dp_ratio, Vector2f vp_dimensions)
  216. {
  217. using namespace Style;
  218. if (property->unit & Property::PERCENT)
  219. return LengthPercentage(LengthPercentage::Percentage, property->Get<float>());
  220. return LengthPercentage(LengthPercentage::Length, ComputeLength(property, font_size, document_font_size, dp_ratio, vp_dimensions));
  221. }
  222. Style::LengthPercentageAuto ComputeLengthPercentageAuto(const Property* property, float font_size, float document_font_size, float dp_ratio, Vector2f vp_dimensions)
  223. {
  224. using namespace Style;
  225. // Assuming here that 'auto' is the only possible keyword
  226. if (property->unit & Property::PERCENT)
  227. return LengthPercentageAuto(LengthPercentageAuto::Percentage, property->Get<float>());
  228. else if (property->unit & Property::KEYWORD)
  229. return LengthPercentageAuto(LengthPercentageAuto::Auto);
  230. return LengthPercentageAuto(LengthPercentageAuto::Length, ComputeLength(property, font_size, document_font_size, dp_ratio, vp_dimensions));
  231. }
  232. Style::LengthPercentage ComputeOrigin(const Property* property, float font_size, float document_font_size, float dp_ratio, Vector2f vp_dimensions)
  233. {
  234. using namespace Style;
  235. static_assert((int)OriginX::Left == (int)OriginY::Top && (int)OriginX::Center == (int)OriginY::Center && (int)OriginX::Right == (int)OriginY::Bottom, "");
  236. if (property->unit & Property::KEYWORD)
  237. {
  238. float percent = 0.0f;
  239. OriginX origin = (OriginX)property->Get<int>();
  240. switch (origin)
  241. {
  242. case OriginX::Left: percent = 0.0f; break;
  243. case OriginX::Center: percent = 50.0f; break;
  244. case OriginX::Right: percent = 100.f; break;
  245. }
  246. return LengthPercentage(LengthPercentage::Percentage, percent);
  247. }
  248. else if (property->unit & Property::PERCENT)
  249. return LengthPercentage(LengthPercentage::Percentage, property->Get<float>());
  250. return LengthPercentage(LengthPercentage::Length, ComputeLength(property, font_size, document_font_size, dp_ratio, vp_dimensions));
  251. }
  252. uint16_t ComputeBorderWidth(float computed_length)
  253. {
  254. if (computed_length <= 0.f)
  255. return 0;
  256. if (computed_length <= 1.f)
  257. return 1;
  258. return uint16_t(computed_length + 0.5f);
  259. }
  260. } // namespace Rml