From c24eb60bd3f1ff670277ce128ed5abfb587197a4 Mon Sep 17 00:00:00 2001 From: Daniel Imms <2193314+Tyriar@users.noreply.github.com> Date: Sun, 18 Jan 2026 15:45:19 -0800 Subject: [PATCH] Fix stack overflow in merge logic Fixes #5585 --- addons/addon-webgl/src/TextureAtlas.ts | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/addons/addon-webgl/src/TextureAtlas.ts b/addons/addon-webgl/src/TextureAtlas.ts index 182dda59..71624fe8 100644 --- a/addons/addon-webgl/src/TextureAtlas.ts +++ b/addons/addon-webgl/src/TextureAtlas.ts @@ -176,6 +176,17 @@ export class TextureAtlas implements ITextureAtlas { // Gather details of the merge const mergingPages = pagesBySize.slice(sameSizeI, sameSizeI + 4); + + // Only proceed with merge if we have exactly 4 same-sized pages. If not, we cannot + // effectively reduce page count and merging would cause issues. + if (mergingPages.length < 4 || mergingPages.some(p => p.canvas.width !== mergingPages[0].canvas.width)) { + const newPage = new AtlasPage(this._document, this._textureSize); + this._pages.push(newPage); + this._activePages.push(newPage); + this._onAddTextureAtlasCanvas.fire(newPage.canvas); + return newPage; + } + const sortedMergingPagesIndexes = mergingPages.map(e => e.glyphs[0].texturePage).sort((a, b) => a > b ? 1 : -1); const mergedPageIndex = this.pages.length - mergingPages.length;