naev 0.12.6
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>
12
13#include "nlua_font.h"
14
15#include "ndata.h"
16#include "nluadef.h"
17
18/* Font metatable methods. */
19static int fontL_gc( lua_State *L );
20static int fontL_eq( lua_State *L );
21static int fontL_new( lua_State *L );
22static int fontL_height( lua_State *L );
23static int fontL_width( lua_State *L );
24static int fontL_setFilter( lua_State *L );
25static int fontL_addFallback( lua_State *L );
26
27static const luaL_Reg fontL_methods[] = {
28 { "__gc", fontL_gc },
29 { "__eq", fontL_eq },
30 { "new", fontL_new },
31 { "height", fontL_height },
32 { "width", fontL_width },
33 { "setFilter", fontL_setFilter },
34 { "addFallback", fontL_addFallback },
35 { 0, 0 } };
36
43int nlua_loadFont( nlua_env env )
44{
45 nlua_register( env, FONT_METATABLE, fontL_methods, 1 );
46 return 0;
47}
48
61glFont *lua_tofont( lua_State *L, int ind )
62{
63 return (glFont *)lua_touserdata( L, ind );
64}
65
72glFont *luaL_checkfont( lua_State *L, int ind )
73{
74 if ( lua_isfont( L, ind ) )
75 return lua_tofont( L, ind );
76 luaL_typerror( L, ind, FONT_METATABLE );
77 return NULL;
78}
79
86glFont *lua_pushfont( lua_State *L, glFont font )
87{
88 glFont *c = (glFont *)lua_newuserdata( L, sizeof( glFont ) );
89 *c = font;
90 luaL_getmetatable( L, FONT_METATABLE );
91 lua_setmetatable( L, -2 );
92 return c;
93}
94
101int lua_isfont( lua_State *L, int ind )
102{
103 int ret;
104
105 if ( lua_getmetatable( L, ind ) == 0 )
106 return 0;
107 lua_getfield( L, LUA_REGISTRYINDEX, FONT_METATABLE );
108
109 ret = 0;
110 if ( lua_rawequal( L, -1, -2 ) ) /* does it have the correct mt? */
111 ret = 1;
112
113 lua_pop( L, 2 ); /* remove both metatables */
114 return ret;
115}
116
123static int fontL_gc( lua_State *L )
124{
125 gl_freeFont( luaL_checkfont( L, 1 ) );
126 return 0;
127}
128
137static int fontL_eq( lua_State *L )
138{
139 glFont *f1, *f2;
140 f1 = luaL_checkfont( L, 1 );
141 f2 = luaL_checkfont( L, 2 );
142 lua_pushboolean( L, ( memcmp( f1, f2, sizeof( glFont ) ) == 0 ) );
143 return 1;
144}
145
156static int fontL_new( lua_State *L )
157{
158 glFont font;
159 int h;
160 const char *fname, *prefix;
161
162 if ( lua_gettop( L ) == 1 ) {
163 fname = NULL;
164 h = luaL_checkint( L, 1 );
165 } else {
166 fname = luaL_optstring( L, 1, NULL );
167 h = luaL_checkint( L, 2 );
168 }
169
170 if ( fname == NULL ) {
171 fname = _( FONT_DEFAULT_PATH );
172 prefix = FONT_PATH_PREFIX;
173 } else
174 prefix = "";
175
176 if ( gl_fontInit( &font, fname, h, prefix, FONT_FLAG_DONTREUSE ) )
177 return NLUA_ERROR( L, _( "failed to load font '%s'" ), fname );
178
179 lua_pushfont( L, font );
180 lua_pushstring( L, fname );
181 lua_pushstring( L, prefix );
182 return 3;
183}
184
192static int fontL_height( lua_State *L )
193{
194 glFont *font = luaL_checkfont( L, 1 );
195 lua_pushnumber( L, font->h );
196 return 1;
197}
198
207static int fontL_width( lua_State *L )
208{
209 const glFont *font = luaL_checkfont( L, 1 );
210 const char *text = luaL_checkstring( L, 2 );
211 int width = gl_printWidthRaw( font, text );
212 lua_pushinteger( L, width );
213 return 1;
214}
215
225static int fontL_setFilter( lua_State *L )
226{
227 const glFont *font = luaL_checkfont( L, 1 );
228 const char *smin = luaL_checkstring( L, 2 );
229 const char *smag = luaL_optstring( L, 3, smin );
230 GLint min, mag;
231
232 min = gl_stringToFilter( smin );
233 mag = gl_stringToFilter( smag );
234
235 if ( min == 0 || mag == 0 )
236 NLUA_INVALID_PARAMETER( L, 2 );
237
238 gl_fontSetFilter( font, min, mag );
239
240 return 0;
241}
242
251static int fontL_addFallback( lua_State *L )
252{
253 glFont *font = luaL_checkfont( L, 1 );
254 const char *s = luaL_checkstring( L, 2 );
255 const char *prefix = luaL_optstring( L, 3, "" );
256 int ret = gl_fontAddFallback( font, s, prefix );
257 lua_pushboolean( L, !ret );
258 return 1;
259}
int gl_printWidthRaw(const glFont *ft_font, const char *text)
Gets the width that it would take to print some text.
Definition font.c:984
int gl_fontAddFallback(glFont *font, const char *fname, const char *prefix)
Adds a fallback font to a font.
Definition font.c:1776
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:1881
int gl_fontInit(glFont *font, const char *fname, const unsigned int h, const char *prefix, unsigned int flags)
Initializes a font.
Definition font.c:1670
void gl_fontSetFilter(const glFont *ft_font, GLint min, GLint mag)
Sets the minification and magnification filters for a font.
Definition font.c:1645
glFont * lua_pushfont(lua_State *L, glFont font)
Pushes a font on the stack.
Definition nlua_font.c:86
static int fontL_height(lua_State *L)
Gets the height of a font.
Definition nlua_font.c:192
static int fontL_width(lua_State *L)
Gets the width of some text for a font.
Definition nlua_font.c:207
static int fontL_addFallback(lua_State *L)
Adds a fallback to a font.
Definition nlua_font.c:251
int nlua_loadFont(nlua_env env)
Loads the font library.
Definition nlua_font.c:43
static int fontL_eq(lua_State *L)
Compares two fonts to see if they are the same.
Definition nlua_font.c:137
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:72
static int fontL_setFilter(lua_State *L)
Sets the font minification and magnification filters.
Definition nlua_font.c:225
static int fontL_new(lua_State *L)
Gets a font.
Definition nlua_font.c:156
int lua_isfont(lua_State *L, int ind)
Checks to see if ind is a font.
Definition nlua_font.c:101
glFont * lua_tofont(lua_State *L, int ind)
Lua bindings to interact with fonts.
Definition nlua_font.c:61
static const luaL_Reg fontL_methods[]
Definition nlua_font.c:27
static int fontL_gc(lua_State *L)
Frees a font.
Definition nlua_font.c:123
GLenum gl_stringToFilter(const char *s)
Gets the associated min/mag filter from a string.
Definition opengl.c:791
static const double c[]
Definition rng.c:256
Represents a font in memory.
Definition font.h:17
int h
Definition font.h:19