2019-08-30 04:04:11 +00:00
|
|
|
// 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.
|
|
|
|
|
2023-10-27 18:10:36 +00:00
|
|
|
using System;
|
2024-01-04 17:26:26 +00:00
|
|
|
using System.Collections.Generic;
|
2019-08-30 04:04:11 +00:00
|
|
|
using System.Threading.Tasks;
|
2021-05-10 13:36:20 +00:00
|
|
|
using osu.Framework.Allocation;
|
2019-08-30 04:04:11 +00:00
|
|
|
using osu.Framework.Graphics.Sprites;
|
|
|
|
using osu.Framework.Text;
|
|
|
|
using osu.Game.Graphics.Sprites;
|
2021-03-06 23:28:08 +00:00
|
|
|
using osuTK;
|
2019-08-30 04:04:11 +00:00
|
|
|
|
|
|
|
namespace osu.Game.Skinning
|
|
|
|
{
|
2021-05-10 13:36:20 +00:00
|
|
|
public sealed partial class LegacySpriteText : OsuSpriteText
|
2019-08-30 04:04:11 +00:00
|
|
|
{
|
2023-10-27 17:48:10 +00:00
|
|
|
public Vector2? MaxSizePerGlyph { get; init; }
|
2023-10-27 18:10:36 +00:00
|
|
|
public bool FixedWidth { get; init; }
|
2023-10-27 17:48:10 +00:00
|
|
|
|
2021-05-10 13:36:20 +00:00
|
|
|
private readonly LegacyFont font;
|
|
|
|
|
2023-01-26 08:52:49 +00:00
|
|
|
private LegacyGlyphStore glyphStore = null!;
|
2019-08-30 04:04:11 +00:00
|
|
|
|
2020-10-19 05:59:03 +00:00
|
|
|
protected override char FixedWidthReferenceCharacter => '5';
|
|
|
|
|
|
|
|
protected override char[] FixedWidthExcludeCharacters => new[] { ',', '.', '%', 'x' };
|
|
|
|
|
2023-10-27 18:10:36 +00:00
|
|
|
// ReSharper disable once UnusedMember.Global
|
|
|
|
// being unused is the point here
|
|
|
|
public new FontUsage Font
|
|
|
|
{
|
|
|
|
get => base.Font;
|
|
|
|
set => throw new InvalidOperationException(@"Attempting to use this setter will not work correctly. "
|
|
|
|
+ $@"Use specific init-only properties exposed by {nameof(LegacySpriteText)} instead.");
|
|
|
|
}
|
|
|
|
|
2023-10-27 17:48:10 +00:00
|
|
|
public LegacySpriteText(LegacyFont font)
|
2019-08-30 04:04:11 +00:00
|
|
|
{
|
2021-05-10 13:36:20 +00:00
|
|
|
this.font = font;
|
2023-09-19 01:37:51 +00:00
|
|
|
|
2019-08-30 04:04:11 +00:00
|
|
|
Shadow = false;
|
|
|
|
UseFullGlyphHeight = false;
|
2021-05-10 13:36:20 +00:00
|
|
|
}
|
2019-08-30 04:04:11 +00:00
|
|
|
|
2021-05-10 13:36:20 +00:00
|
|
|
[BackgroundDependencyLoader]
|
|
|
|
private void load(ISkinSource skin)
|
|
|
|
{
|
2024-01-04 17:26:26 +00:00
|
|
|
string fontPrefix = skin.GetFontPrefix(font);
|
|
|
|
base.Font = new FontUsage(fontPrefix, 1, fixedWidth: FixedWidth);
|
2021-03-06 23:28:08 +00:00
|
|
|
Spacing = new Vector2(-skin.GetFontOverlap(font), 0);
|
|
|
|
|
2024-01-04 17:26:26 +00:00
|
|
|
glyphStore = new LegacyGlyphStore(fontPrefix, skin, MaxSizePerGlyph);
|
2024-01-15 05:01:03 +00:00
|
|
|
|
|
|
|
// cache common lookups ahead of time.
|
|
|
|
foreach (char c in FixedWidthExcludeCharacters)
|
|
|
|
glyphStore.Get(fontPrefix, c);
|
|
|
|
for (int i = 0; i < 10; i++)
|
|
|
|
glyphStore.Get(fontPrefix, (char)('0' + i));
|
2019-08-30 04:04:11 +00:00
|
|
|
}
|
|
|
|
|
2020-10-19 05:59:03 +00:00
|
|
|
protected override TextBuilder CreateTextBuilder(ITexturedGlyphLookupStore store) => base.CreateTextBuilder(glyphStore);
|
2019-08-30 04:04:11 +00:00
|
|
|
|
|
|
|
private class LegacyGlyphStore : ITexturedGlyphLookupStore
|
|
|
|
{
|
|
|
|
private readonly ISkin skin;
|
2023-09-19 01:37:51 +00:00
|
|
|
private readonly Vector2? maxSize;
|
2019-08-30 04:04:11 +00:00
|
|
|
|
2024-01-04 17:26:26 +00:00
|
|
|
private readonly string fontName;
|
|
|
|
|
|
|
|
private readonly Dictionary<char, ITexturedCharacterGlyph?> cache = new Dictionary<char, ITexturedCharacterGlyph?>();
|
|
|
|
|
|
|
|
public LegacyGlyphStore(string fontName, ISkin skin, Vector2? maxSize)
|
2019-08-30 04:04:11 +00:00
|
|
|
{
|
2024-01-04 17:26:26 +00:00
|
|
|
this.fontName = fontName;
|
2019-08-30 04:04:11 +00:00
|
|
|
this.skin = skin;
|
2023-09-19 01:37:51 +00:00
|
|
|
this.maxSize = maxSize;
|
2019-08-30 04:04:11 +00:00
|
|
|
}
|
|
|
|
|
2023-11-24 03:45:55 +00:00
|
|
|
public ITexturedCharacterGlyph? Get(string? fontName, char character)
|
2019-08-30 04:04:11 +00:00
|
|
|
{
|
2024-01-04 17:26:26 +00:00
|
|
|
// We only service one font.
|
2024-01-07 10:49:42 +00:00
|
|
|
if (fontName != this.fontName)
|
|
|
|
return null;
|
2024-01-04 17:26:26 +00:00
|
|
|
|
|
|
|
if (cache.TryGetValue(character, out var cached))
|
|
|
|
return cached;
|
|
|
|
|
2021-10-27 04:04:41 +00:00
|
|
|
string lookup = getLookupName(character);
|
2020-10-15 08:56:37 +00:00
|
|
|
|
|
|
|
var texture = skin.GetTexture($"{fontName}-{lookup}");
|
2019-08-30 04:04:11 +00:00
|
|
|
|
2024-01-04 17:26:26 +00:00
|
|
|
TexturedCharacterGlyph? glyph = null;
|
2019-08-30 04:04:11 +00:00
|
|
|
|
2024-01-04 17:26:26 +00:00
|
|
|
if (texture != null)
|
|
|
|
{
|
|
|
|
if (maxSize != null)
|
|
|
|
texture = texture.WithMaximumSize(maxSize.Value);
|
|
|
|
|
2024-08-15 04:07:42 +00:00
|
|
|
glyph = new TexturedCharacterGlyph(new CharacterGlyph(character, 0, 0, texture.Width, 0, null), texture, 1f / texture.ScaleAdjust);
|
2024-01-04 17:26:26 +00:00
|
|
|
}
|
2023-09-19 01:37:51 +00:00
|
|
|
|
2024-01-04 17:26:26 +00:00
|
|
|
cache[character] = glyph;
|
|
|
|
return glyph;
|
2019-08-30 04:04:11 +00:00
|
|
|
}
|
|
|
|
|
2020-10-15 08:56:37 +00:00
|
|
|
private static string getLookupName(char character)
|
|
|
|
{
|
|
|
|
switch (character)
|
|
|
|
{
|
|
|
|
case ',':
|
|
|
|
return "comma";
|
|
|
|
|
|
|
|
case '.':
|
|
|
|
return "dot";
|
|
|
|
|
|
|
|
case '%':
|
|
|
|
return "percent";
|
|
|
|
|
|
|
|
default:
|
|
|
|
return character.ToString();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-26 08:52:49 +00:00
|
|
|
public Task<ITexturedCharacterGlyph?> GetAsync(string fontName, char character) => Task.Run(() => Get(fontName, character));
|
2019-08-30 04:04:11 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|