String drawInRect NSImage problem

Discussion in 'Mac Programming' started by larswik, Dec 16, 2012.

  1. macrumors 68000

    Joined:
    Sep 8, 2006
    #1
    I have a Mac program that I am combining images and strings into an NSImage and then saving it as a .tif file to the desktop. It all works just fine but as I started to add attributes for my string like color, shadow and stroke I ran into a problem.

    In the included image you can see the stroke is also being offset / effected by the offset for the shadow. It works fine with a shadow or a stroke but when I use both it looks strange? It appears the stroke appears to inherits the attributes of the shadow for offset and blur? I grey'd out code that is not a problem to make it easier to read.

    OSX 10.7.5
    xcode 4.5.2

    Code.
    Code:
    -(void)buildImage:(NSArray*)array{
        [COLOR="Silver"]NSImage *toSave = [[NSImage alloc] initWithSize:CGSizeMake(1280, 720)];
        NSImage *imageSource = [[NSImage alloc]initWithData:[array objectAtIndex:0]];
        NSImage *rustPlate = [NSImage imageNamed:@"rusty-metal-plate.png"];
    
        NSString *itemName = [array objectAtIndex:1];
        NSString *itemWeight = [array objectAtIndex:3];
        NSString *itemCost = [array objectAtIndex:4];
        NSColor* tokenColor = [NSColor yellowColor];[/COLOR]
        
        NSShadow* shadw = [[NSShadow alloc] init];
        [shadw setShadowColor:[NSColor colorWithCalibratedRed:0.0 green:0.0 blue:0.0 alpha:0.4]];
        [shadw setShadowOffset:NSMakeSize( 5.0, -5.0 )];
        [shadw setShadowBlurRadius:1.0];
                                             
        NSDictionary *nameAttributes = [NSDictionary dictionaryWithObjectsAndKeys: [NSFont fontWithName:@"Helvetica-Bold" size:82], NSFontAttributeName,tokenColor,NSForegroundColorAttributeName,[NSNumber numberWithFloat:-3.0],NSStrokeWidthAttributeName,shadw, NSShadowAttributeName, [NSColor blackColor],NSStrokeColorAttributeName, nil];
        
       [COLOR="Silver"] NSDictionary *weightAttributes = [NSDictionary dictionaryWithObjectsAndKeys: [NSFont fontWithName:@"Helvetica-Bold" size:36], NSFontAttributeName,tokenColor,NSForegroundColorAttributeName,[NSNumber numberWithFloat:-5.0],NSStrokeWidthAttributeName,
                                        [NSColor blackColor],NSStrokeColorAttributeName, nil];
        
        NSDictionary *costAttributes = [NSDictionary dictionaryWithObjectsAndKeys: [NSFont fontWithName:@"MarkerFelt-Thin" size:140], NSFontAttributeName,tokenColor,NSForegroundColorAttributeName,[NSNumber numberWithFloat:-3.0],NSStrokeWidthAttributeName,
                                          [NSColor blackColor],NSStrokeColorAttributeName, nil];[/COLOR]
    
        [toSave lockFocus];
        
        [rustPlate drawInRect:NSMakeRect(200,140,900,500) fromRect: NSZeroRect operation:NSCompositeCopy fraction:1.0];
        [imageSource drawInRect:NSMakeRect(650,200,400,400) fromRect: NSZeroRect operation:NSCompositeSourceOver fraction:1.0];
        [itemName drawInRect:NSMakeRect(260, 520, 450, 100) withAttributes: nameAttributes];
        [itemWeight drawInRect:NSMakeRect(260, 420, 450, 100) withAttributes: weightAttributes];
        [itemCost drawInRect:NSMakeRect(280, 180, 450, 250) withAttributes: costAttributes];
        
        [toSave unlockFocus];
        
       [COLOR="Silver"] NSData *data = [toSave TIFFRepresentation];
        [data writeToFile:  @"/Users/larspro/Documents/ERad files/saved/file.tif" atomically: NO];[/COLOR]
    }
    
    Any Ideas?
     

    Attached Files:

    • file.jpg
      file.jpg
      File size:
      29.2 KB
      Views:
      496
  2. macrumors regular

    Joined:
    Apr 8, 2009
    #2
    Looks to me like it's a bug. You can see it's drawing a shadow for both the string's fill and stroke, but the stroke's shadow seems to be drawing on top of everything else. I'd send a bug report to Apple and see if they agree.

    As a workaround, you can draw the shadow as a separate operation and draw the other string on top of it:

    Code:
    NSDictionary *nameAttributes = [NSDictionary dictionaryWithObjectsAndKeys:
    								[NSFont fontWithName:@"Helvetica-Bold" size:82], NSFontAttributeName,
    								tokenColor,NSForegroundColorAttributeName,
    								[NSNumber numberWithFloat:-3.0],NSStrokeWidthAttributeName,
    								[NSColor blackColor],NSStrokeColorAttributeName, nil];
    NSDictionary *nameShadowAttributes = [NSDictionary dictionaryWithObjectsAndKeys:
    								[NSFont fontWithName:@"Helvetica-Bold" size:82], NSFontAttributeName,
    								shadw, NSShadowAttributeName,
    								[NSColor whiteColor],NSForegroundColorAttributeName, nil];
    
    [toSave lockFocus];
    
    // Draw the shadow only
    [[NSGraphicsContext currentContext] saveGraphicsState];
    [[NSGraphicsContext currentContext] setCompositingOperation:NSCompositePlusDarker];
    [itemName drawInRect:NSMakeRect(260, 520, 450, 100) withAttributes: nameShadowAttributes];
    [[NSGraphicsContext currentContext] restoreGraphicsState];
    
    // Draw the stylized string
    [itemName drawInRect:NSMakeRect(260, 520, 450, 100) withAttributes: nameAttributes];
    
    [toSave unlockFocus];
    
     
  3. thread starter macrumors 68000

    Joined:
    Sep 8, 2006
    #3
    That a good approach. I was dealing with this for most of my Saturday night. It's nice to know that it might just be a bug.

    Thanks for the help.
     

Share This Page