x
This commit is contained in:
pc0124 2026-01-17 16:05:03 +08:00 committed by GitHub
parent d123e99ca9
commit c3dee887b5
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 48 additions and 40 deletions

View File

@ -42,10 +42,12 @@ std::vector<uint16_t> ParsedText::calculateWordWidths(const GfxRenderer& rendere
std::vector<uint16_t> wordWidths;
wordWidths.reserve(totalWordCount);
// add em-space at the beginning of first word in paragraph to indent
if ((style == TextBlock::JUSTIFIED || style == TextBlock::LEFT_ALIGN) && !extraParagraphSpacing) {
// add em-space at the beginning of first word in paragraph to indent (independent of paragraph spacing)
if ((style == TextBlock::JUSTIFIED || style == TextBlock::LEFT_ALIGN) && indentParagraph > 0) {
std::string& first_word = words.front();
first_word.insert(0, "\xe2\x80\x83");
for (uint8_t i = 0; i < indentParagraph; ++i) {
first_word.insert(0, "\xe2\x80\x83"); // em-space
}
}
auto wordsIt = words.begin();

View File

@ -16,7 +16,8 @@ class ParsedText {
std::list<std::string> words;
std::list<EpdFontFamily::Style> wordStyles;
TextBlock::Style style;
bool extraParagraphSpacing;
uint8_t extraParagraphSpacing;
uint8_t indentParagraph;
std::vector<size_t> computeLineBreaks(int pageWidth, int spaceWidth, const std::vector<uint16_t>& wordWidths) const;
void extractLine(size_t breakIndex, int pageWidth, int spaceWidth, const std::vector<uint16_t>& wordWidths,
@ -25,8 +26,8 @@ class ParsedText {
std::vector<uint16_t> calculateWordWidths(const GfxRenderer& renderer, int fontId);
public:
explicit ParsedText(const TextBlock::Style style, const bool extraParagraphSpacing)
: style(style), extraParagraphSpacing(extraParagraphSpacing) {}
explicit ParsedText(const TextBlock::Style style, const uint8_t extraParagraphSpacing, const uint8_t indentParagraph = 1)
: style(style), extraParagraphSpacing(extraParagraphSpacing), indentParagraph(indentParagraph) {}
~ParsedText() = default;
void addWord(std::string word, EpdFontFamily::Style fontStyle);

View File

@ -8,8 +8,8 @@
namespace {
constexpr uint8_t SECTION_FILE_VERSION = 9;
constexpr uint32_t HEADER_SIZE = sizeof(uint8_t) + sizeof(int) + sizeof(float) + sizeof(bool) + sizeof(uint8_t) +
sizeof(uint16_t) + sizeof(uint16_t) + sizeof(uint16_t) + sizeof(uint32_t);
constexpr uint32_t HEADER_SIZE = sizeof(uint8_t) + sizeof(int) + sizeof(float) + sizeof(bool) + sizeof(bool) +
sizeof(uint8_t) + sizeof(uint16_t) + sizeof(uint16_t) + sizeof(uint16_t) + sizeof(uint32_t);
} // namespace
uint32_t Section::onPageComplete(std::unique_ptr<Page> page) {
@ -29,21 +29,22 @@ uint32_t Section::onPageComplete(std::unique_ptr<Page> page) {
return position;
}
void Section::writeSectionFileHeader(const int fontId, const float lineCompression, const bool extraParagraphSpacing,
const uint8_t paragraphAlignment, const uint16_t viewportWidth,
const uint16_t viewportHeight) {
void Section::writeSectionFileHeader(const int fontId, const float lineCompression, const uint8_t extraParagraphSpacing,
const uint8_t indentParagraph, const uint8_t paragraphAlignment,
const uint16_t viewportWidth, const uint16_t viewportHeight) {
if (!file) {
Serial.printf("[%lu] [SCT] File not open for writing header\n", millis());
return;
}
static_assert(HEADER_SIZE == sizeof(SECTION_FILE_VERSION) + sizeof(fontId) + sizeof(lineCompression) +
sizeof(extraParagraphSpacing) + sizeof(paragraphAlignment) + sizeof(viewportWidth) +
sizeof(viewportHeight) + sizeof(pageCount) + sizeof(uint32_t),
sizeof(extraParagraphSpacing) + sizeof(indentParagraph) + sizeof(paragraphAlignment) +
sizeof(viewportWidth) + sizeof(viewportHeight) + sizeof(pageCount) + sizeof(uint32_t),
"Header size mismatch");
serialization::writePod(file, SECTION_FILE_VERSION);
serialization::writePod(file, fontId);
serialization::writePod(file, lineCompression);
serialization::writePod(file, extraParagraphSpacing);
serialization::writePod(file, indentParagraph);
serialization::writePod(file, paragraphAlignment);
serialization::writePod(file, viewportWidth);
serialization::writePod(file, viewportHeight);
@ -51,9 +52,9 @@ void Section::writeSectionFileHeader(const int fontId, const float lineCompressi
serialization::writePod(file, static_cast<uint32_t>(0)); // Placeholder for LUT offset
}
bool Section::loadSectionFile(const int fontId, const float lineCompression, const bool extraParagraphSpacing,
const uint8_t paragraphAlignment, const uint16_t viewportWidth,
const uint16_t viewportHeight) {
bool Section::loadSectionFile(const int fontId, const float lineCompression, const uint8_t extraParagraphSpacing,
const uint8_t indentParagraph, const uint8_t paragraphAlignment,
const uint16_t viewportWidth, const uint16_t viewportHeight) {
if (!SdMan.openFileForRead("SCT", filePath, file)) {
return false;
}
@ -72,18 +73,21 @@ bool Section::loadSectionFile(const int fontId, const float lineCompression, con
int fileFontId;
uint16_t fileViewportWidth, fileViewportHeight;
float fileLineCompression;
bool fileExtraParagraphSpacing;
uint8_t fileExtraParagraphSpacing;
uint8_t fileIndentParagraph;
uint8_t fileParagraphAlignment;
serialization::readPod(file, fileFontId);
serialization::readPod(file, fileLineCompression);
serialization::readPod(file, fileExtraParagraphSpacing);
serialization::readPod(file, fileIndentParagraph);
serialization::readPod(file, fileParagraphAlignment);
serialization::readPod(file, fileViewportWidth);
serialization::readPod(file, fileViewportHeight);
if (fontId != fileFontId || lineCompression != fileLineCompression ||
extraParagraphSpacing != fileExtraParagraphSpacing || paragraphAlignment != fileParagraphAlignment ||
viewportWidth != fileViewportWidth || viewportHeight != fileViewportHeight) {
extraParagraphSpacing != fileExtraParagraphSpacing || indentParagraph != fileIndentParagraph ||
paragraphAlignment != fileParagraphAlignment || viewportWidth != fileViewportWidth ||
viewportHeight != fileViewportHeight) {
file.close();
Serial.printf("[%lu] [SCT] Deserialization failed: Parameters do not match\n", millis());
clearCache();
@ -113,9 +117,10 @@ bool Section::clearCache() const {
return true;
}
bool Section::createSectionFile(const int fontId, const float lineCompression, const bool extraParagraphSpacing,
const uint8_t paragraphAlignment, const uint16_t viewportWidth,
const uint16_t viewportHeight, const std::function<void()>& progressSetupFn,
bool Section::createSectionFile(const int fontId, const float lineCompression, const uint8_t extraParagraphSpacing,
const uint8_t indentParagraph, const uint8_t paragraphAlignment,
const uint16_t viewportWidth, const uint16_t viewportHeight,
const std::function<void()>& progressSetupFn,
const std::function<void(int)>& progressFn) {
constexpr uint32_t MIN_SIZE_FOR_PROGRESS = 50 * 1024; // 50KB
const auto localPath = epub->getSpineItem(spineIndex).href;
@ -171,13 +176,13 @@ bool Section::createSectionFile(const int fontId, const float lineCompression, c
if (!SdMan.openFileForWrite("SCT", filePath, file)) {
return false;
}
writeSectionFileHeader(fontId, lineCompression, extraParagraphSpacing, paragraphAlignment, viewportWidth,
viewportHeight);
writeSectionFileHeader(fontId, lineCompression, extraParagraphSpacing, indentParagraph, paragraphAlignment,
viewportWidth, viewportHeight);
std::vector<uint32_t> lut = {};
ChapterHtmlSlimParser visitor(
tmpHtmlPath, renderer, fontId, lineCompression, extraParagraphSpacing, paragraphAlignment, viewportWidth,
viewportHeight,
tmpHtmlPath, renderer, fontId, lineCompression, extraParagraphSpacing, indentParagraph, paragraphAlignment,
viewportWidth, viewportHeight,
[this, &lut](std::unique_ptr<Page> page) { lut.emplace_back(this->onPageComplete(std::move(page))); },
progressFn);
success = visitor.parseAndBuildPages();

View File

@ -14,26 +14,26 @@ class Section {
std::string filePath;
FsFile file;
void writeSectionFileHeader(int fontId, float lineCompression, bool extraParagraphSpacing, uint8_t paragraphAlignment,
uint16_t viewportWidth, uint16_t viewportHeight);
void writeSectionFileHeader(int fontId, float lineCompression, uint8_t extraParagraphSpacing, uint8_t indentParagraph,
uint8_t paragraphAlignment, uint16_t viewportWidth, uint16_t viewportHeight);
uint32_t onPageComplete(std::unique_ptr<Page> page);
public:
uint16_t pageCount = 0;
int currentPage = 0;
public:
uint16_t pageCount = 0;
int currentPage = 0;
explicit Section(const std::shared_ptr<Epub>& epub, const int spineIndex, GfxRenderer& renderer)
explicit Section(const std::shared_ptr<Epub>& epub, const int spineIndex, GfxRenderer& renderer)
: epub(epub),
spineIndex(spineIndex),
renderer(renderer),
filePath(epub->getCachePath() + "/sections/" + std::to_string(spineIndex) + ".bin") {}
~Section() = default;
bool loadSectionFile(int fontId, float lineCompression, bool extraParagraphSpacing, uint8_t paragraphAlignment,
uint16_t viewportWidth, uint16_t viewportHeight);
bool clearCache() const;
bool createSectionFile(int fontId, float lineCompression, bool extraParagraphSpacing, uint8_t paragraphAlignment,
uint16_t viewportWidth, uint16_t viewportHeight,
const std::function<void()>& progressSetupFn = nullptr,
const std::function<void(int)>& progressFn = nullptr);
~Section() = default;
bool loadSectionFile(int fontId, float lineCompression, uint8_t extraParagraphSpacing, uint8_t indentParagraph,
uint8_t paragraphAlignment, uint16_t viewportWidth, uint16_t viewportHeight);
bool clearCache() const;
bool createSectionFile(int fontId, float lineCompression, uint8_t extraParagraphSpacing, uint8_t indentParagraph,
uint8_t paragraphAlignment, uint16_t viewportWidth, uint16_t viewportHeight,
const std::function<void()>& progressSetupFn = nullptr,
const std::function<void(int)>& progressFn = nullptr);
std::unique_ptr<Page> loadPageFromSectionFile();
};