Flutter Engine
path_measure.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 "flutter/lib/ui/painting/path_measure.h"
6 
7 #include <cmath>
8 
9 #include "flutter/lib/ui/painting/matrix.h"
10 #include "flutter/lib/ui/ui_dart_state.h"
15 
16 using tonic::ToDart;
17 
18 namespace flutter {
19 
21 
22 static void PathMeasure_constructor(Dart_NativeArguments args) {
25 }
26 
27 IMPLEMENT_WRAPPERTYPEINFO(ui, PathMeasure);
28 
29 #define FOR_EACH_BINDING(V) \
30  V(PathMeasure, setPath) \
31  V(PathMeasure, getLength) \
32  V(PathMeasure, getPosTan) \
33  V(PathMeasure, getSegment) \
34  V(PathMeasure, isClosed) \
35  V(PathMeasure, nextContour)
36 
38 
39 void CanvasPathMeasure::RegisterNatives(tonic::DartLibraryNatives* natives) {
40  natives->Register(
41  {{"PathMeasure_constructor", PathMeasure_constructor, 3, true},
43 }
44 
46  bool forceClosed) {
48  fml::MakeRefCounted<CanvasPathMeasure>();
49  if (path) {
50  const SkPath skPath = path->path();
51  SkScalar resScale = 1;
52  pathMeasure->path_measure_ =
53  std::make_unique<SkContourMeasureIter>(skPath, forceClosed, resScale);
54  } else {
55  pathMeasure->path_measure_ = std::make_unique<SkContourMeasureIter>();
56  }
57  return pathMeasure;
58 }
59 
60 CanvasPathMeasure::CanvasPathMeasure() {}
61 
63 
65  const SkPath& skPath = path->path();
66  path_measure_->reset(skPath, isClosed);
67 }
68 
69 float CanvasPathMeasure::getLength(int contour_index) {
70  if (static_cast<std::vector<sk_sp<SkContourMeasure>>::size_type>(
71  contour_index) < measures_.size()) {
72  return measures_[contour_index]->length();
73  }
74  return -1;
75 }
76 
77 tonic::Float32List CanvasPathMeasure::getPosTan(int contour_index,
78  float distance) {
79  tonic::Float32List posTan(Dart_NewTypedData(Dart_TypedData_kFloat32, 5));
80  posTan[0] = 0; // dart code will check for this for failure
81  if (static_cast<std::vector<sk_sp<SkContourMeasure>>::size_type>(
82  contour_index) >= measures_.size()) {
83  return posTan;
84  }
85 
86  SkPoint pos;
87  SkVector tan;
88  bool success = measures_[contour_index]->getPosTan(distance, &pos, &tan);
89 
90  if (success) {
91  posTan[0] = 1; // dart code will check for this for success
92  posTan[1] = pos.x();
93  posTan[2] = pos.y();
94  posTan[3] = tan.x();
95  posTan[4] = tan.y();
96  }
97 
98  return posTan;
99 }
100 
101 void CanvasPathMeasure::getSegment(Dart_Handle path_handle,
102  int contour_index,
103  float start_d,
104  float stop_d,
105  bool start_with_move_to) {
106  if (static_cast<std::vector<sk_sp<SkContourMeasure>>::size_type>(
107  contour_index) >= measures_.size()) {
108  CanvasPath::Create(path_handle);
109  }
110  SkPath dst;
111  bool success = measures_[contour_index]->getSegment(start_d, stop_d, &dst,
112  start_with_move_to);
113  if (!success) {
114  CanvasPath::Create(path_handle);
115  } else {
116  CanvasPath::CreateFrom(path_handle, dst);
117  }
118 }
119 
120 bool CanvasPathMeasure::isClosed(int contour_index) {
121  if (static_cast<std::vector<sk_sp<SkContourMeasure>>::size_type>(
122  contour_index) < measures_.size()) {
123  return measures_[contour_index]->isClosed();
124  }
125  return false;
126 }
127 
129  auto measure = path_measure_->next();
130  if (measure) {
131  measures_.push_back(std::move(measure));
132  return true;
133  }
134  return false;
135 }
136 
137 } // namespace flutter
G_BEGIN_DECLS FlValue * args
DEF_SWITCHES_START snapshot asset path
Definition: switches.h:32
static fml::RefPtr< CanvasPath > Create(Dart_Handle path_handle)
Definition: path.h:30
CanvasPathMeasure PathMeasure
Definition: path_measure.cc:20
void DartCallConstructor(Sig func, Dart_NativeArguments args)
Definition: dart_args.h:218
void getSegment(Dart_Handle path_handle, int contour_index, float start_d, float stop_d, bool start_with_move_to)
#define DART_NATIVE_CALLBACK(CLASS, METHOD)
#define FOR_EACH_BINDING(V)
Definition: path_measure.cc:29
const SkContourMeasureIter & pathMeasure() const
Definition: path_measure.h:47
IMPLEMENT_WRAPPERTYPEINFO(ui, Scene)
const SkPath & path() const
Definition: path.h:111
static void PathMeasure_constructor(Dart_NativeArguments args)
Definition: path_measure.cc:22
float getLength(int contour_index)
Definition: path_measure.cc:69
static fml::RefPtr< CanvasPathMeasure > Create(const CanvasPath *path, bool forceClosed)
Definition: path_measure.cc:45
bool isClosed(int contour_index)
static void ThrowIfUIOperationsProhibited()
void setPath(const CanvasPath *path, bool isClosed)
Definition: path_measure.cc:64
tonic::Float32List getPosTan(int contour_index, float distance)
Definition: path_measure.cc:77
#define DART_REGISTER_NATIVE(CLASS, METHOD)
Dart_Handle ToDart(const T &object)
static fml::RefPtr< CanvasPath > CreateFrom(Dart_Handle path_handle, const SkPath &src)
Definition: path.h:36