StyleProperties.cpp 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  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(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. Length StyleProperties::length_or_fallback(CSS::PropertyID id, const Length& fallback, float reference_for_percentages) const
  66. {
  67. auto value = property(id);
  68. if (!value.has_value())
  69. return fallback;
  70. if (value.value()->is_percentage())
  71. return static_cast<const PercentageStyleValue&>(*value.value()).to_length(reference_for_percentages);
  72. return value.value()->to_length();
  73. }
  74. String StyleProperties::string_or_fallback(CSS::PropertyID id, const StringView& fallback) const
  75. {
  76. auto value = property(id);
  77. if (!value.has_value())
  78. return fallback;
  79. return value.value()->to_string();
  80. }
  81. Color StyleProperties::color_or_fallback(CSS::PropertyID id, const Document& document, Color fallback) const
  82. {
  83. auto value = property(id);
  84. if (!value.has_value())
  85. return fallback;
  86. return value.value()->to_color(document);
  87. }
  88. void StyleProperties::load_font() const
  89. {
  90. auto font_family = string_or_fallback(CSS::PropertyID::FontFamily, "Katica");
  91. auto font_weight = string_or_fallback(CSS::PropertyID::FontWeight, "normal");
  92. if (auto cached_font = FontCache::the().get({ font_family, font_weight })) {
  93. m_font = cached_font;
  94. return;
  95. }
  96. String weight;
  97. if (font_weight == "lighter")
  98. weight = "Thin";
  99. else if (font_weight == "normal")
  100. weight = "";
  101. else if (font_weight == "bold")
  102. weight = "Bold";
  103. else {
  104. dbg() << "Unknown font-weight: " << font_weight;
  105. weight = "";
  106. }
  107. auto look_for_file = [](const StringView& expected_name) -> String {
  108. // TODO: handle font sizes properly?
  109. Core::DirIterator it { "/res/fonts/", Core::DirIterator::Flags::SkipDots };
  110. while (it.has_next()) {
  111. String name = it.next_path();
  112. if (!name.ends_with(".font"))
  113. continue;
  114. if (!name.starts_with(expected_name))
  115. continue;
  116. // Check that a numeric size immediately
  117. // follows the font name. This prevents,
  118. // for example, matching KaticaBold when
  119. // the regular Katica is requested.
  120. if (!isdigit(name[expected_name.length()]))
  121. continue;
  122. return name;
  123. }
  124. return {};
  125. };
  126. // FIXME: Do this properly, with quote handling etc.
  127. for (auto& font_name : font_family.split(',')) {
  128. font_name = font_name.trim_whitespace();
  129. if (font_name == "monospace")
  130. font_name = "Csilla";
  131. auto file_name = look_for_file(String::format("%s%s", font_name.characters(), weight.characters()));
  132. if (file_name.is_null() && weight == "")
  133. file_name = look_for_file(String::format("%sRegular", font_name.characters()));
  134. if (file_name.is_null())
  135. continue;
  136. #ifdef HTML_DEBUG
  137. dbg() << "Found font " << file_name << " for family " << font_family << " weight " << font_weight;
  138. #endif
  139. m_font = Gfx::Font::load_from_file(String::format("/res/fonts/%s", file_name.characters()));
  140. FontCache::the().set({ font_name, font_weight }, *m_font);
  141. return;
  142. }
  143. #ifdef HTML_DEBUG
  144. dbg() << "Failed to find a font for family " << font_family << " weight " << font_weight;
  145. #endif
  146. if (font_weight == "bold")
  147. m_font = Gfx::Font::default_bold_font();
  148. else
  149. m_font = Gfx::Font::default_font();
  150. return;
  151. }
  152. float StyleProperties::line_height(const LayoutNode& layout_node) const
  153. {
  154. auto line_height_length = length_or_fallback(CSS::PropertyID::LineHeight, {});
  155. if (line_height_length.is_absolute())
  156. return (float)line_height_length.to_px(layout_node);
  157. return (float)font().glyph_height() * 1.4f;
  158. }
  159. CSS::Position StyleProperties::position() const
  160. {
  161. if (property(CSS::PropertyID::Position).has_value()) {
  162. String position_string = string_or_fallback(CSS::PropertyID::Position, "static");
  163. if (position_string == "relative")
  164. return CSS::Position::Relative;
  165. if (position_string == "absolute")
  166. return CSS::Position::Absolute;
  167. if (position_string == "sticky")
  168. return CSS::Position::Sticky;
  169. if (position_string == "fixed")
  170. return CSS::Position::Fixed;
  171. }
  172. return CSS::Position::Static;
  173. }
  174. bool StyleProperties::operator==(const StyleProperties& other) const
  175. {
  176. if (m_property_values.size() != other.m_property_values.size())
  177. return false;
  178. for (auto& it : m_property_values) {
  179. auto jt = other.m_property_values.find(it.key);
  180. if (jt == other.m_property_values.end())
  181. return false;
  182. auto& my_value = *it.value;
  183. auto& other_value = *jt->value;
  184. if (my_value.type() != other_value.type())
  185. return false;
  186. if (my_value.to_string() != other_value.to_string())
  187. return false;
  188. }
  189. return true;
  190. }
  191. }