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