MCPcopy Create free account
hub / github.com/Tencent/tgfx / makeStructTreeRoot

Method makeStructTreeRoot

src/pdf/PDFTag.cpp:222–302  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

220}
221
222PDFIndirectReference 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

Callers 1

onCloseMethod · 0.80

Calls 15

can_discardFunction · 0.85
MakePDFArrayFunction · 0.85
reserveRefMethod · 0.80
pageCountMethod · 0.80
insertRefMethod · 0.80
insertIntMethod · 0.80
appendRefMethod · 0.80
insertObjectMethod · 0.80
appendTextStringMethod · 0.80
getMethod · 0.45
sizeMethod · 0.45

Tested by

no test coverage detected