IFontFileLoader.InstallFonts

1
Chris Smith
10/5/2021 11:10 PM

Hi Wout,

I'm just updating our Cadlib references to use the latest version and I got a compile error about missing the method InstallFonts on our IFontFileLoader implementation. I'm not sure what "installing" the fonts should entail, are you able to provide some detail or a sample implementation for that method?

Our current OpenFontFileStream implementation is just to check a bunch of font paths:

C# Code:
            public bool OpenFontFileStream(string fontFilename, out Stream stream)
            {
                foreach (var fontDir in fontPaths)
                {
                    var fontFullPath = Path.Combine(fontDir, fontFilename);
                    if (File.Exists(fontFullPath))
                    {
                        stream = new FileStream(fontFullPath, FileMode.Open);
                        return true;
                    }
                }

                this.logging?.WmsWmts.Warn($"Could not find font {fontFilename}");
                stream = null;
                return false;
            }

Cheers,
Chris

Wout
10/12/2021 9:06 AM

Hi,

I would recommend using one of CadLib's IFontFileLoader implementations if you can, there are FontFromDirectoryFileLoader, FontFromAbsoluteFilenameFileLoader, FontFromAssemblyResourceLoader and FontFromCurrentDirectoryFileLoader.
This is from CadLib's FontFromDirectoryFileLoader implementation:

C# Code:
        /// <inheritdoc/>
        public IList<string> InstallFonts(FontCollection fonts) {
            List<string> installedFonts = new List<string>();
            if (System.IO.Directory.Exists(directory)) {
                string[] files = System.IO.Directory.GetFiles(directory, "*.ttf", searchOption);
                foreach (string file in files) {
                    try {
#if NETCORE
                        fonts.Install(file);
#else
                        ((PrivateFontCollection)fonts).AddFontFile(file);
#endif
                        installedFonts.Add(file);
                    } catch (Exception ex) {
#if DEBUG
                    Console.WriteLine("Error installing font " + file + ". Exception: " + ex.Message);
#endif
                    }
                }
            }
            return installedFonts;
        }

- Wout

Chris Smith
10/12/2021 9:16 AM

Thanks Wout, I’ll give the FontFromDirectory one a go

Thanks for the example also!

1