Free cookie consent management tool by TermsFeed Policy Generator

Changeset 6027


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()
Location:
branches/histogram/HeuristicLab.Persistence/3.3
Files:
2 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  }
  • branches/histogram/HeuristicLab.Persistence/3.3/Tests/UseCases.cs

    r5698 r6027  
    12551255    }
    12561256
     1257    [TestMethod]
     1258    public void FontTest() {
     1259      List<Font> fonts = new List<Font>() {
     1260        new Font(FontFamily.Families.First(), 12),
     1261        new Font("Times New Roman", 21, FontStyle.Bold, GraphicsUnit.Pixel),
     1262        new Font("Courier New", 10, FontStyle.Underline, GraphicsUnit.Document),
     1263        new Font("Helvetica", 21, FontStyle.Strikeout, GraphicsUnit.Inch, 0, true),
     1264      };
     1265      XmlGenerator.Serialize(fonts, tempFile);
     1266      var newFonts = XmlParser.Deserialize<List<Font>>(tempFile);
     1267      Assert.AreEqual(fonts[0], newFonts[0]);
     1268      Assert.AreEqual(fonts[1], newFonts[1]);
     1269      Assert.AreEqual(fonts[2], newFonts[2]);
     1270      Assert.AreEqual(fonts[3], newFonts[3]);
     1271    }
     1272
    12571273
    12581274
Note: See TracChangeset for help on using the changeset viewer.