John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 1 | // Copyright 2014 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. |
Lei Zhang | a6d9f0e | 2015-06-13 00:48:38 -0700 | [diff] [blame] | 4 | |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 5 | // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com |
| 6 | |
Lei Zhang | b4e7f30 | 2015-11-06 15:52:32 -0800 | [diff] [blame] | 7 | #include "public/fpdf_text.h" |
| 8 | |
Dan Sinclair | 85c8e7f | 2016-11-21 13:50:32 -0500 | [diff] [blame] | 9 | #include <algorithm> |
| 10 | #include <vector> |
| 11 | |
dsinclair | 41872fa | 2016-10-04 11:29:35 -0700 | [diff] [blame] | 12 | #include "core/fpdfapi/page/cpdf_page.h" |
dsinclair | 1727aee | 2016-09-29 13:12:56 -0700 | [diff] [blame] | 13 | #include "core/fpdfdoc/cpdf_viewerpreferences.h" |
dsinclair | e030786 | 2016-09-29 13:25:38 -0700 | [diff] [blame] | 14 | #include "core/fpdftext/cpdf_linkextract.h" |
| 15 | #include "core/fpdftext/cpdf_textpage.h" |
| 16 | #include "core/fpdftext/cpdf_textpagefind.h" |
dsinclair | 114e46a | 2016-09-29 17:18:21 -0700 | [diff] [blame] | 17 | #include "fpdfsdk/fsdk_define.h" |
tsepez | 6914118 | 2016-04-21 10:43:39 -0700 | [diff] [blame] | 18 | #include "third_party/base/numerics/safe_conversions.h" |
tsepez | df964df | 2016-04-21 12:09:41 -0700 | [diff] [blame] | 19 | #include "third_party/base/stl_util.h" |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 20 | |
Tom Sepez | 40e9ff3 | 2015-11-30 12:39:54 -0800 | [diff] [blame] | 21 | #ifdef PDF_ENABLE_XFA |
dsinclair | 521b750 | 2016-11-02 13:02:28 -0700 | [diff] [blame] | 22 | #include "fpdfsdk/fpdfxfa/cpdfxfa_context.h" |
dsinclair | 4d29e78 | 2016-10-04 14:02:47 -0700 | [diff] [blame] | 23 | #include "fpdfsdk/fpdfxfa/cpdfxfa_page.h" |
Tom Sepez | 40e9ff3 | 2015-11-30 12:39:54 -0800 | [diff] [blame] | 24 | #endif // PDF_ENABLE_XFA |
| 25 | |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 26 | #ifdef _WIN32 |
| 27 | #include <tchar.h> |
| 28 | #endif |
| 29 | |
dsinclair | a28ae38 | 2016-04-19 10:39:24 -0700 | [diff] [blame] | 30 | namespace { |
| 31 | |
| 32 | CPDF_TextPage* CPDFTextPageFromFPDFTextPage(FPDF_TEXTPAGE text_page) { |
| 33 | return static_cast<CPDF_TextPage*>(text_page); |
| 34 | } |
| 35 | |
| 36 | CPDF_TextPageFind* CPDFTextPageFindFromFPDFSchHandle(FPDF_SCHHANDLE handle) { |
| 37 | return static_cast<CPDF_TextPageFind*>(handle); |
| 38 | } |
| 39 | |
| 40 | CPDF_LinkExtract* CPDFLinkExtractFromFPDFPageLink(FPDF_PAGELINK link) { |
| 41 | return static_cast<CPDF_LinkExtract*>(link); |
| 42 | } |
| 43 | |
| 44 | } // namespace |
| 45 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 46 | DLLEXPORT FPDF_TEXTPAGE STDCALL FPDFText_LoadPage(FPDF_PAGE page) { |
Tom Sepez | 1b24628 | 2015-11-25 15:15:31 -0800 | [diff] [blame] | 47 | CPDF_Page* pPDFPage = CPDFPageFromFPDFPage(page); |
| 48 | if (!pPDFPage) |
| 49 | return nullptr; |
dsinclair | a28ae38 | 2016-04-19 10:39:24 -0700 | [diff] [blame] | 50 | |
Tom Sepez | 40e9ff3 | 2015-11-30 12:39:54 -0800 | [diff] [blame] | 51 | #ifdef PDF_ENABLE_XFA |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 52 | CPDFXFA_Page* pPage = (CPDFXFA_Page*)page; |
dsinclair | 521b750 | 2016-11-02 13:02:28 -0700 | [diff] [blame] | 53 | CPDFXFA_Context* pContext = pPage->GetContext(); |
| 54 | CPDF_ViewerPreferences viewRef(pContext->GetPDFDoc()); |
Tom Sepez | 40e9ff3 | 2015-11-30 12:39:54 -0800 | [diff] [blame] | 55 | #else // PDF_ENABLE_XFA |
| 56 | CPDF_ViewerPreferences viewRef(pPDFPage->m_pDocument); |
| 57 | #endif // PDF_ENABLE_XFA |
dsinclair | a28ae38 | 2016-04-19 10:39:24 -0700 | [diff] [blame] | 58 | |
dsinclair | 8f4bf9a | 2016-05-04 13:51:51 -0700 | [diff] [blame] | 59 | CPDF_TextPage* textpage = new CPDF_TextPage( |
| 60 | pPDFPage, viewRef.IsDirectionR2L() ? FPDFText_Direction::Right |
| 61 | : FPDFText_Direction::Left); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 62 | textpage->ParseTextPage(); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 63 | return textpage; |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 64 | } |
dsinclair | a28ae38 | 2016-04-19 10:39:24 -0700 | [diff] [blame] | 65 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 66 | DLLEXPORT void STDCALL FPDFText_ClosePage(FPDF_TEXTPAGE text_page) { |
dsinclair | a28ae38 | 2016-04-19 10:39:24 -0700 | [diff] [blame] | 67 | delete CPDFTextPageFromFPDFTextPage(text_page); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 68 | } |
dsinclair | a28ae38 | 2016-04-19 10:39:24 -0700 | [diff] [blame] | 69 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 70 | DLLEXPORT int STDCALL FPDFText_CountChars(FPDF_TEXTPAGE text_page) { |
| 71 | if (!text_page) |
| 72 | return -1; |
dsinclair | a28ae38 | 2016-04-19 10:39:24 -0700 | [diff] [blame] | 73 | |
| 74 | CPDF_TextPage* textpage = CPDFTextPageFromFPDFTextPage(text_page); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 75 | return textpage->CountChars(); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 76 | } |
Lei Zhang | 0f2ea02 | 2016-01-11 12:01:23 -0800 | [diff] [blame] | 77 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 78 | DLLEXPORT unsigned int STDCALL FPDFText_GetUnicode(FPDF_TEXTPAGE text_page, |
| 79 | int index) { |
| 80 | if (!text_page) |
Wei Li | d4e8f12 | 2016-03-21 11:20:44 -0700 | [diff] [blame] | 81 | return 0; |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 82 | |
dsinclair | a28ae38 | 2016-04-19 10:39:24 -0700 | [diff] [blame] | 83 | CPDF_TextPage* textpage = CPDFTextPageFromFPDFTextPage(text_page); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 84 | if (index < 0 || index >= textpage->CountChars()) |
| 85 | return 0; |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 86 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 87 | FPDF_CHAR_INFO charinfo; |
Lei Zhang | 0f2ea02 | 2016-01-11 12:01:23 -0800 | [diff] [blame] | 88 | textpage->GetCharInfo(index, &charinfo); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 89 | return charinfo.m_Unicode; |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 90 | } |
Lei Zhang | 0f2ea02 | 2016-01-11 12:01:23 -0800 | [diff] [blame] | 91 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 92 | DLLEXPORT double STDCALL FPDFText_GetFontSize(FPDF_TEXTPAGE text_page, |
| 93 | int index) { |
| 94 | if (!text_page) |
| 95 | return 0; |
dsinclair | a28ae38 | 2016-04-19 10:39:24 -0700 | [diff] [blame] | 96 | CPDF_TextPage* textpage = CPDFTextPageFromFPDFTextPage(text_page); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 97 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 98 | if (index < 0 || index >= textpage->CountChars()) |
| 99 | return 0; |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 100 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 101 | FPDF_CHAR_INFO charinfo; |
Lei Zhang | 0f2ea02 | 2016-01-11 12:01:23 -0800 | [diff] [blame] | 102 | textpage->GetCharInfo(index, &charinfo); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 103 | return charinfo.m_FontSize; |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 104 | } |
| 105 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 106 | DLLEXPORT void STDCALL FPDFText_GetCharBox(FPDF_TEXTPAGE text_page, |
| 107 | int index, |
| 108 | double* left, |
| 109 | double* right, |
| 110 | double* bottom, |
| 111 | double* top) { |
| 112 | if (!text_page) |
| 113 | return; |
dsinclair | a28ae38 | 2016-04-19 10:39:24 -0700 | [diff] [blame] | 114 | CPDF_TextPage* textpage = CPDFTextPageFromFPDFTextPage(text_page); |
Lei Zhang | a6d9f0e | 2015-06-13 00:48:38 -0700 | [diff] [blame] | 115 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 116 | if (index < 0 || index >= textpage->CountChars()) |
| 117 | return; |
| 118 | FPDF_CHAR_INFO charinfo; |
Lei Zhang | 0f2ea02 | 2016-01-11 12:01:23 -0800 | [diff] [blame] | 119 | textpage->GetCharInfo(index, &charinfo); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 120 | *left = charinfo.m_CharBox.left; |
| 121 | *right = charinfo.m_CharBox.right; |
| 122 | *bottom = charinfo.m_CharBox.bottom; |
| 123 | *top = charinfo.m_CharBox.top; |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 124 | } |
| 125 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 126 | // select |
| 127 | DLLEXPORT int STDCALL FPDFText_GetCharIndexAtPos(FPDF_TEXTPAGE text_page, |
| 128 | double x, |
| 129 | double y, |
Lei Zhang | 38a5a39 | 2015-08-13 17:52:16 -0700 | [diff] [blame] | 130 | double xTolerance, |
| 131 | double yTolerance) { |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 132 | if (!text_page) |
| 133 | return -3; |
dsinclair | a28ae38 | 2016-04-19 10:39:24 -0700 | [diff] [blame] | 134 | |
| 135 | CPDF_TextPage* textpage = CPDFTextPageFromFPDFTextPage(text_page); |
Lei Zhang | 38a5a39 | 2015-08-13 17:52:16 -0700 | [diff] [blame] | 136 | return textpage->GetIndexAtPos((FX_FLOAT)x, (FX_FLOAT)y, (FX_FLOAT)xTolerance, |
| 137 | (FX_FLOAT)yTolerance); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 138 | } |
| 139 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 140 | DLLEXPORT int STDCALL FPDFText_GetText(FPDF_TEXTPAGE text_page, |
| 141 | int start, |
| 142 | int count, |
| 143 | unsigned short* result) { |
| 144 | if (!text_page) |
| 145 | return 0; |
Lei Zhang | a6d9f0e | 2015-06-13 00:48:38 -0700 | [diff] [blame] | 146 | |
dsinclair | a28ae38 | 2016-04-19 10:39:24 -0700 | [diff] [blame] | 147 | CPDF_TextPage* textpage = CPDFTextPageFromFPDFTextPage(text_page); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 148 | if (start >= textpage->CountChars()) |
| 149 | return 0; |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 150 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 151 | CFX_WideString str = textpage->GetPageText(start, count); |
| 152 | if (str.GetLength() > count) |
| 153 | str = str.Left(count); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 154 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 155 | CFX_ByteString cbUTF16str = str.UTF16LE_Encode(); |
| 156 | FXSYS_memcpy(result, cbUTF16str.GetBuffer(cbUTF16str.GetLength()), |
| 157 | cbUTF16str.GetLength()); |
| 158 | cbUTF16str.ReleaseBuffer(cbUTF16str.GetLength()); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 159 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 160 | return cbUTF16str.GetLength() / sizeof(unsigned short); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 161 | } |
| 162 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 163 | DLLEXPORT int STDCALL FPDFText_CountRects(FPDF_TEXTPAGE text_page, |
| 164 | int start, |
| 165 | int count) { |
| 166 | if (!text_page) |
| 167 | return 0; |
dsinclair | a28ae38 | 2016-04-19 10:39:24 -0700 | [diff] [blame] | 168 | |
| 169 | CPDF_TextPage* textpage = CPDFTextPageFromFPDFTextPage(text_page); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 170 | return textpage->CountRects(start, count); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 171 | } |
dsinclair | a28ae38 | 2016-04-19 10:39:24 -0700 | [diff] [blame] | 172 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 173 | DLLEXPORT void STDCALL FPDFText_GetRect(FPDF_TEXTPAGE text_page, |
| 174 | int rect_index, |
| 175 | double* left, |
| 176 | double* top, |
| 177 | double* right, |
| 178 | double* bottom) { |
| 179 | if (!text_page) |
| 180 | return; |
dsinclair | a28ae38 | 2016-04-19 10:39:24 -0700 | [diff] [blame] | 181 | |
| 182 | CPDF_TextPage* textpage = CPDFTextPageFromFPDFTextPage(text_page); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 183 | CFX_FloatRect rect; |
| 184 | textpage->GetRect(rect_index, rect.left, rect.top, rect.right, rect.bottom); |
| 185 | *left = rect.left; |
| 186 | *top = rect.top; |
| 187 | *right = rect.right; |
| 188 | *bottom = rect.bottom; |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 189 | } |
| 190 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 191 | DLLEXPORT int STDCALL FPDFText_GetBoundedText(FPDF_TEXTPAGE text_page, |
| 192 | double left, |
| 193 | double top, |
| 194 | double right, |
| 195 | double bottom, |
| 196 | unsigned short* buffer, |
| 197 | int buflen) { |
| 198 | if (!text_page) |
| 199 | return 0; |
dsinclair | a28ae38 | 2016-04-19 10:39:24 -0700 | [diff] [blame] | 200 | |
| 201 | CPDF_TextPage* textpage = CPDFTextPageFromFPDFTextPage(text_page); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 202 | CFX_FloatRect rect((FX_FLOAT)left, (FX_FLOAT)bottom, (FX_FLOAT)right, |
| 203 | (FX_FLOAT)top); |
| 204 | CFX_WideString str = textpage->GetTextByRect(rect); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 205 | |
dsinclair | a28ae38 | 2016-04-19 10:39:24 -0700 | [diff] [blame] | 206 | if (buflen <= 0 || !buffer) |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 207 | return str.GetLength(); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 208 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 209 | CFX_ByteString cbUTF16Str = str.UTF16LE_Encode(); |
| 210 | int len = cbUTF16Str.GetLength() / sizeof(unsigned short); |
| 211 | int size = buflen > len ? len : buflen; |
| 212 | FXSYS_memcpy(buffer, cbUTF16Str.GetBuffer(size * sizeof(unsigned short)), |
| 213 | size * sizeof(unsigned short)); |
| 214 | cbUTF16Str.ReleaseBuffer(size * sizeof(unsigned short)); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 215 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 216 | return size; |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 217 | } |
| 218 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 219 | // Search |
Dan Sinclair | 50cce60 | 2016-02-24 09:51:16 -0500 | [diff] [blame] | 220 | // -1 for end |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 221 | DLLEXPORT FPDF_SCHHANDLE STDCALL FPDFText_FindStart(FPDF_TEXTPAGE text_page, |
| 222 | FPDF_WIDESTRING findwhat, |
| 223 | unsigned long flags, |
| 224 | int start_index) { |
| 225 | if (!text_page) |
dsinclair | a28ae38 | 2016-04-19 10:39:24 -0700 | [diff] [blame] | 226 | return nullptr; |
| 227 | |
| 228 | CPDF_TextPageFind* textpageFind = |
| 229 | new CPDF_TextPageFind(CPDFTextPageFromFPDFTextPage(text_page)); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 230 | FX_STRSIZE len = CFX_WideString::WStringLength(findwhat); |
| 231 | textpageFind->FindFirst(CFX_WideString::FromUTF16LE(findwhat, len), flags, |
| 232 | start_index); |
| 233 | return textpageFind; |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 234 | } |
dsinclair | a28ae38 | 2016-04-19 10:39:24 -0700 | [diff] [blame] | 235 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 236 | DLLEXPORT FPDF_BOOL STDCALL FPDFText_FindNext(FPDF_SCHHANDLE handle) { |
| 237 | if (!handle) |
tsepez | 4cf5515 | 2016-11-02 14:37:54 -0700 | [diff] [blame] | 238 | return false; |
dsinclair | a28ae38 | 2016-04-19 10:39:24 -0700 | [diff] [blame] | 239 | |
| 240 | CPDF_TextPageFind* textpageFind = CPDFTextPageFindFromFPDFSchHandle(handle); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 241 | return textpageFind->FindNext(); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 242 | } |
dsinclair | a28ae38 | 2016-04-19 10:39:24 -0700 | [diff] [blame] | 243 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 244 | DLLEXPORT FPDF_BOOL STDCALL FPDFText_FindPrev(FPDF_SCHHANDLE handle) { |
| 245 | if (!handle) |
tsepez | 4cf5515 | 2016-11-02 14:37:54 -0700 | [diff] [blame] | 246 | return false; |
dsinclair | a28ae38 | 2016-04-19 10:39:24 -0700 | [diff] [blame] | 247 | |
| 248 | CPDF_TextPageFind* textpageFind = CPDFTextPageFindFromFPDFSchHandle(handle); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 249 | return textpageFind->FindPrev(); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 250 | } |
dsinclair | a28ae38 | 2016-04-19 10:39:24 -0700 | [diff] [blame] | 251 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 252 | DLLEXPORT int STDCALL FPDFText_GetSchResultIndex(FPDF_SCHHANDLE handle) { |
| 253 | if (!handle) |
| 254 | return 0; |
dsinclair | a28ae38 | 2016-04-19 10:39:24 -0700 | [diff] [blame] | 255 | |
| 256 | CPDF_TextPageFind* textpageFind = CPDFTextPageFindFromFPDFSchHandle(handle); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 257 | return textpageFind->GetCurOrder(); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 258 | } |
dsinclair | a28ae38 | 2016-04-19 10:39:24 -0700 | [diff] [blame] | 259 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 260 | DLLEXPORT int STDCALL FPDFText_GetSchCount(FPDF_SCHHANDLE handle) { |
| 261 | if (!handle) |
| 262 | return 0; |
dsinclair | a28ae38 | 2016-04-19 10:39:24 -0700 | [diff] [blame] | 263 | |
| 264 | CPDF_TextPageFind* textpageFind = CPDFTextPageFindFromFPDFSchHandle(handle); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 265 | return textpageFind->GetMatchedCount(); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 266 | } |
dsinclair | a28ae38 | 2016-04-19 10:39:24 -0700 | [diff] [blame] | 267 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 268 | DLLEXPORT void STDCALL FPDFText_FindClose(FPDF_SCHHANDLE handle) { |
| 269 | if (!handle) |
| 270 | return; |
dsinclair | a28ae38 | 2016-04-19 10:39:24 -0700 | [diff] [blame] | 271 | |
| 272 | CPDF_TextPageFind* textpageFind = CPDFTextPageFindFromFPDFSchHandle(handle); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 273 | delete textpageFind; |
dsinclair | a28ae38 | 2016-04-19 10:39:24 -0700 | [diff] [blame] | 274 | handle = nullptr; |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 275 | } |
| 276 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 277 | // web link |
| 278 | DLLEXPORT FPDF_PAGELINK STDCALL FPDFLink_LoadWebLinks(FPDF_TEXTPAGE text_page) { |
| 279 | if (!text_page) |
dsinclair | a28ae38 | 2016-04-19 10:39:24 -0700 | [diff] [blame] | 280 | return nullptr; |
| 281 | |
tsepez | 6914118 | 2016-04-21 10:43:39 -0700 | [diff] [blame] | 282 | CPDF_LinkExtract* pageLink = |
| 283 | new CPDF_LinkExtract(CPDFTextPageFromFPDFTextPage(text_page)); |
| 284 | pageLink->ExtractLinks(); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 285 | return pageLink; |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 286 | } |
dsinclair | a28ae38 | 2016-04-19 10:39:24 -0700 | [diff] [blame] | 287 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 288 | DLLEXPORT int STDCALL FPDFLink_CountWebLinks(FPDF_PAGELINK link_page) { |
| 289 | if (!link_page) |
| 290 | return 0; |
dsinclair | a28ae38 | 2016-04-19 10:39:24 -0700 | [diff] [blame] | 291 | |
| 292 | CPDF_LinkExtract* pageLink = CPDFLinkExtractFromFPDFPageLink(link_page); |
tsepez | 6914118 | 2016-04-21 10:43:39 -0700 | [diff] [blame] | 293 | return pdfium::base::checked_cast<int>(pageLink->CountLinks()); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 294 | } |
dsinclair | a28ae38 | 2016-04-19 10:39:24 -0700 | [diff] [blame] | 295 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 296 | DLLEXPORT int STDCALL FPDFLink_GetURL(FPDF_PAGELINK link_page, |
| 297 | int link_index, |
| 298 | unsigned short* buffer, |
| 299 | int buflen) { |
tsepez | 6914118 | 2016-04-21 10:43:39 -0700 | [diff] [blame] | 300 | CFX_WideString wsUrl(L""); |
| 301 | if (link_page && link_index >= 0) { |
| 302 | CPDF_LinkExtract* pageLink = CPDFLinkExtractFromFPDFPageLink(link_page); |
| 303 | wsUrl = pageLink->GetURL(link_index); |
| 304 | } |
| 305 | CFX_ByteString cbUTF16URL = wsUrl.UTF16LE_Encode(); |
| 306 | int required = cbUTF16URL.GetLength() / sizeof(unsigned short); |
Lei Zhang | 412e908 | 2015-12-14 18:34:00 -0800 | [diff] [blame] | 307 | if (!buffer || buflen <= 0) |
tsepez | 6914118 | 2016-04-21 10:43:39 -0700 | [diff] [blame] | 308 | return required; |
dsinclair | a28ae38 | 2016-04-19 10:39:24 -0700 | [diff] [blame] | 309 | |
tsepez | 6914118 | 2016-04-21 10:43:39 -0700 | [diff] [blame] | 310 | int size = std::min(required, buflen); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 311 | if (size > 0) { |
dsinclair | a28ae38 | 2016-04-19 10:39:24 -0700 | [diff] [blame] | 312 | int buf_size = size * sizeof(unsigned short); |
| 313 | FXSYS_memcpy(buffer, cbUTF16URL.GetBuffer(buf_size), buf_size); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 314 | } |
| 315 | return size; |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 316 | } |
dsinclair | a28ae38 | 2016-04-19 10:39:24 -0700 | [diff] [blame] | 317 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 318 | DLLEXPORT int STDCALL FPDFLink_CountRects(FPDF_PAGELINK link_page, |
| 319 | int link_index) { |
tsepez | 6914118 | 2016-04-21 10:43:39 -0700 | [diff] [blame] | 320 | if (!link_page || link_index < 0) |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 321 | return 0; |
dsinclair | a28ae38 | 2016-04-19 10:39:24 -0700 | [diff] [blame] | 322 | |
| 323 | CPDF_LinkExtract* pageLink = CPDFLinkExtractFromFPDFPageLink(link_page); |
tsepez | df964df | 2016-04-21 12:09:41 -0700 | [diff] [blame] | 324 | return pdfium::CollectionSize<int>(pageLink->GetRects(link_index)); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 325 | } |
dsinclair | a28ae38 | 2016-04-19 10:39:24 -0700 | [diff] [blame] | 326 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 327 | DLLEXPORT void STDCALL FPDFLink_GetRect(FPDF_PAGELINK link_page, |
| 328 | int link_index, |
| 329 | int rect_index, |
| 330 | double* left, |
| 331 | double* top, |
| 332 | double* right, |
| 333 | double* bottom) { |
tsepez | 6914118 | 2016-04-21 10:43:39 -0700 | [diff] [blame] | 334 | if (!link_page || link_index < 0 || rect_index < 0) |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 335 | return; |
dsinclair | a28ae38 | 2016-04-19 10:39:24 -0700 | [diff] [blame] | 336 | |
tsepez | 6914118 | 2016-04-21 10:43:39 -0700 | [diff] [blame] | 337 | CPDF_LinkExtract* pageLink = CPDFLinkExtractFromFPDFPageLink(link_page); |
tsepez | df964df | 2016-04-21 12:09:41 -0700 | [diff] [blame] | 338 | std::vector<CFX_FloatRect> rectArray = pageLink->GetRects(link_index); |
| 339 | if (rect_index >= pdfium::CollectionSize<int>(rectArray)) |
tsepez | 6914118 | 2016-04-21 10:43:39 -0700 | [diff] [blame] | 340 | return; |
| 341 | |
tsepez | df964df | 2016-04-21 12:09:41 -0700 | [diff] [blame] | 342 | *left = rectArray[rect_index].left; |
| 343 | *right = rectArray[rect_index].right; |
| 344 | *top = rectArray[rect_index].top; |
| 345 | *bottom = rectArray[rect_index].bottom; |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 346 | } |
tsepez | 6914118 | 2016-04-21 10:43:39 -0700 | [diff] [blame] | 347 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 348 | DLLEXPORT void STDCALL FPDFLink_CloseWebLinks(FPDF_PAGELINK link_page) { |
dsinclair | a28ae38 | 2016-04-19 10:39:24 -0700 | [diff] [blame] | 349 | delete CPDFLinkExtractFromFPDFPageLink(link_page); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 350 | } |