-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathfont.c
96 lines (81 loc) · 1.58 KB
/
font.c
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
#include "a.h"
#define FONT "Font"
typedef struct LFont LFont;
struct LFont
{
Font *f;
};
void
pushfont(lua_State *L, Font *f)
{
LFont *l;
l = (LFont*)lua_newuserdata(L, sizeof(LFont));
luaL_getmetatable(L, FONT);
lua_setmetatable(L, -2);
l->f = f;
}
Font*
checkfont(lua_State *L, int index)
{
LFont *l;
l = (LFont*)luaL_checkudata(L, index, FONT);
luaL_argcheck(L, l != NULL, index, "Font expected");
return l->f;
}
static int
font__gc(lua_State *L)
{
LFont *l;
l = (LFont*)luaL_checkudata(L, 1, FONT);
luaL_argcheck(L, l != NULL, 1, "Font expected");
if(l->f == font){
lua_pushboolean(L, 0);
return 1;
}
freefont(l->f);
free(l);
lua_pushboolean(L, 1);
return 1;
}
static int
font__tostring(lua_State *L)
{
void *p;
p = lua_touserdata(L, 1);
lua_pushfstring(L, "font: %p", p);
return 1;
}
static int
font__index(lua_State *L)
{
Font *f;
const char *s;
f = checkfont(L, 1);
s = luaL_checkstring(L, 2);
if(strncmp(s, "name", 4) == 0)
lua_pushstring(L, f->name);
else if(strncmp(s, "height", 6) == 0)
lua_pushinteger(L, f->height);
else if(strncmp(s, "ascent", 6) == 0)
lua_pushinteger(L, f->ascent);
else if(strncmp(s, "width", 5) == 0)
lua_pushinteger(L, f->width);
else if(strncmp(s, "nsub", 4) == 0)
lua_pushinteger(L, f->nsub);
else if(strncmp(s, "age", 3) == 0)
lua_pushinteger(L, f->age);
else
return 0;
return 1;
}
static const struct luaL_Reg font_funcs[] = {
{ "__gc", font__gc },
{ "__tostring", font__tostring },
{ "__index", font__index },
{ NULL, NULL },
};
void
registerfontmeta(lua_State *L)
{
createmetatable(L, FONT, font_funcs);
}