summaryrefslogtreecommitdiff
path: root/chromium/third_party/blink/renderer/core/layout/ng/inline/ng_text_fragment_builder.cc
blob: 0b85af665ca8074ab3659062a49ab5654c4ee0bc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
// 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 "third_party/blink/renderer/core/layout/ng/inline/ng_text_fragment_builder.h"

#include "third_party/blink/renderer/core/layout/layout_text_fragment.h"
#include "third_party/blink/renderer/core/layout/ng/inline/ng_inline_item_result.h"
#include "third_party/blink/renderer/core/layout/ng/inline/ng_inline_node.h"
#include "third_party/blink/renderer/core/layout/ng/inline/ng_line_height_metrics.h"
#include "third_party/blink/renderer/core/layout/ng/inline/ng_physical_text_fragment.h"
#include "third_party/blink/renderer/platform/fonts/shaping/shape_result_view.h"

namespace blink {

NGTextFragmentBuilder::NGTextFragmentBuilder(
    const NGPhysicalTextFragment& fragment)
    : NGFragmentBuilder(fragment),
      text_(fragment.text_),
      start_offset_(fragment.StartOffset()),
      end_offset_(fragment.EndOffset()),
      shape_result_(fragment.TextShapeResult()),
      text_type_(fragment.TextType()) {}

void NGTextFragmentBuilder::SetItem(
    NGPhysicalTextFragment::NGTextType text_type,
    const NGInlineItemsData& items_data,
    NGInlineItemResult* item_result,
    LayoutUnit line_height) {
  DCHECK_NE(text_type, NGPhysicalTextFragment::kGeneratedText)
      << "Please use SetText() instead.";
  DCHECK(item_result);
  DCHECK(item_result->item->Style());

  text_type_ = text_type;
  text_ = items_data.text_content;
  start_offset_ = item_result->start_offset;
  end_offset_ = item_result->end_offset;
  resolved_direction_ = item_result->item->Direction();
  SetStyle(item_result->item->Style(), item_result->item->StyleVariant());
  size_ = {item_result->inline_size, line_height};
  shape_result_ = std::move(item_result->shape_result);
  layout_object_ = item_result->item->GetLayoutObject();
}

void NGTextFragmentBuilder::SetText(
    LayoutObject* layout_object,
    const String& text,
    scoped_refptr<const ComputedStyle> style,
    bool is_ellipsis_style,
    scoped_refptr<const ShapeResultView> shape_result) {
  DCHECK(layout_object);
  DCHECK(style);
  DCHECK(shape_result);

  text_type_ = NGPhysicalTextFragment::kGeneratedText;
  text_ = text;
  start_offset_ = shape_result->StartIndex();
  end_offset_ = shape_result->EndIndex();
  resolved_direction_ = shape_result->Direction();
  SetStyle(style, is_ellipsis_style ? NGStyleVariant::kEllipsis
                                    : NGStyleVariant::kStandard);
  size_ = {shape_result->SnappedWidth(),
           NGLineHeightMetrics(*style).LineHeight()};
  shape_result_ = std::move(shape_result);
  layout_object_ = layout_object;
}

bool NGTextFragmentBuilder::IsGeneratedText() const {
  if (UNLIKELY(style_variant_ == NGStyleVariant::kEllipsis ||
               text_type_ == NGPhysicalTextFragment::kGeneratedText))
    return true;

  DCHECK(layout_object_);
  if (const auto* layout_text_fragment =
          ToLayoutTextFragmentOrNull(layout_object_)) {
    return !layout_text_fragment->AssociatedTextNode();
  }

  const Node* node = layout_object_->GetNode();
  return !node || node->IsPseudoElement();
}

scoped_refptr<const NGPhysicalTextFragment>
NGTextFragmentBuilder::ToTextFragment() {
  scoped_refptr<const NGPhysicalTextFragment> fragment =
      base::AdoptRef(new NGPhysicalTextFragment(this));
  return fragment;
}

}  // namespace blink