ComputeProperty.cpp 11 KB

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