ststeiger / PdfSharpCore

Port of the PdfSharp library to .NET Core - largely removed GDI+ (only missing GetFontData - which can be replaced with freetype2)
Other
1.08k stars 237 forks source link

Reason for supporting only TTF fonts #327

Open sygnowskip opened 1 year ago

sygnowskip commented 1 year ago

Hello, I'm trying to use PdfSharpCore in the Docker environment with the Alpine 3.17 base image and I'm courious why only the TTF font format is supported?

There are a lot of open source fonts (e.g. Inconsolata - Alpine package) but due to the filtering clauses to the TTF extensions https://github.com/ststeiger/PdfSharpCore/blob/202d24d043d6e3f135d65197324a57d15afad134/PdfSharpCore/Utils/LinuxSystemFontResolver.cs#L135 the OTF fonts are not allowed to be used with the PdfSharpCore.

Are there any technical limitations behind this? Cross-platform support? What is the reason of supporting TTF fonts only in PdfSharpCore?

ststeiger commented 1 year ago

PDFsharp can create files with PDF versions from 1.2 (Adobe Acrobat Reader 3.0) through 1.7 (Adobe Reader 8.0).

From PDF 1.6 onwards, OpenType fonts can be stored directly in a PDF file. Thus the PDF file format supports the use of the following font formats: TrueType. & OpenType. As you can see in directory Fonts.OpenType, it has OpenType-Read-Support.

The reason is a historic one: PdfSharp called GetFontData in the Windows-API. And also CreateFontPackage is usually used to create a subset-font for font-embedding. These functions only support TrueType.

Technically, nothing should stop you from using OpenFont-Fonts, but you have to check if the font-embedding works. If you have the font installed on your local machine, you need to check that on a machine where this font is not installed.

Note that depending on the font, the PDF-size might get large - because the entire font is embeeded, not just the part of the fonts that are used in the pdf (removal of CreateFontPackage).

If your font supports Chinese or a variety of east-asian languages, the font-size alone might exceed 100 MB or more.

You can test by allowing the extensions .ttf and .otf, and look at the PDF on a machine that doesn't have the font installed. I'm not sure if font-embedding works properly with *.ttf either.

Essentially, you'd have to program a substitue for CreateFontPackage for OTF-files. That subsititue would have to create an embeddable file from the subset of characters used in the PDF created by the user for the font(s) in question. Bonus points if it converts OTF into TrueType.