Skip to content

Commit c749b24

Browse files
author
liuping
committed
fix blurry font issue when the DPI scale > 1.0 #65
1 parent b6f30d8 commit c749b24

File tree

2 files changed

+33
-0
lines changed

2 files changed

+33
-0
lines changed

pkg/ant.imgui/main.lua

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ function ImGuiAnt.FontAtlasBuild(list)
3030
FontDatas[#FontDatas+1] = FontData
3131
local data, size = fastio.wrap(FontData)()
3232
ImFontConfig.MergeMode = i > 1
33+
ImFontConfig.RasterizerDensity = config.RasterizerDensity
3334
atlas.AddFontFromMemoryTTF(data, size, config.SizePixels, ImFontConfig, glyphRanges(config.GlyphRanges))
3435
end
3536
atlas.Build()

tools/editor/pkg/tools.editor/init_system.lua

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,10 @@ local widget_utils = require "widget.utils"
2121

2222
local m = ecs.system 'init_system'
2323

24+
local ImGui = require "imgui"
25+
local ImGuiBackend = require "imgui.backend"
26+
local ImGuiDPIScale = 1
27+
2428
local 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

156162
end
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+
158190
function m:exit()
159191
if global_data.fileserver and global_data.fileserver.subprocess then
160192
global_data.fileserver.subprocess:wait()

0 commit comments

Comments
 (0)