visual fix
This commit is contained in:
20 files changed
+669
-32
No files matched your search
@@ -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)
|
||||
|
||||
@@ -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(
|
||||
|
||||
Reference in new issue
Block a user