Skip to content

Commit bb6dd0a

Browse files
committed
Track the scope of @font-face families in PDF as a gap
1 parent 7ac1a75 commit bb6dd0a

2 files changed

Lines changed: 89 additions & 1 deletion

File tree

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
using PdfSharp;
2+
using PdfSharp.Pdf;
3+
using PdfSharp.Pdf.Advanced;
4+
using TheArtOfDev.HtmlRenderer.PdfSharp;
5+
using TheArtOfDev.HtmlRenderer.PdfSharp.FontResolution;
6+
7+
namespace HtmlRenderer.PdfSharp.Test;
8+
9+
// Parallel full layout passes race on the adapter singletons.
10+
[TestClass]
11+
[DoNotParallelize]
12+
public sealed class FontFaceFamilyTests
13+
{
14+
[TestMethod]
15+
[Spec("css-fonts-4", "#font-family-desc")]
16+
[Ignore("Gap, see Spec/css-fonts-4.yml")]
17+
public void FontFaceNamedLikeASystemFamily_HidesTheSystemFaces()
18+
{
19+
var resolver = new FontResolver();
20+
var family = SystemFamilyWithABoldFace(resolver);
21+
22+
using (var tuffy = Fallback("Tuffy.ttf"))
23+
resolver.AddFont(tuffy, family, 400, false, 100, null);
24+
25+
var regular = resolver.ResolveTypeface(family, 400, false);
26+
var bold = resolver.ResolveTypeface(family, 700, false);
27+
28+
Assert.AreEqual(regular.FaceName, bold.FaceName);
29+
Assert.IsTrue(bold.MustSimulateBold);
30+
}
31+
32+
[TestMethod]
33+
[Spec("css-fonts-4", "#font-family-desc")]
34+
[Ignore("Gap, see Spec/css-fonts-4.yml")]
35+
public async Task FontFace_DoesntApplyToALaterDocumentWithoutIt()
36+
{
37+
var family = "Scoped" + Guid.NewGuid().ToString("N");
38+
var text = $"<p style=\"font-family: '{family}'\">a</p>";
39+
40+
string fontFace;
41+
using (var tuffy = Fallback("Tuffy Bold Italic.ttf"))
42+
using (var bytes = new MemoryStream())
43+
{
44+
tuffy.CopyTo(bytes);
45+
fontFace = $"<style>@font-face {{ font-family: '{family}'; src: url(data:font/ttf;base64,{Convert.ToBase64String(bytes.ToArray())}) }}</style>";
46+
}
47+
48+
using var withFontFace = await PdfGenerator.GeneratePdf($"<html><head>{fontFace}</head><body>{text}</body></html>", PageSize.A4);
49+
using var without = await PdfGenerator.GeneratePdf($"<html><body>{text}</body></html>", PageSize.A4);
50+
51+
Assert.IsTrue(FontNames(withFontFace).Any(IsTuffyBoldItalic));
52+
Assert.IsFalse(FontNames(without).Any(IsTuffyBoldItalic));
53+
}
54+
55+
private static bool IsTuffyBoldItalic(string baseFont) => baseFont.EndsWith("+Tuffy,BoldItalic", StringComparison.Ordinal);
56+
57+
private static string SystemFamilyWithABoldFace(FontResolver resolver)
58+
{
59+
foreach (var family in resolver.DiscoverFontFamilies())
60+
{
61+
var regular = resolver.ResolveTypeface(family, 400, false);
62+
var bold = resolver.ResolveTypeface(family, 700, false);
63+
if (regular != null && bold != null && !bold.MustSimulateBold && bold.FaceName != regular.FaceName)
64+
return family;
65+
}
66+
67+
Assert.Inconclusive("No system font family with a bold face");
68+
return string.Empty;
69+
}
70+
71+
private static Stream Fallback(string fileName)
72+
{
73+
var assembly = typeof(PdfGenerator).Assembly;
74+
var name = assembly.GetManifestResourceNames().Single(n => n.EndsWith("." + fileName, StringComparison.Ordinal));
75+
return assembly.GetManifestResourceStream(name)!;
76+
}
77+
78+
private static List<string> FontNames(PdfDocument document) =>
79+
document.Pages.Cast<PdfPage>()
80+
.Select(p => p.Resources.Elements.GetDictionary("/Font"))
81+
.Where(fonts => fonts != null)
82+
.SelectMany(fonts => fonts!.Elements.Values.Select(v => v is PdfReference r ? r.Value : v).OfType<PdfDictionary>())
83+
.Select(font => font.Elements.GetName("/BaseFont"))
84+
.ToList();
85+
}

‎Spec/css-fonts-4.yml‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,10 @@ sections:
115115
status: supported
116116
'#font-family-desc':
117117
title: '4.2 Font family: the font-family descriptor'
118-
status: supported
118+
status: partial
119+
gaps:
120+
- 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
121+
- In PDF, a face stays registered for every later document in the process, also for documents whose style sheets don't declare it
119122
'#src-desc':
120123
title: '4.3 Font reference: the src descriptor'
121124
status: partial

0 commit comments

Comments
 (0)