1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
|
using System;
using System.IO;
using System.Runtime.InteropServices;
using OpenTK;
using OpenTK.Graphics.OpenGL;
using SharpFont;
namespace Ryujinx.Profiler.UI.SharpFontHelpers
{
public class FontService
{
private struct CharacterInfo
{
public float Left;
public float Right;
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 = 1024;
private const int SheetHeight = 512;
private int ScreenWidth, ScreenHeight;
private int CharacterTextureSheet;
private CharacterInfo[] characters;
public Color fontColor { get; set; } = Color.Black;
private string GetFontPath()
{
string fontFolder = Environment.GetFolderPath(Environment.SpecialFolder.Fonts);
// Only uses Arial, add more fonts here if wanted
string path = Path.Combine(fontFolder, "arial.ttf");
if (File.Exists(path))
{
return path;
}
throw new Exception($"Profiler exception. Required font Courier New or Arial not installed to {fontFolder}");
}
public void InitializeTextures()
{
// Create and init some vars
uint[] rawCharacterSheet = new uint[SheetWidth * SheetHeight];
int x;
int y;
int lineOffset;
int maxHeight;
x = y = lineOffset = maxHeight = 0;
characters = new CharacterInfo[94];
// Get font
var font = new FontFace(File.OpenRead(GetFontPath()));
// Update raw data for each character
for (int i = 0; i < 94; i++)
{
var surface = RenderSurface((char)(i + 33), font, out float xBearing, out float yBearing, out float advance);
characters[i] = UpdateTexture(surface, ref rawCharacterSheet, ref x, ref y, ref lineOffset);
characters[i].BearingX = xBearing;
characters[i].BearingY = yBearing;
characters[i].Advance = advance;
if (maxHeight < characters[i].Height)
maxHeight = (int)characters[i].Height;
}
// Fix height for characters shorter than line height
for (int i = 0; i < 94; i++)
{
characters[i].BearingX /= characters[i].Width;
characters[i].BearingY /= maxHeight;
characters[i].Advance /= characters[i].Width;
characters[i].Height /= maxHeight;
characters[i].AspectRatio = (float)characters[i].Width / maxHeight;
}
// Convert raw data into texture
CharacterTextureSheet = GL.GenTexture();
GL.BindTexture(TextureTarget.Texture2D, CharacterTextureSheet);
GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMinFilter, (int)TextureMinFilter.Linear);
GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMagFilter, (int)TextureMagFilter.Linear);
GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureWrapS, (int)TextureWrapMode.Clamp);
GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureWrapT, (int)TextureWrapMode.Clamp);
GL.TexImage2D(TextureTarget.Texture2D, 0, PixelInternalFormat.Rgba, SheetWidth, SheetHeight, 0, PixelFormat.Rgba, PixelType.UnsignedInt8888, rawCharacterSheet);
GL.BindTexture(TextureTarget.Texture2D, 0);
}
public void UpdateScreenHeight(int height)
{
ScreenHeight = height;
}
public float DrawText(string text, float x, float y, float height, bool draw = true)
{
float originalX = x;
// Skip out of bounds draw
if (y < height * -2 || y > ScreenHeight + height * 2)
{
draw = false;
}
if (draw)
{
// Use font map texture
GL.BindTexture(TextureTarget.Texture2D, CharacterTextureSheet);
// Enable blending and textures
GL.Enable(EnableCap.Texture2D);
GL.Enable(EnableCap.Blend);
GL.BlendFunc(BlendingFactor.SrcAlpha, BlendingFactor.OneMinusSrcAlpha);
// Draw all characters
GL.Begin(PrimitiveType.Triangles);
GL.Color4(fontColor);
}
for (int i = 0; i < text.Length; i++)
{
if (text[i] == ' ')
{
x += height / 4;
continue;
}
CharacterInfo charInfo = characters[text[i] - 33];
float width = (charInfo.AspectRatio * height);
x += (charInfo.BearingX * charInfo.AspectRatio) * width;
float right = x + width;
if (draw)
{
DrawChar(charInfo, x, right, y + height * (charInfo.Height - charInfo.BearingY), y - height * charInfo.BearingY);
}
x = right + charInfo.Advance * charInfo.AspectRatio + 1;
}
if (draw)
{
GL.End();
// Cleanup for caller
GL.BindTexture(TextureTarget.Texture2D, 0);
GL.Disable(EnableCap.Texture2D);
GL.Disable(EnableCap.Blend);
}
// Return width of rendered text
return x - originalX;
}
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.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.Bottom); GL.Vertex2(right, 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)
{
var glyph = font.GetGlyph(c, 64);
xBearing = glyph.HorizontalMetrics.Bearing.X;
yBearing = glyph.RenderHeight - glyph.HorizontalMetrics.Bearing.Y;
advance = glyph.HorizontalMetrics.Advance;
var surface = new Surface
{
Bits = Marshal.AllocHGlobal(glyph.RenderWidth * glyph.RenderHeight),
Width = glyph.RenderWidth,
Height = glyph.RenderHeight,
Pitch = glyph.RenderWidth
};
var stuff = (byte*)surface.Bits;
for (int i = 0; i < surface.Width * surface.Height; i++)
*stuff++ = 0;
glyph.RenderTo(surface);
return surface;
}
private CharacterInfo UpdateTexture(Surface surface, ref uint[] rawCharMap, ref int posX, ref int posY, ref int lineOffset)
{
int width = surface.Width;
int height = surface.Height;
int len = width * height;
byte[] data = new byte[len];
// Get character bitmap
Marshal.Copy(surface.Bits, data, 0, len);
// Find a slot
if (posX + width > SheetWidth)
{
posX = 0;
posY += lineOffset;
lineOffset = 0;
}
// Update lineOffset
if (lineOffset < height)
{
lineOffset = height + 1;
}
// Copy char to sheet
for (int y = 0; y < height; y++)
{
int destOffset = (y + posY) * SheetWidth + posX;
int sourceOffset = y * width;
for (int x = 0; x < width; x++)
{
rawCharMap[destOffset + x] = (uint)((0xFFFFFF << 8) | data[sourceOffset + x]);
}
}
// Generate character info
CharacterInfo charInfo = new CharacterInfo()
{
Left = (float)posX / SheetWidth,
Right = (float)(posX + width) / SheetWidth,
Top = (float)(posY - 1) / SheetHeight,
Bottom = (float)(posY + height) / SheetHeight,
Width = width,
Height = height,
};
// Update x
posX += width + 1;
// Give the memory back
Marshal.FreeHGlobal(surface.Bits);
return charInfo;
}
}
}
|