Font service cleanup

This commit is contained in:
Andy Adshead 2019-01-27 22:54:07 +00:00
commit b3c768be2b

View file

@ -11,9 +11,19 @@ namespace Ryujinx.Profiler.UI.SharpFontHelpers
{ {
private struct CharacterInfo private struct CharacterInfo
{ {
public float left, right, bottom, top; public float Left;
public float height, aspectRatio, xBearing, yBearing, advance; public float Right;
public int width; public float Top;
public float Bottom;
public int Width;
public float Height;
public float AspectRatio;
public float BearingX;
public float BearingY;
public float Advance;
} }
private const int SheetWidth = 256; private const int SheetWidth = 256;
@ -28,7 +38,11 @@ namespace Ryujinx.Profiler.UI.SharpFontHelpers
{ {
// Create and init some vars // Create and init some vars
uint[] rawCharacterSheet = new uint[SheetWidth * SheetHeight]; uint[] rawCharacterSheet = new uint[SheetWidth * SheetHeight];
int x, y, lineOffset, maxHeight; int x;
int y;
int lineOffset;
int maxHeight;
x = y = lineOffset = maxHeight = 0; x = y = lineOffset = maxHeight = 0;
characters = new CharacterInfo[94]; characters = new CharacterInfo[94];
@ -39,26 +53,25 @@ namespace Ryujinx.Profiler.UI.SharpFontHelpers
// Update raw data for each character // Update raw data for each character
for (int i = 0; i < 94; i++) for (int i = 0; i < 94; i++)
{ {
float xBearing, yBearing, advance; var surface = RenderSurface((char)(i + 33), font, out var xBearing, out var yBearing, out var advance);
var surface = RenderSurface((char)(i + 33), font, out xBearing, out yBearing, out advance);
characters[i] = UpdateTexture(surface, ref rawCharacterSheet, ref x, ref y, ref lineOffset); characters[i] = UpdateTexture(surface, ref rawCharacterSheet, ref x, ref y, ref lineOffset);
characters[i].xBearing = xBearing; characters[i].BearingX = xBearing;
characters[i].yBearing = yBearing; characters[i].BearingY = yBearing;
characters[i].advance = advance; characters[i].Advance = advance;
if (maxHeight < characters[i].height) if (maxHeight < characters[i].Height)
maxHeight = (int)characters[i].height; maxHeight = (int)characters[i].Height;
} }
// Fix height for characters shorter than line height // Fix height for characters shorter than line height
for (int i = 0; i < 94; i++) for (int i = 0; i < 94; i++)
{ {
characters[i].xBearing /= characters[i].width; characters[i].BearingX /= characters[i].Width;
characters[i].yBearing /= maxHeight; characters[i].BearingY /= maxHeight;
characters[i].advance /= characters[i].width; characters[i].Advance /= characters[i].Width;
characters[i].height /= maxHeight; characters[i].Height /= maxHeight;
characters[i].aspectRatio = (float)characters[i].width / maxHeight; characters[i].AspectRatio = (float)characters[i].Width / maxHeight;
} }
// Convert raw data into texture // Convert raw data into texture
@ -114,14 +127,14 @@ namespace Ryujinx.Profiler.UI.SharpFontHelpers
} }
CharacterInfo charInfo = characters[text[i] - 33]; CharacterInfo charInfo = characters[text[i] - 33];
float width = (charInfo.aspectRatio * height); float width = (charInfo.AspectRatio * height);
x += (charInfo.xBearing * charInfo.aspectRatio) * width; x += (charInfo.BearingX * charInfo.AspectRatio) * width;
float right = x + width; float right = x + width;
if (draw) if (draw)
{ {
DrawChar(charInfo, x, right, y + height * (charInfo.height - charInfo.yBearing), y - height * charInfo.yBearing); DrawChar(charInfo, x, right, y + height * (charInfo.Height - charInfo.BearingY), y - height * charInfo.BearingY);
} }
x = right + charInfo.advance * charInfo.aspectRatio; x = right + charInfo.Advance * charInfo.AspectRatio;
} }
if (draw) if (draw)
@ -140,13 +153,13 @@ namespace Ryujinx.Profiler.UI.SharpFontHelpers
private void DrawChar(CharacterInfo charInfo, float left, float right, float top, float bottom) private void DrawChar(CharacterInfo charInfo, float left, float right, float top, float bottom)
{ {
GL.TexCoord2(charInfo.left, charInfo.bottom); GL.Vertex2(left, bottom); GL.TexCoord2(charInfo.Left, charInfo.Bottom); GL.Vertex2(left, bottom);
GL.TexCoord2(charInfo.left, charInfo.top); GL.Vertex2(left, top); GL.TexCoord2(charInfo.Left, charInfo.Top); GL.Vertex2(left, top);
GL.TexCoord2(charInfo.right, charInfo.top); GL.Vertex2(right, top); GL.TexCoord2(charInfo.Right, charInfo.Top); GL.Vertex2(right, top);
GL.TexCoord2(charInfo.right, charInfo.top); GL.Vertex2(right, top); GL.TexCoord2(charInfo.Right, charInfo.Top); GL.Vertex2(right, top);
GL.TexCoord2(charInfo.right, charInfo.bottom); GL.Vertex2(right, bottom); GL.TexCoord2(charInfo.Right, charInfo.Bottom); GL.Vertex2(right, bottom);
GL.TexCoord2(charInfo.left, charInfo.bottom); GL.Vertex2(left, bottom); GL.TexCoord2(charInfo.Left, charInfo.Bottom); GL.Vertex2(left, bottom);
} }
public unsafe Surface RenderSurface(char c, FontFace font, out float xBearing, out float yBearing, out float advance) public unsafe Surface RenderSurface(char c, FontFace font, out float xBearing, out float yBearing, out float advance)
@ -202,6 +215,7 @@ namespace Ryujinx.Profiler.UI.SharpFontHelpers
{ {
int destOffset = (y + posY) * SheetWidth + posX; int destOffset = (y + posY) * SheetWidth + posX;
int sourceOffset = y * width; int sourceOffset = y * width;
for (int x = 0; x < width; x++) for (int x = 0; x < width; x++)
{ {
rawCharMap[destOffset + x] = (uint)((0xFFFFFF << 8) | data[sourceOffset + x]); rawCharMap[destOffset + x] = (uint)((0xFFFFFF << 8) | data[sourceOffset + x]);
@ -211,12 +225,12 @@ namespace Ryujinx.Profiler.UI.SharpFontHelpers
// Generate character info // Generate character info
CharacterInfo charInfo = new CharacterInfo() CharacterInfo charInfo = new CharacterInfo()
{ {
left = (float)posX / SheetWidth, Left = (float)posX / SheetWidth,
right = (float)(posX + width) / SheetWidth, Right = (float)(posX + width) / SheetWidth,
top = (float)(posY - 1) / SheetHeight, Top = (float)(posY - 1) / SheetHeight,
bottom = (float)(posY + height) / SheetHeight, Bottom = (float)(posY + height) / SheetHeight,
width = width, Width = width,
height = height, Height = height,
}; };
// Update x // Update x