Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

case insensitive string comparison for pdf saving and font embedding … #610

Merged
merged 1 commit into from
Jan 30, 2025
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
69 changes: 67 additions & 2 deletions ZUGFeRD.PDF.Test/SaveTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,12 @@
*/
using System;
using System.Formats.Asn1;
using System.Reflection.Metadata;
using PdfSharp.Pdf;
using PdfSharp.Pdf.Advanced;
using PdfSharp.Pdf.IO;
using PdfSharp.Pdf.Structure;
using PdfSharp.Quality;
using s2industries.ZUGFeRD;
using s2industries.ZUGFeRD.PDF;
using static System.Runtime.InteropServices.JavaScript.JSType;
Expand Down Expand Up @@ -55,8 +61,67 @@ public async Task BasicSaveFileAndEmbedFonts()
InvoiceDescriptor descriptor = new InvoiceProvider().CreateInvoice();
await InvoicePdfProcessor.SaveToPdfAsync(targetPath, ZUGFeRDVersion.Version23, Profile.Comfort, ZUGFeRDFormats.CII, sourcePath, descriptor);

/* how to test? */
} // !BasicSaveExampleFile()
// very basic test
string result = System.IO.File.ReadAllText(targetPath);
Assert.IsTrue(result.Contains("/FontFile") || result.Contains("/FontFile2") || result.Contains("/FontFile3"));

// load fonts using PDFsharp
// every font element should appear twice
Dictionary<string, int> fontAppeareanceCount = new Dictionary<string, int>();
Dictionary<string, bool> fontFileOccurs = new Dictionary<string, bool>();
PdfDocument document = PdfReader.Open(targetPath);
Assert.IsTrue(document.Pages.Count > 0);

var resources = document.Pages[0].Elements.GetDictionary("/Resources");
Assert.IsNotNull(resources);

var fonts = resources.Elements.GetDictionary("/Font");
Assert.IsNotNull(fonts);

foreach (var value in fonts.Elements.Values)
{
var reference = (PdfReference)value;
var font = document.Internals.GetObject(new PdfObjectID(reference.ObjectNumber)) as PdfDictionary;


var fontDescriptor = font.Elements.GetDictionary("/FontDescriptor");
Assert.IsNotNull(fontDescriptor);

string fontName = fontDescriptor.Elements.GetString("/FontName");
fontName = fontName.Substring(1);
if (fontName.Contains("+"))
{
fontName = fontName.Substring(fontName.IndexOf("+") + 1);
}

if (!fontAppeareanceCount.ContainsKey(fontName))
{
fontAppeareanceCount.Add(fontName, 1);
fontFileOccurs.Add(fontName, false);
}
else
{
fontAppeareanceCount[fontName]++;
}

if (fontDescriptor.Elements.ContainsKey("/FontFile2"))
{
PdfReference fontFileRef = fontDescriptor.Elements["/FontFile2"] as PdfReference;
Assert.IsNotNull(fontFileRef);

int fontObjectNumber = fontFileRef.ObjectNumber;
PdfObject fontFileObj = document.Internals.GetObject(new PdfObjectID(fontObjectNumber));

Assert.IsInstanceOfType(fontFileObj, typeof(PdfDictionary));
fontFileOccurs[fontName] = true;
}
}

Assert.AreEqual(fontFileOccurs.Count, fontAppeareanceCount.Count);
Assert.IsTrue(fontFileOccurs.Values.All(x => x));
Assert.IsTrue(fontAppeareanceCount.Values.All(x => x == 2));
} // !BasicSaveFileAndEmbedFonts()


[TestMethod]
public async Task BasicSaveExampleAsStream()
Expand Down
1 change: 1 addition & 0 deletions ZUGFeRD.PDF.Test/ZUGFeRD.PDF.Test.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.12.0" />
<PackageReference Include="MSTest" Version="3.6.3" />
<PackageReference Include="PDFsharp-extended" Version="1.2.0-preview-1" />
</ItemGroup>

<ItemGroup>
Expand Down
16 changes: 13 additions & 3 deletions ZUGFeRD.PDF/InvoiceDescriptorPdfSaver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -378,6 +378,7 @@ private static List<XFont> _LoadFonts(PdfDocument pdfDocument)
return retval;
} // !_LoadFonts()


private static void _ExtractFontValues(string baseFont, PdfDictionary fontDict, out string fontFamily, out XFontStyleEx fontStyle)
{
fontStyle = XFontStyleEx.Regular;
Expand All @@ -389,8 +390,16 @@ private static void _ExtractFontValues(string baseFont, PdfDictionary fontDict,
if (fontParts.Length > 1)
{
string stylePart = fontParts[1].ToLower();
if (stylePart.Contains("bold")) fontStyle |= XFontStyleEx.Bold;
if (stylePart.Contains("italic") || stylePart.Contains("oblique")) fontStyle |= XFontStyleEx.Italic;
if (stylePart.IndexOf("bold", StringComparison.OrdinalIgnoreCase) > -1)
{
fontStyle |= XFontStyleEx.Bold;
}

if ((stylePart.IndexOf("italic", StringComparison.OrdinalIgnoreCase) > -1) ||
(stylePart.IndexOf("oblique", StringComparison.OrdinalIgnoreCase) > -1))
{
fontStyle |= XFontStyleEx.Italic;
}
}

// Check FontDescriptor flags (if available)
Expand All @@ -404,7 +413,8 @@ private static void _ExtractFontValues(string baseFont, PdfDictionary fontDict,
if ((flags & 0x10) != 0) fontStyle |= XFontStyleEx.Italic; // Italic flag
}
}
}
} // !_ExtractFontValues()


private static string _DetermineFilenameBasedOnVersionAndProfile(ZUGFeRDVersion version, Profile profile)
{
Expand Down
Loading