This class handles the problem of finding what files contain the definition of the various faces of a particular font. So far the best approach involves a scan of the whole font directory, so it is much more efficient to build a dictionary once, or at least once per operation that uses it. The dictionary and method could be static, but then, we would miss the chance to find any new fonts added since the last operation that needed this information.
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");
 }