StyleProperties.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478
  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 CSS::Length& default_value) const
  71. {
  72. LengthBox box;
  73. box.left = length_or_fallback(left_id, default_value);
  74. box.top = length_or_fallback(top_id, default_value);
  75. box.right = length_or_fallback(right_id, default_value);
  76. box.bottom = length_or_fallback(bottom_id, default_value);
  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_identifier())
  263. return {};
  264. switch (static_cast<const IdentifierStyleValue&>(*value.value()).id()) {
  265. case CSS::ValueID::Normal:
  266. return CSS::WhiteSpace::Normal;
  267. case CSS::ValueID::Nowrap:
  268. return CSS::WhiteSpace::Nowrap;
  269. case CSS::ValueID::Pre:
  270. return CSS::WhiteSpace::Pre;
  271. case CSS::ValueID::PreLine:
  272. return CSS::WhiteSpace::PreLine;
  273. case CSS::ValueID::PreWrap:
  274. return CSS::WhiteSpace::PreWrap;
  275. default:
  276. return {};
  277. }
  278. }
  279. Optional<CSS::LineStyle> StyleProperties::line_style(CSS::PropertyID property_id) const
  280. {
  281. auto value = property(property_id);
  282. if (!value.has_value() || !value.value()->is_identifier())
  283. return {};
  284. switch (static_cast<const IdentifierStyleValue&>(*value.value()).id()) {
  285. case CSS::ValueID::None:
  286. return CSS::LineStyle::None;
  287. case CSS::ValueID::Hidden:
  288. return CSS::LineStyle::Hidden;
  289. case CSS::ValueID::Dotted:
  290. return CSS::LineStyle::Dotted;
  291. case CSS::ValueID::Dashed:
  292. return CSS::LineStyle::Dashed;
  293. case CSS::ValueID::Solid:
  294. return CSS::LineStyle::Solid;
  295. case CSS::ValueID::Double:
  296. return CSS::LineStyle::Double;
  297. case CSS::ValueID::Groove:
  298. return CSS::LineStyle::Groove;
  299. case CSS::ValueID::Ridge:
  300. return CSS::LineStyle::Ridge;
  301. case CSS::ValueID::Inset:
  302. return CSS::LineStyle::Inset;
  303. case CSS::ValueID::Outset:
  304. return CSS::LineStyle::Outset;
  305. default:
  306. return {};
  307. }
  308. }
  309. Optional<CSS::Float> StyleProperties::float_() const
  310. {
  311. auto value = property(CSS::PropertyID::Float);
  312. if (!value.has_value() || !value.value()->is_identifier())
  313. return {};
  314. switch (static_cast<const IdentifierStyleValue&>(*value.value()).id()) {
  315. case CSS::ValueID::None:
  316. return CSS::Float::None;
  317. case CSS::ValueID::Left:
  318. return CSS::Float::Left;
  319. case CSS::ValueID::Right:
  320. return CSS::Float::Right;
  321. default:
  322. return {};
  323. }
  324. }
  325. Optional<CSS::Clear> StyleProperties::clear() const
  326. {
  327. auto value = property(CSS::PropertyID::Clear);
  328. if (!value.has_value() || !value.value()->is_identifier())
  329. return {};
  330. switch (static_cast<const IdentifierStyleValue&>(*value.value()).id()) {
  331. case CSS::ValueID::None:
  332. return CSS::Clear::None;
  333. case CSS::ValueID::Left:
  334. return CSS::Clear::Left;
  335. case CSS::ValueID::Right:
  336. return CSS::Clear::Right;
  337. case CSS::ValueID::Both:
  338. return CSS::Clear::Both;
  339. default:
  340. return {};
  341. }
  342. }
  343. CSS::Display StyleProperties::display() const
  344. {
  345. auto value = property(CSS::PropertyID::Display);
  346. if (!value.has_value() || !value.value()->is_identifier())
  347. return CSS::Display::Inline;
  348. switch (static_cast<const IdentifierStyleValue&>(*value.value()).id()) {
  349. case CSS::ValueID::None:
  350. return CSS::Display::None;
  351. case CSS::ValueID::Block:
  352. return CSS::Display::Block;
  353. case CSS::ValueID::Inline:
  354. return CSS::Display::Inline;
  355. case CSS::ValueID::InlineBlock:
  356. return CSS::Display::InlineBlock;
  357. case CSS::ValueID::ListItem:
  358. return CSS::Display::ListItem;
  359. case CSS::ValueID::Table:
  360. return CSS::Display::Table;
  361. case CSS::ValueID::TableRow:
  362. return CSS::Display::TableRow;
  363. case CSS::ValueID::TableCell:
  364. return CSS::Display::TableCell;
  365. case CSS::ValueID::TableRowGroup:
  366. return CSS::Display::TableRowGroup;
  367. case CSS::ValueID::TableHeaderGroup:
  368. return CSS::Display::TableHeaderGroup;
  369. case CSS::ValueID::TableFooterGroup:
  370. return CSS::Display::TableFooterGroup;
  371. default:
  372. return CSS::Display::Block;
  373. }
  374. }
  375. Optional<CSS::TextDecorationLine> StyleProperties::text_decoration_line() const
  376. {
  377. auto value = property(CSS::PropertyID::TextDecorationLine);
  378. if (!value.has_value() || !value.value()->is_identifier())
  379. return {};
  380. switch (static_cast<const IdentifierStyleValue&>(*value.value()).id()) {
  381. case CSS::ValueID::None:
  382. return CSS::TextDecorationLine::None;
  383. case CSS::ValueID::Underline:
  384. return CSS::TextDecorationLine::Underline;
  385. case CSS::ValueID::Overline:
  386. return CSS::TextDecorationLine::Overline;
  387. case CSS::ValueID::LineThrough:
  388. return CSS::TextDecorationLine::LineThrough;
  389. case CSS::ValueID::Blink:
  390. return CSS::TextDecorationLine::Blink;
  391. default:
  392. return {};
  393. }
  394. }
  395. Optional<CSS::TextTransform> StyleProperties::text_transform() const
  396. {
  397. auto value = property(CSS::PropertyID::TextTransform);
  398. if (!value.has_value())
  399. return {};
  400. switch (value.value()->to_identifier()) {
  401. case CSS::ValueID::None:
  402. return CSS::TextTransform::None;
  403. case CSS::ValueID::Lowercase:
  404. return CSS::TextTransform::Lowercase;
  405. case CSS::ValueID::Uppercase:
  406. return CSS::TextTransform::Uppercase;
  407. case CSS::ValueID::Capitalize:
  408. return CSS::TextTransform::Capitalize;
  409. case CSS::ValueID::FullWidth:
  410. return CSS::TextTransform::FullWidth;
  411. case CSS::ValueID::FullSizeKana:
  412. return CSS::TextTransform::FullSizeKana;
  413. default:
  414. return {};
  415. }
  416. }
  417. Optional<CSS::ListStyleType> StyleProperties::list_style_type() const
  418. {
  419. auto value = property(CSS::PropertyID::ListStyleType);
  420. if (!value.has_value())
  421. return {};
  422. switch (value.value()->to_identifier()) {
  423. case CSS::ValueID::None:
  424. return CSS::ListStyleType::None;
  425. case CSS::ValueID::Disc:
  426. return CSS::ListStyleType::Disc;
  427. case CSS::ValueID::Circle:
  428. return CSS::ListStyleType::Circle;
  429. case CSS::ValueID::Square:
  430. return CSS::ListStyleType::Square;
  431. case CSS::ValueID::Decimal:
  432. return CSS::ListStyleType::Decimal;
  433. default:
  434. return {};
  435. }
  436. }
  437. }