-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBdfFont.cs
More file actions
209 lines (181 loc) · 6.22 KB
/
BdfFont.cs
File metadata and controls
209 lines (181 loc) · 6.22 KB
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
using System.Collections;
using System.Globalization;
using SDL;
using SixLabors.ImageSharp.PixelFormats;
namespace zview;
public unsafe class BdfFont
{
// this is probably the fourth time ive had to write a bdf reader from scratch
// why? do i keep doing this?
// its not to spec, btw
public int FontSize;
public readonly Dictionary<char, Glyph> Glyphs = [];
public struct Glyph
{
public char Character;
public byte[] Rows;
public SDL_Rect Box;
public int MoveX, MoveY;
}
public FontTextureAtlas CreateAtlas(SDL_Renderer* renderer)
{
const int w = 256;
const int h = 128;
const int padding = 1;
var surface = SDL3.SDL_CreateSurface(w, h, SDL_PixelFormat.SDL_PIXELFORMAT_RGBA8888);
var texture = new Texture
{
Image = null,
Height = w,
Width = h,
TextureHandle = SDL3.SDL_CreateTexture(renderer, surface->format,
SDL_TextureAccess.SDL_TEXTUREACCESS_STATIC, w, h),
SurfaceHandle = surface
};
var atlas = new FontTextureAtlas
{
Atlas = texture,
Font = this
};
var pixelsOut = (byte*)surface->pixels;
for (var x = 0; x < w; x++)
for (var y = 0; y < h; y++)
SetPixel(x, y, 0x00);
int cursorX = padding, cursorY = padding;
int maxHeightThisRow = 0;
foreach (var glyph in Glyphs.Values)
{
maxHeightThisRow = int.Max(glyph.Box.h, maxHeightThisRow);
cursorX += glyph.Box.w + padding;
if (cursorX + padding + glyph.Box.w >= w)
{
cursorX = padding;
cursorY += maxHeightThisRow + padding;
maxHeightThisRow = 0;
}
for (var y = 0; y < glyph.Box.h; y++)
{
var row = glyph.Rows[y];
for (var x = 0; x < glyph.Box.w; x++)
{
if ((row & (1 << (glyph.Box.w - x - 1))) != 0)
SetPixel(x + cursorX, y + cursorY, 0xFF);
}
}
atlas.Entries.Add(glyph.Character, new FontTextureAtlas.Entry
{
Glyph = glyph,
TextureRect = new SDL_Rect
{
x = cursorX,
y = cursorY,
w = glyph.Box.w,
h = glyph.Box.h,
},
});
}
void FragToUv(float x, float y, out float uvx, out float uvy)
{
uvx = x / w;
uvy = y / h;
}
void SetPixel(int x, int y, byte value)
{
if (x < 0 || x >= w || y < 0 || y >= h)
throw new IndexOutOfRangeException();
var i = (y * surface->pitch) + (x * 4);
pixelsOut[i + 0] = value;
pixelsOut[i + 1] = value;
pixelsOut[i + 2] = value;
pixelsOut[i + 3] = value;
}
var rect = new SDL_Rect
{
x = 0, y = 0,
w = w, h = h,
};
SDL3.SDL_SetTextureScaleMode(texture.TextureHandle, SDL_ScaleMode.SDL_SCALEMODE_NEAREST);
SDL3.SDL_UpdateTexture(texture.TextureHandle, &rect, surface->pixels, surface->pitch);
return atlas;
}
public static BdfFont Load(Stream stream)
{
using var read = new StreamReader(stream, leaveOpen: false);
BdfFont font = new();
Glyph? readingChar = null;
var ints = new int[8];
var writingBitmap = false;
var bitmapRowIndex = 0;
while (true)
{
var l = read.ReadLine();
if (l == null)
break;
var line = l.AsSpan();
if (line.StartsWith("COMMENT"))
continue;
if (readingChar.HasValue)
{
var glyph = readingChar.Value;
if (line.StartsWith("ENDCHAR"))
{
font.Glyphs.Add(glyph.Character, glyph);
readingChar = null;
continue;
}
if (writingBitmap)
glyph.Rows![bitmapRowIndex++] = byte.Parse(line, NumberStyles.HexNumber);
else if (TryRead(line, "ENCODING", ints) == 1)
glyph.Character = (char)ints[0];
else if (TryRead(line, "DWIDTH", ints) == 2) // we do not support SWIDTH
{
glyph.MoveX = ints[0];
glyph.MoveY = ints[1];
}
else if (TryRead(line, "BBX", ints) == 4)
{
glyph.Box.w = ints[0];
glyph.Box.h = ints[1];
glyph.Box.x = ints[2];
glyph.Box.y = ints[3];
glyph.Rows = new byte[glyph.Box.h];
}
else if (line.StartsWith("BITMAP"))
{
bitmapRowIndex = 0;
writingBitmap = true;
}
readingChar = glyph;
}
else
{
if (TryRead(line, "SIZE", ints) == 3)
{
font.FontSize = ints[0];
// dpi ignored because scalable values are also ignored :)
}
// else if (TryRead(line, "FONT_ASCENT", ints) == 1)
// font.Ascent = ints[0];
// else if (TryRead(line, "FONT_DESCENT", ints) == 1)
// font.Descent = ints[0];
else if (line.StartsWith("STARTCHAR"))
{
writingBitmap = false;
readingChar = new();
}
}
}
return font;
int TryRead(in ReadOnlySpan<char> line, in ReadOnlySpan<char> key, in int[] values)
{
if (!line.StartsWith(key))
return 0;
var i = 0;
var parts = line[key.Length..].Trim().ToString().Split(' ');
foreach (var part in parts)
if (int.TryParse(part, out var x))
values[i++] = x;
return i;
}
}
}