| 1 | /** |
| 2 | * Copyright (C) 2008 by Dominik Seichter <[email protected]> |
| 3 | * Copyright (C) 2021 by Francesco Pretto <[email protected]> |
| 4 | * |
| 5 | * Licensed under GNU Library General Public 2.0 or later. |
| 6 | * Some rights reserved. See COPYING, AUTHORS. |
| 7 | */ |
| 8 | |
| 9 | #include <PdfTest.h> |
| 10 | |
| 11 | constexpr const char* TEST_PAGE_KEY = "TestPageNumber"; |
| 12 | constexpr unsigned TEST_NUM_PAGES = 100; |
| 13 | |
| 14 | namespace PoDoFo |
| 15 | { |
| 16 | class PdfPageTest |
| 17 | { |
| 18 | public: |
| 19 | static std::vector<std::unique_ptr<PdfPage>> CreateSamplePages(PdfMemDocument& doc, unsigned pageCount); |
| 20 | static PdfMemDocument CreateTestTreeCustom(); |
| 21 | }; |
| 22 | } |
| 23 | |
| 24 | using namespace std; |
| 25 | using namespace PoDoFo; |
| 26 | |
| 27 | static void appendChildNode(PdfObject& parent, PdfObject& child); |
| 28 | static bool isPageNumber(PdfPage& page, unsigned number); |
| 29 | |
| 30 | static vector<PdfObject*> createNodes(PdfMemDocument& doc, unsigned nodeCount); |
| 31 | static void createEmptyKidsTree(PdfMemDocument& doc); |
| 32 | static void createNestedArrayTree(PdfMemDocument& doc); |
| 33 | static void createTestTree(PdfMemDocument& doc); |
| 34 | static void createCyclicTree(PdfMemDocument& doc, bool createCycle); |
| 35 | static void testGetPages(PdfMemDocument& doc); |
| 36 | static void testInsert(PdfMemDocument& doc); |
| 37 | static void testDeleteAll(PdfMemDocument& doc); |
| 38 | static void testGetPagesReverse(PdfMemDocument& doc); |
| 39 | |
| 40 | TEST_CASE("testEmptyDoc") |
| 41 | { |
| 42 | PdfMemDocument doc; |
| 43 | |
| 44 | // Empty document must have page count == 0 |
| 45 | REQUIRE(doc.GetPages().GetCount() == 0); |
| 46 | |
| 47 | // Retrieving any page from an empty document must be NULL |
| 48 | ASSERT_THROW_WITH_ERROR_CODE(doc.GetPages().GetPageAt(0), PdfErrorCode::PageNotFound); |
| 49 | } |
| 50 | |
| 51 | TEST_CASE("testCyclicTree") |
| 52 | { |
| 53 | { |
| 54 | PdfMemDocument doc; |
| 55 | createCyclicTree(doc, false); |
| 56 | for (unsigned pagenum = 0; pagenum < doc.GetPages().GetCount(); pagenum++) |
| 57 | { |
| 58 | // pass 0: |
| 59 | // valid tree without cycles should yield all pages |
| 60 | auto& page = doc.GetPages().GetPageAt(pagenum); |
| 61 | REQUIRE(isPageNumber(page, pagenum)); |
| 62 | } |
| 63 | } |
| 64 | |
| 65 | { |
| 66 | PdfMemDocument doc; |
| 67 | createCyclicTree(doc, true); |
| 68 | // pass 1: |
| 69 | // cyclic tree must throw exception to prevent infinite recursion |
| 70 | ASSERT_THROW_WITH_ERROR_CODE(doc.GetPages().GetPageAt(0), PdfErrorCode::PageNotFound); |
| 71 | } |
| 72 | } |
| 73 | |
| 74 | TEST_CASE("testEmptyKidsTree") |
| 75 | { |
| 76 | PdfMemDocument doc; |
| 77 | createEmptyKidsTree(doc); |
| 78 | //doc.Write("tree_zerokids.pdf"); |
| 79 | for (unsigned pagenum = 0; pagenum < doc.GetPages().GetCount(); pagenum++) |
| 80 | { |
| 81 | PdfPage& page = doc.GetPages().GetPageAt(pagenum); |
| 82 | REQUIRE(isPageNumber(page, pagenum)); |
| 83 | } |
| 84 | } |
| 85 | |
| 86 | TEST_CASE("testNestedArrayTree") |
| 87 | { |
| 88 | PdfMemDocument doc; |
| 89 | createNestedArrayTree(doc); |
| 90 | for (unsigned i = 0, count = doc.GetPages().GetCount(); i < count; i++) |
| 91 | ASSERT_THROW_WITH_ERROR_CODE(doc.GetPages().GetPageAt(i), PdfErrorCode::PageNotFound); |
| 92 | } |
| 93 | |
| 94 | TEST_CASE("testCreateDelete") |
| 95 | { |
| 96 | PdfMemDocument doc; |
| 97 | PdfPainter painter; |
| 98 | |
| 99 | // create font |
| 100 | auto font = doc.GetFonts().SearchFont("LiberationSans"); |
| 101 | if (font == nullptr) |
| 102 | FAIL("Coult not find Arial font"); |
| 103 | |
| 104 | { |
| 105 | // write 1. page |
| 106 | auto& page = doc.GetPages().CreatePage(PdfPage::CreateStandardPageSize(PdfPageSize::A4)); |
| 107 | painter.SetCanvas(page); |
| 108 | painter.TextState.SetFont(*font, 16.0); |
| 109 | painter.DrawText("Page 1", 200, 200); |
| 110 | painter.FinishDrawing(); |
| 111 | REQUIRE(doc.GetPages().GetCount() == 1); |
| 112 | } |
| 113 | |
| 114 | { |
| 115 | // write 2. page |
| 116 | auto& page = doc.GetPages().CreatePage(PdfPage::CreateStandardPageSize(PdfPageSize::A4)); |
| 117 | painter.SetCanvas(page); |
| 118 | painter.TextState.SetFont(*font, 16.0); |
| 119 | painter.DrawText("Page 2", 200, 200); |
| 120 | painter.FinishDrawing(); |
| 121 | REQUIRE(doc.GetPages().GetCount() == 2); |
| 122 | } |
| 123 | |
| 124 | // try to delete second page, index is 0 based |
| 125 | doc.GetPages().RemovePageAt(1); |
| 126 | REQUIRE(doc.GetPages().GetCount() == 1); |
| 127 | |
| 128 | { |
| 129 | // write 3. page |
| 130 | auto& page = doc.GetPages().CreatePage(PdfPage::CreateStandardPageSize(PdfPageSize::A4)); |
| 131 | painter.SetCanvas(page); |
| 132 | painter.TextState.SetFont(*font, 16.0); |
| 133 | painter.DrawText("Page 3", 200, 200); |
| 134 | painter.FinishDrawing(); |
| 135 | REQUIRE(doc.GetPages().GetCount() == 2); |
| 136 | } |
| 137 | } |
| 138 | |
| 139 | TEST_CASE("testGetPagesCustom") |
| 140 | { |
| 141 | auto doc = PdfPageTest::CreateTestTreeCustom(); |
| 142 | testGetPages(doc); |
| 143 | } |
| 144 | |
| 145 | TEST_CASE("testGetPages") |
| 146 | { |
| 147 | PdfMemDocument doc; |
| 148 | createTestTree(doc); |
| 149 | testGetPages(doc); |
| 150 | } |
| 151 | |
| 152 | TEST_CASE("testGetPagesReverseCustom") |
| 153 | { |
| 154 | auto doc = PdfPageTest::CreateTestTreeCustom(); |
| 155 | testGetPagesReverse(doc); |
| 156 | } |
| 157 | |
| 158 | TEST_CASE("testGetPagesReverse") |
| 159 | { |
| 160 | PdfMemDocument doc; |
| 161 | createTestTree(doc); |
| 162 | testGetPagesReverse(doc); |
| 163 | } |
| 164 | |
| 165 | TEST_CASE("testInsertCustom") |
| 166 | { |
| 167 | auto doc = PdfPageTest::CreateTestTreeCustom(); |
| 168 | testInsert(doc); |
| 169 | } |
| 170 | |
| 171 | TEST_CASE("testInsert") |
| 172 | { |
| 173 | PdfMemDocument doc; |
| 174 | createTestTree(doc); |
| 175 | testInsert(doc); |
| 176 | } |
| 177 | |
| 178 | TEST_CASE("testDeleteAllCustom") |
| 179 | { |
| 180 | auto doc = PdfPageTest::CreateTestTreeCustom(); |
| 181 | testDeleteAll(doc); |
| 182 | } |
| 183 | |
| 184 | TEST_CASE("testDeleteAll") |
| 185 | { |
| 186 | PdfMemDocument doc; |
| 187 | createTestTree(doc); |
| 188 | testDeleteAll(doc); |
| 189 | } |
| 190 | |
| 191 | TEST_CASE("TestMovePage1") |
| 192 | { |
| 193 | PdfMemDocument doc; |
| 194 | doc.Load(TestUtils::GetTestInputFilePath("TechDocs", "pdf_implementation.pdf")); |
| 195 | |
| 196 | vector<PdfReference> refs = { |
| 197 | PdfReference(15, 0), |
| 198 | PdfReference(82, 0), |
| 199 | PdfReference(88, 0), |
| 200 | PdfReference(83, 0), |
| 201 | PdfReference(84, 0), |
| 202 | PdfReference(85, 0), |
| 203 | PdfReference(86, 0), |
| 204 | PdfReference(87, 0), |
| 205 | PdfReference(89, 0), |
| 206 | PdfReference(90, 0), |
| 207 | PdfReference(91, 0), |
| 208 | }; |
| 209 | |
| 210 | { |
| 211 | auto& pages = doc.GetPages(); |
| 212 | |
| 213 | { |
| 214 | auto& page = pages.GetPageAt(7); |
| 215 | REQUIRE(page.GetIndex() == 7); |
| 216 | REQUIRE(!page.MoveTo(11)); |
| 217 | REQUIRE(page.MoveTo(2)); |
| 218 | } |
| 219 | |
| 220 | for (unsigned i = 0; i < pages.GetCount(); i++) |
| 221 | { |
| 222 | auto& page = pages.GetPageAt(i); |
| 223 | REQUIRE(page.GetIndex() == i); |
| 224 | REQUIRE(page.GetObject().GetIndirectReference() == refs[i]); |
| 225 | } |
| 226 | } |
| 227 | |
| 228 | string filename = TestUtils::GetTestOutputFilePath("TestMovePage1.pdf"); |
| 229 | doc.Save(filename); |
| 230 | |
| 231 | // Re-load the file to check again the references |
| 232 | doc.Load(filename); |
| 233 | auto& pages = doc.GetPages(); |
| 234 | for (unsigned i = 0; i < pages.GetCount(); i++) |
| 235 | REQUIRE(pages.GetPageAt(i).GetObject().GetIndirectReference() == refs[i]); |
| 236 | } |
| 237 | |
| 238 | TEST_CASE("TestMovePage2") |
| 239 | { |
| 240 | PdfMemDocument doc; |
| 241 | doc.Load(TestUtils::GetTestInputFilePath("TechDocs", "pdf_implementation.pdf")); |
| 242 | |
| 243 | vector<PdfReference> refs = { |
| 244 | PdfReference(82, 0), |
| 245 | PdfReference(83, 0), |
| 246 | PdfReference(84, 0), |
| 247 | PdfReference(15, 0), |
| 248 | PdfReference(85, 0), |
| 249 | PdfReference(86, 0), |
| 250 | PdfReference(87, 0), |
| 251 | PdfReference(88, 0), |
| 252 | PdfReference(89, 0), |
| 253 | PdfReference(90, 0), |
| 254 | PdfReference(91, 0), |
| 255 | }; |
| 256 | |
| 257 | { |
| 258 | auto& pages = doc.GetPages(); |
| 259 | |
| 260 | { |
| 261 | auto& page = pages.GetPageAt(0); |
| 262 | REQUIRE(page.GetIndex() == 0); |
| 263 | REQUIRE(!page.MoveTo(0)); |
| 264 | REQUIRE(page.MoveTo(3)); |
| 265 | } |
| 266 | |
| 267 | for (unsigned i = 0; i < pages.GetCount(); i++) |
| 268 | { |
| 269 | auto& page = pages.GetPageAt(i); |
| 270 | REQUIRE(page.GetIndex() == i); |
| 271 | REQUIRE(page.GetObject().GetIndirectReference() == refs[i]); |
| 272 | } |
| 273 | } |
| 274 | |
| 275 | string filename = TestUtils::GetTestOutputFilePath("TestMovePage2.pdf"); |
| 276 | doc.Save(filename); |
| 277 | |
| 278 | // Re-load the file to check again the references |
| 279 | doc.Load(filename); |
| 280 | auto& pages = doc.GetPages(); |
| 281 | for (unsigned i = 0; i < pages.GetCount(); i++) |
| 282 | REQUIRE(pages.GetPageAt(i).GetObject().GetIndirectReference() == refs[i]); |
| 283 | } |
| 284 | |
| 285 | void testGetPages(PdfMemDocument& doc) |
| 286 | { |
| 287 | for (unsigned i = 0; i < TEST_NUM_PAGES; i++) |
| 288 | { |
| 289 | auto& page = doc.GetPages().GetPageAt(i); |
| 290 | REQUIRE(isPageNumber(page, i)); |
| 291 | } |
| 292 | |
| 293 | // Now delete first page |
| 294 | doc.GetPages().RemovePageAt(0); |
| 295 | |
| 296 | for (unsigned i = 0; i < TEST_NUM_PAGES - 1; i++) |
| 297 | { |
| 298 | auto& page = doc.GetPages().GetPageAt(i); |
| 299 | REQUIRE(isPageNumber(page, i + 1)); |
| 300 | } |
| 301 | |
| 302 | // Now delete any page |
| 303 | constexpr unsigned DELETED_PAGE = 50; |
| 304 | doc.GetPages().RemovePageAt(DELETED_PAGE); |
| 305 | |
| 306 | for (unsigned i = 0; i < TEST_NUM_PAGES - 2; i++) |
| 307 | { |
| 308 | auto& page = doc.GetPages().GetPageAt(i); |
| 309 | if (i < DELETED_PAGE) |
| 310 | REQUIRE(isPageNumber(page, i + 1)); |
| 311 | else |
| 312 | REQUIRE(isPageNumber(page, i + 2)); |
| 313 | } |
| 314 | } |
| 315 | |
| 316 | void testGetPagesReverse(PdfMemDocument& doc) |
| 317 | { |
| 318 | for (int i = TEST_NUM_PAGES - 1; i >= 0; i--) |
| 319 | { |
| 320 | unsigned index = (unsigned)i; |
| 321 | auto& page = doc.GetPages().GetPageAt(index); |
| 322 | REQUIRE(isPageNumber(page, index)); |
| 323 | } |
| 324 | |
| 325 | // Now delete first page |
| 326 | doc.GetPages().RemovePageAt(0); |
| 327 | |
| 328 | for (int i = TEST_NUM_PAGES - 2; i >= 0; i--) |
| 329 | { |
| 330 | unsigned index = (unsigned)i; |
| 331 | auto& page = doc.GetPages().GetPageAt(index); |
| 332 | REQUIRE(isPageNumber(page, index + 1)); |
| 333 | } |
| 334 | } |
| 335 | |
| 336 | void testInsert(PdfMemDocument& doc) |
| 337 | { |
| 338 | const unsigned INSERTED_PAGE_FLAG = 1234; |
| 339 | const unsigned INSERTED_PAGE_FLAG1 = 1234 + 1; |
| 340 | const unsigned INSERTED_PAGE_FLAG2 = 1234 + 2; |
| 341 | |
| 342 | { |
| 343 | auto& page = doc.GetPages().CreatePageAt(0, PdfPage::CreateStandardPageSize(PdfPageSize::A4)); |
| 344 | page.GetObject().GetDictionary().AddKey(TEST_PAGE_KEY, |
| 345 | static_cast<int64_t>(INSERTED_PAGE_FLAG)); |
| 346 | } |
| 347 | |
| 348 | // Find inserted page (beginning) |
| 349 | REQUIRE(isPageNumber(doc.GetPages().GetPageAt(0), INSERTED_PAGE_FLAG)); |
| 350 | |
| 351 | // Find old first page |
| 352 | REQUIRE(isPageNumber(doc.GetPages().GetPageAt(1), 0)); |
| 353 | |
| 354 | { |
| 355 | // Insert at end |
| 356 | auto& page = doc.GetPages().CreatePage(PdfPage::CreateStandardPageSize(PdfPageSize::A4)); |
| 357 | page.GetObject().GetDictionary().AddKey(TEST_PAGE_KEY, |
| 358 | static_cast<int64_t>(INSERTED_PAGE_FLAG1)); |
| 359 | } |
| 360 | |
| 361 | REQUIRE(isPageNumber(doc.GetPages().GetPageAt(doc.GetPages().GetCount() - 1), |
| 362 | INSERTED_PAGE_FLAG1)); |
| 363 | |
| 364 | // Insert in middle |
| 365 | const unsigned INSERT_POINT = 50; |
| 366 | { |
| 367 | auto& page = doc.GetPages().CreatePageAt(INSERT_POINT, PdfPage::CreateStandardPageSize(PdfPageSize::A4)); |
| 368 | page.GetObject().GetDictionary().AddKey(TEST_PAGE_KEY, |
| 369 | static_cast<int64_t>(INSERTED_PAGE_FLAG2)); |
| 370 | } |
| 371 | |
| 372 | REQUIRE(isPageNumber(doc.GetPages().GetPageAt(INSERT_POINT), INSERTED_PAGE_FLAG2)); |
| 373 | } |
| 374 | |
| 375 | void testDeleteAll(PdfMemDocument& doc) |
| 376 | { |
| 377 | for (unsigned i = 0; i < TEST_NUM_PAGES; i++) |
| 378 | { |
| 379 | doc.GetPages().RemovePageAt(0); |
| 380 | REQUIRE(doc.GetPages().GetCount() == TEST_NUM_PAGES - (i + 1)); |
| 381 | } |
| 382 | REQUIRE(doc.GetPages().GetCount() == 0); |
| 383 | } |
| 384 | |
| 385 | void createTestTree(PdfMemDocument& doc) |
| 386 | { |
| 387 | for (unsigned i = 0; i < TEST_NUM_PAGES; i++) |
| 388 | { |
| 389 | auto& page = doc.GetPages().CreatePage(PdfPage::CreateStandardPageSize(PdfPageSize::A4)); |
| 390 | page.GetObject().GetDictionary().AddKey(TEST_PAGE_KEY, static_cast<int64_t>(i)); |
| 391 | REQUIRE(doc.GetPages().GetCount() == i + 1); |
| 392 | } |
| 393 | } |
| 394 | |
| 395 | PdfMemDocument PdfPageTest::CreateTestTreeCustom() |
| 396 | { |
| 397 | PdfMemDocument doc; |
| 398 | |
| 399 | constexpr unsigned COUNT = TEST_NUM_PAGES / 10; |
| 400 | auto& root = doc.GetObjects().CreateDictionaryObject("Pages"); |
| 401 | doc.GetCatalog().GetDictionary().AddKeyIndirect("Pages", root); |
| 402 | PdfArray rootKids; |
| 403 | |
| 404 | for (unsigned i = 0; i < COUNT; i++) |
| 405 | { |
| 406 | auto& node = doc.GetObjects().CreateDictionaryObject("Pages"); |
| 407 | PdfArray nodeKids; |
| 408 | |
| 409 | for (unsigned j = 0; j < COUNT; j++) |
| 410 | { |
| 411 | unique_ptr<PdfPage> page(new PdfPage(doc, PdfPage::CreateStandardPageSize(PdfPageSize::A4))); |
| 412 | page->SetIndex(j); |
| 413 | page->GetObject().GetDictionary().AddKey(TEST_PAGE_KEY, |
| 414 | static_cast<int64_t>(i) * COUNT + j); |
| 415 | |
| 416 | nodeKids.Add(page->GetObject().GetIndirectReference()); |
| 417 | } |
| 418 | |
| 419 | node.GetDictionary().AddKey("Kids", nodeKids); |
| 420 | node.GetDictionary().AddKey("Count", static_cast<int64_t>(COUNT)); |
| 421 | rootKids.Add(node.GetIndirectReference()); |
| 422 | } |
| 423 | |
| 424 | root.GetDictionary().AddKey("Kids", rootKids); |
| 425 | root.GetDictionary().AddKey("Count", static_cast<int64_t>(TEST_NUM_PAGES)); |
| 426 | |
| 427 | // NOTE: We must copy the document as the PdfPageCollection |
| 428 | // in the source document is already initialized |
| 429 | return PdfMemDocument(doc); |
| 430 | } |
| 431 | |
| 432 | vector<unique_ptr<PdfPage>> PdfPageTest::CreateSamplePages(PdfMemDocument& doc, unsigned pageCount) |
| 433 | { |
| 434 | // create font |
| 435 | auto font = doc.GetFonts().SearchFont("LiberationSans"); |
| 436 | if (font == nullptr) |
| 437 | FAIL("Coult not find Arial font"); |
| 438 | |
| 439 | vector<unique_ptr<PdfPage>> pages(pageCount); |
| 440 | for (unsigned i = 0; i < pageCount; ++i) |
| 441 | { |
| 442 | pages[i].reset(new PdfPage(doc, PdfPage::CreateStandardPageSize(PdfPageSize::A4))); |
| 443 | pages[i]->SetIndex(i); |
| 444 | pages[i]->GetObject().GetDictionary().AddKey(TEST_PAGE_KEY, static_cast<int64_t>(i)); |
| 445 | |
| 446 | PdfPainter painter; |
| 447 | painter.SetCanvas(*pages[i]); |
| 448 | painter.TextState.SetFont(*font, 16.0); |
| 449 | ostringstream os; |
| 450 | os << "Page " << i + 1; |
| 451 | painter.DrawText(os.str(), 200, 200); |
| 452 | painter.FinishDrawing(); |
| 453 | } |
| 454 | |
| 455 | return pages; |
| 456 | } |
| 457 | |
| 458 | vector<PdfObject*> createNodes(PdfMemDocument& doc, unsigned nodeCount) |
| 459 | { |
| 460 | vector<PdfObject*> nodes(nodeCount); |
| 461 | |
| 462 | for (unsigned i = 0; i < nodeCount; ++i) |
| 463 | { |
| 464 | nodes[i] = &doc.GetObjects().CreateDictionaryObject("Pages"); |
| 465 | // init required keys |
| 466 | nodes[i]->GetDictionary().AddKey("Kids", PdfArray()); |
| 467 | nodes[i]->GetDictionary().AddKey("Count", PdfVariant(static_cast<int64_t>(0L))); |
| 468 | } |
| 469 | |
| 470 | return nodes; |
| 471 | } |
| 472 | |
| 473 | void createCyclicTree(PdfMemDocument& doc, bool createCycle) |
| 474 | { |
| 475 | const unsigned COUNT = 3; |
| 476 | |
| 477 | auto pages = PdfPageTest::CreateSamplePages(doc, COUNT); |
| 478 | auto nodes = createNodes(doc, 2); |
| 479 | |
| 480 | // manually insert pages into pagetree |
| 481 | auto& root = doc.GetPages().GetObject(); |
| 482 | |
| 483 | // tree layout (for !bCreateCycle): |
| 484 | // |
| 485 | // root |
| 486 | // +-- node0 |
| 487 | // +-- node1 |
| 488 | // | +-- page0 |
| 489 | // | +-- page1 |
| 490 | // \-- page2 |
| 491 | |
| 492 | // root node |
| 493 | appendChildNode(root, *nodes[0]); |
| 494 | |
| 495 | // tree node 0 |
| 496 | appendChildNode(*nodes[0], *nodes[1]); |
| 497 | appendChildNode(*nodes[0], pages[2]->GetObject()); |
| 498 | |
| 499 | // tree node 1 |
| 500 | appendChildNode(*nodes[1], pages[0]->GetObject()); |
| 501 | appendChildNode(*nodes[1], pages[1]->GetObject()); |
| 502 | |
| 503 | if (createCycle) |
| 504 | { |
| 505 | // invalid tree: Cycle!!! |
| 506 | // was not detected in PdfPagesTree::GetPageNode() rev. 1937 |
| 507 | nodes[0]->GetDictionary().MustFindKey("Kids").GetArray()[0] = root.GetIndirectReference(); |
| 508 | } |
| 509 | } |
| 510 | |
| 511 | void createEmptyKidsTree(PdfMemDocument& doc) |
| 512 | { |
| 513 | const unsigned COUNT = 3; |
| 514 | |
| 515 | auto pages = PdfPageTest::CreateSamplePages(doc, COUNT); |
| 516 | auto nodes = createNodes(doc, 3); |
| 517 | |
| 518 | // manually insert pages into pagetree |
| 519 | auto& root = doc.GetPages().GetObject(); |
| 520 | |
| 521 | // tree layout: |
| 522 | // |
| 523 | // root |
| 524 | // +-- node0 |
| 525 | // | +-- page0 |
| 526 | // | +-- page1 |
| 527 | // | +-- page2 |
| 528 | // +-- node1 |
| 529 | // \-- node2 |
| 530 | |
| 531 | // root node |
| 532 | appendChildNode(root, *nodes[0]); |
| 533 | appendChildNode(root, *nodes[1]); |
| 534 | appendChildNode(root, *nodes[2]); |
| 535 | |
| 536 | // tree node 0 |
| 537 | appendChildNode(*nodes[0], pages[0]->GetObject()); |
| 538 | appendChildNode(*nodes[0], pages[1]->GetObject()); |
| 539 | appendChildNode(*nodes[0], pages[2]->GetObject()); |
| 540 | |
| 541 | // tree node 1 and node 2 are left empty: this is completely valid |
| 542 | // according to the PDF spec, i.e. the required keys may have the |
| 543 | // values "/Kids [ ]" and "/Count 0" |
| 544 | } |
| 545 | |
| 546 | void createNestedArrayTree(PdfMemDocument& doc) |
| 547 | { |
| 548 | constexpr unsigned COUNT = 3; |
| 549 | |
| 550 | auto pages = PdfPageTest::CreateSamplePages(doc, COUNT); |
| 551 | auto& root = doc.GetPages().GetObject(); |
| 552 | |
| 553 | // create kids array |
| 554 | PdfArray kids; |
| 555 | for (unsigned i = 0; i < COUNT; i++) |
| 556 | { |
| 557 | kids.Add(pages[i]->GetObject().GetIndirectReference()); |
| 558 | pages[i]->GetObject().GetDictionary().AddKey("Parent", root.GetIndirectReference()); |
| 559 | } |
| 560 | |
| 561 | // create nested kids array |
| 562 | PdfArray nested; |
| 563 | nested.Add(kids); |
| 564 | |
| 565 | // manually insert pages into pagetree |
| 566 | root.GetDictionary().AddKey("Count", static_cast<int64_t>(COUNT)); |
| 567 | root.GetDictionary().AddKey("Kids", nested); |
| 568 | } |
| 569 | |
| 570 | bool isPageNumber(PdfPage& page, unsigned number) |
| 571 | { |
| 572 | int64_t pageNumber = page.GetObject().GetDictionary().GetKeyAs<int64_t>(TEST_PAGE_KEY, -1); |
| 573 | |
| 574 | if (pageNumber != static_cast<int64_t>(number)) |
| 575 | { |
| 576 | INFO(utls::Format("PagesTreeTest: Expected page number {} but got {}", number, pageNumber)); |
| 577 | return false; |
| 578 | } |
| 579 | else |
| 580 | return true; |
| 581 | } |
| 582 | |
| 583 | void appendChildNode(PdfObject& parent, PdfObject& child) |
| 584 | { |
| 585 | // 1. Add the reference of the new child to the kids array of parent |
| 586 | PdfArray kids; |
| 587 | PdfObject* oldKids = parent.GetDictionary().FindKey("Kids"); |
| 588 | if (oldKids != nullptr && oldKids->IsArray()) kids = oldKids->GetArray(); |
| 589 | kids.Add(child.GetIndirectReference()); |
| 590 | parent.GetDictionary().AddKey("Kids", kids); |
| 591 | |
| 592 | // 2. If the child is a page (leaf node), increase count of every parent |
| 593 | // (which also includes pParent) |
| 594 | if (child.GetDictionary().GetKeyAs<PdfName>("Type") == "Page") |
| 595 | { |
| 596 | PdfObject* node = &parent; |
| 597 | while (node) |
| 598 | { |
| 599 | int64_t count = 0; |
| 600 | if (node->GetDictionary().FindKey("Count")) count = node->GetDictionary().FindKey("Count")->GetNumber(); |
| 601 | count++; |
| 602 | node->GetDictionary().AddKey("Count", count); |
| 603 | node = node->GetDictionary().FindKey("Parent"); |
| 604 | } |
| 605 | } |
| 606 | |
| 607 | // 3. Add Parent key to the child |
| 608 | child.GetDictionary().AddKey("Parent", parent.GetIndirectReference()); |
| 609 | } |