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    --- ### 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:
co-authored by
coderabbitai[bot]
parent
f2fbdccd53
commit
35988ada55
@@ -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);
|
||||
|
||||
Reference in New Issue
Block a user