示例#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);
     }
 }
        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);
            }
        }