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
3 // found in the LICENSE file.
4 
5 #include "third_party/blink/renderer/core/style/style_path.h"
6 
7 #include <limits>
8 #include <memory>
9 #include <utility>
10 
11 #include "third_party/blink/renderer/core/css/css_path_value.h"
12 #include "third_party/blink/renderer/core/svg/svg_path_byte_stream.h"
13 #include "third_party/blink/renderer/core/svg/svg_path_utilities.h"
14 #include "third_party/blink/renderer/platform/graphics/path.h"
15 #include "third_party/blink/renderer/platform/heap/heap.h"
16 #include "third_party/blink/renderer/platform/transforms/affine_transform.h"
17 
18 namespace blink {
19 
StylePath(std::unique_ptr<SVGPathByteStream> path_byte_stream,WindRule wind_rule)20 StylePath::StylePath(std::unique_ptr<SVGPathByteStream> path_byte_stream,
21                      WindRule wind_rule)
22     : byte_stream_(std::move(path_byte_stream)),
23       path_length_(std::numeric_limits<float>::quiet_NaN()),
24       wind_rule_(wind_rule) {
25   DCHECK(byte_stream_);
26 }
27 
28 StylePath::~StylePath() = default;
29 
Create(std::unique_ptr<SVGPathByteStream> path_byte_stream,WindRule wind_rule)30 scoped_refptr<StylePath> StylePath::Create(
31     std::unique_ptr<SVGPathByteStream> path_byte_stream,
32     WindRule wind_rule) {
33   return base::AdoptRef(new StylePath(std::move(path_byte_stream), wind_rule));
34 }
35 
EmptyPath()36 const StylePath* StylePath::EmptyPath() {
37   DEFINE_STATIC_REF(StylePath, empty_path,
38                     StylePath::Create(std::make_unique<SVGPathByteStream>()));
39   return empty_path;
40 }
41 
GetPath() const42 const Path& StylePath::GetPath() const {
43   if (!path_) {
44     path_ = std::make_unique<Path>();
45     BuildPathFromByteStream(*byte_stream_, *path_);
46   }
47   return *path_;
48 }
49 
length() const50 float StylePath::length() const {
51   if (std::isnan(path_length_))
52     path_length_ = GetPath().length();
53   return path_length_;
54 }
55 
IsClosed() const56 bool StylePath::IsClosed() const {
57   return GetPath().IsClosed();
58 }
59 
ComputedCSSValue() const60 CSSValue* StylePath::ComputedCSSValue() const {
61   return MakeGarbageCollected<cssvalue::CSSPathValue>(
62       const_cast<StylePath*>(this), kTransformToAbsolute);
63 }
64 
operator ==(const BasicShape & o) const65 bool StylePath::operator==(const BasicShape& o) const {
66   if (!IsSameType(o))
67     return false;
68   const StylePath& other = To<StylePath>(o);
69   return wind_rule_ == other.wind_rule_ && *byte_stream_ == *other.byte_stream_;
70 }
71 
GetPath(Path & path,const FloatRect & offset_rect,float zoom)72 void StylePath::GetPath(Path& path, const FloatRect& offset_rect, float zoom) {
73   path = GetPath();
74   path.Transform(AffineTransform::Translation(offset_rect.X(), offset_rect.Y())
75                      .Scale(zoom));
76 }
77 
78 }  // namespace blink
79