Added unit measurements to bitmap font.

This commit is contained in:
Harrison Deng 2020-07-04 17:29:20 -05:00
parent 5a8e7a9ccb
commit 30d7221a85

View File

@ -17,27 +17,28 @@ namespace SlatedGameToolkit.Framework.Graphics.Text
public class BitmapFont : IDisposable
{
private GLContext context;
LRUCache<char, int> glyphIndices;
private LRUCache<char, int> glyphIndices;
private LRUCache<char, CharacterMetrics> metricsCache;
private LRUCache<(int, int), int> kerningCache;
FontTexture[] textures;
StbTrueType.stbtt_fontinfo info;
private int spaceAdvance;
private float scale;
private float pixelHeight;
public float PixelHeight
{
get
{
return pixelHeight;
return scale * (ascent - descent);
}
set
{
this.scale = StbTrueType.stbtt_ScaleForPixelHeight(info, value);
this.pixelHeight = value;
this.scale = value / (ascent - descent);
}
}
private Dictionary<(char, float), int> glyphTexLocations = new Dictionary<(char, float), int>();
private LRUCache<char, CharacterMetrics> metricsCache;
private int ascent, descent, lineGap;
public float PixelsPerUnitHeight { get; set; }
public float PixelsPerUnitWidth { get; set; }
private int drawingTo;
private bool disposed;
@ -49,6 +50,7 @@ namespace SlatedGameToolkit.Framework.Graphics.Text
this.glyphIndices = new LRUCache<char, int>(cacheSize);
this.metricsCache = new LRUCache<char, CharacterMetrics>(cacheSize);
this.kerningCache = new LRUCache<(int, int), int>(cacheSize);
this.textures = new FontTexture[textures];
this.context = glContext ?? WindowContextsManager.CurrentGL;
@ -64,6 +66,11 @@ namespace SlatedGameToolkit.Framework.Graphics.Text
this.ascent = ascent;
this.descent = descent;
this.lineGap = lineGap;
int vWidth, vHeight, vX, vY;
context.GetViewport(out vX, out vY, out vWidth, out vHeight);
this.PixelsPerUnitWidth = vWidth;
this.PixelsPerUnitHeight = vHeight;
}
public BitmapFont(string path, GLContext glContext = null, int cacheSize = 1024, int textures = 2, uint textureSizes = 512) : this(File.ReadAllBytes(path), glContext, cacheSize, textures, textureSizes) {
@ -107,17 +114,14 @@ namespace SlatedGameToolkit.Framework.Graphics.Text
char[] chars = text.ToCharArray();
for (int i = 0; i < chars.Length; i++) {
char c = chars[i];
int glyphIndex = glyphIndices.ComputeIfNonExistent(c, (p) => StbTrueType.stbtt_FindGlyphIndex(info, p));
CharacterMetrics metrics = metricsCache.ComputeIfNonExistent(c, (p) => new CharacterMetrics(glyphIndex, info));
int viewWidth, viewHeight, vX, vY;
context.GetViewport(out vX, out vY, out viewWidth, out viewHeight);
if (c == ' ') {
currentPoint += (scale * spaceAdvance) / viewWidth;
currentPoint += (scale * spaceAdvance) / PixelsPerUnitWidth;
} else if (c == '\n') {
currentPoint = x;
baseLine -= ((ascent - descent + lineGap) * scale) / viewHeight;
baseLine -= ((ascent - descent + lineGap) * scale) / PixelsPerUnitHeight;
} else {
int glyphIndex = glyphIndices.ComputeIfNonExistent(c, (p) => StbTrueType.stbtt_FindGlyphIndex(info, p));
CharacterMetrics metrics = metricsCache.ComputeIfNonExistent(c, (p) => new CharacterMetrics(glyphIndex, info));
//Check if glyph is loaded, if not, throw exception.
if (!glyphTexLocations.ContainsKey((c, scale)) || !textures[glyphTexLocations[(c, scale)]].ContainsChar(c, scale)) {
throw new FrameworkUsageException(string.Format("Character \'{0}\' was not prepared and is missing!", c));
@ -128,15 +132,15 @@ namespace SlatedGameToolkit.Framework.Graphics.Text
RectangleF transformedTexBounds = texBounds.MultiplyBy(1f/texture.Length);
transformedTexBounds.Y += transformedTexBounds.Height;
transformedTexBounds.Height *= -1;
RectangleF glyphBounds = new RectangleF(currentPoint, baseLine, texBounds.Width / viewWidth, texBounds.Height / viewHeight);
glyphBounds.X += (metrics.leftSideBearing * scale) / viewWidth;
glyphBounds.Y += (metrics.vertOffset * scale) / viewHeight;
RectangleF glyphBounds = new RectangleF(currentPoint, baseLine, texBounds.Width / PixelsPerUnitWidth, texBounds.Height / PixelsPerUnitHeight);
glyphBounds.X += (metrics.leftSideBearing * scale) / PixelsPerUnitWidth;
glyphBounds.Y += (metrics.vertOffset * scale) / PixelsPerUnitHeight;
batch.Draw(new RectangleMesh(glyphBounds, transformedTexBounds, texture, color));
currentPoint += (metrics.advanceWidth * scale) / viewWidth;
currentPoint += (metrics.advanceWidth * scale) / PixelsPerUnitWidth;
if (i + 1 < chars.Length) {
int nextGlyph = glyphIndices.ComputeIfNonExistent(chars[i + 1], (p) => StbTrueType.stbtt_FindGlyphIndex(info, p));
currentPoint += (StbTrueType.stbtt_GetGlyphKernAdvance(info, glyphIndex, nextGlyph) * scale) / viewWidth;
int nextGlyphIndex = glyphIndices.ComputeIfNonExistent(chars[i + 1], (p) => StbTrueType.stbtt_FindGlyphIndex(info, p));
currentPoint += (kerningCache.ComputeIfNonExistent((glyphIndex, nextGlyphIndex), (gIndices) => StbTrueType.stbtt_GetGlyphKernAdvance(info, gIndices.Item1, gIndices.Item2)) * scale) / PixelsPerUnitWidth;
}
}
}