force_yminmax.py 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. #!/usr/bin/python
  2. #
  3. # Copyright 2015 Google Inc. All Rights Reserved.
  4. #
  5. # Licensed under the Apache License, Version 2.0 (the "License");
  6. # you may not use this file except in compliance with the License.
  7. # You may obtain a copy of the License at
  8. #
  9. # http://www.apache.org/licenses/LICENSE-2.0
  10. #
  11. # Unless required by applicable law or agreed to in writing, software
  12. # distributed under the License is distributed on an "AS IS" BASIS,
  13. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. # See the License for the specific language governing permissions and
  15. # limitations under the License.
  16. """Post-subset changes for Roboto for Android."""
  17. import sys
  18. from fontTools import ttLib
  19. from nototools import font_data
  20. def output_protruding_glyphs(font, ymin, ymax):
  21. """Outputs all glyphs going outside the specified vertical range."""
  22. protruding_glyphs = []
  23. glyf_table = font['glyf']
  24. for glyph_name in glyf_table.keys():
  25. glyph = glyf_table[glyph_name]
  26. if glyph.numberOfContours == 0:
  27. continue
  28. if glyph.yMin < ymin or glyph.yMax > ymax:
  29. protruding_glyphs.append(glyph_name)
  30. if protruding_glyphs:
  31. print "Protruding glyphs in %s:" % font_data.font_name(font),
  32. print ', '.join(sorted(protruding_glyphs))
  33. YMIN = -555
  34. YMAX = 2163
  35. def main(argv):
  36. """Forces yMin/yMax values and generates a new font."""
  37. source_font_name = argv[1]
  38. target_font_name = argv[2]
  39. font = ttLib.TTFont(source_font_name, recalcBBoxes=False)
  40. head = font['head']
  41. head.yMin = YMIN
  42. head.yMax = YMAX
  43. output_protruding_glyphs(font, YMIN, YMAX)
  44. font.save(target_font_name)
  45. # Make sure the values are set correctly
  46. font = ttLib.TTFont(target_font_name, recalcBBoxes=False)
  47. head = font['head']
  48. assert head.yMin == YMIN and head.yMax == YMAX
  49. if __name__ == "__main__":
  50. main(sys.argv)