Skip to content
Open
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
31 changes: 31 additions & 0 deletions src/EPPlus.Export.Pdf.Tests/FontTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,12 @@ This software is licensed under PolyForm Noncommercial License 1.0.0
using EPPlus.Fonts.OpenType;
using EPPlus.Fonts.OpenType.Integration;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using OfficeOpenXml.FormulaParsing.Utilities;
using OfficeOpenXml.Interfaces.Fonts;
using System;
using System.Collections.Generic;
using System.IO;
using System.Text.RegularExpressions;

namespace EPPlus.Export.Pdf.Tests
{
Expand Down Expand Up @@ -168,5 +170,34 @@ public void AddFontData_Embedded_DoesNotEmitSimpleFontObject()
}
}
}

[TestMethod]
public void FontObjects_ForSubsetFont_ShareIdenticalTaggedName()
{
var engine = new OpenTypeFontEngine(cfg => cfg.SearchSystemDirectories = true);
if(engine.GetFontAvailability("Calibri") != FontAvailability.Exact)
{
Assert.Inconclusive("Calibri font is not available on this system. This test requires Calibri to be installed.");
}
var font = engine.LoadFont("Calibri");
var subset = new SingleFontSubsetter().Subset(font, new HashSet<int> { 65, 66, 67 }, true);

var resource = new PdfFontResource("Calibri", FontSubFamily.Regular, 1, new PdfPageSettings(engine));
resource.fontData = subset;

// Object numbers are arbitrary; they only need to be set in dependency order.
var descriptor = resource.GetFontDescriptorObject(10).RenderDictionary();
var cid = resource.GetCIDFontObject(11).RenderDictionary();
var type0 = resource.GetType0FontDictObject(12).RenderDictionary();

var rx = new Regex(@"/(?:BaseFont|FontName)\s+/(\S+)");
var names = new[] { descriptor, cid, type0 }
.Select(s => rx.Match(s).Groups[1].Value)
.ToList();

foreach (var n in names)
Assert.IsTrue(Regex.IsMatch(n, "^[A-Z]{6}\\+[^+\\s]+$"), n);
Assert.AreEqual(1, names.Distinct().Count(), string.Join(" | ", names.ToArray()));
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
using EPPlus.Fonts.OpenType;
using EPPlus.Fonts.OpenType.Subsetting;
using EPPlus.Fonts.OpenType.Tests;
using OfficeOpenXml.Interfaces.Fonts;
using System.Text.RegularExpressions;

[TestClass]
public class DocumentFontSubsetBuilderTagTests : FontTestBase // adjust to your base class
{
private static readonly Regex TaggedName = new Regex("^[A-Z]{6}\\+.+$");

public override TestContext? TestContext { get; set; }

private DocumentFontSubsetBuilder CreateBuilder()
{
return new DocumentFontSubsetBuilder(SystemFontsEngine);
}

[TestMethod]
public void Build_SubsettedFont_HasSubsetTag()
{
RequireFont(SystemFontsEngine, "Calibri", FontSubFamily.Regular);
var builder = CreateBuilder();
builder.AddText("Calibri", FontSubFamily.Regular, "ABC");
builder.Build();

var fonts = builder.GetFontsToEmbed().ToList();

Assert.AreEqual(1, fonts.Count);
// NOTE: property name for the font on SubsettedFont is assumed to be "Font".
Assert.IsTrue(TaggedName.IsMatch(fonts[0].Font.FullName), fonts[0].Font.FullName);
}

[TestMethod]
public void Build_TaggedSubset_IsStillFoundByOriginalIdentity()
{
// Regression guard: the tag must not leak into the identity used as dictionary key.
RequireFont(SystemFontsEngine, "Calibri", FontSubFamily.Regular);
var builder = CreateBuilder();
builder.AddText("Calibri", FontSubFamily.Regular, "ABC");
builder.Build();

var provider = builder.GetShapingProvider("Calibri", FontSubFamily.Regular);
Assert.IsNotNull(provider);

var fonts = builder.GetFontsToEmbed().ToList();
Assert.AreEqual("Calibri", fonts[0].Family);
Assert.AreEqual(FontSubFamily.Regular, fonts[0].SubFamily);
}

[TestMethod]
public void Build_ShapingProvider_ResolvesGlyphsFromTaggedSubset()
{
RequireFont(SystemFontsEngine, "Calibri", FontSubFamily.Regular);
var builder = CreateBuilder();
builder.AddText("Calibri", FontSubFamily.Regular, "ABC");
builder.Build();

var provider = builder.GetShapingProvider("Calibri", FontSubFamily.Regular);

OpenTypeFont dest;
ushort glyphId;
provider.TryGetGlyphFont((uint)'A', out dest, out glyphId);

Assert.AreNotEqual((ushort)0, glyphId);
Assert.IsTrue(TaggedName.IsMatch(dest.FullName), dest.FullName);
}

[TestMethod]
public void Build_DoesNotMutateCachedOriginalFont()
{
RequireFont(SystemFontsEngine, "Calibri", FontSubFamily.Regular);
var before = SystemFontsEngine.LoadFont("Calibri").FullName;

var builder = CreateBuilder();
builder.AddText("Calibri", FontSubFamily.Regular, "ABC");
builder.Build();

var after = SystemFontsEngine.LoadFont("Calibri").FullName;
Assert.AreEqual(before, after);
Assert.IsFalse(TaggedName.IsMatch(after));
}

[TestMethod]
public void Build_DifferentFontsAndSameInputTwice_TagsAreDistinctAndDeterministic()
{
RequireFont(SystemFontsEngine, "Calibri", FontSubFamily.Regular);
RequireFont(SystemFontsEngine, "Calibri", FontSubFamily.Bold);

var a = CreateBuilder();
a.AddText("Calibri", FontSubFamily.Regular, "ABC");
a.AddText("Calibri", FontSubFamily.Bold, "ABC");
a.Build();

var b = CreateBuilder();
b.AddText("Calibri", FontSubFamily.Regular, "ABC");
b.AddText("Calibri", FontSubFamily.Bold, "ABC");
b.Build();

var namesA = a.GetFontsToEmbed().Select(f => f.Font.FullName).OrderBy(n => n).ToList();
var namesB = b.GetFontsToEmbed().Select(f => f.Font.FullName).OrderBy(n => n).ToList();

Assert.AreEqual(2, namesA.Count);
Assert.AreNotEqual(namesA[0].Substring(0, 6), namesA[1].Substring(0, 6));
CollectionAssert.AreEqual(namesA, namesB); // deterministic across builders
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;

namespace EPPlus.Fonts.OpenType.Tests.Subsetting
Expand Down Expand Up @@ -170,5 +171,60 @@ public void Build_EmbedWholeDecision_EmbedsWholeFontNotSubset()
Assert.IsFalse(provider.PrimaryFont.IsSubset,
"An EmbedWhole font must be embedded whole, not subsetted.");
}

private static readonly Regex TaggedName = new Regex("^[A-Z]{6}\\+.+$");

[TestMethod]
public void Build_EmbedWholeFont_HasNoSubsetTag()
{
var engine = CreateEngineWithCallback(info => FontEmbeddingDecision.EmbedWhole);
RequireFont(engine, "Roboto", FontSubFamily.Regular);
var original = engine.LoadFont("Roboto");

var builder = new DocumentFontSubsetBuilder(engine);
builder.AddText("Roboto", FontSubFamily.Regular, "ABC");
builder.Build();

var embedded = builder.GetFontsToEmbed().Single();

Assert.IsFalse(TaggedName.IsMatch(embedded.Font.FullName), embedded.Font.FullName);
Assert.AreEqual(original.FullName, embedded.Font.FullName);
Assert.IsFalse(embedded.Font.IsSubset);
}

[TestMethod]
public void Build_SubsetDecision_HasSubsetTag()
{
var engine = CreateEngineWithCallback(info => FontEmbeddingDecision.Subset);
RequireFont(engine, "Roboto", FontSubFamily.Regular);

var builder = new DocumentFontSubsetBuilder(engine);
builder.AddText("Roboto", FontSubFamily.Regular, "ABC");
builder.Build();

var embedded = builder.GetFontsToEmbed().Single();

Assert.IsTrue(embedded.Font.IsSubset);
Assert.IsTrue(TaggedName.IsMatch(embedded.Font.FullName), embedded.Font.FullName);
}

[TestMethod]
public void Build_SkippedFont_FallsBackToTaggedLastResortSubset()
{
// Skip for everything: the request is served by the built-in last-resort font,
// which is seeded as Subset and must therefore be tagged too.
var engine = CreateEngineWithCallback(info => FontEmbeddingDecision.Skip);
RequireFont(engine, "Roboto", FontSubFamily.Regular);

var builder = new DocumentFontSubsetBuilder(engine);
builder.AddText("Roboto", FontSubFamily.Regular, "ABC");
builder.Build();

var embedded = builder.GetFontsToEmbed().Single();

Assert.IsTrue(TaggedName.IsMatch(embedded.Font.FullName), embedded.Font.FullName);
Assert.IsTrue(embedded.Font.FullName.Contains("Archivo"), embedded.Font.FullName);
Assert.IsNotNull(builder.GetShapingProvider("Roboto", FontSubFamily.Regular));
}
}
}
122 changes: 122 additions & 0 deletions src/EPPlus.Fonts.OpenType.Tests/Subsetting/SubsetTagTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
using EPPlus.Fonts.OpenType;
using EPPlus.Fonts.OpenType.Integration;
using EPPlus.Fonts.OpenType.Subsetting;
using EPPlus.Fonts.OpenType.Tables.Name;
using EPPlus.Fonts.OpenType.Tests;
using OfficeOpenXml.Interfaces.Fonts;
using System.Text.RegularExpressions;

[TestClass]
public class SubsetTagTests : FontTestBase
{
public override TestContext? TestContext { get; set; }

private static NameTable CreateNameTable()
{
Func<ushort, ushort, ushort, NameRecordTypes, string, NameRecord> rec =
(platform, encoding, lang, type, text) => new NameRecord
{
platformId = platform,
encodingId = encoding,
languageID = lang,
nameId = (ushort)type,
RecordType = type,
Name = text
};

return new NameTable
{
NameRecords = new[]
{
rec(3, 1, 0x0409, NameRecordTypes.FontFamilyName, "Calibri"),
rec(3, 1, 0x0409, NameRecordTypes.FontSubfamilyName, "Bold"),
rec(3, 1, 0x0409, NameRecordTypes.FullFontName, "Calibri Bold"),
rec(3, 1, 0x0409, NameRecordTypes.PostScriptName, "Calibri-Bold"),
rec(1, 0, 0, NameRecordTypes.FullFontName, "Calibri Bold"),
rec(1, 0, 0, NameRecordTypes.PostScriptName, "Calibri-Bold")
}
};
}

[TestMethod]
public void ApplySubsetTag_PrefixesFullNameAndPostScriptName_OnAllPlatforms()
{
var nt = CreateNameTable();
nt.ApplySubsetTag("ABCDEF+");

foreach (var r in nt.NameRecords)
{
if (r.RecordType == NameRecordTypes.FullFontName || r.RecordType == NameRecordTypes.PostScriptName)
Assert.IsTrue(r.Name.StartsWith("ABCDEF+"), r.Name);
}
Assert.AreEqual("ABCDEF+Calibri-Bold", nt.PostScriptName);
Assert.AreEqual("ABCDEF+Calibri Bold", nt.GetFullFontName());
}

[TestMethod]
public void ApplySubsetTag_DoesNotTouchFamilyOrSubfamily()
{
var nt = CreateNameTable();
nt.ApplySubsetTag("ABCDEF+");

Assert.AreEqual("Calibri", nt.NameRecords[0].Name);
Assert.AreEqual("Bold", nt.NameRecords[1].Name);
}

[TestMethod]
public void ApplySubsetTag_IsIdempotent()
{
var nt = CreateNameTable();
nt.ApplySubsetTag("ABCDEF+");
nt.ApplySubsetTag("ABCDEF+");
Assert.AreEqual("ABCDEF+Calibri-Bold", nt.PostScriptName);
}

[TestMethod]
public void Create_IsDeterministic_SixUppercaseLettersAndPlus()
{
var id = new FontKey("Calibri", FontSubFamily.Bold);
var a = SubsetTag.Create(id, new HashSet<int> { 65, 66, 67 });
var b = SubsetTag.Create(id, new HashSet<int> { 67, 66, 65 });
Assert.AreEqual(a, b);
Assert.IsTrue(System.Text.RegularExpressions.Regex.IsMatch(a, "^[A-Z]{6}\\+$"));
}

[TestMethod]
public void Create_DiffersForDifferentCodePointsAndFonts()
{
var bold = new FontKey("Calibri", FontSubFamily.Bold);
var regular = new FontKey("Calibri", FontSubFamily.Regular);
var cps = new HashSet<int> { 65, 66 };
Assert.AreNotEqual(SubsetTag.Create(bold, cps), SubsetTag.Create(bold, new HashSet<int> { 65 }));
Assert.AreNotEqual(SubsetTag.Create(bold, cps), SubsetTag.Create(regular, cps));
}

[TestMethod]
public void Subset_WithTag_SurvivesSerializeRoundtrip()
{
RequireFont(SystemFontsEngine, "Calibri", FontSubFamily.Regular);
var font = SystemFontsEngine.LoadFont("Calibri");
var subset = new SingleFontSubsetter().Subset(font, new HashSet<int> { 65, 66, 67 }, true);

Assert.IsTrue(subset.IsSubset);
Assert.IsTrue(Regex.IsMatch(subset.FullName, "^[A-Z]{6}\\+"), subset.FullName);

var reloaded = new OpenTypeFont(subset.Serialize());
Assert.AreEqual(subset.FullName, reloaded.FullName);
Assert.AreEqual(subset.NameTable.PostScriptName, reloaded.NameTable.PostScriptName);
}

[TestMethod]
public void Subset_WithTag_DoesNotMutateOriginalFont()
{
RequireFont(SystemFontsEngine, "Calibri", FontSubFamily.Regular);
var font = SystemFontsEngine.LoadFont("Calibri");
var before = font.FullName;

new SingleFontSubsetter().Subset(font, new HashSet<int> { 65 }, true);

Assert.AreEqual(before, font.FullName);
Assert.IsFalse(Regex.IsMatch(font.FullName, "^[A-Z]{6}\\+"));
}
}
18 changes: 14 additions & 4 deletions src/EPPlus.Fonts.OpenType/OpenTypeFont.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ Date Author Change
10/07/2025 EPPlus Software AB EPPlus.Fonts.OpenType 1.0
*************************************************************************************************/
using EPPlus.Fonts.OpenType.FontValidation;
using EPPlus.Fonts.OpenType.Integration;
using EPPlus.Fonts.OpenType.Scanner;
using EPPlus.Fonts.OpenType.Subsetting;
using EPPlus.Fonts.OpenType.Tables;
Expand Down Expand Up @@ -645,6 +646,11 @@ internal void AddOrReplaceTable<T>(T table)
}

public OpenTypeFont CreateSubset(IEnumerable<char> usedChars)
{
return CreateSubset(usedChars, false);
}

internal OpenTypeFont CreateSubset(IEnumerable<char> usedChars, bool addSubsetTag)
{
// Validate input
if (usedChars == null)
Expand All @@ -654,14 +660,18 @@ public OpenTypeFont CreateSubset(IEnumerable<char> usedChars)
throw new ArgumentException("Text cannot be empty", nameof(usedChars));

var subsetBuilder = new SubsetFontBuilder();

// Extract Unicode code points, correctly handling surrogate pairs.
// A string like "Hello 😀" contains 7 chars but 6 code points,
// because 😀 (U+1F600) is encoded as two UTF-16 surrogates.
var codePoints = CodePointUtil.ExtractCodePoints(usedChars);

var newFont = subsetBuilder.CreateSubset(this, codePoints);

if (addSubsetTag)
{
// Must happen BEFORE post-processing so lengths, checksums and any
// preprocessed table bytes are computed from the final name table.
var identity = new FontKey(GetEnglishFontFamilyName(), NameTable.GetSubfamilyEnum());
newFont.NameTable.ApplySubsetTag(SubsetTag.Create(identity, codePoints));
}

var postProcessor = new SubsetPostProcessor();
postProcessor.PostProcessSubset(newFont);

Expand Down
Loading
Loading