Beispiel #1
0
 public bool AddFontStreamSource(IFontStreamSource src)
 {
     //preview data of font
     try
     {
         using (Stream stream = src.ReadFontStream())
         {
             var             reader      = new OpenFontReader();
             PreviewFontInfo previewFont = reader.ReadPreview(stream);
             if (previewFont == null || string.IsNullOrEmpty(previewFont.Name))
             {
                 //err!
                 return(false);
             }
             if (previewFont.IsFontCollection)
             {
                 int  mbCount     = previewFont.MemberCount;
                 bool totalResult = true;
                 for (int i = 0; i < mbCount; ++i)
                 {
                     //extract and each members
                     if (!AddFontPreview(previewFont.GetMember(i), src.PathName))
                     {
                         totalResult = false;
                     }
                 }
                 return(totalResult);
             }
             else
             {
                 return(AddFontPreview(previewFont, src.PathName));
             }
         }
     }
     catch (IOException)
     {
         //TODO review here again
         return(false);
     }
 }
Beispiel #2
0
        bool AddFontPreview(PreviewFontInfo previewFont, string srcPath)
        {
            _onlyFontNames[previewFont.Name] = true;

            TypefaceStyle typefaceStyle = TypefaceStyle.Regular;

            switch (previewFont.OS2TranslatedStyle)
            {
            case OpenFont.Extensions.TranslatedOS2FontStyle.BOLD:
                typefaceStyle = TypefaceStyle.Bold;
                break;

            case OpenFont.Extensions.TranslatedOS2FontStyle.ITALIC:
            case OpenFont.Extensions.TranslatedOS2FontStyle.OBLIQUE:
                typefaceStyle = TypefaceStyle.Italic;
                break;

            case OpenFont.Extensions.TranslatedOS2FontStyle.REGULAR:
                typefaceStyle = TypefaceStyle.Regular;
                break;

            case (OpenFont.Extensions.TranslatedOS2FontStyle.BOLD | OpenFont.Extensions.TranslatedOS2FontStyle.ITALIC):
                typefaceStyle = TypefaceStyle.Bold | TypefaceStyle.Italic;
                break;
            }
            //---------------
            //some font subfam="Bold Italic" but OS2TranslatedStyle is only Italic
            //so we should check the subfam name too!
            string[] fontSubFamUpperCaseName_split = previewFont.SubFamilyName.ToUpper().Split(' ');
            if (fontSubFamUpperCaseName_split.Length > 1)
            {
                if (typefaceStyle != (TypefaceStyle.Bold | TypefaceStyle.Italic))
                {
                    //translate more
                    if ((fontSubFamUpperCaseName_split[0] == "BOLD" && fontSubFamUpperCaseName_split[1] == "ITALIC") ||
                        (fontSubFamUpperCaseName_split[0] == "ITALIC" && fontSubFamUpperCaseName_split[1] == "BOLD"))
                    {
                        typefaceStyle = TypefaceStyle.Bold | TypefaceStyle.Italic;
                    }
                }
            }
            else
            {
                //=1
                switch (fontSubFamUpperCaseName_split[0])
                {
                case "BOLD": typefaceStyle = TypefaceStyle.Bold; break;

                case "ITALIC": typefaceStyle = TypefaceStyle.Italic; break;
                }
            }
            return(Register(new InstalledTypeface(
                                previewFont.Name,
                                previewFont.SubFamilyName,
                                previewFont.TypographicFamilyName,
                                previewFont.TypographicSubFamilyName,
                                srcPath,
                                typefaceStyle,
                                previewFont.Weight)
            {
                ActualStreamOffset = previewFont.ActualStreamOffset
            }));
        }
Beispiel #3
0
        public PreviewFontInfo ReadPreview(BinaryReader reader)
        {
            //read preview only
            //WOFF File
            //WOFFHeader        File header with basic font type and version, along with offsets to metadata and private data blocks.
            //TableDirectory    Directory of font tables, indicating the original size, compressed size and location of each table within the WOFF file.
            //FontTables        The font data tables from the input sfnt font, compressed to reduce bandwidth requirements.
            //ExtendedMetadata  An optional block of extended metadata, represented in XML format and compressed for storage in the WOFF file.
            //PrivateData       An optional block of private data for the font designer, foundry, or vendor to use.

            PreviewFontInfo fontPreviewInfo = null;

            _header = ReadWOFFHeader(reader);
            if (_header == null)
            {
#if DEBUG
                System.Diagnostics.Debug.WriteLine("can't read ");
#endif
                return(null); //notify user too
            }

            //
            WoffTableDirectory[] woffTableDirs = ReadTableDirectories(reader);
            if (woffTableDirs == null)
            {
                return(null);
            }
            //try read each compressed table
            if (DecompressHandler == null)
            {
                if (WoffDefaultZlibDecompressFunc.DecompressHandler != null)
                {
                    DecompressHandler = WoffDefaultZlibDecompressFunc.DecompressHandler;
                }
                else
                {
#if DEBUG
                    System.Diagnostics.Debug.WriteLine("no Zlib DecompressHandler ");
#endif
                    return(null); //notify user too
                }
            }


            TableEntryCollection tableEntryCollection = CreateTableEntryCollection(woffTableDirs);
            //for font preview, we may not need to extract
            using (MemoryStream decompressStream = new MemoryStream())
            {
                if (Extract(reader, woffTableDirs, decompressStream))
                {
                    using (ByteOrderSwappingBinaryReader reader2 = new ByteOrderSwappingBinaryReader(decompressStream))
                    {
                        decompressStream.Position = 0;
                        OpenFontReader  openFontReader  = new OpenFontReader();
                        PreviewFontInfo previewFontInfo = openFontReader.ReadPreviewFontInfo(tableEntryCollection, reader2);
                        if (previewFontInfo != null)
                        {
                            //add webfont info to this preview font
                            previewFontInfo.IsWebFont = true;
                        }
                        return(previewFontInfo);
                    }
                }
            }

            return(fontPreviewInfo);
        }
        public bool AddFontStreamSource(IFontStreamSource src)
        {
            //preview data of font
            try
            {
                using (Stream stream = src.ReadFontStream())
                {
                    var             reader      = new OpenFontReader();
                    PreviewFontInfo previewFont = reader.ReadPreview(stream);
                    if (string.IsNullOrEmpty(previewFont.fontName))
                    {
                        //err!
                        return(false);
                    }
                    //if (previewFont.fontName.StartsWith("Bungee"))
                    //{
                    //}


                    TypefaceStyle typefaceStyle = TypefaceStyle.Regular;
                    switch (previewFont.OS2TranslatedStyle)
                    {
                    case OpenFont.Extensions.TranslatedOS2FontStyle.BOLD:
                        typefaceStyle = TypefaceStyle.Bold;
                        break;

                    case OpenFont.Extensions.TranslatedOS2FontStyle.ITALIC:
                    case OpenFont.Extensions.TranslatedOS2FontStyle.OBLIQUE:
                        typefaceStyle = TypefaceStyle.Italic;
                        break;

                    case OpenFont.Extensions.TranslatedOS2FontStyle.REGULAR:
                        typefaceStyle = TypefaceStyle.Regular;
                        break;

                    case (OpenFont.Extensions.TranslatedOS2FontStyle.BOLD | OpenFont.Extensions.TranslatedOS2FontStyle.ITALIC):
                        typefaceStyle = TypefaceStyle.Bold | TypefaceStyle.Italic;
                        break;
                    }


                    //---------------
                    //some font subfam="Bold Italic" but OS2TranslatedStyle is only Italic
                    //so we should check the subfam name too!
                    string[] fontSubFamUpperCaseName_split = previewFont.fontSubFamily.ToUpper().Split(' ');
                    if (fontSubFamUpperCaseName_split.Length > 1)
                    {
                        if (typefaceStyle != (TypefaceStyle.Bold | TypefaceStyle.Italic))
                        {
                            //translate more
                            if ((fontSubFamUpperCaseName_split[0] == "BOLD" && fontSubFamUpperCaseName_split[1] == "ITALIC") ||
                                (fontSubFamUpperCaseName_split[0] == "ITALIC" && fontSubFamUpperCaseName_split[1] == "BOLD"))
                            {
                                typefaceStyle = TypefaceStyle.Bold | TypefaceStyle.Italic;
                            }
                        }
                    }
                    else
                    {
                        //=1
                        switch (fontSubFamUpperCaseName_split[0])
                        {
                        case "BOLD": typefaceStyle = TypefaceStyle.Bold; break;

                        case "ITALIC": typefaceStyle = TypefaceStyle.Italic; break;
                        }
                    }

                    return(Register(new InstalledTypeface(previewFont.fontName, previewFont.fontSubFamily, src.PathName, typefaceStyle)));
                }
            }
            catch (IOException)
            {
                //TODO review here again
                return(false);
            }
        }
Beispiel #5
0
 internal InstalledTypeface(PreviewFontInfo previewFontInfo, string fontPath)
     : this(previewFontInfo.NameEntry,
            previewFontInfo.OS2Table,
            previewFontInfo.Languages, fontPath)
 {
 }