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

[POC] Support ttc font format. #834

Draft
wants to merge 2 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
Original file line number Diff line number Diff line change
Expand Up @@ -326,6 +326,7 @@ private void load(OsuColour colour)
FontFormat.Internal => colour.Gray7,
FontFormat.Fnt => colour.Pink,
FontFormat.Ttf => colour.Blue,
FontFormat.Ttc => colour.BlueDark,
_ => throw new ArgumentOutOfRangeException(nameof(fontFormat))
};

Expand Down
5 changes: 3 additions & 2 deletions osu.Game.Rulesets.Karaoke/IO/Stores/TtfGlyphStore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ public TtfGlyphStore(ResourceStore<byte[]> store, string assetName = null)
Store = new ResourceStore<byte[]>(store);

Store.AddExtension("ttf");
Store.AddExtension("ttc");

AssetName = assetName;

Expand All @@ -73,8 +74,8 @@ public Task LoadFontAsync() => fontLoadTask ??= Task.Factory.StartNew(() =>
using (var s = Store.GetStream($@"{AssetName}"))
{
var fonts = new FontCollection();
var fontFamily = fonts.Add(s);
font = new Font(fontFamily, 1);
var fontFamily = fonts.AddCollection(s, out var description).ToArray();
font = new Font(fontFamily[0], 1);
}

completionSource.SetResult(font);
Expand Down
2 changes: 2 additions & 0 deletions osu.Game.Rulesets.Karaoke/Skinning/Fonts/FontInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,5 +44,7 @@ public enum FontFormat
Fnt,

Ttf,

Ttc,
}
}
19 changes: 18 additions & 1 deletion osu.Game.Rulesets.Karaoke/Skinning/Fonts/FontManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public class FontManager : Component

private Storage storage => host.Storage.GetStorageForDirectory(FONT_BASE_PATH);

private readonly FontFormat[] supportedFormat = { FontFormat.Fnt, FontFormat.Ttf };
private readonly FontFormat[] supportedFormat = { FontFormat.Fnt, FontFormat.Ttf, FontFormat.Ttc };

public readonly BindableList<FontInfo> Fonts = new();

Expand Down Expand Up @@ -188,6 +188,7 @@ private void removeFontFromList(string path, FontFormat fontFormat)
{
FontFormat.Fnt => getFntGlyphStore(fontName),
FontFormat.Ttf => getTtfGlyphStore(fontName),
FontFormat.Ttc => getTtcGlyphStore(fontName),
FontFormat.Internal or _ => throw new ArgumentOutOfRangeException(nameof(fontFormat))
};
}
Expand Down Expand Up @@ -216,11 +217,25 @@ private void removeFontFromList(string path, FontFormat fontFormat)
return new TtfGlyphStore(new ResourceStore<byte[]>(resources), $"{fontName}");
}

private TtfGlyphStore? getTtcGlyphStore(string fontName)
{
string path = Path.Combine(getPathByFontType(FontFormat.Ttc), fontName);
string? pathWithExtension = Path.ChangeExtension(path, getExtensionByFontType(FontFormat.Ttc));

if (!storage.Exists(pathWithExtension))
return null;

// because ttc is just a collection of ttf file, so we can use TtfGlyphStore to read it.
var resources = new StorageBackedResourceStore(storage.GetStorageForDirectory(getPathByFontType(FontFormat.Ttc)));
return new TtfGlyphStore(new ResourceStore<byte[]>(resources), $"{fontName}");
}

private static string getPathByFontType(FontFormat type) =>
type switch
{
FontFormat.Fnt => "fnt",
FontFormat.Ttf => "ttf",
FontFormat.Ttc => "ttc",
FontFormat.Internal or _ => throw new ArgumentOutOfRangeException(nameof(type))
};

Expand All @@ -229,6 +244,7 @@ private static string getExtensionByFontType(FontFormat type) =>
{
FontFormat.Fnt => "zipfnt",
FontFormat.Ttf => "ttf",
FontFormat.Ttc => "ttc",
FontFormat.Internal or _ => throw new ArgumentOutOfRangeException(nameof(type))
};

Expand All @@ -237,6 +253,7 @@ private static FontFormat getFontTypeByExtension(string extension) =>
{
".zipfnt" => FontFormat.Fnt,
".ttf" => FontFormat.Ttf,
".ttc" => FontFormat.Ttc,
_ => throw new FormatException(nameof(extension)),
};

Expand Down