ComputeProperty.cpp 8.4 KB

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