StyleProperties.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400
  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 <LibGfx/FontDatabase.h>
  28. #include <LibWeb/CSS/StyleProperties.h>
  29. #include <LibWeb/FontCache.h>
  30. #include <ctype.h>
  31. namespace Web::CSS {
  32. StyleProperties::StyleProperties()
  33. {
  34. }
  35. StyleProperties::StyleProperties(const StyleProperties& other)
  36. : m_property_values(other.m_property_values)
  37. {
  38. if (other.m_font) {
  39. m_font = other.m_font->clone();
  40. } else {
  41. m_font = nullptr;
  42. }
  43. }
  44. NonnullRefPtr<StyleProperties> StyleProperties::clone() const
  45. {
  46. return adopt(*new StyleProperties(*this));
  47. }
  48. void StyleProperties::set_property(CSS::PropertyID id, NonnullRefPtr<StyleValue> value)
  49. {
  50. m_property_values.set((unsigned)id, move(value));
  51. }
  52. void StyleProperties::set_property(CSS::PropertyID id, const StringView& value)
  53. {
  54. m_property_values.set((unsigned)id, StringStyleValue::create(value));
  55. }
  56. Optional<NonnullRefPtr<StyleValue>> StyleProperties::property(CSS::PropertyID id) const
  57. {
  58. auto it = m_property_values.find((unsigned)id);
  59. if (it == m_property_values.end())
  60. return {};
  61. return it->value;
  62. }
  63. Length StyleProperties::length_or_fallback(CSS::PropertyID id, const Length& fallback) const
  64. {
  65. auto value = property(id);
  66. if (!value.has_value())
  67. return fallback;
  68. return value.value()->to_length();
  69. }
  70. LengthBox StyleProperties::length_box(CSS::PropertyID left_id, CSS::PropertyID top_id, CSS::PropertyID right_id, CSS::PropertyID bottom_id) const
  71. {
  72. LengthBox box;
  73. box.left = length_or_fallback(left_id, CSS::Length::make_auto());
  74. box.top = length_or_fallback(top_id, CSS::Length::make_auto());
  75. box.right = length_or_fallback(right_id, CSS::Length::make_auto());
  76. box.bottom = length_or_fallback(bottom_id, CSS::Length::make_auto());
  77. return box;
  78. }
  79. String StyleProperties::string_or_fallback(CSS::PropertyID id, const StringView& fallback) const
  80. {
  81. auto value = property(id);
  82. if (!value.has_value())
  83. return fallback;
  84. return value.value()->to_string();
  85. }
  86. Color StyleProperties::color_or_fallback(CSS::PropertyID id, const DOM::Document& document, Color fallback) const
  87. {
  88. auto value = property(id);
  89. if (!value.has_value())
  90. return fallback;
  91. return value.value()->to_color(document);
  92. }
  93. void StyleProperties::load_font() const
  94. {
  95. auto family_value = string_or_fallback(CSS::PropertyID::FontFamily, "Katica");
  96. auto font_size = property(CSS::PropertyID::FontSize).value_or(IdentifierStyleValue::create(CSS::ValueID::Medium));
  97. auto font_weight = property(CSS::PropertyID::FontWeight).value_or(IdentifierStyleValue::create(CSS::ValueID::Normal));
  98. auto family_parts = family_value.split(',');
  99. auto family = family_parts[0];
  100. if (family.is_one_of("monospace", "ui-monospace"))
  101. family = "Csilla";
  102. else if (family.is_one_of("serif", "sans-serif", "cursive", "fantasy", "ui-serif", "ui-sans-serif", "ui-rounded"))
  103. family = "Katica";
  104. int weight = 400;
  105. if (font_weight->is_identifier()) {
  106. switch (static_cast<const IdentifierStyleValue&>(*font_weight).id()) {
  107. case CSS::ValueID::Normal:
  108. weight = 400;
  109. break;
  110. case CSS::ValueID::Bold:
  111. weight = 700;
  112. break;
  113. case CSS::ValueID::Lighter:
  114. // FIXME: This should be relative to the parent.
  115. weight = 400;
  116. break;
  117. case CSS::ValueID::Bolder:
  118. // FIXME: This should be relative to the parent.
  119. weight = 700;
  120. break;
  121. default:
  122. break;
  123. }
  124. } else if (font_weight->is_length()) {
  125. // FIXME: This isn't really a length, it's a numeric value..
  126. int font_weight_integer = font_weight->to_length().raw_value();
  127. if (font_weight_integer <= 400)
  128. weight = 400;
  129. if (font_weight_integer <= 700)
  130. weight = 700;
  131. weight = 900;
  132. }
  133. int size = 10;
  134. if (font_size->is_identifier()) {
  135. switch (static_cast<const IdentifierStyleValue&>(*font_size).id()) {
  136. case CSS::ValueID::XxSmall:
  137. case CSS::ValueID::XSmall:
  138. case CSS::ValueID::Small:
  139. case CSS::ValueID::Medium:
  140. // FIXME: Should be based on "user's default font size"
  141. size = 10;
  142. break;
  143. case CSS::ValueID::Large:
  144. case CSS::ValueID::XLarge:
  145. case CSS::ValueID::XxLarge:
  146. case CSS::ValueID::XxxLarge:
  147. // FIXME: Should be based on "user's default font size"
  148. size = 12;
  149. break;
  150. case CSS::ValueID::Smaller:
  151. // FIXME: This should be relative to the parent.
  152. size = 10;
  153. break;
  154. case CSS::ValueID::Larger:
  155. // FIXME: This should be relative to the parent.
  156. size = 12;
  157. break;
  158. default:
  159. break;
  160. }
  161. } else if (font_size->is_length()) {
  162. // FIXME: This isn't really a length, it's a numeric value..
  163. int font_size_integer = font_size->to_length().raw_value();
  164. if (font_size_integer <= 10)
  165. size = 10;
  166. else if (font_size_integer <= 12)
  167. size = 12;
  168. else
  169. size = 14;
  170. }
  171. FontSelector font_selector { family, size, weight };
  172. auto found_font = FontCache::the().get(font_selector);
  173. if (found_font) {
  174. m_font = found_font;
  175. return;
  176. }
  177. Gfx::FontDatabase::the().for_each_font([&](auto& font) {
  178. if (font.family() == family && font.weight() == weight && font.presentation_size() == size)
  179. found_font = font;
  180. });
  181. if (!found_font) {
  182. dbgln("Font not found: '{}' {} {}", family, size, weight);
  183. found_font = Gfx::Font::default_font();
  184. }
  185. m_font = found_font;
  186. FontCache::the().set(font_selector, *m_font);
  187. }
  188. float StyleProperties::line_height(const Layout::Node& layout_node) const
  189. {
  190. auto line_height_length = length_or_fallback(CSS::PropertyID::LineHeight, Length::make_auto());
  191. if (line_height_length.is_absolute())
  192. return (float)line_height_length.to_px(layout_node);
  193. return (float)font().glyph_height() * 1.4f;
  194. }
  195. Optional<int> StyleProperties::z_index() const
  196. {
  197. auto value = property(CSS::PropertyID::ZIndex);
  198. if (!value.has_value())
  199. return {};
  200. return static_cast<int>(value.value()->to_length().raw_value());
  201. }
  202. Optional<CSS::Position> StyleProperties::position() const
  203. {
  204. auto value = property(CSS::PropertyID::Position);
  205. if (!value.has_value() || !value.value()->is_identifier())
  206. return {};
  207. switch (static_cast<const IdentifierStyleValue&>(*value.value()).id()) {
  208. case CSS::ValueID::Static:
  209. return CSS::Position::Static;
  210. case CSS::ValueID::Relative:
  211. return CSS::Position::Relative;
  212. case CSS::ValueID::Absolute:
  213. return CSS::Position::Absolute;
  214. case CSS::ValueID::Fixed:
  215. return CSS::Position::Fixed;
  216. case CSS::ValueID::Sticky:
  217. return CSS::Position::Sticky;
  218. default:
  219. return {};
  220. }
  221. }
  222. bool StyleProperties::operator==(const StyleProperties& other) const
  223. {
  224. if (m_property_values.size() != other.m_property_values.size())
  225. return false;
  226. for (auto& it : m_property_values) {
  227. auto jt = other.m_property_values.find(it.key);
  228. if (jt == other.m_property_values.end())
  229. return false;
  230. auto& my_value = *it.value;
  231. auto& other_value = *jt->value;
  232. if (my_value.type() != other_value.type())
  233. return false;
  234. if (my_value != other_value)
  235. return false;
  236. }
  237. return true;
  238. }
  239. Optional<CSS::TextAlign> StyleProperties::text_align() const
  240. {
  241. auto value = property(CSS::PropertyID::TextAlign);
  242. if (!value.has_value() || !value.value()->is_identifier())
  243. return {};
  244. switch (static_cast<const IdentifierStyleValue&>(*value.value()).id()) {
  245. case CSS::ValueID::Left:
  246. return CSS::TextAlign::Left;
  247. case CSS::ValueID::Center:
  248. return CSS::TextAlign::Center;
  249. case CSS::ValueID::Right:
  250. return CSS::TextAlign::Right;
  251. case CSS::ValueID::Justify:
  252. return CSS::TextAlign::Justify;
  253. case CSS::ValueID::VendorSpecificCenter:
  254. return CSS::TextAlign::VendorSpecificCenter;
  255. default:
  256. return {};
  257. }
  258. }
  259. Optional<CSS::WhiteSpace> StyleProperties::white_space() const
  260. {
  261. auto value = property(CSS::PropertyID::WhiteSpace);
  262. if (!value.has_value() || !value.value()->is_string())
  263. return {};
  264. auto string = value.value()->to_string();
  265. if (string == "normal")
  266. return CSS::WhiteSpace::Normal;
  267. if (string == "nowrap")
  268. return CSS::WhiteSpace::Nowrap;
  269. if (string == "pre")
  270. return CSS::WhiteSpace::Pre;
  271. if (string == "pre-line")
  272. return CSS::WhiteSpace::PreLine;
  273. if (string == "pre-wrap")
  274. return CSS::WhiteSpace::PreWrap;
  275. return {};
  276. }
  277. Optional<CSS::LineStyle> StyleProperties::line_style(CSS::PropertyID property_id) const
  278. {
  279. auto value = property(property_id);
  280. if (!value.has_value() || !value.value()->is_string())
  281. return {};
  282. auto string = value.value()->to_string();
  283. if (string == "none")
  284. return CSS::LineStyle::None;
  285. if (string == "hidden")
  286. return CSS::LineStyle::Hidden;
  287. if (string == "dotted")
  288. return CSS::LineStyle::Dotted;
  289. if (string == "dashed")
  290. return CSS::LineStyle::Dashed;
  291. if (string == "solid")
  292. return CSS::LineStyle::Solid;
  293. if (string == "double")
  294. return CSS::LineStyle::Double;
  295. if (string == "groove")
  296. return CSS::LineStyle::Groove;
  297. if (string == "ridge")
  298. return CSS::LineStyle::Ridge;
  299. if (string == "inset")
  300. return CSS::LineStyle::Inset;
  301. if (string == "outset")
  302. return CSS::LineStyle::Outset;
  303. return {};
  304. }
  305. Optional<CSS::Float> StyleProperties::float_() const
  306. {
  307. auto value = property(CSS::PropertyID::Float);
  308. if (!value.has_value() || !value.value()->is_string())
  309. return {};
  310. auto string = value.value()->to_string();
  311. if (string == "none")
  312. return CSS::Float::None;
  313. if (string == "left")
  314. return CSS::Float::Left;
  315. if (string == "right")
  316. return CSS::Float::Right;
  317. return {};
  318. }
  319. Optional<CSS::Clear> StyleProperties::clear() const
  320. {
  321. auto value = property(CSS::PropertyID::Clear);
  322. if (!value.has_value() || !value.value()->is_string())
  323. return {};
  324. auto string = value.value()->to_string();
  325. if (string == "none")
  326. return CSS::Clear::None;
  327. if (string == "left")
  328. return CSS::Clear::Left;
  329. if (string == "right")
  330. return CSS::Clear::Right;
  331. if (string == "both")
  332. return CSS::Clear::Both;
  333. return {};
  334. }
  335. CSS::Display StyleProperties::display() const
  336. {
  337. auto display = string_or_fallback(CSS::PropertyID::Display, "inline");
  338. if (display == "none")
  339. return CSS::Display::None;
  340. if (display == "block")
  341. return CSS::Display::Block;
  342. if (display == "inline")
  343. return CSS::Display::Inline;
  344. if (display == "inline-block")
  345. return CSS::Display::InlineBlock;
  346. if (display == "list-item")
  347. return CSS::Display::ListItem;
  348. if (display == "table")
  349. return CSS::Display::Table;
  350. if (display == "table-row")
  351. return CSS::Display::TableRow;
  352. if (display == "table-cell")
  353. return CSS::Display::TableCell;
  354. if (display == "table-row-group")
  355. return CSS::Display::TableRowGroup;
  356. if (display == "table-header-group")
  357. return CSS::Display::TableHeaderGroup;
  358. if (display == "table-footer-group")
  359. return CSS::Display::TableFooterGroup;
  360. dbg() << "Unknown display type: _" << display << "_";
  361. return CSS::Display::Block;
  362. }
  363. }