-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathGlyphInfo.test.ts
More file actions
622 lines (532 loc) · 19.2 KB
/
Copy pathGlyphInfo.test.ts
File metadata and controls
622 lines (532 loc) · 19.2 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
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
import { describe, it, expect, beforeAll, afterAll } from "vitest";
import { GlyphInfo } from "./GlyphInfo.js";
import type { GlyphCategory } from "./types.js";
import { defaultResources } from "./resources.js";
let db: GlyphInfo;
beforeAll(() => {
db = new GlyphInfo(defaultResources);
});
afterAll(() => {
db.close();
});
describe("getGlyph", () => {
it("returns full data for dollar sign", () => {
const result = db.getGlyph(0x24);
expect(result).toEqual({
codepoint: 0x24,
name: "dollar",
category: "Symbol",
subCategory: "Currency",
script: null,
production: null,
altNames: null,
});
});
it("returns data for LATIN CAPITAL LETTER A", () => {
const result = db.getGlyph(0x41);
expect(result).not.toBeNull();
expect(result!.name).toBe("A");
expect(result!.category).toBe("Letter");
expect(result!.script).toBe("latin");
});
it("returns null for codepoint not in Glyph.xml", () => {
expect(db.getGlyph(0xfffe)).toBeNull();
});
it("returns data for space (U+0020)", () => {
const result = db.getGlyph(0x20);
expect(result).not.toBeNull();
expect(result!.name).toBe("space");
expect(result!.category).toBe("Separator");
expect(result!.subCategory).toBe("Space");
});
it("returns data with altNames field", () => {
// bar (U+007C) has altNames="verticalbar"
const result = db.getGlyph(0x7c);
expect(result).not.toBeNull();
expect(result!.name).toBe("bar");
expect(result!.altNames).toBe("verticalbar");
});
it("returns data with production field", () => {
// nbspace (U+00A0) has production="uni00A0"
const result = db.getGlyph(0xa0);
expect(result).not.toBeNull();
expect(result!.production).toBe("uni00A0");
});
it("returns null for negative codepoint", () => {
expect(db.getGlyph(-1)).toBeNull();
});
it("returns null for codepoint beyond BMP", () => {
expect(db.getGlyph(0x10000)).toBeNull();
});
it("returns null for surrogate codepoint", () => {
expect(db.getGlyph(0xd800)).toBeNull();
});
it("returns data for digit zero", () => {
const result = db.getGlyph(0x30);
expect(result).not.toBeNull();
expect(result!.name).toBe("zero");
expect(result!.category).toBe("Number");
expect(result!.subCategory).toBe("Decimal Digit");
});
});
describe("getGlyphName", () => {
it("returns name for dollar sign", () => {
expect(db.getGlyphName(0x24)).toBe("dollar");
});
it("returns null for unknown codepoint", () => {
expect(db.getGlyphName(0xfffe)).toBeNull();
});
it("returns name for lowercase letters", () => {
expect(db.getGlyphName(0x61)).toBe("a");
expect(db.getGlyphName(0x7a)).toBe("z");
});
it("returns name for punctuation", () => {
expect(db.getGlyphName(0x2e)).toBe("period");
expect(db.getGlyphName(0x2c)).toBe("comma");
});
});
describe("resolveGlyphName", () => {
it("maps legacy aliases from the glyph database", () => {
expect(db.resolveGlyphName("afii10072")).toBe("zhe-cy");
expect(db.resolveGlyphName("afii10081")).toBe("pe-cy");
});
it("uses the assigned codepoint when the authored name is noncanonical", () => {
expect(db.resolveGlyphName("custom-zhe", 0x0436)).toBe("zhe-cy");
});
it("maps production names and preserves dot suffixes", () => {
expect(db.resolveGlyphName("uni01C9.sc")).toBe("lj.sc");
});
it("preserves unknown authored names", () => {
expect(db.resolveGlyphName("custom.unencoded")).toBe("custom.unencoded");
});
});
describe("getAllGlyph", () => {
it("returns many entries", () => {
const all = db.getAllGlyph();
expect(all.length).toBeGreaterThan(100);
});
it("every entry has required fields", () => {
const all = db.getAllGlyph();
for (const g of all) {
expect(g.codepoint).toBeTypeOf("number");
expect(g.name).toBeTypeOf("string");
expect(g.name.length).toBeGreaterThan(0);
expect(g.category).toBeTypeOf("string");
expect(g.category.length).toBeGreaterThan(0);
}
});
it("contains no duplicate codepoints", () => {
const all = db.getAllGlyph();
const codepoints = all.map((g) => g.codepoint);
const uniqueCodepoints = new Set(codepoints);
expect(uniqueCodepoints.size).toBe(codepoints.length);
});
it("codepoints are all non-negative integers", () => {
const all = db.getAllGlyph();
for (const g of all) {
expect(g.codepoint).toBeGreaterThanOrEqual(0);
expect(Number.isInteger(g.codepoint)).toBe(true);
}
});
});
describe("getGlyphCategories", () => {
it("returns sorted unique categories", () => {
const categories = db.getGlyphCategories();
expect(categories.length).toBeGreaterThan(0);
expect(categories).toContain("Letter");
expect(categories).toContain("Symbol");
const sorted = [...categories].sort();
expect(categories).toEqual(sorted);
});
it("contains no duplicates", () => {
const categories = db.getGlyphCategories();
expect(new Set(categories).size).toBe(categories.length);
});
it("includes common font editing categories", () => {
const categories = db.getGlyphCategories();
expect(categories).toContain("Number");
expect(categories).toContain("Punctuation");
expect(categories).toContain("Separator");
});
});
describe("getGlyphsByCategory", () => {
it("returns glyphs in the Letter category", () => {
const letters = db.getGlyphsByCategory("Letter");
expect(letters.length).toBeGreaterThan(0);
expect(letters.every((g) => g.category === "Letter")).toBe(true);
});
it("returns empty array for non-existent category", () => {
expect(db.getGlyphsByCategory("NonExistent" as GlyphCategory)).toEqual([]);
});
it("all returned glyphs match the requested category", () => {
const symbols = db.getGlyphsByCategory("Symbol");
for (const g of symbols) {
expect(g.category).toBe("Symbol");
}
});
it("category results are consistent with getAllGlyph", () => {
const all = db.getAllGlyph();
const categories = db.getGlyphCategories();
let totalFromCategories = 0;
for (const cat of categories) {
totalFromCategories += db.getGlyphsByCategory(cat).length;
}
expect(totalFromCategories).toBe(all.length);
});
});
describe("getDecomposition", () => {
it("decomposes é (U+00E9) to e + combining acute", () => {
const components = db.getDecomposition(0xe9);
expect(components).toEqual([0x65, 0x301]);
});
it("returns empty array for a base character", () => {
expect(db.getDecomposition(0x41)).toEqual([]);
});
it("decomposes ñ (U+00F1) to n + combining tilde", () => {
const components = db.getDecomposition(0xf1);
expect(components).toEqual([0x6e, 0x303]);
});
it("decomposes ü (U+00FC) to u + combining diaeresis", () => {
const components = db.getDecomposition(0xfc);
expect(components).toEqual([0x75, 0x308]);
});
it("returns empty for surrogate codepoint", () => {
expect(db.getDecomposition(0xd800)).toEqual([]);
});
it("returns empty for negative codepoint", () => {
expect(db.getDecomposition(-1)).toEqual([]);
});
it("decomposes fi ligature (U+FB01) into f + i", () => {
const components = db.getDecomposition(0xfb01);
expect(components).toContain(0x66); // f
expect(components).toContain(0x69); // i
});
it("decomposes superscript 2 (U+00B2) to digit 2", () => {
const components = db.getDecomposition(0xb2);
expect(components).toContain(0x32); // 2
});
it("decomposition components are all valid codepoints", () => {
// Spot-check several decomposable characters
for (const cp of [0xe9, 0xf1, 0xfc, 0xc0, 0xd6]) {
const components = db.getDecomposition(cp);
for (const c of components) {
expect(c).toBeGreaterThanOrEqual(0);
expect(c).toBeLessThanOrEqual(0xffff);
}
}
});
});
describe("getUsedBy", () => {
it("returns composed characters using e (U+0065)", () => {
const usedBy = db.getUsedBy(0x65);
expect(usedBy.length).toBeGreaterThan(0);
expect(usedBy).toContain(0xe9); // é
expect(usedBy).toContain(0xe8); // è
expect(usedBy).toContain(0xea); // ê
expect(usedBy).toContain(0xeb); // ë
});
it("returns empty array for a character not used as component", () => {
expect(db.getUsedBy(0xfffe)).toEqual([]);
});
it("usedBy is sorted in ascending order", () => {
const usedBy = db.getUsedBy(0x65);
for (let i = 1; i < usedBy.length; i++) {
expect(usedBy[i]).toBeGreaterThan(usedBy[i - 1]);
}
});
it("combining acute (U+0301) is used by many accented characters", () => {
const usedBy = db.getUsedBy(0x301);
expect(usedBy.length).toBeGreaterThan(10);
expect(usedBy).toContain(0xe9); // é
expect(usedBy).toContain(0xc1); // Á
});
it("decomposition and usedBy are consistent", () => {
// If é decomposes to [e, acute], then usedBy(e) should contain é
const components = db.getDecomposition(0xe9);
for (const comp of components) {
const usedBy = db.getUsedBy(comp);
expect(usedBy).toContain(0xe9);
}
});
it("returns empty for negative codepoint", () => {
expect(db.getUsedBy(-1)).toEqual([]);
});
});
describe("listCharsets", () => {
it("includes adobe-latin-1", () => {
const charsets = db.listCharsets();
expect(charsets.length).toBeGreaterThan(0);
const al1 = charsets.find((c) => c.id === "adobe-latin-1");
expect(al1).toBeDefined();
expect(al1!.name).toBe("Adobe Latin 1");
expect(al1!.source).toBe("adobe");
expect(al1!.count).toBeGreaterThan(200);
});
it("summary count matches codepoints array length", () => {
const charsets = db.listCharsets();
for (const cs of charsets) {
const codepoints = db.getCharsetCodepoints(cs.id);
expect(cs.count).toBe(codepoints.length);
}
});
it("does not expose codepoints array in summary", () => {
const charsets = db.listCharsets();
for (const cs of charsets) {
expect(cs).not.toHaveProperty("codepoints");
}
});
});
describe("getCharsetCodepoints", () => {
it("returns codepoints for adobe-latin-1", () => {
const codepoints = db.getCharsetCodepoints("adobe-latin-1");
expect(codepoints.length).toBeGreaterThan(200);
expect(codepoints).toContain(0x41); // A
expect(codepoints).toContain(0x24); // $
});
it("returns empty for unknown charset", () => {
expect(db.getCharsetCodepoints("nonexistent")).toEqual([]);
});
it("contains basic ASCII letters and digits", () => {
const codepoints = db.getCharsetCodepoints("adobe-latin-1");
// A-Z
for (let cp = 0x41; cp <= 0x5a; cp++) {
expect(codepoints).toContain(cp);
}
// a-z
for (let cp = 0x61; cp <= 0x7a; cp++) {
expect(codepoints).toContain(cp);
}
// 0-9
for (let cp = 0x30; cp <= 0x39; cp++) {
expect(codepoints).toContain(cp);
}
});
it("contains common accented characters", () => {
const codepoints = db.getCharsetCodepoints("adobe-latin-1");
expect(codepoints).toContain(0xe9); // é
expect(codepoints).toContain(0xf1); // ñ
expect(codepoints).toContain(0xfc); // ü
expect(codepoints).toContain(0xe7); // ç
});
it("contains fi and fl ligatures", () => {
const codepoints = db.getCharsetCodepoints("adobe-latin-1");
expect(codepoints).toContain(0xfb01); // fi
expect(codepoints).toContain(0xfb02); // fl
});
it("contains euro sign", () => {
const codepoints = db.getCharsetCodepoints("adobe-latin-1");
expect(codepoints).toContain(0x20ac);
});
it("codepoints are all valid non-negative integers", () => {
const codepoints = db.getCharsetCodepoints("adobe-latin-1");
for (const cp of codepoints) {
expect(cp).toBeGreaterThanOrEqual(0);
expect(Number.isInteger(cp)).toBe(true);
}
});
it("returns empty string for empty id", () => {
expect(db.getCharsetCodepoints("")).toEqual([]);
});
});
describe("search", () => {
it("finds dollar sign by name", () => {
const results = db.search("dollar");
expect(results.length).toBeGreaterThan(0);
expect(results.some((r) => r.codepoint === 0x24)).toBe(true);
});
it("finds arrow glyphs", () => {
const results = db.search("arrow");
expect(results.length).toBeGreaterThan(0);
});
it("supports prefix matching", () => {
const results = db.search("dol");
expect(results.length).toBeGreaterThan(0);
expect(results.some((r) => r.codepoint === 0x24)).toBe(true);
});
it("returns empty for empty query", () => {
expect(db.search("")).toEqual([]);
});
it("returns empty for whitespace-only query", () => {
expect(db.search(" ")).toEqual([]);
expect(db.search("\t\n")).toEqual([]);
});
it("respects limit parameter", () => {
const results = db.search("latin", 5);
expect(results.length).toBeLessThanOrEqual(5);
});
it("default limit is 50", () => {
const results = db.search("latin");
expect(results.length).toBeLessThanOrEqual(50);
});
it("results have all required fields", () => {
const results = db.search("dollar");
for (const r of results) {
expect(r.codepoint).toBeTypeOf("number");
expect(r.rank).toBeTypeOf("number");
// glyphName, unicodeName, category, subCategory can be null
}
});
it("finds by unicode name", () => {
const results = db.search("EXCLAMATION MARK");
expect(results.length).toBeGreaterThan(0);
expect(results.some((r) => r.codepoint === 0x21)).toBe(true);
});
it("finds by glyph name", () => {
const results = db.search("exclam");
expect(results.length).toBeGreaterThan(0);
expect(results.some((r) => r.codepoint === 0x21)).toBe(true);
});
it("multi-word search narrows results", () => {
const broad = db.search("latin");
const narrow = db.search("latin capital");
expect(narrow.length).toBeLessThanOrEqual(broad.length);
});
it("search with limit 1 returns at most 1 result", () => {
const results = db.search("dollar", 1);
expect(results.length).toBeLessThanOrEqual(1);
});
it("results are ranked (rank values are present)", () => {
const results = db.search("dollar");
expect(results.length).toBeGreaterThan(0);
for (const r of results) {
expect(r.rank).toBeTypeOf("number");
}
});
it("finds characters by category search", () => {
const results = db.search("Currency");
expect(results.length).toBeGreaterThan(0);
});
it("handles special characters gracefully", () => {
// These should not crash — they may return no results
expect(() => db.search('"')).not.toThrow();
expect(() => db.search("(")).not.toThrow();
expect(() => db.search(")")).not.toThrow();
});
});
describe("category helpers", () => {
it("resolves known codepoint category metadata", () => {
const result = db.getCategoryForCodepoint(0x24);
expect(result).toEqual({
category: "Symbol",
subCategoryKey: "Currency",
subCategoryLabel: "Currency",
isKnown: true,
});
});
it("resolves unknown codepoint using defaults", () => {
const result = db.getCategoryForCodepoint(0xfffe);
expect(result).toEqual({
category: "Other",
subCategoryKey: "__null_subcategory__",
subCategoryLabel: "General",
isKnown: false,
});
});
it("supports overriding fallback labels", () => {
const result = db.getCategoryForCodepoint(0xfffe, {
unknownCategoryLabel: "Unknown",
nullSubCategoryKey: "none",
nullSubCategoryLabel: "None",
});
expect(result).toEqual({
category: "Unknown",
subCategoryKey: "none",
subCategoryLabel: "None",
isKnown: false,
});
});
it("builds sorted category summaries with subcategory counts", () => {
const summaries = db.getCategorySummaries([0x24, 0x20, 0x30, 0xfffe]);
expect(summaries.map((summary) => summary.category)).toEqual([
"Number",
"Other",
"Separator",
"Symbol",
]);
const symbolSummary = summaries.find((summary) => summary.category === "Symbol");
expect(symbolSummary).toBeDefined();
expect(symbolSummary!.count).toBe(1);
expect(symbolSummary!.subCategories).toEqual([
{ key: "Currency", label: "Currency", count: 1 },
]);
});
it("can exclude unknown codepoints from summaries", () => {
const summaries = db.getCategorySummaries([0x24, 0xfffe], { includeUnknown: false });
expect(summaries.map((summary) => summary.category)).toEqual(["Symbol"]);
});
it("filters by category and subcategory", () => {
const codepoints = [0x24, 0xa3, 0x20, 0x30, 0xfffe];
const categoryOnly = db.filterCodepoints(codepoints, { category: "Symbol" });
expect(categoryOnly).toEqual([0x24, 0xa3]);
const subCategory = db.filterCodepoints(codepoints, {
category: "Symbol",
subCategoryKey: "Currency",
});
expect(subCategory).toEqual([0x24, 0xa3]);
});
it("filters by full-text query while preserving input order", () => {
const codepoints = [0x41, 0x42, 0x24, 0x21];
const filtered = db.filterCodepoints(codepoints, { query: "exclam" });
expect(filtered).toEqual([0x21]);
});
it("returns empty list for subcategory without category", () => {
const codepoints = [0x24, 0x23, 0x20];
const filtered = db.filterCodepoints(codepoints, { subCategoryKey: "Currency" });
expect(filtered).toEqual([]);
});
it("can exclude unknown codepoints from filtering", () => {
const codepoints = [0x24, 0xfffe];
const filtered = db.filterCodepoints(codepoints, {}, { includeUnknown: false });
expect(filtered).toEqual([0x24]);
});
it("creates reusable catalogs with summaries and filtering", () => {
const catalog = db.createCategoryCatalog([0x24, 0xa3, 0x30, 0xfffe]);
expect(catalog.categories.map((summary) => summary.category)).toEqual([
"Number",
"Other",
"Symbol",
]);
const symbolOnly = catalog.filter({ category: "Symbol" });
expect(symbolOnly).toEqual([0x24, 0xa3]);
});
});
describe("cross-domain consistency", () => {
it("charset codepoints that are in glyphData return valid glyph names", () => {
const codepoints = db.getCharsetCodepoints("adobe-latin-1");
let matchCount = 0;
for (const cp of codepoints) {
const name = db.getGlyphName(cp);
if (name !== null) {
matchCount++;
expect(name.length).toBeGreaterThan(0);
}
}
// Most Adobe Latin 1 codepoints should be in Glyph.xml
expect(matchCount).toBeGreaterThan(200);
});
it("decomposable characters in the charset can be looked up", () => {
const codepoints = db.getCharsetCodepoints("adobe-latin-1");
let decomposableCount = 0;
for (const cp of codepoints) {
const decomp = db.getDecomposition(cp);
if (decomp.length > 0) {
decomposableCount++;
// Each component should be a valid codepoint
for (const comp of decomp) {
expect(comp).toBeGreaterThanOrEqual(0);
expect(comp).toBeLessThanOrEqual(0xffff);
}
}
}
// Accented chars like é, ñ, ü etc. should decompose
expect(decomposableCount).toBeGreaterThan(10);
});
it("searchable codepoints match glyph data lookups", () => {
const searchResults = db.search("dollar");
const dollarResult = searchResults.find((r) => r.codepoint === 0x24);
expect(dollarResult).toBeDefined();
const glyphData = db.getGlyph(0x24);
expect(glyphData).not.toBeNull();
expect(dollarResult!.glyphName).toBe(glyphData!.name);
});
});