public static BitmapFontInfo ReadText(IReadOnlyList <string> lineSegments) { var bitmapFontInfo = new BitmapFontInfo(); bitmapFontInfo.Face = TextFormatUtility.ReadString("face", lineSegments, string.Empty); bitmapFontInfo.Size = TextFormatUtility.ReadInt("size", lineSegments); bitmapFontInfo.Bold = TextFormatUtility.ReadBool("bold", lineSegments); bitmapFontInfo.Italic = TextFormatUtility.ReadBool("italic", lineSegments); bitmapFontInfo.Charset = TextFormatUtility.ReadString("charset", lineSegments, string.Empty); bitmapFontInfo.Unicode = TextFormatUtility.ReadBool("unicode", lineSegments); bitmapFontInfo.StretchHeight = TextFormatUtility.ReadInt("stretchH", lineSegments); bitmapFontInfo.Smooth = TextFormatUtility.ReadBool("smooth", lineSegments); bitmapFontInfo.SuperSamplingLevel = TextFormatUtility.ReadInt("aa", lineSegments); var padding = TextFormatUtility.ReadValue("padding", lineSegments)?.Split(','); if (padding != null) { bitmapFontInfo.PaddingLeft = padding.Length > 3 ? int.Parse(padding[3]) : 0; bitmapFontInfo.PaddingDown = padding.Length > 2 ? int.Parse(padding[2]) : 0; bitmapFontInfo.PaddingRight = padding.Length > 1 ? int.Parse(padding[1]) : 0; bitmapFontInfo.PaddingUp = padding.Length > 0 ? int.Parse(padding[0]) : 0; } var spacing = TextFormatUtility.ReadValue("spacing", lineSegments)?.Split(','); if (spacing != null) { bitmapFontInfo.SpacingVertical = spacing.Length > 1 ? int.Parse(spacing[1]) : 0; bitmapFontInfo.SpacingHorizontal = spacing.Length > 0 ? int.Parse(spacing[0]) : 0; } bitmapFontInfo.Outline = TextFormatUtility.ReadInt("outline", lineSegments); return(bitmapFontInfo); }
public static BitmapFontInfo ReadXML(XElement element) { var bitmapFontInfo = new BitmapFontInfo(); bitmapFontInfo.Face = (string)element.Attribute("face") ?? string.Empty; bitmapFontInfo.Size = (int?)element.Attribute("size") ?? 0; bitmapFontInfo.Bold = (bool?)element.Attribute("bold") ?? false; bitmapFontInfo.Italic = (bool?)element.Attribute("italic") ?? false; bitmapFontInfo.Charset = (string)element.Attribute("charset") ?? string.Empty; bitmapFontInfo.Unicode = (bool?)element.Attribute("unicode") ?? false; bitmapFontInfo.StretchHeight = (int?)element.Attribute("stretchH") ?? 0; bitmapFontInfo.Smooth = (bool?)element.Attribute("smooth") ?? false; bitmapFontInfo.SuperSamplingLevel = (int?)element.Attribute("aa") ?? 0; var padding = ((string)element.Attribute("padding"))?.Split(','); if (padding != null) { bitmapFontInfo.PaddingLeft = padding.Length > 3 ? int.Parse(padding[3]) : 0; bitmapFontInfo.PaddingDown = padding.Length > 2 ? int.Parse(padding[2]) : 0; bitmapFontInfo.PaddingRight = padding.Length > 1 ? int.Parse(padding[1]) : 0; bitmapFontInfo.PaddingUp = padding.Length > 0 ? int.Parse(padding[0]) : 0; } var spacing = ((string)element.Attribute("spacing"))?.Split(','); if (spacing != null) { bitmapFontInfo.SpacingVertical = spacing.Length > 1 ? int.Parse(spacing[1]) : 0; bitmapFontInfo.SpacingHorizontal = spacing.Length > 0 ? int.Parse(spacing[0]) : 0; } bitmapFontInfo.Outline = (int?)element.Attribute("outline") ?? 0; return(bitmapFontInfo); }
public static BitmapFontInfo ReadBinary(BinaryReader binaryReader) { if (binaryReader.ReadInt32() < MinSizeInBytes) { throw new InvalidDataException("Invalid info block size."); } var bitmapFontInfo = new BitmapFontInfo(); bitmapFontInfo.Size = binaryReader.ReadInt16(); var bitField = binaryReader.ReadByte(); bitmapFontInfo.Smooth = bitField.IsBitSet(7); bitmapFontInfo.Unicode = bitField.IsBitSet(6); bitmapFontInfo.Italic = bitField.IsBitSet(5); bitmapFontInfo.Bold = bitField.IsBitSet(4); var characterSet = (CharacterSet)binaryReader.ReadByte(); bitmapFontInfo.Charset = characterSet.ToString().ToUpper(); bitmapFontInfo.StretchHeight = binaryReader.ReadUInt16(); bitmapFontInfo.SuperSamplingLevel = binaryReader.ReadByte(); bitmapFontInfo.PaddingUp = binaryReader.ReadByte(); bitmapFontInfo.PaddingRight = binaryReader.ReadByte(); bitmapFontInfo.PaddingDown = binaryReader.ReadByte(); bitmapFontInfo.PaddingLeft = binaryReader.ReadByte(); bitmapFontInfo.SpacingHorizontal = binaryReader.ReadByte(); bitmapFontInfo.SpacingVertical = binaryReader.ReadByte(); bitmapFontInfo.Outline = binaryReader.ReadByte(); bitmapFontInfo.Face = binaryReader.ReadNullTerminatedString(); return(bitmapFontInfo); }
public static BitmapFont ReadText(TextReader textReader) { var bitmapFont = new BitmapFont(); while (textReader.Peek() != -1) { var lineSegments = TextFormatUtility.GetSegments(textReader.ReadLine()); switch (lineSegments[0]) { case "info": { bitmapFont.Info = BitmapFontInfo.ReadText(lineSegments); break; } case "common": { bitmapFont.Common = BitmapFontCommon.ReadText(lineSegments, out var pageCount); bitmapFont.Pages = new Dictionary <int, string>(pageCount); break; } case "page": { bitmapFont.Pages = bitmapFont.Pages ?? new Dictionary <int, string>(); var id = TextFormatUtility.ReadInt("id", lineSegments); var file = TextFormatUtility.ReadString("file", lineSegments); bitmapFont.Pages[id] = file; break; } case "chars": { var count = TextFormatUtility.ReadInt("count", lineSegments); bitmapFont.Characters = new Dictionary <int, Character>(count); for (var i = 0; i < count; i++) { var characterLineSegments = TextFormatUtility.GetSegments(textReader.ReadLine()); var character = Character.ReadText(characterLineSegments, out var id); bitmapFont.Characters[id] = character; } break; } case "kernings": { var count = TextFormatUtility.ReadInt("count", lineSegments); bitmapFont.KerningPairs = new Dictionary <KerningPair, int>(count); for (var i = 0; i < count; i++) { var kerningLineSegments = TextFormatUtility.GetSegments(textReader.ReadLine()); var kerningPair = KerningPair.ReadText(kerningLineSegments, out var amount); if (bitmapFont.KerningPairs.ContainsKey(kerningPair)) { continue; } bitmapFont.KerningPairs[kerningPair] = amount; } break; } } } return(bitmapFont); }
public static BitmapFont ReadXML(TextReader textReader) { var bitmapFont = new BitmapFont(); var document = XDocument.Load(textReader); var fontElement = document.Element("font"); if (fontElement == null) { throw new InvalidDataException("Missing root font element in XML file."); } // Info var infoElement = fontElement.Element("info"); if (infoElement != null) { bitmapFont.Info = BitmapFontInfo.ReadXML(infoElement); } // Common var pages = 0; var commonElement = fontElement.Element("common"); if (commonElement != null) { bitmapFont.Common = BitmapFontCommon.ReadXML(commonElement, out pages); } // Pages var pagesElement = fontElement.Element("pages"); if (pagesElement != null) { bitmapFont.Pages = new Dictionary <int, string>(pages); foreach (var pageElement in pagesElement.Elements("page")) { var id = (int?)pageElement.Attribute("id") ?? 0; var name = (string)pageElement.Attribute("file"); bitmapFont.Pages[id] = name; } } // Characters var charactersElement = fontElement.Element("chars"); if (charactersElement != null) { var count = (int?)charactersElement.Attribute("count") ?? 0; bitmapFont.Characters = new Dictionary <int, Character>(count); foreach (var characterElement in charactersElement.Elements("char")) { var character = Character.ReadXML(characterElement, out var id); bitmapFont.Characters[id] = character; } } // Kernings var kerningsElement = fontElement.Element("kernings"); if (kerningsElement != null) { var count = (int?)kerningsElement.Attribute("count") ?? 0; bitmapFont.KerningPairs = new Dictionary <KerningPair, int>(count); foreach (var kerningElement in kerningsElement.Elements("kerning")) { var kerningPair = KerningPair.ReadXML(kerningElement, out var amount); if (bitmapFont.KerningPairs.ContainsKey(kerningPair)) { continue; } bitmapFont.KerningPairs[kerningPair] = amount; } } return(bitmapFont); }
public static BitmapFont ReadBinary(BinaryReader binaryReader) { var bitmapFont = new BitmapFont(); var magicOne = binaryReader.ReadByte(); var magicTwo = binaryReader.ReadByte(); var magicThree = binaryReader.ReadByte(); if (magicOne != MagicOne || magicTwo != MagicTwo || magicThree != MagicThree) { throw new InvalidDataException("File is not an FNT bitmap font or it is not in the binary format."); } if (binaryReader.ReadByte() != ImplementedVersion) { throw new InvalidDataException("The version specified is different from the implemented version."); } var pageCount = 0; while (binaryReader.PeekChar() != -1) { var blockID = (BlockID)binaryReader.ReadByte(); switch (blockID) { case BlockID.Info: { bitmapFont.Info = BitmapFontInfo.ReadBinary(binaryReader); break; } case BlockID.Common: { bitmapFont.Common = BitmapFontCommon.ReadBinary(binaryReader, out pageCount); break; } case BlockID.Pages: { binaryReader.ReadInt32(); bitmapFont.Pages = new Dictionary <int, string>(pageCount); for (var i = 0; i < pageCount; i++) { bitmapFont.Pages[i] = binaryReader.ReadNullTerminatedString(); } break; } case BlockID.Characters: { var characterBlockSize = binaryReader.ReadInt32(); if (characterBlockSize % Character.SizeInBytes != 0) { throw new InvalidDataException("Invalid character block size."); } var characterCount = characterBlockSize / Character.SizeInBytes; bitmapFont.Characters = new Dictionary <int, Character>(characterCount); for (var i = 0; i < characterCount; i++) { var character = Character.ReadBinary(binaryReader, out var id); bitmapFont.Characters[id] = character; } break; } case BlockID.KerningPairs: { var kerningBlockSize = binaryReader.ReadInt32(); if (kerningBlockSize % KerningPair.SizeInBytes != 0) { throw new InvalidDataException("Invalid kerning block size."); } var kerningCount = kerningBlockSize / KerningPair.SizeInBytes; bitmapFont.KerningPairs = new Dictionary <KerningPair, int>(kerningCount); for (var i = 0; i < kerningCount; i++) { var kerningPair = KerningPair.ReadBinary(binaryReader, out var amount); if (bitmapFont.KerningPairs.ContainsKey(kerningPair)) { continue; } bitmapFont.KerningPairs[kerningPair] = amount; } break; } default: { throw new InvalidDataException("Invalid block ID."); } } } return(bitmapFont); }