Bloom.Publish.FontFileFinder.GetGroupForFont C# (CSharp) Method

GetGroupForFont() public method

public GetGroupForFont ( string fontName ) : FontGroup
fontName string
return FontGroup
        public FontGroup GetGroupForFont(string fontName)
        {
            // Review Linux: very likely something here is not portable.
            if (FontNameToFiles == null)
            {
                FontNameToFiles = new Dictionary<string, FontGroup>();
            #if __MonoCS__
                using (var lib = new SharpFont.Library())
                {
                    // Find all the font files in the standard system location (/usr/share/font) and $HOME/.font (if it exists)
                    foreach (var fontFile in FindLinuxFonts())
                    {
                        try
                        {
                            using (var face = new SharpFont.Face(lib, fontFile))
                            {
                                var embeddingTypes = face.GetFSTypeFlags();
                                if ((embeddingTypes & EmbeddingTypes.RestrictedLicense) == EmbeddingTypes.RestrictedLicense ||
                                    (embeddingTypes & EmbeddingTypes.BitmapOnly) == EmbeddingTypes.BitmapOnly)
                                {
                                    continue;
                                }
                                var name = face.FamilyName;
                                // If you care about bold, italic, etc, you can filter here.
                                FontGroup files;
                                if (!FontNameToFiles.TryGetValue(name, out files))
                                {
                                    files = new FontGroup();
                                    FontNameToFiles[name] = files;
                                }
                                files.Add(face, fontFile);
                            }
                        }
                        catch (Exception)
                        {
                        }
                    }
                }
            #else
                foreach (var fontFile in Directory.GetFiles(Environment.GetFolderPath(Environment.SpecialFolder.Fonts)))
                {
                    // ePUB only understands these types, so skip anything else.
                    switch (Path.GetExtension(fontFile))
                    {
                        case ".ttf":
                        case ".otf":
                        case ".woff":
                            break;
                        default:
                            continue;
                    }
                    GlyphTypeface gtf;
                    try
                    {
                        gtf = new GlyphTypeface(new Uri("file:///" + fontFile));
                    }
                    catch (Exception)
                    {
                        continue; // file is somehow corrupt or not really a font file? Just ignore it.
                    }
                    switch (gtf.EmbeddingRights)
                    {
                        case FontEmbeddingRight.Editable:
                        case FontEmbeddingRight.EditableButNoSubsetting:
                        case FontEmbeddingRight.Installable:
                        case FontEmbeddingRight.InstallableButNoSubsetting:
                        case FontEmbeddingRight.PreviewAndPrint:
                        case FontEmbeddingRight.PreviewAndPrintButNoSubsetting:
                            break;
                        default:
                            continue; // not allowed to embed (enhance: warn user?)
                    }

                    var fc = new PrivateFontCollection();
                    try
                    {
                        fc.AddFontFile(fontFile);
                    }
                    catch (FileNotFoundException)
                    {
                        continue; // not sure how this can happen but I've seen it.
                    }
                    var name = fc.Families[0].Name;
                    // If you care about bold, italic, etc, you can filter here.
                    FontGroup files;
                    if (!FontNameToFiles.TryGetValue(name, out files))
                    {
                        files = new FontGroup();
                        FontNameToFiles[name] = files;
                    }
                    files.Add(gtf, fontFile);
                }
            #endif
            }
            FontGroup result;
            FontNameToFiles.TryGetValue(fontName, out result);
            return result;
        }

Usage Example

Example #1
0
 /// <summary>
 /// Try to embed the fonts we need.
 /// </summary>
 private void EmbedFonts()
 {
     var fontsWanted = GetFontsUsed(Book.FolderPath, true); // Need to include fallback fonts in case one of the preferred fonts isn't on this machine
     var fontFileFinder = new FontFileFinder();
     var filesToEmbed = fontsWanted.SelectMany(fontFileFinder.GetFilesForFont).ToArray();
     foreach (var file in filesToEmbed)
     {
         CopyFileToEpub(file);
     }
     var sb = new StringBuilder();
     foreach (var font in fontsWanted)
     {
         var group = fontFileFinder.GetGroupForFont(font);
         if (group != null)
         {
             AddFontFace(sb, font, "normal", "normal", group.Normal);
             AddFontFace(sb, font, "bold", "normal", group.Bold);
             AddFontFace(sb, font, "normal", "italic", group.Italic);
             AddFontFace(sb, font, "bold", "italic", group.BoldItalic);
         }
     }
     RobustFile.WriteAllText(Path.Combine(_contentFolder, "fonts.css"), sb.ToString());
     _manifestItems.Add("fonts.css");
 }