subset_for_web.py 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. # Copyright 2014 Google Inc. All rights reserved.
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License");
  4. # you may not use this file except in compliance with the License.
  5. # You may obtain a copy of the License at
  6. #
  7. # http://www.apache.org/licenses/LICENSE-2.0
  8. #
  9. # Unless required by applicable law or agreed to in writing, software
  10. # distributed under the License is distributed on an "AS IS" BASIS,
  11. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. # See the License for the specific language governing permissions and
  13. # limitations under the License.
  14. """Subset for web fonts."""
  15. __authors__ = [
  16. 'roozbeh@google.com (Roozbeh Pournader)',
  17. 'm.foley.88@gmail.com (Marc Foley)'
  18. ]
  19. import sys
  20. import os
  21. from fontTools.ttLib import TTFont
  22. from fontTools.varLib.instancer import instantiateVariableFont
  23. from nototools import subset
  24. def read_charlist(filename):
  25. """Returns a list of characters read from a charset text file."""
  26. with open(filename) as datafile:
  27. charlist = []
  28. for line in datafile:
  29. if '#' in line:
  30. line = line[:line.index('#')]
  31. line = line.strip()
  32. if not line:
  33. continue
  34. if line.startswith('U+'):
  35. line = line[2:]
  36. char = int(line, 16)
  37. charlist.append(char)
  38. return charlist
  39. def main(argv):
  40. """Subset the first argument to second, dropping unused parts of the font.
  41. """
  42. charlist = read_charlist(os.path.join(os.path.dirname(__file__), 'web_subset.txt'))
  43. # Add private use characters for legacy reasons
  44. charlist += [0xEE01, 0xEE02, 0xF6C3]
  45. features_to_keep = [
  46. 'c2sc', 'ccmp', 'cpsp', 'dlig', 'dnom', 'frac', 'kern', 'liga', 'lnum',
  47. 'locl', 'numr', 'onum', 'pnum', 'smcp', 'ss01', 'ss02', 'ss03', 'ss04',
  48. 'ss05', 'ss06', 'ss07', 'tnum', 'sups', 'subs']
  49. source_filename = argv[1]
  50. target_filename = argv[2]
  51. subset.subset_font(
  52. source_filename, target_filename,
  53. include=charlist,
  54. options={'layout_features': features_to_keep})
  55. web_ttfont = TTFont(target_filename)
  56. if __name__ == '__main__':
  57. main(sys.argv)