Debian Codegraph @

podofo/staging/BuildAglMaps.cs

raw
1 using System;
2 using System.Collections.Generic;
3 using System.IO;
4 using System.Text;
5
6 var mappings = new OrderedDictionary<string, AglMapping>();
7 var ligatures = new List<AglLigature>();
8 var inverseMap = new Dictionary<ushort, List<string>>();
9 var latinChars = new string[] {
10 "A",
11 "AE",
12 "Aacute",
13 "Acircumflex",
14 "Adieresis",
15 "Agrave",
16 "Aring",
17 "Atilde",
18 "B",
19 "C",
20 "Ccedilla",
21 "D",
22 "E",
23 "Eacute",
24 "Ecircumflex",
25 "Edieresis",
26 "Egrave",
27 "Eth",
28 "Euro",
29 "F",
30 "G",
31 "H",
32 "I",
33 "Iacute",
34 "Icircumflex",
35 "Idieresis",
36 "Igrave",
37 "J",
38 "K",
39 "L",
40 "Lslash",
41 "M",
42 "N",
43 "Ntilde",
44 "O",
45 "aring",
46 "asciicircum",
47 "asciitilde",
48 "asterisk",
49 "at",
50 "atilde",
51 "b",
52 "backslash",
53 "bar",
54 "braceleft",
55 "braceright",
56 "bracketleft",
57 "bracketright",
58 "breve",
59 "brokenbar",
60 "bullet",
61 "c",
62 "caron",
63 "ccedilla",
64 "cedilla",
65 "cent",
66 "circumflex",
67 "colon",
68 "comma",
69 "copyright",
70 "currency",
71 "d",
72 "dagger",
73 "daggerdbl",
74 "degree",
75 "dieresis",
76 "divide",
77 "dollar",
78 "dotaccent",
79 "dotlessi",
80 "e",
81 "eacute",
82 "ecircumflex",
83 "edieresis",
84 "egrave",
85 "eight",
86 "ellipsis",
87 "emdash",
88 "endash",
89 "equal",
90 "oe",
91 "ogonek",
92 "ograve",
93 "one",
94 "onehalf",
95 "onequarter",
96 "onesuperior",
97 "ordfeminine",
98 "ordmasculine",
99 "oslash",
100 "otilde",
101 "p",
102 "paragraph",
103 "parenleft",
104 "parenright",
105 "percent",
106 "period",
107 "periodcentered",
108 "perthousand",
109 "plus",
110 "plusminus",
111 "q",
112 "question",
113 "questiondown",
114 "quotedbl",
115 "quotedblbase",
116 "quotedblleft",
117 "quotedblright",
118 "quoteleft",
119 "quoteright",
120 "quotesinglbase",
121 "quotesingle",
122 "r",
123 "registered",
124 "ring",
125 "OE",
126 "Oacute",
127 "Ocircumflex",
128 "Odieresis",
129 "Ograve",
130 "Oslash",
131 "Otilde",
132 "P",
133 "Q",
134 "R",
135 "S",
136 "Scaron",
137 "T",
138 "Thorn",
139 "U",
140 "Uacute",
141 "Ucircumflex",
142 "Udieresis",
143 "Ugrave",
144 "V",
145 "W",
146 "X",
147 "Y",
148 "Yacute",
149 "Ydieresis",
150 "Z",
151 "Zcaron",
152 "a",
153 "aacute",
154 "acircumflex",
155 "acute",
156 "adieresis",
157 "ae",
158 "agrave",
159 "ampersand",
160 "eth",
161 "exclam",
162 "exclamdown",
163 "f",
164 "fi",
165 "five",
166 "fl",
167 "florin",
168 "four",
169 "fraction",
170 "g",
171 "germandbls",
172 "grave",
173 "greater",
174 "guillemotleft",
175 "guillemotright",
176 "guilsinglleft",
177 "guilsinglright",
178 "h",
179 "hungarumlaut",
180 "hyphen",
181 "i",
182 "iacute",
183 "icircumflex",
184 "idieresis",
185 "igrave",
186 "j",
187 "k",
188 "l",
189 "less",
190 "logicalnot",
191 "lslash",
192 "m",
193 "macron",
194 "minus",
195 "mu",
196 "multiply",
197 "n",
198 "nine",
199 "ntilde",
200 "numbersign",
201 "o",
202 "oacute",
203 "ocircumflex",
204 "odieresis",
205 "s",
206 "scaron",
207 "section",
208 "semicolon",
209 "seven",
210 "six",
211 "slash",
212 "space",
213 "sterling",
214 "t",
215 "thorn",
216 "three",
217 "threequarters",
218 "threesuperior",
219 "tilde",
220 "trademark",
221 "two",
222 "twosuperior",
223 "u",
224 "uacute",
225 "ucircumflex",
226 "udieresis",
227 "ugrave",
228 "underscore",
229 "v",
230 "w",
231 "x",
232 "y",
233 "yacute",
234 "ydieresis",
235 "yen",
236 "z",
237 "zcaron",
238 "zero",
239 };
240
241 foo(@"D:\glyphlist.txt", AglType.AdobeGlyphList);
242 foo(@"D:\aglfn.txt", AglType.AdobeGlyphListNewFonts);
243 foo(@"D:\zapfdingbats.txt", AglType.ZapfDingbatsGlyphList);
244
245 foreach (var latin in latinChars)
246 mappings[latin].Type |= AglType.LatinTextEncodings;
247
248 var builder = new StringBuilder();
249 foreach (var mapping in mappings)
250 builder.AppendLine($"s_aglList.emplace_back(\"{mapping.Key}\"_n, AglMapping{{ (AglType){((int)mapping.Value.Type).ToString()}, {mapping.Value.CodePointCount}, {mapping.Value.CodeStr}}});");
251
252 File.WriteAllText(@"D:\initagl.txt", builder.ToString());
253
254 builder.Clear();
255 foreach (var ligature in ligatures)
256 {
257 builder.Append($"s_ligatures.emplace_back(s_aglMap[\"{ligature.Name}\"], initializer_list<codepoint>{{");
258 bool first = true;
259 for (int i = 0; i < ligature.Codes.Length; i++)
260 {
261 if (first)
262 first = false;
263 else
264 builder.Append(", ");
265
266 builder.Append($"0x{ligature.Codes[i]:X4}");
267 }
268
269 builder.AppendLine(" });");
270 }
271
272 File.WriteAllText(@"D:\ligatures.txt", builder.ToString());
273
274 void foo(string filepath, AglType type)
275 {
276 using (var stream = new FileStream(filepath, FileMode.Open))
277 using (var reader = new StreamReader(stream))
278 {
279 while (!reader.EndOfStream)
280 {
281 var line = reader.ReadLine()!;
282 if (line.Length == 0 || line.StartsWith("#"))
283 continue;
284
285 var splitted = line.Split(';');
286 string codeStr;
287 string charName;
288 switch (type)
289 {
290 case AglType.AdobeGlyphList:
291 case AglType.ZapfDingbatsGlyphList:
292 {
293 codeStr = splitted[1];
294 charName = splitted[0];
295 break;
296 }
297 case AglType.AdobeGlyphListNewFonts:
298 {
299 codeStr = splitted[0];
300 charName = splitted[1];
301 break;
302 }
303 default:
304 throw new NotImplementedException();
305 }
306
307 var codes = readCodes(codeStr);
308 string codeOrIndex;
309 if (codes.Length == 1)
310 {
311 codeOrIndex = $"0x{codes[0].ToString("X4")}";
312 }
313 else
314 {
315 codeOrIndex = ligatures.Count.ToString();
316 ligatures.Add(new AglLigature() { Codes = codes, Name = charName });
317 }
318
319 if (mappings.TryGetValue(charName, out var mapping))
320 {
321 mapping.Type |= type;
322 }
323 else
324 {
325 mapping = new AglMapping() { Type = type, CodePointCount = codes.Length, CodeStr = codeOrIndex };
326 mappings.Add(charName, mapping);
327 }
328
329 if (codes.Length == 1)
330 {
331 if (inverseMap.TryGetValue(codes[0], out var names))
332 {
333 if (names.Contains(charName))
334 continue;
335
336 names.Add(charName);
337 }
338 else
339 {
340 names = [charName];
341 inverseMap.Add(codes[0], names);
342 }
343 }
344 }
345 }
346 }
347
348 ushort[] readCodes(string codeStr)
349 {
350 var splitted = codeStr.Split(' ');
351 var ret = new ushort[splitted.Length];
352 for (int i = 0; i < splitted.Length; i++)
353 ret[i] = ushort.Parse(splitted[i], System.Globalization.NumberStyles.HexNumber);
354
355 return ret;
356 }
357
358 class AglMapping
359 {
360 public AglType Type;
361 public int CodePointCount;
362 public string CodeStr = null!;
363 }
364
365 class AglLigature
366 {
367 public string Name = null!;
368 public ushort[] Codes = null!;
369 }
370
371 [Flags]
372 enum AglType
373 {
374 AdobeGlyphList = 1,
375 AdobeGlyphListNewFonts = 2,
376 ZapfDingbatsGlyphList = 4,
377 LatinTextEncodings = 8,
378 }