LayoutBlock.cpp 12 KB

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