StyleProperties.cpp 4.0 KB

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