Example #1
0
            protected override Task <ResultStatus> DoCommandOverride(ICommandContext commandContext)
            {
                // try to import the font from the original bitmap or ttf file
                Graphics.SpriteFont staticFont;
                try
                {
                    staticFont = StaticFontCompiler.Compile(FontDataFactory, AssetParameters, colorspace == ColorSpace.Linear);
                }
                catch (FontNotFoundException ex)
                {
                    commandContext.Logger.Error("Font [{0}] was not found on this machine.", ex.FontName);
                    return(Task.FromResult(ResultStatus.Failed));
                }

                // check that the font data is valid
                if (staticFont == null || staticFont.Textures.Count == 0)
                {
                    return(Task.FromResult(ResultStatus.Failed));
                }

                // save the data into the database
                var assetManager = new ContentManager();

                assetManager.Save(Url, staticFont);

                // dispose textures allocated by the StaticFontCompiler
                foreach (var texture in staticFont.Textures)
                {
                    texture.Dispose();
                }

                return(Task.FromResult(ResultStatus.Successful));
            }
        /// <summary>
        /// Generate a precompiled sprite font from the current sprite font asset.
        /// </summary>
        /// <param name="asset">The sprite font asset</param>
        /// <param name="sourceAsset">The source sprite font asset item</param>
        /// <param name="texturePath">The path of the source texture</param>
        /// <param name="srgb">Indicate if the generated texture should be srgb</param>
        /// <returns>The precompiled sprite font asset</returns>
        public static PrecompiledSpriteFontAsset GeneratePrecompiledSpriteFont(this SpriteFontAsset asset, AssetItem sourceAsset, string texturePath, bool srgb)
        {
            var staticFont = (StaticSpriteFont)StaticFontCompiler.Compile(FontDataFactory, asset, srgb);

            var referenceToSourceFont = new AssetReference <SpriteFontAsset>(sourceAsset.Id, sourceAsset.Location);
            var glyphs   = new List <Glyph>(staticFont.CharacterToGlyph.Values);
            var textures = staticFont.Textures;

            var imageType       = ImageFileType.Png;
            var textureFileName = new UFile(texturePath).GetFullPathWithoutExtension() + imageType.ToFileExtension();

            if (textures != null && textures.Count > 0)
            {
                // save the texture   TODO support for multi-texture
                using (var stream = File.OpenWrite(textureFileName))
                    staticFont.Textures[0].GetSerializationData().Save(stream, imageType);
            }

            var precompiledAsset = new PrecompiledSpriteFontAsset
            {
                Glyphs             = glyphs,
                Size               = asset.Size,
                Style              = asset.Style,
                Source             = referenceToSourceFont,
                FontDataFile       = textureFileName,
                BaseOffset         = staticFont.BaseOffsetY,
                DefaultLineSpacing = staticFont.DefaultLineSpacing,
                ExtraSpacing       = staticFont.ExtraSpacing,
                ExtraLineSpacing   = staticFont.ExtraLineSpacing,
                DefaultCharacter   = asset.DefaultCharacter,
                FontName           = !string.IsNullOrEmpty(asset.Source) ? (asset.Source.GetFileName() ?? "") : asset.FontName,
                IsPremultiplied    = asset.IsPremultiplied,
                IsSrgb             = srgb
            };

            return(precompiledAsset);
        }