/
AcornNewImageWithCurvedDropShadow.jstalk
71 lines (51 loc) · 2.77 KB
/
AcornNewImageWithCurvedDropShadow.jstalk
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
var acorn = [JSTalk application:"Acorn"];
var doc = [[acorn orderedDocuments] objectAtIndex:0];
var image = [[[NSImage alloc] initWithData:[doc dataRepresentationOfType:"public.png"]] autorelease];
var bitmap = [image bestRepresentationForDevice:nil];
var extent = NSMakeRect(0, 0, [bitmap pixelsWide], [bitmap pixelsHigh]);
var xOffset = 5;
var yOffset = 35;
var curveHeight = 15;
var imageYOffset = 10;
var whiteBorderWidth = 4;
var doubleBorderWidth = whiteBorderWidth * 2;
var newSize = NSMakeSize(extent.size.width + (whiteBorderWidth * 2),
extent.size.height + imageYOffset + (whiteBorderWidth * 2));
var newImage = [[[NSImage alloc] initWithSize:newSize] autorelease];
[newImage lockFocus];
[[NSGraphicsContext currentContext] saveGraphicsState];
var shadow = [[NSShadow alloc] init];
[shadow setShadowColor:[[NSColor blackColor] colorWithAlphaComponent:.6]];
var shadowOffset = NSMakeSize(0, -(yOffset + 5));
[shadow setShadowOffset:shadowOffset];
[shadow setShadowBlurRadius:5];
shadow.set()
// make a curved path, at the bottom of our image.
bezierPath = NSBezierPath.bezierPath();
[bezierPath moveToPoint:NSMakePoint(xOffset, 40 + yOffset)];
[bezierPath lineToPoint:NSMakePoint(extent.size.width - (xOffset) + doubleBorderWidth, 40 + yOffset)];
[bezierPath lineToPoint:NSMakePoint(extent.size.width - (xOffset) + doubleBorderWidth, 10 + yOffset)];
[bezierPath curveToPoint:NSMakePoint(newSize.width / 2, curveHeight + yOffset)
controlPoint1:NSMakePoint(extent.size.width - (xOffset), 10 + yOffset)
controlPoint2:NSMakePoint(newSize.width *.75, curveHeight + yOffset)];
[bezierPath curveToPoint:NSMakePoint(xOffset, 10 + yOffset)
controlPoint1:NSMakePoint(newSize.width *.25, curveHeight + yOffset)
controlPoint2:NSMakePoint(xOffset, 10 + yOffset)];
[bezierPath fill];
// get rid of our shadow
[[NSGraphicsContext currentContext] restoreGraphicsState];
// draw a white border
[[NSColor whiteColor] set];
[[NSBezierPath bezierPathWithRect:NSMakeRect(0, imageYOffset, newSize.width, extent.size.height + whiteBorderWidth * 2)] fill];
// draw our gray border around the white border
[[NSColor lightGrayColor] set];
[[NSBezierPath bezierPathWithRect:NSMakeRect(.5, imageYOffset + .5 , newSize.width - 1, (extent.size.height - 1) + (whiteBorderWidth * 2))] stroke];
// NSImage takes into account dpi of the image. So we force it's size, to avoid small images.
[image setSize: extent.size];
// draw our image
[image drawAtPoint:NSMakePoint(whiteBorderWidth, imageYOffset + whiteBorderWidth)
fromRect:NSMakeRect(0, 0, extent.size.width, extent.size.height)
operation:NSCompositeCopy
fraction:1];
[newImage unlockFocus];
[[acorn sharedDocumentController] newDocumentWithImageData:[newImage TIFFRepresentation]];