diff --git a/src/Avalonia.Base/Media/Fonts/Tables/Name/NameTable.cs b/src/Avalonia.Base/Media/Fonts/Tables/Name/NameTable.cs index 9a4b664f47..c0c1048e51 100644 --- a/src/Avalonia.Base/Media/Fonts/Tables/Name/NameTable.cs +++ b/src/Avalonia.Base/Media/Fonts/Tables/Name/NameTable.cs @@ -2,26 +2,25 @@ // Licensed under the Apache License, Version 2.0. // Ported from: https://github.com/SixLabors/Fonts/blob/034a440aece357341fcc6b02db58ffbe153e54ef/src/SixLabors.Fonts +using System.Collections; using System.Collections.Generic; using System.IO; +using Avalonia.Utilities; namespace Avalonia.Media.Fonts.Tables.Name { - internal class NameTable + internal class NameTable : IEnumerable { internal const string TableName = "name"; internal static readonly OpenTypeTag Tag = OpenTypeTag.Parse(TableName); private readonly NameRecord[] _names; - internal NameTable(NameRecord[] names, IReadOnlyList languages) + internal NameTable(NameRecord[] names) { _names = names; - Languages = languages; } - public IReadOnlyList Languages { get; } - /// /// Gets the name of the font. /// @@ -133,22 +132,6 @@ namespace Avalonia.Media.Fonts.Tables.Name } } - //var languageNames = Array.Empty(); - - //if (format == 1) - //{ - // // Format 1 adds language data. - // var langCount = reader.ReadUInt16(); - // languageNames = new StringLoader[langCount]; - - // for (var i = 0; i < langCount; i++) - // { - // languageNames[i] = StringLoader.Create(reader); - - // strings.Add(languageNames[i]); - // } - //} - foreach (var readable in strings) { var readableStartOffset = stringOffset + readable.Offset; @@ -158,22 +141,17 @@ namespace Avalonia.Media.Fonts.Tables.Name readable.LoadValue(reader); } - var cultures = new List(); - - foreach (var nameRecord in names) - { - if (nameRecord.NameID != KnownNameIds.FontFamilyName || nameRecord.Platform != PlatformIDs.Windows || nameRecord.LanguageID == 0) - { - continue; - } + return new NameTable(names); + } - if (!cultures.Contains(nameRecord.LanguageID)) - { - cultures.Add(nameRecord.LanguageID); - } - } + public IEnumerator GetEnumerator() + { + return new ImmutableReadOnlyListStructEnumerator(_names); + } - return new NameTable(names, cultures); + IEnumerator IEnumerable.GetEnumerator() + { + return GetEnumerator(); } } } diff --git a/src/Avalonia.Base/Media/IGlyphTypeface2.cs b/src/Avalonia.Base/Media/IGlyphTypeface2.cs index 84b892dae3..3bd2b1e767 100644 --- a/src/Avalonia.Base/Media/IGlyphTypeface2.cs +++ b/src/Avalonia.Base/Media/IGlyphTypeface2.cs @@ -29,5 +29,11 @@ namespace Avalonia.Media /// Gets supported font features. /// IReadOnlyList SupportedFeatures { get; } + + /// + /// Gets the localized face names. + /// Keys are culture identifiers. + /// + IReadOnlyDictionary FaceNames { get; } } } diff --git a/src/Skia/Avalonia.Skia/GlyphTypefaceImpl.cs b/src/Skia/Avalonia.Skia/GlyphTypefaceImpl.cs index 9170393034..2def64c18d 100644 --- a/src/Skia/Avalonia.Skia/GlyphTypefaceImpl.cs +++ b/src/Skia/Avalonia.Skia/GlyphTypefaceImpl.cs @@ -111,18 +111,45 @@ namespace Avalonia.Skia if(_nameTable != null) { - var familyNames = new Dictionary(_nameTable.Languages.Count); + var familyNames = new Dictionary(1); + var faceNames = new Dictionary(1); - foreach (var language in _nameTable.Languages) + foreach (var nameRecord in _nameTable) { - familyNames.Add(language, _nameTable.FontFamilyName(language)); + if(nameRecord.NameID == KnownNameIds.FontFamilyName) + { + if (nameRecord.Platform != PlatformIDs.Windows || nameRecord.LanguageID == 0) + { + continue; + } + + if (!familyNames.ContainsKey(nameRecord.LanguageID)) + { + familyNames[nameRecord.LanguageID] = nameRecord.Value; + } + } + + if(nameRecord.NameID == KnownNameIds.FontSubfamilyName) + { + if (nameRecord.Platform != PlatformIDs.Windows || nameRecord.LanguageID == 0) + { + continue; + } + + if (!faceNames.ContainsKey(nameRecord.LanguageID)) + { + faceNames[nameRecord.LanguageID] = nameRecord.Value; + } + } } FamilyNames = familyNames; + FaceNames = faceNames; } else { FamilyNames = new Dictionary { { (ushort)CultureInfo.InvariantCulture.LCID, FamilyName } }; + FaceNames = new Dictionary { { (ushort)CultureInfo.InvariantCulture.LCID, Weight.ToString() } }; } } @@ -130,6 +157,8 @@ namespace Avalonia.Skia public IReadOnlyDictionary FamilyNames { get; } + public IReadOnlyDictionary FaceNames { get; } + public IReadOnlyList SupportedFeatures { get