#include #include "backend/reading/layout/KnuthPlass.h" class KnuthPlassTest : public QObject { Q_OBJECT private slots: void emptyItemsReturnsEmpty() { QVector items; auto lines = KnuthPlass::breakLines(items, 100); QVERIFY(lines.isEmpty()); } void singleLineParagraph() { // "Hello world" // Box(30), Glue(10, 5, 3), Box(30), finishParagraph QVector 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 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 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 items; items.append(KnuthPlass::Item::box(200, 0)); KnuthPlass::finishParagraph(items); auto lines = KnuthPlass::breakLines(items, 100); QCOMPARE(lines.size(), 1); } }; QTEST_MAIN(KnuthPlassTest) #include "KnuthPlassTest.moc"