2 # -*- coding: utf-8 -*-
5 from PyQt4
import Qt
, QtCore
, QtGui
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):
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)
38 extraWidth
, extraHeight
= 0, 0
40 extraWidth
, extraHeight
= extraImage
.size().width() / COUNT_EXTRA_CHARS
, extraImage
.size().height()
47 elif c
== "." and fontsize
<= 8:
50 r
= metrics
.boundingRect(c
)
54 return r
.width() + r
.left() + 1
59 width
+= getCharWidth(c
)
62 def getFontTopBottom():
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())
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()
77 pen
= QtGui
.QPen(foreground
)
82 painter
.setOpacity(1.0)
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
)
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
98 rect
= metrics
.boundingRect(c
)
100 painter
.drawText(width
- rect
.left() + 1, fontsize
- 2, c
) # fontsize-bottom+1 -17 / 7
102 painter
.drawText(width
+ 1, fontsize
, c
) # fontsize-bottom+1 -17 / 7
103 width
+= getCharWidth(c
)
107 image
.save(filename
+ ".png")
109 with
open(filename
+ ".specs", "w") as f
:
110 f
.write(",".join(str(tmp
) for tmp
in 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))