forked from VolodymyrBaydalka/docxjs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfonts.ts
61 lines (51 loc) · 1.51 KB
/
fonts.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
import { XmlParser } from "../parser/xml-parser";
const embedFontTypeMap = {
embedRegular: 'regular',
embedBold: 'bold',
embedItalic: 'italic',
embedBoldItalic: 'boldItalic',
}
export interface FontDeclaration {
name: string,
altName: string,
family: string,
embedFontRefs: EmbedFontRef[];
}
export interface EmbedFontRef {
id: string;
key: string;
type: 'regular' | 'bold' | 'italic' | 'boldItalic';
}
export function parseFonts(root: Element, xml: XmlParser): FontDeclaration[] {
return xml.elements(root).map(el => parseFont(el, xml));
}
export function parseFont(elem: Element, xml: XmlParser): FontDeclaration {
let result = <FontDeclaration>{
name: xml.attr(elem, "name"),
embedFontRefs: []
};
for (let el of xml.elements(elem)) {
switch (el.localName) {
case "family":
result.family = xml.attr(el, "val");
break;
case "altName":
result.altName = xml.attr(el, "val");
break;
case "embedRegular":
case "embedBold":
case "embedItalic":
case "embedBoldItalic":
result.embedFontRefs.push(parseEmbedFontRef(el, xml));
break;
}
}
return result;
}
export function parseEmbedFontRef(elem: Element, xml: XmlParser): EmbedFontRef {
return {
id: xml.attr(elem, "id"),
key: xml.attr(elem, "fontKey"),
type: embedFontTypeMap[elem.localName]
};
}