Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(301)

Side by Side Diff: fpdfsdk/fpdfdoc.cpp

Issue 2521843003: Add API for getting page labels. (Closed)
Patch Set: Add tests, distinguish between failure and empty label Created 4 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 "public/fpdf_doc.h" 7 #include "public/fpdf_doc.h"
8 8
9 #include <memory> 9 #include <memory>
10 #include <set> 10 #include <set>
11 11
12 #include "core/fpdfapi/page/cpdf_page.h" 12 #include "core/fpdfapi/page/cpdf_page.h"
13 #include "core/fpdfapi/parser/cpdf_array.h" 13 #include "core/fpdfapi/parser/cpdf_array.h"
14 #include "core/fpdfapi/parser/cpdf_document.h" 14 #include "core/fpdfapi/parser/cpdf_document.h"
15 #include "core/fpdfdoc/cpdf_bookmark.h" 15 #include "core/fpdfdoc/cpdf_bookmark.h"
16 #include "core/fpdfdoc/cpdf_bookmarktree.h" 16 #include "core/fpdfdoc/cpdf_bookmarktree.h"
17 #include "core/fpdfdoc/cpdf_dest.h" 17 #include "core/fpdfdoc/cpdf_dest.h"
18 #include "core/fpdfdoc/cpdf_pagelabel.h"
18 #include "fpdfsdk/fsdk_define.h" 19 #include "fpdfsdk/fsdk_define.h"
19 #include "third_party/base/stl_util.h" 20 #include "third_party/base/stl_util.h"
20 21
21 namespace { 22 namespace {
22 23
23 CPDF_Bookmark FindBookmark(const CPDF_BookmarkTree& tree, 24 CPDF_Bookmark FindBookmark(const CPDF_BookmarkTree& tree,
24 CPDF_Bookmark bookmark, 25 CPDF_Bookmark bookmark,
25 const CFX_WideString& title, 26 const CFX_WideString& title,
26 std::set<CPDF_Dictionary*>* visited) { 27 std::set<CPDF_Dictionary*>* visited) {
27 // Return if already checked to avoid circular calling. 28 // Return if already checked to avoid circular calling.
(...skipping 23 matching lines...) Expand all
51 if (!page) 52 if (!page)
52 return nullptr; 53 return nullptr;
53 54
54 CPDF_Document* pDoc = page->m_pDocument; 55 CPDF_Document* pDoc = page->m_pDocument;
55 std::unique_ptr<CPDF_LinkList>* pHolder = pDoc->LinksContext(); 56 std::unique_ptr<CPDF_LinkList>* pHolder = pDoc->LinksContext();
56 if (!pHolder->get()) 57 if (!pHolder->get())
57 pHolder->reset(new CPDF_LinkList); 58 pHolder->reset(new CPDF_LinkList);
58 return pHolder->get(); 59 return pHolder->get();
59 } 60 }
60 61
62 unsigned long Utf16EncodeMaybeCopyAndReturnLength(const CFX_WideString& text,
63 void* buffer,
64 unsigned long buflen) {
65 CFX_ByteString encodedText = text.UTF16LE_Encode();
66 unsigned long len = encodedText.GetLength();
67 if (buffer && buflen >= len)
68 FXSYS_memcpy(buffer, encodedText.c_str(), len);
69 return len;
70 }
71
61 } // namespace 72 } // namespace
62 73
63 DLLEXPORT FPDF_BOOKMARK STDCALL 74 DLLEXPORT FPDF_BOOKMARK STDCALL
64 FPDFBookmark_GetFirstChild(FPDF_DOCUMENT document, FPDF_BOOKMARK pDict) { 75 FPDFBookmark_GetFirstChild(FPDF_DOCUMENT document, FPDF_BOOKMARK pDict) {
65 CPDF_Document* pDoc = CPDFDocumentFromFPDFDocument(document); 76 CPDF_Document* pDoc = CPDFDocumentFromFPDFDocument(document);
66 if (!pDoc) 77 if (!pDoc)
67 return nullptr; 78 return nullptr;
68 CPDF_BookmarkTree tree(pDoc); 79 CPDF_BookmarkTree tree(pDoc);
69 CPDF_Bookmark bookmark = 80 CPDF_Bookmark bookmark =
70 CPDF_Bookmark(ToDictionary(static_cast<CPDF_Object*>(pDict))); 81 CPDF_Bookmark(ToDictionary(static_cast<CPDF_Object*>(pDict)));
(...skipping 13 matching lines...) Expand all
84 return tree.GetNextSibling(bookmark).GetDict(); 95 return tree.GetNextSibling(bookmark).GetDict();
85 } 96 }
86 97
87 DLLEXPORT unsigned long STDCALL FPDFBookmark_GetTitle(FPDF_BOOKMARK pDict, 98 DLLEXPORT unsigned long STDCALL FPDFBookmark_GetTitle(FPDF_BOOKMARK pDict,
88 void* buffer, 99 void* buffer,
89 unsigned long buflen) { 100 unsigned long buflen) {
90 if (!pDict) 101 if (!pDict)
91 return 0; 102 return 0;
92 CPDF_Bookmark bookmark(ToDictionary(static_cast<CPDF_Object*>(pDict))); 103 CPDF_Bookmark bookmark(ToDictionary(static_cast<CPDF_Object*>(pDict)));
93 CFX_WideString title = bookmark.GetTitle(); 104 CFX_WideString title = bookmark.GetTitle();
94 CFX_ByteString encodedTitle = title.UTF16LE_Encode(); 105 return Utf16EncodeMaybeCopyAndReturnLength(title, buffer, buflen);
95 unsigned long len = encodedTitle.GetLength();
96 if (buffer && buflen >= len) {
97 FXSYS_memcpy(buffer, encodedTitle.c_str(), len);
98 }
99 return len;
100 } 106 }
101 107
102 DLLEXPORT FPDF_BOOKMARK STDCALL FPDFBookmark_Find(FPDF_DOCUMENT document, 108 DLLEXPORT FPDF_BOOKMARK STDCALL FPDFBookmark_Find(FPDF_DOCUMENT document,
103 FPDF_WIDESTRING title) { 109 FPDF_WIDESTRING title) {
104 if (!title || title[0] == 0) 110 if (!title || title[0] == 0)
105 return nullptr; 111 return nullptr;
106 CPDF_Document* pDoc = CPDFDocumentFromFPDFDocument(document); 112 CPDF_Document* pDoc = CPDFDocumentFromFPDFDocument(document);
107 if (!pDoc) 113 if (!pDoc)
108 return nullptr; 114 return nullptr;
109 CPDF_BookmarkTree tree(pDoc); 115 CPDF_BookmarkTree tree(pDoc);
(...skipping 237 matching lines...) Expand 10 before | Expand all | Expand 10 after
347 } 353 }
348 354
349 DLLEXPORT FPDF_BOOL STDCALL FPDFLink_GetQuadPoints(FPDF_LINK linkAnnot, 355 DLLEXPORT FPDF_BOOL STDCALL FPDFLink_GetQuadPoints(FPDF_LINK linkAnnot,
350 int quadIndex, 356 int quadIndex,
351 FS_QUADPOINTSF* quadPoints) { 357 FS_QUADPOINTSF* quadPoints) {
352 if (!linkAnnot || !quadPoints) 358 if (!linkAnnot || !quadPoints)
353 return false; 359 return false;
354 CPDF_Dictionary* pAnnotDict = 360 CPDF_Dictionary* pAnnotDict =
355 ToDictionary(static_cast<CPDF_Object*>(linkAnnot)); 361 ToDictionary(static_cast<CPDF_Object*>(linkAnnot));
356 CPDF_Array* pArray = pAnnotDict->GetArrayFor("QuadPoints"); 362 CPDF_Array* pArray = pAnnotDict->GetArrayFor("QuadPoints");
357 if (pArray) { 363 if (!pArray)
358 if (quadIndex < 0 || 364 return false;
359 static_cast<size_t>(quadIndex) >= pArray->GetCount() / 8 || 365
360 (static_cast<size_t>(quadIndex * 8 + 7) >= pArray->GetCount())) 366 if (quadIndex < 0 ||
361 return false; 367 static_cast<size_t>(quadIndex) >= pArray->GetCount() / 8 ||
362 quadPoints->x1 = pArray->GetNumberAt(quadIndex * 8); 368 (static_cast<size_t>(quadIndex * 8 + 7) >= pArray->GetCount())) {
363 quadPoints->y1 = pArray->GetNumberAt(quadIndex * 8 + 1); 369 return false;
364 quadPoints->x2 = pArray->GetNumberAt(quadIndex * 8 + 2);
365 quadPoints->y2 = pArray->GetNumberAt(quadIndex * 8 + 3);
366 quadPoints->x3 = pArray->GetNumberAt(quadIndex * 8 + 4);
367 quadPoints->y3 = pArray->GetNumberAt(quadIndex * 8 + 5);
368 quadPoints->x4 = pArray->GetNumberAt(quadIndex * 8 + 6);
369 quadPoints->y4 = pArray->GetNumberAt(quadIndex * 8 + 7);
370 return true;
371 } 370 }
372 return false; 371
372 quadPoints->x1 = pArray->GetNumberAt(quadIndex * 8);
373 quadPoints->y1 = pArray->GetNumberAt(quadIndex * 8 + 1);
374 quadPoints->x2 = pArray->GetNumberAt(quadIndex * 8 + 2);
375 quadPoints->y2 = pArray->GetNumberAt(quadIndex * 8 + 3);
376 quadPoints->x3 = pArray->GetNumberAt(quadIndex * 8 + 4);
377 quadPoints->y3 = pArray->GetNumberAt(quadIndex * 8 + 5);
378 quadPoints->x4 = pArray->GetNumberAt(quadIndex * 8 + 6);
379 quadPoints->y4 = pArray->GetNumberAt(quadIndex * 8 + 7);
380 return true;
373 } 381 }
374 382
375 DLLEXPORT unsigned long STDCALL FPDF_GetMetaText(FPDF_DOCUMENT doc, 383 DLLEXPORT unsigned long STDCALL FPDF_GetMetaText(FPDF_DOCUMENT document,
376 FPDF_BYTESTRING tag, 384 FPDF_BYTESTRING tag,
377 void* buffer, 385 void* buffer,
378 unsigned long buflen) { 386 unsigned long buflen) {
379 if (!tag) 387 if (!tag)
380 return 0; 388 return 0;
381 CPDF_Document* pDoc = CPDFDocumentFromFPDFDocument(doc); 389 CPDF_Document* pDoc = CPDFDocumentFromFPDFDocument(document);
382 if (!pDoc) 390 if (!pDoc)
383 return 0; 391 return 0;
384 CPDF_Dictionary* pInfo = pDoc->GetInfo(); 392 CPDF_Dictionary* pInfo = pDoc->GetInfo();
385 if (!pInfo) 393 if (!pInfo)
386 return 0; 394 return 0;
387 CFX_WideString text = pInfo->GetUnicodeTextFor(tag); 395 CFX_WideString text = pInfo->GetUnicodeTextFor(tag);
388 // Use UTF-16LE encoding 396 return Utf16EncodeMaybeCopyAndReturnLength(text, buffer, buflen);
389 CFX_ByteString encodedText = text.UTF16LE_Encode();
390 unsigned long len = encodedText.GetLength();
391 if (buffer && buflen >= len) {
392 FXSYS_memcpy(buffer, encodedText.c_str(), len);
393 }
394 return len;
395 } 397 }
398
399 DLLEXPORT unsigned long STDCALL FPDF_GetPagelLabel(FPDF_DOCUMENT document,
400 int page_index,
401 void* buffer,
402 unsigned long buflen) {
403 if (page_index < 0)
404 return 0;
405
406 // CPDF_PageLabel can deal with NULL |document|.
407 CPDF_PageLabel label(CPDFDocumentFromFPDFDocument(document));
408 CFX_WideString str;
409 if (!label.GetLabel(page_index, &str))
410 return 0;
411 return Utf16EncodeMaybeCopyAndReturnLength(str, buffer, buflen);
412 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698