more performance optimizations + cleanup
This commit is contained in:
15 files changed
+396
-469
No files matched your search
+16
-12
@@ -41,13 +41,13 @@ public:
|
||||
QByteArray contentHash() const override { return QByteArray("texthash"); }
|
||||
bool loadMetadata(const QString &) override { return true; }
|
||||
|
||||
QStringList chunks() const override
|
||||
QVector<ContentChunk> chunks() const override
|
||||
{
|
||||
return {
|
||||
QStringLiteral("<p>Chunk 1</p>"),
|
||||
QStringLiteral("<p>Chunk 2</p>"),
|
||||
QStringLiteral("<p>Chunk 3</p>")
|
||||
};
|
||||
return HtmlBlockParser::parseChunks(
|
||||
QStringLiteral("<p>Chunk 1</p><p>Chunk 2</p><p>Chunk 3</p>"),
|
||||
QStringLiteral("text/ch1.xhtml"),
|
||||
1 // targetChars = 1 forces each paragraph into its own chunk
|
||||
);
|
||||
}
|
||||
};
|
||||
|
||||
@@ -63,12 +63,16 @@ public:
|
||||
QByteArray contentHash() const override { return QByteArray("manychunks"); }
|
||||
bool loadMetadata(const QString &) override { return true; }
|
||||
|
||||
QStringList chunks() const override
|
||||
QVector<ContentChunk> chunks() const override
|
||||
{
|
||||
QStringList list;
|
||||
QVector<ContentChunk> list;
|
||||
list.reserve(60);
|
||||
for (int i = 0; i < 60; ++i) {
|
||||
list.append(QStringLiteral("<p>Paragraph chunk number %1 with some text content.</p>").arg(i));
|
||||
auto parsed = HtmlBlockParser::parseChunks(
|
||||
QStringLiteral("<p>Paragraph chunk number %1 with some text content.</p>").arg(i),
|
||||
QStringLiteral("text/ch1.xhtml")
|
||||
);
|
||||
list.append(parsed);
|
||||
}
|
||||
return list;
|
||||
}
|
||||
@@ -200,11 +204,11 @@ private slots:
|
||||
QVERIFY(model.isTextMode());
|
||||
QCOMPARE(model.rowCount(), 3);
|
||||
QCOMPARE(model.data(model.index(0, 0), ReadingModel::ContentRole).toString(),
|
||||
QStringLiteral("<p>Chunk 1</p>"));
|
||||
QStringLiteral("text/ch1.xhtml"));
|
||||
QCOMPARE(model.data(model.index(1, 0), ReadingModel::ContentRole).toString(),
|
||||
QStringLiteral("<p>Chunk 2</p>"));
|
||||
QStringLiteral("text/ch1.xhtml"));
|
||||
QCOMPARE(model.data(model.index(2, 0), ReadingModel::ContentRole).toString(),
|
||||
QStringLiteral("<p>Chunk 3</p>"));
|
||||
QStringLiteral("text/ch1.xhtml"));
|
||||
|
||||
QTRY_COMPARE(restoreSpy.count(), 1);
|
||||
|
||||
|
||||
@@ -123,14 +123,33 @@ private slots:
|
||||
|
||||
QVERIFY(!reader.chunks().isEmpty());
|
||||
|
||||
QString fullHtml = reader.chunks().join(QString());
|
||||
// Verify CSS styling inlined
|
||||
QVERIFY(fullHtml.contains(QStringLiteral("color:red")) || fullHtml.contains(QStringLiteral("italic")));
|
||||
// Verify embedded image resolved to image://epub/ with source path
|
||||
QVERIFY(fullHtml.contains(QStringLiteral("image://epub/OEBPS/images/fig1.png")));
|
||||
QVERIFY(fullHtml.contains(QStringLiteral("src=%2Fvirtual%2Fbook.epub")) || fullHtml.contains(QStringLiteral("src=/virtual/book.epub")));
|
||||
// Verify web image resolved to image://web/
|
||||
QVERIFY(fullHtml.contains(QStringLiteral("image://web/https://example.com/pic.jpg")));
|
||||
bool foundColorOrItalic = false;
|
||||
bool foundEpubImage = false;
|
||||
bool foundWebImage = false;
|
||||
|
||||
for (const auto &chunk : reader.chunks()) {
|
||||
QCOMPARE(chunk.sectionHref, QStringLiteral("OEBPS/text/ch1.xhtml"));
|
||||
for (const auto &b : chunk.blocks) {
|
||||
for (const auto &inl : b.inlines) {
|
||||
if (inl.style.color == QColor(Qt::red) || inl.style.italic) {
|
||||
foundColorOrItalic = true;
|
||||
}
|
||||
if (inl.kind == InlineItem::Kind::Image) {
|
||||
if (inl.imageUrl.contains(QStringLiteral("image://epub/OEBPS/images/fig1.png"))
|
||||
&& (inl.imageUrl.contains(QStringLiteral("src=%2Fvirtual%2Fbook.epub")) || inl.imageUrl.contains(QStringLiteral("src=/virtual/book.epub")))) {
|
||||
foundEpubImage = true;
|
||||
}
|
||||
if (inl.imageUrl.contains(QStringLiteral("image://web/https://example.com/pic.jpg"))) {
|
||||
foundWebImage = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
QVERIFY(foundColorOrItalic);
|
||||
QVERIFY(foundEpubImage);
|
||||
QVERIFY(foundWebImage);
|
||||
}
|
||||
|
||||
void openEpub3WithNavDocument()
|
||||
@@ -187,7 +206,17 @@ private slots:
|
||||
QStringLiteral("EPUB/cover.png"),
|
||||
QStringLiteral("/virtual/epub3.epub")));
|
||||
QVERIFY(!reader.chunks().isEmpty());
|
||||
QVERIFY(reader.chunks().first().contains(QStringLiteral("Hello EPUB 3")));
|
||||
bool foundHello = false;
|
||||
for (const auto &chunk : reader.chunks()) {
|
||||
QCOMPARE(chunk.sectionHref, QStringLiteral("EPUB/ch1.xhtml"));
|
||||
for (const auto &b : chunk.blocks) {
|
||||
for (const auto &inl : b.inlines) {
|
||||
if (inl.text.contains(QStringLiteral("Hello EPUB 3")))
|
||||
foundHello = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
QVERIFY(foundHello);
|
||||
}
|
||||
|
||||
void chapterWrapperUnwrappedIntoGranularChunks()
|
||||
@@ -239,8 +268,13 @@ private slots:
|
||||
// Must be split into multiple granular chunks instead of one single monolithic chunk
|
||||
QVERIFY(reader.chunks().size() >= 3);
|
||||
// Children must inherit text-align: justify
|
||||
for (const QString &c : reader.chunks()) {
|
||||
QVERIFY(c.contains(QStringLiteral("text-align:justify")) || c.contains(QStringLiteral("text-align: justify")));
|
||||
for (const auto &chunk : reader.chunks()) {
|
||||
QCOMPARE(chunk.sectionHref, QStringLiteral("EPUB/ch1.xhtml"));
|
||||
for (const auto &b : chunk.blocks) {
|
||||
if (b.kind == BlockItem::Kind::Paragraph) {
|
||||
QCOMPARE(b.align, TextAlign::Justify);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -290,17 +324,27 @@ private slots:
|
||||
bool foundOdnoglazyiTogether = false;
|
||||
bool foundThoughtTogether = false;
|
||||
|
||||
for (const QString &chunk : reader.chunks()) {
|
||||
if (chunk.contains(QStringLiteral("Одноглазый"))) {
|
||||
// Must have both opening and closing quotes together in the same chunk
|
||||
QVERIFY(chunk.contains(QStringLiteral("«")));
|
||||
QVERIFY(chunk.contains(QStringLiteral("»")));
|
||||
foundOdnoglazyiTogether = true;
|
||||
}
|
||||
if (chunk.contains(QStringLiteral("Когда я умру"))) {
|
||||
// Must have the attribution text in the same chunk
|
||||
QVERIFY(chunk.contains(QStringLiteral("эта мысль странным образом утешала")));
|
||||
foundThoughtTogether = true;
|
||||
for (const auto &chunk : reader.chunks()) {
|
||||
for (const auto &b : chunk.blocks) {
|
||||
if (b.kind != BlockItem::Kind::Paragraph)
|
||||
continue;
|
||||
|
||||
QString paraText;
|
||||
for (const auto &inl : b.inlines) {
|
||||
paraText += inl.text;
|
||||
}
|
||||
|
||||
if (paraText.contains(QStringLiteral("Одноглазый"))) {
|
||||
// Must have both opening and closing quotes together in the same paragraph
|
||||
QVERIFY(paraText.contains(QStringLiteral("«")));
|
||||
QVERIFY(paraText.contains(QStringLiteral("»")));
|
||||
foundOdnoglazyiTogether = true;
|
||||
}
|
||||
if (paraText.contains(QStringLiteral("Когда я умру"))) {
|
||||
// Must have the attribution text in the same paragraph
|
||||
QVERIFY(paraText.contains(QStringLiteral("эта мысль странным образом утешала")));
|
||||
foundThoughtTogether = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -369,6 +369,71 @@ private slots:
|
||||
|
||||
KnuthPlassItem::setImageResolver(nullptr);
|
||||
}
|
||||
|
||||
void contentChunkFontSizeScaling()
|
||||
{
|
||||
const QString html = QStringLiteral("<p>Testing dynamic font scaling directly on structured ContentChunk without re-parsing XML.</p>");
|
||||
auto chunks = HtmlBlockParser::parseChunks(html, QStringLiteral("section.xhtml"));
|
||||
QCOMPARE(chunks.size(), 1);
|
||||
const auto &chunk = chunks.first();
|
||||
|
||||
QVERIFY(chunk.wordCount > 5);
|
||||
QVERIFY(chunk.charCount > 20);
|
||||
|
||||
LayoutParams p16;
|
||||
p16.contentWidth = 400;
|
||||
p16.fontSize = 16;
|
||||
p16.lineSpacing = 1.0;
|
||||
auto prep16 = ChunkLayoutEngine::prepareChunk(chunk, 16);
|
||||
auto l16 = ChunkLayoutEngine::layoutPreparedChunk(prep16, p16);
|
||||
|
||||
LayoutParams p24;
|
||||
p24.contentWidth = 400;
|
||||
p24.fontSize = 24;
|
||||
p24.lineSpacing = 1.0;
|
||||
auto prep24 = ChunkLayoutEngine::prepareChunk(chunk, 24);
|
||||
auto l24 = ChunkLayoutEngine::layoutPreparedChunk(prep24, p24);
|
||||
|
||||
QVERIFY(l24.totalHeight > l16.totalHeight);
|
||||
QVERIFY(!l24.lines.isEmpty());
|
||||
QVERIFY(l24.lines[0].runs[0].font.pixelSize() > l16.lines[0].runs[0].font.pixelSize());
|
||||
}
|
||||
|
||||
void contentChunkPartitionAndUnnestedContainers()
|
||||
{
|
||||
const QString html = QStringLiteral(
|
||||
"<blockquote>"
|
||||
"<p>First paragraph inside a blockquote designed to be split across chunk boundaries cleanly.</p>"
|
||||
"<p>Second paragraph inside the same blockquote continuing the quote.</p>"
|
||||
"</blockquote>"
|
||||
);
|
||||
auto chunks = HtmlBlockParser::parseChunks(html, QStringLiteral("section1.xhtml"), 60);
|
||||
QVERIFY(chunks.size() >= 2);
|
||||
|
||||
QCOMPARE(chunks[0].sectionHref, QStringLiteral("section1.xhtml"));
|
||||
QCOMPARE(chunks[0].unnestedContainers, 0);
|
||||
QVERIFY(chunks[0].wordCount > 0);
|
||||
QVERIFY(chunks[0].charCount > 0);
|
||||
|
||||
QCOMPARE(chunks[1].sectionHref, QStringLiteral("section1.xhtml"));
|
||||
QCOMPARE(chunks[1].unnestedContainers, 1); // Carries the open blockquote container
|
||||
QVERIFY(chunks[1].wordCount > 0);
|
||||
QVERIFY(chunks[1].charCount > 0);
|
||||
|
||||
// Layout both chunks and verify decorations
|
||||
LayoutParams params;
|
||||
params.contentWidth = 400;
|
||||
params.fontSize = 16;
|
||||
params.lineSpacing = 1.0;
|
||||
|
||||
auto l1 = ChunkLayoutEngine::layoutPreparedChunk(ChunkLayoutEngine::prepareChunk(chunks[0], 16), params);
|
||||
auto l2 = ChunkLayoutEngine::layoutPreparedChunk(ChunkLayoutEngine::prepareChunk(chunks[1], 16), params);
|
||||
|
||||
QVERIFY(!l1.decorations.isEmpty());
|
||||
QVERIFY(l1.decorations[0].borderLeft > 0);
|
||||
QVERIFY(!l2.decorations.isEmpty());
|
||||
QVERIFY(l2.decorations[0].borderLeft > 0);
|
||||
}
|
||||
};
|
||||
|
||||
QTEST_MAIN(ChunkLayoutEngineTest)
|
||||
|
||||
Reference in new issue
Block a user