2

I am creating an application that reads in SVG files, parses them, and redraws them in Cocoa using NSBezierPath. The following vector path:

M0 126C0.0507812 106.402 0.443359 78.6089 24.0781 76.0034 29.9937 75.3516 34.063 75.6162 39.6509 74 50.0083 71.0044 48.9268 69.8765 54.7329 60.0034 64.9927 42.5586 75.5684 42.8237 94.0801 42.0195 120.367 40.8779 127.438 2.28662 155.885 2.85254 156.091 1.86523 155.896 0.98877 156 0

looks perfectly fine in Illustrator; it is a short, wavy segment. In my application, the position is correct, but the curve shows up as a straight line (see images below).

Illustrator My application

What could I be missing? Below is my drawing routine, which gets fed from an array that holds all of the values saved in different vector models.

- (void)processImage
{
    float factorX = 1;
    float factorY = 1;

    debugLog(@"[sharedValues paths] is %lu long.", [[sharedValues paths]count]);

    NSArray *objectArray = [sharedValues paths];
    [[NSColor blackColor] set];
    aPath = [NSBezierPath bezierPath];
    [aPath moveToPoint:NSMakePoint(0.0, 0.0)];

    for(int i=0; i<[objectArray count]; i++) {
        debugLog(@"I am in iteration %i", i);
        if([[[objectArray objectAtIndex:i] type]isEqualToString:@"AbsoluteMoveTo"]){
            AbsoluteMoveToModel *model = [objectArray objectAtIndex:i];
            [aPath moveToPoint:NSMakePoint([model x] * factorX, [model y] * factorY)];
        } else if([[[objectArray objectAtIndex:i] type]isEqualToString:@"RelativeMoveTo"]){
            debugLog(@"Drawing a relative path moveto");
            RelativeMoveToModel *model = [objectArray objectAtIndex:i];
            [aPath relativeMoveToPoint:NSMakePoint([model x] * factorX, [model y] * factorY)];
        } else if([[[objectArray objectAtIndex:i] type]isEqualToString:@"AbsoluteLineTo"]){
            debugLog(@"Drawing an absolute path lineto");
            AbsoluteLineToModel *model = [objectArray objectAtIndex:i];
            [aPath lineToPoint:NSMakePoint([model x] * factorX, [model y] * factorY)];
        } else if([[[objectArray objectAtIndex:i] type]isEqualToString:@"RelativeLineTo"]){
            debugLog(@"Drawing a relative path lineto");
            RelativeLineToModel *model = [objectArray objectAtIndex:i];
            [aPath relativeLineToPoint:NSMakePoint([model x] * factorX, [model y] * factorY)];
            //[model release];
        } else if([[[objectArray objectAtIndex:i] type]isEqualToString:@"AbsoluteCurveTo"]){
            debugLog(@"Drawing an absolute path lineto");
            AbsoluteCubicBezierCurveToModel *model = [objectArray objectAtIndex:i];
            [aPath curveToPoint:NSMakePoint([model x] * factorX, [model y] * factorY) controlPoint1:NSMakePoint([model c1x] * factorX, [model c1y] * factorY) controlPoint2:NSMakePoint([model c2x] * factorX, [model c2y] * factorY)];
        } else if([[[objectArray objectAtIndex:i] type]isEqualToString:@"RelativeCurveTo"]){
            RelativeCubicBezierCurveToModel *model = [objectArray objectAtIndex:i];
            [aPath relativeCurveToPoint:NSMakePoint([model x] * factorX, [model y] * factorY) controlPoint1:NSMakePoint([model c1x] * factorX, [model c1y] * factorY) controlPoint2:NSMakePoint([model c2x] * factorX, [model c2y] * factorY)];
        } else if([[[objectArray objectAtIndex:i] type]isEqualToString:@"ClosePath"]){
            [aPath closePath];
        }
    }

    [aPath retain];
    //debugLog(@"%@", aPath);
    [self setNeedsDisplay:YES];
}

- (void)drawRect:(NSRect)rect
{
    //debugLog(@"%@", aPath);
    [[NSColor whiteColor] set];
    NSRectFill( rect );
    [[NSColor blackColor] set];
    [aPath stroke];
}
diatrevolo
  • 2,782
  • 26
  • 45
  • It seems I'm screwing up all but the most basic of curves, so I need to go back to the drawing board a bit (no pun intended). I'll report back with my findings. – diatrevolo Feb 21 '11 at 02:01
  • Hey would you be willing to share your code? i.e. How you are parsing the illustrator svg files. I am looking to do the same thing. I have had some pretty good success but it is not perfect. Here is an example of a path I want to draw: m 222.453,26.633 c 0.164,-0.197 0.23,-0.315 0.23,-0.315 0,0 -0.079,0.116 -0.23,0.315 z – Matt Mar 11 '11 at 04:58
  • Hi Matt: Unfortunately, the code isn't mine to share, but maybe if you provide a specific question I can help you out? – diatrevolo Mar 17 '11 at 01:18

1 Answers1

1

The solution was quite simple...in long beziers, where values are concatenated as above, I wasn't clearing out the old data model before entering a new curve coordinate. All is well now.

diatrevolo
  • 2,782
  • 26
  • 45