2 # -*- coding: utf-8 -*-
6 from PyQt5
import Qt
, QtGui
, QtCore
8 from PyQt4
import Qt
, QtGui
, QtCore
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):
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)
42 extraWidth
, extraHeight
= 0, 0
44 extraWidth
, extraHeight
= extraImage
.size().width() / COUNT_EXTRA_CHARS
, extraImage
.size().height()
51 elif c
== "." and fontsize
<= 8:
54 r
= metrics
.boundingRect(c
)
58 return r
.width() + r
.left() + 1
63 width
+= getCharWidth(c
)
66 def getFontTopBottom():
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())
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()
81 pen
= QtGui
.QPen(foreground
)
86 painter
.setOpacity(1.0)
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
)
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
102 rect
= metrics
.boundingRect(c
)
104 painter
.drawText(width
- rect
.left() + 1, fontsize
- 2, c
) # fontsize-bottom+1 -17 / 7
106 painter
.drawText(width
+ 1, fontsize
, c
) # fontsize-bottom+1 -17 / 7
107 width
+= getCharWidth(c
)
111 image
.save(filename
+ ".png")
113 with
open(filename
+ ".specs", "w") as f
:
114 f
.write(",".join(str(tmp
) for tmp
in 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))