I’d like to get the associated points of a shape’s bezierPath. My hope was to do something like the following but I am greeted by Acorn crashing with invalid memory address exceptions.
Is there a way to allocate and pass an NSPointArray for the associatedPoints?
Thanks!
var path = [selectedShape bezierPath];
var count = [path elementCount];
for (var i=0; i<count; i++) {
try {
var points = [];
points[0] = NSMakePoint(0,0);
points[1] = NSMakePoint(0,0);
points[2] = NSMakePoint(0,0); // I think 3 points is enough for all types
var type = [path elementAtIndex:i associatedPoints:points]
// do something with type and points
}
catch (err) {
}
}
EDIT:
I can get a memory buffer that (I think) works with elementAtIndex but now I need to figure out how to get an NSPoint out of an NSMutableData. I’m not a Cocoa/Mac expert by any means so this might be obvious.
var data = [[NSMutableData alloc] init]
[data setLength:(4*2*8)]; // four pairs of doubles
var points = [data mutableBytes];
var type = [path elementAtIndex:i associatedPoints:points]
This is going to be pretty tough. I think it might be best for me to update Acorn with some better accessors for it. But maybe you want to give the following a try.
function main(image, doc, layer) {
var selectedShape = layer.selectedGraphics()[0]
var path = selectedShape.bezierPath();
var count = path.elementCount();
for (var i=0; i<count; i++) {
var elementType = path.elementAtIndex(i);
var points = path.associatedPointsAtIndex(i);
if (elementType != 3 /*NSBezierPathElementClosePath, which has no elements.*/) {
// Print the first point
var p = points[0].pointValue();
print(p.x + ", " + p.y);
// Or all of them.
print(points);
}
}
}
Wow. Thanks for the awesome turnaround. This works great. I had one odd behavior that may be expected. In the following code, C.x and C.y are empty objects (e.g. {}) instead of the float values. If I add 0.0 I get the floats. I assume this is related to the bridge behavior for fields/properties of structs or something.
var p = points[0].pointValue();
var C = {
x: p.x, // x: p.x+0.0 works?
y: p.y
};
Yea, that is weird. Can you send me a sample .acorn file with a vector shape in it that reproduce the problem (sending it to support@flyingmeat.com will work). I’d like to see what’s going on over here.
So the reason is because the type of x (from p.x) is a javascript object. And if you just print it out, it’s all good- you see the right values. But then the values are being erased when you use JSON.stringify(C) on it. The JSON parser doesn’t know what to turn the value into.
So when you do p.x + 0, you’re telling javascript to coerce the type into a number, when JSON.stringify does know how to handle.
I’m not sure how to bridge the NSPoint struct over into JavaScript. Maybe there could be a NSPointToJSPoint function that takes one and does the right thing - returns a new object with the .x and .y fields set to the right type? You’re basically already doing that with your workaround.
So- I don’t have a good solution right now. But, I’ll be futzing around with it a bit with the JS bridge which will eventually replace JSTalk in Acorn (FMJS: GitHub - ccgus/fmjs: A JavaScript to C/Cocoa bridge )