font-build.py 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. #!/usr/bin/env python2
  2. # SuperTux Font Build Script
  3. # Copyright (C) 2018 Ingo Ruhnke <grumbel@gmail.com>
  4. #
  5. # This program is free software: you can redistribute it and/or modify
  6. # it under the terms of the GNU General Public License as published by
  7. # the Free Software Foundation, either version 3 of the License, or
  8. # (at your option) any later version.
  9. #
  10. # This program is distributed in the hope that it will be useful,
  11. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. # GNU General Public License for more details.
  14. #
  15. # You should have received a copy of the GNU General Public License
  16. # along with this program. If not, see <https://www.gnu.org/licenses/>.
  17. import os
  18. import sys
  19. import fontforge
  20. import psMat;
  21. font = fontforge.font()
  22. font.encoding = "unicode"
  23. font.familyname = "SuperTux"
  24. font.fullname = "SuperTux-Medium"
  25. font.fontname = "SuperTux-Medium"
  26. glyph = font.createChar(ord(" "))
  27. glyph.width = 500
  28. # Import and OCR the SuperTux font
  29. for filename in os.listdir("glyphs"):
  30. idx = int(filename[0:4], 16)
  31. print("processing {} -> {}".format(filename, idx))
  32. glyph = font.createChar(idx)
  33. glyph.importOutlines(os.path.join("glyphs", filename))
  34. font.selection.select(idx)
  35. font.autoTrace()
  36. font.autoWidth(200)
  37. # font.autoHint()
  38. # for glyph in font.glyphs():
  39. # glyph.transform(psMat.translate(0, 30))
  40. # font.ascent = 750
  41. # font.descent = 150
  42. # Set digits to fixed width
  43. # Calculate max digit_width
  44. digit_width = 0
  45. for c in "0123456789":
  46. glyph = font[ord(c)]
  47. digit_width = max(digit_width, glyph.width)
  48. # Or better hardcode it
  49. digit_width = 725
  50. for c in "0123456789":
  51. glyph = font[ord(c)]
  52. old_width = glyph.width
  53. glyph.width = digit_width
  54. w = glyph.left_side_bearing + glyph.right_side_bearing
  55. glyph.left_side_bearing = w / 2
  56. glyph.right_side_bearing = w / 2
  57. # print(glyph.layers)
  58. # glyph.layers[1].nltransform("x - 400", "y")
  59. # Set glyph width twice, since left/right_side_bearing leads to
  60. # rounding issues and unequal width
  61. glyph.width = digit_width
  62. # The next step requires the Google Noto fonts extracted into the noto/
  63. # directory, they can be found at:
  64. # https://noto-website-2.storage.googleapis.com/pkgs/Noto-hinted.zip
  65. font.mergeFonts("noto/NotoSans-Regular.ttf")
  66. font.mergeFonts("noto/NotoSansArabic-Regular.ttf")
  67. font.mergeFonts("noto/NotoSansHebrew-Regular.ttf")
  68. # These make fontforge crash
  69. # font.mergeFonts("noto/NotoSansCJKjp-Regular.otf")
  70. # font.mergeFonts("noto/NotoSansCJKkr-Regular.otf")
  71. if not os.path.exists("build"):
  72. os.mkdir("build")
  73. font.generate("build/SuperTux-Medium.otf")
  74. font.generate("build/SuperTux-Medium.ttf")
  75. font.save("build/SuperTux-Medium.sfd")
  76. # EOF #