Horus fonts / bitmaps tools updated

This commit is contained in:
Bertrand Songis
2015-10-14 23:47:51 +02:00
parent 3d74f2b437
commit c2dade2133
2 changed files with 40 additions and 7 deletions
+22 -6
View File
@@ -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))
+18 -1
View File
@@ -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):