2014-12-05 06:29:54 +02:00
#!/usr/bin/env python2
2015-11-14 23:50:16 +02:00
# coding=utf8
2016-05-08 23:04:21 +02:00
# version: 0.8.0
2014-12-05 06:29:54 +02:00
2016-05-08 23:04:21 +02:00
version = "0.8.0"
2016-03-19 05:16:46 +02:00
projectName = "Nerd Fonts"
projectNameAbbreviation = "NF"
projectNameSingular = projectName[:-1]
2014-12-05 06:29:54 +02:00
import sys
2016-03-19 05:16:46 +02:00
try:
import psMat
except ImportError:
sys.stderr.write(projectName + ": FontForge module is probably not installed. [See: http://designwithfontforge.com/en-US/Installing_Fontforge.html]\n")
sys.exit(1)
2014-12-05 06:29:54 +02:00
import re
2015-11-16 01:05:57 +02:00
import os
2015-03-01 18:24:05 +02:00
import argparse
2015-11-16 01:05:57 +02:00
import errno
2014-12-05 06:29:54 +02:00
try:
2016-03-19 05:16:46 +02:00
#Load the module
import fontforge
2014-12-05 06:29:54 +02:00
except ImportError:
2016-03-19 05:16:46 +02:00
sys.stderr.write(projectName + ": FontForge module could not be loaded. Try installing fontforge python bindings [e.g. on Linux Debian or Ubuntu: `sudo apt-get install fontforge python-fontforge`]\n")
sys.exit(1)
2014-12-05 06:29:54 +02:00
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)')
2016-03-20 18:04:47 +02:00
parser.add_argument('-v', '--version', action='version', version=projectName + ": %(prog)s ("+version+")")
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)
2016-10-14 02:48:30 +02:00
parser.add_argument('-c', '--complete', dest='complete', action='store_true', help='Add all availale Glyphs', default=False)
2016-05-08 22:58:54 +02:00
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('--fontlinux', dest='fontlinux', action='store_true', help='Add Font Linux Glyphs (https://github.com/Lukas-W/font-linux)', default=False)
parser.add_argument('--octicons', dest='octicons', action='store_true', help='Add Octicons Glyphs (https://octicons.github.com)', default=False)
2015-08-11 00:33:21 +02:00
parser.add_argument('--pomicons', dest='pomicons', action='store_true', help='Add Pomicon Glyphs (https://github.com/gabrielelana/pomicons)', default=False)
2015-11-11 03:47:12 +02:00
parser.add_argument('--powerline', dest='powerline', action='store_true', help='Add Powerline Glyphs', default=False)
parser.add_argument('--powerlineextra', dest='powerlineextra', action='store_true', help='Add Powerline Glyphs (https://github.com/ryanoasis/powerline-extra-symbols)', default=False)
2015-11-14 23:39:15 +02:00
parser.add_argument('--careful', dest='careful', action='store_true', help='Do not overwrite existing glyphs if detected', default=False)
2016-10-13 05:12:08 +02:00
parser.add_argument('-ext', '--extension', type=str, nargs='?', dest='extension', help='Change type of font file to create (e.g. ttf, otf)', default="")
2015-11-16 01:05:57 +02:00
parser.add_argument('-out', '--outputdir', type=str, nargs='?', dest='outputdir', help='The directory to output the patched font file to', default=".")
2015-03-01 18:24:05 +02:00
args = parser.parse_args()
2015-08-21 19:17:55 +02:00
changelog = open("changelog.md", "r")
2015-04-21 16:10:25 +02:00
minimumVersion = 20141231
actualVersion = int(fontforge.version())
2016-03-19 05:16:46 +02:00
# un-comment following line for testing invalid version error handling
#actualVersion = 20120731
2015-11-14 23:50:16 +02:00
# versions tested: 20150612, 20150824
2015-04-21 16:10:25 +02:00
if actualVersion < minimumVersion:
2016-03-19 05:16:46 +02:00
print projectName + ": You seem to be using an unsupported (old) version of fontforge: " + str(actualVersion)
print projectName + ": Please use at least version: " + str(minimumVersion)
2015-04-21 16:10:25 +02:00
sys.exit(1)
2014-12-05 06:29:54 +02:00
2016-03-19 05:16:46 +02:00
verboseAdditionalFontNameSuffix = " " + projectNameSingular
2015-08-11 00:33:21 +02:00
2016-10-14 02:48:30 +02:00
if args.complete:
args.fontawesome = True
args.fontlinux = True
args.octicons = True
args.pomicons = True
args.powerline = True
args.powerlineextra = True
2015-08-11 00:33:21 +02:00
if args.windows:
# attempt to shorten here on the additional name BEFORE trimming later
2016-03-19 05:16:46 +02:00
additionalFontNameSuffix = " " + projectNameAbbreviation
2015-08-11 00:33:21 +02:00
else:
additionalFontNameSuffix = verboseAdditionalFontNameSuffix
2015-03-01 18:24:05 +02:00
2015-08-11 00:33:21 +02:00
if args.fontawesome:
2016-10-15 08:19:10 +02:00
additionalFontNameSuffix += " A"
2015-08-11 00:33:21 +02:00
verboseAdditionalFontNameSuffix += " Plus Font Awesome"
if args.octicons:
2016-10-15 08:19:10 +02:00
additionalFontNameSuffix += " O"
2015-08-11 00:33:21 +02:00
verboseAdditionalFontNameSuffix += " Plus Octicons"
if args.pomicons:
2016-10-15 08:19:10 +02:00
additionalFontNameSuffix += " P"
2015-08-11 00:33:21 +02:00
verboseAdditionalFontNameSuffix += " Plus Pomicons"
2016-05-08 22:58:54 +02:00
if args.fontlinux:
2016-10-15 08:19:10 +02:00
additionalFontNameSuffix += " L"
2016-05-08 22:58:54 +02:00
verboseAdditionalFontNameSuffix += " Plus Font Linux"
2015-11-14 23:49:18 +02:00
# if all source glyphs included simplify the name
2016-05-08 22:58:54 +02:00
if args.fontawesome and args.octicons and args.pomicons and args.powerlineextra and args.fontlinux:
2016-10-15 08:19:10 +02:00
additionalFontNameSuffix = " " + projectNameSingular + " C"
2016-03-19 05:15:27 +02:00
verboseAdditionalFontNameSuffix = " " + projectNameSingular + " Complete"
2015-11-14 23:49:18 +02:00
# add mono signifier to end of name
if args.single:
2016-10-15 08:19:10 +02:00
additionalFontNameSuffix += " M"
verboseAdditionalFontNameSuffix += " M"
2015-11-14 23:49:18 +02:00
2015-03-01 18:24:05 +02:00
sourceFont = fontforge.open(args.font)
2014-12-05 06:29:54 +02:00
2016-03-19 05:15:27 +02:00
# basically split the font name around the dash "-" to get the fontname and the style (e.g. Bold)
# this does not seem very reliable so only use the style here as a fallback if the font does not
# have an internal style defined (in sfnt_names)
# using '([^-]*?)' to get the item before the first dash "-"
# using '([^-]*(?!.*-))' to get the item after the last dash "-"
fontname, fallbackStyle = re.match("^([^-]*).*?([^-]*(?!.*-))$", sourceFont.fontname).groups()
# dont trust 'sourceFont.familyname'
familyname = fontname
2015-08-11 00:33:21 +02:00
# fullname (filename) can always use long/verbose font name, even in windows
fullname = sourceFont.fullname + verboseAdditionalFontNameSuffix
2016-03-19 20:08:07 +02:00
fontname = fontname + additionalFontNameSuffix.replace(" ", "")
2015-08-11 00:33:21 +02:00
2016-03-19 05:15:27 +02:00
# let us try to get the 'style' from the font info in sfnt_names and fallback to the
# parse fontname if it fails:
try:
# search tuple:
subFamilyTupleIndex = [x[1] for x in sourceFont.sfnt_names].index("SubFamily")
# String ID is at the second index in the Tuple lists
sfntNamesStringIDIndex = 2
# now we have the correct item:
subFamily = sourceFont.sfnt_names[sfntNamesStringIDIndex][subFamilyTupleIndex]
except IndexError:
print projectName + ": Could not find 'SubFamily' for given font, falling back to parsed fontname"
subFamily = fallbackStyle
# some fonts have inaccurate 'SubFamily', if it is Regular let us trust the filename more:
if subFamily == "Regular":
subFamily = fallbackStyle
2015-08-11 00:33:21 +02:00
if args.windows:
2016-10-15 08:37:45 +02:00
maxFamilyLength = 31
maxFontLength = maxFamilyLength-len('-' + subFamily)
2016-03-19 05:15:27 +02:00
familyname += " " + projectNameAbbreviation
2015-08-11 00:33:21 +02:00
fullname += " Windows Compatible"
# now make sure less than 32 characters name length
2016-10-15 08:37:45 +02:00
if len(fontname) > maxFontLength:
2015-08-11 00:33:21 +02:00
fontname = fontname[:maxLength]
2016-10-15 08:37:45 +02:00
if len(familyname) > maxFamilyLength:
2015-08-11 00:33:21 +02:00
familyname = familyname[:maxLength]
2016-03-19 05:15:27 +02:00
else:
familyname += " " + projectNameSingular
2015-08-11 00:33:21 +02:00
2016-10-14 18:23:56 +02:00
# Don't truncate the subfamily to keep fontname unique. MacOS treats fonts with
# the same name as the same font, even if subFamily is different.
fontname += '-' + subFamily
2015-08-11 00:33:21 +02:00
# rename font
def replace_all(text, dic):
for i, j in dic.iteritems():
text = text.replace(i, j)
return text
2015-11-11 03:47:12 +02:00
2015-11-16 01:05:57 +02:00
def make_sure_path_exists(path):
try:
os.makedirs(path)
except OSError as exception:
if exception.errno != errno.EEXIST:
2016-10-14 02:48:30 +02:00
raise
2015-11-16 01:05:57 +02:00
make_sure_path_exists(args.outputdir)
2015-08-11 00:33:21 +02:00
# comply with SIL Open Font License (OFL)
2015-09-19 17:40:53 +02:00
reservedFontNameReplacements = { 'source': 'sauce', 'Source': 'Sauce', 'hermit': 'hurmit', 'Hermit': 'Hurmit', 'fira': 'fura', 'Fira': 'Fura', 'hack': 'knack', 'Hack': 'Knack' }
2015-08-11 00:33:21 +02:00
2016-03-19 05:16:46 +02:00
projectInfo = "Patched with '" + projectName + " Patcher' (https://github.com/ryanoasis/nerd-fonts)"
2015-08-21 19:17:55 +02:00
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)
2016-03-19 05:15:27 +02:00
sourceFont.appendSFNTName('English (US)', 'SubFamily', subFamily)
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
2016-03-19 05:16:46 +02:00
sourceFont.version += ";" + projectName + " " + version
2015-08-21 19:17:55 +02:00
#print "Version now is " + sourceFont.version
2015-08-11 00:33:21 +02:00
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")
2015-11-11 03:47:12 +02:00
powerlineExtraSymbols = fontforge.open("glyph-source-fonts/PowerlineExtraSymbols.otf")
2015-08-11 00:33:21 +02:00
symbolsDevicons = fontforge.open("glyph-source-fonts/devicons.ttf")
2015-08-21 19:16:20 +02:00
symbols.em = sourceFont.em
symbolsDevicons.em = sourceFont.em
2016-10-13 05:12:08 +02:00
powerlineSymbols.em = sourceFont.em
powerlineExtraSymbols.em = sourceFont.em
2015-08-21 19:16:20 +02:00
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-09-19 19:56:49 +02:00
octiconsExactEncodingPosition = True
2015-08-21 19:16:20 +02:00
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
2016-05-08 22:58:54 +02:00
if args.fontlinux:
fontlinux = fontforge.open("glyph-source-fonts/font-linux.ttf")
fontlinux.em = sourceFont.em
fontlinuxExactEncodingPosition = True
2015-09-19 19:56:49 +02:00
# Prevent glyph encoding position conflicts between glyph sets
if args.fontawesome and args.octicons:
octiconsExactEncodingPosition = False
2016-05-08 22:58:54 +02:00
if args.fontawesome or args.octicons:
fontlinuxExactEncodingPosition = False
2015-08-21 19:16:20 +02:00
# Define the character ranges
2015-09-02 22:21:13 +02:00
# Symbol font ranges
2016-10-15 08:19:10 +02:00
2015-09-02 22:21:13 +02:00
symbolsPomiconsRangeStart = 0xE000
symbolsPomiconsRangeEnd = 0xE00A
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-11-11 03:47:12 +02:00
symbolsPowerlineExtraRange1Start = 0xE0A3
symbolsPowerlineExtraRange1End = 0xE0A3
symbolsPowerlineExtraRange2Start = 0xE0B4
symbolsPowerlineExtraRange2End = 0xE0C8
symbolsPowerlineExtraRange3Start = 0xE0CC
symbolsPowerlineExtraRange3End = 0xE0D4
2015-11-14 23:49:18 +02:00
symbolsOriginalRangeStart = 0xE4FA
2015-09-02 22:21:13 +02:00
symbolsOriginalRangeEnd = 0xE52A
2015-08-11 00:33:21 +02:00
symbolsDeviconsRangeStart = 0xE600
symbolsDeviconsRangeEnd = 0xE6C5
symbolsFontAwesomeRangeStart = 0xF000
2016-03-19 20:27:18 +02:00
symbolsFontAwesomeRangeEnd = 0xF295
2015-08-11 00:33:21 +02:00
symbolsOcticonsRangeStart = 0xF000
symbolsOcticonsRangeEnd = 0xF0DB
2016-05-08 22:58:54 +02:00
symbolsFontLinuxRangeStart = 0xF100
symbolsFontLinuxRangeEnd = 0xF115
2015-09-02 22:21:13 +02:00
# Destination font ranges
sourceFontPomiconsStart = 0xE000
sourceFontPomiconsEnd = 0xE00A
2015-11-14 23:49:18 +02:00
sourceFontOriginalStart = 0xE5FA
2015-08-11 00:33:21 +02:00
sourceFontOriginalEnd = 0xE62A
sourceFontDeviconsStart = 0xE700
sourceFontDeviconsEnd = 0xE7C5
sourceFontFontAwesomeStart = 0xF000
2016-03-19 20:27:18 +02:00
sourceFontFontAwesomeEnd = 0xF295
2015-08-11 00:33:21 +02:00
sourceFontOcticonsStart = 0xF400
sourceFontOcticonsEnd = 0xF4DB
2016-05-08 22:58:54 +02:00
sourceFontFontLinuxStart = 0xF300
sourceFontFontLinuxEnd = 0xF315
2016-10-15 08:19:10 +02:00
# To keep the size of glyphs constant when scaling, choose a larger
# sized glyph from each symbol font to set the scaling amount
symbolsPomiconsScaleGlyph = 0xE000 # first symbol
symbolsPowerlineScaleGlyph = 0xE0A0 # scm branch symbol
symbolsPowerlineExtraScaleGlyph = 0xE70E # Android logo
symbolsDeviconsScaleGlyph = 0xE60E # Android logo
symbolsFontAwesomeScaleGlyph = 0xF17A # Windows logo
symbolsOcticonsScaleGlyph = 0xF02E # magnifying glass
symbolsFontLinuxScaleGlyph = 0xF10E # Ubuntu logo
2015-02-21 03:21:52 +02:00
SYM_ATTR = {
2016-10-14 02:48:30 +02:00
# 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 },
# Powerline dividers
# Arrow tips
0xe0b0: { 'align': 'l', 'stretch': 'xy', 'overlap': False },
0xe0b1: { 'align': 'l', 'stretch': 'xy', 'overlap': False },
0xe0b2: { 'align': 'r', 'stretch': 'xy', 'overlap': False },
0xe0b3: { 'align': 'r', 'stretch': 'xy', 'overlap': False },
# Rounded arcs
0xe0b4: { 'align': 'l', 'stretch': 'xy', 'overlap': False },
0xe0b5: { 'align': 'l', 'stretch': 'xy', 'overlap': False },
0xe0b6: { 'align': 'r', 'stretch': 'xy', 'overlap': False },
0xe0b7: { 'align': 'r', 'stretch': 'xy', 'overlap': False },
# Bottom Triangles
0xe0b8: { 'align': 'l', 'stretch': 'xy', 'overlap': False },
0xe0b9: { 'align': 'l', 'stretch': 'xy', 'overlap': False },
0xe0ba: { 'align': 'r', 'stretch': 'xy', 'overlap': False },
0xe0bb: { 'align': 'r', 'stretch': 'xy', 'overlap': False },
# Top Triangles
0xe0bc: { 'align': 'l', 'stretch': 'xy', 'overlap': False },
0xe0bd: { 'align': 'l', 'stretch': 'xy', 'overlap': False },
0xe0be: { 'align': 'r', 'stretch': 'xy', 'overlap': False },
0xe0bf: { 'align': 'r', 'stretch': 'xy', 'overlap': False },
# Flames
0xe0c0: { 'align': 'l', 'stretch': 'xy', 'overlap': False },
0xe0c1: { 'align': 'l', 'stretch': 'xy', 'overlap': False },
0xe0c2: { 'align': 'r', 'stretch': 'xy', 'overlap': False },
0xe0c3: { 'align': 'r', 'stretch': 'xy', 'overlap': False },
# Small squares
0xe0c4: { 'align': 'l', 'stretch': 'xy', 'overlap': False },
0xe0c5: { 'align': 'r', 'stretch': 'xy', 'overlap': False },
# Bigger squares
0xe0c6: { 'align': 'l', 'stretch': 'xy', 'overlap': False },
0xe0c7: { 'align': 'r', 'stretch': 'xy', 'overlap': False },
# Waveform
0xe0c8: { 'align': 'l', 'stretch': 'xy', 'overlap': False },
# Hexagons
0xe0cc: { 'align': 'l', 'stretch': 'xy', 'overlap': False },
0xe0cd: { 'align': 'l', 'stretch': 'xy', 'overlap': False },
# Legos
0xe0ce: { 'align': 'l', 'stretch': 'xy', 'overlap': False },
0xe0cf: { 'align': 'c', 'stretch': 'xy', 'overlap': False },
0xe0d1: { 'align': 'l', 'stretch': 'xy', 'overlap': False },
# Top and bottom trapezoid
0xe0d2: { 'align': 'l', 'stretch': 'xy', 'overlap': False },
0xe0d4: { 'align': 'r', 'stretch': 'xy', 'overlap': False },
2015-02-21 03:21:52 +02:00
}
2016-10-13 05:12:08 +02:00
SYM_ATTR_DEFAULT = {
2016-10-14 02:48:30 +02:00
# 'pa' == preserve aspect ratio
'align': 'c', 'stretch': 'pa', 'overlap': False
}
2015-02-21 03:21:52 +02:00
# Initial font dimensions
font_dim = {
2016-10-15 08:19:10 +02:00
# Use os2_winXXXXXX for ymin and ymax as this is typically what is used for line size
2016-10-14 02:48:30 +02:00
'xmin' : 0,
'ymin' : -sourceFont.os2_windescent,
'xmax' : 0,
'ymax' : sourceFont.os2_winascent,
'width' : 0,
'height': 0,
2015-02-21 03:21:52 +02:00
}
# 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):
2016-10-14 02:48:30 +02:00
try:
(xmin, ymin, xmax, ymax) = sourceFont[glyph].boundingBox()
except TypeError:
continue
2015-02-21 03:21:52 +02:00
2016-10-14 02:48:30 +02:00
if font_dim['width'] == 0:
font_dim['width'] = sourceFont[glyph].width
2015-02-21 03:21:52 +02:00
2016-10-14 02:48:30 +02:00
if font_dim['width'] < sourceFont[glyph].width:
font_dim['width'] = sourceFont[glyph].width
2016-10-13 05:12:08 +02:00
2016-10-14 02:48:30 +02:00
# Ignore the y-values, os2_winXXXXX values set above are more accurate
# 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
2015-02-21 03:21:52 +02:00
# 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):
2016-10-14 02:48:30 +02:00
bbox = glyph.boundingBox()
2015-02-21 03:21:52 +02:00
2016-10-14 02:48:30 +02:00
return {
'xmin' : bbox[0],
'ymin' : bbox[1],
'xmax' : bbox[2],
'ymax' : bbox[3],
2015-02-21 03:21:52 +02:00
2016-10-14 02:48:30 +02:00
'width' : bbox[2] + (-bbox[0]),
'height': bbox[3] + (-bbox[1]),
}
2015-02-25 21:45:58 +02:00
2016-10-14 09:35:44 +02:00
def set_width(sourceFont, width):
sourceFont.selection.all()
for glyph in sourceFont.selection.byGlyphs:
glyph.width = width
2016-10-15 08:19:10 +02:00
def get_scale_factor(font_dim, sym_dim):
# We want to preserve x/y aspect ratio, so find biggest scale factor that allows symbol to fit
scale_ratio_x = font_dim['width'] / sym_dim['width']
# font_dim['height'] represents total line height, keep our symbols sized based upon font's em
scale_ratio_y = sourceFont.em / sym_dim['height']
if scale_ratio_x > scale_ratio_y:
return scale_ratio_y
else:
return scale_ratio_x
def copy_glyphs(sourceFont, sourceFontStart, sourceFontEnd, symbolFont, symbolFontStart, symbolFontEnd, exactEncoding=False, scaleGlyph=None):
2015-09-19 19:56:49 +02:00
if exactEncoding is False:
sourceFontList = []
sourceFontCounter = 0
2015-02-25 21:45:58 +02:00
2015-09-19 19:56:49 +02:00
for i in xrange(sourceFontStart, sourceFontEnd + 1):
sourceFontList.append(format(i, 'X'))
2015-02-25 21:45:58 +02:00
2016-10-15 08:19:10 +02:00
scale_factor = 0
if scaleGlyph:
sym_dim = get_dim(symbolFont[scaleGlyph])
scale_factor = get_scale_factor(font_dim, sym_dim)
2015-02-25 21:45:58 +02:00
2016-10-15 08:19:10 +02:00
# Create glyphs from symbol font
2015-02-25 21:45:58 +02:00
symbolFont.selection.select(("ranges","unicode"),symbolFontStart,symbolFontEnd)
sourceFont.selection.select(("ranges","unicode"),sourceFontStart,sourceFontEnd)
for sym_glyph in symbolFont.selection.byGlyphs:
2016-10-14 02:48:30 +02:00
try:
sym_attr = SYM_ATTR[sym_glyph.unicode]
except KeyError:
sym_attr = SYM_ATTR_DEFAULT
2016-03-19 05:16:46 +02:00
2016-10-14 02:48:30 +02:00
glyphName = sym_glyph.glyphname
2016-10-13 05:12:08 +02:00
2016-10-14 02:48:30 +02:00
if exactEncoding:
# use the exact same hex values for the source font as for the symbol font
currentSourceFontGlyph = sym_glyph.encoding
copiedToSlot = str(sym_glyph.unicode)
else:
# use source font defined hex values based on passed in start and end
# convince that this string really is a hex:
currentSourceFontGlyph = int("0x" + sourceFontList[sourceFontCounter], 16)
copiedToSlot = sourceFontList[sourceFontCounter]
2015-09-19 19:56:49 +02:00
2016-10-14 02:48:30 +02:00
if args.quiet == False:
print "updating glyph: " + str(sym_glyph) + " " + str(sym_glyph.glyphname) + " putting at: " + str(copiedToSlot)
2015-11-14 23:39:15 +02:00
2016-10-14 02:48:30 +02:00
# Prepare symbol glyph dimensions
sym_dim = get_dim(sym_glyph)
2015-02-25 21:45:58 +02:00
2016-10-14 02:48:30 +02:00
# Select and copy symbol from its encoding point
symbolFont.selection.select(sym_glyph.encoding)
symbolFont.copy()
2015-03-01 18:24:05 +02:00
2016-10-14 02:48:30 +02:00
# check it
if args.careful:
if copiedToSlot.startswith("uni"):
copiedToSlot = copiedToSlot[3:]
codepoint = int("0x" + copiedToSlot, 16)
try:
sourceFont[codepoint]
except TypeError:
# nothing there go ahead and paste at this codepoint
sourceFont.selection.select(currentSourceFontGlyph)
sourceFont.paste()
else:
sourceFont.selection.select(currentSourceFontGlyph)
sourceFont.paste()
sourceFont[currentSourceFontGlyph].glyphname = glyphName
2016-10-15 08:19:10 +02:00
# Now that we have copy/pasted the glyph, if we are creating a monospace
# font we need to scale and move the glyphs. It is possible to have
# empty glyphs, so we need to skip those.
2016-10-14 02:48:30 +02:00
if args.single and sym_dim['width'] and sym_dim['height']:
scale_ratio_x = 1
scale_ratio_y = 1
2016-10-15 08:19:10 +02:00
# If we want to preserve that aspect ratio of the glyphs we need to
# find the largest possible scaling factor that will allow the glyph
# to fit in both the x and y directions
2016-10-14 02:48:30 +02:00
if sym_attr['stretch'] == 'pa':
2016-10-15 08:19:10 +02:00
if scale_factor:
# We want to preserve the relative size of each glyph to other glyphs
# in the same symbol font.
scale_ratio_x = scale_factor
scale_ratio_y = scale_factor
2016-10-14 02:48:30 +02:00
else:
2016-10-15 08:19:10 +02:00
# In this case, each glyph is sized independantly to each other
scale_ratio_x = get_scale_factor(font_dim, sym_dim)
2016-10-14 02:48:30 +02:00
scale_ratio_y = scale_ratio_x
else:
if 'x' in sym_attr['stretch']:
2016-10-15 08:19:10 +02:00
# Stretch the glyph horizontally to fit the entire available width
2016-10-13 05:12:08 +02:00
scale_ratio_x = font_dim['width'] / sym_dim['width']
2016-10-14 02:48:30 +02:00
if 'y' in sym_attr['stretch']:
# Stretch the glyph vertically to total line height (good for powerline separators)
2016-10-13 05:12:08 +02:00
scale_ratio_y = font_dim['height'] / sym_dim['height']
2016-10-14 02:48:30 +02:00
if scale_ratio_x != 1 or scale_ratio_y != 1:
2016-10-13 05:12:08 +02:00
sourceFont.transform(psMat.scale(scale_ratio_x, scale_ratio_y))
2015-06-21 18:29:01 +02:00
2016-10-15 08:19:10 +02:00
# Use the dimensions from the newly pasted and stretched glyph
2016-10-14 02:48:30 +02:00
sym_dim = get_dim(sourceFont[currentSourceFontGlyph])
2015-06-21 18:29:01 +02:00
2016-10-15 08:19:10 +02:00
# Center the symbol vertically by matching the center of the line height and center of symbol
2016-10-14 02:48:30 +02:00
sym_ycenter = sym_dim['ymax'] - (sym_dim['height'] / 2)
font_ycenter = font_dim['ymax'] - (font_dim['height'] / 2)
2016-10-15 08:19:10 +02:00
y_align_distance = font_ycenter - sym_ycenter
2016-10-13 05:12:08 +02:00
2016-10-15 08:19:10 +02:00
# Handle glyph l/r/c alignment
2016-10-13 05:12:08 +02:00
2016-10-15 08:19:10 +02:00
# First find the baseline x-alignment (left alignment amount)
x_align_distance = font_dim['xmin']-sym_dim['xmin']
2016-10-14 02:48:30 +02:00
if sym_attr['align'] == 'c':
# Center align
2016-10-15 08:19:10 +02:00
x_align_distance += (font_dim['width']/2) - (sym_dim['width']/2)
2016-10-14 02:48:30 +02:00
elif sym_attr['align'] == 'r':
# Right align
2016-10-15 08:19:10 +02:00
x_align_distance += font_dim['width'] - sym_dim['width']
2015-06-21 18:29:01 +02:00
2016-10-15 08:19:10 +02:00
align_matrix = psMat.translate(x_align_distance, y_align_distance)
2016-10-14 02:48:30 +02:00
sourceFont.transform(align_matrix)
2015-06-21 18:29:01 +02:00
2016-10-14 02:48:30 +02:00
if sym_attr['overlap'] is True:
overlap_width = sourceFont.em / 48
2015-06-21 18:29:01 +02:00
2016-10-14 02:48:30 +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
2016-10-14 02:48:30 +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
2016-10-14 08:00:59 +02:00
if args.single:
2016-10-15 08:19:10 +02:00
# Ensure the font is considered monospaced on Windows by setting the same
# width for all character glyphs.
# This needs to be done for all glyphs, even the ones that are empty and
# didn't go through the scaling operations.
2016-10-14 02:48:30 +02:00
sourceFont[currentSourceFontGlyph].width = font_dim['width']
2015-02-21 03:21:52 +02:00
2016-10-14 02:48:30 +02:00
if exactEncoding is False:
sourceFontCounter += 1
2015-09-19 19:56:49 +02:00
2016-10-14 02:48:30 +02:00
# reset selection so iteration works propertly @todo fix? rookie misunderstanding?
2016-10-15 08:19:10 +02:00
# This is likely needed because the selection was changed when the glyph was copy/pasted
2016-10-14 02:48:30 +02:00
symbolFont.selection.select(("ranges","unicode"),symbolFontStart,symbolFontEnd)
2015-02-25 21:45:58 +02:00
# end for
return
2015-02-21 03:21:52 +02:00
2016-10-14 09:35:44 +02:00
if args.extension == "":
extension = os.path.splitext(sourceFont.path)[1]
else:
extension = '.'+args.extension
if args.single and extension == '.ttf':
# Force width to be equal on all glyphs to ensure the font is considered monospaced on Windows.
2016-10-15 08:19:10 +02:00
# This needs to be done on all characters, as some information seems to be lost from the original font file.
# This is only a problem with ttf files, otf files seem to be okay.
2016-10-14 09:35:44 +02:00
set_width(sourceFont, font_dim['width'])
2015-08-11 00:33:21 +02:00
copy_glyphs(sourceFont, sourceFontOriginalStart, sourceFontOriginalEnd, symbols, symbolsOriginalRangeStart, symbolsOriginalRangeEnd)
2016-10-15 08:19:10 +02:00
copy_glyphs(sourceFont, sourceFontDeviconsStart, sourceFontDeviconsEnd, symbolsDevicons, symbolsDeviconsRangeStart, symbolsDeviconsRangeEnd, scaleGlyph=symbolsDeviconsScaleGlyph)
2015-08-11 00:33:21 +02:00
2015-09-02 22:21:13 +02:00
if args.powerline:
2016-10-15 08:19:10 +02:00
copy_glyphs(sourceFont, symbolsPowerlineRange1Start, symbolsPowerlineRange1End, powerlineSymbols, symbolsPowerlineRange1Start, symbolsPowerlineRange1End, scaleGlyph=symbolsPowerlineScaleGlyph)
copy_glyphs(sourceFont, symbolsPowerlineRange2Start, symbolsPowerlineRange2End, powerlineSymbols, symbolsPowerlineRange2Start, symbolsPowerlineRange2End, scaleGlyph=symbolsPowerlineScaleGlyph)
2015-09-02 22:21:13 +02:00
2015-11-11 03:47:12 +02:00
if args.powerlineextra:
2016-10-15 08:19:10 +02:00
copy_glyphs(sourceFont, symbolsPowerlineExtraRange1Start, symbolsPowerlineExtraRange1End, powerlineExtraSymbols, symbolsPowerlineExtraRange1Start, symbolsPowerlineExtraRange1End, True, scaleGlyph=symbolsPowerlineExtraScaleGlyph)
copy_glyphs(sourceFont, symbolsPowerlineExtraRange2Start, symbolsPowerlineExtraRange2End, powerlineExtraSymbols, symbolsPowerlineExtraRange2Start, symbolsPowerlineExtraRange2End, True, scaleGlyph=symbolsPowerlineExtraScaleGlyph)
copy_glyphs(sourceFont, symbolsPowerlineExtraRange3Start, symbolsPowerlineExtraRange3End, powerlineExtraSymbols, symbolsPowerlineExtraRange3Start, symbolsPowerlineExtraRange3End, True, scaleGlyph=symbolsPowerlineExtraScaleGlyph)
2015-09-02 22:21:13 +02:00
2015-08-11 00:33:21 +02:00
if args.fontawesome:
2016-10-15 08:19:10 +02:00
copy_glyphs(sourceFont, sourceFontFontAwesomeStart, sourceFontFontAwesomeEnd, fontawesome, symbolsFontAwesomeRangeStart, symbolsFontAwesomeRangeEnd, True, scaleGlyph=symbolsFontAwesomeScaleGlyph)
2015-08-11 00:33:21 +02:00
if args.octicons:
2016-10-15 08:19:10 +02:00
copy_glyphs(sourceFont, sourceFontOcticonsStart, sourceFontOcticonsEnd, octicons, symbolsOcticonsRangeStart, symbolsOcticonsRangeEnd, octiconsExactEncodingPosition, scaleGlyph=symbolsOcticonsScaleGlyph)
2015-08-11 00:33:21 +02:00
if args.pomicons:
2016-10-15 08:19:10 +02:00
copy_glyphs(sourceFont, sourceFontPomiconsStart, sourceFontPomiconsEnd, pomicons, symbolsPomiconsRangeStart, symbolsPomiconsRangeEnd, scaleGlyph=symbolsPomiconsScaleGlyph)
2014-12-05 06:29:54 +02:00
2016-05-08 22:58:54 +02:00
if args.fontlinux:
2016-10-15 08:19:10 +02:00
copy_glyphs(sourceFont, sourceFontFontLinuxStart, sourceFontFontLinuxEnd, fontlinux, symbolsFontLinuxRangeStart, symbolsFontLinuxRangeEnd, fontlinuxExactEncodingPosition, scaleGlyph=symbolsFontLinuxScaleGlyph)
2016-05-08 22:58:54 +02:00
2015-08-21 19:17:55 +02:00
# the `PfEd-comments` flag is required for Fontforge to save
# '.comment' and '.fontlog'.
2015-11-16 01:05:57 +02:00
sourceFont.generate(args.outputdir + "/" + sourceFont.fullname + extension, flags=('opentype', 'PfEd-comments'))
2014-12-05 06:29:54 +02:00
print "Generated"
print sourceFont.fullname