-
Notifications
You must be signed in to change notification settings - Fork 1
/
font.go
142 lines (123 loc) · 4.04 KB
/
font.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
// Copyright (C) 2012-2014 by krepa098. All rights reserved.
// Use of this source code is governed by a zlib-style
// license that can be found in the license.txt file.
package gosfml2
// #include <SFML/Graphics/Font.h>
// #include <stdlib.h>
import "C"
import (
"errors"
"runtime"
"unsafe"
)
/////////////////////////////////////
/// STRUCTS
/////////////////////////////////////
type Font struct {
cptr *C.sfFont
}
/////////////////////////////////////
/// FUNCS
/////////////////////////////////////
// Font constructor
// Creates a new font from file
//
// The supported font formats are: TrueType, Type 1, CFF,
// OpenType, SFNT, X11 PCF, Windows FNT, BDF, PFR and Type 42.
// Note that this function know nothing about the standard
// fonts installed on the user's system, thus you can't
// load them directly.
func NewFontFromFile(filename string) (*Font, error) {
cFilename := C.CString(filename)
defer C.free(unsafe.Pointer(cFilename))
if cptr := C.sfFont_createFromFile(cFilename); cptr != nil {
font := &Font{cptr}
runtime.SetFinalizer(font, (*Font).destroy)
return font, nil
}
return nil, genericError
}
// Font constructor
// Creates a new font from memory
//
// The supported font formats are: TrueType, Type 1, CFF,
// OpenType, SFNT, X11 PCF, Windows FNT, BDF, PFR and Type 42.
func NewFontFromMemory(data []byte) (*Font, error) {
if len(data) == 0 {
return nil, errors.New("NewFontFromMemory: len(data)==0")
}
if cptr := C.sfFont_createFromMemory(unsafe.Pointer(&data[0]), C.size_t(len(data))); cptr != nil {
font := &Font{cptr}
runtime.SetFinalizer(font, (*Font).destroy)
return font, nil
}
return nil, genericError
}
func (this *Font) Copy() *Font {
font := &Font{C.sfFont_copy(this.cptr)}
runtime.SetFinalizer(font, (*Font).destroy)
return font
}
func (this *Font) destroy() {
globalCtxSetActive(true)
C.sfFont_destroy(this.cptr)
globalCtxSetActive(false)
}
// Retrieve a glyph of the font
//
// codePoint: Unicode code point of the character to get
// characterSize: Reference character size
// bold: Retrieve the bold version or the regular one?
//
// return The glyph corresponding to codePoint and characterSize
func (this *Font) GetGlyph(codePoint uint, characterSize uint32, bold bool) (glyph Glyph) {
glyph.fromC(C.sfFont_getGlyph(this.cptr, C.sfUint32(codePoint), C.uint(characterSize), goBool2C(bold)))
return
}
//Get the kerning offset of two glyphs
//
// The kerning is an extra offset (negative) to apply between two
// glyphs when rendering them, to make the pair look more "natural".
// For example, the pair "AV" have a special kerning to make them
// closer than other characters. Most of the glyphs pairs have a
// kerning offset of zero, though.
//
// first: Unicode code point of the first character
// second: Unicode code point of the second character
// characterSize: Reference character size
//
// return Kerning value for first and second, in pixels
func (this *Font) GetKerning(first uint32, second uint32, characterSize uint) float32 {
return float32(C.sfFont_getKerning(this.cptr, C.sfUint32(first), C.sfUint32(second), C.uint(characterSize)))
}
// Get the line spacing
//
// Line spacing is the vertical offset to apply between two
// consecutive lines of text.
//
// characterSize: Reference character size
//
// return Line spacing, in pixels
func (this *Font) GetLineSpacing(characterSize uint) float32 {
return float32(C.sfFont_getLineSpacing(this.cptr, C.uint(characterSize)))
}
// Retrieve the texture containing the loaded glyphs of a certain size
//
// The contents of the returned texture changes as more glyphs
// are requested, thus it is not very relevant.
//
// characterSize: Reference character size
//
// Texture containing the glyphs of the requested size
func (this *Font) GetTexture(characterSize uint) Texture {
return Texture{C.sfFont_getTexture(this.cptr, C.uint(characterSize))}
}
/////////////////////////////////////
/// GO <-> C
/////////////////////////////////////
func (this *Font) toCPtr() *C.sfFont {
if this != nil {
return this.cptr
}
return nil
}