1
0
mirror of https://github.com/ppy/osu.git synced 2024-12-18 07:22:57 +08:00
osu-lazer/osu.Game/Graphics/OsuFont.cs

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

158 lines
5.9 KiB
C#
Raw Normal View History

// Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the MIT Licence.
// See the LICENCE file in the repository root for full licence text.
2022-06-17 15:37:17 +08:00
#nullable disable
2022-12-04 04:42:16 +08:00
using System.ComponentModel;
using osu.Framework.Graphics.Sprites;
namespace osu.Game.Graphics
{
public static class OsuFont
{
2019-02-20 20:04:18 +08:00
/// <summary>
/// The default font size.
/// </summary>
public const float DEFAULT_FONT_SIZE = 16;
2019-02-20 20:04:18 +08:00
/// <summary>
/// The default font.
/// </summary>
public static FontUsage Default => GetFont();
2020-03-04 10:21:37 +08:00
public static FontUsage Numeric => GetFont(Typeface.Venera, weight: FontWeight.Bold);
2020-03-03 17:04:12 +08:00
public static FontUsage Torus => GetFont(Typeface.Torus, weight: FontWeight.Regular);
2021-10-04 05:36:39 +08:00
public static FontUsage TorusAlternate => GetFont(Typeface.TorusAlternate, weight: FontWeight.Regular);
2021-08-03 23:10:33 +08:00
public static FontUsage Inter => GetFont(Typeface.Inter, weight: FontWeight.Regular);
2019-02-20 20:04:18 +08:00
/// <summary>
/// Retrieves a <see cref="FontUsage"/>.
/// </summary>
/// <param name="typeface">The font typeface.</param>
/// <param name="size">The size of the text in local space. For a value of 16, a single line will have a height of 16px.</param>
/// <param name="weight">The font weight.</param>
/// <param name="italics">Whether the font is italic.</param>
/// <param name="fixedWidth">Whether all characters should be spaced the same distance apart.</param>
/// <returns>The <see cref="FontUsage"/>.</returns>
2020-03-13 12:32:16 +08:00
public static FontUsage GetFont(Typeface typeface = Typeface.Torus, float size = DEFAULT_FONT_SIZE, FontWeight weight = FontWeight.Medium, bool italics = false, bool fixedWidth = false)
{
string familyString = GetFamilyString(typeface);
return new FontUsage(familyString, size, GetWeightString(familyString, weight), getItalics(italics), fixedWidth);
}
2020-03-14 23:38:27 +08:00
private static bool getItalics(in bool italicsRequested)
{
// right now none of our fonts support italics.
// should add exceptions to this rule if they come up.
return false;
}
2019-02-20 20:04:18 +08:00
/// <summary>
/// Retrieves the string representation of a <see cref="Typeface"/>.
/// </summary>
/// <param name="typeface">The <see cref="Typeface"/>.</param>
/// <returns>The string representation.</returns>
public static string GetFamilyString(Typeface typeface)
{
switch (typeface)
{
case Typeface.Venera:
2022-06-13 09:43:14 +08:00
return @"Venera";
2020-03-03 17:04:12 +08:00
case Typeface.Torus:
2022-06-13 09:43:14 +08:00
return @"Torus";
2021-08-03 23:10:33 +08:00
2021-10-04 05:36:39 +08:00
case Typeface.TorusAlternate:
2022-06-13 09:43:14 +08:00
return @"Torus-Alternate";
2021-10-04 05:36:39 +08:00
2021-08-03 23:10:33 +08:00
case Typeface.Inter:
2022-06-13 09:43:14 +08:00
return @"Inter";
}
return null;
}
2019-02-20 20:04:18 +08:00
/// <summary>
/// Retrieves the string representation of a <see cref="FontWeight"/>.
/// </summary>
/// <param name="family">The font family.</param>
/// <param name="weight">The font weight.</param>
/// <returns>The string representation of <paramref name="weight"/> in the specified <paramref name="family"/>.</returns>
public static string GetWeightString(string family, FontWeight weight)
{
if ((family == GetFamilyString(Typeface.Torus) || family == GetFamilyString(Typeface.TorusAlternate)) && weight == FontWeight.Medium)
// torus doesn't have a medium; fallback to regular.
weight = FontWeight.Regular;
return weight.ToString();
}
}
public static class OsuFontExtensions
{
2019-02-20 20:04:18 +08:00
/// <summary>
/// Creates a new <see cref="FontUsage"/> by applying adjustments to this <see cref="FontUsage"/>.
/// </summary>
2019-04-25 16:36:17 +08:00
/// <param name="usage">The base <see cref="FontUsage"/>.</param>
2019-02-20 20:04:18 +08:00
/// <param name="typeface">The font typeface. If null, the value is copied from this <see cref="FontUsage"/>.</param>
/// <param name="size">The text size. If null, the value is copied from this <see cref="FontUsage"/>.</param>
/// <param name="weight">The font weight. If null, the value is copied from this <see cref="FontUsage"/>.</param>
/// <param name="italics">Whether the font is italic. If null, the value is copied from this <see cref="FontUsage"/>.</param>
/// <param name="fixedWidth">Whether all characters should be spaced apart the same distance. If null, the value is copied from this <see cref="FontUsage"/>.</param>
/// <returns>The resulting <see cref="FontUsage"/>.</returns>
public static FontUsage With(this FontUsage usage, Typeface? typeface = null, float? size = null, FontWeight? weight = null, bool? italics = null, bool? fixedWidth = null)
{
string familyString = typeface != null ? OsuFont.GetFamilyString(typeface.Value) : usage.Family;
string weightString = weight != null ? OsuFont.GetWeightString(familyString, weight.Value) : usage.Weight;
return usage.With(familyString, size, weightString, italics, fixedWidth);
}
}
public enum Typeface
{
Venera,
2021-08-03 23:10:33 +08:00
Torus,
2022-12-04 04:42:16 +08:00
[Description("Torus (alternate)")]
2021-10-04 05:36:39 +08:00
TorusAlternate,
2021-08-03 23:10:33 +08:00
Inter,
}
public enum FontWeight
{
2020-03-04 10:21:37 +08:00
/// <summary>
2020-03-04 11:47:01 +08:00
/// Equivalent to weight 300.
2020-03-04 10:21:37 +08:00
/// </summary>
Light = 300,
/// <summary>
/// Equivalent to weight 400.
2020-03-04 10:21:37 +08:00
/// </summary>
Regular = 400,
/// <summary>
/// Equivalent to weight 500.
2020-03-04 10:21:37 +08:00
/// </summary>
Medium = 500,
/// <summary>
/// Equivalent to weight 600.
2020-03-04 10:21:37 +08:00
/// </summary>
SemiBold = 600,
/// <summary>
/// Equivalent to weight 700.
2020-03-04 10:21:37 +08:00
/// </summary>
Bold = 700,
2020-03-17 16:16:10 +08:00
/// <summary>
/// Equivalent to weight 900.
/// </summary>
Black = 900
}
}