| 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
5 #include "impeller/geometry/dashed_line_path_source.h"
6
7 namespace impeller {
8
9 DashedLinePathSource::DashedLinePathSource(Point p0,
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
15 DashedLinePathSource::~DashedLinePathSource() = default;
16
17 FillType DashedLinePathSource::GetFillType() const {
18 return FillType::kNonZero;
19 }
20
21 Rect DashedLinePathSource::GetBounds() const {
22 return Rect::MakeLTRB(p0_.x, p0_.y, p1_.x, p1_.y).GetPositive();
23 }
24
25 bool DashedLinePathSource::IsConvex() const {
26 return false;
27 }
28
29 void DashedLinePathSource::Dispatch(PathReceiver& receiver) const {
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
impeller::DashedLinePathSource::DashedLinePathSource
DashedLinePathSource(Point p0, Point p1, Scalar on_length, Scalar off_length)
Definition: dashed_line_path_source.cc:9
impeller::DashedLinePathSource::IsConvex
bool IsConvex() const override
Definition: dashed_line_path_source.cc:25
impeller::DashedLinePathSource::GetFillType
FillType GetFillType() const override
Definition: dashed_line_path_source.cc:17
impeller::DashedLinePathSource::GetBounds
Rect GetBounds() const override
Definition: dashed_line_path_source.cc:21
impeller::DashedLinePathSource::~DashedLinePathSource
~DashedLinePathSource()
impeller::DashedLinePathSource::Dispatch
void Dispatch(PathReceiver &receiver) const override
Definition: dashed_line_path_source.cc:29
impeller::PathReceiver
Collection of functions to receive path segments from the underlying path representation via the DlPa...
Definition: path_source.h:42
impeller::PathReceiver::LineTo
virtual void LineTo(const Point &p2)=0
impeller::PathReceiver::MoveTo
virtual void MoveTo(const Point &p2, bool will_be_closed)=0
dashed_line_path_source.h
impeller
Definition: allocation.cc:12
impeller::Scalar
float Scalar
Definition: scalar.h:19
impeller::FillType
FillType
Definition: path_source.h:13
impeller::FillType::kNonZero
@ kNonZero
impeller::TPoint< Scalar >
impeller::TPoint::GetDistance
constexpr Type GetDistance(const TPoint &p) const
Definition: point.h:200
impeller::TPoint::y
Type y
Definition: point.h:31
impeller::TPoint::x
Type x
Definition: point.h:30
impeller::TRect< Scalar >
impeller::TRect::GetPositive
constexpr TRect GetPositive() const
Get a version of this rectangle that has a non-negative size.
Definition: rect.h:402
impeller::TRect< Scalar >::MakeLTRB
constexpr static TRect MakeLTRB(Type left, Type top, Type right, Type bottom)
Definition: rect.h:129