StyleProperties.cpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. #include <LibCore/CDirIterator.h>
  2. #include <LibHTML/CSS/StyleProperties.h>
  3. #include <ctype.h>
  4. void StyleProperties::set_property(CSS::PropertyID id, NonnullRefPtr<StyleValue> value)
  5. {
  6. m_property_values.set((unsigned)id, move(value));
  7. }
  8. Optional<NonnullRefPtr<StyleValue>> StyleProperties::property(CSS::PropertyID id) const
  9. {
  10. auto it = m_property_values.find((unsigned)id);
  11. if (it == m_property_values.end())
  12. return {};
  13. return it->value;
  14. }
  15. Length StyleProperties::length_or_fallback(CSS::PropertyID id, const Length& fallback) const
  16. {
  17. auto value = property(id);
  18. if (!value.has_value())
  19. return fallback;
  20. return value.value()->to_length();
  21. }
  22. String StyleProperties::string_or_fallback(CSS::PropertyID id, const StringView& fallback) const
  23. {
  24. auto value = property(id);
  25. if (!value.has_value())
  26. return fallback;
  27. return value.value()->to_string();
  28. }
  29. Color StyleProperties::color_or_fallback(CSS::PropertyID id, const Document& document, Color fallback) const
  30. {
  31. auto value = property(id);
  32. if (!value.has_value())
  33. return fallback;
  34. return value.value()->to_color(document);
  35. }
  36. void StyleProperties::load_font() const
  37. {
  38. auto font_family = string_or_fallback(CSS::PropertyID::FontFamily, "Katica");
  39. auto font_weight = string_or_fallback(CSS::PropertyID::FontWeight, "normal");
  40. String weight;
  41. if (font_weight == "lighter")
  42. weight = "Thin";
  43. else if (font_weight == "normal")
  44. weight = "";
  45. else if (font_weight == "bold")
  46. weight = "Bold";
  47. else
  48. ASSERT_NOT_REACHED();
  49. auto look_for_file = [](const StringView& expected_name) -> String {
  50. // TODO: handle font sizes properly?
  51. CDirIterator it { "/res/fonts/", CDirIterator::Flags::SkipDots };
  52. while (it.has_next()) {
  53. String name = it.next_path();
  54. ASSERT(name.ends_with(".font"));
  55. if (!name.starts_with(expected_name))
  56. continue;
  57. // Check that a numeric size immediately
  58. // follows the font name. This prevents,
  59. // for example, matching KaticaBold when
  60. // the regular Katica is requested.
  61. if (!isdigit(name[expected_name.length()]))
  62. continue;
  63. return name;
  64. }
  65. return {};
  66. };
  67. String file_name = look_for_file(String::format("%s%s", font_family.characters(), weight.characters()));
  68. if (file_name.is_null() && weight == "")
  69. file_name = look_for_file(String::format("%sRegular", font_family.characters()));
  70. if (file_name.is_null()) {
  71. dbg() << "Failed to find a font for family " << font_family << " weight " << font_weight;
  72. m_font = Font::default_font();
  73. return;
  74. }
  75. #ifdef HTML_DEBUG
  76. dbg() << "Found font " << file_name << " for family " << font_family << " weight " << font_weight;
  77. #endif
  78. m_font = Font::load_from_file(String::format("/res/fonts/%s", file_name.characters()));
  79. }
  80. int StyleProperties::line_height() const
  81. {
  82. // FIXME: Allow overriding the line-height. We currently default to 140% which seems to look nice.
  83. return (int)(font().glyph_height() * 1.4f);
  84. }
  85. bool StyleProperties::operator==(const StyleProperties& other) const
  86. {
  87. if (m_property_values.size() != other.m_property_values.size())
  88. return false;
  89. for (auto& it : m_property_values) {
  90. auto jt = other.m_property_values.find(it.key);
  91. if (jt == other.m_property_values.end())
  92. return false;
  93. auto& my_value = *it.value;
  94. auto& other_value = *jt->value;
  95. if (my_value.type() != other_value.type())
  96. return false;
  97. if (my_value.to_string() != other_value.to_string())
  98. return false;
  99. }
  100. return true;
  101. }