Fix ar9x compile/boot (#7102)
[opentx.git] / radio / util / font2png.py
blobe734cc0a613b917f699157cdf16b3c6fe4c94737
1 #!/usr/bin/env python
2 # -*- coding: utf-8 -*-
4 import sys, os
5 import glob
7 from PIL import Image, ImageDraw, ImageFont
9 chars_en = u""" !"#$%&'()*+,-./0123456789:;<=>?°ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklmnopqrstuvwxyz~|≥"""
10 chars_fr = u"""éèàîç"""
11 chars_de = u"""ÄäÖöÜüß"""
12 chars_cz = u"""ěščřžýáíéňóůúďťĚŠČŘŽÝÁÍÉŇÓÚŮĎŤ"""
13 chars_es = u"""Ññ"""
14 chars_fi = u"""åäöÅÄÖ"""
15 chars_it = u"""àù"""
16 chars_pl = u"""ąćęłńóśżźĄĆĘŁŃÓŚŻŹ"""
17 chars_pt = u"""ÁáÂâÃãÀàÇçÉéÊêÍíÓóÔôÕõÚú"""
19 COUNT_EXTRA_CHARS = 21
21 chars_extra = u"".join([chr(1+i) for i in range(COUNT_EXTRA_CHARS)])
22 chars = chars_en + chars_extra + chars_fr + chars_de + chars_cz + chars_es + chars_fi + chars_it + chars_pl
25 def createFontBitmap(filename, fontname, fontsize, fontoffset, foreground, background, coordsfile=True):
28 coords = []
29 font_filename = 'fonts/' + fontname + '.ttf'
31 print("Font filename: %s" % font_filename)
32 font = ImageFont.truetype(font_filename, fontsize)
34 extraImage = None
35 extraFilename = 'fonts/extra_%dpx.png' % fontsize
37 try:
38 extraImage = Image.open(extraFilename)
39 extraImage = extraImage.convert('RGB');
41 except IOError:
42 print("Missing extra symbol filename %s" % extraFilename)
44 def getCharWidth(c):
45 if c in chars_extra:
46 if extraImage:
47 # Extra characters 16px
48 #if fontsize == 16:
49 if ord(c) <= 9:
50 return 13
51 else:
52 return 15
53 else:
54 return 0
55 elif c == " ":
56 return 4
57 else:
58 w, h = font.getsize(c)
59 return w + 1
61 def getFontWidth():
62 width = 0
63 for c in chars:
64 width += getCharWidth(c)
65 return width
67 width = getFontWidth()
68 image = Image.new('RGB', (width, fontsize + 4), background)
69 draw = ImageDraw.Draw(image)
71 width = 0
72 for c in chars:
73 coords.append(width)
75 if c in chars_extra:
76 if extraImage and c == chars_extra[0]:
77 image.paste(extraImage.copy(),(width,1))
78 elif c == " ":
79 pass
80 else:
81 w, h = font.getsize(c)
82 draw.text((width + 1, fontoffset), c, fill=foreground, font=font)
84 width += getCharWidth(c)
86 coords.append(width)
88 image.save(filename + ".png")
89 if coordsfile:
90 with open(filename + ".specs", "w") as f:
91 f.write(",".join(str(tmp) for tmp in coords))
94 # Main
95 if __name__ == "__main__":
96 createFontBitmap(sys.argv[4], sys.argv[1], int(sys.argv[2]), int(sys.argv[3]), (0,0,0), (255,255,255))