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