Can I load SpriteFonts without the use of Content Pipeline?

Hi,

Is there a way to load a SpriteFont without the use of content pipeline, similarly to loading Texture2D files? I do not really want to use the content pipeline at all, but I have not found any solution for SpriteFonts, and coding a custom text handler seems to be a big headache and tons of unnecessary work.

So something similar to this Texture2D loader:

public Texture2D TextureFromStreamForWP8(GraphicsDevice graphicsDevice, Stream stream)
{                 
   Texture2D result = null;

   var image = new BitmapImage();
   image.SetSource(stream);
   var bitmap = new WriteableBitmap(image);
   var pixels = SetColorsFromPixelData(bitmap.Pixels);
   Texture2D texture2D = new Texture2D(graphicsDevice,
      bitmap.PixelWidth, bitmap.PixelHeight);
   texture2D.SetData(pixels);
   result = texture2D;

   return result;
}

private Color[] SetColorsFromPixelData(int[] pixelData)
{
   var Colors = new Color[pixelData.Length];
   for (var i = 0; i < pixelData.Length; i++)
   {
      var packedColor = pixelData[i];
      Color unpackedColor = new Color();
      unpackedColor.B = (byte)(packedColor);
      unpackedColor.G = (byte)(packedColor >> 8);
      unpackedColor.R = (byte)(packedColor >> 16);
      unpackedColor.A = (byte)(packedColor >> 24);
      Colors[i] = unpackedColor;
    }
    
   return Colors;
   }
}

No, it’s way more complicated than a Texture2D.

By the way, did you know that there is a Texture2D.FromStream function tha does just what your code is doing? :slight_smile:

1 Like

Yesterday I spent another long hours to find an answer and I found how the drawtext method works, why do I need an xml and the likes… Well, it wont be a big headache to create a class that can handle text rendering from an image, so I think I will choose that. There are tools that can create an image from fonts, I will only need a container that will hold information about the x/y/width/height parameter of each character and a method that will accept a string and will draw the required portion of the image to the screen for each character in the string.

I still do not know how I will achieve the simulation of a keyboard (to give the user the ability of writing texts into controls), but that is another issue I need to solve. :slight_smile:

Back in my XNA development days, I have used the FromFile method. There is a reason why I had to use this one, though I do not really know what it is, so I will re-check that FromStream method! Thanks for pointing on it! :wink:

It’s still tons of times easier to use the Content Pipeline.

I tried it now. I got an exception “method not implemented”. It seems in 3.2.2 for windows phone it is not implemented, so that was the reason I had to use a different approach. :slight_smile:

Thats strange…
https://github.com/mono/MonoGame/blob/develop/MonoGame.Framework/Graphics/Texture2D.DirectX.cs#L172-189

I have installed Monogame through NuGet, if it matters, as the 3.2.2 installer has wrong dlls in the templates and there are issues with the orientation and with who knows what else. :slight_smile: