wrap_Font.cpp 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. /**
  2. * Copyright (c) 2006-2015 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_Font.h"
  21. #include "Font.h"
  22. #include "freetype/Font.h"
  23. #include "wrap_GlyphData.h"
  24. #include "wrap_Rasterizer.h"
  25. #include "filesystem/wrap_Filesystem.h"
  26. namespace love
  27. {
  28. namespace font
  29. {
  30. #define instance() (Module::getInstance<Font>(Module::M_FONT))
  31. int w_newRasterizer(lua_State *L)
  32. {
  33. if (lua_isnoneornil(L, 2))
  34. {
  35. // Single number argument: use the default TrueType font.
  36. if (lua_type(L, 1) == LUA_TNUMBER)
  37. return w_newTrueTypeRasterizer(L);
  38. // Single argument of another type: call Font::newRasterizer.
  39. Rasterizer *t = nullptr;
  40. filesystem::FileData *d = filesystem::luax_getfiledata(L, 1);
  41. luax_catchexcept(L,
  42. [&]() { t = instance()->newRasterizer(d); },
  43. [&]() { d->release(); }
  44. );
  45. luax_pushtype(L, "Rasterizer", FONT_RASTERIZER_T, t);
  46. t->release();
  47. return 1;
  48. }
  49. else if (lua_type(L, 2) == LUA_TNUMBER)
  50. {
  51. // Second argument is a number: call newTrueTypeRasterizer.
  52. return w_newTrueTypeRasterizer(L);
  53. }
  54. else
  55. {
  56. // Otherwise call newBMFontRasterizer.
  57. return w_newBMFontRasterizer(L);
  58. }
  59. }
  60. int w_newTrueTypeRasterizer(lua_State *L)
  61. {
  62. Rasterizer *t = nullptr;
  63. if (lua_type(L, 1) == LUA_TNUMBER)
  64. {
  65. // First argument is a number: use the default TrueType font.
  66. int size = luaL_checkint(L, 1);
  67. luax_catchexcept(L, [&](){ t = instance()->newTrueTypeRasterizer(size); });
  68. }
  69. else
  70. {
  71. love::Data *d = nullptr;
  72. if (luax_istype(L, 1, DATA_T))
  73. d = luax_checkdata(L, 1);
  74. else
  75. d = filesystem::luax_getfiledata(L, 1);
  76. int size = luaL_optint(L, 2, 12);
  77. luax_catchexcept(L,
  78. [&]() { t = instance()->newTrueTypeRasterizer(d, size); },
  79. [&]() { d->release(); }
  80. );
  81. }
  82. luax_pushtype(L, "Rasterizer", FONT_RASTERIZER_T, t);
  83. t->release();
  84. return 1;
  85. }
  86. static void convimagedata(lua_State *L, int idx)
  87. {
  88. if (lua_isstring(L, idx) || luax_istype(L, idx, FILESYSTEM_FILE_T) || luax_istype(L, idx, FILESYSTEM_FILE_DATA_T))
  89. luax_convobj(L, idx, "image", "newImageData");
  90. }
  91. int w_newBMFontRasterizer(lua_State *L)
  92. {
  93. Rasterizer *t = nullptr;
  94. filesystem::FileData *d = filesystem::luax_getfiledata(L, 1);
  95. std::vector<image::ImageData *> images;
  96. if (lua_istable(L, 2))
  97. {
  98. for (int i = 1; i <= (int) lua_objlen(L, 2); i++)
  99. {
  100. lua_rawgeti(L, 2, i);
  101. convimagedata(L, -1);
  102. image::ImageData *id = luax_checktype<image::ImageData>(L, -1, "ImageData", IMAGE_IMAGE_DATA_T);
  103. images.push_back(id);
  104. id->retain();
  105. lua_pop(L, 1);
  106. }
  107. }
  108. else
  109. {
  110. for (int i = 2; i <= lua_gettop(L); i++)
  111. {
  112. convimagedata(L, i);
  113. image::ImageData *id = luax_checktype<image::ImageData>(L, i, "ImageData", IMAGE_IMAGE_DATA_T);
  114. images.push_back(id);
  115. id->retain();
  116. }
  117. }
  118. luax_catchexcept(L,
  119. [&]() { t = instance()->newBMFontRasterizer(d, images); },
  120. [&]() { d->release(); for (auto id : images) id->release(); }
  121. );
  122. luax_pushtype(L, "Rasterizer", FONT_RASTERIZER_T, t);
  123. t->release();
  124. return 1;
  125. }
  126. int w_newImageRasterizer(lua_State *L)
  127. {
  128. Rasterizer *t = nullptr;
  129. convimagedata(L, 1);
  130. image::ImageData *d = luax_checktype<image::ImageData>(L, 1, "ImageData", IMAGE_IMAGE_DATA_T);
  131. std::string glyphs = luax_checkstring(L, 2);
  132. luax_catchexcept(L, [&](){ t = instance()->newImageRasterizer(d, glyphs); });
  133. luax_pushtype(L, "Rasterizer", FONT_RASTERIZER_T, t);
  134. t->release();
  135. return 1;
  136. }
  137. int w_newGlyphData(lua_State *L)
  138. {
  139. Rasterizer *r = luax_checkrasterizer(L, 1);
  140. GlyphData *t = nullptr;
  141. // newGlyphData accepts a unicode character or a codepoint number.
  142. if (lua_type(L, 2) == LUA_TSTRING)
  143. {
  144. std::string glyph = luax_checkstring(L, 2);
  145. luax_catchexcept(L, [&](){ t = instance()->newGlyphData(r, glyph); });
  146. }
  147. else
  148. {
  149. uint32 g = (uint32) luaL_checknumber(L, 2);
  150. t = instance()->newGlyphData(r, g);
  151. }
  152. luax_pushtype(L, "GlyphData", FONT_GLYPH_DATA_T, t);
  153. t->release();
  154. return 1;
  155. }
  156. // List of functions to wrap.
  157. static const luaL_Reg functions[] =
  158. {
  159. { "newRasterizer", w_newRasterizer },
  160. { "newTrueTypeRasterizer", w_newTrueTypeRasterizer },
  161. { "newBMFontRasterizer", w_newBMFontRasterizer },
  162. { "newImageRasterizer", w_newImageRasterizer },
  163. { "newGlyphData", w_newGlyphData },
  164. { 0, 0 }
  165. };
  166. static const lua_CFunction types[] =
  167. {
  168. luaopen_glyphdata,
  169. luaopen_rasterizer,
  170. 0
  171. };
  172. extern "C" int luaopen_love_font(lua_State *L)
  173. {
  174. Font *instance = instance();
  175. if (instance == nullptr)
  176. {
  177. luax_catchexcept(L, [&](){ instance = new freetype::Font(); });
  178. }
  179. else
  180. instance->retain();
  181. WrappedModule w;
  182. w.module = instance;
  183. w.name = "font";
  184. w.flags = MODULE_T;
  185. w.functions = functions;
  186. w.types = types;
  187. return luax_register_module(L, w);
  188. }
  189. } // font
  190. } // love