font2png script updated
This commit is contained in:
+39
-18
@@ -4,41 +4,62 @@
|
||||
import sys
|
||||
from PyQt4 import Qt, QtGui
|
||||
|
||||
try:
|
||||
app = Qt.QApplication(sys.argv)
|
||||
except:
|
||||
pass
|
||||
|
||||
chars = u""" !"#$%&'()*+,-./0123456789:;<=>?°ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklmnopqrstuvwxyz~ ≥→←↑↓ △Ⓘ"""
|
||||
|
||||
def getCharWidth(metrics):
|
||||
def getCharWidth(fontsize, metrics, index):
|
||||
if index == 0:
|
||||
return 4
|
||||
elif index == 14 and fontsize <= 8:
|
||||
return 1
|
||||
else:
|
||||
rect = metrics.boundingRect(chars[index])
|
||||
return rect.width()
|
||||
|
||||
def getFontWidth(fontsize, metrics):
|
||||
width = 0
|
||||
for c in chars:
|
||||
rect = metrics.boundingRect(c)
|
||||
if rect.width() > width:
|
||||
width = rect.width()
|
||||
for i, c in enumerate(chars):
|
||||
width += getCharWidth(fontsize, metrics, i)
|
||||
return width
|
||||
|
||||
def createFontBitmap(filename, fontname, fontsize, foreground, background):
|
||||
|
||||
def createFontBitmap(filename, fontname, fontsize, foreground, background, coordsfile=True):
|
||||
coords = [ ]
|
||||
font = QtGui.QFont(fontname)
|
||||
font.setPointSize(fontsize)
|
||||
metrics = QtGui.QFontMetrics(font)
|
||||
width = getCharWidth(metrics)
|
||||
image = QtGui.QImage(width*len(chars), fontsize+4, QtGui.QImage.Format_RGB32)
|
||||
width = getFontWidth(fontsize, metrics)
|
||||
image = QtGui.QImage(width, fontsize+4, QtGui.QImage.Format_RGB32)
|
||||
image.fill(background)
|
||||
painter = QtGui.QPainter()
|
||||
painter.begin(image)
|
||||
painter.setFont(font)
|
||||
pen = QtGui.QPen(foreground)
|
||||
painter.setPen(pen)
|
||||
width = 0
|
||||
for i, c in enumerate(chars):
|
||||
rect = metrics.boundingRect(c)
|
||||
coords.append(width)
|
||||
if i == 0:
|
||||
rect.setWidth(4)
|
||||
elif i == 14:
|
||||
rect.setWidth(1)
|
||||
painter.drawPoint(width*i, fontsize);
|
||||
pass
|
||||
elif i == 14 and fontsize <= 8:
|
||||
painter.drawPoint(width, fontsize);
|
||||
else:
|
||||
painter.drawText(width*i-rect.left(), fontsize+1, c)
|
||||
for j in range(rect.width(), width):
|
||||
painter.drawLine(width*i+j, 0, width*i+j, fontsize+4)
|
||||
rect = metrics.boundingRect(c)
|
||||
painter.drawText(width-rect.left(), fontsize+1, c)
|
||||
width += getCharWidth(fontsize, metrics, i)
|
||||
coords.append(width)
|
||||
painter.end()
|
||||
image.save(filename)
|
||||
image.save(filename + ".png")
|
||||
if coordsfile:
|
||||
f = file(filename + ".specs", "w")
|
||||
f.write("{ ")
|
||||
f.write(",".join(str(tmp) for tmp in coords))
|
||||
f.write(" }")
|
||||
f.close()
|
||||
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))
|
||||
|
||||
Reference in New Issue
Block a user