PDFsharp & MigraDoc Foundation
https://forum.pdfsharp.net/

MigraDoc.DocumentObjectModel.Font.Exists alternative?
https://forum.pdfsharp.net/viewtopic.php?f=2&t=3754
Page 1 of 1

Author:  garycuthbert [ Thu Mar 29, 2018 5:04 pm ]
Post subject:  MigraDoc.DocumentObjectModel.Font.Exists alternative?

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

Author:  TH-Soft [ Thu Mar 29, 2018 5:46 pm ]
Post subject:  Re: MigraDoc.DocumentObjectModel.Font.Exists alternative?

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.

Author:  garycuthbert [ Fri Mar 30, 2018 8:29 am ]
Post subject:  Re: MigraDoc.DocumentObjectModel.Font.Exists alternative?

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

Author:  TH-Soft [ Fri Mar 30, 2018 11:23 am ]
Post subject:  Re: MigraDoc.DocumentObjectModel.Font.Exists alternative?

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.

Page 1 of 1 All times are UTC
Powered by phpBB® Forum Software © phpBB Group
https://www.phpbb.com/