Skip to content
This repository has been archived by the owner on Jan 23, 2023. It is now read-only.
/ corefx Public archive

Commit

Permalink
Make Font serializable (#33445)
Browse files Browse the repository at this point in the history
  • Loading branch information
ViktorHofer authored Nov 13, 2018
1 parent 314f594 commit 6e55e0f
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 35 deletions.
1 change: 0 additions & 1 deletion src/System.Drawing.Common/src/System.Drawing.Common.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,6 @@
<Compile Include="System\Drawing\Internal\ISystemEventTracker.cs" />
<Compile Include="System\Drawing\Brush.cs" />
<Compile Include="System\Drawing\Font.cs" />
<Compile Include="System\Drawing\Font.NotSerializable.cs" />
<Compile Include="System\Drawing\FontFamily.cs" />
<Compile Include="System\Drawing\SolidBrush.cs" />
<Compile Include="System\Drawing\SystemBrushes.cs" />
Expand Down

This file was deleted.

39 changes: 23 additions & 16 deletions src/System.Drawing.Common/src/System/Drawing/Font.Unix.cs
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ private void CreateFont(string familyName, float emSize, FontStyle style, Graphi
family = FontFamily.GenericSansSerif;
}

setProperties(family, emSize, style, unit, charSet, isVertical);
Initialize(family, emSize, style, unit, charSet, isVertical);
int status = Gdip.GdipCreateFont(new HandleRef(this, family.NativeFamily), emSize, style, unit, out _nativeFont);

if (status == Gdip.FontStyleNotFound)
Expand Down Expand Up @@ -130,7 +130,25 @@ internal void unitConversion(GraphicsUnit fromUnit, GraphicsUnit toUnit, float n
}
}

void setProperties(FontFamily family, float emSize, FontStyle style, GraphicsUnit unit, byte charSet, bool isVertical)
private void Initialize(string familyName, float emSize, FontStyle style, GraphicsUnit unit, byte charSet, bool isVertical)
{
_originalFontName = familyName;
FontFamily family;
// NOTE: If family name is null, empty or invalid,
// MS creates Microsoft Sans Serif font.
try
{
family = new FontFamily(familyName);
}
catch (Exception)
{
family = FontFamily.GenericSansSerif;
}

Initialize(family, emSize, style, unit, charSet, isVertical);
}

private void Initialize(FontFamily family, float emSize, FontStyle style, GraphicsUnit unit, byte charSet, bool isVertical)
{
_fontFamily = family;
_fontSize = emSize;
Expand Down Expand Up @@ -205,25 +223,14 @@ public IntPtr ToHfont()

internal Font(IntPtr nativeFont, string familyName, FontStyle style, float size)
{
FontFamily fontFamily;

try
{
fontFamily = new FontFamily(familyName);
}
catch (Exception)
{
fontFamily = FontFamily.GenericSansSerif;
}

setProperties(fontFamily, size, style, GraphicsUnit.Pixel, 0, false);
Initialize(familyName, size, style, GraphicsUnit.Pixel, 0, false);
_nativeFont = nativeFont;
}

public Font(Font prototype, FontStyle newStyle)
{
// no null checks, MS throws a NullReferenceException if original is null
setProperties(prototype.FontFamily, prototype.Size, newStyle, prototype.Unit, prototype.GdiCharSet, prototype.GdiVerticalFont);
Initialize(prototype.FontFamily, prototype.Size, newStyle, prototype.Unit, prototype.GdiCharSet, prototype.GdiVerticalFont);

int status = Gdip.GdipCreateFont(new HandleRef(_fontFamily, _fontFamily.NativeFamily), Size, Style, Unit, out _nativeFont);
Gdip.CheckStatus(status);
Expand Down Expand Up @@ -266,7 +273,7 @@ public Font(FontFamily family, float emSize, FontStyle style,
throw new ArgumentNullException(nameof(family));

int status;
setProperties(family, emSize, style, unit, gdiCharSet, gdiVerticalFont);
Initialize(family, emSize, style, unit, gdiCharSet, gdiVerticalFont);
status = Gdip.GdipCreateFont(new HandleRef(this, family.NativeFamily), emSize, style, unit, out _nativeFont);
Gdip.CheckStatus(status);
}
Expand Down
2 changes: 0 additions & 2 deletions src/System.Drawing.Common/src/System/Drawing/Font.Windows.cs
Original file line number Diff line number Diff line change
Expand Up @@ -340,8 +340,6 @@ private void SetFontFamily(FontFamily family)
GC.SuppressFinalize(_fontFamily);
}

private static bool IsVerticalName(string familyName) => familyName?.Length > 0 && familyName[0] == '@';

private static string StripVerticalName(string familyName)
{
if (familyName?.Length > 1 && familyName[0] == '@')
Expand Down
22 changes: 22 additions & 0 deletions src/System.Drawing.Common/src/System/Drawing/Font.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ namespace System.Drawing
#if netcoreapp
[TypeConverter("System.Drawing.FontConverter, System.Windows.Extensions, Version=4.0.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51")]
#endif
[Serializable]
public sealed partial class Font : MarshalByRefObject, ICloneable, IDisposable, ISerializable
{
private IntPtr _nativeFont;
Expand Down Expand Up @@ -143,6 +144,27 @@ public sealed partial class Font : MarshalByRefObject, ICloneable, IDisposable,
/// </summary>
~Font() => Dispose(false);

private Font(SerializationInfo info, StreamingContext context)
{
string name = info.GetString("Name"); // Do not rename (binary serialization)
FontStyle style = (FontStyle)info.GetValue("Style", typeof(FontStyle)); // Do not rename (binary serialization)
GraphicsUnit unit = (GraphicsUnit)info.GetValue("Unit", typeof(GraphicsUnit)); // Do not rename (binary serialization)
float size = info.GetSingle("Size"); // Do not rename (binary serialization)

Initialize(name, size, style, unit, SafeNativeMethods.DEFAULT_CHARSET, IsVerticalName(name));
}

void ISerializable.GetObjectData(SerializationInfo si, StreamingContext context)
{
string name = string.IsNullOrEmpty(OriginalFontName) ? Name : OriginalFontName;
si.AddValue("Name", name); // Do not rename (binary serialization)
si.AddValue("Size", Size); // Do not rename (binary serialization)
si.AddValue("Style", Style); // Do not rename (binary serialization)
si.AddValue("Unit", Unit); // Do not rename (binary serialization)
}

private static bool IsVerticalName(string familyName) => familyName?.Length > 0 && familyName[0] == '@';

/// <summary>
/// Cleans up Windows resources for this <see cref='Font'/>.
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1216,6 +1216,11 @@ private static IEnumerable<object[]> SerializableObjects()
yield return new object[] { ContentAlignment.BottomCenter, new TypeSerializableValue[] { new TypeSerializableValue("AAEAAAD/////AQAAAAAAAAAMAgAAAFFTeXN0ZW0uRHJhd2luZywgVmVyc2lvbj00LjAuMC4wLCBDdWx0dXJlPW5ldXRyYWwsIFB1YmxpY0tleVRva2VuPWIwM2Y1ZjdmMTFkNTBhM2EFAQAAAB9TeXN0ZW0uRHJhd2luZy5Db250ZW50QWxpZ25tZW50AQAAAAd2YWx1ZV9fAAgCAAAAAAIAAAs=", TargetFrameworkMoniker.netcoreapp21), new TypeSerializableValue("AAEAAAD/////AQAAAAAAAAAMAgAAAFFTeXN0ZW0uRHJhd2luZywgVmVyc2lvbj00LjAuMC4wLCBDdWx0dXJlPW5ldXRyYWwsIFB1YmxpY0tleVRva2VuPWIwM2Y1ZjdmMTFkNTBhM2EFAQAAAB9TeXN0ZW0uRHJhd2luZy5Db250ZW50QWxpZ25tZW50AQAAAAd2YWx1ZV9fAAgCAAAAAAIAAAs=", TargetFrameworkMoniker.netfx461) } };
yield return new object[] { LinearGradientMode.BackwardDiagonal, new TypeSerializableValue[] { new TypeSerializableValue("AAEAAAD/////AQAAAAAAAAAMAgAAAFFTeXN0ZW0uRHJhd2luZywgVmVyc2lvbj00LjAuMC4wLCBDdWx0dXJlPW5ldXRyYWwsIFB1YmxpY0tleVRva2VuPWIwM2Y1ZjdmMTFkNTBhM2EFAQAAACtTeXN0ZW0uRHJhd2luZy5EcmF3aW5nMkQuTGluZWFyR3JhZGllbnRNb2RlAQAAAAd2YWx1ZV9fAAgCAAAAAwAAAAs=", TargetFrameworkMoniker.netcoreapp21), new TypeSerializableValue("AAEAAAD/////AQAAAAAAAAAMAgAAAFFTeXN0ZW0uRHJhd2luZywgVmVyc2lvbj00LjAuMC4wLCBDdWx0dXJlPW5ldXRyYWwsIFB1YmxpY0tleVRva2VuPWIwM2Y1ZjdmMTFkNTBhM2EFAQAAACtTeXN0ZW0uRHJhd2luZy5EcmF3aW5nMkQuTGluZWFyR3JhZGllbnRNb2RlAQAAAAd2YWx1ZV9fAAgCAAAAAwAAAAs=", TargetFrameworkMoniker.netfx461) } };
yield return new object[] { FontStyle.Bold, new TypeSerializableValue[] { new TypeSerializableValue("AAEAAAD/////AQAAAAAAAAAMAgAAAFFTeXN0ZW0uRHJhd2luZywgVmVyc2lvbj00LjAuMC4wLCBDdWx0dXJlPW5ldXRyYWwsIFB1YmxpY0tleVRva2VuPWIwM2Y1ZjdmMTFkNTBhM2EFAQAAABhTeXN0ZW0uRHJhd2luZy5Gb250U3R5bGUBAAAAB3ZhbHVlX18ACAIAAAABAAAACw==", TargetFrameworkMoniker.netcoreapp21), new TypeSerializableValue("AAEAAAD/////AQAAAAAAAAAMAgAAAFFTeXN0ZW0uRHJhd2luZywgVmVyc2lvbj00LjAuMC4wLCBDdWx0dXJlPW5ldXRyYWwsIFB1YmxpY0tleVRva2VuPWIwM2Y1ZjdmMTFkNTBhM2EFAQAAABhTeXN0ZW0uRHJhd2luZy5Gb250U3R5bGUBAAAAB3ZhbHVlX18ACAIAAAABAAAACw==", TargetFrameworkMoniker.netfx461) } };
// libgdiplus is not supported on some Windows variants.
if (PlatformDetection.IsDrawingSupported)
{
yield return new object[] { new Font("coreFxAwesomeFont", 8.5f, FontStyle.Strikeout, GraphicsUnit.Pixel), new TypeSerializableValue[] { new TypeSerializableValue("AAEAAAD/////AQAAAAAAAAAMAgAAAFhTeXN0ZW0uRHJhd2luZy5Db21tb24sIFZlcnNpb249NC4wLjEuMCwgQ3VsdHVyZT1uZXV0cmFsLCBQdWJsaWNLZXlUb2tlbj1jYzdiMTNmZmNkMmRkZDUxDAMAAABRU3lzdGVtLkRyYXdpbmcsIFZlcnNpb249NC4wLjAuMCwgQ3VsdHVyZT1uZXV0cmFsLCBQdWJsaWNLZXlUb2tlbj1iMDNmNWY3ZjExZDUwYTNhBQEAAAATU3lzdGVtLkRyYXdpbmcuRm9udAQAAAAETmFtZQRTaXplBVN0eWxlBFVuaXQBAAQECxhTeXN0ZW0uRHJhd2luZy5Gb250U3R5bGUDAAAAG1N5c3RlbS5EcmF3aW5nLkdyYXBoaWNzVW5pdAMAAAACAAAABgQAAAARY29yZUZ4QXdlc29tZUZvbnQAAAhBBfv///8YU3lzdGVtLkRyYXdpbmcuRm9udFN0eWxlAQAAAAd2YWx1ZV9fAAgDAAAACAAAAAX6////G1N5c3RlbS5EcmF3aW5nLkdyYXBoaWNzVW5pdAEAAAAHdmFsdWVfXwAIAwAAAAIAAAAL", TargetFrameworkMoniker.netcoreapp30), new TypeSerializableValue("AAEAAAD/////AQAAAAAAAAAMAgAAAFFTeXN0ZW0uRHJhd2luZywgVmVyc2lvbj00LjAuMC4wLCBDdWx0dXJlPW5ldXRyYWwsIFB1YmxpY0tleVRva2VuPWIwM2Y1ZjdmMTFkNTBhM2EFAQAAABNTeXN0ZW0uRHJhd2luZy5Gb250BAAAAAROYW1lBFNpemUFU3R5bGUEVW5pdAEABAQLGFN5c3RlbS5EcmF3aW5nLkZvbnRTdHlsZQIAAAAbU3lzdGVtLkRyYXdpbmcuR3JhcGhpY3NVbml0AgAAAAIAAAAGAwAAABFjb3JlRnhBd2Vzb21lRm9udAAACEEF/P///xhTeXN0ZW0uRHJhd2luZy5Gb250U3R5bGUBAAAAB3ZhbHVlX18ACAIAAAAIAAAABfv///8bU3lzdGVtLkRyYXdpbmcuR3JhcGhpY3NVbml0AQAAAAd2YWx1ZV9fAAgCAAAAAgAAAAs=", TargetFrameworkMoniker.netfx461) } };
}
}

// Custom object
Expand Down

0 comments on commit 6e55e0f

Please sign in to comment.