diff --git a/lib/Epub/Epub/parsers/ChapterHtmlSlimParser.cpp b/lib/Epub/Epub/parsers/ChapterHtmlSlimParser.cpp
index 1df3ceab..241e7e0d 100644
--- a/lib/Epub/Epub/parsers/ChapterHtmlSlimParser.cpp
+++ b/lib/Epub/Epub/parsers/ChapterHtmlSlimParser.cpp
@@ -772,8 +772,12 @@ void XMLCALL ChapterHtmlSlimParser::characterData(void* userData, const XML_Char
// Spotted when reading Intermezzo, there are some really long text blocks in there.
if (self->currentTextBlock->size() > 750) {
LOG_DBG("EHP", "Text block too long, splitting into multiple pages");
+ const int horizontalInset = self->currentTextBlock->getBlockStyle().totalHorizontalInset();
+ const uint16_t effectiveWidth = (horizontalInset < self->viewportWidth)
+ ? static_cast(self->viewportWidth - horizontalInset)
+ : self->viewportWidth;
self->currentTextBlock->layoutAndExtractLines(
- self->renderer, self->fontId, self->viewportWidth,
+ self->renderer, self->fontId, effectiveWidth,
[self](const std::shared_ptr& textBlock) { self->addLineToPage(textBlock); }, false);
}
}
@@ -1020,6 +1024,11 @@ bool ChapterHtmlSlimParser::parseAndBuildPages() {
void ChapterHtmlSlimParser::addLineToPage(std::shared_ptr line) {
const int lineHeight = renderer.getLineHeight(fontId) * lineCompression;
+ if (!currentPage) {
+ currentPage.reset(new Page());
+ currentPageNextY = 0;
+ }
+
if (currentPageNextY + lineHeight > viewportHeight) {
completePageFn(std::move(currentPage));
completedPageCount++;