|
| 1 | +#if PLATFORM_AGNOSTIC |
| 2 | + |
| 3 | +using FontStashSharp; |
| 4 | +using Myra; |
| 5 | +using StbImageSharp; |
| 6 | +using System.Drawing; |
| 7 | +using System.IO; |
| 8 | + |
| 9 | +using Texture2D = System.Object; |
| 10 | + |
| 11 | +namespace AssetManagementBase |
| 12 | +{ |
| 13 | + partial class MyraAssetManagerExtensions |
| 14 | + { |
| 15 | + internal class Texture2DWrapper |
| 16 | + { |
| 17 | + public int Width { get; private set; } |
| 18 | + public int Height { get; private set; } |
| 19 | + public object Texture { get; private set; } |
| 20 | + |
| 21 | + public Texture2DWrapper(int width, int height, object texture) |
| 22 | + { |
| 23 | + Width = width; |
| 24 | + Height = height; |
| 25 | + Texture = texture; |
| 26 | + } |
| 27 | + } |
| 28 | + |
| 29 | + private static AssetLoader<Texture2DWrapper> _textureLoader = (manager, assetName, settings, tag) => |
| 30 | + { |
| 31 | + ImageResult result = null; |
| 32 | + using (var stream = manager.Open(assetName)) |
| 33 | + { |
| 34 | + if (stream.CanSeek) |
| 35 | + { |
| 36 | + result = ImageResult.FromStream(stream, ColorComponents.RedGreenBlueAlpha); |
| 37 | + } |
| 38 | + else |
| 39 | + { |
| 40 | + // If stream doesnt provide seek functionaly, use MemoryStream instead |
| 41 | + using (var ms = new MemoryStream()) |
| 42 | + { |
| 43 | + stream.CopyTo(ms); |
| 44 | + ms.Seek(0, SeekOrigin.Begin); |
| 45 | + result = ImageResult.FromStream(ms, ColorComponents.RedGreenBlueAlpha); |
| 46 | + } |
| 47 | + } |
| 48 | + } |
| 49 | + |
| 50 | + // Premultiply Alpha |
| 51 | + var b = result.Data; |
| 52 | + for (var i = 0; i < result.Data.Length; i += 4) |
| 53 | + { |
| 54 | + var falpha = b[i + 3] / 255.0f; |
| 55 | + b[i] = (byte)(b[i] * falpha); |
| 56 | + b[i + 1] = (byte)(b[i + 1] * falpha); |
| 57 | + b[i + 2] = (byte)(b[i + 2] * falpha); |
| 58 | + } |
| 59 | + |
| 60 | + var textureManager = MyraEnvironment.Platform.Renderer.TextureManager; |
| 61 | + var texture = textureManager.CreateTexture(result.Width, result.Height); |
| 62 | + textureManager.SetTextureData(texture, new Rectangle(0, 0, result.Width, result.Height), result.Data); |
| 63 | + return new Texture2DWrapper(result.Width, result.Height, texture); |
| 64 | + }; |
| 65 | + |
| 66 | + private class FontSystemLoadingSettings : IAssetSettings |
| 67 | + { |
| 68 | + public Texture2D ExistingTexture { get; set; } |
| 69 | + public Rectangle ExistingTextureUsedSpace { get; set; } |
| 70 | + public string[] AdditionalFonts { get; set; } |
| 71 | + |
| 72 | + public string BuildKey() => string.Empty; |
| 73 | + } |
| 74 | + |
| 75 | + private static AssetLoader<FontSystem> _fontSystemLoader = (manager, assetName, settings, tag) => |
| 76 | + { |
| 77 | + var fontSystemSettings = new FontSystemSettings(); |
| 78 | + |
| 79 | + var fontSystemLoadingSettings = (FontSystemLoadingSettings)settings; |
| 80 | + if (fontSystemLoadingSettings != null) |
| 81 | + { |
| 82 | + fontSystemSettings.ExistingTexture = fontSystemLoadingSettings.ExistingTexture; |
| 83 | + fontSystemSettings.ExistingTextureUsedSpace = fontSystemLoadingSettings.ExistingTextureUsedSpace; |
| 84 | + } |
| 85 | + ; |
| 86 | + |
| 87 | + var fontSystem = new FontSystem(fontSystemSettings); |
| 88 | + var data = manager.ReadAsByteArray(assetName); |
| 89 | + fontSystem.AddFont(data); |
| 90 | + if (fontSystemLoadingSettings != null && fontSystemLoadingSettings.AdditionalFonts != null) |
| 91 | + { |
| 92 | + foreach (var file in fontSystemLoadingSettings.AdditionalFonts) |
| 93 | + { |
| 94 | + data = manager.ReadAsByteArray(file); |
| 95 | + fontSystem.AddFont(data); |
| 96 | + } |
| 97 | + } |
| 98 | + |
| 99 | + return fontSystem; |
| 100 | + }; |
| 101 | + |
| 102 | + internal static Texture2DWrapper LoadTexture2D(this AssetManager assetManager, string assetName) => |
| 103 | + assetManager.UseLoader(_textureLoader, assetName); |
| 104 | + |
| 105 | + public static FontSystem LoadFontSystem(this AssetManager assetManager, string assetName, string[] additionalFonts = null, Texture2D existingTexture = null, Rectangle existingTextureUsedSpace = default(Rectangle)) |
| 106 | + { |
| 107 | + FontSystemLoadingSettings settings = null; |
| 108 | + if (additionalFonts != null || existingTexture != null) |
| 109 | + { |
| 110 | + settings = new FontSystemLoadingSettings |
| 111 | + { |
| 112 | + AdditionalFonts = additionalFonts, |
| 113 | + ExistingTexture = existingTexture, |
| 114 | + ExistingTextureUsedSpace = existingTextureUsedSpace |
| 115 | + }; |
| 116 | + } |
| 117 | + |
| 118 | + return assetManager.UseLoader(_fontSystemLoader, assetName, settings); |
| 119 | + } |
| 120 | + } |
| 121 | +} |
| 122 | + |
| 123 | +#endif |
0 commit comments