Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit d23417ab authored by Deepanshu Gupta's avatar Deepanshu Gupta
Browse files

Parallel processing of the fonts. [DO NOT MERGE]

Font renaming for the SDK is now done in parallel.

This now reduces the running time for this script from about 30 mins to
about 5 to 10 mins. More speedup is expected when fonttools is updated.

Change-Id: I2c405f6d3e637e3067c8a1653e3cae206c32c0c3
(cherry picked from commit c6364fd90de1600526d3431ce0740b6b856e48f0)
parent 49a60d4d
Loading
Loading
Loading
Loading
+60 −37
Original line number Original line Diff line number Diff line
@@ -30,13 +30,18 @@ import os
from lxml import etree
from lxml import etree
import shutil
import shutil
import glob
import glob
from multiprocessing import Pool

# global variable
dest_dir = '/tmp'


def main(argv):
def main(argv):
  if len(argv) < 2:
  if len(argv) < 2:
    sys.exit('Usage: build_font.py /path/to/input_fonts/ /path/to/out/dir/')
    sys.exit('Usage: build_font.py /path/to/input_fonts/ /path/to/out/dir/')
  for dir in argv:
  for directory in argv:
    if not os.path.isdir(dir):
    if not os.path.isdir(directory):
      sys.exit(dir + ' is not a valid directory')
      sys.exit(directory + ' is not a valid directory')
  global dest_dir
  dest_dir = argv[-1]
  dest_dir = argv[-1]
  src_dirs = argv[:-1]
  src_dirs = argv[:-1]
  cwd = os.getcwd()
  cwd = os.getcwd()
@@ -45,6 +50,7 @@ def main(argv):
  for filename in files:
  for filename in files:
    os.remove(filename)
    os.remove(filename)
  os.chdir(cwd)
  os.chdir(cwd)
  input_fonts = list()
  for src_dir in src_dirs:
  for src_dir in src_dirs:
    for filename in os.listdir(src_dir):
    for filename in os.listdir(src_dir):
      if os.path.isdir(os.path.join(src_dir, filename)):
      if os.path.isdir(os.path.join(src_dir, filename)):
@@ -52,14 +58,25 @@ def main(argv):
      if not os.path.splitext(filename)[1].lower() == '.ttf':
      if not os.path.splitext(filename)[1].lower() == '.ttf':
        shutil.copy(os.path.join(src_dir, filename), dest_dir)
        shutil.copy(os.path.join(src_dir, filename), dest_dir)
        continue
        continue
      old_ttf_path = os.path.join(src_dir, filename)
      input_fonts.append(os.path.join(src_dir, filename))
  # Create as many threads as the number of CPUs
  pool = Pool(processes=None)
  pool.map(convert_font, input_fonts)


class InvalidFontException(Exception):
  pass

def convert_font(input_path):
  filename = os.path.basename(input_path)
  print 'Converting font: ' + filename
  # the path to the output file. The file name is the fontfilename.ttx
  # the path to the output file. The file name is the fontfilename.ttx
  ttx_path = os.path.join(dest_dir, filename)
  ttx_path = os.path.join(dest_dir, filename)
  ttx_path = ttx_path[:-1] + 'x'
  ttx_path = ttx_path[:-1] + 'x'
  try:
  try:
    # run ttx to generate an xml file in the output folder which represents all
    # run ttx to generate an xml file in the output folder which represents all
    # its info
    # its info
        ttx_args = ['-d', dest_dir, old_ttf_path]
    ttx_args = ['-q', '-d', dest_dir, input_path]
    ttx.main(ttx_args)
    ttx.main(ttx_args)
    # now parse the xml file to change its PS name.
    # now parse the xml file to change its PS name.
    tree = etree.parse(ttx_path)
    tree = etree.parse(ttx_path)
@@ -72,12 +89,18 @@ def main(argv):
        update_name(name, new_ps_name)
        update_name(name, new_ps_name)
    tree.write(ttx_path, xml_declaration=True, encoding=encoding )
    tree.write(ttx_path, xml_declaration=True, encoding=encoding )
    # generate the udpated font now.
    # generate the udpated font now.
        ttx_args = ['-d', dest_dir, ttx_path]
    ttx_args = ['-q', '-d', dest_dir, ttx_path]
    ttx.main(ttx_args)
    ttx.main(ttx_args)
      except Exception:
  except InvalidFontException:
    # In case of invalid fonts, we exit.
    print filename + ' is not a valid font'
    raise
  except Exception as e:
    print 'Error converting font: ' + filename
    print e
    # Some fonts are too big to be handled by the ttx library.
    # Some fonts are too big to be handled by the ttx library.
    # Just copy paste them.
    # Just copy paste them.
        shutil.copy(old_ttf_path, dest_dir)
    shutil.copy(input_path, dest_dir)
  try:
  try:
    # delete the temp ttx file is it exists.
    # delete the temp ttx file is it exists.
    os.remove(ttx_path)
    os.remove(ttx_path)
@@ -94,14 +117,14 @@ def get_font_info(tag):
      if namerecord.attrib['nameID'] == '6':
      if namerecord.attrib['nameID'] == '6':
        if ps_name is not None:
        if ps_name is not None:
          if not sanitize(namerecord.text) == ps_name:
          if not sanitize(namerecord.text) == ps_name:
            sys.exit('found multiple possibilities of the font name')
            raise InvalidFontException('found multiple possibilities of the font name')
        else:
        else:
          ps_name = sanitize(namerecord.text)
          ps_name = sanitize(namerecord.text)
      # nameID=5 means the font version
      # nameID=5 means the font version
      if namerecord.attrib['nameID'] == '5':
      if namerecord.attrib['nameID'] == '5':
        if ps_version is not None:
        if ps_version is not None:
          if not ps_version == get_version(namerecord.text):
          if not ps_version == get_version(namerecord.text):
            sys.exit('found multiple possibilities of the font version')
            raise InvalidFontException('found multiple possibilities of the font version')
        else:
        else:
          ps_version = get_version(namerecord.text)
          ps_version = get_version(namerecord.text)
  return [ps_name, ps_version]
  return [ps_name, ps_version]
@@ -121,7 +144,7 @@ def get_version(string):
  # to extract n.nn, we return the second entry in the split strings.
  # to extract n.nn, we return the second entry in the split strings.
  string = string.strip()
  string = string.strip()
  if not string.startswith('Version '):
  if not string.startswith('Version '):
    sys.exit('mal-formed font version')
    raise InvalidFontException('mal-formed font version')
  return sanitize(string.split()[1])
  return sanitize(string.split()[1])


if __name__ == '__main__':
if __name__ == '__main__':