visual fix

This commit is contained in:
Agel committed 2026-10-25 22:12:32 +01:00
1 parent 00c88a940c
commit 905c61da52
20 files changed
+669 -32

No files matched your search

+36
View File
@@ -207,6 +207,42 @@ private slots:
QVERIFY(!model.isTextMode());
QCOMPARE(model.rowCount(), 0);
}
void useBookColoursToggleAndPersistence()
{
Ctx ctx;
ReadingModel model(*ctx.store);
FakeTextReader reader;
QCOMPARE(model.useBookColours(), false);
// Pre-save position with useBookColours = true for reader
BookPosition savedPos;
savedPos.chunkIndex = 1;
savedPos.useBookColours = true;
ctx.store->save(reader.contentHash(), savedPos);
QSignalSpy spy(&model, &ReadingModel::useBookColoursChanged);
model.setReader(&reader);
QCOMPARE(model.useBookColours(), true);
QVERIFY(spy.count() >= 1);
// Toggle to false
spy.clear();
model.setUseBookColours(false);
QCOMPARE(model.useBookColours(), false);
QCOMPARE(spy.count(), 1);
// Verify it was persisted to store
BookPosition loaded = ctx.store->load(reader.contentHash());
QCOMPARE(loaded.useBookColours, false);
// Setting reader to null resets useBookColours to false
model.setUseBookColours(true);
QCOMPARE(model.useBookColours(), true);
model.setReader(nullptr);
QCOMPARE(model.useBookColours(), false);
}
};
QTEST_MAIN(ReadingModelTest)
+146
View File
@@ -1,5 +1,6 @@
#include <QtTest>
#include "backend/reading/BookImageResolver.h"
#include "backend/reading/KnuthPlassItem.h"
#include "backend/reading/layout/ChunkLayoutEngine.h"
class MockImageResolver : public IBookImageResolver {
@@ -165,6 +166,151 @@ private slots:
QCOMPARE(l1.contentWidth, 300.0);
QCOMPARE(l2.contentWidth, 600.0);
}
void calibreHeaderLayout()
{
const QString html = QStringLiteral(
"<h1 style=\"background-color: #e7e7e7; border: 1px solid black; font-size: 1.375em; text-align: left;\">"
"<a id=\"TOC_1\"></a>СЛУГА КУПЦА <br class=\"calibre9\"/></h1>"
);
LayoutParams params;
params.contentWidth = 600;
params.fontSize = 16;
params.lineSpacing = 1.2;
auto layout = ChunkLayoutEngine::layoutChunk(html, params);
// Should have exactly 1 block decoration for the h1 box
QCOMPARE(layout.decorations.size(), 1);
const auto &dec = layout.decorations[0];
QCOMPARE(dec.background, QColor(0xe7, 0xe7, 0xe7));
QCOMPARE(dec.borderColor, QColor(Qt::black));
QCOMPARE(dec.borderLeft, 1.0);
QCOMPARE(dec.borderRight, 1.0);
QCOMPARE(dec.borderTop, 1.0);
QCOMPARE(dec.borderBottom, 1.0);
// Trailing <br/> must be stripped and not produce a spurious empty second line
QCOMPARE(layout.lines.size(), 1);
QVERIFY(!layout.lines[0].runs.isEmpty());
// Words must NOT have individual highlight backgrounds
for (const auto &run : layout.lines[0].runs) {
QVERIFY(!run.background.isValid());
// But they should know the enclosing box background
QCOMPARE(run.surfaceBackground, QColor(0xe7, 0xe7, 0xe7));
}
}
void inlineBackgrounds()
{
const QString html = QStringLiteral("<p>Normal <span style=\"background-color: yellow;\">highlighted</span> text</p>");
LayoutParams params;
params.contentWidth = 600;
params.fontSize = 16;
params.lineSpacing = 1.2;
auto layout = ChunkLayoutEngine::layoutChunk(html, params);
// No container box decorations for an inline span
QCOMPARE(layout.decorations.size(), 0);
QVERIFY(!layout.lines.isEmpty());
bool foundYellow = false;
bool foundNormal = false;
for (const auto &run : layout.lines[0].runs) {
if (run.text.contains(QLatin1String("highlighted"))) {
QCOMPARE(run.background, QColor(Qt::yellow));
foundYellow = true;
} else if (run.text.contains(QLatin1String("Normal"))) {
QVERIFY(!run.background.isValid());
foundNormal = true;
}
}
QVERIFY(foundYellow);
QVERIFY(foundNormal);
}
void nestedBackgroundContainer()
{
const QString html = QStringLiteral("<div style=\"background-color: #ff0000;\"><p>Inner text</p></div>");
LayoutParams params;
params.contentWidth = 600;
params.fontSize = 16;
params.lineSpacing = 1.2;
auto layout = ChunkLayoutEngine::layoutChunk(html, params);
QCOMPARE(layout.decorations.size(), 1);
QCOMPARE(layout.decorations[0].background, QColor(0xff, 0, 0));
QVERIFY(!layout.lines.isEmpty());
QVERIFY(!layout.lines[0].runs.isEmpty());
// Enclosing background is inherited as surfaceBackground
QCOMPARE(layout.lines[0].runs[0].surfaceBackground, QColor(0xff, 0, 0));
// But not as run.background
QVERIFY(!layout.lines[0].runs[0].background.isValid());
}
void contrastRatioHelper()
{
QColor lightBg(0xe7, 0xe7, 0xe7);
QColor darkBg(0x19, 0x1d, 0x35);
qreal crLightBlack = KnuthPlassItem::contrastRatio(Qt::black, lightBg);
qreal crLightWhite = KnuthPlassItem::contrastRatio(Qt::white, lightBg);
QVERIFY(crLightBlack > 10.0);
QVERIFY(crLightWhite < 2.0);
qreal crDarkBlack = KnuthPlassItem::contrastRatio(Qt::black, darkBg);
qreal crDarkWhite = KnuthPlassItem::contrastRatio(Qt::white, darkBg);
QVERIFY(crDarkWhite > 10.0);
QVERIFY(crDarkBlack < 2.0);
}
void knuthPlassItemPaintBoxSuppression()
{
const QString html = QStringLiteral(
"<h1 style=\"background-color: #e7e7e7; border: 2px solid black;\">Header Title</h1>"
);
LayoutParams params;
params.contentWidth = 400;
params.fontSize = 16;
params.lineSpacing = 1.0;
auto layout = ChunkLayoutEngine::layoutChunk(html, params);
QCOMPARE(layout.decorations.size(), 1);
KnuthPlassItem item;
item.setSize(QSizeF(400, 200));
item.setLayout(QVariant::fromValue(layout));
// When useBookColours is false:
item.setUseBookColours(false);
QImage imgFalse(400, 200, QImage::Format_ARGB32_Premultiplied);
imgFalse.fill(Qt::transparent);
{
QPainter p(&imgFalse);
item.paint(&p);
}
// When useBookColours is true:
item.setUseBookColours(true);
QImage imgTrue(400, 200, QImage::Format_ARGB32_Premultiplied);
imgTrue.fill(Qt::transparent);
{
QPainter p(&imgTrue);
item.paint(&p);
}
const auto &dec = layout.decorations[0];
int testX = int(dec.rect.left());
int testY = int(dec.rect.top());
// With useBookColours = false, (testX, testY) should be transparent (no background, no border box)
QCOMPARE(imgFalse.pixelColor(testX, testY).alpha(), 0);
// With useBookColours = true, (testX, testY) is drawn with border or background
QVERIFY(imgTrue.pixelColor(testX, testY).alpha() > 0);
}
};
QTEST_MAIN(ChunkLayoutEngineTest)
+38
View File
@@ -53,6 +53,44 @@ private slots:
QCOMPARE(c5, QColor(Qt::red));
}
void parseBorders()
{
Css::LengthContext ctx;
ctx.em = 16.0;
ctx.rem = 16.0;
ctx.percentBase = 500.0;
ctx.absoluteScale = 1.0;
auto b1 = Css::parseBorderSide(QStringLiteral("1px solid Black"), ctx);
QVERIFY(b1.width.has_value());
QCOMPARE(*b1.width, 1.0);
QCOMPARE(b1.color, QColor(Qt::black));
QVERIFY(!b1.isNone);
auto b2 = Css::parseBorderSide(QStringLiteral("none"), ctx);
QVERIFY(b2.width.has_value());
QCOMPARE(*b2.width, 0.0);
QVERIFY(b2.isNone);
auto b3 = Css::parseBorderSide(QStringLiteral("thin dotted #cccccc"), ctx);
QVERIFY(b3.width.has_value());
QCOMPARE(*b3.width, 1.0);
QCOMPARE(b3.color, QColor(0xcc, 0xcc, 0xcc));
auto b4 = Css::parseBorderSide(QStringLiteral("2px"), ctx);
QVERIFY(b4.width.has_value());
QCOMPARE(*b4.width, 2.0);
auto b5 = Css::parseBorderSide(QStringLiteral("solid red"), ctx);
QVERIFY(b5.width.has_value());
QCOMPARE(*b5.width, 3.0); // Default medium width when style is present
QCOMPARE(b5.color, QColor(Qt::red));
auto b6 = Css::parseBorderSide(QStringLiteral("1px inset"), ctx);
QVERIFY(b6.width.has_value());
QCOMPARE(*b6.width, 1.0);
}
void stylesheetParsingAndCascade()
{
const QString css = QStringLiteral(
+32
View File
@@ -25,6 +25,7 @@ private slots:
QCOMPARE(pos.progress, 0.0);
QCOMPARE(pos.pageShift, true);
QCOMPARE(pos.mangaRTL, true);
QCOMPARE(pos.useBookColours, false);
}
void saveAndLoad()
@@ -119,6 +120,37 @@ private slots:
const BookPosition pos = store.load(QByteArray());
QCOMPARE(pos.chunkIndex, 0);
}
void setUseBookColoursPreservesPosition()
{
QTemporaryFile f;
f.setFileTemplate(QDir::tempPath() + QStringLiteral("/bps-ubc-XXXXXX.ini"));
(void)f.open(); f.close();
QSettings s(f.fileName(), QSettings::IniFormat);
AppState appState(s);
BookPositionStore store(appState);
const QByteArray hash = QByteArray("ubc-test-hash");
BookPosition p;
p.chunkIndex = 12;
p.intraOffset = 45.5;
p.progress = 0.35;
p.pageShift = true;
p.mangaRTL = false;
p.useBookColours = false;
store.save(hash, p);
// Update only useBookColours
store.setUseBookColours(hash, true);
const BookPosition updated = store.load(hash);
QCOMPARE(updated.useBookColours, true);
QCOMPARE(updated.chunkIndex, 12);
QCOMPARE(updated.intraOffset, 45.5);
QCOMPARE(updated.progress, 0.35);
QCOMPARE(updated.pageShift, true);
QCOMPARE(updated.mangaRTL, false);
}
};
QTEST_MAIN(BookPositionStoreTest)