mirror of
https://github.com/ryanoasis/nerd-fonts.git
synced 2024-09-12 09:41:47 +02:00
#4 Using argparse for cli arguments and options for output quieting and conditional param for generating double vs single width glyphs
This commit is contained in:
parent
ca9fc22333
commit
a7b979e3fc
1 changed files with 73 additions and 63 deletions
136
font-patcher
136
font-patcher
|
@ -4,6 +4,7 @@ import sys
|
|||
import psMat
|
||||
import re
|
||||
import os.path
|
||||
import argparse
|
||||
|
||||
try:
|
||||
#Load the module
|
||||
|
@ -13,15 +14,29 @@ except ImportError:
|
|||
sys.stderr.write("FontForge module could not be loaded. Try installing fontforge python bindings\n")
|
||||
sys.exit(1)
|
||||
|
||||
|
||||
# argparse stuff
|
||||
|
||||
parser = argparse.ArgumentParser(description='Patches a given font with programming and web development related glyphs (mainly for vim-webdevicons)')
|
||||
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)
|
||||
args = parser.parse_args()
|
||||
|
||||
print "using fontforge package version: " + str(fontforge.__version__)
|
||||
|
||||
sourceFont = fontforge.open(sys.argv[1])
|
||||
additionalFontNameSuffix = " Plus Nerd File Types"
|
||||
|
||||
if args.single:
|
||||
additionalFontNameSuffix += " Mono"
|
||||
|
||||
sourceFont = fontforge.open(args.font)
|
||||
|
||||
# rename font
|
||||
fontname, style = re.match("^([^-]*)(?:(-.*))?$", sourceFont.fontname).groups()
|
||||
sourceFont.familyname = sourceFont.familyname + " Plus Nerd File Types"
|
||||
sourceFont.fullname = sourceFont.fullname + " Plus Nerd File Types"
|
||||
sourceFont.fontname = fontname + 'PlusNerdFileTypes'
|
||||
sourceFont.familyname = sourceFont.familyname + additionalFontNameSuffix
|
||||
sourceFont.fullname = sourceFont.fullname + additionalFontNameSuffix
|
||||
sourceFont.fontname = fontname + additionalFontNameSuffix.replace(" ", "")
|
||||
sourceFont.appendSFNTName('English (US)', 'Preferred Family', sourceFont.familyname)
|
||||
sourceFont.appendSFNTName('English (US)', 'Compatible Full', sourceFont.fullname)
|
||||
|
||||
|
@ -33,13 +48,13 @@ symbols = fontforge.open("glyph-source-fonts/original-source.otf")
|
|||
symbols2 = fontforge.open("glyph-source-fonts/devicons.ttf")
|
||||
|
||||
symbolsRangeStart = 0xE500
|
||||
symbolsRangeEnd = 0xE51D
|
||||
symbolsRangeEnd = 0xE521
|
||||
|
||||
symbols2RangeStart = 0xE600
|
||||
symbols2RangeEnd = 0xE6A4
|
||||
|
||||
sourceFontRange1Start = 0xE600
|
||||
sourceFontRange1End = 0xE61D
|
||||
sourceFontRange1End = 0xE621
|
||||
|
||||
sourceFontRange2Start = 0xE700
|
||||
sourceFontRange2End = 0xE7A4
|
||||
|
@ -114,13 +129,10 @@ def get_dim(glyph):
|
|||
|
||||
def copy_glyphs(sourceFont, sourceFontStart, sourceFontEnd, symbolFont, symbolFontStart, symbolFontEnd):
|
||||
|
||||
#sourceFontStart = 0xE600 # hex literal, gives us a regular integer
|
||||
#sourceFontEnd = 0xE61D
|
||||
sourceFontList = []
|
||||
sourceFontCounter = 0
|
||||
|
||||
for i in xrange(sourceFontStart, sourceFontEnd + 1):
|
||||
print format(i, 'X')
|
||||
sourceFontList.append(format(i, 'X'))
|
||||
|
||||
# Create glyphs from symbol font
|
||||
|
@ -130,14 +142,10 @@ def copy_glyphs(sourceFont, sourceFontStart, sourceFontEnd, symbolFont, symbolFo
|
|||
|
||||
for sym_glyph in symbolFont.selection.byGlyphs:
|
||||
#sym_attr = SYM_ATTR[sym_glyph.unicode]
|
||||
print "updating glyph: " + str(sym_glyph)
|
||||
print "updating glyph: " + str(sym_glyph.encoding)
|
||||
print "updating glyph: " + str(sym_glyph.unicode)
|
||||
#print sourceFontCounter
|
||||
#print sourceFontList[sourceFontCounter]
|
||||
if args.quiet == False:
|
||||
print "updating glyph: " + str(sym_glyph)
|
||||
# convince that this string really is a hex:
|
||||
currentSourceFontGlyph = int("0x" + sourceFontList[sourceFontCounter], 16)
|
||||
#print currentSourceFontGlyph
|
||||
|
||||
# Prepare symbol glyph dimensions
|
||||
sym_dim = get_dim(sym_glyph)
|
||||
|
@ -150,70 +158,72 @@ def copy_glyphs(sourceFont, sourceFontStart, sourceFontEnd, symbolFont, symbolFo
|
|||
sourceFont.selection.select(currentSourceFontGlyph)
|
||||
sourceFont.paste()
|
||||
|
||||
# Now that we have copy/pasted the glyph, it's time to scale and move it
|
||||
if args.single:
|
||||
# Now that we have copy/pasted the glyph, it's time to scale and move it
|
||||
|
||||
# Handle glyph stretching
|
||||
#if 'x' in sym_attr['stretch']:
|
||||
# # Stretch the glyph horizontally
|
||||
# scale_ratio = font_dim['width'] / sym_dim['width']
|
||||
# Handle glyph stretching
|
||||
#if 'x' in sym_attr['stretch']:
|
||||
# # Stretch the glyph horizontally
|
||||
# scale_ratio = font_dim['width'] / sym_dim['width']
|
||||
|
||||
# sourceFont.transform(psMat.scale(scale_ratio, 1))
|
||||
#if 'y' in sym_attr['stretch']:
|
||||
# # Stretch the glyph vertically
|
||||
# scale_ratio = font_dim['height'] / sym_dim['height']
|
||||
# sourceFont.transform(psMat.scale(scale_ratio, 1))
|
||||
#if 'y' in sym_attr['stretch']:
|
||||
# # Stretch the glyph vertically
|
||||
# scale_ratio = font_dim['height'] / sym_dim['height']
|
||||
|
||||
# sourceFont.transform(psMat.scale(1, scale_ratio))
|
||||
# sourceFont.transform(psMat.scale(1, scale_ratio))
|
||||
|
||||
# Use the dimensions from the pasted and stretched glyph
|
||||
sym_dim = get_dim(sourceFont[currentSourceFontGlyph])
|
||||
# Use the dimensions from the pasted and stretched glyph
|
||||
sym_dim = get_dim(sourceFont[currentSourceFontGlyph])
|
||||
|
||||
# Center-align the glyph vertically
|
||||
font_ycenter = font_dim['height'] / 2
|
||||
sym_ycenter = sym_dim['height'] / 2
|
||||
# Center-align the glyph vertically
|
||||
font_ycenter = font_dim['height'] / 2
|
||||
sym_ycenter = sym_dim['height'] / 2
|
||||
|
||||
# First move it to the ymax (top)
|
||||
sourceFont.transform(psMat.translate(0, font_dim['ymax'] - sym_dim['ymax']))
|
||||
# First move it to the ymax (top)
|
||||
sourceFont.transform(psMat.translate(0, font_dim['ymax'] - sym_dim['ymax']))
|
||||
|
||||
# Then move it the y center difference
|
||||
sourceFont.transform(psMat.translate(0, sym_ycenter - font_ycenter))
|
||||
# Then move it the y center difference
|
||||
sourceFont.transform(psMat.translate(0, sym_ycenter - font_ycenter))
|
||||
|
||||
# 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)
|
||||
# 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)
|
||||
|
||||
sourceFont.transform(scale_matrix)
|
||||
sourceFont.transform(scale_matrix)
|
||||
|
||||
# Use the dimensions from the stretched glyph
|
||||
sym_dim = get_dim(sourceFont[currentSourceFontGlyph])
|
||||
# Use the dimensions from the stretched glyph
|
||||
sym_dim = get_dim(sourceFont[currentSourceFontGlyph])
|
||||
|
||||
# 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)
|
||||
# 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)
|
||||
|
||||
sourceFont.transform(align_matrix)
|
||||
sourceFont.transform(align_matrix)
|
||||
|
||||
#if sym_attr['overlap'] is True:
|
||||
# overlap_width = sourceFont.em / 48
|
||||
#if sym_attr['overlap'] is True:
|
||||
# overlap_width = sourceFont.em / 48
|
||||
|
||||
# # Stretch the glyph slightly horizontally if it should overlap
|
||||
# sourceFont.transform(psMat.scale((sym_dim['width'] + overlap_width) / sym_dim['width'], 1))
|
||||
# # Stretch the glyph slightly horizontally if it should overlap
|
||||
# sourceFont.transform(psMat.scale((sym_dim['width'] + overlap_width) / sym_dim['width'], 1))
|
||||
|
||||
# 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))
|
||||
# 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))
|
||||
|
||||
# Ensure the font is considered monospaced on Windows
|
||||
sourceFont[currentSourceFontGlyph].width = font_dim['width']
|
||||
|
||||
# Ensure the font is considered monospaced on Windows
|
||||
sourceFont[currentSourceFontGlyph].width = font_dim['width']
|
||||
sourceFontCounter += 1
|
||||
# reset selection so iteration works propertly @todo fix? rookie misunderstanding?
|
||||
symbolFont.selection.select(("ranges","unicode"),symbolFontStart,symbolFontEnd)
|
||||
|
|
Loading…
Reference in a new issue