More about font styles
summary
It describes the details of the sprite font file and the characters that are displayed.
Operating environment
Prerequisites
Supported XNA Versions |
|
Supported Platforms |
|
Windows Required Vertex Shader Version | 2.0 |
Windows Required Pixel Shader Version | 2.0 |
Operating environment
platform |
|
substance
In a sprite font file, the following seven items can be specified.
- Font name
- Font size
- Width between characters
- With or without kerning
- Font style
- Default Characters
- Character encoding to be created
Here, the following font is used as the default font. (There is also the size of the letters, so I have been tinkering with it a little since the sprite font was created.)
Font name | Segoe UI Mono |
size | 24 |
space | 0 |
kerning | true |
style | Regular |
Font name
<FontName>MS 明朝</FontName>
You can specify a font name. The fonts that can be specified are the fonts that are installed in the development environment. Since the font image is generated at build time, it can be used without problems even in an environment where the created font is not installed. The text below is displayed as "MS Mincho".
Font name | MS Mincho |
size | 24 |
space | 0 |
kerning | true |
style | Regular |
size
<Size>36</Size>
The size of the text. It will be the size on the screen, and you can also specify a decimal place. (To be precise, it will be the size on the back buffer)
Font name | Segoe UI Mono |
size | 36 |
space | 0 |
kerning | true |
style | Regular |
Intercharacter spacing
<Spacing>12</Spacing>
You can specify the width between each character. The number will be the size on the screen. (To be precise, it will be the size on the back buffer)
Font name | Segoe UI Mono |
size | 24 |
space | 12 |
kerning | true |
style | Regular |
kerning
<UseKerning>false</UseKerning>
Balance the letter spacing. For more information, please refer to the link below.
By default, this is true, which means that the space between characters is automatically adjusted to make it easier to see the space between characters. If set to false, the space between the characters will not be adjusted and will appear jammed.
Font name | Segoe UI Mono |
size | 24 |
space | 0 |
kerning | false |
style | Regular |
style
<Style>Bold</Style>
You can make the letters bold or italic. The parameters that can be specified are "Regular", "Bold", and "Italic". If you want to specify more than one, separate them with a comma (,).
Font name | Segoe UI Mono |
size | 24 |
space | 0 |
kerning | true |
style | Bold and Italic |
Default Characters
<DefaultCharacter>◆</DefaultCharacter>
In sprite fonts, you can specify the character encoding that can be used in "CharacterRegions", but if you try to display other characters, an exception occurs at runtime in the XNA Framework (up to 3.1).
However, if you specify this "DefaultCharacter", you can display it as an alternate character even if an unexpected character is entered. I think it will be especially effective for user-input text.
In the display example below, the program is trying to display ""Default Character", but since this sprite font only allows alphanumeric symbols, it is replaced with "◆" specified in DefaultCharacter.
By the way, only one Unicode character can be specified here, and characters that are not specified in CharacterRegions can also be specified.
Character encoding
<CharacterRegions>
<CharacterRegion>
<Start> </Start>
<End>~</End>
</CharacterRegion>
</CharacterRegions>
Specifies the characters that can be displayed in a code defined in Unicode. Characters between the codes specified by "Start" and "End" can be used.
If you specify a large number, you can use characters such as Japanese, but be careful because it takes a long time to build and the file size of the finished font image becomes quite large.
Since multiple nodes of "CharacterRegion" can be defined, it can be used when you want to specify the character code by skipping.
Unicode tables (32~127)
(Numbers in rows ×16 + numbers in columns) will be the code.
0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | A | B | C | D | E | F | ||
2 | ! | " | # | $ | % | & | ' | ( | ) | * | + | , | - | . | / | ||
3 | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | : | ; | < | = | > | ? | |
4 | @ | A | B | C | D | E | F | G | H | I | J | K | L | M | N | O | |
5 | P | Q | R | S | T | U | V | W | X | Y | Z | [ | \ | ] | ^ | _ | |
6 | ` | a | b | c | d | e | f | g | h | i | j | k | l | m | n | o | |
7 | p | q | r | s | t | u | v | w | x | y | z | { | } | ~ | |
All Codes
using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
#if WINDOWS_PHONE
using Microsoft.Xna.Framework.Input.Touch;
#endif
namespace FontStyle
{
<summary>
ゲームメインクラス
</summary>
public class GameMain : Microsoft.Xna.Framework.Game
{
<summary>
グラフィックデバイス管理クラス
</summary>
private GraphicsDeviceManager graphics = null;
<summary>
スプライトのバッチ化クラス
</summary>
private SpriteBatch spriteBatch = null;
<summary>
デフォルトフォント
</summary>
private SpriteFont defaultFont = null;
<summary>
フォント名変更フォント
</summary>
private SpriteFont fontNameFont = null;
<summary>
サイズ変更フォント
</summary>
private SpriteFont sizeFont = null;
<summary>
文字間変更フォント
</summary>
private SpriteFont spacingFont = null;
<summary>
太字フォント
</summary>
private SpriteFont boldFont = null;
<summary>
イタリックフォント
</summary>
private SpriteFont italicFont = null;
<summary>
カーニングOFFフォント
</summary>
private SpriteFont kerningOffFont = null;
<summary>
デフォルト文字フォント
</summary>
private SpriteFont defaultCharacterFont = null;
<summary>
GameMain コンストラクタ
</summary>
public GameMain()
{
// グラフィックデバイス管理クラスの作成
this.graphics = new GraphicsDeviceManager(this);
// ゲームコンテンツのルートディレクトリを設定
this.Content.RootDirectory = "Content";
#if WINDOWS_PHONE
// Windows Phone のデフォルトのフレームレートは 30 FPS
this.TargetElapsedTime = TimeSpan.FromTicks(333333);
// バックバッファサイズの設定
this.graphics.PreferredBackBufferWidth = 480;
this.graphics.PreferredBackBufferHeight = 800;
// フルスクリーン表示
this.graphics.IsFullScreen = true;
#endif
}
<summary>
ゲームが始まる前の初期化処理を行うメソッド
グラフィック以外のデータの読み込み、コンポーネントの初期化を行う
</summary>
protected override void Initialize()
{
// TODO: ここに初期化ロジックを書いてください
// コンポーネントの初期化などを行います
base.Initialize();
}
<summary>
ゲームが始まるときに一回だけ呼ばれ
すべてのゲームコンテンツを読み込みます
</summary>
protected override void LoadContent()
{
// テクスチャーを描画するためのスプライトバッチクラスを作成します
this.spriteBatch = new SpriteBatch(this.GraphicsDevice);
// フォントをコンテンツパイプラインから読み込む
this.defaultFont = this.Content.Load<SpriteFont>("Default");
this.fontNameFont = this.Content.Load<SpriteFont>("FontName");
this.sizeFont = this.Content.Load<SpriteFont>("Size");
this.spacingFont = this.Content.Load<SpriteFont>("Spacing");
this.boldFont = this.Content.Load<SpriteFont>("Bold");
this.italicFont = this.Content.Load<SpriteFont>("Italic");
this.kerningOffFont = this.Content.Load<SpriteFont>("KerningOff");
this.defaultCharacterFont = this.Content.Load<SpriteFont>("DefaultCharacter");
}
<summary>
ゲームが終了するときに一回だけ呼ばれ
すべてのゲームコンテンツをアンロードします
</summary>
protected override void UnloadContent()
{
// TODO: ContentManager で管理されていないコンテンツを
// ここでアンロードしてください
}
<summary>
描画以外のデータ更新等の処理を行うメソッド
主に入力処理、衝突判定などの物理計算、オーディオの再生など
</summary>
<param name="gameTime">このメソッドが呼ばれたときのゲーム時間</param>
protected override void Update(GameTime gameTime)
{
// Xbox 360 コントローラ、Windows Phone の BACK ボタンを押したときに
// ゲームを終了させます
if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
{
this.Exit();
}
// TODO: ここに更新処理を記述してください
// 登録された GameComponent を更新する
base.Update(gameTime);
}
<summary>
描画処理を行うメソッド
</summary>
<param name="gameTime">このメソッドが呼ばれたときのゲーム時間</param>
protected override void Draw(GameTime gameTime)
{
// 画面を指定した色でクリアします
this.GraphicsDevice.Clear(Color.CornflowerBlue);
// スプライトの描画準備
this.spriteBatch.Begin();
// デフォルト
this.spriteBatch.DrawString(this.defaultFont, "Sample Text -Default-",
new Vector2(20.0f, 20.0f), Color.White);
// カーニング OFF
this.spriteBatch.DrawString(this.kerningOffFont, "Sample Text -Kerning OFF-",
new Vector2(20.0f, 70.0f), Color.White);
// フォント名
this.spriteBatch.DrawString(this.fontNameFont, "Sample Text -FontName-",
new Vector2(20.0f, 120.0f), Color.White);
// フォントサイズ
this.spriteBatch.DrawString(this.sizeFont, "Sample Text -Size-",
new Vector2(20.0f, 170.0f), Color.White);
// 文字間
this.spriteBatch.DrawString(this.spacingFont, "Sample Text -Spacing-",
new Vector2(20.0f, 220.0f), Color.White);
// 太字
this.spriteBatch.DrawString(this.boldFont, "Sample Text -Bold-",
new Vector2(20.0f, 270.0f), Color.White);
// イタリック
this.spriteBatch.DrawString(this.italicFont, "Sample Text -Italic-",
new Vector2(20.0f, 320.0f), Color.White);
// デフォルト文字
this.spriteBatch.DrawString(this.defaultCharacterFont, "Sample Text -デフォルト文字-",
new Vector2(20.0f, 370.0f), Color.White);
// スプライトの一括描画
this.spriteBatch.End();
// 登録された DrawableGameComponent を描画する
base.Draw(gameTime);
}
}
}