/
page-size.ts
48 lines (42 loc) · 1.79 KB
/
page-size.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
import { NextAttributeComponent, XmlComponent } from "@file/xml-components";
import { PositiveUniversalMeasure, twipsMeasureValue } from "@util/values";
// <xsd:simpleType name="ST_PageOrientation">
// <xsd:restriction base="xsd:string">
// <xsd:enumeration value="portrait"/>
// <xsd:enumeration value="landscape"/>
// </xsd:restriction>
// </xsd:simpleType>
export const PageOrientation = {
PORTRAIT: "portrait",
LANDSCAPE: "landscape",
} as const;
// <xsd:complexType name="CT_PageSz">
// <xsd:attribute name="w" type="s:ST_TwipsMeasure"/>
// <xsd:attribute name="h" type="s:ST_TwipsMeasure"/>
// <xsd:attribute name="orient" type="ST_PageOrientation" use="optional"/>
// <xsd:attribute name="code" type="ST_DecimalNumber" use="optional"/>
// </xsd:complexType>
export type IPageSizeAttributes = {
readonly width?: number | PositiveUniversalMeasure;
readonly height?: number | PositiveUniversalMeasure;
readonly orientation?: (typeof PageOrientation)[keyof typeof PageOrientation];
};
export class PageSize extends XmlComponent {
public constructor(
width: number | PositiveUniversalMeasure,
height: number | PositiveUniversalMeasure,
orientation: (typeof PageOrientation)[keyof typeof PageOrientation],
) {
super("w:pgSz");
const flip = orientation === PageOrientation.LANDSCAPE;
const widthTwips = twipsMeasureValue(width);
const heightTwips = twipsMeasureValue(height);
this.root.push(
new NextAttributeComponent<IPageSizeAttributes>({
width: { key: "w:w", value: flip ? heightTwips : widthTwips },
height: { key: "w:h", value: flip ? widthTwips : heightTwips },
orientation: { key: "w:orient", value: orientation },
}),
);
}
}