在Monogame中使用BMP图像作为字体

Stacknerd

有没有一种方法可以使用BMP图像加载自定义字体。

我已经看到了Microsoft提供的在线解决方案,但是尝试运行此解决方案时,我一直在获取内容加载异常。

看起来这曾经与XNA一起使用,但Monogame可能不再适用。

我需要自己的自定义字体,因为该字体不会预先安装在客户端计算机上。

我已经查看了SpriteFont Converter中的XNB文件,但这不是我想要的解决方案。

任何帮助将不胜感激,谢谢

Stacknerd

经过长时间的研究,我最终找到了在线解决方案。这是教程的链接:http : //www.craftworkgames.com/blog/tutorial-bmfont-rendering-with-monogame/

此方法要求您下载名为bmFont的软件:http ://www.angelcode.com/products/bmfont/

有了这个软件,您会收到2个文件的字体输出:

  1. .fnt文件,用于纹理中的图案
  2. .png文件,即实际字符。

为了使这些文件与您的monoproject一起工作(我想也可以与XNA一起工作),您需要将此类添加到您的项目中(注意:您将需要更改名称空间):

// ---- AngelCode BmFont XML serializer ----------------------
// ---- By DeadlyDan @ [email protected] -------------------
// ---- There's no license restrictions, use as you will. ----
// ---- Credits to http://www.angelcode.com/ -----------------

using System;
using System.IO;
using System.Xml.Serialization;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Content;

namespace Dashboard
{
    public class BmFont {

        String fontFilePath;
        FontFile fontFile;
        Texture2D fontTexture;
        FontRenderer _fontRenderer;

        public BmFont(String fontTexture, String png, ContentManager c) {
            fontFilePath = Path.Combine(c.RootDirectory, fontTexture);
            fontFile = FontLoader.Load(fontFilePath);
            this.fontTexture = c.Load<Texture2D>(png);
            _fontRenderer = new FontRenderer(fontFile, this.fontTexture);
        }

        public void draw(String message, Vector2 pos, SpriteBatch _spriteBatch) {
            _fontRenderer.DrawText(_spriteBatch, (int)pos.X, (int)pos.Y, message);
        }

    }


    public class FontRenderer
    {

        public static FontFile Load(Stream stream)
        {
            XmlSerializer deserializer = new XmlSerializer(typeof(FontFile));
            FontFile file = (FontFile) deserializer.Deserialize(stream);
            return file;
        }

        public FontRenderer (FontFile fontFile, Texture2D fontTexture)
        {
            _fontFile = fontFile;
            _texture = fontTexture;
            _characterMap = new Dictionary<char, FontChar>();

            foreach(var fontCharacter in _fontFile.Chars)
            {
                char c = (char)fontCharacter.ID;
                _characterMap.Add(c, fontCharacter);
            }
        }

        private Dictionary<char, FontChar> _characterMap;
        private FontFile _fontFile;
        private Texture2D _texture;
        public void DrawText(SpriteBatch spriteBatch, int x, int y, string text)
        {
            int dx = x;
            int dy = y;
            foreach(char c in text)
            {
                FontChar fc;
                if(_characterMap.TryGetValue(c, out fc))
                {
                    var sourceRectangle = new Rectangle(fc.X, fc.Y, fc.Width, fc.Height);
                    var position = new Vector2(dx + fc.XOffset, dy + fc.YOffset);

                    spriteBatch.Draw(_texture, position, sourceRectangle, Color.White);
                    dx += fc.XAdvance;
                }
            }
        }
    }


    [Serializable]
    [XmlRoot ( "font" )]
    public class FontFile
    {
        [XmlElement ( "info" )]
        public FontInfo Info
        {
            get;
            set;
        }

        [XmlElement ( "common" )]
        public FontCommon Common
        {
            get;
            set;
        }

        [XmlArray ( "pages" )]
        [XmlArrayItem ( "page" )]
        public List<FontPage> Pages
        {
            get;
            set;
        }

        [XmlArray ( "chars" )]
        [XmlArrayItem ( "char" )]
        public List<FontChar> Chars
        {
            get;
            set;
        }

        [XmlArray ( "kernings" )]
        [XmlArrayItem ( "kerning" )]
        public List<FontKerning> Kernings
        {
            get;
            set;
        }
    }

    [Serializable]
    public class FontInfo
    {
        [XmlAttribute ( "face" )]
        public String Face
        {
            get;
            set;
        }

        [XmlAttribute ( "size" )]
        public Int32 Size
        {
            get;
            set;
        }

        [XmlAttribute ( "bold" )]
        public Int32 Bold
        {
            get;
            set;
        }

        [XmlAttribute ( "italic" )]
        public Int32 Italic
        {
            get;
            set;
        }

        [XmlAttribute ( "charset" )]
        public String CharSet
        {
            get;
            set;
        }

        [XmlAttribute ( "unicode" )]
        public Int32 Unicode
        {
            get;
            set;
        }

        [XmlAttribute ( "stretchH" )]
        public Int32 StretchHeight
        {
            get;
            set;
        }

        [XmlAttribute ( "smooth" )]
        public Int32 Smooth
        {
            get;
            set;
        }

        [XmlAttribute ( "aa" )]
        public Int32 SuperSampling
        {
            get;
            set;
        }

        private Rectangle _Padding;
        [XmlAttribute ( "padding" )]
        public String Padding
        {
            get
            {
                return _Padding.X + "," + _Padding.Y + "," + _Padding.Width + "," + _Padding.Height;
            }
            set
            {
                String[] padding = value.Split ( ',' );
                _Padding = new Rectangle ( Convert.ToInt32 ( padding[0] ), Convert.ToInt32 ( padding[1] ), Convert.ToInt32 ( padding[2] ), Convert.ToInt32 ( padding[3] ) );
            }
        }

        private Point _Spacing;
        [XmlAttribute ( "spacing" )]
        public String Spacing
        {
            get
            {
                return _Spacing.X + "," + _Spacing.Y;
            }
            set
            {
                String[] spacing = value.Split ( ',' );
                _Spacing = new Point ( Convert.ToInt32 ( spacing[0] ), Convert.ToInt32 ( spacing[1] ) );
            }
        }

        [XmlAttribute ( "outline" )]
        public Int32 OutLine
        {
            get;
            set;
        }
    }

    [Serializable]
    public class FontCommon
    {
        [XmlAttribute ( "lineHeight" )]
        public Int32 LineHeight
        {
            get;
            set;
        }

        [XmlAttribute ( "base" )]
        public Int32 Base
        {
            get;
            set;
        }

        [XmlAttribute ( "scaleW" )]
        public Int32 ScaleW
        {
            get;
            set;
        }

        [XmlAttribute ( "scaleH" )]
        public Int32 ScaleH
        {
            get;
            set;
        }

        [XmlAttribute ( "pages" )]
        public Int32 Pages
        {
            get;
            set;
        }

        [XmlAttribute ( "packed" )]
        public Int32 Packed
        {
            get;
            set;
        }

        [XmlAttribute ( "alphaChnl" )]
        public Int32 AlphaChannel
        {
            get;
            set;
        }

        [XmlAttribute ( "redChnl" )]
        public Int32 RedChannel
        {
            get;
            set;
        }

        [XmlAttribute ( "greenChnl" )]
        public Int32 GreenChannel
        {
            get;
            set;
        }

        [XmlAttribute ( "blueChnl" )]
        public Int32 BlueChannel
        {
            get;
            set;
        }
    }

    [Serializable]
    public class FontPage
    {
        [XmlAttribute ( "id" )]
        public Int32 ID
        {
            get;
            set;
        }

        [XmlAttribute ( "file" )]
        public String File
        {
            get;
            set;
        }
    }

    [Serializable]
    public class FontChar
    {
        [XmlAttribute ( "id" )]
        public Int32 ID
        {
            get;
            set;
        }

        [XmlAttribute ( "x" )]
        public Int32 X
        {
            get;
            set;
        }

        [XmlAttribute ( "y" )]
        public Int32 Y
        {
            get;
            set;
        }

        [XmlAttribute ( "width" )]
        public Int32 Width
        {
            get;
            set;
        }

        [XmlAttribute ( "height" )]
        public Int32 Height
        {
            get;
            set;
        }

        [XmlAttribute ( "xoffset" )]
        public Int32 XOffset
        {
            get;
            set;
        }

        [XmlAttribute ( "yoffset" )]
        public Int32 YOffset
        {
            get;
            set;
        }

        [XmlAttribute ( "xadvance" )]
        public Int32 XAdvance
        {
            get;
            set;
        }

        [XmlAttribute ( "page" )]
        public Int32 Page
        {
            get;
            set;
        }

        [XmlAttribute ( "chnl" )]
        public Int32 Channel
        {
            get;
            set;
        }
    }

    [Serializable]
    public class FontKerning
    {
        [XmlAttribute ( "first" )]
        public Int32 First
        {
            get;
            set;
        }

        [XmlAttribute ( "second" )]
        public Int32 Second
        {
            get;
            set;
        }

        [XmlAttribute ( "amount" )]
        public Int32 Amount
        {
            get;
            set;
        }
    }

    public class FontLoader
    {
        public static FontFile Load ( String filename )
        {
            XmlSerializer deserializer = new XmlSerializer ( typeof ( FontFile ) );
            TextReader textReader = new StreamReader ( filename );
            FontFile file = ( FontFile ) deserializer.Deserialize ( textReader );
            textReader.Close ( );
            return file;
        }
    }
}

我稍微修改了该类,以便以面向对象的方式实现它。这是在Game.cs主文件中使用带有自定义字体的此类的方式。

在这个例子中,我有文件time_0.pngtime.fnt由软件BmFonts生产。它们是我想使用的Avenir Next Condensed字体的结果。

public class Game1 : Game
{
    // Graphic variables used for the game to work
    GraphicsDeviceManager graphics;
    SpriteBatch spriteBatch;
    BmFont fontTime;



    public Game1 ()
    {
        graphics = new GraphicsDeviceManager (this);
        Content.RootDirectory = "Content";              
        graphics.IsFullScreen = true;       
    }

    protected override void LoadContent ()
    {
        // Create a new SpriteBatch, which can be used to draw textures.
        spriteBatch = new SpriteBatch (GraphicsDevice);
        fontTime = new BmFont ("time.fnt", "time_0.png", this.Content);
    }

    protected override void Draw (GameTime gameTime)
    {
        graphics.GraphicsDevice.Clear (Color.CornflowerBlue);



        spriteBatch.Begin();
            fontTime.draw (DateTime.Now.ToString("HH mm"), new Vector2 (100, 50)), spriteBatch);
        spriteBatch.End();
        base.Draw (gameTime);
    }



}

妳去 现在您应该一切正常,看到它对自己有用。困难的部分是字体大小,因为您需要为所需的每种字体大小生成一个文件。

虽然,这种技术为您提供了直接嵌入字体的可能性,而无需最终用户将字体安装在他的计算机上(以前崩溃)。

凯文,享受

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

在 SFML 2.4 中使用字体精灵表作为字体

来自分类Dev

使用图像作为XNA中的字体(SpriteFont?还是其他?)

来自分类Dev

Java使用PNG图像作为字体(Java.awt.graphics)

来自分类Dev

如何使用字体图标作为背景图像?

来自分类Dev

在matplotlib中使用图像作为刻度标签

来自分类Dev

在AsciiDoc中使用图像作为链接

来自分类Dev

在Java中使用图像作为背景

来自分类Dev

在matplotlib中使用图像作为刻度标签

来自分类Dev

使用C旋转BMP图像

来自分类Dev

苹果在iOS中使用哪种字体/字体大小作为占位符

来自分类Dev

在缩放的svg中使用栅格图像作为平铺背景

来自分类Dev

在uiview或uiimageview中使用图像而不是颜色作为边框

来自分类Dev

在SVG中使用WebP作为背景图像

来自分类Dev

我可以在 WPF 中使用 ViewBox 作为图像的源吗?

来自分类Dev

使用 od 命令读取 .bmp 图像

来自分类Dev

使用R,如何提取.png(或其他格式)图像文件中使用的颜色和字体?

来自分类Dev

在python 3中使用.bmp文件

来自分类Dev

将BMP图像转换为GRF格式C#/ VB.NET(在ZPL打印机中使用)

来自分类Dev

字体无法在IE,OTF字体中使用

来自分类Dev

如何在 Xcode 的 Interface Builder 中使用约束/自动布局来正确缩放图像和字体?

来自分类Dev

在OpenCv C ++中使用原始图像作为遮罩从图像中删除徽标

来自分类Dev

在 Swift/Xcode 中使用图像作为手势识别器,图像出现为零

来自分类Dev

如何在媒体查询中使用 API 返回的图像作为背景图像?

来自分类Dev

如何在媒体查询中使用 API 返回的图像作为背景图像?

来自分类Dev

在Inkscape中使用Awesome字体

来自分类Dev

Powerpoint中使用的字体数

来自分类Dev

在VSCode中使用字体

来自分类Dev

MonoGame中的字体太尖锐

来自分类Dev

MonoGame / XNA旋转图像

Related 相关文章

  1. 1

    在 SFML 2.4 中使用字体精灵表作为字体

  2. 2

    使用图像作为XNA中的字体(SpriteFont?还是其他?)

  3. 3

    Java使用PNG图像作为字体(Java.awt.graphics)

  4. 4

    如何使用字体图标作为背景图像?

  5. 5

    在matplotlib中使用图像作为刻度标签

  6. 6

    在AsciiDoc中使用图像作为链接

  7. 7

    在Java中使用图像作为背景

  8. 8

    在matplotlib中使用图像作为刻度标签

  9. 9

    使用C旋转BMP图像

  10. 10

    苹果在iOS中使用哪种字体/字体大小作为占位符

  11. 11

    在缩放的svg中使用栅格图像作为平铺背景

  12. 12

    在uiview或uiimageview中使用图像而不是颜色作为边框

  13. 13

    在SVG中使用WebP作为背景图像

  14. 14

    我可以在 WPF 中使用 ViewBox 作为图像的源吗?

  15. 15

    使用 od 命令读取 .bmp 图像

  16. 16

    使用R,如何提取.png(或其他格式)图像文件中使用的颜色和字体?

  17. 17

    在python 3中使用.bmp文件

  18. 18

    将BMP图像转换为GRF格式C#/ VB.NET(在ZPL打印机中使用)

  19. 19

    字体无法在IE,OTF字体中使用

  20. 20

    如何在 Xcode 的 Interface Builder 中使用约束/自动布局来正确缩放图像和字体?

  21. 21

    在OpenCv C ++中使用原始图像作为遮罩从图像中删除徽标

  22. 22

    在 Swift/Xcode 中使用图像作为手势识别器,图像出现为零

  23. 23

    如何在媒体查询中使用 API 返回的图像作为背景图像?

  24. 24

    如何在媒体查询中使用 API 返回的图像作为背景图像?

  25. 25

    在Inkscape中使用Awesome字体

  26. 26

    Powerpoint中使用的字体数

  27. 27

    在VSCode中使用字体

  28. 28

    MonoGame中的字体太尖锐

  29. 29

    MonoGame / XNA旋转图像

热门标签

归档