Example #1
0
        static XFontSource()
        {
            var searchingPaths = new List <string>();
            // Application
            var executingPath = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);

            searchingPaths.Add(executingPath);
            // Windows fonts
            var systemFontsPath = Environment.GetFolderPath(Environment.SpecialFolder.Fonts);

            if (!string.IsNullOrWhiteSpace(systemFontsPath))
            {
                searchingPaths.Add(systemFontsPath);
            }
            else
            {
                // Debian fonts
                searchingPaths.Add("/usr/share/fonts/truetype");
                //searchingPaths.Add("/usr/share/X11/fonts");
                //searchingPaths.Add("/usr/X11R6/lib/X11/fonts");
                //searchingPaths.Add("~/.fonts");
            }

            foreach (var searchingPath in searchingPaths)
            {
                // TODO: *.ttc not supported yet!
                var fileNames = new List <string>();
                try
                {
                    fileNames = Directory.GetFiles(searchingPath, "*", SearchOption.AllDirectories)
                                .Where(f => Path.GetExtension(f).ToLower() == ".ttf")
                                .ToList();
                }
                catch (Exception ex)
                {
                    Debug.WriteLine(ex);
                    continue;
                }

                foreach (var filePath in fileNames)
                {
                    try
                    {
                        var fontStyle = TtfHelper.GetFontStyle(filePath);
                        var key       = new XFontSourceKey(TtfHelper.GetFontFamilyName(filePath), fontStyle);
                        if (!_fontFilePaths.ContainsKey(key))
                        {
                            _fontFilePaths.Add(key, filePath);
                            Debug.WriteLine($"Added font '{key.FontFamilyName}': {filePath}");
                        }
                    }
                    catch (Exception ex)
                    {
                        Debug.WriteLine(ex);
                    }
                }
            }
        }
Example #2
0
        private static byte[] ReadFontBytesFromGdi(GdiFont gdiFont)
        {
            var fontKey = new XFontSourceKey(gdiFont.Name, gdiFont.Style);

            if (_fontFilePaths.ContainsKey(fontKey))
            {
                var filePath = _fontFilePaths[fontKey];
                Debug.WriteLine($"Retrieving font '{fontKey.ToString()}' from {filePath}");
                return(File.ReadAllBytes(filePath));
            }

            // use embedded resource font if the specified one is not found
            using (var fontStream = Assembly.GetAssembly(typeof(XFontSource)).GetManifestResourceStream($"PdfSharp.Assets.{FALLBACK_FONT}"))
            {
                Debug.WriteLine($"'{fontKey.ToString()}' not found, falling back by default to '{FALLBACK_FONT}'.");
                var fontData = new byte[fontStream.Length];
                fontStream.Read(fontData, 0, (int)fontStream.Length);
                return(fontData);
            }
        }