ComputeProperty.cpp 9.4 KB

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