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