From 8920c62957aebc5a14fa60871961fe4213e4a0e7 Mon Sep 17 00:00:00 2001 From: cottongin Date: Tue, 27 Jan 2026 07:36:06 -0500 Subject: [PATCH] fix: line break - flush word before
tag (#525) Manual implementation of upstream PR #525 Added flushPartWordBuffer() function and call it before
handling to fix issue where preceding word was incorrectly wrapped to new line --- .../Epub/parsers/ChapterHtmlSlimParser.cpp | 23 +++++++++++++++++++ lib/Epub/Epub/parsers/ChapterHtmlSlimParser.h | 1 + 2 files changed, 24 insertions(+) diff --git a/lib/Epub/Epub/parsers/ChapterHtmlSlimParser.cpp b/lib/Epub/Epub/parsers/ChapterHtmlSlimParser.cpp index fdc059c..d77b0c0 100644 --- a/lib/Epub/Epub/parsers/ChapterHtmlSlimParser.cpp +++ b/lib/Epub/Epub/parsers/ChapterHtmlSlimParser.cpp @@ -80,6 +80,26 @@ void ChapterHtmlSlimParser::updateEffectiveInlineStyle() { } } +// Flush the contents of partWordBuffer to currentTextBlock +void ChapterHtmlSlimParser::flushPartWordBuffer() { + if (partWordBufferIndex == 0) return; + + // Determine font style using effective styles + EpdFontFamily::Style fontStyle = EpdFontFamily::REGULAR; + if (effectiveBold && effectiveItalic) { + fontStyle = EpdFontFamily::BOLD_ITALIC; + } else if (effectiveBold) { + fontStyle = EpdFontFamily::BOLD; + } else if (effectiveItalic) { + fontStyle = EpdFontFamily::ITALIC; + } + + // Flush the buffer + partWordBuffer[partWordBufferIndex] = '\0'; + currentTextBlock->addWord(partWordBuffer, fontStyle, effectiveUnderline); + partWordBufferIndex = 0; +} + // start a new text block if needed void ChapterHtmlSlimParser::startNewTextBlock(const TextBlock::Style style, const BlockStyle& blockStyle) { if (currentTextBlock) { @@ -341,6 +361,9 @@ void XMLCALL ChapterHtmlSlimParser::startElement(void* userData, const XML_Char* self->updateEffectiveInlineStyle(); } else if (matches(name, BLOCK_TAGS, NUM_BLOCK_TAGS)) { if (strcmp(name, "br") == 0) { + // Flush word preceding
to currentTextBlock before calling startNewTextBlock + // This fixes issue where
incorrectly wrapped the preceding word to a new line + self->flushPartWordBuffer(); self->startNewTextBlock(self->currentTextBlock->getStyle()); } else { // Determine alignment from CSS or default diff --git a/lib/Epub/Epub/parsers/ChapterHtmlSlimParser.h b/lib/Epub/Epub/parsers/ChapterHtmlSlimParser.h index 0d19a68..cad8fad 100644 --- a/lib/Epub/Epub/parsers/ChapterHtmlSlimParser.h +++ b/lib/Epub/Epub/parsers/ChapterHtmlSlimParser.h @@ -67,6 +67,7 @@ class ChapterHtmlSlimParser { void updateEffectiveInlineStyle(); void startNewTextBlock(TextBlock::Style style); void startNewTextBlock(TextBlock::Style style, const BlockStyle& blockStyle); + void flushPartWordBuffer(); void makePages(); // XML callbacks static void XMLCALL startElement(void* userData, const XML_Char* name, const XML_Char** atts);