Free cookie consent management tool by TermsFeed Policy Generator

source: branches/histogram/HeuristicLab.Persistence/3.3/Default/Xml/Primitive/System.Drawing/Font2XmlSerializer.cs @ 6020

Last change on this file since 6020 was 6020, checked in by abeham, 12 years ago

#1465

  • Fixed some bugs
  • Fixed out-of-sync issue in DataRowVisualPropertiesControl
  • Added border in histogram (white for very dark colors)
  • Added serializer for System.Drawing.Font
  • Added option to choose the font of the title as well as the axis of the chart
File size: 2.2 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2011 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
4 *
5 * This file is part of HeuristicLab.
6 *
7 * HeuristicLab is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * HeuristicLab is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
19 */
20#endregion
21
22using System;
23using System.Drawing;
24using System.Xml;
25
26namespace HeuristicLab.Persistence.Default.Xml.Primitive {
27  internal sealed class Font2XmlSerializer : PrimitiveXmlSerializerBase<Font> {
28
29    public override XmlString Format(Font font) {
30      XmlDocument doc = new XmlDocument();
31      XmlNode fontNode = doc.CreateElement("Font");
32      fontNode.Attributes.Append(doc.CreateAttribute("Name"));
33      fontNode.Attributes.Append(doc.CreateAttribute("Size"));
34      fontNode.Attributes.Append(doc.CreateAttribute("Style"));
35      if (font != null) {
36        fontNode.Attributes["Name"].Value = font.Name;
37        fontNode.Attributes["Size"].Value = font.Size.ToString();
38        fontNode.Attributes["Style"].Value = font.Style.ToString("d");
39      }
40      return new XmlString(fontNode.OuterXml);
41    }
42
43    public override Font Parse(XmlString fontData) {
44      XmlTextReader tr = new XmlTextReader(fontData.Data, XmlNodeType.Element, null);
45      if (!tr.Read() || tr.Name != "Font") return null;
46      string name = tr.GetAttribute("Name");
47      if (string.IsNullOrEmpty(name)) return null;
48      float size = 12.0f;
49      if (!float.TryParse(tr.GetAttribute("Size"), out size)) return null;
50      FontStyle style = FontStyle.Regular;
51      if (!Enum.TryParse<FontStyle>(tr.GetAttribute("Style"), out style)) return null;
52      return new Font(name, size, style);
53    }
54  }
55}
Note: See TracBrowser for help on using the repository browser.