-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathbitmaps.go
79 lines (65 loc) · 1.76 KB
/
bitmaps.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
package truetype
import (
"github.com/benoitkugler/textlayout/fonts"
)
func (t bitmapTable) availableSizes(avgWidth, upem uint16) []fonts.BitmapSize {
out := make([]fonts.BitmapSize, 0, len(t))
for _, size := range t {
v := size.sizeMetrics(avgWidth, upem)
/* only use strikes with valid PPEM values */
if v.XPpem == 0 || v.YPpem == 0 {
continue
}
out = append(out, v)
}
return out
}
func (t tableSbix) availableSizes(horizontal *TableHVhea, avgWidth, upem uint16) []fonts.BitmapSize {
out := make([]fonts.BitmapSize, 0, len(t.strikes))
for _, size := range t.strikes {
v := size.sizeMetrics(horizontal, avgWidth, upem)
/* only use strikes with valid PPEM values */
if v.XPpem == 0 || v.YPpem == 0 {
continue
}
out = append(out, v)
}
return out
}
func inferBitmapWidth(size *fonts.BitmapSize, avgWidth, upem uint16) {
size.Width = uint16((uint32(avgWidth)*uint32(size.XPpem) + uint32(upem/2)) / uint32(upem))
}
// return nil if no table is valid (or present)
func (pr *FontParser) selectBitmapTable() bitmapTable {
color, err := pr.colorBitmapTable()
if err == nil {
return color
}
gray, err := pr.grayBitmapTable()
if err == nil {
return gray
}
apple, err := pr.appleBitmapTable()
if err == nil {
return apple
}
return nil
}
// LoadBitmaps checks for the various bitmaps table and returns
// the first valid
func (font *Font) LoadBitmaps() []fonts.BitmapSize {
upem := font.Head.UnitsPerEm
avgWidth := font.OS2.XAvgCharWidth
if upem == 0 || font.OS2.Version == 0xFFFF {
avgWidth = 1
upem = 1
}
// adapted from freetype tt_face_load_sbit
if font.bitmap != nil {
return font.bitmap.availableSizes(avgWidth, upem)
}
if hori := font.hhea; hori != nil {
return font.sbix.availableSizes(hori, avgWidth, upem)
}
return nil
}