Skip to content

Commit d55421b

Browse files
GillibaldMrJul
andauthored
Introduce GlyphTypeface.FaceNames (#18392)
Co-authored-by: Julien Lebosquain <[email protected]>
1 parent edaaf5b commit d55421b

File tree

3 files changed

+51
-38
lines changed

3 files changed

+51
-38
lines changed

src/Avalonia.Base/Media/Fonts/Tables/Name/NameTable.cs

Lines changed: 13 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -2,26 +2,25 @@
22
// Licensed under the Apache License, Version 2.0.
33
// Ported from: https://github.com/SixLabors/Fonts/blob/034a440aece357341fcc6b02db58ffbe153e54ef/src/SixLabors.Fonts
44

5+
using System.Collections;
56
using System.Collections.Generic;
67
using System.IO;
8+
using Avalonia.Utilities;
79

810
namespace Avalonia.Media.Fonts.Tables.Name
911
{
10-
internal class NameTable
12+
internal class NameTable : IEnumerable<NameRecord>
1113
{
1214
internal const string TableName = "name";
1315
internal static readonly OpenTypeTag Tag = OpenTypeTag.Parse(TableName);
1416

1517
private readonly NameRecord[] _names;
1618

17-
internal NameTable(NameRecord[] names, IReadOnlyList<ushort> languages)
19+
internal NameTable(NameRecord[] names)
1820
{
1921
_names = names;
20-
Languages = languages;
2122
}
2223

23-
public IReadOnlyList<ushort> Languages { get; }
24-
2524
/// <summary>
2625
/// Gets the name of the font.
2726
/// </summary>
@@ -133,22 +132,6 @@ public static NameTable Load(BigEndianBinaryReader reader)
133132
}
134133
}
135134

136-
//var languageNames = Array.Empty<StringLoader>();
137-
138-
//if (format == 1)
139-
//{
140-
// // Format 1 adds language data.
141-
// var langCount = reader.ReadUInt16();
142-
// languageNames = new StringLoader[langCount];
143-
144-
// for (var i = 0; i < langCount; i++)
145-
// {
146-
// languageNames[i] = StringLoader.Create(reader);
147-
148-
// strings.Add(languageNames[i]);
149-
// }
150-
//}
151-
152135
foreach (var readable in strings)
153136
{
154137
var readableStartOffset = stringOffset + readable.Offset;
@@ -158,22 +141,17 @@ public static NameTable Load(BigEndianBinaryReader reader)
158141
readable.LoadValue(reader);
159142
}
160143

161-
var cultures = new List<ushort>();
162-
163-
foreach (var nameRecord in names)
164-
{
165-
if (nameRecord.NameID != KnownNameIds.FontFamilyName || nameRecord.Platform != PlatformIDs.Windows || nameRecord.LanguageID == 0)
166-
{
167-
continue;
168-
}
144+
return new NameTable(names);
145+
}
169146

170-
if (!cultures.Contains(nameRecord.LanguageID))
171-
{
172-
cultures.Add(nameRecord.LanguageID);
173-
}
174-
}
147+
public IEnumerator<NameRecord> GetEnumerator()
148+
{
149+
return new ImmutableReadOnlyListStructEnumerator<NameRecord>(_names);
150+
}
175151

176-
return new NameTable(names, cultures);
152+
IEnumerator IEnumerable.GetEnumerator()
153+
{
154+
return GetEnumerator();
177155
}
178156
}
179157
}

src/Avalonia.Base/Media/IGlyphTypeface2.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,5 +29,11 @@ internal interface IGlyphTypeface2 : IGlyphTypeface
2929
/// Gets supported font features.
3030
/// </summary>
3131
IReadOnlyList<OpenTypeTag> SupportedFeatures { get; }
32+
33+
/// <summary>
34+
/// Gets the localized face names.
35+
/// <para>Keys are culture identifiers.</para>
36+
/// </summary>
37+
IReadOnlyDictionary<ushort, string> FaceNames { get; }
3238
}
3339
}

src/Skia/Avalonia.Skia/GlyphTypefaceImpl.cs

Lines changed: 32 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -111,25 +111,54 @@ public GlyphTypefaceImpl(SKTypeface typeface, FontSimulations fontSimulations)
111111

112112
if(_nameTable != null)
113113
{
114-
var familyNames = new Dictionary<ushort, string>(_nameTable.Languages.Count);
114+
var familyNames = new Dictionary<ushort, string>(1);
115+
var faceNames = new Dictionary<ushort, string>(1);
115116

116-
foreach (var language in _nameTable.Languages)
117+
foreach (var nameRecord in _nameTable)
117118
{
118-
familyNames.Add(language, _nameTable.FontFamilyName(language));
119+
if(nameRecord.NameID == KnownNameIds.FontFamilyName)
120+
{
121+
if (nameRecord.Platform != PlatformIDs.Windows || nameRecord.LanguageID == 0)
122+
{
123+
continue;
124+
}
125+
126+
if (!familyNames.ContainsKey(nameRecord.LanguageID))
127+
{
128+
familyNames[nameRecord.LanguageID] = nameRecord.Value;
129+
}
130+
}
131+
132+
if(nameRecord.NameID == KnownNameIds.FontSubfamilyName)
133+
{
134+
if (nameRecord.Platform != PlatformIDs.Windows || nameRecord.LanguageID == 0)
135+
{
136+
continue;
137+
}
138+
139+
if (!faceNames.ContainsKey(nameRecord.LanguageID))
140+
{
141+
faceNames[nameRecord.LanguageID] = nameRecord.Value;
142+
}
143+
}
119144
}
120145

121146
FamilyNames = familyNames;
147+
FaceNames = faceNames;
122148
}
123149
else
124150
{
125151
FamilyNames = new Dictionary<ushort, string> { { (ushort)CultureInfo.InvariantCulture.LCID, FamilyName } };
152+
FaceNames = new Dictionary<ushort, string> { { (ushort)CultureInfo.InvariantCulture.LCID, Weight.ToString() } };
126153
}
127154
}
128155

129156
public string TypographicFamilyName { get; }
130157

131158
public IReadOnlyDictionary<ushort, string> FamilyNames { get; }
132159

160+
public IReadOnlyDictionary<ushort, string> FaceNames { get; }
161+
133162
public IReadOnlyList<OpenTypeTag> SupportedFeatures
134163
{
135164
get

0 commit comments

Comments
 (0)