-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.ts
148 lines (139 loc) · 3.95 KB
/
index.ts
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
import {
AllPackages,
delimiters,
entities,
JSONFontData,
JSONOutput,
liteAdaptor,
mathjax,
RegisterHTMLHandler,
SVG,
TeX,
Wrapper,
countUnicode,
} from './src/wrapper';
// https://stackoverflow.com/questions/11616630/how-can-i-print-a-circular-structure-in-a-json-like-format
const getCircularReplacer = () => {
const seen = new WeakSet();
return (_key: any, value: object) => {
if (typeof value === "object" && value !== null) {
if (seen.has(value)) {
return;
}
seen.add(value);
}
return value;
};
};
function deepCopy<T>(obj: T) {
return JSON.parse(JSON.stringify(obj), getCircularReplacer()) as T;
}
const styledCharRange: Array<Array<Number>> = (() => {
const result = [];
for (const k of Object.keys(JSONFontData.VariantSmp)) {
const v = deepCopy(JSONFontData.VariantSmp[k]);
for (let i = 0; i < v.length; i++) {
if (!v[i]) {
v[i] = 0;
}
}
for (let i = v.length; i < 5; i++) {
v.push(0);
}
result.push(v);
}
return result;
})();
const charRange: Array<Array<Number>> = (() => {
const result = [];
for (const [, beg, end] of JSONFontData.SmpRanges) {
result.push([beg, end]);
}
return result;
})();
const charNameMap: { [name: string]: string } = entities;
const composedCharMap: { [name: string]: Array<string> } = (() => {
const result = {};
for (const k of Object.keys(delimiters)) {
const stretch = delimiters[k]['stretch'];
const v = []
if (stretch instanceof Array) {
for (const c of stretch) {
if (c) {
v.push(String.fromCodePoint(c as number))
}
}
}
result[String.fromCodePoint(Number.parseInt(k, 10))] = v;
}
return result;
})();
const variants: {
[fontName: string]: Array<{
[charName: string]: {
path: string,
}
}>
} = (() => {
const variantMap = (Wrapper.jsonDocument.outputJax as JSONOutput<any, any, any>).getFullVariant();
// const variantMap = Wrapper.jsonOutput.getFullVariant();
// "normal": {
// "linked": [],
// "chars": {
// "32": [
// 0,
// 0,
// 0.25,
// {
// "p": ""
// }
const result = {};
for (const fontName of Object.keys(variantMap)) {
const chars = variantMap[fontName]['chars'];
const charMap = {};
for (const charCode of Object.keys(chars)) {
const charPropList = chars[charCode];
const charName = String.fromCodePoint(Number.parseInt(charCode, 10));
for (const charProp of charPropList) {
if (charProp instanceof Object) {
const path = charProp['p'];
const maybePath = charMap[charName];
if (maybePath) {
if (maybePath !== path) {
throw new Error(`Same Char Different Path: ${maybePath} !== ${path}`);
}
} else {
charMap[charName] = path;
}
}
}
}
result[fontName] = charMap;
}
return result;
})();
function unicodeCharToHex(str: string) {
const unicodeLength = countUnicode(str);
if (unicodeLength !== 1) {
throw new Error(`unicodeCharToHex: unexpected input string length, expected 1, got ${unicodeLength}`);
}
return str.codePointAt(0);
}
export {
AllPackages,
liteAdaptor,
mathjax,
RegisterHTMLHandler,
SVG,
TeX,
variants,
entities,
delimiters,
styledCharRange,
charRange,
charNameMap,
composedCharMap,
unicodeCharToHex,
countUnicode,
Wrapper,
};