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 | |
Dan Sinclair | 00d2ad1 | 2017-08-10 14:13:02 -0400 | [diff] [blame^] | 46 | FPDF_EXPORT FPDF_TEXTPAGE FPDF_CALLCONV 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 |
Tom Sepez | 4cb82ee | 2017-05-22 15:15:30 -0700 | [diff] [blame] | 56 | CPDF_ViewerPreferences viewRef(pPDFPage->m_pDocument.Get()); |
Tom Sepez | 40e9ff3 | 2015-11-30 12:39:54 -0800 | [diff] [blame] | 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 | |
Dan Sinclair | 00d2ad1 | 2017-08-10 14:13:02 -0400 | [diff] [blame^] | 66 | FPDF_EXPORT void FPDF_CALLCONV 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 | |
Dan Sinclair | 00d2ad1 | 2017-08-10 14:13:02 -0400 | [diff] [blame^] | 70 | FPDF_EXPORT int FPDF_CALLCONV FPDFText_CountChars(FPDF_TEXTPAGE text_page) { |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 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 | |
Dan Sinclair | 00d2ad1 | 2017-08-10 14:13:02 -0400 | [diff] [blame^] | 78 | FPDF_EXPORT unsigned int FPDF_CALLCONV |
| 79 | FPDFText_GetUnicode(FPDF_TEXTPAGE text_page, int index) { |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 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 | |
Dan Sinclair | 00d2ad1 | 2017-08-10 14:13:02 -0400 | [diff] [blame^] | 92 | FPDF_EXPORT double FPDF_CALLCONV FPDFText_GetFontSize(FPDF_TEXTPAGE text_page, |
| 93 | int index) { |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 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 | |
Dan Sinclair | 00d2ad1 | 2017-08-10 14:13:02 -0400 | [diff] [blame^] | 106 | FPDF_EXPORT void FPDF_CALLCONV FPDFText_GetCharBox(FPDF_TEXTPAGE text_page, |
| 107 | int index, |
| 108 | double* left, |
| 109 | double* right, |
| 110 | double* bottom, |
| 111 | double* top) { |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 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 |
Dan Sinclair | 00d2ad1 | 2017-08-10 14:13:02 -0400 | [diff] [blame^] | 127 | FPDF_EXPORT int FPDF_CALLCONV |
| 128 | FPDFText_GetCharIndexAtPos(FPDF_TEXTPAGE text_page, |
| 129 | double x, |
| 130 | double y, |
| 131 | double xTolerance, |
| 132 | double yTolerance) { |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 133 | if (!text_page) |
| 134 | return -3; |
dsinclair | a28ae38 | 2016-04-19 10:39:24 -0700 | [diff] [blame] | 135 | |
| 136 | CPDF_TextPage* textpage = CPDFTextPageFromFPDFTextPage(text_page); |
Dan Sinclair | d476adc | 2017-02-21 14:31:41 -0500 | [diff] [blame] | 137 | return textpage->GetIndexAtPos( |
Dan Sinclair | 05df075 | 2017-03-14 14:43:42 -0400 | [diff] [blame] | 138 | CFX_PointF(static_cast<float>(x), static_cast<float>(y)), |
| 139 | CFX_SizeF(static_cast<float>(xTolerance), |
| 140 | static_cast<float>(yTolerance))); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 141 | } |
| 142 | |
Dan Sinclair | 00d2ad1 | 2017-08-10 14:13:02 -0400 | [diff] [blame^] | 143 | FPDF_EXPORT int FPDF_CALLCONV FPDFText_GetText(FPDF_TEXTPAGE text_page, |
| 144 | int start, |
| 145 | int count, |
| 146 | unsigned short* result) { |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 147 | if (!text_page) |
| 148 | return 0; |
Lei Zhang | a6d9f0e | 2015-06-13 00:48:38 -0700 | [diff] [blame] | 149 | |
dsinclair | a28ae38 | 2016-04-19 10:39:24 -0700 | [diff] [blame] | 150 | CPDF_TextPage* textpage = CPDFTextPageFromFPDFTextPage(text_page); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 151 | if (start >= textpage->CountChars()) |
| 152 | return 0; |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 153 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 154 | CFX_WideString str = textpage->GetPageText(start, count); |
| 155 | if (str.GetLength() > count) |
| 156 | str = str.Left(count); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 157 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 158 | CFX_ByteString cbUTF16str = str.UTF16LE_Encode(); |
Dan Sinclair | 1c5d0b4 | 2017-04-03 15:05:11 -0400 | [diff] [blame] | 159 | memcpy(result, cbUTF16str.GetBuffer(cbUTF16str.GetLength()), |
| 160 | cbUTF16str.GetLength()); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 161 | cbUTF16str.ReleaseBuffer(cbUTF16str.GetLength()); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 162 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 163 | return cbUTF16str.GetLength() / sizeof(unsigned short); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 164 | } |
| 165 | |
Dan Sinclair | 00d2ad1 | 2017-08-10 14:13:02 -0400 | [diff] [blame^] | 166 | FPDF_EXPORT int FPDF_CALLCONV FPDFText_CountRects(FPDF_TEXTPAGE text_page, |
| 167 | int start, |
| 168 | int count) { |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 169 | if (!text_page) |
| 170 | return 0; |
dsinclair | a28ae38 | 2016-04-19 10:39:24 -0700 | [diff] [blame] | 171 | |
| 172 | CPDF_TextPage* textpage = CPDFTextPageFromFPDFTextPage(text_page); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 173 | return textpage->CountRects(start, count); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 174 | } |
dsinclair | a28ae38 | 2016-04-19 10:39:24 -0700 | [diff] [blame] | 175 | |
Dan Sinclair | 00d2ad1 | 2017-08-10 14:13:02 -0400 | [diff] [blame^] | 176 | FPDF_EXPORT void FPDF_CALLCONV FPDFText_GetRect(FPDF_TEXTPAGE text_page, |
| 177 | int rect_index, |
| 178 | double* left, |
| 179 | double* top, |
| 180 | double* right, |
| 181 | double* bottom) { |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 182 | if (!text_page) |
| 183 | return; |
dsinclair | a28ae38 | 2016-04-19 10:39:24 -0700 | [diff] [blame] | 184 | |
| 185 | CPDF_TextPage* textpage = CPDFTextPageFromFPDFTextPage(text_page); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 186 | CFX_FloatRect rect; |
| 187 | textpage->GetRect(rect_index, rect.left, rect.top, rect.right, rect.bottom); |
| 188 | *left = rect.left; |
| 189 | *top = rect.top; |
| 190 | *right = rect.right; |
| 191 | *bottom = rect.bottom; |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 192 | } |
| 193 | |
Dan Sinclair | 00d2ad1 | 2017-08-10 14:13:02 -0400 | [diff] [blame^] | 194 | FPDF_EXPORT int FPDF_CALLCONV FPDFText_GetBoundedText(FPDF_TEXTPAGE text_page, |
| 195 | double left, |
| 196 | double top, |
| 197 | double right, |
| 198 | double bottom, |
| 199 | unsigned short* buffer, |
| 200 | int buflen) { |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 201 | if (!text_page) |
| 202 | return 0; |
dsinclair | a28ae38 | 2016-04-19 10:39:24 -0700 | [diff] [blame] | 203 | |
| 204 | CPDF_TextPage* textpage = CPDFTextPageFromFPDFTextPage(text_page); |
Dan Sinclair | 05df075 | 2017-03-14 14:43:42 -0400 | [diff] [blame] | 205 | CFX_FloatRect rect((float)left, (float)bottom, (float)right, (float)top); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 206 | CFX_WideString str = textpage->GetTextByRect(rect); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 207 | |
dsinclair | a28ae38 | 2016-04-19 10:39:24 -0700 | [diff] [blame] | 208 | if (buflen <= 0 || !buffer) |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 209 | return str.GetLength(); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 210 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 211 | CFX_ByteString cbUTF16Str = str.UTF16LE_Encode(); |
| 212 | int len = cbUTF16Str.GetLength() / sizeof(unsigned short); |
| 213 | int size = buflen > len ? len : buflen; |
Dan Sinclair | 1c5d0b4 | 2017-04-03 15:05:11 -0400 | [diff] [blame] | 214 | memcpy(buffer, cbUTF16Str.GetBuffer(size * sizeof(unsigned short)), |
| 215 | size * sizeof(unsigned short)); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 216 | cbUTF16Str.ReleaseBuffer(size * sizeof(unsigned short)); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 217 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 218 | return size; |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 219 | } |
| 220 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 221 | // Search |
Dan Sinclair | 50cce60 | 2016-02-24 09:51:16 -0500 | [diff] [blame] | 222 | // -1 for end |
Dan Sinclair | 00d2ad1 | 2017-08-10 14:13:02 -0400 | [diff] [blame^] | 223 | FPDF_EXPORT FPDF_SCHHANDLE FPDF_CALLCONV |
| 224 | FPDFText_FindStart(FPDF_TEXTPAGE text_page, |
| 225 | FPDF_WIDESTRING findwhat, |
| 226 | unsigned long flags, |
| 227 | int start_index) { |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 228 | if (!text_page) |
dsinclair | a28ae38 | 2016-04-19 10:39:24 -0700 | [diff] [blame] | 229 | return nullptr; |
| 230 | |
| 231 | CPDF_TextPageFind* textpageFind = |
| 232 | new CPDF_TextPageFind(CPDFTextPageFromFPDFTextPage(text_page)); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 233 | FX_STRSIZE len = CFX_WideString::WStringLength(findwhat); |
| 234 | textpageFind->FindFirst(CFX_WideString::FromUTF16LE(findwhat, len), flags, |
| 235 | start_index); |
| 236 | return textpageFind; |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 237 | } |
dsinclair | a28ae38 | 2016-04-19 10:39:24 -0700 | [diff] [blame] | 238 | |
Dan Sinclair | 00d2ad1 | 2017-08-10 14:13:02 -0400 | [diff] [blame^] | 239 | FPDF_EXPORT FPDF_BOOL FPDF_CALLCONV FPDFText_FindNext(FPDF_SCHHANDLE handle) { |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 240 | if (!handle) |
tsepez | 4cf5515 | 2016-11-02 14:37:54 -0700 | [diff] [blame] | 241 | return false; |
dsinclair | a28ae38 | 2016-04-19 10:39:24 -0700 | [diff] [blame] | 242 | |
| 243 | CPDF_TextPageFind* textpageFind = CPDFTextPageFindFromFPDFSchHandle(handle); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 244 | return textpageFind->FindNext(); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 245 | } |
dsinclair | a28ae38 | 2016-04-19 10:39:24 -0700 | [diff] [blame] | 246 | |
Dan Sinclair | 00d2ad1 | 2017-08-10 14:13:02 -0400 | [diff] [blame^] | 247 | FPDF_EXPORT FPDF_BOOL FPDF_CALLCONV FPDFText_FindPrev(FPDF_SCHHANDLE handle) { |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 248 | if (!handle) |
tsepez | 4cf5515 | 2016-11-02 14:37:54 -0700 | [diff] [blame] | 249 | return false; |
dsinclair | a28ae38 | 2016-04-19 10:39:24 -0700 | [diff] [blame] | 250 | |
| 251 | CPDF_TextPageFind* textpageFind = CPDFTextPageFindFromFPDFSchHandle(handle); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 252 | return textpageFind->FindPrev(); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 253 | } |
dsinclair | a28ae38 | 2016-04-19 10:39:24 -0700 | [diff] [blame] | 254 | |
Dan Sinclair | 00d2ad1 | 2017-08-10 14:13:02 -0400 | [diff] [blame^] | 255 | FPDF_EXPORT int FPDF_CALLCONV |
| 256 | FPDFText_GetSchResultIndex(FPDF_SCHHANDLE handle) { |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 257 | if (!handle) |
| 258 | return 0; |
dsinclair | a28ae38 | 2016-04-19 10:39:24 -0700 | [diff] [blame] | 259 | |
| 260 | CPDF_TextPageFind* textpageFind = CPDFTextPageFindFromFPDFSchHandle(handle); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 261 | return textpageFind->GetCurOrder(); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 262 | } |
dsinclair | a28ae38 | 2016-04-19 10:39:24 -0700 | [diff] [blame] | 263 | |
Dan Sinclair | 00d2ad1 | 2017-08-10 14:13:02 -0400 | [diff] [blame^] | 264 | FPDF_EXPORT int FPDF_CALLCONV FPDFText_GetSchCount(FPDF_SCHHANDLE handle) { |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 265 | if (!handle) |
| 266 | return 0; |
dsinclair | a28ae38 | 2016-04-19 10:39:24 -0700 | [diff] [blame] | 267 | |
| 268 | CPDF_TextPageFind* textpageFind = CPDFTextPageFindFromFPDFSchHandle(handle); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 269 | return textpageFind->GetMatchedCount(); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 270 | } |
dsinclair | a28ae38 | 2016-04-19 10:39:24 -0700 | [diff] [blame] | 271 | |
Dan Sinclair | 00d2ad1 | 2017-08-10 14:13:02 -0400 | [diff] [blame^] | 272 | FPDF_EXPORT void FPDF_CALLCONV FPDFText_FindClose(FPDF_SCHHANDLE handle) { |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 273 | if (!handle) |
| 274 | return; |
dsinclair | a28ae38 | 2016-04-19 10:39:24 -0700 | [diff] [blame] | 275 | |
| 276 | CPDF_TextPageFind* textpageFind = CPDFTextPageFindFromFPDFSchHandle(handle); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 277 | delete textpageFind; |
dsinclair | a28ae38 | 2016-04-19 10:39:24 -0700 | [diff] [blame] | 278 | handle = nullptr; |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 279 | } |
| 280 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 281 | // web link |
Dan Sinclair | 00d2ad1 | 2017-08-10 14:13:02 -0400 | [diff] [blame^] | 282 | FPDF_EXPORT FPDF_PAGELINK FPDF_CALLCONV |
| 283 | FPDFLink_LoadWebLinks(FPDF_TEXTPAGE text_page) { |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 284 | if (!text_page) |
dsinclair | a28ae38 | 2016-04-19 10:39:24 -0700 | [diff] [blame] | 285 | return nullptr; |
| 286 | |
tsepez | 6914118 | 2016-04-21 10:43:39 -0700 | [diff] [blame] | 287 | CPDF_LinkExtract* pageLink = |
| 288 | new CPDF_LinkExtract(CPDFTextPageFromFPDFTextPage(text_page)); |
| 289 | pageLink->ExtractLinks(); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 290 | return pageLink; |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 291 | } |
dsinclair | a28ae38 | 2016-04-19 10:39:24 -0700 | [diff] [blame] | 292 | |
Dan Sinclair | 00d2ad1 | 2017-08-10 14:13:02 -0400 | [diff] [blame^] | 293 | FPDF_EXPORT int FPDF_CALLCONV FPDFLink_CountWebLinks(FPDF_PAGELINK link_page) { |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 294 | if (!link_page) |
| 295 | return 0; |
dsinclair | a28ae38 | 2016-04-19 10:39:24 -0700 | [diff] [blame] | 296 | |
| 297 | CPDF_LinkExtract* pageLink = CPDFLinkExtractFromFPDFPageLink(link_page); |
tsepez | 6914118 | 2016-04-21 10:43:39 -0700 | [diff] [blame] | 298 | return pdfium::base::checked_cast<int>(pageLink->CountLinks()); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 299 | } |
dsinclair | a28ae38 | 2016-04-19 10:39:24 -0700 | [diff] [blame] | 300 | |
Dan Sinclair | 00d2ad1 | 2017-08-10 14:13:02 -0400 | [diff] [blame^] | 301 | FPDF_EXPORT int FPDF_CALLCONV FPDFLink_GetURL(FPDF_PAGELINK link_page, |
| 302 | int link_index, |
| 303 | unsigned short* buffer, |
| 304 | int buflen) { |
tsepez | 6914118 | 2016-04-21 10:43:39 -0700 | [diff] [blame] | 305 | CFX_WideString wsUrl(L""); |
| 306 | if (link_page && link_index >= 0) { |
| 307 | CPDF_LinkExtract* pageLink = CPDFLinkExtractFromFPDFPageLink(link_page); |
| 308 | wsUrl = pageLink->GetURL(link_index); |
| 309 | } |
| 310 | CFX_ByteString cbUTF16URL = wsUrl.UTF16LE_Encode(); |
| 311 | int required = cbUTF16URL.GetLength() / sizeof(unsigned short); |
Lei Zhang | 412e908 | 2015-12-14 18:34:00 -0800 | [diff] [blame] | 312 | if (!buffer || buflen <= 0) |
tsepez | 6914118 | 2016-04-21 10:43:39 -0700 | [diff] [blame] | 313 | return required; |
dsinclair | a28ae38 | 2016-04-19 10:39:24 -0700 | [diff] [blame] | 314 | |
tsepez | 6914118 | 2016-04-21 10:43:39 -0700 | [diff] [blame] | 315 | int size = std::min(required, buflen); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 316 | if (size > 0) { |
dsinclair | a28ae38 | 2016-04-19 10:39:24 -0700 | [diff] [blame] | 317 | int buf_size = size * sizeof(unsigned short); |
Dan Sinclair | 1c5d0b4 | 2017-04-03 15:05:11 -0400 | [diff] [blame] | 318 | memcpy(buffer, cbUTF16URL.GetBuffer(buf_size), buf_size); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 319 | } |
| 320 | return size; |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 321 | } |
dsinclair | a28ae38 | 2016-04-19 10:39:24 -0700 | [diff] [blame] | 322 | |
Dan Sinclair | 00d2ad1 | 2017-08-10 14:13:02 -0400 | [diff] [blame^] | 323 | FPDF_EXPORT int FPDF_CALLCONV FPDFLink_CountRects(FPDF_PAGELINK link_page, |
| 324 | int link_index) { |
tsepez | 6914118 | 2016-04-21 10:43:39 -0700 | [diff] [blame] | 325 | if (!link_page || link_index < 0) |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 326 | return 0; |
dsinclair | a28ae38 | 2016-04-19 10:39:24 -0700 | [diff] [blame] | 327 | |
| 328 | CPDF_LinkExtract* pageLink = CPDFLinkExtractFromFPDFPageLink(link_page); |
tsepez | df964df | 2016-04-21 12:09:41 -0700 | [diff] [blame] | 329 | return pdfium::CollectionSize<int>(pageLink->GetRects(link_index)); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 330 | } |
dsinclair | a28ae38 | 2016-04-19 10:39:24 -0700 | [diff] [blame] | 331 | |
Dan Sinclair | 00d2ad1 | 2017-08-10 14:13:02 -0400 | [diff] [blame^] | 332 | FPDF_EXPORT void FPDF_CALLCONV FPDFLink_GetRect(FPDF_PAGELINK link_page, |
| 333 | int link_index, |
| 334 | int rect_index, |
| 335 | double* left, |
| 336 | double* top, |
| 337 | double* right, |
| 338 | double* bottom) { |
tsepez | 6914118 | 2016-04-21 10:43:39 -0700 | [diff] [blame] | 339 | if (!link_page || link_index < 0 || rect_index < 0) |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 340 | return; |
dsinclair | a28ae38 | 2016-04-19 10:39:24 -0700 | [diff] [blame] | 341 | |
tsepez | 6914118 | 2016-04-21 10:43:39 -0700 | [diff] [blame] | 342 | CPDF_LinkExtract* pageLink = CPDFLinkExtractFromFPDFPageLink(link_page); |
tsepez | df964df | 2016-04-21 12:09:41 -0700 | [diff] [blame] | 343 | std::vector<CFX_FloatRect> rectArray = pageLink->GetRects(link_index); |
| 344 | if (rect_index >= pdfium::CollectionSize<int>(rectArray)) |
tsepez | 6914118 | 2016-04-21 10:43:39 -0700 | [diff] [blame] | 345 | return; |
| 346 | |
tsepez | df964df | 2016-04-21 12:09:41 -0700 | [diff] [blame] | 347 | *left = rectArray[rect_index].left; |
| 348 | *right = rectArray[rect_index].right; |
| 349 | *top = rectArray[rect_index].top; |
| 350 | *bottom = rectArray[rect_index].bottom; |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 351 | } |
tsepez | 6914118 | 2016-04-21 10:43:39 -0700 | [diff] [blame] | 352 | |
Dan Sinclair | 00d2ad1 | 2017-08-10 14:13:02 -0400 | [diff] [blame^] | 353 | FPDF_EXPORT void FPDF_CALLCONV FPDFLink_CloseWebLinks(FPDF_PAGELINK link_page) { |
dsinclair | a28ae38 | 2016-04-19 10:39:24 -0700 | [diff] [blame] | 354 | delete CPDFLinkExtractFromFPDFPageLink(link_page); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 355 | } |