StyleProperties.cpp 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  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/DirIterator.h>
  27. #include <LibWeb/CSS/StyleProperties.h>
  28. #include <LibWeb/FontCache.h>
  29. #include <ctype.h>
  30. namespace Web {
  31. StyleProperties::StyleProperties()
  32. {
  33. }
  34. StyleProperties::StyleProperties(const StyleProperties& other)
  35. : m_property_values(*new HashMap(other.m_property_values))
  36. {
  37. if (other.m_font) {
  38. m_font = other.m_font->clone();
  39. } else {
  40. m_font = nullptr;
  41. }
  42. }
  43. NonnullRefPtr<StyleProperties> StyleProperties::clone() const
  44. {
  45. return adopt(*new StyleProperties(*this));
  46. }
  47. void StyleProperties::set_property(CSS::PropertyID id, NonnullRefPtr<StyleValue> value)
  48. {
  49. m_property_values.set((unsigned)id, move(value));
  50. }
  51. Optional<NonnullRefPtr<StyleValue>> StyleProperties::property(CSS::PropertyID id) const
  52. {
  53. auto it = m_property_values.find((unsigned)id);
  54. if (it == m_property_values.end())
  55. return {};
  56. return it->value;
  57. }
  58. Length StyleProperties::length_or_fallback(CSS::PropertyID id, const Length& fallback) const
  59. {
  60. auto value = property(id);
  61. if (!value.has_value())
  62. return fallback;
  63. return value.value()->to_length();
  64. }
  65. String StyleProperties::string_or_fallback(CSS::PropertyID id, const StringView& fallback) const
  66. {
  67. auto value = property(id);
  68. if (!value.has_value())
  69. return fallback;
  70. return value.value()->to_string();
  71. }
  72. Color StyleProperties::color_or_fallback(CSS::PropertyID id, const Document& document, Color fallback) const
  73. {
  74. auto value = property(id);
  75. if (!value.has_value())
  76. return fallback;
  77. return value.value()->to_color(document);
  78. }
  79. void StyleProperties::load_font() const
  80. {
  81. auto font_family = string_or_fallback(CSS::PropertyID::FontFamily, "Katica");
  82. auto font_weight = string_or_fallback(CSS::PropertyID::FontWeight, "normal");
  83. if (auto cached_font = FontCache::the().get({ font_family, font_weight })) {
  84. m_font = cached_font;
  85. return;
  86. }
  87. String weight;
  88. if (font_weight == "lighter")
  89. weight = "Thin";
  90. else if (font_weight == "normal")
  91. weight = "";
  92. else if (font_weight == "bold")
  93. weight = "Bold";
  94. else {
  95. dbg() << "Unknown font-weight: " << font_weight;
  96. weight = "";
  97. }
  98. auto look_for_file = [](const StringView& expected_name) -> String {
  99. // TODO: handle font sizes properly?
  100. Core::DirIterator it { "/res/fonts/", Core::DirIterator::Flags::SkipDots };
  101. while (it.has_next()) {
  102. String name = it.next_path();
  103. if (!name.ends_with(".font"))
  104. continue;
  105. if (!name.starts_with(expected_name))
  106. continue;
  107. // Check that a numeric size immediately
  108. // follows the font name. This prevents,
  109. // for example, matching KaticaBold when
  110. // the regular Katica is requested.
  111. if (!isdigit(name[expected_name.length()]))
  112. continue;
  113. return name;
  114. }
  115. return {};
  116. };
  117. String file_name = look_for_file(String::format("%s%s", font_family.characters(), weight.characters()));
  118. if (file_name.is_null() && weight == "")
  119. file_name = look_for_file(String::format("%sRegular", font_family.characters()));
  120. if (file_name.is_null()) {
  121. dbg() << "Failed to find a font for family " << font_family << " weight " << font_weight;
  122. if (font_weight == "bold")
  123. m_font = Gfx::Font::default_bold_font();
  124. else
  125. m_font = Gfx::Font::default_font();
  126. return;
  127. }
  128. #ifdef HTML_DEBUG
  129. dbg() << "Found font " << file_name << " for family " << font_family << " weight " << font_weight;
  130. #endif
  131. m_font = Gfx::Font::load_from_file(String::format("/res/fonts/%s", file_name.characters()));
  132. FontCache::the().set({ font_family, font_weight }, *m_font);
  133. }
  134. float StyleProperties::line_height() const
  135. {
  136. auto line_height_length = length_or_fallback(CSS::PropertyID::LineHeight, {});
  137. if (line_height_length.is_absolute())
  138. return (float)font().glyph_height() * line_height_length.to_px();
  139. return (float)font().glyph_height() * 1.4f;
  140. }
  141. CSS::Position StyleProperties::position() const
  142. {
  143. if (property(CSS::PropertyID::Position).has_value()) {
  144. String position_string = string_or_fallback(CSS::PropertyID::Position, "static");
  145. if (position_string == "relative")
  146. return CSS::Position::Relative;
  147. if (position_string == "absolute")
  148. return CSS::Position::Absolute;
  149. if (position_string == "sticky")
  150. return CSS::Position::Sticky;
  151. if (position_string == "fixed")
  152. return CSS::Position::Fixed;
  153. }
  154. return CSS::Position::Static;
  155. }
  156. bool StyleProperties::operator==(const StyleProperties& other) const
  157. {
  158. if (m_property_values.size() != other.m_property_values.size())
  159. return false;
  160. for (auto& it : m_property_values) {
  161. auto jt = other.m_property_values.find(it.key);
  162. if (jt == other.m_property_values.end())
  163. return false;
  164. auto& my_value = *it.value;
  165. auto& other_value = *jt->value;
  166. if (my_value.type() != other_value.type())
  167. return false;
  168. if (my_value.to_string() != other_value.to_string())
  169. return false;
  170. }
  171. return true;
  172. }
  173. }