SixLabors/Fonts

NullReferenceException thrown when invoking TextMeasurer.MeasureBounds

AndrewShepherd opened this issue · 8 comments

Prerequisites

  • I have written a descriptive issue title
  • I have verified that I am running the latest version of Fonts
  • I have verified if the problem exist in both DEBUG and RELEASE mode
  • I have searched open and closed issues to ensure it has not already been reported

Description

The method TextMeasurer.MeasureBounds throws a System.NullReferenceException when using the OpenSans font.

Steps to Reproduce

I downloaded the OpenSans font from Google Fonts

The following code demonstrates the issue:

using SixLabors.Fonts;

var fontCollection = new FontCollection();
FontFamily fontFamily = fontCollection.Add("./OpenSans/OpenSans-Regular.ttf");
Font font = new Font(fontFamily, 100, FontStyle.Regular);
var rendererOptions = new TextOptions(font)
{
	HorizontalAlignment = HorizontalAlignment.Left,
	VerticalAlignment = VerticalAlignment.Top,
};
FontRectangle bounds = TextMeasurer.MeasureBounds(
	"Hello World",
	rendererOptions
);

The call to MeasureBounds throws a NullReference exception. Here are the exception details:

System.NullReferenceException
  HResult=0x80004003
  Message=Object reference not set to an instance of an object.
  Source=SixLabors.Fonts
  StackTrace:
   at SixLabors.Fonts.Tables.AdvancedTypographic.GPosTable.GetFeatureLookups(Tag& stageFeature, ScriptClass script)
   at SixLabors.Fonts.Tables.AdvancedTypographic.GPosTable.TryUpdatePositions(FontMetrics fontMetrics, GlyphPositioningCollection collection, KerningMode kerningMode, Boolean& kerned)
   at SixLabors.Fonts.StreamFontMetrics.UpdatePositions(GlyphPositioningCollection collection, KerningMode kerningMode)
   at SixLabors.Fonts.FileFontMetrics.UpdatePositions(GlyphPositioningCollection collection, KerningMode kerningMode)
   at SixLabors.Fonts.TextLayout.ProcessText(ReadOnlySpan`1 text, TextOptions options)
   at SixLabors.Fonts.TextLayout.GenerateLayout(ReadOnlySpan`1 text, TextOptions options)
   at SixLabors.Fonts.TextMeasurer.TextMeasurerInt.MeasureBounds(ReadOnlySpan`1 text, TextOptions options)
   at SixLabors.Fonts.TextMeasurer.MeasureBounds(String text, TextOptions options)
   at Program.<Main>$(String[] args) in C:\Development\PlayArea\ImageSharp\MeasureBounds\MeasureBounds\Program.cs:line 11

System Configuration

  • Fonts version: 1.0.0-beta 16 (I am not explicitly including it)
  • Other Six Labors packages and versions:
    • SixLabors.ImageSharp version 2.1.2
    • SixLabors.ImageSharp.Drawing version 1.0.0-beta14
  • Environment (Operating system, version and so on): Windows 10
  • .NET Framework version: 6.0

Thanks for all the info.

Looks like a duplicate of #266 if you specifically target 1.0.0-beta17 the issue should be resolved.

I have attempted to explicitly target 1.0.0-beta17, but the problem still occurs.

To confirm if I've done it correctly: here are the references in my .csproj file:

	<ItemGroup>
		<PackageReference Include="SixLabors.ImageSharp" Version="2.1.2" />
		<PackageReference Include="SixLabors.ImageSharp.Drawing" Version="1.0.0-beta14" />
		<PackageReference Include="SixLabors.Fonts" Version="1.0.0-beta17" />
	</ItemGroup>

And here are the properties of the .DLL file in my project output folder:

fonts-dll-properties

Here is the file OpenSans-Regular.ttf that my example code was loading:

OpenSans-Regular.zip

Great. I'll have a look asap.

I do think #266 is a duplicate since it was reported against 1.0.0-beta17.

The fix hasn't been released to nuget yet, I haven't tested the myget beta17.8 (https://www.myget.org/feed/sixlabors/package/nuget/SixLabors.Fonts) but it would probably work since the fix was "obvious" 😄

Ha! I thought it was against 16!

Yeah, the MyGet version should work though I’ll double check.

I have tested this code using the myget repository, version 1.0.0-beta17.8. I can confirm it now runs without crashing.

	<ItemGroup>
		<PackageReference Include="SixLabors.ImageSharp" Version="2.1.2" />
		<PackageReference Include="SixLabors.Fonts" Version="1.0.0-beta17.8" />
	</ItemGroup>

Even though it was triggered through a different call then in #266, it's clearly the same error.

Not sure whether this case should be closed now or if it should be left open until the fix is in the public repository, just to avoid more duplicate posts. I'll leave that to you to decide.

Let's close this one. The search should still pick up duplicates.