예제 #1
0
        public (bool success, string?filePath) LoadFont(EmbeddedFont font)
        {
            try
            {
                if (font.ResourceStream == null)
                {
                    throw new InvalidOperationException("ResourceStream was null.");
                }

                var data     = NSData.FromStream(font.ResourceStream);
                var provider = new CGDataProvider(data);
                var cGFont   = CGFont.CreateFromProvider(provider);
                var name     = cGFont.PostScriptName;

                if (CTFontManager.RegisterGraphicsFont(cGFont, out var error))
                {
                    return(true, name);
                }

                var uiFont = UIFont.FromName(name, 10);
                if (uiFont != null)
                {
                    return(true, name);
                }

                throw new NSErrorException(error);
            }
            catch (Exception ex)
            {
                _logger?.LogWarning(ex, "Unable register font {Font} with the system.", font.FontName);
            }

            return(false, null);
        }
예제 #2
0
        public (bool success, string?filePath) LoadFont(EmbeddedFont font)
        {
            try
            {
                if (font.ResourceStream == null)
                {
                    throw new InvalidOperationException("ResourceStream was null.");
                }

                var data     = NSData.FromStream(font.ResourceStream);
                var provider = new CGDataProvider(data);
                var cGFont   = CGFont.CreateFromProvider(provider);
                var name     = cGFont.PostScriptName;
                if (CTFontManager.RegisterGraphicsFont(cGFont, out var error))
                {
                    return(true, name);
                }
                else                 //Lets check if the font is already registered
                {
                    var uiFont = UIFont.FromName(name, 10);
                    if (uiFont != null)
                    {
                        return(true, name);
                    }
                }
                Debug.WriteLine(error.Description);
            }
            catch (Exception ex)
            {
                Debug.WriteLine(ex);
            }

            return(false, null);
        }
예제 #3
0
        public (bool success, string?filePath) LoadFont(EmbeddedFont font)
        {
            var tmpdir   = Path.GetTempPath();
            var filePath = Path.Combine(tmpdir, font.FontName !);

            if (File.Exists(filePath))
            {
                return(true, filePath);
            }

            try
            {
                if (font.ResourceStream == null)
                {
                    throw new InvalidOperationException("ResourceStream was null.");
                }

                using (var fileStream = File.Create(filePath))
                {
                    font.ResourceStream.CopyTo(fileStream);
                }

                return(true, filePath);
            }
            catch (Exception ex)
            {
                Debug.WriteLine(ex);
                File.Delete(filePath);
            }

            return(false, null);
        }
예제 #4
0
		public (bool success, string? filePath) LoadFont(EmbeddedFont font)
		{
			var filePath = Path.Combine(_rootPath, font.FontName!);
			if (File.Exists(filePath))
				return (true, filePath);

			try
			{
				if (font.ResourceStream == null)
					throw new InvalidOperationException("ResourceStream was null.");

				if (!Directory.Exists(_rootPath))
					Directory.CreateDirectory(_rootPath);

				using (var fileStream = File.Create(filePath))
				{
					font.ResourceStream.CopyTo(fileStream);
				}

				return (true, filePath);
			}
			catch (Exception ex)
			{
				_logger?.LogWarning(ex, "Unable copy font {Font} to local file system.", font.FontName);

				File.Delete(filePath);
			}

			return (false, null);
		}
예제 #5
0
        public string?LoadFont(EmbeddedFont font)
        {
            try
            {
                CGFont?cgFont;

                if (font.ResourceStream == null)
                {
                    if (!System.IO.File.Exists(font.FontName))
                    {
                        throw new InvalidOperationException("ResourceStream was null.");
                    }

                    var provider = new CGDataProvider(font.FontName);
                    cgFont = CGFont.CreateFromProvider(provider);
                }
                else
                {
                    var data = NSData.FromStream(font.ResourceStream);
                    if (data == null)
                    {
                        throw new InvalidOperationException("Unable to load font stream data.");
                    }
                    var provider = new CGDataProvider(data);
                    cgFont = CGFont.CreateFromProvider(provider);
                }

                if (cgFont == null)
                {
                    throw new InvalidOperationException("Unable to load font from the stream.");
                }

                var name = cgFont.PostScriptName;

                if (CTFontManager.RegisterGraphicsFont(cgFont, out var error))
                {
                    return(name);
                }

                var uiFont = UIFont.FromName(name, 10);
                if (uiFont != null)
                {
                    return(name);
                }

                throw new NSErrorException(error);
            }
            catch (Exception ex)
            {
                _serviceProvider?.CreateLogger <EmbeddedFontLoader>()?.LogWarning(ex, "Unable register font {Font} with the system.", font.FontName);
            }

            return(null);
        }
예제 #6
0
        public string?LoadFont(EmbeddedFont font)
        {
            if (FontCacheDirectory == null)
            {
                FontCacheDirectory = Directory.CreateDirectory(IOPath.Combine(TApplication.Current.DirectoryInfo.Data, _fontCacheFolderName));
                Utility.AppendGlobalFontPath(FontCacheDirectory.FullName);
            }

            var filePath = IOPath.Combine(FontCacheDirectory.FullName, font.FontName !);
            var name     = IOPath.GetFileNameWithoutExtension(filePath);

            if (File.Exists(filePath))
            {
                return(name);
            }
            try
            {
                using (var fileStream = File.Create(filePath))
                {
                    if (font.ResourceStream == null)
                    {
                        throw new InvalidOperationException("ResourceStream was null.");
                    }

                    font.ResourceStream.CopyTo(fileStream);
                }

#if __TIZEN__
                if (DotnetUtil.TizenAPIVersion > 5)
                {
                    Utility.FontReinit();
                }
#endif
                return(name);
            }
            catch (Exception ex)
            {
                Debug.WriteLine(ex.Message);
                File.Delete(filePath);
            }
            return(null);
        }
예제 #7
0
        public string?LoadFont(EmbeddedFont font)
        {
            var tmpdir = ApplicationData.Current.LocalFolder.CreateFolderAsync(FontCacheFolderName, CreationCollisionOption.OpenIfExists).AsTask().Result;

            var file = tmpdir.TryGetItemAsync(font.FontName).AsTask().Result;

            if (file != null)
            {
                return(CleanseFilePath(file.Path));
            }

            StorageFile?newFile = null;

            try
            {
                if (font.ResourceStream == null)
                {
                    throw new InvalidOperationException("ResourceStream was null.");
                }

                newFile = tmpdir.CreateFileAsync(font.FontName).AsTask().Result;
                using (var fileStream = newFile.OpenStreamForWriteAsync().Result)
                {
                    font.ResourceStream.CopyTo(fileStream);
                }

                return(CleanseFilePath(newFile.Path));
            }
            catch (Exception ex)
            {
                _serviceProvider?.CreateLogger <FontManager>()?.LogWarning(ex, "Unable copy font {Font} to local file system.", font.FontName);

                if (newFile != null)
                {
                    newFile.DeleteAsync().AsTask().Wait();
                }
            }

            return(null);
        }
예제 #8
0
        public (bool success, string?filePath) LoadFont(EmbeddedFont font)
        {
            var tmpdir = ApplicationData.Current.LocalFolder.CreateFolderAsync(FontCacheFolderName, CreationCollisionOption.OpenIfExists).AsTask().Result;

            var file = tmpdir.TryGetItemAsync(font.FontName).AsTask().Result;

            if (file != null)
            {
                return(true, CleanseFilePath(file.Path));
            }

            StorageFile?newFile = null;

            try
            {
                if (font.ResourceStream == null)
                {
                    throw new InvalidOperationException("ResourceStream was null.");
                }

                newFile = tmpdir.CreateFileAsync(font.FontName).AsTask().Result;
                using (var fileStream = newFile.OpenStreamForWriteAsync().Result)
                {
                    font.ResourceStream.CopyTo(fileStream);
                }

                return(true, CleanseFilePath(newFile.Path));
            }
            catch (Exception ex)
            {
                Debug.WriteLine(ex);

                if (newFile != null)
                {
                    newFile.DeleteAsync().AsTask().Wait();
                }
            }

            return(false, null);
        }
예제 #9
0
        public string?LoadFont(EmbeddedFont font)
        {
            string rootPath = RootPath;
            var    filePath = Path.Combine(rootPath, font.FontName !);

            if (File.Exists(filePath))
            {
                return(filePath);
            }

            try
            {
                if (font.ResourceStream == null)
                {
                    throw new InvalidOperationException("ResourceStream was null.");
                }

                if (!Directory.Exists(rootPath))
                {
                    Directory.CreateDirectory(rootPath);
                }

                using (var fileStream = File.Create(filePath))
                {
                    font.ResourceStream.CopyTo(fileStream);
                }

                return(filePath);
            }
            catch (Exception ex)
            {
                _serviceProvider?.CreateLogger <EmbeddedFontLoader>()?.LogWarning(ex, "Unable copy font {Font} to local file system.", font.FontName);

                File.Delete(filePath);
            }

            return(null);
        }