-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmultiface.go
118 lines (105 loc) · 3.02 KB
/
multiface.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
package multiface
import (
"github.com/golang/freetype/truetype"
"golang.org/x/image/font"
"golang.org/x/image/math/fixed"
"image"
)
type entry struct {
font *truetype.Font
face font.Face
}
type Face struct {
entries []entry
}
func (f *Face) AddTruetypeFace(face font.Face, fnt *truetype.Font) {
f.entries = append(f.entries, entry{fnt, face})
}
// AddFace adds a font.Face to the multiface.Face
//
// DO NOT USE this for Truetype fonts.
// github.com/golang/freetype/truetype.Face methods return ok = true even when the glyph could not be found in the font.
func (f *Face) AddFace(face font.Face) {
f.entries = append(f.entries, entry{nil, face})
}
func (f *Face) Close() error {
var e error
for i := range f.entries {
err := f.entries[i].face.Close()
if err != nil {
e = err
}
}
return e
}
func (f *Face) Glyph(dot fixed.Point26_6, r rune) (dr image.Rectangle, mask image.Image, maskp image.Point, advance fixed.Int26_6, ok bool) {
for i := range f.entries {
// truetype.faces don't return ok = false for missing glyphs, but it seems when .Index(r) == 0 the glyph is missing
if f.entries[i].font != nil && f.entries[i].font.Index(r) == 0 && i < len(f.entries)-1 {
continue
}
dr, mask, maskp, advance, ok = f.entries[i].face.Glyph(dot, r)
if !ok && i < len(f.entries)-1 {
continue
}
return
}
return
}
func (f *Face) GlyphBounds(r rune) (bounds fixed.Rectangle26_6, advance fixed.Int26_6, ok bool) {
for i := range f.entries {
// truetype.faces don't return ok = false for missing glyphs, but it seems when .Index(r) == 0 the glyph is missing
if f.entries[i].font != nil && f.entries[i].font.Index(r) == 0 && i < len(f.entries)-1 {
continue
}
bounds, advance, ok = f.entries[i].face.GlyphBounds(r)
if !ok && i < len(f.entries)-1 {
continue
}
return
}
return
}
func (f *Face) GlyphAdvance(r rune) (advance fixed.Int26_6, ok bool) {
for i := range f.entries {
// truetype.faces don't return ok = false for missing glyphs, but it seems when .Index(r) == 0 the glyph is missing
if f.entries[i].font != nil && f.entries[i].font.Index(r) == 0 && i < len(f.entries)-1 {
continue
}
advance, ok = f.entries[i].face.GlyphAdvance(r)
if !ok && i < len(f.entries)-1 {
continue
}
return
}
return
}
func (f *Face) Kern(r0, r1 rune) fixed.Int26_6 {
for i := range f.entries {
// truetype.faces don't return ok = false for missing glyphs, but it seems when .Index(r) == 0 the glyph is missing
if f.entries[i].font != nil && f.entries[i].font.Index(r0) == 0 && i < len(f.entries)-1 {
continue
}
if f.entries[i].font != nil && f.entries[i].font.Index(r1) == 0 && i < len(f.entries)-1 {
continue
}
var ok bool
_, ok = f.entries[i].face.GlyphAdvance(r0)
if !ok && i < len(f.entries)-1 {
continue
}
_, ok = f.entries[i].face.GlyphAdvance(r1)
if !ok && i < len(f.entries)-1 {
continue
}
return f.entries[i].face.Kern(r0, r1)
}
return 0
}
func (f *Face) Metrics() font.Metrics {
var m font.Metrics
if len(f.entries) > 0 {
m = f.entries[0].face.Metrics()
}
return m
}