ComputeProperty.cpp 9.3 KB

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