-
Notifications
You must be signed in to change notification settings - Fork 0
/
stroker.go
155 lines (136 loc) · 4.98 KB
/
stroker.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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
package freetype
/*
#cgo pkg-config: freetype2
#include <ft2build.h>
#include FT_FREETYPE_H
#include FT_STROKER_H
*/
import "C"
const (
StrokerLinejoinRound int = C.FT_STROKER_LINEJOIN_ROUND
StrokerLinejoinBevel int = C.FT_STROKER_LINEJOIN_BEVEL
StrokerLinejoinMiterVariable int = C.FT_STROKER_LINEJOIN_MITER_VARIABLE
StrokerLinejoinMiter int = C.FT_STROKER_LINEJOIN_MITER
StrokerLinejoinMiterFixed int = C.FT_STROKER_LINEJOIN_MITER_FIXED
StrokerLinecapButt int = C.FT_STROKER_LINECAP_BUTT
StrokerLinecapRound int = C.FT_STROKER_LINECAP_ROUND
StrokerLinecapSquare int = C.FT_STROKER_LINECAP_SQUARE
StrokerBorderLeft int = C.FT_STROKER_BORDER_LEFT
StrokerBorderRight int = C.FT_STROKER_BORDER_RIGHT
)
type Stroker struct {
handle C.FT_Stroker
}
func NewStroker(library *Library) (*Stroker, error) {
var handle C.FT_Stroker
errno := C.FT_Stroker_New(library.handle, &handle)
if errno != 0 {
return nil, GetError(errno)
}
return &Stroker{handle}, nil
}
// Set resets the attributes.
func (s *Stroker) Set(radius int64, lineCap, lineJoin int, miterLimit int64) {
C.FT_Stroker_Set(s.handle, C.FT_Fixed(radius), C.FT_Stroker_LineCap(lineCap), C.FT_Stroker_LineJoin(lineJoin), C.FT_Fixed(miterLimit))
}
// Reset a stroker object without changing its attributes.
// You should call this function before beginning
// a new series of calls to Stroker.BeginSubPath or Stroker.EndSubPath.
func (s *Stroker) Rewind() {
C.FT_Stroker_Rewind(s.handle)
}
// A convenience function used to parse a whole outline with the stroker.
// The resulting outline(s) can be retrieved later by functions
// like Stroker.GetCounts and Stroker.Export.
func (s *Stroker) ParseOutline(outline *Outline, opened bool) error {
var o C.FT_Bool
if opened {
o = 1
}
errno := C.FT_Stroker_ParseOutline(s.handle, &outline.handle, o)
if errno != 0 {
return GetError(errno)
}
return nil
}
// Start a new sub-path in the stroker.
func (s *Stroker) BeginSubPath(to Vector, open bool) error {
var o C.FT_Bool
if open {
o = 1
}
errno := C.FT_Stroker_BeginSubPath(s.handle, &to.handle, o)
if errno != 0 {
return GetError(errno)
}
return nil
}
// Close the current sub-path in the stroker.
func (s *Stroker) EndSubPath() error {
errno := C.FT_Stroker_EndSubPath(s.handle)
if errno != 0 {
return GetError(errno)
}
return nil
}
// ‘Draw’ a single line segment in the stroker's current sub-path, from the last position.
func (s *Stroker) LineTo(to *Vector) error {
errno := C.FT_Stroker_LineTo(s.handle, &to.handle)
if errno != 0 {
return GetError(errno)
}
return nil
}
// ‘Draw’ a single quadratic Bézier in the stroker's current sub-path, from the last position.
func (s *Stroker) ConicTo(control, to *Vector) error {
errno := C.FT_Stroker_ConicTo(s.handle, &control.handle, &to.handle)
if errno != 0 {
return GetError(errno)
}
return nil
}
// ‘Draw’ a single cubic Bézier in the stroker's current sub-path, from the last position.
func (s *Stroker) CubicTo(control1, control2, to *Vector) error {
errno := C.FT_Stroker_CubicTo(s.handle, &control1.handle, &control2.handle, &to.handle)
if errno != 0 {
return GetError(errno)
}
return nil
}
// Call this function once you have finished parsing your paths with the stroker.
// It returns the number of points and contours necessary to export one of the ‘border’ or ‘stroke’ outlines generated by the stroker.
func (s *Stroker) GetBorderCounts(border int) (uint, uint, error) {
var anumPoints, anumContours C.FT_UInt
errno := C.FT_Stroker_GetBorderCounts(s.handle, C.FT_StrokerBorder(border), &anumPoints, &anumContours)
if errno != 0 {
return uint(anumPoints), uint(anumContours), GetError(errno)
}
return uint(anumPoints), uint(anumContours), nil
}
// Call this function after Stroker.GetBorderCounts to export the corresponding border to your own Outline structure.
//
// Note that this function appends the border points and contours to your outline, but does not try to resize its arrays.
func (s *Stroker) ExportBorder(border int, outline *Outline) {
C.FT_Stroker_ExportBorder(s.handle, C.FT_StrokerBorder(border), &outline.handle)
}
// Call this function once you have finished parsing your paths with the stroker.
// It returns the number of points and contours necessary to export all points/borders from the stroked outline/path.
func (s *Stroker) GetCounts() (uint, uint, error) {
var anumPoints, anumContours C.FT_UInt
errno := C.FT_Stroker_GetCounts(s.handle, &anumPoints, &anumContours)
if errno != 0 {
return uint(anumPoints), uint(anumContours), GetError(errno)
}
return uint(anumPoints), uint(anumContours), nil
}
// Call this function after Stroker.GetBorderCounts to export all borders to your own Outline structure.
//
// Note that this function appends the border points and contours to your outline,
// but does not try to resize its arrays.
func (s *Stroker) Export(outline *Outline) {
C.FT_Stroker_Export(s.handle, &outline.handle)
}
// Destroy stroker object.
func (s *Stroker) Done() {
C.FT_Stroker_Done(s.handle)
}