forked from lxn/walk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
font.go
222 lines (183 loc) · 4.64 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
// 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.
// +build windows
package walk
import (
"syscall"
)
import (
"github.com/lxn/win"
)
type FontStyle byte
// Font style flags
const (
FontBold FontStyle = 0x01
FontItalic FontStyle = 0x02
FontUnderline FontStyle = 0x04
FontStrikeOut FontStyle = 0x08
)
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 > FontBold|FontItalic|FontUnderline|FontStrikeOut {
return nil, newError("invalid style")
}
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
}
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
if lf.LfWeight > win.FW_NORMAL {
style |= FontBold
}
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)
if f.style&FontBold > 0 {
lf.LfWeight = win.FW_BOLD
} else {
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
}
// Bold returns if text drawn using the Font appears with
// greater weight than normal.
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
}
func screenDPI() int {
hDC := win.GetDC(0)
defer win.ReleaseDC(0, hDC)
return int(win.GetDeviceCaps(hDC, win.LOGPIXELSY))
}