wrap_Font.cpp 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. /**
  2. * Copyright (c) 2006-2013 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. // LOVE
  21. #include "wrap_Font.h"
  22. namespace love
  23. {
  24. namespace graphics
  25. {
  26. namespace opengl
  27. {
  28. Font *luax_checkfont(lua_State *L, int idx)
  29. {
  30. return luax_checktype<Font>(L, idx, "Font", GRAPHICS_FONT_T);
  31. }
  32. int w_Font_getHeight(lua_State *L)
  33. {
  34. Font *t = luax_checkfont(L, 1);
  35. lua_pushnumber(L, t->getHeight());
  36. return 1;
  37. }
  38. int w_Font_getWidth(lua_State *L)
  39. {
  40. Font *t = luax_checkfont(L, 1);
  41. const char *str = luaL_checkstring(L, 2);
  42. try
  43. {
  44. lua_pushinteger(L, t->getWidth(str));
  45. }
  46. catch(love::Exception &e)
  47. {
  48. return luaL_error(L, e.what());
  49. }
  50. return 1;
  51. }
  52. int w_Font_getWrap(lua_State *L)
  53. {
  54. Font *t = luax_checkfont(L, 1);
  55. const char *str = luaL_checkstring(L, 2);
  56. float wrap = (float) luaL_checknumber(L, 3);
  57. int max_width = 0, numlines = 0;
  58. try
  59. {
  60. std::vector<std::string> lines = t->getWrap(str, wrap, &max_width);
  61. numlines = lines.size();
  62. }
  63. catch(love::Exception &e)
  64. {
  65. return luaL_error(L, e.what());
  66. }
  67. lua_pushinteger(L, max_width);
  68. lua_pushinteger(L, numlines);
  69. return 2;
  70. }
  71. int w_Font_setLineHeight(lua_State *L)
  72. {
  73. Font *t = luax_checkfont(L, 1);
  74. float h = (float)luaL_checknumber(L, 2);
  75. t->setLineHeight(h);
  76. return 0;
  77. }
  78. int w_Font_getLineHeight(lua_State *L)
  79. {
  80. Font *t = luax_checkfont(L, 1);
  81. lua_pushnumber(L, t->getLineHeight());
  82. return 1;
  83. }
  84. int w_Font_setFilter(lua_State *L)
  85. {
  86. Font *t = luax_checkfont(L, 1);
  87. Image::Filter f = t->getFilter();
  88. const char *minstr = luaL_checkstring(L, 2);
  89. const char *magstr = luaL_optstring(L, 3, minstr);
  90. if (!Image::getConstant(minstr, f.min))
  91. return luaL_error(L, "Invalid filter mode: %s", minstr);
  92. if (!Image::getConstant(magstr, f.mag))
  93. return luaL_error(L, "Invalid filter mode: %s", magstr);
  94. f.anisotropy = (float) luaL_optnumber(L, 4, 1.0);
  95. try
  96. {
  97. t->setFilter(f);
  98. }
  99. catch(love::Exception &e)
  100. {
  101. return luaL_error(L, "%s", e.what());
  102. }
  103. return 0;
  104. }
  105. int w_Font_getFilter(lua_State *L)
  106. {
  107. Font *t = luax_checkfont(L, 1);
  108. const Image::Filter f = t->getFilter();
  109. const char *minstr;
  110. const char *magstr;
  111. Image::getConstant(f.min, minstr);
  112. Image::getConstant(f.mag, magstr);
  113. lua_pushstring(L, minstr);
  114. lua_pushstring(L, magstr);
  115. lua_pushnumber(L, f.anisotropy);
  116. return 3;
  117. }
  118. int w_Font_getAscent(lua_State *L)
  119. {
  120. Font *t = luax_checkfont(L, 1);
  121. lua_pushnumber(L, t->getAscent());
  122. return 1;
  123. }
  124. int w_Font_getDescent(lua_State *L)
  125. {
  126. Font *t = luax_checkfont(L, 1);
  127. lua_pushnumber(L, t->getDescent());
  128. return 1;
  129. }
  130. int w_Font_getBaseline(lua_State *L)
  131. {
  132. Font *t = luax_checkfont(L, 1);
  133. lua_pushnumber(L, t->getBaseline());
  134. return 1;
  135. }
  136. int w_Font_hasGlyph(lua_State *L)
  137. {
  138. Font *t = luax_checkfont(L, 1);
  139. bool hasglyph = false;
  140. try
  141. {
  142. if (lua_type(L, 2) == LUA_TSTRING)
  143. hasglyph = t->hasGlyph(luax_checkstring(L, 2));
  144. else
  145. hasglyph = t->hasGlyph((uint32) luaL_checknumber(L, 2));
  146. }
  147. catch (love::Exception &e)
  148. {
  149. return luaL_error(L, "%s", e.what());
  150. }
  151. luax_pushboolean(L, hasglyph);
  152. return 1;
  153. }
  154. static const luaL_Reg functions[] =
  155. {
  156. { "getHeight", w_Font_getHeight },
  157. { "getWidth", w_Font_getWidth },
  158. { "getWrap", w_Font_getWrap },
  159. { "setLineHeight", w_Font_setLineHeight },
  160. { "getLineHeight", w_Font_getLineHeight },
  161. { "setFilter", w_Font_setFilter },
  162. { "getFilter", w_Font_getFilter },
  163. { "getAscent", w_Font_getAscent },
  164. { "getDescent", w_Font_getDescent },
  165. { "getBaseline", w_Font_getBaseline },
  166. { "hasGlyph", w_Font_hasGlyph },
  167. { 0, 0 }
  168. };
  169. extern "C" int luaopen_font(lua_State *L)
  170. {
  171. return luax_register_type(L, "Font", functions);
  172. }
  173. } // opengl
  174. } // graphics
  175. } // love