PEP8 radio/util/font2png.py
Fix whitespace and indentations, remove semicolons
This commit is contained in:
+27
-23
@@ -12,6 +12,7 @@ for f in glob.glob("fonts/*.ttf"):
|
|||||||
|
|
||||||
chars = u""" !"#$%&'()*+,-./0123456789:;<=>?°ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklmnopqrstuvwxyz~ ≥→←↑↓↗↘↙↖△"""
|
chars = u""" !"#$%&'()*+,-./0123456789:;<=>?°ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklmnopqrstuvwxyz~ ≥→←↑↓↗↘↙↖△"""
|
||||||
|
|
||||||
|
|
||||||
def getCharWidth(fontsize, metrics, index):
|
def getCharWidth(fontsize, metrics, index):
|
||||||
if index == 0:
|
if index == 0:
|
||||||
return 4
|
return 4
|
||||||
@@ -24,39 +25,42 @@ def getCharWidth(fontsize, metrics, index):
|
|||||||
else:
|
else:
|
||||||
return rect.width() + 1
|
return rect.width() + 1
|
||||||
|
|
||||||
|
|
||||||
def getFontTopBottom(fontsize, metrics):
|
def getFontTopBottom(fontsize, metrics):
|
||||||
top, bottom = 0, 0
|
top, bottom = 0, 0
|
||||||
for i, c in enumerate(chars):
|
for i, c in enumerate(chars):
|
||||||
rect = metrics.boundingRect(chars[i])
|
rect = metrics.boundingRect(chars[i])
|
||||||
top = min(top, rect.top())
|
top = min(top, rect.top())
|
||||||
bottom = max(bottom, rect.bottom())
|
bottom = max(bottom, rect.bottom())
|
||||||
return top, bottom
|
return top, bottom
|
||||||
|
|
||||||
|
|
||||||
def getFontWidth(fontsize, metrics):
|
def getFontWidth(fontsize, metrics):
|
||||||
width = 0
|
width = 0
|
||||||
for i, c in enumerate(chars):
|
for i, c in enumerate(chars):
|
||||||
width += getCharWidth(fontsize, metrics, i)
|
width += getCharWidth(fontsize, metrics, i)
|
||||||
return width
|
return width
|
||||||
|
|
||||||
|
|
||||||
def createFontBitmap(filename, fontname, fontsize, fontbold, foreground, background, coordsfile=True):
|
def createFontBitmap(filename, fontname, fontsize, fontbold, foreground, background, coordsfile=True):
|
||||||
coords = [ ]
|
coords = []
|
||||||
font = QtGui.QFont(fontname)
|
font = QtGui.QFont(fontname)
|
||||||
font.setPixelSize(fontsize)
|
font.setPixelSize(fontsize)
|
||||||
font.setBold(fontbold)
|
font.setBold(fontbold)
|
||||||
font.setHintingPreference(QtGui.QFont.PreferNoHinting)
|
font.setHintingPreference(QtGui.QFont.PreferNoHinting)
|
||||||
font.setStyleStrategy(QtGui.QFont.PreferAntialias)
|
font.setStyleStrategy(QtGui.QFont.PreferAntialias)
|
||||||
metrics = QtGui.QFontMetrics(font)
|
metrics = QtGui.QFontMetrics(font)
|
||||||
|
|
||||||
width = getFontWidth(fontsize, metrics)
|
width = getFontWidth(fontsize, metrics)
|
||||||
top, bottom = getFontTopBottom(fontsize, metrics)
|
top, bottom = getFontTopBottom(fontsize, metrics)
|
||||||
extraFilename = "fonts/extra_%dpx.png" % fontsize
|
extraFilename = "fonts/extra_%dpx.png" % fontsize
|
||||||
extraImage = QtGui.QImage(extraFilename)
|
extraImage = QtGui.QImage(extraFilename)
|
||||||
if extraImage.isNull():
|
if extraImage.isNull():
|
||||||
print "No extra font file", extraFilename
|
print("No extra font file", extraFilename)
|
||||||
extraWidth = 0
|
extraWidth = 0
|
||||||
else:
|
else:
|
||||||
extraWidth = extraImage.size().width()
|
extraWidth = extraImage.size().width()
|
||||||
image = QtGui.QImage(width + extraWidth, fontsize+2, QtGui.QImage.Format_RGB32)
|
image = QtGui.QImage(width + extraWidth, fontsize + 2, QtGui.QImage.Format_RGB32)
|
||||||
image.fill(background)
|
image.fill(background)
|
||||||
painter = QtGui.QPainter()
|
painter = QtGui.QPainter()
|
||||||
painter.begin(image)
|
painter.begin(image)
|
||||||
@@ -69,31 +73,31 @@ def createFontBitmap(filename, fontname, fontsize, fontbold, foreground, backgro
|
|||||||
if i == 0:
|
if i == 0:
|
||||||
pass
|
pass
|
||||||
elif i == 14 and fontsize <= 8:
|
elif i == 14 and fontsize <= 8:
|
||||||
painter.drawPoint(width, fontsize);
|
painter.drawPoint(width, fontsize)
|
||||||
else:
|
else:
|
||||||
rect = metrics.boundingRect(c)
|
rect = metrics.boundingRect(c)
|
||||||
if fontsize >= 24:
|
if fontsize >= 24:
|
||||||
painter.drawText(width-rect.left()+1, fontsize-2, c) # fontsize-bottom+1 -17 / 7
|
painter.drawText(width - rect.left() + 1, fontsize - 2, c) # fontsize-bottom+1 -17 / 7
|
||||||
else:
|
else:
|
||||||
painter.drawText(width-rect.left(), fontsize-1, c) # fontsize-bottom+1 -17 / 7
|
painter.drawText(width - rect.left(), fontsize - 1, c) # fontsize-bottom+1 -17 / 7
|
||||||
width += getCharWidth(fontsize, metrics, i)
|
width += getCharWidth(fontsize, metrics, i)
|
||||||
if extraWidth:
|
if extraWidth:
|
||||||
painter.drawImage(QtCore.QPoint(width, 0), extraImage)
|
painter.drawImage(QtCore.QPoint(width, 0), extraImage)
|
||||||
|
|
||||||
coords.append(width)
|
coords.append(width)
|
||||||
painter.end()
|
painter.end()
|
||||||
image.save(filename + ".png")
|
image.save(filename + ".png")
|
||||||
if coordsfile:
|
if coordsfile:
|
||||||
f = file(filename + ".specs", "w")
|
f = file(filename + ".specs", "w")
|
||||||
f.write("{ ")
|
f.write("{ ")
|
||||||
f.write(",".join(str(tmp) for tmp in coords))
|
f.write(",".join(str(tmp) for tmp in coords))
|
||||||
if extraWidth:
|
if extraWidth:
|
||||||
for i in range(1, 14):
|
for i in range(1, 14):
|
||||||
f.write(", %d" % (int(coords[-1])+i*(extraWidth/12)))
|
f.write(", %d" % (int(coords[-1]) + i * (extraWidth / 12)))
|
||||||
# f.write(file("fonts/extra_%dpx.specs" % fontsize).read())
|
# f.write(file("fonts/extra_%dpx.specs" % fontsize).read())
|
||||||
f.write(" }")
|
f.write(" }")
|
||||||
f.close()
|
f.close()
|
||||||
return coords
|
return coords
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
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))
|
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))
|
||||||
|
|||||||
Reference in New Issue
Block a user