1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196
| export class LabelBMFontHelper { private static FNT_HEAD = /fntframes [^\n]*(\n|$)/gi; private static FNT_FRAME_NAME = /fntframe [^\n]*(\n|$)/gi; private static INFO_EXP = /info [^\n]*(\n|$)/gi; private static COMMON_EXP = /common [^\n]*(\n|$)/gi; private static PAGE_EXP = /page [^\n]*(\n|$)/gi; private static CHAR_EXP = /char [^\n]*(\n|$)/gi; private static KERNING_EXP = /kerning [^\n]*(\n|$)/gi; private static ITEM_EXP = /\w+=[^ \r\n]+/gi; private static INT_EXP = /^[\-]?\d+$/;
public static parseFnt(fntStr, url) { let fnt = {}; let headString = fntStr.match(LabelBMFontHelper.FNT_HEAD); fnt = this.parseFntContent(fnt, fntStr, url, null); return fnt; }
private static parseStrToObj(str) { let arr = str.match(LabelBMFontHelper.ITEM_EXP); let obj = {}; if (arr) { for (let i = 0, li = arr.length; i < li; i++) { let tempStr = arr[i]; let index = tempStr.indexOf("="); let key = tempStr.substring(0, index); let value = tempStr.substring(index + 1); if (value.match(LabelBMFontHelper.INT_EXP)) value = parseInt(value); else if (value[0] === '"') value = value.substring(1, value.length - 1); obj[key] = value; } } return obj; }
private static parseFntContent(fnt, fntStr, url, useAtlas: any) { let self = this; let commonObj = self.parseStrToObj(fntStr.match(LabelBMFontHelper.COMMON_EXP)[0]); fnt.commonHeight = commonObj["lineHeight"]; var infoObj = self.parseStrToObj(fntStr.match(self.INFO_EXP)[0]); fnt.fontSize = infoObj["size"]; let padding = infoObj["padding"].split(","); fnt.padding = padding.map((item:string)=>{ if(item.indexOf(".")!=-1){ return parseFloat(item); } return parseInt(item); }); let pageObj = self.parseStrToObj(fntStr.match(LabelBMFontHelper.PAGE_EXP)[0]); if (pageObj["id"] !== 0) console.log("cc.LabelBMFontHelper._parseImageFileName() : file could not be found"); if (!useAtlas) { fnt.atlasName = SMPath.path.changeBasename(url, pageObj["file"], false); } else { fnt.atlasName = SMPath.path.join(SMPath.path.dirname(useAtlas.path) + pageObj["file"]); }
let charLines = fntStr.match(LabelBMFontHelper.CHAR_EXP); let fontDefDictionary = fnt.fontDefDictionary = {}; for (let i = 0, li = charLines.length; i < li; i++) { let charObj = self.parseStrToObj(charLines[i]); let charId = charObj["id"]; fontDefDictionary[charId] = { rect: {x: charObj["x"], y: charObj["y"], width: charObj["width"], height: charObj["height"]}, xoffset: charObj["xoffset"], yoffset: charObj["yoffset"], xadvance: charObj["xadvance"] }; }
let kerningDict = fnt.kerningDict = {}; let kerningLines = fntStr.match(LabelBMFontHelper.KERNING_EXP); if (kerningLines) { for (let i = 0, li = kerningLines.length; i < li; i++) { let kerningObj = self.parseStrToObj(kerningLines[i]); kerningDict[(kerningObj["first"] << 16) | (kerningObj["second"] & 0xffff)] = kerningObj["amount"]; } } return fnt; } }
export class SMPath { static get path(): SMPath { if (!this._Ins) this._Ins = new SMPath(); return this._Ins; }
private static _Ins: SMPath;
public join(data: any) { let l = arguments.length; let result = ""; for (let i = 0; i < l; i++) { result = (result + (result === "" ? "" : "/") + arguments[i]).replace(/(\/|\\\\)$/, ""); } return result; }
extname(pathStr) { let temp = /(\.[^\.\/\?\\]*)(\?.*)?$/.exec(pathStr); return temp ? temp[1] : null; }
dirname(pathStr) { return pathStr.replace(/((.*)(\/|\\|\\\\))?(.*?\..*$)?/, '$2'); }
changeExtname(pathStr, extname) { extname = extname || ""; let index = pathStr.indexOf("?"); let tempStr = ""; if (index > 0) { tempStr = pathStr.substring(index); pathStr = pathStr.substring(0, index); } index = pathStr.lastIndexOf("."); if (index < 0) return pathStr + extname + tempStr; return pathStr.substring(0, index) + extname + tempStr; }
changeBasename(pathStr, basename, isSameExt) { if (basename.indexOf(".") === 0) return this.changeExtname(pathStr, basename); let index = pathStr.indexOf("?"); let tempStr = ""; let ext = isSameExt ? this.extname(pathStr) : ""; if (index > 0) { tempStr = pathStr.substring(index); pathStr = pathStr.substring(0, index); } index = pathStr.lastIndexOf("/"); index = index <= 0 ? 0 : index + 1; return pathStr.substring(0, index) + basename + ext + tempStr; }
}
|