Beispiel #1
0
        /// <summary>
        /// Note: There is some performance overhead to initializing this.
        /// </summary>
        private void InitializeFontData()
        {
            FontNameToFiles = new Dictionary <string, FontGroup>();
            if (NoteFontsWeCantInstall)
            {
                FontsWeCantInstall = new HashSet <string>();
            }
#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)
                            {
                                if (NoteFontsWeCantInstall)
                                {
                                    FontsWeCantInstall.Add(face.FamilyName);
                                }
                                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 FindWindowsFonts())
            {
                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:
                    if (NoteFontsWeCantInstall)
                    {
                        string name1 = GetFontNameFromFile(fontFile);
                        if (name1 != null)
                        {
                            FontsWeCantInstall.Add(name1);
                        }
                    }
                    continue;                             // not allowed to embed
                }

                string name = GetFontNameFromFile(fontFile);
                if (name == null)
                {
                    continue;                     // not sure how this can happen but I've seen it.
                }
                // 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
        }
Beispiel #2
0
        /// <summary>
        /// Note: There is some performance overhead to initializing this.
        /// </summary>
        private void InitializeFontData()
        {
            FontNameToFiles    = new Dictionary <string, FontGroup>();
            FontsWeCantInstall = new HashSet <string>();
#if __MonoCS__
            using (var lib = new SharpFont.Library())
            {
                // Find all the font files in the standard system location (/usr/share/font) and $HOME/.local/share/fonts (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)
                            {
                                // Our font UI allows any font on the computer, but gives the user indications that some are more
                                // useable in publishing Bloom books. The NoteFontsWeCantInstall prop is only true when we call this
                                // from BloomPubMaker so that it can note that certain fonts are unsuitable for embedding in ePUBs.
                                if (NoteFontsWeCantInstall)
                                {
                                    FontsWeCantInstall.Add(face.FamilyName);
                                    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)
                    {
                        continue;
                    }
                }
            }
#else
            foreach (var fontFile in FindWindowsFonts())
            {
                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.
                }

                // Our font UI allows any font on the computer, but gives the user indications that some are more
                // useable in publishing Bloom books. The NoteFontsWeCantInstall prop is only true when we call this
                // from BloomPubMaker so that it can note that certain fonts are unsuitable for embedding in ePUBs.
                if (!FontIsEmbeddable(gtf.EmbeddingRights) && NoteFontsWeCantInstall)
                {
                    string name1 = GetFontNameFromFile(fontFile);
                    if (name1 != null)
                    {
                        FontsWeCantInstall.Add(name1);
                    }
                    continue;                     // not allowed to embed in ePUB
                }

                string name = GetFontNameFromFile(fontFile);
                if (name == null)
                {
                    continue;                     // not sure how this can happen but I've seen it.
                }
                // 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
        }
Beispiel #3
0
        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);
        }
        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;
        }