-
Notifications
You must be signed in to change notification settings - Fork 0
/
face_test.go
117 lines (91 loc) · 2.55 KB
/
face_test.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
package freetype
import (
. "gopkg.in/check.v1"
"image"
"io/ioutil"
//"image/png"
//"os"
)
func (s *FreetypeSuite) TestFace(c *C) {
face, err := NewFace(s.lib, "", 0)
c.Assert(err, Equals, ErrCanNotOpenResource)
c.Assert(face, IsNil)
}
func (s *FreetypeSuite) TestTutorial1(c *C) {
face, err := NewFace(s.lib, s.fileName, 0)
c.Assert(err, IsNil)
c.Assert(face, Not(IsNil))
err = face.SetCharSize(0, 16*64, 300, 300)
c.Assert(err, IsNil)
err = face.SetPixelSizes(0, 16)
c.Assert(err, IsNil)
index := face.GetCharIndex('A')
c.Check(index, Equals, uint(36))
err = face.LoadGlyph(index, LoadDefault)
c.Assert(err, IsNil)
err = face.Glyph().Render(RenderModeNormal)
c.Assert(err, IsNil)
// From memory
data, err := ioutil.ReadFile(s.fileName)
c.Assert(err, IsNil)
memoryFace, err := NewMemoryFace(s.lib, data, 0)
err = memoryFace.SetCharSize(0, 16*64, 300, 300)
c.Assert(err, IsNil)
err = memoryFace.SetPixelSizes(0, 16)
c.Assert(err, IsNil)
memoryIndex := memoryFace.GetCharIndex('A')
c.Check(memoryIndex, Equals, index)
err = memoryFace.LoadGlyph(memoryIndex, LoadDefault)
c.Assert(err, IsNil)
err = memoryFace.Glyph().Render(RenderModeNormal)
c.Assert(err, IsNil)
// free memory
err = face.Done()
c.Assert(err, IsNil)
err = memoryFace.Done()
c.Assert(err, IsNil)
}
func (s *FreetypeSuite) TestTutorial1Refined(c *C) {
face, err := NewFace(s.lib, s.fileName, 0)
c.Assert(err, IsNil)
c.Assert(face, Not(IsNil))
err = face.SetCharSize(0, 16*64, 72, 72)
c.Assert(err, IsNil)
slot := face.Glyph()
var (
penX int = 16
penY int = 16
n int
text string = "Hello, World" // "Hello, 世界" doesn't render correctly
// because the last the chars are under East Asian Scripts, CJK Unified Ideographs
// the charmap picked by default is the first one which is European Alphabets, Basic Latin
img *image.Gray = image.NewGray(image.Rect(0, 0, 256, 256))
)
for n = 0; n < len(text); n++ {
err = face.LoadChar(uint64(text[n]), LoadRender)
c.Check(err, IsNil)
drawBitmap(img, slot.Bitmap(), penX, penY, slot.BitmapLeft(), slot.BitmapTop())
penX += int(slot.Advance().X()) >> 6
}
/*
file, err := os.Create("test.png")
c.Assert(err, IsNil)
err = png.Encode(file, img)
c.Assert(err, IsNil)
*/
err = face.Done()
c.Assert(err, IsNil)
}
func drawBitmap(img *image.Gray, bitmap *Bitmap, x, y, left, top int) error {
b, err := bitmap.GrayImage()
if err != nil {
return err
}
rec := b.Bounds()
for i := 0; i < rec.Dx(); i++ {
for j := 0; j < rec.Dy(); j++ {
img.Set(x+i, y+j-top, b.At(i, j))
}
}
return nil
}