126 lines
4.6 KiB
C++
126 lines
4.6 KiB
C++
#include <QtTest>
|
|
#include "backend/reading/layout/KnuthPlass.h"
|
|
|
|
class KnuthPlassTest : public QObject {
|
|
Q_OBJECT
|
|
|
|
private slots:
|
|
void emptyItemsReturnsEmpty()
|
|
{
|
|
QVector<KnuthPlass::Item> items;
|
|
auto lines = KnuthPlass::breakLines(items, 100);
|
|
QVERIFY(lines.isEmpty());
|
|
}
|
|
|
|
void singleLineParagraph()
|
|
{
|
|
// "Hello world"
|
|
// Box(30), Glue(10, 5, 3), Box(30), finishParagraph
|
|
QVector<KnuthPlass::Item> items;
|
|
items.append(KnuthPlass::Item::box(30, 0));
|
|
items.append(KnuthPlass::Item::glue(10, 5, 3, -1));
|
|
items.append(KnuthPlass::Item::box(30, 1));
|
|
KnuthPlass::finishParagraph(items);
|
|
|
|
auto lines = KnuthPlass::breakLines(items, 100);
|
|
QCOMPARE(lines.size(), 1);
|
|
QCOMPARE(lines[0].start, 0);
|
|
// The last line of a paragraph has infinite stretch in finishing glue -> ratio is 0
|
|
QCOMPARE(lines[0].ratio, 0.0);
|
|
QVERIFY(!lines[0].hyphenated);
|
|
}
|
|
|
|
void multiLineJustification()
|
|
{
|
|
// 4 words of width 40, glue width 10, stretch 5, shrink 3
|
|
// Line width 95 -> line 1: 2 words (40 + 10 + 40 = 90) + glue needs to stretch by 5 (ratio = 1.0)
|
|
QVector<KnuthPlass::Item> items;
|
|
items.append(KnuthPlass::Item::box(40, 0));
|
|
items.append(KnuthPlass::Item::glue(10, 5, 3, -1));
|
|
items.append(KnuthPlass::Item::box(40, 1));
|
|
items.append(KnuthPlass::Item::glue(10, 5, 3, -1));
|
|
items.append(KnuthPlass::Item::box(40, 2));
|
|
items.append(KnuthPlass::Item::glue(10, 5, 3, -1));
|
|
items.append(KnuthPlass::Item::box(40, 3));
|
|
KnuthPlass::finishParagraph(items);
|
|
|
|
auto lines = KnuthPlass::breakLines(items, 95);
|
|
QCOMPARE(lines.size(), 2);
|
|
QCOMPARE(lines[0].start, 0);
|
|
QCOMPARE(lines[0].ratio, 1.0); // 95 - 90 = 5 stretch, 5 / 5 = 1.0
|
|
QCOMPARE(lines[1].ratio, 0.0); // paragraph end line
|
|
}
|
|
|
|
void softHyphenBreak()
|
|
{
|
|
// Word 1 (width 50), Glue(10), Word 2 part A (30), soft-hyphen penalty (width 8, penalty 50, flagged), Word 2 part B (30)
|
|
// Line width 98: 50 + 10 + 30 + 8 = 98 -> exactly breaks at hyphen!
|
|
QVector<KnuthPlass::Item> items;
|
|
items.append(KnuthPlass::Item::box(50, 0));
|
|
items.append(KnuthPlass::Item::glue(10, 5, 3, -1));
|
|
items.append(KnuthPlass::Item::box(30, 1));
|
|
items.append(KnuthPlass::Item::penaltyItem(8, 50, true, -1));
|
|
items.append(KnuthPlass::Item::box(30, 2));
|
|
KnuthPlass::finishParagraph(items);
|
|
|
|
auto lines = KnuthPlass::breakLines(items, 98);
|
|
QCOMPARE(lines.size(), 2);
|
|
QVERIFY(lines[0].hyphenated);
|
|
QCOMPARE(lines[1].start, 4); // starts at Word 2 part B
|
|
}
|
|
|
|
void overlongLineRescue()
|
|
{
|
|
// A single box wider than target line width should not crash or return empty;
|
|
// it falls back to emergency pass.
|
|
QVector<KnuthPlass::Item> items;
|
|
items.append(KnuthPlass::Item::box(200, 0));
|
|
KnuthPlass::finishParagraph(items);
|
|
|
|
auto lines = KnuthPlass::breakLines(items, 100);
|
|
QCOMPARE(lines.size(), 1);
|
|
}
|
|
|
|
void inlineLineBreakFinishLine()
|
|
{
|
|
// "Line 1" <br/> "Line 2"
|
|
QVector<KnuthPlass::Item> items;
|
|
items.append(KnuthPlass::Item::box(30, 0));
|
|
items.append(KnuthPlass::Item::glue(10, 5, 3, -1));
|
|
items.append(KnuthPlass::Item::box(30, 1));
|
|
KnuthPlass::finishLine(items);
|
|
items.append(KnuthPlass::Item::box(40, 2));
|
|
items.append(KnuthPlass::Item::glue(10, 5, 3, -1));
|
|
items.append(KnuthPlass::Item::box(40, 3));
|
|
KnuthPlass::finishParagraph(items);
|
|
|
|
// Line width 200: both lines are short.
|
|
// Because finishLine inserted infinite stretch glue, line 1 must have ratio 0.0 (like line 2).
|
|
auto lines = KnuthPlass::breakLines(items, 200);
|
|
QCOMPARE(lines.size(), 2);
|
|
QCOMPARE(lines[0].ratio, 0.0);
|
|
QCOMPARE(lines[1].ratio, 0.0);
|
|
}
|
|
|
|
void consecutiveLineBreaks()
|
|
{
|
|
// "Line 1" <br/> <br/> "Line 2"
|
|
QVector<KnuthPlass::Item> items;
|
|
items.append(KnuthPlass::Item::box(30, 0));
|
|
KnuthPlass::finishLine(items);
|
|
KnuthPlass::finishLine(items); // consecutive break with empty line
|
|
items.append(KnuthPlass::Item::box(40, 1));
|
|
KnuthPlass::finishParagraph(items);
|
|
|
|
auto lines = KnuthPlass::breakLines(items, 200);
|
|
QCOMPARE(lines.size(), 3);
|
|
QCOMPARE(lines[0].ratio, 0.0);
|
|
QCOMPARE(lines[1].ratio, 0.0);
|
|
QCOMPARE(lines[2].ratio, 0.0);
|
|
}
|
|
};
|
|
|
|
QTEST_MAIN(KnuthPlassTest)
|
|
#include "KnuthPlassTest.moc"
|
|
|