-
Notifications
You must be signed in to change notification settings - Fork 21
/
text.go
228 lines (199 loc) · 6.08 KB
/
text.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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
/* text.go contains functions for text annotation
TODO:
getters and setters for
- Stretch (redefine C type)
- Style (redefine C type)
- Resolution (two doubles)
- Decoration (redefine C type)
- Encoding (string)
- DrawSetTextInterlineSpacing(DrawingWand *,const double),
- DrawSetTextInterwordSpacing(DrawingWand *,const double),
- DrawSetGravity(DrawingWand *wand,const GravityType gravity)
*/
package canvas
/*
#cgo CFLAGS: -fopenmp -I./_include
#cgo LDFLAGS: -lMagickWand -lMagickCore
#include <wand/magick_wand.h>
*/
import "C"
import (
"unsafe"
)
type Alignment uint
const (
UndefinedAlign Alignment = Alignment(C.UndefinedAlign)
LeftAlign = Alignment(C.LeftAlign)
CenterAlign = Alignment(C.CenterAlign)
RightAlign = Alignment(C.RightAlign)
)
// structure containing all text properties for an annotation
// except the colors that are defined by FillColor and StrokeColor
type TextProperties struct {
Font string
Family string
Size float64
// Stretch C.StretchType
Weight uint
// Style C.StyleType
// Resolution [2]C.double
Alignment Alignment
Antialias bool
// Decoration C.DecorationType
// Encoding string
Kerning float64
// Interline float64
// Interword float64
UnderColor *C.PixelWand
}
// Returns a TextProperties structure.
// Parameters:
// readDefault: if false, returns an empty structure.
// if true, returns a structure set with current canvas settings
func (self *Canvas) NewTextProperties(readDefault bool) *TextProperties {
if readDefault {
cfont := C.DrawGetFont(self.drawing)
defer C.free(unsafe.Pointer(cfont))
cfamily := C.DrawGetFontFamily(self.drawing)
defer C.free(unsafe.Pointer(cfamily))
csize := C.DrawGetFontSize(self.drawing)
cweight := C.DrawGetFontWeight(self.drawing)
calignment := C.DrawGetTextAlignment(self.drawing)
cantialias := C.DrawGetTextAntialias(self.drawing)
ckerning := C.DrawGetTextKerning(self.drawing)
antialias := cantialias == C.MagickTrue
underColor := C.NewPixelWand()
C.DrawGetTextUnderColor(self.drawing, underColor)
return &TextProperties{
Font: C.GoString(cfont),
Family: C.GoString(cfamily),
Size: float64(csize),
Weight: uint(cweight),
Alignment: Alignment(calignment),
Antialias: antialias,
Kerning: float64(ckerning),
UnderColor: underColor,
}
}
return &TextProperties{
UnderColor: C.NewPixelWand(),
}
}
// Sets canvas' default TextProperties
func (self *Canvas) SetTextProperties(def *TextProperties) {
if def != nil {
self.text = def
self.SetFontFamily(def.Family)
self.SetFont(def.Font, def.Size)
self.SetFontWeight(def.Weight)
self.SetTextAlignment(def.Alignment)
self.SetTextAntialias(def.Antialias)
self.SetTextKerning(def.Kerning)
}
}
// Gets a copy of canvas' current TextProperties
func (self *Canvas) TextProperties() *TextProperties {
if self.text == nil {
return nil
}
cpy := *self.text
return &cpy
}
// Sets canvas' default font name
func (self *Canvas) SetFontName(font string) {
self.text.Font = font
cfont := C.CString(font)
defer C.free(unsafe.Pointer(cfont))
C.DrawSetFont(self.drawing, cfont)
}
// Returns canvas' current font name
func (self *Canvas) FontName() string {
return self.text.Font
}
// Sets canvas' default font family
func (self *Canvas) SetFontFamily(family string) {
self.text.Family = family
cfamily := C.CString(family)
defer C.free(unsafe.Pointer(cfamily))
C.DrawSetFontFamily(self.drawing, cfamily)
}
// Returns canvas' current font family
func (self *Canvas) FontFamily() string {
return self.text.Family
}
// Sets canvas' default font size
func (self *Canvas) SetFontSize(size float64) {
self.text.Size = size
C.DrawSetFontSize(self.drawing, C.double(size))
}
// Returns canvas' current font size
func (self *Canvas) FontSize() float64 {
return self.text.Size
}
// Sets canvas' default font weight
func (self *Canvas) SetFontWeight(weight uint) {
self.text.Weight = weight
C.DrawSetFontWeight(self.drawing, C.size_t(weight))
}
// Returns canvas' current font weight
func (self *Canvas) FontWeight() uint {
return self.text.Weight
}
// Sets canvas' font name and size.
// If font is 0-length, the current font family is not changed
// If size is <= 0, the current font size is not changed
func (self *Canvas) SetFont(font string, size float64) {
if len(font) > 0 {
self.SetFontName(font)
}
if size > 0 {
self.SetFontSize(size)
}
}
// Returns canvas' current font name and size
func (self *Canvas) Font() (string, float64) {
return self.text.Font, self.text.Size
}
// Sets canvas' default text alignment. Available values are:
// UndefinedAlign (?), LeftAlign, CenterAlign, RightAlign
func (self *Canvas) SetTextAlignment(a Alignment) {
self.text.Alignment = a
C.DrawSetTextAlignment(self.drawing, C.AlignType(a))
}
// Returns the canvas' current text aligment
func (self *Canvas) TextAlignment() Alignment {
return self.text.Alignment
}
// Sets canvas' default text antialiasing option.
func (self *Canvas) SetTextAntialias(b bool) {
self.text.Antialias = b
C.DrawSetTextAntialias(self.drawing, magickBoolean(b))
}
// Returns the canvas' current text aligment
func (self *Canvas) TextAntialias() bool {
return self.text.Antialias
}
// Sets canvas' default text antialiasing option.
func (self *Canvas) SetTextKerning(k float64) {
self.text.Kerning = k
C.DrawSetTextKerning(self.drawing, C.double(k))
}
// Returns the canvas' current text aligment
func (self *Canvas) TextKerning() float64 {
return self.text.Kerning
}
// Draws a string at the specified coordinates and using the current canvas
// Alignment.
func (self *Canvas) Annotate(text string, x, y float64) {
c_text := C.CString(text)
defer C.free(unsafe.Pointer(c_text))
C.DrawAnnotation(self.drawing, C.double(x), C.double(y), (*C.uchar)(unsafe.Pointer(c_text)))
}
// Draws a string at the specified coordinates and using the specified Text Properties
// Does not modify the canvas' default TextProperties
func (self *Canvas) AnnotateWithProperties(text string, x, y float64, prop *TextProperties) {
tmp := self.TextProperties()
self.SetTextProperties(prop)
self.Annotate(text, x, y)
self.SetTextProperties(tmp)
}