From f70cf396342186a5ddd328031bfa1d26eb669c45 Mon Sep 17 00:00:00 2001 From: Benedikt Stebner Date: Mon, 21 Oct 2024 13:01:10 +0200 Subject: [PATCH] Make the name table optional (#17306) --- .../Media/Fonts/Tables/Name/NameTable.cs | 4 ++-- src/Skia/Avalonia.Skia/GlyphTypefaceImpl.cs | 24 ++++++++++++------- 2 files changed, 18 insertions(+), 10 deletions(-) diff --git a/src/Avalonia.Base/Media/Fonts/Tables/Name/NameTable.cs b/src/Avalonia.Base/Media/Fonts/Tables/Name/NameTable.cs index e2a5cbb681d..9a4b664f47b 100644 --- a/src/Avalonia.Base/Media/Fonts/Tables/Name/NameTable.cs +++ b/src/Avalonia.Base/Media/Fonts/Tables/Name/NameTable.cs @@ -98,11 +98,11 @@ public string GetNameById(ushort culture, KnownNameIds nameId) public string GetNameById(ushort culture, ushort nameId) => GetNameById(culture, (KnownNameIds)nameId); - public static NameTable Load(IGlyphTypeface glyphTypeface) + public static NameTable? Load(IGlyphTypeface glyphTypeface) { if (!glyphTypeface.TryGetTable(Tag, out var table)) { - throw new MissingFontTableException("Could not load table", "name"); + return null; } using var stream = new MemoryStream(table); diff --git a/src/Skia/Avalonia.Skia/GlyphTypefaceImpl.cs b/src/Skia/Avalonia.Skia/GlyphTypefaceImpl.cs index 06fe0e333c5..0b9a48deaba 100644 --- a/src/Skia/Avalonia.Skia/GlyphTypefaceImpl.cs +++ b/src/Skia/Avalonia.Skia/GlyphTypefaceImpl.cs @@ -17,7 +17,7 @@ internal class GlyphTypefaceImpl : IGlyphTypeface2 { private bool _isDisposed; private readonly SKTypeface _typeface; - private readonly NameTable _nameTable; + private readonly NameTable? _nameTable; private readonly OS2Table? _os2Table; private readonly HorizontalHeadTable? _hhTable; private IReadOnlyList? _supportedFeatures; @@ -100,18 +100,26 @@ public GlyphTypefaceImpl(SKTypeface typeface, FontSimulations fontSimulations) _nameTable = NameTable.Load(this); - TypographicFamilyName = _nameTable.GetNameById((ushort)CultureInfo.InvariantCulture.LCID, KnownNameIds.TypographicFamilyName); + //Rely on Skia if no name table is present + FamilyName = _nameTable?.FontFamilyName((ushort)CultureInfo.InvariantCulture.LCID) ?? typeface.FamilyName; - FamilyName = _nameTable.FontFamilyName((ushort)CultureInfo.InvariantCulture.LCID); + TypographicFamilyName = _nameTable?.GetNameById((ushort)CultureInfo.InvariantCulture.LCID, KnownNameIds.TypographicFamilyName) ?? FamilyName; - var familyNames = new Dictionary(_nameTable.Languages.Count); + if(_nameTable != null) + { + var familyNames = new Dictionary(_nameTable.Languages.Count); + + foreach (var language in _nameTable.Languages) + { + familyNames.Add(language, _nameTable.FontFamilyName(language)); + } - foreach (var language in _nameTable.Languages) + FamilyNames = familyNames; + } + else { - familyNames.Add(language, _nameTable.FontFamilyName(language)); + FamilyNames = new Dictionary { { (ushort)CultureInfo.InvariantCulture.LCID, FamilyName } }; } - - FamilyNames = familyNames; } public string TypographicFamilyName { get; }