This commit completes a series of fixes addressing dictionary crashes,
memory issues, and UI/UX improvements.
Memory & Stability (from previous checkpoints):
- Add uncompressed dictionary (.dict) support to avoid decompression
memory issues with large dictzip chunks (58KB -> direct read)
- Implement chunked on-demand HTML parsing for large definitions,
parsing pages as user navigates rather than all at once
- Refactor TextBlock/ParsedText from std::list to std::vector,
reducing heap allocations by ~12x per TextBlock and eliminating
crashes from repeated page navigation due to heap fragmentation
- Limit cached pages to MAX_CACHED_PAGES (4) with re-parse capability
for backward navigation beyond the cache window
UI/Layout Fixes (this commit):
- Restore DictionaryMargins.h for proper orientation-aware button
hint space (front buttons: 45px, side buttons: 50px)
- Add side button hints to definition screen with proper "<" / ">"
labels for page navigation
- Add side button hints to word selection screen ("UP"/"DOWN" labels,
borderless, small font, 2px edge margin)
- Add side button hints to dictionary menu ("< Prev", "Next >")
- Fix double-button press bug when loading new chunks by checking
forward navigation availability after parsing instead of page count
- Add drawSideButtonHints() drawBorder parameter for minimal hints
- Add drawTextRotated90CCW() for LandscapeCCW text orientation
- Move page indicator up to avoid bezel cutoff
1224 lines
44 KiB
C++
1224 lines
44 KiB
C++
#include "GfxRenderer.h"
|
|
|
|
#include <Utf8.h>
|
|
|
|
void GfxRenderer::insertFont(const int fontId, EpdFontFamily font) { fontMap.insert({fontId, font}); }
|
|
|
|
void GfxRenderer::rotateCoordinates(const int x, const int y, int* rotatedX, int* rotatedY) const {
|
|
switch (orientation) {
|
|
case Portrait: {
|
|
// Logical portrait (480x800) → panel (800x480)
|
|
// Rotation: 90 degrees clockwise
|
|
*rotatedX = y;
|
|
*rotatedY = EInkDisplay::DISPLAY_HEIGHT - 1 - x;
|
|
break;
|
|
}
|
|
case LandscapeClockwise: {
|
|
// Logical landscape (800x480) rotated 180 degrees (swap top/bottom and left/right)
|
|
*rotatedX = EInkDisplay::DISPLAY_WIDTH - 1 - x;
|
|
*rotatedY = EInkDisplay::DISPLAY_HEIGHT - 1 - y;
|
|
break;
|
|
}
|
|
case PortraitInverted: {
|
|
// Logical portrait (480x800) → panel (800x480)
|
|
// Rotation: 90 degrees counter-clockwise
|
|
*rotatedX = EInkDisplay::DISPLAY_WIDTH - 1 - y;
|
|
*rotatedY = x;
|
|
break;
|
|
}
|
|
case LandscapeCounterClockwise: {
|
|
// Logical landscape (800x480) aligned with panel orientation
|
|
*rotatedX = x;
|
|
*rotatedY = y;
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
|
|
void GfxRenderer::drawPixel(const int x, const int y, const bool state) const {
|
|
uint8_t* frameBuffer = einkDisplay.getFrameBuffer();
|
|
|
|
// Early return if no framebuffer is set
|
|
if (!frameBuffer) {
|
|
Serial.printf("[%lu] [GFX] !! No framebuffer\n", millis());
|
|
return;
|
|
}
|
|
|
|
int rotatedX = 0;
|
|
int rotatedY = 0;
|
|
rotateCoordinates(x, y, &rotatedX, &rotatedY);
|
|
|
|
// Bounds checking against physical panel dimensions
|
|
if (rotatedX < 0 || rotatedX >= EInkDisplay::DISPLAY_WIDTH || rotatedY < 0 ||
|
|
rotatedY >= EInkDisplay::DISPLAY_HEIGHT) {
|
|
Serial.printf("[%lu] [GFX] !! Outside range (%d, %d) -> (%d, %d)\n", millis(), x, y, rotatedX, rotatedY);
|
|
return;
|
|
}
|
|
|
|
// Calculate byte position and bit position
|
|
const uint16_t byteIndex = rotatedY * EInkDisplay::DISPLAY_WIDTH_BYTES + (rotatedX / 8);
|
|
const uint8_t bitPosition = 7 - (rotatedX % 8); // MSB first
|
|
|
|
if (state) {
|
|
frameBuffer[byteIndex] &= ~(1 << bitPosition); // Clear bit
|
|
} else {
|
|
frameBuffer[byteIndex] |= 1 << bitPosition; // Set bit
|
|
}
|
|
}
|
|
|
|
int GfxRenderer::getTextWidth(const int fontId, const char* text, const EpdFontFamily::Style style) const {
|
|
if (fontMap.count(fontId) == 0) {
|
|
Serial.printf("[%lu] [GFX] Font %d not found\n", millis(), fontId);
|
|
return 0;
|
|
}
|
|
|
|
int w = 0, h = 0;
|
|
fontMap.at(fontId).getTextDimensions(text, &w, &h, style);
|
|
return w;
|
|
}
|
|
|
|
void GfxRenderer::drawCenteredText(const int fontId, const int y, const char* text, const bool black,
|
|
const EpdFontFamily::Style style) const {
|
|
const int x = (getScreenWidth() - getTextWidth(fontId, text, style)) / 2;
|
|
drawText(fontId, x, y, text, black, style);
|
|
}
|
|
|
|
void GfxRenderer::drawText(const int fontId, const int x, const int y, const char* text, const bool black,
|
|
const EpdFontFamily::Style style) const {
|
|
const int yPos = y + getFontAscenderSize(fontId);
|
|
int xpos = x;
|
|
|
|
// cannot draw a NULL / empty string
|
|
if (text == nullptr || *text == '\0') {
|
|
return;
|
|
}
|
|
|
|
if (fontMap.count(fontId) == 0) {
|
|
Serial.printf("[%lu] [GFX] Font %d not found\n", millis(), fontId);
|
|
return;
|
|
}
|
|
const auto font = fontMap.at(fontId);
|
|
|
|
// no printable characters
|
|
if (!font.hasPrintableChars(text, style)) {
|
|
return;
|
|
}
|
|
|
|
uint32_t cp;
|
|
while ((cp = utf8NextCodepoint(reinterpret_cast<const uint8_t**>(&text)))) {
|
|
renderChar(font, cp, &xpos, &yPos, black, style);
|
|
}
|
|
}
|
|
|
|
void GfxRenderer::drawLine(int x1, int y1, int x2, int y2, const bool state) const {
|
|
if (x1 == x2) {
|
|
if (y2 < y1) {
|
|
std::swap(y1, y2);
|
|
}
|
|
for (int y = y1; y <= y2; y++) {
|
|
drawPixel(x1, y, state);
|
|
}
|
|
} else if (y1 == y2) {
|
|
if (x2 < x1) {
|
|
std::swap(x1, x2);
|
|
}
|
|
for (int x = x1; x <= x2; x++) {
|
|
drawPixel(x, y1, state);
|
|
}
|
|
} else {
|
|
// TODO: Implement
|
|
Serial.printf("[%lu] [GFX] Line drawing not supported\n", millis());
|
|
}
|
|
}
|
|
|
|
void GfxRenderer::drawRect(const int x, const int y, const int width, const int height, const bool state) const {
|
|
drawLine(x, y, x + width - 1, y, state);
|
|
drawLine(x + width - 1, y, x + width - 1, y + height - 1, state);
|
|
drawLine(x + width - 1, y + height - 1, x, y + height - 1, state);
|
|
drawLine(x, y, x, y + height - 1, state);
|
|
}
|
|
|
|
void GfxRenderer::fillRect(const int x, const int y, const int width, const int height, const bool state) const {
|
|
for (int fillY = y; fillY < y + height; fillY++) {
|
|
drawLine(x, fillY, x + width - 1, fillY, state);
|
|
}
|
|
}
|
|
|
|
void GfxRenderer::fillRectGray(const int x, const int y, const int width, const int height,
|
|
const uint8_t grayLevel) const {
|
|
// Fill rectangle with 4-level grayscale value.
|
|
// The grayscale encoding for 4 levels uses 3 passes:
|
|
// - Level 0 (black): BW=black, MSB=skip, LSB=skip
|
|
// - Level 1 (dark gray): BW=black, MSB=white, LSB=white
|
|
// - Level 2 (light gray): BW=black, MSB=white, LSB=skip
|
|
// - Level 3 (white): BW=skip, MSB=skip, LSB=skip
|
|
|
|
if (width <= 0 || height <= 0) return;
|
|
|
|
switch (renderMode) {
|
|
case BW:
|
|
// In BW mode, fill with black for levels 0-2, skip for level 3
|
|
if (grayLevel < 3) {
|
|
fillRect(x, y, width, height, true); // true = black
|
|
}
|
|
// Level 3 = white, which is the default clear color, so skip
|
|
break;
|
|
|
|
case GRAYSCALE_MSB:
|
|
// In MSB mode (buffer cleared to black), fill with white for levels 1-2
|
|
if (grayLevel == 1 || grayLevel == 2) {
|
|
fillRect(x, y, width, height, false); // false = white
|
|
}
|
|
// Levels 0 and 3 stay black (which is correct for 0, will combine correctly for 3)
|
|
break;
|
|
|
|
case GRAYSCALE_LSB:
|
|
// In LSB mode (buffer cleared to black), fill with white for level 1 only
|
|
if (grayLevel == 1) {
|
|
fillRect(x, y, width, height, false); // false = white
|
|
}
|
|
// Levels 0, 2, 3 stay black
|
|
break;
|
|
}
|
|
}
|
|
|
|
void GfxRenderer::drawImage(const uint8_t bitmap[], const int x, const int y, const int width, const int height) const {
|
|
int rotatedX = 0;
|
|
int rotatedY = 0;
|
|
rotateCoordinates(x, y, &rotatedX, &rotatedY);
|
|
// Rotate origin corner
|
|
switch (orientation) {
|
|
case Portrait:
|
|
rotatedY = rotatedY - height;
|
|
break;
|
|
case PortraitInverted:
|
|
rotatedX = rotatedX - width;
|
|
break;
|
|
case LandscapeClockwise:
|
|
rotatedY = rotatedY - height;
|
|
rotatedX = rotatedX - width;
|
|
break;
|
|
case LandscapeCounterClockwise:
|
|
break;
|
|
}
|
|
// TODO: Rotate bits
|
|
einkDisplay.drawImage(bitmap, rotatedX, rotatedY, width, height);
|
|
}
|
|
|
|
void GfxRenderer::drawImageRotated(const uint8_t bitmap[], const int x, const int y, const int width, const int height,
|
|
const ImageRotation rotation, const bool invert) const {
|
|
// Calculate output dimensions based on rotation
|
|
const int outWidth = (rotation == ROTATE_90 || rotation == ROTATE_270) ? height : width;
|
|
const int outHeight = (rotation == ROTATE_90 || rotation == ROTATE_270) ? width : height;
|
|
|
|
// Draw each pixel from the source bitmap with rotation
|
|
for (int srcY = 0; srcY < height; srcY++) {
|
|
for (int srcX = 0; srcX < width; srcX++) {
|
|
// Read source pixel (1-bit packed, MSB first)
|
|
const int byteIndex = srcY * ((width + 7) / 8) + (srcX / 8);
|
|
const int bitIndex = 7 - (srcX % 8);
|
|
const bool srcPixel = (bitmap[byteIndex] >> bitIndex) & 1;
|
|
|
|
// Apply inversion if requested
|
|
const bool pixelState = invert ? !srcPixel : srcPixel;
|
|
|
|
// Skip black pixels (transparent on black background)
|
|
if (!pixelState) continue;
|
|
|
|
// Calculate destination coordinates based on rotation (clockwise)
|
|
int dstX, dstY;
|
|
switch (rotation) {
|
|
case ROTATE_0:
|
|
dstX = srcX;
|
|
dstY = srcY;
|
|
break;
|
|
case ROTATE_90: // 90° clockwise
|
|
dstX = height - 1 - srcY;
|
|
dstY = srcX;
|
|
break;
|
|
case ROTATE_180:
|
|
dstX = width - 1 - srcX;
|
|
dstY = height - 1 - srcY;
|
|
break;
|
|
case ROTATE_270: // 270° clockwise (= 90° counter-clockwise)
|
|
dstX = srcY;
|
|
dstY = width - 1 - srcX;
|
|
break;
|
|
}
|
|
|
|
// Draw the pixel at the rotated position
|
|
// Note: pixelState=true means white pixel, but drawPixel(state=true) draws black
|
|
// So we invert: draw with state=false for white pixels
|
|
drawPixel(x + dstX, y + dstY, false);
|
|
}
|
|
}
|
|
}
|
|
|
|
void GfxRenderer::drawBitmap(const Bitmap& bitmap, const int x, const int y, const int maxWidth, const int maxHeight,
|
|
const float cropX, const float cropY, const bool invert) const {
|
|
// For 1-bit bitmaps, use optimized 1-bit rendering path (no crop support for 1-bit)
|
|
if (bitmap.is1Bit() && cropX == 0.0f && cropY == 0.0f) {
|
|
drawBitmap1Bit(bitmap, x, y, maxWidth, maxHeight, invert);
|
|
return;
|
|
}
|
|
|
|
float scale = 1.0f;
|
|
int cropPixX = std::floor(bitmap.getWidth() * cropX / 2.0f);
|
|
int cropPixY = std::floor(bitmap.getHeight() * cropY / 2.0f);
|
|
Serial.printf("[%lu] [GFX] Cropping %dx%d by %dx%d pix, is %s\n", millis(), bitmap.getWidth(), bitmap.getHeight(),
|
|
cropPixX, cropPixY, bitmap.isTopDown() ? "top-down" : "bottom-up");
|
|
|
|
// Calculate effective image dimensions after cropping
|
|
const int effectiveWidth = static_cast<int>((1.0f - cropX) * bitmap.getWidth());
|
|
const int effectiveHeight = static_cast<int>((1.0f - cropY) * bitmap.getHeight());
|
|
|
|
// Calculate scale to fit within maxWidth/maxHeight (supports both up and down scaling)
|
|
if (maxWidth > 0 && maxHeight > 0) {
|
|
const float scaleX = static_cast<float>(maxWidth) / static_cast<float>(effectiveWidth);
|
|
const float scaleY = static_cast<float>(maxHeight) / static_cast<float>(effectiveHeight);
|
|
scale = std::min(scaleX, scaleY);
|
|
} else if (maxWidth > 0) {
|
|
scale = static_cast<float>(maxWidth) / static_cast<float>(effectiveWidth);
|
|
} else if (maxHeight > 0) {
|
|
scale = static_cast<float>(maxHeight) / static_cast<float>(effectiveHeight);
|
|
}
|
|
|
|
const bool isUpscaling = scale > 1.0f;
|
|
const bool isDownscaling = scale < 1.0f;
|
|
Serial.printf("[%lu] [GFX] Scaling by %f - %s\n", millis(), scale,
|
|
isUpscaling ? "upscaling" : (isDownscaling ? "downscaling" : "no scaling"));
|
|
|
|
// Calculate output row size (2 bits per pixel, packed into bytes)
|
|
// IMPORTANT: Use int, not uint8_t, to avoid overflow for images > 1020 pixels wide
|
|
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) {
|
|
Serial.printf("[%lu] [GFX] !! Failed to allocate BMP row buffers\n", millis());
|
|
free(outputRow);
|
|
free(rowBytes);
|
|
return;
|
|
}
|
|
|
|
// Track the last drawn Y position for upscaling (to fill gaps)
|
|
int lastDrawnY = -1;
|
|
|
|
for (int bmpY = 0; bmpY < bitmap.getHeight(); bmpY++) {
|
|
if (bitmap.readNextRow(outputRow, rowBytes) != BmpReaderError::Ok) {
|
|
Serial.printf("[%lu] [GFX] Failed to read row %d from bitmap\n", millis(), bmpY);
|
|
free(outputRow);
|
|
free(rowBytes);
|
|
return;
|
|
}
|
|
|
|
// Skip rows in the crop area
|
|
if (bmpY < cropPixY || bmpY >= bitmap.getHeight() - cropPixY) {
|
|
continue;
|
|
}
|
|
|
|
// Calculate the source Y coordinate (relative to cropped area)
|
|
const int srcY = bmpY - cropPixY;
|
|
|
|
// The BMP's (0, 0) is the bottom-left corner (if the height is positive, top-left if negative).
|
|
// Screen's (0, 0) is the top-left corner.
|
|
const int logicalY = bitmap.isTopDown() ? srcY : (effectiveHeight - 1 - srcY);
|
|
|
|
// Calculate screen Y position
|
|
const int screenYStart = y + static_cast<int>(std::floor(logicalY * scale));
|
|
// For upscaling, calculate the end position for this source row
|
|
const int screenYEnd =
|
|
isUpscaling ? (y + static_cast<int>(std::floor((logicalY + 1) * scale))) : (screenYStart + 1);
|
|
|
|
// Draw to all Y positions this source row maps to (for upscaling, this fills gaps)
|
|
for (int screenY = screenYStart; screenY < screenYEnd; screenY++) {
|
|
if (screenY < 0) continue;
|
|
if (screenY >= getScreenHeight()) break;
|
|
|
|
for (int bmpX = cropPixX; bmpX < bitmap.getWidth() - cropPixX; bmpX++) {
|
|
const int srcX = bmpX - cropPixX;
|
|
|
|
// Calculate screen X position
|
|
const int screenXStart = x + static_cast<int>(std::floor(srcX * scale));
|
|
// For upscaling, calculate the end position for this source pixel
|
|
const int screenXEnd =
|
|
isUpscaling ? (x + static_cast<int>(std::floor((srcX + 1) * scale))) : (screenXStart + 1);
|
|
|
|
const uint8_t val = outputRow[bmpX / 4] >> (6 - ((bmpX * 2) % 8)) & 0x3;
|
|
|
|
// Draw to all X positions this source pixel maps to (for upscaling, this fills gaps)
|
|
for (int screenX = screenXStart; screenX < screenXEnd; screenX++) {
|
|
if (screenX < 0) continue;
|
|
if (screenX >= getScreenWidth()) break;
|
|
|
|
if (renderMode == BW && val < 3) {
|
|
drawPixel(screenX, screenY);
|
|
} else if (renderMode == GRAYSCALE_MSB && (val == 1 || val == 2)) {
|
|
drawPixel(screenX, screenY, false);
|
|
} else if (renderMode == GRAYSCALE_LSB && val == 1) {
|
|
drawPixel(screenX, screenY, false);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
free(outputRow);
|
|
free(rowBytes);
|
|
}
|
|
|
|
void GfxRenderer::drawBitmap1Bit(const Bitmap& bitmap, const int x, const int y, const int maxWidth,
|
|
const int maxHeight, const bool invert) const {
|
|
float scale = 1.0f;
|
|
|
|
// Calculate scale to fit within maxWidth/maxHeight (supports both up and down scaling)
|
|
if (maxWidth > 0 && maxHeight > 0) {
|
|
const float scaleX = static_cast<float>(maxWidth) / static_cast<float>(bitmap.getWidth());
|
|
const float scaleY = static_cast<float>(maxHeight) / static_cast<float>(bitmap.getHeight());
|
|
scale = std::min(scaleX, scaleY);
|
|
} else if (maxWidth > 0) {
|
|
scale = static_cast<float>(maxWidth) / static_cast<float>(bitmap.getWidth());
|
|
} else if (maxHeight > 0) {
|
|
scale = static_cast<float>(maxHeight) / static_cast<float>(bitmap.getHeight());
|
|
}
|
|
|
|
const bool isUpscaling = scale > 1.0f;
|
|
|
|
// For 1-bit BMP, output is still 2-bit packed (for consistency with readNextRow)
|
|
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) {
|
|
Serial.printf("[%lu] [GFX] !! Failed to allocate 1-bit BMP row buffers\n", millis());
|
|
free(outputRow);
|
|
free(rowBytes);
|
|
return;
|
|
}
|
|
|
|
for (int bmpY = 0; bmpY < bitmap.getHeight(); bmpY++) {
|
|
// Read rows sequentially using readNextRow
|
|
if (bitmap.readNextRow(outputRow, rowBytes) != BmpReaderError::Ok) {
|
|
Serial.printf("[%lu] [GFX] Failed to read row %d from 1-bit bitmap\n", millis(), bmpY);
|
|
free(outputRow);
|
|
free(rowBytes);
|
|
return;
|
|
}
|
|
|
|
// Calculate screen Y based on whether BMP is top-down or bottom-up
|
|
const int logicalY = bitmap.isTopDown() ? bmpY : bitmap.getHeight() - 1 - bmpY;
|
|
|
|
// Calculate screen Y position
|
|
const int screenYStart = y + static_cast<int>(std::floor(logicalY * scale));
|
|
// For upscaling, calculate the end position for this source row
|
|
const int screenYEnd =
|
|
isUpscaling ? (y + static_cast<int>(std::floor((logicalY + 1) * scale))) : (screenYStart + 1);
|
|
|
|
// Draw to all Y positions this source row maps to (for upscaling, this fills gaps)
|
|
for (int screenY = screenYStart; screenY < screenYEnd; screenY++) {
|
|
if (screenY < 0) continue;
|
|
if (screenY >= getScreenHeight()) continue;
|
|
|
|
for (int bmpX = 0; bmpX < bitmap.getWidth(); bmpX++) {
|
|
// Calculate screen X position
|
|
const int screenXStart = x + static_cast<int>(std::floor(bmpX * scale));
|
|
// For upscaling, calculate the end position for this source pixel
|
|
const int screenXEnd =
|
|
isUpscaling ? (x + static_cast<int>(std::floor((bmpX + 1) * scale))) : (screenXStart + 1);
|
|
|
|
// Get 2-bit value (result of readNextRow quantization)
|
|
const uint8_t val = outputRow[bmpX / 4] >> (6 - ((bmpX * 2) % 8)) & 0x3;
|
|
|
|
// For 1-bit source: 0 or 1 -> map to black (0,1,2) or white (3)
|
|
// val < 3 means black pixel, val == 3 means white pixel
|
|
// When inverted: draw white pixels as black, skip black pixels
|
|
const bool isBlackPixel = (val < 3);
|
|
const bool shouldDraw = invert ? !isBlackPixel : isBlackPixel;
|
|
|
|
if (shouldDraw) {
|
|
// Draw to all X positions this source pixel maps to (for upscaling, this fills gaps)
|
|
for (int screenX = screenXStart; screenX < screenXEnd; screenX++) {
|
|
if (screenX < 0) continue;
|
|
if (screenX >= getScreenWidth()) break;
|
|
drawPixel(screenX, screenY, true);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
free(outputRow);
|
|
free(rowBytes);
|
|
}
|
|
|
|
void GfxRenderer::fillPolygon(const int* xPoints, const int* yPoints, int numPoints, bool state) const {
|
|
if (numPoints < 3) return;
|
|
|
|
// Find bounding box
|
|
int minY = yPoints[0], maxY = yPoints[0];
|
|
for (int i = 1; i < numPoints; i++) {
|
|
if (yPoints[i] < minY) minY = yPoints[i];
|
|
if (yPoints[i] > maxY) maxY = yPoints[i];
|
|
}
|
|
|
|
// Clip to screen
|
|
if (minY < 0) minY = 0;
|
|
if (maxY >= getScreenHeight()) maxY = getScreenHeight() - 1;
|
|
|
|
// Allocate node buffer for scanline algorithm
|
|
auto* nodeX = static_cast<int*>(malloc(numPoints * sizeof(int)));
|
|
if (!nodeX) {
|
|
Serial.printf("[%lu] [GFX] !! Failed to allocate polygon node buffer\n", millis());
|
|
return;
|
|
}
|
|
|
|
// Scanline fill algorithm
|
|
for (int scanY = minY; scanY <= maxY; scanY++) {
|
|
int nodes = 0;
|
|
|
|
// Find all intersection points with edges
|
|
int j = numPoints - 1;
|
|
for (int i = 0; i < numPoints; i++) {
|
|
if ((yPoints[i] < scanY && yPoints[j] >= scanY) || (yPoints[j] < scanY && yPoints[i] >= scanY)) {
|
|
// Calculate X intersection using fixed-point to avoid float
|
|
int dy = yPoints[j] - yPoints[i];
|
|
if (dy != 0) {
|
|
nodeX[nodes++] = xPoints[i] + (scanY - yPoints[i]) * (xPoints[j] - xPoints[i]) / dy;
|
|
}
|
|
}
|
|
j = i;
|
|
}
|
|
|
|
// Sort nodes by X (simple bubble sort, numPoints is small)
|
|
for (int i = 0; i < nodes - 1; i++) {
|
|
for (int k = i + 1; k < nodes; k++) {
|
|
if (nodeX[i] > nodeX[k]) {
|
|
int temp = nodeX[i];
|
|
nodeX[i] = nodeX[k];
|
|
nodeX[k] = temp;
|
|
}
|
|
}
|
|
}
|
|
|
|
// Fill between pairs of nodes
|
|
for (int i = 0; i < nodes - 1; i += 2) {
|
|
int startX = nodeX[i];
|
|
int endX = nodeX[i + 1];
|
|
|
|
// Clip to screen
|
|
if (startX < 0) startX = 0;
|
|
if (endX >= getScreenWidth()) endX = getScreenWidth() - 1;
|
|
|
|
// Draw horizontal line
|
|
for (int x = startX; x <= endX; x++) {
|
|
drawPixel(x, scanY, state);
|
|
}
|
|
}
|
|
}
|
|
|
|
free(nodeX);
|
|
}
|
|
|
|
void GfxRenderer::clearScreen(const uint8_t color) const { einkDisplay.clearScreen(color); }
|
|
|
|
void GfxRenderer::invertScreen() const {
|
|
uint8_t* buffer = einkDisplay.getFrameBuffer();
|
|
if (!buffer) {
|
|
Serial.printf("[%lu] [GFX] !! No framebuffer in invertScreen\n", millis());
|
|
return;
|
|
}
|
|
for (int i = 0; i < EInkDisplay::BUFFER_SIZE; i++) {
|
|
buffer[i] = ~buffer[i];
|
|
}
|
|
}
|
|
|
|
void GfxRenderer::displayBuffer(const EInkDisplay::RefreshMode refreshMode) const {
|
|
einkDisplay.displayBuffer(refreshMode);
|
|
}
|
|
|
|
std::string GfxRenderer::truncatedText(const int fontId, const char* text, const int maxWidth,
|
|
const EpdFontFamily::Style style) const {
|
|
std::string item = text;
|
|
int itemWidth = getTextWidth(fontId, item.c_str(), style);
|
|
while (itemWidth > maxWidth && item.length() > 8) {
|
|
item.replace(item.length() - 5, 5, "...");
|
|
itemWidth = getTextWidth(fontId, item.c_str(), style);
|
|
}
|
|
return item;
|
|
}
|
|
|
|
// Note: Internal driver treats screen in command orientation; this library exposes a logical orientation
|
|
int GfxRenderer::getScreenWidth() const {
|
|
switch (orientation) {
|
|
case Portrait:
|
|
case PortraitInverted:
|
|
// 480px wide in portrait logical coordinates
|
|
return EInkDisplay::DISPLAY_HEIGHT;
|
|
case LandscapeClockwise:
|
|
case LandscapeCounterClockwise:
|
|
// 800px wide in landscape logical coordinates
|
|
return EInkDisplay::DISPLAY_WIDTH;
|
|
}
|
|
return EInkDisplay::DISPLAY_HEIGHT;
|
|
}
|
|
|
|
int GfxRenderer::getScreenHeight() const {
|
|
switch (orientation) {
|
|
case Portrait:
|
|
case PortraitInverted:
|
|
// 800px tall in portrait logical coordinates
|
|
return EInkDisplay::DISPLAY_WIDTH;
|
|
case LandscapeClockwise:
|
|
case LandscapeCounterClockwise:
|
|
// 480px tall in landscape logical coordinates
|
|
return EInkDisplay::DISPLAY_HEIGHT;
|
|
}
|
|
return EInkDisplay::DISPLAY_WIDTH;
|
|
}
|
|
|
|
int GfxRenderer::getSpaceWidth(const int fontId) const {
|
|
if (fontMap.count(fontId) == 0) {
|
|
Serial.printf("[%lu] [GFX] Font %d not found\n", millis(), fontId);
|
|
return 0;
|
|
}
|
|
|
|
return fontMap.at(fontId).getGlyph(' ', EpdFontFamily::REGULAR)->advanceX;
|
|
}
|
|
|
|
int GfxRenderer::getIndentWidth(const int fontId, const char* text) const {
|
|
if (fontMap.count(fontId) == 0) {
|
|
Serial.printf("[%lu] [GFX] Font %d not found\n", millis(), fontId);
|
|
return 0;
|
|
}
|
|
|
|
uint32_t cp;
|
|
int width = 0;
|
|
while ((cp = utf8NextCodepoint(reinterpret_cast<const uint8_t**>(&text)))) {
|
|
width += fontMap.at(fontId).getGlyph(cp, EpdFontFamily::REGULAR)->advanceX;
|
|
}
|
|
return width;
|
|
}
|
|
|
|
int GfxRenderer::getFontAscenderSize(const int fontId) const {
|
|
if (fontMap.count(fontId) == 0) {
|
|
Serial.printf("[%lu] [GFX] Font %d not found\n", millis(), fontId);
|
|
return 0;
|
|
}
|
|
|
|
return fontMap.at(fontId).getData(EpdFontFamily::REGULAR)->ascender;
|
|
}
|
|
|
|
int GfxRenderer::getLineHeight(const int fontId) const {
|
|
if (fontMap.count(fontId) == 0) {
|
|
Serial.printf("[%lu] [GFX] Font %d not found\n", millis(), fontId);
|
|
return 0;
|
|
}
|
|
|
|
return fontMap.at(fontId).getData(EpdFontFamily::REGULAR)->advanceY;
|
|
}
|
|
|
|
void GfxRenderer::drawButtonHints(const int fontId, const char* btn1, const char* btn2, const char* btn3,
|
|
const char* btn4) {
|
|
const Orientation orig_orientation = getOrientation();
|
|
setOrientation(Orientation::Portrait);
|
|
|
|
const int pageHeight = getScreenHeight();
|
|
constexpr int buttonWidth = 106;
|
|
constexpr int buttonHeight = 40;
|
|
constexpr int baseButtonY = 40; // Base distance from bottom
|
|
constexpr int textYOffset = 7; // Distance from top of button to text baseline
|
|
constexpr int baseButtonPositions[] = {25, 130, 245, 350};
|
|
const char* labels[] = {btn1, btn2, btn3, btn4};
|
|
|
|
// Apply bezel compensation (in portrait mode, bottom bezel affects Y position)
|
|
const int bezelBottom = getBezelOffsetBottom();
|
|
const int bezelLeft = getBezelOffsetLeft();
|
|
const int buttonY = baseButtonY + bezelBottom;
|
|
|
|
for (int i = 0; i < 4; i++) {
|
|
// Only draw if the label is non-empty
|
|
if (labels[i] != nullptr && labels[i][0] != '\0') {
|
|
const int x = baseButtonPositions[i] + bezelLeft;
|
|
fillRect(x, pageHeight - buttonY, buttonWidth, buttonHeight, false);
|
|
drawRect(x, pageHeight - buttonY, buttonWidth, buttonHeight);
|
|
const int textWidth = getTextWidth(fontId, labels[i]);
|
|
const int textX = x + (buttonWidth - 1 - textWidth) / 2;
|
|
drawText(fontId, textX, pageHeight - buttonY + textYOffset, labels[i]);
|
|
}
|
|
}
|
|
|
|
setOrientation(orig_orientation);
|
|
}
|
|
|
|
void GfxRenderer::drawSideButtonHints(const int fontId, const char* topBtn, const char* bottomBtn,
|
|
const bool drawBorder) {
|
|
const Orientation orig_orientation = getOrientation();
|
|
setOrientation(Orientation::Portrait);
|
|
|
|
const int screenWidth = getScreenWidth();
|
|
constexpr int buttonWidth = 40; // Width on screen (height when rotated)
|
|
constexpr int buttonHeight = 80; // Height on screen (width when rotated)
|
|
constexpr int baseButtonX = 5; // Base distance from right edge
|
|
// Position for the button group - buttons share a border so they're adjacent
|
|
constexpr int baseTopButtonY = 345; // Base top button position
|
|
|
|
// Apply bezel compensation (in portrait mode)
|
|
const int bezelRight = getBezelOffsetRight();
|
|
const int buttonX = baseButtonX + bezelRight;
|
|
const int topButtonY = baseTopButtonY; // Y position doesn't need adjustment for side buttons
|
|
|
|
const char* labels[] = {topBtn, bottomBtn};
|
|
|
|
// Draw the shared border for both buttons as one unit
|
|
const int x = screenWidth - buttonX - buttonWidth;
|
|
|
|
if (drawBorder) {
|
|
// Draw top button outline (3 sides, bottom open)
|
|
if (topBtn != nullptr && topBtn[0] != '\0') {
|
|
drawLine(x, topButtonY, x + buttonWidth - 1, topButtonY); // Top
|
|
drawLine(x, topButtonY, x, topButtonY + buttonHeight - 1); // Left
|
|
drawLine(x + buttonWidth - 1, topButtonY, x + buttonWidth - 1, topButtonY + buttonHeight - 1); // Right
|
|
}
|
|
|
|
// Draw shared middle border
|
|
if ((topBtn != nullptr && topBtn[0] != '\0') || (bottomBtn != nullptr && bottomBtn[0] != '\0')) {
|
|
drawLine(x, topButtonY + buttonHeight, x + buttonWidth - 1, topButtonY + buttonHeight); // Shared border
|
|
}
|
|
|
|
// Draw bottom button outline (3 sides, top is shared)
|
|
if (bottomBtn != nullptr && bottomBtn[0] != '\0') {
|
|
drawLine(x, topButtonY + buttonHeight, x, topButtonY + 2 * buttonHeight - 1); // Left
|
|
drawLine(x + buttonWidth - 1, topButtonY + buttonHeight, x + buttonWidth - 1,
|
|
topButtonY + 2 * buttonHeight - 1); // Right
|
|
drawLine(x, topButtonY + 2 * buttonHeight - 1, x + buttonWidth - 1, topButtonY + 2 * buttonHeight - 1); // Bottom
|
|
}
|
|
}
|
|
|
|
// Draw text for each button
|
|
// Use CCW rotation for LandscapeCCW so text reads in same direction as screen content
|
|
const bool useCCW = (orig_orientation == Orientation::LandscapeCounterClockwise);
|
|
|
|
for (int i = 0; i < 2; i++) {
|
|
if (labels[i] != nullptr && labels[i][0] != '\0') {
|
|
const int y = topButtonY + i * buttonHeight;
|
|
|
|
// Draw rotated text centered in the button
|
|
const int textWidth = getTextWidth(fontId, labels[i]);
|
|
const int textHeight = getTextHeight(fontId);
|
|
|
|
int textX, textY;
|
|
if (drawBorder) {
|
|
// Center the rotated text in the button
|
|
textX = x + (buttonWidth - textHeight) / 2;
|
|
textY = useCCW ? y + (buttonHeight - textWidth) / 2 : y + (buttonHeight + textWidth) / 2;
|
|
} else {
|
|
// Position at edge with 2px margin (no border mode)
|
|
textX = screenWidth - bezelRight - textHeight - 2;
|
|
textY = useCCW ? y + (buttonHeight - textWidth) / 2 : y + (buttonHeight + textWidth) / 2;
|
|
}
|
|
|
|
if (useCCW) {
|
|
drawTextRotated90CCW(fontId, textX, textY, labels[i]);
|
|
} else {
|
|
drawTextRotated90CW(fontId, textX, textY, labels[i]);
|
|
}
|
|
}
|
|
}
|
|
|
|
setOrientation(orig_orientation);
|
|
}
|
|
|
|
int GfxRenderer::getTextHeight(const int fontId) const {
|
|
if (fontMap.count(fontId) == 0) {
|
|
Serial.printf("[%lu] [GFX] Font %d not found\n", millis(), fontId);
|
|
return 0;
|
|
}
|
|
return fontMap.at(fontId).getData(EpdFontFamily::REGULAR)->ascender;
|
|
}
|
|
|
|
void GfxRenderer::drawTextRotated90CW(const int fontId, const int x, const int y, const char* text, const bool black,
|
|
const EpdFontFamily::Style style) const {
|
|
// Cannot draw a NULL / empty string
|
|
if (text == nullptr || *text == '\0') {
|
|
return;
|
|
}
|
|
|
|
if (fontMap.count(fontId) == 0) {
|
|
Serial.printf("[%lu] [GFX] Font %d not found\n", millis(), fontId);
|
|
return;
|
|
}
|
|
const auto font = fontMap.at(fontId);
|
|
|
|
// No printable characters
|
|
if (!font.hasPrintableChars(text, style)) {
|
|
return;
|
|
}
|
|
|
|
// For 90° clockwise rotation:
|
|
// Original (glyphX, glyphY) -> Rotated (glyphY, -glyphX)
|
|
// Text reads from bottom to top
|
|
|
|
int yPos = y; // Current Y position (decreases as we draw characters)
|
|
|
|
uint32_t cp;
|
|
while ((cp = utf8NextCodepoint(reinterpret_cast<const uint8_t**>(&text)))) {
|
|
const EpdGlyph* glyph = font.getGlyph(cp, style);
|
|
if (!glyph) {
|
|
glyph = font.getGlyph(REPLACEMENT_GLYPH, style);
|
|
}
|
|
if (!glyph) {
|
|
continue;
|
|
}
|
|
|
|
const int is2Bit = font.getData(style)->is2Bit;
|
|
const uint32_t offset = glyph->dataOffset;
|
|
const uint8_t width = glyph->width;
|
|
const uint8_t height = glyph->height;
|
|
const int left = glyph->left;
|
|
const int top = glyph->top;
|
|
|
|
const uint8_t* bitmap = &font.getData(style)->bitmap[offset];
|
|
|
|
if (bitmap != nullptr) {
|
|
for (int glyphY = 0; glyphY < height; glyphY++) {
|
|
for (int glyphX = 0; glyphX < width; glyphX++) {
|
|
const int pixelPosition = glyphY * width + glyphX;
|
|
|
|
// 90° clockwise rotation transformation:
|
|
// screenX = x + (ascender - top + glyphY)
|
|
// screenY = yPos - (left + glyphX)
|
|
const int screenX = x + (font.getData(style)->ascender - top + glyphY);
|
|
const int screenY = yPos - left - glyphX;
|
|
|
|
if (is2Bit) {
|
|
const uint8_t byte = bitmap[pixelPosition / 4];
|
|
const uint8_t bit_index = (3 - pixelPosition % 4) * 2;
|
|
const uint8_t bmpVal = 3 - (byte >> bit_index) & 0x3;
|
|
|
|
if (renderMode == BW && bmpVal < 3) {
|
|
drawPixel(screenX, screenY, black);
|
|
} else if (renderMode == GRAYSCALE_MSB && (bmpVal == 1 || bmpVal == 2)) {
|
|
drawPixel(screenX, screenY, false);
|
|
} else if (renderMode == GRAYSCALE_LSB && bmpVal == 1) {
|
|
drawPixel(screenX, screenY, false);
|
|
}
|
|
} else {
|
|
const uint8_t byte = bitmap[pixelPosition / 8];
|
|
const uint8_t bit_index = 7 - (pixelPosition % 8);
|
|
|
|
if ((byte >> bit_index) & 1) {
|
|
drawPixel(screenX, screenY, black);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
// Move to next character position (going up, so decrease Y)
|
|
yPos -= glyph->advanceX;
|
|
}
|
|
}
|
|
|
|
void GfxRenderer::drawTextRotated90CCW(const int fontId, const int x, const int y, const char* text, const bool black,
|
|
const EpdFontFamily::Style style) const {
|
|
// Cannot draw a NULL / empty string
|
|
if (text == nullptr || *text == '\0') {
|
|
return;
|
|
}
|
|
|
|
if (fontMap.count(fontId) == 0) {
|
|
Serial.printf("[%lu] [GFX] Font %d not found\n", millis(), fontId);
|
|
return;
|
|
}
|
|
const auto font = fontMap.at(fontId);
|
|
|
|
// No printable characters
|
|
if (!font.hasPrintableChars(text, style)) {
|
|
return;
|
|
}
|
|
|
|
// For 90° counter-clockwise rotation:
|
|
// Original (glyphX, glyphY) -> Rotated (-glyphY, glyphX)
|
|
// Text reads from top to bottom
|
|
|
|
int yPos = y; // Current Y position (increases as we draw characters)
|
|
|
|
uint32_t cp;
|
|
while ((cp = utf8NextCodepoint(reinterpret_cast<const uint8_t**>(&text)))) {
|
|
const EpdGlyph* glyph = font.getGlyph(cp, style);
|
|
if (!glyph) {
|
|
glyph = font.getGlyph(REPLACEMENT_GLYPH, style);
|
|
}
|
|
if (!glyph) {
|
|
continue;
|
|
}
|
|
|
|
const int is2Bit = font.getData(style)->is2Bit;
|
|
const uint32_t offset = glyph->dataOffset;
|
|
const uint8_t width = glyph->width;
|
|
const uint8_t height = glyph->height;
|
|
const int left = glyph->left;
|
|
const int top = glyph->top;
|
|
|
|
const uint8_t* bitmap = &font.getData(style)->bitmap[offset];
|
|
|
|
if (bitmap != nullptr) {
|
|
for (int glyphY = 0; glyphY < height; glyphY++) {
|
|
for (int glyphX = 0; glyphX < width; glyphX++) {
|
|
const int pixelPosition = glyphY * width + glyphX;
|
|
|
|
// 90° counter-clockwise rotation transformation:
|
|
// screenX = x + (top - glyphY)
|
|
// screenY = yPos + (left + glyphX)
|
|
const int screenX = x + (top - glyphY);
|
|
const int screenY = yPos + left + glyphX;
|
|
|
|
if (is2Bit) {
|
|
const uint8_t byte = bitmap[pixelPosition / 4];
|
|
const uint8_t bit_index = (3 - pixelPosition % 4) * 2;
|
|
const uint8_t bmpVal = 3 - (byte >> bit_index) & 0x3;
|
|
|
|
if (renderMode == BW && bmpVal < 3) {
|
|
drawPixel(screenX, screenY, black);
|
|
} else if (renderMode == GRAYSCALE_MSB && (bmpVal == 1 || bmpVal == 2)) {
|
|
drawPixel(screenX, screenY, false);
|
|
} else if (renderMode == GRAYSCALE_LSB && bmpVal == 1) {
|
|
drawPixel(screenX, screenY, false);
|
|
}
|
|
} else {
|
|
const uint8_t byte = bitmap[pixelPosition / 8];
|
|
const uint8_t bit_index = 7 - (pixelPosition % 8);
|
|
|
|
if ((byte >> bit_index) & 1) {
|
|
drawPixel(screenX, screenY, black);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
// Move to next character position (going down, so increase Y)
|
|
yPos += glyph->advanceX;
|
|
}
|
|
}
|
|
|
|
uint8_t* GfxRenderer::getFrameBuffer() const { return einkDisplay.getFrameBuffer(); }
|
|
|
|
size_t GfxRenderer::getBufferSize() { return EInkDisplay::BUFFER_SIZE; }
|
|
|
|
void GfxRenderer::grayscaleRevert() const { einkDisplay.grayscaleRevert(); }
|
|
|
|
void GfxRenderer::copyGrayscaleLsbBuffers() const { einkDisplay.copyGrayscaleLsbBuffers(einkDisplay.getFrameBuffer()); }
|
|
|
|
void GfxRenderer::copyGrayscaleMsbBuffers() const { einkDisplay.copyGrayscaleMsbBuffers(einkDisplay.getFrameBuffer()); }
|
|
|
|
void GfxRenderer::displayGrayBuffer() const { einkDisplay.displayGrayBuffer(); }
|
|
|
|
void GfxRenderer::freeBwBufferChunks() {
|
|
for (auto& bwBufferChunk : bwBufferChunks) {
|
|
if (bwBufferChunk) {
|
|
free(bwBufferChunk);
|
|
bwBufferChunk = nullptr;
|
|
}
|
|
}
|
|
}
|
|
|
|
/**
|
|
* This should be called before grayscale buffers are populated.
|
|
* A `restoreBwBuffer` call should always follow the grayscale render if this method was called.
|
|
* Uses chunked allocation to avoid needing 48KB of contiguous memory.
|
|
* Returns true if buffer was stored successfully, false if allocation failed.
|
|
*/
|
|
bool GfxRenderer::storeBwBuffer() {
|
|
const uint8_t* frameBuffer = einkDisplay.getFrameBuffer();
|
|
if (!frameBuffer) {
|
|
Serial.printf("[%lu] [GFX] !! No framebuffer in storeBwBuffer\n", millis());
|
|
return false;
|
|
}
|
|
|
|
// Allocate and copy each chunk
|
|
for (size_t i = 0; i < BW_BUFFER_NUM_CHUNKS; i++) {
|
|
// Check if any chunks are already allocated
|
|
if (bwBufferChunks[i]) {
|
|
Serial.printf("[%lu] [GFX] !! BW buffer chunk %zu already stored - this is likely a bug, freeing chunk\n",
|
|
millis(), i);
|
|
free(bwBufferChunks[i]);
|
|
bwBufferChunks[i] = nullptr;
|
|
}
|
|
|
|
const size_t offset = i * BW_BUFFER_CHUNK_SIZE;
|
|
bwBufferChunks[i] = static_cast<uint8_t*>(malloc(BW_BUFFER_CHUNK_SIZE));
|
|
|
|
if (!bwBufferChunks[i]) {
|
|
Serial.printf("[%lu] [GFX] !! Failed to allocate BW buffer chunk %zu (%zu bytes)\n", millis(), i,
|
|
BW_BUFFER_CHUNK_SIZE);
|
|
// Free previously allocated chunks
|
|
freeBwBufferChunks();
|
|
return false;
|
|
}
|
|
|
|
memcpy(bwBufferChunks[i], frameBuffer + offset, BW_BUFFER_CHUNK_SIZE);
|
|
}
|
|
|
|
Serial.printf("[%lu] [GFX] Stored BW buffer in %zu chunks (%zu bytes each)\n", millis(), BW_BUFFER_NUM_CHUNKS,
|
|
BW_BUFFER_CHUNK_SIZE);
|
|
return true;
|
|
}
|
|
|
|
/**
|
|
* This can only be called if `storeBwBuffer` was called prior to the grayscale render.
|
|
* It should be called to restore the BW buffer state after grayscale rendering is complete.
|
|
* Uses chunked restoration to match chunked storage.
|
|
*/
|
|
void GfxRenderer::restoreBwBuffer() {
|
|
// Check if all chunks are allocated
|
|
bool missingChunks = false;
|
|
for (const auto& bwBufferChunk : bwBufferChunks) {
|
|
if (!bwBufferChunk) {
|
|
missingChunks = true;
|
|
break;
|
|
}
|
|
}
|
|
|
|
if (missingChunks) {
|
|
freeBwBufferChunks();
|
|
// CRITICAL: Even if restore fails, we must clean up the grayscale state
|
|
// to prevent grayscaleRevert() from being called with corrupted RAM state
|
|
// Use the current framebuffer content (which may not be ideal but prevents worse issues)
|
|
uint8_t* frameBuffer = einkDisplay.getFrameBuffer();
|
|
if (frameBuffer) {
|
|
einkDisplay.cleanupGrayscaleBuffers(frameBuffer);
|
|
}
|
|
return;
|
|
}
|
|
|
|
uint8_t* frameBuffer = einkDisplay.getFrameBuffer();
|
|
if (!frameBuffer) {
|
|
Serial.printf("[%lu] [GFX] !! No framebuffer in restoreBwBuffer\n", millis());
|
|
freeBwBufferChunks();
|
|
return;
|
|
}
|
|
|
|
for (size_t i = 0; i < BW_BUFFER_NUM_CHUNKS; i++) {
|
|
// Check if chunk is missing
|
|
if (!bwBufferChunks[i]) {
|
|
Serial.printf("[%lu] [GFX] !! BW buffer chunks not stored - this is likely a bug\n", millis());
|
|
freeBwBufferChunks();
|
|
// CRITICAL: Clean up grayscale state even on mid-restore failure
|
|
einkDisplay.cleanupGrayscaleBuffers(frameBuffer);
|
|
return;
|
|
}
|
|
|
|
const size_t offset = i * BW_BUFFER_CHUNK_SIZE;
|
|
memcpy(frameBuffer + offset, bwBufferChunks[i], BW_BUFFER_CHUNK_SIZE);
|
|
}
|
|
|
|
einkDisplay.cleanupGrayscaleBuffers(frameBuffer);
|
|
|
|
freeBwBufferChunks();
|
|
Serial.printf("[%lu] [GFX] Restored and freed BW buffer chunks\n", millis());
|
|
}
|
|
|
|
/**
|
|
* Cleanup grayscale buffers using the current frame buffer.
|
|
* Use this when BW buffer was re-rendered instead of stored/restored.
|
|
*/
|
|
void GfxRenderer::cleanupGrayscaleWithFrameBuffer() const {
|
|
uint8_t* frameBuffer = einkDisplay.getFrameBuffer();
|
|
if (frameBuffer) {
|
|
einkDisplay.cleanupGrayscaleBuffers(frameBuffer);
|
|
}
|
|
}
|
|
|
|
void GfxRenderer::renderChar(const EpdFontFamily& fontFamily, const uint32_t cp, int* x, const int* y,
|
|
const bool pixelState, const EpdFontFamily::Style style) const {
|
|
const EpdGlyph* glyph = fontFamily.getGlyph(cp, style);
|
|
if (!glyph) {
|
|
glyph = fontFamily.getGlyph(REPLACEMENT_GLYPH, style);
|
|
}
|
|
|
|
// no glyph?
|
|
if (!glyph) {
|
|
Serial.printf("[%lu] [GFX] No glyph for codepoint %d\n", millis(), cp);
|
|
return;
|
|
}
|
|
|
|
const int is2Bit = fontFamily.getData(style)->is2Bit;
|
|
const uint32_t offset = glyph->dataOffset;
|
|
const uint8_t width = glyph->width;
|
|
const uint8_t height = glyph->height;
|
|
const int left = glyph->left;
|
|
|
|
const uint8_t* bitmap = nullptr;
|
|
bitmap = &fontFamily.getData(style)->bitmap[offset];
|
|
|
|
if (bitmap != nullptr) {
|
|
for (int glyphY = 0; glyphY < height; glyphY++) {
|
|
const int screenY = *y - glyph->top + glyphY;
|
|
for (int glyphX = 0; glyphX < width; glyphX++) {
|
|
const int pixelPosition = glyphY * width + glyphX;
|
|
const int screenX = *x + left + glyphX;
|
|
|
|
if (is2Bit) {
|
|
const uint8_t byte = bitmap[pixelPosition / 4];
|
|
const uint8_t bit_index = (3 - pixelPosition % 4) * 2;
|
|
// the direct bit from the font is 0 -> white, 1 -> light gray, 2 -> dark gray, 3 -> black
|
|
// we swap this to better match the way images and screen think about colors:
|
|
// 0 -> black, 1 -> dark grey, 2 -> light grey, 3 -> white
|
|
const uint8_t bmpVal = 3 - (byte >> bit_index) & 0x3;
|
|
|
|
if (renderMode == BW && bmpVal < 3) {
|
|
// Black (also paints over the grays in BW mode)
|
|
drawPixel(screenX, screenY, pixelState);
|
|
} else if (renderMode == GRAYSCALE_MSB && (bmpVal == 1 || bmpVal == 2)) {
|
|
// Light gray (also mark the MSB if it's going to be a dark gray too)
|
|
// We have to flag pixels in reverse for the gray buffers, as 0 leave alone, 1 update
|
|
drawPixel(screenX, screenY, false);
|
|
} else if (renderMode == GRAYSCALE_LSB && bmpVal == 1) {
|
|
// Dark gray
|
|
drawPixel(screenX, screenY, false);
|
|
}
|
|
} else {
|
|
const uint8_t byte = bitmap[pixelPosition / 8];
|
|
const uint8_t bit_index = 7 - (pixelPosition % 8);
|
|
|
|
if ((byte >> bit_index) & 1) {
|
|
drawPixel(screenX, screenY, pixelState);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
*x += glyph->advanceX;
|
|
}
|
|
|
|
// Helper to map physical bezel edge to logical edge based on orientation
|
|
// bezelEdge: 0=physical bottom, 1=physical top, 2=physical left, 3=physical right (in portrait)
|
|
// Returns: 0=logical bottom, 1=logical top, 2=logical left, 3=logical right
|
|
int mapPhysicalToLogicalEdge(int bezelEdge, GfxRenderer::Orientation orientation) {
|
|
switch (orientation) {
|
|
case GfxRenderer::Portrait:
|
|
return bezelEdge;
|
|
case GfxRenderer::LandscapeClockwise:
|
|
switch (bezelEdge) {
|
|
case 0:
|
|
return 2; // Physical bottom -> logical left
|
|
case 1:
|
|
return 3; // Physical top -> logical right
|
|
case 2:
|
|
return 1; // Physical left -> logical top
|
|
case 3:
|
|
return 0; // Physical right -> logical bottom
|
|
}
|
|
break;
|
|
case GfxRenderer::PortraitInverted:
|
|
switch (bezelEdge) {
|
|
case 0:
|
|
return 1; // Physical bottom -> logical top
|
|
case 1:
|
|
return 0; // Physical top -> logical bottom
|
|
case 2:
|
|
return 3; // Physical left -> logical right
|
|
case 3:
|
|
return 2; // Physical right -> logical left
|
|
}
|
|
break;
|
|
case GfxRenderer::LandscapeCounterClockwise:
|
|
switch (bezelEdge) {
|
|
case 0:
|
|
return 3; // Physical bottom -> logical right
|
|
case 1:
|
|
return 2; // Physical top -> logical left
|
|
case 2:
|
|
return 0; // Physical left -> logical bottom
|
|
case 3:
|
|
return 1; // Physical right -> logical top
|
|
}
|
|
break;
|
|
}
|
|
return bezelEdge;
|
|
}
|
|
|
|
int GfxRenderer::getViewableMarginTop() const {
|
|
int logicalEdge = mapPhysicalToLogicalEdge(bezelEdge, orientation);
|
|
return BASE_VIEWABLE_MARGIN_TOP + (logicalEdge == 1 ? bezelCompensation : 0);
|
|
}
|
|
|
|
int GfxRenderer::getViewableMarginRight() const {
|
|
int logicalEdge = mapPhysicalToLogicalEdge(bezelEdge, orientation);
|
|
return BASE_VIEWABLE_MARGIN_RIGHT + (logicalEdge == 3 ? bezelCompensation : 0);
|
|
}
|
|
|
|
int GfxRenderer::getViewableMarginBottom() const {
|
|
int logicalEdge = mapPhysicalToLogicalEdge(bezelEdge, orientation);
|
|
return BASE_VIEWABLE_MARGIN_BOTTOM + (logicalEdge == 0 ? bezelCompensation : 0);
|
|
}
|
|
|
|
int GfxRenderer::getViewableMarginLeft() const {
|
|
int logicalEdge = mapPhysicalToLogicalEdge(bezelEdge, orientation);
|
|
return BASE_VIEWABLE_MARGIN_LEFT + (logicalEdge == 2 ? bezelCompensation : 0);
|
|
}
|
|
|
|
int GfxRenderer::getBezelOffsetTop() const {
|
|
int logicalEdge = mapPhysicalToLogicalEdge(bezelEdge, orientation);
|
|
return (logicalEdge == 1) ? bezelCompensation : 0;
|
|
}
|
|
|
|
int GfxRenderer::getBezelOffsetRight() const {
|
|
int logicalEdge = mapPhysicalToLogicalEdge(bezelEdge, orientation);
|
|
return (logicalEdge == 3) ? bezelCompensation : 0;
|
|
}
|
|
|
|
int GfxRenderer::getBezelOffsetBottom() const {
|
|
int logicalEdge = mapPhysicalToLogicalEdge(bezelEdge, orientation);
|
|
return (logicalEdge == 0) ? bezelCompensation : 0;
|
|
}
|
|
|
|
int GfxRenderer::getBezelOffsetLeft() const {
|
|
int logicalEdge = mapPhysicalToLogicalEdge(bezelEdge, orientation);
|
|
return (logicalEdge == 2) ? bezelCompensation : 0;
|
|
}
|
|
|
|
void GfxRenderer::getOrientedViewableTRBL(int* outTop, int* outRight, int* outBottom, int* outLeft) const {
|
|
// Get base margins rotated for current orientation, with bezel compensation applied
|
|
switch (orientation) {
|
|
case Portrait:
|
|
*outTop = getViewableMarginTop();
|
|
*outRight = getViewableMarginRight();
|
|
*outBottom = getViewableMarginBottom();
|
|
*outLeft = getViewableMarginLeft();
|
|
break;
|
|
case LandscapeClockwise:
|
|
*outTop =
|
|
BASE_VIEWABLE_MARGIN_LEFT + (mapPhysicalToLogicalEdge(bezelEdge, orientation) == 1 ? bezelCompensation : 0);
|
|
*outRight =
|
|
BASE_VIEWABLE_MARGIN_TOP + (mapPhysicalToLogicalEdge(bezelEdge, orientation) == 3 ? bezelCompensation : 0);
|
|
*outBottom =
|
|
BASE_VIEWABLE_MARGIN_RIGHT + (mapPhysicalToLogicalEdge(bezelEdge, orientation) == 0 ? bezelCompensation : 0);
|
|
*outLeft =
|
|
BASE_VIEWABLE_MARGIN_BOTTOM + (mapPhysicalToLogicalEdge(bezelEdge, orientation) == 2 ? bezelCompensation : 0);
|
|
break;
|
|
case PortraitInverted:
|
|
*outTop =
|
|
BASE_VIEWABLE_MARGIN_BOTTOM + (mapPhysicalToLogicalEdge(bezelEdge, orientation) == 1 ? bezelCompensation : 0);
|
|
*outRight =
|
|
BASE_VIEWABLE_MARGIN_LEFT + (mapPhysicalToLogicalEdge(bezelEdge, orientation) == 3 ? bezelCompensation : 0);
|
|
*outBottom =
|
|
BASE_VIEWABLE_MARGIN_TOP + (mapPhysicalToLogicalEdge(bezelEdge, orientation) == 0 ? bezelCompensation : 0);
|
|
*outLeft =
|
|
BASE_VIEWABLE_MARGIN_RIGHT + (mapPhysicalToLogicalEdge(bezelEdge, orientation) == 2 ? bezelCompensation : 0);
|
|
break;
|
|
case LandscapeCounterClockwise:
|
|
*outTop =
|
|
BASE_VIEWABLE_MARGIN_RIGHT + (mapPhysicalToLogicalEdge(bezelEdge, orientation) == 1 ? bezelCompensation : 0);
|
|
*outRight =
|
|
BASE_VIEWABLE_MARGIN_BOTTOM + (mapPhysicalToLogicalEdge(bezelEdge, orientation) == 3 ? bezelCompensation : 0);
|
|
*outBottom =
|
|
BASE_VIEWABLE_MARGIN_LEFT + (mapPhysicalToLogicalEdge(bezelEdge, orientation) == 0 ? bezelCompensation : 0);
|
|
*outLeft =
|
|
BASE_VIEWABLE_MARGIN_TOP + (mapPhysicalToLogicalEdge(bezelEdge, orientation) == 2 ? bezelCompensation : 0);
|
|
break;
|
|
}
|
|
}
|