Index: Core/Common/src/Core.Common.Util/Drawing/FontHelper.cs =================================================================== diff -u -rc0a586e98573119668b51c29185aab183b580e40 -rb63fade0e22ce3993cd31e2ced93261ed43e6c42 --- Core/Common/src/Core.Common.Util/Drawing/FontHelper.cs (.../FontHelper.cs) (revision c0a586e98573119668b51c29185aab183b580e40) +++ Core/Common/src/Core.Common.Util/Drawing/FontHelper.cs (.../FontHelper.cs) (revision b63fade0e22ce3993cd31e2ced93261ed43e6c42) @@ -36,8 +36,15 @@ /// /// The data to create the from. /// The created . + /// Thrown when is null. + /// Thrown when is not a valid font. public static Font CreateFont(byte[] fontData) { + if (fontData == null) + { + throw new ArgumentNullException(nameof(fontData)); + } + uint dummy = 0; var fonts = new PrivateFontCollection(); @@ -47,6 +54,11 @@ AddFontMemResourceEx(fontPtr, (uint) fontData.Length, IntPtr.Zero, ref dummy); Marshal.FreeCoTaskMem(fontPtr); + if (fonts.Families.Length == 0) + { + throw new ArgumentException("Font data could not be loaded."); + } + return new Font(fonts.Families[0], 14.0F); } Index: Core/Common/test/Core.Common.Util.Test/Drawing/FontHelperTest.cs =================================================================== diff -u --- Core/Common/test/Core.Common.Util.Test/Drawing/FontHelperTest.cs (revision 0) +++ Core/Common/test/Core.Common.Util.Test/Drawing/FontHelperTest.cs (revision b63fade0e22ce3993cd31e2ced93261ed43e6c42) @@ -0,0 +1,72 @@ +// Copyright (C) Stichting Deltares 2021. All rights reserved. +// +// This file is part of Riskeer. +// +// Riskeer is free software: you can redistribute it and/or modify +// it under the terms of the GNU Lesser General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU Lesser General Public License for more details. +// +// You should have received a copy of the GNU Lesser General Public License +// along with this program. If not, see . +// +// All names, logos, and references to "Deltares" are registered trademarks of +// Stichting Deltares and remain full property of Stichting Deltares at all times. +// All rights reserved. + +using System; +using System.Drawing; +using Core.Common.Util.Drawing; +using Core.Common.Util.Test.Properties; +using NUnit.Framework; + +namespace Core.Common.Util.Test.Drawing +{ + [TestFixture] + public class FontHelperTest + { + [Test] + public void CreateFont_ValidFontData_CreatesExpectedFont() + { + // Call + Font font = FontHelper.CreateFont(Resources.ValidFont); + + // Assert + Assert.IsNotNull(font); + Assert.AreEqual(14, font.Size); + } + + [Test] + public void CreateFont_FontDataNull_ThrowsArgumentNullException() + { + // Call + void Call() => FontHelper.CreateFont(null); + + // Assert + var exception = Assert.Throws(Call); + Assert.AreEqual("fontData", exception.ParamName); + } + + [Test] + public void CreateFont_InvalidFontData_ThrowsArgumentException() + { + // Setup + var random = new Random(); + var fontData = new byte[100000]; + + random.NextBytes(fontData); + + // Call + void Call() => FontHelper.CreateFont(fontData); + + // Assert + var exception = Assert.Throws(Call); + Assert.AreEqual("Font data could not be loaded.", exception.Message); + } + } +} \ No newline at end of file Index: Core/Common/test/Core.Common.Util.Test/Properties/Resources.Designer.cs =================================================================== diff -u -r3d088548dfdebfd5c3adbbd67075b42e134f5c03 -rb63fade0e22ce3993cd31e2ced93261ed43e6c42 --- Core/Common/test/Core.Common.Util.Test/Properties/Resources.Designer.cs (.../Resources.Designer.cs) (revision 3d088548dfdebfd5c3adbbd67075b42e134f5c03) +++ Core/Common/test/Core.Common.Util.Test/Properties/Resources.Designer.cs (.../Resources.Designer.cs) (revision b63fade0e22ce3993cd31e2ced93261ed43e6c42) @@ -40,7 +40,7 @@ // class via a tool like ResGen or Visual Studio. // To add or remove a member, edit your .ResX file then rerun ResGen // with the /str option, or rebuild your VS project. - [global::System.CodeDom.Compiler.GeneratedCodeAttribute("System.Resources.Tools.StronglyTypedResourceBuilder", "4.0.0.0")] + [global::System.CodeDom.Compiler.GeneratedCodeAttribute("System.Resources.Tools.StronglyTypedResourceBuilder", "16.0.0.0")] [global::System.Diagnostics.DebuggerNonUserCodeAttribute()] [global::System.Runtime.CompilerServices.CompilerGeneratedAttribute()] internal class Resources { @@ -213,5 +213,15 @@ return ((System.Drawing.Bitmap)(obj)); } } + + /// + /// Looks up a localized resource of type System.Byte[]. + /// + internal static byte[] ValidFont { + get { + object obj = ResourceManager.GetObject("ValidFont", resourceCulture); + return ((byte[])(obj)); + } + } } } Index: Core/Common/test/Core.Common.Util.Test/Properties/Resources.resx =================================================================== diff -u -rac96d7c315129af851634ed5a4a6800b59ede718 -rb63fade0e22ce3993cd31e2ced93261ed43e6c42 --- Core/Common/test/Core.Common.Util.Test/Properties/Resources.resx (.../Resources.resx) (revision ac96d7c315129af851634ed5a4a6800b59ede718) +++ Core/Common/test/Core.Common.Util.Test/Properties/Resources.resx (.../Resources.resx) (revision b63fade0e22ce3993cd31e2ced93261ed43e6c42) @@ -160,4 +160,7 @@ ..\Resources\acornWithCross.png;System.Drawing.Bitmap, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a + + ..\Resources\ValidFont.ttf;System.Byte[], mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + \ No newline at end of file Index: Core/Common/test/Core.Common.Util.Test/Resources/ValidFont.ttf =================================================================== diff -u Binary files differ