Skip to content

Commit c523e15

Browse files
committed
Implement SkPoint class & SkPathMeasure
1 parent f832549 commit c523e15

File tree

3 files changed

+138
-3
lines changed

3 files changed

+138
-3
lines changed

modules/pathkit/chaining.js

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,43 @@
195195
}
196196
return null;
197197
};
198+
199+
PathKit.SkPoint.prototype.set = function(x, y) {
200+
this._set(x, y);
201+
return this;
202+
};
203+
204+
PathKit.SkPoint.prototype.normalize = function() {
205+
this._normalize();
206+
return this;
207+
}
208+
209+
PathKit.SkPoint.prototype.scale = function(s) {
210+
this._scale(s);
211+
return this;
212+
}
213+
214+
PathKit.SkPoint.prototype.offset = function(dx, dy) {
215+
this._offset(dx, dy);
216+
return this;
217+
}
218+
219+
PathKit.SkPoint.prototype.negate = function() {
220+
this._negate();
221+
return this;
222+
}
223+
224+
PathKit.SkPathMeasure.prototype.setPath = function(path, forceClosed) {
225+
this._setPath(path, !!forceClosed);
226+
return this;
227+
};
228+
229+
PathKit.SkPathMeasure.prototype.getPosTan = function(distance, position, tangent) {
230+
if(this._getPosTan(distance, position, tangent)){
231+
return this;
232+
}
233+
return null;
234+
};
198235
};
199236

200237
}(Module)); // When this file is loaded in, the high level object is "Module";

modules/pathkit/externs.js

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,25 @@ var PathKit = {
7070
MITER: {},
7171
ROUND: {},
7272
BEVEL: {},
73+
},
74+
75+
SkPoint: {
76+
_set: function(x, y) {},
77+
_normalize: function() {},
78+
_scale: function(s) {},
79+
_offset: function(dx, dy) {},
80+
_negate: function() {},
81+
},
82+
83+
SkPathMeasure:{
84+
_setPath: function(path, forceClosed) {},
85+
_getPosTan: function(distance, position, tangent) {},
86+
},
87+
88+
MatrixFlag: {
89+
GET_POSITION: {},
90+
GET_TANGENT: {},
91+
GET_POS_AND_TAN: {},
7392
}
7493
};
7594

@@ -112,3 +131,12 @@ PathKit.SkPath.prototype.simplify = function() {};
112131
PathKit.SkPath.prototype.stroke = function(opts) {};
113132
PathKit.SkPath.prototype.transform = function() {};
114133
PathKit.SkPath.prototype.trim = function(startT, stopT, isComplement) {};
134+
135+
PathKit.SkPathMeasure.prototype.setPath = function() {};
136+
PathKit.SkPathMeasure.prototype.getPosTan = function() {};
137+
138+
PathKit.SkPoint.prototype.set = function(x, y) {};
139+
PathKit.SkPoint.prototype.normalize = function() {};
140+
PathKit.SkPoint.prototype.scale = function(s) {};
141+
PathKit.SkPoint.prototype.offset = function(dx, dy) {};
142+
PathKit.SkPoint.prototype.negate = function() {};

modules/pathkit/pathkit_wasm_bindings.cpp

Lines changed: 73 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
#include "include/core/SkRect.h"
1515
#include "include/core/SkString.h"
1616
#include "include/core/SkStrokeRec.h"
17+
#include "include/core/SkPathMeasure.h"
1718
#include "include/effects/SkDashPathEffect.h"
1819
#include "include/effects/SkTrimPathEffect.h"
1920
#include "include/pathops/SkPathOps.h"
@@ -456,6 +457,41 @@ void ApplyTransform(SkPath& orig,
456457
orig.transform(m);
457458
}
458459

460+
//========================================================================================
461+
// SkPoint things
462+
//========================================================================================
463+
void ApplySet(SkPoint& p, SkScalar x, SkScalar y) {
464+
p.set(x, y);
465+
}
466+
467+
void ApplyNormalize(SkPoint& p) {
468+
p.normalize();
469+
}
470+
471+
void ApplyScale(SkPoint& p, SkScalar scale) {
472+
p.scale(scale);
473+
}
474+
475+
void ApplyOffset(SkPoint& p, SkScalar dx, SkScalar dy) {
476+
p.offset(dx, dy);
477+
}
478+
479+
void ApplyNegate(SkPoint& p) {
480+
p.negate();
481+
}
482+
483+
//========================================================================================
484+
// SkPathMeasure things
485+
//========================================================================================
486+
487+
void ApplySetPath(SkPathMeasure& pm, const SkPath& path, bool forceClosed) {
488+
pm.setPath(&path, forceClosed);
489+
}
490+
491+
bool EMSCRIPTEN_KEEPALIVE GetPosTan(SkPathMeasure& pm, SkScalar distance, SkPoint *position, SkPoint *tangent) {
492+
return pm.getPosTan(distance, position, tangent);
493+
}
494+
459495
//========================================================================================
460496
// Testing things
461497
//========================================================================================
@@ -633,9 +669,25 @@ EMSCRIPTEN_BINDINGS(skia) {
633669
.element(&SimpleMatrix::pers1)
634670
.element(&SimpleMatrix::pers2);
635671

636-
value_array<SkPoint>("SkPoint")
637-
.element(&SkPoint::fX)
638-
.element(&SkPoint::fY);
672+
class_<SkPoint>("SkPoint")
673+
.constructor<>()
674+
.function("_set", &ApplySet)
675+
.function("_normalize", &ApplyNormalize)
676+
.function("_scale", &ApplyScale)
677+
.function("_offset", &ApplyOffset)
678+
.function("_negate", &ApplyNegate)
679+
680+
.function("length", &SkPoint::length)
681+
.function("dot", &SkPoint::dot)
682+
.function("cross", &SkPoint::cross)
683+
.property("x", &SkPoint::fX)
684+
.property("y", &SkPoint::fY)
685+
686+
// Static methods
687+
.class_function("Distance", &SkPoint::Distance)
688+
.class_function("DotProduct", &SkPoint::DotProduct)
689+
.class_function("CrossProduct", &SkPoint::CrossProduct);
690+
639691

640692
// Not intended for external clients to call directly.
641693
// See helper.js for the client-facing implementation.
@@ -645,6 +697,24 @@ EMSCRIPTEN_BINDINGS(skia) {
645697
.function("computeYFromX", &SkCubicMap::computeYFromX)
646698
.function("computePtFromT", &SkCubicMap::computeFromT);
647699

700+
class_<SkPathMeasure>("SkPathMeasure")
701+
.constructor<>()
702+
.constructor<const SkPath&, bool>()
703+
.constructor<const SkPath&, bool, SkScalar>()
704+
705+
.function("_setPath", &ApplySetPath)
706+
.function("_getPosTan", &GetPosTan, allow_raw_pointer<arg<3>>(), allow_raw_pointer<arg<4>>())
707+
708+
.function("getLength", &SkPathMeasure::getLength)
709+
.function("getSegment", &SkPathMeasure::getSegment, allow_raw_pointer<arg<3>>())
710+
.function("isClosed", &SkPathMeasure::isClosed)
711+
.function("nextContour", &SkPathMeasure::nextContour)
712+
;
713+
714+
enum_<SkPathMeasure::MatrixFlags>("MatrixFlags")
715+
.value("GET_POSITION", SkPathMeasure::kGetPosition_MatrixFlag)
716+
.value("GET_TANGENT", SkPathMeasure::kGetTangent_MatrixFlag)
717+
.value("GET_POS_AND_TAN", SkPathMeasure::kGetPosAndTan_MatrixFlag);
648718

649719
// Test Utils
650720
function("SkBits2FloatUnsigned", &SkBits2FloatUnsigned);

0 commit comments

Comments
 (0)