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