#region License Information
/* HeuristicLab
* Copyright (C) 2002-2011 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
*
* This file is part of HeuristicLab.
*
* HeuristicLab is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* HeuristicLab is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with HeuristicLab. If not, see .
*/
#endregion
using System;
using System.Drawing;
using System.Xml;
namespace HeuristicLab.Persistence.Default.Xml.Primitive {
internal sealed class Font2XmlSerializer : PrimitiveXmlSerializerBase {
public override XmlString Format(Font font) {
XmlDocument doc = new XmlDocument();
XmlNode fontNode = doc.CreateElement("Font");
fontNode.Attributes.Append(doc.CreateAttribute("Name"));
fontNode.Attributes.Append(doc.CreateAttribute("Size"));
fontNode.Attributes.Append(doc.CreateAttribute("Style"));
if (font != null) {
fontNode.Attributes["Name"].Value = font.Name;
fontNode.Attributes["Size"].Value = font.Size.ToString();
fontNode.Attributes["Style"].Value = font.Style.ToString("d");
}
return new XmlString(fontNode.OuterXml);
}
public override Font Parse(XmlString fontData) {
XmlTextReader tr = new XmlTextReader(fontData.Data, XmlNodeType.Element, null);
if (!tr.Read() || tr.Name != "Font") return null;
string name = tr.GetAttribute("Name");
if (string.IsNullOrEmpty(name)) return null;
float size = 12.0f;
if (!float.TryParse(tr.GetAttribute("Size"), out size)) return null;
FontStyle style = FontStyle.Regular;
if (!Enum.TryParse(tr.GetAttribute("Style"), out style)) return null;
return new Font(name, size, style);
}
}
}