@@ -21,6 +21,10 @@ local widget_utils = require "widget.utils"
2121
2222local m = ecs .system ' init_system'
2323
24+ local ImGui = require " imgui"
25+ local ImGuiBackend = require " imgui.backend"
26+ local ImGuiDPIScale = 1
27+
2428local function start_fileserver (luaexe , path )
2529 local fsa = require " fileserver_adapter"
2630 fsa .init (luaexe , path )
@@ -32,11 +36,13 @@ local function init_font()
3236 fonts [# fonts + 1 ] = {
3337 FontPath = " /pkg/ant.resources.binary/font/Alibaba-PuHuiTi-Regular.ttf" ,
3438 SizePixels = 18 ,
39+ RasterizerDensity = ImGuiDPIScale ,
3540 GlyphRanges = { 0x0020 , 0xFFFF }
3641 }
3742 fonts [# fonts + 1 ] = {
3843 FontPath = " /pkg/tools.editor/resource/fonts/fa-solid-900.ttf" ,
3944 SizePixels = 16 ,
45+ RasterizerDensity = ImGuiDPIScale ,
4046 GlyphRanges = {
4147 0xf062 , 0xf062 , -- ICON_FA_ARROW_UP "\xef\x81\xa2" U+f062
4248 0xf063 , 0xf063 , -- ICON_FA_ARROW_DOWN "\xef\x81\xa3" U+f063
@@ -155,6 +161,32 @@ function m:data_changed()
155161
156162end
157163
164+ local dpi_scale_range = {min = 0.0 , max = 2.77 , value = 1 , enabled = true }
165+ function m :start_frame ()
166+ -- GetWindowDpiScale/GetMainViewport only availbled in start_frame/end_frame
167+ if dpi_scale_range .enabled then
168+ dpi_scale_range .value = ImGui .GetWindowDpiScale ()
169+ end
170+ end
171+
172+ function m :final ()
173+ -- ImGui says: Can not modify a locked ImFontAtlas between NewFrame() and EndFrame/Render()
174+ if dpi_scale_range .enabled then
175+ -- calculate DPI scale
176+ local min = dpi_scale_range .min
177+ local max = dpi_scale_range .max
178+ local value = dpi_scale_range .value
179+ ImGuiDPIScale = math.max (min , math.min (max , value ))
180+ dpi_scale_range .enabled = false
181+
182+ -- rebuild font atlas
183+ init_font ()
184+ ImGuiBackend .RenderCreateFontsTexture ()
185+
186+ log .warn (string.format (' rebuild the font atlas due to the DPI scale changed dpi:1.0->%.2f' , value ))
187+ end
188+ end
189+
158190function m :exit ()
159191 if global_data .fileserver and global_data .fileserver .subprocess then
160192 global_data .fileserver .subprocess :wait ()
0 commit comments