Load custom PNG fonts without the pipeline.

I’m trying to load my fonts without going through the pipeline or any compilation of assets I just want to load my fonts with Texture.From…
I searched through several solutions 4 days have passed and I found nothing. I found solutions like programs that create fnt files but it is still very complex. Would anyone have a simple solution
How to load a PNG image split into pieces with reference to each character?

So, you are effectivly trying to load a sprite sheet right?

So, load the whole texture in, and to render a character, you will need to know it’s position in the texture, then in the SpriteBatch.Draw() call you can give it the texture, and the source rect of the letter you are after.

So, if your sprite sheet has letters A B C D in it, lets say each char is 128x128 pixels in there, so the texture size is 512x128. So to get the letter C, you would need the source rect of (256,0,128,128)

Hope this helps.

2 Likes

I’ll try thanks for the tip

To add a bit more to the previous post - If you make your characters equally spaced apart horizontally (and vertically) - you can loop through the text (a = 0 to text.length) - you can specify the source rectangle by using a source_letter_width * character_index [-32 or -33 probably - I forget]. If it reaches the right side, you add a vertical amount (letter height) and set the source X position back to zero.
Same idea applies to the scaled destination rectangles you’re placing on screen but controlled by a separate set of variables. If the source and destination rectangles are the same size, it’s easier - scaling destination text can make it a bit more complicated.
You can get the source offset by specifying something like
int n = (int)text[a];
source_index = n-33; // 33 or whatever the starting ASCII character is for your font
source_x_offset = source_index * letter_width; // something like that…

2 Likes

Hi @Tekadon58 Welcome to the Community!

These might be interesting to take a gander over at:

https://docs.monogame.net/api/Microsoft.Xna.Framework.Content.Pipeline.Graphics.BitmapContent.html

https://docs.monogame.net/api/Microsoft.Xna.Framework.Content.Pipeline.Graphics.PixelBitmapContent-1.html

https://docs.monogame.net/api/Microsoft.Xna.Framework.Content.Pipeline.Graphics.PvrtcRgba2BitmapContent.html

More importantly:

https://docs.monogame.net/api/Microsoft.Xna.Framework.Graphics.SpriteBatch.html

Happy Coding!

As charles said a font is basically a spritesheet aka a texture and a array or list of rectangles that describe were thing in it are like characters in that case you need a list of characters that match the rectangle indexes ect. So those are the things you need spritebatch also has kerning information.

You might find this helpful as a starting point.

This goes a step beyond and takes a regular spritefont you loaded thru the pipeline tears it down into its component parts into arrays of description data that also includes the image which it tears down into a byte array.

Then instead of writing that info to disk as a texture and description file. It writes it to disk in the format of c# class file but one that not only holds the data but the methods to reconstruct it into a spritefont, As if that resulting class was also a instruction class on how to make the spritefont from the data it also held onto. The writer sticks a .cs extension on the end of the text file and viola its a c# class.

However before you write it to disk everything is in its component parts and all the steps neccessary to take apart a spritefont and put one together are obviously there.

The only part that maybe a bit confusing is when i wrote the data to a class i sort of do a naive run length compression on the png byte array so there isn’t a page full of numbers.

The result is that the class file in the link at the bottom will actually create a sprite font from nothing as the class file is the instructions itself to create a spritefont and you just use it.

For example from the above this .cs is generated in this post.

I actually use this quite a bit cause i can just drag and drop it right into a project and call the below in loadcontent which is actually slightly quicker then going thru the pipeline tool.

myfont = new SpriteFontAsClassFile_TheSpriteFont_Tahoma().LoadHardCodeSpriteFont(GraphicsDevice);