diff --git a/Source/Test/HtmlRenderer.PdfSharp.Test/FontFaceFamilyTests.cs b/Source/Test/HtmlRenderer.PdfSharp.Test/FontFaceFamilyTests.cs new file mode 100644 index 000000000..0f4dbcbcb --- /dev/null +++ b/Source/Test/HtmlRenderer.PdfSharp.Test/FontFaceFamilyTests.cs @@ -0,0 +1,85 @@ +using PdfSharp; +using PdfSharp.Pdf; +using PdfSharp.Pdf.Advanced; +using TheArtOfDev.HtmlRenderer.PdfSharp; +using TheArtOfDev.HtmlRenderer.PdfSharp.FontResolution; + +namespace HtmlRenderer.PdfSharp.Test; + +// Parallel full layout passes race on the adapter singletons. +[TestClass] +[DoNotParallelize] +public sealed class FontFaceFamilyTests +{ + [TestMethod] + [Spec("css-fonts-4", "#font-family-desc")] + [Ignore("Gap, see Spec/css-fonts-4.yml")] + public void FontFaceNamedLikeASystemFamily_HidesTheSystemFaces() + { + var resolver = new FontResolver(); + var family = SystemFamilyWithABoldFace(resolver); + + using (var tuffy = Fallback("Tuffy.ttf")) + resolver.AddFont(tuffy, family, 400, false, 100, null); + + var regular = resolver.ResolveTypeface(family, 400, false); + var bold = resolver.ResolveTypeface(family, 700, false); + + Assert.AreEqual(regular.FaceName, bold.FaceName); + Assert.IsTrue(bold.MustSimulateBold); + } + + [TestMethod] + [Spec("css-fonts-4", "#font-family-desc")] + [Ignore("Gap, see Spec/css-fonts-4.yml")] + public async Task FontFace_DoesntApplyToALaterDocumentWithoutIt() + { + var family = "Scoped" + Guid.NewGuid().ToString("N"); + var text = $"

a

"; + + string fontFace; + using (var tuffy = Fallback("Tuffy Bold Italic.ttf")) + using (var bytes = new MemoryStream()) + { + tuffy.CopyTo(bytes); + fontFace = $""; + } + + using var withFontFace = await PdfGenerator.GeneratePdf($"{fontFace}{text}", PageSize.A4); + using var without = await PdfGenerator.GeneratePdf($"{text}", PageSize.A4); + + Assert.IsTrue(FontNames(withFontFace).Any(IsTuffyBoldItalic)); + Assert.IsFalse(FontNames(without).Any(IsTuffyBoldItalic)); + } + + private static bool IsTuffyBoldItalic(string baseFont) => baseFont.EndsWith("+Tuffy,BoldItalic", StringComparison.Ordinal); + + private static string SystemFamilyWithABoldFace(FontResolver resolver) + { + foreach (var family in resolver.DiscoverFontFamilies()) + { + var regular = resolver.ResolveTypeface(family, 400, false); + var bold = resolver.ResolveTypeface(family, 700, false); + if (regular != null && bold != null && !bold.MustSimulateBold && bold.FaceName != regular.FaceName) + return family; + } + + Assert.Inconclusive("No system font family with a bold face"); + return string.Empty; + } + + private static Stream Fallback(string fileName) + { + var assembly = typeof(PdfGenerator).Assembly; + var name = assembly.GetManifestResourceNames().Single(n => n.EndsWith("." + fileName, StringComparison.Ordinal)); + return assembly.GetManifestResourceStream(name)!; + } + + private static List FontNames(PdfDocument document) => + document.Pages.Cast() + .Select(p => p.Resources.Elements.GetDictionary("/Font")) + .Where(fonts => fonts != null) + .SelectMany(fonts => fonts!.Elements.Values.Select(v => v is PdfReference r ? r.Value : v).OfType()) + .Select(font => font.Elements.GetName("/BaseFont")) + .ToList(); +} diff --git a/Spec/css-fonts-4.yml b/Spec/css-fonts-4.yml index b58b62478..a981f8ed0 100644 --- a/Spec/css-fonts-4.yml +++ b/Spec/css-fonts-4.yml @@ -115,7 +115,10 @@ sections: status: supported '#font-family-desc': title: '4.2 Font family: the font-family descriptor' - status: supported + status: partial + gaps: + - In PDF, a face named like a system family only replaces the system face of the same weight, style and stretch, the other system faces stay matchable + - In PDF, a face stays registered for every later document in the process, also for documents whose style sheets don't declare it '#src-desc': title: '4.3 Font reference: the src descriptor' status: partial