-
Notifications
You must be signed in to change notification settings - Fork 5
/
font.v
101 lines (87 loc) · 1.83 KB
/
font.v
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
module viup
struct Font {
pub mut:
condensed string
face string
font string
italic bool
size int = 12
strikeout bool
underline bool
weight string
}
// parse_font parses the provided font string and
// converts it to a valid `Font`
pub fn parse_font(font string) Font {
if font == '' {
return Font{
font: font
}
}
mut obj := Font{
font: font
}
mut split := []string{}
if font.contains(',') {
split_1 := font.split(',')
if split_1.len <= 1 {
return Font{
face: split_1[0]
font: font
}
}
obj.face = split_1[0]
split = split_1[1].trim_space().split(' ')
} else {
split = font.split(' ')
if split.len <= 1 {
return Font{
face: split[0]
font: font
}
}
obj.face = split[0]
split.delete(0)
}
for val in split {
match val.to_lower() {
'ultra-light', 'light', 'medium', 'semi-bold', 'bold', 'ultra-bold', 'heavy' {
obj.weight = val
}
'ultra-condensed', 'extra-condensed', 'condensed', 'semi-condensed', 'semi-expanded',
'expanded', 'extra-expanded', 'ultra-expanded' {
obj.condensed = val
}
'italic' {
obj.italic = true
}
'strikeout' {
obj.strikeout = true
}
'underline' {
obj.underline = true
}
else {
obj.size = val.int()
}
}
}
return obj
}
// show_picker is a helper function to show a font_dialog. It
// will automatically show the font dialog with this `Font`s
// values pre-selected and returns back the new font on select
pub fn (font Font) show_picker() Font {
dialog := font_dialog('value=$font')
dialog.popup(pos_current, pos_current)
if dialog.get_bool('status') {
value := dialog.get_attr('value')
parsed := parse_font(value)
return parsed
}
return font
}
// str returns back the original font that was provided in `parse_font`
pub fn (font Font) str() string {
return font.font
}