A fork of https://github.com/crosspoint-reader/crosspoint-reader
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

Process lines into pages as they are built

+27 -28
+5 -8
lib/Epub/Epub/ParsedText.cpp
··· 4 4 5 5 #include <algorithm> 6 6 #include <cmath> 7 + #include <functional> 7 8 #include <limits> 8 9 #include <vector> 9 10 ··· 17 18 } 18 19 19 20 // Consumes data to minimize memory usage 20 - std::list<std::shared_ptr<TextBlock>> ParsedText::layoutAndExtractLines(const GfxRenderer& renderer, const int fontId, 21 - const int horizontalMargin) { 21 + void ParsedText::layoutAndExtractLines(const GfxRenderer& renderer, const int fontId, const int horizontalMargin, 22 + const std::function<void(std::shared_ptr<TextBlock>)>& processLine) { 22 23 if (words.empty()) { 23 - return {}; 24 + return; 24 25 } 25 26 26 27 const size_t totalWordCount = words.size(); ··· 99 100 currentWordIndex = nextBreakIndex; 100 101 } 101 102 102 - std::list<std::shared_ptr<TextBlock>> lines; 103 - 104 103 // Initialize iterators for consumption 105 104 auto wordStartIt = words.begin(); 106 105 auto wordStyleStartIt = wordStyles.begin(); ··· 153 152 std::list<EpdFontStyle> lineWordStyles; 154 153 lineWordStyles.splice(lineWordStyles.begin(), wordStyles, wordStyleStartIt, wordStyleEndIt); 155 154 156 - lines.push_back( 155 + processLine( 157 156 std::make_shared<TextBlock>(std::move(lineWords), std::move(lineXPos), std::move(lineWordStyles), style)); 158 157 159 158 // Update pointers/indices for the next line ··· 162 161 wordWidthIndex += lineWordCount; 163 162 lastBreakAt = lineBreak; 164 163 } 165 - 166 - return lines; 167 164 }
+3 -2
lib/Epub/Epub/ParsedText.h
··· 3 3 #include <EpdFontFamily.h> 4 4 5 5 #include <cstdint> 6 + #include <functional> 6 7 #include <list> 7 8 #include <memory> 8 9 #include <string> ··· 24 25 void setStyle(const TextBlock::BLOCK_STYLE style) { this->style = style; } 25 26 TextBlock::BLOCK_STYLE getStyle() const { return style; } 26 27 bool isEmpty() const { return words.empty(); } 27 - std::list<std::shared_ptr<TextBlock>> layoutAndExtractLines(const GfxRenderer& renderer, int fontId, 28 - int horizontalMargin); 28 + void layoutAndExtractLines(const GfxRenderer& renderer, int fontId, int horizontalMargin, 29 + const std::function<void(std::shared_ptr<TextBlock>)>& processLine); 29 30 };
+18 -18
lib/Epub/Epub/parsers/ChapterHtmlSlimParser.cpp
··· 245 245 return true; 246 246 } 247 247 248 + void ChapterHtmlSlimParser::addLineToPage(std::shared_ptr<TextBlock> line) { 249 + const int lineHeight = renderer.getLineHeight(fontId) * lineCompression; 250 + const int pageHeight = GfxRenderer::getScreenHeight() - marginTop - marginBottom; 251 + 252 + if (currentPageNextY + lineHeight > pageHeight) { 253 + completePageFn(std::move(currentPage)); 254 + currentPage.reset(new Page()); 255 + currentPageNextY = marginTop; 256 + } 257 + 258 + currentPage->elements.push_back(std::make_shared<PageLine>(line, marginLeft, currentPageNextY)); 259 + currentPageNextY += lineHeight; 260 + } 261 + 248 262 void ChapterHtmlSlimParser::makePages() { 249 263 if (!currentTextBlock) { 250 264 Serial.printf("[%lu] [EHP] !! No text block to make pages for !!\n", millis()); ··· 257 271 } 258 272 259 273 const int lineHeight = renderer.getLineHeight(fontId) * lineCompression; 260 - const int pageHeight = GfxRenderer::getScreenHeight() - marginTop - marginBottom; 261 - 262 - // Long running task, make sure to let other things happen 263 - vTaskDelay(1); 264 - 265 - const auto lines = currentTextBlock->layoutAndExtractLines(renderer, fontId, marginLeft + marginRight); 266 - 267 - for (auto&& line : lines) { 268 - if (currentPageNextY + lineHeight > pageHeight) { 269 - completePageFn(std::move(currentPage)); 270 - currentPage.reset(new Page()); 271 - currentPageNextY = marginTop; 272 - } 273 - 274 - currentPage->elements.push_back(std::make_shared<PageLine>(line, marginLeft, currentPageNextY)); 275 - currentPageNextY += lineHeight; 276 - } 277 - // add some extra line between blocks 274 + currentTextBlock->layoutAndExtractLines( 275 + renderer, fontId, marginLeft + marginRight, 276 + [this](const std::shared_ptr<TextBlock>& textBlock) { addLineToPage(textBlock); }); 277 + // Extra paragrpah spacing 278 278 currentPageNextY += lineHeight / 2; 279 279 }
+1
lib/Epub/Epub/parsers/ChapterHtmlSlimParser.h
··· 59 59 completePageFn(completePageFn) {} 60 60 ~ChapterHtmlSlimParser() = default; 61 61 bool parseAndBuildPages(); 62 + void addLineToPage(std::shared_ptr<TextBlock> line); 62 63 };