-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
337 lines (280 loc) · 13.7 KB
/
Program.cs
File metadata and controls
337 lines (280 loc) · 13.7 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
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
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
using SkiaSharp;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Xml.Linq;
// ── Per-thread reusable resources ────────────────────────────────────────────
sealed class ThreadResources : IDisposable
{
public readonly SKFont FontHi;
public readonly SKFont FontFinal;
public readonly SKPaint Paint;
SKBitmap? _pairBmp, _baseBmp;
int _scratchW, _scratchH;
public ThreadResources(SKTypeface tf, int renderSize, int fontSize)
{
FontHi = new SKFont(tf, renderSize)
{ Subpixel = true, LinearMetrics = true, Edging = SKFontEdging.Antialias };
FontFinal = new SKFont(tf, fontSize)
{ Subpixel = true, LinearMetrics = true, Edging = SKFontEdging.Antialias };
Paint = new SKPaint { Color = SKColors.White, IsAntialias = true };
}
public (SKBitmap pair, SKBitmap baseB) GetScratch(int w, int h)
{
if (_pairBmp is null || _scratchW < w || _scratchH < h)
{
_pairBmp?.Dispose();
_baseBmp?.Dispose();
_scratchW = Math.Max(w, _scratchW);
_scratchH = Math.Max(h, _scratchH);
_pairBmp = new SKBitmap(_scratchW, _scratchH, SKColorType.Rgba8888, SKAlphaType.Premul);
_baseBmp = new SKBitmap(_scratchW, _scratchH, SKColorType.Rgba8888, SKAlphaType.Premul);
}
return (_pairBmp, _baseBmp!);
}
public void Dispose()
{
FontHi.Dispose(); FontFinal.Dispose(); Paint.Dispose();
_pairBmp?.Dispose(); _baseBmp?.Dispose();
}
}
// ── Per-glyph result ──────────────────────────────────────────────────────────
struct GlyphSlot
{
public SKBitmap? Bitmap;
public int BmpW, BmpH, Advance, XOffset, YOffset;
public bool Valid;
}
class GlyphInfo
{
public int id, x, y, w, h, xoffset, yoffset, xadvance;
}
class Program
{
const int ThaiBase = 0x0E01; // ใช้ "ก" เป็นตัวฐาน
static void Main()
{
string fontPath = "font.ttf";
int fontSize = 64;
int supersample = 4;
int renderSize = fontSize * supersample;
int atlasSize = 16384;
int padding = 4;
int threadCount = Environment.ProcessorCount;
Console.WriteLine($"Threads : {threadCount}");
Console.WriteLine("Loading font...");
using var typeface = SKTypeface.FromFile(fontPath)
?? throw new Exception("โหลดฟอนต์ไม่ได้");
using var fontMetric = new SKFont(typeface, fontSize) { LinearMetrics = true };
SKFontMetrics m = fontMetric.Metrics;
int stdAscent = (int)Math.Ceiling(-m.Ascent);
int stdDescent = (int)Math.Ceiling(m.Descent);
int vPad = (int)(fontSize * 0.20f);
int hPad = (int)(fontSize * 0.08f);
List<int> charset = BuildCharset();
int total = charset.Count;
Console.WriteLine($"Glyph count : {total}");
Console.WriteLine($"Atlas size : {atlasSize}x{atlasSize} px");
Console.WriteLine($"Supersample : {supersample}x ({renderSize}px→{fontSize}px)");
Console.WriteLine("Rendering (parallel, max speed)...");
var slots = new GlyphSlot[total];
int done = 0;
var tlPool = new ThreadLocal<ThreadResources>(
() => new ThreadResources(typeface, renderSize, fontSize),
trackAllValues: true);
var sw = System.Diagnostics.Stopwatch.StartNew();
Parallel.For(0, total,
new ParallelOptions { MaxDegreeOfParallelism = threadCount },
i =>
{
int code = charset[i];
var res = tlPool.Value!;
string s = char.ConvertFromUtf32(code);
bool isCombining = IsThaiCombining(code);
string measureStr = isCombining ? char.ConvertFromUtf32(ThaiBase) + s : s;
// ── advance ───────────────────────────────────────────────────
float pairAdv = res.FontFinal.MeasureText(measureStr);
float baseAdv = isCombining ? res.FontFinal.MeasureText(char.ConvertFromUtf32(ThaiBase)) : 0f;
int advance = isCombining
? Math.Max(0, (int)Math.Ceiling(pairAdv - baseAdv))
: (int)Math.Ceiling(pairAdv);
if (advance <= 0 && !isCombining) advance = 1;
// ── bounds — Dynamic Bounding Box (แก้บัคสระโดนตัด) ────────────
res.FontFinal.MeasureText(measureStr, out SKRect bounds);
int minLeft = (int)Math.Floor(bounds.Left);
int maxRight = (int)Math.Ceiling(bounds.Right);
int minTop = (int)Math.Floor(bounds.Top);
int maxBottom = (int)Math.Ceiling(bounds.Bottom);
// ขยายกล่อง X ถ้าตัวอักษรล้ำไปด้านซ้าย
int minX = Math.Min(0, minLeft);
int maxX = Math.Max(advance, maxRight);
int drawX = hPad - minX;
int bmpW = (maxX - minX) + hPad * 2;
if (bmpW <= 0) bmpW = 1;
// ขยายกล่อง Y ถ้าสระสูง/ต่ำทะลุ Ascent หรือ Descent มาตรฐาน
int actualAscent = Math.Max(stdAscent, -minTop);
int actualDescent = Math.Max(stdDescent, maxBottom);
int bmpH = actualAscent + actualDescent + vPad * 2;
int baselineY = actualAscent + vPad;
int bmpWHi = bmpW * supersample;
int bmpHHi = bmpH * supersample;
float drawXHi = drawX * supersample;
float drawYHi = baselineY * supersample;
using var hiRes = RenderHiRes(res, measureStr, s, isCombining,
bmpWHi, bmpHHi, drawXHi, drawYHi);
var info = new SKImageInfo(bmpW, bmpH, SKColorType.Rgba8888, SKAlphaType.Premul);
var final = hiRes.Resize(info, new SKSamplingOptions(SKFilterMode.Linear, SKMipmapMode.Linear));
if (final is null) return;
// คำนวณ Offset ชดเชยพิกัดที่ดึงหลบเข้ามา ให้เอนจินวาดลงไปตรงจุดเดิมเป๊ะ
int xOffset = isCombining ? -(drawX + (int)Math.Ceiling(baseAdv)) : -drawX;
int yOffset = -baselineY;
slots[i] = new GlyphSlot
{
Bitmap = final,
BmpW = bmpW,
BmpH = bmpH,
Advance = advance,
XOffset = xOffset,
YOffset = yOffset,
Valid = true,
};
int current = Interlocked.Increment(ref done);
if (current % 50 == 0 || current == total)
Console.WriteLine($" Rendering : {current}/{total} ({current * 100 / total}%)");
});
sw.Stop();
Console.WriteLine($"Render : {sw.ElapsedMilliseconds} ms");
foreach (var r in tlPool.Values) r.Dispose();
tlPool.Dispose();
// ── Pack atlas ────────────────────────────────────────────────────────
Console.WriteLine("Packing atlas...");
sw.Restart();
using var atlasBmp = new SKBitmap(atlasSize, atlasSize, SKColorType.Rgba8888, SKAlphaType.Premul);
using var atlasCanvas = new SKCanvas(atlasBmp);
atlasCanvas.Clear(SKColors.Transparent);
using var paintAtlas = new SKPaint { IsAntialias = false };
var glyphs = new List<GlyphInfo>(total);
int cx = padding, cy = padding, rowH = 0;
for (int i = 0; i < total; i++)
{
ref GlyphSlot s = ref slots[i];
if (!s.Valid) continue;
if (cx + s.BmpW + padding >= atlasSize)
{ cx = padding; cy += rowH + padding; rowH = 0; }
if (cy + s.BmpH + padding >= atlasSize)
{
s.Bitmap!.Dispose(); continue;
}
atlasCanvas.DrawBitmap(s.Bitmap!, cx, cy, paintAtlas);
s.Bitmap!.Dispose();
glyphs.Add(new GlyphInfo
{
id = charset[i],
x = cx,
y = cy,
w = s.BmpW,
h = s.BmpH,
xoffset = s.XOffset,
yoffset = s.YOffset,
xadvance = s.Advance,
});
rowH = Math.Max(rowH, s.BmpH);
cx += s.BmpW + padding;
}
sw.Stop();
Console.WriteLine($"Pack : {sw.ElapsedMilliseconds} ms");
// ── Save PNG ──────────────────────────────────────────────────────────
Console.WriteLine("Saving font.png...");
using var img = SKImage.FromBitmap(atlasBmp);
using var imgData = img.Encode(SKEncodedImageFormat.Png, 100);
using (var fs = File.OpenWrite("font.png")) imgData.SaveTo(fs);
SaveXml("font.xml", glyphs, atlasSize, atlasSize, fontSize);
Console.WriteLine($"DONE ✔ ({glyphs.Count} glyphs) atlas={atlasSize}x{atlasSize}");
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
static SKBitmap RenderHiRes(ThreadResources res,
string measureStr, string s, bool isCombining,
int w, int h, float drawX, float drawY)
{
var bmp = new SKBitmap(w, h, SKColorType.Rgba8888, SKAlphaType.Premul);
if (!isCombining)
{
using var canvas = new SKCanvas(bmp);
canvas.Clear(SKColors.Transparent);
DrawString(canvas, res.FontHi, res.Paint, s, drawX, drawY);
return bmp;
}
var (pairBmp, baseBmp) = res.GetScratch(w, h);
using (var c = new SKCanvas(pairBmp))
{
c.Clear(SKColors.Transparent);
DrawString(c, res.FontHi, res.Paint, measureStr, drawX, drawY);
}
using (var c = new SKCanvas(baseBmp))
{
c.Clear(SKColors.Transparent);
DrawString(c, res.FontHi, res.Paint, char.ConvertFromUtf32(ThaiBase), drawX, drawY);
}
EraseBase(pairBmp, baseBmp, bmp, w, h);
return bmp;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
static void DrawString(SKCanvas canvas, SKFont font, SKPaint paint, string text, float x, float y)
{
using var blob = SKTextBlob.Create(text, font);
if (blob is not null) canvas.DrawText(blob, x, y, paint);
}
static void EraseBase(SKBitmap pair, SKBitmap baseB, SKBitmap dst, int w, int h)
{
var spanP = MemoryMarshal.Cast<byte, uint>(pair.GetPixelSpan());
var spanB = MemoryMarshal.Cast<byte, uint>(baseB.GetPixelSpan());
var spanD = MemoryMarshal.Cast<byte, uint>(dst.GetPixelSpan());
int pairStride = pair.Width;
int baseStride = baseB.Width;
int dstStride = dst.Width;
for (int y = 0; y < h; y++)
for (int x = 0; x < w; x++)
{
uint p = spanP[y * pairStride + x];
uint b = spanB[y * baseStride + x];
byte pA = (byte)(p >> 24);
byte bA = (byte)(b >> 24);
byte outA = pA > bA ? (byte)(pA - bA) : (byte)0;
spanD[y * dstStride + x] = (uint)outA | ((uint)outA << 8) | ((uint)outA << 16) | ((uint)outA << 24);
}
}
static List<int> BuildCharset()
{
var set = new HashSet<int>();
for (int i = 32; i <= 126; i++) set.Add(i);
for (int i = 0x0E01; i <= 0x0E2E; i++) set.Add(i);
for (int i = 0x0E30; i <= 0x0E3A; i++) set.Add(i);
for (int i = 0x0E40; i <= 0x0E5B; i++) set.Add(i);
foreach (char c in "€£¥©®™°•…—–\u201C\u201D\u2018\u2019«»±×÷≈≠≤≥") set.Add(c);
return set.ToList();
}
static bool IsThaiCombining(int code) =>
code == 0x0E31 ||
(code >= 0x0E34 && code <= 0x0E3A) ||
(code >= 0x0E47 && code <= 0x0E4E);
static void SaveXml(string path, List<GlyphInfo> glyphs, int texW, int texH, int fontSize)
{
var root = new XElement("font",
new XElement("info", new XAttribute("size", fontSize)),
new XElement("common", new XAttribute("scaleW", texW),
new XAttribute("scaleH", texH)),
new XElement("chars",
glyphs.Select(g => new XElement("char",
new XAttribute("id", g.id),
new XAttribute("x", g.x),
new XAttribute("y", g.y),
new XAttribute("width", g.w),
new XAttribute("height", g.h),
new XAttribute("xoffset", g.xoffset),
new XAttribute("yoffset", g.yoffset),
new XAttribute("xadvance", g.xadvance)
))
)
);
root.Save(path);
}
}