1 /**
2  * Copyright (c) 2006-2016 LOVE Development Team
3  *
4  * This software is provided 'as-is', without any express or implied
5  * warranty.  In no event will the authors be held liable for any damages
6  * arising from the use of this software.
7  *
8  * Permission is granted to anyone to use this software for any purpose,
9  * including commercial applications, and to alter it and redistribute it
10  * freely, subject to the following restrictions:
11  *
12  * 1. The origin of this software must not be misrepresented; you must not
13  *    claim that you wrote the original software. If you use this software
14  *    in a product, an acknowledgment in the product documentation would be
15  *    appreciated but is not required.
16  * 2. Altered source versions must be plainly marked as such, and must not be
17  *    misrepresented as being the original software.
18  * 3. This notice may not be removed or altered from any source distribution.
19  **/
20 
21 #include "wrap_Rasterizer.h"
22 
23 #include "common/wrap_Data.h"
24 
25 namespace love
26 {
27 namespace font
28 {
29 
luax_checkrasterizer(lua_State * L,int idx)30 Rasterizer *luax_checkrasterizer(lua_State *L, int idx)
31 {
32 	return luax_checktype<Rasterizer>(L, idx, FONT_RASTERIZER_ID);
33 }
34 
w_Rasterizer_getHeight(lua_State * L)35 int w_Rasterizer_getHeight(lua_State *L)
36 {
37 	Rasterizer *t = luax_checkrasterizer(L, 1);
38 	lua_pushinteger(L, t->getHeight());
39 	return 1;
40 }
41 
w_Rasterizer_getAdvance(lua_State * L)42 int w_Rasterizer_getAdvance(lua_State *L)
43 {
44 	Rasterizer *t = luax_checkrasterizer(L, 1);
45 	lua_pushinteger(L, t->getAdvance());
46 	return 1;
47 }
48 
w_Rasterizer_getAscent(lua_State * L)49 int w_Rasterizer_getAscent(lua_State *L)
50 {
51 	Rasterizer *t = luax_checkrasterizer(L, 1);
52 	lua_pushinteger(L, t->getAscent());
53 	return 1;
54 }
55 
w_Rasterizer_getDescent(lua_State * L)56 int w_Rasterizer_getDescent(lua_State *L)
57 {
58 	Rasterizer *t = luax_checkrasterizer(L, 1);
59 	lua_pushinteger(L, t->getDescent());
60 	return 1;
61 }
62 
w_Rasterizer_getLineHeight(lua_State * L)63 int w_Rasterizer_getLineHeight(lua_State *L)
64 {
65 	Rasterizer *t = luax_checkrasterizer(L, 1);
66 	lua_pushinteger(L, t->getLineHeight());
67 	return 1;
68 }
69 
w_Rasterizer_getGlyphData(lua_State * L)70 int w_Rasterizer_getGlyphData(lua_State *L)
71 {
72 	Rasterizer *t = luax_checkrasterizer(L, 1);
73 	GlyphData *g = 0;
74 
75 	luax_catchexcept(L, [&]() {
76 		// getGlyphData accepts a unicode character or a codepoint number.
77 		if (lua_type(L, 2) == LUA_TSTRING)
78 		{
79 			std::string glyph = luax_checkstring(L, 2);
80 			g = t->getGlyphData(glyph);
81 		}
82 		else
83 		{
84 			uint32 glyph = (uint32) luaL_checknumber(L, 2);
85 			g = t->getGlyphData(glyph);
86 		}
87 	});
88 
89 	luax_pushtype(L, FONT_GLYPH_DATA_ID, g);
90 	g->release();
91 	return 1;
92 }
93 
w_Rasterizer_getGlyphCount(lua_State * L)94 int w_Rasterizer_getGlyphCount(lua_State *L)
95 {
96 	Rasterizer *t = luax_checkrasterizer(L, 1);
97 	lua_pushinteger(L, t->getGlyphCount());
98 	return 1;
99 }
100 
w_Rasterizer_hasGlyphs(lua_State * L)101 int w_Rasterizer_hasGlyphs(lua_State *L)
102 {
103 	Rasterizer *t = luax_checkrasterizer(L, 1);
104 
105 	bool hasglyph = false;
106 
107 	int count = lua_gettop(L) - 1;
108 	count = count < 1 ? 1 : count;
109 
110 	luax_catchexcept(L, [&]() {
111 		for (int i = 2; i < count + 2; i++)
112 		{
113 			if (lua_type(L, i) == LUA_TSTRING)
114 				hasglyph = t->hasGlyphs(luax_checkstring(L, i));
115 			else
116 				hasglyph = t->hasGlyph((uint32) luaL_checknumber(L, i));
117 
118 			if (!hasglyph)
119 				break;
120 		}
121 	});
122 
123 	luax_pushboolean(L, hasglyph);
124 	return 1;
125 }
126 
127 const luaL_Reg w_Rasterizer_functions[] =
128 {
129 	{ "getHeight", w_Rasterizer_getHeight },
130 	{ "getAdvance", w_Rasterizer_getAdvance },
131 	{ "getAscent", w_Rasterizer_getAscent },
132 	{ "getDescent", w_Rasterizer_getDescent },
133 	{ "getLineHeight", w_Rasterizer_getLineHeight },
134 	{ "getGlyphData", w_Rasterizer_getGlyphData },
135 	{ "getGlyphCount", w_Rasterizer_getGlyphCount },
136 	{ "hasGlyphs", w_Rasterizer_hasGlyphs },
137 	{ 0, 0 }
138 };
139 
luaopen_rasterizer(lua_State * L)140 extern "C" int luaopen_rasterizer(lua_State *L)
141 {
142 	return luax_register_type(L, FONT_RASTERIZER_ID, "Rasterizer", w_Rasterizer_functions, nullptr);
143 }
144 
145 } // font
146 } // love
147