Ejemplo n.º 1
0
 static UIFont LoadAndRegisterEmbeddedFont(string resourceId, nfloat size, Assembly assembly)
 {
     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));
     }
 }
Ejemplo n.º 2
0
        static Typeface LoadAndRegisterEmbeddedFont(string resouceId, Assembly assembly)
        {
            using (var inputStream = EmbeddedResourceCache.GetStream(resouceId, assembly))
            {
                if (inputStream == null)
                {
                    System.Console.WriteLine("Embedded Resource for FontFamily \"" + resouceId + "\" not found.");
                    return(null);
                }

                var cachedFontDir = new File(CustomFontDirRoot + "/ResourceFonts");
                if (!cachedFontDir.Exists())
                {
                    cachedFontDir.Mkdir();
                }

                var cachedFontFile = new File(cachedFontDir, resouceId);
                using (var outputStream = new FileOutputStream(cachedFontFile))
                {
                    const int bufferSize = 1024;
                    var       buffer     = new byte[bufferSize];
                    int       length     = -1;
                    while ((length = inputStream.Read(buffer, 0, bufferSize)) > 0)
                    {
                        outputStream.Write(buffer, 0, length);
                    }
                }
                Typeface typeface = Typeface.CreateFromFile(cachedFontFile);
                if (typeface == null)
                {
                    System.Console.WriteLine("Embedded Resource font file (" + resouceId + ") could not be loaded as an Android Typeface.");
                    return(null);
                }
                _fontFiles.Add(resouceId, cachedFontFile.AbsolutePath);
                return(typeface);
            }
        }
Ejemplo n.º 3
0
        public static string ReconcileFontFamily(string f9pFontFamily)
        {
            if (string.IsNullOrWhiteSpace(f9pFontFamily))
            {
                return(DefaultSystemFontFamily);
            }

            string localStorageFileName = null;
            string uri        = null;
            string resourceId = null;

            switch (f9pFontFamily.ToLower())
            {
            case "monospace":
                return("Consolas");

            case "serif":
                return("Cambria");

            case "sans-serif":
                return("Segoe UI");

            case "stixgeneral":
                resourceId    = "Forms9Patch.Resources.Fonts.STIXGeneral.otf";
                f9pFontFamily = resourceId + "#" + "STIXGeneral";
                break;
            }

            if (EmbeddedFontSources.ContainsKey(f9pFontFamily))
            {
                return(EmbeddedFontSources[f9pFontFamily]);
            }

            var idParts = f9pFontFamily.Split('#');

            resourceId = idParts[0];

            if (localStorageFileName == null)
            {
                // we've got to go hunting for this ... and UWP doesn't give us much help
                // first, try the main assembly!
                var    targetParts    = f9pFontFamily.Split('.');
                string targetAsmNameA = "invalid_assembly_name";
                if (targetParts.Contains("Resources"))
                {
                    targetAsmNameA = "";
                    foreach (var part in targetParts)
                    {
                        if (part == "Resources")
                        {
                            break;
                        }
                        targetAsmNameA += part + ".";
                    }
                    if (targetAsmNameA.Length > 0)
                    {
                        targetAsmNameA = targetAsmNameA.Substring(0, targetAsmNameA.Length - 1);
                    }
                }
                var targetAsmNameB = targetParts.First();

                var appAsmName = Forms9Patch.ApplicationInfoService.Assembly.GetName().Name;
                if (targetAsmNameA == appAsmName || targetAsmNameB == appAsmName)
                {
                    localStorageFileName = EmbeddedResourceCache.LocalStorageSubPathForEmbeddedResource(resourceId, Forms9Patch.ApplicationInfoService.Assembly);
                    if (localStorageFileName != null)
                    {
                        uri = EmbeddedResourceCache.ApplicationUri(resourceId, Forms9Patch.ApplicationInfoService.Assembly);
                    }
                }

                // if that doesn't work, look through all known assemblies
                if (localStorageFileName == null)
                {
                    foreach (var asm in Settings.AssembliesToInclude)
                    {
                        var asmName = asm.GetName().Name;
                        if (targetAsmNameA == asmName || targetAsmNameB == asmName)
                        {
                            localStorageFileName = EmbeddedResourceCache.LocalStorageSubPathForEmbeddedResource(resourceId, asm);
                            uri = EmbeddedResourceCache.ApplicationUri(resourceId, asm);
                            break;
                        }
                    }
                }
            }

            if (localStorageFileName == null)
            {
                return(f9pFontFamily);
            }

            string fontName = null;

            if (idParts.Count() > 1)
            {
                fontName = idParts.Last();
            }
            else
            {
                //var cachedFilePath = System.IO.Path.Combine(P42.Utils.Environment.ApplicationDataPath, localStorageFileName);
                var cachedFilePath = System.IO.Path.Combine(P42.Utils.EmbeddedResourceCache.FolderPath(), localStorageFileName);
                fontName = TTFAnalyzer.FontFamily(cachedFilePath);
            }
            //var uwpFontFamily = "ms-appdata:///local/" + localStorageFileName.Replace('\\','/') + (string.IsNullOrWhiteSpace(fontName) ? null : "#" + fontName);
            var uwpFontFamily = uri + (string.IsNullOrWhiteSpace(fontName) ? null : "#" + fontName);

            //var uwpFontFamily = "ms-appdata:///local/EmbeddedResourceCache/02fe60e0da81514d145d946ab9ad9b97#Pacifico";
            //foreach (var c in uwpFontFamily)
            //    System.Diagnostics.Debug.WriteLine("c=["+c+"]["+(int)c+"]");
            EmbeddedFontSources.Add(f9pFontFamily, uwpFontFamily);
            return(uwpFontFamily);
        }
Ejemplo n.º 4
0
        public static Typeface TypefaceForFontFamily(string fontFamily, Assembly assembly = null)
        {
            //string fontFilePath;
            if (fontFamily == null)
            {
                //return Typeface.Default;
                return(null);
            }

            Typeface result;

            if (fontFamily.ToLower() == "monospace")
            {
                result = TrySystemFont("monospace");
                if (result != null)
                {
                    return(result);
                }
                result = TrySystemFont("Droid Sans Mono");
                if (result != null)
                {
                    return(result);
                }
            }
            if (fontFamily.ToLower() == "serif")
            {
                result = TrySystemFont("serif");
                if (result != null)
                {
                    return(result);
                }
                result = TrySystemFont("Droid Serif");
                if (result != null)
                {
                    return(result);
                }
            }
            if (fontFamily.ToLower() == "sans-serif")
            {
                result = TrySystemFont("sans-serif");
                if (result != null)
                {
                    return(result);
                }
                result = TrySystemFont("normal");
                if (result != null)
                {
                    return(result);
                }
                result = TrySystemFont("Roboto");
                if (result != null)
                {
                    return(result);
                }
                result = TrySystemFont("Droid Sans");
                if (result != null)
                {
                    return(result);
                }
            }

            result = TrySystemFont(fontFamily);
            if (result != null)
            {
                return(result);
            }

            if (fontFamily.Contains(".Resources.Fonts."))
            {
                // it's an Embedded Resource
                if (!fontFamily.ToLower().EndsWith(".ttf") && !fontFamily.ToLower().EndsWith(".otf"))
                {
                    throw new InvalidObjectException("Embedded Font file names must end with \".ttf\" or \".otf\".");
                }
                // what is the assembly?

                /*
                 * var assemblyName = fontFamily.Substring(0, fontFamily.IndexOf(".Resources.Fonts."));
                 *
                 * //var assembly = System.Reflection.Assembly.Load (assemblyName);
                 * var assembly = ReflectionExtensions.GetAssemblyByName(assemblyName) ?? Forms9Patch.Droid.Settings.ApplicationAssembly;
                 *
                 * if (assembly == null)
                 * {
                 *  // try using the current application assembly instead (as is the case with Shared Applications)
                 *  assembly = ReflectionExtensions.GetAssemblyByName(assemblyName + ".Droid");
                 *  ///System.Console.WriteLine ("Assembly for FontFamily \"" + fontFamily + "\" not found.");
                 *  //return null;
                 * }
                 */
                // move it to the Application's CustomFontDirRoot
                using (var inputStream = EmbeddedResourceCache.GetStream(fontFamily, assembly))
                {
                    if (inputStream == null)
                    {
                        System.Console.WriteLine("Embedded Resource for FontFamily \"" + fontFamily + "\" not found.");
                        return(null);
                    }

                    var cachedFontDir = new File(CustomFontDirRoot + "/ResourceFonts");
                    if (!cachedFontDir.Exists())
                    {
                        cachedFontDir.Mkdir();
                    }

                    var cachedFontFile = new File(cachedFontDir, fontFamily);
                    using (var outputStream = new FileOutputStream(cachedFontFile))
                    {
                        const int bufferSize = 1024;
                        var       buffer     = new byte[bufferSize];
                        int       length     = -1;
                        while ((length = inputStream.Read(buffer, 0, bufferSize)) > 0)
                        {
                            outputStream.Write(buffer, 0, length);
                        }
                    }
                    Typeface typeface = Typeface.CreateFromFile(cachedFontFile);
                    if (typeface == null)
                    {
                        System.Console.WriteLine("Embedded Resource font file (" + fontFamily + ") could not be loaded as an Android Typeface.");
                        return(null);
                    }
                    _fontFiles.Add(fontFamily, cachedFontFile.AbsolutePath);
                    return(typeface);
                }
            }
            return(null);
        }
Ejemplo n.º 5
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);
        }