blob: e56849caef033257bae64da0b3514dd7f517d1cc (
plain)
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
|
import dingbats from "./dingbats";
export interface UnicodeScalarValue {
codePoint: number;
string: string;
}
const dingbatsByCodePoint: {[key: string]: UnicodeScalarValue} = {};
const fromCodePoint = String.fromCodePoint ? String.fromCodePoint : fromCodePointPolyfill;
for (const dingbat of dingbats) {
const codePoint = parseInt(dingbat["Unicode dec"], 10);
const scalarValue = {
codePoint: codePoint,
string: fromCodePoint(codePoint),
};
dingbatsByCodePoint[dingbat["Typeface name"].toUpperCase() + "_" + dingbat["Dingbat dec"]] = scalarValue;
}
export function codePoint(typeface: string, codePoint: number): UnicodeScalarValue | undefined {
return dingbatsByCodePoint[typeface.toUpperCase() + "_" + codePoint];
}
export function dec(typeface: string, dec: string): UnicodeScalarValue | undefined {
return codePoint(typeface, parseInt(dec, 10));
}
export function hex(typeface: string, hex: string): UnicodeScalarValue | undefined {
return codePoint(typeface, parseInt(hex, 16));
}
function fromCodePointPolyfill(codePoint: number) {
if (codePoint <= 0xFFFF) {
// BMP
return String.fromCharCode(codePoint);
} else {
// Astral
// https://mathiasbynens.be/notes/javascript-encoding#surrogate-formulae
const highSurrogate = Math.floor((codePoint - 0x10000) / 0x400) + 0xD800;
const lowSurrogate = (codePoint - 0x10000) % 0x400 + 0xDC00;
return String.fromCharCode(highSurrogate, lowSurrogate);
}
};
|