PdfSharpCore icon indicating copy to clipboard operation
PdfSharpCore copied to clipboard

Font resolving problem

Open mishun opened this issue 5 years ago • 5 comments

If we look at how font styles are resolved: https://github.com/ststeiger/PdfSharpCore/blob/543ab752370b3281f040c32f27489f9ab98f280b/PdfSharpCore/Utils/FontResolver.cs#L171-L195 then there's a problem here: "segoeuib" (Segoe UI Bold) actually ends with "ib" (and there's obviously bunch of similar problems). And should italic check for "ib" at all?

mishun avatar Feb 28 '21 05:02 mishun

I tried to use ttf file content to resolve XFontStyle instead of filename and it seems to work fine: #149 Were there any deep reasons to use filename?

mishun avatar Mar 02 '21 23:03 mishun

It never made sense. But people sent pull requests, and added it, since they needed it. In doing so, I've broken font resolution multiple times. Every time you change something there, you break something. Font resolving is terribly broken in .NET Core. There's not a single good library for it. Best way would be to add libfontconfig for Linux, read the registry on Windows, god knows what on Mac (i don't want to add a litany of xamarin.* references), and whatever works on iOS and Android... But then, you depend on native libraries, and if they are not installed on iOS/Android/Mac ... Implementing fontconfig is also out of the question (no time) - the entire thing is utterly undocumented, and seems overly complicated. Sufficive to say, font files can be pretty much anywhere, and if you want to be on the safe side, you might even need to emed them (if the license allows that).

I think one way would be to do it like the NodeJS font-manager

  • Mac OS X 10.5 and later supported via CoreText
  • Windows 7 and later supported via DirectWrite
  • Linux supported via fontconfig

ststeiger avatar Mar 03 '21 12:03 ststeiger

I tried to also look skia's source up and that's more or less exactly what it does. Although there would be a problem with backward compatibility with approach like that: there are, for example, 4 font files with family specified as "Segoe UI", 3 files with "Segoe UI Semilight", 3 with "Segoe UI Semibold", and so on (18 files in total). System loader squashes all of that into a single family named "Segoe UI" with bunch of different font weights (beyond usual {Regular, Bold}). Official MigraDoc examples actively rely on font loader not doing that: https://github.com/empira/MigraDoc-samples/blob/ee781f44a16dc3864a689ef48f761449fe93e1d9/samples/core/Invoice/InvoiceForm.cs#L86-L94 (and if that's what official examples are doing then some users probably rely on that too). Some clever font family search that system libraries don't support "out of box" would be needed, I suppose?

mishun avatar Mar 05 '21 02:03 mishun

One could use FontConfigSharp from https://github.com/CallumDev/FontConfigSharp. If one adds the windows-dll, it works on Windows, too - leaving OSX as the only problem child.

ststeiger avatar Mar 05 '21 14:03 ststeiger

FontConfigSharp can't be used as a library as-is, but I tried to P/invoke libfontconfig directly using it as reference and it seems to work pretty well: #151

mishun avatar Mar 11 '21 22:03 mishun