-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfd.c
131 lines (118 loc) · 2.61 KB
/
fd.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
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
/*
* fd.c
* inspect glyph geometry using FreeType via Lua
* Luiz Henrique de Figueiredo <[email protected]>
* 23 May 2024 14:22:19
* This code is hereby placed in the public domain and also under the MIT license.
*/
#include "lua.h"
#include "lualib.h"
#include "lauxlib.h"
#include "outline.h"
static int outline_begin(int a, int b, void *data)
{
lua_State *L=data;
lua_pushvalue(L,-1);
lua_pushliteral(L,"B");
lua_pushinteger(L,a);
lua_pushinteger(L,b);
lua_call(L,3,0);
return 0;
}
static int outline_move_to(Point p, void *data)
{
lua_State *L=data;
lua_pushvalue(L,-1);
lua_pushliteral(L,"M");
lua_pushnumber(L,X(p));
lua_pushnumber(L,Y(p));
lua_call(L,3,0);
return 0;
}
static int outline_line_to(Point p, void *data)
{
lua_State *L=data;
lua_pushvalue(L,-1);
lua_pushliteral(L,"L");
lua_pushnumber(L,X(p));
lua_pushnumber(L,Y(p));
lua_call(L,3,0);
return 0;
}
static int outline_conic_to(Point c, Point p, void *data)
{
lua_State *L=data;
lua_pushvalue(L,-1);
lua_pushliteral(L,"Q");
lua_pushnumber(L,X(c));
lua_pushnumber(L,Y(c));
lua_pushnumber(L,X(p));
lua_pushnumber(L,Y(p));
lua_call(L,5,0);
return 0;
}
static int outline_cubic_to(Point c1, Point c2, Point p, void *data)
{
lua_State *L=data;
lua_pushvalue(L,-1);
lua_pushliteral(L,"C");
lua_pushnumber(L,X(c1));
lua_pushnumber(L,Y(c1));
lua_pushnumber(L,X(c2));
lua_pushnumber(L,Y(c2));
lua_pushnumber(L,X(p));
lua_pushnumber(L,Y(p));
lua_call(L,7,0);
return 0;
}
static int outline_end(void *data)
{
lua_State *L=data;
lua_pushvalue(L,-1);
lua_pushliteral(L,"E");
lua_call(L,1,0);
return 0;
}
#include "outline.c"
static int doit(lua_State *L)
{
lua_settop(L,3);
const char *f=luaL_checkstring(L,2);
int size=512;
int g;
if (lua_type(L,1)==LUA_TSTRING)
g=*lua_tostring(L,1);
else
g=luaL_checkinteger(L,1);
const char *rc=outline(g,f,size,L);
if (rc!=NULL) luaL_error(L,"%s failed",rc);
return 0;
}
LUALIB_API int luaopen_outline(lua_State *L)
{
lua_pushcfunction(L,doit);
return 1;
}
#ifdef STANDALONE
static void fatal(const char *format, const char *what)
{
fprintf(stderr,"fd: ");
fprintf(stderr,format,what);
fprintf(stderr,"\n");
exit(1);
}
int main(int argc, char **argv)
{
lua_State *L=luaL_newstate();
if (L==NULL) fatal("cannot create Lua state: not enough memory","");
luaL_openlibs(L);
luaL_requiref(L,"outline",luaopen_outline,0);
lua_createtable(L,2,0);
if (argc>2) { lua_pushstring(L,argv[2]); lua_rawseti(L,-2,1); }
if (argc>3) { lua_pushstring(L,argv[3]); lua_rawseti(L,-2,2); }
lua_setglobal(L,"arg");
if (luaL_dofile(L,argv[1])) fatal("%s",lua_tostring(L,-1));
lua_close(L);
return 0;
}
#endif