tree: 53e7201c91b29444c344b42f6fe58c7dff3e2e6b [path history] [tgz]
  1. api/
  2. doc/
  3. geometry/
  4. line/
  5. ng/
  6. resources/
  7. shapes/
  8. svg/
  9. adjust_for_absolute_zoom.h
  10. background_bleed_avoidance.h
  11. bidi_run.h
  12. bidi_run_for_line.cc
  13. bidi_run_for_line.h
  14. box_layout_extra_input.h
  15. BUILD.gn
  16. collapsed_border_value.cc
  17. collapsed_border_value.h
  18. collapsed_border_value_test.cc
  19. column_balancer.cc
  20. column_balancer.h
  21. content_change_type.h
  22. counter_node.cc
  23. counter_node.h
  24. custom_scrollbar.cc
  25. custom_scrollbar.h
  26. DEPS
  27. depth_ordered_layout_object_list.cc
  28. depth_ordered_layout_object_list.h
  29. flexible_box_algorithm.cc
  30. flexible_box_algorithm.h
  31. floating_objects.cc
  32. floating_objects.h
  33. force_legacy_layout_test.cc
  34. fragmentainer_iterator.cc
  35. fragmentainer_iterator.h
  36. fragmentation_context.h
  37. generated_children.h
  38. grid.cc
  39. grid.h
  40. grid_baseline_alignment.cc
  41. grid_baseline_alignment.h
  42. grid_layout_utils.cc
  43. grid_layout_utils.h
  44. grid_test.cc
  45. grid_track_sizing_algorithm.cc
  46. grid_track_sizing_algorithm.h
  47. hit_test_cache.cc
  48. hit_test_cache.h
  49. hit_test_canvas_result.cc
  50. hit_test_canvas_result.h
  51. hit_test_location.cc
  52. hit_test_location.h
  53. hit_test_request.h
  54. hit_test_result.cc
  55. hit_test_result.h
  56. hit_testing_test.cc
  57. hit_testing_transform_state.cc
  58. hit_testing_transform_state.h
  59. intrinsic_sizing_info.h
  60. layout_analyzer.cc
  61. layout_analyzer.h
  62. layout_block.cc
  63. layout_block.h
  64. layout_block_flow.cc
  65. layout_block_flow.h
  66. layout_block_flow_line.cc
  67. layout_block_test.cc
  68. layout_box.cc
  69. layout_box.h
  70. layout_box_model_object.cc
  71. layout_box_model_object.h
  72. layout_box_model_object_test.cc
  73. layout_box_test.cc
  74. layout_br.cc
  75. layout_br.h
  76. layout_button.cc
  77. layout_button.h
  78. layout_count_test.cc
  79. layout_counter.cc
  80. layout_counter.h
  81. layout_custom_scrollbar_part.cc
  82. layout_custom_scrollbar_part.h
  83. layout_deprecated_flexible_box.cc
  84. layout_deprecated_flexible_box.h
  85. layout_details_marker.cc
  86. layout_details_marker.h
  87. layout_embedded_content.cc
  88. layout_embedded_content.h
  89. layout_embedded_content_test.cc
  90. layout_embedded_object.cc
  91. layout_embedded_object.h
  92. layout_fieldset.cc
  93. layout_fieldset.h
  94. layout_file_upload_control.cc
  95. layout_file_upload_control.h
  96. layout_flexible_box.cc
  97. layout_flexible_box.h
  98. layout_flexible_box_test.cc
  99. layout_flow_thread.cc
  100. layout_flow_thread.h
  101. layout_frame.cc
  102. layout_frame.h
  103. layout_frame_set.cc
  104. layout_frame_set.h
  105. layout_geometry_map.cc
  106. layout_geometry_map.h
  107. layout_geometry_map_step.h
  108. layout_geometry_map_test.cc
  109. layout_grid.cc
  110. layout_grid.h
  111. layout_grid_test.cc
  112. layout_html_canvas.cc
  113. layout_html_canvas.h
  114. layout_iframe.cc
  115. layout_iframe.h
  116. layout_image.cc
  117. layout_image.h
  118. layout_image_resource.cc
  119. layout_image_resource.h
  120. layout_image_resource_style_image.cc
  121. layout_image_resource_style_image.h
  122. layout_image_test.cc
  123. layout_inline.cc
  124. layout_inline.h
  125. layout_inline_test.cc
  126. layout_inside_list_marker.cc
  127. layout_inside_list_marker.h
  128. layout_list_item.cc
  129. layout_list_item.h
  130. layout_list_marker.cc
  131. layout_list_marker.h
  132. layout_list_marker_image.cc
  133. layout_list_marker_image.h
  134. layout_media.cc
  135. layout_media.h
  136. layout_media_test.cc
  137. layout_multi_column_flow_thread.cc
  138. layout_multi_column_flow_thread.h
  139. layout_multi_column_flow_thread_test.cc
  140. layout_multi_column_set.cc
  141. layout_multi_column_set.h
  142. layout_multi_column_spanner_placeholder.cc
  143. layout_multi_column_spanner_placeholder.h
  144. layout_object.cc
  145. layout_object.h
  146. layout_object_child_list.cc
  147. layout_object_child_list.h
  148. layout_object_factory.cc
  149. layout_object_factory.h
  150. layout_object_inlines.h
  151. layout_object_test.cc
  152. layout_outside_list_marker.cc
  153. layout_outside_list_marker.h
  154. layout_progress.cc
  155. layout_progress.h
  156. layout_progress_test.cc
  157. layout_quote.cc
  158. layout_quote.h
  159. layout_replaced.cc
  160. layout_replaced.h
  161. layout_replaced_test.cc
  162. layout_ruby.cc
  163. layout_ruby.h
  164. layout_ruby_base.cc
  165. layout_ruby_base.h
  166. layout_ruby_run.cc
  167. layout_ruby_run.h
  168. layout_ruby_text.cc
  169. layout_ruby_text.h
  170. layout_shift_region.cc
  171. layout_shift_region.h
  172. layout_shift_region_test.cc
  173. layout_shift_tracker.cc
  174. layout_shift_tracker.h
  175. layout_shift_tracker_test.cc
  176. layout_slider_container.cc
  177. layout_slider_container.h
  178. layout_state.cc
  179. layout_state.h
  180. layout_table.cc
  181. layout_table.h
  182. layout_table_box_component.cc
  183. layout_table_box_component.h
  184. layout_table_caption.cc
  185. layout_table_caption.h
  186. layout_table_cell.cc
  187. layout_table_cell.h
  188. layout_table_cell_test.cc
  189. layout_table_col.cc
  190. layout_table_col.h
  191. layout_table_col_test.cc
  192. layout_table_row.cc
  193. layout_table_row.h
  194. layout_table_row_test.cc
  195. layout_table_section.cc
  196. layout_table_section.h
  197. layout_table_section_test.cc
  198. layout_table_test.cc
  199. layout_text.cc
  200. layout_text.h
  201. layout_text_combine.cc
  202. layout_text_combine.h
  203. layout_text_control.cc
  204. layout_text_control.h
  205. layout_text_control_multi_line.cc
  206. layout_text_control_multi_line.h
  207. layout_text_control_single_line.cc
  208. layout_text_control_single_line.h
  209. layout_text_control_single_line_test.cc
  210. layout_text_control_test.cc
  211. layout_text_fragment.cc
  212. layout_text_fragment.h
  213. layout_text_fragment_test.cc
  214. layout_text_test.cc
  215. layout_theme.cc
  216. layout_theme.h
  217. layout_theme_android.cc
  218. layout_theme_android.h
  219. layout_theme_default.cc
  220. layout_theme_default.h
  221. layout_theme_font_provider.cc
  222. layout_theme_font_provider.h
  223. layout_theme_font_provider_default.cc
  224. layout_theme_font_provider_win.cc
  225. layout_theme_fuchsia.cc
  226. layout_theme_linux.cc
  227. layout_theme_linux.h
  228. layout_theme_mac.h
  229. layout_theme_mac.mm
  230. layout_theme_mobile.cc
  231. layout_theme_mobile.h
  232. layout_theme_test.cc
  233. layout_theme_win.cc
  234. layout_theme_win.h
  235. layout_tree_as_text.cc
  236. layout_tree_as_text.h
  237. layout_video.cc
  238. layout_video.h
  239. layout_video_test.cc
  240. layout_view.cc
  241. layout_view.h
  242. layout_view_test.cc
  243. layout_vtt_cue.cc
  244. layout_vtt_cue.h
  245. layout_word_break.cc
  246. layout_word_break.h
  247. list_marker.cc
  248. list_marker.h
  249. list_marker_text.cc
  250. list_marker_text.h
  251. map_coordinates_flags.h
  252. map_coordinates_test.cc
  253. min_max_size_test.cc
  254. min_max_sizes.cc
  255. min_max_sizes.h
  256. multi_column_fragmentainer_group.cc
  257. multi_column_fragmentainer_group.h
  258. multi_column_fragmentainer_group_test.cc
  259. order_iterator.cc
  260. order_iterator.h
  261. overflow_model.h
  262. overflow_model_test.cc
  263. OWNERS
  264. paint_containment_test.cc
  265. pointer_events_hit_rules.cc
  266. pointer_events_hit_rules.h
  267. README.md
  268. scroll_anchor.cc
  269. scroll_anchor.h
  270. scroll_anchor_test.cc
  271. scrollbars_test.cc
  272. style_retain_scope.cc
  273. style_retain_scope.h
  274. style_retain_scope_test.cc
  275. subtree_layout_scope.cc
  276. subtree_layout_scope.h
  277. table_grid_cell.cc
  278. table_grid_cell.h
  279. table_layout_algorithm.h
  280. table_layout_algorithm_auto.cc
  281. table_layout_algorithm_auto.h
  282. table_layout_algorithm_fixed.cc
  283. table_layout_algorithm_fixed.h
  284. text_autosizer.cc
  285. text_autosizer.h
  286. text_autosizer_test.cc
  287. text_decoration_offset.cc
  288. text_decoration_offset.h
  289. text_decoration_offset_base.cc
  290. text_decoration_offset_base.h
  291. text_run_constructor.cc
  292. text_run_constructor.h
  293. traced_layout_object.cc
  294. traced_layout_object.h
  295. vertical_position_cache.h
  296. view_fragmentation_context.cc
  297. view_fragmentation_context.h
  298. visual_rect_mapping_perftest.cc
  299. visual_rect_mapping_test.cc
  300. web_font_rendering_win.cc
third_party/blink/renderer/core/layout/README.md

Blink Layout

The renderer/core/layout directory contains the implementation of layout objects. It covers the following document lifecycle states:

  • LayoutSubtreeChange (InLayoutSubtreeChange and LayoutSubtreeChangeClean)
  • PreLayout (InPreLayout)
  • PerformLayout (InPerformLayout)
  • AfterPerformLayout (AfterPerformLayout and LayoutClean)

Note that a new Blink layout system is under development. See the LayoutNG design document.

The layout code is maintained by the layout team.

Box model

The CSS box model is based on a series of nested boxes, from outside to inside:

  • Margin box
  • Border box: the main coordinate space of a LayoutBox
  • Padding box: a.k.a. client box
  • Content box

When there are non-overlay scrollbars, according to css-overflow-3, they should be inserted between the inner border edge and the outer padding edge.

The following graph is modified from the graph in the css box model spec, showing scrollbars:

    |-------------------------------------------------|
    |                                                 |
    |                  margin-top                     |
    |                                                 |
    |    |---------------------------------------|    |
    |    |                                       |    |
    |    |             border-top                |    |
    |    |                                       |    |
    |    |    |--------------------------|--|    |    |
    |    |    |                          |  |    |    |
    |    |    |       padding-top        |##|    |    |
    |    |    |                          |##|    |    |
    |    |    |    |----------------|    |##|    |    |
    |    |    |    |                |    |  |    |    |
    | ML | BL | PL |  content box   | PR |SW| BR | MR |
    |    |    |    |                |    |  |    |    |
    |    |    |    |----------------|    |  |    |    |
    |    |    |                          |  |    |    |
    |    |    |      padding-bottom      |  |    |    |
    |    |    |                          |  |    |    |
    |    |    |--------------------------|--|    |    |
    |    |    |     scrollbar height ####|SC|    |    |
    |    |    |-----------------------------|    |    |
    |    |                                       |    |
    |    |           border-bottom               |    |
    |    |                                       |    |
    |    |---------------------------------------|    |
    |                                                 |
    |                margin-bottom                    |
    |                                                 |
    |-------------------------------------------------|

    BL = border-left
    BR = border-right
    ML = margin-left
    MR = margin-right
    PL = padding-left
    PR = padding-right
    SC = scroll corner
    SW = scrollbar width

Note that the vertical scrollbar (if existing) will be on the left in right-to-left direction horizontal writing-mode. The horizontal scrollbar (if existing) is always at the bottom.

Scroll origin vs. offset vs. position

When a LayoutBox has scrollable overflow, it is associated with a PaintLayerScrollableArea. PaintLayerScrollableArea uses a “scroll origin” to represent the location of the top/left corner of the content rect (the visible part of the content) in the coordinate system defined by the top/left corner of the overflow rect, when the box is scrolled all the way to the beginning of its content.

For content which flows left-to-right and top-to-bottom, the scroll origin will be (0, 0), i.e., the top/left of the content rect is coincident with the top/left of the overflow rect when the box is scrolled all the way to the beginning of content.

For content which flows right-to-left (including direction:ltr, writing-mode:vertical-rl, and flex-direction:row-reverse), the x-coordinate of the scroll origin will be positive; and for content which flows bottom-to-top (e.g., flex-direction:column-reverse and vertical writing-mode with direction:ltr), the y-coordinate of the scroll origin will be positive.

In all cases, the term ‘scrollOffset’ (or just ‘offset’) is used to represent the distance of the scrolling viewport from its location when scrolled to the beginning of content, and it uses type ScrollOffset. The term ‘scrollPosition’ (or just ‘position’) represents a point in the coordinate space defined by the overflow rect, and it uses type FloatPoint.

For illustrations of these concepts, see these files:

doc/ltr-tb-scroll.png doc/rtl-bt-scroll.png doc/rtl-tb-scroll.png

When computing the scroll origin, if the box is laid out right-to-left and it has a scrollbar for the orthogonal direction (e.g., a vertical scrollbar in a direction:rtl block), the size of the scrollbar must be added to the scroll origin calculation. Here are two examples -- note that it doesn't matter whether the vertical scrollbar is placed on the right or left of the box (the vertical scrollbar is the |/| part):

                               content
                                 rect
                             |<-------->|
                    scroll
                    origin
                |----------->|
                 _______________________
                |          |/|          |
                |          |/|          |
                |          |/|          |
direction:rtl   |          |/|   box    |
                |          |/|          |
                |          |/|          |
                |__________|/|__________|

                      overflow rect
                |<--------------------->|

                               content
                                 rect
                             |<-------->|
                    scroll
                    origin
                |----------->|
                 _________________________
                |            |          |/|
                |            |          |/|
                |            |          |/|
writing-mode:   |            |    box   |/|
  vertical-rl   |            |          |/|
                |            |          |/|
                |____________|__________|/|

                      overflow rect
                |<--------------------->|

Coordinate Spaces

Layout and Paint work with and frequently refer to four coordinate spaces (really two, with two variants):

  • Physical coordinates: Corresponds to physical direction of the output per the physical display (screen, printed page). Generally used for painting, thus layout logic that feeds into paint may produce values in this space. CSS properties such as top, right, bottom, and left are in this space. See also the ‘flipped block-flow direction’ variant space below.

  • Logical coordinates: Used in layout to allow for generalized positioning that fits with whatever the writing-mode and direction CSS property values may be. Properties named with before, after, start or end are in this space. These are also known respectively as ‘logical top’, ‘logical bottom’, ‘logical left’, and ‘logical right’.

  • Physical coordinates with flipped block-flow direction: The same as ‘physical coordinates’, but for writing-mode: vertical-rl where blocks are laid out right-to-left, block position is “flipped” from the left to the right side of their containing block. This is essentially a mirror reflection horizontally across the center of a block's containing block.

    For writing-mode values other than vertical-rl there is no change from physical coordinates.

    Layout and paint logic reference this space to connote whether “flipping” has been applied to the values. Final painted output for “flipped block-flow” writing mode must, by definition, incorporate flipping. It can be expensive to look up the writing mode of an object. Performing computation on values known to be in this space can save on the overhead required to unflip/reflip.

  • Logical coordinates without flipping inline direction: those are “logical block coordinates”, without considering text direction. Examples are “LogicalLeft” and “LogicalRight”.

Example with writing-mode: vertical-rl; direction: ltr:

                   'top' / 'start' side

                    block-flow direction
          <------------------------------------ |
          ------------------------------------- |
          |        c   |          s           | |
'left'    |        o   |          o           | |   inline     'right'
   /      |        n   |          m           | |  direction      /
'after'   |        t   |          e           | |              'before'
 side     |        e   |                      | |                side
          |        n   |                      | |
          |        t   |                      | |
          ------------------------------------- v

                'bottom' / 'end' side

Another example -- consider a relative-positioned element:

<style>
html {
    writing-mode: vertical-rl;
}
</style>
<div id="container" style="background-color: lightBlue; width: 300px; height: 200px;">
  <div id="relpos" style="position: relative; top: 50px; left: -60px; width: 70px; height: 80px; background-color: red;"></div>
</div>

The final location of these within an 800x600 frame is as:

container: (492, 8  300x200)
relpos:    (662, 58 70x80)

The 8px is the default margin of HTML body element per https://html.spec.whatwg.org/multipage/rendering.html#the-page, which is specified in ../html/resources/html.css.

See the diagram for full detail on dimensions of the involved elements.

Determining the paint invalidation rect for relpos via MapToVisualRectInAncestorSpace() involves walking up the layout tree from relpos flipping the rect within its container at each box. Below we sketch each step as we recurse toward the top of the document, with ‘this’ on the left, the current rect being mapped on the right, and explanation beneath each:

LayoutBlockFlow (relative positioned) DIV id='relpos'   0,0    70x80

    Apply the relative position of 'relpos' while flipping within
    'container' to respect writing mode.

    170 = 300 (container width) - 70 (relpos width) - 60 (relpos left)
    50 = relpos top

LayoutBlockFlow DIV id='container'                      170,50 70x80

    Since body has the same width as container, flipping has
    no effect on the rect in this step.

LayoutBlockFlow BODY                                    170,50 70x80

    Flip within the html block, which is symmetrically 8px larger than body
    due to default margin.

LayoutBlockFlow HTML                                    178,58 70x80

    Flip the rectangle within the view.

    662 = 800 (view width) - 316 (html width) + 178 (current rect left)

LayoutView #document                                    662,58 70x80

Since relative-positioned elements are positioned via physical coordinates, and flipping at each step mirrors the position based on the width of the containing box at that step, we can only compute the final physical pixels in screen space for a relative-positioned element if we walk up the full layout tree from the starting object to the topmost view as described above.

For more examples of writing mode and direction combinations, see this demo page though note horizontal-bt is obsolete.

Flipped Block-Flow Coordinates

The nature of “flipping” a value as a mirror reflection within its containing block is such that flipping twice with the same container will produce the original result. Thus when working on involved logic it can be easy to accidentally flip unnecessarily, since flipping (say) one too many times can be “corrected” by flipping again. This can obviously lead to confusing and less performant code, so care should be taken to understand and document any changes to flipping logic.

Blink test coverage for features used in vertical writing modes, and vertical-rl in particular, may not be as comprehensive as for horizontal writing mode. Keep this in mind when writing new functionality or tests by making sure to incorporate coverage for all writing modes when appropriate.

Values are generally transformed into flipped block-flow coordinates via a set of methods on the involved layout objects. See in particular FlipForWritingMode(), FlipForWritingModeForChild().

InlineBox::FlipForWritingMode() variants flip the input value within the inline box's containing block.

LayoutBox::FlipForWritingMode() variants flip the input value within the referenced box.

LayoutBox::FlipForWritingModeForChild() variants flip the input value within the referenced box, offsetting for the specified child box's current x-position and width. This is useful for a common pattern wherein we build up a point location starting with the current location of the (child) box.

For LayoutBox and InlineBox classes and subclasses:

  • PhysicalLocation() returns the physical location of a box or inline in the containing block. (0,0) is the top-left corner of the containing block. Flipping is performed on the values as needed. For LayoutBox, if the containing block is not passed to PhysicalLocation(), looking it up requires walking up the layout tree, which can be expensive. InlineBox::PhysicalLocation() is expensive only if the InlineBox is in flipped block-flow writing mode.
  • Location() returns the location of a box or inline in the “physical coordinates with flipped block-flow direction” coordinate space. (0,0) is the top-left corner of the containing block for writing-mode in normal blocks direction (horizontal-tb and vertical-lr), and is the top-right corner of the containing block for writing-mode in flipped block-flow direction (vertical-rl).

Note there are two primary similar, but slightly different, methods regarding finding the containing block for an element:

  • LayoutObject::Container() returns the containing block for an element as defined by CSS.
  • LayoutObject::ContainingBlock() which returns the enclosing non-anonymous block for an element. If the containing block is a relatively positioned inline, it returns that inline's enclosing non-anonymous block. This is the one used by PhysicalLocation().

There are other containing block methods in LayoutObject for special purposes such as fixed position, absolute position, and paint invalidation. Code will sometimes just refer to the ‘containing’ element, which is an unfortunately ambiguous term. Paying close attention to which method was used to obtain the containing element is important.

More complex web platform features such as tables, flexbox, and multicol are typically implemented atop these primitives, along with checks such as IsFlippedBlocksWritingMode(), IsLeftToRightDirection(), and IsHorizontalWritingMode(). See for example LayoutTableSection::LogicalRectForWritingModeAndDirection(), LayoutFlexibleBox::UpdateAutoMarginsInCrossAxis() or LayoutMultiColumnFlowThread::FlowThreadTranslationAtPoint().

Geometry mapping

TODO(wkorman): Elaborate on:

  • mapToVisualRectInAncestorSpace()
  • mapAncestorToLocal()
  • Widget and FrameView trees. Note the former will be done away with at some point per http://crbug.com/637460.
  • GeometryMapper (or just point to its section in paint README). For now, see the Web page geometries design document.

Scrolling

TODO(wkorman): Provide an overview of scrolling. For now, the BlinkOn talk on Scrolling in Blink is a good overview.

Root layer scrolling is an ongoing refactoring of Blink's scrolling architecture, which makes the root PaintLayer responsible for the scrolling that was previously done by FrameView. For more details, see: Root Layer Scrolling.

Glossaries

Here we provide a brief overview of key terms relevant to box flow, inline flow, and text orientation. For more detail see CSS Writing Modes Level 3.

The CSS Logical Properties Level 1 specification represents the latest CSSWG thinking on logical coordinate space naming. CSSWG has standardized on block-start, block-end, inline-start, and inline-end, or just start and end when the axis is either implied or irrelevant.

Note that much of the Blink code base predates the logical properties specification and so does not yet reference logical direction consistently in the stated manner, though we would like to head in that direction over time. See also the physical, flow-relative, and line-relative abstract box terminology specification.

  • writing-mode: either horizontal or vertical, with vertical having either left-to-right or right-to-left block flow. Geometry is transposed for vertical writing mode. See calls to transposed{Rect,Point,Size}().
  • direction/dir: “inline base direction” of a box. One of ltr or rtl. See calls to isLeftToRightDirection().
  • text-orientation: orientation of text in a line. Only relevant for vertical modes.
  • orthogonal flow: when a box has a writing mode perpendicular to its containing block. This can lead to complex cases. See specification for more.