StyleValue.cpp 7.7 KB

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