Skip to content

Commit 7ea836f

Browse files
committed
Share UI demo font atlases across density changes
1 parent b6c1afd commit 7ea836f

3 files changed

Lines changed: 43 additions & 2 deletions

File tree

‎examples/nupp/ui.nupp‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,16 @@ module ui
33
--[[Four retained UI scenes over an animated, lit sunflower field. Run `nupp task ex-ui`.]]
44

55
local function fontAt(size: number, density: number): tecs.gfx.fonts.Font
6+
const name = "ui-demo-" .. size .. "-" .. density
7+
const existing = tecs.gfx.fonts.find(name)
8+
if existing then
9+
return existing
10+
end
11+
612
return assert(
713
tecs.gfx.fonts.newTTF({
814
source = tecs.files.assetPath("fonts/JetBrainsMono-ExtraBold.ttf"),
9-
name = "ui-demo-" .. size .. "-" .. density,
15+
name = name,
1016
size = size * density,
1117
raster = "alpha"
1218
})

‎examples/nupp/uistandalone.nupp‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,16 @@ module uistandalone
33
--[[A complete centered retained interface. Run `nupp task ex-uistandalone`.]]
44

55
local function fontAt(size: number, density: number): tecs.gfx.fonts.Font
6+
const name = "ui-demo-" .. size .. "-" .. density
7+
const existing = tecs.gfx.fonts.find(name)
8+
if existing then
9+
return existing
10+
end
11+
612
return assert(
713
tecs.gfx.fonts.newTTF({
814
source = tecs.files.assetPath("fonts/JetBrainsMono-ExtraBold.ttf"),
9-
name = "ui-demo-" .. size .. "-" .. density,
15+
name = name,
1016
size = size * density,
1117
raster = "alpha"
1218
})

‎tests/uiexampletest.nupp‎

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,28 @@ local function controlBox(world: tecs.ecs.World, label: string): tecs.ui.Layout
3131
return assert(world:get(control, tecs.ui.Layout))
3232
end
3333

34+
local function checkSharedFonts(world: tecs.ecs.World, density: number): nil
35+
local fonts: {[number]: tecs.gfx.fonts.Font} = {}
36+
local texts = world:newQuery({include = {tecs.gfx.Text}})
37+
local checked = 0
38+
for candidate, count in texts:iter() do
39+
const column = assert(candidate:get(tecs.gfx.Text))
40+
for row = 1, count as integer do
41+
const text = column[row]
42+
const font = assert(text.font)
43+
nupp.test.equal(font.size, text.size * density)
44+
const shared = fonts[text.size]
45+
if shared then
46+
nupp.test.assert(font == shared, "demo labels overwrite the same image with different font atlases")
47+
checked = checked + 1
48+
else
49+
fonts[text.size] = font
50+
end
51+
end
52+
end
53+
nupp.test.assert(checked > 0, "demo must contain labels sharing a font size")
54+
end
55+
3456
local function checkSceneGeometry(world: tecs.ecs.World, scene: string): nil
3557
if scene == "Flex" then
3658
const fixed = controlBox(world, "88px")
@@ -105,6 +127,7 @@ function suite.switchesEveryOriginalSceneThroughPointerInput(): nil
105127
end
106128
local heading = assert(world:get(textEntity(world, "Tecs UI"), tecs.gfx.Text))
107129
nupp.test.equal(assert(heading.font).size, 64)
130+
checkSharedFonts(world, 2)
108131
tecs.host.shutdown(session)
109132
tecs.files.reset()
110133
end
@@ -125,6 +148,12 @@ function suite.keepsTheCenteredStandalonePanel(): nil
125148
nupp.test.equal(layout.width, 420)
126149
nupp.test.equal(layout.height, 540)
127150
nupp.test.assert(#tecs.gfx.text.glyphsOf(world, textEntity(world, "Rectangle instance")) > 0)
151+
checkSharedFonts(world, 1)
152+
for index, density in ipairs({2, 1, 2}) do
153+
tecs.host.pushResize(session, false, 960, 640, 960 * density, 640 * density, density, index, index)
154+
frame(session)
155+
checkSharedFonts(world, density)
156+
end
128157
tecs.host.shutdown(session)
129158
tecs.files.reset()
130159
end

0 commit comments

Comments
 (0)