Added SkPathMeasure to the C API
authorMatthew Leibowitz <mattleibow@live.com>
Sat, 15 Oct 2016 01:37:36 +0000 (03:37 +0200)
committerMatthew Leibowitz <mattleibow@live.com>
Sat, 15 Oct 2016 01:37:36 +0000 (03:37 +0200)
include/c/sk_path.h
include/c/sk_types.h
src/c/sk_enums.cpp
src/c/sk_path.cpp
src/c/sk_types_priv.h

index 04aee9f..6ed8073 100644 (file)
@@ -241,6 +241,26 @@ SK_API bool sk_opbuilder_resolve(sk_opbuilder_t* builder, sk_path_t* result);
 
 SK_API int sk_path_convert_conic_to_quads(const sk_point_t* p0, const sk_point_t* p1, const sk_point_t* p2, float w, sk_point_t* pts, int pow2);
 
+SK_API sk_pathmeasure_t* sk_pathmeasure_new();
+
+SK_API sk_pathmeasure_t* sk_pathmeasure_new_with_path(const sk_path_t* path, bool forceClosed, float resScale);
+
+SK_API void sk_pathmeasure_destroy(sk_pathmeasure_t* pathMeasure);
+
+SK_API void sk_pathmeasure_set_path(sk_pathmeasure_t* pathMeasure, const sk_path_t* path, bool forceClosed);
+
+SK_API float sk_pathmeasure_get_length(sk_pathmeasure_t* pathMeasure);
+
+SK_API bool sk_pathmeasure_get_pos_tan(sk_pathmeasure_t* pathMeasure, float distance, sk_point_t* position, sk_vector_t* tangent);
+
+SK_API bool sk_pathmeasure_get_matrix(sk_pathmeasure_t* pathMeasure, float distance, sk_matrix_t* matrix, sk_pathmeasure_matrixflags_t flags);
+
+SK_API bool sk_pathmeasure_get_segment(sk_pathmeasure_t* pathMeasure, float start, float stop, sk_path_t* dst, bool startWithMoveTo);
+
+SK_API bool sk_pathmeasure_is_closed(sk_pathmeasure_t* pathMeasure);
+
+SK_API bool sk_pathmeasure_next_contour(sk_pathmeasure_t* pathMeasure);
+
 SK_C_PLUS_PLUS_END_GUARD
 
 #endif
index 358b205..617a002 100644 (file)
@@ -105,6 +105,8 @@ typedef struct {
     float   y;
 } sk_point_t;
 
+typedef sk_point_t sk_vector_t;
+
 typedef struct {
     int32_t left;
     int32_t top;
@@ -688,6 +690,14 @@ typedef struct {
     const sk_irect_t* fBounds;
 } sk_lattice_t;
 
+typedef struct sk_pathmeasure_t sk_pathmeasure_t;
+
+typedef enum {
+    GET_POSITION_SK_PATHMEASURE_MATRIXFLAGS = 0x01,
+    GET_TANGENT_SK_PATHMEASURE_MATRIXFLAGS = 0x02,
+    GET_POS_AND_TAN_SK_PATHMEASURE_MATRIXFLAGS = GET_POSITION_SK_PATHMEASURE_MATRIXFLAGS | GET_TANGENT_SK_PATHMEASURE_MATRIXFLAGS,
+} sk_pathmeasure_matrixflags_t;
+
 SK_C_PLUS_PLUS_END_GUARD
 
 #endif
index d80fa7e..ac7342b 100644 (file)
@@ -338,4 +338,9 @@ static_assert ((int)SkPath::Convexity::kConcave_Convexity   == (int)CONCAVE_SK_P
 static_assert ((int)0                                              == (int)DEFAULT_SK_LATTICE_FLAGS,       ASSERT_MSG(SkCanvas::Lattice::Flags, sk_lattice_flags_t));
 static_assert ((int)SkCanvas::Lattice::Flags::kTransparent_Flags   == (int)TRANSPARENT_SK_LATTICE_FLAGS,   ASSERT_MSG(SkCanvas::Lattice::Flags, sk_lattice_flags_t));
 
+// sk_pathmeasure_matrixflags_t
+static_assert ((int)SkPathMeasure::MatrixFlags::kGetPosition_MatrixFlag    == (int)GET_POSITION_SK_PATHMEASURE_MATRIXFLAGS,      ASSERT_MSG(SkPathMeasure::MatrixFlags, sk_pathmeasure_matrixflags_t));
+static_assert ((int)SkPathMeasure::MatrixFlags::kGetTangent_MatrixFlag     == (int)GET_TANGENT_SK_PATHMEASURE_MATRIXFLAGS,       ASSERT_MSG(SkPathMeasure::MatrixFlags, sk_pathmeasure_matrixflags_t));
+static_assert ((int)SkPathMeasure::MatrixFlags::kGetPosAndTan_MatrixFlag   == (int)GET_POS_AND_TAN_SK_PATHMEASURE_MATRIXFLAGS,   ASSERT_MSG(SkPathMeasure::MatrixFlags, sk_pathmeasure_matrixflags_t));
+
 #endif
index 0bbf406..9fc6558 100644 (file)
@@ -6,6 +6,7 @@
  */
 
 #include "SkPath.h"
+#include "SkPathMeasure.h"
 #include "SkPathOps.h"
 #include "SkParsePath.h"
 
@@ -256,11 +257,11 @@ void sk_path_set_convexity (sk_path_t* cpath, sk_path_convexity_t convexity) {
     AsPath(cpath)->setConvexity((SkPath::Convexity)convexity);
 }
 
-SK_API bool sk_path_parse_svg_string (sk_path_t* cpath, const char* str) {
+bool sk_path_parse_svg_string (sk_path_t* cpath, const char* str) {
     return SkParsePath::FromSVGString(str, AsPath(cpath));
 }
 
-SK_API void sk_path_to_svg_string (const sk_path_t* cpath, sk_string_t* str) {
+void sk_path_to_svg_string (const sk_path_t* cpath, sk_string_t* str) {
     SkParsePath::ToSVGString(AsPath(*cpath), AsString(str));
 }
 
@@ -303,3 +304,46 @@ bool sk_opbuilder_resolve(sk_opbuilder_t* builder, sk_path_t* result) {
 int sk_path_convert_conic_to_quads(const sk_point_t* p0, const sk_point_t* p1, const sk_point_t* p2, float w, sk_point_t* pts, int pow2) {
     return SkPath::ConvertConicToQuads(AsPoint(*p0), AsPoint(*p1), AsPoint(*p2), w, AsPoint(pts), pow2);
 }
+
+sk_pathmeasure_t* sk_pathmeasure_new() {
+    return ToPathMeasure(new SkPathMeasure());
+}
+
+sk_pathmeasure_t* sk_pathmeasure_new_with_path(const sk_path_t* path, bool forceClosed, float resScale) {
+    return ToPathMeasure(new SkPathMeasure(AsPath(*path), forceClosed, resScale));
+}
+
+void sk_pathmeasure_destroy(sk_pathmeasure_t* pathMeasure) {
+    delete AsPathMeasure(pathMeasure);
+}
+
+void sk_pathmeasure_set_path(sk_pathmeasure_t* pathMeasure, const sk_path_t* path, bool forceClosed) {
+    AsPathMeasure(pathMeasure)->setPath(AsPath(path), forceClosed);
+}
+
+float sk_pathmeasure_get_length(sk_pathmeasure_t* pathMeasure) {
+    return AsPathMeasure(pathMeasure)->getLength();
+}
+
+bool sk_pathmeasure_get_pos_tan(sk_pathmeasure_t* pathMeasure, float distance, sk_point_t* position, sk_vector_t* tangent) {
+    return AsPathMeasure(pathMeasure)->getPosTan(distance, AsPoint(position), AsPoint(tangent));
+}
+
+bool sk_pathmeasure_get_matrix(sk_pathmeasure_t* pathMeasure, float distance, sk_matrix_t* matrix, sk_pathmeasure_matrixflags_t flags) {
+    SkMatrix skmatrix;
+    bool result = AsPathMeasure(pathMeasure)->getMatrix(distance, &skmatrix, (SkPathMeasure::MatrixFlags)flags);
+    from_sk(&skmatrix, matrix);
+    return result;
+}
+
+bool sk_pathmeasure_get_segment(sk_pathmeasure_t* pathMeasure, float start, float stop, sk_path_t* dst, bool startWithMoveTo) {
+    return AsPathMeasure(pathMeasure)->getSegment(start, stop, AsPath(dst), startWithMoveTo);
+}
+
+bool sk_pathmeasure_is_closed(sk_pathmeasure_t* pathMeasure) {
+    return AsPathMeasure(pathMeasure)->isClosed();
+}
+
+bool sk_pathmeasure_next_contour(sk_pathmeasure_t* pathMeasure) {
+    return AsPathMeasure(pathMeasure)->nextContour();
+}
index b9bb8aa..fc7e1b2 100644 (file)
@@ -13,6 +13,7 @@
 #include "SkDocument.h"
 #include "SkPaint.h"
 #include "SkPath.h"
+#include "SkPathMeasure.h"
 #include "SkCodec.h"
 #include "SkPicture.h"
 #include "SkPictureRecorder.h"
@@ -124,6 +125,10 @@ static inline SkPath* AsPath(sk_path_t* cpath) {
     return reinterpret_cast<SkPath*>(cpath);
 }
 
+static inline const SkPath* AsPath(const sk_path_t* cpath) {
+    return reinterpret_cast<const SkPath*>(cpath);
+}
+
 static inline const SkImage* AsImage(const sk_image_t* cimage) {
     return reinterpret_cast<const SkImage*>(cimage);
 }
@@ -504,6 +509,14 @@ static inline const SkCanvas::Lattice& AsLattice(const sk_lattice_t& p) {
     return reinterpret_cast<const SkCanvas::Lattice&>(p);
 }
 
+static inline sk_pathmeasure_t* ToPathMeasure(SkPathMeasure* p) {
+    return reinterpret_cast<sk_pathmeasure_t*>(p);
+}
+
+static inline SkPathMeasure* AsPathMeasure(sk_pathmeasure_t* p) {
+    return reinterpret_cast<SkPathMeasure*>(p);
+}
+
 static inline void from_c(const sk_matrix_t* cmatrix, SkMatrix* matrix) {
     matrix->setAll(
         cmatrix->mat[0], cmatrix->mat[1], cmatrix->mat[2],