OLD | NEW |
1 // Copyright 2014 PDFium Authors. All rights reserved. | 1 // Copyright 2014 PDFium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com | 5 // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com |
6 | 6 |
7 #include "../../include/fpdfdoc/fpdf_doc.h" | 7 #include "../../include/fpdfdoc/fpdf_doc.h" |
8 | 8 |
| 9 CPDF_LinkList::CPDF_LinkList() {} |
| 10 |
9 CPDF_LinkList::~CPDF_LinkList() { | 11 CPDF_LinkList::~CPDF_LinkList() { |
10 FX_POSITION pos = m_PageMap.GetStartPosition(); | |
11 while (pos) { | |
12 void* key; | |
13 void* value; | |
14 m_PageMap.GetNextAssoc(pos, key, value); | |
15 delete (CFX_PtrArray*)value; | |
16 } | |
17 } | 12 } |
18 CFX_PtrArray* CPDF_LinkList::GetPageLinks(CPDF_Page* pPage) { | 13 |
| 14 const std::vector<CPDF_Dictionary*>* CPDF_LinkList::GetPageLinks( |
| 15 CPDF_Page* pPage) { |
19 FX_DWORD objnum = pPage->m_pFormDict->GetObjNum(); | 16 FX_DWORD objnum = pPage->m_pFormDict->GetObjNum(); |
20 if (objnum == 0) { | 17 if (objnum == 0) |
21 return NULL; | 18 return nullptr; |
22 } | 19 |
23 CFX_PtrArray* pPageLinkList = NULL; | 20 auto it = m_PageMap.find(objnum); |
24 if (!m_PageMap.Lookup((void*)(uintptr_t)objnum, (void*&)pPageLinkList)) { | 21 if (it != m_PageMap.end()) |
25 pPageLinkList = new CFX_PtrArray; | 22 return &it->second; |
26 m_PageMap.SetAt((void*)(uintptr_t)objnum, pPageLinkList); | 23 |
27 LoadPageLinks(pPage, pPageLinkList); | 24 // std::map::operator[] forces the creation of a map entry. |
28 } | 25 std::vector<CPDF_Dictionary*>& page_link_list = m_PageMap[objnum]; |
29 return pPageLinkList; | 26 LoadPageLinks(pPage, &page_link_list); |
| 27 return &page_link_list; |
30 } | 28 } |
31 int CPDF_LinkList::CountLinks(CPDF_Page* pPage) { | 29 |
32 CFX_PtrArray* pPageLinkList = GetPageLinks(pPage); | |
33 if (pPageLinkList == NULL) { | |
34 return 0; | |
35 } | |
36 return pPageLinkList->GetSize(); | |
37 } | |
38 CPDF_Link CPDF_LinkList::GetLink(CPDF_Page* pPage, int index) { | |
39 CFX_PtrArray* pPageLinkList = GetPageLinks(pPage); | |
40 if (!pPageLinkList) { | |
41 return CPDF_Link(); | |
42 } | |
43 return CPDF_Link((CPDF_Dictionary*)pPageLinkList->GetAt(index)); | |
44 } | |
45 CPDF_Link CPDF_LinkList::GetLinkAtPoint(CPDF_Page* pPage, | 30 CPDF_Link CPDF_LinkList::GetLinkAtPoint(CPDF_Page* pPage, |
46 FX_FLOAT pdf_x, | 31 FX_FLOAT pdf_x, |
47 FX_FLOAT pdf_y) { | 32 FX_FLOAT pdf_y, |
48 CFX_PtrArray* pPageLinkList = GetPageLinks(pPage); | 33 int* z_order) { |
49 if (!pPageLinkList) { | 34 const std::vector<CPDF_Dictionary*>* pPageLinkList = GetPageLinks(pPage); |
| 35 if (!pPageLinkList) |
50 return CPDF_Link(); | 36 return CPDF_Link(); |
51 } | 37 |
52 int size = pPageLinkList->GetSize(); | 38 for (size_t i = pPageLinkList->size(); i > 0; --i) { |
53 for (int i = size - 1; i >= 0; --i) { | 39 size_t annot_index = i - 1; |
54 CPDF_Link link((CPDF_Dictionary*)pPageLinkList->GetAt(i)); | 40 CPDF_Dictionary* pAnnot = (*pPageLinkList)[annot_index]; |
| 41 if (!pAnnot) |
| 42 continue; |
| 43 |
| 44 CPDF_Link link(pAnnot); |
55 CPDF_Rect rect = link.GetRect(); | 45 CPDF_Rect rect = link.GetRect(); |
56 if (rect.Contains(pdf_x, pdf_y)) { | 46 if (!rect.Contains(pdf_x, pdf_y)) |
57 return link; | 47 continue; |
58 } | 48 |
| 49 if (z_order) |
| 50 *z_order = annot_index; |
| 51 return link; |
59 } | 52 } |
60 return CPDF_Link(); | 53 return CPDF_Link(); |
61 } | 54 } |
62 void CPDF_LinkList::LoadPageLinks(CPDF_Page* pPage, CFX_PtrArray* pList) { | 55 |
| 56 void CPDF_LinkList::LoadPageLinks(CPDF_Page* pPage, |
| 57 std::vector<CPDF_Dictionary*>* pList) { |
63 CPDF_Array* pAnnotList = pPage->m_pFormDict->GetArray("Annots"); | 58 CPDF_Array* pAnnotList = pPage->m_pFormDict->GetArray("Annots"); |
64 if (pAnnotList == NULL) { | 59 if (!pAnnotList) |
65 return; | 60 return; |
66 } | 61 |
67 for (FX_DWORD i = 0; i < pAnnotList->GetCount(); i++) { | 62 for (FX_DWORD i = 0; i < pAnnotList->GetCount(); ++i) { |
68 CPDF_Dictionary* pAnnot = pAnnotList->GetDict(i); | 63 CPDF_Dictionary* pAnnot = pAnnotList->GetDict(i); |
69 if (pAnnot == NULL) { | 64 bool add_link = (pAnnot && pAnnot->GetString("Subtype") == "Link"); |
70 continue; | 65 // Add non-links as nullptrs to preserve z-order. |
71 } | 66 pList->push_back(add_link ? pAnnot : nullptr); |
72 if (pAnnot->GetString("Subtype") != "Link") { | |
73 continue; | |
74 } | |
75 pList->Add(pAnnot); | |
76 } | 67 } |
77 } | 68 } |
| 69 |
78 CPDF_Rect CPDF_Link::GetRect() { | 70 CPDF_Rect CPDF_Link::GetRect() { |
79 return m_pDict->GetRect("Rect"); | 71 return m_pDict->GetRect("Rect"); |
80 } | 72 } |
81 CPDF_Dest CPDF_Link::GetDest(CPDF_Document* pDoc) { | 73 CPDF_Dest CPDF_Link::GetDest(CPDF_Document* pDoc) { |
82 CPDF_Object* pDest = m_pDict->GetElementValue("Dest"); | 74 CPDF_Object* pDest = m_pDict->GetElementValue("Dest"); |
83 if (pDest == NULL) { | 75 if (pDest == NULL) { |
84 return CPDF_Dest(); | 76 return CPDF_Dest(); |
85 } | 77 } |
86 if (pDest->GetType() == PDFOBJ_STRING || pDest->GetType() == PDFOBJ_NAME) { | 78 if (pDest->GetType() == PDFOBJ_STRING || pDest->GetType() == PDFOBJ_NAME) { |
87 CPDF_NameTree name_tree(pDoc, FX_BSTRC("Dests")); | 79 CPDF_NameTree name_tree(pDoc, FX_BSTRC("Dests")); |
88 CFX_ByteStringC name = pDest->GetString(); | 80 CFX_ByteStringC name = pDest->GetString(); |
89 return CPDF_Dest(name_tree.LookupNamedDest(pDoc, name)); | 81 return CPDF_Dest(name_tree.LookupNamedDest(pDoc, name)); |
90 } | 82 } |
91 if (pDest->GetType() == PDFOBJ_ARRAY) { | 83 if (pDest->GetType() == PDFOBJ_ARRAY) { |
92 return CPDF_Dest((CPDF_Array*)pDest); | 84 return CPDF_Dest((CPDF_Array*)pDest); |
93 } | 85 } |
94 return CPDF_Dest(); | 86 return CPDF_Dest(); |
95 } | 87 } |
96 CPDF_Action CPDF_Link::GetAction() { | 88 CPDF_Action CPDF_Link::GetAction() { |
97 return CPDF_Action(m_pDict->GetDict("A")); | 89 return CPDF_Action(m_pDict->GetDict("A")); |
98 } | 90 } |
OLD | NEW |