Add Vimball (vba) as supported filetype
[MacVim.git] / src / MacVim / icons / make_icons.py
blobc0439a7869f0af459925a29fd5cc3eed5d7650dd
1 # Creates all of MacVim document icons.
3 # http://www.macresearch.org/cocoa-scientists-part-xx-python-scriptersmeet-cocoa
4 try:
5 # Make us independent of sysprefs->appearance->antialias fonts smaller than...
6 # Needs to happen before docerator is imported.
7 from AppKit import NSUserDefaults
8 prefs = NSUserDefaults.standardUserDefaults()
9 prefs.setInteger_forKey_(4, 'AppleAntiAliasingThreshold')
11 import docerator
13 # Load Envy Code R from a file and register it under its postscript name
14 # Thanks to DamienG for this font (redistributed with permission):
15 # http://damieng.com/blog/2008/05/26/envy-code-r-preview-7-coding-font-released
16 import loadfont
17 loadfont.loadfont('Envy Code R Bold.ttf')
19 from Foundation import NSString
20 from AppKit import *
22 dont_create = False
23 except:
24 dont_create = True # most likely because we're on tiger
26 import os
27 import sys
30 # icon types
31 LARGE = 0 # 512, 128, 32, 16; about 96kB
32 SMALL = 1 # 128, 32, 16; about 36kB
33 LINK = 2 # Create link to generic icon; 4kB (== smallest block size on HFS+)
35 iconsizes = {
36 SMALL: [128, 32, 16],
37 LARGE: [512, 128, 32, 16],
41 # Resources
42 MAKEICNS = 'makeicns/makeicns'
43 APPICON = 'vim-noshadow-512.png'
44 DEFAULT_BACKGROUND = '/System/Library/CoreServices/CoreTypes.bundle/' + \
45 'Contents/Resources/GenericDocumentIcon.icns'
48 # List of icons to create
49 GENERIC_ICON_NAME = 'MacVim-generic'
50 vimIcons = {
51 GENERIC_ICON_NAME: [u'', LARGE],
52 'MacVim-vim': [u'VIM', LARGE],
53 'MacVim-txt': [u'TXT', SMALL],
54 'MacVim-tex': [u'TEX', SMALL],
55 'MacVim-h': [u'H', SMALL],
56 'MacVim-c': [u'C', SMALL],
57 'MacVim-m': [u'M', SMALL],
58 'MacVim-mm': [u'MM', SMALL],
59 'MacVim-cpp': [u'C\uff0b\uff0b,C++,C++', SMALL], # fullwidth plusses
60 'MacVim-java': [u'JAVA', SMALL],
61 'MacVim-f': [u'FTRAN', SMALL],
62 'MacVim-html': [u'HTML', SMALL],
63 'MacVim-xml': [u'XML', SMALL],
64 'MacVim-js': [u'JS', SMALL],
65 'MacVim-perl': [u'PERL,PL', SMALL],
66 'MacVim-py': [u'PYTHON,PY', SMALL],
67 'MacVim-php': [u'PHP', SMALL],
68 'MacVim-rb': [u'RUBY,RB', SMALL],
69 'MacVim-bash': [u'SH', SMALL],
70 'MacVim-patch': [u'DIFF', SMALL],
71 'MacVim-applescript': [u'\uf8ffSCPT,\uf8ffS', SMALL], # apple sign
72 'MacVim-as': [u'FLASH', LINK],
73 'MacVim-asp': [u'ASP', LINK],
74 'MacVim-bib': [u'BIB', LINK],
75 'MacVim-cs': [u'C#', LINK],
76 'MacVim-csfg': [u'CFDG', LINK],
77 'MacVim-csv': [u'CSV', LINK],
78 'MacVim-tsv': [u'TSV', LINK],
79 'MacVim-cgi': [u'CGI', LINK],
80 'MacVim-cfg': [u'CFG', LINK],
81 'MacVim-css': [u'CSS', SMALL],
82 'MacVim-dtd': [u'DTD', LINK],
83 'MacVim-dylan': [u'DYLAN', LINK],
84 'MacVim-erl': [u'ERLANG,ERL', SMALL],
85 'MacVim-fscript': [u'FSCPT,FSCR,FS', SMALL],
86 'MacVim-hs': [u'HS', SMALL],
87 'MacVim-inc': [u'INC', LINK],
88 'MacVim-ics': [u'ICS', SMALL],
89 'MacVim-ini': [u'INI', LINK],
90 'MacVim-io': [u'IO', LINK],
91 'MacVim-bsh': [u'BSH', LINK],
92 'MacVim-properties': [u'PROP', LINK],
93 'MacVim-jsp': [u'JSP', SMALL],
94 'MacVim-lisp': [u'LISP,LISP,LSP', SMALL],
95 'MacVim-log': [u'LOG', SMALL],
96 'MacVim-wiki': [u'WIKI', SMALL],
97 'MacVim-ps': [u'PS', LINK],
98 #'MacVim-plist': [u'PLIST', SMALL],
99 'MacVim-sch': [u'SCHEME,SCM', SMALL],
100 'MacVim-sql': [u'SQL', SMALL],
101 'MacVim-tcl': [u'TCL', SMALL],
102 'MacVim-xsl': [u'XSL', LINK],
103 'MacVim-vcf': [u'VCARD,VCF', SMALL],
104 'MacVim-vb': [u'VBASIC,VB', LINK],
105 'MacVim-yaml': [u'YAML,YAML,YML', SMALL],
106 'MacVim-gtd': [u'GTD', LINK],
107 'MacVim-markdown': [u'MARK\u2193,M\u2193', LINK], # down arrow
108 'MacVim-rst': [u'RST', LINK],
109 'MacVim-vba': [u'VBA', LINK],
113 def createLinks(icons, target):
114 assert len(icons) > 0
115 for name in icons:
116 icnsName = '%s.icns' % name
117 if os.access(icnsName, os.F_OK):
118 os.remove(icnsName)
119 os.symlink(target, icnsName)
122 if not dont_create:
123 # define a few classes to render custom 16x16 icons
125 class NoTextRenderer(docerator.TextRenderer):
126 def drawTextAtSize(self, text, s):
127 if s == 16: return # No text at 16x16
128 docerator.TextRenderer.drawTextAtSize(self, text, s)
130 class NoIconRenderer(docerator.BackgroundRenderer):
131 def drawIcon(self, s):
132 if s == 16: return # no "MacVim" icon on the sheet at 16x16
133 docerator.BackgroundRenderer.drawIcon(self, s)
135 class SmallTextRenderer(docerator.TextRenderer):
136 def _attribsAtSize(self, s):
137 attribs = docerator.TextRenderer._attribsAtSize(self, s)
138 if s == 16:
139 font = NSFont.fontWithName_size_('EnvyCodeR-Bold', 7.0)
140 assert font
141 attribs[NSFontAttributeName] = font
142 attribs[NSForegroundColorAttributeName] = \
143 NSColor.colorWithDeviceRed_green_blue_alpha_(
144 0/255.0, 82/255.0, 0/255.0, 1)
145 return attribs
147 def drawTextAtSize(self, text, s):
148 if s != 16:
149 docerator.TextRenderer.drawTextAtSize(self, text, s)
150 return
151 text = NSString.stringWithString_(text.lower()[0:3]) # at most 3 chars
152 attribs = self.attribsAtSize(s)
153 if len(text) <= 2:
154 attribs[NSKernAttributeName] = 0 # we have some space
155 else:
156 attribs[NSKernAttributeName] = -1 # we need all the room we can get
157 text.drawInRect_withAttributes_( ((1, 2), (15, 11)), attribs)
160 def main():
161 if dont_create:
162 print "PyObjC not found, only using a stock icon for document icons."
163 # Can't use the constants from docerator in this case
164 import shutil
165 shutil.copyfile(DEFAULT_BACKGROUND, '%s.icns' % GENERIC_ICON_NAME)
166 createLinks([name for name in vimIcons if name != GENERIC_ICON_NAME],
167 '%s.icns' % GENERIC_ICON_NAME)
168 return
170 srcdir = os.getcwd()
171 if len(sys.argv) > 1:
172 os.chdir(sys.argv[1])
173 appIcon = os.path.join(srcdir, APPICON)
174 makeIcns = os.path.join(srcdir, MAKEICNS)
176 # create LARGE and SMALL icons first...
177 for name, t in vimIcons.iteritems():
178 text, size = t
179 if size == LINK: continue
180 print name
181 if name == GENERIC_ICON_NAME:
182 # The generic icon has no text; make the appicon a bit larger
183 docerator.makedocicon(outname='%s.icns' % name, appicon=appIcon,
184 text=text, sizes=iconsizes[size], makeicns=makeIcns,
185 textrenderer=NoTextRenderer, rects={16:(0.0, 0.5533, 0.0, 0.5533)})
186 else:
187 # For the other icons, leave out appicon and render text in Envy Code R
188 docerator.makedocicon(outname='%s.icns' % name, appicon=appIcon,
189 text=text, sizes=iconsizes[size], makeicns=makeIcns,
190 textrenderer=SmallTextRenderer, backgroundrenderer=NoIconRenderer)
192 # ...create links later (to make sure the link targets exist)
193 createLinks([name for (name, t) in vimIcons.items() if t[1] == LINK],
194 '%s.icns' % GENERIC_ICON_NAME)
197 if __name__ == '__main__':
198 main()