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 GitHub
parent e32d41a37e
commit 10a2678584
4 changed files with 121 additions and 37 deletions

View File

@@ -4,9 +4,11 @@
#include <HalPowerManager.h>
#include <HalStorage.h>
#include <I18n.h>
#include <Utf8.h>
#include <cstdint>
#include <string>
#include <vector>
#include "RecentBooksStore.h"
#include "components/UITheme.h"
@@ -483,13 +485,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);
}