wrap_Font.cpp 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. /**
  2. * Copyright (c) 2006-2011 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. static const luaL_Reg functions[] = {
  85. { "getHeight", w_Font_getHeight },
  86. { "getWidth", w_Font_getWidth },
  87. { "getWrap", w_Font_getWrap },
  88. { "setLineHeight", w_Font_setLineHeight },
  89. { "getLineHeight", w_Font_getLineHeight },
  90. { 0, 0 }
  91. };
  92. int luaopen_font(lua_State * L)
  93. {
  94. return luax_register_type(L, "Font", functions);
  95. }
  96. } // opengl
  97. } // graphics
  98. } // love