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

Use empty anchor as fallback when parsing mark anchors #419

Merged
merged 1 commit into from
Oct 29, 2024
Merged
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
22 changes: 20 additions & 2 deletions src/SixLabors.Fonts/Tables/AdvancedTypographic/GPos/AnchorTable.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ namespace SixLabors.Fonts.Tables.AdvancedTypographic.GPos;
[DebuggerDisplay("X: {XCoordinate}, Y: {YCoordinate}")]
internal abstract class AnchorTable
{
private static readonly AnchorTable Empty = new EmptyAnchor();

/// <summary>
/// Initializes a new instance of the <see cref="AnchorTable"/> class.
/// </summary>
Expand Down Expand Up @@ -50,7 +52,10 @@ public static AnchorTable Load(BigEndianBinaryReader reader, long offset)
1 => AnchorFormat1.Load(reader),
2 => AnchorFormat2.Load(reader),
3 => AnchorFormat3.Load(reader),
_ => throw new InvalidFontFileException($"anchorFormat identifier {anchorFormat} is invalid. Should be '1', '2' or '3'.")

// Harfbuzz (Anchor.hh) and FontKit appear to treat this as a default anchor and do not throw.
// NotoSans Regular can trigger this. See https://github.com/SixLabors/Fonts/issues/417
_ => Empty,
};
}

Expand Down Expand Up @@ -119,7 +124,6 @@ public override AnchorXY GetAnchor(FontMetrics fontMetrics, GlyphShapingData dat
{
foreach (GlyphMetrics metric in metrics)
{
// TODO: What does HarfBuzz do here?
if (metric is not TrueTypeGlyphMetrics ttmetric)
{
break;
Expand Down Expand Up @@ -185,4 +189,18 @@ public static AnchorFormat3 Load(BigEndianBinaryReader reader)
public override AnchorXY GetAnchor(FontMetrics fontMetrics, GlyphShapingData data, GlyphPositioningCollection collection)
=> new(this.XCoordinate, this.YCoordinate);
}

internal sealed class EmptyAnchor : AnchorTable
{
public EmptyAnchor()
: base(0, 0)
{
}

public override AnchorXY GetAnchor(
FontMetrics fontMetrics,
GlyphShapingData data,
GlyphPositioningCollection collection)
=> new(0, 0);
}
}
Binary file not shown.
38 changes: 38 additions & 0 deletions tests/SixLabors.Fonts.Tests/Issues/Issues_417.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
// Copyright (c) Six Labors.
// Licensed under the Six Labors Split License.

namespace SixLabors.Fonts.Tests.Issues;

public class Issues_417
{
[Fact]
public void DoesNotThrow_InvalidAnchor()
{
FontFamily family = new FontCollection().Add(TestFonts.NotoSansRegular);
family.TryGetMetrics(FontStyle.Regular, out FontMetrics metrics);

Font font = family.CreateFont(metrics?.UnitsPerEm ?? 1000);

TextOptions options = new(font);

// References values generated using.
// https://www.corvelsoftware.co.uk/crowbar/
TextMeasurer.TryMeasureCharacterAdvances("Text", options, out ReadOnlySpan<GlyphBounds> advances);

Assert.Equal(4, advances.Length);
Assert.Equal(486, advances[0].Bounds.Width);
Assert.Equal(544, advances[1].Bounds.Width);
Assert.Equal(529, advances[2].Bounds.Width);
Assert.Equal(361, advances[3].Bounds.Width);

GlyphRenderer renderer = new();
TextRenderer.RenderTextTo(renderer, "Text", new TextOptions(font));

int[] expectedGlyphIndices = { 55, 72, 91, 87 };
Assert.Equal(expectedGlyphIndices.Length, renderer.GlyphKeys.Count);
for (int i = 0; i < expectedGlyphIndices.Length; i++)
{
Assert.Equal(expectedGlyphIndices[i], renderer.GlyphKeys[i].GlyphId);
}
}
}
2 changes: 2 additions & 0 deletions tests/SixLabors.Fonts.Tests/TestFonts.cs
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,8 @@

public static string KellySlabFile => GetFullPath("KellySlab-Regular.ttf");

public static string NotoSansRegular => GetFullPath("NotoSans-Regular.ttf");

public static Stream TwemojiMozillaData() => OpenStream(TwemojiMozillaFile);

public static Stream SegoeuiEmojiData() => OpenStream(SegoeuiEmojiFile);
Expand Down Expand Up @@ -308,7 +310,7 @@

private static string GetFullPath(string path)
{
string root = Path.GetDirectoryName(new Uri(typeof(TestFonts).GetTypeInfo().Assembly.CodeBase).LocalPath);

Check warning on line 313 in tests/SixLabors.Fonts.Tests/TestFonts.cs

View workflow job for this annotation

GitHub Actions / Build (false, macos-latest, net6.0, 6.0.x, -x64, false)

'Assembly.CodeBase' is obsolete: 'Assembly.CodeBase and Assembly.EscapedCodeBase are only included for .NET Framework compatibility. Use Assembly.Location.' (https://aka.ms/dotnet-warnings/SYSLIB0012)

Check warning on line 313 in tests/SixLabors.Fonts.Tests/TestFonts.cs

View workflow job for this annotation

GitHub Actions / Build (false, windows-latest, net6.0, 6.0.x, -x64, true)

'Assembly.CodeBase' is obsolete: 'Assembly.CodeBase and Assembly.EscapedCodeBase are only included for .NET Framework compatibility. Use Assembly.Location.' (https://aka.ms/dotnet-warnings/SYSLIB0012)

Check warning on line 313 in tests/SixLabors.Fonts.Tests/TestFonts.cs

View workflow job for this annotation

GitHub Actions / Build (false, windows-latest, net6.0, 6.0.x, -x64, true)

'Assembly.CodeBase' is obsolete: 'Assembly.CodeBase and Assembly.EscapedCodeBase are only included for .NET Framework compatibility. Use Assembly.Location.' (https://aka.ms/dotnet-warnings/SYSLIB0012)

string[] paths = new[]
{
Expand Down
Loading