subset_for_web.py 2.0 KB

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