Skip to content

Commit

Permalink
Merge pull request #37 from Emersont1/master
Browse files Browse the repository at this point in the history
Added Functions to get glyph dimensions
  • Loading branch information
rds1983 committed Jun 15, 2020
2 parents 672d673 + b5ab1e2 commit b372c3f
Show file tree
Hide file tree
Showing 2 changed files with 98 additions and 0 deletions.
8 changes: 8 additions & 0 deletions src/DynamicSpriteFont.cs
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,14 @@ public Rectangle GetTextBounds(Vector2 position, StringBuilder text)
return new Rectangle((int)bounds.X, (int)bounds.Y, (int)(bounds.X2 - bounds.X), (int)(bounds.Y2 - bounds.Y));
}

public List<Rectangle> GetGlyphRects(Vector2 position, string text){
return _fontSystem.GetGlyphRects(position.X, position.Y, text);
}

public List<Rectangle> GetGlyphRects(Vector2 position, StringBuilder text){
return _fontSystem.GetGlyphRects(position.X, position.Y, text);
}

public void Reset(int width, int height)
{
_fontSystem.Reset(width, height);
Expand Down
90 changes: 90 additions & 0 deletions src/FontStashSharp/FontSystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -555,6 +555,96 @@ public float TextBounds(float x, float y, StringBuilder str, ref Bounds bounds)
return advance;
}

public List<Rectangle> GetGlyphRects(float x, float y, string str){
List<Rectangle> Rects = new List<Rectangle>();
if (string.IsNullOrEmpty(str)) return Rects;

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

var q = new FontGlyphSquad();
y += ascent;

float minx, maxx, miny, maxy;
minx = maxx = x;
miny = maxy = y;
float startx = x;

FontGlyph prevGlyph = null;

for (int i = 0; i < str.Length; i += char.IsSurrogatePair(str, i) ? 2 : 1)
{
var codepoint = char.ConvertToUtf32(str, i);

if (codepoint == '\n')
{
x = startx;
y += lineHeight;
prevGlyph = null;
continue;
}

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

GetQuad(glyph, prevGlyph, Spacing, ref x, ref y, ref q);

Rects.Add(new Rectangle((int)q.X0, (int)q.Y0, (int)(q.X1-q.X0), (int)(q.Y1-q.Y0)));
prevGlyph = glyph;
}

return Rects;
}

public List<Rectangle> GetGlyphRects(float x, float y, StringBuilder str){
List<Rectangle> Rects = new List<Rectangle>();
if (str == null || str.Length == 0) return Rects;

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

var q = new FontGlyphSquad();
y += ascent;

float minx, maxx, miny, maxy;
minx = maxx = x;
miny = maxy = y;
float startx = x;

FontGlyph prevGlyph = null;

for (int i = 0; i < str.Length; i += StringBuilderIsSurrogatePair(str, i) ? 2 : 1)
{
var codepoint = StringBuilderConvertToUtf32(str, i);

if (codepoint == '\n')
{
x = startx;
y += lineHeight;
prevGlyph = null;
continue;
}

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

GetQuad(glyph, prevGlyph, Spacing, ref x, ref y, ref q);

Rects.Add(new Rectangle((int)q.X0, (int)q.Y0, (int)(q.X1-q.X0), (int)(q.Y1-q.Y0)));
prevGlyph = glyph;
}

return Rects;
}

bool StringBuilderIsSurrogatePair(StringBuilder sb, int index)
{
if (index + 1 < sb.Length)
Expand Down

0 comments on commit b372c3f

Please sign in to comment.