CSSNumericType.h 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. /*
  2. * Copyright (c) 2023, Sam Atkins <atkinssj@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #pragma once
  7. #include <AK/Array.h>
  8. #include <AK/Optional.h>
  9. #include <LibWeb/CSS/PropertyID.h>
  10. namespace Web::CSS {
  11. // https://drafts.css-houdini.org/css-typed-om-1/#numeric-typing
  12. // FIXME: Add IDL for this.
  13. class CSSNumericType {
  14. public:
  15. // https://drafts.css-houdini.org/css-typed-om-1/#cssnumericvalue-base-type
  16. enum class BaseType {
  17. Length,
  18. Angle,
  19. Time,
  20. Frequency,
  21. Resolution,
  22. Flex,
  23. Percent,
  24. __Count,
  25. };
  26. static Optional<BaseType> base_type_from_value_type(ValueType);
  27. static constexpr StringView base_type_name(BaseType base_type)
  28. {
  29. switch (base_type) {
  30. case BaseType::Length:
  31. return "length"sv;
  32. case BaseType::Angle:
  33. return "angle"sv;
  34. case BaseType::Time:
  35. return "time"sv;
  36. case BaseType::Frequency:
  37. return "frequency"sv;
  38. case BaseType::Resolution:
  39. return "resolution"sv;
  40. case BaseType::Flex:
  41. return "flex"sv;
  42. case BaseType::Percent:
  43. return "percent"sv;
  44. case BaseType::__Count:
  45. break;
  46. }
  47. VERIFY_NOT_REACHED();
  48. }
  49. static Optional<CSSNumericType> create_from_unit(StringView unit);
  50. CSSNumericType() = default;
  51. CSSNumericType(BaseType type, i32 power)
  52. {
  53. set_exponent(type, power);
  54. }
  55. Optional<CSSNumericType> added_to(CSSNumericType const& other) const;
  56. Optional<CSSNumericType> multiplied_by(CSSNumericType const& other) const;
  57. CSSNumericType inverted() const;
  58. bool matches_angle() const { return matches_dimension(BaseType::Angle); }
  59. bool matches_angle_percentage() const { return matches_dimension_percentage(BaseType::Angle); }
  60. bool matches_flex() const { return matches_dimension(BaseType::Flex); }
  61. bool matches_frequency() const { return matches_dimension(BaseType::Frequency); }
  62. bool matches_frequency_percentage() const { return matches_dimension_percentage(BaseType::Frequency); }
  63. bool matches_length() const { return matches_dimension(BaseType::Length); }
  64. bool matches_length_percentage() const { return matches_dimension_percentage(BaseType::Length); }
  65. bool matches_number() const;
  66. bool matches_number_percentage() const;
  67. bool matches_percentage() const;
  68. bool matches_resolution() const { return matches_dimension(BaseType::Resolution); }
  69. bool matches_resolution_percentage() const { return matches_dimension_percentage(BaseType::Resolution); }
  70. bool matches_time() const { return matches_dimension(BaseType::Time); }
  71. bool matches_time_percentage() const { return matches_dimension_percentage(BaseType::Time); }
  72. bool matches_dimension() const;
  73. Optional<i32> const& exponent(BaseType type) const { return m_type_exponents[to_underlying(type)]; }
  74. void set_exponent(BaseType type, i32 exponent) { m_type_exponents[to_underlying(type)] = exponent; }
  75. Optional<BaseType> const& percent_hint() const { return m_percent_hint; }
  76. void set_percent_hint(Optional<BaseType> hint) { m_percent_hint = hint; }
  77. void apply_percent_hint(BaseType hint);
  78. bool operator==(CSSNumericType const& other) const = default;
  79. ErrorOr<String> dump() const;
  80. private:
  81. bool contains_all_the_non_zero_entries_of_other_with_the_same_value(CSSNumericType const& other) const;
  82. bool contains_a_key_other_than_percent_with_a_non_zero_value() const;
  83. enum class SkipIfAlreadyPresent {
  84. No,
  85. Yes,
  86. };
  87. void copy_all_entries_from(CSSNumericType const& other, SkipIfAlreadyPresent);
  88. bool matches_dimension(BaseType) const;
  89. bool matches_dimension_percentage(BaseType) const;
  90. Array<Optional<i32>, to_underlying(BaseType::__Count)> m_type_exponents;
  91. Optional<BaseType> m_percent_hint;
  92. };
  93. }