Created
September 28, 2013 07:44
-
-
Save liyonghelpme/6739618 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- coding: utf-8 -*- | |
import os | |
import Image | |
import sys | |
import ImageOps | |
import MySQLdb | |
import codecs | |
#得到最小 2次幂值 | |
def getMin2(v): | |
old = v | |
id = 0 | |
while (v>>1) > 0: | |
v = v >> 1 | |
id += 1 | |
res = 1 << id | |
if res < old: | |
res = res << 1 | |
return res | |
def getBox(boxes, size): | |
retBox = None | |
newBox = None | |
index = 0 | |
for box in boxes: | |
index = index+1 | |
print(index, box, size) | |
if box!=None and box[2]-box[0]>=size[0] and box[3]-box[1]>=size[1]: | |
retBox = [box[0], box[1], box[0]+size[0], box[1]+size[1]] | |
newBox = [box[0], box[1] + size[1], box[2], box[3]] | |
box[0] = box[0] + size[0] | |
box[3] = box[1] + size[1] | |
break | |
if newBox!=None and newBox[0]<newBox[2] and newBox[1]<newBox[3]: | |
boxes.append(newBox) | |
return retBox | |
def mergeBoxes(boxes): | |
mergeDict = dict() | |
mergeList = [] | |
for box in boxes: | |
key = '%d_%d' % (box[0], box[2]) | |
if key in mergeDict: | |
mergeDict[key].append(box) | |
else: | |
mergeDict[key] = [box] | |
for blist in mergeDict.values(): | |
l = len(blist) | |
if l<=1: | |
continue | |
slist = sorted(blist, key=lambda b:b[3]) | |
offset = 1 | |
for i in range(1,l): | |
if slist[i-offset][3] == slist[i][1]: | |
slist[i-offset][3] = slist[i][3] | |
slist[i][1] = slist[i][3] | |
offset = offset + 1 | |
mergeList.append(slist[i]) | |
else: | |
offset=1 | |
for box in mergeList: | |
boxes.remove(box) | |
def getCharId(id): | |
if id<128: | |
return id | |
else: | |
return (id/65536 % 16)*4096 + (id/256%64)*64 + (id%64) | |
def publishFont(dir, size): | |
dir = dir.replace('/', '') | |
maxBoundary = [1000, 1000, 0, 0] | |
flist = os.listdir(dir) | |
slist = [] | |
for f in flist: | |
if f.find(".png")!=-1: | |
try: | |
im = Image.open('%s/%s' % (dir, f)) | |
except: | |
print "notFind pic", id | |
continue | |
try: | |
box = list(im.getbbox()) | |
slist.append([f, box[3]-box[1]]) | |
except: | |
print "cropPic Error", id | |
continue | |
if box[0] < maxBoundary[0]: | |
maxBoundary[0] = box[0] | |
if box[1] < maxBoundary[1]: | |
maxBoundary[1] = box[1] | |
if box[2] > maxBoundary[2]: | |
maxBoundary[2] = box[2] | |
if box[3] > maxBoundary[3]: | |
maxBoundary[3] = box[3] | |
print maxBoundary | |
fheight = maxBoundary[3] - maxBoundary[1] | |
width = 512 | |
height = 256 | |
nim = Image.new('RGBA', (width, height), (0, 0, 0, 0)) | |
boxes = [[0, 0, width, height]] | |
chars = [] | |
chars.append([32, 0, 0, 0, 0, 0, fheight, fheight/2]) | |
slist = sorted(slist, key=lambda b:b[1], reverse=True) | |
flist = [f[0] for f in slist] | |
for f in flist: | |
if f.find(".png")!=-1: | |
im = Image.open('%s/%s' % (dir, f)) | |
cbox = list(im.getbbox()) | |
tim = im.crop(cbox) | |
s = [cbox[2]-cbox[0]+1, cbox[3]-cbox[1]+1] | |
box = getBox(boxes, s) | |
tbox = [box[0], box[1], box[2]-1, box[3]-1] | |
nim.paste(tim, tbox) | |
charInfo = [getCharId(int(f.split(".")[0])), tbox[0], tbox[1], tbox[2]-tbox[0], tbox[3]-tbox[1], cbox[0] - maxBoundary[0], cbox[1] - maxBoundary[1], tbox[2]-tbox[0]] | |
chars.append(charInfo) | |
if charInfo[0]==8221: | |
chars.append([34, tbox[0], tbox[1], tbox[2]-tbox[0], tbox[3]-tbox[1], cbox[0] - maxBoundary[0], cbox[1] - maxBoundary[1], tbox[2]-tbox[0]]) | |
elif charInfo[0]==8217: | |
chars.append([39, tbox[0], tbox[1], tbox[2]-tbox[0], tbox[3]-tbox[1], cbox[0] - maxBoundary[0], cbox[1] - maxBoundary[1], tbox[2]-tbox[0]]) | |
mergeBoxes(boxes) | |
picName = '%s.png' % dir | |
fntName = '%s.fnt' % dir | |
print picName, fntName | |
picName = picName.replace('/', '') | |
fntName = fntName.replace('/', '') | |
nim.save(picName) | |
chars = sorted(chars, key=lambda b:b[0]) | |
outputFile = codecs.open(fntName, "w", 'utf-8') | |
outputFile.write(u'info face="%s" size=%d bold=0 italic=0 charset="" unicode=0 stretchH=100 smooth=1 aa=1 padding=0,0,0,0 spacing=1,1\n' % (dir, size)) | |
outputFile.write(u'common lineHeight=%d base=%d scaleW=%d scaleH=%d pages=1 packed=0\n' % (fheight, size, width, height)) | |
outputFile.write(u'page id=0 file="%s"\n' % picName) | |
outputFile.write(u'chars count=%d\n' % len(chars)) | |
for charInfo in chars: | |
outputFile.write(u'char id=%d\tx=%d\ty=%d\twidth=%d\theight=%d\txoffset=%d\tyoffset=%d\txadvance=%d\tpage=0\tchnl=0\n' % (charInfo[0], charInfo[1], charInfo[2], charInfo[3], charInfo[4], charInfo[5], charInfo[6], charInfo[7])) | |
outputFile.write(u'kernings count=-1') | |
outputFile.close() | |
print sys.argv | |
def main(): | |
publishFont(sys.argv[1], int(sys.argv[2])) | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment