1 | import { makeValidator, makeParser } from '../core/properties';
|
2 | export * from './font-interfaces';
|
3 | export class Font {
|
4 | constructor(fontFamily, fontSize, fontStyle, fontWeight, fontScale) {
|
5 | this.fontFamily = fontFamily;
|
6 | this.fontSize = fontSize;
|
7 | this.fontStyle = fontStyle;
|
8 | this.fontWeight = fontWeight;
|
9 | this.fontScale = fontScale;
|
10 | }
|
11 | get isItalic() {
|
12 | return this.fontStyle === FontStyle.ITALIC;
|
13 | }
|
14 | get isBold() {
|
15 | return this.fontWeight === FontWeight.SEMI_BOLD || this.fontWeight === FontWeight.BOLD || this.fontWeight === '700' || this.fontWeight === FontWeight.EXTRA_BOLD || this.fontWeight === FontWeight.BLACK;
|
16 | }
|
17 | static equals(value1, value2) {
|
18 |
|
19 | if (!value1 && !value2) {
|
20 | return true;
|
21 | }
|
22 |
|
23 | if (!value1 || !value2) {
|
24 | return false;
|
25 | }
|
26 | return value1.fontFamily === value2.fontFamily && value1.fontSize === value2.fontSize && value1.fontStyle === value2.fontStyle && value1.fontWeight === value2.fontWeight;
|
27 | }
|
28 | }
|
29 | Font.default = undefined;
|
30 | export var FontStyle;
|
31 | (function (FontStyle) {
|
32 | FontStyle.NORMAL = 'normal';
|
33 | FontStyle.ITALIC = 'italic';
|
34 | FontStyle.isValid = makeValidator(FontStyle.NORMAL, FontStyle.ITALIC);
|
35 | FontStyle.parse = makeParser(FontStyle.isValid);
|
36 | })(FontStyle || (FontStyle = {}));
|
37 | export var FontWeight;
|
38 | (function (FontWeight) {
|
39 | FontWeight.THIN = '100';
|
40 | FontWeight.EXTRA_LIGHT = '200';
|
41 | FontWeight.LIGHT = '300';
|
42 | FontWeight.NORMAL = 'normal';
|
43 | FontWeight.MEDIUM = '500';
|
44 | FontWeight.SEMI_BOLD = '600';
|
45 | FontWeight.BOLD = 'bold';
|
46 | FontWeight.EXTRA_BOLD = '800';
|
47 | FontWeight.BLACK = '900';
|
48 | FontWeight.isValid = makeValidator(FontWeight.THIN, FontWeight.EXTRA_LIGHT, FontWeight.LIGHT, FontWeight.NORMAL, '400', FontWeight.MEDIUM, FontWeight.SEMI_BOLD, FontWeight.BOLD, '700', FontWeight.EXTRA_BOLD, FontWeight.BLACK);
|
49 | FontWeight.parse = makeParser(FontWeight.isValid);
|
50 | })(FontWeight || (FontWeight = {}));
|
51 | export function parseFontFamily(value) {
|
52 | if (!value) {
|
53 | return [];
|
54 | }
|
55 | return value
|
56 | .split(',')
|
57 | .map((v) => (v || '').trim().replace(/['"]+/g, ''))
|
58 | .filter((v) => !!v);
|
59 | }
|
60 | export var genericFontFamilies;
|
61 | (function (genericFontFamilies) {
|
62 | genericFontFamilies.serif = 'serif';
|
63 | genericFontFamilies.sansSerif = 'sans-serif';
|
64 | genericFontFamilies.monospace = 'monospace';
|
65 | genericFontFamilies.system = 'system';
|
66 | })(genericFontFamilies || (genericFontFamilies = {}));
|
67 | const styles = new Set();
|
68 | [FontStyle.NORMAL, FontStyle.ITALIC].forEach((val, i, a) => styles.add(val));
|
69 |
|
70 |
|
71 |
|
72 |
|
73 |
|
74 |
|
75 |
|
76 |
|
77 |
|
78 |
|
79 |
|
80 |
|
81 | const weights = new Set();
|
82 | [FontWeight.THIN, FontWeight.EXTRA_LIGHT, FontWeight.LIGHT, FontWeight.NORMAL, '400', FontWeight.MEDIUM, FontWeight.SEMI_BOLD, FontWeight.BOLD, '700', FontWeight.EXTRA_BOLD, FontWeight.BLACK].forEach((val, i, a) => weights.add(val));
|
83 | export function parseFont(fontValue) {
|
84 | const result = {
|
85 | fontStyle: 'normal',
|
86 | fontVariant: 'normal',
|
87 | fontWeight: 'normal',
|
88 | };
|
89 | const parts = fontValue.split(/\s+/);
|
90 | let part;
|
91 | while ((part = parts.shift())) {
|
92 | if (part === 'normal') {
|
93 |
|
94 | }
|
95 | else if (part === 'small-caps') {
|
96 |
|
97 | result.fontVariant = part;
|
98 | }
|
99 | else if (styles.has(part)) {
|
100 | result.fontStyle = part;
|
101 | }
|
102 | else if (weights.has(part)) {
|
103 | result.fontWeight = part;
|
104 | }
|
105 | else if (!result.fontSize) {
|
106 | const sizes = part.split('/');
|
107 | result.fontSize = sizes[0];
|
108 | result.lineHeight = sizes.length > 1 ? sizes[1] : undefined;
|
109 | }
|
110 | else {
|
111 | result.fontFamily = part;
|
112 | if (parts.length) {
|
113 | result.fontFamily += ' ' + parts.join(' ');
|
114 | }
|
115 | break;
|
116 | }
|
117 | }
|
118 | return result;
|
119 | }
|
120 |
|
\ | No newline at end of file |