2025-12-17 23:32:18 +11:00
|
|
|
|
#include "SleepActivity.h"
|
2025-12-06 04:20:03 +11:00
|
|
|
|
|
2025-12-21 18:42:06 +11:00
|
|
|
|
#include <Epub.h>
|
2025-12-08 22:06:09 +11:00
|
|
|
|
#include <GfxRenderer.h>
|
2026-02-08 21:29:14 +01:00
|
|
|
|
#include <HalStorage.h>
|
2026-02-09 11:52:55 -05:00
|
|
|
|
#include <Serialization.h>
|
2026-01-14 19:36:40 +09:00
|
|
|
|
#include <Txt.h>
|
2025-12-28 23:56:05 +09:00
|
|
|
|
#include <Xtc.h>
|
2025-12-06 04:20:03 +11:00
|
|
|
|
|
2026-02-09 11:52:55 -05:00
|
|
|
|
#include <algorithm>
|
2026-02-13 11:12:27 -05:00
|
|
|
|
#include <cmath>
|
2026-02-09 11:52:55 -05:00
|
|
|
|
|
2025-12-15 23:17:23 +11:00
|
|
|
|
#include "CrossPointSettings.h"
|
2025-12-21 18:42:06 +11:00
|
|
|
|
#include "CrossPointState.h"
|
feat: UI themes, Lyra (#528)
## Summary
### What is the goal of this PR?
- Visual UI overhaul
- UI theme selection
### What changes are included?
- Added a setting "UI Theme": Classic, Lyra
- The classic theme is the current Crosspoint theme
- The Lyra theme implements these mockups:
https://www.figma.com/design/UhxoV4DgUnfrDQgMPPTXog/Lyra-Theme?node-id=2003-7596&t=4CSOZqf0n9uQMxDt-0
by Discord users yagofarias, ruby and gan_shu
- New functions in GFXRenderer to render rounded rectangles, greyscale
fills (using dithering) and thick lines
- Basic UI components are factored into BaseTheme methods which can be
overridden by each additional theme. Methods that are not overridden
will fallback to BaseTheme behavior. This means any new
features/components in CrossPoint only need to be developed for the
"Classic" BaseTheme.
- Additional themes can easily be developed by the community using this
foundation



## Additional Context
- Only the Home, Library and main Settings screens have been implemented
so far, this will be extended to the transfer screens and chapter
selection screen later on, but we need to get the ball rolling somehow
:)
- Loading extra covers on the home screen in the Lyra theme takes a
little more time (about 2 seconds), I added a loading bar popup (reusing
the Indexing progress bar from the reader view, factored into a neat UI
component) but the popup adds ~400ms to the loading time.
- ~~Home screen thumbnails will need to be generated separately for each
theme, because they are displayed in different sizes. Because we're
using dithering, displaying a thumb with the wrong size causes the
picture to look janky or dark as it does on the screenshots above. No
worries this will be fixed in a future PR.~~ Thumbs are now generated
with a size parameter
- UI Icons will need to be implemented in a future PR.
---
### 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? _**PARTIALLY**_
This is not a vibe coded PR. Copilot was used for autocompletion to save
time but I reviewed, understood and edited all generated code.
---------
Co-authored-by: Dave Allie <dave@daveallie.com>
2026-02-05 17:50:11 +07:00
|
|
|
|
#include "components/UITheme.h"
|
Aleo, Noto Sans, Open Dyslexic fonts (#163)
## Summary
* Swap out Bookerly font due to licensing issues, replace default font
with Aleo
* I did a bunch of searching around for a nice replacement font, and
this trumped several other like Literata, Merriwether, Vollkorn, etc
* Add Noto Sans, and Open Dyslexic as font options
* They can be selected in the settings screen
* Add font size options (Small, Medium, Large, Extra Large)
* Adjustable in settings
* Swap out uses of reader font in headings and replaced with slightly
larger Ubuntu font
* Replaced PixelArial14 font as it was difficult to track down, replace
with Space Grotesk
* Remove auto formatting on generated font files
* Massively speeds up formatting step now that there is a lot more CPP
font source
* Include fonts with their licenses in the repo
## Additional Context
Line compression setting will follow
| Font | Small | Medium | Large | X Large |
| --- | --- | --- | --- | --- |
| Aleo |

|

|

|

|
| Noto Sans |

|

|

|

|
| Open Dyslexic |

|

|

|

|
2025-12-30 18:21:47 +10:00
|
|
|
|
#include "fontIds.h"
|
2026-02-06 02:50:01 +11:00
|
|
|
|
#include "images/Logo120.h"
|
2026-01-07 20:07:23 +10:00
|
|
|
|
#include "util/StringUtils.h"
|
2025-12-28 23:56:05 +09:00
|
|
|
|
|
2026-02-09 11:52:55 -05:00
|
|
|
|
namespace {
|
|
|
|
|
|
|
2026-02-13 11:12:27 -05:00
|
|
|
|
// Number of source pixels along the image edge to average for the dominant color
|
2026-02-09 11:52:55 -05:00
|
|
|
|
constexpr int EDGE_SAMPLE_DEPTH = 20;
|
|
|
|
|
|
|
|
|
|
|
|
// Map a 2-bit quantized pixel value to an 8-bit grayscale value
|
|
|
|
|
|
constexpr uint8_t val2bitToGray(uint8_t val2bit) { return val2bit * 85; }
|
|
|
|
|
|
|
2026-02-13 11:12:27 -05:00
|
|
|
|
// Letterbox fill data: one average gray value per edge (top/bottom or left/right).
|
|
|
|
|
|
struct LetterboxFillData {
|
|
|
|
|
|
uint8_t avgA = 128; // average gray of edge A (top or left)
|
|
|
|
|
|
uint8_t avgB = 128; // average gray of edge B (bottom or right)
|
2026-02-09 11:52:55 -05:00
|
|
|
|
int letterboxA = 0; // pixel size of the first letterbox area (top or left)
|
|
|
|
|
|
int letterboxB = 0; // pixel size of the second letterbox area (bottom or right)
|
|
|
|
|
|
bool horizontal = false; // true = top/bottom letterbox, false = left/right
|
2026-02-13 11:12:27 -05:00
|
|
|
|
bool valid = false;
|
|
|
|
|
|
};
|
2026-02-09 11:52:55 -05:00
|
|
|
|
|
2026-02-13 11:12:27 -05:00
|
|
|
|
// Snap an 8-bit gray value to the nearest of the 4 e-ink levels: 0, 85, 170, 255.
|
|
|
|
|
|
uint8_t snapToEinkLevel(uint8_t gray) {
|
|
|
|
|
|
// Thresholds at midpoints: 42, 127, 212
|
|
|
|
|
|
if (gray < 43) return 0;
|
|
|
|
|
|
if (gray < 128) return 85;
|
|
|
|
|
|
if (gray < 213) return 170;
|
|
|
|
|
|
return 255;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 4x4 Bayer ordered dithering matrix, values 0-255.
|
|
|
|
|
|
// Produces a structured halftone pattern for 4-level quantization.
|
|
|
|
|
|
// clang-format off
|
|
|
|
|
|
constexpr uint8_t BAYER_4X4[4][4] = {
|
|
|
|
|
|
{ 0, 128, 32, 160},
|
|
|
|
|
|
{192, 64, 224, 96},
|
|
|
|
|
|
{ 48, 176, 16, 144},
|
|
|
|
|
|
{240, 112, 208, 80}
|
2026-02-09 11:52:55 -05:00
|
|
|
|
};
|
2026-02-13 11:12:27 -05:00
|
|
|
|
// clang-format on
|
|
|
|
|
|
|
|
|
|
|
|
// Ordered (Bayer) dithering for 4-level e-ink display.
|
|
|
|
|
|
// Maps an 8-bit gray value to a 2-bit level (0-3) using the Bayer matrix
|
|
|
|
|
|
// to produce a structured, repeating halftone pattern.
|
|
|
|
|
|
uint8_t quantizeBayerDither(int gray, int x, int y) {
|
|
|
|
|
|
const int threshold = BAYER_4X4[y & 3][x & 3];
|
|
|
|
|
|
const int scaled = gray * 3;
|
|
|
|
|
|
if (scaled < 255) {
|
|
|
|
|
|
return (scaled + threshold >= 255) ? 1 : 0;
|
|
|
|
|
|
} else if (scaled < 510) {
|
|
|
|
|
|
return ((scaled - 255) + threshold >= 255) ? 2 : 1;
|
|
|
|
|
|
} else {
|
|
|
|
|
|
return ((scaled - 510) + threshold >= 255) ? 3 : 2;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2026-02-09 11:52:55 -05:00
|
|
|
|
|
2026-02-13 14:49:42 -05:00
|
|
|
|
// Check whether a gray value would produce a dithered mix that crosses the
|
|
|
|
|
|
// level-2 / level-3 boundary. This is the ONLY boundary where some dithered
|
|
|
|
|
|
// pixels are BLACK (level ≤ 2) and others are WHITE (level 3) in the BW pass,
|
|
|
|
|
|
// creating a high-frequency checkerboard that causes e-ink display crosstalk
|
|
|
|
|
|
// and washes out adjacent content during HALF_REFRESH.
|
|
|
|
|
|
// Gray values 171-254 produce a level-2/level-3 mix via Bayer dithering.
|
|
|
|
|
|
bool bayerCrossesBwBoundary(uint8_t gray) {
|
|
|
|
|
|
return gray > 170 && gray < 255;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Hash-based block dithering for BW-boundary gray values (171-254).
|
|
|
|
|
|
// Each blockSize×blockSize pixel block gets a single uniform level (2 or 3),
|
|
|
|
|
|
// determined by a deterministic spatial hash. The proportion of level-3 blocks
|
|
|
|
|
|
// approximates the target gray. Unlike Bayer, the pattern is irregular
|
|
|
|
|
|
// (noise-like), making it much less visually obvious at the same block size.
|
|
|
|
|
|
// The hash is purely spatial (depends only on x, y, blockSize) so it produces
|
|
|
|
|
|
// identical levels across BW, LSB, and MSB render passes.
|
|
|
|
|
|
static constexpr int BW_DITHER_BLOCK = 2;
|
|
|
|
|
|
|
|
|
|
|
|
uint8_t hashBlockDither(uint8_t avg, int x, int y) {
|
|
|
|
|
|
const int bx = x / BW_DITHER_BLOCK;
|
|
|
|
|
|
const int by = y / BW_DITHER_BLOCK;
|
|
|
|
|
|
// Fast mixing hash (splitmix32-inspired)
|
|
|
|
|
|
uint32_t h = (uint32_t)bx * 2654435761u ^ (uint32_t)by * 2246822519u;
|
|
|
|
|
|
h ^= h >> 16;
|
|
|
|
|
|
h *= 0x45d9f3bu;
|
|
|
|
|
|
h ^= h >> 16;
|
|
|
|
|
|
// Proportion of level-3 blocks needed to approximate the target gray
|
|
|
|
|
|
const float ratio = (avg - 170.0f) / 85.0f;
|
|
|
|
|
|
const uint32_t threshold = (uint32_t)(ratio * 4294967295.0f);
|
|
|
|
|
|
return (h < threshold) ? 3 : 2;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-02-13 11:12:27 -05:00
|
|
|
|
// --- Edge average cache ---
|
|
|
|
|
|
// Caches the computed edge averages alongside the cover BMP so we don't rescan on every sleep.
|
|
|
|
|
|
constexpr uint8_t EDGE_CACHE_VERSION = 2;
|
2026-02-09 11:52:55 -05:00
|
|
|
|
|
2026-02-13 11:12:27 -05:00
|
|
|
|
bool loadEdgeCache(const std::string& path, int screenWidth, int screenHeight, LetterboxFillData& data) {
|
2026-02-09 11:52:55 -05:00
|
|
|
|
FsFile file;
|
|
|
|
|
|
if (!Storage.openFileForRead("SLP", path, file)) return false;
|
|
|
|
|
|
|
|
|
|
|
|
uint8_t version;
|
|
|
|
|
|
serialization::readPod(file, version);
|
|
|
|
|
|
if (version != EDGE_CACHE_VERSION) {
|
|
|
|
|
|
file.close();
|
|
|
|
|
|
return false;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
uint16_t cachedW, cachedH;
|
|
|
|
|
|
serialization::readPod(file, cachedW);
|
|
|
|
|
|
serialization::readPod(file, cachedH);
|
|
|
|
|
|
if (cachedW != static_cast<uint16_t>(screenWidth) || cachedH != static_cast<uint16_t>(screenHeight)) {
|
|
|
|
|
|
file.close();
|
|
|
|
|
|
return false;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
uint8_t horizontal;
|
|
|
|
|
|
serialization::readPod(file, horizontal);
|
|
|
|
|
|
data.horizontal = (horizontal != 0);
|
|
|
|
|
|
|
2026-02-13 11:12:27 -05:00
|
|
|
|
serialization::readPod(file, data.avgA);
|
|
|
|
|
|
serialization::readPod(file, data.avgB);
|
2026-02-09 11:52:55 -05:00
|
|
|
|
|
|
|
|
|
|
int16_t lbA, lbB;
|
|
|
|
|
|
serialization::readPod(file, lbA);
|
|
|
|
|
|
serialization::readPod(file, lbB);
|
|
|
|
|
|
data.letterboxA = lbA;
|
|
|
|
|
|
data.letterboxB = lbB;
|
|
|
|
|
|
|
|
|
|
|
|
file.close();
|
2026-02-13 11:12:27 -05:00
|
|
|
|
data.valid = true;
|
|
|
|
|
|
Serial.printf("[%lu] [SLP] Loaded edge cache from %s (avgA=%d, avgB=%d)\n", millis(), path.c_str(), data.avgA,
|
|
|
|
|
|
data.avgB);
|
2026-02-09 11:52:55 -05:00
|
|
|
|
return true;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-02-13 11:12:27 -05:00
|
|
|
|
bool saveEdgeCache(const std::string& path, int screenWidth, int screenHeight, const LetterboxFillData& data) {
|
|
|
|
|
|
if (!data.valid) return false;
|
2026-02-09 11:52:55 -05:00
|
|
|
|
|
|
|
|
|
|
FsFile file;
|
|
|
|
|
|
if (!Storage.openFileForWrite("SLP", path, file)) return false;
|
|
|
|
|
|
|
|
|
|
|
|
serialization::writePod(file, EDGE_CACHE_VERSION);
|
|
|
|
|
|
serialization::writePod(file, static_cast<uint16_t>(screenWidth));
|
|
|
|
|
|
serialization::writePod(file, static_cast<uint16_t>(screenHeight));
|
|
|
|
|
|
serialization::writePod(file, static_cast<uint8_t>(data.horizontal ? 1 : 0));
|
2026-02-13 11:12:27 -05:00
|
|
|
|
serialization::writePod(file, data.avgA);
|
|
|
|
|
|
serialization::writePod(file, data.avgB);
|
2026-02-09 11:52:55 -05:00
|
|
|
|
serialization::writePod(file, static_cast<int16_t>(data.letterboxA));
|
|
|
|
|
|
serialization::writePod(file, static_cast<int16_t>(data.letterboxB));
|
|
|
|
|
|
file.close();
|
|
|
|
|
|
|
2026-02-13 11:12:27 -05:00
|
|
|
|
Serial.printf("[%lu] [SLP] Saved edge cache to %s\n", millis(), path.c_str());
|
2026-02-09 11:52:55 -05:00
|
|
|
|
return true;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-02-13 11:12:27 -05:00
|
|
|
|
// Read the bitmap once to compute a single average gray value for the top/bottom or left/right edges.
|
|
|
|
|
|
// Only computes running sums -- no per-pixel arrays, no malloc beyond row buffers.
|
2026-02-09 11:52:55 -05:00
|
|
|
|
// After sampling the bitmap is rewound via rewindToData().
|
2026-02-13 11:12:27 -05:00
|
|
|
|
LetterboxFillData computeEdgeAverages(const Bitmap& bitmap, int imgX, int imgY, int pageWidth, int pageHeight,
|
|
|
|
|
|
float scale, float cropX, float cropY) {
|
|
|
|
|
|
LetterboxFillData data;
|
2026-02-09 11:52:55 -05:00
|
|
|
|
|
|
|
|
|
|
const int cropPixX = static_cast<int>(std::floor(bitmap.getWidth() * cropX / 2.0f));
|
|
|
|
|
|
const int cropPixY = static_cast<int>(std::floor(bitmap.getHeight() * cropY / 2.0f));
|
|
|
|
|
|
const int visibleWidth = bitmap.getWidth() - 2 * cropPixX;
|
|
|
|
|
|
const int visibleHeight = bitmap.getHeight() - 2 * cropPixY;
|
|
|
|
|
|
|
|
|
|
|
|
if (visibleWidth <= 0 || visibleHeight <= 0) return data;
|
|
|
|
|
|
|
|
|
|
|
|
const int outputRowSize = (bitmap.getWidth() + 3) / 4;
|
|
|
|
|
|
auto* outputRow = static_cast<uint8_t*>(malloc(outputRowSize));
|
|
|
|
|
|
auto* rowBytes = static_cast<uint8_t*>(malloc(bitmap.getRowBytes()));
|
|
|
|
|
|
if (!outputRow || !rowBytes) {
|
2026-02-13 11:12:27 -05:00
|
|
|
|
free(outputRow);
|
|
|
|
|
|
free(rowBytes);
|
2026-02-09 11:52:55 -05:00
|
|
|
|
return data;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (imgY > 0) {
|
2026-02-13 11:12:27 -05:00
|
|
|
|
// Top/bottom letterboxing -- compute overall average of first/last EDGE_SAMPLE_DEPTH rows
|
2026-02-09 11:52:55 -05:00
|
|
|
|
data.horizontal = true;
|
|
|
|
|
|
const int scaledHeight = static_cast<int>(std::round(static_cast<float>(visibleHeight) * scale));
|
|
|
|
|
|
data.letterboxA = imgY;
|
|
|
|
|
|
data.letterboxB = pageHeight - imgY - scaledHeight;
|
|
|
|
|
|
if (data.letterboxB < 0) data.letterboxB = 0;
|
|
|
|
|
|
|
|
|
|
|
|
const int sampleRows = std::min(EDGE_SAMPLE_DEPTH, visibleHeight);
|
2026-02-13 11:12:27 -05:00
|
|
|
|
uint64_t sumTop = 0, sumBot = 0;
|
|
|
|
|
|
int countTop = 0, countBot = 0;
|
2026-02-09 11:52:55 -05:00
|
|
|
|
|
|
|
|
|
|
for (int bmpY = 0; bmpY < bitmap.getHeight(); bmpY++) {
|
|
|
|
|
|
if (bitmap.readNextRow(outputRow, rowBytes) != BmpReaderError::Ok) break;
|
|
|
|
|
|
const int logicalY = bitmap.isTopDown() ? bmpY : bitmap.getHeight() - 1 - bmpY;
|
|
|
|
|
|
if (logicalY < cropPixY || logicalY >= bitmap.getHeight() - cropPixY) continue;
|
|
|
|
|
|
const int outY = logicalY - cropPixY;
|
|
|
|
|
|
|
|
|
|
|
|
const bool inTop = (outY < sampleRows);
|
|
|
|
|
|
const bool inBot = (outY >= visibleHeight - sampleRows);
|
|
|
|
|
|
if (!inTop && !inBot) continue;
|
|
|
|
|
|
|
|
|
|
|
|
for (int bmpX = cropPixX; bmpX < bitmap.getWidth() - cropPixX; bmpX++) {
|
|
|
|
|
|
const uint8_t val = (outputRow[bmpX / 4] >> (6 - ((bmpX * 2) % 8))) & 0x3;
|
|
|
|
|
|
const uint8_t gray = val2bitToGray(val);
|
2026-02-13 11:12:27 -05:00
|
|
|
|
if (inTop) {
|
|
|
|
|
|
sumTop += gray;
|
|
|
|
|
|
countTop++;
|
|
|
|
|
|
}
|
|
|
|
|
|
if (inBot) {
|
|
|
|
|
|
sumBot += gray;
|
|
|
|
|
|
countBot++;
|
|
|
|
|
|
}
|
2026-02-09 11:52:55 -05:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-02-13 11:12:27 -05:00
|
|
|
|
data.avgA = countTop > 0 ? static_cast<uint8_t>(sumTop / countTop) : 128;
|
|
|
|
|
|
data.avgB = countBot > 0 ? static_cast<uint8_t>(sumBot / countBot) : 128;
|
|
|
|
|
|
data.valid = true;
|
2026-02-09 11:52:55 -05:00
|
|
|
|
} else if (imgX > 0) {
|
2026-02-13 11:12:27 -05:00
|
|
|
|
// Left/right letterboxing -- compute overall average of first/last EDGE_SAMPLE_DEPTH columns
|
2026-02-09 11:52:55 -05:00
|
|
|
|
data.horizontal = false;
|
|
|
|
|
|
const int scaledWidth = static_cast<int>(std::round(static_cast<float>(visibleWidth) * scale));
|
|
|
|
|
|
data.letterboxA = imgX;
|
|
|
|
|
|
data.letterboxB = pageWidth - imgX - scaledWidth;
|
|
|
|
|
|
if (data.letterboxB < 0) data.letterboxB = 0;
|
|
|
|
|
|
|
|
|
|
|
|
const int sampleCols = std::min(EDGE_SAMPLE_DEPTH, visibleWidth);
|
2026-02-13 11:12:27 -05:00
|
|
|
|
uint64_t sumLeft = 0, sumRight = 0;
|
|
|
|
|
|
int countLeft = 0, countRight = 0;
|
2026-02-09 11:52:55 -05:00
|
|
|
|
|
|
|
|
|
|
for (int bmpY = 0; bmpY < bitmap.getHeight(); bmpY++) {
|
|
|
|
|
|
if (bitmap.readNextRow(outputRow, rowBytes) != BmpReaderError::Ok) break;
|
|
|
|
|
|
const int logicalY = bitmap.isTopDown() ? bmpY : bitmap.getHeight() - 1 - bmpY;
|
|
|
|
|
|
if (logicalY < cropPixY || logicalY >= bitmap.getHeight() - cropPixY) continue;
|
|
|
|
|
|
|
|
|
|
|
|
for (int bmpX = cropPixX; bmpX < cropPixX + sampleCols; bmpX++) {
|
|
|
|
|
|
const uint8_t val = (outputRow[bmpX / 4] >> (6 - ((bmpX * 2) % 8))) & 0x3;
|
2026-02-13 11:12:27 -05:00
|
|
|
|
sumLeft += val2bitToGray(val);
|
|
|
|
|
|
countLeft++;
|
2026-02-09 11:52:55 -05:00
|
|
|
|
}
|
|
|
|
|
|
for (int bmpX = bitmap.getWidth() - cropPixX - sampleCols; bmpX < bitmap.getWidth() - cropPixX; bmpX++) {
|
|
|
|
|
|
const uint8_t val = (outputRow[bmpX / 4] >> (6 - ((bmpX * 2) % 8))) & 0x3;
|
2026-02-13 11:12:27 -05:00
|
|
|
|
sumRight += val2bitToGray(val);
|
|
|
|
|
|
countRight++;
|
2026-02-09 11:52:55 -05:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-02-13 11:12:27 -05:00
|
|
|
|
data.avgA = countLeft > 0 ? static_cast<uint8_t>(sumLeft / countLeft) : 128;
|
|
|
|
|
|
data.avgB = countRight > 0 ? static_cast<uint8_t>(sumRight / countRight) : 128;
|
|
|
|
|
|
data.valid = true;
|
2026-02-09 11:52:55 -05:00
|
|
|
|
}
|
|
|
|
|
|
|
2026-02-13 14:49:42 -05:00
|
|
|
|
bitmap.rewindToData();
|
2026-02-13 11:12:27 -05:00
|
|
|
|
free(outputRow);
|
|
|
|
|
|
free(rowBytes);
|
2026-02-09 11:52:55 -05:00
|
|
|
|
return data;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-02-13 11:12:27 -05:00
|
|
|
|
// Draw letterbox fill in the areas around the cover image.
|
|
|
|
|
|
// DITHERED: fills with the edge average using Bayer ordered dithering to approximate the color.
|
|
|
|
|
|
// SOLID: snaps edge average to nearest e-ink level (0/85/170/255) for a clean uniform fill.
|
2026-02-09 11:52:55 -05:00
|
|
|
|
// Must be called once per render pass (BW, GRAYSCALE_LSB, GRAYSCALE_MSB).
|
2026-02-13 11:12:27 -05:00
|
|
|
|
void drawLetterboxFill(GfxRenderer& renderer, const LetterboxFillData& data, uint8_t fillMode) {
|
|
|
|
|
|
if (!data.valid) return;
|
2026-02-09 11:52:55 -05:00
|
|
|
|
|
|
|
|
|
|
const bool isSolid = (fillMode == CrossPointSettings::SLEEP_SCREEN_LETTERBOX_FILL::LETTERBOX_SOLID);
|
|
|
|
|
|
|
2026-02-13 14:49:42 -05:00
|
|
|
|
// For DITHERED mode with gray values in 171-254 (the level-2/level-3 BW boundary):
|
|
|
|
|
|
// Pixel-level Bayer dithering creates a regular high-frequency checkerboard in
|
|
|
|
|
|
// the BW pass that causes e-ink display crosstalk during HALF_REFRESH.
|
|
|
|
|
|
//
|
|
|
|
|
|
// Solution: HASH-BASED BLOCK DITHERING. Each 2x2 pixel block gets a single
|
|
|
|
|
|
// level (2 or 3) determined by a spatial hash, with the proportion of level-3
|
|
|
|
|
|
// blocks tuned to approximate the target gray. The 2px minimum run avoids BW
|
|
|
|
|
|
// crosstalk, and the irregular hash pattern is much less visible than a regular
|
|
|
|
|
|
// Bayer grid at the same block size.
|
|
|
|
|
|
const bool hashA = !isSolid && bayerCrossesBwBoundary(data.avgA);
|
|
|
|
|
|
const bool hashB = !isSolid && bayerCrossesBwBoundary(data.avgB);
|
|
|
|
|
|
|
|
|
|
|
|
// For solid mode: snap to nearest e-ink level
|
|
|
|
|
|
const uint8_t levelA = isSolid ? snapToEinkLevel(data.avgA) / 85 : 0;
|
|
|
|
|
|
const uint8_t levelB = isSolid ? snapToEinkLevel(data.avgB) / 85 : 0;
|
2026-02-09 11:52:55 -05:00
|
|
|
|
|
|
|
|
|
|
if (data.horizontal) {
|
|
|
|
|
|
if (data.letterboxA > 0) {
|
2026-02-13 11:12:27 -05:00
|
|
|
|
for (int y = 0; y < data.letterboxA; y++)
|
2026-02-13 14:49:42 -05:00
|
|
|
|
for (int x = 0; x < renderer.getScreenWidth(); x++) {
|
|
|
|
|
|
uint8_t lv;
|
|
|
|
|
|
if (isSolid) lv = levelA;
|
|
|
|
|
|
else if (hashA) lv = hashBlockDither(data.avgA, x, y);
|
|
|
|
|
|
else lv = quantizeBayerDither(data.avgA, x, y);
|
|
|
|
|
|
renderer.drawPixelGray(x, y, lv);
|
|
|
|
|
|
}
|
2026-02-09 11:52:55 -05:00
|
|
|
|
}
|
|
|
|
|
|
if (data.letterboxB > 0) {
|
2026-02-13 11:12:27 -05:00
|
|
|
|
const int start = renderer.getScreenHeight() - data.letterboxB;
|
|
|
|
|
|
for (int y = start; y < renderer.getScreenHeight(); y++)
|
2026-02-13 14:49:42 -05:00
|
|
|
|
for (int x = 0; x < renderer.getScreenWidth(); x++) {
|
|
|
|
|
|
uint8_t lv;
|
|
|
|
|
|
if (isSolid) lv = levelB;
|
|
|
|
|
|
else if (hashB) lv = hashBlockDither(data.avgB, x, y);
|
|
|
|
|
|
else lv = quantizeBayerDither(data.avgB, x, y);
|
|
|
|
|
|
renderer.drawPixelGray(x, y, lv);
|
|
|
|
|
|
}
|
2026-02-09 11:52:55 -05:00
|
|
|
|
}
|
|
|
|
|
|
} else {
|
|
|
|
|
|
if (data.letterboxA > 0) {
|
2026-02-13 11:12:27 -05:00
|
|
|
|
for (int x = 0; x < data.letterboxA; x++)
|
2026-02-13 14:49:42 -05:00
|
|
|
|
for (int y = 0; y < renderer.getScreenHeight(); y++) {
|
|
|
|
|
|
uint8_t lv;
|
|
|
|
|
|
if (isSolid) lv = levelA;
|
|
|
|
|
|
else if (hashA) lv = hashBlockDither(data.avgA, x, y);
|
|
|
|
|
|
else lv = quantizeBayerDither(data.avgA, x, y);
|
|
|
|
|
|
renderer.drawPixelGray(x, y, lv);
|
|
|
|
|
|
}
|
2026-02-09 11:52:55 -05:00
|
|
|
|
}
|
|
|
|
|
|
if (data.letterboxB > 0) {
|
2026-02-13 11:12:27 -05:00
|
|
|
|
const int start = renderer.getScreenWidth() - data.letterboxB;
|
|
|
|
|
|
for (int x = start; x < renderer.getScreenWidth(); x++)
|
2026-02-13 14:49:42 -05:00
|
|
|
|
for (int y = 0; y < renderer.getScreenHeight(); y++) {
|
|
|
|
|
|
uint8_t lv;
|
|
|
|
|
|
if (isSolid) lv = levelB;
|
|
|
|
|
|
else if (hashB) lv = hashBlockDither(data.avgB, x, y);
|
|
|
|
|
|
else lv = quantizeBayerDither(data.avgB, x, y);
|
|
|
|
|
|
renderer.drawPixelGray(x, y, lv);
|
|
|
|
|
|
}
|
2026-02-09 11:52:55 -05:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
|
|
2025-12-17 23:32:18 +11:00
|
|
|
|
void SleepActivity::onEnter() {
|
2025-12-21 21:17:00 +11:00
|
|
|
|
Activity::onEnter();
|
feat: UI themes, Lyra (#528)
## Summary
### What is the goal of this PR?
- Visual UI overhaul
- UI theme selection
### What changes are included?
- Added a setting "UI Theme": Classic, Lyra
- The classic theme is the current Crosspoint theme
- The Lyra theme implements these mockups:
https://www.figma.com/design/UhxoV4DgUnfrDQgMPPTXog/Lyra-Theme?node-id=2003-7596&t=4CSOZqf0n9uQMxDt-0
by Discord users yagofarias, ruby and gan_shu
- New functions in GFXRenderer to render rounded rectangles, greyscale
fills (using dithering) and thick lines
- Basic UI components are factored into BaseTheme methods which can be
overridden by each additional theme. Methods that are not overridden
will fallback to BaseTheme behavior. This means any new
features/components in CrossPoint only need to be developed for the
"Classic" BaseTheme.
- Additional themes can easily be developed by the community using this
foundation



## Additional Context
- Only the Home, Library and main Settings screens have been implemented
so far, this will be extended to the transfer screens and chapter
selection screen later on, but we need to get the ball rolling somehow
:)
- Loading extra covers on the home screen in the Lyra theme takes a
little more time (about 2 seconds), I added a loading bar popup (reusing
the Indexing progress bar from the reader view, factored into a neat UI
component) but the popup adds ~400ms to the loading time.
- ~~Home screen thumbnails will need to be generated separately for each
theme, because they are displayed in different sizes. Because we're
using dithering, displaying a thumb with the wrong size causes the
picture to look janky or dark as it does on the screenshots above. No
worries this will be fixed in a future PR.~~ Thumbs are now generated
with a size parameter
- UI Icons will need to be implemented in a future PR.
---
### 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? _**PARTIALLY**_
This is not a vibe coded PR. Copilot was used for autocompletion to save
time but I reviewed, understood and edited all generated code.
---------
Co-authored-by: Dave Allie <dave@daveallie.com>
2026-02-05 17:50:11 +07:00
|
|
|
|
GUI.drawPopup(renderer, "Entering Sleep...");
|
2025-12-21 18:42:06 +11:00
|
|
|
|
|
2026-02-05 18:46:14 +07:00
|
|
|
|
switch (SETTINGS.sleepScreen) {
|
|
|
|
|
|
case (CrossPointSettings::SLEEP_SCREEN_MODE::BLANK):
|
|
|
|
|
|
return renderBlankSleepScreen();
|
|
|
|
|
|
case (CrossPointSettings::SLEEP_SCREEN_MODE::CUSTOM):
|
|
|
|
|
|
return renderCustomSleepScreen();
|
|
|
|
|
|
case (CrossPointSettings::SLEEP_SCREEN_MODE::COVER):
|
|
|
|
|
|
case (CrossPointSettings::SLEEP_SCREEN_MODE::COVER_CUSTOM):
|
|
|
|
|
|
return renderCoverSleepScreen();
|
|
|
|
|
|
default:
|
|
|
|
|
|
return renderDefaultSleepScreen();
|
2025-12-21 18:42:06 +11:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void SleepActivity::renderCustomSleepScreen() const {
|
2025-12-19 14:17:26 +01:00
|
|
|
|
// Check if we have a /sleep directory
|
2026-02-08 21:29:14 +01:00
|
|
|
|
auto dir = Storage.open("/sleep");
|
2025-12-19 14:17:26 +01:00
|
|
|
|
if (dir && dir.isDirectory()) {
|
|
|
|
|
|
std::vector<std::string> files;
|
2026-01-07 22:43:19 +10:00
|
|
|
|
char name[500];
|
2025-12-19 14:17:26 +01:00
|
|
|
|
// collect all valid BMP files
|
2025-12-30 15:09:30 +10:00
|
|
|
|
for (auto file = dir.openNextFile(); file; file = dir.openNextFile()) {
|
2025-12-19 14:17:26 +01:00
|
|
|
|
if (file.isDirectory()) {
|
|
|
|
|
|
file.close();
|
|
|
|
|
|
continue;
|
|
|
|
|
|
}
|
2025-12-30 15:09:30 +10:00
|
|
|
|
file.getName(name, sizeof(name));
|
|
|
|
|
|
auto filename = std::string(name);
|
2025-12-19 14:17:26 +01:00
|
|
|
|
if (filename[0] == '.') {
|
|
|
|
|
|
file.close();
|
|
|
|
|
|
continue;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (filename.substr(filename.length() - 4) != ".bmp") {
|
2025-12-30 15:09:30 +10:00
|
|
|
|
Serial.printf("[%lu] [SLP] Skipping non-.bmp file name: %s\n", millis(), name);
|
2025-12-19 14:17:26 +01:00
|
|
|
|
file.close();
|
|
|
|
|
|
continue;
|
|
|
|
|
|
}
|
|
|
|
|
|
Bitmap bitmap(file);
|
|
|
|
|
|
if (bitmap.parseHeaders() != BmpReaderError::Ok) {
|
2025-12-30 15:09:30 +10:00
|
|
|
|
Serial.printf("[%lu] [SLP] Skipping invalid BMP file: %s\n", millis(), name);
|
2025-12-19 14:17:26 +01:00
|
|
|
|
file.close();
|
|
|
|
|
|
continue;
|
|
|
|
|
|
}
|
|
|
|
|
|
files.emplace_back(filename);
|
|
|
|
|
|
file.close();
|
|
|
|
|
|
}
|
2025-12-21 18:42:06 +11:00
|
|
|
|
const auto numFiles = files.size();
|
2025-12-19 14:17:26 +01:00
|
|
|
|
if (numFiles > 0) {
|
|
|
|
|
|
// Generate a random number between 1 and numFiles
|
2026-01-14 05:05:08 -05:00
|
|
|
|
auto randomFileIndex = random(numFiles);
|
|
|
|
|
|
// If we picked the same image as last time, reroll
|
|
|
|
|
|
while (numFiles > 1 && randomFileIndex == APP_STATE.lastSleepImage) {
|
|
|
|
|
|
randomFileIndex = random(numFiles);
|
|
|
|
|
|
}
|
|
|
|
|
|
APP_STATE.lastSleepImage = randomFileIndex;
|
|
|
|
|
|
APP_STATE.saveToFile();
|
2025-12-21 18:42:06 +11:00
|
|
|
|
const auto filename = "/sleep/" + files[randomFileIndex];
|
2025-12-30 15:09:30 +10:00
|
|
|
|
FsFile file;
|
2026-02-08 21:29:14 +01:00
|
|
|
|
if (Storage.openFileForRead("SLP", filename, file)) {
|
2025-12-21 18:42:06 +11:00
|
|
|
|
Serial.printf("[%lu] [SLP] Randomly loading: /sleep/%s\n", millis(), files[randomFileIndex].c_str());
|
2025-12-19 14:17:26 +01:00
|
|
|
|
delay(100);
|
2026-01-12 12:36:19 +01:00
|
|
|
|
Bitmap bitmap(file, true);
|
2025-12-19 14:17:26 +01:00
|
|
|
|
if (bitmap.parseHeaders() == BmpReaderError::Ok) {
|
2025-12-21 18:42:06 +11:00
|
|
|
|
renderBitmapSleepScreen(bitmap);
|
2025-12-19 14:17:26 +01:00
|
|
|
|
dir.close();
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
if (dir) dir.close();
|
|
|
|
|
|
|
2025-12-19 08:45:14 +11:00
|
|
|
|
// Look for sleep.bmp on the root of the sd card to determine if we should
|
|
|
|
|
|
// render a custom sleep screen instead of the default.
|
2025-12-30 15:09:30 +10:00
|
|
|
|
FsFile file;
|
2026-02-08 21:29:14 +01:00
|
|
|
|
if (Storage.openFileForRead("SLP", "/sleep.bmp", file)) {
|
2026-01-12 12:36:19 +01:00
|
|
|
|
Bitmap bitmap(file, true);
|
2025-12-19 08:45:14 +11:00
|
|
|
|
if (bitmap.parseHeaders() == BmpReaderError::Ok) {
|
2025-12-21 18:42:06 +11:00
|
|
|
|
Serial.printf("[%lu] [SLP] Loading: /sleep.bmp\n", millis());
|
|
|
|
|
|
renderBitmapSleepScreen(bitmap);
|
2025-12-19 08:45:14 +11:00
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
renderDefaultSleepScreen();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void SleepActivity::renderDefaultSleepScreen() const {
|
2025-12-21 18:42:06 +11:00
|
|
|
|
const auto pageWidth = renderer.getScreenWidth();
|
|
|
|
|
|
const auto pageHeight = renderer.getScreenHeight();
|
2025-12-08 19:48:49 +11:00
|
|
|
|
|
|
|
|
|
|
renderer.clearScreen();
|
2026-02-06 02:50:01 +11:00
|
|
|
|
renderer.drawImage(Logo120, (pageWidth - 120) / 2, (pageHeight - 120) / 2, 120, 120);
|
2025-12-31 12:11:36 +10:00
|
|
|
|
renderer.drawCenteredText(UI_10_FONT_ID, pageHeight / 2 + 70, "CrossPoint", true, EpdFontFamily::BOLD);
|
2025-12-08 22:52:19 +11:00
|
|
|
|
renderer.drawCenteredText(SMALL_FONT_ID, pageHeight / 2 + 95, "SLEEPING");
|
2025-12-15 13:16:46 +01:00
|
|
|
|
|
2025-12-21 18:42:06 +11:00
|
|
|
|
// Make sleep screen dark unless light is selected in settings
|
|
|
|
|
|
if (SETTINGS.sleepScreen != CrossPointSettings::SLEEP_SCREEN_MODE::LIGHT) {
|
2025-12-15 13:16:46 +01:00
|
|
|
|
renderer.invertScreen();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-01-27 18:50:15 +01:00
|
|
|
|
renderer.displayBuffer(HalDisplay::HALF_REFRESH);
|
2025-12-08 19:48:49 +11:00
|
|
|
|
}
|
2025-12-19 08:45:14 +11:00
|
|
|
|
|
2026-02-09 11:52:55 -05:00
|
|
|
|
void SleepActivity::renderBitmapSleepScreen(const Bitmap& bitmap, const std::string& edgeCachePath) const {
|
2025-12-19 08:45:14 +11:00
|
|
|
|
int x, y;
|
2025-12-21 18:42:06 +11:00
|
|
|
|
const auto pageWidth = renderer.getScreenWidth();
|
|
|
|
|
|
const auto pageHeight = renderer.getScreenHeight();
|
2026-01-05 11:07:27 +01:00
|
|
|
|
float cropX = 0, cropY = 0;
|
2025-12-19 08:45:14 +11:00
|
|
|
|
|
2026-01-05 11:07:27 +01:00
|
|
|
|
Serial.printf("[%lu] [SLP] bitmap %d x %d, screen %d x %d\n", millis(), bitmap.getWidth(), bitmap.getHeight(),
|
|
|
|
|
|
pageWidth, pageHeight);
|
2026-02-09 11:52:55 -05:00
|
|
|
|
|
|
|
|
|
|
// Always compute aspect-ratio-preserving scale and position (supports both larger and smaller images)
|
|
|
|
|
|
float ratio = static_cast<float>(bitmap.getWidth()) / static_cast<float>(bitmap.getHeight());
|
|
|
|
|
|
const float screenRatio = static_cast<float>(pageWidth) / static_cast<float>(pageHeight);
|
|
|
|
|
|
|
|
|
|
|
|
Serial.printf("[%lu] [SLP] bitmap ratio: %f, screen ratio: %f\n", millis(), ratio, screenRatio);
|
|
|
|
|
|
if (ratio > screenRatio) {
|
|
|
|
|
|
// image wider than viewport ratio, needs to be centered vertically
|
|
|
|
|
|
if (SETTINGS.sleepScreenCoverMode == CrossPointSettings::SLEEP_SCREEN_COVER_MODE::CROP) {
|
|
|
|
|
|
cropX = 1.0f - (screenRatio / ratio);
|
|
|
|
|
|
Serial.printf("[%lu] [SLP] Cropping bitmap x: %f\n", millis(), cropX);
|
|
|
|
|
|
ratio = (1.0f - cropX) * static_cast<float>(bitmap.getWidth()) / static_cast<float>(bitmap.getHeight());
|
2025-12-19 08:45:14 +11:00
|
|
|
|
}
|
2026-02-09 11:52:55 -05:00
|
|
|
|
x = 0;
|
|
|
|
|
|
y = std::round((static_cast<float>(pageHeight) - static_cast<float>(pageWidth) / ratio) / 2);
|
|
|
|
|
|
Serial.printf("[%lu] [SLP] Centering with ratio %f to y=%d\n", millis(), ratio, y);
|
2025-12-19 08:45:14 +11:00
|
|
|
|
} else {
|
2026-02-09 11:52:55 -05:00
|
|
|
|
// image taller than or equal to viewport ratio, needs to be centered horizontally
|
|
|
|
|
|
if (SETTINGS.sleepScreenCoverMode == CrossPointSettings::SLEEP_SCREEN_COVER_MODE::CROP) {
|
|
|
|
|
|
cropY = 1.0f - (ratio / screenRatio);
|
|
|
|
|
|
Serial.printf("[%lu] [SLP] Cropping bitmap y: %f\n", millis(), cropY);
|
|
|
|
|
|
ratio = static_cast<float>(bitmap.getWidth()) / ((1.0f - cropY) * static_cast<float>(bitmap.getHeight()));
|
|
|
|
|
|
}
|
|
|
|
|
|
x = std::round((static_cast<float>(pageWidth) - static_cast<float>(pageHeight) * ratio) / 2);
|
|
|
|
|
|
y = 0;
|
|
|
|
|
|
Serial.printf("[%lu] [SLP] Centering with ratio %f to x=%d\n", millis(), ratio, x);
|
2025-12-19 08:45:14 +11:00
|
|
|
|
}
|
|
|
|
|
|
|
2026-01-05 11:07:27 +01:00
|
|
|
|
Serial.printf("[%lu] [SLP] drawing to %d x %d\n", millis(), x, y);
|
2026-02-09 11:52:55 -05:00
|
|
|
|
|
|
|
|
|
|
// Compute the scale factor (same formula as drawBitmap) so we can map screen coords to source coords
|
|
|
|
|
|
const float effectiveWidth = (1.0f - cropX) * bitmap.getWidth();
|
|
|
|
|
|
const float effectiveHeight = (1.0f - cropY) * bitmap.getHeight();
|
|
|
|
|
|
const float scale =
|
|
|
|
|
|
std::min(static_cast<float>(pageWidth) / effectiveWidth, static_cast<float>(pageHeight) / effectiveHeight);
|
|
|
|
|
|
|
|
|
|
|
|
// Determine letterbox fill settings
|
|
|
|
|
|
const uint8_t fillMode = SETTINGS.sleepScreenLetterboxFill;
|
|
|
|
|
|
const bool wantFill = (fillMode != CrossPointSettings::SLEEP_SCREEN_LETTERBOX_FILL::LETTERBOX_NONE);
|
|
|
|
|
|
|
2026-02-13 11:12:27 -05:00
|
|
|
|
static const char* fillModeNames[] = {"dithered", "solid", "none"};
|
|
|
|
|
|
const char* fillModeName = (fillMode < 3) ? fillModeNames[fillMode] : "unknown";
|
2026-02-09 11:52:55 -05:00
|
|
|
|
|
2026-02-13 11:12:27 -05:00
|
|
|
|
// Compute edge averages if letterbox fill is requested (try cache first)
|
|
|
|
|
|
LetterboxFillData fillData;
|
2026-02-09 11:52:55 -05:00
|
|
|
|
const bool hasLetterbox = (x > 0 || y > 0);
|
|
|
|
|
|
if (hasLetterbox && wantFill) {
|
|
|
|
|
|
bool cacheLoaded = false;
|
|
|
|
|
|
if (!edgeCachePath.empty()) {
|
2026-02-13 11:12:27 -05:00
|
|
|
|
cacheLoaded = loadEdgeCache(edgeCachePath, pageWidth, pageHeight, fillData);
|
2026-02-09 11:52:55 -05:00
|
|
|
|
}
|
|
|
|
|
|
if (!cacheLoaded) {
|
2026-02-13 11:12:27 -05:00
|
|
|
|
Serial.printf("[%lu] [SLP] Letterbox detected (x=%d, y=%d), computing edge averages for %s fill\n", millis(), x,
|
|
|
|
|
|
y, fillModeName);
|
|
|
|
|
|
fillData = computeEdgeAverages(bitmap, x, y, pageWidth, pageHeight, scale, cropX, cropY);
|
|
|
|
|
|
if (fillData.valid && !edgeCachePath.empty()) {
|
|
|
|
|
|
saveEdgeCache(edgeCachePath, pageWidth, pageHeight, fillData);
|
2026-02-09 11:52:55 -05:00
|
|
|
|
}
|
|
|
|
|
|
}
|
2026-02-13 11:12:27 -05:00
|
|
|
|
if (fillData.valid) {
|
|
|
|
|
|
Serial.printf("[%lu] [SLP] Letterbox fill: %s, horizontal=%d, avgA=%d, avgB=%d, letterboxA=%d, letterboxB=%d\n",
|
|
|
|
|
|
millis(), fillModeName, fillData.horizontal, fillData.avgA, fillData.avgB, fillData.letterboxA,
|
|
|
|
|
|
fillData.letterboxB);
|
|
|
|
|
|
}
|
2026-02-09 11:52:55 -05:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-12-19 08:45:14 +11:00
|
|
|
|
renderer.clearScreen();
|
2026-01-27 13:21:59 +00:00
|
|
|
|
|
|
|
|
|
|
const bool hasGreyscale = bitmap.hasGreyscale() &&
|
|
|
|
|
|
SETTINGS.sleepScreenCoverFilter == CrossPointSettings::SLEEP_SCREEN_COVER_FILTER::NO_FILTER;
|
|
|
|
|
|
|
2026-02-09 11:52:55 -05:00
|
|
|
|
// Draw letterbox fill (BW pass)
|
2026-02-13 11:12:27 -05:00
|
|
|
|
if (fillData.valid) {
|
|
|
|
|
|
drawLetterboxFill(renderer, fillData, fillMode);
|
2026-02-09 11:52:55 -05:00
|
|
|
|
}
|
|
|
|
|
|
|
2026-01-05 11:07:27 +01:00
|
|
|
|
renderer.drawBitmap(bitmap, x, y, pageWidth, pageHeight, cropX, cropY);
|
2026-01-27 13:21:59 +00:00
|
|
|
|
|
|
|
|
|
|
if (SETTINGS.sleepScreenCoverFilter == CrossPointSettings::SLEEP_SCREEN_COVER_FILTER::INVERTED_BLACK_AND_WHITE) {
|
|
|
|
|
|
renderer.invertScreen();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-01-27 18:50:15 +01:00
|
|
|
|
renderer.displayBuffer(HalDisplay::HALF_REFRESH);
|
2025-12-19 08:45:14 +11:00
|
|
|
|
|
2026-01-27 13:21:59 +00:00
|
|
|
|
if (hasGreyscale) {
|
2025-12-19 08:45:14 +11:00
|
|
|
|
bitmap.rewindToData();
|
|
|
|
|
|
renderer.clearScreen(0x00);
|
|
|
|
|
|
renderer.setRenderMode(GfxRenderer::GRAYSCALE_LSB);
|
2026-02-13 11:12:27 -05:00
|
|
|
|
if (fillData.valid) {
|
|
|
|
|
|
drawLetterboxFill(renderer, fillData, fillMode);
|
2026-02-09 11:52:55 -05:00
|
|
|
|
}
|
2026-01-05 11:07:27 +01:00
|
|
|
|
renderer.drawBitmap(bitmap, x, y, pageWidth, pageHeight, cropX, cropY);
|
2025-12-19 08:45:14 +11:00
|
|
|
|
renderer.copyGrayscaleLsbBuffers();
|
|
|
|
|
|
|
|
|
|
|
|
bitmap.rewindToData();
|
|
|
|
|
|
renderer.clearScreen(0x00);
|
|
|
|
|
|
renderer.setRenderMode(GfxRenderer::GRAYSCALE_MSB);
|
2026-02-13 11:12:27 -05:00
|
|
|
|
if (fillData.valid) {
|
|
|
|
|
|
drawLetterboxFill(renderer, fillData, fillMode);
|
2026-02-09 11:52:55 -05:00
|
|
|
|
}
|
2026-01-05 11:07:27 +01:00
|
|
|
|
renderer.drawBitmap(bitmap, x, y, pageWidth, pageHeight, cropX, cropY);
|
2025-12-19 08:45:14 +11:00
|
|
|
|
renderer.copyGrayscaleMsbBuffers();
|
|
|
|
|
|
|
|
|
|
|
|
renderer.displayGrayBuffer();
|
|
|
|
|
|
renderer.setRenderMode(GfxRenderer::BW);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2025-12-21 18:42:06 +11:00
|
|
|
|
|
|
|
|
|
|
void SleepActivity::renderCoverSleepScreen() const {
|
2026-02-05 18:46:14 +07:00
|
|
|
|
void (SleepActivity::*renderNoCoverSleepScreen)() const;
|
|
|
|
|
|
switch (SETTINGS.sleepScreen) {
|
|
|
|
|
|
case (CrossPointSettings::SLEEP_SCREEN_MODE::COVER_CUSTOM):
|
|
|
|
|
|
renderNoCoverSleepScreen = &SleepActivity::renderCustomSleepScreen;
|
|
|
|
|
|
break;
|
|
|
|
|
|
default:
|
|
|
|
|
|
renderNoCoverSleepScreen = &SleepActivity::renderDefaultSleepScreen;
|
|
|
|
|
|
break;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-12-21 21:17:00 +11:00
|
|
|
|
if (APP_STATE.openEpubPath.empty()) {
|
2026-02-05 18:46:14 +07:00
|
|
|
|
return (this->*renderNoCoverSleepScreen)();
|
2025-12-21 21:17:00 +11:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-12-28 23:56:05 +09:00
|
|
|
|
std::string coverBmpPath;
|
2026-01-12 10:55:47 +01:00
|
|
|
|
bool cropped = SETTINGS.sleepScreenCoverMode == CrossPointSettings::SLEEP_SCREEN_COVER_MODE::CROP;
|
2025-12-21 21:17:00 +11:00
|
|
|
|
|
2026-01-14 19:36:40 +09:00
|
|
|
|
// Check if the current book is XTC, TXT, or EPUB
|
2026-01-07 20:07:23 +10:00
|
|
|
|
if (StringUtils::checkFileExtension(APP_STATE.openEpubPath, ".xtc") ||
|
|
|
|
|
|
StringUtils::checkFileExtension(APP_STATE.openEpubPath, ".xtch")) {
|
2025-12-28 23:56:05 +09:00
|
|
|
|
// Handle XTC file
|
|
|
|
|
|
Xtc lastXtc(APP_STATE.openEpubPath, "/.crosspoint");
|
|
|
|
|
|
if (!lastXtc.load()) {
|
2026-02-11 16:25:17 +01:00
|
|
|
|
Serial.printf("[%lu] [SLP] Failed to load last XTC\n", millis());
|
2026-02-05 18:46:14 +07:00
|
|
|
|
return (this->*renderNoCoverSleepScreen)();
|
2025-12-28 23:56:05 +09:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (!lastXtc.generateCoverBmp()) {
|
2026-02-11 16:25:17 +01:00
|
|
|
|
Serial.printf("[%lu] [SLP] Failed to generate XTC cover bmp\n", millis());
|
2026-02-05 18:46:14 +07:00
|
|
|
|
return (this->*renderNoCoverSleepScreen)();
|
2025-12-28 23:56:05 +09:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
coverBmpPath = lastXtc.getCoverBmpPath();
|
2026-01-14 19:36:40 +09:00
|
|
|
|
} else if (StringUtils::checkFileExtension(APP_STATE.openEpubPath, ".txt")) {
|
|
|
|
|
|
// Handle TXT file - looks for cover image in the same folder
|
|
|
|
|
|
Txt lastTxt(APP_STATE.openEpubPath, "/.crosspoint");
|
|
|
|
|
|
if (!lastTxt.load()) {
|
2026-02-11 16:25:17 +01:00
|
|
|
|
Serial.printf("[%lu] [SLP] Failed to load last TXT\n", millis());
|
2026-02-05 18:46:14 +07:00
|
|
|
|
return (this->*renderNoCoverSleepScreen)();
|
2026-01-14 19:36:40 +09:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (!lastTxt.generateCoverBmp()) {
|
2026-02-11 16:25:17 +01:00
|
|
|
|
Serial.printf("[%lu] [SLP] No cover image found for TXT file\n", millis());
|
2026-02-05 18:46:14 +07:00
|
|
|
|
return (this->*renderNoCoverSleepScreen)();
|
2026-01-14 19:36:40 +09:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
coverBmpPath = lastTxt.getCoverBmpPath();
|
2026-01-07 20:07:23 +10:00
|
|
|
|
} else if (StringUtils::checkFileExtension(APP_STATE.openEpubPath, ".epub")) {
|
2025-12-28 23:56:05 +09:00
|
|
|
|
// Handle EPUB file
|
|
|
|
|
|
Epub lastEpub(APP_STATE.openEpubPath, "/.crosspoint");
|
feat: Add CSS parsing and CSS support in EPUBs (#411)
## Summary
* **What is the goal of this PR?**
- Adds basic CSS parsing to EPUBs and determine the CSS rules when
rendering to the screen so that text is styled correctly. Currently
supports bold, underline, italics, margin, padding, and text alignment
## Additional Context
- My main reason for wanting this is that the book I'm currently
reading, Carl's Doomsday Scenario (2nd in the Dungeon Crawler Carl
series), relies _a lot_ on styled text for telling parts of the story.
When text is bolded, it's supposed to be a message that's rendered
"on-screen" in the story. When characters are "chatting" with each
other, the text is bolded and their names are underlined. Plus, normal
emphasis is provided with italicizing words here and there. So, this
greatly improves my experience reading this book on the Xteink, and I
figured it was useful enough for others too.
- For transparency: I'm a software engineer, but I'm mostly frontend and
TypeScript/JavaScript. It's been _years_ since I did any C/C++, so I
would not be surprised if I'm doing something dumb along the way in this
code. Please don't hesitate to ask for changes if something looks off. I
heavily relied on Claude Code for help, and I had a lot of inspiration
from how [microreader](https://github.com/CidVonHighwind/microreader)
achieves their CSS parsing and styling. I did give this as good of a
code review as I could and went through everything, and _it works on my
machine_ 😄
### Before


### After


---
### AI Usage
Did you use AI tools to help write this code? **YES**, Claude Code
2026-02-05 05:28:10 -05:00
|
|
|
|
// Skip loading css since we only need metadata here
|
|
|
|
|
|
if (!lastEpub.load(true, true)) {
|
2026-02-11 16:25:17 +01:00
|
|
|
|
Serial.printf("[%lu] [SLP] Failed to load last epub\n", millis());
|
2026-02-05 18:46:14 +07:00
|
|
|
|
return (this->*renderNoCoverSleepScreen)();
|
2025-12-28 23:56:05 +09:00
|
|
|
|
}
|
|
|
|
|
|
|
2026-01-12 10:55:47 +01:00
|
|
|
|
if (!lastEpub.generateCoverBmp(cropped)) {
|
2026-02-11 16:25:17 +01:00
|
|
|
|
Serial.printf("[%lu] [SLP] Failed to generate cover bmp\n", millis());
|
2026-02-05 18:46:14 +07:00
|
|
|
|
return (this->*renderNoCoverSleepScreen)();
|
2025-12-28 23:56:05 +09:00
|
|
|
|
}
|
|
|
|
|
|
|
2026-01-12 10:55:47 +01:00
|
|
|
|
coverBmpPath = lastEpub.getCoverBmpPath(cropped);
|
2026-01-07 20:07:23 +10:00
|
|
|
|
} else {
|
2026-02-05 18:46:14 +07:00
|
|
|
|
return (this->*renderNoCoverSleepScreen)();
|
2025-12-21 18:42:06 +11:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-12-30 15:09:30 +10:00
|
|
|
|
FsFile file;
|
2026-02-08 21:29:14 +01:00
|
|
|
|
if (Storage.openFileForRead("SLP", coverBmpPath, file)) {
|
2025-12-21 18:42:06 +11:00
|
|
|
|
Bitmap bitmap(file);
|
|
|
|
|
|
if (bitmap.parseHeaders() == BmpReaderError::Ok) {
|
2026-02-11 16:25:17 +01:00
|
|
|
|
Serial.printf("[%lu] [SLP] Rendering sleep cover: %s\n", millis(), coverBmpPath.c_str());
|
2026-02-13 11:12:27 -05:00
|
|
|
|
// Derive edge cache path from cover BMP path (e.g. cover.bmp -> cover_edges.bin)
|
|
|
|
|
|
std::string edgeCachePath;
|
|
|
|
|
|
const auto dotPos = coverBmpPath.rfind(".bmp");
|
|
|
|
|
|
if (dotPos != std::string::npos) {
|
|
|
|
|
|
edgeCachePath = coverBmpPath.substr(0, dotPos) + "_edges.bin";
|
|
|
|
|
|
}
|
2026-02-09 11:52:55 -05:00
|
|
|
|
renderBitmapSleepScreen(bitmap, edgeCachePath);
|
2025-12-21 18:42:06 +11:00
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-02-05 18:46:14 +07:00
|
|
|
|
return (this->*renderNoCoverSleepScreen)();
|
2025-12-21 18:42:06 +11:00
|
|
|
|
}
|
2026-01-05 10:08:39 +01:00
|
|
|
|
|
|
|
|
|
|
void SleepActivity::renderBlankSleepScreen() const {
|
|
|
|
|
|
renderer.clearScreen();
|
2026-01-27 18:50:15 +01:00
|
|
|
|
renderer.displayBuffer(HalDisplay::HALF_REFRESH);
|
2026-01-05 10:08:39 +01:00
|
|
|
|
}
|