#!/usr/bin/python # -*- coding:utf-8 -*- import sys import os import math from PIL import Image import os,sys,shutil,subprocess,glob import fnmatch import re pattern = re.compile( "([a-zA-Z]+)=([0-9]+)") def parse( input): result=pattern.findall( input) return result class FontCommon: lineHeight:int = 0 base:int = 0 source: str = "" def __init__( self, input: str): self.source = input params=parse(input) for param in params: if hasattr( self, param[0]): setattr( self, param[0], int(param[1])) def tostr( self): return self.source class FontChar: id: int = 0 x: int = 0 y: int = 0 width: int = 0 height: int = 0 xoffset: int = 0 yoffset: int = 0 xadvance: int = 0 page: int = 0 chnl: int = 0 def __init__( self, input): params=parse(input) for param in params: if hasattr( self, param[0]): setattr( self, param[0], int(param[1])) def tostr(self): temp=["char"] temp.append("id={}".format(self.id)) temp.append("x={}".format(self.x)) temp.append("y={}".format(self.y)) temp.append("width={}".format(self.width)) temp.append("height={}".format(self.height)) temp.append("xoffset={}".format(self.xoffset)) temp.append("yoffset={}".format(self.yoffset)) temp.append("xadvance={}".format(self.xadvance)) temp.append("page={}".format(self.page)) temp.append("chnl={}\n".format(self.chnl)) return " ".join( temp) def recursive_glob(treeroot, pattern): results = [] for base, _, files in os.walk(treeroot): goodfiles = fnmatch.filter(files, pattern) results.extend(os.path.join(base, f) for f in goodfiles) return results input_path = 'fonts/' files = recursive_glob( input_path, "*.fnt") output = "fonts_n/" if not os.path.exists( output): os.mkdir( output) for fname in files: with open(os.path.join( output, os.path.split( fname)[1]), mode="w", encoding='UTF-8') as out: with open(fname, mode='r', encoding='UTF-8') as f: try: lines = f.readlines() com:FontCommon = None for line in lines: if line.startswith("common"): com = FontCommon( line) out.write( line) elif line.startswith("chars"): out.write( line) elif line.startswith("char"): fchar= FontChar(line) if com: fchar.yoffset = int(fchar.yoffset - (com.lineHeight-com.base)*0.5) out.write( fchar.tostr()) else: out.write( line) finally: f.close() out.close()