StyleProperties.cpp 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice, this
  9. * list of conditions and the following disclaimer.
  10. *
  11. * 2. Redistributions in binary form must reproduce the above copyright notice,
  12. * this list of conditions and the following disclaimer in the documentation
  13. * and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  16. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  18. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  19. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  20. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  21. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  22. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  23. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  24. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. #include <LibCore/CDirIterator.h>
  27. #include <LibHTML/CSS/StyleProperties.h>
  28. #include <LibHTML/FontCache.h>
  29. #include <ctype.h>
  30. StyleProperties::StyleProperties()
  31. {
  32. }
  33. StyleProperties::StyleProperties(const StyleProperties& other)
  34. : m_property_values(*new HashMap(other.m_property_values))
  35. {
  36. if (other.m_font) {
  37. m_font = other.m_font->clone();
  38. } else {
  39. m_font = nullptr;
  40. }
  41. }
  42. NonnullRefPtr<StyleProperties> StyleProperties::clone() const
  43. {
  44. return adopt(*new StyleProperties(*this));
  45. }
  46. void StyleProperties::set_property(CSS::PropertyID id, NonnullRefPtr<StyleValue> value)
  47. {
  48. m_property_values.set((unsigned)id, move(value));
  49. }
  50. Optional<NonnullRefPtr<StyleValue>> StyleProperties::property(CSS::PropertyID id) const
  51. {
  52. auto it = m_property_values.find((unsigned)id);
  53. if (it == m_property_values.end())
  54. return {};
  55. return it->value;
  56. }
  57. Length StyleProperties::length_or_fallback(CSS::PropertyID id, const Length& fallback) const
  58. {
  59. auto value = property(id);
  60. if (!value.has_value())
  61. return fallback;
  62. return value.value()->to_length();
  63. }
  64. String StyleProperties::string_or_fallback(CSS::PropertyID id, const StringView& fallback) const
  65. {
  66. auto value = property(id);
  67. if (!value.has_value())
  68. return fallback;
  69. return value.value()->to_string();
  70. }
  71. Color StyleProperties::color_or_fallback(CSS::PropertyID id, const Document& document, Color fallback) const
  72. {
  73. auto value = property(id);
  74. if (!value.has_value())
  75. return fallback;
  76. return value.value()->to_color(document);
  77. }
  78. void StyleProperties::load_font() const
  79. {
  80. auto font_family = string_or_fallback(CSS::PropertyID::FontFamily, "Katica");
  81. auto font_weight = string_or_fallback(CSS::PropertyID::FontWeight, "normal");
  82. if (auto cached_font = FontCache::the().get({ font_family, font_weight })) {
  83. m_font = cached_font;
  84. return;
  85. }
  86. String weight;
  87. if (font_weight == "lighter")
  88. weight = "Thin";
  89. else if (font_weight == "normal")
  90. weight = "";
  91. else if (font_weight == "bold")
  92. weight = "Bold";
  93. else {
  94. dbg() << "Unknown font-weight: " << font_weight;
  95. weight = "";
  96. }
  97. auto look_for_file = [](const StringView& expected_name) -> String {
  98. // TODO: handle font sizes properly?
  99. Core::DirIterator it { "/res/fonts/", Core::DirIterator::Flags::SkipDots };
  100. while (it.has_next()) {
  101. String name = it.next_path();
  102. ASSERT(name.ends_with(".font"));
  103. if (!name.starts_with(expected_name))
  104. continue;
  105. // Check that a numeric size immediately
  106. // follows the font name. This prevents,
  107. // for example, matching KaticaBold when
  108. // the regular Katica is requested.
  109. if (!isdigit(name[expected_name.length()]))
  110. continue;
  111. return name;
  112. }
  113. return {};
  114. };
  115. String file_name = look_for_file(String::format("%s%s", font_family.characters(), weight.characters()));
  116. if (file_name.is_null() && weight == "")
  117. file_name = look_for_file(String::format("%sRegular", font_family.characters()));
  118. if (file_name.is_null()) {
  119. dbg() << "Failed to find a font for family " << font_family << " weight " << font_weight;
  120. if (font_weight == "bold")
  121. m_font = Gfx::Font::default_bold_font();
  122. else
  123. m_font = Gfx::Font::default_font();
  124. return;
  125. }
  126. #ifdef HTML_DEBUG
  127. dbg() << "Found font " << file_name << " for family " << font_family << " weight " << font_weight;
  128. #endif
  129. m_font = Gfx::Font::load_from_file(String::format("/res/fonts/%s", file_name.characters()));
  130. FontCache::the().set({ font_family, font_weight }, *m_font);
  131. }
  132. float StyleProperties::line_height() const
  133. {
  134. auto line_height_length = length_or_fallback(CSS::PropertyID::LineHeight, {});
  135. if (line_height_length.is_absolute())
  136. return (float)font().glyph_height() * line_height_length.to_px();
  137. return (float)font().glyph_height() * 1.4f;
  138. }
  139. bool StyleProperties::operator==(const StyleProperties& other) const
  140. {
  141. if (m_property_values.size() != other.m_property_values.size())
  142. return false;
  143. for (auto& it : m_property_values) {
  144. auto jt = other.m_property_values.find(it.key);
  145. if (jt == other.m_property_values.end())
  146. return false;
  147. auto& my_value = *it.value;
  148. auto& other_value = *jt->value;
  149. if (my_value.type() != other_value.type())
  150. return false;
  151. if (my_value.to_string() != other_value.to_string())
  152. return false;
  153. }
  154. return true;
  155. }