Пример #1
0
        public static Typeface ResolveTypefaceFromFile(this InstalledTypefaceCollection fontCollection, string filename)
        {
            //from user input typeface filename

            bool enable_absPath = false; //enable abs path or not

#if DEBUG
            enable_absPath = true;//in debug mode
#endif
            //TODO: review here again!!!
            if (!fontCollection._installedTypefacesByFilenames.TryGetValue(filename, out InstalledTypeface found))
            {
                if (!enable_absPath && Path.IsPathRooted(filename))
                {
                    return(null);//***
                }

                //search for a file
                if (File.Exists(filename))
                {
                    //TODO: handle duplicated font!!
                    //try read this
                    InstalledTypeface instTypeface;
                    using (FileStream fs = new FileStream(filename, FileMode.Open))
                    {
                        OpenFontReader reader   = new OpenFontReader();
                        Typeface       typeface = reader.Read(fs);

                        //
                        OpenFont.Extensions.TypefaceExtensions.SetCustomTypefaceKey(
                            typeface,
                            TinyCRC32Calculator.CalculateCrc32(typeface.Name.ToUpper()));

                        instTypeface = new InstalledTypeface(typeface, filename);
                        fontCollection._installedTypefacesByFilenames.Add(filename, instTypeface);

                        return(instTypeface.ResolvedTypeface = typeface);//assign  and return
                    }
                }
            }
            else
            {
                //found inst type
                return(ResolveTypeface(fontCollection, found));
            }
            return(null);
        }
Пример #2
0
        public static Typeface ResolveTypeface(this InstalledTypefaceCollection fontCollection, InstalledTypeface installedFont)
        {
            if (installedFont.ResolvedTypeface != null)
            {
                return(installedFont.ResolvedTypeface);
            }

            //load
            Typeface typeface;

            if (CustomFontStreamLoader != null)
            {
                using (var fontStream = CustomFontStreamLoader(installedFont.FontPath))
                {
                    var reader = new OpenFontReader();
                    typeface          = reader.Read(fontStream, installedFont.ActualStreamOffset);
                    typeface.Filename = installedFont.FontPath;
                    installedFont.ResolvedTypeface = typeface;//cache
                }
            }
            else
            {
                using (var fs = new FileStream(installedFont.FontPath, FileMode.Open, FileAccess.Read))
                {
                    var reader = new OpenFontReader();
                    typeface          = reader.Read(fs, installedFont.ActualStreamOffset);
                    typeface.Filename = installedFont.FontPath;
                    installedFont.ResolvedTypeface = typeface;//cache
                }
            }

            //calculate typeface key for the new create typeface
            //custom key

            OpenFont.Extensions.TypefaceExtensions.SetCustomTypefaceKey(
                typeface,
                TinyCRC32Calculator.CalculateCrc32(typeface.Name.ToUpper()));

            return(typeface);
        }