ComputeProperty.cpp 8.6 KB

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