2014-12-05 06:29:54 +02:00
#!/usr/bin/env python2
2015-08-21 19:17:55 +02:00
# version: 0.4.1
2014-12-05 06:29:54 +02:00
import sys
import psMat
import re
import os.path
2015-03-01 18:24:05 +02:00
import argparse
2014-12-05 06:29:54 +02:00
try:
#Load the module
import fontforge
except ImportError:
sys.stderr.write("FontForge module could not be loaded. Try installing fontforge python bindings\n")
sys.exit(1)
2015-03-01 18:24:05 +02:00
# argparse stuff
2015-08-11 00:33:21 +02:00
parser = argparse.ArgumentParser(description='Patches a given font with programming and web development related glyphs (mainly for https://github.com/ryanoasis/vim-devicons)')
2015-03-01 18:24:05 +02:00
parser.add_argument('font', help='The path to the font to be patched (e.g. Inconsolata.otf)')
parser.add_argument('-s', '--use-single-width-glyphs', dest='single', action='store_true', help='Whether to generate the glyphs as single-width not double-width (default is double-width)', default=False)
parser.add_argument('-q', '--quiet', '--shutup', dest='quiet', action='store_true', help='Do not generate verbose output', default=False)
2015-08-11 00:33:21 +02:00
parser.add_argument('-w', '--windows', '--limit-font-name-length', dest='windows', action='store_true', help='Limit the internal font name to a maximum of 31 characters (for safe Windows compatiblity)', default=False)
parser.add_argument('--fontawesome', dest='fontawesome', action='store_true', help='Add Font Awesome Glyphs (http://fortawesome.github.io/Font-Awesome/)', default=False)
parser.add_argument('--octicons', dest='octicons', action='store_true', help='Add Octicons Glyphs (https://octicons.github.com/)', default=False)
parser.add_argument('--pomicons', dest='pomicons', action='store_true', help='Add Pomicon Glyphs (https://github.com/gabrielelana/pomicons)', default=False)
2015-03-01 18:24:05 +02:00
args = parser.parse_args()
2015-04-21 16:10:25 +02:00
#print "using fontforge package version: " + str(fontforge.__version__) + " " + str(fontforge.version())
#print type(fontforge.version())
#print int(fontforge.version())
2015-08-21 19:17:55 +02:00
version = "0.4.1"
changelog = open("changelog.md", "r")
2015-04-21 16:10:25 +02:00
minimumVersion = 20141231
actualVersion = int(fontforge.version())
#actualVersion = 20120731 # for testing invalid version
if actualVersion < minimumVersion:
print "You seem to be using an unsupported (old) version of fontforge: " + str(actualVersion)
print "Please use at least version: " + str(minimumVersion)
sys.exit(1)
2014-12-05 06:29:54 +02:00
2015-08-11 00:33:21 +02:00
verboseAdditionalFontNameSuffix = " Plus Nerd File Types"
if args.windows:
# attempt to shorten here on the additional name BEFORE trimming later
additionalFontNameSuffix = " PNFT"
else:
additionalFontNameSuffix = verboseAdditionalFontNameSuffix
2015-03-01 18:24:05 +02:00
if args.single:
additionalFontNameSuffix += " Mono"
2015-08-11 00:33:21 +02:00
verboseAdditionalFontNameSuffix += " Mono"
if args.fontawesome:
additionalFontNameSuffix += " Plus Font Awesome"
verboseAdditionalFontNameSuffix += " Plus Font Awesome"
if args.octicons:
additionalFontNameSuffix += " Plus Octicons"
verboseAdditionalFontNameSuffix += " Plus Octicons"
if args.pomicons:
additionalFontNameSuffix += " Plus Pomicons"
verboseAdditionalFontNameSuffix += " Plus Pomicons"
2015-03-01 18:24:05 +02:00
sourceFont = fontforge.open(args.font)
2014-12-05 06:29:54 +02:00
fontname, style = re.match("^([^-]*)(?:(-.*))?$", sourceFont.fontname).groups()
2015-08-11 00:33:21 +02:00
familyname = sourceFont.familyname + additionalFontNameSuffix
# fullname (filename) can always use long/verbose font name, even in windows
fullname = sourceFont.fullname + verboseAdditionalFontNameSuffix
fontname = fontname + additionalFontNameSuffix.replace(" ", "")
if args.windows:
maxLength = 31
fullname += " Windows Compatible"
# now make sure less than 32 characters name length
#if len(fullname) > maxLength:
# fullname = fullname[:maxLength]
if len(fontname) > maxLength:
fontname = fontname[:maxLength]
if len(fullname) > maxLength:
familyname = familyname[:maxLength]
# rename font
def replace_all(text, dic):
for i, j in dic.iteritems():
text = text.replace(i, j)
return text
# comply with SIL Open Font License (OFL)
reservedFontNameReplacements = {'source': 'sauce', 'Source': 'Sauce', 'hermit': 'hurmit', 'Hermit': 'Hurmit'}
2015-08-21 19:17:55 +02:00
projectInfo = "Patched with 'Nerd Fonts Patcher' (https://github.com/ryanoasis/nerd-fonts)"
2015-08-11 00:33:21 +02:00
sourceFont.familyname = replace_all(familyname, reservedFontNameReplacements)
sourceFont.fullname = replace_all(fullname, reservedFontNameReplacements)
sourceFont.fontname = replace_all(fontname, reservedFontNameReplacements)
2014-12-05 06:29:54 +02:00
sourceFont.appendSFNTName('English (US)', 'Preferred Family', sourceFont.familyname)
sourceFont.appendSFNTName('English (US)', 'Compatible Full', sourceFont.fullname)
2015-08-21 19:17:55 +02:00
sourceFont.comment = projectInfo
sourceFont.fontlog = projectInfo + "\n\n" + changelog.read()
2014-12-05 06:29:54 +02:00
2015-08-21 19:17:55 +02:00
# todo version not being set for all font types (e.g. ttf)
#print "Version was " + sourceFont.version
sourceFont.version += ";Nerd Fonts " + version
#print "Version now is " + sourceFont.version
2015-08-11 00:33:21 +02:00
2014-12-05 06:29:54 +02:00
# glyph font
sourceFont_em_original = sourceFont.em
2015-08-21 19:16:20 +02:00
# Open fonts and force the em size to be equal
2015-02-25 21:45:58 +02:00
symbols = fontforge.open("glyph-source-fonts/original-source.otf")
2015-08-11 00:33:21 +02:00
powerlineSymbols = fontforge.open("glyph-source-fonts/PowerlineSymbols.otf")
symbolsDevicons = fontforge.open("glyph-source-fonts/devicons.ttf")
2015-08-21 19:16:20 +02:00
symbols.em = sourceFont.em
symbolsDevicons.em = sourceFont.em
if args.fontawesome:
fontawesome = fontforge.open("glyph-source-fonts/FontAwesome.otf")
fontawesome.em = sourceFont.em
if args.octicons:
octicons = fontforge.open("glyph-source-fonts/octicons.ttf")
octicons.em = sourceFont.em
2015-08-04 13:29:55 +02:00
if args.pomicons:
pomicons = fontforge.open("glyph-source-fonts/Pomicons.otf")
2015-08-21 19:16:20 +02:00
pomicons.em = sourceFont.em
# Define the character ranges
2015-08-11 00:33:21 +02:00
symbolsOriginalRangeStart = 0xE4FE
symbolsOriginalRangeEnd = 0xE52A
2015-02-25 21:45:58 +02:00
2015-08-11 00:33:21 +02:00
symbolsPowerlineRange1Start = 0xE0A0
symbolsPowerlineRange1End = 0xE0A2
2015-02-25 21:45:58 +02:00
2015-08-11 00:33:21 +02:00
symbolsPowerlineRange2Start = 0xE0B0
symbolsPowerlineRange2End = 0xE0B3
2015-02-25 21:45:58 +02:00
2015-08-11 00:33:21 +02:00
symbolsDeviconsRangeStart = 0xE600
symbolsDeviconsRangeEnd = 0xE6C5
symbolsFontAwesomeRangeStart = 0xF000
symbolsFontAwesomeRangeEnd = 0xF23A
symbolsOcticonsRangeStart = 0xF000
symbolsOcticonsRangeEnd = 0xF0DB
symbolsPomiconsRangeStart = 0xE000
symbolsPomiconsRangeEnd = 0xE00A
sourceFontOriginalStart = 0xE5FE
sourceFontOriginalEnd = 0xE62A
sourceFontDeviconsStart = 0xE700
sourceFontDeviconsEnd = 0xE7C5
sourceFontFontAwesomeStart = 0xF000
sourceFontFontAwesomeEnd = 0xF23A
sourceFontOcticonsStart = 0xF400
sourceFontOcticonsEnd = 0xF4DB
sourceFontPomiconsStart = 0xE000
sourceFontPomiconsEnd = 0xE00A
2015-02-25 21:45:58 +02:00
2015-02-21 03:21:52 +02:00
SYM_ATTR = {
# Right/left-aligned glyphs will have their advance width reduced in order to overlap the next glyph slightly
0x2b60: { 'align': 'c', 'stretch': 'y' , 'overlap': False },
0x2b61: { 'align': 'c', 'stretch': '' , 'overlap': False },
0x2b62: { 'align': 'r', 'stretch': '' , 'overlap': False },
0x2b63: { 'align': 'l', 'stretch': '' , 'overlap': False },
0x2b64: { 'align': 'c', 'stretch': '' , 'overlap': False },
0x2b80: { 'align': 'l', 'stretch': 'xy', 'overlap': True },
0x2b81: { 'align': 'l', 'stretch': 'xy', 'overlap': True },
0x2b82: { 'align': 'r', 'stretch': 'xy', 'overlap': True },
0x2b83: { 'align': 'r', 'stretch': 'xy', 'overlap': True },
}
# Initial font dimensions
font_dim = {
'xmin' : 0,
'ymin' : -sourceFont.descent,
'xmax' : 0,
'ymax' : sourceFont.ascent,
'width' : 0,
'height': 0,
}
# Find the biggest char width and height
2014-12-05 06:29:54 +02:00
#
2015-02-21 03:21:52 +02:00
# 0x00-0x17f is the Latin Extended-A range
# 0x2500-0x2600 is the box drawing range
for glyph in range(0x00, 0x17f) + range(0x2500, 0x2600):
try:
(xmin, ymin, xmax, ymax) = sourceFont[glyph].boundingBox()
except TypeError:
continue
if font_dim['width'] == 0:
font_dim['width'] = sourceFont[glyph].width
if ymin < font_dim['ymin']: font_dim['ymin'] = ymin
if ymax > font_dim['ymax']: font_dim['ymax'] = ymax
if xmax > font_dim['xmax']: font_dim['xmax'] = xmax
# Calculate font height
font_dim['height'] = abs(font_dim['ymin']) + font_dim['ymax']
# Update the font encoding to ensure that the Unicode glyphs are available
sourceFont.encoding = 'ISO10646'
# Fetch this property before adding outlines
onlybitmaps = sourceFont.onlybitmaps
def get_dim(glyph):
bbox = glyph.boundingBox()
return {
'xmin' : bbox[0],
'ymin' : bbox[1],
'xmax' : bbox[2],
'ymax' : bbox[3],
2015-08-11 00:33:21 +02:00
2015-02-21 03:21:52 +02:00
'width' : bbox[2] + (-bbox[0]),
'height': bbox[3] + (-bbox[1]),
}
2015-02-25 21:45:58 +02:00
def copy_glyphs(sourceFont, sourceFontStart, sourceFontEnd, symbolFont, symbolFontStart, symbolFontEnd):
sourceFontList = []
sourceFontCounter = 0
for i in xrange(sourceFontStart, sourceFontEnd + 1):
sourceFontList.append(format(i, 'X'))
# Create glyphs from symbol font
symbolFont.selection.select(("ranges","unicode"),symbolFontStart,symbolFontEnd)
sourceFont.selection.select(("ranges","unicode"),sourceFontStart,sourceFontEnd)
for sym_glyph in symbolFont.selection.byGlyphs:
2015-08-11 00:33:21 +02:00
#sym_attr = SYM_ATTR[sym_glyph.unicode]
2015-03-01 18:24:05 +02:00
if args.quiet == False:
print "updating glyph: " + str(sym_glyph)
2015-02-25 21:45:58 +02:00
# convince that this string really is a hex:
currentSourceFontGlyph = int("0x" + sourceFontList[sourceFontCounter], 16)
# Prepare symbol glyph dimensions
sym_dim = get_dim(sym_glyph)
# Select and copy symbol from its encoding point
symbolFont.selection.select(sym_glyph.encoding)
symbolFont.copy()
# Select and paste symbol to its unicode code point
sourceFont.selection.select(currentSourceFontGlyph)
sourceFont.paste()
2015-08-11 00:33:21 +02:00
if args.single:
2015-03-01 18:24:05 +02:00
# Now that we have copy/pasted the glyph, it's time to scale and move it
# Handle glyph stretching
2015-08-11 00:33:21 +02:00
#if 'x' in sym_attr['stretch']:
# # Stretch the glyph horizontally
# scale_ratio = font_dim['width'] / sym_dim['width']
2015-02-25 21:45:58 +02:00
2015-03-01 18:24:05 +02:00
# sourceFont.transform(psMat.scale(scale_ratio, 1))
2015-08-11 00:33:21 +02:00
#if 'y' in sym_attr['stretch']:
# # Stretch the glyph vertically
# scale_ratio = font_dim['height'] / sym_dim['height']
2015-06-21 18:29:01 +02:00
2015-08-11 00:33:21 +02:00
# sourceFont.transform(psMat.scale(1, scale_ratio))
2015-06-21 18:29:01 +02:00
2015-08-11 00:33:21 +02:00
# Use the dimensions from the pasted and stretched glyph
sym_dim = get_dim(sourceFont[currentSourceFontGlyph])
2015-06-21 18:29:01 +02:00
2015-08-11 00:33:21 +02:00
# Center-align the glyph vertically
font_ycenter = font_dim['height'] / 2
sym_ycenter = sym_dim['height'] / 2
2015-06-21 18:29:01 +02:00
2015-08-11 00:33:21 +02:00
# First move it to the ymax (top)
sourceFont.transform(psMat.translate(0, font_dim['ymax'] - sym_dim['ymax']))
2015-06-21 18:29:01 +02:00
2015-08-11 00:33:21 +02:00
# Then move it the y center difference
sourceFont.transform(psMat.translate(0, sym_ycenter - font_ycenter))
2015-06-21 18:29:01 +02:00
2015-08-11 00:33:21 +02:00
# Ensure that the glyph doesn't extend outside the font's bounding box
if sym_dim['width'] > font_dim['width']:
# The glyph is too wide, scale it down to fit
scale_matrix = psMat.scale(font_dim['width'] / sym_dim['width'], 1)
2015-06-21 18:29:01 +02:00
2015-08-11 00:33:21 +02:00
sourceFont.transform(scale_matrix)
2015-06-21 18:29:01 +02:00
2015-08-11 00:33:21 +02:00
# Use the dimensions from the stretched glyph
sym_dim = get_dim(sourceFont[currentSourceFontGlyph])
2015-06-21 18:29:01 +02:00
2015-08-11 00:33:21 +02:00
# Handle glyph alignment
#if sym_attr['align'] == 'c':
# # Center align
# align_matrix = psMat.translate(font_dim['width'] / 2 - sym_dim['width'] / 2 , 0)
align_matrix = psMat.translate(font_dim['width'] / 2 - sym_dim['width'] / 2 , 0)
#elif sym_attr['align'] == 'r':
# # Right align
# align_matrix = psMat.translate(font_dim['width'] - sym_dim['width'], 0)
#else:
# No alignment (left alignment)
#align_matrix = psMat.translate(0, 0)
2015-06-21 18:29:01 +02:00
2015-08-11 00:33:21 +02:00
sourceFont.transform(align_matrix)
2015-06-21 18:29:01 +02:00
2015-08-11 00:33:21 +02:00
#if sym_attr['overlap'] is True:
# overlap_width = sourceFont.em / 48
2015-06-21 18:29:01 +02:00
2015-08-11 00:33:21 +02:00
# # Stretch the glyph slightly horizontally if it should overlap
# sourceFont.transform(psMat.scale((sym_dim['width'] + overlap_width) / sym_dim['width'], 1))
2015-06-21 18:29:01 +02:00
2015-08-11 00:33:21 +02:00
# if sym_attr['align'] == 'l':
# # The glyph should be left-aligned, so it must be moved overlap_width to the left
# # This only applies to left-aligned glyphs because the glyph is scaled to the right
# sourceFont.transform(psMat.translate(-overlap_width, 0))
2015-06-21 18:29:01 +02:00
2015-08-11 00:33:21 +02:00
# Ensure the font is considered monospaced on Windows
sourceFont[currentSourceFontGlyph].width = font_dim['width']
2015-02-21 03:21:52 +02:00
2015-02-25 21:45:58 +02:00
sourceFontCounter += 1
# reset selection so iteration works propertly @todo fix? rookie misunderstanding?
symbolFont.selection.select(("ranges","unicode"),symbolFontStart,symbolFontEnd)
# end for
return
2015-02-21 03:21:52 +02:00
2014-12-05 06:29:54 +02:00
2015-08-11 00:33:21 +02:00
copy_glyphs(sourceFont, sourceFontOriginalStart, sourceFontOriginalEnd, symbols, symbolsOriginalRangeStart, symbolsOriginalRangeEnd)
copy_glyphs(sourceFont, symbolsPowerlineRange1Start, symbolsPowerlineRange1End, powerlineSymbols, symbolsPowerlineRange1Start, symbolsPowerlineRange1End)
copy_glyphs(sourceFont, symbolsPowerlineRange2Start, symbolsPowerlineRange2End, powerlineSymbols, symbolsPowerlineRange2Start, symbolsPowerlineRange2End)
copy_glyphs(sourceFont, sourceFontDeviconsStart, sourceFontDeviconsEnd, symbolsDevicons, symbolsDeviconsRangeStart, symbolsDeviconsRangeEnd)
if args.fontawesome:
copy_glyphs(sourceFont, sourceFontFontAwesomeStart, sourceFontFontAwesomeEnd, fontawesome, symbolsFontAwesomeRangeStart, symbolsFontAwesomeRangeEnd)
if args.octicons:
copy_glyphs(sourceFont, sourceFontOcticonsStart, sourceFontOcticonsEnd, octicons, symbolsOcticonsRangeStart, symbolsOcticonsRangeEnd)
if args.pomicons:
copy_glyphs(sourceFont, sourceFontPomiconsStart, sourceFontPomiconsEnd, pomicons, symbolsPomiconsRangeStart, symbolsPomiconsRangeEnd)
2014-12-05 06:29:54 +02:00
extension = os.path.splitext(sourceFont.path)[1]
# @todo later add option to generate the sfd?
#sourceFont.save(sourceFont.fullname + ".sfd")
2015-08-21 19:17:55 +02:00
# the `PfEd-comments` flag is required for Fontforge to save
# '.comment' and '.fontlog'.
sourceFont.generate(sourceFont.fullname + extension, flags=('PfEd-comments',))
2014-12-05 06:29:54 +02:00
print "Generated"
print sourceFont.fullname