示例#1
0
        public void RegisterTTF()
        {
            var ttf = Path.GetFullPath("Pacifico.ttf");

            if (!File.Exists(ttf))
            {
                Assert.Ignore("Could not find the font file {0}", ttf);
            }

            var url = NSUrl.FromFilename(ttf);
            var err = CTFontManager.RegisterFontsForUrl(url, CTFontManagerScope.Process);

            Assert.IsNull(err, "err 1");
            err = CTFontManager.UnregisterFontsForUrl(url, CTFontManagerScope.Process);
            Assert.IsNull(err, "err 2");

            url = NSUrl.FromFilename(Path.GetFullPath("NonExistent.ttf"));
            err = CTFontManager.RegisterFontsForUrl(url, CTFontManagerScope.Process);
            Assert.IsNotNull(err, "err 3");
            err = CTFontManager.UnregisterFontsForUrl(url, CTFontManagerScope.Process);
            Assert.IsNotNull(err, "err 4");
        }
示例#2
0
        /// <summary>
        /// Init this instance to
        /// </summary>
        public static void Init()
        {
            NControl.Controls.FontLoader.LoadFonts(AppDomain.CurrentDomain.GetAssemblies(), (fontName, s) => {
                var data = new byte[s.Length];
                s.Read(data, 0, data.Length);

                var dataProvider = new CGDataProvider(data, 0, data.Length);
                var cgFont       = CGFont.CreateFromProvider(dataProvider);
                NSError error;

                var registered = CTFontManager.RegisterGraphicsFont(cgFont, out error);
                if (!registered)
                {
                    // If the error code is 105 then the font we are trying to register is already registered
                    // We will not report this as an error.
                    if (error.Code != 105)
                    {
                        throw new ArgumentException("Error registering: " + fontName +
                                                    " (" + error.LocalizedDescription + ")");
                    }
                }
            });
        }
示例#3
0
 public override bool RegisterFontFromFile(string fontPath)
 {
     return(CTFontManager.RegisterFontsForUrl(NSUrl.FromFilename(fontPath), CTFontManagerScope.Process) == null);
 }
        void LoadFontFile(string fileName)
        {
            CTFont nativeFont;
            var    dpiSize = 0;
            var    ext     = Path.GetExtension(fileName);

            if (!String.IsNullOrEmpty(ext))
            {
                if (nativeFontDescriptors == null)
                {
                    nativeFontDescriptors = new Dictionary <string, CTFontDescriptor> ();
                }

                //Try loading from Bundle first
                var fontName        = fileName.Substring(0, fileName.Length - ext.Length);
                var pathForResource = NSBundle.MainBundle.PathForResource(fontName, ext.Substring(1));

                NSUrl url;

                if (!string.IsNullOrEmpty(pathForResource))
                {
                    url = NSUrl.FromFilename(pathForResource);
                }
                else
                {
                    url = NSUrl.FromFilename(fileName);
                }

                // We will not use CTFontManager.RegisterFontsForUrl (url, CTFontManagerScope.Process);
                // here.  The reason is that there is no way we can be sure that the font can be created to
                // to identify the family name afterwards.  So instead we will create a CGFont from a data provider.
                // create CTFont to obtain the CTFontDescriptor, store family name and font descriptor to be accessed
                // later.
                try {
                    var dataProvider = new CGDataProvider(url.Path);
                    var cgFont       = CGFont.CreateFromProvider(dataProvider);

                    try {
                        nativeFont = new CTFont(cgFont, dpiSize, null);
                        if (!nativeFontDescriptors.ContainsKey(nativeFont.FamilyName))
                        {
                            nativeFontDescriptors.Add(nativeFont.FamilyName, nativeFont.GetFontDescriptor());
                            NSError error;
                            var     registered = CTFontManager.RegisterGraphicsFont(cgFont, out error);
                            if (!registered)
                            {
                                // If the error code is 105 then the font we are trying to register is already registered
                                // We will not report this as an error.
                                if (error.Code != 105)
                                {
                                    throw new ArgumentException("Error registering: " + Path.GetFileName(fileName));
                                }
                            }
                        }
                    }
                    catch
                    {
                        // note: MS throw the same exception FileNotFoundException if the file exists but isn't a valid font file
                        throw new System.IO.FileNotFoundException(fileName);
                    }
                }
                catch (Exception)
                {
                    // note: MS throw the same exception FileNotFoundException if the file exists but isn't a valid font file
                    throw new System.IO.FileNotFoundException(fileName);
                }
            }
        }
示例#5
0
 public void UnregisterFonts_Null()
 {
     TestRuntime.AssertXcodeVersion(11, 0);
     Assert.Throws <ArgumentNullException> (() => CTFontManager.UnregisterFonts(null, CTFontManagerScope.Process, null), "null array");
     Assert.Throws <ArgumentException> (() => CTFontManager.UnregisterFonts(new NSUrl [] { null }, CTFontManagerScope.Process, null), "null element");
 }
示例#6
0
 public void GetFontsNullUrl()
 {
     TestRuntime.AssertXcodeVersion(5, 0);
     CTFontManager.GetFonts(null);
 }
示例#7
0
 public void RegisterFontDescriptors_Null()
 {
     TestRuntime.AssertXcodeVersion(11, 0);
     Assert.Throws <ArgumentNullException> (() => CTFontManager.RegisterFontDescriptors(null, CTFontManagerScope.Process, true, null), "null array");
     Assert.Throws <ArgumentException> (() => CTFontManager.RegisterFontDescriptors(new CTFontDescriptor [] { null }, CTFontManagerScope.Process, true, null), "null element");
 }
示例#8
0
        string LoadFontFile(string fileName)
        {
            CTFont nativeFont;
            var    dpiSize = 0;
            var    ext     = Path.GetExtension(fileName);

            if (!String.IsNullOrEmpty(ext))
            {
                if (nativeFontDescriptors == null)
                {
                    nativeFontDescriptors = new Dictionary <string, string> ();
                }

                string fd = null;
                if (nativeFontDescriptors.TryGetValue(fileName, out fd))
                {
                    return(fd);
                }

                // We will not use CTFontManager.RegisterFontsForUrl (url, CTFontManagerScope.Process);
                // here.  The reason is that there is no way we can be sure that the font can be created to
                // to identify the family name afterwards.  So instead we will create a CGFont from a data provider.
                // create CTFont to obtain the CTFontDescriptor, store family name and font descriptor to be accessed
                // later.
                try {
                    var dataProvider = GetFontDataProvider(fileName);
                    var cgFont       = CGFont.CreateFromProvider(dataProvider);

                    try
                    {
                        nativeFont = new CTFont(cgFont, dpiSize, null);
                        if (!nativeFontDescriptors.ContainsKey(fileName))
                        {
                            nativeFontDescriptors.Add(fileName, nativeFont.PostScriptName);
                            NSError error;

                            var registered = CTFontManager.RegisterGraphicsFont(cgFont, out error);
                            if (!registered)
                            {
                                // If the error code is 105 then the font we are trying to register is already registered
                                // We will not report this as an error.
                                if (error.Code != 105)
                                {
                                    throw new ArgumentException("Error registering: " + Path.GetFileName(fileName));
                                }
                            }
                        }

                        return(nativeFont.PostScriptName);
                    }
                    catch
                    {
                        // note: MS throw the same exception FileNotFoundException if the file exists but isn't a valid font file
                        throw new System.IO.FileNotFoundException(fileName);
                    }
                }
                catch (Exception)
                {
                    // note: MS throw the same exception FileNotFoundException if the file exists but isn't a valid font file
                    throw new System.IO.FileNotFoundException(fileName);
                }
            }
            return(fileName);
        }
示例#9
0
 public void GetFontsNullUrl()
 {
     TestRuntime.AssertXcodeVersion(5, 0);
     Assert.Throws <ArgumentNullException> (() => CTFontManager.GetFonts(null));
 }
示例#10
0
        internal static UIFont EmbeddedFont(string resourceId, nfloat size, Assembly assembly = null)
        {
            if (resourceId == "STIXGeneral")
            {
                resourceId = "Forms9Patch.Resources.Fonts.STIXGeneral.otf";
            }

            if (_embeddedResourceFonts.ContainsKey(resourceId))
            {
                string family = _embeddedResourceFonts[resourceId];
                return(UIFont.FromName(family, size));
            }
            if (resourceId.Contains(".Resources.Fonts."))
            {
                // it's an Embedded Resource
                if (!resourceId.ToLower().EndsWith(".ttf") && !resourceId.ToLower().EndsWith(".otf"))
                {
                    throw new MissingMemberException("Embedded Font file names must end with \".ttf\" or \".otf\".");
                }
                lock (_loadFontLock)
                {
                    // what is the assembly?

                    /*
                     * var assemblyName = resourceId.Substring(0, resourceId.IndexOf(".Resources.Fonts."));
                     * //var assembly = System.Reflection.Assembly.Load (assemblyName);
                     * var assembly = ReflectionExtensions.GetAssemblyByName(assemblyName);
                     * if (assembly == null)
                     * {
                     *  // try using the current application assembly instead (as is the case with Shared Applications)
                     *  assembly = ReflectionExtensions.GetAssemblyByName(assemblyName + ".Droid");
                     *  //Console.WriteLine ("Assembly for Resource ID \"" + resourceID + "\" not found.");
                     *  //return null;
                     * }
                     */
                    // load it!
                    using (var stream = EmbeddedResourceCache.GetStream(resourceId, assembly))
                    {
                        if (stream == null)
                        {
                            Console.WriteLine("Could not open Embedded Resource [" + resourceId + "]");
                            return(null);
                        }
                        var data = NSData.FromStream(stream);
                        if (data == null)
                        {
                            Console.WriteLine("Could not retrieve data from Embedded Resource [" + resourceId + "].");
                            return(null);
                        }
                        var provider = new CGDataProvider(data);
                        var font     = CGFont.CreateFromProvider(provider);
                        if (!CTFontManager.RegisterGraphicsFont(font, out NSError error))
                        {
                            if (error.Code != 105)
                            {
                                Console.WriteLine("Could load but failed to register [" + resourceId + "] font.  Error messages:");
                                Console.WriteLine("\tdescription: " + error.Description);
                                throw new MissingMemberException("Failed to register [" + resourceId + "] font.  Error messages in console.");
                            }
                        }
                        _embeddedResourceFonts.Add(resourceId, font.PostScriptName);
                        return(UIFont.FromName(font.PostScriptName, size));
                    }
                }
            }
            return(null);
        }