-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfont_weight.go
128 lines (117 loc) · 2.94 KB
/
font_weight.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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
// Copyright ©2021-2022 by Richard A. Wilkes. All rights reserved.
//
// This Source Code Form is subject to the terms of the Mozilla Public
// License, version 2.0. If a copy of the MPL was not distributed with
// this file, You can obtain one at http://mozilla.org/MPL/2.0/.
//
// This Source Code Form is "Incompatible With Secondary Licenses", as
// defined by the Mozilla Public License, version 2.0.
package unison
import (
"strings"
"github.com/ddkwork/toolbox/i18n"
)
// FontWeight holds the weight of a font.
type FontWeight int32
// Possible values for FontWeight.
const (
InvisibleFontWeight FontWeight = iota * 100
ThinFontWeight
ExtraLightFontWeight
LightFontWeight
NormalFontWeight
MediumFontWeight
SemiBoldFontWeight
BoldFontWeight
ExtraBoldFontWeight
BlackFontWeight
ExtraBlackFontWeight
)
// FontWeights holds the set of possible FontWeight values.
var FontWeights = []FontWeight{
InvisibleFontWeight,
ThinFontWeight,
ExtraLightFontWeight,
LightFontWeight,
NormalFontWeight,
MediumFontWeight,
SemiBoldFontWeight,
BoldFontWeight,
ExtraBoldFontWeight,
BlackFontWeight,
ExtraBlackFontWeight,
}
// MarshalText implements the encoding.TextMarshaler interface.
func (w FontWeight) MarshalText() (text []byte, err error) {
return []byte(w.Key()), nil
}
// UnmarshalText implements the encoding.TextUnmarshaler interface.
func (w *FontWeight) UnmarshalText(text []byte) error {
*w = WeightFromString(string(text))
return nil
}
// WeightFromString extracts the FontWeight from a string.
func WeightFromString(str string) FontWeight {
if str == "" {
return NormalFontWeight
}
for w := InvisibleFontWeight; w <= ExtraBlackFontWeight; w += 100 {
if strings.EqualFold(w.Key(), str) {
return w
}
}
return NormalFontWeight
}
// Key returns the key that is used when serializing.
func (w FontWeight) Key() string {
switch w {
case InvisibleFontWeight:
return "invisible"
case ThinFontWeight:
return "thin"
case ExtraLightFontWeight:
return "extra-light"
case LightFontWeight:
return "light"
case MediumFontWeight:
return "medium"
case SemiBoldFontWeight:
return "semi-bold"
case BoldFontWeight:
return "bold"
case ExtraBoldFontWeight:
return "extra-bold"
case BlackFontWeight:
return "black"
case ExtraBlackFontWeight:
return "extra-black"
default:
return "regular"
}
}
func (w FontWeight) String() string {
switch w {
case InvisibleFontWeight:
return i18n.Text("Invisible")
case ThinFontWeight:
return i18n.Text("Thin")
case ExtraLightFontWeight:
return i18n.Text("Extra-Light")
case LightFontWeight:
return i18n.Text("Light")
case MediumFontWeight:
return i18n.Text("Medium")
case SemiBoldFontWeight:
return i18n.Text("Semi-Bold")
case BoldFontWeight:
return i18n.Text("Bold")
case ExtraBoldFontWeight:
return i18n.Text("Extra-Bold")
case BlackFontWeight:
return i18n.Text("Black")
case ExtraBlackFontWeight:
return i18n.Text("Extra-Black")
default:
return i18n.Text("Regular")
}
}