Пример #1
0
        /// <summary>
        /// Searches for font assets at a given location and only accepts assets that fit to a given filename expression.
        /// <para>File names can target multiple files with * wildcard. For example "FontFile*.ttf"</para>
        /// </summary>
        /// <param name="fontFamilyKey"></param>
        /// <returns></returns>
        private static IEnumerable <Uri> GetFontAssetsByExpression(FontFamilyKey fontFamilyKey)
        {
            var assetLoader = AvaloniaLocator.Current.GetService <IAssetLoader>();

            var fileName = GetFileName(fontFamilyKey, out var fileExtension, out var location);

            var availableResources = assetLoader.GetAssets(location, fontFamilyKey.BaseUri);

            string compareTo;

            if (fontFamilyKey.Source.IsAbsoluteUri)
            {
                if (fontFamilyKey.Source.Scheme == "resm")
                {
                    compareTo = location.AbsolutePath + "." + fileName.Split('*').First();
                }
                else
                {
                    compareTo = location.AbsolutePath + fileName.Split('*').First();
                }
            }
            else
            {
                compareTo = location.AbsolutePath + fileName.Split('*').First();
            }

            var matchingResources = availableResources.Where(
                x => x.AbsolutePath.Contains(compareTo) &&
                x.AbsolutePath.EndsWith(fileExtension));

            return(matchingResources);
        }
Пример #2
0
        private static string GetFileName(FontFamilyKey fontFamilyKey, out string fileExtension, out Uri location)
        {
            if (fontFamilyKey.Source.IsAbsoluteUri && fontFamilyKey.Source.Scheme == "resm")
            {
                fileExtension = "." + fontFamilyKey.Source.AbsolutePath.Split('.').LastOrDefault();

                var fileName = fontFamilyKey.Source.LocalPath.Replace(fileExtension, string.Empty).Split('.').LastOrDefault();

                location = new Uri(fontFamilyKey.Source.AbsoluteUri.Replace("." + fileName + fileExtension, string.Empty), UriKind.RelativeOrAbsolute);

                return(fileName);
            }

            var pathSegments = fontFamilyKey.Source.OriginalString.Split('/');

            var fileNameWithExtension = pathSegments.Last();

            var fileNameSegments = fileNameWithExtension.Split('.');

            fileExtension = "." + fileNameSegments.Last();

            if (fontFamilyKey.BaseUri != null)
            {
                var relativePath = fontFamilyKey.Source.OriginalString
                                   .Replace(fileNameWithExtension, string.Empty);

                location = new Uri(fontFamilyKey.BaseUri, relativePath);
            }
            else
            {
                location = new Uri(fontFamilyKey.Source.AbsolutePath.Replace(fileNameWithExtension, string.Empty));
            }

            return(fileNameSegments.First());
        }
Пример #3
0
        /// <summary>
        /// Searches for font assets at a given location and returns a quantity of found assets
        /// </summary>
        /// <param name="fontFamilyKey"></param>
        /// <returns></returns>
        private static IEnumerable <Uri> GetFontAssetsBySource(FontFamilyKey fontFamilyKey)
        {
            var assetLoader     = AvaloniaLocator.Current.GetRequiredService <IAssetLoader>();
            var availableAssets = assetLoader.GetAssets(fontFamilyKey.Source, fontFamilyKey.BaseUri);

            return(availableAssets.Where(x => IsFontTtfOrOtf(x)));
        }
Пример #4
0
        private static (string fileNameWithoutExtension, string extension) GetFileName(
            FontFamilyKey fontFamilyKey, out Uri location)
        {
            if (fontFamilyKey.Source.IsAbsoluteResm())
            {
                var fileName = GetFileNameAndExtension(fontFamilyKey.Source.GetUnescapeAbsolutePath(), '.');

                var uriLocation = fontFamilyKey.Source.GetUnescapeAbsoluteUri()
                                  .Replace("." + fileName.fileNameWithoutExtension + fileName.extension, string.Empty);
                location = new Uri(uriLocation, UriKind.RelativeOrAbsolute);

                return(fileName);
            }

            var filename     = GetFileNameAndExtension(fontFamilyKey.Source.OriginalString);
            var fullFilename = filename.fileNameWithoutExtension + filename.extension;

            if (fontFamilyKey.BaseUri != null)
            {
                var relativePath = fontFamilyKey.Source.OriginalString
                                   .Replace(fullFilename, string.Empty);

                location = new Uri(fontFamilyKey.BaseUri, relativePath);
            }
            else
            {
                var uriString = fontFamilyKey.Source
                                .GetUnescapeAbsoluteUri()
                                .Replace(fullFilename, string.Empty);
                location = new Uri(uriString);
            }

            return(filename);
        }
        /// <summary>
        /// Searches for font assets at a given location and returns a quantity of found assets
        /// </summary>
        /// <param name="fontFamilyKey"></param>
        /// <returns></returns>
        private static IEnumerable <Uri> GetFontAssetsBySource(FontFamilyKey fontFamilyKey)
        {
            var availableAssets = s_assetLoader.GetAssets(fontFamilyKey.Source, fontFamilyKey.BaseUri);

            var matchingAssets =
                availableAssets.Where(x => x.AbsolutePath.EndsWith(".ttf") || x.AbsolutePath.EndsWith(".otf"));

            return(matchingAssets);
        }
Пример #6
0
        private static string CreateFilePattern(
            FontFamilyKey fontFamilyKey, Uri location, string fileNameWithoutExtension)
        {
            var path = location.GetUnescapeAbsolutePath();
            var file = GetSubString(fileNameWithoutExtension, '*');

            return(fontFamilyKey.Source.IsAbsoluteResm()
                ? path + "." + file
                : path + file);
        }
Пример #7
0
        /// <summary>
        /// Searches for font assets at a given location and only accepts assets that fit to a given filename expression.
        /// <para>File names can target multiple files with * wildcard. For example "FontFile*.ttf"</para>
        /// </summary>
        /// <param name="fontFamilyKey"></param>
        /// <returns></returns>
        private static IEnumerable <Uri> GetFontAssetsByExpression(FontFamilyKey fontFamilyKey)
        {
            var(fileNameWithoutExtension, extension) = GetFileName(fontFamilyKey, out var location);
            var filePattern = CreateFilePattern(fontFamilyKey, location, fileNameWithoutExtension);

            var assetLoader        = AvaloniaLocator.Current.GetRequiredService <IAssetLoader>();
            var availableResources = assetLoader.GetAssets(location, fontFamilyKey.BaseUri);

            return(availableResources.Where(x => IsContainsFile(x, filePattern, extension)));
        }
Пример #8
0
        /// <summary>
        /// Searches for font assets at a given location and returns a quantity of found assets
        /// </summary>
        /// <param name="fontFamilyKey"></param>
        /// <returns></returns>
        private static IEnumerable <Uri> GetFontAssetsBySource(FontFamilyKey fontFamilyKey)
        {
            var assetLoader     = AvaloniaLocator.Current.GetRequiredService <IAssetLoader>();
            var availableAssets = assetLoader.GetAssets(fontFamilyKey.Source, fontFamilyKey.BaseUri);

            var matchingAssets =
                availableAssets.Where(x => x.AbsolutePath.EndsWith(".ttf") || x.AbsolutePath.EndsWith(".otf"));

            return(matchingAssets);
        }
Пример #9
0
        /// <summary>
        /// Loads all font assets that belong to the specified <see cref="FontFamilyKey"/>
        /// </summary>
        /// <param name="fontFamilyKey"></param>
        /// <returns></returns>
        public static IEnumerable <Uri> LoadFontAssets(FontFamilyKey fontFamilyKey)
        {
            var sourceWithoutArguments = fontFamilyKey.Source.OriginalString.Split('?').First();

            if (sourceWithoutArguments.EndsWith(".ttf") ||
                sourceWithoutArguments.EndsWith(".otf"))
            {
                return(GetFontAssetsByExpression(fontFamilyKey));
            }

            return(GetFontAssetsBySource(fontFamilyKey));
        }
Пример #10
0
 /// <summary>
 /// Loads all font assets that belong to the specified <see cref="FontFamilyKey"/>
 /// </summary>
 /// <param name="fontFamilyKey"></param>
 /// <returns></returns>
 public static IEnumerable <Uri> LoadFontAssets(FontFamilyKey fontFamilyKey) =>
 IsFontTtfOrOtf(fontFamilyKey.Source) ?
 GetFontAssetsByExpression(fontFamilyKey) :
 GetFontAssetsBySource(fontFamilyKey);
Пример #11
0
 public static IEnumerable <Uri> LoadFontAssets(FontFamilyKey fontFamilyKey)
 {
     return(fontFamilyKey.FileName != null
         ? GetFontAssetsByFileName(fontFamilyKey.Location, fontFamilyKey.FileName)
         : GetFontAssetsByLocation(fontFamilyKey.Location));
 }