OLD | NEW |
1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef NGBlockLayoutAlgorithm_h | 5 #ifndef NGBlockLayoutAlgorithm_h |
6 #define NGBlockLayoutAlgorithm_h | 6 #define NGBlockLayoutAlgorithm_h |
7 | 7 |
8 #include "core/CoreExport.h" | 8 #include "core/CoreExport.h" |
9 #include "core/layout/ng/ng_box.h" | 9 #include "core/layout/ng/ng_box.h" |
| 10 #include "core/layout/ng/ng_constraint_space_builder.h" |
10 #include "core/layout/ng/ng_fragment_builder.h" | 11 #include "core/layout/ng/ng_fragment_builder.h" |
11 #include "core/layout/ng/ng_layout_algorithm.h" | 12 #include "core/layout/ng/ng_layout_algorithm.h" |
12 #include "wtf/RefPtr.h" | 13 #include "wtf/RefPtr.h" |
13 | 14 |
14 namespace blink { | 15 namespace blink { |
15 | 16 |
16 class ComputedStyle; | 17 class ComputedStyle; |
17 class NGConstraintSpace; | 18 class NGConstraintSpace; |
18 class NGPhysicalFragment; | 19 class NGPhysicalFragment; |
19 | 20 |
20 // A class for general block layout (e.g. a <div> with no special style). | 21 // A class for general block layout (e.g. a <div> with no special style). |
21 // Lays out the children in sequence. | 22 // Lays out the children in sequence. |
22 class CORE_EXPORT NGBlockLayoutAlgorithm : public NGLayoutAlgorithm { | 23 class CORE_EXPORT NGBlockLayoutAlgorithm : public NGLayoutAlgorithm { |
23 public: | 24 public: |
24 // Default constructor. | 25 // Default constructor. |
25 // @param style Style reference of the block that is being laid out. | 26 // @param style Style reference of the block that is being laid out. |
26 // @param first_child Our first child; the algorithm will use its NextSibling | 27 // @param first_child Our first child; the algorithm will use its NextSibling |
27 // method to access all the children. | 28 // method to access all the children. |
28 NGBlockLayoutAlgorithm(PassRefPtr<const ComputedStyle>, NGBox* first_child); | 29 // @param space The constraint space which the algorithm should generate a |
| 30 // fragment within. |
| 31 NGBlockLayoutAlgorithm(PassRefPtr<const ComputedStyle>, |
| 32 NGBox* first_child, |
| 33 NGConstraintSpace* space); |
29 | 34 |
30 // Actual layout implementation. Lays out the children in sequence within the | 35 bool Layout(NGPhysicalFragment**) override; |
31 // constraints given by the NGConstraintSpace. Returns a fragment with the | |
32 // resulting layout information. | |
33 // This function can not be const because for interruptible layout, we have | |
34 // to be able to store state information. | |
35 // Returns true when done; when this function returns false, it has to be | |
36 // called again. The out parameter will only be set when this function | |
37 // returns true. The same constraint space has to be passed each time. | |
38 bool Layout(const NGConstraintSpace*, NGPhysicalFragment**) override; | |
39 | 36 |
40 DECLARE_VIRTUAL_TRACE(); | 37 DECLARE_VIRTUAL_TRACE(); |
41 | 38 |
42 private: | 39 private: |
| 40 bool LayoutCurrentChild(); |
| 41 |
43 // Computes collapsed margins for 2 adjoining blocks and updates the resultant | 42 // Computes collapsed margins for 2 adjoining blocks and updates the resultant |
44 // fragment's MarginStrut if needed. | 43 // fragment's MarginStrut if needed. |
45 // See https://www.w3.org/TR/CSS2/box.html#collapsing-margins | 44 // See https://www.w3.org/TR/CSS2/box.html#collapsing-margins |
46 // | 45 // |
47 // @param space Constraint space for the block. | |
48 // @param child_margins Margins information for the current child. | 46 // @param child_margins Margins information for the current child. |
49 // @param fragment Current child's fragment. | 47 // @param fragment Current child's fragment. |
50 // @return NGBoxStrut with margins block start/end. | 48 // @return NGBoxStrut with margins block start/end. |
51 NGBoxStrut CollapseMargins(const NGConstraintSpace& space, | 49 NGBoxStrut CollapseMargins(const NGBoxStrut& child_margins, |
52 const NGBoxStrut& child_margins, | |
53 const NGFragment& fragment); | 50 const NGFragment& fragment); |
54 | 51 |
55 // Calculates position of the in-flow block-level fragment that needs to be | 52 // Calculates position of the in-flow block-level fragment that needs to be |
56 // positioned relative to the current fragment that is being built. | 53 // positioned relative to the current fragment that is being built. |
57 // | 54 // |
58 // @param fragment Fragment that needs to be placed. | 55 // @param fragment Fragment that needs to be placed. |
59 // @param child_margins Margins information for the current child fragment. | 56 // @param child_margins Margins information for the current child fragment. |
60 // @param space Constraint space for the block. | |
61 // @return Position of the fragment in the parent's constraint space. | 57 // @return Position of the fragment in the parent's constraint space. |
62 NGLogicalOffset PositionFragment(const NGFragment& fragment, | 58 NGLogicalOffset PositionFragment(const NGFragment& fragment, |
63 const NGBoxStrut& child_margins, | 59 const NGBoxStrut& child_margins); |
64 const NGConstraintSpace& space); | |
65 | 60 |
66 // Calculates position of the float fragment that needs to be | 61 // Calculates position of the float fragment that needs to be |
67 // positioned relative to the current fragment that is being built. | 62 // positioned relative to the current fragment that is being built. |
68 // | 63 // |
69 // @param fragment Fragment that needs to be placed. | 64 // @param fragment Fragment that needs to be placed. |
70 // @param margins Margins information for the fragment. | 65 // @param margins Margins information for the fragment. |
71 // @return Position of the fragment in the parent's constraint space. | 66 // @return Position of the fragment in the parent's constraint space. |
72 NGLogicalOffset PositionFloatFragment(const NGFragment& fragment, | 67 NGLogicalOffset PositionFloatFragment(const NGFragment& fragment, |
73 const NGBoxStrut& margins); | 68 const NGBoxStrut& margins); |
74 | 69 |
75 // Updates block-{start|end} of the currently constructed fragment. | 70 // Updates block-{start|end} of the currently constructed fragment. |
76 // | 71 // |
77 // This method is supposed to be called on every child but it only updates | 72 // This method is supposed to be called on every child but it only updates |
78 // the block-start once (on the first non-zero height child fragment) and | 73 // the block-start once (on the first non-zero height child fragment) and |
79 // keeps updating block-end (on every non-zero height child). | 74 // keeps updating block-end (on every non-zero height child). |
80 void UpdateMarginStrut(const NGMarginStrut& from); | 75 void UpdateMarginStrut(const NGMarginStrut& from); |
81 | 76 |
82 bool LayoutCurrentChild(const NGConstraintSpace*); | |
83 | |
84 // Read-only Getters. | 77 // Read-only Getters. |
85 const ComputedStyle& Style() const { return *style_; } | 78 const ComputedStyle& Style() const { return *style_; } |
86 | 79 |
87 RefPtr<const ComputedStyle> style_; | |
88 Member<NGBox> first_child_; | |
89 | |
90 enum State { kStateInit, kStateChildLayout, kStateFinalize }; | 80 enum State { kStateInit, kStateChildLayout, kStateFinalize }; |
91 State state_; | 81 State state_; |
| 82 |
| 83 RefPtr<const ComputedStyle> style_; |
| 84 |
| 85 Member<NGBox> first_child_; |
| 86 Member<NGConstraintSpace> constraint_space_; |
92 Member<NGFragmentBuilder> builder_; | 87 Member<NGFragmentBuilder> builder_; |
93 Member<NGConstraintSpace> constraint_space_for_children_; | 88 Member<NGConstraintSpaceBuilder> space_builder_; |
| 89 Member<NGConstraintSpace> space_for_current_child_; |
94 Member<NGBox> current_child_; | 90 Member<NGBox> current_child_; |
| 91 |
95 NGBoxStrut border_and_padding_; | 92 NGBoxStrut border_and_padding_; |
96 LayoutUnit content_size_; | 93 LayoutUnit content_size_; |
97 LayoutUnit max_inline_size_; | 94 LayoutUnit max_inline_size_; |
98 // MarginStrut for the previous child. | 95 // MarginStrut for the previous child. |
99 NGMarginStrut prev_child_margin_strut_; | 96 NGMarginStrut prev_child_margin_strut_; |
100 // Whether the block-start was set for the currently built | 97 // Whether the block-start was set for the currently built |
101 // fragment's margin strut. | 98 // fragment's margin strut. |
102 bool is_fragment_margin_strut_block_start_updated_ : 1; | 99 bool is_fragment_margin_strut_block_start_updated_ : 1; |
103 }; | 100 }; |
104 | 101 |
105 } // namespace blink | 102 } // namespace blink |
106 | 103 |
107 #endif // NGBlockLayoutAlgorithm_h | 104 #endif // NGBlockLayoutAlgorithm_h |
OLD | NEW |