Skip to content
This repository has been archived by the owner on Aug 3, 2024. It is now read-only.

Commit

Permalink
fix crash with font loading
Browse files Browse the repository at this point in the history
  • Loading branch information
galister committed May 4, 2023
1 parent 2c37e5b commit a6e0955
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 15 deletions.
24 changes: 16 additions & 8 deletions GFX/Font.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,20 +36,20 @@ private void LoaderInit()

var err = FT_Init_FreeType(out _ftLib);
if (err != FT_Error.FT_Err_Ok)
throw new ApplicationException($"Could not load FreeType library: {err}");
throw new FontLoaderException($"Could not load FreeType library: {err}");


err = FT_New_Face(_ftLib, _path, _index, out _ftFace);
if (err != FT_Error.FT_Err_Ok)
throw new ApplicationException($"Could not load font {_font}: {err}");
throw new FontLoaderException($"Could not load font {_font}: {err}");

err = FT_Select_Charmap(_ftFace, FT_Encoding.FT_ENCODING_UNICODE);
if (err != FT_Error.FT_Err_Ok)
throw new ApplicationException($"Could not use unicode char map on {_font}: {err}");
throw new FontLoaderException($"Could not use unicode char map on {_font}: {err}");

err = FT_Set_Char_Size(_ftFace, (IntPtr)(_size << 6), (IntPtr)(_size << 6), 96, 96);
if (err != FT_Error.FT_Err_Ok)
throw new ApplicationException($"Could not set size to {_size}px for {_font}: {err}");
throw new FontLoaderException($"Could not set size to {_size}px for {_font}: {err}");
}

private void LoadGlyphIndices()
Expand Down Expand Up @@ -84,13 +84,13 @@ private unsafe void LoadGlyph(int ch)

var err = FT_Load_Glyph(_ftFace, chIdx, FT_LOAD_DEFAULT);
if (err != FT_Error.FT_Err_Ok)
throw new ApplicationException($"Could not Load_Glyph for U+{ch:X4} - {err}");
throw new FontLoaderException($"Could not Load_Glyph for U+{ch:X4} - {err}");

var ftFaceRec = (FT_FaceRec*)_ftFace;

err = FT_Get_Glyph((IntPtr)ftFaceRec->glyph, out var glyph);
if (err != FT_Error.FT_Err_Ok)
throw new ApplicationException($"Could not Get_Glyph for U+{ch:X4} - {err}");
throw new FontLoaderException($"Could not Get_Glyph for U+{ch:X4} - {err}");

FT_Glyph_To_Bitmap(ref glyph, FT_Render_Mode.FT_RENDER_MODE_NORMAL, ref _nullVector, true);

Expand Down Expand Up @@ -118,7 +118,7 @@ private unsafe void LoadGlyph(int ch)
inputFormat = GraphicsFormat.BGRA8;
break;
default:
throw new ApplicationException($"Unsupported FT_Pixel_Mode: {bitmap.pixel_mode}");
throw new FontLoaderException($"Unsupported FT_Pixel_Mode: {bitmap.pixel_mode}");
}

var gSlot = ((FT_FaceRec*)_ftFace)->glyph;
Expand Down Expand Up @@ -166,4 +166,12 @@ public class Glyph
public int BearX;
public int BearY;
public int AdvX;
}
}

public class FontLoaderException : Exception
{
public FontLoaderException(string str) : base(str)
{

}
}
21 changes: 14 additions & 7 deletions GFX/FontCollection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -82,12 +82,19 @@ private void LoadFontForCodePoint(int codepoint, int size, FontStyle style)
_codePointToFont[codepoint] = _codePointToFont[0];
return;
}

var font = new Font(parts[0], int.Parse(parts[1]), size);
lock (Lock)
_loadedFonts.Add(font);
foreach (var cp in font.GetSupportedCodePoints())
_codePointToFont.TryAdd(cp, font);

try
{
var font = new Font(parts[0], int.Parse(parts[1]), size);
lock (Lock)
_loadedFonts.Add(font);
foreach (var cp in font.GetSupportedCodePoints())
_codePointToFont.TryAdd(cp, font);
}
catch (FontLoaderException x)
{
Console.WriteLine("WARN: " + x.Message);
}

if (!_codePointToFont.ContainsKey(codepoint))
_codePointToFont[codepoint] = _codePointToFont[0];
Expand All @@ -98,4 +105,4 @@ public enum FontStyle
{
Regular,
Bold,
}
}

0 comments on commit a6e0955

Please sign in to comment.