Ejemplo n.º 1
0
        internal static string EmbeddedFontFamilyName(string resourceID)
        {
            if (resourceID == "STIXGeneral")
            {
                resourceID = "Forms9Patch.Resources.Fonts.STIXGeneral.otf";
            }

            if (_embeddedResourceFonts.ContainsKey(resourceID))
            {
                return(_embeddedResourceFonts[resourceID]);
            }

            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");
                        assembly = Forms9Patch.ApplicationInfoService.Assembly;
                        //Console.WriteLine ("Assembly for Resource ID \"" + resourceID + "\" not found.");
                        //return null;
                    }
                    // get font's name
                    var analyzer = new TTFAnalyzer();
                    var name     = analyzer.FontFamily(assembly, resourceID);
                    if (!string.IsNullOrWhiteSpace(name))
                    {
                        var uwpPathComponents = resourceID.Split('.');
                        var uwpPath           = "";
                        for (int i = 0; i < uwpPathComponents.Count(); i++)
                        {
                            uwpPath += uwpPathComponents[i];
                            if (i < uwpPathComponents.Count() - 2)
                            {
                                uwpPath += "/";
                            }
                            else if (i < uwpPathComponents.Count() - 1)
                            {
                                uwpPath += ".";
                            }
                        }
                        var fontFamilyName = "/" + assembly.FullName + ";" + uwpPath + "#" + name;
                    }
                }
                //} else {
                //	Console.WriteLine ("Font [] is assumed not to be an embedded resource because it does not contain \".Resources.Fonts.\" in its Resource ID");
            }
            return(null);
        }
Ejemplo n.º 2
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);
        }