/
gfx-text.go
396 lines (359 loc) · 13.1 KB
/
gfx-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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
// Copyright 2012. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
////////////////////////////////////////////////////////////
//
// SFML - Simple and Fast Multimedia Library
// Copyright (C) 2007-2012 Laurent Gomila (laurent.gom@gmail.com)
//
// This software is provided 'as-is', without any express or implied warranty.
// In no event will the authors be held liable for any damages arising from the use of this software.
//
// Permission is granted to anyone to use this software for any purpose,
// including commercial applications, and to alter it and redistribute it freely,
// subject to the following restrictions:
//
// 1. The origin of this software must not be misrepresented;
// you must not claim that you wrote the original software.
// If you use this software in a product, an acknowledgment
// in the product documentation would be appreciated but is not required.
//
// 2. Altered source versions must be plainly marked as such,
// and must not be misrepresented as being the original software.
//
// 3. This notice may not be removed or altered from any source distribution.
//
////////////////////////////////////////////////////////////
package sfml
// #cgo LDFLAGS:-lcsfml-graphics -ljpeg
// #include <SFML/Graphics/Text.h>
// #include <SFML/Graphics/Export.h>
// #include <SFML/Graphics/Color.h>
// #include <SFML/Graphics/Rect.h>
// #include <SFML/Graphics/Types.h>
// #include <SFML/System/Vector2.h>
// #include <stddef.h>
// #include <stdio.h>
// extern int _UnicodeStringSize(sfUint32 *s) {
// int i = 0;
// for (i = 0; s[i] != 0; i++)
// ;
// return i;
// }
import "C"
import (
"errors"
"unsafe"
"unicode/utf8"
)
type Text struct {
Cref *C.sfText
}
// sfText styles
// Create a new text
// \return A new sfText object, or NULL if it failed
// sfText* sfText_create(void);
func NewText() (Text, error) {
txt := C.sfText_create()
if txt == nil {
return Text{nil}, errors.New("Couldn't make a text")
}
return Text{txt}, nil
}
// Copy an existing text
// \param text Text to copy
// \return Copied object
// sfText* sfText_copy(sfText* text);
func (self Text) Copy() Text {
return Text{C.sfText_copy(self.Cref)}
}
// Destroy an existing text
// \param text Text to delete
// void sfText_destroy(sfText* text);
func (self Text) Destroy() {
C.sfText_destroy(self.Cref)
}
// Set the position of a text
// This function completely overwrites the previous position.
// See sfText_move to apply an offset based on the previous position instead.
// The default position of a text Text object is (0, 0).
// \param text Text object
// \param position New position
// void sfText_setPosition(sfText* text, sfVector2f position);
func (self Text) SetPosition(x, y float32) {
v := C.sfVector2f{C.float(x), C.float(y)}
C.sfText_setPosition(self.Cref, v)
}
// Set the orientation of a text
// This function completely overwrites the previous rotation.
// See sfText_rotate to add an angle based on the previous rotation instead.
// The default rotation of a text Text object is 0.
// \param text Text object
// \param angle New rotation, in degrees
// void sfText_setRotation(sfText* text, float angle);
func (self Text) SetRotation(angle float32) {
C.sfText_setRotation(self.Cref, C.float(angle))
}
// Set the scale factors of a text
// This function completely overwrites the previous scale.
// See sfText_scale to add a factor based on the previous scale instead.
// The default scale of a text Text object is (1, 1).
// \param text Text object
// \param scale New scale factors
// void sfText_setScale(sfText* text, sfVector2f scale);
func (self Text) SetScale(x, y float32) {
v := C.sfVector2f{C.float(x), C.float(y)}
C.sfText_setScale(self.Cref, v)
}
// Set the local origin of a text
// The origin of an object defines the center point for
// all transformations (position, scale, rotation).
// The coordinates of this point must be relative to the
// top-left corner of the object, and ignore all
// transformations (position, scale, rotation).
// The default origin of a text object is (0, 0).
// \param text Text object
// \param origin New origin
// void sfText_setOrigin(sfText* text, sfVector2f origin);
func (self Text) SetOrigin(x, y float32) {
v := C.sfVector2f{C.float(x), C.float(y)}
C.sfText_setOrigin(self.Cref, v)
}
// Get the position of a text
// \param text Text object
// \return Current position
// sfVector2f sfText_getPosition(const sfText* text);
func (self Text) Position(x, y float32) (float32, float32) {
v := C.sfText_getPosition(self.Cref)
return float32(v.x), float32(v.y)
}
// Get the orientation of a text
// The rotation is always in the range [0, 360].
// \param text Text object
// \return Current rotation, in degrees
// float sfText_getRotation(const sfText* text);
func (self Text) Rotation() float32 {
return float32(C.sfText_getRotation(self.Cref))
}
// Get the current scale of a text
// \param text Text object
// \return Current scale factors
// sfVector2f sfText_getScale(const sfText* text);
func (self Text) GetScale() (x, y float32) {
v := C.sfText_getScale(self.Cref)
return float32(v.x), float32(v.y)
}
// Get the local origin of a text
// \param text Text object
// \return Current origin
// sfVector2f sfText_getOrigin(const sfText* text);
func (self Text) Origin() (x, y float32) {
v := C.sfText_getOrigin(self.Cref)
return float32(v.x), float32(v.y)
}
// Move a text by a given offset
// This function adds to the current position of the object,
// unlike sfText_setPosition which overwrites it.
// \param text Text object
// \param offset Offset
// void sfText_move(sfText* text, sfVector2f offset);
func (self Text) Move(x, y float32) {
v := C.sfVector2f{C.float(x), C.float(y)}
C.sfText_move(self.Cref, v)
}
// Rotate a text
// This function adds to the current rotation of the object,
// unlike sfText_setRotation which overwrites it.
// \param text Text object
// \param angle Angle of rotation, in degrees
// void sfText_rotate(sfText* text, float angle);
func (self Text) Rotate(angle float32) {
C.sfText_rotate(self.Cref, C.float(angle))
}
// Scale a text
// This function multiplies the current scale of the object,
// unlike sfText_setScale which overwrites it.
// \param text Text object
// \param factors Scale factors
// void sfText_scale(sfText* text, sfVector2f factors);
func (self Text) Scale(x, y float32) {
v := C.sfVector2f{C.float(x), C.float(y)}
C.sfText_scale(self.Cref, v)
}
// Get the combined transform of a text
// \param text Text object
// \return Transform combining the position/rotation/scale/origin of the object
// const sfTransform* sfText_getTransform(const sfText* text);
func (self Text) Transform() Transform {
t := C.sfText_getTransform(self.Cref)
return Transform{&t}
}
// Get the inverse of the combined transform of a text
// \param text Text object
// \return Inverse of the combined transformations applied to the object
// const sfTransform* sfText_getInverseTransform(const sfText* text);
func (self Text) InverseTransform() Transform {
t := C.sfText_getInverseTransform(self.Cref)
return Transform{&t}
}
// Set the string of a text (from an ANSI string)
// A text's string is empty by default.
// \param text Text object
// \param string New string
// void sfText_setString(sfText* text, const char* string);
func (self Text) SetString(s string) {
C.sfText_setString(self.Cref, C.CString(s))
}
// Set the string of a text (from a unicode string)
// \param text Text object
// \param string New string
// void sfText_setUnicodeString(sfText* text, const sfUint32* string);
func (self Text) SetUnicodeString(s string) {
/* Go string are encoded in UTF-8, whereas SFML unicode
strings are encoded in UTF-32, so we have to make the
conversion */
bs := []byte(s)
n := utf8.RuneCount(bs)
runes := make([]rune, n+1)
var size int
i := 0
j := 0
for ; i < n; i += 1 {
runes[i], size = utf8.DecodeRune(bs[j:])
j += size
}
runes[i] = 0 /* don't forget the null terminator */
C.sfText_setUnicodeString(self.Cref, (*C.sfUint32)(unsafe.Pointer(&runes[0])))
}
// Set the font of a text
// The \a font argument refers to a texture that must
// exist as long as the text uses it. Indeed, the text
// doesn't store its own copy of the font, but rather keeps
// a pointer to the one that you passed to this function.
// If the font is destroyed and the text tries to
// use it, the behaviour is undefined.
// Texts have a valid font by default, which the built-in
// sfFont_getDefaultFont().
// \param text Text object
// \param font New font
// void sfText_setFont(sfText* text, const sfFont* font);
func (self Text) SetFont(font Font) {
C.sfText_setFont(self.Cref, font.Cref)
}
// Set the character size of a text
// The default size is 30.
// \param text Text object
// \param size New character size, in pixels
// void sfText_setCharacterSize(sfText* text, unsigned int size);
func (self Text) SetCharacterSize(size int) {
C.sfText_setCharacterSize(self.Cref, C.uint(size))
}
// Set the style of a text
// You can pass a combination of one or more styles, for
// example sfTextBold | sfTextItalic.
// The default style is sfTextRegular.
// \param text Text object
// \param style New style
// void sfText_setStyle(sfText* text, sfUint32 style);
func (self Text) SetStyle(style uint32) {
C.sfText_setStyle(self.Cref, C.sfUint32(style))
}
// Set the global color of a text
// By default, the text's color is opaque white.
// \param text Text object
// \param color New color of the text
// void sfText_setColor(sfText* text, sfColor color);
func (self Text) SetColor(color Color) {
C.sfText_setColor(self.Cref, color.Cref)
}
// Get the text of a text (returns an ANSI string)
// \param text Text object
// \return String an a locale-dependant ANSI string
// const char* sfText_getString(const sfText* text);
func (self Text) String() string {
return C.GoString(C.sfText_getString(self.Cref))
}
// Get the string of a text (returns a unicode string)
// \param text Text object
// \return String as UTF-32
// const sfUint32* sfText_getUnicodeString(const sfText* text);
func (self Text) UnicodeString() string {
/* We have to make the inverse conversion from UTF-32 to UTF-8 */
s := C.sfText_getUnicodeString(self.Cref)
length := C._UnicodeStringSize(s)
b := C.GoBytes(unsafe.Pointer(s), length*4)
buf := make([]byte, length*4)
for i := 0; i < len(b); i += 4 {
n := uint(b[i]) + uint(b[i+1]) << 8 + uint(b[i+2]) << 16 + uint(b[i+3]) << 24
utf8.EncodeRune(buf[i:], rune(n))
}
return string(buf)
}
// Get the font used by a text
// \param text Text object
// \return Pointer to the font
// const sfFont* sfText_getFont(const sfText* text);
func (self Text) Font() Font {
return Font{C.sfText_getFont(self.Cref)}
}
// Get the size of the characters of a text
// \param text Text object
// \return Size of the characters
// unsigned int sfText_getCharacterSize(const sfText* text);
func (self Text) CharacterSize() uint {
return uint(C.sfText_getCharacterSize(self.Cref))
}
// Get the style of a text
// \param text Text object
// \return Current string style (see sfTextStyle enum)
// sfUint32 sfText_getStyle(const sfText* text);
func (self Text) Style() uint {
return uint(C.sfText_getStyle(self.Cref))
}
// Get the global color of a text
// \param text Text object
// \return Global color of the text
// sfColor sfText_getColor(const sfText* text);
func (self Text) Color() Color {
return Color{C.sfText_getColor(self.Cref)}
}
// Return the position of the \a index-th character in a text
// This function computes the visual position of a character
// from its index in the string. The returned position is
// in global coordinates (translation, rotation, scale and
// origin are applied).
// If \a index is out of range, the position of the end of
// the string is returned.
// \param text Text object
// \param index Index of the character
// \return Position of the character
// sfVector2f sfText_findCharacterPos(const sfText* text, size_t index);
func (self Text) FindCharacterPos(index uint) Vector2f {
return Vector2f{C.sfText_findCharacterPos(self.Cref, C.size_t(index))}
}
// Get the local bounding rectangle of a text
// The returned rectangle is in local coordinates, which means
// that it ignores the transformations (translation, rotation,
// scale, ...) that are applied to the entity.
// In other words, this function returns the bounds of the
// entity in the entity's coordinate system.
// \param text Text object
// \return Local bounding rectangle of the entity
// sfFloatRect sfText_getLocalBounds(const sfText* text);
func (self Text) LocalBounds() FloatRect {
r := C.sfText_getLocalBounds(self.Cref)
return FloatRect{&r}
}
// Get the global bounding rectangle of a text
// The returned rectangle is in global coordinates, which means
// that it takes in account the transformations (translation,
// rotation, scale, ...) that are applied to the entity.
// In other words, this function returns the bounds of the
// text in the global 2D world's coordinate system.
// \param text Text object
// \return Global bounding rectangle of the entity
// sfFloatRect sfText_getGlobalBounds(const sfText* text);
func (self Text) GlobalBounds() FloatRect {
r := C.sfText_getGlobalBounds(self.Cref)
return FloatRect{&r}
}