StyleProperties.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  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::CSS {
  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. void StyleProperties::set_property(CSS::PropertyID id, const StringView& value)
  52. {
  53. m_property_values.set((unsigned)id, StringStyleValue::create(value));
  54. }
  55. Optional<NonnullRefPtr<StyleValue>> StyleProperties::property(CSS::PropertyID id) const
  56. {
  57. auto it = m_property_values.find((unsigned)id);
  58. if (it == m_property_values.end())
  59. return {};
  60. return it->value;
  61. }
  62. Length StyleProperties::length_or_fallback(CSS::PropertyID id, const Length& fallback) const
  63. {
  64. auto value = property(id);
  65. if (!value.has_value())
  66. return fallback;
  67. return value.value()->to_length();
  68. }
  69. LengthBox StyleProperties::length_box(CSS::PropertyID left_id, CSS::PropertyID top_id, CSS::PropertyID right_id, CSS::PropertyID bottom_id) const
  70. {
  71. LengthBox box;
  72. box.left = length_or_fallback(left_id, {});
  73. box.top = length_or_fallback(top_id, {});
  74. box.right = length_or_fallback(right_id, {});
  75. box.bottom = length_or_fallback(bottom_id, {});
  76. return box;
  77. }
  78. String StyleProperties::string_or_fallback(CSS::PropertyID id, const StringView& fallback) const
  79. {
  80. auto value = property(id);
  81. if (!value.has_value())
  82. return fallback;
  83. return value.value()->to_string();
  84. }
  85. Color StyleProperties::color_or_fallback(CSS::PropertyID id, const DOM::Document& document, Color fallback) const
  86. {
  87. auto value = property(id);
  88. if (!value.has_value())
  89. return fallback;
  90. return value.value()->to_color(document);
  91. }
  92. void StyleProperties::load_font() const
  93. {
  94. auto font_family = string_or_fallback(CSS::PropertyID::FontFamily, "Katica");
  95. auto font_weight = string_or_fallback(CSS::PropertyID::FontWeight, "normal");
  96. if (auto cached_font = FontCache::the().get({ font_family, font_weight })) {
  97. m_font = cached_font;
  98. return;
  99. }
  100. String weight;
  101. if (font_weight == "lighter")
  102. weight = "Thin";
  103. else if (font_weight == "normal")
  104. weight = "";
  105. else if (font_weight == "bold")
  106. weight = "Bold";
  107. else {
  108. dbg() << "Unknown font-weight: " << font_weight;
  109. weight = "";
  110. }
  111. auto look_for_file = [](const StringView& expected_name) -> String {
  112. // TODO: handle font sizes properly?
  113. Core::DirIterator it { "/res/fonts/", Core::DirIterator::Flags::SkipDots };
  114. while (it.has_next()) {
  115. String name = it.next_path();
  116. if (!name.ends_with(".font"))
  117. continue;
  118. if (!name.starts_with(expected_name))
  119. continue;
  120. // Check that a numeric size immediately
  121. // follows the font name. This prevents,
  122. // for example, matching KaticaBold when
  123. // the regular Katica is requested.
  124. if (!isdigit(name[expected_name.length()]))
  125. continue;
  126. return name;
  127. }
  128. return {};
  129. };
  130. // FIXME: Do this properly, with quote handling etc.
  131. for (auto& font_name : font_family.split(',')) {
  132. font_name = font_name.trim_whitespace();
  133. if (font_name == "monospace")
  134. font_name = "Csilla";
  135. auto file_name = look_for_file(String::format("%s%s", font_name.characters(), weight.characters()));
  136. if (file_name.is_null() && weight == "")
  137. file_name = look_for_file(String::format("%sRegular", font_name.characters()));
  138. if (file_name.is_null())
  139. continue;
  140. #ifdef HTML_DEBUG
  141. dbg() << "Found font " << file_name << " for family " << font_family << " weight " << font_weight;
  142. #endif
  143. m_font = Gfx::Font::load_from_file(String::format("/res/fonts/%s", file_name.characters()));
  144. FontCache::the().set({ font_name, font_weight }, *m_font);
  145. return;
  146. }
  147. #ifdef HTML_DEBUG
  148. dbg() << "Failed to find a font for family " << font_family << " weight " << font_weight;
  149. #endif
  150. if (font_weight == "bold")
  151. m_font = Gfx::Font::default_bold_font();
  152. else
  153. m_font = Gfx::Font::default_font();
  154. return;
  155. }
  156. float StyleProperties::line_height(const LayoutNode& layout_node) const
  157. {
  158. auto line_height_length = length_or_fallback(CSS::PropertyID::LineHeight, Length::make_auto());
  159. if (line_height_length.is_absolute())
  160. return (float)line_height_length.to_px(layout_node);
  161. return (float)font().glyph_height() * 1.4f;
  162. }
  163. Optional<int> StyleProperties::z_index() const
  164. {
  165. auto value = property(CSS::PropertyID::ZIndex);
  166. if (!value.has_value())
  167. return {};
  168. return static_cast<int>(value.value()->to_length().raw_value());
  169. }
  170. CSS::Position StyleProperties::position() const
  171. {
  172. if (property(CSS::PropertyID::Position).has_value()) {
  173. String position_string = string_or_fallback(CSS::PropertyID::Position, "static");
  174. if (position_string == "relative")
  175. return CSS::Position::Relative;
  176. if (position_string == "absolute")
  177. return CSS::Position::Absolute;
  178. if (position_string == "sticky")
  179. return CSS::Position::Sticky;
  180. if (position_string == "fixed")
  181. return CSS::Position::Fixed;
  182. }
  183. return CSS::Position::Static;
  184. }
  185. bool StyleProperties::operator==(const StyleProperties& other) const
  186. {
  187. if (m_property_values.size() != other.m_property_values.size())
  188. return false;
  189. for (auto& it : m_property_values) {
  190. auto jt = other.m_property_values.find(it.key);
  191. if (jt == other.m_property_values.end())
  192. return false;
  193. auto& my_value = *it.value;
  194. auto& other_value = *jt->value;
  195. if (my_value.type() != other_value.type())
  196. return false;
  197. if (my_value.to_string() != other_value.to_string())
  198. return false;
  199. }
  200. return true;
  201. }
  202. CSS::TextAlign StyleProperties::text_align() const
  203. {
  204. auto string = string_or_fallback(CSS::PropertyID::TextAlign, "left");
  205. if (string == "center")
  206. return CSS::TextAlign::Center;
  207. if (string == "right")
  208. return CSS::TextAlign::Right;
  209. if (string == "justify")
  210. return CSS::TextAlign::Justify;
  211. if (string == "-libweb-center")
  212. return CSS::TextAlign::VendorSpecificCenter;
  213. // Otherwise, just assume "left"..
  214. return CSS::TextAlign::Left;
  215. }
  216. Optional<CSS::WhiteSpace> StyleProperties::white_space() const
  217. {
  218. auto value = property(CSS::PropertyID::WhiteSpace);
  219. if (!value.has_value() || !value.value()->is_string())
  220. return {};
  221. auto string = value.value()->to_string();
  222. if (string == "normal")
  223. return CSS::WhiteSpace::Normal;
  224. if (string == "nowrap")
  225. return CSS::WhiteSpace::Nowrap;
  226. if (string == "pre")
  227. return CSS::WhiteSpace::Pre;
  228. if (string == "pre-line")
  229. return CSS::WhiteSpace::PreLine;
  230. if (string == "pre-wrap")
  231. return CSS::WhiteSpace::PreWrap;
  232. return {};
  233. }
  234. Optional<CSS::Float> StyleProperties::float_() const
  235. {
  236. auto value = property(CSS::PropertyID::Float);
  237. if (!value.has_value() || !value.value()->is_string())
  238. return {};
  239. auto string = value.value()->to_string();
  240. if (string == "none")
  241. return CSS::Float::None;
  242. if (string == "left")
  243. return CSS::Float::Left;
  244. if (string == "right")
  245. return CSS::Float::Right;
  246. return {};
  247. }
  248. CSS::Display StyleProperties::display() const
  249. {
  250. auto display = string_or_fallback(CSS::PropertyID::Display, "inline");
  251. if (display == "none")
  252. return CSS::Display::None;
  253. if (display == "block")
  254. return CSS::Display::Block;
  255. if (display == "inline")
  256. return CSS::Display::Inline;
  257. if (display == "inline-block")
  258. return CSS::Display::InlineBlock;
  259. if (display == "list-item")
  260. return CSS::Display::ListItem;
  261. if (display == "table")
  262. return CSS::Display::Table;
  263. if (display == "table-row")
  264. return CSS::Display::TableRow;
  265. if (display == "table-cell")
  266. return CSS::Display::TableCell;
  267. if (display == "table-row-group")
  268. return CSS::Display::TableRowGroup;
  269. if (display == "table-header-group")
  270. return CSS::Display::TableHeaderGroup;
  271. if (display == "table-footer-group")
  272. return CSS::Display::TableFooterGroup;
  273. dbg() << "Unknown display type: _" << display << "_";
  274. return CSS::Display::Block;
  275. }
  276. }