StyleValue.cpp 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. /*
  2. * Copyright (c) 2018-2023, Andreas Kling <kling@serenityos.org>
  3. * Copyright (c) 2021-2023, Sam Atkins <atkinssj@serenityos.org>
  4. * Copyright (c) 2021, Tobias Christiansen <tobyase@serenityos.org>
  5. * Copyright (c) 2022-2023, MacDue <macdue@dueutil.tech>
  6. *
  7. * SPDX-License-Identifier: BSD-2-Clause
  8. */
  9. #include <LibGfx/Font/FontStyleMapping.h>
  10. #include <LibGfx/Font/FontWeight.h>
  11. #include <LibWeb/CSS/StyleValue.h>
  12. #include <LibWeb/CSS/StyleValues/AbstractImageStyleValue.h>
  13. #include <LibWeb/CSS/StyleValues/AngleStyleValue.h>
  14. #include <LibWeb/CSS/StyleValues/BackgroundRepeatStyleValue.h>
  15. #include <LibWeb/CSS/StyleValues/BackgroundSizeStyleValue.h>
  16. #include <LibWeb/CSS/StyleValues/BasicShapeStyleValue.h>
  17. #include <LibWeb/CSS/StyleValues/BorderRadiusStyleValue.h>
  18. #include <LibWeb/CSS/StyleValues/CalculatedStyleValue.h>
  19. #include <LibWeb/CSS/StyleValues/ColorStyleValue.h>
  20. #include <LibWeb/CSS/StyleValues/ConicGradientStyleValue.h>
  21. #include <LibWeb/CSS/StyleValues/ContentStyleValue.h>
  22. #include <LibWeb/CSS/StyleValues/CustomIdentStyleValue.h>
  23. #include <LibWeb/CSS/StyleValues/DisplayStyleValue.h>
  24. #include <LibWeb/CSS/StyleValues/EasingStyleValue.h>
  25. #include <LibWeb/CSS/StyleValues/EdgeStyleValue.h>
  26. #include <LibWeb/CSS/StyleValues/FilterValueListStyleValue.h>
  27. #include <LibWeb/CSS/StyleValues/FlexStyleValue.h>
  28. #include <LibWeb/CSS/StyleValues/FrequencyStyleValue.h>
  29. #include <LibWeb/CSS/StyleValues/GridAutoFlowStyleValue.h>
  30. #include <LibWeb/CSS/StyleValues/GridTemplateAreaStyleValue.h>
  31. #include <LibWeb/CSS/StyleValues/GridTrackPlacementStyleValue.h>
  32. #include <LibWeb/CSS/StyleValues/GridTrackSizeListStyleValue.h>
  33. #include <LibWeb/CSS/StyleValues/IdentifierStyleValue.h>
  34. #include <LibWeb/CSS/StyleValues/ImageStyleValue.h>
  35. #include <LibWeb/CSS/StyleValues/InheritStyleValue.h>
  36. #include <LibWeb/CSS/StyleValues/InitialStyleValue.h>
  37. #include <LibWeb/CSS/StyleValues/IntegerStyleValue.h>
  38. #include <LibWeb/CSS/StyleValues/LengthStyleValue.h>
  39. #include <LibWeb/CSS/StyleValues/LinearGradientStyleValue.h>
  40. #include <LibWeb/CSS/StyleValues/MathDepthStyleValue.h>
  41. #include <LibWeb/CSS/StyleValues/NumberStyleValue.h>
  42. #include <LibWeb/CSS/StyleValues/PercentageStyleValue.h>
  43. #include <LibWeb/CSS/StyleValues/PositionStyleValue.h>
  44. #include <LibWeb/CSS/StyleValues/RadialGradientStyleValue.h>
  45. #include <LibWeb/CSS/StyleValues/RatioStyleValue.h>
  46. #include <LibWeb/CSS/StyleValues/RectStyleValue.h>
  47. #include <LibWeb/CSS/StyleValues/ResolutionStyleValue.h>
  48. #include <LibWeb/CSS/StyleValues/RevertStyleValue.h>
  49. #include <LibWeb/CSS/StyleValues/ShadowStyleValue.h>
  50. #include <LibWeb/CSS/StyleValues/ShorthandStyleValue.h>
  51. #include <LibWeb/CSS/StyleValues/StringStyleValue.h>
  52. #include <LibWeb/CSS/StyleValues/StyleValueList.h>
  53. #include <LibWeb/CSS/StyleValues/TimeStyleValue.h>
  54. #include <LibWeb/CSS/StyleValues/TransformationStyleValue.h>
  55. #include <LibWeb/CSS/StyleValues/TransitionStyleValue.h>
  56. #include <LibWeb/CSS/StyleValues/URLStyleValue.h>
  57. #include <LibWeb/CSS/StyleValues/UnresolvedStyleValue.h>
  58. #include <LibWeb/CSS/StyleValues/UnsetStyleValue.h>
  59. namespace Web::CSS {
  60. StyleValue::StyleValue(Type type)
  61. : m_type(type)
  62. {
  63. }
  64. AbstractImageStyleValue const& StyleValue::as_abstract_image() const
  65. {
  66. VERIFY(is_abstract_image());
  67. return static_cast<AbstractImageStyleValue const&>(*this);
  68. }
  69. #define __ENUMERATE_STYLE_VALUE_TYPE(TitleCaseName, SnakeCaseName) \
  70. TitleCaseName##StyleValue const& StyleValue::as_##SnakeCaseName() const \
  71. { \
  72. VERIFY(is_##SnakeCaseName()); \
  73. return static_cast<TitleCaseName##StyleValue const&>(*this); \
  74. }
  75. ENUMERATE_STYLE_VALUE_TYPES
  76. #undef __ENUMERATE_STYLE_VALUE_TYPE
  77. ValueComparingNonnullRefPtr<StyleValue const> StyleValue::absolutized(CSSPixelRect const&, Length::FontMetrics const&, Length::FontMetrics const&) const
  78. {
  79. return *this;
  80. }
  81. bool StyleValue::has_auto() const
  82. {
  83. return is_identifier() && as_identifier().id() == ValueID::Auto;
  84. }
  85. ValueID StyleValue::to_identifier() const
  86. {
  87. if (is_identifier())
  88. return as_identifier().id();
  89. return ValueID::Invalid;
  90. }
  91. int StyleValue::to_font_weight() const
  92. {
  93. if (is_identifier()) {
  94. switch (static_cast<IdentifierStyleValue const&>(*this).id()) {
  95. case CSS::ValueID::Normal:
  96. return Gfx::FontWeight::Regular;
  97. case CSS::ValueID::Bold:
  98. return Gfx::FontWeight::Bold;
  99. case CSS::ValueID::Lighter:
  100. // FIXME: This should be relative to the parent.
  101. return Gfx::FontWeight::Regular;
  102. case CSS::ValueID::Bolder:
  103. // FIXME: This should be relative to the parent.
  104. return Gfx::FontWeight::Bold;
  105. default:
  106. return Gfx::FontWeight::Regular;
  107. }
  108. }
  109. if (is_number()) {
  110. return round_to<int>(as_number().number());
  111. }
  112. if (is_calculated()) {
  113. auto maybe_weight = const_cast<CalculatedStyleValue&>(as_calculated()).resolve_integer();
  114. if (maybe_weight.has_value())
  115. return maybe_weight.value();
  116. }
  117. return Gfx::FontWeight::Regular;
  118. }
  119. int StyleValue::to_font_slope() const
  120. {
  121. // FIXME: Implement oblique <angle>
  122. if (is_identifier()) {
  123. switch (static_cast<IdentifierStyleValue const&>(*this).id()) {
  124. case CSS::ValueID::Italic: {
  125. static int italic_slope = Gfx::name_to_slope("Italic"sv);
  126. return italic_slope;
  127. }
  128. case CSS::ValueID::Oblique:
  129. static int oblique_slope = Gfx::name_to_slope("Oblique"sv);
  130. return oblique_slope;
  131. case CSS::ValueID::Normal:
  132. default:
  133. break;
  134. }
  135. }
  136. static int normal_slope = Gfx::name_to_slope("Normal"sv);
  137. return normal_slope;
  138. }
  139. int StyleValue::to_font_stretch_width() const
  140. {
  141. int width = Gfx::FontWidth::Normal;
  142. if (is_identifier()) {
  143. switch (static_cast<IdentifierStyleValue const&>(*this).id()) {
  144. case CSS::ValueID::UltraCondensed:
  145. width = Gfx::FontWidth::UltraCondensed;
  146. break;
  147. case CSS::ValueID::ExtraCondensed:
  148. width = Gfx::FontWidth::ExtraCondensed;
  149. break;
  150. case CSS::ValueID::Condensed:
  151. width = Gfx::FontWidth::Condensed;
  152. break;
  153. case CSS::ValueID::SemiCondensed:
  154. width = Gfx::FontWidth::SemiCondensed;
  155. break;
  156. case CSS::ValueID::Normal:
  157. width = Gfx::FontWidth::Normal;
  158. break;
  159. case CSS::ValueID::SemiExpanded:
  160. width = Gfx::FontWidth::SemiExpanded;
  161. break;
  162. case CSS::ValueID::Expanded:
  163. width = Gfx::FontWidth::Expanded;
  164. break;
  165. case CSS::ValueID::ExtraExpanded:
  166. width = Gfx::FontWidth::ExtraExpanded;
  167. break;
  168. case CSS::ValueID::UltraExpanded:
  169. width = Gfx::FontWidth::UltraExpanded;
  170. break;
  171. default:
  172. break;
  173. }
  174. } else if (is_percentage()) {
  175. float percentage = as_percentage().percentage().value();
  176. if (percentage <= 50) {
  177. width = Gfx::FontWidth::UltraCondensed;
  178. } else if (percentage <= 62.5f) {
  179. width = Gfx::FontWidth::ExtraCondensed;
  180. } else if (percentage <= 75.0f) {
  181. width = Gfx::FontWidth::Condensed;
  182. } else if (percentage <= 87.5f) {
  183. width = Gfx::FontWidth::SemiCondensed;
  184. } else if (percentage <= 100.0f) {
  185. width = Gfx::FontWidth::Normal;
  186. } else if (percentage <= 112.5f) {
  187. width = Gfx::FontWidth::SemiExpanded;
  188. } else if (percentage <= 125.0f) {
  189. width = Gfx::FontWidth::Expanded;
  190. } else if (percentage <= 150.0f) {
  191. width = Gfx::FontWidth::ExtraExpanded;
  192. } else {
  193. width = Gfx::FontWidth::UltraExpanded;
  194. }
  195. }
  196. return width;
  197. }
  198. }