Advanced Japanese display using custom content processors
About Japanese display
Text drawing in MonoGame uses SpriteFont as well as XNA. For SpriteFont, see Displaying Alphanumeric Characters and Font Style Details, as it is the same as XNA.
If you use SpriteFont, you must specify the characters you want to use in advance. If it is only alphanumeric symbols, it can be done with almost no cost as a character specification and build time because it is about 100 characters, but when it comes to Japanese, it becomes thousands of characters, so the build time becomes extremely long. If you specify only the characters to be used, the build time can be shortened to some extent, but it is troublesome to specify the character to be used in reverse, and it becomes even more troublesome if there is a change in the character to be used.
In response to this problem, "Ninari GD"site has been customized to make Drawing Japanese in XNA more smoothly (by the way, the site-run Yuichi Ito - MSFT is one of the XNA Framework developers). This makes it easier to specify the characters to use, shorten build time, decorate characters, and more. For more information, please refer to the link.
However, the "WPF font processor" presented in the page is for XNA and cannot be used as is in MonoGame. So I'd like to port this to MonoGame.
Download the WPF Font Processor
Open the following sites:
Scroll down to two links, and click the link above to download the project that contains the sort code. The lower one is only a DLL, so you don't have to download it.
Please extract the downloaded file so that you can retrieve the contents.
Create a WPF Font Processor Project
I'd like to create a WPF font processor DLL that works with "Any CPU", but for some reason when I create a project with the "Content Pipeline Extension Library" of the XNA Framework, I can only create it in x86, so I'll create it from a regular class library here.
When you create a project, you don't need "Class1.cs", so delete it.
Next, add a reference.
Because you draw text in WPF, you need a WPF-related reference. First, check "PresentationCore" from "Framework".
Next, check "WindowsBase". Now press the OK button to confirm and open the Reference Manager again.
Select Browse on the left and click the Browse button below.
Open the following folder path:
- C:\Program Files(x86)\MSBuild\MonoGame\v3.0\Tools\
From there, add the following two DLLs:
- MonoGame.Framework.dll
- MonoGame.Framework.Content.Pipeline.dll
Make sure it is checked and click the OK button.
Review the references that have been added.
Add the source code from the "WpfFontPipeline" folder from the project you downloaded to the project you are currently editing.
The state you added.
Verify that the build configuration is "Release" and "Any CPU".
Perform a build to see if it was built successfully.
DLL is created.
Create a game project
Create a project for MonoGame. It doesn't matter what platform it is.
Place the DLL you just created in the Content folder. It doesn't matter where the DLL is, but in that case you'll need a relative or absolute path, so i'll leave it here for now.
Open Content.mgcb and open References with Content selected.
If you double-click "Content.mmcb" to not open it, start "MonoGame Pipeline" from the Start menu.
In the text box, type the file name of the DLL you just created, or the path relative to or absolute path from Content.mgcb.
After you set it up, the DLL still does not reflect, so once you save the project, exit monoGame Pipeline and reopen it.
Creates a sprite font. Select "Edit" >Add→New Item from the menu.
Select "SpriteFont Description" from among the items to add it. (Incidentally, after adding SpriteFont, I opened the SpriteFont file to make it easier to see at run time and changed the font name and size.)
If you select the SpriteFont that you added, you will see that the WPF Font Processor is available from the Processor.
Since each parameter can be set, this time I tried to set it as shown in the figure for the time being. Unfortunately, you can't change the color in the current version of MonoGame Pipeline.
Try to build. 6860 characters were built because it included up to JIS level 2. Still, the time is about nine seconds, so you can see that it's pretty fast.
Now, we're drawing using this SpriteFont, and the code is exactly the same as XNA. You can draw text by using the code introduced in "Display alphanumeric characters" as it is. Of course, Japanese is also possible.
I'd like to put only the code that I added once.
The definition part of the field. "SpriteFont" has been added.
GraphicsDeviceManager graphics;
SpriteBatch spriteBatch;
SpriteFont font;
Loading and creating font data in the LoadContent method.
protected override void LoadContent()
{
// Create a new SpriteBatch, which can be used to draw textures.
spriteBatch = new SpriteBatch(GraphicsDevice);
// TODO: use this.Content to load your game content here
// フォントデータの読み込み
font = Content.Load<SpriteFont>("Font");
}
Drawing with Japanese in the Draw method.
protected override void Draw(GameTime gameTime)
{
GraphicsDevice.Clear(Color.CornflowerBlue);
// TODO: Add your drawing code here
spriteBatch.Begin();
spriteBatch.DrawString(font, "真・簡単(かもしれない)日本語表示",
new Vector2(50, 50), Color.White);
spriteBatch.DrawString(font, @"・OpenTypeフォント対応
・処理速度の高速化
・JIS漢字追加機能
・文字装飾",
new Vector2(50, 150), Color.White);
spriteBatch.End();
base.Draw(gameTime);
}
When executed, the text is drawn. It's a little hard to understand, but the letters have borders and blue gradations inside.
It became easy to draw Japanese like this, and it became possible to draw the character with the decoration.
Finally, you can't change the color of the text decoration on monoGame Pipeline, but you can open the file directly and modify it.
Once you have saved and closed the project, open the .mccb file as a text file.
If you scroll down, there is color information in the target parameter, so please edit it directly there.
After saving the text, open monoGame Pipeline again and build it. You can see that the color has changed when you run it.