From c2dade2133dd6d467a95fb084fa2fc611d72f9d3 Mon Sep 17 00:00:00 2001 From: Bertrand Songis Date: Wed, 7 Oct 2015 20:23:42 +0200 Subject: [PATCH] Horus fonts / bitmaps tools updated --- radio/util/font2png.py | 28 ++++++++++++++++++++++------ radio/util/img2lbm.py | 19 ++++++++++++++++++- 2 files changed, 40 insertions(+), 7 deletions(-) diff --git a/radio/util/font2png.py b/radio/util/font2png.py index 8d23f6806..86a6ce8bb 100755 --- a/radio/util/font2png.py +++ b/radio/util/font2png.py @@ -3,12 +3,16 @@ import sys from PyQt4 import Qt, QtGui +import glob try: app = Qt.QApplication(sys.argv) except: pass +for f in glob.glob("fonts/*.ttf"): + id = QtGui.QFontDatabase.addApplicationFont(f) + chars = u""" !"#$%&'()*+,-./0123456789:;<=>?°ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklmnopqrstuvwxyz~ ≥→←↑↓ △Ⓘ""" def getCharWidth(fontsize, metrics, index): @@ -18,8 +22,16 @@ def getCharWidth(fontsize, metrics, index): return 1 else: rect = metrics.boundingRect(chars[index]) - return rect.width() - + 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()) + return top, bottom + def getFontWidth(fontsize, metrics): width = 0 for i, c in enumerate(chars): @@ -28,11 +40,15 @@ def getFontWidth(fontsize, metrics): def createFontBitmap(filename, fontname, fontsize, foreground, background, coordsfile=True): coords = [ ] + # font = QtGui.QFont(QtGui.QFontDatabase.applicationFontFamilies(id)[0]) font = QtGui.QFont(fontname) - font.setPointSize(fontsize) + font.setPixelSize(fontsize) + font.setHintingPreference(QtGui.QFont.PreferNoHinting) metrics = QtGui.QFontMetrics(font) width = getFontWidth(fontsize, metrics) - image = QtGui.QImage(width, fontsize+4, QtGui.QImage.Format_RGB32) + top, bottom = getFontTopBottom(fontsize, metrics) + print top, bottom + image = QtGui.QImage(width, fontsize+1, QtGui.QImage.Format_RGB32) image.fill(background) painter = QtGui.QPainter() painter.begin(image) @@ -48,7 +64,7 @@ def createFontBitmap(filename, fontname, fontsize, foreground, background, coord painter.drawPoint(width, fontsize); else: rect = metrics.boundingRect(c) - painter.drawText(width-rect.left(), fontsize+1, c) + painter.drawText(width-rect.left(), fontsize-2, c) # fontsize-bottom+1 -17 / 7 width += getCharWidth(fontsize, metrics, i) coords.append(width) painter.end() @@ -62,4 +78,4 @@ def createFontBitmap(filename, fontname, fontsize, foreground, background, coord return coords if len(sys.argv) == 4: - createFontBitmap(sys.argv[3], sys.argv[1], int(sys.argv[2]), QtGui.QColor(0x00, 0x00, 0x00), QtGui.QColor(0xFF, 0xFF, 0xFF)) + createFontBitmap(sys.argv[3], sys.argv[1], float(sys.argv[2]), QtGui.QColor(0x00, 0x00, 0x00), QtGui.QColor(0xFF, 0xFF, 0xFF)) diff --git a/radio/util/img2lbm.py b/radio/util/img2lbm.py index 83e151d98..f6b92ca98 100755 --- a/radio/util/img2lbm.py +++ b/radio/util/img2lbm.py @@ -36,7 +36,7 @@ if what == "1bit": value += 1 << z f.write("0x%02x," % value) f.write("\n") -elif what == "16bits": +elif what == "4/4/4/4": colors = [] f.write("%d,%d,\n" % (width, height)) for y in range(height): @@ -44,6 +44,14 @@ elif what == "16bits": pixel = image.pixel(x, y) f.write("0x%1x%1x%1x%1x," % (Qt.qAlpha(pixel)/16, Qt.qRed(pixel)/16, Qt.qGreen(pixel)/16, Qt.qBlue(pixel)/16)) f.write("\n") +elif what == "5/6/5": + colors = [] + f.write("%d,%d,\n" % (width, height)) + for y in range(height): + for x in range(width): + pixel = image.pixel(x, y) + f.write("0x%04x," % (((Qt.qRed(pixel) >> 3) << 11) + ((Qt.qGreen(pixel) >> 2) << 5) + ((Qt.qBlue(pixel) >> 3) << 0))) + f.write("\n") elif what == "4bits": colors = [] writeSize(f, width, height) @@ -62,6 +70,15 @@ elif what == "4bits": value -= 1<<(4+i) f.write("0x%02x," % value) f.write("\n") +elif what == "8bits": + colors = [] + writeSize(f, width, height) + for y in range(height): + for x in range(width): + value = Qt.qGray(image.pixel(x, y)) + value = 0x0f - (value >> 4) + f.write("0x%02x," % value) + f.write("\n") elif what == "03x05": for y in range(0, height, 5): for x in range(width):