Skip to content

Commit

Permalink
Fixes #34
Browse files Browse the repository at this point in the history
  • Loading branch information
Roman Shapiro committed May 9, 2020
1 parent 15bcc2d commit 672d673
Showing 1 changed file with 30 additions and 68 deletions.
98 changes: 30 additions & 68 deletions src/FontStashSharp/FontSystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ private Dictionary<int, FontGlyph> GetGlyphsCollection(int size)
return result;
}

private void PreDraw(SpriteBatch batch, string str, out Dictionary<int, FontGlyph> glyphs, out float ascent, out float lineHeight)
private void PreDraw(string str, out Dictionary<int, FontGlyph> glyphs, out float ascent, out float lineHeight)
{
glyphs = GetGlyphsCollection(FontSize);

Expand All @@ -128,7 +128,7 @@ private void PreDraw(SpriteBatch batch, string str, out Dictionary<int, FontGlyp
{
var codepoint = char.ConvertToUtf32(str, i);

var glyph = GetGlyph(batch.GraphicsDevice, glyphs, codepoint);
var glyph = GetGlyph(null, glyphs, codepoint);
if (glyph == null)
{
continue;
Expand All @@ -146,7 +146,7 @@ public float DrawText(SpriteBatch batch, float x, float y, string str, Color col

Dictionary<int, FontGlyph> glyphs;
float ascent, lineHeight;
PreDraw(batch, str, out glyphs, out ascent, out lineHeight);
PreDraw(str, out glyphs, out ascent, out lineHeight);

float originX = 0.0f;
float originY = 0.0f;
Expand Down Expand Up @@ -213,7 +213,7 @@ public float DrawText(SpriteBatch batch, float x, float y, string str, Color[] g

Dictionary<int, FontGlyph> glyphs;
float ascent, lineHeight;
PreDraw(batch, str, out glyphs, out ascent, out lineHeight);
PreDraw(str, out glyphs, out ascent, out lineHeight);

float originX = 0.0f;
float originY = 0.0f;
Expand Down Expand Up @@ -278,7 +278,7 @@ public float DrawText(SpriteBatch batch, float x, float y, string str, Color[] g
return x;
}

private void PreDraw(SpriteBatch batch, StringBuilder str, out Dictionary<int, FontGlyph> glyphs, out float ascent, out float lineHeight)
private void PreDraw(StringBuilder str, out Dictionary<int, FontGlyph> glyphs, out float ascent, out float lineHeight)
{
glyphs = GetGlyphsCollection(FontSize);

Expand All @@ -289,7 +289,7 @@ private void PreDraw(SpriteBatch batch, StringBuilder str, out Dictionary<int, F
{
var codepoint = StringBuilderConvertToUtf32(str, i);

var glyph = GetGlyph(batch.GraphicsDevice, glyphs, codepoint);
var glyph = GetGlyph(null, glyphs, codepoint);
if (glyph == null)
{
continue;
Expand All @@ -307,7 +307,7 @@ public float DrawText(SpriteBatch batch, float x, float y, StringBuilder str, Co

Dictionary<int, FontGlyph> glyphs;
float ascent, lineHeight;
PreDraw(batch, str, out glyphs, out ascent, out lineHeight);
PreDraw(str, out glyphs, out ascent, out lineHeight);

float originX = 0.0f;
float originY = 0.0f;
Expand Down Expand Up @@ -374,7 +374,7 @@ public float DrawText(SpriteBatch batch, float x, float y, StringBuilder str, Co

Dictionary<int, FontGlyph> glyphs;
float ascent, lineHeight;
PreDraw(batch, str, out glyphs, out ascent, out lineHeight);
PreDraw(str, out glyphs, out ascent, out lineHeight);

float originX = 0.0f;
float originY = 0.0f;
Expand Down Expand Up @@ -443,25 +443,9 @@ public float TextBounds(float x, float y, string str, ref Bounds bounds)
{
if (string.IsNullOrEmpty(str)) return 0.0f;

var glyphs = GetGlyphsCollection(FontSize);

// Determine ascent and lineHeight from first character
float ascent = 0, lineHeight = 0;
for (int i = 0; i < str.Length; i += char.IsSurrogatePair(str, i) ? 2 : 1)
{
var codepoint = char.ConvertToUtf32(str, i);

var glyph = GetGlyph(null, glyphs, codepoint);
if (glyph == null)
{
continue;
}

ascent = glyph.Font.Ascent;
lineHeight = glyph.Font.LineHeight;
break;
}

Dictionary<int, FontGlyph> glyphs;
float ascent, lineHeight;
PreDraw(str, out glyphs, out ascent, out lineHeight);

var q = new FontGlyphSquad();
y += ascent;
Expand Down Expand Up @@ -492,17 +476,14 @@ public float TextBounds(float x, float y, string str, ref Bounds bounds)
}

GetQuad(glyph, prevGlyph, Spacing, ref x, ref y, ref q);
if (!glyph.IsEmpty)
{
if (q.X0 < minx)
minx = q.X0;
if (x > maxx)
maxx = x;
if (q.Y0 < miny)
miny = q.Y0;
if (q.Y1 > maxy)
maxy = q.Y1;
}
if (q.X0 < minx)
minx = q.X0;
if (x > maxx)
maxx = x;
if (q.Y0 < miny)
miny = q.Y0;
if (q.Y1 > maxy)
maxy = q.Y1;

prevGlyph = glyph;
}
Expand All @@ -520,25 +501,9 @@ public float TextBounds(float x, float y, StringBuilder str, ref Bounds bounds)
{
if (str == null || str.Length == 0) return 0.0f;

var glyphs = GetGlyphsCollection(FontSize);

// Determine ascent and lineHeight from first character
float ascent = 0, lineHeight = 0;
for (int i = 0; i < str.Length; i += StringBuilderIsSurrogatePair(str, i) ? 2 : 1)
{
var codepoint = StringBuilderConvertToUtf32(str, i);

var glyph = GetGlyph(null, glyphs, codepoint);
if (glyph == null)
{
continue;
}

ascent = glyph.Font.Ascent;
lineHeight = glyph.Font.LineHeight;
break;
}

Dictionary<int, FontGlyph> glyphs;
float ascent, lineHeight;
PreDraw(str, out glyphs, out ascent, out lineHeight);

var q = new FontGlyphSquad();
y += ascent;
Expand Down Expand Up @@ -569,17 +534,14 @@ public float TextBounds(float x, float y, StringBuilder str, ref Bounds bounds)
}

GetQuad(glyph, prevGlyph, Spacing, ref x, ref y, ref q);
if (!glyph.IsEmpty)
{
if (q.X0 < minx)
minx = q.X0;
if (x > maxx)
maxx = x;
if (q.Y0 < miny)
miny = q.Y0;
if (q.Y1 > maxy)
maxy = q.Y1;
}
if (q.X0 < minx)
minx = q.X0;
if (x > maxx)
maxx = x;
if (q.Y0 < miny)
miny = q.Y0;
if (q.Y1 > maxy)
maxy = q.Y1;

prevGlyph = glyph;
}
Expand Down

0 comments on commit 672d673

Please sign in to comment.