Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 6022 was 6022, checked in by abeham, 13 years ago

#1465

  • Updated branch with changes from the trunk
  • Updated Font2XmlSerializer according to Erik's suggestions
File size: 2.1 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.Globalization;
25
26namespace HeuristicLab.Persistence.Default.Xml.Primitive {
27  internal sealed class Font2XmlSerializer : PrimitiveXmlSerializerBase<Font> {
28
29    public override XmlString Format(Font font) {
30      if (font != null) {
31        return new XmlString(font.Name
32          + ";" + font.SizeInPoints.ToString(CultureInfo.InvariantCulture.NumberFormat)
33          + ";" + font.Style.ToString("d")
34          + ";" + font.GdiCharSet.ToString());
35      }
36      return new XmlString(string.Empty);
37    }
38
39    public override Font Parse(XmlString fontData) {
40      if (string.IsNullOrEmpty(fontData.Data)) return null;
41      string[] tokens = fontData.Data.Split(';');
42      if (tokens.Length < 4) return null;
43      float size = 12.0f;
44      FontStyle style = FontStyle.Regular;
45      byte gdiCharSet = 1;
46      if (!float.TryParse(tokens[1], NumberStyles.Float, CultureInfo.InvariantCulture.NumberFormat, out size))
47        return null;
48      if (!Enum.TryParse(tokens[2], out style))
49        return null;
50      if (!byte.TryParse(tokens[3], out gdiCharSet))
51        return null;
52
53      return new Font(tokens[0], size, style, GraphicsUnit.Point, gdiCharSet);
54    }
55  }
56}
Note: See TracBrowser for help on using the repository browser.