Reduces heap fragmentation by ~12x fewer allocations per TextBlock. This fixes crashes when repeatedly navigating dictionary pages. - Replace std::list with std::vector in TextBlock members - Replace splice() with move+erase in ParsedText::extractLine() - Use index-based access in hyphenateWordAtIndex()
55 lines
2.5 KiB
C++
55 lines
2.5 KiB
C++
#pragma once
|
|
|
|
#include <EpdFontFamily.h>
|
|
|
|
#include <functional>
|
|
#include <memory>
|
|
#include <string>
|
|
#include <vector>
|
|
|
|
#include "blocks/BlockStyle.h"
|
|
#include "blocks/TextBlock.h"
|
|
|
|
class GfxRenderer;
|
|
|
|
class ParsedText {
|
|
std::vector<std::string> words;
|
|
std::vector<EpdFontFamily::Style> wordStyles;
|
|
std::vector<bool> wordUnderlines; // Track underline per word
|
|
TextBlock::Style style;
|
|
BlockStyle blockStyle;
|
|
bool extraParagraphSpacing;
|
|
bool hyphenationEnabled;
|
|
|
|
void applyParagraphIndent();
|
|
std::vector<size_t> computeLineBreaks(const GfxRenderer& renderer, int fontId, int pageWidth, int spaceWidth,
|
|
std::vector<uint16_t>& wordWidths);
|
|
std::vector<size_t> computeHyphenatedLineBreaks(const GfxRenderer& renderer, int fontId, int pageWidth,
|
|
int spaceWidth, std::vector<uint16_t>& wordWidths);
|
|
bool hyphenateWordAtIndex(size_t wordIndex, int availableWidth, const GfxRenderer& renderer, int fontId,
|
|
std::vector<uint16_t>& wordWidths, bool allowFallbackBreaks);
|
|
void extractLine(size_t breakIndex, int pageWidth, int spaceWidth, const std::vector<uint16_t>& wordWidths,
|
|
const std::vector<size_t>& lineBreakIndices,
|
|
const std::function<void(std::shared_ptr<TextBlock>)>& processLine);
|
|
std::vector<uint16_t> calculateWordWidths(const GfxRenderer& renderer, int fontId);
|
|
|
|
public:
|
|
explicit ParsedText(const TextBlock::Style style, const bool extraParagraphSpacing,
|
|
const bool hyphenationEnabled = false, const BlockStyle& blockStyle = BlockStyle())
|
|
: style(style),
|
|
blockStyle(blockStyle),
|
|
extraParagraphSpacing(extraParagraphSpacing),
|
|
hyphenationEnabled(hyphenationEnabled) {}
|
|
~ParsedText() = default;
|
|
|
|
void addWord(std::string word, EpdFontFamily::Style fontStyle, bool underline = false);
|
|
void setStyle(const TextBlock::Style style) { this->style = style; }
|
|
void setBlockStyle(const BlockStyle& blockStyle) { this->blockStyle = blockStyle; }
|
|
TextBlock::Style getStyle() const { return style; }
|
|
const BlockStyle& getBlockStyle() const { return blockStyle; }
|
|
size_t size() const { return words.size(); }
|
|
bool isEmpty() const { return words.empty(); }
|
|
void layoutAndExtractLines(const GfxRenderer& renderer, int fontId, uint16_t viewportWidth,
|
|
const std::function<void(std::shared_ptr<TextBlock>)>& processLine,
|
|
bool includeLastLine = true);
|
|
}; |