Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright 2016 PDFium Authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "public/fpdf_structtree.h" | |
| 6 | |
| 7 #include "core/fpdfapi/page/cpdf_page.h" | |
| 8 #include "core/fpdfapi/parser/cpdf_dictionary.h" | |
| 9 #include "core/fpdfdoc/fpdf_tagged.h" | |
| 10 #include "fpdfsdk/fsdk_define.h" | |
| 11 | |
| 12 DLLEXPORT FPDF_STRUCTTREE STDCALL FPDF_StructTree_GetForPage(FPDF_PAGE page) { | |
| 13 CPDF_Page* pPage = CPDFPageFromFPDFPage(page); | |
| 14 if (!pPage) | |
| 15 return nullptr; | |
| 16 return IPDF_StructTree::LoadPage(pPage->m_pDocument, pPage->m_pFormDict); | |
|
dsinclair
2016/11/23 14:04:19
return page ? IPDF_StructTree::LoadPage(pPage->m_p
Lei Zhang
2016/11/23 19:49:55
It's kind of ugly:
return pPage
? I
| |
| 17 } | |
| 18 | |
| 19 DLLEXPORT void STDCALL FPDF_StructTree_Close(FPDF_STRUCTTREE struct_tree) { | |
| 20 delete reinterpret_cast<IPDF_StructTree*>(struct_tree); | |
| 21 } | |
|
dsinclair
2016/11/23 14:04:19
Add a anonymous ToStructTree helper and remove the
Lei Zhang
2016/11/23 19:49:55
Done.
| |
| 22 | |
| 23 DLLEXPORT int STDCALL | |
| 24 FPDF_StructTree_CountChildren(FPDF_STRUCTTREE struct_tree) { | |
| 25 IPDF_StructTree* tree = reinterpret_cast<IPDF_StructTree*>(struct_tree); | |
| 26 return tree ? tree->CountTopElements() : -1; | |
| 27 } | |
| 28 | |
| 29 DLLEXPORT FPDF_STRUCTELEMENT STDCALL | |
| 30 FPDF_StructTree_GetChildAtIndex(FPDF_STRUCTTREE struct_tree, int index) { | |
| 31 IPDF_StructTree* tree = reinterpret_cast<IPDF_StructTree*>(struct_tree); | |
| 32 if (!tree || index < 0 || index >= tree->CountTopElements()) | |
| 33 return nullptr; | |
| 34 return tree->GetTopElement(index); | |
| 35 } | |
| 36 | |
| 37 DLLEXPORT unsigned long STDCALL | |
| 38 FPDF_StructElement_GetAltText(FPDF_STRUCTELEMENT struct_element, | |
| 39 void* buffer, | |
| 40 unsigned long buflen) { | |
| 41 IPDF_StructElement* elem = | |
| 42 reinterpret_cast<IPDF_StructElement*>(struct_element); | |
| 43 if (!elem) | |
| 44 return 0; | |
| 45 | |
| 46 CPDF_Dictionary* dict = elem->GetDict(); | |
| 47 if (!dict) | |
| 48 return 0; | |
| 49 | |
| 50 CFX_WideString str = elem->GetDict()->GetUnicodeTextFor("Alt"); | |
|
Lei Zhang
2016/11/23 01:00:40
Made this code, the test, and the comments in the
| |
| 51 if (str.IsEmpty()) | |
| 52 return 0; | |
| 53 | |
| 54 CFX_ByteString encodedStr = str.UTF16LE_Encode(); | |
| 55 const unsigned long len = encodedStr.GetLength(); | |
|
Lei Zhang
2016/11/23 01:00:40
The length of the UTF16LE_Encoded string includes
| |
| 56 if (buffer || buflen >= len) | |
|
dsinclair
2016/11/23 14:04:19
This should be && yea?
Lei Zhang
2016/11/23 19:49:55
Ya....
Tom Sepez
2016/11/23 20:28:33
Also len < buflen may read better, in that < compa
Lei Zhang
2016/11/23 21:38:56
It's copy pasta, so I flipped all the obvious ones
| |
| 57 FXSYS_memcpy(buffer, encodedStr.c_str(), len); | |
| 58 return len; | |
| 59 } | |
| 60 | |
| 61 DLLEXPORT int STDCALL | |
| 62 FPDF_StructElement_CountChildren(FPDF_STRUCTELEMENT struct_element) { | |
| 63 IPDF_StructElement* elem = | |
| 64 reinterpret_cast<IPDF_StructElement*>(struct_element); | |
|
dsinclair
2016/11/23 14:04:18
ToStructElement helper?
Lei Zhang
2016/11/23 19:49:55
Done.
| |
| 65 return elem ? elem->CountKids() : -1; | |
| 66 } | |
| 67 | |
| 68 DLLEXPORT FPDF_STRUCTELEMENT STDCALL | |
| 69 FPDF_StructElement_GetChildAtIndex(FPDF_STRUCTELEMENT struct_element, | |
| 70 int index) { | |
| 71 IPDF_StructElement* elem = | |
| 72 reinterpret_cast<IPDF_StructElement*>(struct_element); | |
| 73 if (!elem || index < 0 || index >= elem->CountKids()) | |
| 74 return nullptr; | |
| 75 | |
| 76 CPDF_StructKid kid = elem->GetKid(index); | |
| 77 return kid.m_Type == CPDF_StructKid::Element ? kid.m_Element.m_pElement | |
| 78 : nullptr; | |
| 79 } | |
| OLD | NEW |