LayoutBlock.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  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. float 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. float min_line_height = style().line_height();
  60. float 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. float max_height = min_line_height;
  74. for (auto& fragment : line_box.fragments()) {
  75. max_height = max(max_height, 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(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. Length margin_left;
  137. Length margin_right;
  138. Length border_left;
  139. Length border_right;
  140. Length padding_left;
  141. Length padding_right;
  142. auto try_compute_width = [&](const auto& a_width) {
  143. Length width = a_width;
  144. #ifdef HTML_DEBUG
  145. dbg() << " Left: " << margin_left << "+" << border_left << "+" << padding_left;
  146. dbg() << "Right: " << margin_right << "+" << border_right << "+" << padding_right;
  147. #endif
  148. margin_left = style.length_or_fallback(CSS::PropertyID::MarginLeft, zero_value);
  149. margin_right = style.length_or_fallback(CSS::PropertyID::MarginRight, zero_value);
  150. border_left = style.length_or_fallback(CSS::PropertyID::BorderLeftWidth, zero_value);
  151. border_right = style.length_or_fallback(CSS::PropertyID::BorderRightWidth, zero_value);
  152. padding_left = style.length_or_fallback(CSS::PropertyID::PaddingLeft, zero_value);
  153. padding_right = style.length_or_fallback(CSS::PropertyID::PaddingRight, zero_value);
  154. float total_px = 0;
  155. for (auto& value : { margin_left, border_left, padding_left, width, padding_right, border_right, margin_right }) {
  156. total_px += value.to_px();
  157. }
  158. #ifdef HTML_DEBUG
  159. dbg() << "Total: " << total_px;
  160. #endif
  161. // 10.3.3 Block-level, non-replaced elements in normal flow
  162. // 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.
  163. if (width.is_auto() && total_px > containing_block()->width()) {
  164. if (margin_left.is_auto())
  165. margin_left = zero_value;
  166. if (margin_right.is_auto())
  167. margin_right = zero_value;
  168. }
  169. // 10.3.3 cont'd.
  170. auto underflow_px = containing_block()->width() - total_px;
  171. if (width.is_auto()) {
  172. if (margin_left.is_auto())
  173. margin_left = zero_value;
  174. if (margin_right.is_auto())
  175. margin_right = zero_value;
  176. if (underflow_px >= 0) {
  177. width = Length(underflow_px, Length::Type::Absolute);
  178. } else {
  179. width = zero_value;
  180. margin_right = Length(margin_right.to_px() + underflow_px, Length::Type::Absolute);
  181. }
  182. } else {
  183. if (!margin_left.is_auto() && !margin_right.is_auto()) {
  184. margin_right = Length(margin_right.to_px() + underflow_px, Length::Type::Absolute);
  185. } else if (!margin_left.is_auto() && margin_right.is_auto()) {
  186. margin_right = Length(underflow_px, Length::Type::Absolute);
  187. } else if (margin_left.is_auto() && !margin_right.is_auto()) {
  188. margin_left = Length(underflow_px, Length::Type::Absolute);
  189. } else { // margin_left.is_auto() && margin_right.is_auto()
  190. auto half_of_the_underflow = Length(underflow_px / 2, Length::Type::Absolute);
  191. margin_left = half_of_the_underflow;
  192. margin_right = half_of_the_underflow;
  193. }
  194. }
  195. return width;
  196. };
  197. auto specified_width = style.length_or_fallback(CSS::PropertyID::Width, auto_value);
  198. // 1. The tentative used width is calculated (without 'min-width' and 'max-width')
  199. auto used_width = try_compute_width(specified_width);
  200. // 2. The tentative used width is greater than 'max-width', the rules above are applied again,
  201. // but this time using the computed value of 'max-width' as the computed value for 'width'.
  202. auto specified_max_width = style.length_or_fallback(CSS::PropertyID::MaxWidth, auto_value);
  203. if (!specified_max_width.is_auto()) {
  204. if (used_width.to_px() > specified_max_width.to_px()) {
  205. used_width = try_compute_width(specified_max_width);
  206. }
  207. }
  208. // 3. If the resulting width is smaller than 'min-width', the rules above are applied again,
  209. // but this time using the value of 'min-width' as the computed value for 'width'.
  210. auto specified_min_width = style.length_or_fallback(CSS::PropertyID::MinWidth, auto_value);
  211. if (!specified_min_width.is_auto()) {
  212. if (used_width.to_px() < specified_min_width.to_px()) {
  213. used_width = try_compute_width(specified_min_width);
  214. }
  215. }
  216. rect().set_width(used_width.to_px());
  217. box_model().margin().left = margin_left;
  218. box_model().margin().right = margin_right;
  219. box_model().border().left = border_left;
  220. box_model().border().right = border_right;
  221. box_model().padding().left = padding_left;
  222. box_model().padding().right = padding_right;
  223. }
  224. void LayoutBlock::compute_position()
  225. {
  226. auto& style = this->style();
  227. auto auto_value = Length();
  228. auto zero_value = Length(0, Length::Type::Absolute);
  229. auto width = style.length_or_fallback(CSS::PropertyID::Width, auto_value);
  230. box_model().margin().top = style.length_or_fallback(CSS::PropertyID::MarginTop, zero_value);
  231. box_model().margin().bottom = style.length_or_fallback(CSS::PropertyID::MarginBottom, zero_value);
  232. box_model().border().top = style.length_or_fallback(CSS::PropertyID::BorderTopWidth, zero_value);
  233. box_model().border().bottom = style.length_or_fallback(CSS::PropertyID::BorderBottomWidth, zero_value);
  234. box_model().padding().top = style.length_or_fallback(CSS::PropertyID::PaddingTop, zero_value);
  235. box_model().padding().bottom = style.length_or_fallback(CSS::PropertyID::PaddingBottom, zero_value);
  236. rect().set_x(containing_block()->x() + box_model().margin().left.to_px() + box_model().border().left.to_px() + box_model().padding().left.to_px());
  237. float top_border = -1;
  238. if (previous_sibling() != nullptr) {
  239. auto& previous_sibling_rect = previous_sibling()->rect();
  240. auto& previous_sibling_style = previous_sibling()->box_model();
  241. top_border = previous_sibling_rect.y() + previous_sibling_rect.height();
  242. top_border += previous_sibling_style.full_margin().bottom;
  243. } else {
  244. top_border = containing_block()->y();
  245. }
  246. rect().set_y(top_border + box_model().full_margin().top);
  247. }
  248. void LayoutBlock::compute_height()
  249. {
  250. auto& style = this->style();
  251. auto height_property = style.property(CSS::PropertyID::Height);
  252. if (!height_property.has_value())
  253. return;
  254. auto height_length = height_property.value()->to_length();
  255. if (height_length.is_absolute())
  256. rect().set_height(height_length.to_px());
  257. }
  258. void LayoutBlock::render(RenderingContext& context)
  259. {
  260. if (!is_visible())
  261. return;
  262. LayoutBox::render(context);
  263. if (children_are_inline()) {
  264. for (auto& line_box : m_line_boxes) {
  265. for (auto& fragment : line_box.fragments()) {
  266. if (context.should_show_line_box_borders())
  267. context.painter().draw_rect(enclosing_int_rect(fragment.rect()), Color::Green);
  268. fragment.render(context);
  269. }
  270. }
  271. }
  272. }
  273. HitTestResult LayoutBlock::hit_test(const Point& position) const
  274. {
  275. if (!children_are_inline())
  276. return LayoutBox::hit_test(position);
  277. HitTestResult result;
  278. for (auto& line_box : m_line_boxes) {
  279. for (auto& fragment : line_box.fragments()) {
  280. if (enclosing_int_rect(fragment.rect()).contains(position)) {
  281. return { fragment.layout_node(), fragment.text_index_at(position.x()) };
  282. }
  283. }
  284. }
  285. return {};
  286. }
  287. NonnullRefPtr<StyleProperties> LayoutBlock::style_for_anonymous_block() const
  288. {
  289. auto new_style = StyleProperties::create();
  290. style().for_each_property([&](auto property_id, auto& value) {
  291. if (StyleResolver::is_inherited_property(property_id))
  292. new_style->set_property(property_id, value);
  293. });
  294. return new_style;
  295. }
  296. LineBox& LayoutBlock::ensure_last_line_box()
  297. {
  298. if (m_line_boxes.is_empty())
  299. m_line_boxes.append(LineBox());
  300. return m_line_boxes.last();
  301. }
  302. LineBox& LayoutBlock::add_line_box()
  303. {
  304. m_line_boxes.append(LineBox());
  305. return m_line_boxes.last();
  306. }