summaryrefslogtreecommitdiff
path: root/chromium/third_party/blink/renderer/core/paint/ng/ng_fragment_painter.cc
blob: dd8d402df61e459eabdcbb8f08c07b3864a0872b (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
// 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/paint/ng/ng_fragment_painter.h"

#include "third_party/blink/renderer/core/layout/ng/ng_outline_utils.h"
#include "third_party/blink/renderer/core/paint/paint_info.h"
#include "third_party/blink/renderer/core/style/computed_style.h"
#include "third_party/blink/renderer/platform/graphics/graphics_context_state_saver.h"
#include "third_party/blink/renderer/platform/graphics/paint/drawing_recorder.h"

namespace blink {

void NGFragmentPainter::PaintOutline(const PaintInfo& paint_info,
                                     const PhysicalOffset& paint_offset,
                                     const ComputedStyle& style_to_use) {
  const NGPhysicalBoxFragment& fragment = PhysicalFragment();
  DCHECK(NGOutlineUtils::HasPaintedOutline(style_to_use, fragment.GetNode()));
  Vector<PhysicalRect> outline_rects;
  fragment.AddSelfOutlineRects(
      paint_offset, style_to_use.OutlineRectsShouldIncludeBlockVisualOverflow(),
      &outline_rects);

  if (outline_rects.IsEmpty())
    return;

  const DisplayItemClient& display_item_client = GetDisplayItemClient();
  if (DrawingRecorder::UseCachedDrawingIfPossible(
          paint_info.context, display_item_client, paint_info.phase))
    return;

  IntRect visual_rect =
      PixelSnappedIntRect(UnionRectEvenIfEmpty(outline_rects));
  visual_rect.Inflate(style_to_use.OutlineOutsetExtent());
  DrawingRecorder recorder(paint_info.context, display_item_client,
                           paint_info.phase, visual_rect);
  PaintOutlineRects(paint_info, outline_rects, style_to_use);
}

void NGFragmentPainter::AddURLRectIfNeeded(const PaintInfo& paint_info,
                                           const PhysicalOffset& paint_offset) {
  DCHECK(paint_info.ShouldAddUrlMetadata());

  // TODO(layout-dev): Should use break token when NG has its own tree building.
  const NGPhysicalBoxFragment& fragment = PhysicalFragment();
  if (fragment.GetLayoutObject()->IsElementContinuation() ||
      fragment.Style().Visibility() != EVisibility::kVisible)
    return;

  Node* node = fragment.GetNode();
  if (!node || !node->IsLink())
    return;

  KURL url = To<Element>(node)->HrefURL();
  if (!url.IsValid())
    return;

  auto outline_rects = fragment.GetLayoutObject()->OutlineRects(
      paint_offset, NGOutlineType::kIncludeBlockVisualOverflow);
  IntRect rect = PixelSnappedIntRect(UnionRect(outline_rects));
  if (rect.IsEmpty())
    return;

  const DisplayItemClient& display_item_client = GetDisplayItemClient();
  if (DrawingRecorder::UseCachedDrawingIfPossible(
          paint_info.context, display_item_client,
          DisplayItem::kPrintedContentPDFURLRect))
    return;

  DrawingRecorder recorder(paint_info.context, display_item_client,
                           DisplayItem::kPrintedContentPDFURLRect);

  Document& document = fragment.GetLayoutObject()->GetDocument();
  if (url.HasFragmentIdentifier() &&
      EqualIgnoringFragmentIdentifier(url, document.BaseURL())) {
    String fragment_name = url.FragmentIdentifier();
    if (document.FindAnchor(fragment_name))
      paint_info.context.SetURLFragmentForRect(fragment_name, rect);
    return;
  }
  paint_info.context.SetURLForRect(url, rect);
}

}  // namespace blink