- Update open-x4-sdk submodule to 9f76376 (BatteryMonitor ESP-IDF 5.x compat) - Add RTC_NOINIT bounds check for logHead in Logging.cpp - Add drawTextRotated90CCW to GfxRenderer for dictionary UI - Add getWordXpos() accessor to TextBlock for dictionary word selection - Fix bare include paths (ActivityResult.h, RenderLock.h) across 10 files - Fix rvalue ref binding in setResult() lambdas (std::move pattern) - Fix std::max type mismatch (uint8_t vs int) in EpubReaderActivity - Fix FsFile forward declaration conflict in Dictionary.h - Restore StringUtils::checkFileExtension() and sortFileList() - Restore RecentBooksStore::removeBook() Made-with: Cursor
1439 lines
51 KiB
C++
1439 lines
51 KiB
C++
#include "GfxRenderer.h"
|
|
|
|
#include <Logging.h>
|
|
#include <Utf8.h>
|
|
|
|
#include <cstring>
|
|
|
|
const uint8_t* GfxRenderer::getGlyphBitmap(const EpdFontData* fontData, const EpdGlyph* glyph) const {
|
|
if (fontData->groups != nullptr) {
|
|
if (!fontDecompressor) {
|
|
LOG_ERR("GFX", "Compressed font but no FontDecompressor set");
|
|
return nullptr;
|
|
}
|
|
uint16_t glyphIndex = static_cast<uint16_t>(glyph - fontData->glyph);
|
|
return fontDecompressor->getBitmap(fontData, glyph, glyphIndex);
|
|
}
|
|
return &fontData->bitmap[glyph->dataOffset];
|
|
}
|
|
|
|
void GfxRenderer::begin() {
|
|
frameBuffer = display.getFrameBuffer();
|
|
if (!frameBuffer) {
|
|
LOG_ERR("GFX", "!! No framebuffer");
|
|
assert(false);
|
|
}
|
|
}
|
|
|
|
void GfxRenderer::insertFont(const int fontId, EpdFontFamily font) { fontMap.insert({fontId, font}); }
|
|
|
|
// Translate logical (x,y) coordinates to physical panel coordinates based on current orientation
|
|
// This should always be inlined for better performance
|
|
static inline void rotateCoordinates(const GfxRenderer::Orientation orientation, const int x, const int y, int* phyX,
|
|
int* phyY) {
|
|
switch (orientation) {
|
|
case GfxRenderer::Portrait: {
|
|
// Logical portrait (480x800) → panel (800x480)
|
|
// Rotation: 90 degrees clockwise
|
|
*phyX = y;
|
|
*phyY = HalDisplay::DISPLAY_HEIGHT - 1 - x;
|
|
break;
|
|
}
|
|
case GfxRenderer::LandscapeClockwise: {
|
|
// Logical landscape (800x480) rotated 180 degrees (swap top/bottom and left/right)
|
|
*phyX = HalDisplay::DISPLAY_WIDTH - 1 - x;
|
|
*phyY = HalDisplay::DISPLAY_HEIGHT - 1 - y;
|
|
break;
|
|
}
|
|
case GfxRenderer::PortraitInverted: {
|
|
// Logical portrait (480x800) → panel (800x480)
|
|
// Rotation: 90 degrees counter-clockwise
|
|
*phyX = HalDisplay::DISPLAY_WIDTH - 1 - y;
|
|
*phyY = x;
|
|
break;
|
|
}
|
|
case GfxRenderer::LandscapeCounterClockwise: {
|
|
// Logical landscape (800x480) aligned with panel orientation
|
|
*phyX = x;
|
|
*phyY = y;
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
|
|
enum class TextRotation { None, Rotated90CW, Rotated90CCW };
|
|
|
|
// Shared glyph rendering logic for normal and rotated text.
|
|
// Coordinate mapping and cursor advance direction are selected at compile time via the template parameter.
|
|
template <TextRotation rotation>
|
|
static void renderCharImpl(const GfxRenderer& renderer, GfxRenderer::RenderMode renderMode,
|
|
const EpdFontFamily& fontFamily, const uint32_t cp, int cursorX, int cursorY,
|
|
const bool pixelState, const EpdFontFamily::Style style) {
|
|
const EpdGlyph* glyph = fontFamily.getGlyph(cp, style);
|
|
if (!glyph) {
|
|
LOG_ERR("GFX", "No glyph for codepoint %d", cp);
|
|
return;
|
|
}
|
|
|
|
const EpdFontData* fontData = fontFamily.getData(style);
|
|
const bool is2Bit = fontData->is2Bit;
|
|
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 = renderer.getGlyphBitmap(fontData, glyph);
|
|
|
|
if (bitmap != nullptr) {
|
|
// For Normal: outer loop advances screenY, inner loop advances screenX
|
|
// For Rotated: outer loop advances screenX, inner loop advances screenY (in reverse)
|
|
int outerBase, innerBase;
|
|
if constexpr (rotation == TextRotation::Rotated90CW) {
|
|
outerBase = cursorX + fontData->ascender - top; // screenX = outerBase + glyphY
|
|
innerBase = cursorY - left; // screenY = innerBase - glyphX
|
|
} else if constexpr (rotation == TextRotation::Rotated90CCW) {
|
|
outerBase = cursorX + fontData->advanceY - 1 - fontData->ascender + top; // screenX = outerBase - glyphY
|
|
innerBase = cursorY + left; // screenY = innerBase + glyphX
|
|
} else {
|
|
outerBase = cursorY - top; // screenY = outerBase + glyphY
|
|
innerBase = cursorX + left; // screenX = innerBase + glyphX
|
|
}
|
|
|
|
if (is2Bit) {
|
|
int pixelPosition = 0;
|
|
for (int glyphY = 0; glyphY < height; glyphY++) {
|
|
const int outerCoord =
|
|
(rotation == TextRotation::Rotated90CCW) ? outerBase - glyphY : outerBase + glyphY;
|
|
for (int glyphX = 0; glyphX < width; glyphX++, pixelPosition++) {
|
|
int screenX, screenY;
|
|
if constexpr (rotation == TextRotation::Rotated90CW) {
|
|
screenX = outerCoord;
|
|
screenY = innerBase - glyphX;
|
|
} else if constexpr (rotation == TextRotation::Rotated90CCW) {
|
|
screenX = outerCoord;
|
|
screenY = innerBase + glyphX;
|
|
} else {
|
|
screenX = innerBase + glyphX;
|
|
screenY = outerCoord;
|
|
}
|
|
|
|
const uint8_t byte = bitmap[pixelPosition >> 2];
|
|
const uint8_t bit_index = (3 - (pixelPosition & 3)) * 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 == GfxRenderer::BW && bmpVal < 3) {
|
|
// Black (also paints over the grays in BW mode)
|
|
renderer.drawPixel(screenX, screenY, pixelState);
|
|
} else if (renderMode == GfxRenderer::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
|
|
renderer.drawPixel(screenX, screenY, false);
|
|
} else if (renderMode == GfxRenderer::GRAYSCALE_LSB && bmpVal == 1) {
|
|
// Dark gray
|
|
renderer.drawPixel(screenX, screenY, false);
|
|
}
|
|
}
|
|
}
|
|
} else {
|
|
int pixelPosition = 0;
|
|
for (int glyphY = 0; glyphY < height; glyphY++) {
|
|
const int outerCoord =
|
|
(rotation == TextRotation::Rotated90CCW) ? outerBase - glyphY : outerBase + glyphY;
|
|
for (int glyphX = 0; glyphX < width; glyphX++, pixelPosition++) {
|
|
int screenX, screenY;
|
|
if constexpr (rotation == TextRotation::Rotated90CW) {
|
|
screenX = outerCoord;
|
|
screenY = innerBase - glyphX;
|
|
} else if constexpr (rotation == TextRotation::Rotated90CCW) {
|
|
screenX = outerCoord;
|
|
screenY = innerBase + glyphX;
|
|
} else {
|
|
screenX = innerBase + glyphX;
|
|
screenY = outerCoord;
|
|
}
|
|
|
|
const uint8_t byte = bitmap[pixelPosition >> 3];
|
|
const uint8_t bit_index = 7 - (pixelPosition & 7);
|
|
|
|
if ((byte >> bit_index) & 1) {
|
|
renderer.drawPixel(screenX, screenY, pixelState);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
// IMPORTANT: This function is in critical rendering path and is called for every pixel. Please keep it as simple and
|
|
// efficient as possible.
|
|
void GfxRenderer::drawPixel(const int x, const int y, const bool state) const {
|
|
int phyX = 0;
|
|
int phyY = 0;
|
|
|
|
// Note: this call should be inlined for better performance
|
|
rotateCoordinates(orientation, x, y, &phyX, &phyY);
|
|
|
|
// Bounds checking against physical panel dimensions
|
|
if (phyX < 0 || phyX >= HalDisplay::DISPLAY_WIDTH || phyY < 0 || phyY >= HalDisplay::DISPLAY_HEIGHT) {
|
|
LOG_ERR("GFX", "!! Outside range (%d, %d) -> (%d, %d)", x, y, phyX, phyY);
|
|
return;
|
|
}
|
|
|
|
// Calculate byte position and bit position
|
|
const uint16_t byteIndex = phyY * HalDisplay::DISPLAY_WIDTH_BYTES + (phyX / 8);
|
|
const uint8_t bitPosition = 7 - (phyX % 8); // MSB first
|
|
|
|
if (state) {
|
|
frameBuffer[byteIndex] &= ~(1 << bitPosition); // Clear bit
|
|
} else {
|
|
frameBuffer[byteIndex] |= 1 << bitPosition; // Set bit
|
|
}
|
|
}
|
|
|
|
void GfxRenderer::drawPixelGray(const int x, const int y, const uint8_t val2bit) const {
|
|
if (renderMode == BW && val2bit < 3) {
|
|
drawPixel(x, y);
|
|
} else if (renderMode == GRAYSCALE_MSB && (val2bit == 1 || val2bit == 2)) {
|
|
drawPixel(x, y, false);
|
|
} else if (renderMode == GRAYSCALE_LSB && val2bit == 1) {
|
|
drawPixel(x, y, false);
|
|
}
|
|
}
|
|
|
|
int GfxRenderer::getTextWidth(const int fontId, const char* text, const EpdFontFamily::Style style) const {
|
|
const auto fontIt = fontMap.find(fontId);
|
|
if (fontIt == fontMap.end()) {
|
|
LOG_ERR("GFX", "Font %d not found", fontId);
|
|
return 0;
|
|
}
|
|
|
|
int w = 0, h = 0;
|
|
fontIt->second.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);
|
|
int32_t xPosFP = fp4::fromPixel(x); // 12.4 fixed-point accumulator
|
|
int lastBaseX = x;
|
|
int lastBaseAdvanceFP = 0; // 12.4 fixed-point
|
|
int lastBaseTop = 0;
|
|
|
|
// cannot draw a NULL / empty string
|
|
if (text == nullptr || *text == '\0') {
|
|
return;
|
|
}
|
|
|
|
const auto fontIt = fontMap.find(fontId);
|
|
if (fontIt == fontMap.end()) {
|
|
LOG_ERR("GFX", "Font %d not found", fontId);
|
|
return;
|
|
}
|
|
const auto& font = fontIt->second;
|
|
constexpr int MIN_COMBINING_GAP_PX = 1;
|
|
|
|
uint32_t cp;
|
|
uint32_t prevCp = 0;
|
|
while ((cp = utf8NextCodepoint(reinterpret_cast<const uint8_t**>(&text)))) {
|
|
if (utf8IsCombiningMark(cp)) {
|
|
const EpdGlyph* combiningGlyph = font.getGlyph(cp, style);
|
|
int raiseBy = 0;
|
|
if (combiningGlyph) {
|
|
const int currentGap = combiningGlyph->top - combiningGlyph->height - lastBaseTop;
|
|
if (currentGap < MIN_COMBINING_GAP_PX) {
|
|
raiseBy = MIN_COMBINING_GAP_PX - currentGap;
|
|
}
|
|
}
|
|
|
|
const int combiningX = lastBaseX + fp4::toPixel(lastBaseAdvanceFP / 2);
|
|
const int combiningY = yPos - raiseBy;
|
|
renderCharImpl<TextRotation::None>(*this, renderMode, font, cp, combiningX, combiningY, black, style);
|
|
continue;
|
|
}
|
|
|
|
cp = font.applyLigatures(cp, text, style);
|
|
const int kernFP = (prevCp != 0) ? font.getKerning(prevCp, cp, style) : 0; // 4.4 fixed-point kern
|
|
xPosFP += kernFP;
|
|
|
|
lastBaseX = fp4::toPixel(xPosFP); // snap 12.4 fixed-point to nearest pixel
|
|
const EpdGlyph* glyph = font.getGlyph(cp, style);
|
|
|
|
lastBaseAdvanceFP = glyph ? glyph->advanceX : 0;
|
|
lastBaseTop = glyph ? glyph->top : 0;
|
|
|
|
renderCharImpl<TextRotation::None>(*this, renderMode, font, cp, lastBaseX, yPos, black, style);
|
|
if (glyph) {
|
|
xPosFP += glyph->advanceX; // 12.4 fixed-point advance
|
|
}
|
|
prevCp = cp;
|
|
}
|
|
}
|
|
|
|
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);
|
|
}
|
|
// In Portrait/PortraitInverted a logical vertical line maps to a physical horizontal span.
|
|
switch (orientation) {
|
|
case Portrait:
|
|
fillPhysicalHSpan(HalDisplay::DISPLAY_HEIGHT - 1 - x1, y1, y2, state);
|
|
return;
|
|
case PortraitInverted:
|
|
fillPhysicalHSpan(x1, HalDisplay::DISPLAY_WIDTH - 1 - y2, HalDisplay::DISPLAY_WIDTH - 1 - y1, state);
|
|
return;
|
|
default:
|
|
for (int y = y1; y <= y2; y++) drawPixel(x1, y, state);
|
|
return;
|
|
}
|
|
} else if (y1 == y2) {
|
|
if (x2 < x1) {
|
|
std::swap(x1, x2);
|
|
}
|
|
// In Landscape a logical horizontal line maps to a physical horizontal span.
|
|
switch (orientation) {
|
|
case LandscapeCounterClockwise:
|
|
fillPhysicalHSpan(y1, x1, x2, state);
|
|
return;
|
|
case LandscapeClockwise:
|
|
fillPhysicalHSpan(HalDisplay::DISPLAY_HEIGHT - 1 - y1, HalDisplay::DISPLAY_WIDTH - 1 - x2,
|
|
HalDisplay::DISPLAY_WIDTH - 1 - x1, state);
|
|
return;
|
|
default:
|
|
for (int x = x1; x <= x2; x++) drawPixel(x, y1, state);
|
|
return;
|
|
}
|
|
} else {
|
|
// Bresenham's line algorithm — integer arithmetic only
|
|
int dx = x2 - x1;
|
|
int dy = y2 - y1;
|
|
int sx = (dx > 0) ? 1 : -1;
|
|
int sy = (dy > 0) ? 1 : -1;
|
|
dx = sx * dx; // abs
|
|
dy = sy * dy; // abs
|
|
|
|
int err = dx - dy;
|
|
while (true) {
|
|
drawPixel(x1, y1, state);
|
|
if (x1 == x2 && y1 == y2) break;
|
|
int e2 = 2 * err;
|
|
if (e2 > -dy) {
|
|
err -= dy;
|
|
x1 += sx;
|
|
}
|
|
if (e2 < dx) {
|
|
err += dx;
|
|
y1 += sy;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
void GfxRenderer::drawLine(int x1, int y1, int x2, int y2, const int lineWidth, const bool state) const {
|
|
for (int i = 0; i < lineWidth; i++) {
|
|
drawLine(x1, y1 + i, x2, y2 + i, state);
|
|
}
|
|
}
|
|
|
|
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);
|
|
}
|
|
|
|
// Border is inside the rectangle
|
|
void GfxRenderer::drawRect(const int x, const int y, const int width, const int height, const int lineWidth,
|
|
const bool state) const {
|
|
for (int i = 0; i < lineWidth; i++) {
|
|
drawLine(x + i, y + i, x + width - i, y + i, state);
|
|
drawLine(x + width - i, y + i, x + width - i, y + height - i, state);
|
|
drawLine(x + width - i, y + height - i, x + i, y + height - i, state);
|
|
drawLine(x + i, y + height - i, x + i, y + i, state);
|
|
}
|
|
}
|
|
|
|
void GfxRenderer::drawArc(const int maxRadius, const int cx, const int cy, const int xDir, const int yDir,
|
|
const int lineWidth, const bool state) const {
|
|
const int stroke = std::min(lineWidth, maxRadius);
|
|
const int innerRadius = std::max(maxRadius - stroke, 0);
|
|
const int outerRadiusSq = maxRadius * maxRadius;
|
|
const int innerRadiusSq = innerRadius * innerRadius;
|
|
for (int dy = 0; dy <= maxRadius; ++dy) {
|
|
for (int dx = 0; dx <= maxRadius; ++dx) {
|
|
const int distSq = dx * dx + dy * dy;
|
|
if (distSq > outerRadiusSq || distSq < innerRadiusSq) {
|
|
continue;
|
|
}
|
|
const int px = cx + xDir * dx;
|
|
const int py = cy + yDir * dy;
|
|
drawPixel(px, py, state);
|
|
}
|
|
}
|
|
};
|
|
|
|
// Border is inside the rectangle, rounded corners
|
|
void GfxRenderer::drawRoundedRect(const int x, const int y, const int width, const int height, const int lineWidth,
|
|
const int cornerRadius, bool state) const {
|
|
drawRoundedRect(x, y, width, height, lineWidth, cornerRadius, true, true, true, true, state);
|
|
}
|
|
|
|
// Border is inside the rectangle, rounded corners
|
|
void GfxRenderer::drawRoundedRect(const int x, const int y, const int width, const int height, const int lineWidth,
|
|
const int cornerRadius, bool roundTopLeft, bool roundTopRight, bool roundBottomLeft,
|
|
bool roundBottomRight, bool state) const {
|
|
if (lineWidth <= 0 || width <= 0 || height <= 0) {
|
|
return;
|
|
}
|
|
|
|
const int maxRadius = std::min({cornerRadius, width / 2, height / 2});
|
|
if (maxRadius <= 0) {
|
|
drawRect(x, y, width, height, lineWidth, state);
|
|
return;
|
|
}
|
|
|
|
const int stroke = std::min(lineWidth, maxRadius);
|
|
const int right = x + width - 1;
|
|
const int bottom = y + height - 1;
|
|
|
|
const int horizontalWidth = width - 2 * maxRadius;
|
|
if (horizontalWidth > 0) {
|
|
if (roundTopLeft || roundTopRight) {
|
|
fillRect(x + maxRadius, y, horizontalWidth, stroke, state);
|
|
}
|
|
if (roundBottomLeft || roundBottomRight) {
|
|
fillRect(x + maxRadius, bottom - stroke + 1, horizontalWidth, stroke, state);
|
|
}
|
|
}
|
|
|
|
const int verticalHeight = height - 2 * maxRadius;
|
|
if (verticalHeight > 0) {
|
|
if (roundTopLeft || roundBottomLeft) {
|
|
fillRect(x, y + maxRadius, stroke, verticalHeight, state);
|
|
}
|
|
if (roundTopRight || roundBottomRight) {
|
|
fillRect(right - stroke + 1, y + maxRadius, stroke, verticalHeight, state);
|
|
}
|
|
}
|
|
|
|
if (roundTopLeft) {
|
|
drawArc(maxRadius, x + maxRadius, y + maxRadius, -1, -1, lineWidth, state);
|
|
}
|
|
if (roundTopRight) {
|
|
drawArc(maxRadius, right - maxRadius, y + maxRadius, 1, -1, lineWidth, state);
|
|
}
|
|
if (roundBottomRight) {
|
|
drawArc(maxRadius, right - maxRadius, bottom - maxRadius, 1, 1, lineWidth, state);
|
|
}
|
|
if (roundBottomLeft) {
|
|
drawArc(maxRadius, x + maxRadius, bottom - maxRadius, -1, 1, lineWidth, state);
|
|
}
|
|
}
|
|
|
|
// Write a patterned horizontal span directly into the physical framebuffer with byte-level operations.
|
|
// Handles partial left/right bytes and fills the aligned middle with memset.
|
|
// Bit layout: MSB-first (bit 7 = phyX=0, bit 0 = phyX=7); 0 bits = dark pixel, 1 bits = white pixel.
|
|
void GfxRenderer::fillPhysicalHSpanByte(const int phyY, const int phyX_start, const int phyX_end,
|
|
const uint8_t patternByte) const {
|
|
const int cX0 = std::max(phyX_start, 0);
|
|
const int cX1 = std::min(phyX_end, static_cast<int>(HalDisplay::DISPLAY_WIDTH) - 1);
|
|
if (cX0 > cX1 || phyY < 0 || phyY >= static_cast<int>(HalDisplay::DISPLAY_HEIGHT)) return;
|
|
|
|
uint8_t* const row = frameBuffer + phyY * HalDisplay::DISPLAY_WIDTH_BYTES;
|
|
const int startByte = cX0 >> 3;
|
|
const int endByte = cX1 >> 3;
|
|
const int leftBits = cX0 & 7;
|
|
const int rightBits = cX1 & 7;
|
|
|
|
if (startByte == endByte) {
|
|
const uint8_t fillMask = (0xFF >> leftBits) & ~(0xFF >> (rightBits + 1));
|
|
row[startByte] = (row[startByte] & ~fillMask) | (patternByte & fillMask);
|
|
return;
|
|
}
|
|
|
|
// Left partial byte
|
|
if (leftBits != 0) {
|
|
const uint8_t fillMask = 0xFF >> leftBits;
|
|
row[startByte] = (row[startByte] & ~fillMask) | (patternByte & fillMask);
|
|
}
|
|
|
|
// Full bytes in the middle
|
|
const int fullStart = (leftBits == 0) ? startByte : startByte + 1;
|
|
const int fullEnd = (rightBits == 7) ? endByte : endByte - 1;
|
|
if (fullStart <= fullEnd) {
|
|
memset(row + fullStart, patternByte, static_cast<size_t>(fullEnd - fullStart + 1));
|
|
}
|
|
|
|
// Right partial byte
|
|
if (rightBits != 7) {
|
|
const uint8_t fillMask = ~(0xFF >> (rightBits + 1));
|
|
row[endByte] = (row[endByte] & ~fillMask) | (patternByte & fillMask);
|
|
}
|
|
}
|
|
|
|
// Thin wrapper: state=true → 0x00 (all dark), false → 0xFF (all white).
|
|
void GfxRenderer::fillPhysicalHSpan(const int phyY, const int phyX_start, const int phyX_end, const bool state) const {
|
|
fillPhysicalHSpanByte(phyY, phyX_start, phyX_end, state ? 0x00 : 0xFF);
|
|
}
|
|
|
|
void GfxRenderer::fillRect(const int x, const int y, const int width, const int height, const bool state) const {
|
|
if (width <= 0 || height <= 0) return;
|
|
|
|
// For each orientation, one logical dimension maps to a constant physical row, allowing the
|
|
// perpendicular dimension to be written as a byte-level span — eliminating per-pixel overhead.
|
|
switch (orientation) {
|
|
case Portrait:
|
|
for (int lx = x; lx < x + width; lx++) {
|
|
fillPhysicalHSpan(HalDisplay::DISPLAY_HEIGHT - 1 - lx, y, y + height - 1, state);
|
|
}
|
|
return;
|
|
case PortraitInverted:
|
|
for (int lx = x; lx < x + width; lx++) {
|
|
fillPhysicalHSpan(lx, HalDisplay::DISPLAY_WIDTH - 1 - (y + height - 1), HalDisplay::DISPLAY_WIDTH - 1 - y,
|
|
state);
|
|
}
|
|
return;
|
|
case LandscapeCounterClockwise:
|
|
for (int ly = y; ly < y + height; ly++) {
|
|
fillPhysicalHSpan(ly, x, x + width - 1, state);
|
|
}
|
|
return;
|
|
case LandscapeClockwise:
|
|
for (int ly = y; ly < y + height; ly++) {
|
|
fillPhysicalHSpan(HalDisplay::DISPLAY_HEIGHT - 1 - ly, HalDisplay::DISPLAY_WIDTH - 1 - (x + width - 1),
|
|
HalDisplay::DISPLAY_WIDTH - 1 - x, state);
|
|
}
|
|
return;
|
|
}
|
|
}
|
|
|
|
// NOTE: Those are in critical path, and need to be templated to avoid runtime checks for every pixel.
|
|
// Any branching must be done outside the loops to avoid performance degradation.
|
|
template <>
|
|
void GfxRenderer::drawPixelDither<Color::Clear>(const int x, const int y) const {
|
|
// Do nothing
|
|
}
|
|
|
|
template <>
|
|
void GfxRenderer::drawPixelDither<Color::Black>(const int x, const int y) const {
|
|
drawPixel(x, y, true);
|
|
}
|
|
|
|
template <>
|
|
void GfxRenderer::drawPixelDither<Color::White>(const int x, const int y) const {
|
|
drawPixel(x, y, false);
|
|
}
|
|
|
|
template <>
|
|
void GfxRenderer::drawPixelDither<Color::LightGray>(const int x, const int y) const {
|
|
drawPixel(x, y, x % 2 == 0 && y % 2 == 0);
|
|
}
|
|
|
|
template <>
|
|
void GfxRenderer::drawPixelDither<Color::DarkGray>(const int x, const int y) const {
|
|
drawPixel(x, y, (x + y) % 2 == 0); // TODO: maybe find a better pattern?
|
|
}
|
|
|
|
void GfxRenderer::fillRectDither(const int x, const int y, const int width, const int height, Color color) const {
|
|
if (color == Color::Clear) {
|
|
} else if (color == Color::Black) {
|
|
fillRect(x, y, width, height, true);
|
|
} else if (color == Color::White) {
|
|
fillRect(x, y, width, height, false);
|
|
} else if (color == Color::DarkGray) {
|
|
// Pattern: dark where (phyX + phyY) % 2 == 0 (alternating checkerboard).
|
|
// Byte patterns (phyY even / phyY odd):
|
|
// Portrait / PortraitInverted: 0xAA / 0x55
|
|
// LandscapeCW / LandscapeCCW: 0x55 / 0xAA
|
|
switch (orientation) {
|
|
case Portrait:
|
|
for (int lx = x; lx < x + width; lx++) {
|
|
const int phyY = HalDisplay::DISPLAY_HEIGHT - 1 - lx;
|
|
const uint8_t pb = (phyY % 2 == 0) ? 0xAA : 0x55;
|
|
fillPhysicalHSpanByte(phyY, y, y + height - 1, pb);
|
|
}
|
|
return;
|
|
case PortraitInverted:
|
|
for (int lx = x; lx < x + width; lx++) {
|
|
const int phyY = lx;
|
|
const uint8_t pb = (phyY % 2 == 0) ? 0xAA : 0x55;
|
|
fillPhysicalHSpanByte(phyY, HalDisplay::DISPLAY_WIDTH - 1 - (y + height - 1),
|
|
HalDisplay::DISPLAY_WIDTH - 1 - y, pb);
|
|
}
|
|
return;
|
|
case LandscapeCounterClockwise:
|
|
for (int ly = y; ly < y + height; ly++) {
|
|
const int phyY = ly;
|
|
const uint8_t pb = (phyY % 2 == 0) ? 0x55 : 0xAA;
|
|
fillPhysicalHSpanByte(phyY, x, x + width - 1, pb);
|
|
}
|
|
return;
|
|
case LandscapeClockwise:
|
|
for (int ly = y; ly < y + height; ly++) {
|
|
const int phyY = HalDisplay::DISPLAY_HEIGHT - 1 - ly;
|
|
const uint8_t pb = (phyY % 2 == 0) ? 0x55 : 0xAA;
|
|
fillPhysicalHSpanByte(phyY, HalDisplay::DISPLAY_WIDTH - 1 - (x + width - 1),
|
|
HalDisplay::DISPLAY_WIDTH - 1 - x, pb);
|
|
}
|
|
return;
|
|
}
|
|
} else if (color == Color::LightGray) {
|
|
// Pattern: dark where phyX % 2 == 0 && phyY % 2 == 0 (1-in-4 pixels dark).
|
|
// Rows that would be all-white are skipped entirely.
|
|
switch (orientation) {
|
|
case Portrait:
|
|
for (int lx = x; lx < x + width; lx++) {
|
|
const int phyY = HalDisplay::DISPLAY_HEIGHT - 1 - lx;
|
|
if (phyY % 2 == 0) continue;
|
|
fillPhysicalHSpanByte(phyY, y, y + height - 1, 0x55);
|
|
}
|
|
return;
|
|
case PortraitInverted:
|
|
for (int lx = x; lx < x + width; lx++) {
|
|
const int phyY = lx;
|
|
if (phyY % 2 != 0) continue;
|
|
fillPhysicalHSpanByte(phyY, HalDisplay::DISPLAY_WIDTH - 1 - (y + height - 1),
|
|
HalDisplay::DISPLAY_WIDTH - 1 - y, 0xAA);
|
|
}
|
|
return;
|
|
case LandscapeCounterClockwise:
|
|
for (int ly = y; ly < y + height; ly++) {
|
|
const int phyY = ly;
|
|
if (phyY % 2 != 0) continue;
|
|
fillPhysicalHSpanByte(phyY, x, x + width - 1, 0x55);
|
|
}
|
|
return;
|
|
case LandscapeClockwise:
|
|
for (int ly = y; ly < y + height; ly++) {
|
|
const int phyY = HalDisplay::DISPLAY_HEIGHT - 1 - ly;
|
|
if (phyY % 2 == 0) continue;
|
|
fillPhysicalHSpanByte(phyY, HalDisplay::DISPLAY_WIDTH - 1 - (x + width - 1),
|
|
HalDisplay::DISPLAY_WIDTH - 1 - x, 0xAA);
|
|
}
|
|
return;
|
|
}
|
|
}
|
|
}
|
|
|
|
template <Color color>
|
|
void GfxRenderer::fillArc(const int maxRadius, const int cx, const int cy, const int xDir, const int yDir) const {
|
|
const int radiusSq = maxRadius * maxRadius;
|
|
for (int dy = 0; dy <= maxRadius; ++dy) {
|
|
for (int dx = 0; dx <= maxRadius; ++dx) {
|
|
const int distSq = dx * dx + dy * dy;
|
|
const int px = cx + xDir * dx;
|
|
const int py = cy + yDir * dy;
|
|
if (distSq <= radiusSq) {
|
|
drawPixelDither<color>(px, py);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
void GfxRenderer::fillRoundedRect(const int x, const int y, const int width, const int height, const int cornerRadius,
|
|
const Color color) const {
|
|
fillRoundedRect(x, y, width, height, cornerRadius, true, true, true, true, color);
|
|
}
|
|
|
|
void GfxRenderer::fillRoundedRect(const int x, const int y, const int width, const int height, const int cornerRadius,
|
|
bool roundTopLeft, bool roundTopRight, bool roundBottomLeft, bool roundBottomRight,
|
|
const Color color) const {
|
|
if (width <= 0 || height <= 0) {
|
|
return;
|
|
}
|
|
|
|
// Assume if we're not rounding all corners then we are only rounding one side
|
|
const int roundedSides = (!roundTopLeft || !roundTopRight || !roundBottomLeft || !roundBottomRight) ? 1 : 2;
|
|
const int maxRadius = std::min({cornerRadius, width / roundedSides, height / roundedSides});
|
|
if (maxRadius <= 0) {
|
|
fillRectDither(x, y, width, height, color);
|
|
return;
|
|
}
|
|
|
|
const int horizontalWidth = width - 2 * maxRadius;
|
|
if (horizontalWidth > 0) {
|
|
fillRectDither(x + maxRadius + 1, y, horizontalWidth - 2, height, color);
|
|
}
|
|
|
|
const int leftFillTop = y + (roundTopLeft ? (maxRadius + 1) : 0);
|
|
const int leftFillBottom = y + height - 1 - (roundBottomLeft ? (maxRadius + 1) : 0);
|
|
if (leftFillBottom >= leftFillTop) {
|
|
fillRectDither(x, leftFillTop, maxRadius + 1, leftFillBottom - leftFillTop + 1, color);
|
|
}
|
|
|
|
const int rightFillTop = y + (roundTopRight ? (maxRadius + 1) : 0);
|
|
const int rightFillBottom = y + height - 1 - (roundBottomRight ? (maxRadius + 1) : 0);
|
|
if (rightFillBottom >= rightFillTop) {
|
|
fillRectDither(x + width - maxRadius - 1, rightFillTop, maxRadius + 1, rightFillBottom - rightFillTop + 1, color);
|
|
}
|
|
|
|
auto fillArcTemplated = [this](int maxRadius, int cx, int cy, int xDir, int yDir, Color color) {
|
|
switch (color) {
|
|
case Color::Clear:
|
|
break;
|
|
case Color::Black:
|
|
fillArc<Color::Black>(maxRadius, cx, cy, xDir, yDir);
|
|
break;
|
|
case Color::White:
|
|
fillArc<Color::White>(maxRadius, cx, cy, xDir, yDir);
|
|
break;
|
|
case Color::LightGray:
|
|
fillArc<Color::LightGray>(maxRadius, cx, cy, xDir, yDir);
|
|
break;
|
|
case Color::DarkGray:
|
|
fillArc<Color::DarkGray>(maxRadius, cx, cy, xDir, yDir);
|
|
break;
|
|
}
|
|
};
|
|
|
|
if (roundTopLeft) {
|
|
fillArcTemplated(maxRadius, x + maxRadius, y + maxRadius, -1, -1, color);
|
|
}
|
|
|
|
if (roundTopRight) {
|
|
fillArcTemplated(maxRadius, x + width - maxRadius - 1, y + maxRadius, 1, -1, color);
|
|
}
|
|
|
|
if (roundBottomRight) {
|
|
fillArcTemplated(maxRadius, x + width - maxRadius - 1, y + height - maxRadius - 1, 1, 1, color);
|
|
}
|
|
|
|
if (roundBottomLeft) {
|
|
fillArcTemplated(maxRadius, x + maxRadius, y + height - maxRadius - 1, -1, 1, color);
|
|
}
|
|
}
|
|
|
|
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(orientation, 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
|
|
display.drawImage(bitmap, rotatedX, rotatedY, width, height);
|
|
}
|
|
|
|
void GfxRenderer::drawIcon(const uint8_t bitmap[], const int x, const int y, const int width, const int height) const {
|
|
display.drawImageTransparent(bitmap, y, getScreenWidth() - width - x, height, width);
|
|
}
|
|
|
|
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 {
|
|
// 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);
|
|
return;
|
|
}
|
|
|
|
float scale = 1.0f;
|
|
bool isScaled = false;
|
|
int cropPixX = std::floor(bitmap.getWidth() * cropX / 2.0f);
|
|
int cropPixY = std::floor(bitmap.getHeight() * cropY / 2.0f);
|
|
LOG_DBG("GFX", "Cropping %dx%d by %dx%d pix, is %s", bitmap.getWidth(), bitmap.getHeight(), cropPixX, cropPixY,
|
|
bitmap.isTopDown() ? "top-down" : "bottom-up");
|
|
|
|
if (maxWidth > 0 && (1.0f - cropX) * bitmap.getWidth() > maxWidth) {
|
|
scale = static_cast<float>(maxWidth) / static_cast<float>((1.0f - cropX) * bitmap.getWidth());
|
|
isScaled = true;
|
|
}
|
|
if (maxHeight > 0 && (1.0f - cropY) * bitmap.getHeight() > maxHeight) {
|
|
scale = std::min(scale, static_cast<float>(maxHeight) / static_cast<float>((1.0f - cropY) * bitmap.getHeight()));
|
|
isScaled = true;
|
|
}
|
|
LOG_DBG("GFX", "Scaling by %f - %s", scale, isScaled ? "scaled" : "not scaled");
|
|
|
|
// 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) {
|
|
LOG_ERR("GFX", "!! Failed to allocate BMP row buffers");
|
|
free(outputRow);
|
|
free(rowBytes);
|
|
return;
|
|
}
|
|
|
|
for (int bmpY = 0; bmpY < (bitmap.getHeight() - cropPixY); bmpY++) {
|
|
// 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.
|
|
int screenY = -cropPixY + (bitmap.isTopDown() ? bmpY : bitmap.getHeight() - 1 - bmpY);
|
|
if (isScaled) {
|
|
screenY = std::floor(screenY * scale);
|
|
}
|
|
screenY += y; // the offset should not be scaled
|
|
if (screenY >= getScreenHeight()) {
|
|
break;
|
|
}
|
|
|
|
if (bitmap.readNextRow(outputRow, rowBytes) != BmpReaderError::Ok) {
|
|
LOG_ERR("GFX", "Failed to read row %d from bitmap", bmpY);
|
|
free(outputRow);
|
|
free(rowBytes);
|
|
return;
|
|
}
|
|
|
|
if (screenY < 0) {
|
|
continue;
|
|
}
|
|
|
|
if (bmpY < cropPixY) {
|
|
// Skip the row if it's outside the crop area
|
|
continue;
|
|
}
|
|
|
|
for (int bmpX = cropPixX; bmpX < bitmap.getWidth() - cropPixX; bmpX++) {
|
|
int screenX = bmpX - cropPixX;
|
|
if (isScaled) {
|
|
screenX = std::floor(screenX * scale);
|
|
}
|
|
screenX += x; // the offset should not be scaled
|
|
if (screenX >= getScreenWidth()) {
|
|
break;
|
|
}
|
|
if (screenX < 0) {
|
|
continue;
|
|
}
|
|
|
|
const uint8_t val = outputRow[bmpX / 4] >> (6 - ((bmpX * 2) % 8)) & 0x3;
|
|
|
|
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 {
|
|
float scale = 1.0f;
|
|
bool isScaled = false;
|
|
if (maxWidth > 0 && bitmap.getWidth() > maxWidth) {
|
|
scale = static_cast<float>(maxWidth) / static_cast<float>(bitmap.getWidth());
|
|
isScaled = true;
|
|
}
|
|
if (maxHeight > 0 && bitmap.getHeight() > maxHeight) {
|
|
scale = std::min(scale, static_cast<float>(maxHeight) / static_cast<float>(bitmap.getHeight()));
|
|
isScaled = true;
|
|
}
|
|
|
|
// 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) {
|
|
LOG_ERR("GFX", "!! Failed to allocate 1-bit BMP row buffers");
|
|
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) {
|
|
LOG_ERR("GFX", "Failed to read row %d from 1-bit bitmap", bmpY);
|
|
free(outputRow);
|
|
free(rowBytes);
|
|
return;
|
|
}
|
|
|
|
// Calculate screen Y based on whether BMP is top-down or bottom-up
|
|
const int bmpYOffset = bitmap.isTopDown() ? bmpY : bitmap.getHeight() - 1 - bmpY;
|
|
int screenY = y + (isScaled ? static_cast<int>(std::floor(bmpYOffset * scale)) : bmpYOffset);
|
|
if (screenY >= getScreenHeight()) {
|
|
continue; // Continue reading to keep row counter in sync
|
|
}
|
|
if (screenY < 0) {
|
|
continue;
|
|
}
|
|
|
|
for (int bmpX = 0; bmpX < bitmap.getWidth(); bmpX++) {
|
|
int screenX = x + (isScaled ? static_cast<int>(std::floor(bmpX * scale)) : bmpX);
|
|
if (screenX >= getScreenWidth()) {
|
|
break;
|
|
}
|
|
if (screenX < 0) {
|
|
continue;
|
|
}
|
|
|
|
// 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 (draw it)
|
|
if (val < 3) {
|
|
drawPixel(screenX, screenY, true);
|
|
}
|
|
// White pixels (val == 3) are not drawn (leave background)
|
|
}
|
|
}
|
|
|
|
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) {
|
|
LOG_ERR("GFX", "!! Failed to allocate polygon node buffer");
|
|
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;
|
|
|
|
// In Landscape orientations, horizontal scanlines map to physical horizontal spans.
|
|
if (orientation == LandscapeCounterClockwise) {
|
|
fillPhysicalHSpan(scanY, startX, endX, state);
|
|
} else if (orientation == LandscapeClockwise) {
|
|
fillPhysicalHSpan(HalDisplay::DISPLAY_HEIGHT - 1 - scanY, HalDisplay::DISPLAY_WIDTH - 1 - endX,
|
|
HalDisplay::DISPLAY_WIDTH - 1 - startX, state);
|
|
} else {
|
|
for (int px = startX; px <= endX; px++) {
|
|
drawPixel(px, scanY, state);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
free(nodeX);
|
|
}
|
|
|
|
// For performance measurement (using static to allow "const" methods)
|
|
static unsigned long start_ms = 0;
|
|
|
|
void GfxRenderer::clearScreen(const uint8_t color) const {
|
|
start_ms = millis();
|
|
display.clearScreen(color);
|
|
}
|
|
|
|
void GfxRenderer::invertScreen() const {
|
|
for (int i = 0; i < HalDisplay::BUFFER_SIZE; i++) {
|
|
frameBuffer[i] = ~frameBuffer[i];
|
|
}
|
|
}
|
|
|
|
void GfxRenderer::displayBuffer(const HalDisplay::RefreshMode refreshMode) const {
|
|
auto elapsed = millis() - start_ms;
|
|
LOG_DBG("GFX", "Time = %lu ms from clearScreen to displayBuffer", elapsed);
|
|
display.displayBuffer(refreshMode, fadingFix);
|
|
}
|
|
|
|
std::string GfxRenderer::truncatedText(const int fontId, const char* text, const int maxWidth,
|
|
const EpdFontFamily::Style style) const {
|
|
if (!text || maxWidth <= 0) return "";
|
|
|
|
std::string item = text;
|
|
// 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
|
|
return item;
|
|
}
|
|
|
|
while (!item.empty() && getTextWidth(fontId, (item + ellipsis).c_str(), style) >= maxWidth) {
|
|
utf8RemoveLastChar(item);
|
|
}
|
|
|
|
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) {
|
|
case Portrait:
|
|
case PortraitInverted:
|
|
// 480px wide in portrait logical coordinates
|
|
return HalDisplay::DISPLAY_HEIGHT;
|
|
case LandscapeClockwise:
|
|
case LandscapeCounterClockwise:
|
|
// 800px wide in landscape logical coordinates
|
|
return HalDisplay::DISPLAY_WIDTH;
|
|
}
|
|
return HalDisplay::DISPLAY_HEIGHT;
|
|
}
|
|
|
|
int GfxRenderer::getScreenHeight() const {
|
|
switch (orientation) {
|
|
case Portrait:
|
|
case PortraitInverted:
|
|
// 800px tall in portrait logical coordinates
|
|
return HalDisplay::DISPLAY_WIDTH;
|
|
case LandscapeClockwise:
|
|
case LandscapeCounterClockwise:
|
|
// 480px tall in landscape logical coordinates
|
|
return HalDisplay::DISPLAY_HEIGHT;
|
|
}
|
|
return HalDisplay::DISPLAY_WIDTH;
|
|
}
|
|
|
|
int GfxRenderer::getSpaceWidth(const int fontId, const EpdFontFamily::Style style) const {
|
|
const auto fontIt = fontMap.find(fontId);
|
|
if (fontIt == fontMap.end()) {
|
|
LOG_ERR("GFX", "Font %d not found", fontId);
|
|
return 0;
|
|
}
|
|
|
|
const EpdGlyph* spaceGlyph = fontIt->second.getGlyph(' ', style);
|
|
return spaceGlyph ? fp4::toPixel(spaceGlyph->advanceX) : 0; // snap 12.4 fixed-point to nearest pixel
|
|
}
|
|
|
|
int GfxRenderer::getSpaceKernAdjust(const int fontId, const uint32_t leftCp, const uint32_t rightCp,
|
|
const EpdFontFamily::Style style) const {
|
|
const auto fontIt = fontMap.find(fontId);
|
|
if (fontIt == fontMap.end()) return 0;
|
|
const auto& font = fontIt->second;
|
|
const int kernFP = font.getKerning(leftCp, ' ', style) + font.getKerning(' ', rightCp, style); // 4.4 fixed-point
|
|
return fp4::toPixel(kernFP); // snap 4.4 fixed-point to nearest pixel
|
|
}
|
|
|
|
int GfxRenderer::getKerning(const int fontId, const uint32_t leftCp, const uint32_t rightCp,
|
|
const EpdFontFamily::Style style) const {
|
|
const auto fontIt = fontMap.find(fontId);
|
|
if (fontIt == fontMap.end()) return 0;
|
|
const int kernFP = fontIt->second.getKerning(leftCp, rightCp, style); // 4.4 fixed-point
|
|
return fp4::toPixel(kernFP); // snap 4.4 fixed-point to nearest pixel
|
|
}
|
|
|
|
int GfxRenderer::getTextAdvanceX(const int fontId, const char* text, EpdFontFamily::Style style) const {
|
|
const auto fontIt = fontMap.find(fontId);
|
|
if (fontIt == fontMap.end()) {
|
|
LOG_ERR("GFX", "Font %d not found", fontId);
|
|
return 0;
|
|
}
|
|
|
|
uint32_t cp;
|
|
uint32_t prevCp = 0;
|
|
int32_t widthFP = 0; // 12.4 fixed-point accumulator
|
|
const auto& font = fontIt->second;
|
|
while ((cp = utf8NextCodepoint(reinterpret_cast<const uint8_t**>(&text)))) {
|
|
if (utf8IsCombiningMark(cp)) {
|
|
continue;
|
|
}
|
|
cp = font.applyLigatures(cp, text, style);
|
|
if (prevCp != 0) {
|
|
widthFP += font.getKerning(prevCp, cp, style); // 4.4 fixed-point kern
|
|
}
|
|
const EpdGlyph* glyph = font.getGlyph(cp, style);
|
|
if (glyph) widthFP += glyph->advanceX; // 12.4 fixed-point advance
|
|
prevCp = cp;
|
|
}
|
|
return fp4::toPixel(widthFP); // snap 12.4 fixed-point to nearest pixel
|
|
}
|
|
|
|
int GfxRenderer::getFontAscenderSize(const int fontId) const {
|
|
const auto fontIt = fontMap.find(fontId);
|
|
if (fontIt == fontMap.end()) {
|
|
LOG_ERR("GFX", "Font %d not found", fontId);
|
|
return 0;
|
|
}
|
|
|
|
return fontIt->second.getData(EpdFontFamily::REGULAR)->ascender;
|
|
}
|
|
|
|
int GfxRenderer::getLineHeight(const int fontId) const {
|
|
const auto fontIt = fontMap.find(fontId);
|
|
if (fontIt == fontMap.end()) {
|
|
LOG_ERR("GFX", "Font %d not found", fontId);
|
|
return 0;
|
|
}
|
|
|
|
return fontIt->second.getData(EpdFontFamily::REGULAR)->advanceY;
|
|
}
|
|
|
|
int GfxRenderer::getTextHeight(const int fontId) const {
|
|
const auto fontIt = fontMap.find(fontId);
|
|
if (fontIt == fontMap.end()) {
|
|
LOG_ERR("GFX", "Font %d not found", fontId);
|
|
return 0;
|
|
}
|
|
return fontIt->second.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;
|
|
}
|
|
|
|
const auto fontIt = fontMap.find(fontId);
|
|
if (fontIt == fontMap.end()) {
|
|
LOG_ERR("GFX", "Font %d not found", fontId);
|
|
return;
|
|
}
|
|
|
|
const auto& font = fontIt->second;
|
|
|
|
int32_t yPosFP = fp4::fromPixel(y); // 12.4 fixed-point accumulator
|
|
int lastBaseY = y;
|
|
int lastBaseAdvanceFP = 0; // 12.4 fixed-point
|
|
int lastBaseTop = 0;
|
|
constexpr int MIN_COMBINING_GAP_PX = 1;
|
|
|
|
uint32_t cp;
|
|
uint32_t prevCp = 0;
|
|
while ((cp = utf8NextCodepoint(reinterpret_cast<const uint8_t**>(&text)))) {
|
|
if (utf8IsCombiningMark(cp)) {
|
|
const EpdGlyph* combiningGlyph = font.getGlyph(cp, style);
|
|
int raiseBy = 0;
|
|
if (combiningGlyph) {
|
|
const int currentGap = combiningGlyph->top - combiningGlyph->height - lastBaseTop;
|
|
if (currentGap < MIN_COMBINING_GAP_PX) {
|
|
raiseBy = MIN_COMBINING_GAP_PX - currentGap;
|
|
}
|
|
}
|
|
|
|
const int combiningX = x - raiseBy;
|
|
const int combiningY = lastBaseY - fp4::toPixel(lastBaseAdvanceFP / 2);
|
|
renderCharImpl<TextRotation::Rotated90CW>(*this, renderMode, font, cp, combiningX, combiningY, black, style);
|
|
continue;
|
|
}
|
|
|
|
cp = font.applyLigatures(cp, text, style);
|
|
if (prevCp != 0) {
|
|
yPosFP -= font.getKerning(prevCp, cp, style); // 4.4 fixed-point kern (subtract for rotated)
|
|
}
|
|
|
|
lastBaseY = fp4::toPixel(yPosFP); // snap 12.4 fixed-point to nearest pixel
|
|
const EpdGlyph* glyph = font.getGlyph(cp, style);
|
|
|
|
lastBaseAdvanceFP = glyph ? glyph->advanceX : 0; // 12.4 fixed-point
|
|
lastBaseTop = glyph ? glyph->top : 0;
|
|
|
|
renderCharImpl<TextRotation::Rotated90CW>(*this, renderMode, font, cp, x, lastBaseY, black, style);
|
|
if (glyph) {
|
|
yPosFP -= glyph->advanceX; // 12.4 fixed-point advance (subtract for rotated)
|
|
}
|
|
prevCp = cp;
|
|
}
|
|
}
|
|
|
|
void GfxRenderer::drawTextRotated90CCW(const int fontId, const int x, const int y, const char* text, const bool black,
|
|
const EpdFontFamily::Style style) const {
|
|
if (text == nullptr || *text == '\0') {
|
|
return;
|
|
}
|
|
|
|
const auto fontIt = fontMap.find(fontId);
|
|
if (fontIt == fontMap.end()) {
|
|
LOG_ERR("GFX", "Font %d not found", fontId);
|
|
return;
|
|
}
|
|
|
|
const auto& font = fontIt->second;
|
|
|
|
int32_t yPosFP = fp4::fromPixel(y);
|
|
int lastBaseY = y;
|
|
int lastBaseAdvanceFP = 0;
|
|
int lastBaseTop = 0;
|
|
constexpr int MIN_COMBINING_GAP_PX = 1;
|
|
|
|
uint32_t cp;
|
|
uint32_t prevCp = 0;
|
|
while ((cp = utf8NextCodepoint(reinterpret_cast<const uint8_t**>(&text)))) {
|
|
if (utf8IsCombiningMark(cp)) {
|
|
const EpdGlyph* combiningGlyph = font.getGlyph(cp, style);
|
|
int raiseBy = 0;
|
|
if (combiningGlyph) {
|
|
const int currentGap = combiningGlyph->top - combiningGlyph->height - lastBaseTop;
|
|
if (currentGap < MIN_COMBINING_GAP_PX) {
|
|
raiseBy = MIN_COMBINING_GAP_PX - currentGap;
|
|
}
|
|
}
|
|
|
|
const int combiningX = x + raiseBy;
|
|
const int combiningY = lastBaseY + fp4::toPixel(lastBaseAdvanceFP / 2);
|
|
renderCharImpl<TextRotation::Rotated90CCW>(*this, renderMode, font, cp, combiningX, combiningY, black, style);
|
|
continue;
|
|
}
|
|
|
|
cp = font.applyLigatures(cp, text, style);
|
|
if (prevCp != 0) {
|
|
yPosFP += font.getKerning(prevCp, cp, style); // add for CCW (opposite of CW)
|
|
}
|
|
|
|
lastBaseY = fp4::toPixel(yPosFP);
|
|
const EpdGlyph* glyph = font.getGlyph(cp, style);
|
|
|
|
lastBaseAdvanceFP = glyph ? glyph->advanceX : 0;
|
|
lastBaseTop = glyph ? glyph->top : 0;
|
|
|
|
renderCharImpl<TextRotation::Rotated90CCW>(*this, renderMode, font, cp, x, lastBaseY, black, style);
|
|
if (glyph) {
|
|
yPosFP += glyph->advanceX; // add for CCW (opposite of CW)
|
|
}
|
|
prevCp = cp;
|
|
}
|
|
}
|
|
|
|
uint8_t* GfxRenderer::getFrameBuffer() const { return frameBuffer; }
|
|
|
|
size_t GfxRenderer::getBufferSize() { return HalDisplay::BUFFER_SIZE; }
|
|
|
|
// unused
|
|
// void GfxRenderer::grayscaleRevert() const { display.grayscaleRevert(); }
|
|
|
|
void GfxRenderer::copyGrayscaleLsbBuffers() const { display.copyGrayscaleLsbBuffers(frameBuffer); }
|
|
|
|
void GfxRenderer::copyGrayscaleMsbBuffers() const { display.copyGrayscaleMsbBuffers(frameBuffer); }
|
|
|
|
void GfxRenderer::displayGrayBuffer() const { display.displayGrayBuffer(fadingFix); }
|
|
|
|
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() {
|
|
// 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]) {
|
|
LOG_ERR("GFX", "!! BW buffer chunk %zu already stored - this is likely a bug, freeing chunk", 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]) {
|
|
LOG_ERR("GFX", "!! Failed to allocate BW buffer chunk %zu (%zu bytes)", i, BW_BUFFER_CHUNK_SIZE);
|
|
// Free previously allocated chunks
|
|
freeBwBufferChunks();
|
|
return false;
|
|
}
|
|
|
|
memcpy(bwBufferChunks[i], frameBuffer + offset, BW_BUFFER_CHUNK_SIZE);
|
|
}
|
|
|
|
LOG_DBG("GFX", "Stored BW buffer in %zu chunks (%zu bytes each)", 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();
|
|
return;
|
|
}
|
|
|
|
for (size_t i = 0; i < BW_BUFFER_NUM_CHUNKS; i++) {
|
|
const size_t offset = i * BW_BUFFER_CHUNK_SIZE;
|
|
memcpy(frameBuffer + offset, bwBufferChunks[i], BW_BUFFER_CHUNK_SIZE);
|
|
}
|
|
|
|
display.cleanupGrayscaleBuffers(frameBuffer);
|
|
|
|
freeBwBufferChunks();
|
|
LOG_DBG("GFX", "Restored and freed BW buffer chunks");
|
|
}
|
|
|
|
/**
|
|
* Cleanup grayscale buffers using the current frame buffer.
|
|
* Use this when BW buffer was re-rendered instead of stored/restored.
|
|
*/
|
|
void GfxRenderer::cleanupGrayscaleWithFrameBuffer() const {
|
|
if (frameBuffer) {
|
|
display.cleanupGrayscaleBuffers(frameBuffer);
|
|
}
|
|
}
|
|
|
|
void GfxRenderer::getOrientedViewableTRBL(int* outTop, int* outRight, int* outBottom, int* outLeft) const {
|
|
switch (orientation) {
|
|
case Portrait:
|
|
*outTop = VIEWABLE_MARGIN_TOP;
|
|
*outRight = VIEWABLE_MARGIN_RIGHT;
|
|
*outBottom = VIEWABLE_MARGIN_BOTTOM;
|
|
*outLeft = VIEWABLE_MARGIN_LEFT;
|
|
break;
|
|
case LandscapeClockwise:
|
|
*outTop = VIEWABLE_MARGIN_LEFT;
|
|
*outRight = VIEWABLE_MARGIN_TOP;
|
|
*outBottom = VIEWABLE_MARGIN_RIGHT;
|
|
*outLeft = VIEWABLE_MARGIN_BOTTOM;
|
|
break;
|
|
case PortraitInverted:
|
|
*outTop = VIEWABLE_MARGIN_BOTTOM;
|
|
*outRight = VIEWABLE_MARGIN_LEFT;
|
|
*outBottom = VIEWABLE_MARGIN_TOP;
|
|
*outLeft = VIEWABLE_MARGIN_RIGHT;
|
|
break;
|
|
case LandscapeCounterClockwise:
|
|
*outTop = VIEWABLE_MARGIN_RIGHT;
|
|
*outRight = VIEWABLE_MARGIN_BOTTOM;
|
|
*outBottom = VIEWABLE_MARGIN_LEFT;
|
|
*outLeft = VIEWABLE_MARGIN_TOP;
|
|
break;
|
|
}
|
|
}
|