diff --git a/lib/Epub/Epub/ParsedText.cpp b/lib/Epub/Epub/ParsedText.cpp index 8e050a5..73a3988 100644 --- a/lib/Epub/Epub/ParsedText.cpp +++ b/lib/Epub/Epub/ParsedText.cpp @@ -4,6 +4,7 @@ #include #include +#include #include #include @@ -17,10 +18,10 @@ void ParsedText::addWord(std::string word, const EpdFontStyle fontStyle) { } // Consumes data to minimize memory usage -std::list> ParsedText::layoutAndExtractLines(const GfxRenderer& renderer, const int fontId, - const int horizontalMargin) { +void ParsedText::layoutAndExtractLines(const GfxRenderer& renderer, const int fontId, const int horizontalMargin, + const std::function)>& processLine) { if (words.empty()) { - return {}; + return; } const size_t totalWordCount = words.size(); @@ -99,8 +100,6 @@ std::list> ParsedText::layoutAndExtractLines(const Gf currentWordIndex = nextBreakIndex; } - std::list> lines; - // Initialize iterators for consumption auto wordStartIt = words.begin(); auto wordStyleStartIt = wordStyles.begin(); @@ -153,7 +152,7 @@ std::list> ParsedText::layoutAndExtractLines(const Gf std::list lineWordStyles; lineWordStyles.splice(lineWordStyles.begin(), wordStyles, wordStyleStartIt, wordStyleEndIt); - lines.push_back( + processLine( std::make_shared(std::move(lineWords), std::move(lineXPos), std::move(lineWordStyles), style)); // Update pointers/indices for the next line @@ -162,6 +161,4 @@ std::list> ParsedText::layoutAndExtractLines(const Gf wordWidthIndex += lineWordCount; lastBreakAt = lineBreak; } - - return lines; } diff --git a/lib/Epub/Epub/ParsedText.h b/lib/Epub/Epub/ParsedText.h index 05f2532..188cb12 100644 --- a/lib/Epub/Epub/ParsedText.h +++ b/lib/Epub/Epub/ParsedText.h @@ -3,6 +3,7 @@ #include #include +#include #include #include #include @@ -24,6 +25,6 @@ class ParsedText { void setStyle(const TextBlock::BLOCK_STYLE style) { this->style = style; } TextBlock::BLOCK_STYLE getStyle() const { return style; } bool isEmpty() const { return words.empty(); } - std::list> layoutAndExtractLines(const GfxRenderer& renderer, int fontId, - int horizontalMargin); + void layoutAndExtractLines(const GfxRenderer& renderer, int fontId, int horizontalMargin, + const std::function)>& processLine); }; diff --git a/lib/Epub/Epub/parsers/ChapterHtmlSlimParser.cpp b/lib/Epub/Epub/parsers/ChapterHtmlSlimParser.cpp index cafb1a5..c22fcf2 100644 --- a/lib/Epub/Epub/parsers/ChapterHtmlSlimParser.cpp +++ b/lib/Epub/Epub/parsers/ChapterHtmlSlimParser.cpp @@ -245,6 +245,20 @@ bool ChapterHtmlSlimParser::parseAndBuildPages() { return true; } +void ChapterHtmlSlimParser::addLineToPage(std::shared_ptr line) { + const int lineHeight = renderer.getLineHeight(fontId) * lineCompression; + const int pageHeight = GfxRenderer::getScreenHeight() - marginTop - marginBottom; + + if (currentPageNextY + lineHeight > pageHeight) { + completePageFn(std::move(currentPage)); + currentPage.reset(new Page()); + currentPageNextY = marginTop; + } + + currentPage->elements.push_back(std::make_shared(line, marginLeft, currentPageNextY)); + currentPageNextY += lineHeight; +} + void ChapterHtmlSlimParser::makePages() { if (!currentTextBlock) { Serial.printf("[%lu] [EHP] !! No text block to make pages for !!\n", millis()); @@ -257,23 +271,9 @@ void ChapterHtmlSlimParser::makePages() { } const int lineHeight = renderer.getLineHeight(fontId) * lineCompression; - const int pageHeight = GfxRenderer::getScreenHeight() - marginTop - marginBottom; - - // Long running task, make sure to let other things happen - vTaskDelay(1); - - const auto lines = currentTextBlock->layoutAndExtractLines(renderer, fontId, marginLeft + marginRight); - - for (auto&& line : lines) { - if (currentPageNextY + lineHeight > pageHeight) { - completePageFn(std::move(currentPage)); - currentPage.reset(new Page()); - currentPageNextY = marginTop; - } - - currentPage->elements.push_back(std::make_shared(line, marginLeft, currentPageNextY)); - currentPageNextY += lineHeight; - } - // add some extra line between blocks + currentTextBlock->layoutAndExtractLines( + renderer, fontId, marginLeft + marginRight, + [this](const std::shared_ptr& textBlock) { addLineToPage(textBlock); }); + // Extra paragrpah spacing currentPageNextY += lineHeight / 2; } diff --git a/lib/Epub/Epub/parsers/ChapterHtmlSlimParser.h b/lib/Epub/Epub/parsers/ChapterHtmlSlimParser.h index 1212ec6..9791f50 100644 --- a/lib/Epub/Epub/parsers/ChapterHtmlSlimParser.h +++ b/lib/Epub/Epub/parsers/ChapterHtmlSlimParser.h @@ -59,4 +59,5 @@ class ChapterHtmlSlimParser { completePageFn(completePageFn) {} ~ChapterHtmlSlimParser() = default; bool parseAndBuildPages(); + void addLineToPage(std::shared_ptr line); };