From b342ac7ea1e994683d631d04ab997164a5469525 Mon Sep 17 00:00:00 2001 From: Garfield Date: Mon, 16 Nov 2026 12:58:57 +0800 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20=E5=A4=9A=E4=B9=A6=E6=BA=90?= =?UTF-8?q?=E5=80=9F=E6=AE=B5=E8=AF=84=E7=99=BB=E8=AE=B0=EF=BC=9B=E8=BF=81?= =?UTF-8?q?=E5=85=A5=E4=B8=8E=E5=BE=AE=E4=BF=A1=E8=AF=BB=E4=B9=A6=E8=80=A6?= =?UTF-8?q?=E5=90=88=E7=9A=84=E6=A0=B8=E5=BF=83=E6=B5=8B=E8=AF=95=E4=B8=8E?= =?UTF-8?q?=E5=AD=97=E4=BD=93=E6=9E=84=E5=BB=BA=E8=84=9A=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> --- tools/build_tadu_font.ps1 | 146 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 146 insertions(+) create mode 100644 tools/build_tadu_font.ps1 diff --git a/tools/build_tadu_font.ps1 b/tools/build_tadu_font.ps1 new file mode 100644 index 0000000..65cb33c --- /dev/null +++ b/tools/build_tadu_font.ps1 @@ -0,0 +1,146 @@ +param( + [string]$FontPath = "$env:WINDIR\Fonts\msyh.ttc", + [Parameter(Mandatory = $true)][string]$OutputPath +) + +$ErrorActionPreference = "Stop" +# Generate Unicode fingerprints on Windows; no font bytes or outlines are exported. +Add-Type -AssemblyName PresentationCore +Add-Type -TypeDefinition @' +using System; +using System.IO; +using System.Text; +using System.Collections.Generic; +using System.Security.Cryptography; +using System.Windows.Media; + +public class TaduFontIndex { + readonly byte[] data; + readonly Dictionary tables = new Dictionary(); + readonly int em, metrics; + readonly bool longLoca; + + int U16(int p) { return data[p] * 256 + data[p + 1]; } + int I16(int p) { int n = U16(p); return n >= 32768 ? n - 65536 : n; } + int U32(int p) { return checked(U16(p) * 65536 + U16(p + 2)); } + int Scaled(int n) { return (int)Math.Floor(n * 2048.0 / em + 0.5); } + + TaduFontIndex(string path) { + data = File.ReadAllBytes(path); + int face = Encoding.ASCII.GetString(data, 0, 4) == "ttcf" ? U32(12) : 0; + for (int i = 0; i < U16(face + 4); i++) { + int p = face + 12 + i * 16; + tables[Encoding.ASCII.GetString(data, p, 4)] = U32(p + 8); + } + em = U16(tables["head"] + 18); + longLoca = I16(tables["head"] + 50) == 1; + metrics = U16(tables["hhea"] + 34); + } + + int[] Axis(byte[] flags, ref int pos, int shortBit, int sameBit) { + var values = new int[flags.Length]; + int current = 0; + for (int i = 0; i < flags.Length; i++) { + if ((flags[i] & shortBit) != 0) { + int delta = data[pos++]; + current += (flags[i] & sameBit) != 0 ? delta : -delta; + } else if ((flags[i] & sameBit) == 0) { + current += I16(pos); + pos += 2; + } + values[i] = current; + } + return values; + } + + string Fingerprint(int gid) { + int loc = tables["loca"] + gid * (longLoca ? 4 : 2); + int from = longLoca ? U32(loc) : U16(loc) * 2; + int to = longLoca ? U32(loc + 4) : U16(loc + 2) * 2; + if (from == to) return null; + int pos = tables["glyf"] + from; + int contours = I16(pos); + if (contours <= 0) return null; + pos += 10; + var ends = new int[contours]; + for (int i = 0; i < contours; i++, pos += 2) ends[i] = U16(pos); + int count = ends[contours - 1] + 1; + pos += U16(pos) + 2; + var flags = new byte[count]; + for (int i = 0; i < count; i++) { + byte flag = data[pos++]; + flags[i] = flag; + if ((flag & 8) != 0) { + int repeats = data[pos++]; + while (repeats-- > 0) flags[++i] = flag; + } + } + int[] xs = Axis(flags, ref pos, 2, 16); + int[] ys = Axis(flags, ref pos, 4, 32); + var paths = new List(); + int begin = 0; + foreach (int end in ends) { + var points = new List(); + for (int i = begin; i <= end; i++) { + points.Add((flags[i] & 1) + "," + Scaled(xs[i]) + "," + Scaled(ys[i])); + } + string best = null; + for (int i = 0; i < points.Count; i++) { + var rotated = new List(); + for (int j = 0; j < points.Count; j++) rotated.Add(points[(i + j) % points.Count]); + string value = String.Join(";", rotated); + if (best == null || String.CompareOrdinal(value, best) < 0) best = value; + } + paths.Add(best); + begin = end + 1; + } + paths.Sort(StringComparer.Ordinal); + string shape = Scaled(U16(tables["hmtx"] + Math.Min(gid, metrics - 1) * 4)) + + "|" + String.Join("|", paths); + using (var sha = SHA256.Create()) { + return BitConverter.ToString(sha.ComputeHash(Encoding.ASCII.GetBytes(shape))).Replace("-", "").ToLowerInvariant(); + } + } + + static int Canonical(int cp) { + if ((cp >= 0xf900 && cp <= 0xfaff) || (cp >= 0x2f00 && cp <= 0x2fdf)) { + string text = Char.ConvertFromUtf32(cp).Normalize(NormalizationForm.FormKC); + if (text.Length == 1) return text[0]; + } + return cp; + } + + public static void Build(string path, string output) { + var font = new TaduFontIndex(path); + var face = new GlyphTypeface(new Uri(Path.GetFullPath(path))); + var rows = new SortedDictionary(StringComparer.Ordinal); + var ambiguous = new HashSet(); + foreach (var pair in face.CharacterToGlyphMap) { + if (pair.Key < 32) continue; + string hash = font.Fingerprint(pair.Value); + if (hash == null || ambiguous.Contains(hash)) continue; + int cp = Canonical(pair.Key); + int old; + if (rows.TryGetValue(hash, out old) && old != cp) { + rows.Remove(hash); + ambiguous.Add(hash); + } else rows[hash] = cp; + } + // MTF1 + sorted records: full SHA-256 (32 bytes), Unicode (3 bytes, big-endian). + using (var file = File.Create(output)) { + file.Write(Encoding.ASCII.GetBytes("MTF1"), 0, 4); + foreach (var row in rows) { + for (int i = 0; i < row.Key.Length; i += 2) { + file.WriteByte(Convert.ToByte(row.Key.Substring(i, 2), 16)); + } + file.WriteByte((byte)(row.Value >> 16)); + file.WriteByte((byte)(row.Value >> 8)); + file.WriteByte((byte)row.Value); + } + } + Console.WriteLine("fingerprints={0}; ambiguous={1}", rows.Count, ambiguous.Count); + } +} +'@ -ReferencedAssemblies PresentationCore, WindowsBase + +[TaduFontIndex]::Build($FontPath, $OutputPath)