// Copyright (c) ppy Pty Ltd . Licensed under the MIT Licence. // See the LICENCE file in the repository root for full licence text. using System; using JetBrains.Annotations; namespace osu.Framework.Graphics.Sprites { /// /// Represents a specific usage of a font. /// public readonly struct FontUsage : IEquatable { private const float default_text_size = 20; /// /// Gets the default , using the fallback font family. /// public static FontUsage Default => new FontUsage(null); /// /// The font family name. /// [CanBeNull] public string Family { get; } /// /// The font weight. /// [CanBeNull] public string Weight { get; } /// /// Whether the font is italic. /// public bool Italics { get; } /// /// The size of the text in local space. For a value of 16, a single line will have a height of 16px. /// public float Size { get; } /// /// Whether all characters should be spaced the same distance apart. /// public bool FixedWidth { get; } /// /// The font's full name to be used for lookups. This is an aggregate of all other properties of . /// /// The format is of the form:
/// {Family}
/// {Family}-Italic
/// {Family}-{Weight}Italic ///
///
[NotNull] public string FontName { get; } /// /// Creates an instance of using the specified font , font and a value indicating whether the used font is italic or not. /// /// The font family name. /// The size of the text in local space. For a value of 16, a single line will have a height of 16px. /// The font weight. /// Whether the font is italic. /// Whether all characters should be spaced the same distance apart. public FontUsage([CanBeNull] string family = null, float size = default_text_size, [CanBeNull] string weight = null, bool italics = false, bool fixedWidth = false) { Family = family; Size = size >= 0 ? size : throw new ArgumentOutOfRangeException(nameof(size), "Must be non-negative."); Weight = weight; Italics = italics; FixedWidth = fixedWidth; FontName = Family + "-"; if (!string.IsNullOrEmpty(weight)) FontName += weight; if (italics) FontName += "Italic"; FontName = FontName.TrimEnd('-'); } /// /// Creates a new by applying adjustments to this . /// /// The font family. If null, the value is copied from this . /// The text size. If null, the value is copied from this . /// The font weight. If null, the value is copied from this . /// Whether the font is italic. If null, the value is copied from this . /// Whether all characters should be spaced apart the same distance. If null, the value is copied from this . /// The resulting . public FontUsage With([CanBeNull] string family = null, [CanBeNull] float? size = null, [CanBeNull] string weight = null, [CanBeNull] bool? italics = null, [CanBeNull] bool? fixedWidth = null) => new FontUsage(family ?? Family, size ?? Size, weight ?? Weight, italics ?? Italics, fixedWidth ?? FixedWidth); public override string ToString() => $"Font={FontName}, Size={Size}, Italics={Italics}, FixedWidth={FixedWidth}"; public bool Equals(FontUsage other) => Family == other.Family && Weight == other.Weight && Italics == other.Italics && Size.Equals(other.Size) && FixedWidth == other.FixedWidth; public override bool Equals(object obj) => obj is FontUsage other && Equals(other); public override int GetHashCode() => HashCode.Combine(Family, Weight, Italics, Size, FixedWidth); } }