-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtext.go
55 lines (45 loc) · 1.54 KB
/
text.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
// Copyright 2017 The apiholdit Authors. All rights reserved.
package apiholdit
import (
"github.com/golang/freetype"
"github.com/golang/freetype/truetype"
)
// getFont ...
func getFont(path string) (*truetype.Font, error) {
ttf, err := Asset(path)
if err != nil {
return nil, err
}
fontTTF, err := freetype.ParseFont(ttf)
if err != nil {
return nil, err
}
return fontTTF, nil
}
// maxPointSize returns the maximum point size we can use to fit text inside
// width and height as well as the resulting text-width in pixels
func getFontFinalSize(text string, c *freetype.Context, width int, height int, marginratio float64) (float64, int, int) {
scaledWidth, scaledHeight := getFontScaledSize(width, height, marginratio)
finalFontSize := DefaultMaxFontSize
// find the biggest matching font size for the requested width
for int(c.PointToFixed(finalFontSize)/64) > scaledHeight {
finalFontSize -= 2
}
var finalWidth int
for finalWidth = width + 1; finalWidth > scaledWidth; finalFontSize -= 2 {
c.SetFontSize(finalFontSize)
textExtent, err := c.DrawString(text, freetype.Pt(0, 0))
if err != nil {
return 0, 0, 0
}
finalWidth = int(float64(textExtent.X) / 64)
}
finalHeight := int(c.PointToFixed(finalFontSize/2.0) / 64)
return finalFontSize, finalWidth, finalHeight
}
// getFontScaledSize ...
func getFontScaledSize(width int, height int, marginratio float64) (int, int) {
scaledWidth := int(float64(width) * (1.0 - marginratio))
scaledHeight := int(float64(height) * (1.0 - marginratio))
return scaledWidth, scaledHeight
}