Skip to content

Commit 826df74

Browse files
committed
fix: keep text styles alive in block closures
1 parent d33c78c commit 826df74

2 files changed

Lines changed: 24 additions & 8 deletions

File tree

src/material_text.c

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1138,6 +1138,7 @@ ltext_block(lua_State *L) {
11381138
return luaL_error(L, "Text material is not registered");
11391139
}
11401140
void * font_mgr = lua_touserdata(L, 1);
1141+
int styles_arg = 0;
11411142
int fontid = 0;
11421143
int fontsize = 0;
11431144
uint32_t color = 0;
@@ -1146,6 +1147,7 @@ ltext_block(lua_State *L) {
11461147
if (lua_type(L, 2) == LUA_TSTRING) {
11471148
// font_mgr is struct styles
11481149
luaL_checktype(L, 1, LUA_TUSERDATA);
1150+
styles_arg = 1;
11491151
alignment = parse_alignment(L, 2);
11501152
} else {
11511153
luaL_checktype(L, 1, LUA_TLIGHTUSERDATA);
@@ -1160,13 +1162,21 @@ ltext_block(lua_State *L) {
11601162
color |= 0xff000000;
11611163
}
11621164

1163-
lua_pushlightuserdata(L, font_mgr); // 1
1165+
if (styles_arg) {
1166+
lua_pushvalue(L, 1);
1167+
} else {
1168+
lua_pushlightuserdata(L, font_mgr);
1169+
}
11641170
lua_pushinteger(L, fontid); // 2
11651171
lua_pushinteger(L, fontsize); // 3
11661172
lua_pushinteger(L, color); // 4
11671173
lua_pushinteger(L, alignment); // 5
11681174
lua_pushcclosure(L, ltext, 5);
1169-
lua_pushlightuserdata(L, font_mgr); // 1
1175+
if (styles_arg) {
1176+
lua_pushvalue(L, 1);
1177+
} else {
1178+
lua_pushlightuserdata(L, font_mgr);
1179+
}
11701180
lua_pushinteger(L, fontid); // 2
11711181
lua_pushinteger(L, fontsize); // 3
11721182
lua_pushinteger(L, color); // 4

test/text.lua

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -63,12 +63,18 @@ end
6363
local TEXT <const> = "[004000]Hello[n], 这个句子中有[s1]大字[n],也有[s2]小字[n]。这句话会在文本区居中。"
6464
-- size 32; color 0; alignment center
6565
-- local block, layout = mattext.block(fontcobj, fontid, 32, 0, "CV")
66-
local styles = mattext.styles(fontcobj, {
67-
{ font = fontid, size = 24, color = 0 },
68-
{ font = fontid, size = 32, color = 0x800000 },
69-
{ font = fontid, size = 16, color = 0x000080 },
70-
})
71-
local block, layout = mattext.block(styles, "CV")
66+
local function text_block()
67+
local styles = mattext.styles(fontcobj, {
68+
{ font = fontid, size = 24, color = 0 },
69+
{ font = fontid, size = 32, color = 0x800000 },
70+
{ font = fontid, size = 16, color = 0x000080 },
71+
})
72+
return mattext.block(styles, "CV")
73+
end
74+
75+
local block, layout = text_block()
76+
collectgarbage "collect"
77+
collectgarbage "collect"
7278
local label = block(TEXT, WIDTH, HEIGHT)
7379
local label_layout = layout(TEXT, WIDTH, HEIGHT)
7480

0 commit comments

Comments
 (0)