From 48ee9270f63bc78ee060987f8dfad98d1e8dd53b Mon Sep 17 00:00:00 2001 From: 4DBug <4DBug@github.com> Date: Sat, 17 Oct 2026 09:00:40 -0500 Subject: [PATCH] Glyph baking: comparator-free edge sort (atlas pixels unchanged) --- build/ImGui.linemap.json | 2 +- build/ImGui.luau | 38 ++++++++++++++++++++++++++++++++++- build/imgui_love.linemap.json | 2 +- build/imgui_love.lua | 38 ++++++++++++++++++++++++++++++++++- imstb_truetype.lua | 38 ++++++++++++++++++++++++++++++++++- 5 files changed, 113 insertions(+), 5 deletions(-) diff --git a/build/ImGui.linemap.json b/build/ImGui.linemap.json index 1f1e6c9..ed68130 100644 --- a/build/ImGui.linemap.json +++ b/build/ImGui.linemap.json @@ -1 +1 @@ -[[46, "imgui.lua"], [22664, "imgui_demo.lua"], [34521, "imgui_draw.lua"], [42861, "imgui_h.lua"], [45864, "roblox/raster.lua"], [46340, "roblox/textures.lua"], [46511, "roblox/layers.lua"], [46848, "roblox/overlays.lua"], [46929, "roblox/render.lua"], [47774, "roblox/parallel.lua"], [48173, "imgui_impl_roblox.lua"], [48674, "imgui_impl_roblox_fonts.lua"], [55077, "imgui_internal.lua"], [59045, "imgui_tables.lua"], [64137, "imgui_widgets.lua"], [76378, "imstb_rectpack.lua"], [77055, "imstb_textedit.lua"], [78597, "imstb_truetype.lua"], [81504, "imstd_minstdio.lua"]] \ No newline at end of file +[[46, "imgui.lua"], [22664, "imgui_demo.lua"], [34521, "imgui_draw.lua"], [42861, "imgui_h.lua"], [45864, "roblox/raster.lua"], [46340, "roblox/textures.lua"], [46511, "roblox/layers.lua"], [46848, "roblox/overlays.lua"], [46929, "roblox/render.lua"], [47774, "roblox/parallel.lua"], [48173, "imgui_impl_roblox.lua"], [48674, "imgui_impl_roblox_fonts.lua"], [55077, "imgui_internal.lua"], [59045, "imgui_tables.lua"], [64137, "imgui_widgets.lua"], [76378, "imstb_rectpack.lua"], [77055, "imstb_textedit.lua"], [78597, "imstb_truetype.lua"], [81540, "imstd_minstdio.lua"]] \ No newline at end of file diff --git a/build/ImGui.luau b/build/ImGui.luau index dc768fa..bc9a455 100644 --- a/build/ImGui.luau +++ b/build/ImGui.luau @@ -80975,6 +80975,42 @@ local function stbtt__sort_edges(p) STBTT_sort(p, STBTT__COMPARE) end +-- [Luau] sorts p[lo..hi] by y0 without a comparator callback (like upstream's stbtt__sort_edges_quicksort + ins_sort) +@native +local function stbtt__sort_edges_range(p, lo, hi) + while hi - lo > 12 do + -- median of three -> p[lo] + local m = math.floor((lo + hi) / 2) + local a, b, c = p[lo], p[m], p[hi] + if b.y0 < a.y0 then a, b = b, a end + if c.y0 < b.y0 then b, c = c, b; if b.y0 < a.y0 then a, b = b, a end end + p[lo], p[m], p[hi] = b, a, c + local pivot = p[lo].y0 + local i, j = lo + 1, hi + while true do + while p[i].y0 < pivot do i = i + 1 end + while pivot < p[j].y0 do j = j - 1 end + if i >= j then break end + p[i], p[j] = p[j], p[i] + i = i + 1; j = j - 1 + end + p[lo], p[j] = p[j], p[lo] + -- recurse into the smaller side, loop on the larger + if j - lo < hi - j then + stbtt__sort_edges_range(p, lo, j - 1); lo = j + 1 + else + stbtt__sort_edges_range(p, j + 1, hi); hi = j - 1 + end + end + for i = lo + 1, hi do + local t = p[i] + local y = t.y0 + local j = i - 1 + while j >= lo and p[j].y0 > y do p[j + 1] = p[j]; j = j - 1 end + p[j + 1] = t + end +end + --- @param result stbtt__bitmap --- @param pts stbtt__point[] --- @param wcount int[] @@ -81049,7 +81085,7 @@ local function stbtt__rasterize(result, pts, wcount, windings, scale_x, scale_y, end -- now sort the edges by their highest point (should snap to integer, and then by x) - stbtt__sort_edges(e) + stbtt__sort_edges_range(e, 1, n) -- [Luau] e[n + 1..] are y0 == nil sentinels, already last -- now, traverse the scanlines and find the intersections on each scanline, use xor winding rule stbtt__rasterize_sorted_edges(result, e, n, vsubsample, off_x, off_y) diff --git a/build/imgui_love.linemap.json b/build/imgui_love.linemap.json index 6325687..01f7de7 100644 --- a/build/imgui_love.linemap.json +++ b/build/imgui_love.linemap.json @@ -1 +1 @@ -[[183, "imgui.lua"], [22801, "imgui_demo.lua"], [34658, "imgui_draw.lua"], [42998, "imgui_h.lua"], [45999, "imgui_impl_love.lua"], [46543, "imgui_internal.lua"], [50511, "imgui_tables.lua"], [55603, "imgui_widgets.lua"], [67844, "imstb_rectpack.lua"], [68521, "imstb_textedit.lua"], [70063, "imstb_truetype.lua"], [72970, "imstd_minstdio.lua"]] \ No newline at end of file +[[183, "imgui.lua"], [22801, "imgui_demo.lua"], [34658, "imgui_draw.lua"], [42998, "imgui_h.lua"], [45999, "imgui_impl_love.lua"], [46543, "imgui_internal.lua"], [50511, "imgui_tables.lua"], [55603, "imgui_widgets.lua"], [67844, "imstb_rectpack.lua"], [68521, "imstb_textedit.lua"], [70063, "imstb_truetype.lua"], [73006, "imstd_minstdio.lua"]] \ No newline at end of file diff --git a/build/imgui_love.lua b/build/imgui_love.lua index 21d2188..1d2519d 100644 --- a/build/imgui_love.lua +++ b/build/imgui_love.lua @@ -72441,6 +72441,42 @@ local function stbtt__sort_edges(p) STBTT_sort(p, STBTT__COMPARE) end +-- [Luau] sorts p[lo..hi] by y0 without a comparator callback (like upstream's stbtt__sort_edges_quicksort + ins_sort) + +local function stbtt__sort_edges_range(p, lo, hi) + while hi - lo > 12 do + -- median of three -> p[lo] + local m = math.floor((lo + hi) / 2) + local a, b, c = p[lo], p[m], p[hi] + if b.y0 < a.y0 then a, b = b, a end + if c.y0 < b.y0 then b, c = c, b; if b.y0 < a.y0 then a, b = b, a end end + p[lo], p[m], p[hi] = b, a, c + local pivot = p[lo].y0 + local i, j = lo + 1, hi + while true do + while p[i].y0 < pivot do i = i + 1 end + while pivot < p[j].y0 do j = j - 1 end + if i >= j then break end + p[i], p[j] = p[j], p[i] + i = i + 1; j = j - 1 + end + p[lo], p[j] = p[j], p[lo] + -- recurse into the smaller side, loop on the larger + if j - lo < hi - j then + stbtt__sort_edges_range(p, lo, j - 1); lo = j + 1 + else + stbtt__sort_edges_range(p, j + 1, hi); hi = j - 1 + end + end + for i = lo + 1, hi do + local t = p[i] + local y = t.y0 + local j = i - 1 + while j >= lo and p[j].y0 > y do p[j + 1] = p[j]; j = j - 1 end + p[j + 1] = t + end +end + --- @param result stbtt__bitmap --- @param pts stbtt__point[] --- @param wcount int[] @@ -72515,7 +72551,7 @@ local function stbtt__rasterize(result, pts, wcount, windings, scale_x, scale_y, end -- now sort the edges by their highest point (should snap to integer, and then by x) - stbtt__sort_edges(e) + stbtt__sort_edges_range(e, 1, n) -- [Luau] e[n + 1..] are y0 == nil sentinels, already last -- now, traverse the scanlines and find the intersections on each scanline, use xor winding rule stbtt__rasterize_sorted_edges(result, e, n, vsubsample, off_x, off_y) diff --git a/imstb_truetype.lua b/imstb_truetype.lua index 3de56a1..e949a77 100644 --- a/imstb_truetype.lua +++ b/imstb_truetype.lua @@ -2379,6 +2379,42 @@ local function stbtt__sort_edges(p) STBTT_sort(p, STBTT__COMPARE) end +-- [Luau] sorts p[lo..hi] by y0 without a comparator callback (like upstream's stbtt__sort_edges_quicksort + ins_sort) +@native +local function stbtt__sort_edges_range(p, lo, hi) + while hi - lo > 12 do + -- median of three -> p[lo] + local m = math.floor((lo + hi) / 2) + local a, b, c = p[lo], p[m], p[hi] + if b.y0 < a.y0 then a, b = b, a end + if c.y0 < b.y0 then b, c = c, b; if b.y0 < a.y0 then a, b = b, a end end + p[lo], p[m], p[hi] = b, a, c + local pivot = p[lo].y0 + local i, j = lo + 1, hi + while true do + while p[i].y0 < pivot do i = i + 1 end + while pivot < p[j].y0 do j = j - 1 end + if i >= j then break end + p[i], p[j] = p[j], p[i] + i = i + 1; j = j - 1 + end + p[lo], p[j] = p[j], p[lo] + -- recurse into the smaller side, loop on the larger + if j - lo < hi - j then + stbtt__sort_edges_range(p, lo, j - 1); lo = j + 1 + else + stbtt__sort_edges_range(p, j + 1, hi); hi = j - 1 + end + end + for i = lo + 1, hi do + local t = p[i] + local y = t.y0 + local j = i - 1 + while j >= lo and p[j].y0 > y do p[j + 1] = p[j]; j = j - 1 end + p[j + 1] = t + end +end + --- @param result stbtt__bitmap --- @param pts stbtt__point[] --- @param wcount int[] @@ -2453,7 +2489,7 @@ local function stbtt__rasterize(result, pts, wcount, windings, scale_x, scale_y, end -- now sort the edges by their highest point (should snap to integer, and then by x) - stbtt__sort_edges(e) + stbtt__sort_edges_range(e, 1, n) -- [Luau] e[n + 1..] are y0 == nil sentinels, already last -- now, traverse the scanlines and find the intersections on each scanline, use xor winding rule stbtt__rasterize_sorted_edges(result, e, n, vsubsample, off_x, off_y)