| 1 |
// SPDX-FileCopyrightText: 2018 Amin Massad <[email protected]> |
| 2 |
// SPDX-FileCopyrightText: 2018 Clayton Wheeler <[email protected]> |
| 3 |
// SPDX-License-Identifier: GPL-2.0-or-later |
| 4 |
|
| 5 |
// --------------- |
| 6 |
// !!! WARNING !!! |
| 7 |
// --------------- |
| 8 |
// These tests are still GPLv2+ licensed and |
| 9 |
// are not yet relicensed/rewritten to MIT-0 |
| 10 |
|
| 11 |
#ifdef PODOFO_ENABLE_GPL2_TESTS |
| 12 |
|
| 13 |
#include <PdfTest.h> |
| 14 |
|
| 15 |
#include <ostream> |
| 16 |
#include <iostream> |
| 17 |
|
| 18 |
using namespace std; |
| 19 |
using namespace PoDoFo; |
| 20 |
|
| 21 |
#define TEST_PAGE_KEY "TestPageNumber"_n |
| 22 |
|
| 23 |
static vector<PdfObject*> createNodes(PdfMemDocument& doc, unsigned nodeCount); |
| 24 |
static void createEmptyKidsTree(PdfMemDocument& doc); |
| 25 |
static void createNestedArrayTree(PdfMemDocument& doc); |
| 26 |
static void createCyclicTree(PdfMemDocument& doc, bool createCycle); |
| 27 |
static void appendChildNode(PdfObject& parent, PdfObject& child); |
| 28 |
static bool isPageNumber(PdfPage& page, unsigned number); |
| 29 |
|
| 30 |
namespace PoDoFo |
| 31 |
{ |
| 32 |
class PdfPageTest |
| 33 |
{ |
| 34 |
public: |
| 35 |
static std::vector<std::unique_ptr<PdfPage>> CreateSamplePages(PdfMemDocument& doc, unsigned pageCount); |
| 36 |
static PdfMemDocument CreateTestTreeCustom(); |
| 37 |
}; |
| 38 |
} |
| 39 |
|
| 40 |
TEST_CASE("TestCyclicTree") |
| 41 |
{ |
| 42 |
{ |
| 43 |
PdfMemDocument doc; |
| 44 |
createCyclicTree(doc, false); |
| 45 |
for (unsigned pagenum = 0; pagenum < doc.GetPages().GetCount(); pagenum++) |
| 46 |
{ |
| 47 |
// pass 0: |
| 48 |
// valid tree without cycles should yield all pages |
| 49 |
auto& page = doc.GetPages().GetPageAt(pagenum); |
| 50 |
REQUIRE(isPageNumber(page, pagenum)); |
| 51 |
} |
| 52 |
} |
| 53 |
|
| 54 |
{ |
| 55 |
PdfMemDocument doc; |
| 56 |
createCyclicTree(doc, true); |
| 57 |
// pass 1: |
| 58 |
// cyclic tree must throw exception to prevent infinite recursion |
| 59 |
ASSERT_THROW_WITH_ERROR_CODE(doc.GetPages().GetPageAt(0), PdfErrorCode::ValueOutOfRange); |
| 60 |
} |
| 61 |
} |
| 62 |
|
| 63 |
TEST_CASE("TestEmptyKidsTree") |
| 64 |
{ |
| 65 |
PdfMemDocument doc; |
| 66 |
createEmptyKidsTree(doc); |
| 67 |
//doc.Write("tree_zerokids.pdf"); |
| 68 |
for (unsigned pagenum = 0; pagenum < doc.GetPages().GetCount(); pagenum++) |
| 69 |
{ |
| 70 |
PdfPage& page = doc.GetPages().GetPageAt(pagenum); |
| 71 |
REQUIRE(isPageNumber(page, pagenum)); |
| 72 |
} |
| 73 |
} |
| 74 |
|
| 75 |
TEST_CASE("TestNestedArrayTree") |
| 76 |
{ |
| 77 |
PdfMemDocument doc; |
| 78 |
createNestedArrayTree(doc); |
| 79 |
for (unsigned i = 0, count = doc.GetPages().GetCount(); i < count; i++) |
| 80 |
ASSERT_THROW_WITH_ERROR_CODE(doc.GetPages().GetPageAt(i), PdfErrorCode::ValueOutOfRange); |
| 81 |
} |
| 82 |
|
| 83 |
vector<unique_ptr<PdfPage>> PdfPageTest::CreateSamplePages(PdfMemDocument& doc, unsigned pageCount) |
| 84 |
{ |
| 85 |
// create font |
| 86 |
auto font = doc.GetFonts().SearchFont("LiberationSans"); |
| 87 |
if (font == nullptr) |
| 88 |
FAIL("Could not find Arial font"); |
| 89 |
|
| 90 |
vector<unique_ptr<PdfPage>> pages(pageCount); |
| 91 |
for (unsigned i = 0; i < pageCount; ++i) |
| 92 |
{ |
| 93 |
pages[i].reset(new PdfPage(doc, PdfPage::CreateStandardPageSize(PdfPageSize::A4))); |
| 94 |
pages[i]->SetIndex(i); |
| 95 |
pages[i]->GetDictionary().AddKey(TEST_PAGE_KEY, static_cast<int64_t>(i)); |
| 96 |
|
| 97 |
PdfPainter painter; |
| 98 |
painter.SetCanvas(*pages[i]); |
| 99 |
painter.TextState.SetFont(*font, 16.0); |
| 100 |
ostringstream os; |
| 101 |
os << "Page " << i + 1; |
| 102 |
painter.DrawText(os.str(), 200, 200); |
| 103 |
painter.FinishDrawing(); |
| 104 |
} |
| 105 |
|
| 106 |
return pages; |
| 107 |
} |
| 108 |
|
| 109 |
vector<PdfObject*> createNodes(PdfMemDocument& doc, unsigned nodeCount) |
| 110 |
{ |
| 111 |
vector<PdfObject*> nodes(nodeCount); |
| 112 |
|
| 113 |
for (unsigned i = 0; i < nodeCount; ++i) |
| 114 |
{ |
| 115 |
nodes[i] = &doc.GetObjects().CreateDictionaryObject("Pages"_n); |
| 116 |
// init required keys |
| 117 |
nodes[i]->GetDictionary().AddKey("Kids"_n, PdfArray()); |
| 118 |
nodes[i]->GetDictionary().AddKey("Count"_n, PdfVariant(static_cast<int64_t>(0L))); |
| 119 |
} |
| 120 |
|
| 121 |
return nodes; |
| 122 |
} |
| 123 |
|
| 124 |
void createCyclicTree(PdfMemDocument& doc, bool createCycle) |
| 125 |
{ |
| 126 |
const unsigned COUNT = 3; |
| 127 |
|
| 128 |
auto pages = PdfPageTest::CreateSamplePages(doc, COUNT); |
| 129 |
auto nodes = createNodes(doc, 2); |
| 130 |
|
| 131 |
// manually insert pages into pagetree |
| 132 |
auto& root = doc.GetPages().GetObject(); |
| 133 |
|
| 134 |
// tree layout (for !bCreateCycle): |
| 135 |
// |
| 136 |
// root |
| 137 |
// +-- node0 |
| 138 |
// +-- node1 |
| 139 |
// | +-- page0 |
| 140 |
// | +-- page1 |
| 141 |
// \-- page2 |
| 142 |
|
| 143 |
// root node |
| 144 |
appendChildNode(root, *nodes[0]); |
| 145 |
|
| 146 |
// tree node 0 |
| 147 |
appendChildNode(*nodes[0], *nodes[1]); |
| 148 |
appendChildNode(*nodes[0], pages[2]->GetObject()); |
| 149 |
|
| 150 |
// tree node 1 |
| 151 |
appendChildNode(*nodes[1], pages[0]->GetObject()); |
| 152 |
appendChildNode(*nodes[1], pages[1]->GetObject()); |
| 153 |
|
| 154 |
if (createCycle) |
| 155 |
{ |
| 156 |
// invalid tree: Cycle!!! |
| 157 |
// was not detected in PdfPagesTree::GetPageNode() rev. 1937 |
| 158 |
nodes[0]->GetDictionary().MustFindKey("Kids").GetArray()[0] = root.GetIndirectReference(); |
| 159 |
} |
| 160 |
} |
| 161 |
|
| 162 |
void createEmptyKidsTree(PdfMemDocument& doc) |
| 163 |
{ |
| 164 |
const unsigned COUNT = 3; |
| 165 |
|
| 166 |
auto pages = PdfPageTest::CreateSamplePages(doc, COUNT); |
| 167 |
auto nodes = createNodes(doc, 3); |
| 168 |
|
| 169 |
// manually insert pages into pagetree |
| 170 |
auto& root = doc.GetPages().GetObject(); |
| 171 |
|
| 172 |
// tree layout: |
| 173 |
// |
| 174 |
// root |
| 175 |
// +-- node0 |
| 176 |
// | +-- page0 |
| 177 |
// | +-- page1 |
| 178 |
// | +-- page2 |
| 179 |
// +-- node1 |
| 180 |
// \-- node2 |
| 181 |
|
| 182 |
// root node |
| 183 |
appendChildNode(root, *nodes[0]); |
| 184 |
appendChildNode(root, *nodes[1]); |
| 185 |
appendChildNode(root, *nodes[2]); |
| 186 |
|
| 187 |
// tree node 0 |
| 188 |
appendChildNode(*nodes[0], pages[0]->GetObject()); |
| 189 |
appendChildNode(*nodes[0], pages[1]->GetObject()); |
| 190 |
appendChildNode(*nodes[0], pages[2]->GetObject()); |
| 191 |
|
| 192 |
// tree node 1 and node 2 are left empty: this is completely valid |
| 193 |
// according to the PDF spec, i.e. the required keys may have the |
| 194 |
// values "/Kids [ ]" and "/Count 0" |
| 195 |
} |
| 196 |
|
| 197 |
void createNestedArrayTree(PdfMemDocument& doc) |
| 198 |
{ |
| 199 |
constexpr unsigned COUNT = 3; |
| 200 |
|
| 201 |
auto pages = PdfPageTest::CreateSamplePages(doc, COUNT); |
| 202 |
auto& root = doc.GetPages().GetObject(); |
| 203 |
|
| 204 |
// create kids array |
| 205 |
PdfArray kids; |
| 206 |
for (unsigned i = 0; i < COUNT; i++) |
| 207 |
{ |
| 208 |
kids.Add(pages[i]->GetObject().GetIndirectReference()); |
| 209 |
pages[i]->GetDictionary().AddKey("Parent"_n, root.GetIndirectReference()); |
| 210 |
} |
| 211 |
|
| 212 |
// create nested kids array |
| 213 |
PdfArray nested; |
| 214 |
nested.Add(kids); |
| 215 |
|
| 216 |
// manually insert pages into pagetree |
| 217 |
root.GetDictionary().AddKey("Count"_n, static_cast<int64_t>(COUNT)); |
| 218 |
root.GetDictionary().AddKey("Kids"_n, nested); |
| 219 |
} |
| 220 |
|
| 221 |
void appendChildNode(PdfObject& parent, PdfObject& child) |
| 222 |
{ |
| 223 |
// 1. Add the reference of the new child to the kids array of parent |
| 224 |
PdfArray kids; |
| 225 |
PdfObject* oldKids = parent.GetDictionary().FindKey("Kids"); |
| 226 |
if (oldKids != nullptr && oldKids->IsArray()) kids = oldKids->GetArray(); |
| 227 |
kids.Add(child.GetIndirectReference()); |
| 228 |
parent.GetDictionary().AddKey("Kids"_n, kids); |
| 229 |
|
| 230 |
// 2. If the child is a page (leaf node), increase count of every parent |
| 231 |
// (which also includes pParent) |
| 232 |
if (child.GetDictionary().GetKeyAs<PdfName>("Type") == "Page") |
| 233 |
{ |
| 234 |
PdfObject* node = &parent; |
| 235 |
while (node) |
| 236 |
{ |
| 237 |
int64_t count = 0; |
| 238 |
if (node->GetDictionary().FindKey("Count")) count = node->GetDictionary().FindKey("Count")->GetNumber(); |
| 239 |
count++; |
| 240 |
node->GetDictionary().AddKey("Count"_n, count); |
| 241 |
node = node->GetDictionary().FindKey("Parent"); |
| 242 |
} |
| 243 |
} |
| 244 |
|
| 245 |
// 3. Add Parent key to the child |
| 246 |
child.GetDictionary().AddKey("Parent"_n, parent.GetIndirectReference()); |
| 247 |
} |
| 248 |
|
| 249 |
bool isPageNumber(PdfPage& page, unsigned number) |
| 250 |
{ |
| 251 |
int64_t pageNumber = page.GetDictionary().GetKeyAsSafe<int64_t>(TEST_PAGE_KEY, -1); |
| 252 |
|
| 253 |
if (pageNumber != static_cast<int64_t>(number)) |
| 254 |
{ |
| 255 |
INFO(utls::Format("PagesTreeTest: Expected page number {} but got {}", number, pageNumber)); |
| 256 |
return false; |
| 257 |
} |
| 258 |
else |
| 259 |
return true; |
| 260 |
} |
| 261 |
|
| 262 |
// Clayton Wheeler, commit 2cddb84ae550afd612749cac831ae0bb330c6422 |
| 263 |
TEST_CASE("TestSaveIncrementalRoundTrip") |
| 264 |
{ |
| 265 |
ostringstream oss; |
| 266 |
oss << "%PDF-1.1\n"; |
| 267 |
unsigned currObj = 1; |
| 268 |
streamoff objPos[20]; |
| 269 |
|
| 270 |
// Pages |
| 271 |
|
| 272 |
unsigned pagesObj = currObj; |
| 273 |
objPos[currObj] = oss.tellp(); |
| 274 |
oss << currObj++ << " 0 obj\n"; |
| 275 |
oss << "<</Type /Pages /Count 0 /Kids []>>\n"; |
| 276 |
oss << "endobj\n"; |
| 277 |
|
| 278 |
// Root catalog |
| 279 |
|
| 280 |
unsigned rootObj = currObj; |
| 281 |
objPos[currObj] = oss.tellp(); |
| 282 |
oss << currObj++ << " 0 obj\n"; |
| 283 |
oss << "<</Type /Catalog /Pages " << pagesObj << " 0 R>>\n"; |
| 284 |
oss << "endobj\n"; |
| 285 |
|
| 286 |
// ID |
| 287 |
unsigned idObj = currObj; |
| 288 |
objPos[currObj] = oss.tellp(); |
| 289 |
oss << currObj++ << " 0 obj\n"; |
| 290 |
oss << "[<F1E375363A6314E3766EDF396D614748> <F1E375363A6314E3766EDF396D614748>]\n"; |
| 291 |
oss << "endobj\n"; |
| 292 |
|
| 293 |
streamoff xrefPos = oss.tellp(); |
| 294 |
oss << "xref\n"; |
| 295 |
oss << "0 " << currObj << "\n"; |
| 296 |
oss << "0000000000 65535 f \n"; |
| 297 |
for (unsigned i = 1; i < currObj; i++) |
| 298 |
oss << utls::Format("{:010d} 00000 n \n", objPos[i]); |
| 299 |
|
| 300 |
oss << "trailer <<\n" |
| 301 |
<< " /Size " << currObj << "\n" |
| 302 |
<< " /Root " << rootObj << " 0 R\n" |
| 303 |
<< " /ID " << idObj << " 0 R\n" // indirect ID |
| 304 |
<< ">>\n" |
| 305 |
<< "startxref\n" |
| 306 |
<< xrefPos << "\n" |
| 307 |
<< "%%EOF\n"; |
| 308 |
|
| 309 |
string docBuff = oss.str(); |
| 310 |
try |
| 311 |
{ |
| 312 |
PdfMemDocument doc; |
| 313 |
// load for update |
| 314 |
doc.LoadFromBuffer(docBuff); |
| 315 |
|
| 316 |
StringStreamDevice outDev(docBuff); |
| 317 |
|
| 318 |
doc.SaveUpdate(outDev); |
| 319 |
doc.LoadFromBuffer(docBuff); |
| 320 |
} |
| 321 |
catch (PdfError&) |
| 322 |
{ |
| 323 |
FAIL("Unexpected PdfError"); |
| 324 |
} |
| 325 |
} |
| 326 |
|
| 327 |
#endif // PODOFO_ENABLE_GPL2_TESTS |