Skip to content

Commit 30eeab6

Browse files
Merge pull request #513 from SixLabors/js/fix-512
Ensure CFF table is present on load
2 parents b7e6a28 + 5b1919e commit 30eeab6

File tree

4 files changed

+29
-4
lines changed

4 files changed

+29
-4
lines changed

src/SixLabors.Fonts/StreamFontMetrics.Cff.cs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,9 @@ private static StreamFontMetrics LoadCompactFont(FontReader reader)
3333
NameTable name = reader.GetTable<NameTable>();
3434
CMapTable cmap = reader.GetTable<CMapTable>();
3535
PostTable post = reader.GetTable<PostTable>();
36-
ICffTable? cff = reader.TryGetTable<Cff1Table>() ?? (ICffTable?)reader.TryGetTable<Cff2Table>();
36+
ICffTable? cff =
37+
(reader.TryGetTable<Cff1Table>() ?? (ICffTable?)reader.TryGetTable<Cff2Table>())
38+
?? throw new InvalidFontFileException("Missing required CFF table.");
3739

3840
// TODO: VORG
3941
HorizontalMetricsTable htmx = reader.GetTable<HorizontalMetricsTable>();
@@ -63,18 +65,17 @@ private static StreamFontMetrics LoadCompactFont(FontReader reader)
6365
MVarTable? mVar = reader.TryGetTable<MVarTable>();
6466

6567
GlyphVariationProcessor? glyphVariationProcessor = null;
66-
if (cff?.ItemVariationStore != null)
68+
if (cff.ItemVariationStore != null)
6769
{
6870
if (fVar is null)
6971
{
7072
throw new InvalidFontFileException("missing fvar table required for glyph variations processing");
7173
}
7274

73-
// TODO: The docs say that hvar and vvar can be used for CFF fonts so how do we determine when to use them?
7475
glyphVariationProcessor = new GlyphVariationProcessor(cff.ItemVariationStore, fVar, aVar, gVar, hVar, vVar, mVar);
7576
}
7677

77-
CompactFontTables tables = new(cmap, head, hhea, htmx, maxp, name, os2, post, cff!)
78+
CompactFontTables tables = new(cmap, head, hhea, htmx, maxp, name, os2, post, cff)
7879
{
7980
Kern = kern,
8081
Vhea = vhea,
Binary file not shown.
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
// Copyright (c) Six Labors.
2+
// Licensed under the Six Labors Split License.
3+
4+
namespace SixLabors.Fonts.Tests.Issues;
5+
6+
public class Issues_512
7+
{
8+
[Fact]
9+
public void MissingCffTableThrowsInvalidFontFileExceptionInsteadOfNullReferenceException()
10+
{
11+
const string text = "Hello";
12+
13+
FontFamily family = new FontCollection().Add(TestFonts.Issues.Issue512_CreateCffGlyphMetrics);
14+
15+
Font font = family.CreateFont(12);
16+
17+
TextOptions options = new(font);
18+
19+
InvalidFontFileException exception = Assert.Throws<InvalidFontFileException>(() => TextMeasurer.MeasureSize(text, options));
20+
Assert.Equal("Missing required CFF table.", exception.Message);
21+
}
22+
}

tests/SixLabors.Fonts.Tests/TestFonts.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -411,6 +411,8 @@ public static class Issues
411411
public static string Issue97File => GetFullPath("Issues/Issue97.fuzz");
412412

413413
public static string Issue298File => GetFullPath("Issues/StyleScript.ttf");
414+
415+
public static string Issue512_CreateCffGlyphMetrics => GetFullPath("Issues/StreamFontMetri.CreateCffGlyphMetrics.otf");
414416
}
415417

416418
private static Stream OpenStream(string path) =>

0 commit comments

Comments
 (0)