LayoutBlock.cpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421
  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 <LibGUI/Painter.h>
  27. #include <LibWeb/CSS/StyleResolver.h>
  28. #include <LibWeb/DOM/Element.h>
  29. #include <LibWeb/Layout/LayoutBlock.h>
  30. #include <LibWeb/Layout/LayoutInline.h>
  31. #include <LibWeb/Layout/LayoutReplaced.h>
  32. #include <LibWeb/Layout/LayoutText.h>
  33. #include <math.h>
  34. namespace Web {
  35. LayoutBlock::LayoutBlock(const Node* node, NonnullRefPtr<StyleProperties> style)
  36. : LayoutBox(node, move(style))
  37. {
  38. }
  39. LayoutBlock::~LayoutBlock()
  40. {
  41. }
  42. LayoutNode& LayoutBlock::inline_wrapper()
  43. {
  44. if (!last_child() || !last_child()->is_block() || last_child()->node() != nullptr) {
  45. append_child(adopt(*new LayoutBlock(nullptr, style_for_anonymous_block())));
  46. last_child()->set_children_are_inline(true);
  47. }
  48. return *last_child();
  49. }
  50. void LayoutBlock::layout()
  51. {
  52. compute_width();
  53. compute_position();
  54. if (children_are_inline())
  55. layout_inline_children();
  56. else
  57. layout_block_children();
  58. compute_height();
  59. }
  60. void LayoutBlock::layout_block_children()
  61. {
  62. ASSERT(!children_are_inline());
  63. float content_height = 0;
  64. for_each_child([&](auto& child) {
  65. // FIXME: What should we do here? Something like a <table> might have a bunch of useless text children..
  66. if (child.is_inline())
  67. return;
  68. auto& child_block = static_cast<LayoutBlock&>(child);
  69. child_block.layout();
  70. content_height = child_block.rect().bottom() + child_block.box_model().full_margin().bottom - rect().top();
  71. });
  72. rect().set_height(content_height);
  73. }
  74. void LayoutBlock::layout_inline_children()
  75. {
  76. ASSERT(children_are_inline());
  77. m_line_boxes.clear();
  78. for_each_child([&](auto& child) {
  79. ASSERT(child.is_inline());
  80. child.split_into_lines(*this);
  81. });
  82. for (auto& line_box : m_line_boxes) {
  83. line_box.trim_trailing_whitespace();
  84. }
  85. float min_line_height = style().line_height();
  86. float line_spacing = min_line_height - style().font().glyph_height();
  87. float content_height = 0;
  88. // FIXME: This should be done by the CSS parser!
  89. CSS::ValueID text_align = CSS::ValueID::Left;
  90. auto text_align_string = style().string_or_fallback(CSS::PropertyID::TextAlign, "left");
  91. if (text_align_string == "center")
  92. text_align = CSS::ValueID::Center;
  93. else if (text_align_string == "left")
  94. text_align = CSS::ValueID::Left;
  95. else if (text_align_string == "right")
  96. text_align = CSS::ValueID::Right;
  97. else if (text_align_string == "justify")
  98. text_align = CSS::ValueID::Justify;
  99. for (auto& line_box : m_line_boxes) {
  100. float max_height = min_line_height;
  101. for (auto& fragment : line_box.fragments()) {
  102. max_height = max(max_height, fragment.rect().height());
  103. }
  104. float x_offset = x();
  105. float excess_horizontal_space = (float)width() - line_box.width();
  106. switch (text_align) {
  107. case CSS::ValueID::Center:
  108. x_offset += excess_horizontal_space / 2;
  109. break;
  110. case CSS::ValueID::Right:
  111. x_offset += excess_horizontal_space;
  112. break;
  113. case CSS::ValueID::Left:
  114. case CSS::ValueID::Justify:
  115. default:
  116. break;
  117. }
  118. float excess_horizontal_space_including_whitespace = excess_horizontal_space;
  119. int whitespace_count = 0;
  120. if (text_align == CSS::ValueID::Justify) {
  121. for (auto& fragment : line_box.fragments()) {
  122. if (fragment.is_justifiable_whitespace()) {
  123. ++whitespace_count;
  124. excess_horizontal_space_including_whitespace += fragment.rect().width();
  125. }
  126. }
  127. }
  128. float justified_space_width = whitespace_count ? (excess_horizontal_space_including_whitespace / (float)whitespace_count) : 0;
  129. for (size_t i = 0; i < line_box.fragments().size(); ++i) {
  130. auto& fragment = line_box.fragments()[i];
  131. // Vertically align everyone's bottom to the line.
  132. // FIXME: Support other kinds of vertical alignment.
  133. fragment.rect().set_x(roundf(x_offset + fragment.rect().x()));
  134. fragment.rect().set_y(y() + content_height + (max_height - fragment.rect().height()) - (line_spacing / 2));
  135. if (text_align == CSS::ValueID::Justify) {
  136. if (fragment.is_justifiable_whitespace()) {
  137. if (fragment.rect().width() != justified_space_width) {
  138. float diff = justified_space_width - fragment.rect().width();
  139. fragment.rect().set_width(justified_space_width);
  140. // Shift subsequent sibling fragments to the right to adjust for change in width.
  141. for (size_t j = i + 1; j < line_box.fragments().size(); ++j) {
  142. line_box.fragments()[j].rect().move_by(diff, 0);
  143. }
  144. }
  145. }
  146. }
  147. if (is<LayoutReplaced>(fragment.layout_node()))
  148. const_cast<LayoutReplaced&>(to<LayoutReplaced>(fragment.layout_node())).set_rect(fragment.rect());
  149. float final_line_box_width = 0;
  150. for (auto& fragment : line_box.fragments())
  151. final_line_box_width += fragment.rect().width();
  152. line_box.m_width = final_line_box_width;
  153. }
  154. content_height += max_height;
  155. }
  156. rect().set_height(content_height);
  157. }
  158. void LayoutBlock::compute_width()
  159. {
  160. auto& style = this->style();
  161. auto auto_value = Length();
  162. auto zero_value = Length(0, Length::Type::Absolute);
  163. Length margin_left;
  164. Length margin_right;
  165. Length border_left;
  166. Length border_right;
  167. Length padding_left;
  168. Length padding_right;
  169. auto try_compute_width = [&](const auto& a_width) {
  170. Length width = a_width;
  171. #ifdef HTML_DEBUG
  172. dbg() << " Left: " << margin_left << "+" << border_left << "+" << padding_left;
  173. dbg() << "Right: " << margin_right << "+" << border_right << "+" << padding_right;
  174. #endif
  175. margin_left = style.length_or_fallback(CSS::PropertyID::MarginLeft, zero_value);
  176. margin_right = style.length_or_fallback(CSS::PropertyID::MarginRight, zero_value);
  177. border_left = style.length_or_fallback(CSS::PropertyID::BorderLeftWidth, zero_value);
  178. border_right = style.length_or_fallback(CSS::PropertyID::BorderRightWidth, zero_value);
  179. padding_left = style.length_or_fallback(CSS::PropertyID::PaddingLeft, zero_value);
  180. padding_right = style.length_or_fallback(CSS::PropertyID::PaddingRight, zero_value);
  181. float total_px = 0;
  182. for (auto& value : { margin_left, border_left, padding_left, width, padding_right, border_right, margin_right }) {
  183. total_px += value.to_px();
  184. }
  185. #ifdef HTML_DEBUG
  186. dbg() << "Total: " << total_px;
  187. #endif
  188. // 10.3.3 Block-level, non-replaced elements in normal flow
  189. // If 'width' is not 'auto' and 'border-left-width' + 'padding-left' + 'width' + 'padding-right' + 'border-right-width' (plus any of 'margin-left' or 'margin-right' that are not 'auto') is larger than the width of the containing block, then any 'auto' values for 'margin-left' or 'margin-right' are, for the following rules, treated as zero.
  190. if (width.is_auto() && total_px > containing_block()->width()) {
  191. if (margin_left.is_auto())
  192. margin_left = zero_value;
  193. if (margin_right.is_auto())
  194. margin_right = zero_value;
  195. }
  196. // 10.3.3 cont'd.
  197. auto underflow_px = containing_block()->width() - total_px;
  198. if (width.is_auto()) {
  199. if (margin_left.is_auto())
  200. margin_left = zero_value;
  201. if (margin_right.is_auto())
  202. margin_right = zero_value;
  203. if (underflow_px >= 0) {
  204. width = Length(underflow_px, Length::Type::Absolute);
  205. } else {
  206. width = zero_value;
  207. margin_right = Length(margin_right.to_px() + underflow_px, Length::Type::Absolute);
  208. }
  209. } else {
  210. if (!margin_left.is_auto() && !margin_right.is_auto()) {
  211. margin_right = Length(margin_right.to_px() + underflow_px, Length::Type::Absolute);
  212. } else if (!margin_left.is_auto() && margin_right.is_auto()) {
  213. margin_right = Length(underflow_px, Length::Type::Absolute);
  214. } else if (margin_left.is_auto() && !margin_right.is_auto()) {
  215. margin_left = Length(underflow_px, Length::Type::Absolute);
  216. } else { // margin_left.is_auto() && margin_right.is_auto()
  217. auto half_of_the_underflow = Length(underflow_px / 2, Length::Type::Absolute);
  218. margin_left = half_of_the_underflow;
  219. margin_right = half_of_the_underflow;
  220. }
  221. }
  222. return width;
  223. };
  224. auto specified_width = style.length_or_fallback(CSS::PropertyID::Width, auto_value);
  225. // 1. The tentative used width is calculated (without 'min-width' and 'max-width')
  226. auto used_width = try_compute_width(specified_width);
  227. // 2. The tentative used width is greater than 'max-width', the rules above are applied again,
  228. // but this time using the computed value of 'max-width' as the computed value for 'width'.
  229. auto specified_max_width = style.length_or_fallback(CSS::PropertyID::MaxWidth, auto_value);
  230. if (!specified_max_width.is_auto()) {
  231. if (used_width.to_px() > specified_max_width.to_px()) {
  232. used_width = try_compute_width(specified_max_width);
  233. }
  234. }
  235. // 3. If the resulting width is smaller than 'min-width', the rules above are applied again,
  236. // but this time using the value of 'min-width' as the computed value for 'width'.
  237. auto specified_min_width = style.length_or_fallback(CSS::PropertyID::MinWidth, auto_value);
  238. if (!specified_min_width.is_auto()) {
  239. if (used_width.to_px() < specified_min_width.to_px()) {
  240. used_width = try_compute_width(specified_min_width);
  241. }
  242. }
  243. rect().set_width(used_width.to_px());
  244. box_model().margin().left = margin_left;
  245. box_model().margin().right = margin_right;
  246. box_model().border().left = border_left;
  247. box_model().border().right = border_right;
  248. box_model().padding().left = padding_left;
  249. box_model().padding().right = padding_right;
  250. }
  251. void LayoutBlock::compute_position()
  252. {
  253. auto& style = this->style();
  254. auto auto_value = Length();
  255. auto zero_value = Length(0, Length::Type::Absolute);
  256. auto width = style.length_or_fallback(CSS::PropertyID::Width, auto_value);
  257. if (style.position() == CSS::Position::Absolute) {
  258. box_model().offset().top = style.length_or_fallback(CSS::PropertyID::Top, zero_value);
  259. box_model().offset().right = style.length_or_fallback(CSS::PropertyID::Right, zero_value);
  260. box_model().offset().bottom = style.length_or_fallback(CSS::PropertyID::Bottom, zero_value);
  261. box_model().offset().left = style.length_or_fallback(CSS::PropertyID::Left, zero_value);
  262. }
  263. box_model().margin().top = style.length_or_fallback(CSS::PropertyID::MarginTop, zero_value);
  264. box_model().margin().bottom = style.length_or_fallback(CSS::PropertyID::MarginBottom, zero_value);
  265. box_model().border().top = style.length_or_fallback(CSS::PropertyID::BorderTopWidth, zero_value);
  266. box_model().border().bottom = style.length_or_fallback(CSS::PropertyID::BorderBottomWidth, zero_value);
  267. box_model().padding().top = style.length_or_fallback(CSS::PropertyID::PaddingTop, zero_value);
  268. box_model().padding().bottom = style.length_or_fallback(CSS::PropertyID::PaddingBottom, zero_value);
  269. float position_x = box_model().margin().left.to_px()
  270. + box_model().border().left.to_px()
  271. + box_model().padding().left.to_px()
  272. + box_model().offset().left.to_px();
  273. if (style.position() != CSS::Position::Absolute || containing_block()->style().position() == CSS::Position::Absolute)
  274. position_x += containing_block()->x();
  275. rect().set_x(position_x);
  276. float position_y = box_model().full_margin().top
  277. + box_model().offset().top.to_px();
  278. if (style.position() != CSS::Position::Absolute || containing_block()->style().position() == CSS::Position::Absolute) {
  279. LayoutBlock* relevant_sibling = previous_sibling();
  280. while (relevant_sibling != nullptr) {
  281. if (relevant_sibling->style().position() != CSS::Position::Absolute)
  282. break;
  283. relevant_sibling = relevant_sibling->previous_sibling();
  284. }
  285. if (relevant_sibling == nullptr) {
  286. position_y += containing_block()->y();
  287. } else {
  288. auto& previous_sibling_rect = relevant_sibling->rect();
  289. auto& previous_sibling_style = relevant_sibling->box_model();
  290. position_y += previous_sibling_rect.y() + previous_sibling_rect.height();
  291. position_y += previous_sibling_style.full_margin().bottom;
  292. }
  293. }
  294. rect().set_y(position_y);
  295. }
  296. void LayoutBlock::compute_height()
  297. {
  298. auto& style = this->style();
  299. auto height_property = style.property(CSS::PropertyID::Height);
  300. if (!height_property.has_value())
  301. return;
  302. auto height_length = height_property.value()->to_length();
  303. if (height_length.is_absolute())
  304. rect().set_height(height_length.to_px());
  305. }
  306. void LayoutBlock::render(RenderingContext& context)
  307. {
  308. if (!is_visible())
  309. return;
  310. LayoutBox::render(context);
  311. if (children_are_inline()) {
  312. for (auto& line_box : m_line_boxes) {
  313. for (auto& fragment : line_box.fragments()) {
  314. if (context.should_show_line_box_borders())
  315. context.painter().draw_rect(enclosing_int_rect(fragment.rect()), Color::Green);
  316. fragment.render(context);
  317. }
  318. }
  319. }
  320. }
  321. HitTestResult LayoutBlock::hit_test(const Gfx::Point& position) const
  322. {
  323. if (!children_are_inline())
  324. return LayoutBox::hit_test(position);
  325. HitTestResult result;
  326. for (auto& line_box : m_line_boxes) {
  327. for (auto& fragment : line_box.fragments()) {
  328. if (enclosing_int_rect(fragment.rect()).contains(position)) {
  329. return { fragment.layout_node(), fragment.text_index_at(position.x()) };
  330. }
  331. }
  332. }
  333. // FIXME: This should be smarter about the text position if we're hitting a block
  334. // that has text inside it, but `position` is to the right of the text box.
  335. return { rect().contains(position.x(), position.y()) ? this : nullptr };
  336. }
  337. NonnullRefPtr<StyleProperties> LayoutBlock::style_for_anonymous_block() const
  338. {
  339. auto new_style = StyleProperties::create();
  340. style().for_each_property([&](auto property_id, auto& value) {
  341. if (StyleResolver::is_inherited_property(property_id))
  342. new_style->set_property(property_id, value);
  343. });
  344. return new_style;
  345. }
  346. LineBox& LayoutBlock::ensure_last_line_box()
  347. {
  348. if (m_line_boxes.is_empty())
  349. m_line_boxes.append(LineBox());
  350. return m_line_boxes.last();
  351. }
  352. LineBox& LayoutBlock::add_line_box()
  353. {
  354. m_line_boxes.append(LineBox());
  355. return m_line_boxes.last();
  356. }
  357. }