2009年4月3日金曜日

NSBezierPathからSVGのpath要素d属性の値を作る

NSBezierPathの'elementAtIndex:associatedPoints:'メソッドについてのあれこれ.


NSBezierPath *path= ...;
NSMutableString *dValue= [NSMutableString string];

int j, cj= [path elementCount];
for(j= 0; j<cj; j++) {
NSPoint points[3];
NSBezierPathElement e= [path elementAtIndex:j associatedPoints:points];
if (e == NSMoveToBezierPathElement) {
NSPoint p= points[0];
[dValue appendFormat:@"M %.4f,%.4f", p.x, p.y];
}
else if (e == NSLineToBezierPathElement) {
NSPoint p= points[0];
[dValue appendFormat:@" L%.4f,%.4f", p.x, p.y];
}
else if (e == NSCurveToBezierPathElement) {
NSPoint c1= points[0];
NSPoint c2= points[1];
NSPoint p= points[2];
[dValue appendFormat:@" C%.4f,%.4f,%.4f,%.4f,%.4f,%.4f", c1.x, c1.y, c2.x, c2.y, p.x, p.y];
}
else if (e == NSClosePathBezierPathElement) {
[dValue appendFormat:@" Z"];
break; //グリフなどのように複数パスがある場合はこれいらない.
}
}

0 件のコメント: