Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python
- # -*- coding: ascii -*-
- # vim:ts=4:sw=4:softtabstop=4:smarttab:expandtab
- #
- """SFont image generator.
- Converts a TTF into an Image suitable for use with SFont and SDL.
- See http://www.linux-games.com/sfont/
- """
- import os
- import sys
- from PIL import Image, ImageDraw, ImageFont
- def ttf2image(ttf_font_fullpathname, fontsize=None, fontcolor=None, fontpng=None, use_unicode=False):
- # default unspecified params
- fontsize = fontsize or 11
- fontcolor = fontcolor or '000000'
- fontpng = fontpng or './'
- # Setup character delimiter for SFont
- PINK = (255, 0, 255)
- # Convert colour/color hex string into tuple of RGB strings
- split = (fontcolor[0:2], fontcolor[2:4], fontcolor[4:6])
- font_color = (int(split[0], 16), int(split[1], 16), int(split[2], 16))
- # setting the font string
- text = u'!"#$%&\'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~'
- if use_unicode:
- 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'
- number_of_codepoints = len(text) # number of characters
- # setting the font and it's size
- font = ImageFont.truetype(ttf_font_fullpathname, fontsize)
- FONT_PIXEL_HEIGHT_OFFSET = 5 # original offset - I think this is too much .... I guess this needs to be a parameter :-)
- FONT_PIXEL_HEIGHT_OFFSET = 1
- # setting width/height to 0 before calculating actual image width/height
- text_width = 0
- text_height = 0
- seperator_width = 2 # +2 for "pink" kerning seperator/indicator
- # start calculating pixel width and height
- for i in range(0, number_of_codepoints):
- glyph_width, glyph_height = font.getsize(text[i])
- text_width = text_width + glyph_width + seperator_width
- text_height = max(text_height, glyph_height)
- text_height += FONT_PIXEL_HEIGHT_OFFSET
- text_width = text_width + seperator_width # final seperator
- # creating a new image
- img = Image.new('RGBA', (text_width, text_height), (0, 0, 0, 0))
- draw = ImageDraw.Draw(img)
- # drawing the first 2 pixels of dividing line
- draw.line(((0, 0), (1, 0)), fill=PINK)
- # setting X position for the first symbol
- start = 2
- glyph_y = max(0, FONT_PIXEL_HEIGHT_OFFSET - 1)
- print glyph_y
- for i in range(0, number_of_codepoints):
- draw.text((start, glyph_y), text[i], font=font, fill=font_color)
- sz = draw.textsize(text[i], font=font)
- start = start + sz[0]
- draw.line(((start, 0), (start + (seperator_width - 1), 0)), fill=PINK)
- start = start + seperator_width
- del draw
- """TODO scan image and check the height, if there are blank lines above/below, consider removing them.
- PIL seems to return larger pixel heights than actual, leading to large vertical spacing
- """
- save_filename = os.path.join(fontpng, "font.png")
- img.save(save_filename, "PNG")
- def main(argv=None):
- if argv is None:
- argv = sys.argv
- if len(argv) < 5:
- 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]")
- ttf_font_fullpathname = argv[1]
- fontsize = int(argv[2])
- fontcolor = argv[3]
- fontpng = argv[4]
- if len(argv) > 5:
- use_unicode = True
- else:
- use_unicode = False
- ttf2image(ttf_font_fullpathname, fontsize, fontcolor, fontpng, use_unicode)
- if __name__ == "__main__":
- sys.exit(main())
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement