summaryrefslogtreecommitdiff
path: root/chromium/third_party/blink/renderer/core/layout/svg/layout_svg_path.cc
blob: 50755dfa2676d3ce743f41dee4e5d65cd1c01e44 (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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
/*
 * Copyright (C) 2004, 2005, 2007 Nikolas Zimmermann <zimmermann@kde.org>
 * Copyright (C) 2004, 2005, 2008 Rob Buis <buis@kde.org>
 * Copyright (C) 2005, 2007 Eric Seidel <eric@webkit.org>
 * Copyright (C) 2009 Google, Inc.
 * Copyright (C) 2009 Dirk Schulze <krit@webkit.org>
 * Copyright (C) Research In Motion Limited 2010. All rights reserved.
 * Copyright (C) 2009 Jeff Schiller <codedread@gmail.com>
 * Copyright (C) 2011 Renata Hodovan <reni@webkit.org>
 * Copyright (C) 2011 University of Szeged
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public License
 * along with this library; see the file COPYING.LIB.  If not, write to
 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 * Boston, MA 02110-1301, USA.
 */

#include "third_party/blink/renderer/core/layout/svg/layout_svg_path.h"

#include "third_party/blink/renderer/core/layout/svg/layout_svg_resource_marker.h"
#include "third_party/blink/renderer/core/layout/svg/svg_resources.h"
#include "third_party/blink/renderer/core/svg/svg_geometry_element.h"

namespace blink {

namespace {

bool SupportsMarkers(const SVGGeometryElement& element) {
  return element.HasTagName(svg_names::kLineTag) ||
         element.HasTagName(svg_names::kPathTag) ||
         element.HasTagName(svg_names::kPolygonTag) ||
         element.HasTagName(svg_names::kPolylineTag);
}

}  // namespace

LayoutSVGPath::LayoutSVGPath(SVGGeometryElement* node)
    // <line> elements have no joins and thus needn't care about miters.
    : LayoutSVGShape(node, IsA<SVGLineElement>(node) ? kNoMiters : kComplex) {
  DCHECK(SupportsMarkers(*node));
}

LayoutSVGPath::~LayoutSVGPath() = default;

void LayoutSVGPath::StyleDidChange(StyleDifference diff,
                                   const ComputedStyle* old_style) {
  NOT_DESTROYED();
  LayoutSVGShape::StyleDidChange(diff, old_style);
  SVGResources::UpdateMarkers(*GetElement(), old_style, StyleRef());
}

void LayoutSVGPath::WillBeDestroyed() {
  NOT_DESTROYED();
  SVGResources::ClearMarkers(*GetElement(), Style());
  LayoutSVGShape::WillBeDestroyed();
}

void LayoutSVGPath::UpdateShapeFromElement() {
  NOT_DESTROYED();
  LayoutSVGShape::UpdateShapeFromElement();
  UpdateMarkers();
}

const StylePath* LayoutSVGPath::GetStylePath() const {
  NOT_DESTROYED();
  if (!IsA<SVGPathElement>(*GetElement()))
    return nullptr;
  return StyleRef().D();
}

void LayoutSVGPath::UpdateMarkers() {
  NOT_DESTROYED();
  marker_positions_.clear();

  const ComputedStyle& style = StyleRef();
  if (!style.HasMarkers())
    return;
  SVGElementResourceClient* client = SVGResources::GetClient(*this);
  if (!client)
    return;
  auto* marker_start = GetSVGResourceAsType<LayoutSVGResourceMarker>(
      *client, style.MarkerStartResource());
  auto* marker_mid = GetSVGResourceAsType<LayoutSVGResourceMarker>(
      *client, style.MarkerMidResource());
  auto* marker_end = GetSVGResourceAsType<LayoutSVGResourceMarker>(
      *client, style.MarkerEndResource());
  if (!(marker_start || marker_mid || marker_end))
    return;

  SVGMarkerDataBuilder builder(marker_positions_);
  if (const StylePath* style_path = GetStylePath())
    builder.Build(style_path->ByteStream());
  else
    builder.Build(GetPath());

  if (marker_positions_.IsEmpty())
    return;

  const float stroke_width = StrokeWidthForMarkerUnits();
  FloatRect boundaries;
  for (const auto& position : marker_positions_) {
    if (LayoutSVGResourceMarker* marker =
            position.SelectMarker(marker_start, marker_mid, marker_end)) {
      boundaries.Unite(marker->MarkerBoundaries(
          marker->MarkerTransformation(position, stroke_width)));
    }
  }

  stroke_bounding_box_.Unite(boundaries);
}

}  // namespace blink