PDFsharp & MigraDoc Foundation

PDFsharp - A .NET library for processing PDF & MigraDoc Foundation - Creating documents on the fly
It is currently Tue Mar 19, 2024 3:57 am

All times are UTC


Forum rules


Please read this before posting on this forum: Forum Rules



Post new topic Reply to topic  [ 4 posts ] 
Author Message
PostPosted: Thu Mar 29, 2018 5:04 pm 
Offline

Joined: Thu Feb 08, 2018 9:07 am
Posts: 7
Hello, i notice that the Font method 'Exists' is marked as obsolete and always returns false, is there an alternative?

I am looking for a way to confirm if a requested system font is available, our solution can be deployed under Windows or different variations of Linux environments and the character support (Cyrillic, Chinese etc) varies from user to user.

The intention is to just used the fonts available on the host environment (specified by user settings) but it would be useful to log an error at time of report generation if the requested font is not available. It looked like the 'Exists(String)' method would cover this but it is now obsolete, is there an easy way to verify if the requested font exists?

Many thanks
Gary


Top
 Profile  
Reply with quote  
PostPosted: Thu Mar 29, 2018 5:46 pm 
Offline
PDFsharp Expert
User avatar

Joined: Sat Mar 14, 2015 10:15 am
Posts: 905
Location: CCAA
Hi!
garycuthbert wrote:
is there an alternative?
If we knew a simple solution then it would be included in MigraDoc.
If you find a good solution please let us know.

The old implementation is still there, but commented out:
Code:
[Obsolete("This function is removed from DocumentObjectModel and always returns false.")]
public static bool Exists(string fontName)
{
    //System.Drawing.FontFamily[] families = System.Drawing.FontFamily.Families;
    //foreach (System.Drawing.FontFamily family in families)
    //{
    //  if (String.Compare(family.Name, fontName, true) == 0)
    //    return true;
    //}
    return false;
}
I don't know how good this old implementation works.
It may work better for the GDI build than the WPF build.

_________________
Best regards
Thomas
(Freelance Software Developer with several years of MigraDoc/PDFsharp experience)


Top
 Profile  
Reply with quote  
PostPosted: Fri Mar 30, 2018 8:29 am 
Offline

Joined: Thu Feb 08, 2018 9:07 am
Posts: 7
Thanks for the explanation. I had put together this method (which looks similar to the one commented out) which relies on the 'ArgumentException' raising behavior of the FontFamily constructor:

Code:
private static bool FontFamilyExists(string fontFamily, out string reason)
{
   reason = "";
           
   try
   {
      var testFamily = new System.Drawing.FontFamily(fontFamily);
      testFamily.Dispose();
   }
   catch (ArgumentException argException)
   {
      reason = argException.Message;
      return false;
   }

   return true;
}


It may have the same issues under WPF.
We are using the GDI build so I think it will be suitable for our deployments although i have still to test it on our Linux build.

Thanks again
Gary


Top
 Profile  
Reply with quote  
PostPosted: Fri Mar 30, 2018 11:23 am 
Offline
PDFsharp Expert
User avatar

Joined: Sat Mar 14, 2015 10:15 am
Posts: 905
Location: CCAA
I found this on SO:
https://stackoverflow.com/a/253741/1015447

They try "new Font()".

Possible problems: Not all fonts installed in Windows will work with the GDI build of PDFsharp (no support for PostScript outlines). You first must test for fonts supplied via IFontResolver (if any).

Another approach could be using PDFsharp's XFont class for a test.
I think with the WPF build this will not fail even if the font does not exist, so you must also check if the returned font is not the default (fallback) font.
And Linux will be a different story - not my area of expertise.

_________________
Best regards
Thomas
(Freelance Software Developer with several years of MigraDoc/PDFsharp experience)


Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 4 posts ] 

All times are UTC


Who is online

Users browsing this forum: Google [Bot] and 40 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Privacy Policy, Data Protection Declaration, Impressum
Powered by phpBB® Forum Software © phpBB Group