-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathrd.go
106 lines (85 loc) · 2.51 KB
/
rd.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
package truetype
import (
"errors"
"fmt"
"github.com/benoitkugler/textlayout/fonts"
)
var _ fonts.FaceRenderer = (*Font)(nil)
func (sbix tableSbix) glyphData(gid GID, xPpem, yPpem uint16) (fonts.GlyphBitmap, error) {
st := sbix.chooseStrike(xPpem, yPpem)
if st == nil {
return fonts.GlyphBitmap{}, errors.New("empty 'sbix' table")
}
glyph := st.getGlyph(gid, 0)
if glyph.isNil() {
return fonts.GlyphBitmap{}, fmt.Errorf("no glyph %d in 'sbix' table for resolution (%d, %d)", gid, xPpem, yPpem)
}
out := fonts.GlyphBitmap{Data: glyph.data}
var err error
out.Width, out.Height, out.Format, err = glyph.decodeConfig()
return out, err
}
func (colorBitmap bitmapTable) glyphData(gid GID, xPpem, yPpem uint16) (fonts.GlyphBitmap, error) {
st := colorBitmap.chooseStrike(xPpem, yPpem)
if st == nil || st.ppemX == 0 || st.ppemY == 0 {
return fonts.GlyphBitmap{}, errors.New("empty bitmap table")
}
subtable := st.findTable(gid)
if subtable == nil {
return fonts.GlyphBitmap{}, fmt.Errorf("no glyph %d in bitmap table for resolution (%d, %d)", gid, xPpem, yPpem)
}
glyph := subtable.getImage(gid)
if glyph == nil {
return fonts.GlyphBitmap{}, fmt.Errorf("no glyph %d in bitmap table for resolution (%d, %d)", gid, xPpem, yPpem)
}
out := fonts.GlyphBitmap{
Data: glyph.image,
Width: int(glyph.metrics.width),
Height: int(glyph.metrics.height),
}
switch subtable.imageFormat() {
case 17, 18, 19: // PNG
out.Format = fonts.PNG
case 2, 5:
out.Format = fonts.BlackAndWhite
default:
return fonts.GlyphBitmap{}, fmt.Errorf("unsupported format %d in bitmap table", subtable.imageFormat())
}
return out, nil
}
// look for data in 'glyf' and 'cff' tables
func (f *Font) outlineGlyphData(gid GID) (fonts.GlyphOutline, bool) {
out, err := f.glyphDataFromCFF1(gid)
if err == nil {
return out, true
}
out, err = f.glyphDataFromGlyf(gid)
if err == nil {
return out, true
}
return fonts.GlyphOutline{}, false
}
func (f *Font) GlyphData(gid GID, xPpem, yPpem uint16) fonts.GlyphData {
var out fonts.GlyphData
// try every table
out, err := f.sbix.glyphData(gid, xPpem, yPpem)
if err == nil {
return out
}
out, err = f.bitmap.glyphData(gid, xPpem, yPpem)
if err == nil {
return out
}
out_, ok := f.svg.glyphData(gid)
if ok {
// Spec :
// For every SVG glyph description, there must be a corresponding TrueType,
// CFF or CFF2 glyph description in the font.
out_.Outline, _ = f.outlineGlyphData(gid)
return out_
}
if out, ok := f.outlineGlyphData(gid); ok {
return out
}
return nil
}