From 95b47042f04f91d711287540df54fd16df1d8cdc Mon Sep 17 00:00:00 2001 From: Scott Percival Date: Fri, 19 Jan 2024 20:29:54 +0800 Subject: [PATCH] GRAPHICS: MACGUI: Stop MacTextCanvas::chopChunk from removing last chunk There needs to be at least one chunk per line of text, or else many basic text operations will cause a crash. --- graphics/macgui/mactext-canvas.cpp | 10 ++++++++-- graphics/macgui/mactext.cpp | 16 ++++++++++++++++ 2 files changed, 24 insertions(+), 2 deletions(-) diff --git a/graphics/macgui/mactext-canvas.cpp b/graphics/macgui/mactext-canvas.cpp index ccbad5ad7ff..27ebff61ac4 100644 --- a/graphics/macgui/mactext-canvas.cpp +++ b/graphics/macgui/mactext-canvas.cpp @@ -60,7 +60,7 @@ void MacTextCanvas::chopChunk(const Common::U32String &str, int *curLinePtr, int // Check if there is nothing to add, then remove the last chunk // This happens when the previous run is finished only with // empty formatting, or when we were adding text for the first time - if (chunk->text.empty() && str.empty()) { + if (chunk->text.empty() && str.empty() && (_text[curLine].chunks.size() > 1)) { D(9, "** chopChunk, replaced formatting, line %d", curLine); _text[curLine].chunks.pop_back(); @@ -1109,7 +1109,7 @@ void MacTextCanvas::reshuffleParagraph(int *row, int *col, MacFontRun &defaultFo bool paragraphEnd = _text[end].paragraphEnd; #if DEBUG - D(9, "MacTextCanvas::reshuffleParagraph: ppos: %d", ppos); + D(9, "MacTextCanvas::reshuffleParagraph: ppos: %d, start: %d, end: %d", ppos, start, end); debugPrint("MacTextCanvas::reshuffleParagraph(1)"); #endif @@ -1168,8 +1168,14 @@ void MacTextCanvas::reshuffleParagraph(int *row, int *col, MacFontRun &defaultFo #if DEBUG debugPrint("MacTextCanvas::reshuffleParagraph(3)"); + D(9, "Chunks: "); + for (auto &ch : _text[curLine].chunks) + ch.debugPrint(); + + D(9, ""); #endif + // Restore the paragraph marker _text[curLine].paragraphEnd = paragraphEnd; diff --git a/graphics/macgui/mactext.cpp b/graphics/macgui/mactext.cpp index ddf59b8c41c..a81ec16bef4 100644 --- a/graphics/macgui/mactext.cpp +++ b/graphics/macgui/mactext.cpp @@ -1850,6 +1850,13 @@ void MacText::deletePreviousCharInternal(int *row, int *col) { *col = _canvas.getLineCharWidth(*row - 1); (*row)--; +#if DEBUG + D(9, "MacText::deletePreviousCharInternal: Chunks: "); + for (auto &ch : _canvas._text[*row].chunks) + ch.debugPrint(); + + D(9, ""); +#endif // formatting matches, glue texts as normal if (_canvas._text[*row].lastChunk().equals(_canvas._text[*row + 1].firstChunk())) { _canvas._text[*row].lastChunk().text += _canvas._text[*row + 1].firstChunk().text; @@ -1911,6 +1918,15 @@ void MacText::addNewLine(int *row, int *col) { MacTextLine *line = &_canvas._text[*row]; int pos = *col; uint ch = line->getChunkNum(&pos); + +#if DEBUG + D(9, "MacText::addNewLine: Chunks: "); + for (auto &c : line->chunks) + c.debugPrint(); + + D(9, ""); +#endif + MacFontRun newchunk = line->chunks[ch]; MacTextLine newline;