Skip to content

Commit

Permalink
feat: Crossplatform font loading using freetype
Browse files Browse the repository at this point in the history
  • Loading branch information
Jklawreszuk committed Dec 19, 2024
1 parent b59fbc5 commit 4bc2a75
Show file tree
Hide file tree
Showing 14 changed files with 282 additions and 358 deletions.
47 changes: 23 additions & 24 deletions sources/engine/Stride.Assets/SpriteFont/Compiler/BitmapImporter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Imaging;
using FreeImageAPI;

namespace Stride.Assets.SpriteFont.Compiler
{
Expand All @@ -102,20 +103,20 @@ internal class BitmapImporter : IFontImporter
public void Import(SpriteFontAsset options, List<char> characters)
{
// Load the source bitmap.
Bitmap bitmap;
FreeImageBitmap bitmap;

try
{
// TODO Check if source can be used as is from here
bitmap = new Bitmap(options.FontSource.GetFontPath());
bitmap = FreeImageBitmap.FromFile(options.FontSource.GetFontPath());
}
catch
{
throw new FontNotFoundException(options.FontSource.GetFontPath());
}

// Convert to our desired pixel format.
bitmap = BitmapUtils.ChangePixelFormat(bitmap, PixelFormat.Format32bppArgb);
bitmap = bitmap.ConvertTo32Bits();

// What characters are included in this font?
int characterIndex = 0;
Expand Down Expand Up @@ -148,34 +149,32 @@ public void Import(SpriteFontAsset options, List<char> characters)

// Seems to be the same as this one: http://www.tonicodes.net/blog/creating-custom-fonts-with-outline-for-wp7-and-xna/
// Searches a 2D bitmap for characters that are surrounded by a marker pink color.
static IEnumerable<Rectangle> FindGlyphs(Bitmap bitmap)
static IEnumerable<Rectangle> FindGlyphs(FreeImageBitmap bitmap)
{
using (var bitmapData = new BitmapUtils.PixelAccessor(bitmap, ImageLockMode.ReadOnly))
var bitmapData = new BitmapUtils.PixelAccessor(bitmap, ImageLockMode.ReadOnly);
for (int y = 1; y < bitmap.Height; y++)
{
for (int y = 1; y < bitmap.Height; y++)
for (int x = 1; x < bitmap.Width; x++)
{
for (int x = 1; x < bitmap.Width; x++)
// Look for the top left corner of a character (a pixel that is not pink, but was pink immediately to the left and above it)
if (!IsMarkerColor(bitmap.GetPixel(x, y)) &&
IsMarkerColor(bitmap.GetPixel(x - 1, y)) &&
IsMarkerColor(bitmap.GetPixel(x, y - 1)))
{
// Look for the top left corner of a character (a pixel that is not pink, but was pink immediately to the left and above it)
if (!IsMarkerColor(bitmapData[x, y]) &&
IsMarkerColor(bitmapData[x - 1, y]) &&
IsMarkerColor(bitmapData[x, y - 1]))
{
// Measure the size of this character.
int w = 1, h = 1;

while ((x + w < bitmap.Width) && !IsMarkerColor(bitmapData[x + w, y]))
{
w++;
}
// Measure the size of this character.
int w = 1, h = 1;

while ((y + h < bitmap.Height) && !IsMarkerColor(bitmapData[x, y + h]))
{
h++;
}
while ((x + w < bitmap.Width) && !IsMarkerColor(bitmap.GetPixel(x + w, y)))
{
w++;
}

yield return new Rectangle(x, y, w, h);
while ((y + h < bitmap.Height) && !IsMarkerColor(bitmap.GetPixel(x, y + h)))
{
h++;
}

yield return new Rectangle(x, y, w, h);
}
}
}
Expand Down
Loading

0 comments on commit 4bc2a75

Please sign in to comment.