-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathRemapTables.cs
184 lines (151 loc) · 5.8 KB
/
RemapTables.cs
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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Globalization;
using System.Diagnostics;
namespace BFGFontTool
{
class RemapHelper
{
/// <summary>
/// All UTF32 characters that we want to support in our remap table.
/// </summary>
HashSet<int> allInterestingCharacters = new HashSet<int>();
public void AddCharactersFromFont(BMFont font)
{
allInterestingCharacters.UnionWith(from glyph in font.glyphs select glyph.id);
}
public void AddCharactersFromUTF8File(string fileName)
{
AddCharactersFromFile(fileName, Encoding.UTF8);
}
public void AddCharactersFromFile(string fileName, Encoding encoding)
{
byte[] bytes = File.ReadAllBytes(fileName);
string text = encoding.GetString(bytes);
byte[] utf32bytes = Encoding.UTF32.GetBytes(text);
Debug.Assert(utf32bytes.Length % 4 == 0);
for (int i = 0; i < utf32bytes.Length; i += 4)
{
int utf32 = utf32bytes[0];
utf32 += (int)utf32bytes[1] << 8;
utf32 += (int)utf32bytes[2] << 16;
utf32 += (int)utf32bytes[3] << 24;
allInterestingCharacters.Add(utf32);
}
}
public IDictionary<int, T> GenerateMap<T>(Func<int, T> fun)
{
return allInterestingCharacters.ToDictionary(c => c, c => fun(c));
}
public void SaveAsRemapTable<T>(string fileName, IDictionary<int, T> table)
{
FileStream fs = File.Create(fileName);
BinaryWriter bw = new BinaryWriter(fs);
const int CHARACTER_REMAP_TABLE_VERSION = 1;
//const int CHARACTER_REMAP_TABLE_MAGIC1 = (CHARACTER_REMAP_TABLE_VERSION | ('t' << 24) | ('d' << 16) | ('m' << 8));
//const int CHARACTER_REMAP_TABLE_MAGIC2 = (('c' << 24) | ('r' << 16) | ('m' << 8) | ('t')); // Character ReMap Table
ASCIIEncoding asen = new ASCIIEncoding();
byte[] ascii = asen.GetBytes("tdm1crmt");
ascii[3] = CHARACTER_REMAP_TABLE_VERSION;
bw.Write(ascii);
bw.Write((Int32)table.Count);
foreach (var kv in table)
{
bw.Write((Int32)kv.Key);
bw.Write(System.Convert.ToInt32(kv.Value));
}
bw.Close();
fs.Close();
}
static int CharToUTF32(string utf16OneChar)
{
return char.ConvertToUtf32(utf16OneChar, 0);
}
static string ToUpper(string utf16OneChar)
{
return utf16OneChar.ToUpperInvariant();
}
static string ToLower(string utf16OneChar)
{
return utf16OneChar.ToLowerInvariant();
}
static string StripDiacritics(string utf16OneChar)
{
string result = "";
//var normalization = NormalizationForm.FormKD;
var normalization = NormalizationForm.FormD;
string normalized = utf16OneChar.Normalize(normalization);
foreach (char c in normalized)
switch (CharUnicodeInfo.GetUnicodeCategory(c))
{
case UnicodeCategory.NonSpacingMark:
case UnicodeCategory.SpacingCombiningMark:
case UnicodeCategory.EnclosingMark:
//do nothing
break;
default:
result += c;
break;
}
return result;
}
static string SortOrder(string utf16OneChar)
{
return utf16OneChar.ToUpperInvariant();
}
static int Convert(Func<string, string> fun, int c)
{
string utf16OneChar = char.ConvertFromUtf32(c);
string converted = fun(utf16OneChar);
return CharToUTF32(converted);
}
public void SaveToUpperTable(string fileName)
{
var map = GenerateMap(c => Convert(ToUpper, c));
SaveAsRemapTable(fileName, map);
}
public void SaveToLowerTable(string fileName)
{
var map = GenerateMap(c => Convert(ToLower, c));
SaveAsRemapTable(fileName, map);
}
public void SaveStipDiacriticsTable(string fileName)
{
var map = GenerateMap(c => Convert(StripDiacritics, c));
SaveAsRemapTable(fileName, map);
}
class Comp : IComparer<string>
{
CultureInfo culture;
public Comp(CultureInfo culture)
{
this.culture = culture;
}
public int Compare(string x, string y)
{
return culture.CompareInfo.Compare(x, y, CompareOptions.StringSort);
}
}
public void SaveSortOrderTable(string fileName)
{
SaveSortOrderTable(fileName, System.Globalization.CultureInfo.InvariantCulture);
}
public void SaveSortOrderTable(string fileName, CultureInfo culture)
{
var utf16s = (from c in allInterestingCharacters
select new { Key = c, Value = char.ConvertFromUtf32(c) })
.OrderBy(c => c.Value, new Comp(culture));
int idx = 0;
var sortIdxs = new Dictionary<int, int>();
foreach (var kv in utf16s)
{
sortIdxs.Add(kv.Key, idx);
idx++;
}
SaveAsRemapTable(fileName, sortIdxs);
}
}
}