Cherry-pick merge from pablohc/crosspoint-reader@2d8cbcf, based on upstream PR #556 by martinbrook with pablohc's refresh optimization. - Add JPEG decoder (picojpeg) and PNG decoder (PNGdec) with 4-level grayscale Bayer dithering for e-ink display - Add pixel caching system (.pxc files) for fast image re-rendering - Integrate image extraction from EPUB HTML parser (<img> tag support) - Add ImageBlock/PageImage types with serialization support - Add image-aware refresh optimization (double FAST_REFRESH technique) - Add experimental displayWindow() partial refresh support - Bump section cache version 12->13 to invalidate stale caches - Resolve TAG_PageImage=3 to avoid conflict with mod's TAG_PageTableRow=2 Co-authored-by: Cursor <cursoragent@cursor.com>
115 lines
4.0 KiB
C++
115 lines
4.0 KiB
C++
#pragma once
|
|
|
|
#include <expat.h>
|
|
|
|
#include <climits>
|
|
#include <functional>
|
|
#include <memory>
|
|
|
|
#include "../ParsedText.h"
|
|
#include "../TableData.h"
|
|
#include "../blocks/ImageBlock.h"
|
|
#include "../blocks/TextBlock.h"
|
|
#include "../css/CssParser.h"
|
|
#include "../css/CssStyle.h"
|
|
|
|
class Page;
|
|
class PageTableRow;
|
|
class GfxRenderer;
|
|
class Epub;
|
|
|
|
#define MAX_WORD_SIZE 200
|
|
|
|
class ChapterHtmlSlimParser {
|
|
std::shared_ptr<Epub> epub;
|
|
const std::string& filepath;
|
|
GfxRenderer& renderer;
|
|
std::function<void(std::unique_ptr<Page>)> completePageFn;
|
|
std::function<void()> popupFn; // Popup callback
|
|
int depth = 0;
|
|
int skipUntilDepth = INT_MAX;
|
|
int boldUntilDepth = INT_MAX;
|
|
int italicUntilDepth = INT_MAX;
|
|
int underlineUntilDepth = INT_MAX;
|
|
// buffer for building up words from characters, will auto break if longer than this
|
|
// leave one char at end for null pointer
|
|
char partWordBuffer[MAX_WORD_SIZE + 1] = {};
|
|
int partWordBufferIndex = 0;
|
|
bool nextWordContinues = false; // true when next flushed word attaches to previous (inline element boundary)
|
|
std::unique_ptr<ParsedText> currentTextBlock = nullptr;
|
|
std::unique_ptr<Page> currentPage = nullptr;
|
|
int16_t currentPageNextY = 0;
|
|
int fontId;
|
|
float lineCompression;
|
|
bool extraParagraphSpacing;
|
|
uint8_t paragraphAlignment;
|
|
uint16_t viewportWidth;
|
|
uint16_t viewportHeight;
|
|
bool hyphenationEnabled;
|
|
const CssParser* cssParser;
|
|
bool embeddedStyle;
|
|
std::string contentBase;
|
|
std::string imageBasePath;
|
|
int imageCounter = 0;
|
|
|
|
// Style tracking (replaces depth-based approach)
|
|
struct StyleStackEntry {
|
|
int depth = 0;
|
|
bool hasBold = false, bold = false;
|
|
bool hasItalic = false, italic = false;
|
|
bool hasUnderline = false, underline = false;
|
|
};
|
|
std::vector<StyleStackEntry> inlineStyleStack;
|
|
CssStyle currentCssStyle;
|
|
bool effectiveBold = false;
|
|
bool effectiveItalic = false;
|
|
bool effectiveUnderline = false;
|
|
|
|
// Table buffering state
|
|
bool inTable = false;
|
|
std::unique_ptr<TableData> tableData;
|
|
|
|
void updateEffectiveInlineStyle();
|
|
void startNewTextBlock(const BlockStyle& blockStyle);
|
|
void flushPartWordBuffer();
|
|
void makePages();
|
|
void processTable();
|
|
void addTableRowToPage(std::shared_ptr<PageTableRow> row);
|
|
// XML callbacks
|
|
static void XMLCALL startElement(void* userData, const XML_Char* name, const XML_Char** atts);
|
|
static void XMLCALL characterData(void* userData, const XML_Char* s, int len);
|
|
static void XMLCALL defaultHandlerExpand(void* userData, const XML_Char* s, int len);
|
|
static void XMLCALL endElement(void* userData, const XML_Char* name);
|
|
|
|
public:
|
|
explicit ChapterHtmlSlimParser(std::shared_ptr<Epub> epub, const std::string& filepath, GfxRenderer& renderer,
|
|
const int fontId, const float lineCompression, const bool extraParagraphSpacing,
|
|
const uint8_t paragraphAlignment, const uint16_t viewportWidth,
|
|
const uint16_t viewportHeight, const bool hyphenationEnabled,
|
|
const std::function<void(std::unique_ptr<Page>)>& completePageFn,
|
|
const bool embeddedStyle, const std::string& contentBase,
|
|
const std::string& imageBasePath, const std::function<void()>& popupFn = nullptr,
|
|
const CssParser* cssParser = nullptr)
|
|
|
|
: epub(epub),
|
|
filepath(filepath),
|
|
renderer(renderer),
|
|
fontId(fontId),
|
|
lineCompression(lineCompression),
|
|
extraParagraphSpacing(extraParagraphSpacing),
|
|
paragraphAlignment(paragraphAlignment),
|
|
viewportWidth(viewportWidth),
|
|
viewportHeight(viewportHeight),
|
|
hyphenationEnabled(hyphenationEnabled),
|
|
completePageFn(completePageFn),
|
|
popupFn(popupFn),
|
|
cssParser(cssParser),
|
|
embeddedStyle(embeddedStyle),
|
|
contentBase(contentBase),
|
|
imageBasePath(imageBasePath) {}
|
|
|
|
~ChapterHtmlSlimParser() = default;
|
|
bool parseAndBuildPages();
|
|
void addLineToPage(std::shared_ptr<TextBlock> line);
|
|
};
|