// Copyright (c) Six Labors. // Licensed under the Six Labors Split License. using System; using System.Collections.Generic; using System.Linq; namespace SixLabors.Fonts { /// /// Exception for detailing missing font families. /// /// public class FontFamilyNotFoundException : FontException { /// /// Initializes a new instance of the class. /// /// The name of the missing font family. public FontFamilyNotFoundException(string family) : this(family, Array.Empty()) { } /// /// Initializes a new instance of the class. /// /// The name of the missing font family. /// /// The collection of directories that were searched for the font family. /// Pass an empty collection if font families were not searched in directories. /// public FontFamilyNotFoundException(string family, IReadOnlyCollection searchDirectories) : base(GetMessage(family, searchDirectories)) { this.FontFamily = family; this.SearchDirectories = searchDirectories; } /// /// Gets the name of the font family that was not found. /// public string FontFamily { get; } /// /// Gets the collection of directories that were unsuccessfully searched for the font family. /// /// /// If the exception did not originate from the then this property will be empty. /// public IReadOnlyCollection SearchDirectories { get; } private static string GetMessage(string family, IReadOnlyCollection searchDirectories) { if (searchDirectories.Count == 0) { return $"The \"{family}\" font family could not be found"; } if (searchDirectories.Count == 1) { return $"The \"{family}\" font family could not be found in the following directory: {searchDirectories.First()}"; } return $"The \"{family}\" font family could not be found in the following directories:{Environment.NewLine}{string.Join(Environment.NewLine, searchDirectories.Select(e => $" * {e}"))}"; } } }