Add drawCenteredText to GfxRenderer

This commit is contained in:
Dave Allie 2025-12-08 22:52:19 +11:00
parent 07cc589e59
commit 02b157c02b
No known key found for this signature in database
GPG Key ID: F2FDDB3AD8D0276F
7 changed files with 19 additions and 21 deletions

View File

@ -47,6 +47,12 @@ int GfxRenderer::getTextWidth(const int fontId, const char* text, const EpdFontS
return w; return w;
} }
void GfxRenderer::drawCenteredText(const int fontId, const int y, const char* text, const bool black,
const EpdFontStyle style) const {
const int x = (getScreenWidth() - getTextWidth(fontId, text, style)) / 2;
drawText(fontId, x, y, text, black, style);
}
void GfxRenderer::drawText(const int fontId, const int x, const int y, const char* text, const bool black, void GfxRenderer::drawText(const int fontId, const int x, const int y, const char* text, const bool black,
const EpdFontStyle style) const { const EpdFontStyle style) const {
const int yPos = y + getLineHeight(fontId); const int yPos = y + getLineHeight(fontId);
@ -109,7 +115,8 @@ void GfxRenderer::fillRect(const int x, const int y, const int width, const int
} }
void GfxRenderer::drawImage(const uint8_t bitmap[], const int x, const int y, const int width, const int height) const { void GfxRenderer::drawImage(const uint8_t bitmap[], const int x, const int y, const int width, const int height) const {
einkDisplay.drawImage(bitmap, x, y, width, height); // Flip X and Y for portrait mode
einkDisplay.drawImage(bitmap, y, x, height, width);
} }
void GfxRenderer::clearScreen(const uint8_t color) const { einkDisplay.clearScreen(color); } void GfxRenderer::clearScreen(const uint8_t color) const { einkDisplay.clearScreen(color); }

View File

@ -40,6 +40,7 @@ class GfxRenderer {
// Text // Text
int getTextWidth(int fontId, const char* text, EpdFontStyle style = REGULAR) const; int getTextWidth(int fontId, const char* text, EpdFontStyle style = REGULAR) const;
void drawCenteredText(int fontId, int y, const char* text, bool black = true, EpdFontStyle style = REGULAR) const;
void drawText(int fontId, int x, int y, const char* text, bool black = true, EpdFontStyle style = REGULAR) const; void drawText(int fontId, int x, int y, const char* text, bool black = true, EpdFontStyle style = REGULAR) const;
void setFontRenderMode(const FontRenderMode mode) { this->fontRenderMode = mode; } void setFontRenderMode(const FontRenderMode mode) { this->fontRenderMode = mode; }
int getSpaceWidth(int fontId) const; int getSpaceWidth(int fontId) const;

View File

@ -10,11 +10,8 @@ void BootLogoScreen::onEnter() {
const auto pageHeight = GfxRenderer::getScreenHeight(); const auto pageHeight = GfxRenderer::getScreenHeight();
renderer.clearScreen(); renderer.clearScreen();
// Location for images is from top right in landscape orientation renderer.drawImage(CrossLarge, (pageWidth - 128) / 2, (pageHeight - 128) / 2, 128, 128);
renderer.drawImage(CrossLarge, (pageHeight - 128) / 2, (pageWidth - 128) / 2, 128, 128); renderer.drawCenteredText(UI_FONT_ID, pageHeight / 2 + 70, "CrossPoint", true, BOLD);
const int width = renderer.getTextWidth(UI_FONT_ID, "CrossPoint", BOLD); renderer.drawCenteredText(SMALL_FONT_ID, pageHeight / 2 + 95, "BOOTING");
renderer.drawText(UI_FONT_ID, (pageWidth - width) / 2, pageHeight / 2 + 70, "CrossPoint", true, BOLD);
const int bootingWidth = renderer.getTextWidth(SMALL_FONT_ID, "BOOTING");
renderer.drawText(SMALL_FONT_ID, (pageWidth - bootingWidth) / 2, pageHeight / 2 + 95, "BOOTING");
renderer.displayBuffer(); renderer.displayBuffer();
} }

View File

@ -198,8 +198,7 @@ void EpubReaderScreen::renderScreen() {
if (section->pageCount == 0) { if (section->pageCount == 0) {
Serial.printf("[%lu] [ERS] No pages to render\n", millis()); Serial.printf("[%lu] [ERS] No pages to render\n", millis());
const int width = renderer.getTextWidth(READER_FONT_ID, "Empty chapter", BOLD); renderer.drawCenteredText(READER_FONT_ID, 300, "Empty chapter", true, BOLD);
renderer.drawText(READER_FONT_ID, (GfxRenderer::getScreenWidth() - width) / 2, 300, "Empty chapter", true, BOLD);
renderStatusBar(); renderStatusBar();
renderer.displayBuffer(); renderer.displayBuffer();
return; return;
@ -207,8 +206,7 @@ void EpubReaderScreen::renderScreen() {
if (section->currentPage < 0 || section->currentPage >= section->pageCount) { if (section->currentPage < 0 || section->currentPage >= section->pageCount) {
Serial.printf("[%lu] [ERS] Page out of bounds: %d (max %d)\n", millis(), section->currentPage, section->pageCount); Serial.printf("[%lu] [ERS] Page out of bounds: %d (max %d)\n", millis(), section->currentPage, section->pageCount);
const int width = renderer.getTextWidth(READER_FONT_ID, "Out of bounds", BOLD); renderer.drawCenteredText(READER_FONT_ID, 300, "Out of bounds", true, BOLD);
renderer.drawText(READER_FONT_ID, (GfxRenderer::getScreenWidth() - width) / 2, 300, "Out of bounds", true, BOLD);
renderStatusBar(); renderStatusBar();
renderer.displayBuffer(); renderer.displayBuffer();
return; return;

View File

@ -121,8 +121,7 @@ void FileSelectionScreen::render() const {
renderer.clearScreen(); renderer.clearScreen();
const auto pageWidth = GfxRenderer::getScreenWidth(); const auto pageWidth = GfxRenderer::getScreenWidth();
const auto titleWidth = renderer.getTextWidth(READER_FONT_ID, "CrossPoint Reader", BOLD); renderer.drawCenteredText(READER_FONT_ID, 10, "CrossPoint Reader", true, BOLD);
renderer.drawText(READER_FONT_ID, (pageWidth - titleWidth) / 2, 10, "CrossPoint Reader", true, BOLD);
if (files.empty()) { if (files.empty()) {
renderer.drawText(UI_FONT_ID, 20, 60, "No EPUBs found"); renderer.drawText(UI_FONT_ID, 20, 60, "No EPUBs found");

View File

@ -5,12 +5,10 @@
#include "config.h" #include "config.h"
void FullScreenMessageScreen::onEnter() { void FullScreenMessageScreen::onEnter() {
const auto width = renderer.getTextWidth(UI_FONT_ID, text.c_str(), style);
const auto height = renderer.getLineHeight(UI_FONT_ID); const auto height = renderer.getLineHeight(UI_FONT_ID);
const auto left = (GfxRenderer::getScreenWidth() - width) / 2;
const auto top = (GfxRenderer::getScreenHeight() - height) / 2; const auto top = (GfxRenderer::getScreenHeight() - height) / 2;
renderer.clearScreen(); renderer.clearScreen();
renderer.drawText(UI_FONT_ID, left, top, text.c_str(), true, style); renderer.drawCenteredText(UI_FONT_ID, top, text.c_str(), true, style);
renderer.displayBuffer(refreshMode); renderer.displayBuffer(refreshMode);
} }

View File

@ -10,11 +10,9 @@ void SleepScreen::onEnter() {
const auto pageHeight = GfxRenderer::getScreenHeight(); const auto pageHeight = GfxRenderer::getScreenHeight();
renderer.clearScreen(); renderer.clearScreen();
renderer.drawImage(CrossLarge, (pageHeight - 128) / 2, (pageWidth - 128) / 2, 128, 128); renderer.drawImage(CrossLarge, (pageWidth - 128) / 2, (pageHeight - 128) / 2, 128, 128);
const int width = renderer.getTextWidth(UI_FONT_ID, "CrossPoint", BOLD); renderer.drawCenteredText(UI_FONT_ID, pageHeight / 2 + 70, "CrossPoint", true, BOLD);
renderer.drawText(UI_FONT_ID, (pageWidth - width) / 2, pageHeight / 2 + 70, "CrossPoint", true, BOLD); renderer.drawCenteredText(SMALL_FONT_ID, pageHeight / 2 + 95, "SLEEPING");
const int bootingWidth = renderer.getTextWidth(SMALL_FONT_ID, "SLEEPING");
renderer.drawText(SMALL_FONT_ID, (pageWidth - bootingWidth) / 2, pageHeight / 2 + 95, "SLEEPING");
renderer.invertScreen(); renderer.invertScreen();
renderer.displayBuffer(EInkDisplay::HALF_REFRESH); renderer.displayBuffer(EInkDisplay::HALF_REFRESH);
} }