1.python文件处理
#!/usr/bin/env python
#-*- coding:utf-8 -*-
import os,sys
from xml.etree import ElementTree
reload(sys)
sys.setdefaultencoding( "utf-8" )
TOOLS_PATH = os.getcwd()
def tree_to_dict(tree):
count = 0
d = {}
pngName = ""
for index,item in enumerate(tree):
if item.tag == "key" and item.text == "frames":
for framesIndex,framesItem in enumerate(tree[index+1]):
if framesItem.tag == "key":
count = count + 1
d[count] = {}
d[count]['letter'] = os.path.splitext(framesItem.text)[0]
for frameIndex,frameItem in enumerate(tree[index+1][framesIndex+1]):
if frameItem.tag == "key" and frameItem.text == "frame":
frameData = tree[index+1][framesIndex+1][frameIndex+1].text
tempData = frameData.replace('{','').replace('}','').split(",")
d[count]['x'] = tempData[0]
d[count]['y'] = tempData[1]
d[count]['width'] = tempData[2]
d[count]['height'] = tempData[3]
if item.tag == "key" and item.text == "metadata":
for metadataIndex,metadataItem in enumerate(tree[index+1]):
if metadataItem.tag == "key" and metadataItem.text == "realTextureFileName":
pngName = tree[index+1][metadataIndex+1].text
fntFileName = TOOLS_PATH+"/"+os.path.splitext(pngName)[0]+".fnt"
openFile = open(fntFileName,'w')
openFile.write('info face=\"\" size=1 bold=0 italic=0 charset=\"\" unicode=0 stretchH=100 smooth=1 aa=1 padding=0,0,0,0 spacing=2,2')
openFile.write('\ncommon lineHeight={:d} base=0 scaleW=0 scaleH=0 pages=1 packed=0'.format(int(d[1]['height'])))
openFile.write("\npage id=0 file=\"{:s}\"\n".format(pngName))
openFile.write("chars count={:d}\n".format(len(d)))
for k in d:
openFile.write("char id={:d} x={:d} y={:d} width={:d} height={:d} xoffset=1 yoffset=0 xadvance={:d} page=0 chnl=0 letter=\"{:s}\"\n".format(int(ord(d[k]['letter'])),int(d[k]['x']),int(d[k]['y']),int(d[k]['width']),int(d[k]['height']),int(d[k]['width']),str(d[k]['letter'])))
openFile.write("kernings count=0")
openFile.close()
return d
def plist_to_fnt_change(plist_filename="/Users/liwenrui/Desktop/shareMidText.plist"):
print plist_filename
root = ElementTree.fromstring(open(plist_filename,'r').read())
tree_to_dict(root[0])
if __name__ == '__main__':
if len(sys.argv) > 1:
plist_to_fnt_change(sys.argv[1]);
else:
plist_to_fnt_change();
2.图片为命以图片的字为名字的png
3.然后打包成大图不允许旋转生成plist
4.pyton读取plist生成fnt 文件目录在同一个目录下 如果图片不一致导致上下不对齐 xadvance 手动调整或做高度一样图片
5.注意特殊字符需要手动处理和生成的Plist暂时没有处理路径问题。