From 476cd69a6f5c5096a3145e0c4d567010f37739c3 Mon Sep 17 00:00:00 2001 From: Bo Xu Date: Sat, 10 Jan 2015 22:52:59 -0800 Subject: Add APIs for getting bookmarks and named destinations. R=tsepez@chromium.org Review URL: https://codereview.chromium.org/834703002 --- fpdfsdk/include/fpdfdoc.h | 39 ++++++++++++++++++++++++++ fpdfsdk/include/fpdfview.h | 26 +++++++++++++++++ fpdfsdk/src/fpdfdoc.cpp | 36 ++++++++++++++++++++++++ fpdfsdk/src/fpdfview.cpp | 69 ++++++++++++++++++++++++++++++++++++++++++++-- 4 files changed, 168 insertions(+), 2 deletions(-) diff --git a/fpdfsdk/include/fpdfdoc.h b/fpdfsdk/include/fpdfdoc.h index 54ede4037a..1587ef4069 100644 --- a/fpdfsdk/include/fpdfdoc.h +++ b/fpdfsdk/include/fpdfdoc.h @@ -14,6 +14,45 @@ extern "C" { #endif +// Function: FPDFBookmark_GetFirstChild +// Get the first child of a bookmark item, or the first top level bookmark item. +// Parameters: +// document - Handle to the document. Returned by FPDF_LoadDocument or FPDF_LoadMemDocument. +// bookmark - Handle to the current bookmark. Can be NULL if you want to get the first top level item. +// Return value: +// Handle to the first child or top level bookmark item. NULL if no child or top level bookmark found. +// +DLLEXPORT FPDF_BOOKMARK STDCALL FPDFBookmark_GetFirstChild(FPDF_DOCUMENT document, FPDF_BOOKMARK bookmark); + +// Function: FPDFBookmark_GetNextSibling +// Get next bookmark item at the same level. +// Parameters: +// document - Handle to the document. Returned by FPDF_LoadDocument or FPDF_LoadMemDocument. +// bookmark - Handle to the current bookmark. Cannot be NULL. +// Return value: +// Handle to the next bookmark item at the same level. NULL if this is the last bookmark at this level. +// +DLLEXPORT FPDF_BOOKMARK STDCALL FPDFBookmark_GetNextSibling(FPDF_DOCUMENT document, FPDF_BOOKMARK bookmark); + +// Function: FPDFBookmark_GetTitle +// Get title of a bookmark. +// Parameters: +// bookmark - Handle to the bookmark. +// buffer - Buffer for the title. Can be NULL. +// buflen - The length of the buffer in bytes. Can be 0. +// Return value: +// Number of bytes the title consumes, including trailing zeros. +// Comments: +// Regardless of the platform, the title is always in UTF-16LE encoding. That means the buffer +// can be treated as an array of WORD (on Intel and compatible CPUs), each WORD representing the Unicode of +// a character(some special Unicode may take 2 WORDs).The string is followed by two bytes of zero +// indicating the end of the string. +// +// The return value always indicates the number of bytes required for the buffer, even if no buffer is specified +// or the buffer size is less then required. In these cases, the buffer will not be modified. +// +DLLEXPORT unsigned long STDCALL FPDFBookmark_GetTitle(FPDF_BOOKMARK bookmark, void* buffer, unsigned long buflen); + // Function: FPDFBookmark_Find // Find a bookmark in the document, using the bookmark title. // Parameters: diff --git a/fpdfsdk/include/fpdfview.h b/fpdfsdk/include/fpdfview.h index ddf359c127..e36d54e0a2 100644 --- a/fpdfsdk/include/fpdfview.h +++ b/fpdfsdk/include/fpdfview.h @@ -602,6 +602,14 @@ DLLEXPORT FPDF_PAGERANGE STDCALL FPDF_VIEWERREF_GetPrintPageRange(FPDF_DOCUMENT // DLLEXPORT FPDF_DUPLEXTYPE STDCALL FPDF_VIEWERREF_GetDuplex(FPDF_DOCUMENT document); +// Function: FPDF_CountNamedDests +// Get the count of named destinations in the PDF document. +// Parameters: +// document - Handle to a document +// Return value: +// The count of named destinations. +DLLEXPORT FPDF_DWORD STDCALL FPDF_CountNamedDests(FPDF_DOCUMENT document); + // Function: FPDF_GetNamedDestByName // get a special dest handle by the index. // Parameters: @@ -612,6 +620,24 @@ DLLEXPORT FPDF_DUPLEXTYPE STDCALL FPDF_VIEWERREF_GetDuplex(FPDF_DOCUMENT documen // DLLEXPORT FPDF_DEST STDCALL FPDF_GetNamedDestByName(FPDF_DOCUMENT document,FPDF_BYTESTRING name); +// Function: FPDF_GetNamedDest +// Get the specified named destinations of the PDF document by index. +// Parameters: +// document - Handle to a document +// index - The index of named destination. +// buffer - The buffer to obtain destination name, used as wchar_t*. +// buflen - The length of the buffer in byte. +// Return value: +// The destination handle of a named destination, NULL when retrieving the length. +// Comments: +// Call this function twice to get the name of the named destination: +// 1) First time pass in |buffer| as NULL and get buflen. +// 2) Second time pass in allocated |buffer| and buflen to retrieve |buffer|, which should be used as wchar_t*. +// If buflen is not sufficiently large, it will be returned as -1. +// +DLLEXPORT FPDF_DEST STDCALL FPDF_GetNamedDest(FPDF_DOCUMENT document, int index, void* buffer, unsigned long& buflen); + + #ifdef __cplusplus }; #endif diff --git a/fpdfsdk/src/fpdfdoc.cpp b/fpdfsdk/src/fpdfdoc.cpp index ab2351f9d8..feb7cfbde5 100644 --- a/fpdfsdk/src/fpdfdoc.cpp +++ b/fpdfsdk/src/fpdfdoc.cpp @@ -27,6 +27,42 @@ static CPDF_Bookmark FindBookmark(const CPDF_BookmarkTree& tree, CPDF_Bookmark b return CPDF_Bookmark(); } +DLLEXPORT FPDF_BOOKMARK STDCALL FPDFBookmark_GetFirstChild(FPDF_DOCUMENT document, FPDF_BOOKMARK pDict) +{ + if (!document || !pDict) + return NULL; + CPDF_Document* pDoc = (CPDF_Document*)document; + CPDF_BookmarkTree tree(pDoc); + CPDF_Bookmark bookmark = CPDF_Bookmark((CPDF_Dictionary*)pDict); + return tree.GetFirstChild(bookmark).GetDict(); +} + +DLLEXPORT FPDF_BOOKMARK STDCALL FPDFBookmark_GetNextSibling(FPDF_DOCUMENT document, FPDF_BOOKMARK pDict) +{ + if (!document || !pDict) + return NULL; + CPDF_Document* pDoc = (CPDF_Document*)document; + CPDF_BookmarkTree tree(pDoc); + CPDF_Bookmark bookmark = CPDF_Bookmark((CPDF_Dictionary*)pDict); + return tree.GetNextSibling(bookmark).GetDict(); +} + +DLLEXPORT unsigned long STDCALL FPDFBookmark_GetTitle(FPDF_BOOKMARK pDict, void* buffer, unsigned long buflen) +{ + if (!pDict) + return 0; + CPDF_Bookmark bookmark((CPDF_Dictionary*)pDict); + CFX_WideString title = bookmark.GetTitle(); + CFX_ByteString encodedTitle = title.UTF16LE_Encode(FALSE); + unsigned long len = encodedTitle.GetLength(); + if (buffer && buflen >= len + 2) { + FXSYS_memcpy(buffer, encodedTitle.c_str(), len); + ((FX_BYTE*)buffer)[len] = 0; + ((FX_BYTE*)buffer)[len + 1] = 0; + } + return len + 2; +} + DLLEXPORT FPDF_BOOKMARK STDCALL FPDFBookmark_Find(FPDF_DOCUMENT document, FPDF_WIDESTRING title) { if (!document) diff --git a/fpdfsdk/src/fpdfview.cpp b/fpdfsdk/src/fpdfview.cpp index 653702382c..43feaf4d5a 100644 --- a/fpdfsdk/src/fpdfview.cpp +++ b/fpdfsdk/src/fpdfview.cpp @@ -789,14 +789,79 @@ DLLEXPORT FPDF_DUPLEXTYPE STDCALL FPDF_VIEWERREF_GetDuplex(FPDF_DOCUMENT documen return DuplexUndefined; } +DLLEXPORT FPDF_DWORD STDCALL FPDF_CountNamedDests(FPDF_DOCUMENT document) +{ + if (!document) return 0; + CPDF_Document* pDoc = (CPDF_Document*)document; + + CPDF_Dictionary* pRoot = pDoc->GetRoot(); + if (!pRoot) return 0; + + CPDF_NameTree nameTree(pDoc, FX_BSTRC("Dests")); + int count = nameTree.GetCount(); + CPDF_Dictionary* pDest = pRoot->GetDict(FX_BSTRC("Dests")); + if (pDest) + count += pDest->GetCount(); + return count; +} + DLLEXPORT FPDF_DEST STDCALL FPDF_GetNamedDestByName(FPDF_DOCUMENT document,FPDF_BYTESTRING name) { - if (document == NULL) + if (!document) return NULL; - if (name == NULL || name[0] == 0) + if (!name || name[0] == 0) return NULL; CPDF_Document* pDoc = (CPDF_Document*)document; CPDF_NameTree name_tree(pDoc, FX_BSTRC("Dests")); return name_tree.LookupNamedDest(pDoc, name); } + +DLLEXPORT FPDF_DEST STDCALL FPDF_GetNamedDest(FPDF_DOCUMENT document, int index, void* buffer, unsigned long& buflen) +{ + if (!buffer) + buflen = 0; + if (!document || index < 0) return NULL; + CPDF_Document* pDoc = (CPDF_Document*)document; + + CPDF_Dictionary* pRoot = pDoc->GetRoot(); + if (!pRoot) return NULL; + + CPDF_Object* pDestObj = NULL; + CFX_ByteString bsName; + CPDF_NameTree nameTree(pDoc, FX_BSTRC("Dests")); + int count = nameTree.GetCount(); + if (index >= count) { + CPDF_Dictionary* pDest = pRoot->GetDict(FX_BSTRC("Dests")); + if (!pDest) return NULL; + if (index >= count + pDest->GetCount()) return NULL; + index -= count; + FX_POSITION pos = pDest->GetStartPos(); + int i = 0; + while (pos) { + pDestObj = pDest->GetNextElement(pos, bsName); + if (!pDestObj) continue; + if (i == index) break; + i++; + } + } else { + pDestObj = nameTree.LookupValue(index, bsName); + } + if (!pDestObj) return NULL; + if (pDestObj->GetType() == PDFOBJ_DICTIONARY) + pDestObj = ((CPDF_Dictionary*)pDestObj)->GetArray(FX_BSTRC("D")); + if (pDestObj->GetType() != PDFOBJ_ARRAY) return NULL; + CFX_WideString wsName = PDF_DecodeText(bsName); + CFX_ByteString utf16Name = wsName.UTF16LE_Encode(); + unsigned int len = utf16Name.GetLength(); + if (!buffer) { + buflen = len + 2; + } else if (buflen >= len + 2) { + memcpy(buffer, utf16Name.c_str(), len); + ((FX_BYTE*)buffer)[len] = 0; + ((FX_BYTE*)buffer)[len + 1] = 0; + } else { + len = -1; + } + return (FPDF_DEST)pDestObj; +} -- cgit v1.2.3