forked from g3n/engine
/
font.go
340 lines (284 loc) · 9.5 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
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
// Copyright 2016 The G3N 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 text
import (
"github.com/hecate-tech/engine/math32"
"github.com/golang/freetype/truetype"
"golang.org/x/image/font"
"golang.org/x/image/math/fixed"
"image"
"image/color"
"image/draw"
"io/ioutil"
"strings"
)
// Font represents a TrueType font face.
// Attributes must be set prior to drawing.
type Font struct {
ttf *truetype.Font // The TrueType font
face font.Face // The font face
attrib FontAttributes // Internal attribute cache
fg *image.Uniform // Text color cache
bg *image.Uniform // Background color cache
changed bool // Whether attributes have changed and the font face needs to be recreated
}
// FontAttributes contains tunable attributes of a font.
type FontAttributes struct {
PointSize float64 // Point size of the font
DPI float64 // Resolution of the font in dots per inch
LineSpacing float64 // Spacing between lines (in terms of font height)
Hinting font.Hinting // Font hinting
}
// Font Hinting types.
const (
HintingNone = font.HintingNone
HintingVertical = font.HintingVertical
HintingFull = font.HintingFull
)
// NewFont creates and returns a new font object using the specified TrueType font file.
func NewFont(ttfFile string) (*Font, error) {
// Reads font bytes
fontBytes, err := ioutil.ReadFile(ttfFile)
if err != nil {
return nil, err
}
return NewFontFromData(fontBytes)
}
// NewFontFromData creates and returns a new font object from the specified TTF data.
func NewFontFromData(fontData []byte) (*Font, error) {
// Parses the font data
ttf, err := truetype.Parse(fontData)
if err != nil {
return nil, err
}
f := new(Font)
f.ttf = ttf
// Initialize with default values
f.attrib = FontAttributes{}
f.attrib.PointSize = 12
f.attrib.DPI = 72
f.attrib.LineSpacing = 1.0
f.attrib.Hinting = font.HintingNone
f.SetColor(&math32.Color4{0, 0, 0, 1})
// Create font face
f.face = truetype.NewFace(f.ttf, &truetype.Options{
Size: f.attrib.PointSize,
DPI: f.attrib.DPI,
Hinting: f.attrib.Hinting,
})
return f, nil
}
// SetPointSize sets the point size of the font.
func (f *Font) SetPointSize(size float64) {
if size == f.attrib.PointSize {
return
}
f.attrib.PointSize = size
f.changed = true
}
// SetDPI sets the resolution of the font in dots per inches (DPI).
func (f *Font) SetDPI(dpi float64) {
if dpi == f.attrib.DPI {
return
}
f.attrib.DPI = dpi
f.changed = true
}
// SetLineSpacing sets the amount of spacing between lines (in terms of font height).
func (f *Font) SetLineSpacing(spacing float64) {
if spacing == f.attrib.LineSpacing {
return
}
f.attrib.LineSpacing = spacing
f.changed = true
}
// SetHinting sets the hinting type.
func (f *Font) SetHinting(hinting font.Hinting) {
if hinting == f.attrib.Hinting {
return
}
f.attrib.Hinting = hinting
f.changed = true
}
// SetFgColor sets the text color.
func (f *Font) SetFgColor(color *math32.Color4) {
f.fg = image.NewUniform(Color4RGBA(color))
}
// SetBgColor sets the background color.
func (f *Font) SetBgColor(color *math32.Color4) {
f.bg = image.NewUniform(Color4RGBA(color))
}
// SetColor sets the text color to the specified value and makes the background color transparent.
// Note that for perfect transparency in the anti-aliased region it's important that the RGB components
// of the text and background colors match. This method handles that for the user.
func (f *Font) SetColor(fg *math32.Color4) {
f.fg = image.NewUniform(Color4RGBA(fg))
f.bg = image.NewUniform(Color4RGBA(&math32.Color4{fg.R, fg.G, fg.B, 0}))
}
// SetAttributes sets the font attributes.
func (f *Font) SetAttributes(fa *FontAttributes) {
f.SetPointSize(fa.PointSize)
f.SetDPI(fa.DPI)
f.SetLineSpacing(fa.LineSpacing)
f.SetHinting(fa.Hinting)
}
// updateFace updates the font face if parameters have changed.
func (f *Font) updateFace() {
if f.changed {
f.face = truetype.NewFace(f.ttf, &truetype.Options{
Size: f.attrib.PointSize,
DPI: f.attrib.DPI,
Hinting: f.attrib.Hinting,
})
f.changed = false
}
}
// MeasureText returns the minimum width and height in pixels necessary for an image to contain
// the specified text. The supplied text string can contain line break escape sequences (\n).
func (f *Font) MeasureText(text string) (int, int) {
// Create font drawer
f.updateFace()
d := &font.Drawer{Dst: nil, Src: f.fg, Face: f.face}
// Draw text
var width, height int
metrics := f.face.Metrics()
lineHeight := (metrics.Ascent + metrics.Descent).Ceil()
lineGap := int((f.attrib.LineSpacing - float64(1)) * float64(lineHeight))
lines := strings.Split(text, "\n")
for i, s := range lines {
d.Dot = fixed.P(0, height)
lineWidth := d.MeasureString(s).Ceil()
if lineWidth > width {
width = lineWidth
}
height += lineHeight
if i > 1 {
height += lineGap
}
}
return width, height
}
// Metrics returns the font metrics.
func (f *Font) Metrics() font.Metrics {
f.updateFace()
return f.face.Metrics()
}
// DrawText draws the specified text on a new, tightly fitting image, and returns a pointer to the image.
func (f *Font) DrawText(text string) *image.RGBA {
width, height := f.MeasureText(text)
img := image.NewRGBA(image.Rect(0, 0, width, height))
draw.Draw(img, img.Bounds(), f.bg, image.ZP, draw.Src)
f.DrawTextOnImage(text, 0, 0, img)
return img
}
// DrawTextOnImage draws the specified text on the specified image at the specified coordinates.
func (f *Font) DrawTextOnImage(text string, x, y int, dst *image.RGBA) {
f.updateFace()
d := &font.Drawer{Dst: dst, Src: f.fg, Face: f.face}
// Draw text
metrics := f.face.Metrics()
py := y + metrics.Ascent.Round()
lineHeight := (metrics.Ascent + metrics.Descent).Ceil()
lineGap := int((f.attrib.LineSpacing - float64(1)) * float64(lineHeight))
lines := strings.Split(text, "\n")
for i, s := range lines {
d.Dot = fixed.P(x, py)
d.DrawString(s)
py += lineHeight
if i > 1 {
py += lineGap
}
}
}
// Canvas is an image to draw on.
type Canvas struct {
RGBA *image.RGBA
bgColor *image.Uniform
}
// NewCanvas creates and returns a pointer to a new canvas with the
// specified width and height in pixels and background color
func NewCanvas(width, height int, bgColor *math32.Color4) *Canvas {
c := new(Canvas)
c.RGBA = image.NewRGBA(image.Rect(0, 0, width, height))
// Creates the image.Uniform for the background color
c.bgColor = image.NewUniform(Color4RGBA(bgColor))
// Draw image
draw.Draw(c.RGBA, c.RGBA.Bounds(), c.bgColor, image.ZP, draw.Src)
return c
}
// DrawText draws text at the specified position (in pixels)
// of this canvas, using the specified font.
// The supplied text string can contain line break escape sequences (\n).
func (c Canvas) DrawText(x, y int, text string, f *Font) {
f.DrawTextOnImage(text, x, y, c.RGBA)
}
// DrawTextCaret draws text at the specified position (in pixels)
// of this canvas, using the specified font, and also a caret at
// the specified line and column.
// The supplied text string can contain line break escape sequences (\n).
// TODO Implement caret as a gui.Panel in gui.Edit
func (c Canvas) DrawTextCaret(x, y int, text string, f *Font, line, col int) error {
// Creates drawer
f.updateFace()
d := &font.Drawer{Dst: c.RGBA, Src: f.fg, Face: f.face}
// Draw text
metrics := f.face.Metrics()
py := y + metrics.Ascent.Round()
lineHeight := (metrics.Ascent + metrics.Descent).Ceil()
lineGap := int((f.attrib.LineSpacing - float64(1)) * float64(lineHeight))
lines := strings.Split(text, "\n")
for l, s := range lines {
d.Dot = fixed.P(x, py)
d.DrawString(s)
// Checks for caret position
if l == line && col <= StrCount(s) {
width, _ := f.MeasureText(StrPrefix(s, col))
// Draw caret vertical line
caretH := int(f.attrib.PointSize) + 2
caretY := int(d.Dot.Y>>6) - int(f.attrib.PointSize) + 2
color := Color4RGBA(&math32.Color4{0, 0, 0, 1}) // Hardcoded to black
for j := caretY; j < caretY+caretH; j++ {
c.RGBA.Set(x+width, j, color)
}
}
py += lineHeight
if l > 1 {
py += lineGap
}
}
// TODO remove ?
// pt := freetype.Pt(font.marginX+x, font.marginY+y+int(font.ctx.PointToFixed(font.attrib.PointSize)>>6))
// for l, s := range lines {
// // Draw string
// _, err := font.ctx.DrawString(s, pt)
// if err != nil {
// return err
// }
// // Checks for caret position
// if l == line && col <= StrCount(s) {
// width, _, err := font.MeasureText(StrPrefix(s, col))
// if err != nil {
// return err
// }
// // Draw caret vertical line
// caretH := int(font.PointSize) + 2
// caretY := int(pt.Y>>6) - int(font.PointSize) + 2
// color := Color4RGBA(&math32.Color4{0, 0, 0, 1})
// for j := caretY; j < caretY+caretH; j++ {
// c.RGBA.Set(x+width, j, color)
// }
// }
// // Increment y coordinate
// pt.Y += font.ctx.PointToFixed(font.PointSize * font.LineSpacing)
// }
return nil
}
// Color4RGBA converts a math32.Color4 to Go's color.RGBA.
func Color4RGBA(c *math32.Color4) color.RGBA {
red := uint8(c.R * 0xFF)
green := uint8(c.G * 0xFF)
blue := uint8(c.B * 0xFF)
alpha := uint8(c.A * 0xFF)
return color.RGBA{red, green, blue, alpha}
}