ComputeProperty.cpp 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  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-2023 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. #include "ControlledLifetimeResource.h"
  33. namespace Rml {
  34. struct ComputedPropertyData {
  35. const Style::ComputedValues computed{nullptr};
  36. };
  37. static ControlledLifetimeResource<ComputedPropertyData> computed_property_data;
  38. const Style::ComputedValues& DefaultComputedValues()
  39. {
  40. return computed_property_data->computed;
  41. }
  42. void InitializeComputeProperty()
  43. {
  44. computed_property_data.Initialize();
  45. }
  46. void ShutdownComputeProperty()
  47. {
  48. computed_property_data.Shutdown();
  49. }
  50. static constexpr float PixelsPerInch = 96.0f;
  51. static float ComputePPILength(NumericValue value, float dp_ratio)
  52. {
  53. RMLUI_ASSERT(Any(value.unit & Unit::PPI_UNIT));
  54. // Values based on pixels-per-inch. Scaled by the dp-ratio as a placeholder solution until we make the pixel unit itself scalable.
  55. const float inch = value.number * PixelsPerInch * dp_ratio;
  56. switch (value.unit)
  57. {
  58. case Unit::INCH: return inch;
  59. case Unit::CM: return inch * (1.0f / 2.54f);
  60. case Unit::MM: return inch * (1.0f / 25.4f);
  61. case Unit::PT: return inch * (1.0f / 72.0f);
  62. case Unit::PC: return inch * (1.0f / 6.0f);
  63. default: break;
  64. }
  65. RMLUI_ERROR;
  66. return 0.f;
  67. }
  68. float ComputeLength(NumericValue value, float font_size, float document_font_size, float dp_ratio, Vector2f vp_dimensions)
  69. {
  70. if (Any(value.unit & Unit::PPI_UNIT))
  71. return ComputePPILength(value, dp_ratio);
  72. switch (value.unit)
  73. {
  74. case Unit::PX: return value.number;
  75. case Unit::EM: return value.number * font_size;
  76. case Unit::REM: return value.number * document_font_size;
  77. case Unit::DP: return value.number * dp_ratio;
  78. case Unit::VW: return value.number * vp_dimensions.x * 0.01f;
  79. case Unit::VH: return value.number * vp_dimensions.y * 0.01f;
  80. default: break;
  81. }
  82. RMLUI_ERROR;
  83. return 0.0f;
  84. }
  85. float ComputeAngle(NumericValue value)
  86. {
  87. switch (value.unit)
  88. {
  89. case Unit::NUMBER:
  90. case Unit::RAD: return value.number;
  91. case Unit::DEG: return Math::DegreesToRadians(value.number);
  92. default: break;
  93. }
  94. RMLUI_ERROR;
  95. return 0.0f;
  96. }
  97. float ComputeFontsize(NumericValue value, const Style::ComputedValues& values, const Style::ComputedValues* parent_values,
  98. const Style::ComputedValues* document_values, float dp_ratio, Vector2f vp_dimensions)
  99. {
  100. if (Any(value.unit & (Unit::PERCENT | Unit::EM | Unit::REM)))
  101. {
  102. // Relative values are based on the parent's or document's font size instead of our own.
  103. float multiplier = 1.0f;
  104. switch (value.unit)
  105. {
  106. case Unit::PERCENT:
  107. multiplier = 0.01f;
  108. //-fallthrough
  109. case Unit::EM:
  110. if (!parent_values)
  111. return 0;
  112. return value.number * multiplier * parent_values->font_size();
  113. case Unit::REM:
  114. // If the current element is a document, the rem unit is relative to the default size.
  115. if (!document_values || &values == document_values)
  116. return value.number * DefaultComputedValues().font_size();
  117. // Otherwise it is relative to the document font size.
  118. return value.number * document_values->font_size();
  119. default: break;
  120. }
  121. }
  122. // Font-relative lengths handled above, other lengths should be handled as normal.
  123. return ComputeLength(value, 0.f, 0.f, dp_ratio, vp_dimensions);
  124. }
  125. String ComputeFontFamily(String font_family)
  126. {
  127. return StringUtilities::ToLower(std::move(font_family));
  128. }
  129. Style::Clip ComputeClip(const Property* property)
  130. {
  131. const int value = property->Get<int>();
  132. if (property->unit == Unit::KEYWORD)
  133. return Style::Clip(static_cast<Style::Clip::Type>(value));
  134. else if (property->unit == Unit::NUMBER)
  135. return Style::Clip(Style::Clip::Type::Number, static_cast<int8_t>(value));
  136. RMLUI_ERRORMSG("Invalid clip type");
  137. return Style::Clip();
  138. }
  139. Style::LineHeight ComputeLineHeight(const Property* property, float font_size, float document_font_size, float dp_ratio, Vector2f vp_dimensions)
  140. {
  141. if (Any(property->unit & Unit::LENGTH))
  142. {
  143. float value = ComputeLength(property->GetNumericValue(), font_size, document_font_size, dp_ratio, vp_dimensions);
  144. return Style::LineHeight(value, Style::LineHeight::Length, value);
  145. }
  146. float scale_factor = 1.0f;
  147. switch (property->unit)
  148. {
  149. case Unit::NUMBER: scale_factor = property->value.Get<float>(); break;
  150. case Unit::PERCENT: scale_factor = property->value.Get<float>() * 0.01f; break;
  151. default: RMLUI_ERRORMSG("Invalid unit for line-height");
  152. }
  153. float value = font_size * scale_factor;
  154. return Style::LineHeight(value, Style::LineHeight::Number, scale_factor);
  155. }
  156. Style::VerticalAlign ComputeVerticalAlign(const Property* property, float line_height, float font_size, float document_font_size, float dp_ratio,
  157. Vector2f vp_dimensions)
  158. {
  159. if (Any(property->unit & Unit::LENGTH))
  160. {
  161. float value = ComputeLength(property->GetNumericValue(), font_size, document_font_size, dp_ratio, vp_dimensions);
  162. return Style::VerticalAlign(value);
  163. }
  164. else if (property->unit == Unit::PERCENT)
  165. {
  166. return Style::VerticalAlign(property->Get<float>() * line_height * 0.01f);
  167. }
  168. RMLUI_ASSERT(property->unit == Unit::KEYWORD);
  169. return Style::VerticalAlign((Style::VerticalAlign::Type)property->Get<int>());
  170. }
  171. Style::LengthPercentage ComputeLengthPercentage(const Property* property, float font_size, float document_font_size, float dp_ratio,
  172. Vector2f vp_dimensions)
  173. {
  174. using namespace Style;
  175. if (property->unit == Unit::PERCENT)
  176. return LengthPercentage(LengthPercentage::Percentage, property->Get<float>());
  177. return LengthPercentage(LengthPercentage::Length,
  178. ComputeLength(property->GetNumericValue(), font_size, document_font_size, dp_ratio, vp_dimensions));
  179. }
  180. Style::LengthPercentageAuto ComputeLengthPercentageAuto(const Property* property, float font_size, float document_font_size, float dp_ratio,
  181. Vector2f vp_dimensions)
  182. {
  183. using namespace Style;
  184. if (property->unit == Unit::PERCENT)
  185. return LengthPercentageAuto(LengthPercentageAuto::Percentage, property->Get<float>());
  186. else if (property->unit == Unit::KEYWORD)
  187. return LengthPercentageAuto(LengthPercentageAuto::Auto);
  188. return LengthPercentageAuto(LengthPercentageAuto::Length,
  189. ComputeLength(property->GetNumericValue(), font_size, document_font_size, dp_ratio, vp_dimensions));
  190. }
  191. Style::LengthPercentage ComputeOrigin(const Property* property, float font_size, float document_font_size, float dp_ratio, Vector2f vp_dimensions)
  192. {
  193. using namespace Style;
  194. static_assert(
  195. (int)OriginX::Left == (int)OriginY::Top && (int)OriginX::Center == (int)OriginY::Center && (int)OriginX::Right == (int)OriginY::Bottom, "");
  196. if (property->unit == Unit::KEYWORD)
  197. {
  198. float percent = 0.0f;
  199. OriginX origin = (OriginX)property->Get<int>();
  200. switch (origin)
  201. {
  202. case OriginX::Left: percent = 0.0f; break;
  203. case OriginX::Center: percent = 50.0f; break;
  204. case OriginX::Right: percent = 100.f; break;
  205. }
  206. return LengthPercentage(LengthPercentage::Percentage, percent);
  207. }
  208. else if (property->unit == Unit::PERCENT)
  209. return LengthPercentage(LengthPercentage::Percentage, property->Get<float>());
  210. return LengthPercentage(LengthPercentage::Length,
  211. ComputeLength(property->GetNumericValue(), font_size, document_font_size, dp_ratio, vp_dimensions));
  212. }
  213. Style::LengthPercentage ComputeMaxSize(const Property* property, float font_size, float document_font_size, float dp_ratio, Vector2f vp_dimensions)
  214. {
  215. using namespace Style;
  216. if (Any(property->unit & Unit::KEYWORD))
  217. return LengthPercentage(LengthPercentage::Length, FLT_MAX);
  218. else if (Any(property->unit & Unit::PERCENT))
  219. return LengthPercentage(LengthPercentage::Percentage, property->Get<float>());
  220. const float length = ComputeLength(property->GetNumericValue(), font_size, document_font_size, dp_ratio, vp_dimensions);
  221. return LengthPercentage(LengthPercentage::Length, length < 0.f ? FLT_MAX : length);
  222. }
  223. uint16_t ComputeBorderWidth(float computed_length)
  224. {
  225. if (computed_length <= 0.f)
  226. return 0;
  227. if (computed_length <= 1.f)
  228. return 1;
  229. return uint16_t(computed_length + 0.5f);
  230. }
  231. String GetFontFaceDescription(const String& font_family, Style::FontStyle style, Style::FontWeight weight)
  232. {
  233. String font_attributes;
  234. if (style == Style::FontStyle::Italic)
  235. font_attributes += "italic, ";
  236. if (weight == Style::FontWeight::Bold)
  237. font_attributes += "bold, ";
  238. else if (weight != Style::FontWeight::Auto && weight != Style::FontWeight::Normal)
  239. font_attributes += "weight=" + ToString((int)weight) + ", ";
  240. if (font_attributes.empty())
  241. font_attributes = "regular";
  242. else
  243. font_attributes.resize(font_attributes.size() - 2);
  244. return CreateString("'%s' [%s]", font_family.c_str(), font_attributes.c_str());
  245. }
  246. } // namespace Rml