/
TextRenderingStyle.ts
55 lines (42 loc) · 1.26 KB
/
TextRenderingStyle.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
import RenderingStyle, { cloneStyle, styleFromJSON, styleToJSON } from './RenderingStyle';
export interface TextRenderingStyle {
readonly size: number;
/** Name of the font. */
readonly fontFamily: string;
/** For example, `bold`. Like CSS `font-weight`. */
readonly fontWeight?: string;
/** For example, `italic`. Like CSS `font-style`. */
readonly fontStyle?: string;
readonly fontVariant?: string;
/** Fill and stroke of the text. */
readonly renderingStyle: RenderingStyle;
}
export default TextRenderingStyle;
export const cloneTextStyle = (style: TextRenderingStyle) => {
return {
...style,
renderingStyle: cloneStyle(style.renderingStyle),
};
};
export const textStyleFromJSON = (json: any) => {
if (typeof json === 'string') {
json = JSON.parse(json);
}
if (typeof(json.fontFamily) !== 'string') {
throw new Error('Serialized textStyle missing string fontFamily attribute!');
}
const style: TextRenderingStyle = {
renderingStyle: styleFromJSON(json.renderingStyle),
size: json.size,
fontWeight: json.fontWeight,
fontVariant: json.fontVariant,
fontFamily: json.fontFamily,
};
return style;
};
export const textStyleToJSON = (style: TextRenderingStyle) => {
return {
...style,
renderingStyle: styleToJSON(style.renderingStyle),
};
};