Skip to content

Commit

Permalink
[LayoutNG] Add NGLineBoxFragment
Browse files Browse the repository at this point in the history
This patch adds NGLineBoxFragment, along with:
- NGPhysicalLineBoxFragment
- NGLineBoxFragmentBuilder
- NGTextFragmentBuilder for consistency

Part of code in NGLineBuilder was extracted to new classes, in
preparation of renaming it to NGLineLayoutAlgorithm.

NGFragment::Overflow was moved to NGBoxFragment, since neither text nor
linebox has layout overflow. All types of fragments have ink overflow,
but this is not included in this CL.

Some of NGFragmentBuilders could be shared as super classes. This is not
in this CL but to be discussed further.

This patch moves one step towards computing baseline position from
NGFragment, but its implementation is not included in this CL.

BUG=636993

Review-Url: https://codereview.chromium.org/2764753007
Cr-Commit-Position: refs/heads/master@{#459446}
  • Loading branch information
kojiishi authored and Commit bot committed Mar 24, 2017
1 parent 8f57f07 commit 9872e64
Show file tree
Hide file tree
Showing 26 changed files with 579 additions and 231 deletions.
10 changes: 10 additions & 0 deletions third_party/WebKit/Source/core/layout/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -361,8 +361,14 @@ blink_core_sources("layout") {
"ng/ng_layout_result.h",
"ng/ng_length_utils.cc",
"ng/ng_length_utils.h",
"ng/ng_line_box_fragment.cc",
"ng/ng_line_box_fragment.h",
"ng/ng_line_box_fragment_builder.cc",
"ng/ng_line_box_fragment_builder.h",
"ng/ng_line_builder.cc",
"ng/ng_line_builder.h",
"ng/ng_line_height_metrics.cc",
"ng/ng_line_height_metrics.h",
"ng/ng_macros.h",
"ng/ng_min_max_content_size.cc",
"ng/ng_min_max_content_size.h",
Expand All @@ -372,13 +378,17 @@ blink_core_sources("layout") {
"ng/ng_physical_box_fragment.h",
"ng/ng_physical_fragment.cc",
"ng/ng_physical_fragment.h",
"ng/ng_physical_line_box_fragment.cc",
"ng/ng_physical_line_box_fragment.h",
"ng/ng_physical_text_fragment.h",
"ng/ng_relative_utils.cc",
"ng/ng_relative_utils.h",
"ng/ng_space_utils.cc",
"ng/ng_space_utils.h",
"ng/ng_text_fragment.cc",
"ng/ng_text_fragment.h",
"ng/ng_text_fragment_builder.cc",
"ng/ng_text_fragment_builder.h",
"ng/ng_text_layout_algorithm.cc",
"ng/ng_text_layout_algorithm.h",
"ng/ng_writing_mode.cc",
Expand Down
12 changes: 12 additions & 0 deletions third_party/WebKit/Source/core/layout/ng/ng_box_fragment.cc
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,18 @@

namespace blink {

LayoutUnit NGBoxFragment::InlineOverflow() const {
return writing_mode_ == kHorizontalTopBottom
? toNGPhysicalBoxFragment(physical_fragment_)->WidthOverflow()
: toNGPhysicalBoxFragment(physical_fragment_)->HeightOverflow();
}

LayoutUnit NGBoxFragment::BlockOverflow() const {
return writing_mode_ == kHorizontalTopBottom
? toNGPhysicalBoxFragment(physical_fragment_)->HeightOverflow()
: toNGPhysicalBoxFragment(physical_fragment_)->WidthOverflow();
}

const WTF::Optional<NGLogicalOffset>& NGBoxFragment::BfcOffset() const {
WRITING_MODE_IGNORED(
"Accessing BFC offset is allowed here because writing"
Expand Down
4 changes: 4 additions & 0 deletions third_party/WebKit/Source/core/layout/ng/ng_box_fragment.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ class CORE_EXPORT NGBoxFragment final : public NGFragment {
const NGPhysicalBoxFragment* physical_fragment)
: NGFragment(writing_mode, physical_fragment) {}

// Returns the total size, including the contents outside of the border-box.
LayoutUnit InlineOverflow() const;
LayoutUnit BlockOverflow() const;

const WTF::Optional<NGLogicalOffset>& BfcOffset() const;

const NGMarginStrut& EndMarginStrut() const;
Expand Down
12 changes: 0 additions & 12 deletions third_party/WebKit/Source/core/layout/ng/ng_fragment.cc
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,6 @@ LayoutUnit NGFragment::BlockSize() const {
: physical_fragment_->Width();
}

LayoutUnit NGFragment::InlineOverflow() const {
return writing_mode_ == kHorizontalTopBottom
? physical_fragment_->WidthOverflow()
: physical_fragment_->HeightOverflow();
}

LayoutUnit NGFragment::BlockOverflow() const {
return writing_mode_ == kHorizontalTopBottom
? physical_fragment_->HeightOverflow()
: physical_fragment_->WidthOverflow();
}

LayoutUnit NGFragment::InlineOffset() const {
return writing_mode_ == kHorizontalTopBottom
? physical_fragment_->LeftOffset()
Expand Down
4 changes: 0 additions & 4 deletions third_party/WebKit/Source/core/layout/ng/ng_fragment.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,6 @@ class CORE_EXPORT NGFragment {
LayoutUnit InlineSize() const;
LayoutUnit BlockSize() const;

// Returns the total size, including the contents outside of the border-box.
LayoutUnit InlineOverflow() const;
LayoutUnit BlockOverflow() const;

// Returns the offset relative to the parent fragment's content-box.
LayoutUnit InlineOffset() const;
LayoutUnit BlockOffset() const;
Expand Down
15 changes: 0 additions & 15 deletions third_party/WebKit/Source/core/layout/ng/ng_fragment_builder.cc
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,6 @@ NGFragmentBuilder& NGFragmentBuilder::AddOutOfFlowDescendant(
}

RefPtr<NGLayoutResult> NGFragmentBuilder::ToBoxFragment() {
// TODO(layout-ng): Support text fragments
DCHECK_EQ(type_, NGPhysicalFragment::kFragmentBox);
DCHECK_EQ(offsets_.size(), children_.size());

Expand Down Expand Up @@ -198,18 +197,4 @@ RefPtr<NGLayoutResult> NGFragmentBuilder::ToBoxFragment() {
out_of_flow_positions_, unpositioned_floats_));
}

RefPtr<NGPhysicalTextFragment> NGFragmentBuilder::ToTextFragment(
unsigned index,
unsigned start_offset,
unsigned end_offset) {
DCHECK_EQ(type_, NGPhysicalFragment::kFragmentText);
DCHECK(children_.isEmpty());
DCHECK(offsets_.isEmpty());

return adoptRef(new NGPhysicalTextFragment(
node_->GetLayoutObject(), toNGInlineNode(node_), index, start_offset,
end_offset, size_.ConvertToPhysical(writing_mode_),
overflow_.ConvertToPhysical(writing_mode_)));
}

} // namespace blink
Original file line number Diff line number Diff line change
Expand Up @@ -99,9 +99,6 @@ class CORE_EXPORT NGFragmentBuilder final {

// Creates the fragment. Can only be called once.
RefPtr<NGLayoutResult> ToBoxFragment();
RefPtr<NGPhysicalTextFragment> ToTextFragment(unsigned index,
unsigned start_offset,
unsigned end_offset);

Vector<RefPtr<NGPhysicalFragment>>& MutableChildren() { return children_; }

Expand Down
10 changes: 7 additions & 3 deletions third_party/WebKit/Source/core/layout/ng/ng_inline_node_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#include "core/layout/ng/ng_fragment_builder.h"
#include "core/layout/ng/ng_line_builder.h"
#include "core/layout/ng/ng_physical_box_fragment.h"
#include "core/layout/ng/ng_physical_line_box_fragment.h"
#include "core/layout/ng/ng_physical_text_fragment.h"
#include "core/layout/ng/ng_text_fragment.h"
#include "core/layout/ng/ng_text_layout_algorithm.h"
Expand Down Expand Up @@ -98,9 +99,12 @@ class NGInlineNodeTest : public RenderingTest {
algorithm.LayoutInline(&line_builder);

RefPtr<NGLayoutResult> result = line_builder.CreateFragments();
for (const auto& child :
toNGPhysicalBoxFragment(result->PhysicalFragment().get())
->Children()) {
const NGPhysicalBoxFragment* container =
toNGPhysicalBoxFragment(result->PhysicalFragment().get());
EXPECT_EQ(container->Children().size(), 1u);
const NGPhysicalLineBoxFragment* line =
toNGPhysicalLineBoxFragment(container->Children()[0].get());
for (const auto& child : line->Children()) {
fragments_out->push_back(toNGPhysicalTextFragment(child.get()));
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
// Copyright 2017 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "core/layout/ng/ng_line_box_fragment.h"

namespace blink {} // namespace blink
30 changes: 30 additions & 0 deletions third_party/WebKit/Source/core/layout/ng/ng_line_box_fragment.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
// Copyright 2017 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef NGLineBoxFragment_h
#define NGLineBoxFragment_h

#include "core/CoreExport.h"
#include "core/layout/ng/ng_fragment.h"
#include "core/layout/ng/ng_physical_line_box_fragment.h"
#include "core/layout/ng/ng_writing_mode.h"

namespace blink {

class CORE_EXPORT NGLineBoxFragment final : public NGFragment {
public:
NGLineBoxFragment(NGWritingMode writing_mode,
const NGPhysicalLineBoxFragment* physical_fragment)
: NGFragment(writing_mode, physical_fragment) {}
};

DEFINE_TYPE_CASTS(NGLineBoxFragment,
NGFragment,
fragment,
fragment->Type() == NGPhysicalFragment::kFragmentLineBox,
fragment.Type() == NGPhysicalFragment::kFragmentLineBox);

} // namespace blink

#endif // NGLineBoxFragment_h
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
// Copyright 2017 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "core/layout/ng/ng_line_box_fragment_builder.h"

#include "core/layout/ng/geometry/ng_logical_size.h"
#include "core/layout/ng/ng_fragment.h"
#include "core/layout/ng/ng_inline_node.h"
#include "core/layout/ng/ng_physical_line_box_fragment.h"
#include "platform/heap/Handle.h"

namespace blink {

NGLineBoxFragmentBuilder::NGLineBoxFragmentBuilder(NGInlineNode* node)
: direction_(TextDirection::kLtr), node_(node) {}

NGLineBoxFragmentBuilder& NGLineBoxFragmentBuilder::SetDirection(
TextDirection direction) {
direction_ = direction;
return *this;
}

NGLineBoxFragmentBuilder& NGLineBoxFragmentBuilder::SetInlineSize(
LayoutUnit size) {
inline_size_ = size;
return *this;
}

NGLineBoxFragmentBuilder& NGLineBoxFragmentBuilder::AddChild(
RefPtr<NGPhysicalFragment> child,
const NGLogicalOffset& child_offset) {
children_.push_back(std::move(child));
offsets_.push_back(child_offset);

return *this;
}

void NGLineBoxFragmentBuilder::MoveChildrenInBlockDirection(LayoutUnit delta) {
for (auto& offset : offsets_)
offset.block_offset += delta;
}

void NGLineBoxFragmentBuilder::UniteMetrics(
const NGLineHeightMetrics& metrics) {
metrics_.Unite(metrics);
}

RefPtr<NGPhysicalLineBoxFragment>
NGLineBoxFragmentBuilder::ToLineBoxFragment() {
DCHECK_EQ(offsets_.size(), children_.size());

NGWritingMode writing_mode(
FromPlatformWritingMode(node_->BlockStyle()->getWritingMode()));
NGPhysicalSize physical_size =
NGLogicalSize(inline_size_, Metrics().LineHeight())
.ConvertToPhysical(writing_mode);

for (size_t i = 0; i < children_.size(); ++i) {
NGPhysicalFragment* child = children_[i].get();
child->SetOffset(offsets_[i].ConvertToPhysical(
writing_mode, direction_, physical_size, child->Size()));
}

// TODO(kojii): Implement BreakToken.
return adoptRef(new NGPhysicalLineBoxFragment(physical_size, children_,
metrics_, nullptr));
}

} // namespace blink
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
// Copyright 2017 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef NGLineBoxFragmentBuilder_h
#define NGLineBoxFragmentBuilder_h

#include "core/layout/ng/geometry/ng_logical_offset.h"
#include "core/layout/ng/ng_line_height_metrics.h"
#include "wtf/Allocator.h"

namespace blink {

class NGInlineNode;
class NGPhysicalFragment;
class NGPhysicalLineBoxFragment;

class CORE_EXPORT NGLineBoxFragmentBuilder final {
STACK_ALLOCATED();

public:
NGLineBoxFragmentBuilder(NGInlineNode*);

NGLineBoxFragmentBuilder& SetDirection(TextDirection);

NGLineBoxFragmentBuilder& SetInlineSize(LayoutUnit);

NGLineBoxFragmentBuilder& AddChild(RefPtr<NGPhysicalFragment>,
const NGLogicalOffset&);
void MoveChildrenInBlockDirection(LayoutUnit);

const Vector<RefPtr<NGPhysicalFragment>>& Children() const {
return children_;
}

void UniteMetrics(const NGLineHeightMetrics&);
const NGLineHeightMetrics& Metrics() const { return metrics_; }

// Creates the fragment. Can only be called once.
RefPtr<NGPhysicalLineBoxFragment> ToLineBoxFragment();

private:
TextDirection direction_;

Persistent<NGInlineNode> node_;

LayoutUnit inline_size_;

Vector<RefPtr<NGPhysicalFragment>> children_;
Vector<NGLogicalOffset> offsets_;

NGLineHeightMetrics metrics_;
};

} // namespace blink

#endif // NGLineBoxFragmentBuilder
Loading

0 comments on commit 9872e64

Please sign in to comment.