Skip to content

Commit da39581

Browse files
committed
Add a lua demo
1 parent 6434138 commit da39581

File tree

2 files changed

+72
-0
lines changed

2 files changed

+72
-0
lines changed

fontconfig-resolve.c

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
// cc fontconfig-resolve.c `pkg-config --libs --cflags fontconfig` -Wall && ./a.out
2+
// https://gist.github.com/CallumDev/7c66b3f9cf7a876ef75f#gistcomment-3355764
3+
#include <stdio.h>
4+
#include <stdlib.h>
5+
6+
#include <fontconfig/fontconfig.h>
7+
8+
int main() {
9+
FcInit();
10+
FcConfig *config = FcInitLoadConfigAndFonts();
11+
12+
// not necessarily has to be a specific name
13+
FcPattern *pat = FcNameParse((const FcChar8 *)"Arial");
14+
15+
// NECESSARY; it increases the scope of possible fonts
16+
FcConfigSubstitute(config, pat, FcMatchPattern);
17+
// NECESSARY; it increases the scope of possible fonts
18+
FcDefaultSubstitute(pat);
19+
20+
char *fontFile;
21+
FcResult result;
22+
23+
FcPattern *font = FcFontMatch(config, pat, &result);
24+
25+
if (font) {
26+
FcChar8 *file = NULL;
27+
28+
if (FcPatternGetString(font, FC_FILE, 0, &file) == FcResultMatch) {
29+
fontFile = (char *)file;
30+
printf("%s\n", fontFile);
31+
}
32+
}
33+
FcPatternDestroy(font);
34+
FcPatternDestroy(pat);
35+
FcConfigDestroy(config);
36+
FcFini();
37+
return 0;
38+
}

lua-run.c

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
// clang lua-run.c `pkg-config --libs --cflags lua` -Wall && ./a.out
2+
// https://lucasklassmann.com/blog/2019-02-02-how-to-embeddeding-lua-in-c/
3+
#include <lauxlib.h>
4+
#include <lua.h>
5+
#include <lualib.h>
6+
7+
int multiplication(lua_State *L) {
8+
int a = luaL_checkinteger(L, 1);
9+
int b = luaL_checkinteger(L, 2);
10+
lua_Integer c = a * b;
11+
lua_pushinteger(L, c);
12+
return 1;
13+
}
14+
15+
int main(int argc, char **argv) {
16+
lua_State *L = luaL_newstate();
17+
luaL_openlibs(L);
18+
lua_newtable(L);
19+
const struct luaL_Reg my_functions_registery[] = {{"mul", multiplication}};
20+
// put the registry on the stack
21+
luaL_setfuncs(L, my_functions_registery, 0);
22+
// set it to my_functions_registry
23+
lua_setglobal(L, "my_functions_registry");
24+
char *code = "print(my_functions_registry.mul(7, 8))";
25+
26+
if (luaL_loadstring(L, code) == LUA_OK) {
27+
if (lua_pcall(L, 0, 1, 0) == LUA_OK) {
28+
lua_pop(L, lua_gettop(L));
29+
}
30+
}
31+
32+
lua_close(L);
33+
return 0;
34+
}

0 commit comments

Comments
 (0)