Skip to content
Merged
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
85 changes: 85 additions & 0 deletions Source/Test/HtmlRenderer.PdfSharp.Test/FontFaceFamilyTests.cs
Original file line number Diff line number Diff line change
@@ -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 = $"<p style=\"font-family: '{family}'\">a</p>";

string fontFace;
using (var tuffy = Fallback("Tuffy Bold Italic.ttf"))
using (var bytes = new MemoryStream())
{
tuffy.CopyTo(bytes);
fontFace = $"<style>@font-face {{ font-family: '{family}'; src: url(data:font/ttf;base64,{Convert.ToBase64String(bytes.ToArray())}) }}</style>";
}

using var withFontFace = await PdfGenerator.GeneratePdf($"<html><head>{fontFace}</head><body>{text}</body></html>", PageSize.A4);
using var without = await PdfGenerator.GeneratePdf($"<html><body>{text}</body></html>", 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<string> FontNames(PdfDocument document) =>
document.Pages.Cast<PdfPage>()
.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<PdfDictionary>())
.Select(font => font.Elements.GetName("/BaseFont"))
.ToList();
}
5 changes: 4 additions & 1 deletion Spec/css-fonts-4.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Loading