Exemple #1
0
        private static ICollection <FontFamily> CreateFamilyCollection(
            Uri fontLocation,
            Uri fontFamilyBaseUri,
            string fontFamilyLocationReference
            )
        {
            // Use reference comparison to determine the critical isWindowsFonts value. We want this
            // to be true ONLY if we're called internally to enumerate the default family collection.
            // See the SecurityNote for the FamilyCollection constructor.
            FamilyCollection familyCollection =
                object.ReferenceEquals(fontLocation, Util.WindowsFontsUriObject) ?
                FamilyCollection.FromWindowsFonts(fontLocation) :
                FamilyCollection.FromUri(fontLocation);

            FontFamily[] fontFamilyList = familyCollection.GetFontFamilies(fontFamilyBaseUri, fontFamilyLocationReference);

            return(Array.AsReadOnly <FontFamily>(fontFamilyList));
        }
Exemple #2
0
        private static IFontFamily LookupFontFamily(NormalizedFontFamilyReference familyReference)
        {
            if (familyReference.IsUnresolved)
            {
                return(null);
            }

            FamilyCollection familyCollection;

            if (familyReference.LocationUri != null)
            {
                familyCollection = FamilyCollection.FromUri(familyReference.LocationUri);
            }
            else
            {
                familyCollection = FamilyCollection.Default;
            }
            return(familyCollection.LookupFamily(familyReference.FamilyName));
        }
Exemple #3
0
        internal static IFontFamily LookupFontFamilyAndFace(
            CanonicalFontFamilyReference canonicalFamilyReference,
            ref FontStyle style,
            ref FontWeight weight,
            ref FontStretch stretch
            )
        {
            if (canonicalFamilyReference == null || object.ReferenceEquals(canonicalFamilyReference, CanonicalFontFamilyReference.Unresolved))
            {
                // no canonical name, e.g., because the friendly name was an empty string
                // or could not be canonicalized
                return(null);
            }

            try
            {
                FamilyCollection familyCollection;

                if (canonicalFamilyReference.LocationUri == null && canonicalFamilyReference.EscapedFileName == null)
                {
                    // No explicit location; use the default family collection.
                    familyCollection = _defaultFamilyCollection;
                }
                else if (canonicalFamilyReference.LocationUri != null)
                {
                    // Look in the location specified by the font family reference.
                    familyCollection = FamilyCollection.FromUri(canonicalFamilyReference.LocationUri);
                }
                else // canonicalFamilyReference.EscapedFileName != null
                {
                    // Look in the specified file in the Windows Fonts folder
                    // Note: CanonicalFamilyReference.EscapedFileName is safe to combine with Util.WindowsFontsUriObject because CanonicalFamilyReference guarantees that it will be a simple filename
                    // without relative path or directory components.
                    Uri locationUri = new Uri(Util.WindowsFontsUriObject, canonicalFamilyReference.EscapedFileName);
                    familyCollection = FamilyCollection.FromWindowsFonts(locationUri);
                }

                IFontFamily fontFamily = familyCollection.LookupFamily(
                    canonicalFamilyReference.FamilyName,
                    ref style,
                    ref weight,
                    ref stretch
                    );
                return(fontFamily);
            }
            // The method returns null in case of malformed/non-existent fonts and we fall back to the next font.
            // Therefore, we can disable PreSharp warning about empty catch bodies.
#pragma warning disable 6502
            catch (FileFormatException)
            {
                // malformed font file
            }
            catch (IOException)
            {
                // canonical name points to a place that doesn't exist or can't be read for some reason
            }
            catch (UnauthorizedAccessException)
            {
                // canonical name points to a place caller doesn't have permission to access
            }
            catch (ArgumentException)
            {
                // canonical name points to a valid Uri that doesn't point to a well formed
                // OS local path
            }
            catch (NotSupportedException)
            {
                // canonical name points to a Uri that specifies an unregistered scheme
            }
            catch (UriFormatException)
            {
                // canonical name points to a malformed Uri
            }
#pragma warning restore 6502
            // we want to fall back to the default fallback font instead of crashing
            return(null);
        }