forked from linuxdeepin/dde-daemon
-
Notifications
You must be signed in to change notification settings - Fork 0
/
family.go
205 lines (174 loc) · 4.29 KB
/
family.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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
/*
* Copyright (C) 2014 ~ 2018 Deepin Technology Co., Ltd.
*
* Author: jouyouyun <jouyouwen717@gmail.com>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package fonts
import (
"crypto/md5"
"fmt"
"path"
"sort"
"strconv"
"strings"
"sync"
"github.com/linuxdeepin/go-gir/gio-2.0"
"github.com/linuxdeepin/go-lib/xdg/basedir"
)
type Family struct {
Id string
Name string
Styles []string
Monospace bool
Show bool
}
type FamilyHashTable map[string]*Family
const (
fallbackStandard = "Noto Sans"
fallbackMonospace = "Noto Mono"
xsettingsSchema = "com.deepin.xsettings"
gsKeyFontName = "gtk-font-name"
)
var (
locker sync.Mutex
xsSetting = gio.NewSettings(xsettingsSchema)
DeepinFontConfig = path.Join(basedir.GetUserConfigDir(), "fontconfig", "conf.d", "99-deepin.conf")
)
func IsFontFamily(value string) bool {
if isVirtualFont(value) {
return true
}
info := GetFamilyTable().GetFamily(value)
return info != nil
}
func IsFontSizeValid(size float64) bool {
if size >= 7.0 && size <= 22.0 {
return true
}
return false
}
func SetFamily(standard, monospace string, size float64) error {
locker.Lock()
defer locker.Unlock()
if isVirtualFont(standard) {
standard = fcFontMatch(standard)
}
if isVirtualFont(monospace) {
monospace = fcFontMatch(monospace)
}
table := GetFamilyTable()
standInfo := table.GetFamily(standard)
if standInfo == nil {
return fmt.Errorf("Invalid standard id '%s'", standard)
}
// standard += " " + standInfo.preferredStyle()
monoInfo := table.GetFamily(monospace)
if monoInfo == nil {
return fmt.Errorf("Invalid monospace id '%s'", monospace)
}
// monospace += " " + monoInfo.preferredStyle()
// fc-match can not real time update
/*
curStand := fcFontMatch("sans-serif")
curMono := fcFontMatch("monospace")
if (standInfo.Id == curStand || standInfo.Name == curStand) &&
(monoInfo.Id == curMono || monoInfo.Name == curMono) {
return nil
}
*/
err := writeFontConfig(configContent(standInfo.Id, monoInfo.Id), DeepinFontConfig)
if err != nil {
return err
}
return setFontByXSettings(standard, size)
}
func GetFontSize() float64 {
return getFontSize(xsSetting)
}
func (table FamilyHashTable) ListMonospace() []string {
var ids []string
for _, info := range table {
if !info.Monospace {
continue
}
ids = append(ids, info.Id)
}
sort.Strings(ids)
return ids
}
func (table FamilyHashTable) ListStandard() []string {
var ids []string
for _, info := range table {
if info.Monospace || !info.Show {
continue
}
ids = append(ids, info.Id)
}
sort.Strings(ids)
return ids
}
func (table FamilyHashTable) Get(key string) *Family {
info := table[key]
return info
}
func (table FamilyHashTable) GetFamily(id string) *Family {
info, ok := table[sumStrHash(id)]
if !ok {
return nil
}
return info
}
func (table FamilyHashTable) GetFamilies(ids []string) []*Family {
var infos []*Family
for _, id := range ids {
info, ok := table[sumStrHash(id)]
if !ok {
continue
}
infos = append(infos, info)
}
return infos
}
func setFontByXSettings(name string, size float64) error {
if size == -1 {
size = getFontSize(xsSetting)
}
v := fmt.Sprintf("%s %v", name, size)
if v == xsSetting.GetString(gsKeyFontName) {
return nil
}
xsSetting.SetString(gsKeyFontName, v)
return nil
}
func getFontSize(setting *gio.Settings) float64 {
value := setting.GetString(gsKeyFontName)
if len(value) == 0 {
return 0
}
array := strings.Split(value, " ")
size, _ := strconv.ParseFloat(array[len(array)-1], 64)
return size
}
func isVirtualFont(name string) bool {
switch name {
case "monospace", "mono", "sans-serif", "sans", "serif":
return true
}
return false
}
func sumStrHash(v string) string {
return fmt.Sprintf("%x", md5.Sum([]byte(v)))
}