61 lines
1.8 KiB
C++
61 lines
1.8 KiB
C++
#pragma once
|
|
|
|
#include <expat.h>
|
|
|
|
#include <climits>
|
|
#include <functional>
|
|
|
|
#include "blocks/TextBlock.h"
|
|
|
|
class Page;
|
|
class GfxRenderer;
|
|
|
|
#define MAX_WORD_SIZE 200
|
|
|
|
class EpubHtmlParserSlim {
|
|
const char* filepath;
|
|
GfxRenderer& renderer;
|
|
std::function<void(Page*)> completePageFn;
|
|
int depth = 0;
|
|
int skipUntilDepth = INT_MAX;
|
|
int boldUntilDepth = INT_MAX;
|
|
int italicUntilDepth = 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;
|
|
TextBlock* currentTextBlock = nullptr;
|
|
Page* currentPage = nullptr;
|
|
int currentPageNextY = 0;
|
|
int fontId;
|
|
float lineCompression;
|
|
int marginTop;
|
|
int marginRight;
|
|
int marginBottom;
|
|
int marginLeft;
|
|
|
|
void startNewTextBlock(BLOCK_STYLE style);
|
|
void makePages();
|
|
// 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 endElement(void* userData, const XML_Char* name);
|
|
|
|
public:
|
|
explicit EpubHtmlParserSlim(const char* filepath, GfxRenderer& renderer, const int fontId,
|
|
const float lineCompression, const int marginTop, const int marginRight,
|
|
const int marginBottom, const int marginLeft,
|
|
const std::function<void(Page*)>& completePageFn)
|
|
: filepath(filepath),
|
|
renderer(renderer),
|
|
fontId(fontId),
|
|
lineCompression(lineCompression),
|
|
marginTop(marginTop),
|
|
marginRight(marginRight),
|
|
marginBottom(marginBottom),
|
|
marginLeft(marginLeft),
|
|
completePageFn(completePageFn) {}
|
|
~EpubHtmlParserSlim() = default;
|
|
bool parseAndBuildPages();
|
|
};
|