/
make_icons.py
195 lines (167 loc) · 6.5 KB
/
make_icons.py
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
# Creates all of MacVim document icons.
# http://www.macresearch.org/cocoa-scientists-part-xx-python-scriptersmeet-cocoa
try:
# Make us independent of sysprefs->appearance->antialias fonts smaller than...
# Needs to happen before docerator is imported.
from AppKit import NSUserDefaults
prefs = NSUserDefaults.standardUserDefaults()
prefs.setInteger_forKey_(4, 'AppleAntiAliasingThreshold')
import docerator
# Load Envy Code R from a file and register it under its postscript name
# Thanks to DamienG for this font (redistributed with permission):
# http://damieng.com/blog/2008/05/26/envy-code-r-preview-7-coding-font-released
import loadfont
loadfont.loadfont('Envy Code R Bold.ttf')
from Foundation import NSString
from AppKit import *
dont_create = False
except:
dont_create = True # most likely because we're on tiger
import os
import sys
# icon types
LARGE = 0 # 512, 128, 32, 16; about 96kB
SMALL = 1 # 128, 32, 16; about 36kB
LINK = 2 # Create link to generic icon; 4kB (== smallest block size on HFS+)
iconsizes = {
SMALL: [128, 32, 16],
LARGE: [512, 128, 32, 16],
}
# Resources
MAKEICNS = 'makeicns/makeicns'
APPICON = 'vim-noshadow-512.png'
DEFAULT_BACKGROUND = '/System/Library/CoreServices/CoreTypes.bundle/' + \
'Contents/Resources/GenericDocumentIcon.icns'
# List of icons to create
GENERIC_ICON_NAME = 'MacVim-generic'
vimIcons = {
GENERIC_ICON_NAME: [u'', LARGE],
'MacVim-vim': [u'VIM', LARGE],
'MacVim-txt': [u'TXT', SMALL],
'MacVim-tex': [u'TEX', SMALL],
'MacVim-h': [u'H', SMALL],
'MacVim-c': [u'C', SMALL],
'MacVim-m': [u'M', SMALL],
'MacVim-mm': [u'MM', SMALL],
'MacVim-cpp': [u'C\uff0b\uff0b,C++,C++', SMALL], # fullwidth plusses
'MacVim-java': [u'JAVA', SMALL],
'MacVim-f': [u'FTRAN', SMALL],
'MacVim-html': [u'HTML', SMALL],
'MacVim-xml': [u'XML', SMALL],
'MacVim-js': [u'JS', SMALL],
'MacVim-perl': [u'PERL,PL', SMALL],
'MacVim-py': [u'PYTHON,PY', SMALL],
'MacVim-php': [u'PHP', SMALL],
'MacVim-rb': [u'RUBY,RB', SMALL],
'MacVim-bash': [u'SH', SMALL],
'MacVim-patch': [u'DIFF', SMALL],
'MacVim-applescript': [u'\uf8ffSCPT,\uf8ffS', SMALL], # apple sign
'MacVim-as': [u'FLASH', LINK],
'MacVim-asp': [u'ASP', LINK],
'MacVim-bib': [u'BIB', LINK],
'MacVim-cs': [u'C#', LINK],
'MacVim-csfg': [u'CFDG', LINK],
'MacVim-csv': [u'CSV', LINK],
'MacVim-tsv': [u'TSV', LINK],
'MacVim-cgi': [u'CGI', LINK],
'MacVim-cfg': [u'CFG', LINK],
'MacVim-css': [u'CSS', SMALL],
'MacVim-dtd': [u'DTD', LINK],
'MacVim-dylan': [u'DYLAN', LINK],
'MacVim-erl': [u'ERLANG,ERL', SMALL],
'MacVim-fscript': [u'FSCPT,FSCR,FS', SMALL],
'MacVim-hs': [u'HS', SMALL],
'MacVim-inc': [u'INC', LINK],
'MacVim-ics': [u'ICS', SMALL],
'MacVim-ini': [u'INI', LINK],
'MacVim-io': [u'IO', LINK],
'MacVim-bsh': [u'BSH', LINK],
'MacVim-properties': [u'PROP', LINK],
'MacVim-jsp': [u'JSP', SMALL],
'MacVim-lisp': [u'LISP,LISP,LSP', SMALL],
'MacVim-log': [u'LOG', SMALL],
'MacVim-wiki': [u'WIKI', SMALL],
'MacVim-ps': [u'PS', LINK],
#'MacVim-plist': [u'PLIST', SMALL],
'MacVim-sch': [u'SCHEME,SCM', SMALL],
'MacVim-sql': [u'SQL', SMALL],
'MacVim-tcl': [u'TCL', SMALL],
'MacVim-xsl': [u'XSL', LINK],
'MacVim-vcf': [u'VCARD,VCF', SMALL],
'MacVim-vb': [u'VBASIC,VB', LINK],
'MacVim-yaml': [u'YAML,YAML,YML', SMALL],
'MacVim-gtd': [u'GTD', LINK],
}
def createLinks(icons, target):
assert len(icons) > 0
for name in icons:
icnsName = '%s.icns' % name
if os.access(icnsName, os.F_OK):
os.remove(icnsName)
os.symlink(target, icnsName)
if not dont_create:
# define a few classes to render custom 16x16 icons
class NoTextRenderer(docerator.TextRenderer):
def drawTextAtSize(self, text, s):
if s == 16: return # No text at 16x16
docerator.TextRenderer.drawTextAtSize(self, text, s)
class NoIconRenderer(docerator.BackgroundRenderer):
def drawIcon(self, s):
if s == 16: return # no "MacVim" icon on the sheet at 16x16
docerator.BackgroundRenderer.drawIcon(self, s)
class SmallTextRenderer(docerator.TextRenderer):
def _attribsAtSize(self, s):
attribs = docerator.TextRenderer._attribsAtSize(self, s)
if s == 16:
font = NSFont.fontWithName_size_('EnvyCodeR-Bold', 7.0)
assert font
attribs[NSFontAttributeName] = font
attribs[NSForegroundColorAttributeName] = \
NSColor.colorWithDeviceRed_green_blue_alpha_(
0/255.0, 82/255.0, 0/255.0, 1)
return attribs
def drawTextAtSize(self, text, s):
if s != 16:
docerator.TextRenderer.drawTextAtSize(self, text, s)
return
text = NSString.stringWithString_(text.lower()[0:3]) # at most 3 chars
attribs = self.attribsAtSize(s)
if len(text) <= 2:
attribs[NSKernAttributeName] = 0 # we have some space
else:
attribs[NSKernAttributeName] = -1 # we need all the room we can get
text.drawInRect_withAttributes_( ((1, 2), (15, 11)), attribs)
def main():
if dont_create:
print "PyObjC not found, only using a stock icon for document icons."
# Can't use the constants from docerator in this case
import shutil
shutil.copyfile(DEFAULT_BACKGROUND, '%s.icns' % GENERIC_ICON_NAME)
createLinks([name for name in vimIcons if name != GENERIC_ICON_NAME],
'%s.icns' % GENERIC_ICON_NAME)
return
srcdir = os.getcwd()
if len(sys.argv) > 1:
os.chdir(sys.argv[1])
appIcon = os.path.join(srcdir, APPICON)
makeIcns = os.path.join(srcdir, MAKEICNS)
# create LARGE and SMALL icons first...
for name, t in vimIcons.iteritems():
text, size = t
if size == LINK: continue
print name
if name == GENERIC_ICON_NAME:
# The generic icon has no text; make the appicon a bit larger
docerator.makedocicon(outname='%s.icns' % name, appicon=appIcon,
text=text, sizes=iconsizes[size], makeicns=makeIcns,
textrenderer=NoTextRenderer, rects={16:(0.0, 0.5533, 0.0, 0.5533)})
else:
# For the other icons, leave out appicon and render text in Envy Code R
docerator.makedocicon(outname='%s.icns' % name, appicon=appIcon,
text=text, sizes=iconsizes[size], makeicns=makeIcns,
textrenderer=SmallTextRenderer, backgroundrenderer=NoIconRenderer)
# ...create links later (to make sure the link targets exist)
createLinks([name for (name, t) in vimIcons.items() if t[1] == LINK],
'%s.icns' % GENERIC_ICON_NAME)
if __name__ == '__main__':
main()