Skip to content

Commit

Permalink
Fixes #18
Browse files Browse the repository at this point in the history
  • Loading branch information
rds1983 committed Jan 4, 2020
1 parent b85c44a commit aa43c86
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 14 deletions.
23 changes: 15 additions & 8 deletions samples/SpriteFontPlus.Samples.DynamicSpriteFont/Game1.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public class Game1 : Game
private int _fontIdJapanese, _fontIdEmojis;
private Texture2D _white;
private bool _drawBackground = false;
private bool _wasSpaceDown;
private bool _wasSpaceDown, _wasEnterDown;

public Game1()
{
Expand Down Expand Up @@ -60,13 +60,20 @@ protected override void Update(GameTime gameTime)
var state = Keyboard.GetState();

var isSpaceDown = state.IsKeyDown(Keys.Space);

if (isSpaceDown && !_wasSpaceDown)
{
_drawBackground = !_drawBackground;
}

_wasSpaceDown = isSpaceDown;

var isEnterDown = state.IsKeyDown(Keys.Enter);
if (isEnterDown && !_wasEnterDown)
{
_font.UseKernings = !_font.UseKernings;
}

_wasEnterDown = isEnterDown;
}

private void DrawString(string text, int y, Color color)
Expand Down Expand Up @@ -111,25 +118,25 @@ protected override void Draw(GameTime gameTime)
DrawString("Sævör grét áðan því úlpan var ónýt, P: Pchnąć w tę łódź jeża lub osiem skrzyń fig", 90);

_font.Size = 26;
DrawString("Příliš žluťoučký kůň úpěl ďábelské kódy, R: В чащах юга жил-был цитрус? Да, но фальшивый экземпляр! ёъ.", 120);
DrawString("Příliš žluťoučký kůň úpěl ďábelské kódy,\nR: В чащах юга жил-был цитрус?\nДа, но фальшивый экземпляр! ёъ.", 120);

_font.Size = 28;
DrawString("kilómetros y frío, añoraba,\nP: vôo à noite, F: Les naïfs ægithales\nhâtifs pondant à Noël où", 150);
DrawString("kilómetros y frío, añoraba,\nP: vôo à noite, F: Les naïfs ægithales\nhâtifs pondant à Noël où", 210);

_font.FontId = _fontIdJapanese;
_font.Size = 30;
DrawString("いろはにほへど", 250);
DrawString("いろはにほへど", 310);

_font.FontId = _fontIdEmojis;
_font.Size = 32;
DrawString("🙌📦👏🔥👍😻😂🎉💻😍🚀😁🙈🇧🇪👩😉🍻🎶🏆👀👉👶💕😎😱🌌🌻🍺🏀👇👯💁💝💩😃😅🙏🚄🇫🌧🌾🍀🍁🍓🍕🎾🏈", 280, Color.Gold);
DrawString("🙌📦👏🔥👍😻😂🎉💻😍🚀😁🙈🇧🇪👩😉🍻🎶🏆👀👉👶💕😎😱🌌🌻🍺🏀👇👯💁💝💩😃😅🙏🚄🇫🌧🌾🍀🍁🍓🍕🎾🏈", 340, Color.Gold);

_font.FontId = _font.DefaultFontId;
_font.Size = 26;
DrawString("Texture:", 320);
DrawString("Texture:", 380);

var texture = _font.Textures.First();
_spriteBatch.Draw(texture, new Vector2(0, 350), Color.White);
_spriteBatch.Draw(texture, new Vector2(0, 410), Color.White);

_spriteBatch.End();

Expand Down
13 changes: 13 additions & 0 deletions src/SpriteFontPlus/DynamicSpriteFont.cs
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,19 @@ public int DefaultFontId
}
}

public bool UseKernings
{
get
{
return _fontSystem.UseKernings;
}

set
{
_fontSystem.UseKernings = value;
}
}

public event EventHandler CurrentAtlasFull
{
add
Expand Down
17 changes: 17 additions & 0 deletions src/SpriteFontPlus/FontStashSharp/Font.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ internal class Font
public float Ascender;
public float Descender;
public float LineHeight;
private readonly Dictionary<ulong, int> _kernings = new Dictionary<ulong, int>();

public Dictionary<ulong, FontGlyph> Glyphs
{
Expand Down Expand Up @@ -44,5 +45,21 @@ public void SetGlyph(int codePoint, int size, int blur, FontGlyph glyph)
var key = BuildKey(codePoint, size, blur);
_glyphs[key] = glyph;
}

public int GetKerning(int glyphIndex1, int glyphIndex2)
{
ulong key = (uint)glyphIndex1 << 32;
key |= (uint)glyphIndex2;

int result;
if (_kernings.TryGetValue(key, out result))
{
return result;
}
result = stbtt_GetGlyphKernAdvance(_font, glyphIndex1, glyphIndex2);
_kernings[key] = result;

return result;
}
}
}
9 changes: 8 additions & 1 deletion src/SpriteFontPlus/FontStashSharp/FontSystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ internal unsafe class FontSystem
public float BlurValue;
public float Spacing;
public Vector2 Scale;
public bool UseKernings = true;

public FontAtlas CurrentAtlas
{
Expand Down Expand Up @@ -155,6 +156,7 @@ public float DrawText(SpriteBatch batch, float x, float y, StringSegment str, fl
{
originX = 0.0f;
originY += lineHeight;
prevGlyphIndex = -1;
continue;
}

Expand Down Expand Up @@ -228,6 +230,7 @@ public float TextBounds(float x, float y, StringSegment str, ref Bounds bounds)
{
x = startx;
y += lineHeight;
prevGlyphIndex = -1;
continue;
}

Expand Down Expand Up @@ -451,7 +454,11 @@ private void GetQuad(Font font, int prevGlyphIndex, FontGlyph glyph, float scale
{
if (prevGlyphIndex != -1)
{
var adv = font._font.fons__tt_getGlyphKernAdvance(prevGlyphIndex, glyph.Index) * scale;
float adv = 0;
if (UseKernings)
{
adv = font.GetKerning(prevGlyphIndex, glyph.Index) * scale;
}
x += (int)(adv + spacing + 0.5f);
}

Expand Down
5 changes: 0 additions & 5 deletions src/SpriteFontPlus/StbTrueTypeExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,5 @@ public static void fons__tt_renderGlyphBitmap(this stbtt_fontinfo font, byte* ou
{
stbtt_MakeGlyphBitmap(font, output, (int)(outWidth), (int)(outHeight), (int)(outStride), (float)(scaleX), (float)(scaleY), (int)(glyph));
}

public static int fons__tt_getGlyphKernAdvance(this stbtt_fontinfo font, int glyph1, int glyph2)
{
return (int)(stbtt_GetGlyphKernAdvance(font, (int)(glyph1), (int)(glyph2)));
}
}
}

0 comments on commit aa43c86

Please sign in to comment.