feat: wrapped text in GfxRender, implemented in themes so far (#1141)

## Summary

* **What is the goal of this PR?** (e.g., Implements the new feature for
file uploading.)


* **What changes are included?**
Conrgegate the changes of #1074 , #1013 , and extended upon #911 by
@lkristensen
New function implemented in GfxRenderer.cpp
```C++
std::vector<std::string> GfxRenderer::wrappedText(const int fontId, const char* text, const int maxWidth,
                                                  const int maxLines, const EpdFontFamily::Style style) const
```
Applied logic to all uses in Lyra, Lyra Extended, and base theme
(continue reading card as pointed out by @znelson


## Additional Context





![IMG_8604](https://github.com/user-attachments/assets/49da71c9-a44f-4cde-b3bf-6773d71601b6)

![IMG_8605](https://github.com/user-attachments/assets/5eab4293-65c1-47fb-b422-8ab53a6b50a2)

![IMG_8606](https://github.com/user-attachments/assets/e0f98d19-0e3f-4294-83a1-e49264378dca)


---

### AI Usage

While CrossPoint doesn't have restrictions on AI tools in contributing,
please be transparent about their usage as it
helps set the right context for reviewers.

Did you use AI tools to help write this code? _**< YES >**_

---------

Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com>
This commit is contained in:
iandchasse
2026-02-25 10:24:35 +01:00
committed by GitHub
co-authored by coderabbitai[bot]
parent f2fbdccd53
commit 35988ada55
6 changed files with 102 additions and 166 deletions
@@ -5,6 +5,7 @@
#include <cstdint>
#include <string>
#include <vector>
#include "RecentBooksStore.h"
#include "components/UITheme.h"
@@ -15,15 +16,12 @@
namespace {
constexpr int hPaddingInSelection = 8;
constexpr int cornerRadius = 6;
int coverWidth = 0;
} // namespace
void Lyra3CoversTheme::drawRecentBookCover(GfxRenderer& renderer, Rect rect, const std::vector<RecentBook>& recentBooks,
const int selectorIndex, bool& coverRendered, bool& coverBufferStored,
bool& bufferRestored, std::function<bool()> storeCoverBuffer) const {
const int tileWidth = (rect.width - 2 * Lyra3CoversMetrics::values.contentSidePadding) / 3;
const int tileHeight = rect.height;
const int bookTitleHeight = tileHeight - Lyra3CoversMetrics::values.homeCoverHeight - hPaddingInSelection;
const int tileY = rect.y;
const bool hasContinueReading = !recentBooks.empty();
@@ -87,8 +85,15 @@ void Lyra3CoversTheme::drawRecentBookCover(GfxRenderer& renderer, Rect rect, con
bool bookSelected = (selectorIndex == i);
int tileX = Lyra3CoversMetrics::values.contentSidePadding + tileWidth * i;
auto title =
renderer.truncatedText(UI_10_FONT_ID, recentBooks[i].title.c_str(), tileWidth - 2 * hPaddingInSelection);
const int maxLineWidth = tileWidth - 2 * hPaddingInSelection;
auto titleLines = renderer.wrappedText(SMALL_FONT_ID, recentBooks[i].title.c_str(), maxLineWidth, 3);
const int titleLineHeight = renderer.getLineHeight(SMALL_FONT_ID);
const int dynamicBlockHeight = static_cast<int>(titleLines.size()) * titleLineHeight;
// Add a little padding below the text inside the selection box just like the top padding (5 + hPaddingSelection)
const int dynamicTitleBoxHeight = dynamicBlockHeight + hPaddingInSelection + 5;
if (bookSelected) {
// Draw selection box
@@ -99,10 +104,15 @@ void Lyra3CoversTheme::drawRecentBookCover(GfxRenderer& renderer, Rect rect, con
renderer.fillRectDither(tileX + tileWidth - hPaddingInSelection, tileY + hPaddingInSelection,
hPaddingInSelection, Lyra3CoversMetrics::values.homeCoverHeight, Color::LightGray);
renderer.fillRoundedRect(tileX, tileY + Lyra3CoversMetrics::values.homeCoverHeight + hPaddingInSelection,
tileWidth, bookTitleHeight, cornerRadius, false, false, true, true, Color::LightGray);
tileWidth, dynamicTitleBoxHeight, cornerRadius, false, false, true, true,
Color::LightGray);
}
int currentY = tileY + Lyra3CoversMetrics::values.homeCoverHeight + hPaddingInSelection + 5;
for (const auto& line : titleLines) {
renderer.drawText(SMALL_FONT_ID, tileX + hPaddingInSelection, currentY, line.c_str(), true);
currentY += titleLineHeight;
}
renderer.drawText(UI_10_FONT_ID, tileX + hPaddingInSelection,
tileY + tileHeight - bookTitleHeight + hPaddingInSelection + 5, title.c_str(), true);
}
} else {
drawEmptyRecents(renderer, rect);
@@ -25,7 +25,7 @@ constexpr ThemeMetrics values = {.batteryWidth = 16,
.scrollBarRightOffset = 5,
.homeTopPadding = 56,
.homeCoverHeight = 226,
.homeCoverTileHeight = 287,
.homeCoverTileHeight = 300,
.homeRecentBooksCount = 3,
.buttonHintsHeight = 40,
.sideButtonHintsWidth = 30,
+1 -52
View File
@@ -4,7 +4,6 @@
#include <HalPowerManager.h>
#include <HalStorage.h>
#include <I18n.h>
#include <Utf8.h>
#include <cstdint>
#include <string>
@@ -485,57 +484,7 @@ void LyraTheme::drawRecentBookCover(GfxRenderer& renderer, Rect rect, const std:
hPaddingInSelection, cornerRadius, false, false, true, true, Color::LightGray);
}
// Wrap title to up to 3 lines (word-wrap by advance width)
const std::string& lastBookTitle = book.title;
std::vector<std::string> words;
words.reserve(8);
std::string::size_type wordStart = 0;
std::string::size_type wordEnd = 0;
// find_first_not_of skips leading/interstitial spaces
while ((wordStart = lastBookTitle.find_first_not_of(' ', wordEnd)) != std::string::npos) {
wordEnd = lastBookTitle.find(' ', wordStart);
if (wordEnd == std::string::npos) wordEnd = lastBookTitle.size();
words.emplace_back(lastBookTitle.substr(wordStart, wordEnd - wordStart));
}
const int maxLineWidth = textWidth;
const int spaceWidth = renderer.getSpaceWidth(UI_12_FONT_ID, EpdFontFamily::BOLD);
std::vector<std::string> titleLines;
std::string currentLine;
for (auto& w : words) {
if (titleLines.size() >= 3) {
titleLines.back().append("...");
while (!titleLines.back().empty() && titleLines.back().size() > 3 &&
renderer.getTextWidth(UI_12_FONT_ID, titleLines.back().c_str(), EpdFontFamily::BOLD) > maxLineWidth) {
titleLines.back().resize(titleLines.back().size() - 3);
utf8RemoveLastChar(titleLines.back());
titleLines.back().append("...");
}
break;
}
int wordW = renderer.getTextWidth(UI_12_FONT_ID, w.c_str(), EpdFontFamily::BOLD);
while (wordW > maxLineWidth && !w.empty()) {
utf8RemoveLastChar(w);
std::string withE = w + "...";
wordW = renderer.getTextWidth(UI_12_FONT_ID, withE.c_str(), EpdFontFamily::BOLD);
if (wordW <= maxLineWidth) {
w = withE;
break;
}
}
if (w.empty()) continue; // Skip words that couldn't fit even truncated
int newW = renderer.getTextAdvanceX(UI_12_FONT_ID, currentLine.c_str(), EpdFontFamily::BOLD);
if (newW > 0) newW += spaceWidth;
newW += renderer.getTextAdvanceX(UI_12_FONT_ID, w.c_str(), EpdFontFamily::BOLD);
if (newW > maxLineWidth && !currentLine.empty()) {
titleLines.push_back(currentLine);
currentLine = w;
} else if (currentLine.empty()) {
currentLine = w;
} else {
currentLine.append(" ").append(w);
}
}
if (!currentLine.empty() && titleLines.size() < 3) titleLines.push_back(currentLine);
auto titleLines = renderer.wrappedText(UI_12_FONT_ID, book.title.c_str(), textWidth, 3, EpdFontFamily::BOLD);
auto author = renderer.truncatedText(UI_10_FONT_ID, book.author.c_str(), textWidth);
const int titleLineHeight = renderer.getLineHeight(UI_12_FONT_ID);