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
|
#include <lua.h>
#include <honeysuckle.h>
#include <cairo/cairo.h>
/* assimp provides its own stb_image implementation */
/*#define STB_IMAGE_IMPLEMENTATION*/
#include "stb_image.h"
#include "image.h"
int load_image(lua_State *L);
int free_image(lua_State *L);
int surface_create(lua_State *L);
int surface_destroy(lua_State *L);
int surface_get_data(lua_State *L);
int context_create(lua_State *L);
int context_destroy(lua_State *L);
int context_select_font_face(lua_State *L);
int context_set_font_size(lua_State *L);
int context_show_text(lua_State *L);
int context_set_source_rgb(lua_State *L);
int context_move_to(lua_State *L);
void setup_image(lua_State *L, int honey_index)
{
hs_create_table(L,
/* basic images */
hs_str_cfunc("load", load_image),
hs_str_cfunc("destroy", free_image),
/* cairo bindings */
hs_str_cfunc("surface_create", surface_create),
hs_str_cfunc("surface_destroy", surface_destroy),
hs_str_cfunc("surface_get_data", surface_get_data),
hs_str_cfunc("context_create", context_create),
hs_str_cfunc("context_destroy", context_destroy),
hs_str_cfunc("context_select_font_face", context_select_font_face),
hs_str_cfunc("context_set_font_size", context_set_font_size),
hs_str_cfunc("context_show_text", context_show_text),
hs_str_cfunc("context_set_source_rgb", context_set_source_rgb),
hs_str_cfunc("context_move_to", context_move_to),
/* format enum */
hs_str_int("FORMAT_ARGB32", CAIRO_FORMAT_ARGB32),
/* font enums */
hs_str_int("FONT_SLANT_NORMAL", CAIRO_FONT_SLANT_NORMAL),
hs_str_int("FONT_WEIGHT_NORMAL", CAIRO_FONT_WEIGHT_NORMAL),
);
lua_setfield(L, honey_index, "image");
}
/* --===== basic images =====-- */
int load_image(lua_State *L)
{
char *filename;
lua_Integer requested_channels;
hs_parse_args(L, hs_str(filename), hs_int(requested_channels));
int width, height, channels;
unsigned char *data = stbi_load(filename, &width, &height, &channels, requested_channels);
if (data == NULL) hs_throw_error(L, "failed to load image '%s'", filename);
lua_pushlightuserdata(L, data);
lua_pushinteger(L, width);
lua_pushinteger(L, height);
lua_pushinteger(L, channels);
return 4;
}
int free_image(lua_State *L)
{
void *data;
hs_parse_args(L, hs_light(data));
stbi_image_free(data);
return 0;
}
/* --===== cairo bindings =====-- */
int surface_create(lua_State *L)
{
lua_Integer format, width, height;
hs_parse_args(L, hs_int(format), hs_int(width), hs_int(height));
cairo_surface_t *surface = cairo_image_surface_create(format, width, height);
cairo_status_t status = cairo_surface_status(surface);
if (status != CAIRO_STATUS_SUCCESS)
hs_throw_error(L, "error creating cairo surface: %s", cairo_status_to_string(status));
lua_pushlightuserdata(L, surface);
return 1;
}
int surface_destroy(lua_State *L)
{
void *surface_ptr;
hs_parse_args(L, hs_light(surface_ptr));
cairo_surface_t *surface = surface_ptr;
cairo_surface_destroy(surface);
return 0;
}
int surface_get_data(lua_State *L)
{
void *surface_ptr;
hs_parse_args(L, hs_light(surface_ptr));
cairo_surface_t *surface = surface_ptr;
unsigned char *data = cairo_image_surface_get_data(surface);
lua_pushlightuserdata(L, data);
return 1;
}
int context_create(lua_State *L)
{
void *surface_ptr;
hs_parse_args(L, hs_light(surface_ptr));
cairo_surface_t *surface = surface_ptr;
cairo_t *cr = cairo_create(surface);
cairo_status_t status = cairo_status(cr);
if (status != CAIRO_STATUS_SUCCESS)
hs_throw_error(L, "error creating cairo context: %s", cairo_status_to_string(status));
lua_pushlightuserdata(L, cr);
return 1;
}
int context_destroy(lua_State *L)
{
void *cr_ptr;
hs_parse_args(L, hs_light(cr_ptr));
cairo_t *cr = cr_ptr;
cairo_destroy(cr);
return 0;
}
int context_select_font_face(lua_State *L)
{
void *cr_ptr;
char *family;
lua_Integer slant, weight;
hs_parse_args(L, hs_light(cr_ptr), hs_str(family), hs_int(slant), hs_int(weight));
cairo_t *cr = cr_ptr;
cairo_select_font_face(cr, family, slant, weight);
return 0;
}
int context_set_font_size(lua_State *L)
{
void *cr_ptr;
lua_Number size;
hs_parse_args(L, hs_light(cr_ptr), hs_num(size));
cairo_t *cr = cr_ptr;
cairo_set_font_size(cr, size);
return 0;
}
int context_show_text(lua_State *L)
{
void *cr_ptr;
char *str;
hs_parse_args(L, hs_light(cr_ptr), hs_str(str));
cairo_t *cr = cr_ptr;
cairo_show_text(cr, str);
return 0;
}
int context_set_source_rgb(lua_State *L)
{
void *cr_ptr;
lua_Number r, g, b;
hs_parse_args(L, hs_light(cr_ptr), hs_num(r), hs_num(g), hs_num(b));
cairo_t *cr = cr_ptr;
cairo_set_source_rgb(cr, r, g, b);
return 0;
}
int context_move_to(lua_State *L)
{
void *cr_ptr;
lua_Number x, y;
hs_parse_args(L, hs_light(cr_ptr), hs_num(x), hs_num(y));
cairo_t *cr = cr_ptr;
cairo_move_to(cr, x, y);
return 0;
}
|