/// <summary> /// 分解字符串 /// </summary> /// /// <param name="string"></param> /// <param name="tag"></param> /// <returns></returns> public static string[] Split(string s, string tag) { StringTokenizer str = new StringTokenizer(s, tag); string[] result = new string[str.CountTokens()]; int index = 0; for (; str.HasMoreTokens(); ) { result[index++] = str.NextToken(); } return result; }
private CharDef ParseChar(string line) { CharDef def = new CharDef(this); StringTokenizer tokens = new StringTokenizer(line, " ="); tokens.NextToken(); tokens.NextToken(); def.id = short.Parse(tokens.NextToken()); if (def.id < 0) { return null; } if (def.id > DEFAULT_MAX_CHAR) { throw new Exception(def.id + " > " + DEFAULT_MAX_CHAR); } tokens.NextToken(); def.tx = short.Parse(tokens.NextToken()); tokens.NextToken(); def.ty = short.Parse(tokens.NextToken()); tokens.NextToken(); def.width = short.Parse(tokens.NextToken()); tokens.NextToken(); def.height = short.Parse(tokens.NextToken()); tokens.NextToken(); def.xoffset = short.Parse(tokens.NextToken()); tokens.NextToken(); def.yoffset = short.Parse(tokens.NextToken()); tokens.NextToken(); def.advance = short.Parse(tokens.NextToken()); if (def.id != ' ') { lineHeight = MathUtils.Max(def.height + def.yoffset, lineHeight); } return def; }
private void Parse(Stream file) { if (displays == null) { displays = new ArrayMap(DEFAULT_MAX_CHAR); } else { displays.Clear(); } try { StreamReader ins = new StreamReader(file, System.Text.Encoding.UTF8); info = ins.ReadLine(); common = ins.ReadLine(); page = ins.ReadLine(); ArrayMap kerning = new ArrayMap( 64); List<CharDef> charDefs = new List<CharDef>( DEFAULT_MAX_CHAR); int maxChar = 0; bool done = false; for (; !done;) { string line = ins.ReadLine(); if (line == null) { done = true; } else { if (line.StartsWith("chars c")) { } else if (line.StartsWith("char")) { CharDef def = ParseChar(line); if (def != null) { maxChar = MathUtils.Max(maxChar, def.id); charDefs.Add(def); } } if (line.StartsWith("kernings c")) { } else if (line.StartsWith("kerning")) { StringTokenizer tokens = new StringTokenizer(line, " ="); tokens.NextToken(); tokens.NextToken(); short first = short.Parse(tokens.NextToken()); tokens.NextToken(); int second = int.Parse(tokens.NextToken()); tokens.NextToken(); int offset = int.Parse(tokens.NextToken()); List<short> values = (List<short>)kerning.GetValue(first); if (values == null) { values = new List<short>(); kerning.Put(first, values); } values.Add((short) ((offset << 8) | second)); } } } this.chars = new CharDef[maxChar + 1]; for (IEnumerator<CharDef> iter = charDefs.GetEnumerator(); iter.MoveNext();) { CharDef def = (CharDef) iter.Current; chars[def.id] = def; } ArrayMap.Entry[] entrys = kerning.ToEntrys(); for (int j=0;j<entrys.Length;j++) { ArrayMap.Entry entry = entrys[j]; short first = (short) entry.GetKey(); List<short> valueList = (List<short>) entry.GetValue(); short[] valueArray = new short[valueList.Count]; int i = 0; for (IEnumerator<short> valueIter = valueList.GetEnumerator(); valueIter .MoveNext(); i++) { valueArray[i] = (short) valueIter.Current; } chars[first].kerning = valueArray; } } catch (IOException e) { Log.Exception(e); throw new Exception("Invalid font file: " + file); } }