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