2 * Copyright (c) 2024 Netflix, Inc.
4 * SPDX-License-Identifier: BSD-2-Clause
7 /* Copied from a file that likely shoulve have had this at the top */
9 * SPDX-License-Identifier: BSD-2-Clause
11 * Copyright 2020 Toomas Soome
12 * Copyright 2020 RackTop Systems, Inc.
14 * Redistribution and use in source and binary forms, with or without
15 * modification, are permitted provided that the following conditions
17 * 1. Redistributions of source code must retain the above copyright
18 * notice, this list of conditions and the following disclaimer.
19 * 2. Redistributions in binary form must reproduce the above copyright
20 * notice, this list of conditions and the following disclaimer in the
21 * documentation and/or other materials provided with the distribution.
23 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
43 * put image using terminal coordinates.
46 lua_term_putimage(lua_State *L)
50 uint32_t x1, y1, x2, y2, f;
51 int nargs, ret = 0, error;
53 nargs = lua_gettop(L);
55 lua_pushboolean(L, 0);
59 name = luaL_checkstring(L, 1);
60 x1 = luaL_checknumber(L, 2);
61 y1 = luaL_checknumber(L, 3);
62 x2 = luaL_checknumber(L, 4);
63 y2 = luaL_checknumber(L, 5);
64 f = luaL_checknumber(L, 6);
66 x1 = gfx_state.tg_origin.tp_col + x1 * gfx_state.tg_font.vf_width;
67 y1 = gfx_state.tg_origin.tp_row + y1 * gfx_state.tg_font.vf_height;
69 x2 = gfx_state.tg_origin.tp_col +
70 x2 * gfx_state.tg_font.vf_width;
73 y2 = gfx_state.tg_origin.tp_row +
74 y2 * gfx_state.tg_font.vf_height;
77 if ((error = png_open(&png, name)) != PNG_NO_ERROR) {
78 if (f & FL_PUTIMAGE_DEBUG)
79 printf("%s\n", png_error_string(error));
81 if (gfx_fb_putimage(&png, x1, y1, x2, y2, f) == 0)
83 (void) png_close(&png);
85 lua_pushboolean(L, ret);
90 lua_fb_putimage(lua_State *L)
94 uint32_t x1, y1, x2, y2, f;
95 int nargs, ret = 0, error;
97 nargs = lua_gettop(L);
99 lua_pushboolean(L, 0);
103 name = luaL_checkstring(L, 1);
104 x1 = luaL_checknumber(L, 2);
105 y1 = luaL_checknumber(L, 3);
106 x2 = luaL_checknumber(L, 4);
107 y2 = luaL_checknumber(L, 5);
108 f = luaL_checknumber(L, 6);
110 if ((error = png_open(&png, name)) != PNG_NO_ERROR) {
111 if (f & FL_PUTIMAGE_DEBUG)
112 printf("%s\n", png_error_string(error));
114 if (gfx_fb_putimage(&png, x1, y1, x2, y2, f) == 0)
116 (void) png_close(&png);
118 lua_pushboolean(L, ret);
123 lua_fb_setpixel(lua_State *L)
128 nargs = lua_gettop(L);
134 x = luaL_checknumber(L, 1);
135 y = luaL_checknumber(L, 2);
136 gfx_fb_setpixel(x, y);
141 lua_fb_line(lua_State *L)
143 uint32_t x0, y0, x1, y1, wd;
146 nargs = lua_gettop(L);
152 x0 = luaL_checknumber(L, 1);
153 y0 = luaL_checknumber(L, 2);
154 x1 = luaL_checknumber(L, 3);
155 y1 = luaL_checknumber(L, 4);
156 wd = luaL_checknumber(L, 5);
157 gfx_fb_line(x0, y0, x1, y1, wd);
162 lua_fb_bezier(lua_State *L)
164 uint32_t x0, y0, x1, y1, x2, y2, width;
167 nargs = lua_gettop(L);
173 x0 = luaL_checknumber(L, 1);
174 y0 = luaL_checknumber(L, 2);
175 x1 = luaL_checknumber(L, 3);
176 y1 = luaL_checknumber(L, 4);
177 x2 = luaL_checknumber(L, 5);
178 y2 = luaL_checknumber(L, 6);
179 width = luaL_checknumber(L, 7);
180 gfx_fb_bezier(x0, y0, x1, y1, x2, y2, width);
185 lua_fb_drawrect(lua_State *L)
187 uint32_t x0, y0, x1, y1, fill;
190 nargs = lua_gettop(L);
196 x0 = luaL_checknumber(L, 1);
197 y0 = luaL_checknumber(L, 2);
198 x1 = luaL_checknumber(L, 3);
199 y1 = luaL_checknumber(L, 4);
200 fill = luaL_checknumber(L, 5);
201 gfx_fb_drawrect(x0, y0, x1, y1, fill);
206 lua_term_drawrect(lua_State *L)
208 uint32_t x0, y0, x1, y1;
211 nargs = lua_gettop(L);
217 x0 = luaL_checknumber(L, 1);
218 y0 = luaL_checknumber(L, 2);
219 x1 = luaL_checknumber(L, 3);
220 y1 = luaL_checknumber(L, 4);
221 gfx_term_drawrect(x0, y0, x1, y1);
225 #define REG_SIMPLE(n) { #n, lua_ ## n }
226 static const struct luaL_Reg gfxlib[] = {
227 REG_SIMPLE(fb_bezier),
228 REG_SIMPLE(fb_drawrect),
230 REG_SIMPLE(fb_putimage),
231 REG_SIMPLE(fb_setpixel),
232 REG_SIMPLE(term_drawrect),
233 REG_SIMPLE(term_putimage),
238 luaopen_gfx(lua_State *L)
240 luaL_newlib(L, gfxlib);
250 gfx_init_md(lua_State *L)
252 luaL_requiref(L, "gfx", luaopen_gfx, 1);
253 lua_pop(L, 1); /* Remove lib */
256 * Add in the compatibility references in the loader table. Doing it with
257 * a pseudo-embedded script is easier than the raw calls.
260 "loader.fb_bezier = gfx.fb_bezier\n"
261 "loader.fb_drawrect = gfx.fb_drawrect\n"
262 "loader.fb_line = gfx.fb_line\n"
263 "loader.fb_putimage = gfx.fb_putimage\n"
264 "loader.fb_setpixel = gfx.fb_setpixel\n"
265 "loader.term_drawrect = gfx.term_drawrect\n"
266 "loader.term_putimage = gfx.term_putimage") != 0) {
268 const char *errstr = lua_tostring(L, -1);
269 errstr = errstr == NULL ? "unknown" : errstr;
270 printf("Error adding compat loader bindings: %s.\n", errstr);
274 LUA_COMPILE_SET(gfx_init_md);