This directory contains the implementation of Blink's new layout engine “LayoutNG”.
This README can be viewed in formatted form here.
The original design document can be seen here.
CSS has many different types of layout modes, controlled by the display
property. (In addition to this specific HTML elements have custom layout modes as well). For each different type of layout, we have a NGLayoutAlgorithm.
The input to an NGLayoutAlgorithm is the same tuple for every kind of layout:
The NGBlockNode which we are currently performing layout for. The following information is accessed:
The ComputedStyle for the node which we are currently performing laying for.
The list of children NGBlockNodees to perform layout upon, and their respective style objects.
The NGConstraintSpace which represents the “space” in which the current layout should produce a NGPhysicalFragment.
TODO(layout-dev): BreakTokens should go here once implemented.
The current layout should not access any information outside this set, this will break invariants in the system. (As a concrete example we intend to cache NGPhysicalFragments based on this set, accessing additional information outside this set will break caching behaviour).
TODO(layout-dev): Document with lots of pretty pictures.
Inline layout is performed in the following phases:
This is similar to CSS Text Processing Order of Operations, but not exactly the same, because the spec prioritizes the simple description than being accurate.
For inline layout there is a pre-layout pass that prepares the internal data structures needed to perform line layout.
The pre-layout pass, triggered by calling NGInlineNode::PrepareLayout()
, has three separate steps or stages that are executed in order:
CollectInlines
: Performs a depth-first scan of the container collecting all non-atomic inlines and TextNodes
s. Atomic inlines are represented as a unicode object replacement character but are otherwise skipped. Each non-atomic inline and TextNodes
is fed to a NGInlineItemsBuilder instance which collects the text content for all non-atomic inlines in the container.
During this process white-space is collapsed and normalized according to CSS white-space processing rules.
The CSS text-transform is already applied in LayoutObject tree. The plan is to implement in this phase when LayoutNG builds the tree from DOM.
SegmentText
: Performs BiDi segmentation and resolution. See Bidirectional text below.
ShapeText
: Shapes the resolved BiDi runs using HarfBuzz. TODO(eae): Fill out
NGLineBreaker takes a list of NGInlineItem, measure them, break into lines, and produces a list of NGInlineItemResult for each line.
NGInlineItemResult keeps several information needed during the line box construction, such as inline size and ShapeResult, though the inline position is recomputed later because Bidirectional text may change it.
This phase:
ShapingLineBreaker is... TODO(kojii): fill out.
NGInlineLayoutAlgorithm::CreateLine()
takes a list of NGInlineItemResult and produces NGPhysicalLineBoxFragment for each line.
Lines are then wrapped in an anonymous NGPhysicalBoxFragment so that one NGInlineNode has one corresponding fragment.
This phase consists of following sub-phases:
Bidirectional reordering: Reorder the list of NGInlineItemResult according to UAX#9 Reordering Resolved Levels. See Bidirectional text below.
After this point forward, the list of NGInlineItemResult is in visual order; which is from line-left to line-right. The block direction is still logical, but the inline direction is physical.
Create a NGPhysicalFragment for each NGInlineItemResult in visual (line-left to line-right) order, and place them into NGPhysicalLineBoxFragment.
The inline size of each item was already determined by NGLineBreaker, but the inline position is recomputed because BiDi reordering may have changed it.
In block direction, NGPhysicalFragment is placed as if the baseline is at 0. This is adjusted later, possibly multiple times. See Inline Box Tree and the post-process below.
Post-process the constructed line box. This includes:
A flat list structure is suitable for many inline operations, but some operations require an inline box tree structure. A stack of NGInlineBoxState is constructed from a list of NGInlineItemResult to represent the box tree structure.
This stack:
Because of index-based operations to the list of NGInlineItemResult, the list is append-only during the process. When all operations are done, OnEndPlaceItems()
turns the list into the final fragment tree structure.
UAX#9 Unicode Bidirectional Algorithm defines processing algorithm for bidirectional text.
The core logic is implemented in NGBidiParagraph, which is a thin wrapper for ICU BiDi.
In a bird's‐eye view, it consists of two parts:
Before line breaking: Segmenting text and resolve embedding levels as defined in UAX#9 Resolving Embedding Levels.
The core logic uses ICU BiDi ubidi_getLogicalRun()
function.
This is part of the Pre-layout phase above.
After line breaking: Reorder text as defined in UAX#9 Reordering Resolved Levels.
The core logic uses ICU BiDi ubidi_reorderVisual()
function.
This is part of the Line Box Construction phase above.
TODO(layout-dev): Document with lots of pretty pictures.
TODO(layout-dev): Document with lots of pretty pictures.
This section contains details specific to the NGBlockLayoutAlgorithm.
TODO(layout-dev): Document with lots of pretty pictures.
TODO(layout-dev): Document with lots of pretty pictures.
The latest code coverage (from Feb 14 2017) can be found here. Here is the instruction how to generate a new result.
git clone https://github.com/DynamoRIO/dynamorio.git
npm install vinyl, npm install vinyl-fs
git clone https://github.com/mweibel/lcov-result-merger
chromium\src> ninja -C out\Debug webkit_unit_tests
chromium\src>DynamoRIO\bin64\drrun.exe -t drcov -- .\out\Debug\webkit_unit_tests.exe --gtest_filter=NG*
chromium\src>for %file in (*.log) do DynamoRIO\tools\bin64\drcov2lcov.exe -input %file -output %file.info -src_filter layout/ng -src_skip_filter _test
chromium\src>node lcov-result-merger\bin\lcov-result-merger.js *.info output.info
chromium\src>C:\Perl64\bin\perl.exe dynamorio.git\third_party\lcov\genhtml output.info -o output