Support custom symbol font and specify which glyphs use fixed scale factor

This commit is contained in:
Marcus Kellerman 2016-10-24 17:05:15 -07:00
parent 994267d94c
commit 96eafa9bb9

View file

@ -43,6 +43,7 @@ parser.add_argument('--octicons', dest='octicons', action='store_true', help='Ad
parser.add_argument('--pomicons', dest='pomicons', action='store_true', help='Add Pomicon Glyphs (https://github.com/gabrielelana/pomicons)', default=False)
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)
parser.add_argument('--custom', type=str, nargs='?', dest='custom', help='Specify a custom symbol font. All new glyphs will be copied, with no scaling applied.', default=False)
parser.add_argument('--careful', dest='careful', action='store_true', help='Do not overwrite existing glyphs if detected', default=False)
parser.add_argument('-ext', '--extension', type=str, nargs='?', dest='extension', help='Change type of font file to create (e.g. ttf, otf)', default="")
parser.add_argument('-out', '--outputdir', type=str, nargs='?', dest='outputdir', help='The directory to output the patched font file to', default=".")
@ -262,6 +263,16 @@ SYM_ATTR_DEFAULT = {
'default': { 'align': 'c', 'valign': 'c', 'stretch': 'pa', 'params': '' },
}
CUSTOM_ATTR = {
# 'pa' == preserve aspect ratio
'default': { 'align': 'c', 'valign': '', 'stretch': '', 'params': '' },
}
# Most glyphs we want to maximize during the scale. However, there are some
# that need to be small or stay relative in size to each other.
# The following list are those glyphs. A tuple represents a range.
SCALE_GLYPH_LIST = [ 0xe621, (0xe7bd, 0xe7c3), 0xf005, 0xf006, (0xf026, 0xf028), 0xf02b, 0xf02c, (0xf031, 0xf035), (0xf044, 0xf054), (0xf060, 0xf063), 0xf077, 0xf078, 0xf07d, 0xf07e, 0xf089, (0xf0d7, 0xf0da), (0xf0dc, 0xf0de), (0xf100, 0xf107), 0xf141, 0xf142, (0xf153, 0xf15a), (0xf175, 0xf178), 0xf182, 0xf183, (0xf221, 0xf22d), (0xf255, 0xf25b), (0xf431, 0xf434), 0xf438, (0xf443, 0xf445), 0xf44a, 0xf44b, 0xf44f, 0xf45d, 0xf461, (0xf479, 0xf47f), 0xf48b, ]
# Define the character ranges
# Symbol font ranges
@ -277,6 +288,7 @@ PATCH_SET = [
{ 'Enabled': args.fontawesome, 'Filename': "FontAwesome.otf", 'Exact': True, 'SymStart': 0xF000, 'SymEnd': 0xF295, 'SrcStart': None, 'SrcEnd': None, 'ScaleGlyph': 0xF17A,'Attributes': SYM_ATTR_DEFAULT }, # Windows logo
{ 'Enabled': args.fontlinux, 'Filename': "font-linux.ttf", 'Exact': fontlinuxExactEncodingPosition, 'SymStart': 0xF100, 'SymEnd': 0xF115, 'SrcStart': 0xF300, 'SrcEnd': 0xF315, 'ScaleGlyph': 0xF10E, 'Attributes': SYM_ATTR_DEFAULT }, # Ubuntu logo
{ 'Enabled': args.octicons, 'Filename': "octicons.ttf", 'Exact': octiconsExactEncodingPosition, 'SymStart': 0xF000, 'SymEnd': 0xF0DB, 'SrcStart': 0xF400, 'SrcEnd': 0xF4DB, 'ScaleGlyph': 0xF02E, 'Attributes': SYM_ATTR_DEFAULT }, # Magnifying glass
{ 'Enabled': args.custom, 'Filename': args.custom, 'Exact': True, 'SymStart': 0x0000, 'SymEnd': 0x0000, 'SrcStart': 0x0000, 'SrcEnd': 0x0000, 'ScaleGlyph': None, 'Attributes': CUSTOM_ATTR },
]
# win_ascent and win_descent are used to set the line height for windows fonts.
@ -319,8 +331,8 @@ for glyph in range(0x00, 0x17f):
# 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'
# Update the font encoding to ensure that the Unicode glyphs are available.
sourceFont.encoding = 'UnicodeFull'
# Fetch this property before adding outlines
onlybitmaps = sourceFont.onlybitmaps
@ -355,8 +367,22 @@ def get_scale_factor(font_dim, sym_dim):
scale_ratio = scale_ratio_x
return scale_ratio
def use_scale_glyph( unicode_value ):
for i in SCALE_GLYPH_LIST:
if isinstance(i, tuple):
if unicode_value >= i[0] and unicode_value <= i[1]:
return True
else:
if unicode_value == i:
return True
return False
def copy_glyphs(sourceFont, sourceFontStart, sourceFontEnd, symbolFont, symbolFontStart, symbolFontEnd, exactEncoding, scaleGlyph, attributes):
careful = False
if args.careful:
careful = True
if exactEncoding is False:
sourceFontList = []
sourceFontCounter = 0
@ -370,8 +396,16 @@ def copy_glyphs(sourceFont, sourceFontStart, sourceFontEnd, symbolFont, symbolFo
scale_factor = get_scale_factor(font_dim, sym_dim)
# Create glyphs from symbol font
symbolFont.selection.select(("ranges","unicode"),symbolFontStart,symbolFontEnd)
sourceFont.selection.select(("ranges","unicode"),sourceFontStart,sourceFontEnd)
# If we are going to copy all Glyphs, then assume we want to be careful
# and only copy those that are not already contained in the source font
if symbolFontStart == 0:
symbolFont.selection.all()
sourceFont.selection.all()
careful = True
else:
symbolFont.selection.select(("ranges","unicode"),symbolFontStart,symbolFontEnd)
sourceFont.selection.select(("ranges","unicode"),sourceFontStart,sourceFontEnd)
for sym_glyph in symbolFont.selection.byGlyphs:
try:
@ -391,6 +425,10 @@ def copy_glyphs(sourceFont, sourceFontStart, sourceFontEnd, symbolFont, symbolFo
copiedToSlot = sourceFontList[sourceFontCounter]
sourceFontCounter += 1
if int(copiedToSlot, 16) < 0:
print "Found invalid glyph slot number. Skipping."
continue
if args.quiet == False:
print "Updating glyph: " + str(sym_glyph) + " " + str(sym_glyph.glyphname) + " putting at: " + copiedToSlot
@ -398,8 +436,7 @@ def copy_glyphs(sourceFont, sourceFontStart, sourceFontEnd, symbolFont, symbolFo
sym_dim = get_dim(sym_glyph)
# check if a glyph already exists in this location
if args.careful or 'careful' in sym_attr['params']:
if careful or 'careful' in sym_attr['params']:
if copiedToSlot.startswith("uni"):
copiedToSlot = copiedToSlot[3:]
@ -432,7 +469,7 @@ def copy_glyphs(sourceFont, sourceFontStart, sourceFontEnd, symbolFont, symbolFo
# find the largest possible scaling factor that will allow the glyph
# to fit in both the x and y directions
if sym_attr['stretch'] == 'pa':
if scale_factor:
if scale_factor and use_scale_glyph(currentSourceFontGlyph):
# We want to preserve the relative size of each glyph to other glyphs
# in the same symbol font.
scale_ratio_x = scale_factor
@ -496,7 +533,10 @@ def copy_glyphs(sourceFont, sourceFontStart, sourceFontEnd, symbolFont, symbolFo
# reset selection so iteration works propertly @todo fix? rookie misunderstanding?
# This is likely needed because the selection was changed when the glyph was copy/pasted
symbolFont.selection.select(("ranges","unicode"),symbolFontStart,symbolFontEnd)
if symbolFontStart == 0:
symbolFont.selection.all()
else:
symbolFont.selection.select(("ranges","unicode"),symbolFontStart,symbolFontEnd)
# end for
return