Skip to content

Commit

Permalink
Merge pull request #360 from SixLabors/js/optional-cdf-load
Browse files Browse the repository at this point in the history
Fix for invalid ClassDefinitionTable offset handling
  • Loading branch information
JimBobSquarePants authored Sep 25, 2023
2 parents e646d68 + 85dd84f commit 63db8ad
Show file tree
Hide file tree
Showing 9 changed files with 45 additions and 11 deletions.
2 changes: 1 addition & 1 deletion samples/DrawWithImageSharp/DrawWithImageSharp.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="SixLabors.ImageSharp.Drawing" Version="1.0.0-beta15.14" />
<PackageReference Include="SixLabors.ImageSharp.Drawing" Version="2.0.0" />
<PackageReference Include="System.ValueTuple" Version="4.5.0" />
</ItemGroup>

Expand Down
2 changes: 1 addition & 1 deletion samples/DrawWithImageSharp/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -289,7 +289,7 @@ public static void RenderTextProcessorWithAlignment(

FontRectangle textSize = TextMeasurer.MeasureSize(text, textOptions);
using var img = new Image<Rgba32>(((int)textSize.Width * 2) + 20, ((int)textSize.Height * 2) + 20);
Size size = img.Size();
Size size = img.Size;
textOptions.Origin = new PointF(size.Width / 2F, size.Height / 2F);

img.Mutate(x => x.Fill(Color.Black).ApplyProcessor(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public static AttachmentListTable Load(BigEndianBinaryReader reader, long offset
Span<ushort> attachPointOffsets = attachPointOffsetsBuffer.GetSpan();
reader.ReadUInt16Array(attachPointOffsets);

var attachmentListTable = new AttachmentListTable
AttachmentListTable attachmentListTable = new()
{
CoverageTable = CoverageTable.Load(reader, offset + coverageOffset),
AttachPoints = new AttachPoint[glyphCount]
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
// Copyright (c) Six Labors.
// Licensed under the Six Labors Split License.

using System.Diagnostics.CodeAnalysis;

namespace SixLabors.Fonts.Tables.AdvancedTypographic;

/// <summary>
Expand All @@ -20,6 +22,26 @@ internal abstract class ClassDefinitionTable
/// <returns>The class id.</returns>
public abstract int ClassIndexOf(ushort glyphId);

public static bool TryLoad(BigEndianBinaryReader reader, long offset, [NotNullWhen(true)] out ClassDefinitionTable? table)
{
if (offset == 0)
{
table = null;
return false;
}

reader.Seek(offset, SeekOrigin.Begin);
ushort classFormat = reader.ReadUInt16();
table = classFormat switch
{
1 => ClassDefinitionFormat1Table.Load(reader),
2 => ClassDefinitionFormat2Table.Load(reader),
_ => null
};

return table is not null;
}

public static ClassDefinitionTable Load(BigEndianBinaryReader reader, long offset)
{
reader.Seek(offset, SeekOrigin.Begin);
Expand Down Expand Up @@ -97,7 +119,7 @@ public static ClassDefinitionFormat2Table Load(BigEndianBinaryReader reader)
// | | | startGlyphID |
// +------------------+------------------------------------+-----------------------------------------+
ushort classRangeCount = reader.ReadUInt16();
var records = new ClassRangeRecord[classRangeCount];
ClassRangeRecord[] records = new ClassRangeRecord[classRangeCount];
for (int i = 0; i < records.Length; ++i)
{
// +--------+--------------+------------------------------------+
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -145,22 +145,20 @@ public static GlyphDefinitionTable Load(BigEndianBinaryReader reader)
throw new InvalidFontFileException($"Invalid value for 'minor version' {minorVersion} of GDEF table. Should be '0', '2' or '3'.");
}

ClassDefinitionTable? classDefinitionTable = glyphClassDefOffset is 0 ? null : ClassDefinitionTable.Load(reader, glyphClassDefOffset);
ClassDefinitionTable.TryLoad(reader, glyphClassDefOffset, out ClassDefinitionTable? classDefinitionTable);
AttachmentListTable? attachmentListTable = attachListOffset is 0 ? null : AttachmentListTable.Load(reader, attachListOffset);
LigatureCaretList? ligatureCaretList = ligatureCaretListOffset is 0 ? null : LigatureCaretList.Load(reader, ligatureCaretListOffset);
ClassDefinitionTable? markAttachmentClassDef = markAttachClassDefOffset is 0 ? null : ClassDefinitionTable.Load(reader, markAttachClassDefOffset);
ClassDefinitionTable.TryLoad(reader, markAttachClassDefOffset, out ClassDefinitionTable? markAttachmentClassDef);
MarkGlyphSetsTable? markGlyphSetsTable = markGlyphSetsDefOffset is 0 ? null : MarkGlyphSetsTable.Load(reader, markGlyphSetsDefOffset);

var glyphDefinitionTable = new GlyphDefinitionTable()
// TODO: read itemVarStore.
return new GlyphDefinitionTable()
{
GlyphClassDefinition = classDefinitionTable,
AttachmentListTable = attachmentListTable,
LigatureCaretList = ligatureCaretList,
MarkAttachmentClassDef = markAttachmentClassDef,
MarkGlyphSetsTable = markGlyphSetsTable
};

// TODO: read itemVarStore.
return glyphDefinitionTable;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public static MarkGlyphSetsTable Load(BigEndianBinaryReader reader, long offset)
{
reader.Seek(offset, SeekOrigin.Begin);

var markGlyphSetsTable = new MarkGlyphSetsTable
MarkGlyphSetsTable markGlyphSetsTable = new()
{
Format = reader.ReadUInt16()
};
Expand Down
12 changes: 12 additions & 0 deletions tests/SixLabors.Fonts.Tests/FontLoaderTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -137,4 +137,16 @@ public void LoadFontWoff()
// the test font only has characters .notdef, 'a' & 'b' defined
Assert.Equal(6, r.ControlPoints.Distinct().Count());
}

[Fact]
public void LoadFontWithIncorrectClassDefinitionTableOffset()
{
// The following font contains a ClassDefinitionTable with an invalid offset.
// See https://forum.stimulsoft.com/viewtopic.php?t=60972
Font font = new FontCollection().Add(TestFonts.THSarabunFile).CreateFont(12);

FontRectangle advance = TextMeasurer.MeasureAdvance("เราใช้คุกกี้เพื่อพัฒนาประสิทธิภาพ และประสบการณ์ที่ดีในการใช้เว็บไซต์ของคุณ คุณสามารถศึกษารายละเอียดได้ที่", new(font));

Assert.NotEqual(default, advance);
}
}
Binary file added tests/SixLabors.Fonts.Tests/Fonts/THSarabun.ttf
Binary file not shown.
2 changes: 2 additions & 0 deletions tests/SixLabors.Fonts.Tests/TestFonts.cs
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,8 @@ public static class TestFonts

public static string DFKaiSBFile => GetFullPath("kaiu.ttf");

public static string THSarabunFile => GetFullPath("THSarabun.ttf");

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

public static Stream SegoeuiEmojiData() => OpenStream(SegoeuiEmojiFile);
Expand Down

0 comments on commit 63db8ad

Please sign in to comment.