| 1 |
// SPDX-FileCopyrightText: 2008 Dominik Seichter <[email protected]> |
| 2 |
// SPDX-FileCopyrightText: 2021 Francesco Pretto <[email protected]> |
| 3 |
// SPDX-License-Identifier: MIT-0 |
| 4 |
|
| 5 |
#include <PdfTest.h> |
| 6 |
|
| 7 |
#define TEST_PAGE_KEY "TestPageNumber"_n |
| 8 |
constexpr unsigned TEST_NUM_PAGES = 100; |
| 9 |
|
| 10 |
namespace PoDoFo |
| 11 |
{ |
| 12 |
class PdfPageTest |
| 13 |
{ |
| 14 |
public: |
| 15 |
static std::vector<std::unique_ptr<PdfPage>> CreateSamplePages(PdfMemDocument& doc, unsigned pageCount); |
| 16 |
static PdfMemDocument CreateTestTreeCustom(); |
| 17 |
}; |
| 18 |
} |
| 19 |
|
| 20 |
using namespace std; |
| 21 |
using namespace PoDoFo; |
| 22 |
|
| 23 |
static bool isPageNumber(PdfPage& page, unsigned number); |
| 24 |
static void createTestTree(PdfMemDocument& doc); |
| 25 |
static void testGetPages(PdfMemDocument& doc); |
| 26 |
static void testInsert(PdfMemDocument& doc); |
| 27 |
static void testDeleteAll(PdfMemDocument& doc); |
| 28 |
static void testGetPagesReverse(PdfMemDocument& doc); |
| 29 |
|
| 30 |
TEST_CASE("TestEmptyDoc") |
| 31 |
{ |
| 32 |
PdfMemDocument doc; |
| 33 |
|
| 34 |
// Empty document must have page count == 0 |
| 35 |
REQUIRE(doc.GetPages().GetCount() == 0); |
| 36 |
|
| 37 |
// Retrieving any page from an empty document must be NULL |
| 38 |
ASSERT_THROW_WITH_ERROR_CODE(doc.GetPages().GetPageAt(0), PdfErrorCode::ValueOutOfRange); |
| 39 |
} |
| 40 |
|
| 41 |
TEST_CASE("TestCreateDelete") |
| 42 |
{ |
| 43 |
PdfMemDocument doc; |
| 44 |
PdfPainter painter; |
| 45 |
|
| 46 |
// create font |
| 47 |
auto font = doc.GetFonts().SearchFont("LiberationSans"); |
| 48 |
if (font == nullptr) |
| 49 |
FAIL("Could not find Arial font"); |
| 50 |
|
| 51 |
{ |
| 52 |
// write 1. page |
| 53 |
auto& page = doc.GetPages().CreatePage(PdfPageSize::A4); |
| 54 |
painter.SetCanvas(page); |
| 55 |
painter.TextState.SetFont(*font, 16.0); |
| 56 |
painter.DrawText("Page 1", 200, 200); |
| 57 |
painter.FinishDrawing(); |
| 58 |
REQUIRE(doc.GetPages().GetCount() == 1); |
| 59 |
} |
| 60 |
|
| 61 |
{ |
| 62 |
// write 2. page |
| 63 |
auto& page = doc.GetPages().CreatePage(PdfPageSize::A4); |
| 64 |
painter.SetCanvas(page); |
| 65 |
painter.TextState.SetFont(*font, 16.0); |
| 66 |
painter.DrawText("Page 2", 200, 200); |
| 67 |
painter.FinishDrawing(); |
| 68 |
REQUIRE(doc.GetPages().GetCount() == 2); |
| 69 |
} |
| 70 |
|
| 71 |
// try to delete second page, index is 0 based |
| 72 |
doc.GetPages().RemovePageAt(1); |
| 73 |
REQUIRE(doc.GetPages().GetCount() == 1); |
| 74 |
|
| 75 |
{ |
| 76 |
// write 3. page |
| 77 |
auto& page = doc.GetPages().CreatePage(PdfPageSize::A4); |
| 78 |
painter.SetCanvas(page); |
| 79 |
painter.TextState.SetFont(*font, 16.0); |
| 80 |
painter.DrawText("Page 3", 200, 200); |
| 81 |
painter.FinishDrawing(); |
| 82 |
REQUIRE(doc.GetPages().GetCount() == 2); |
| 83 |
} |
| 84 |
} |
| 85 |
|
| 86 |
TEST_CASE("TestGetPagesCustom") |
| 87 |
{ |
| 88 |
auto doc = PdfPageTest::CreateTestTreeCustom(); |
| 89 |
testGetPages(doc); |
| 90 |
} |
| 91 |
|
| 92 |
TEST_CASE("TestGetPages") |
| 93 |
{ |
| 94 |
PdfMemDocument doc; |
| 95 |
createTestTree(doc); |
| 96 |
testGetPages(doc); |
| 97 |
} |
| 98 |
|
| 99 |
TEST_CASE("TestGetPagesReverseCustom") |
| 100 |
{ |
| 101 |
auto doc = PdfPageTest::CreateTestTreeCustom(); |
| 102 |
testGetPagesReverse(doc); |
| 103 |
} |
| 104 |
|
| 105 |
TEST_CASE("TestGetPagesReverse") |
| 106 |
{ |
| 107 |
PdfMemDocument doc; |
| 108 |
createTestTree(doc); |
| 109 |
testGetPagesReverse(doc); |
| 110 |
} |
| 111 |
|
| 112 |
TEST_CASE("TestInsertCustom") |
| 113 |
{ |
| 114 |
auto doc = PdfPageTest::CreateTestTreeCustom(); |
| 115 |
testInsert(doc); |
| 116 |
} |
| 117 |
|
| 118 |
TEST_CASE("TestInsert") |
| 119 |
{ |
| 120 |
PdfMemDocument doc; |
| 121 |
createTestTree(doc); |
| 122 |
testInsert(doc); |
| 123 |
} |
| 124 |
|
| 125 |
TEST_CASE("TestDeleteAllCustom") |
| 126 |
{ |
| 127 |
auto doc = PdfPageTest::CreateTestTreeCustom(); |
| 128 |
testDeleteAll(doc); |
| 129 |
} |
| 130 |
|
| 131 |
TEST_CASE("TestDeleteAll") |
| 132 |
{ |
| 133 |
PdfMemDocument doc; |
| 134 |
createTestTree(doc); |
| 135 |
testDeleteAll(doc); |
| 136 |
} |
| 137 |
|
| 138 |
TEST_CASE("TestMovePage1") |
| 139 |
{ |
| 140 |
PdfMemDocument doc; |
| 141 |
doc.Load(TestUtils::GetTestInputFilePath("TechDocs", "pdf_implementation.pdf")); |
| 142 |
|
| 143 |
vector<PdfReference> refs = { |
| 144 |
PdfReference(15, 0), |
| 145 |
PdfReference(82, 0), |
| 146 |
PdfReference(88, 0), |
| 147 |
PdfReference(83, 0), |
| 148 |
PdfReference(84, 0), |
| 149 |
PdfReference(85, 0), |
| 150 |
PdfReference(86, 0), |
| 151 |
PdfReference(87, 0), |
| 152 |
PdfReference(89, 0), |
| 153 |
PdfReference(90, 0), |
| 154 |
PdfReference(91, 0), |
| 155 |
}; |
| 156 |
|
| 157 |
{ |
| 158 |
auto& pages = doc.GetPages(); |
| 159 |
|
| 160 |
{ |
| 161 |
auto& page = pages.GetPageAt(7); |
| 162 |
REQUIRE(page.GetIndex() == 7); |
| 163 |
REQUIRE(!page.MoveTo(11)); |
| 164 |
REQUIRE(page.MoveTo(2)); |
| 165 |
} |
| 166 |
|
| 167 |
for (unsigned i = 0; i < pages.GetCount(); i++) |
| 168 |
{ |
| 169 |
auto& page = pages.GetPageAt(i); |
| 170 |
REQUIRE(page.GetIndex() == i); |
| 171 |
REQUIRE(page.GetObject().GetIndirectReference() == refs[i]); |
| 172 |
} |
| 173 |
} |
| 174 |
|
| 175 |
string filename = TestUtils::GetTestOutputFilePath("TestMovePage1.pdf"); |
| 176 |
doc.Save(filename); |
| 177 |
|
| 178 |
// Re-load the file to check again the references |
| 179 |
doc.Load(filename); |
| 180 |
auto& pages = doc.GetPages(); |
| 181 |
for (unsigned i = 0; i < pages.GetCount(); i++) |
| 182 |
REQUIRE(pages.GetPageAt(i).GetObject().GetIndirectReference() == refs[i]); |
| 183 |
} |
| 184 |
|
| 185 |
TEST_CASE("TestMovePage2") |
| 186 |
{ |
| 187 |
PdfMemDocument doc; |
| 188 |
doc.Load(TestUtils::GetTestInputFilePath("TechDocs", "pdf_implementation.pdf")); |
| 189 |
|
| 190 |
vector<PdfReference> refs = { |
| 191 |
PdfReference(82, 0), |
| 192 |
PdfReference(83, 0), |
| 193 |
PdfReference(84, 0), |
| 194 |
PdfReference(15, 0), |
| 195 |
PdfReference(85, 0), |
| 196 |
PdfReference(86, 0), |
| 197 |
PdfReference(87, 0), |
| 198 |
PdfReference(88, 0), |
| 199 |
PdfReference(89, 0), |
| 200 |
PdfReference(90, 0), |
| 201 |
PdfReference(91, 0), |
| 202 |
}; |
| 203 |
|
| 204 |
{ |
| 205 |
auto& pages = doc.GetPages(); |
| 206 |
|
| 207 |
{ |
| 208 |
auto& page = pages.GetPageAt(0); |
| 209 |
REQUIRE(page.GetIndex() == 0); |
| 210 |
REQUIRE(!page.MoveTo(0)); |
| 211 |
REQUIRE(page.MoveTo(3)); |
| 212 |
} |
| 213 |
|
| 214 |
for (unsigned i = 0; i < pages.GetCount(); i++) |
| 215 |
{ |
| 216 |
auto& page = pages.GetPageAt(i); |
| 217 |
REQUIRE(page.GetIndex() == i); |
| 218 |
REQUIRE(page.GetObject().GetIndirectReference() == refs[i]); |
| 219 |
} |
| 220 |
} |
| 221 |
|
| 222 |
string filename = TestUtils::GetTestOutputFilePath("TestMovePage2.pdf"); |
| 223 |
doc.Save(filename); |
| 224 |
|
| 225 |
// Re-load the file to check again the references |
| 226 |
doc.Load(filename); |
| 227 |
auto& pages = doc.GetPages(); |
| 228 |
for (unsigned i = 0; i < pages.GetCount(); i++) |
| 229 |
REQUIRE(pages.GetPageAt(i).GetObject().GetIndirectReference() == refs[i]); |
| 230 |
} |
| 231 |
|
| 232 |
void testGetPages(PdfMemDocument& doc) |
| 233 |
{ |
| 234 |
for (unsigned i = 0; i < TEST_NUM_PAGES; i++) |
| 235 |
{ |
| 236 |
auto& page = doc.GetPages().GetPageAt(i); |
| 237 |
REQUIRE(isPageNumber(page, i)); |
| 238 |
} |
| 239 |
|
| 240 |
// Now delete first page |
| 241 |
doc.GetPages().RemovePageAt(0); |
| 242 |
|
| 243 |
for (unsigned i = 0; i < TEST_NUM_PAGES - 1; i++) |
| 244 |
{ |
| 245 |
auto& page = doc.GetPages().GetPageAt(i); |
| 246 |
REQUIRE(isPageNumber(page, i + 1)); |
| 247 |
} |
| 248 |
|
| 249 |
// Now delete any page |
| 250 |
constexpr unsigned DELETED_PAGE = 50; |
| 251 |
doc.GetPages().RemovePageAt(DELETED_PAGE); |
| 252 |
|
| 253 |
for (unsigned i = 0; i < TEST_NUM_PAGES - 2; i++) |
| 254 |
{ |
| 255 |
auto& page = doc.GetPages().GetPageAt(i); |
| 256 |
if (i < DELETED_PAGE) |
| 257 |
REQUIRE(isPageNumber(page, i + 1)); |
| 258 |
else |
| 259 |
REQUIRE(isPageNumber(page, i + 2)); |
| 260 |
} |
| 261 |
} |
| 262 |
|
| 263 |
void testGetPagesReverse(PdfMemDocument& doc) |
| 264 |
{ |
| 265 |
for (int i = TEST_NUM_PAGES - 1; i >= 0; i--) |
| 266 |
{ |
| 267 |
unsigned index = (unsigned)i; |
| 268 |
auto& page = doc.GetPages().GetPageAt(index); |
| 269 |
REQUIRE(isPageNumber(page, index)); |
| 270 |
} |
| 271 |
|
| 272 |
// Now delete first page |
| 273 |
doc.GetPages().RemovePageAt(0); |
| 274 |
|
| 275 |
for (int i = TEST_NUM_PAGES - 2; i >= 0; i--) |
| 276 |
{ |
| 277 |
unsigned index = (unsigned)i; |
| 278 |
auto& page = doc.GetPages().GetPageAt(index); |
| 279 |
REQUIRE(isPageNumber(page, index + 1)); |
| 280 |
} |
| 281 |
} |
| 282 |
|
| 283 |
void testInsert(PdfMemDocument& doc) |
| 284 |
{ |
| 285 |
const unsigned INSERTED_PAGE_FLAG = 1234; |
| 286 |
const unsigned INSERTED_PAGE_FLAG1 = 1234 + 1; |
| 287 |
const unsigned INSERTED_PAGE_FLAG2 = 1234 + 2; |
| 288 |
|
| 289 |
{ |
| 290 |
auto& page = doc.GetPages().CreatePageAt(0, PdfPageSize::A4); |
| 291 |
page.GetDictionary().AddKey(TEST_PAGE_KEY, |
| 292 |
static_cast<int64_t>(INSERTED_PAGE_FLAG)); |
| 293 |
} |
| 294 |
|
| 295 |
// Find inserted page (beginning) |
| 296 |
REQUIRE(isPageNumber(doc.GetPages().GetPageAt(0), INSERTED_PAGE_FLAG)); |
| 297 |
|
| 298 |
// Find old first page |
| 299 |
REQUIRE(isPageNumber(doc.GetPages().GetPageAt(1), 0)); |
| 300 |
|
| 301 |
{ |
| 302 |
// Insert at end |
| 303 |
auto& page = doc.GetPages().CreatePage(PdfPageSize::A4); |
| 304 |
page.GetDictionary().AddKey(TEST_PAGE_KEY, |
| 305 |
static_cast<int64_t>(INSERTED_PAGE_FLAG1)); |
| 306 |
} |
| 307 |
|
| 308 |
REQUIRE(isPageNumber(doc.GetPages().GetPageAt(doc.GetPages().GetCount() - 1), |
| 309 |
INSERTED_PAGE_FLAG1)); |
| 310 |
|
| 311 |
// Insert in middle |
| 312 |
const unsigned INSERT_POINT = 50; |
| 313 |
{ |
| 314 |
auto& page = doc.GetPages().CreatePageAt(INSERT_POINT, PdfPageSize::A4); |
| 315 |
page.GetDictionary().AddKey(TEST_PAGE_KEY, |
| 316 |
static_cast<int64_t>(INSERTED_PAGE_FLAG2)); |
| 317 |
} |
| 318 |
|
| 319 |
REQUIRE(isPageNumber(doc.GetPages().GetPageAt(INSERT_POINT), INSERTED_PAGE_FLAG2)); |
| 320 |
} |
| 321 |
|
| 322 |
void testDeleteAll(PdfMemDocument& doc) |
| 323 |
{ |
| 324 |
for (unsigned i = 0; i < TEST_NUM_PAGES; i++) |
| 325 |
{ |
| 326 |
doc.GetPages().RemovePageAt(0); |
| 327 |
REQUIRE(doc.GetPages().GetCount() == TEST_NUM_PAGES - (i + 1)); |
| 328 |
} |
| 329 |
REQUIRE(doc.GetPages().GetCount() == 0); |
| 330 |
} |
| 331 |
|
| 332 |
void createTestTree(PdfMemDocument& doc) |
| 333 |
{ |
| 334 |
for (unsigned i = 0; i < TEST_NUM_PAGES; i++) |
| 335 |
{ |
| 336 |
auto& page = doc.GetPages().CreatePage(PdfPageSize::A4); |
| 337 |
page.GetDictionary().AddKey(TEST_PAGE_KEY, static_cast<int64_t>(i)); |
| 338 |
REQUIRE(doc.GetPages().GetCount() == i + 1); |
| 339 |
} |
| 340 |
} |
| 341 |
|
| 342 |
PdfMemDocument PdfPageTest::CreateTestTreeCustom() |
| 343 |
{ |
| 344 |
PdfMemDocument doc; |
| 345 |
|
| 346 |
constexpr unsigned COUNT = TEST_NUM_PAGES / 10; |
| 347 |
auto& root = doc.GetObjects().CreateDictionaryObject("Pages"_n); |
| 348 |
doc.GetCatalog().GetDictionary().AddKeyIndirect("Pages"_n, root); |
| 349 |
PdfArray rootKids; |
| 350 |
|
| 351 |
for (unsigned i = 0; i < COUNT; i++) |
| 352 |
{ |
| 353 |
auto& node = doc.GetObjects().CreateDictionaryObject("Pages"_n); |
| 354 |
PdfArray nodeKids; |
| 355 |
|
| 356 |
for (unsigned j = 0; j < COUNT; j++) |
| 357 |
{ |
| 358 |
unique_ptr<PdfPage> page(new PdfPage(doc, PdfPage::CreateStandardPageSize(PdfPageSize::A4))); |
| 359 |
page->SetIndex(j); |
| 360 |
page->GetDictionary().AddKey(TEST_PAGE_KEY, |
| 361 |
static_cast<int64_t>(i) * COUNT + j); |
| 362 |
|
| 363 |
nodeKids.Add(page->GetObject().GetIndirectReference()); |
| 364 |
} |
| 365 |
|
| 366 |
node.GetDictionary().AddKey("Kids"_n, nodeKids); |
| 367 |
node.GetDictionary().AddKey("Count"_n, static_cast<int64_t>(COUNT)); |
| 368 |
rootKids.Add(node.GetIndirectReference()); |
| 369 |
} |
| 370 |
|
| 371 |
root.GetDictionary().AddKey("Kids"_n, rootKids); |
| 372 |
root.GetDictionary().AddKey("Count"_n, static_cast<int64_t>(TEST_NUM_PAGES)); |
| 373 |
|
| 374 |
// NOTE: We must copy the document as the PdfPageCollection |
| 375 |
// in the source document is already initialized |
| 376 |
return PdfMemDocument(doc); |
| 377 |
} |
| 378 |
|
| 379 |
bool isPageNumber(PdfPage& page, unsigned number) |
| 380 |
{ |
| 381 |
int64_t pageNumber = page.GetDictionary().GetKeyAsSafe<int64_t>(TEST_PAGE_KEY, -1); |
| 382 |
|
| 383 |
if (pageNumber != static_cast<int64_t>(number)) |
| 384 |
{ |
| 385 |
INFO(utls::Format("PagesTreeTest: Expected page number {} but got {}", number, pageNumber)); |
| 386 |
return false; |
| 387 |
} |
| 388 |
else |
| 389 |
return true; |
| 390 |
} |