wrap_Font.cpp 4.9 KB

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