UNPKG

4.49 kBJavaScriptView Raw
1import { makeValidator, makeParser } from '../core/properties';
2export * from './font-interfaces';
3export 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 // both values are falsy
19 if (!value1 && !value2) {
20 return true;
21 }
22 // only one is falsy
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}
29Font.default = undefined;
30export 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 = {}));
37export 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 = {}));
51export 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}
60export 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 = {}));
67const styles = new Set();
68[FontStyle.NORMAL, FontStyle.ITALIC].forEach((val, i, a) => styles.add(val));
69// http://www.w3schools.com/cssref/pr_font_weight.asp
70//- normal(same as 400)
71//- bold(same as 700)
72//- 100(Thin) (API16 -thin)
73//- 200(Extra Light / Ultra Light) (API16 -light)
74//- 300(Light) (API16 -light)
75//- 400(Normal)
76//- 500(Medium) (API21 -medium)
77//- 600(Semi Bold / Demi Bold) (API21 -medium)
78//- 700(Bold) (API16 -bold)
79//- 800(Extra Bold / Ultra Bold) (API16 -bold)
80//- 900(Black / Heavy) (API21 -black)
81const 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));
83export 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 // nothing to do here
94 }
95 else if (part === 'small-caps') {
96 // The only supported font variant in shorthand font
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//# sourceMappingURL=font-common.js.map
\No newline at end of file