StyleProperties.cpp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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. dbg() << "Unknown font-weight: " << font_weight;
  49. weight = "";
  50. }
  51. auto look_for_file = [](const StringView& expected_name) -> String {
  52. // TODO: handle font sizes properly?
  53. CDirIterator it { "/res/fonts/", CDirIterator::Flags::SkipDots };
  54. while (it.has_next()) {
  55. String name = it.next_path();
  56. ASSERT(name.ends_with(".font"));
  57. if (!name.starts_with(expected_name))
  58. continue;
  59. // Check that a numeric size immediately
  60. // follows the font name. This prevents,
  61. // for example, matching KaticaBold when
  62. // the regular Katica is requested.
  63. if (!isdigit(name[expected_name.length()]))
  64. continue;
  65. return name;
  66. }
  67. return {};
  68. };
  69. String file_name = look_for_file(String::format("%s%s", font_family.characters(), weight.characters()));
  70. if (file_name.is_null() && weight == "")
  71. file_name = look_for_file(String::format("%sRegular", font_family.characters()));
  72. if (file_name.is_null()) {
  73. dbg() << "Failed to find a font for family " << font_family << " weight " << font_weight;
  74. m_font = Font::default_font();
  75. return;
  76. }
  77. #ifdef HTML_DEBUG
  78. dbg() << "Found font " << file_name << " for family " << font_family << " weight " << font_weight;
  79. #endif
  80. m_font = Font::load_from_file(String::format("/res/fonts/%s", file_name.characters()));
  81. }
  82. int StyleProperties::line_height() const
  83. {
  84. // FIXME: Allow overriding the line-height. We currently default to 140% which seems to look nice.
  85. return (int)(font().glyph_height() * 1.4f);
  86. }
  87. bool StyleProperties::operator==(const StyleProperties& other) const
  88. {
  89. if (m_property_values.size() != other.m_property_values.size())
  90. return false;
  91. for (auto& it : m_property_values) {
  92. auto jt = other.m_property_values.find(it.key);
  93. if (jt == other.m_property_values.end())
  94. return false;
  95. auto& my_value = *it.value;
  96. auto& other_value = *jt->value;
  97. if (my_value.type() != other_value.type())
  98. return false;
  99. if (my_value.to_string() != other_value.to_string())
  100. return false;
  101. }
  102. return true;
  103. }