fix page counter

This commit is contained in:
Agel committed 2026-10-30 15:47:53 +01:00
1 parent 511472bc73
commit da8abf0332
13 files changed
+310 -122

No files matched your search

+156
View File
@@ -78,6 +78,39 @@ public:
}
};
class VariableCharsTextReader : public FormatReader {
public:
explicit VariableCharsTextReader(const QVector<int> &charsPerChunk)
{
m_chunks.reserve(charsPerChunk.size());
for (int i = 0; i < charsPerChunk.size(); ++i) {
ContentChunk chunk;
chunk.sectionHref = QStringLiteral("sec_%1.xhtml").arg(i);
chunk.charCount = charsPerChunk[i];
BlockItem p;
p.kind = BlockItem::Kind::Paragraph;
InlineItem t;
t.kind = InlineItem::Kind::Text;
t.text = QString(charsPerChunk[i], QLatin1Char('A'));
p.inlines.append(t);
chunk.blocks.append(p);
m_chunks.append(chunk);
}
}
ReadingMode mode() const override { return ReadingMode::Text; }
bool open(const QString &) override { return true; }
QString lastError() const override { return {}; }
QString title() const override { return QStringLiteral("Norm Reader"); }
QString author() const override { return QStringLiteral("Author"); }
QString filePath() const override { return QStringLiteral("/fake/norm.epub"); }
QString coverImageUrl() const override { return {}; }
QByteArray contentHash() const override { return QByteArray("normhash"); }
bool loadMetadata(const QString &) override { return true; }
QVector<ContentChunk> chunks() const override { return m_chunks; }
private:
QVector<ContentChunk> m_chunks;
};
class ReadingModelTest : public QObject {
Q_OBJECT
@@ -293,6 +326,129 @@ private slots:
const auto layout55 = model.data(model.index(55, 0), ReadingModel::LayoutRole).value<ChunkLayout>();
QVERIFY(!layout55.lines.isEmpty());
}
void normalizedPageCountCalculations()
{
Ctx ctx;
ReadingModel model(*ctx.store);
// 1. Empty book
QCOMPARE(model.totalPages(), 0);
QCOMPARE(model.currentPage(), 0);
QCOMPARE(model.currentChunk(), 0);
// 2. Short book: 500 characters (< 1800) -> 1 page
VariableCharsTextReader shortReader({500});
model.setReader(&shortReader);
QCOMPARE(model.totalPages(), 1);
QCOMPARE(model.currentPage(), 1);
QCOMPARE(model.currentChunk(), 0);
// 3. Exactly 1800 characters -> 1 page
VariableCharsTextReader exactReader({1800});
model.setReader(&exactReader);
QCOMPARE(model.totalPages(), 1);
QCOMPARE(model.currentPage(), 1);
// 4. 1801 characters -> 2 pages
VariableCharsTextReader splitReader({1801});
model.setReader(&splitReader);
QCOMPARE(model.totalPages(), 2);
QCOMPARE(model.currentPage(), 1);
// 5. Multi-chunk book: 5 chunks with 720 chars each (3600 chars total) -> 2 pages
VariableCharsTextReader multiChunk({720, 720, 720, 720, 720});
model.setReader(&multiChunk);
QCOMPARE(model.rowCount(), 5);
QCOMPARE(model.totalPages(), 2);
QCOMPARE(model.currentPage(), 1);
}
void normalizedPageCurrentPageProgression()
{
Ctx ctx;
ReadingModel model(*ctx.store);
// 6 chunks of 900 chars each = 5400 chars = 3 normalized pages (1800 chars/page)
// Chunk 0: 0..900 (Page 1)
// Chunk 1: 900..1800 (Page 1)
// Chunk 2: 1800..2700 (Page 2)
// Chunk 3: 2700..3600 (Page 2)
// Chunk 4: 3600..4500 (Page 3)
// Chunk 5: 4500..5400 (Page 3)
VariableCharsTextReader reader({900, 900, 900, 900, 900, 900});
model.setReader(&reader);
QCOMPARE(model.totalPages(), 3);
QCOMPARE(model.currentPage(), 1);
QCOMPARE(model.currentChunk(), 0);
QSignalSpy pageSpy(&model, &ReadingModel::currentPageChanged);
QSignalSpy chunkSpy(&model, &ReadingModel::currentChunkChanged);
// Scroll to chunk 1: 900 chars scrolled -> still Page 1
model.setScrollPosition(1, 0.0, 0.16);
QCOMPARE(model.currentChunk(), 1);
QCOMPARE(model.currentPage(), 1);
QCOMPARE(chunkSpy.count(), 1);
QCOMPARE(pageSpy.count(), 0);
// Scroll to chunk 2: 1800 chars scrolled -> Page 2
model.setScrollPosition(2, 0.0, 0.33);
QCOMPARE(model.currentChunk(), 2);
QCOMPARE(model.currentPage(), 2);
QCOMPARE(pageSpy.count(), 1);
// Scroll to chunk 3: 2700 chars scrolled -> Page 2
model.setScrollPosition(3, 0.0, 0.50);
QCOMPARE(model.currentChunk(), 3);
QCOMPARE(model.currentPage(), 2);
// Scroll to chunk 4: 3600 chars scrolled -> Page 3
model.setScrollPosition(4, 0.0, 0.67);
QCOMPARE(model.currentChunk(), 4);
QCOMPARE(model.currentPage(), 3);
QCOMPARE(pageSpy.count(), 2);
// Scroll to end of chunk 5: Page 3 (clamped to totalPages)
model.setScrollPosition(5, 100.0, 1.0);
QCOMPARE(model.currentChunk(), 5);
QCOMPARE(model.currentPage(), 3);
}
void viewportGeometryInvariance()
{
Ctx ctx;
ReadingModel model(*ctx.store);
// 10 chunks of 1800 chars each = 18000 chars = 10 normalized pages
QVector<int> chunkSizes(10, 1800);
VariableCharsTextReader reader(chunkSizes);
model.setReader(&reader);
QCOMPARE(model.totalPages(), 10);
QCOMPARE(model.currentPage(), 1);
// Scroll to chunk 3 (Page 4)
model.setScrollPosition(3, 0.0, 0.3);
QCOMPARE(model.currentPage(), 4);
QCOMPARE(model.currentChunk(), 3);
// Simulate resizing viewport width and changing font preferences
model.updateLayoutMetrics(300, 12, 1.0, 3);
QCOMPARE(model.totalPages(), 10);
QCOMPARE(model.currentPage(), 4);
QCOMPARE(model.currentChunk(), 3);
model.updateLayoutMetrics(1400, 28, 1.8, 3);
QCOMPARE(model.totalPages(), 10);
QCOMPARE(model.currentPage(), 4);
QCOMPARE(model.currentChunk(), 3);
model.updateLayoutMetrics(800, 16, 1.2, 3);
QCOMPARE(model.totalPages(), 10);
QCOMPARE(model.currentPage(), 4);
QCOMPARE(model.currentChunk(), 3);
}
};
QTEST_MAIN(ReadingModelTest)
+10 -17
View File
@@ -214,7 +214,7 @@ public:
} else {
book.cover_image_url = std::nullopt;
}
ContentChunkValue fallbackChunk{book.file_path, 1, 0, 0, 0};
ContentChunkValue fallbackChunk{book.file_path, 1, 0, 0};
book.chunks = QVector<ContentChunkValue>{fallbackChunk};
book.binaries = binary_resources;
return true;
@@ -324,19 +324,17 @@ private slots:
// Obligation 5: value-equality.ContentChunkValue
void testValueEqualityContentChunkValue()
{
ContentChunkValue c1{QStringLiteral("sec1"), 5, 100, 500, 2};
ContentChunkValue c2{QStringLiteral("sec1"), 5, 100, 500, 2};
ContentChunkValue cDiffHref{QStringLiteral("sec2"), 5, 100, 500, 2};
ContentChunkValue cDiffBlocks{QStringLiteral("sec1"), 6, 100, 500, 2};
ContentChunkValue cDiffWords{QStringLiteral("sec1"), 5, 101, 500, 2};
ContentChunkValue cDiffChars{QStringLiteral("sec1"), 5, 100, 501, 2};
ContentChunkValue cDiffContainers{QStringLiteral("sec1"), 5, 100, 500, 3};
ContentChunkValue c1{QStringLiteral("sec1"), 5, 500, 2};
ContentChunkValue c2{QStringLiteral("sec1"), 5, 500, 2};
ContentChunkValue cDiffHref{QStringLiteral("sec2"), 5, 500, 2};
ContentChunkValue cDiffBlocks{QStringLiteral("sec1"), 6, 500, 2};
ContentChunkValue cDiffChars{QStringLiteral("sec1"), 5, 501, 2};
ContentChunkValue cDiffContainers{QStringLiteral("sec1"), 5, 500, 3};
QVERIFY(c1 == c2);
QVERIFY(!(c1 != c2));
QVERIFY(c1 != cDiffHref);
QVERIFY(c1 != cDiffBlocks);
QVERIFY(c1 != cDiffWords);
QVERIFY(c1 != cDiffChars);
QVERIFY(c1 != cDiffContainers);
}
@@ -347,19 +345,16 @@ private slots:
ContentChunkValue c;
QVERIFY(c.section_href.isEmpty());
QCOMPARE(c.blocks_count, 0);
QCOMPARE(c.word_count, 0);
QCOMPARE(c.char_count, 0);
QCOMPARE(c.unnested_containers, 0);
c.section_href = QStringLiteral("body/s0");
c.blocks_count = 3;
c.word_count = 50;
c.char_count = 250;
c.unnested_containers = 1;
QCOMPARE(c.section_href, QStringLiteral("body/s0"));
QCOMPARE(c.blocks_count, 3);
QCOMPARE(c.word_count, 50);
QCOMPARE(c.char_count, 250);
QCOMPARE(c.unnested_containers, 1);
}
@@ -391,7 +386,6 @@ private slots:
QCOMPARE(chunks1.size(), chunks2.size());
QVERIFY(!chunks1.isEmpty());
QCOMPARE(chunks1.first().sectionHref, chunks2.first().sectionHref);
QCOMPARE(chunks1.first().wordCount, chunks2.first().wordCount);
QCOMPARE(chunks1.first().charCount, chunks2.first().charCount);
QCOMPARE(chunks1.first().blocks.size(), chunks2.first().blocks.size());
@@ -525,7 +519,7 @@ private slots:
QCOMPARE(s.book.status, Fb2LoadStatus::loading_full);
// Edge 5: loading_full -> loaded
ContentChunkValue chunk{"sec1", 1, 10, 50, 0};
ContentChunkValue chunk{"sec1", 1, 50, 0};
QVERIFY(s.completeFullOpenWithSections(client, "hash", "Title", "stem", {}, "Author", std::nullopt, {}, {chunk}));
QCOMPARE(s.book.status, Fb2LoadStatus::loaded);
}
@@ -755,7 +749,7 @@ private slots:
s.book.file_path = QStringLiteral("/books/novel.fb2");
s.book.status = Fb2LoadStatus::loading_full;
ContentChunkValue chunk{"sec1", 2, 20, 100, 0};
ContentChunkValue chunk{"sec1", 2, 100, 0};
// Failure 1: actor != client
QVERIFY(!s.completeFullOpenWithSections(c2, "hash", "Title", "stem", {}, "", std::nullopt, {}, {chunk}));
@@ -786,7 +780,7 @@ private slots:
s.book.file_path = QStringLiteral("/books/empty.fb2");
s.book.status = Fb2LoadStatus::loading_full;
ContentChunkValue chunk{"sec1", 1, 0, 0, 0};
ContentChunkValue chunk{"sec1", 1, 0, 0};
// Failure 1: actor != client
QVERIFY(!s.completeFullOpenWithEmptyBodyFallback(c2, "hash", "", "empty", {}, "", std::nullopt, {}, {}));
@@ -806,7 +800,6 @@ private slots:
ContentChunkValue fallback = s.book.chunks.value().first();
QCOMPARE(fallback.section_href, QStringLiteral("/books/empty.fb2"));
QCOMPARE(fallback.blocks_count, 1);
QCOMPARE(fallback.word_count, 0);
QCOMPARE(fallback.char_count, 0);
}
+28 -31
View File
@@ -312,6 +312,34 @@ private slots:
QVERIFY(imgTrue.pixelColor(testX, testY).alpha() > 0);
}
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.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 clipRectCullingSkipsOffscreenImage()
{
class TrackingResolver : public IBookImageResolver {
@@ -370,35 +398,6 @@ 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(
@@ -412,12 +411,10 @@ private slots:
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
@@ -218,6 +218,9 @@ public:
QStringLiteral("<p>Content paragraph for chunk index %1 with sufficient words for layout.</p>").arg(i),
QStringLiteral("text/section_%1.xhtml").arg(i)
);
if (!parsed.isEmpty()) {
parsed[0].charCount = kNormalizedCharsPerPage;
}
list.append(parsed);
}
return list;
@@ -746,7 +749,6 @@ private slots:
const auto &chunk = chunks.first();
QCOMPARE(chunk.sectionHref, QStringLiteral("section.xhtml"));
QVERIFY(chunk.wordCount > 0);
QVERIFY(chunk.charCount > 0);
QCOMPARE(chunk.unnestedContainers, 0);
}
@@ -929,7 +931,7 @@ private slots:
model.setScrollPosition(3, 45.0, 0.35);
QCOMPARE(posSpy.count(), 1);
QCOMPARE(model.currentPage(), 4);
QCOMPARE(model.currentChunk(), 3);
QCOMPARE(model.progress(), 0.35);
BookPosition loaded = ctx.store->load(reader.contentHash());
@@ -1157,8 +1159,7 @@ private slots:
model.setReader(&reader);
model.setScrollPosition(5, 0.0, 0.5);
const int page = model.currentPage();
const int anchorChunk = page - 1;
const int anchorChunk = model.currentChunk();
QVERIFY(anchorChunk >= 0);
QVERIFY(anchorChunk < model.rowCount());
}
@@ -1240,7 +1241,7 @@ private slots:
// 2. progress
QCOMPARE(model.progress(), 0.0);
// 3. anchor_chunk
QCOMPARE(model.currentPage() - 1, 0);
QCOMPARE(model.currentChunk(), 0);
// 4. use_book_colours
QCOMPARE(model.useBookColours(), false);
// 5. status
+1 -3
View File
@@ -46,8 +46,7 @@ public:
for (int i = 0; i < m_count; ++i) {
ContentChunk chunk;
chunk.sectionHref = QStringLiteral("text/ch_%1.xhtml").arg(i);
chunk.wordCount = 10;
chunk.charCount = 60;
chunk.charCount = kNormalizedCharsPerPage;
chunk.unnestedContainers = 0;
BlockItem p;
@@ -411,7 +410,6 @@ private slots:
{
ContentChunk chunk;
chunk.sectionHref = QStringLiteral("sec1.xhtml");
chunk.wordCount = 100;
chunk.charCount = 500;
chunk.unnestedContainers = 1;
QVERIFY(chunk.isEmpty());