Powerline字體補丁
@2013/05/03
接上一篇。
使用spf13的vim插件,如果當前系統的vim支持python的話,會安裝 powerline插件,如果不支持的話則會安裝vim-powerline,powerline相當于vim-powerline的升級版,后者的效果可以參看上一篇,前者的效果可以看下圖:

看的出powerline比vim-power的效果要炫的多,但是使用powerline的話需要對終端使用的字體打補丁,不然會顯示亂碼(當然亂碼的原因也可能是終端編碼設置不對,這里只考慮字體的原因)。
使用gnome-terminal、konsole等linux原生的終端也可以不給使用的字體打補丁,具體可以參考官方的文檔,但如果使用putty、securecrt等遠程終端工具的話必須打補丁。
Powerline插件自帶了補丁工具,如果直接使用spf13的腳本安裝的powerline插件,該工具在spf13-vim/.vim/bundle/powerline/font文件夾下:fontpatcher.py
使用類似下面的命令,就可以給consola.ttf字體打補丁:
./fontpatcher.py consola.ttf
我在使用該工具對consolas字體打補丁時遇到了一些問題:
1. The required FontForge modules could not be loaded.
該工具依賴fontforge,但使用系統的包管理工具安裝了fontforge之后可能仍然出現該問題(至少在我的opensuse12.2 64bit下不能用),可以通過下載源碼重新編譯(configure時注意加上--enable-pyextension選項);
2. 雖然重新編譯、安裝了fontforge,執行該工具時仍然可能出現1中的問題
切換到fontforge源代碼目錄下的pyhook目錄,然后執行python setup.py install安裝fontforge的python模塊(不知道為什么fontforge的makefile會缺這一步)
3. 打了補丁的字體putty、securecrt等工具不認識,在字體設置無法選擇打了補丁的字體,或者使用了打了補丁的字體后powerline插件顯示的仍然是亂碼
Github上有打了補丁的consolas等字體,但都有這個問題,可能只適用于linux下的原生終端使用,在網上找到了一個該字體工具的補丁,但是是針對老版的vim-powerline的,和新版工具差異太大,已經不能用了,參考它的思路,對新版的powerline字體工具打了個補丁如下:
--- fontpatcher/old/fontpatcher.py 2013-05-03 23:23:11.788069924 +0800
+++ fontpatcher/new/fontpatcher.py 2013-05-03 23:23:53.145073351 +0800
@@ -22,14 +22,16 @@
parser.add_argument('target_fonts', help='font files to patch', metavar='font', nargs='+', type=argparse.FileType('rb'))
parser.add_argument('--no-rename', help='don\'t add " for Powerline" to the font name', default=True, action='store_false', dest='rename_font')
parser.add_argument('--source-font', help='source symbol font', metavar='font', dest='source_font', default='{0}/fontpatcher-symbols.sfd'.format(sys.path[0]), type=argparse.FileType('rb'))
+parser.add_argument('--fix-mono', help='fixes some mono-fonts which have glyphs of 0 widths', default=False, action='store_true', dest='fix_mono')
args = parser.parse_args()
class FontPatcher(object):
- def __init__(self, source_font, target_fonts, rename_font=True):
+ def __init__(self, source_font, target_fonts, rename_font=True, fix_mono=False):
self.source_font = fontforge.open(source_font.name)
self.target_fonts = (fontforge.open(target_font.name) for target_font in target_fonts)
self.rename_font = rename_font
+ self.fix_mono = fix_mono
def patch(self):
for target_font in self.target_fonts:
@@ -40,10 +42,10 @@
# Rename font
if self.rename_font:
- target_font.familyname += ' for Powerline'
- target_font.fullname += ' for Powerline'
+ target_font.familyname = 'Powerline ' + target_font.familyname
+ target_font.fullname = 'Powerline ' + target_font.fullname
fontname, style = re.match("^([^-]*)(?:(-.*))?$", target_font.fontname).groups()
- target_font.fontname = fontname + 'ForPowerline'
+ target_font.fontname = 'Powerline ' + fontname
if style is not None:
target_font.fontname += style
target_font.appendSFNTName('English (US)', 'Preferred Family', target_font.familyname)
@@ -102,6 +104,11 @@
# Reset the font's glyph width so it's still considered monospaced
target_font[source_glyph.unicode].width = target_font_width
+ if self.fix_mono:
+ for target_glyph in target_font.glyphs():
+ if target_glyph.width == 0:
+ target_glyph.width = target_font_width
+
target_font.em = target_font_em_original
# Generate patched font
@@ -111,5 +118,5 @@
extension = '.otf'
target_font.generate('{0}{1}'.format(target_font.fullname, extension))
-fp = FontPatcher(args.source_font, args.target_fonts, args.rename_font)
+fp = FontPatcher(args.source_font, args.target_fonts, args.rename_font, args.fix_mono)
fp.patch()
使用如下的命令對consola字體打補丁,然后安裝字體就可以在putty、securecrt等工具中使用了:
./fontpatcher.py –fix-mono consola.ttf
我在github上傳了一份打了補丁的consolas字體,下載之后可以直接使用:
https://github.com/runsisi/consolas-font-for-powerline
同時我也在github上上傳了一份打包好了的spf13插件,只需要解壓然后執行./install.sh即可(注意要求vim7.3,且vim支持python):
https://github.com/runsisi/a-ready-to-use-vimrc
/Files/runsisi/powerline_fontpatcher.pdf
/Files/runsisi/fontpatcher.rar
/Files/runsisi/fontpatcher_diff.rar