forked from KinsonDigital/Velaptor
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
🚧Improve font loader (KinsonDigital#753)
* Start work for issue KinsonDigital#752 * chore: set the graphics extensions methods class to internal * refactor!: change the name of the FontLoader.Load() parameter * refactor!: change the name of the FontLoader.Load() parameter * feat: create new load font methods * chore: refactor scenes to use new load method extension methods * cleanup: remove unused using
- Loading branch information
1 parent
3d1baa3
commit d304095
Showing
8 changed files
with
132 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
37 changes: 37 additions & 0 deletions
37
Testing/VelaptorTests/ExtensionMethods/ContentExtensionsTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
// <copyright file="ContentExtensionsTests.cs" company="KinsonDigital"> | ||
// Copyright (c) KinsonDigital. All rights reserved. | ||
// </copyright> | ||
|
||
namespace VelaptorTests.ExtensionMethods; | ||
|
||
using NSubstitute; | ||
using Velaptor.Content; | ||
using Velaptor.Content.Fonts; | ||
using Velaptor.ExtensionMethods; | ||
using Xunit; | ||
|
||
/// <summary> | ||
/// Tests the <see cref="ContentExtensions"/> clas. | ||
/// </summary> | ||
public class ContentExtensionsTests | ||
{ | ||
#region Method Tests | ||
[Theory] | ||
[InlineData("test-font", 12, "test-font.ttf|size:12")] | ||
[InlineData("test-font.ttf", 14, "test-font.ttf|size:14")] | ||
[InlineData("test-font.invalid-extension", 16, "test-font.ttf|size:16")] | ||
public void Load_WhenInvokingILoaderOfTypeIFont_LoadsFont(string fontName, uint size, string expected) | ||
{ | ||
// Arrange | ||
var mockFont = Substitute.For<IFont>(); | ||
var mockFontLoader = Substitute.For<ILoader<IFont>>(); | ||
mockFontLoader.Load(Arg.Any<string>()).Returns(mockFont); | ||
|
||
// Act | ||
mockFontLoader.Load(fontName, size); | ||
|
||
// Assert | ||
mockFontLoader.Received(1).Load(expected); | ||
} | ||
#endregion | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
// <copyright file="ContentExtensions.cs" company="KinsonDigital"> | ||
// Copyright (c) KinsonDigital. All rights reserved. | ||
// </copyright> | ||
|
||
namespace Velaptor.ExtensionMethods; | ||
|
||
using System; | ||
using System.Diagnostics.CodeAnalysis; | ||
using System.IO; | ||
using Content; | ||
using Content.Fonts; | ||
|
||
/// <summary> | ||
/// Provides content related extension methods. | ||
/// </summary> | ||
public static class ContentExtensions | ||
{ | ||
/// <summary> | ||
/// Loads font content from the application's content directory or directly using a full file path. | ||
/// </summary> | ||
/// <param name="loader">The font loader.</param> | ||
/// <param name="fontName">The name or full file path to the font with metadata.</param> | ||
/// <param name="size">The size of the font.</param> | ||
/// <returns>The loaded font.</returns> | ||
/// <exception cref="ArgumentNullException"> | ||
/// Occurs when the <paramref name="fontName"/> argument is null or empty. | ||
/// </exception> | ||
/// <exception cref="FileNotFoundException"> | ||
/// Occurs if the font file does not exist. | ||
/// </exception> | ||
/// <remarks> | ||
/// If a path is used, it must be a fully qualified file path. | ||
/// <para>Directory paths are not valid.</para> | ||
/// </remarks> | ||
[ExcludeFromCodeCoverage(Justification = $"Cannot test due to interaction with '{nameof(IoC)}' container.")] | ||
public static IFont Load(this FontLoader loader, string fontName, uint size) | ||
{ | ||
ArgumentException.ThrowIfNullOrEmpty(fontName); | ||
|
||
fontName = Path.HasExtension(fontName) ? Path.GetFileNameWithoutExtension(fontName) : fontName; | ||
fontName = $"{fontName}.ttf|size:{size}"; | ||
|
||
return loader.Load(fontName); | ||
} | ||
|
||
/// <summary> | ||
/// Loads font content from the application's content directory or directly using a full file path. | ||
/// </summary> | ||
/// <param name="loader">The font loader.</param> | ||
/// <param name="fontName">The name or full file path to the font with metadata.</param> | ||
/// <param name="size">The size of the font.</param> | ||
/// <returns>The loaded font.</returns> | ||
/// <exception cref="ArgumentNullException"> | ||
/// Occurs when the <paramref name="fontName"/> argument is null or empty. | ||
/// </exception> | ||
/// <exception cref="FileNotFoundException"> | ||
/// Occurs if the font file does not exist. | ||
/// </exception> | ||
/// <remarks> | ||
/// If a path is used, it must be a fully qualified file path. | ||
/// <para>Directory paths are not valid.</para> | ||
/// </remarks> | ||
public static IFont Load(this ILoader<IFont> loader, string fontName, uint size) | ||
{ | ||
ArgumentException.ThrowIfNullOrEmpty(fontName); | ||
|
||
fontName = Path.HasExtension(fontName) ? Path.GetFileNameWithoutExtension(fontName) : fontName; | ||
fontName = $"{fontName}.ttf|size:{size}"; | ||
|
||
return loader.Load(fontName); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters