This repository has been archived by the owner on Jan 25, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
/
swamp.swift
executable file
·104 lines (83 loc) · 3.39 KB
/
swamp.swift
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
#!/usr/bin/xcrun swift
import AppKit
extension NSImage {
func bitmapRepresentation() -> NSBitmapImageRep {
let cgRef = self.CGImageForProposedRect(nil, context: nil, hints: nil)
return NSBitmapImageRep(CGImage: cgRef!.takeRetainedValue())
}
}
struct Swamp {
let icon: NSImage
let textAttributes: Dictionary<String, AnyObject>
let fontName = "HelveticaNeue-Light"
init(icon: NSImage) {
self.icon = icon
self.textAttributes = [
NSShadowAttributeName: {
let unit = icon.size.height / 64
let shadow = NSShadow()
shadow.shadowOffset = CGSizeMake(0, -unit)
shadow.shadowBlurRadius = unit
shadow.shadowColor = NSColor.controlDarkShadowColor()
return shadow
}(),
NSParagraphStyleAttributeName: {
let paragraphStyle = NSParagraphStyle.defaultParagraphStyle().mutableCopy() as NSMutableParagraphStyle
paragraphStyle.alignment = NSTextAlignment.CenterTextAlignment
paragraphStyle.lineBreakMode = NSLineBreakMode.ByWordWrapping;
return paragraphStyle
}(),
NSForegroundColorAttributeName: NSColor.whiteColor()
]
}
func addText(text: String) {
let offset = self.icon.size.height / 20
let containerSize = CGSize(width: self.icon.size.width,
height: self.icon.size.height - 2 * offset)
var fontSize = self.icon.size.height / 4
var textContainer: NSTextContainer
var textStorage: NSTextStorage
var layoutManager: NSLayoutManager
var renderedRange: NSRange
var usedRect: CGRect
// Keep decreasing the font size until it's either 8 pts or the text
// fits completely
do {
textContainer = NSTextContainer(containerSize: containerSize)
textStorage = NSTextStorage(string: text, attributes:textAttributes)
textStorage.font = NSFont(name:self.fontName, size: fontSize)
layoutManager = NSLayoutManager()
layoutManager.addTextContainer(textContainer)
layoutManager.textStorage = textStorage
renderedRange = layoutManager.glyphRangeForTextContainer(textContainer)
usedRect = layoutManager.usedRectForTextContainer(textContainer)
fontSize -= 0.1
} while renderedRange.length < countElements(text) && fontSize > 8
let point = CGPointMake(0, self.icon.size.height - usedRect.size.height - offset)
self.icon.lockFocusFlipped(true)
layoutManager.drawGlyphsForGlyphRange(renderedRange, atPoint:point)
self.icon.unlockFocus()
}
func save(path: String) {
self.icon
.bitmapRepresentation()
.representationUsingType(NSBitmapImageFileType.NSPNGFileType,
properties: [:])?
.writeToFile(path, atomically:true)
}
}
if Process.arguments.count < 4 {
println("Usage: stamp.swift -- [input] [output] [text]")
exit(1)
}
let input = Process.arguments[1]
let output = Process.arguments[2]
let text = Process.arguments[3]
let icon: NSImage? = NSImage(data: NSData(contentsOfFile: input)!)
if icon == nil {
println("Could not load file \(input)")
exit(1)
}
let swamp = Swamp(icon: icon!)
swamp.addText(text)
swamp.save(output)