| 220 | } |
| 221 | |
| 222 | PDFIndirectReference PDFTagTree::makeStructTreeRoot(PDFDocumentImpl* document) { |
| 223 | if (!root || can_discard(root.get())) { |
| 224 | return PDFIndirectReference(); |
| 225 | } |
| 226 | |
| 227 | PDFIndirectReference ref = document->reserveRef(); |
| 228 | |
| 229 | auto pageCount = static_cast<uint32_t>(document->pageCount()); |
| 230 | |
| 231 | // Build the StructTreeRoot. |
| 232 | PDFDictionary structTreeRoot("StructTreeRoot"); |
| 233 | structTreeRoot.insertRef("K", PrepareTagTreeToEmit(ref, root.get(), document)); |
| 234 | structTreeRoot.insertInt("ParentTreeNextKey", static_cast<int>(pageCount)); |
| 235 | |
| 236 | // Build the parent tree, which consists of two things: |
| 237 | // (1) For each page, a mapping from the marked content IDs on |
| 238 | // each page to their corresponding tags |
| 239 | // (2) For each annotation, an indirect reference to that |
| 240 | // annotation's struct tree element. |
| 241 | PDFDictionary parentTree("ParentTree"); |
| 242 | auto parentTreeNums = MakePDFArray(); |
| 243 | |
| 244 | // First, one entry per page. |
| 245 | DEBUG_ASSERT(static_cast<uint32_t>(marksPerPage.size()) <= pageCount); |
| 246 | for (size_t j = 0; j < marksPerPage.size(); ++j) { |
| 247 | auto pageMarks = marksPerPage[j]; |
| 248 | PDFArray markToTagArray; |
| 249 | for (PDFTagNode* mark : pageMarks) { |
| 250 | DEBUG_ASSERT(mark->ref); |
| 251 | markToTagArray.appendRef(mark->ref); |
| 252 | } |
| 253 | parentTreeNums->appendInt(static_cast<int>(j)); |
| 254 | parentTreeNums->appendRef(document->emit(markToTagArray)); |
| 255 | } |
| 256 | |
| 257 | // Then, one entry per annotation. |
| 258 | for (size_t j = 0; j < parentTreeAnnotationNodeIds.size(); ++j) { |
| 259 | int nodeId = parentTreeAnnotationNodeIds[j]; |
| 260 | int structParentKey = FirstAnnotationStructParentKey + static_cast<int>(j); |
| 261 | |
| 262 | auto iter = nodeMap.find(nodeId); |
| 263 | if (iter == nodeMap.end()) { |
| 264 | continue; |
| 265 | } |
| 266 | auto tag = iter->second; |
| 267 | parentTreeNums->appendInt(structParentKey); |
| 268 | parentTreeNums->appendRef(tag->ref); |
| 269 | } |
| 270 | |
| 271 | parentTree.insertObject("Nums", std::move(parentTreeNums)); |
| 272 | structTreeRoot.insertRef("ParentTree", document->emit(parentTree)); |
| 273 | |
| 274 | // Build the IDTree, a mapping from every unique ID string to |
| 275 | // a reference to its corresponding structure element node. |
| 276 | if (!IDTreeEntries.empty()) { |
| 277 | std::sort(IDTreeEntries.begin(), IDTreeEntries.end(), |
| 278 | [](const IDTreeEntry& a, const IDTreeEntry& b) { return a.nodeId < b.nodeId; }); |
| 279 |
no test coverage detected