mirror of
https://github.com/ppy/osu.git
synced 2025-02-26 06:02:59 +08:00
Cache legacy skin character glyph lookups to reduce string allocations
This commit is contained in:
parent
91bb3f6c57
commit
5b55ca6692
@ -2,6 +2,8 @@
|
||||
// See the LICENCE file in the repository root for full licence text.
|
||||
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Diagnostics;
|
||||
using System.Threading.Tasks;
|
||||
using osu.Framework.Allocation;
|
||||
using osu.Framework.Graphics.Sprites;
|
||||
@ -44,10 +46,11 @@ namespace osu.Game.Skinning
|
||||
[BackgroundDependencyLoader]
|
||||
private void load(ISkinSource skin)
|
||||
{
|
||||
base.Font = new FontUsage(skin.GetFontPrefix(font), 1, fixedWidth: FixedWidth);
|
||||
string fontPrefix = skin.GetFontPrefix(font);
|
||||
base.Font = new FontUsage(fontPrefix, 1, fixedWidth: FixedWidth);
|
||||
Spacing = new Vector2(-skin.GetFontOverlap(font), 0);
|
||||
|
||||
glyphStore = new LegacyGlyphStore(skin, MaxSizePerGlyph);
|
||||
glyphStore = new LegacyGlyphStore(fontPrefix, skin, MaxSizePerGlyph);
|
||||
}
|
||||
|
||||
protected override TextBuilder CreateTextBuilder(ITexturedGlyphLookupStore store) => base.CreateTextBuilder(glyphStore);
|
||||
@ -57,25 +60,41 @@ namespace osu.Game.Skinning
|
||||
private readonly ISkin skin;
|
||||
private readonly Vector2? maxSize;
|
||||
|
||||
public LegacyGlyphStore(ISkin skin, Vector2? maxSize)
|
||||
private readonly string fontName;
|
||||
|
||||
private readonly Dictionary<char, ITexturedCharacterGlyph?> cache = new Dictionary<char, ITexturedCharacterGlyph?>();
|
||||
|
||||
public LegacyGlyphStore(string fontName, ISkin skin, Vector2? maxSize)
|
||||
{
|
||||
this.fontName = fontName;
|
||||
this.skin = skin;
|
||||
this.maxSize = maxSize;
|
||||
}
|
||||
|
||||
public ITexturedCharacterGlyph? Get(string? fontName, char character)
|
||||
{
|
||||
// We only service one font.
|
||||
Debug.Assert(fontName == this.fontName);
|
||||
|
||||
if (cache.TryGetValue(character, out var cached))
|
||||
return cached;
|
||||
|
||||
string lookup = getLookupName(character);
|
||||
|
||||
var texture = skin.GetTexture($"{fontName}-{lookup}");
|
||||
|
||||
if (texture == null)
|
||||
return null;
|
||||
TexturedCharacterGlyph? glyph = null;
|
||||
|
||||
if (maxSize != null)
|
||||
texture = texture.WithMaximumSize(maxSize.Value);
|
||||
if (texture != null)
|
||||
{
|
||||
if (maxSize != null)
|
||||
texture = texture.WithMaximumSize(maxSize.Value);
|
||||
|
||||
return new TexturedCharacterGlyph(new CharacterGlyph(character, 0, 0, texture.Width, texture.Height, null), texture, 1f / texture.ScaleAdjust);
|
||||
glyph = new TexturedCharacterGlyph(new CharacterGlyph(character, 0, 0, texture.Width, texture.Height, null), texture, 1f / texture.ScaleAdjust);
|
||||
}
|
||||
|
||||
cache[character] = glyph;
|
||||
return glyph;
|
||||
}
|
||||
|
||||
private static string getLookupName(char character)
|
||||
|
Loading…
Reference in New Issue
Block a user