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:
@@ -830,7 +830,8 @@ std::string GfxRenderer::truncatedText(const int fontId, const char* text, const
|
||||
if (!text || maxWidth <= 0) return "";
|
||||
|
||||
std::string item = text;
|
||||
const char* ellipsis = "...";
|
||||
// U+2026 HORIZONTAL ELLIPSIS (UTF-8: 0xE2 0x80 0xA6)
|
||||
const char* ellipsis = "\xe2\x80\xa6";
|
||||
int textWidth = getTextWidth(fontId, item.c_str(), style);
|
||||
if (textWidth <= maxWidth) {
|
||||
// Text fits, return as is
|
||||
@@ -844,6 +845,70 @@ std::string GfxRenderer::truncatedText(const int fontId, const char* text, const
|
||||
return item.empty() ? ellipsis : item + ellipsis;
|
||||
}
|
||||
|
||||
std::vector<std::string> GfxRenderer::wrappedText(const int fontId, const char* text, const int maxWidth,
|
||||
const int maxLines, const EpdFontFamily::Style style) const {
|
||||
std::vector<std::string> lines;
|
||||
|
||||
if (!text || maxWidth <= 0 || maxLines <= 0) return lines;
|
||||
|
||||
std::string remaining = text;
|
||||
std::string currentLine;
|
||||
|
||||
while (!remaining.empty()) {
|
||||
if (static_cast<int>(lines.size()) == maxLines - 1) {
|
||||
// Last available line: combine any word already started on this line with
|
||||
// the rest of the text, then let truncatedText fit it with an ellipsis.
|
||||
std::string lastContent = currentLine.empty() ? remaining : currentLine + " " + remaining;
|
||||
lines.push_back(truncatedText(fontId, lastContent.c_str(), maxWidth, style));
|
||||
return lines;
|
||||
}
|
||||
|
||||
// Find next word
|
||||
size_t spacePos = remaining.find(' ');
|
||||
std::string word;
|
||||
|
||||
if (spacePos == std::string::npos) {
|
||||
word = remaining;
|
||||
remaining.clear();
|
||||
} else {
|
||||
word = remaining.substr(0, spacePos);
|
||||
remaining.erase(0, spacePos + 1);
|
||||
}
|
||||
|
||||
std::string testLine = currentLine.empty() ? word : currentLine + " " + word;
|
||||
|
||||
if (getTextWidth(fontId, testLine.c_str(), style) <= maxWidth) {
|
||||
currentLine = testLine;
|
||||
} else {
|
||||
if (!currentLine.empty()) {
|
||||
lines.push_back(currentLine);
|
||||
// If the carried-over word itself exceeds maxWidth, truncate it and
|
||||
// push it as a complete line immediately — storing it in currentLine
|
||||
// would allow a subsequent short word to be appended after the ellipsis.
|
||||
if (getTextWidth(fontId, word.c_str(), style) > maxWidth) {
|
||||
lines.push_back(truncatedText(fontId, word.c_str(), maxWidth, style));
|
||||
currentLine.clear();
|
||||
if (static_cast<int>(lines.size()) >= maxLines) return lines;
|
||||
} else {
|
||||
currentLine = word;
|
||||
}
|
||||
} else {
|
||||
// Single word wider than maxWidth: truncate and stop to avoid complicated
|
||||
// splitting rules (different between languages). Results in an aesthetically
|
||||
// pleasing end.
|
||||
lines.push_back(truncatedText(fontId, word.c_str(), maxWidth, style));
|
||||
return lines;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (!currentLine.empty() && static_cast<int>(lines.size()) < maxLines) {
|
||||
lines.push_back(currentLine);
|
||||
}
|
||||
|
||||
return lines;
|
||||
}
|
||||
|
||||
// Note: Internal driver treats screen in command orientation; this library exposes a logical orientation
|
||||
int GfxRenderer::getScreenWidth() const {
|
||||
switch (orientation) {
|
||||
|
||||
Reference in New Issue
Block a user