Crossfire baudrate selection for X7 support ( fixes #4922 ) (#5194)
[opentx.git] / radio / util / font2png.py
blob079c5ab268db4341ac6018da97e66d49b8f9417a
1 #!/usr/bin/env python
2 # -*- coding: utf-8 -*-
4 import sys
5 from PyQt4 import Qt, QtCore, QtGui
6 import glob
8 app = Qt.QApplication(sys.argv)
10 for f in glob.glob("fonts/*.ttf"):
11 QtGui.QFontDatabase.addApplicationFont(f)
13 chars_en = u""" !"#$%&'()*+,-./0123456789:;<=>?°ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklmnopqrstuvwxyz~|≥→←↑↓↗↘↙↖△"""
14 chars_fr = u"""éèàîç"""
15 chars_de = u"""ÄäÖöÜüß"""
16 chars_cz = u"""ěščřžýáíéňóůúďťĚŠČŘŽÝÁÍÉŇÓÚŮĎŤ"""
18 COUNT_EXTRA_CHARS = 12
20 chars_extra = u"".join([chr(1+i) for i in range(COUNT_EXTRA_CHARS)])
21 chars = chars_en + chars_extra + chars_fr + chars_de + chars_cz
24 def createFontBitmap(filename, fontname, fontsize, fontbold, foreground, background, coordsfile=True):
25 coords = []
26 font = QtGui.QFont(fontname)
27 font.setPixelSize(fontsize)
28 font.setBold(fontbold)
29 font.setHintingPreference(QtGui.QFont.PreferNoHinting)
30 font.setStyleStrategy(QtGui.QFont.PreferAntialias)
31 metrics = QtGui.QFontMetrics(font)
33 extraFilename = "fonts/extra_%dpx.png" % fontsize
34 extraImage = QtGui.QImage(extraFilename)
35 if extraImage.isNull():
36 # print("No extra font file", extraFilename)
37 extraImage = None
38 extraWidth, extraHeight = 0, 0
39 else:
40 extraWidth, extraHeight = extraImage.size().width() / COUNT_EXTRA_CHARS, extraImage.size().height()
42 def getCharWidth(c):
43 if c in chars_extra:
44 return extraWidth
45 elif c == " ":
46 return 4
47 elif c == "." and fontsize <= 8:
48 return 1
49 else:
50 r = metrics.boundingRect(c)
51 if fontsize >= 24:
52 return r.width() + 3
53 else:
54 return r.width() + r.left() + 1
56 def getFontWidth():
57 width = 0
58 for c in chars:
59 width += getCharWidth(c)
60 return width
62 def getFontTopBottom():
63 top, bottom = 0, 0
64 for i, c in enumerate(chars):
65 r = metrics.boundingRect(chars[i])
66 top = min(top, r.top())
67 bottom = max(bottom, r.bottom())
68 return top, bottom
70 width = getFontWidth()
71 top, bottom = getFontTopBottom()
72 image = QtGui.QImage(width, fontsize + 4, QtGui.QImage.Format_RGB32)
73 image.fill(background)
74 painter = QtGui.QPainter()
75 painter.begin(image)
76 painter.setFont(font)
77 pen = QtGui.QPen(foreground)
78 painter.setPen(pen)
79 width = 0
80 for c in chars:
81 coords.append(width)
82 painter.setOpacity(1.0)
83 if c in chars_extra:
84 if extraImage and c == chars_extra[0]:
85 for x in range(extraWidth * COUNT_EXTRA_CHARS):
86 for y in range(extraHeight):
87 rgb = extraImage.pixel(x, y)
88 painter.setOpacity(1.0 - float(QtGui.qGray(rgb)) / 256)
89 painter.drawPoint(x+width, y)
90 elif c == " ":
91 pass
92 elif c == "." and fontsize <= 8:
93 painter.drawPoint(width, fontsize)
94 elif (c == u"↑" or c == u"↓") and fontsize == 16:
95 rect = metrics.boundingRect(c)
96 painter.drawText(width - 1, fontsize, c) # fontsize-bottom+1 -17 / 7
97 else:
98 rect = metrics.boundingRect(c)
99 if fontsize >= 24:
100 painter.drawText(width - rect.left() + 1, fontsize - 2, c) # fontsize-bottom+1 -17 / 7
101 else:
102 painter.drawText(width + 1, fontsize, c) # fontsize-bottom+1 -17 / 7
103 width += getCharWidth(c)
105 coords.append(width)
106 painter.end()
107 image.save(filename + ".png")
108 if coordsfile:
109 with open(filename + ".specs", "w") as f:
110 f.write(",".join(str(tmp) for tmp in coords))
112 return coords
114 if __name__ == "__main__":
115 createFontBitmap(sys.argv[4], sys.argv[1], float(sys.argv[2]), sys.argv[3] == "True", QtGui.QColor(0x00, 0x00, 0x00), QtGui.QColor(0xFF, 0xFF, 0xFF))