wrap_Text.cpp 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  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. #include "wrap_Text.h"
  21. namespace love
  22. {
  23. namespace graphics
  24. {
  25. namespace opengl
  26. {
  27. Text *luax_checktext(lua_State *L, int idx)
  28. {
  29. return luax_checktype<Text>(L, idx, Text::type);
  30. }
  31. void luax_checkcoloredstring(lua_State *L, int idx, std::vector<Font::ColoredString> &strings)
  32. {
  33. Font::ColoredString coloredstr;
  34. coloredstr.color = Colorf(1.0f, 1.0f, 1.0f, 1.0f);
  35. if (lua_istable(L, idx))
  36. {
  37. int len = (int) luax_objlen(L, idx);
  38. for (int i = 1; i <= len; i++)
  39. {
  40. lua_rawgeti(L, idx, i);
  41. if (lua_istable(L, -1))
  42. {
  43. for (int j = 1; j <= 4; j++)
  44. lua_rawgeti(L, -j, j);
  45. coloredstr.color.r = (float) luaL_checknumber(L, -4);
  46. coloredstr.color.g = (float) luaL_checknumber(L, -3);
  47. coloredstr.color.b = (float) luaL_checknumber(L, -2);
  48. coloredstr.color.a = (float) luaL_optnumber(L, -1, 1.0);
  49. lua_pop(L, 4);
  50. }
  51. else
  52. {
  53. coloredstr.str = luaL_checkstring(L, -1);
  54. strings.push_back(coloredstr);
  55. }
  56. lua_pop(L, 1);
  57. }
  58. }
  59. else
  60. {
  61. coloredstr.str = luaL_checkstring(L, idx);
  62. strings.push_back(coloredstr);
  63. }
  64. }
  65. int w_Text_set(lua_State *L)
  66. {
  67. Text *t = luax_checktext(L, 1);
  68. if (lua_isnoneornil(L, 3))
  69. {
  70. // Single argument: unformatted text.
  71. std::vector<Font::ColoredString> newtext;
  72. luax_checkcoloredstring(L, 2, newtext);
  73. luax_catchexcept(L, [&](){ t->set(newtext); });
  74. }
  75. else
  76. {
  77. // Multiple arguments: formatted text.
  78. float wraplimit = (float) luaL_checknumber(L, 3);
  79. Font::AlignMode align;
  80. const char *alignstr = luaL_checkstring(L, 4);
  81. if (!Font::getConstant(alignstr, align))
  82. return luaL_error(L, "Invalid align mode: %s", alignstr);
  83. std::vector<Font::ColoredString> newtext;
  84. luax_checkcoloredstring(L, 2, newtext);
  85. luax_catchexcept(L, [&](){ t->set(newtext, wraplimit, align); });
  86. }
  87. return 0;
  88. }
  89. int w_Text_setf(lua_State *L)
  90. {
  91. Text *t = luax_checktext(L, 1);
  92. float wraplimit = (float) luaL_checknumber(L, 3);
  93. Font::AlignMode align;
  94. const char *alignstr = luaL_checkstring(L, 4);
  95. if (!Font::getConstant(alignstr, align))
  96. return luaL_error(L, "Invalid align mode: %s", alignstr);
  97. std::vector<Font::ColoredString> newtext;
  98. luax_checkcoloredstring(L, 2, newtext);
  99. luax_catchexcept(L, [&](){ t->set(newtext, wraplimit, align); });
  100. return 0;
  101. }
  102. int w_Text_add(lua_State *L)
  103. {
  104. Text *t = luax_checktext(L, 1);
  105. std::vector<Font::ColoredString> text;
  106. luax_checkcoloredstring(L, 2, text);
  107. float x = (float) luaL_optnumber(L, 3, 0.0);
  108. float y = (float) luaL_optnumber(L, 4, 0.0);
  109. float a = (float) luaL_optnumber(L, 5, 0.0);
  110. float sx = (float) luaL_optnumber(L, 6, 1.0);
  111. float sy = (float) luaL_optnumber(L, 7, sx);
  112. float ox = (float) luaL_optnumber(L, 8, 0.0);
  113. float oy = (float) luaL_optnumber(L, 9, 0.0);
  114. float kx = (float) luaL_optnumber(L, 10, 0.0);
  115. float ky = (float) luaL_optnumber(L, 11, 0.0);
  116. Matrix4 m(x, y, a, sx, sy, ox, oy, kx, ky);
  117. int index = 0;
  118. luax_catchexcept(L, [&](){ index = t->add(text, m); });
  119. lua_pushnumber(L, index + 1);
  120. return 1;
  121. }
  122. int w_Text_addf(lua_State *L)
  123. {
  124. Text *t = luax_checktext(L, 1);
  125. std::vector<Font::ColoredString> text;
  126. luax_checkcoloredstring(L, 2, text);
  127. float wrap = (float) luaL_checknumber(L, 3);
  128. Font::AlignMode align = Font::ALIGN_MAX_ENUM;
  129. const char *alignstr = luaL_checkstring(L, 4);
  130. if (!Font::getConstant(alignstr, align))
  131. return luaL_error(L, "Invalid align mode: %s", alignstr);
  132. float x = (float) luaL_optnumber(L, 5, 0.0);
  133. float y = (float) luaL_optnumber(L, 6, 0.0);
  134. float a = (float) luaL_optnumber(L, 7, 0.0);
  135. float sx = (float) luaL_optnumber(L, 8, 1.0);
  136. float sy = (float) luaL_optnumber(L, 9, sx);
  137. float ox = (float) luaL_optnumber(L, 10, 0.0);
  138. float oy = (float) luaL_optnumber(L, 11, 0.0);
  139. float kx = (float) luaL_optnumber(L, 12, 0.0);
  140. float ky = (float) luaL_optnumber(L, 13, 0.0);
  141. Matrix4 m(x, y, a, sx, sy, ox, oy, kx, ky);
  142. int index = 0;
  143. luax_catchexcept(L, [&](){ index = t->addf(text, wrap, align, m); });
  144. lua_pushnumber(L, index + 1);
  145. return 1;
  146. }
  147. int w_Text_clear(lua_State *L)
  148. {
  149. Text *t = luax_checktext(L, 1);
  150. luax_catchexcept(L, [&](){ t->clear(); });
  151. return 0;
  152. }
  153. int w_Text_setFont(lua_State *L)
  154. {
  155. Text *t = luax_checktext(L, 1);
  156. Font *f = luax_checktype<Font>(L, 2, Font::type);
  157. luax_catchexcept(L, [&](){ t->setFont(f); });
  158. return 0;
  159. }
  160. int w_Text_getFont(lua_State *L)
  161. {
  162. Text *t = luax_checktext(L, 1);
  163. Font *f = t->getFont();
  164. luax_pushtype(L, Font::type, f);
  165. return 1;
  166. }
  167. int w_Text_getWidth(lua_State *L)
  168. {
  169. Text *t = luax_checktext(L, 1);
  170. int index = (int) luaL_optnumber(L, 2, 0) - 1;
  171. lua_pushnumber(L, t->getWidth(index));
  172. return 1;
  173. }
  174. int w_Text_getHeight(lua_State *L)
  175. {
  176. Text *t = luax_checktext(L, 1);
  177. int index = (int) luaL_optnumber(L, 2, 0) - 1;
  178. lua_pushnumber(L, t->getHeight(index));
  179. return 1;
  180. }
  181. int w_Text_getDimensions(lua_State *L)
  182. {
  183. Text *t = luax_checktext(L, 1);
  184. int index = (int) luaL_optnumber(L, 2, 0) - 1;
  185. lua_pushnumber(L, t->getWidth(index));
  186. lua_pushnumber(L, t->getHeight(index));
  187. return 2;
  188. }
  189. static const luaL_Reg w_Text_functions[] =
  190. {
  191. { "set", w_Text_set },
  192. { "setf", w_Text_setf },
  193. { "add", w_Text_add },
  194. { "addf", w_Text_addf },
  195. { "clear", w_Text_clear },
  196. { "setFont", w_Text_setFont },
  197. { "getFont", w_Text_getFont },
  198. { "getWidth", w_Text_getWidth },
  199. { "getHeight", w_Text_getHeight },
  200. { "getDimensions", w_Text_getDimensions },
  201. { 0, 0 }
  202. };
  203. extern "C" int luaopen_text(lua_State *L)
  204. {
  205. return luax_register_type(L, Text::type, "Text", w_Text_functions, nullptr);
  206. }
  207. } // opengl
  208. } // graphics
  209. } // love