clang-format all pdfium code.
[pdfium.git] / fpdfsdk / src / fpdftext_embeddertest.cpp
1 // Copyright 2015 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_text.h"
6 #include "../../public/fpdfview.h"
7 #include "../../testing/embedder_test.h"
8 #include "testing/gtest/include/gtest/gtest.h"
9
10 namespace {
11
12 static bool check_unsigned_shorts(const char* expected,
13                                   const unsigned short* actual,
14                                   size_t length) {
15   if (length > strlen(expected) + 1) {
16     return false;
17   }
18   for (size_t i = 0; i < length; ++i) {
19     if (actual[i] != static_cast<unsigned short>(expected[i])) {
20       return false;
21     }
22   }
23   return true;
24 }
25
26 }  // namespace
27
28 class FPDFTextEmbeddertest : public EmbedderTest {};
29
30 TEST_F(FPDFTextEmbeddertest, Text) {
31   EXPECT_TRUE(OpenDocument("testing/resources/hello_world.pdf"));
32   FPDF_PAGE page = LoadPage(0);
33   EXPECT_NE(nullptr, page);
34
35   FPDF_TEXTPAGE textpage = FPDFText_LoadPage(page);
36   EXPECT_NE(nullptr, textpage);
37
38   static const char expected[] = "Hello, world!\r\nGoodbye, world!";
39   unsigned short fixed_buffer[128];
40   memset(fixed_buffer, 0xbd, sizeof(fixed_buffer));
41
42   // Check includes the terminating NUL that is provided.
43   EXPECT_EQ(sizeof(expected), FPDFText_GetText(textpage, 0, 128, fixed_buffer));
44   EXPECT_TRUE(check_unsigned_shorts(expected, fixed_buffer, sizeof(expected)));
45
46   // Count does not include the terminating NUL in the string literal.
47   EXPECT_EQ(sizeof(expected) - 1, FPDFText_CountChars(textpage));
48   for (size_t i = 0; i < sizeof(expected) - 1; ++i) {
49     EXPECT_EQ(expected[i], FPDFText_GetUnicode(textpage, i)) << " at " << i;
50   }
51
52   EXPECT_EQ(12.0, FPDFText_GetFontSize(textpage, 0));
53   EXPECT_EQ(16.0, FPDFText_GetFontSize(textpage, 15));
54
55   double left = 0.0;
56   double right = 0.0;
57   double bottom = 0.0;
58   double top = 0.0;
59   FPDFText_GetCharBox(textpage, 4, &left, &right, &bottom, &top);
60   EXPECT_NEAR(41.071, left, 0.001);
61   EXPECT_NEAR(46.243, right, 0.001);
62   EXPECT_NEAR(49.844, bottom, 0.001);
63   EXPECT_NEAR(55.520, top, 0.001);
64
65   EXPECT_EQ(4, FPDFText_GetCharIndexAtPos(textpage, 42.0, 50.0, 1.0, 1.0));
66   EXPECT_EQ(-1, FPDFText_GetCharIndexAtPos(textpage, 0.0, 0.0, 1.0, 1.0));
67   EXPECT_EQ(-1, FPDFText_GetCharIndexAtPos(textpage, 199.0, 199.0, 1.0, 1.0));
68
69   // Test out of range indicies.
70   EXPECT_EQ(-1,
71             FPDFText_GetCharIndexAtPos(textpage, 42.0, 10000000.0, 1.0, 1.0));
72   EXPECT_EQ(-1, FPDFText_GetCharIndexAtPos(textpage, -1.0, 50.0, 1.0, 1.0));
73
74   // Count does not include the terminating NUL in the string literal.
75   EXPECT_EQ(2, FPDFText_CountRects(textpage, 0, sizeof(expected) - 1));
76
77   left = 0.0;
78   right = 0.0;
79   bottom = 0.0;
80   top = 0.0;
81   FPDFText_GetRect(textpage, 1, &left, &top, &right, &bottom);
82   EXPECT_NEAR(20.847, left, 0.001);
83   EXPECT_NEAR(135.167, right, 0.001);
84   EXPECT_NEAR(96.655, bottom, 0.001);
85   EXPECT_NEAR(116.000, top, 0.001);
86
87   // Test out of range indicies set outputs to (0.0, 0.0, 0.0, 0.0).
88   left = -1.0;
89   right = -1.0;
90   bottom = -1.0;
91   top = -1.0;
92   FPDFText_GetRect(textpage, -1, &left, &top, &right, &bottom);
93   EXPECT_EQ(0.0, left);
94   EXPECT_EQ(0.0, right);
95   EXPECT_EQ(0.0, bottom);
96   EXPECT_EQ(0.0, top);
97
98   left = -2.0;
99   right = -2.0;
100   bottom = -2.0;
101   top = -2.0;
102   FPDFText_GetRect(textpage, 2, &left, &top, &right, &bottom);
103   EXPECT_EQ(0.0, left);
104   EXPECT_EQ(0.0, right);
105   EXPECT_EQ(0.0, bottom);
106   EXPECT_EQ(0.0, top);
107
108   EXPECT_EQ(9, FPDFText_GetBoundedText(textpage, 41.0, 56.0, 82.0, 48.0, 0, 0));
109
110   // Extract starting at character 4 as above.
111   memset(fixed_buffer, 0xbd, sizeof(fixed_buffer));
112   EXPECT_EQ(1, FPDFText_GetBoundedText(textpage, 41.0, 56.0, 82.0, 48.0,
113                                        fixed_buffer, 1));
114   EXPECT_TRUE(check_unsigned_shorts(expected + 4, fixed_buffer, 1));
115   EXPECT_EQ(0xbdbd, fixed_buffer[1]);
116
117   memset(fixed_buffer, 0xbd, sizeof(fixed_buffer));
118   EXPECT_EQ(9, FPDFText_GetBoundedText(textpage, 41.0, 56.0, 82.0, 48.0,
119                                        fixed_buffer, 9));
120   EXPECT_TRUE(check_unsigned_shorts(expected + 4, fixed_buffer, 9));
121   EXPECT_EQ(0xbdbd, fixed_buffer[9]);
122
123   memset(fixed_buffer, 0xbd, sizeof(fixed_buffer));
124   EXPECT_EQ(10, FPDFText_GetBoundedText(textpage, 41.0, 56.0, 82.0, 48.0,
125                                         fixed_buffer, 128));
126   EXPECT_TRUE(check_unsigned_shorts(expected + 4, fixed_buffer, 9));
127   EXPECT_EQ(0u, fixed_buffer[9]);
128   EXPECT_EQ(0xbdbd, fixed_buffer[10]);
129
130   FPDFText_ClosePage(textpage);
131   UnloadPage(page);
132 }
133
134 TEST_F(FPDFTextEmbeddertest, TextSearch) {
135   EXPECT_TRUE(OpenDocument("testing/resources/hello_world.pdf"));
136   FPDF_PAGE page = LoadPage(0);
137   EXPECT_NE(nullptr, page);
138
139   FPDF_TEXTPAGE textpage = FPDFText_LoadPage(page);
140   EXPECT_NE(nullptr, textpage);
141
142   // Avoid issues with system wchar_t width vs. FPDF_WideString.
143   const unsigned short nope[] = {'n', 'o', 'p', 'e', '\0'};
144   const unsigned short world[] = {'w', 'o', 'r', 'l', 'd', '\0'};
145   const unsigned short world_caps[] = {'W', 'O', 'R', 'L', 'D', '\0'};
146   const unsigned short world_substr[] = {'o', 'r', 'l', 'd', '\0'};
147
148   // No occurences of "nope" in test page.
149   FPDF_SCHHANDLE search = FPDFText_FindStart(textpage, nope, 0, 0);
150   EXPECT_NE(nullptr, search);
151   EXPECT_EQ(0, FPDFText_GetSchResultIndex(search));
152   EXPECT_EQ(0, FPDFText_GetSchCount(search));
153
154   // Advancing finds nothing.
155   EXPECT_FALSE(FPDFText_FindNext(search));
156   EXPECT_EQ(0, FPDFText_GetSchResultIndex(search));
157   EXPECT_EQ(0, FPDFText_GetSchCount(search));
158
159   // Retreating finds nothing.
160   EXPECT_FALSE(FPDFText_FindPrev(search));
161   EXPECT_EQ(0, FPDFText_GetSchResultIndex(search));
162   EXPECT_EQ(0, FPDFText_GetSchCount(search));
163   FPDFText_FindClose(search);
164
165   // Two occurences of "world" in test page.
166   search = FPDFText_FindStart(textpage, world, 0, 2);
167   EXPECT_NE(nullptr, search);
168
169   // Remains not found until advanced.
170   EXPECT_EQ(0, FPDFText_GetSchResultIndex(search));
171   EXPECT_EQ(0, FPDFText_GetSchCount(search));
172
173   // First occurence of "world" in this test page.
174   EXPECT_TRUE(FPDFText_FindNext(search));
175   EXPECT_EQ(7, FPDFText_GetSchResultIndex(search));
176   EXPECT_EQ(5, FPDFText_GetSchCount(search));
177
178   // Last occurence of "world" in this test page.
179   EXPECT_TRUE(FPDFText_FindNext(search));
180   EXPECT_EQ(24, FPDFText_GetSchResultIndex(search));
181   EXPECT_EQ(5, FPDFText_GetSchCount(search));
182
183   // Found position unchanged when fails to advance.
184   EXPECT_FALSE(FPDFText_FindNext(search));
185   EXPECT_EQ(24, FPDFText_GetSchResultIndex(search));
186   EXPECT_EQ(5, FPDFText_GetSchCount(search));
187
188   // Back to first occurence.
189   EXPECT_TRUE(FPDFText_FindPrev(search));
190   EXPECT_EQ(7, FPDFText_GetSchResultIndex(search));
191   EXPECT_EQ(5, FPDFText_GetSchCount(search));
192
193   // Found position unchanged when fails to retreat.
194   EXPECT_FALSE(FPDFText_FindPrev(search));
195   EXPECT_EQ(7, FPDFText_GetSchResultIndex(search));
196   EXPECT_EQ(5, FPDFText_GetSchCount(search));
197   FPDFText_FindClose(search);
198
199   // Exact search unaffected by case sensitiity and whole word flags.
200   search = FPDFText_FindStart(textpage, world,
201                               FPDF_MATCHCASE | FPDF_MATCHWHOLEWORD, 0);
202   EXPECT_NE(nullptr, search);
203   EXPECT_TRUE(FPDFText_FindNext(search));
204   EXPECT_EQ(7, FPDFText_GetSchResultIndex(search));
205   EXPECT_EQ(5, FPDFText_GetSchCount(search));
206   FPDFText_FindClose(search);
207
208   // Default is case-insensitive, so matching agaist caps works.
209   search = FPDFText_FindStart(textpage, world_caps, 0, 0);
210   EXPECT_NE(nullptr, search);
211   EXPECT_TRUE(FPDFText_FindNext(search));
212   EXPECT_EQ(7, FPDFText_GetSchResultIndex(search));
213   EXPECT_EQ(5, FPDFText_GetSchCount(search));
214   FPDFText_FindClose(search);
215
216   // But can be made case sensitive, in which case this fails.
217   search = FPDFText_FindStart(textpage, world_caps, FPDF_MATCHCASE, 0);
218   EXPECT_FALSE(FPDFText_FindNext(search));
219   EXPECT_EQ(0, FPDFText_GetSchResultIndex(search));
220   EXPECT_EQ(0, FPDFText_GetSchCount(search));
221   FPDFText_FindClose(search);
222
223   // Default is match anywhere within word, so matching substirng works.
224   search = FPDFText_FindStart(textpage, world_substr, 0, 0);
225   EXPECT_TRUE(FPDFText_FindNext(search));
226   EXPECT_EQ(8, FPDFText_GetSchResultIndex(search));
227   EXPECT_EQ(4, FPDFText_GetSchCount(search));
228   FPDFText_FindClose(search);
229
230   // But can be made to mach word boundaries, in which case this fails.
231   search = FPDFText_FindStart(textpage, world_substr, FPDF_MATCHWHOLEWORD, 0);
232   EXPECT_FALSE(FPDFText_FindNext(search));
233   // TODO(tsepez): investigate strange index/count values in this state.
234   FPDFText_FindClose(search);
235
236   FPDFText_ClosePage(textpage);
237   UnloadPage(page);
238 }
239
240 // Test that the page has characters despite a bad stream length.
241 TEST_F(FPDFTextEmbeddertest, StreamLengthPastEndOfFile) {
242   EXPECT_TRUE(OpenDocument("testing/resources/bug_57.pdf"));
243   FPDF_PAGE page = LoadPage(0);
244   EXPECT_NE(nullptr, page);
245
246   FPDF_TEXTPAGE textpage = FPDFText_LoadPage(page);
247   EXPECT_NE(nullptr, textpage);
248   EXPECT_EQ(13, FPDFText_CountChars(textpage));
249
250   FPDFText_ClosePage(textpage);
251   UnloadPage(page);
252 }
253
254 TEST_F(FPDFTextEmbeddertest, WebLinks) {
255   EXPECT_TRUE(OpenDocument("testing/resources/weblinks.pdf"));
256   FPDF_PAGE page = LoadPage(0);
257   EXPECT_NE(nullptr, page);
258
259   FPDF_TEXTPAGE textpage = FPDFText_LoadPage(page);
260   EXPECT_NE(nullptr, textpage);
261
262   FPDF_PAGELINK pagelink = FPDFLink_LoadWebLinks(textpage);
263   EXPECT_NE(nullptr, pagelink);
264
265   // Page contains two HTTP-style URLs.
266   EXPECT_EQ(2, FPDFLink_CountWebLinks(pagelink));
267
268   // Only a terminating NUL required for bogus links.
269   EXPECT_EQ(1, FPDFLink_GetURL(pagelink, 2, nullptr, 0));
270   EXPECT_EQ(1, FPDFLink_GetURL(pagelink, 1400, nullptr, 0));
271   EXPECT_EQ(1, FPDFLink_GetURL(pagelink, -1, nullptr, 0));
272
273   // Query the number of characters required for each link (incl NUL).
274   EXPECT_EQ(25, FPDFLink_GetURL(pagelink, 0, nullptr, 0));
275   EXPECT_EQ(26, FPDFLink_GetURL(pagelink, 1, nullptr, 0));
276
277   static const char expected_url[] = "http://example.com?q=foo";
278   unsigned short fixed_buffer[128];
279
280   // Retrieve a link with too small a buffer.  Buffer will not be
281   // NUL-terminated, but must not be modified past indicated length,
282   // so pre-fill with a pattern to check write bounds.
283   memset(fixed_buffer, 0xbd, sizeof(fixed_buffer));
284   EXPECT_EQ(1, FPDFLink_GetURL(pagelink, 0, fixed_buffer, 1));
285   EXPECT_TRUE(check_unsigned_shorts(expected_url, fixed_buffer, 1));
286   EXPECT_EQ(0xbdbd, fixed_buffer[1]);
287
288   // Check buffer that doesn't have space for a terminating NUL.
289   memset(fixed_buffer, 0xbd, sizeof(fixed_buffer));
290   EXPECT_EQ(
291       sizeof(expected_url) - 1,
292       FPDFLink_GetURL(pagelink, 0, fixed_buffer, sizeof(expected_url) - 1));
293   EXPECT_TRUE(check_unsigned_shorts(expected_url, fixed_buffer,
294                                     sizeof(expected_url) - 1));
295   EXPECT_EQ(0xbdbd, fixed_buffer[sizeof(expected_url) - 1]);
296
297   // Retreive link with exactly-sized buffer.
298   memset(fixed_buffer, 0xbd, sizeof(fixed_buffer));
299   EXPECT_EQ(sizeof(expected_url),
300             FPDFLink_GetURL(pagelink, 0, fixed_buffer, sizeof(expected_url)));
301   EXPECT_TRUE(
302       check_unsigned_shorts(expected_url, fixed_buffer, sizeof(expected_url)));
303   EXPECT_EQ(0u, fixed_buffer[sizeof(expected_url) - 1]);
304   EXPECT_EQ(0xbdbd, fixed_buffer[sizeof(expected_url)]);
305
306   // Retreive link with ample-sized-buffer.
307   memset(fixed_buffer, 0xbd, sizeof(fixed_buffer));
308   EXPECT_EQ(sizeof(expected_url),
309             FPDFLink_GetURL(pagelink, 0, fixed_buffer, 128));
310   EXPECT_TRUE(
311       check_unsigned_shorts(expected_url, fixed_buffer, sizeof(expected_url)));
312   EXPECT_EQ(0u, fixed_buffer[sizeof(expected_url) - 1]);
313   EXPECT_EQ(0xbdbd, fixed_buffer[sizeof(expected_url)]);
314
315   // Each link rendered in a single rect in this test page.
316   EXPECT_EQ(1, FPDFLink_CountRects(pagelink, 0));
317   EXPECT_EQ(1, FPDFLink_CountRects(pagelink, 1));
318
319   // Each link rendered in a single rect in this test page.
320   EXPECT_EQ(0, FPDFLink_CountRects(pagelink, -1));
321   EXPECT_EQ(0, FPDFLink_CountRects(pagelink, 2));
322   EXPECT_EQ(0, FPDFLink_CountRects(pagelink, 10000));
323
324   // Check boundary of valid link index with valid rect index.
325   double left = 0.0;
326   double right = 0.0;
327   double top = 0.0;
328   double bottom = 0.0;
329   FPDFLink_GetRect(pagelink, 0, 0, &left, &top, &right, &bottom);
330   EXPECT_NEAR(50.791, left, 0.001);
331   EXPECT_NEAR(187.963, right, 0.001);
332   EXPECT_NEAR(97.624, bottom, 0.001);
333   EXPECT_NEAR(108.736, top, 0.001);
334
335   // Check that valid link with invalid rect index leaves parameters unchanged.
336   left = -1.0;
337   right = -1.0;
338   top = -1.0;
339   bottom = -1.0;
340   FPDFLink_GetRect(pagelink, 0, 1, &left, &top, &right, &bottom);
341   EXPECT_EQ(-1.0, left);
342   EXPECT_EQ(-1.0, right);
343   EXPECT_EQ(-1.0, bottom);
344   EXPECT_EQ(-1.0, top);
345
346   // Check that invalid link index leaves parameters unchanged.
347   left = -2.0;
348   right = -2.0;
349   top = -2.0;
350   bottom = -2.0;
351   FPDFLink_GetRect(pagelink, -1, 0, &left, &top, &right, &bottom);
352   EXPECT_EQ(-2.0, left);
353   EXPECT_EQ(-2.0, right);
354   EXPECT_EQ(-2.0, bottom);
355   EXPECT_EQ(-2.0, top);
356
357   FPDFLink_CloseWebLinks(pagelink);
358   FPDFText_ClosePage(textpage);
359   UnloadPage(page);
360 }