Flutter Impeller
dashed_line_path_source.cc
Go to the documentation of this file.
1 // Copyright 2013 The Flutter 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 
6 
7 namespace impeller {
8 
10  Point p1,
11  Scalar on_length,
12  Scalar off_length)
13  : p0_(p0), p1_(p1), on_length_(on_length), off_length_(off_length) {}
14 
16 
18  return FillType::kNonZero;
19 }
20 
22  return Rect::MakeLTRB(p0_.x, p0_.y, p1_.x, p1_.y).GetPositive();
23 }
24 
26  return false;
27 }
28 
30  // Exceptional conditions:
31  // - length is non-positive - result will draw only a "dot"
32  // - off_length is non-positive - no gaps, result is a solid line
33  // - on_length is negative - invalid dashing
34  // Note that a 0 length "on" dash will draw "dot"s every "off" distance
35  // apart so we still generate the dashing for that case.
36  //
37  // Note that Canvas will detect these conditions and use its own DrawLine
38  // method directly for performance reasons for a single line, but in case
39  // someone uses this PathSource with these exceptional cases, we degenerate
40  // gracefully into a single line segment path description below.
41  Scalar length = p0_.GetDistance(p1_);
42  if (length > 0.0f && on_length_ >= 0.0f && off_length_ > 0.0f) {
43  Point delta = (p1_ - p0_) / length; // length > 0 already verified
44 
45  Scalar consumed = 0.0f;
46  while (consumed < length) {
47  receiver.MoveTo(p0_ + delta * consumed, false);
48 
49  Scalar dash_end = consumed + on_length_;
50  if (dash_end < length) {
51  receiver.LineTo(p0_ + delta * dash_end);
52  } else {
53  receiver.LineTo(p1_);
54  // Should happen anyway due to the math, but let's make it explicit
55  // in case of bit errors. We're done with this line.
56  break;
57  }
58 
59  consumed = dash_end + off_length_;
60  }
61  } else {
62  receiver.MoveTo(p0_, false);
63  receiver.LineTo(p1_);
64  }
65 }
66 
67 } // namespace impeller
DashedLinePathSource(Point p0, Point p1, Scalar on_length, Scalar off_length)
FillType GetFillType() const override
void Dispatch(PathReceiver &receiver) const override
Collection of functions to receive path segments from the underlying path representation via the DlPa...
Definition: path_source.h:42
virtual void LineTo(const Point &p2)=0
virtual void MoveTo(const Point &p2, bool will_be_closed)=0
float Scalar
Definition: scalar.h:19
constexpr Type GetDistance(const TPoint &p) const
Definition: point.h:200
constexpr TRect GetPositive() const
Get a version of this rectangle that has a non-negative size.
Definition: rect.h:402
constexpr static TRect MakeLTRB(Type left, Type top, Type right, Type bottom)
Definition: rect.h:129