Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Crossplatform font loading #2530

Open
wants to merge 11 commits into
base: master
Choose a base branch
from
48 changes: 23 additions & 25 deletions sources/engine/Stride.Assets/SpriteFont/Compiler/BitmapImporter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Imaging;
using FreeImageAPI;

namespace Stride.Assets.SpriteFont.Compiler
{
Expand All @@ -102,20 +102,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 +148,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);
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(bitmapData[x, y]) &&
IsMarkerColor(bitmapData[x - 1, y]) &&
IsMarkerColor(bitmapData[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(bitmapData[x + w, y]))
{
w++;
}

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

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