Use reference passing for EpdRenderer

This commit is contained in:
Dave Allie
2025-12-06 12:56:39 +11:00
parent 6414f85257
commit 9a33030623
23 changed files with 109 additions and 114 deletions

View File

@@ -146,8 +146,8 @@ void EpubHtmlParser::makePages() {
currentPage = new Page();
}
const int lineHeight = renderer->getLineHeight();
const int pageHeight = renderer->getPageHeight();
const int lineHeight = renderer.getLineHeight();
const int pageHeight = renderer.getPageHeight();
// Long running task, make sure to let other things happen
vTaskDelay(1);

View File

@@ -10,7 +10,7 @@ class EpdRenderer;
class EpubHtmlParser final : public tinyxml2::XMLVisitor {
const char* filepath;
EpdRenderer* renderer;
EpdRenderer& renderer;
std::function<void(Page*)> completePageFn;
bool insideBoldTag = false;
@@ -27,7 +27,7 @@ class EpubHtmlParser final : public tinyxml2::XMLVisitor {
bool VisitExit(const tinyxml2::XMLElement& element) override;
// xml parser callbacks
public:
explicit EpubHtmlParser(const char* filepath, EpdRenderer* renderer, const std::function<void(Page*)>& completePageFn)
explicit EpubHtmlParser(const char* filepath, EpdRenderer& renderer, const std::function<void(Page*)>& completePageFn)
: filepath(filepath), renderer(renderer), completePageFn(completePageFn) {}
~EpubHtmlParser() override = default;
bool parseAndBuildPages();

View File

@@ -3,7 +3,7 @@
#include <HardwareSerial.h>
#include <Serialization.h>
void PageLine::render(EpdRenderer* renderer) { block->render(renderer, 0, yPos); }
void PageLine::render(EpdRenderer& renderer) { block->render(renderer, 0, yPos); }
void PageLine::serialize(std::ostream& os) {
serialization::writePod(os, yPos);
@@ -20,7 +20,7 @@ PageLine* PageLine::deserialize(std::istream& is) {
return new PageLine(tb, yPos);
}
void Page::render(EpdRenderer* renderer) const {
void Page::render(EpdRenderer& renderer) const {
const auto start = millis();
for (const auto element : elements) {
element->render(renderer);

View File

@@ -11,7 +11,7 @@ class PageElement {
int yPos;
explicit PageElement(const int yPos) : yPos(yPos) {}
virtual ~PageElement() = default;
virtual void render(EpdRenderer* renderer) = 0;
virtual void render(EpdRenderer& renderer) = 0;
virtual void serialize(std::ostream& os) = 0;
};
@@ -22,7 +22,7 @@ class PageLine final : public PageElement {
public:
PageLine(const TextBlock* block, const int yPos) : PageElement(yPos), block(block) {}
~PageLine() override { delete block; }
void render(EpdRenderer* renderer) override;
void render(EpdRenderer& renderer) override;
void serialize(std::ostream& os) override;
static PageLine* deserialize(std::istream& is);
};
@@ -32,7 +32,7 @@ class Page {
int nextY = 0;
// the list of block index and line numbers on this page
std::vector<PageElement*> elements;
void render(EpdRenderer* renderer) const;
void render(EpdRenderer& renderer) const;
~Page() {
for (const auto element : elements) {
delete element;

View File

@@ -107,11 +107,11 @@ void Section::renderPage() {
delete p;
} else if (pageCount == 0) {
Serial.println("No pages to render");
const int width = renderer->getTextWidth("Empty chapter", BOLD);
renderer->drawText((renderer->getPageWidth() - width) / 2, 300, "Empty chapter", 1, BOLD);
const int width = renderer.getTextWidth("Empty chapter", BOLD);
renderer.drawText((renderer.getPageWidth() - width) / 2, 300, "Empty chapter", 1, BOLD);
} else {
Serial.printf("Page out of bounds: %d (max %d)\n", currentPage, pageCount);
const int width = renderer->getTextWidth("Out of bounds", BOLD);
renderer->drawText((renderer->getPageWidth() - width) / 2, 300, "Out of bounds", 1, BOLD);
const int width = renderer.getTextWidth("Out of bounds", BOLD);
renderer.drawText((renderer.getPageWidth() - width) / 2, 300, "Out of bounds", 1, BOLD);
}
}

View File

@@ -7,7 +7,7 @@ class EpdRenderer;
class Section {
Epub* epub;
const int spineIndex;
EpdRenderer* renderer;
EpdRenderer& renderer;
std::string cachePath;
void onPageComplete(const Page* page);
@@ -16,7 +16,7 @@ class Section {
int pageCount = 0;
int currentPage = 0;
explicit Section(Epub* epub, const int spineIndex, EpdRenderer* renderer)
explicit Section(Epub* epub, const int spineIndex, EpdRenderer& renderer)
: epub(epub), spineIndex(spineIndex), renderer(renderer) {
cachePath = epub->getCachePath() + "/" + std::to_string(spineIndex);
}

View File

@@ -8,7 +8,7 @@ typedef enum { TEXT_BLOCK, IMAGE_BLOCK } BlockType;
class Block {
public:
virtual ~Block() = default;
virtual void layout(EpdRenderer* renderer) = 0;
virtual void layout(EpdRenderer& renderer) = 0;
virtual BlockType getType() = 0;
virtual bool isEmpty() = 0;
virtual void finish() {}

View File

@@ -43,10 +43,10 @@ void TextBlock::addSpan(const std::string& span, const bool is_bold, const bool
}
}
std::list<TextBlock*> TextBlock::splitIntoLines(const EpdRenderer* renderer) {
std::list<TextBlock*> TextBlock::splitIntoLines(const EpdRenderer& renderer) {
const int totalWordCount = words.size();
const int pageWidth = renderer->getPageWidth();
const int spaceWidth = renderer->getSpaceWidth();
const int pageWidth = renderer.getPageWidth();
const int spaceWidth = renderer.getSpaceWidth();
words.shrink_to_fit();
wordStyles.shrink_to_fit();
@@ -66,7 +66,7 @@ std::list<TextBlock*> TextBlock::splitIntoLines(const EpdRenderer* renderer) {
} else if (wordStyles[i] & ITALIC_SPAN) {
fontStyle = ITALIC;
}
const int width = renderer->getTextWidth(words[i].c_str(), fontStyle);
const int width = renderer.getTextWidth(words[i].c_str(), fontStyle);
wordWidths[i] = width;
}
@@ -187,7 +187,7 @@ std::list<TextBlock*> TextBlock::splitIntoLines(const EpdRenderer* renderer) {
return lines;
}
void TextBlock::render(const EpdRenderer* renderer, const int x, const int y) const {
void TextBlock::render(const EpdRenderer& renderer, const int x, const int y) const {
for (int i = 0; i < words.size(); i++) {
// get the style
const uint8_t wordStyle = wordStyles[i];
@@ -203,7 +203,7 @@ void TextBlock::render(const EpdRenderer* renderer, const int x, const int y) co
} else if (wordStyles[i] & ITALIC_SPAN) {
fontStyle = ITALIC;
}
renderer->drawText(x + wordXpos[i], y, words[i].c_str(), 1, fontStyle);
renderer.drawText(x + wordXpos[i], y, words[i].c_str(), 1, fontStyle);
}
}

View File

@@ -40,10 +40,10 @@ class TextBlock final : public Block {
void set_style(const BLOCK_STYLE style) { this->style = style; }
BLOCK_STYLE get_style() const { return style; }
bool isEmpty() override { return words.empty(); }
void layout(EpdRenderer* renderer) override {};
void layout(EpdRenderer& renderer) override {};
// given a renderer works out where to break the words into lines
std::list<TextBlock*> splitIntoLines(const EpdRenderer* renderer);
void render(const EpdRenderer* renderer, int x, int y) const;
std::list<TextBlock*> splitIntoLines(const EpdRenderer& renderer);
void render(const EpdRenderer& renderer, int x, int y) const;
BlockType getType() override { return TEXT_BLOCK; }
void serialize(std::ostream& os) const;
static TextBlock* deserialize(std::istream& is);