-
Notifications
You must be signed in to change notification settings - Fork 8
/
font.go
289 lines (236 loc) · 6.22 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
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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
// Copyright 2010 The Walk Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
//go:build windows
// +build windows
package walk
import (
"math/bits"
"syscall"
"unsafe"
"github.com/tailscale/win"
)
type FontStyle byte
// Font style flags
const (
FontNormal FontStyle = 0
FontSemiLight FontStyle = (1 << iota)
FontLight
FontSemiBold
FontBold
FontItalic
FontUnderline
FontStrikeOut
)
const (
fontWeightBits = FontLight | FontSemiLight | FontSemiBold | FontBold
fontStyleBits = fontWeightBits | FontItalic | FontUnderline | FontStrikeOut
)
var (
defaultFont *Font
knownFonts = make(map[fontInfo]*Font)
)
func init() {
AppendToWalkInit(func() {
// Initialize default font
var err error
if defaultFont, err = NewFont("MS Shell Dlg 2", 8, 0); err != nil {
panic("failed to create default font")
}
})
}
type fontInfo struct {
family string
pointSize int
style FontStyle
}
// Font represents a typographic typeface that is used for text drawing
// operations and on many GUI widgets.
type Font struct {
dpi2hFont map[int]win.HFONT
family string
pointSize int
style FontStyle
}
// NewFont returns a new Font with the specified attributes.
func NewFont(family string, pointSize int, style FontStyle) (*Font, error) {
if style > fontStyleBits {
return nil, newError("invalid style")
}
if bits.OnesCount8(byte(style&fontWeightBits)) > 1 {
return nil, newError("invalid style: font weights are mutually-exclusive")
}
fi := fontInfo{
family: family,
pointSize: pointSize,
style: style,
}
if font, ok := knownFonts[fi]; ok {
return font, nil
}
font := &Font{
family: family,
pointSize: pointSize,
style: style,
}
knownFonts[fi] = font
return font, nil
}
const fontWeightSemiLight = (win.FW_LIGHT + win.FW_NORMAL) / 2
func newFontFromLOGFONT(lf *win.LOGFONT, dpi int) (*Font, error) {
if lf == nil {
return nil, newError("lf cannot be nil")
}
family := win.UTF16PtrToString(&lf.LfFaceName[0])
pointSize := int(win.MulDiv(lf.LfHeight, 72, int32(dpi)))
if pointSize < 0 {
pointSize = -pointSize
}
var style FontStyle
// This switch is ordered from heaviest weight to lightest weight.
switch {
case lf.LfWeight > win.FW_SEMIBOLD:
style |= FontBold
case lf.LfWeight > win.FW_NORMAL:
style |= FontSemiBold
case lf.LfWeight == win.FW_NORMAL:
// Just break
case lf.LfWeight >= fontWeightSemiLight:
style |= FontSemiLight
case lf.LfWeight >= win.FW_LIGHT:
style |= FontLight
}
if lf.LfItalic == win.TRUE {
style |= FontItalic
}
if lf.LfUnderline == win.TRUE {
style |= FontUnderline
}
if lf.LfStrikeOut == win.TRUE {
style |= FontStrikeOut
}
return NewFont(family, pointSize, style)
}
func (f *Font) createForDPI(dpi int) (win.HFONT, error) {
var lf win.LOGFONT
lf.LfHeight = -win.MulDiv(int32(f.pointSize), int32(dpi), 72)
switch f.style & fontWeightBits {
case FontLight:
lf.LfWeight = win.FW_LIGHT
case FontSemiLight:
lf.LfWeight = fontWeightSemiLight
case FontSemiBold:
lf.LfWeight = win.FW_SEMIBOLD
case FontBold:
lf.LfWeight = win.FW_BOLD
default:
lf.LfWeight = win.FW_NORMAL
}
if f.style&FontItalic > 0 {
lf.LfItalic = 1
}
if f.style&FontUnderline > 0 {
lf.LfUnderline = 1
}
if f.style&FontStrikeOut > 0 {
lf.LfStrikeOut = 1
}
lf.LfCharSet = win.DEFAULT_CHARSET
lf.LfOutPrecision = win.OUT_TT_PRECIS
lf.LfClipPrecision = win.CLIP_DEFAULT_PRECIS
lf.LfQuality = win.CLEARTYPE_QUALITY
lf.LfPitchAndFamily = win.VARIABLE_PITCH | win.FF_SWISS
src := syscall.StringToUTF16(f.family)
dest := lf.LfFaceName[:]
copy(dest, src)
hFont := win.CreateFontIndirect(&lf)
if hFont == 0 {
return 0, newError("CreateFontIndirect failed")
}
return hFont, nil
}
// SemiLight returns if text drawn using the Font appears with semi-light weight.
func (f *Font) SemiLight() bool {
return f.style&FontSemiLight > 0
}
// Light returns if text drawn using the Font appears with light weight.
func (f *Font) Light() bool {
return f.style&FontLight > 0
}
// SemiBold returns if text drawn using the Font appears with semi-bold weight.
func (f *Font) SemiBold() bool {
return f.style&FontBold > 0
}
// Bold returns if text drawn using the Font appears with bold weight.
func (f *Font) Bold() bool {
return f.style&FontBold > 0
}
// Dispose releases the os resources that were allocated for the Font.
//
// The Font can no longer be used for drawing operations or with GUI widgets
// after calling this method. It is safe to call Dispose multiple times.
func (f *Font) Dispose() {
if len(f.dpi2hFont) == 0 {
return
}
for dpi, hFont := range f.dpi2hFont {
win.DeleteObject(win.HGDIOBJ(hFont))
delete(f.dpi2hFont, dpi)
}
}
// Family returns the family name of the Font.
func (f *Font) Family() string {
return f.family
}
// Italic returns if text drawn using the Font appears slanted.
func (f *Font) Italic() bool {
return f.style&FontItalic > 0
}
// HandleForDPI returns the os resource handle of the font for the specified
// DPI value.
func (f *Font) handleForDPI(dpi int) win.HFONT {
if f.dpi2hFont == nil {
f.dpi2hFont = make(map[int]win.HFONT)
} else if handle, ok := f.dpi2hFont[dpi]; ok {
return handle
}
hFont, err := f.createForDPI(dpi)
if err != nil {
return 0
}
f.dpi2hFont[dpi] = hFont
return hFont
}
// StrikeOut returns if text drawn using the Font appears striked out.
func (f *Font) StrikeOut() bool {
return f.style&FontStrikeOut > 0
}
// Style returns the combination of style flags of the Font.
func (f *Font) Style() FontStyle {
return f.style
}
// Underline returns if text drawn using the font appears underlined.
func (f *Font) Underline() bool {
return f.style&FontUnderline > 0
}
// PointSize returns the size of the Font in point units.
func (f *Font) PointSize() int {
return f.pointSize
}
// LOGFONTforDPI returns a GDI LOGFONT for f at the specified dpi.
func (f *Font) LOGFONTForDPI(dpi int) *win.LOGFONT {
hfont := f.handleForDPI(dpi)
if hfont == 0 {
return nil
}
var lf win.LOGFONT
if win.GetObject(win.HGDIOBJ(hfont), unsafe.Sizeof(lf), unsafe.Pointer(&lf)) == 0 {
return nil
}
return &lf
}
func screenDPI() int {
hDC := win.GetDC(0)
defer win.ReleaseDC(0, hDC)
return int(win.GetDeviceCaps(hDC, win.LOGPIXELSY))
}