forked from jednano/parse-css-font
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathstringify.js
102 lines (82 loc) · 2.8 KB
/
stringify.js
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
'use strict'
var pick = require('pick-by-alias')
var isSize = require('./lib/util').isSize
var globals = a2o(require('css-global-keywords'))
var systems = a2o(require('css-system-font-keywords'))
var weights = a2o(require('css-font-weight-keywords'))
var styles = a2o(require('css-font-style-keywords'))
var stretches = a2o(require('css-font-stretch-keywords'))
var variants = {'normal': 1, 'small-caps': 1}
var fams = {
'serif': 1,
'sans-serif': 1,
'monospace': 1,
'cursive': 1,
'fantasy': 1,
'system-ui': 1
}
var defaults = {
style: 'normal',
variant: 'normal',
weight: 'normal',
stretch: 'normal',
size: '1rem',
lineHeight: 'normal',
family: 'serif'
}
module.exports = function stringifyFont (o) {
o = pick(o, {
style: 'style fontstyle fontStyle font-style slope distinction',
variant: 'variant font-variant fontVariant fontvariant var capitalization',
weight: 'weight w font-weight fontWeight fontweight',
stretch: 'stretch font-stretch fontStretch fontstretch width',
size: 'size s font-size fontSize fontsize height em emSize',
lineHeight: 'lh line-height lineHeight lineheight leading',
family: 'font family fontFamily font-family fontfamily type typeface face',
system: 'system reserved default global',
})
if (o.system) {
if (o.system) verify(o.system, systems)
return o.system
}
verify(o.style, styles)
verify(o.variant, variants)
verify(o.weight, weights)
verify(o.stretch, stretches)
// default root value is medium, but by default it's inherited
if (o.size == null) o.size = defaults.size
if (typeof o.size === 'number') o.size += 'px'
if (!isSize) throw Error('Bad size value `' + o.size + '`')
// many user-agents use serif, we don't detect that for consistency
if (!o.family) o.family = defaults.family
if (Array.isArray(o.family)) {
if (!o.family.length) o.family = [defaults.family]
o.family = o.family.map(function (f) {
return fams[f] ? f : '"' + f + '"'
}).join(', ')
}
// [ [ <'font-style'> || <font-variant-css21> || <'font-weight'> || <'font-stretch'> ]? <'font-size'> [ / <'line-height'> ]? <'font-family'> ]
var result = []
result.push(o.style)
if (o.variant !== o.style) result.push(o.variant)
if (o.weight !== o.variant &&
o.weight !== o.style) result.push(o.weight)
if (o.stretch !== o.weight &&
o.stretch !== o.variant &&
o.stretch !== o.style) result.push(o.stretch)
result.push(o.size + (o.lineHeight == null || o.lineHeight === 'normal' || (o.lineHeight + '' === '1') ? '' : ('/' + o.lineHeight)))
result.push(o.family)
return result.filter(Boolean).join(' ')
}
function verify (value, values) {
if (value && !values[value] && !globals[value]) throw Error('Unknown keyword `' + value +'`')
return value
}
// ['a', 'b'] -> {a: true, b: true}
function a2o (a) {
var o = {}
for (var i = 0; i < a.length; i++) {
o[a[i]] = 1
}
return o
}