/// <summary> /// Loads the data to render the bitmap text with. /// </summary> /// <param name="data">The data.</param> /// <param name="dataType">Type of the data.</param> /// <returns>The BitmapFont</returns> public BitmapFont LoadData(string data, BitmapFontDataType dataType) { FontData = data; DataType = dataType; switch (DataType) { case BitmapFontDataType.Littera: var xmlDoc = new XmlDocument(); xmlDoc.LoadXml(data); foreach (XmlNode n in xmlDoc.SelectNodes("//common")) { LineSpacing = n.AttributeInt("lineHeight"); } foreach (XmlNode n in xmlDoc.SelectNodes("//char")) { var c = new CharData(); c.CharacterId = n.AttributeInt("id"); c.Width = n.AttributeInt("width"); c.Height = n.AttributeInt("height"); c.X = n.AttributeInt("x"); c.Y = n.AttributeInt("y"); c.OffsetX = n.AttributeInt("xoffset"); c.OffsetY = n.AttributeInt("yoffset"); c.Advance = n.AttributeInt("xadvance"); c.Character = (char)c.CharacterId; if (!charData.ContainsKey(c.Character)) { charData.Add(c.Character, c); } } foreach (XmlNode n in xmlDoc.SelectNodes("//kerning")) { var first = (char)n.AttributeInt("first"); var second = (char)n.AttributeInt("second"); var amount = n.AttributeInt("amount"); AddKerning(first, second, amount); } break; case BitmapFontDataType.BMFontXml: xmlDoc = new XmlDocument(); xmlDoc.LoadXml(data); foreach (XmlNode n in xmlDoc.SelectNodes("//common")) { LineSpacing = n.AttributeInt("lineHeight"); } foreach (XmlNode n in xmlDoc.SelectNodes("//char")) { var c = new CharData(); c.CharacterId = n.AttributeInt("id"); c.Width = n.AttributeInt("width"); c.Height = n.AttributeInt("height"); c.X = n.AttributeInt("x"); c.Y = n.AttributeInt("y"); c.OffsetX = n.AttributeInt("xoffset"); c.OffsetY = n.AttributeInt("yoffset"); c.Advance = n.AttributeInt("xadvance"); c.Character = (char)c.CharacterId; if (!charData.ContainsKey(c.Character)) { charData.Add(c.Character, c); } } foreach (XmlNode n in xmlDoc.SelectNodes("//kerning")) { var first = (char)n.AttributeInt("first"); var second = (char)n.AttributeInt("second"); var amount = n.AttributeInt("amount"); AddKerning(first, second, amount); } break; case BitmapFontDataType.BMFontText: var lines = data.Split(new string[] { "\r\n", "\n" }, StringSplitOptions.RemoveEmptyEntries); foreach (var line in lines) { var entries = line.Split(new string[] { " " }, StringSplitOptions.RemoveEmptyEntries); var header = entries[0]; var textData = new Dictionary<string, string>(); switch (header) { case "info": // Dont need any of this for now. break; case "common": foreach (var e in entries) { if (!e.Contains('=')) continue; textData.Add(GetKeyValueFromString(e)[0], GetKeyValueFromString(e)[1]); } LineSpacing = int.Parse(textData["lineHeight"]); break; case "char": var c = new CharData(); foreach (var e in entries) { if (!e.Contains('=')) continue; textData.Add(GetKeyValueFromString(e)[0], GetKeyValueFromString(e)[1]); } c.CharacterId = int.Parse(textData["id"]); c.Character = (char)int.Parse(textData["id"]); c.Width = int.Parse(textData["width"]); c.Height = int.Parse(textData["height"]); c.X = int.Parse(textData["x"]); c.Y = int.Parse(textData["y"]); c.OffsetX = int.Parse(textData["xoffset"]); c.OffsetY = int.Parse(textData["yoffset"]); c.Advance = int.Parse(textData["xadvance"]); if (!charData.ContainsKey(c.Character)) { charData.Add(c.Character, c); } break; case "kerning": foreach (var e in entries) { if (!e.Contains('=')) continue; textData.Add(GetKeyValueFromString(e)[0], GetKeyValueFromString(e)[1]); } AddKerning((char)int.Parse(textData["first"]), (char)int.Parse(textData["second"]), int.Parse(textData["amount"])); break; } } break; case BitmapFontDataType.CodeheadCSV: lines = data.Split(new string[] { "\r\n", "\n" }, StringSplitOptions.RemoveEmptyEntries); var csv = new Dictionary<string, string>(); foreach(var line in lines) { csv.Add(GetKeyValueFromString(line, ',')[0], GetKeyValueFromString(line, ',')[1]); } var imgWidth = int.Parse(csv["Image Width"]); var startChar = int.Parse(csv["Start Char"]); var imgCellWidth = int.Parse(csv["Cell Width"]); var imgCellHeight = int.Parse(csv["Cell Height"]); var charsPerRow = (int)Util.Floor(imgWidth / imgCellWidth); for (var i = 0; i < 256; i++) { var c = new CharData(); c.Width = int.Parse(csv[string.Format("Char {0} Base Width", i)]); c.OffsetX = int.Parse(csv[string.Format("Char {0} X Offset", i)]); c.OffsetY = int.Parse(csv[string.Format("Char {0} Y Offset", i)]); var charPos = i - startChar; c.X = Util.TwoDeeX(charPos, charsPerRow) * imgCellWidth; c.Y = Util.TwoDeeY(charPos, charsPerRow) * imgCellHeight; var widthOffset = int.Parse(csv[string.Format("Char {0} Width Offset", i)]); c.Height = int.Parse(csv["Font Height"]); c.Advance = c.Width + widthOffset; c.Character = (char)i; LineSpacing = c.Height; if (!charData.ContainsKey(c.Character)) { charData.Add(c.Character, c); } } break; case BitmapFontDataType.Shoebox: lines = data.Split(new string[] { "\r\n", "\n" }, StringSplitOptions.RemoveEmptyEntries); foreach (var line in lines) { var entries = line.Split(new string[] { " " }, StringSplitOptions.RemoveEmptyEntries); var header = entries[0]; var textData = new Dictionary<string, string>(); switch (header) { case "info": break; case "common": foreach (var e in entries) { if (!e.Contains('=')) continue; textData.Add(GetKeyValueFromString(e)[0], GetKeyValueFromString(e)[1]); } LineSpacing = int.Parse(textData["lineHeight"]); break; case "char": var c = new CharData(); foreach (var e in entries) { if (!e.Contains('=')) continue; textData.Add(GetKeyValueFromString(e)[0], GetKeyValueFromString(e)[1]); } c.CharacterId = int.Parse(textData["id"]); c.Character = (char)int.Parse(textData["id"]); c.Width = int.Parse(textData["width"]); c.Height = int.Parse(textData["height"]); c.X = int.Parse(textData["x"]); c.Y = int.Parse(textData["y"]); c.OffsetX = int.Parse(textData["xoffset"]); c.OffsetY = int.Parse(textData["yoffset"]); c.Advance = int.Parse(textData["xadvance"]); charData.Add(c.Character, c); break; case "kerning": foreach (var e in entries) { if (!e.Contains('=')) continue; textData.Add(GetKeyValueFromString(e)[0], GetKeyValueFromString(e)[1]); } AddKerning((char)int.Parse(textData["first"]), (char)int.Parse(textData["second"]), int.Parse(textData["amount"])); break; } } break; } return this; }
/// <summary> /// Loads the data to render the bitmap text with. /// </summary> /// <param name="data">The data.</param> /// <param name="dataType">Type of the data.</param> /// <returns>The BitmapFont</returns> public BitmapFont LoadData(string data, BitmapFontDataType dataType) { FontData = data; DataType = dataType; switch (DataType) { case BitmapFontDataType.Littera: var xmlDoc = new XmlDocument(); xmlDoc.LoadXml(data); foreach (XmlNode n in xmlDoc.SelectNodes("//common")) { LineSpacing = n.AttributeInt("lineHeight"); } foreach (XmlNode n in xmlDoc.SelectNodes("//char")) { var c = new CharData(); c.CharacterId = n.AttributeInt("id"); c.Width = n.AttributeInt("width"); c.Height = n.AttributeInt("height"); c.X = n.AttributeInt("x"); c.Y = n.AttributeInt("y"); c.OffsetX = n.AttributeInt("xoffset"); c.OffsetY = n.AttributeInt("yoffset"); c.Advance = n.AttributeInt("xadvance"); c.Character = (char)c.CharacterId; if (!charData.ContainsKey(c.Character)) { charData.Add(c.Character, c); } } foreach (XmlNode n in xmlDoc.SelectNodes("//kerning")) { var first = (char)n.AttributeInt("first"); var second = (char)n.AttributeInt("second"); var amount = n.AttributeInt("amount"); AddKerning(first, second, amount); } break; case BitmapFontDataType.BMFontXml: xmlDoc = new XmlDocument(); xmlDoc.LoadXml(data); foreach (XmlNode n in xmlDoc.SelectNodes("//common")) { LineSpacing = n.AttributeInt("lineHeight"); } foreach (XmlNode n in xmlDoc.SelectNodes("//char")) { var c = new CharData(); c.CharacterId = n.AttributeInt("id"); c.Width = n.AttributeInt("width"); c.Height = n.AttributeInt("height"); c.X = n.AttributeInt("x"); c.Y = n.AttributeInt("y"); c.OffsetX = n.AttributeInt("xoffset"); c.OffsetY = n.AttributeInt("yoffset"); c.Advance = n.AttributeInt("xadvance"); c.Character = (char)c.CharacterId; if (!charData.ContainsKey(c.Character)) { charData.Add(c.Character, c); } } foreach (XmlNode n in xmlDoc.SelectNodes("//kerning")) { var first = (char)n.AttributeInt("first"); var second = (char)n.AttributeInt("second"); var amount = n.AttributeInt("amount"); AddKerning(first, second, amount); } break; case BitmapFontDataType.BMFontText: var lines = data.Split(new string[] { "\r\n", "\n" }, StringSplitOptions.RemoveEmptyEntries); foreach (var line in lines) { var entries = line.Split(new string[] { " " }, StringSplitOptions.RemoveEmptyEntries); var header = entries[0]; var textData = new Dictionary <string, string>(); switch (header) { case "info": // Dont need any of this for now. break; case "common": foreach (var e in entries) { if (!e.Contains('=')) { continue; } textData.Add(GetKeyValueFromString(e)[0], GetKeyValueFromString(e)[1]); } LineSpacing = int.Parse(textData["lineHeight"]); break; case "char": var c = new CharData(); foreach (var e in entries) { if (!e.Contains('=')) { continue; } textData.Add(GetKeyValueFromString(e)[0], GetKeyValueFromString(e)[1]); } c.CharacterId = int.Parse(textData["id"]); c.Character = (char)int.Parse(textData["id"]); c.Width = int.Parse(textData["width"]); c.Height = int.Parse(textData["height"]); c.X = int.Parse(textData["x"]); c.Y = int.Parse(textData["y"]); c.OffsetX = int.Parse(textData["xoffset"]); c.OffsetY = int.Parse(textData["yoffset"]); c.Advance = int.Parse(textData["xadvance"]); if (!charData.ContainsKey(c.Character)) { charData.Add(c.Character, c); } break; case "kerning": foreach (var e in entries) { if (!e.Contains('=')) { continue; } textData.Add(GetKeyValueFromString(e)[0], GetKeyValueFromString(e)[1]); } AddKerning((char)int.Parse(textData["first"]), (char)int.Parse(textData["second"]), int.Parse(textData["amount"])); break; } } break; case BitmapFontDataType.CodeheadCSV: lines = data.Split(new string[] { "\r\n", "\n" }, StringSplitOptions.RemoveEmptyEntries); var csv = new Dictionary <string, string>(); foreach (var line in lines) { csv.Add(GetKeyValueFromString(line, ',')[0], GetKeyValueFromString(line, ',')[1]); } var imgWidth = int.Parse(csv["Image Width"]); var startChar = int.Parse(csv["Start Char"]); var imgCellWidth = int.Parse(csv["Cell Width"]); var imgCellHeight = int.Parse(csv["Cell Height"]); var charsPerRow = (int)Util.Floor(imgWidth / imgCellWidth); for (var i = 0; i < 256; i++) { var c = new CharData(); c.Width = int.Parse(csv[string.Format("Char {0} Base Width", i)]); c.OffsetX = int.Parse(csv[string.Format("Char {0} X Offset", i)]); c.OffsetY = int.Parse(csv[string.Format("Char {0} Y Offset", i)]); var charPos = i - startChar; c.X = Util.TwoDeeX(charPos, charsPerRow) * imgCellWidth; c.Y = Util.TwoDeeY(charPos, charsPerRow) * imgCellHeight; var widthOffset = int.Parse(csv[string.Format("Char {0} Width Offset", i)]); c.Height = int.Parse(csv["Font Height"]); c.Advance = c.Width + widthOffset; c.Character = (char)i; LineSpacing = c.Height; if (!charData.ContainsKey(c.Character)) { charData.Add(c.Character, c); } } break; case BitmapFontDataType.Shoebox: lines = data.Split(new string[] { "\r\n", "\n" }, StringSplitOptions.RemoveEmptyEntries); foreach (var line in lines) { var entries = line.Split(new string[] { " " }, StringSplitOptions.RemoveEmptyEntries); var header = entries[0]; var textData = new Dictionary <string, string>(); switch (header) { case "info": break; case "common": foreach (var e in entries) { if (!e.Contains('=')) { continue; } textData.Add(GetKeyValueFromString(e)[0], GetKeyValueFromString(e)[1]); } LineSpacing = int.Parse(textData["lineHeight"]); break; case "char": var c = new CharData(); foreach (var e in entries) { if (!e.Contains('=')) { continue; } textData.Add(GetKeyValueFromString(e)[0], GetKeyValueFromString(e)[1]); } c.CharacterId = int.Parse(textData["id"]); c.Character = (char)int.Parse(textData["id"]); c.Width = int.Parse(textData["width"]); c.Height = int.Parse(textData["height"]); c.X = int.Parse(textData["x"]); c.Y = int.Parse(textData["y"]); c.OffsetX = int.Parse(textData["xoffset"]); c.OffsetY = int.Parse(textData["yoffset"]); c.Advance = int.Parse(textData["xadvance"]); charData.Add(c.Character, c); break; case "kerning": foreach (var e in entries) { if (!e.Contains('=')) { continue; } textData.Add(GetKeyValueFromString(e)[0], GetKeyValueFromString(e)[1]); } AddKerning((char)int.Parse(textData["first"]), (char)int.Parse(textData["second"]), int.Parse(textData["amount"])); break; } } break; } return(this); }