Advertisement
Guest User

gen_sfont_latin.py

a guest
Apr 7th, 2013
141
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.94 KB | None | 0 0
  1. #!/usr/bin/env python
  2. # -*- coding: ascii -*-
  3. # vim:ts=4:sw=4:softtabstop=4:smarttab:expandtab
  4. #
  5. """SFont image generator.
  6. Converts a TTF into an Image suitable for use with SFont and SDL.
  7. See http://www.linux-games.com/sfont/
  8. """
  9.  
  10. import os
  11. import sys
  12. from PIL import Image, ImageDraw, ImageFont
  13.  
  14.  
  15. def ttf2image(ttf_font_fullpathname, fontsize=None, fontcolor=None, fontpng=None, use_unicode=False):
  16.     # default unspecified params
  17.     fontsize = fontsize or 11
  18.     fontcolor = fontcolor or '000000'
  19.     fontpng = fontpng or './'
  20.    
  21.     # Setup character delimiter for SFont
  22.     PINK = (255, 0, 255)
  23.    
  24.     # Convert colour/color hex string into tuple of RGB strings
  25.     split = (fontcolor[0:2], fontcolor[2:4], fontcolor[4:6])
  26.    
  27.     font_color = (int(split[0], 16), int(split[1], 16), int(split[2], 16))
  28.  
  29.     # setting the font string
  30.     text = u'!"#$%&\'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~'
  31.     if use_unicode:
  32.         text = u'!"#$%&\'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~_________________________________\xa0\xa1\xa2\xa3\xa4\xa5\xa6\xa7\xa8\xa9\xaa\xab\xac\xad\xae\xaf\xb0\xb1\xb2\xb3\xb4\xb5\xb6\xb7\xb8\xb9\xba\xbb\xbc\xbd\xbe\xbf\xc0\xc1\xc2\xc3\xc4\xc5\xc6\xc7\xc8\xc9\xca\xcb\xcc\xcd\xce\xcf\xd0\xd1\xd2\xd3\xd4\xd5\xd6\xd7\xd8\xd9\xda\xdb\xdc\xdd\xde\xdf\xe0\xe1\xe2\xe3\xe4\xe5\xe6\xe7\xe8\xe9\xea\xeb\xec\xed\xee\xef\xf0\xf1\xf2\xf3\xf4\xf5\xf6\xf7\xf8\xf9\xfa\xfb\xfc\xfd\xfe\xff'
  33.  
  34.     number_of_codepoints = len(text)  # number of characters
  35.  
  36.     # setting the font and it's size
  37.     font = ImageFont.truetype(ttf_font_fullpathname, fontsize)
  38.  
  39.     FONT_PIXEL_HEIGHT_OFFSET = 5  # original offset - I think this is too much .... I guess this needs to be a parameter :-)
  40.     FONT_PIXEL_HEIGHT_OFFSET = 1
  41.  
  42.     # setting width/height to 0 before calculating actual image width/height
  43.     text_width = 0
  44.     text_height = 0
  45.     seperator_width = 2  # +2 for "pink" kerning seperator/indicator
  46.  
  47.     # start calculating pixel width and height
  48.     for i in range(0, number_of_codepoints):
  49.         glyph_width, glyph_height = font.getsize(text[i])
  50.         text_width = text_width + glyph_width + seperator_width
  51.         text_height = max(text_height, glyph_height)
  52.     text_height += FONT_PIXEL_HEIGHT_OFFSET
  53.     text_width = text_width + seperator_width  # final seperator
  54.  
  55.     # creating a new image
  56.     img = Image.new('RGBA', (text_width, text_height), (0, 0, 0, 0))
  57.     draw = ImageDraw.Draw(img)
  58.  
  59.     # drawing the first 2 pixels of dividing line
  60.     draw.line(((0, 0), (1, 0)), fill=PINK)
  61.  
  62.     # setting X position for the first symbol
  63.     start = 2
  64.  
  65.     glyph_y = max(0, FONT_PIXEL_HEIGHT_OFFSET - 1)
  66.     print glyph_y
  67.     for i in range(0, number_of_codepoints):
  68.         draw.text((start, glyph_y), text[i], font=font, fill=font_color)
  69.         sz = draw.textsize(text[i], font=font)
  70.         start = start + sz[0]
  71.         draw.line(((start, 0), (start + (seperator_width - 1), 0)), fill=PINK)
  72.         start = start + seperator_width
  73.     del draw
  74.    
  75.     """TODO scan image and check the height, if there are blank lines above/below, consider removing them.
  76.    PIL seems to return larger pixel heights than actual, leading to large vertical spacing
  77.    """
  78.    
  79.     save_filename = os.path.join(fontpng, "font.png")
  80.     img.save(save_filename, "PNG")
  81.  
  82.  
  83. def main(argv=None):
  84.     if argv is None:
  85.         argv = sys.argv
  86.  
  87.     if len(argv) < 5:
  88.         sys.exit("Usage: python font.py /path/to/font.ttf font_size(e.g. 11) font_color(e.g. 000000) /path/to/save/dir/ [unicode]")
  89.  
  90.     ttf_font_fullpathname = argv[1]
  91.     fontsize = int(argv[2])
  92.     fontcolor = argv[3]
  93.     fontpng = argv[4]
  94.     if len(argv) > 5:
  95.         use_unicode = True
  96.     else:
  97.         use_unicode = False
  98.     ttf2image(ttf_font_fullpathname, fontsize, fontcolor, fontpng, use_unicode)
  99.  
  100. if __name__ == "__main__":
  101.     sys.exit(main())
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement