Skip to content

Commit

Permalink
Merge branch 'szymski-master' into develop
Browse files Browse the repository at this point in the history
Add support for loading freetype fonts from memory.
Merge pull request #32
  • Loading branch information
opcon committed Mar 26, 2017
2 parents 640c725 + bf0a2fe commit 95a748a
Show file tree
Hide file tree
Showing 2 changed files with 96 additions and 7 deletions.
84 changes: 77 additions & 7 deletions QuickFont.Shared/FreeTypeFont.cs
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,37 @@ public FreeTypeFont(string fontPath, float size, FontStyle style, int superSampl
LoadFontFace(fontPath, size, fontStyle, superSampleLevels, scale);
}

/// <summary>
/// Creates a new instace of FreeTypeFont
/// </summary>
/// <param name="fontData">Contents of the font file</param>
/// <param name="size">Size of the font</param>
/// <param name="style">Style of the font</param>
/// <param name="superSampleLevels">Super sample levels</param>
/// <param name="scale">Scale</param>
/// <exception cref="ArgumentException"></exception>
public FreeTypeFont(byte[] fontData, float size, FontStyle style, int superSampleLevels = 1, float scale = 1.0f)
{
StyleFlags fontStyle = StyleFlags.None;
switch (style)
{
case FontStyle.Bold:
fontStyle = StyleFlags.Bold;
break;
case FontStyle.Italic:
fontStyle = StyleFlags.Italic;
break;
case FontStyle.Regular:
fontStyle = StyleFlags.None;
break;
default:
Debug.WriteLine("Invalid style flag chosen for FreeTypeFont: " + style);
break;
}

LoadFontFace(fontData, size, fontStyle, superSampleLevels, scale);
}

private void LoadFontFace(string fontPath, float size, StyleFlags fontStyle, int superSampleLevels, float scale)
{
// Get total number of faces in a font file
Expand Down Expand Up @@ -104,6 +135,45 @@ private void LoadFontFace(string fontPath, float size, StyleFlags fontStyle, int
_fontFace.SetCharSize(0, Size, 0, DPI);
}

private void LoadFontFace(byte[] fontData, float size, StyleFlags fontStyle, int superSampleLevels, float scale)
{
// Get total number of faces in a font file
var tempFace = _fontLibrary.NewMemoryFace(fontData, -1);
int numberOfFaces = tempFace.FaceCount;

// Dispose of the temporary face
tempFace.Dispose();
tempFace = null;

// Loop through to find the style we want
for (int i = 0; i < numberOfFaces; i++)
{
tempFace = _fontLibrary.NewMemoryFace(fontData, i);

// If we've found the style, exit loop
if (tempFace.StyleFlags == fontStyle)
break;

// Dispose temp face and keep searching
tempFace.Dispose();
tempFace = null;
}

// Use default font face if correct style not found
if (tempFace == null)
{
Debug.WriteLine("Could not find correct face style in font: " + fontStyle);
tempFace = _fontLibrary.NewMemoryFace(fontData, 0);
}

// Set the face for this instance
_fontFace = tempFace;

// Set the size
Size = size * scale * superSampleLevels;
_fontFace.SetCharSize(0, Size, 0, DPI);
}

/// <summary>Returns a string that represents the current object.</summary>
/// <returns>A string that represents the current object.</returns>
/// <filterpriority>2</filterpriority>
Expand Down Expand Up @@ -131,7 +201,7 @@ public Point DrawString(string s, Graphics graph, Brush color, int x, int y)
if (!(color is SolidBrush))
throw new ArgumentException("Brush is required to be a SolidBrush (single, solid color)", nameof(color));

var fontColor = ((SolidBrush) color).Color;
var fontColor = ((SolidBrush)color).Color;

// Load the glyph into the face's glyph slot
LoadGlyph(s[0]);
Expand All @@ -145,7 +215,7 @@ public Point DrawString(string s, Graphics graph, Brush color, int x, int y)
var bitmap = _fontFace.Glyph.Bitmap.ToGdipBitmap(fontColor);
int baseline = y + _maxHorizontalBearyingY;
graph.DrawImageUnscaled(bitmap, x, (baseline - _fontFace.Glyph.Metrics.HorizontalBearingY.Ceiling()));
return new Point(0, baseline - _fontFace.Glyph.Metrics.HorizontalBearingY.Ceiling() - 2*y);
return new Point(0, baseline - _fontFace.Glyph.Metrics.HorizontalBearingY.Ceiling() - 2 * y);
}

return Point.Empty;
Expand Down Expand Up @@ -214,11 +284,11 @@ protected virtual void Dispose(bool disposing)
_fontFace.Dispose();
_fontFace = null;
}
if (_fontLibrary != null)
{
_fontLibrary.Dispose();
_fontLibrary = null;
}
if (_fontLibrary != null)
{
_fontLibrary.Dispose();
_fontLibrary = null;
}
}
_disposedValue = true;
}
Expand Down
19 changes: 19 additions & 0 deletions QuickFont.Shared/QFont.cs
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,25 @@ public QFont(string fontPath, float size, QFontBuilderConfiguration config,
}
}

/// <summary>
/// Initialise QFont from memory
/// </summary>
/// <param name="fontData">Contents of the font file</param>
/// <param name="size">The size</param>
/// <param name="config">The configuration</param>
/// <param name="style">The style</param>
public QFont(byte[] fontData, float size, QFontBuilderConfiguration config,
FontStyle style = FontStyle.Regular)
{
float fontScale = 1f;

using (IFont font = new FreeTypeFont(fontData, size, style, config?.SuperSampleLevels ?? 1, fontScale))
{
FontName = font.ToString();
InitialiseQFont(font, config);
}
}

/// <summary>
/// Initialise QFont from a .qfont file
/// </summary>
Expand Down

0 comments on commit 95a748a

Please sign in to comment.