LayoutBlock.cpp 15 KB

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