Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(125)

Side by Side Diff: third_party/WebKit/Source/core/layout/README.md

Issue 2380933004: Update layout documentation re: coordinate systems and writing mode. (Closed)
Patch Set: Sync to head. Created 4 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « third_party/WebKit/Source/core/layout/LayoutBoxModelObject.h ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 # `Source/core/layout` 1 # `Source/core/layout`
2 2
3 This directory contains implementation of layout objects. It covers the
4 following document lifecycle states:
5
6 * LayoutSubtreeChange (`InLayoutSubtreeChange` and `LayoutSubtreeChangeClean`)
7 * PreLayout (`InPreLayout`)
8 * PerformLayout (`InPerformLayout`)
9 * AfterPerformLayout (`AfterPerformLayout` and `LayoutClean`)
10
11 Note that a new Blink layout system is under development. See the
12 [LayoutNG design document](https://docs.google.com/document/d/1uxbDh4uONFQOiGuiu mlJBLGgO4KDWB8ZEkp7Rd47fw4/preview).
13
3 ## Overflow rects and scroll offsets 14 ## Overflow rects and scroll offsets
4 15
5 PaintLayerScrollableArea uses a "scroll origin" to conceptually represent the di stance between 16 PaintLayerScrollableArea uses a "scroll origin" to conceptually represent the di stance between
6 the top-left corner of the box'es content rect and the top-left corner of its ov erflow rect 17 the top-left corner of the box'es content rect and the top-left corner of its ov erflow rect
7 when the box is scrolled to the logical beginning of its content (.e.g. all the way to the left for 18 when the box is scrolled to the logical beginning of its content (e.g. all the w ay to the left for
8 LTR, all the way to the right for RTL). For left-to-right and top-to-bottom flo ws, the scroll 19 LTR, all the way to the right for RTL). For left-to-right and top-to-bottom flo ws, the scroll
9 origin is zero, i.e., the top/left of the overflow rect is at the same position as the top/left of 20 origin is zero, i.e., the top/left of the overflow rect is at the same position as the top/left of
10 the box'es content rect when scrolled to the beginning of flow. For right-to-le ft and bottom-to-top 21 the box'es content rect when scrolled to the beginning of flow. For right-to-le ft and bottom-to-top
11 flows, the overflow rect extends to the top/left of the client rect. 22 flows, the overflow rect extends to the top/left of the client rect.
12 23
13 The default calculation for scroll origin is the distance between the top-left c orner of the content 24 The default calculation for scroll origin is the distance between the top-left c orner of the content
14 rect and the top-left corner of the overflow rect. To illustrate, here is a box with left overflow and 25 rect and the top-left corner of the overflow rect. To illustrate, here is a box with left overflow and
15 no vertical scrollbar: 26 no vertical scrollbar:
16 27
17 content 28 content
18 rect 29 rect
19 |<-------->| 30 |<-------->|
20 scroll 31 scroll
21 origin 32 origin
22 |<-------->| 33 |<-------->|
23 _____________________ 34 _____________________
24 | | | 35 | | |
25 | | | 36 | | |
26 | | | 37 | | |
27 direction:rtl | | box | 38 direction:rtl | | box |
28 | | | 39 | | |
29 | | | 40 | | |
30 |__________|__________| 41 |__________|__________|
31 42
32 overflow rect 43 overflow rect
33 |<--------------------->| 44 |<--------------------->|
34 45
35 46
36 However, if the box has a scrollbar for the orthogonal direction (e.g., a vertic al scrollbar 47 However, if the box has a scrollbar for the orthogonal direction (e.g., a vertic al scrollbar
37 in a direction:rtl block), the size of the scrollbar must be added to the scroll origin calculation. 48 in a direction:rtl block), the size of the scrollbar must be added to the scroll origin calculation.
38 Here are two examples -- note that it doesn't matter whether the vertical scroll bar is placed on 49 Here are two examples -- note that it doesn't matter whether the vertical scroll bar is placed on
39 the right or left of the box (the vertical scrollbar is the |/| part): 50 the right or left of the box (the vertical scrollbar is the `|/|` part):
40 51
41 content 52 content
42 rect 53 rect
43 |<-------->| 54 |<-------->|
44 scroll 55 scroll
45 origin 56 origin
46 |<---------->| 57 |<---------->|
47 _______________________
48 | |/| |
49 | |/| |
50 | |/| |
51 direction:rtl | |/| box |
52 | |/| |
53 | |/| |
54 |__________|/|__________|
55
56 overflow rect
57 |<--------------------->|
58
59
60
61 content
62 rect
63 |<-------->|
64 scroll
65 origin
66 |<---------->|
67 _______________________ 58 _______________________
68 | | |/| 59 | |/| |
69 | | |/| 60 | |/| |
70 | | |/| 61 | |/| |
71 writing-mode: | | |/| 62 direction:rtl | |/| box |
72 vertical-rl | | |/| 63 | |/| |
73 | | |/| 64 | |/| |
74 | | |/| 65 |__________|/|__________|
75 | | |/| 66
76 |__________|__________|/|
77
78 overflow rect 67 overflow rect
79 |<--------------------->| 68 |<--------------------->|
80 69
70 content
71 rect
72 |<-------->|
73 scroll
74 origin
75 |<---------->|
76 _______________________
77 | | |/|
78 | | |/|
79 | | |/|
80 writing-mode: | | |/|
81 vertical-rl | | |/|
82 | | |/|
83 | | |/|
84 | | |/|
85 |__________|__________|/|
86
87 overflow rect
88 |<--------------------->|
89
81 ## Coordinate Spaces 90 ## Coordinate Spaces
82 91
83 TODO(wkorman): Document writing mode, particularly flipped blocks. 92 Layout and Paint work with and frequently refer to three main coordinate spaces
93 (really two, with one variant):
94
95 * Physical coordinates: Corresponds to physical direction of the output per the
96 physical display (screen, printed page). Generally used for painting, thus
97 layout logic that feeds into paint may produce values in this space. CSS
98 properties such as `top`, `right`, `bottom`, and `left` are in this space. See
99 also the 'flipped block-flow direction' variant space below.
100
101 * Logical coordinates: Used in layout to allow for generalized positioning that
102 fits with whatever the `writing-mode` and `direction` CSS property values may
103 be. Properties named with `before`, `after`, `start` or `end` are in this
104 space. These are also known respectively as 'logical top', 'logical bottom',
105 'logical left', and 'logical right'.
106
107 * Physical coordinates with flipped block-flow direction: The same as 'physical
108 coordinates', but for `writing-mode: vertical-rl` where blocks are laid out
109 right-to-left, block position is "flipped" from the left to the right side of
110 their containing block. This is essentially a mirror reflection horizontally
111 across the center of a block's containing block.
112
113 For `writing-mode` values other than `vertical-rl` there is no change from
114 physical coordinates.
115
116 Layout and paint logic reference this space to connote whether "flipping" has
117 been applied to the values. Final painted output for "flipped block-flow"
118 writing mode must, by definition, incorporate flipping. It can be expensive to
119 look up the writing mode of an object. Performing computation on values known
120 to be in this space can save on the overhead required to unflip/reflip.
121
122 Example with `writing-mode: vertical-rl; direction: ltr`:
123
124 'top' / 'start' side
125
126 block-flow direction
127 <------------------------------------ |
128 ------------------------------------- |
129 | c | s | |
130 'left' | o | o | | inline 'right'
131 / | n | m | | direction /
132 'after' | t | e | | 'before'
133 side | e | | | side
134 | n | | |
135 | t | | |
136 ------------------------------------- v
137
138 'bottom' / 'end' side
139
140 Another example -- consider a relative-positioned element:
141
142 <style>
143 html {
144 writing-mode: vertical-rl;
145 }
146 </style>
147 <div id="container" style="background-color: lightBlue; width: 300px; height : 200px;">
148 <div id="relpos" style="position: relative; top: 50px; left: -60px; width: 70px; height: 80px; background-color: red;"></div>
149 </div>
150
151 The final location of these within an 800x600 frame is as:
152
153 container: (492, 8 300x200)
154 relpos: (662, 58 70x80)
155
156 See the [diagram](https://docs.google.com/drawings/d/1k5lOzQxhfRQ1J4HknGByJPJfpD V4o_N_ZuVTvveiSaw/edit?usp=sharing)
157 for full detail on dimensions of the involved elements.
158
159 Determining the paint invalidation rect for `relpos` via
160 `mapToVisualRectInAncestorSpace()` involves walking up the layout tree from
161 `relpos` flipping the rect within its container at each box. Below we sketch
162 each step as we recurse toward the top of the document, with 'this' on the left,
163 the current rect being mapped on the right, and explanation beneath each:
164
165 LayoutBlockFlow (relative positioned) DIV id='relpos' 0,0 70x80
166
167 Apply the relative position of 'relpos' while flipping within
168 'container' to respect writing mode.
169
170 170 = 300 (container width) - 70 (relpos width) - 60 (relpos left)
171 50 = relpos top
172
173 LayoutBlockFlow DIV id='container' 170,50 70x80
174
175 Since body has the same width as container, flipping has
176 no effect on the rect in this step.
177
178 LayoutBlockFlow BODY 170,50 70x80
179
180 Flip within the html block, which is symmetrically 8px larger than body
181 due to default margin.
182
183 LayoutBlockFlow HTML 178,58 70x80
184
185 Flip the rectangle within the view.
186
187 662 = 800 (view width) - 316 (html width) + 178 (current rect left)
188
189 LayoutView #document 662,58 70x80
190
191 Since relative-positioned elements are positioned via physical coordinates, and
192 flipping at each step mirrors the position based on the width of the containing
193 box at that step, we can only compute the final physical pixels in screen space
194 for a relative-positioned element if we walk up the full layout tree from the
195 starting object to the topmost view as described above.
196
197 For more examples of writing mode and direction combinations, see this
198 [demo page](http://pauljadam.com/demos/csstext.html) though note `horizontal-bt`
199 is obsolete.
200
201 ### Flipped Block-Flow Coordinates
202
203 The nature of "flipping" a value as a mirror reflection within its containing
204 block is such that flipping twice with the same container will produce the
205 original result. Thus when working on involved logic it can be easy to
206 accidentally flip unnecessarily, since flipping (say) one too many times can be
207 "corrected" by flipping again. This can obviously lead to confusing and less
208 performant code, so care should be taken to understand and document any changes
209 to flipping logic.
210
211 Blink test coverage for features used in vertical writing modes, and
212 `vertical-rl` in particular, may not be as comprehensive as for horizontal
213 writing mode. Keep this in mind when writing new functionality or tests by
214 making sure to incorporate coverage for all writing modes when appropriate.
215
216 Values are generally transformed into flipped block-flow coordinates via a set
217 of methods on the involved layout objects. See in particular
218 `flipForWritingMode()`, `flipForWritingModeForChild()`, and `topLeftLocation()`.
219
220 `InlineBox::flipForWritingMode()` variants flip the input value within the
221 inline box's containing block.
222
223 `LayoutBox::flipForWritingMode()` variants flip the input value within the
224 referenced box.
225
226 `LayoutBox::flipForWritingModeForChild()` variants flip the input value within
227 the referenced box, offsetting for the specified child box's current x-position
228 and width. This is useful for a common pattern wherein we build up a point
229 location starting with the current location of the (child) box.
230
231 `LayoutBox::topLeftLocation()` performs flipping as needed. If the containing
232 block is not passed to the method, looking it up requires walking up the layout
233 tree, which can be expensive.
234
235 Note there are two primary similar, but slightly different, methods regarding
236 finding the containing block for an element:
237
238 * `LayoutObject::container()` returns the containing block for an element as
239 defined by CSS.
240 * `LayoutObject::containingBlock()` which returns the enclosing non-anonymous
241 block for an element. If the containing block is a relatively positioned inline,
242 it returns that inline's enclosing non-anonymous block. This is the one used by
243 `topLeftLocation()`.
244
245 There are other containing block methods in `LayoutObject` for special purposes
246 such as fixed position, absolute position, and paint invalidation. Code will
247 sometimes just refer to the 'containing' element, which is an unfortunately
248 ambiguous term. Paying close attention to which method was used to obtain the
249 containing element is important.
250
251 More complex web platform features such as tables, flexbox, and multicol are
252 typically implemented atop these primitives, along with checks such as
253 `isFlippedBlocksWritingMode()`, `isLeftToRightDirection()`, and
254 `isHorizontalWritingMode()`. See for example
255 `LayoutTableSection::logicalRectForWritingModeAndDirection()`,
256 `LayoutFlexibleBox::updateAutoMarginsInCrossAxis()` or
257 `LayoutMultiColumnFlowThread::flowThreadTranslationAtPoint()`.
258
259 ## Geometry mapping
260
261 TODO(wkorman): Elaborate on:
262 * `mapToVisualRectInAncestorSpace()`
263 * `mapAncestorToLocal()`
264 * `Widget` and `FrameView` trees. Note the former will be done away with at some
265 point per http://crbug.com/637460.
266 * `GeometryMapper` (or just point to its section in paint README). For now, see
267 the
268 [Web page geometries](https://docs.google.com/document/d/1WZKlOSUK4XI0Le0fgCsy UTVw0dTwutZXGWwzlHXewiU/preview)
269 design document.
270
271 ## Scrolling
272
273 TODO(wkorman): Provide an overview of scrolling. For now, the BlinkOn talk
274 on
275 [Scrolling in Blink](https://docs.google.com/presentation/d/1pwx0qBW4wSmYAOJxq2g b3SMvSTCHz2L2TFx_bjsvm8E/preview)
276 is a good overview.
277
278 ## Glossaries
279
280 Here we provide a brief overview of key terms relevant to box flow, inline flow,
281 and text orientation. For more detail see
282 [CSS Writing Modes Level 3](https://www.w3.org/TR/css-writing-modes-3/).
283
284 The
285 [CSS Logical Properties Level 1](https://drafts.csswg.org/css-logical-props/)
286 specification represents the latest CSSWG thinking on logical coordinate space
287 naming. CSSWG has standardized on `block-start`, `block-end`, `inline-start`,
288 and `inline-end`, or just `start` and `end` when the axis is either implied or
289 irrelevant.
290
291 Note that much of the Blink code base predates the logical properties
292 specification and so does not yet reference logical direction consistently in
293 the stated manner, though we would like to head in that direction over time.
294 See also the *physical*, *flow-relative*, and *line-relative*
295 [abstract box terminology](https://www.w3.org/TR/css-writing-modes-3/#abstract-b ox)
296 specification.
297
298 * `writing-mode`: either horizontal or vertical, with vertical having either
299 left-to-right or right-to-left block flow. Geometry is transposed for vertical
300 writing mode. See calls to `transposed{Rect,Point,Size}()`.
301 * `direction`/`dir`: "inline base direction" of a box. One of `ltr` or
302 `rtl`. See calls to `isLeftToRightDirection()`.
303 * `text-orientation`: orientation of text in a line. Only relevant for vertical
304 modes.
305 * orthogonal flow: when a box has a writing mode perpendicular to its containing
306 block. This can lead to complex cases. See
307 [specification](https://www.w3.org/TR/css-writing-modes-3/#orthogonal-flows)
308 for more.
OLDNEW
« no previous file with comments | « third_party/WebKit/Source/core/layout/LayoutBoxModelObject.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698