StyleProperties.cpp 4.6 KB

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