forked from zenozeng/fonts.css
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparser.ts
More file actions
74 lines (64 loc) · 2.19 KB
/
parser.ts
File metadata and controls
74 lines (64 loc) · 2.19 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
export interface GenericFamily {
name: string
fallbackGenericFamily: string
className: string
}
export interface Font {
name: string
alias: string[] | string
genericFamilyName: string
platform: string[] | string
note: string[] | string
}
export interface ParseResult extends GenericFamily {
fonts: Font[]
cssFontFamilies: string[]
platforms: string[]
notes: string[]
}
const genericFamilies: GenericFamily[] = [
{
name: "黑体",
fallbackGenericFamily: "sans-serif",
className: "font-hei",
},
{
name: "楷体",
fallbackGenericFamily: "serif",
className: "font-kai",
},
{
name: "宋体",
fallbackGenericFamily: "serif",
className: "font-song",
},
{
name: "仿宋",
fallbackGenericFamily: "serif",
className: "font-fang-song",
},
]
function flatten(strings: (string[] | string)[]) : string[] {
return ([] as string[]).concat(...strings)
}
function uniq(strings: string[]) {
return strings.filter((v, i, arr) => arr.indexOf(v) == i)
}
export class Parser {
constructor(private fonts: Font[], private enFonts: Font[]) {}
parseGenericFamily(genericFamily: GenericFamily) : ParseResult {
let result = JSON.parse(JSON.stringify(genericFamily)) as ParseResult
const filter = (font: Font) => font.genericFamilyName == genericFamily.name
result.fonts = this.enFonts.filter(filter)
result.fonts = result.fonts.concat(this.fonts.filter(filter))
result.cssFontFamilies = flatten(result.fonts.map((font) => font.alias))
result.cssFontFamilies.push(genericFamily.fallbackGenericFamily)
result.cssFontFamilies = result.cssFontFamilies.map((str) => str.indexOf(" ") > -1 || str.indexOf("\\") > -1 ? `"${str}"` : str)
result.platforms = uniq(flatten(this.fonts.filter(filter).map((font) => font.platform))) // based on Chinese fonts here
result.notes = flatten(result.fonts.map((font) => font.note)).filter((note) => note)
return result
}
parse() : ParseResult[] {
return genericFamilies.map((genericFamily) => this.parseGenericFamily(genericFamily))
}
}