ComputeProperty.cpp 8.7 KB

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