Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update OsuFont to support CSS-style font sizing and supply osu! font with metrics #17946

Draft
wants to merge 3 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 13 additions & 4 deletions osu.Game/Graphics/OsuFont.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// See the LICENCE file in the repository root for full licence text.

using osu.Framework.Graphics.Sprites;
using osu.Framework.IO.Stores;

namespace osu.Game.Graphics
{
Expand Down Expand Up @@ -33,9 +34,13 @@ public static class OsuFont
/// <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>
/// <param name="css">
/// Whether the text glyphs should scale according to their respective <see cref="IGlyphStore.Metrics"/>, matching CSS.
/// It is recommended to enable this for better alignment with other fonts.
/// </param>
/// <returns>The <see cref="FontUsage"/>.</returns>
public static FontUsage GetFont(Typeface typeface = Typeface.Torus, float size = DEFAULT_FONT_SIZE, FontWeight weight = FontWeight.Medium, bool italics = false, bool fixedWidth = false)
=> new FontUsage(GetFamilyString(typeface), size, GetWeightString(typeface, weight), getItalics(italics), fixedWidth);
public static FontUsage GetFont(Typeface typeface = Typeface.Torus, float size = DEFAULT_FONT_SIZE, FontWeight weight = FontWeight.Medium, bool italics = false, bool fixedWidth = false, bool css = false)
=> new FontUsage(GetFamilyString(typeface), size, GetWeightString(typeface, weight), getItalics(italics), fixedWidth, css);

private static bool getItalics(in bool italicsRequested)
{
Expand Down Expand Up @@ -104,13 +109,17 @@ public static class OsuFontExtensions
/// <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>
/// <param name="css">
/// Whether the text glyphs should scale according to their respective <see cref="IGlyphStore.Metrics"/>, matching CSS.
/// It is recommended to enable this for better alignment with other fonts.
/// </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)
public static FontUsage With(this FontUsage usage, Typeface? typeface = null, float? size = null, FontWeight? weight = null, bool? italics = null, bool? fixedWidth = null, bool? css = 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);
return usage.With(familyString, size, weightString, italics, fixedWidth, css);
}
}

Expand Down
63 changes: 35 additions & 28 deletions osu.Game/OsuGameBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
using osu.Framework.IO.Stores;
using osu.Framework.Logging;
using osu.Framework.Platform;
using osu.Framework.Text;
using osu.Game.Audio;
using osu.Game.Beatmaps;
using osu.Game.Beatmaps.Formats;
Expand Down Expand Up @@ -352,34 +353,40 @@ protected virtual void InitialiseFonts()
{
AddFont(Resources, @"Fonts/osuFont");

AddFont(Resources, @"Fonts/Torus/Torus-Regular");
AddFont(Resources, @"Fonts/Torus/Torus-Light");
AddFont(Resources, @"Fonts/Torus/Torus-SemiBold");
AddFont(Resources, @"Fonts/Torus/Torus-Bold");

AddFont(Resources, @"Fonts/Torus-Alternate/Torus-Alternate-Regular");
AddFont(Resources, @"Fonts/Torus-Alternate/Torus-Alternate-Light");
AddFont(Resources, @"Fonts/Torus-Alternate/Torus-Alternate-SemiBold");
AddFont(Resources, @"Fonts/Torus-Alternate/Torus-Alternate-Bold");

AddFont(Resources, @"Fonts/Inter/Inter-Regular");
AddFont(Resources, @"Fonts/Inter/Inter-RegularItalic");
AddFont(Resources, @"Fonts/Inter/Inter-Light");
AddFont(Resources, @"Fonts/Inter/Inter-LightItalic");
AddFont(Resources, @"Fonts/Inter/Inter-SemiBold");
AddFont(Resources, @"Fonts/Inter/Inter-SemiBoldItalic");
AddFont(Resources, @"Fonts/Inter/Inter-Bold");
AddFont(Resources, @"Fonts/Inter/Inter-BoldItalic");

AddFont(Resources, @"Fonts/Noto/Noto-Basic");
AddFont(Resources, @"Fonts/Noto/Noto-Hangul");
AddFont(Resources, @"Fonts/Noto/Noto-CJK-Basic");
AddFont(Resources, @"Fonts/Noto/Noto-CJK-Compatibility");
AddFont(Resources, @"Fonts/Noto/Noto-Thai");

AddFont(Resources, @"Fonts/Venera/Venera-Light");
AddFont(Resources, @"Fonts/Venera/Venera-Bold");
AddFont(Resources, @"Fonts/Venera/Venera-Black");
var torusMetrics = new FontMetrics(ascent: 800, descent: 200, emSize: 1000, lineGap: 200);
var interMetrics = new FontMetrics(ascent: 2728, descent: 680, emSize: 2816, lineGap: 0);
var notoCjkMetrics = new FontMetrics(ascent: 880, descent: 120, emSize: 1000, lineGap: 0);
var notoThaiMetrics = new FontMetrics(ascent: 1061, descent: 450, emSize: 1000, lineGap: 0);
var veneraMetrics = new FontMetrics(ascent: 750, descent: 250, emSize: 1000, lineGap: 200);

AddFont(Resources, @"Fonts/Torus/Torus-Regular", metrics: torusMetrics);
AddFont(Resources, @"Fonts/Torus/Torus-Light", metrics: torusMetrics);
AddFont(Resources, @"Fonts/Torus/Torus-SemiBold", metrics: torusMetrics);
AddFont(Resources, @"Fonts/Torus/Torus-Bold", metrics: torusMetrics);

AddFont(Resources, @"Fonts/Torus-Alternate/Torus-Alternate-Regular", metrics: torusMetrics);
AddFont(Resources, @"Fonts/Torus-Alternate/Torus-Alternate-Light", metrics: torusMetrics);
AddFont(Resources, @"Fonts/Torus-Alternate/Torus-Alternate-SemiBold", metrics: torusMetrics);
AddFont(Resources, @"Fonts/Torus-Alternate/Torus-Alternate-Bold", metrics: torusMetrics);

AddFont(Resources, @"Fonts/Inter/Inter-Regular", metrics: interMetrics);
AddFont(Resources, @"Fonts/Inter/Inter-RegularItalic", metrics: interMetrics);
AddFont(Resources, @"Fonts/Inter/Inter-Light", metrics: interMetrics);
AddFont(Resources, @"Fonts/Inter/Inter-LightItalic", metrics: interMetrics);
AddFont(Resources, @"Fonts/Inter/Inter-SemiBold", metrics: interMetrics);
AddFont(Resources, @"Fonts/Inter/Inter-SemiBoldItalic", metrics: interMetrics);
AddFont(Resources, @"Fonts/Inter/Inter-Bold", metrics: interMetrics);
AddFont(Resources, @"Fonts/Inter/Inter-BoldItalic", metrics: interMetrics);

AddFont(Resources, @"Fonts/Noto/Noto-Basic", metrics: notoCjkMetrics);
AddFont(Resources, @"Fonts/Noto/Noto-Hangul", metrics: notoCjkMetrics);
AddFont(Resources, @"Fonts/Noto/Noto-CJK-Basic", metrics: notoCjkMetrics);
AddFont(Resources, @"Fonts/Noto/Noto-CJK-Compatibility", metrics: notoCjkMetrics);
AddFont(Resources, @"Fonts/Noto/Noto-Thai", metrics: notoThaiMetrics);

AddFont(Resources, @"Fonts/Venera/Venera-Light", metrics: veneraMetrics);
AddFont(Resources, @"Fonts/Venera/Venera-Bold", metrics: veneraMetrics);
AddFont(Resources, @"Fonts/Venera/Venera-Black", metrics: veneraMetrics);
}

protected override void LoadComplete()
Expand Down
3 changes: 3 additions & 0 deletions osu.Game/Skinning/LegacySpriteText.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System.Threading.Tasks;
using osu.Framework.Allocation;
using osu.Framework.Graphics.Sprites;
using osu.Framework.IO.Stores;
using osu.Framework.Text;
using osu.Game.Graphics.Sprites;
using osuTK;
Expand Down Expand Up @@ -78,6 +79,8 @@ private static string getLookupName(char character)
}

public Task<ITexturedCharacterGlyph> GetAsync(string fontName, char character) => Task.Run(() => Get(fontName, character));

public IGlyphStore GetFont(string name) => null;
}
}
}