-
Notifications
You must be signed in to change notification settings - Fork 14
/
graphics.go
244 lines (205 loc) · 6.33 KB
/
graphics.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
package gopi
import (
"fmt"
"image"
"image/color"
"os"
"strings"
)
/*
This file contains interface defininitons for graphics implementation:
* Graphic surfaces
* Pixel Formats, Bitmaps
* Fonts
There is yet to be interfaces for drawable surfaces (3D and 2D)
*/
////////////////////////////////////////////////////////////////////////////////
// TYPES
type (
FontFlags uint16
FontSizeUnit uint
// SurfaceFlags are flags associated with renderable surface
SurfaceFlags uint16
// SurfaceFormat defines the pixel format for a surface
SurfaceFormat uint
)
type FontSize struct {
Size float32
Unit FontSizeUnit
}
//type SurfaceManagerCallback func(GraphicsContext) error
////////////////////////////////////////////////////////////////////////////////
// INTERFACES
// SurfaceManager manages graphics surfaces
type SurfaceManager interface {
//CreateBackground(GraphicsContext, SurfaceFlags) (Surface, error)
//CreateSurface(GraphicsContext, SurfaceFlags, float32, uint16, Point, Size) (Surface, error)
//CreateSurfaceWithBitmap(GraphicsContext, SurfaceFlags, Bitmap, float32, uint16, Point, Size) (Surface, error)
//DisposeSurface(GraphicsContext, Surface) error
// CreateBitmap returns a new bitmap with a specific pixel format
// and size. The size cannot be zero
CreateBitmap(SurfaceFormat, Size) (Bitmap, error)
// DisposeBitmap discards a bitmap
DisposeBitmap(Bitmap) error
// Do method is used to make graphics updates
//Do(SurfaceManagerCallback) error
}
// Surface is an on-screen surface, which embeds a drawable canvas
type Surface interface {
Origin() Point
Size() Size
Bitmap() Bitmap
}
// GraphicsContext is an opaque type
//type GraphicsContext interface{}
// Bitmap represents pixel-based images
type Bitmap interface {
image.Image
Format() SurfaceFormat
Size() Size
ClearToColor(color.Color)
SetAt(color.Color, int, int) error
}
// FontManager for font management
type FontManager interface {
// Open a font face - first face at index 0 is loaded
OpenFace(path string) (FontFace, error)
// Open a font face - indexed within file of several faces
OpenFaceAtIndex(path string, index uint) (FontFace, error)
// Open font faces at path, checking to see if individual files should
// be opened through a callback function
OpenFacesAtPath(path string, callback func(manager FontManager, path string, info os.FileInfo) bool) error
// Destroy a font face
DestroyFace(FontFace) error
// Return an array of font families which are loaded
Families() []string
// Return faces in a family and/or with a particular set of attributes
Faces(family string, flags FontFlags) []FontFace
}
// FontFace represents a typeface
type FontFace interface {
Name() string // Get Face Name (from the filename)
Index() uint // Get Face Index
NumFaces() uint // Get Number of faces within the file
NumGlyphs() uint // Number of glyphs for the face
Family() string // Return name of font family
Style() string // Return style name of font face
Flags() FontFlags // Return properties for face
}
////////////////////////////////////////////////////////////////////////////////
// CONSTANTS
const (
FONT_FLAGS_NONE FontFlags = 0x0000
FONT_FLAGS_STYLE_ITALIC FontFlags = 0x0001
FONT_FLAGS_STYLE_BOLD FontFlags = 0x0002
FONT_FLAGS_STYLE_BOLDITALIC FontFlags = 0x0003
FONT_FLAGS_STYLE_REGULAR FontFlags = 0x0004
FONT_FLAGS_STYLE_ANY FontFlags = 0x0007
)
const (
FONT_SIZE_PIXELS FontSizeUnit = iota
FONT_SIZE_POINTS
)
////////////////////////////////////////////////////////////////////////////////
// CONSTANTS
const (
SURFACE_FLAG_BITMAP SurfaceFlags = (1 << iota)
SURFACE_FLAG_OPENGL
SURFACE_FLAG_OPENGL_ES
SURFACE_FLAG_OPENGL_ES2
SURFACE_FLAG_OPENGL_ES3
SURFACE_FLAG_OPENVG
SURFACE_FLAG_NONE SurfaceFlags = 0
SURFACE_FLAG_MASK = (SURFACE_FLAG_OPENVG << 1) - 1
SURFACE_FLAG_MIN = SURFACE_FLAG_BITMAP
SURFACE_FLAG_MAX = SURFACE_FLAG_OPENVG
)
const (
SURFACE_FMT_NONE SurfaceFormat = iota
SURFACE_FMT_RGBA32 // 4 bytes per pixel with transparency
SURFACE_FMT_XRGB32 // 4 bytes per pixel without transparency
SURFACE_FMT_RGB888 // 3 bytes per pixel
SURFACE_FMT_RGB565 // 2 bytes per pixel
SURFACE_FMT_1BPP // 1 bit per pixel (Mono)
SURFACE_FMT_MAX = SURFACE_FMT_1BPP
)
////////////////////////////////////////////////////////////////////////////////
// STRINGIFY
func (f FontFlags) String() string {
if f == FONT_FLAGS_NONE {
return f.StringFlag()
}
str := ""
for v := FONT_FLAGS_STYLE_ITALIC; v <= FONT_FLAGS_STYLE_REGULAR; v <<= 1 {
if f&v == v {
str += v.StringFlag() + "|"
}
}
return strings.TrimSuffix(str, "|")
}
func (f FontFlags) StringFlag() string {
switch f {
case FONT_FLAGS_NONE:
return "FONT_FLAGS_NONE"
case FONT_FLAGS_STYLE_REGULAR:
return "FONT_FLAGS_STYLE_REGULAR"
case FONT_FLAGS_STYLE_BOLD:
return "FONT_FLAGS_STYLE_BOLD"
case FONT_FLAGS_STYLE_ITALIC:
return "FONT_FLAGS_STYLE_ITALIC"
case FONT_FLAGS_STYLE_BOLDITALIC:
return "FONT_FLAGS_STYLE_BOLDITALIC"
case FONT_FLAGS_STYLE_ANY:
return "FONT_FLAGS_STYLE_ANY"
default:
return fmt.Sprintf("[?? Invalid FontFlags value %d]", int(f))
}
}
func (f SurfaceFlags) String() string {
if f == SURFACE_FLAG_NONE {
return f.StringFlag()
}
str := ""
for v := SURFACE_FLAG_MIN; v <= SURFACE_FLAG_MAX; v <<= 1 {
if f&v == v {
str += v.StringFlag() + "|"
}
}
return strings.TrimSuffix(str, "|")
}
func (f SurfaceFlags) StringFlag() string {
switch f {
case SURFACE_FLAG_NONE:
return "SURFACE_FLAG_NONE"
case SURFACE_FLAG_BITMAP:
return "SURFACE_FLAG_BITMAP"
case SURFACE_FLAG_OPENGL:
return "SURFACE_FLAG_OPENGL"
case SURFACE_FLAG_OPENGL_ES:
return "SURFACE_FLAG_OPENGL_ES"
case SURFACE_FLAG_OPENGL_ES2:
return "SURFACE_FLAG_OPENGL_ES2"
case SURFACE_FLAG_OPENVG:
return "SURFACE_FLAG_OPENVG"
default:
return "[?? Invalid SurfaceFlags value]"
}
}
func (f SurfaceFormat) String() string {
switch f {
case SURFACE_FMT_NONE:
return "SURFACE_FMT_NONE"
case SURFACE_FMT_RGBA32:
return "SURFACE_FMT_RGBA32"
case SURFACE_FMT_XRGB32:
return "SURFACE_FMT_XRGB32"
case SURFACE_FMT_RGB888:
return "SURFACE_FMT_RGB888"
case SURFACE_FMT_RGB565:
return "SURFACE_FMT_RGB565"
case SURFACE_FMT_1BPP:
return "SURFACE_FMT_1BPP"
default:
return "[?? Invalid SurfaceFormat value]"
}
}