Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
04/19/11 12:37:16 (14 years ago)
Author:
epitzer
Message:

Small improvements to font serialization: (#1487)

  • included font size units and vertical font indicator for complete serialization
  • removed unnecessary checks for null during serialization
  • removed checks during deserializaiton: incorrect files should not cause nulls to be deserialized but throw an error instead
  • added unit test
  • replaced string concatenation with string.Format()
File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/histogram/HeuristicLab.Persistence/3.3/Default/Xml/Primitive/System.Drawing/Font2XmlSerializer.cs

    r6022 r6027  
    2222using System;
    2323using System.Drawing;
    24 using System.Globalization;
    2524
    2625namespace HeuristicLab.Persistence.Default.Xml.Primitive {
     
    2827
    2928    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);
     29      return new XmlString(string.Format("{0};{1};{2};{3};{4};{5}",
     30        font.Name,
     31        Float2XmlSerializer.FormatG8(font.Size),
     32        font.Style,
     33        font.Unit,
     34        font.GdiCharSet,
     35        font.GdiVerticalFont));
    3736    }
    3837
    3938    public override Font Parse(XmlString fontData) {
    40       if (string.IsNullOrEmpty(fontData.Data)) return null;
    4139      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);
     40      return new Font(
     41        tokens[0],
     42        Float2XmlSerializer.ParseG8(tokens[1]),
     43        (FontStyle)Enum.Parse(typeof(FontStyle), tokens[2]),
     44        (GraphicsUnit)Enum.Parse(typeof(GraphicsUnit), tokens[3]),
     45        byte.Parse(tokens[4]),
     46        bool.Parse(tokens[5]));
    5447    }
    5548  }
Note: See TracChangeset for help on using the changeset viewer.