Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

larswik

macrumors 68000
Original poster
Sep 8, 2006
1,552
11
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?
 

Attachments

  • file.jpg
    file.jpg
    29.2 KB · Views: 609

JoshDC

macrumors regular
Apr 8, 2009
115
0
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];
 

larswik

macrumors 68000
Original poster
Sep 8, 2006
1,552
11
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.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.