public static Font Load(TextReader reader) { var document = XDocument.Load(reader); var fontNode = document.Root; var font = new Font(); font.Name = fontNode.Attribute("name").Value; font.Style = fontNode.Attribute("style").Value; font.Version = fontNode.Attribute("version").Value; foreach (var letterNode in fontNode.Elements("letter")) { var letter = new Letter(); letter.Character = (char)(int)letterNode.Attribute("code"); letter.Start = (double)letterNode.Attribute("start"); letter.End = (double)letterNode.Attribute("end"); letter.Vectors = new List<List<Vector3d>>(); foreach (var pathNode in letterNode.Elements("path")) { var path = new List<Vector3d>(); var lastX = (double)pathNode.Attribute("x"); var lastY = (double)pathNode.Attribute("y"); path.Add(new Vector3d(lastX, lastY, 0)); foreach (var toNode in pathNode.Elements("to")) { var toX = (double)toNode.Attribute("x"); var toY = (double)toNode.Attribute("y"); if (toNode.Attribute("b") == null) { path.Add(Vector3d.Zero); } else { var b = (double)toNode.Attribute("b"); var chordX = toX - lastX; var chordY = toY - lastY; var deltaOver2 = 2 * Math.Atan(b); var cs = Math.Cos(deltaOver2); var sn = Math.Sin(deltaOver2); var tangentX = chordX * cs - chordY * sn; var tangentY = chordX * sn + chordY * cs; var tangent = new Vector3d(tangentX, tangentY, 0); path.Add(tangent); } path.Add(new Vector3d(toX, toY, 0)); lastX = toX; lastY = toY; } letter.Vectors.Add(path); } font.Letters.Add(letter); } return font; }
public Typewriter(Font font) { Letters = font.Letters.ToDictionary(o => o.Character); }
public static void Save(Stream stream, Font font) { var xml = new XElement("bbfont", new XAttribute("name", font.Name), new XAttribute("style", font.Style), new XAttribute("version", font.Version), LettersToXml(font.Letters)); var document = new XDocument(xml); document.Save(stream); }