fix: Fixed book title in home screen (#1013)

## Summary

* **What is the goal of this PR?** (e.g., Implements the new feature for
file uploading.)
* The goal is to fix the title of books in the Home Screen. 

Before

![IMG_8867](https://github.com/user-attachments/assets/6cc9ca22-b95b-4863-872d-ef427c42f833)

After:

![IMG_8868](https://github.com/user-attachments/assets/585031b1-2348-444c-8f32-073fed3b6582)

* **What changes are included?**

## Additional Context

* Add any other information that might be helpful for the reviewer
(e.g., performance implications, potential risks,
  specific areas to focus on).

---

### 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, Cursor
This commit is contained in:
DestinySpeaker
2026-02-21 17:55:59 -08:00
committed by Dave Allie
parent e44c004be6
commit 498e087a68
4 changed files with 121 additions and 37 deletions

View File

@@ -519,7 +519,8 @@ void BaseTheme::drawRecentBookCover(GfxRenderer& renderer, Rect rect, const std:
// Still have words left, so add ellipsis to last line
lines.back().append("...");
while (!lines.back().empty() && renderer.getTextWidth(UI_12_FONT_ID, lines.back().c_str()) > maxLineWidth) {
while (!lines.back().empty() && lines.back().size() > 3 &&
renderer.getTextWidth(UI_12_FONT_ID, lines.back().c_str()) > maxLineWidth) {
// Remove "..." first, then remove one UTF-8 char, then add "..." back
lines.back().resize(lines.back().size() - 3); // Remove "..."
utf8RemoveLastChar(lines.back());
@@ -540,17 +541,20 @@ void BaseTheme::drawRecentBookCover(GfxRenderer& renderer, Rect rect, const std:
break;
}
}
if (i.empty()) continue; // Skip words that couldn't fit even truncated
int newLineWidth = renderer.getTextWidth(UI_12_FONT_ID, currentLine.c_str());
int newLineWidth = renderer.getTextAdvanceX(UI_12_FONT_ID, currentLine.c_str(), EpdFontFamily::REGULAR);
if (newLineWidth > 0) {
newLineWidth += spaceWidth;
}
newLineWidth += wordWidth;
newLineWidth += renderer.getTextAdvanceX(UI_12_FONT_ID, i.c_str(), EpdFontFamily::REGULAR);
if (newLineWidth > maxLineWidth && !currentLine.empty()) {
// New line too long, push old line
lines.push_back(currentLine);
currentLine = i;
} else if (currentLine.empty()) {
currentLine = i;
} else {
currentLine.append(" ").append(i);
}

View File

@@ -3,9 +3,11 @@
#include <GfxRenderer.h>
#include <HalStorage.h>
#include <I18n.h>
#include <Utf8.h>
#include <cstdint>
#include <string>
#include <vector>
#include "Battery.h"
#include "RecentBooksStore.h"
@@ -482,13 +484,73 @@ void LyraTheme::drawRecentBookCover(GfxRenderer& renderer, Rect rect, const std:
hPaddingInSelection, cornerRadius, false, false, true, true, Color::LightGray);
}
auto title = renderer.truncatedText(UI_12_FONT_ID, book.title.c_str(), textWidth, EpdFontFamily::BOLD);
// 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 author = renderer.truncatedText(UI_10_FONT_ID, book.author.c_str(), textWidth);
auto bookTitleHeight = renderer.getTextHeight(UI_12_FONT_ID);
renderer.drawText(UI_12_FONT_ID, tileX + hPaddingInSelection + coverWidth + LyraMetrics::values.verticalSpacing,
tileY + tileHeight / 2 - bookTitleHeight, title.c_str(), true, EpdFontFamily::BOLD);
renderer.drawText(UI_10_FONT_ID, tileX + hPaddingInSelection + coverWidth + LyraMetrics::values.verticalSpacing,
tileY + tileHeight / 2 + 5, author.c_str(), true);
const int titleLineHeight = renderer.getLineHeight(UI_12_FONT_ID);
const int titleBlockHeight = titleLineHeight * static_cast<int>(titleLines.size());
const int authorHeight = book.author.empty() ? 0 : (renderer.getLineHeight(UI_10_FONT_ID) * 3 / 2);
const int totalBlockHeight = titleBlockHeight + authorHeight;
int titleY = tileY + tileHeight / 2 - totalBlockHeight / 2;
const int textX = tileX + hPaddingInSelection + coverWidth + LyraMetrics::values.verticalSpacing;
for (const auto& line : titleLines) {
renderer.drawText(UI_12_FONT_ID, textX, titleY, line.c_str(), true, EpdFontFamily::BOLD);
titleY += titleLineHeight;
}
if (!book.author.empty()) {
titleY += renderer.getLineHeight(UI_10_FONT_ID) / 2;
renderer.drawText(UI_10_FONT_ID, textX, titleY, author.c_str(), true);
}
} else {
drawEmptyRecents(renderer, rect);
}