-
Notifications
You must be signed in to change notification settings - Fork 0
/
font.go
195 lines (162 loc) · 4.36 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
// 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.
package walk
import "syscall"
import . "github.com/lxn/go-winapi"
type FontStyle byte
// Font style flags
const (
FontBold FontStyle = 0x01
FontItalic FontStyle = 0x02
FontUnderline FontStyle = 0x04
FontStrikeOut FontStyle = 0x08
)
var (
screenDPIX int
screenDPIY int
defaultFont *Font
)
func init() {
// Retrieve screen DPI
hDC := GetDC(0)
defer ReleaseDC(0, hDC)
screenDPIX = int(GetDeviceCaps(hDC, LOGPIXELSX))
screenDPIY = int(GetDeviceCaps(hDC, LOGPIXELSY))
// Initialize default font
var err error
if defaultFont, err = NewFont("MS Shell Dlg 2", 8, 0); err != nil {
panic("failed to create default font")
}
}
// Font represents a typographic typeface that is used for text drawing
// operations and on many GUI widgets.
type Font struct {
dpi2hFont map[int]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")
}
font := &Font{
family: family,
pointSize: pointSize,
style: style,
dpi2hFont: make(map[int]HFONT),
}
hFont := font.createForDPI(screenDPIY)
if hFont == 0 {
return nil, newError("CreateFontIndirect failed")
}
font.dpi2hFont[screenDPIY] = hFont
font.dpi2hFont[0] = hFont // Make HFONT for screen easier accessible.
return font, nil
}
func newFontFromLOGFONT(lf *LOGFONT, dpi int) (*Font, error) {
if lf == nil {
return nil, newError("lf cannot be nil")
}
family := UTF16PtrToString(&lf.LfFaceName[0])
pointSize := int(MulDiv(lf.LfHeight, 72, int32(dpi)))
if pointSize < 0 {
pointSize = -pointSize
}
var style FontStyle
if lf.LfWeight > FW_NORMAL {
style |= FontBold
}
if lf.LfItalic == TRUE {
style |= FontItalic
}
if lf.LfUnderline == TRUE {
style |= FontUnderline
}
if lf.LfStrikeOut == TRUE {
style |= FontStrikeOut
}
return NewFont(family, pointSize, style)
}
func (f *Font) createForDPI(dpi int) HFONT {
var lf LOGFONT
lf.LfHeight = -MulDiv(int32(f.pointSize), int32(dpi), 72)
if f.style&FontBold > 0 {
lf.LfWeight = FW_BOLD
} else {
lf.LfWeight = 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 = DEFAULT_CHARSET
lf.LfOutPrecision = OUT_TT_PRECIS
lf.LfClipPrecision = CLIP_DEFAULT_PRECIS
lf.LfQuality = CLEARTYPE_QUALITY
lf.LfPitchAndFamily = VARIABLE_PITCH | FF_SWISS
src := syscall.StringToUTF16(f.family)
dest := lf.LfFaceName[:]
copy(dest, src)
return CreateFontIndirect(&lf)
}
// 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() {
for dpi, hFont := range f.dpi2hFont {
if dpi != 0 {
DeleteObject(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.
//
// A value of 0 returns a HFONT suitable for the screen.
func (f *Font) handleForDPI(dpi int) HFONT {
hFont := f.dpi2hFont[dpi]
if hFont == 0 {
hFont = f.createForDPI(dpi)
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
}