blob: 4245cdcd715bf7049ae3e7d91e2fe81dbd1861b6 [file] [log] [blame]
#!/usr/bin/env python
# Copyright (C) 2014 The Android Open Source Project
# Licensed under the Apache License, Version 2.0 (the 'License');
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an 'AS IS' BASIS,
# See the License for the specific language governing permissions and
# limitations under the License.
Rename the PS name of the input font.
OpenType fonts (*.otf) are not currently supported. They are copied to the destination without renaming.
XML files are also copied in case they are passed there by mistake.
Usage: /path/to/input_font.ttf /path/to/output_font.ttf
import glob
import os
import re
import shutil
import sys
import xml.etree.ElementTree as etree
# Prevent .pyc files from being created.
sys.dont_write_bytecode = True
# fontTools is available at platform/external/fonttools
from fontTools import ttx
class FontInfo(object):
family = None
style = None
version = None
ends_in_regular = False
fullname = None
class InvalidFontException(Exception):
# A constant to copy the font without modifying. This is useful when running
# locally and speed up the time to build the SDK.
# These constants represent the value of nameID parameter in the namerecord for
# different information.
# see
# A list of extensions to process.
EXTENSIONS = ['.ttf', '.otf', '.xml']
def main(argv):
if len(argv) < 2:
print 'Incorrect usage: ' + str(argv)
sys.exit('Usage: /path/to/input/font.ttf /path/to/out/font.ttf')
dest_path = argv[-1]
input_path = argv[0]
extension = os.path.splitext(input_path)[1].lower()
if extension in EXTENSIONS:
if not COPY_ONLY and extension == '.ttf':
convert_font(input_path, dest_path)
shutil.copy(input_path, dest_path)
def convert_font(input_path, dest_path):
filename = os.path.basename(input_path)
print 'Converting font: ' + filename
# the path to the output file. The file name is the fontfilename.ttx
ttx_path = dest_path[:-1] + 'x'
# run ttx to generate an xml file in the output folder which represents all
# its info
ttx_args = ['-q', '-o', ttx_path, input_path]
# now parse the xml file to change its PS name.
tree = etree.parse(ttx_path)
root = tree.getroot()
for name in root.iter('name'):
update_tag(name, get_font_info(name))
tree.write(ttx_path, xml_declaration=True, encoding='utf-8')
# generate the udpated font now.
ttx_args = ['-q', '-o', dest_path, ttx_path]
except InvalidFontException:
# In case of invalid fonts, we exit.
print filename + ' is not a valid font'
except Exception as e:
print 'Error converting font: ' + filename
print e
# Some fonts are too big to be handled by the ttx library.
# Just copy paste them.
shutil.copy(input_path, dest_path)
# delete the temp ttx file is it exists.
except OSError:
def get_font_info(tag):
""" Returns a list of FontInfo representing the various sets of namerecords
found in the name table of the font. """
fonts = []
font = None
last_name_id = sys.maxint
for namerecord in tag.iter('namerecord'):
if 'nameID' in namerecord.attrib:
name_id = int(namerecord.attrib['nameID'])
# A new font should be created for each platform, encoding and language
# id. But, since the nameIDs are sorted, we use the easy approach of
# creating a new one when the nameIDs reset.
if name_id <= last_name_id and font is not None:
font = None
last_name_id = name_id
if font is None:
font = FontInfo()
if name_id == NAMEID_FAMILY: = namerecord.text.strip()
if name_id == NAMEID_STYLE: = namerecord.text.strip()
if name_id == NAMEID_FULLNAME:
font.ends_in_regular = ends_in_regular(namerecord.text)
font.fullname = namerecord.text.strip()
if name_id == NAMEID_VERSION:
font.version = get_version(namerecord.text)
if font is not None:
return fonts
def update_tag(tag, fonts):
last_name_id = sys.maxint
fonts_iterator = fonts.__iter__()
font = None
for namerecord in tag.iter('namerecord'):
if 'nameID' in namerecord.attrib:
name_id = int(namerecord.attrib['nameID'])
if name_id <= last_name_id:
font =
font = update_font_name(font)
last_name_id = name_id
if name_id == NAMEID_FAMILY:
namerecord.text =
if name_id == NAMEID_FULLNAME:
namerecord.text = font.fullname
def update_font_name(font):
""" Compute the new font family name and font fullname. If the font has a
valid version, it's sanitized and appended to the font family name. The
font fullname is then created by joining the new family name and the
style. If the style is 'Regular', it is appended only if the original font
had it. """
if is None or is None:
raise InvalidFontException('Font doesn\'t have proper family name or style')
if font.version is not None:
new_family = + font.version
new_family =
if is 'Regular' and not font.ends_in_regular:
font.fullname = new_family
font.fullname = new_family + ' ' + = new_family
return font
def ends_in_regular(string):
""" According to the specification, the font fullname should not end in
'Regular' for plain fonts. However, some fonts don't obey this rule. We
keep the style info, to minimize the diff. """
string = string.strip().split()[-1]
return string is 'Regular'
def get_version(string):
string = string.strip()
# The spec says that the version string should start with "Version ". But not
# all fonts do. So, we return the complete string if it doesn't start with
# the prefix, else we return the rest of the string after sanitizing it.
prefix = 'Version '
if string.startswith(prefix):
string = string[len(prefix):]
return sanitize(string)
def sanitize(string):
""" Remove non-standard chars. """
return re.sub(r'[^\w-]+', '', string)
if __name__ == '__main__':