naev 0.11.5
nlua_font.c
Go to the documentation of this file.
1/*
2 * See Licensing and Copyright notice in naev.h
3 */
10#include <lauxlib.h>
11
12#include "naev.h"
15#include "nlua_font.h"
16
17#include "log.h"
18#include "ndata.h"
19#include "nluadef.h"
20
21/* Font metatable methods. */
22static int fontL_gc( lua_State *L );
23static int fontL_eq( lua_State *L );
24static int fontL_new( lua_State *L );
25static int fontL_height( lua_State *L );
26static int fontL_width( lua_State *L );
27static int fontL_setFilter( lua_State *L );
28static int fontL_addFallback( lua_State *L );
29static const luaL_Reg fontL_methods[] = {
30 { "__gc", fontL_gc },
31 { "__eq", fontL_eq },
32 { "new", fontL_new },
33 { "height", fontL_height },
34 { "width", fontL_width },
35 { "setFilter", fontL_setFilter },
36 { "addFallback", fontL_addFallback },
37 {0,0}
38};
46int nlua_loadFont( nlua_env env )
47{
48 nlua_register(env, FONT_METATABLE, fontL_methods, 1);
49 return 0;
50}
51
64glFont* lua_tofont( lua_State *L, int ind )
65{
66 return (glFont*) lua_touserdata(L,ind);
67}
75glFont* luaL_checkfont( lua_State *L, int ind )
76{
77 if (lua_isfont(L,ind))
78 return lua_tofont(L,ind);
79 luaL_typerror(L, ind, FONT_METATABLE);
80 return NULL;
81}
89glFont* lua_pushfont( lua_State *L, glFont font )
90{
91 glFont *c = (glFont*) lua_newuserdata(L, sizeof(glFont));
92 *c = font;
93 luaL_getmetatable(L, FONT_METATABLE);
94 lua_setmetatable(L, -2);
95 return c;
96}
104int lua_isfont( lua_State *L, int ind )
105{
106 int ret;
107
108 if (lua_getmetatable(L,ind)==0)
109 return 0;
110 lua_getfield(L, LUA_REGISTRYINDEX, FONT_METATABLE);
111
112 ret = 0;
113 if (lua_rawequal(L, -1, -2)) /* does it have the correct mt? */
114 ret = 1;
115
116 lua_pop(L, 2); /* remove both metatables */
117 return ret;
118}
119
126static int fontL_gc( lua_State *L )
127{
129 return 0;
130}
131
140static int fontL_eq( lua_State *L )
141{
142 glFont *f1, *f2;
143 f1 = luaL_checkfont(L,1);
144 f2 = luaL_checkfont(L,2);
145 lua_pushboolean( L, (memcmp( f1, f2, sizeof(glFont) )==0) );
146 return 1;
147}
148
159static int fontL_new( lua_State *L )
160{
161 glFont font;
162 int h;
163 const char *fname, *prefix;
164
165 if (lua_gettop(L)==1) {
166 fname = NULL;
167 h = luaL_checkint(L,1);
168 }
169 else {
170 fname = luaL_optstring(L,1,NULL);
171 h = luaL_checkint(L,2);
172 }
173
174 if (fname == NULL) {
175 fname = _(FONT_DEFAULT_PATH);
176 prefix = FONT_PATH_PREFIX;
177 }
178 else
179 prefix = "";
180
181 if (gl_fontInit( &font, fname, h, prefix, FONT_FLAG_DONTREUSE ))
182 return NLUA_ERROR(L, _("failed to load font '%s'"), fname);
183
184 lua_pushfont( L, font );
185 lua_pushstring( L, fname );
186 lua_pushstring( L, prefix );
187 return 3;
188}
189
197static int fontL_height( lua_State *L )
198{
199 glFont *font = luaL_checkfont(L,1);
200 lua_pushnumber(L, font->h);
201 return 1;
202}
203
212static int fontL_width( lua_State *L )
213{
214 const glFont *font = luaL_checkfont(L,1);
215 const char *text = luaL_checkstring(L,2);
216 int width = gl_printWidthRaw( font, text );
217 lua_pushinteger(L, width);
218 return 1;
219}
220
229static int fontL_setFilter( lua_State *L )
230{
231 const glFont *font = luaL_checkfont(L,1);
232 const char *smin = luaL_checkstring(L,2);
233 const char *smag = luaL_optstring(L,3,smin);
234 GLint min, mag;
235
236 min = gl_stringToFilter( smin );
237 mag = gl_stringToFilter( smag );
238
239 if (min==0 || mag==0)
240 NLUA_INVALID_PARAMETER(L,2);
241
242 gl_fontSetFilter( font, min, mag );
243
244 return 0;
245}
246
255static int fontL_addFallback( lua_State *L )
256{
257 glFont *font = luaL_checkfont(L,1);
258 const char *s = luaL_checkstring(L,2);
259 const char *prefix = luaL_optstring(L,3,"");
260 int ret = gl_fontAddFallback( font, s, prefix );
261 lua_pushboolean(L, !ret);
262 return 1;
263}
int gl_printWidthRaw(const glFont *ft_font, const char *text)
Gets the width that it would take to print some text.
Definition font.c:961
int gl_fontAddFallback(glFont *font, const char *fname, const char *prefix)
Adds a fallback font to a font.
Definition font.c:1618
void gl_freeFont(glFont *font)
Frees a loaded font. Caution: its glFontStash still has a slot in avail_fonts. At the time of writing...
Definition font.c:1723
int gl_fontInit(glFont *font, const char *fname, const unsigned int h, const char *prefix, unsigned int flags)
Initializes a font.
Definition font.c:1517
void gl_fontSetFilter(const glFont *ft_font, GLint min, GLint mag)
Sets the minification and magnification filters for a font.
Definition font.c:1492
Header file with generic functions and naev-specifics.
glFont * lua_pushfont(lua_State *L, glFont font)
Pushes a font on the stack.
Definition nlua_font.c:89
static int fontL_height(lua_State *L)
Gets the height of a font.
Definition nlua_font.c:197
static int fontL_width(lua_State *L)
Gets the width of some text for a font.
Definition nlua_font.c:212
static int fontL_addFallback(lua_State *L)
Adds a fallback to a font.
Definition nlua_font.c:255
int nlua_loadFont(nlua_env env)
Loads the font library.
Definition nlua_font.c:46
static int fontL_eq(lua_State *L)
Compares two fonts to see if they are the same.
Definition nlua_font.c:140
glFont * luaL_checkfont(lua_State *L, int ind)
Gets font at index or raises error if there is no font at index.
Definition nlua_font.c:75
static int fontL_setFilter(lua_State *L)
Sets the font minification and magnification filters.
Definition nlua_font.c:229
static int fontL_new(lua_State *L)
Gets a font.
Definition nlua_font.c:159
int lua_isfont(lua_State *L, int ind)
Checks to see if ind is a font.
Definition nlua_font.c:104
glFont * lua_tofont(lua_State *L, int ind)
Lua bindings to interact with fonts.
Definition nlua_font.c:64
static const luaL_Reg fontL_methods[]
Definition nlua_font.c:29
static int fontL_gc(lua_State *L)
Frees a font.
Definition nlua_font.c:126
GLint gl_stringToFilter(const char *s)
Gets the associated min/mag filter from a string.
Definition opengl.c:643
static const double c[]
Definition rng.c:264
Represents a font in memory.
Definition font.h:16
int h
Definition font.h:18