-
Notifications
You must be signed in to change notification settings - Fork 11
/
font.go
92 lines (77 loc) · 2.08 KB
/
font.go
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
package css
import (
"bytes"
)
type unicodeRange string
func (u unicodeRange) Rule() Rule { return Rule(u) }
type FontFace struct {
FontFamily string
Src urlType
FontStretch fontStretchValue
FontStyle fontStyleValue
FontWeight fontWeightValue
FontDisplay fontDisplayValue
UnicodeRange unicodeRange
}
func (font FontFace) fontFamilyValue() {}
func (font FontFace) Rule() Rule { return Rule(font.FontFamily) }
func NewFontFace(name string, src string) FontFace {
return FontFace{
FontFamily: name,
Src: urlType("url(" + src + ")"),
FontDisplay: Swap,
}
}
func (font FontFace) Set(property, value string) {
switch property {
case "font-family":
font.FontFamily = value
case "src":
font.Src = urlType(value)
case "font-stretch":
font.FontStretch = fontStretchType(value)
case "font-style":
font.FontStyle = fontStyleType(value)
case "font-weight":
font.FontWeight = fontWeightType(value)
case "unicode-range":
font.UnicodeRange = unicodeRange(value)
case "font-display":
font.FontDisplay = fontDisplayType(value)
}
}
func (font FontFace) Bytes() []byte {
var buffer bytes.Buffer
buffer.WriteString("font-family:")
buffer.WriteString(font.FontFamily)
buffer.WriteByte(';')
buffer.WriteString("src:")
buffer.WriteString(font.Src.String())
buffer.WriteByte(';')
if font.FontStretch != nil {
buffer.WriteString("font-stretch: ")
buffer.WriteString(string(font.FontStretch.Rule()))
buffer.WriteByte(';')
}
if font.FontStyle != nil {
buffer.WriteString("font-style: ")
buffer.WriteString(string(font.FontStyle.Rule()))
buffer.WriteByte(';')
}
if font.FontWeight != nil {
buffer.WriteString("font-weight: ")
buffer.WriteString(string(font.FontWeight.Rule()))
buffer.WriteByte(';')
}
if font.UnicodeRange.Rule() != "" {
buffer.WriteString("unicode-range: ")
buffer.WriteString(string(font.UnicodeRange.Rule()))
buffer.WriteByte(';')
}
if font.FontDisplay.Rule() != "" {
buffer.WriteString("font-display: ")
buffer.WriteString(string(font.FontDisplay.Rule()))
buffer.WriteByte(';')
}
return buffer.Bytes()
}