Upstream version 10.39.225.0
[platform/framework/web/crosswalk.git] / src / pdf / pdfium / pdfium_engine.cc
1 // Copyright (c) 2012 The Chromium 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 "pdf/pdfium/pdfium_engine.h"
6
7 #include <math.h>
8
9 #include "base/json/json_writer.h"
10 #include "base/logging.h"
11 #include "base/memory/scoped_ptr.h"
12 #include "base/stl_util.h"
13 #include "base/strings/string_number_conversions.h"
14 #include "base/strings/string_piece.h"
15 #include "base/strings/string_util.h"
16 #include "base/strings/utf_string_conversions.h"
17 #include "base/values.h"
18 #include "pdf/draw_utils.h"
19 #include "pdf/pdfium/pdfium_mem_buffer_file_read.h"
20 #include "pdf/pdfium/pdfium_mem_buffer_file_write.h"
21 #include "ppapi/c/pp_errors.h"
22 #include "ppapi/c/pp_input_event.h"
23 #include "ppapi/c/ppb_core.h"
24 #include "ppapi/c/private/ppb_pdf.h"
25 #include "ppapi/cpp/dev/memory_dev.h"
26 #include "ppapi/cpp/input_event.h"
27 #include "ppapi/cpp/instance.h"
28 #include "ppapi/cpp/module.h"
29 #include "ppapi/cpp/private/pdf.h"
30 #include "ppapi/cpp/trusted/browser_font_trusted.h"
31 #include "ppapi/cpp/url_response_info.h"
32 #include "ppapi/cpp/var.h"
33 #include "third_party/pdfium/fpdfsdk/include/fpdf_ext.h"
34 #include "third_party/pdfium/fpdfsdk/include/fpdf_flatten.h"
35 #include "third_party/pdfium/fpdfsdk/include/fpdf_searchex.h"
36 #include "third_party/pdfium/fpdfsdk/include/fpdf_sysfontinfo.h"
37 #include "third_party/pdfium/fpdfsdk/include/fpdf_transformpage.h"
38 #include "third_party/pdfium/fpdfsdk/include/fpdfedit.h"
39 #include "third_party/pdfium/fpdfsdk/include/fpdfoom.h"
40 #include "third_party/pdfium/fpdfsdk/include/fpdfppo.h"
41 #include "third_party/pdfium/fpdfsdk/include/fpdfsave.h"
42 #include "third_party/pdfium/fpdfsdk/include/pdfwindow/PDFWindow.h"
43 #include "third_party/pdfium/fpdfsdk/include/pdfwindow/PWL_FontMap.h"
44 #include "ui/events/keycodes/keyboard_codes.h"
45
46 namespace chrome_pdf {
47
48 namespace {
49
50 #define kPageShadowTop    3
51 #define kPageShadowBottom 7
52 #define kPageShadowLeft   5
53 #define kPageShadowRight  5
54
55 #define kPageSeparatorThickness 4
56 #define kHighlightColorR 153
57 #define kHighlightColorG 193
58 #define kHighlightColorB 218
59
60 const uint32 kPendingPageColor = 0xFFEEEEEE;
61
62 #define kFormHighlightColor 0xFFE4DD
63 #define kFormHighlightAlpha 100
64
65 #define kMaxPasswordTries 3
66
67 // See Table 3.20 in
68 // http://www.adobe.com/devnet/acrobat/pdfs/pdf_reference_1-7.pdf
69 #define kPDFPermissionPrintLowQualityMask  1 << 2
70 #define kPDFPermissionPrintHighQualityMask 1 << 11
71 #define kPDFPermissionCopyMask             1 << 4
72 #define kPDFPermissionCopyAccessibleMask   1 << 9
73
74 #define kLoadingTextVerticalOffset 50
75
76 // The maximum amount of time we'll spend doing a paint before we give back
77 // control of the thread.
78 #define kMaxProgressivePaintTimeMs 50
79
80 // The maximum amount of time we'll spend doing the first paint. This is less
81 // than the above to keep things smooth if the user is scrolling quickly. We
82 // try painting a little because with accelerated compositing, we get flushes
83 // only every 16 ms. If we were to wait until the next flush to paint the rest
84 // of the pdf, we would never get to draw the pdf and would only draw the
85 // scrollbars. This value is picked to give enough time for gpu related code to
86 // do its thing and still fit within the timelimit for 60Hz. For the
87 // non-composited case, this doesn't make things worse since we're still
88 // painting the scrollbars > 60 Hz.
89 #define kMaxInitialProgressivePaintTimeMs 10
90
91 // Copied from printing/units.cc because we don't want to depend on printing
92 // since it brings in libpng which causes duplicate symbols with PDFium.
93 const int kPointsPerInch = 72;
94 const int kPixelsPerInch = 96;
95
96 struct ClipBox {
97   float left;
98   float right;
99   float top;
100   float bottom;
101 };
102
103 int ConvertUnit(int value, int old_unit, int new_unit) {
104   // With integer arithmetic, to divide a value with correct rounding, you need
105   // to add half of the divisor value to the dividend value. You need to do the
106   // reverse with negative number.
107   if (value >= 0) {
108     return ((value * new_unit) + (old_unit / 2)) / old_unit;
109   } else {
110     return ((value * new_unit) - (old_unit / 2)) / old_unit;
111   }
112 }
113
114 std::vector<uint32_t> GetPageNumbersFromPrintPageNumberRange(
115     const PP_PrintPageNumberRange_Dev* page_ranges,
116     uint32_t page_range_count) {
117   std::vector<uint32_t> page_numbers;
118   for (uint32_t index = 0; index < page_range_count; ++index) {
119     for (uint32_t page_number = page_ranges[index].first_page_number;
120          page_number <= page_ranges[index].last_page_number; ++page_number) {
121       page_numbers.push_back(page_number);
122     }
123   }
124   return page_numbers;
125 }
126
127 #if defined(OS_LINUX)
128
129 PP_Instance g_last_instance_id;
130
131 struct PDFFontSubstitution {
132   const char* pdf_name;
133   const char* face;
134   bool bold;
135   bool italic;
136 };
137
138 PP_BrowserFont_Trusted_Weight WeightToBrowserFontTrustedWeight(int weight) {
139   COMPILE_ASSERT(PP_BROWSERFONT_TRUSTED_WEIGHT_100 == 0,
140                  PP_BrowserFont_Trusted_Weight_Min);
141   COMPILE_ASSERT(PP_BROWSERFONT_TRUSTED_WEIGHT_900 == 8,
142                  PP_BrowserFont_Trusted_Weight_Max);
143   const int kMinimumWeight = 100;
144   const int kMaximumWeight = 900;
145   int normalized_weight =
146       std::min(std::max(weight, kMinimumWeight), kMaximumWeight);
147   normalized_weight = (normalized_weight / 100) - 1;
148   return static_cast<PP_BrowserFont_Trusted_Weight>(normalized_weight);
149 }
150
151 // This list is for CPWL_FontMap::GetDefaultFontByCharset().
152 // We pretend to have these font natively and let the browser (or underlying
153 // fontconfig) to pick the proper font on the system.
154 void EnumFonts(struct _FPDF_SYSFONTINFO* sysfontinfo, void* mapper) {
155   FPDF_AddInstalledFont(mapper, "Arial", FXFONT_DEFAULT_CHARSET);
156
157   int i = 0;
158   while (CPWL_FontMap::defaultTTFMap[i].charset != -1) {
159     FPDF_AddInstalledFont(mapper,
160                           CPWL_FontMap::defaultTTFMap[i].fontname,
161                           CPWL_FontMap::defaultTTFMap[i].charset);
162     ++i;
163   }
164 }
165
166 const PDFFontSubstitution PDFFontSubstitutions[] = {
167     {"Courier", "Courier New", false, false},
168     {"Courier-Bold", "Courier New", true, false},
169     {"Courier-BoldOblique", "Courier New", true, true},
170     {"Courier-Oblique", "Courier New", false, true},
171     {"Helvetica", "Arial", false, false},
172     {"Helvetica-Bold", "Arial", true, false},
173     {"Helvetica-BoldOblique", "Arial", true, true},
174     {"Helvetica-Oblique", "Arial", false, true},
175     {"Times-Roman", "Times New Roman", false, false},
176     {"Times-Bold", "Times New Roman", true, false},
177     {"Times-BoldItalic", "Times New Roman", true, true},
178     {"Times-Italic", "Times New Roman", false, true},
179
180     // MS P?(Mincho|Gothic) are the most notable fonts in Japanese PDF files
181     // without embedding the glyphs. Sometimes the font names are encoded
182     // in Japanese Windows's locale (CP932/Shift_JIS) without space.
183     // Most Linux systems don't have the exact font, but for outsourcing
184     // fontconfig to find substitutable font in the system, we pass ASCII
185     // font names to it.
186     {"MS-PGothic", "MS PGothic", false, false},
187     {"MS-Gothic", "MS Gothic", false, false},
188     {"MS-PMincho", "MS PMincho", false, false},
189     {"MS-Mincho", "MS Mincho", false, false},
190     // MS PGothic in Shift_JIS encoding.
191     {"\x82\x6C\x82\x72\x82\x6F\x83\x53\x83\x56\x83\x62\x83\x4E",
192      "MS PGothic", false, false},
193     // MS Gothic in Shift_JIS encoding.
194     {"\x82\x6C\x82\x72\x83\x53\x83\x56\x83\x62\x83\x4E",
195      "MS Gothic", false, false},
196     // MS PMincho in Shift_JIS encoding.
197     {"\x82\x6C\x82\x72\x82\x6F\x96\xBE\x92\xA9",
198      "MS PMincho", false, false},
199     // MS Mincho in Shift_JIS encoding.
200     {"\x82\x6C\x82\x72\x96\xBE\x92\xA9",
201      "MS Mincho", false, false},
202 };
203
204 void* MapFont(struct _FPDF_SYSFONTINFO*, int weight, int italic,
205               int charset, int pitch_family, const char* face, int* exact) {
206   // Do not attempt to map fonts if pepper is not initialized (for privet local
207   // printing).
208   // TODO(noamsml): Real font substitution (http://crbug.com/391978)
209   if (!pp::Module::Get())
210     return NULL;
211
212   pp::BrowserFontDescription description;
213
214   // Pretend the system does not have the Symbol font to force a fallback to
215   // the built in Symbol font in CFX_FontMapper::FindSubstFont().
216   if (strcmp(face, "Symbol") == 0)
217     return NULL;
218
219   if (pitch_family & FXFONT_FF_FIXEDPITCH) {
220     description.set_family(PP_BROWSERFONT_TRUSTED_FAMILY_MONOSPACE);
221   } else if (pitch_family & FXFONT_FF_ROMAN) {
222     description.set_family(PP_BROWSERFONT_TRUSTED_FAMILY_SERIF);
223   }
224
225   // Map from the standard PDF fonts to TrueType font names.
226   size_t i;
227   for (i = 0; i < arraysize(PDFFontSubstitutions); ++i) {
228     if (strcmp(face, PDFFontSubstitutions[i].pdf_name) == 0) {
229       description.set_face(PDFFontSubstitutions[i].face);
230       if (PDFFontSubstitutions[i].bold)
231         description.set_weight(PP_BROWSERFONT_TRUSTED_WEIGHT_BOLD);
232       if (PDFFontSubstitutions[i].italic)
233         description.set_italic(true);
234       break;
235     }
236   }
237
238   if (i == arraysize(PDFFontSubstitutions)) {
239     // TODO(kochi): Pass the face in UTF-8. If face is not encoded in UTF-8,
240     // convert to UTF-8 before passing.
241     description.set_face(face);
242
243     description.set_weight(WeightToBrowserFontTrustedWeight(weight));
244     description.set_italic(italic > 0);
245   }
246
247   if (!pp::PDF::IsAvailable()) {
248     NOTREACHED();
249     return NULL;
250   }
251
252   PP_Resource font_resource = pp::PDF::GetFontFileWithFallback(
253       pp::InstanceHandle(g_last_instance_id),
254       &description.pp_font_description(),
255       static_cast<PP_PrivateFontCharset>(charset));
256   long res_id = font_resource;
257   return reinterpret_cast<void*>(res_id);
258 }
259
260 unsigned long GetFontData(struct _FPDF_SYSFONTINFO*, void* font_id,
261                           unsigned int table, unsigned char* buffer,
262                           unsigned long buf_size) {
263   if (!pp::PDF::IsAvailable()) {
264     NOTREACHED();
265     return 0;
266   }
267
268   uint32_t size = buf_size;
269   long res_id = reinterpret_cast<long>(font_id);
270   if (!pp::PDF::GetFontTableForPrivateFontFile(res_id, table, buffer, &size))
271     return 0;
272   return size;
273 }
274
275 void DeleteFont(struct _FPDF_SYSFONTINFO*, void* font_id) {
276   long res_id = reinterpret_cast<long>(font_id);
277   pp::Module::Get()->core()->ReleaseResource(res_id);
278 }
279
280 FPDF_SYSFONTINFO g_font_info = {
281   1,
282   0,
283   EnumFonts,
284   MapFont,
285   0,
286   GetFontData,
287   0,
288   0,
289   DeleteFont
290 };
291 #endif  // defined(OS_LINUX)
292
293 void OOM_Handler(_OOM_INFO*) {
294   // Kill the process.  This is important for security, since the code doesn't
295   // NULL-check many memory allocations.  If a malloc fails, returns NULL, and
296   // the buffer is then used, it provides a handy mapping of memory starting at
297   // address 0 for an attacker to utilize.
298   abort();
299 }
300
301 OOM_INFO g_oom_info = {
302   1,
303   OOM_Handler
304 };
305
306 PDFiumEngine* g_engine_for_unsupported;
307
308 void Unsupported_Handler(UNSUPPORT_INFO*, int type) {
309   if (!g_engine_for_unsupported) {
310     NOTREACHED();
311     return;
312   }
313
314   g_engine_for_unsupported->UnsupportedFeature(type);
315 }
316
317 UNSUPPORT_INFO g_unsuppored_info = {
318   1,
319   Unsupported_Handler
320 };
321
322 // Set the destination page size and content area in points based on source
323 // page rotation and orientation.
324 //
325 // |rotated| True if source page is rotated 90 degree or 270 degree.
326 // |is_src_page_landscape| is true if the source page orientation is landscape.
327 // |page_size| has the actual destination page size in points.
328 // |content_rect| has the actual destination page printable area values in
329 // points.
330 void SetPageSizeAndContentRect(bool rotated,
331                                bool is_src_page_landscape,
332                                pp::Size* page_size,
333                                pp::Rect* content_rect) {
334   bool is_dst_page_landscape = page_size->width() > page_size->height();
335   bool page_orientation_mismatched = is_src_page_landscape !=
336                                      is_dst_page_landscape;
337   bool rotate_dst_page = rotated ^ page_orientation_mismatched;
338   if (rotate_dst_page) {
339     page_size->SetSize(page_size->height(), page_size->width());
340     content_rect->SetRect(content_rect->y(), content_rect->x(),
341                           content_rect->height(), content_rect->width());
342   }
343 }
344
345 // Calculate the scale factor between |content_rect| and a page of size
346 // |src_width| x |src_height|.
347 //
348 // |scale_to_fit| is true, if we need to calculate the scale factor.
349 // |content_rect| specifies the printable area of the destination page, with
350 // origin at left-bottom. Values are in points.
351 // |src_width| specifies the source page width in points.
352 // |src_height| specifies the source page height in points.
353 // |rotated| True if source page is rotated 90 degree or 270 degree.
354 double CalculateScaleFactor(bool scale_to_fit,
355                             const pp::Rect& content_rect,
356                             double src_width, double src_height, bool rotated) {
357   if (!scale_to_fit || src_width == 0 || src_height == 0)
358     return 1.0;
359
360   double actual_source_page_width = rotated ? src_height : src_width;
361   double actual_source_page_height = rotated ? src_width : src_height;
362   double ratio_x = static_cast<double>(content_rect.width()) /
363                    actual_source_page_width;
364   double ratio_y = static_cast<double>(content_rect.height()) /
365                    actual_source_page_height;
366   return std::min(ratio_x, ratio_y);
367 }
368
369 // Compute source clip box boundaries based on the crop box / media box of
370 // source page and scale factor.
371 //
372 // |page| Handle to the source page. Returned by FPDF_LoadPage function.
373 // |scale_factor| specifies the scale factor that should be applied to source
374 // clip box boundaries.
375 // |rotated| True if source page is rotated 90 degree or 270 degree.
376 // |clip_box| out param to hold the computed source clip box values.
377 void CalculateClipBoxBoundary(FPDF_PAGE page, double scale_factor, bool rotated,
378                               ClipBox* clip_box) {
379   if (!FPDFPage_GetCropBox(page, &clip_box->left, &clip_box->bottom,
380                            &clip_box->right, &clip_box->top)) {
381     if (!FPDFPage_GetMediaBox(page, &clip_box->left, &clip_box->bottom,
382                               &clip_box->right, &clip_box->top)) {
383       // Make the default size to be letter size (8.5" X 11"). We are just
384       // following the PDFium way of handling these corner cases. PDFium always
385       // consider US-Letter as the default page size.
386       float paper_width = 612;
387       float paper_height = 792;
388       clip_box->left = 0;
389       clip_box->bottom = 0;
390       clip_box->right = rotated ? paper_height : paper_width;
391       clip_box->top = rotated ? paper_width : paper_height;
392     }
393   }
394   clip_box->left *= scale_factor;
395   clip_box->right *= scale_factor;
396   clip_box->bottom *= scale_factor;
397   clip_box->top *= scale_factor;
398 }
399
400 // Calculate the clip box translation offset for a page that does need to be
401 // scaled. All parameters are in points.
402 //
403 // |content_rect| specifies the printable area of the destination page, with
404 // origin at left-bottom.
405 // |source_clip_box| specifies the source clip box positions, relative to
406 // origin at left-bottom.
407 // |offset_x| and |offset_y| will contain the final translation offsets for the
408 // source clip box, relative to origin at left-bottom.
409 void CalculateScaledClipBoxOffset(const pp::Rect& content_rect,
410                                   const ClipBox& source_clip_box,
411                                   double* offset_x, double* offset_y) {
412   const float clip_box_width = source_clip_box.right - source_clip_box.left;
413   const float clip_box_height = source_clip_box.top - source_clip_box.bottom;
414
415   // Center the intended clip region to real clip region.
416   *offset_x = (content_rect.width() - clip_box_width) / 2 + content_rect.x() -
417               source_clip_box.left;
418   *offset_y = (content_rect.height() - clip_box_height) / 2 + content_rect.y() -
419               source_clip_box.bottom;
420 }
421
422 // Calculate the clip box offset for a page that does not need to be scaled.
423 // All parameters are in points.
424 //
425 // |content_rect| specifies the printable area of the destination page, with
426 // origin at left-bottom.
427 // |rotation| specifies the source page rotation values which are N / 90
428 // degrees.
429 // |page_width| specifies the screen destination page width.
430 // |page_height| specifies the screen destination page height.
431 // |source_clip_box| specifies the source clip box positions, relative to origin
432 // at left-bottom.
433 // |offset_x| and |offset_y| will contain the final translation offsets for the
434 // source clip box, relative to origin at left-bottom.
435 void CalculateNonScaledClipBoxOffset(const pp::Rect& content_rect, int rotation,
436                                      int page_width, int page_height,
437                                      const ClipBox& source_clip_box,
438                                      double* offset_x, double* offset_y) {
439   // Align the intended clip region to left-top corner of real clip region.
440   switch (rotation) {
441     case 0:
442       *offset_x = -1 * source_clip_box.left;
443       *offset_y = page_height - source_clip_box.top;
444       break;
445     case 1:
446       *offset_x = 0;
447       *offset_y = -1 * source_clip_box.bottom;
448       break;
449     case 2:
450       *offset_x = page_width - source_clip_box.right;
451       *offset_y = 0;
452       break;
453     case 3:
454       *offset_x = page_height - source_clip_box.right;
455       *offset_y = page_width - source_clip_box.top;
456       break;
457     default:
458       NOTREACHED();
459       break;
460   }
461 }
462
463 // This formats a string with special 0xfffe end-of-line hyphens the same way
464 // as Adobe Reader. When a hyphen is encountered, the next non-CR/LF whitespace
465 // becomes CR+LF and the hyphen is erased. If there is no whitespace between
466 // two hyphens, the latter hyphen is erased and ignored.
467 void FormatStringWithHyphens(base::string16* text) {
468   // First pass marks all the hyphen positions.
469   struct HyphenPosition {
470     HyphenPosition() : position(0), next_whitespace_position(0) {}
471     size_t position;
472     size_t next_whitespace_position;  // 0 for none
473   };
474   std::vector<HyphenPosition> hyphen_positions;
475   HyphenPosition current_hyphen_position;
476   bool current_hyphen_position_is_valid = false;
477   const base::char16 kPdfiumHyphenEOL = 0xfffe;
478
479   for (size_t i = 0; i < text->size(); ++i) {
480     const base::char16& current_char = (*text)[i];
481     if (current_char == kPdfiumHyphenEOL) {
482       if (current_hyphen_position_is_valid)
483         hyphen_positions.push_back(current_hyphen_position);
484       current_hyphen_position = HyphenPosition();
485       current_hyphen_position.position = i;
486       current_hyphen_position_is_valid = true;
487     } else if (IsWhitespace(current_char)) {
488       if (current_hyphen_position_is_valid) {
489         if (current_char != L'\r' && current_char != L'\n')
490           current_hyphen_position.next_whitespace_position = i;
491         hyphen_positions.push_back(current_hyphen_position);
492         current_hyphen_position_is_valid = false;
493       }
494     }
495   }
496   if (current_hyphen_position_is_valid)
497     hyphen_positions.push_back(current_hyphen_position);
498
499   // With all the hyphen positions, do the search and replace.
500   while (!hyphen_positions.empty()) {
501     static const base::char16 kCr[] = {L'\r', L'\0'};
502     const HyphenPosition& position = hyphen_positions.back();
503     if (position.next_whitespace_position != 0) {
504       (*text)[position.next_whitespace_position] = L'\n';
505       text->insert(position.next_whitespace_position, kCr);
506     }
507     text->erase(position.position, 1);
508     hyphen_positions.pop_back();
509   }
510
511   // Adobe Reader also get rid of trailing spaces right before a CRLF.
512   static const base::char16 kSpaceCrCn[] = {L' ', L'\r', L'\n', L'\0'};
513   static const base::char16 kCrCn[] = {L'\r', L'\n', L'\0'};
514   ReplaceSubstringsAfterOffset(text, 0, kSpaceCrCn, kCrCn);
515 }
516
517 // Replace CR/LF with just LF on POSIX.
518 void FormatStringForOS(base::string16* text) {
519 #if defined(OS_POSIX)
520   static const base::char16 kCr[] = {L'\r', L'\0'};
521   static const base::char16 kBlank[] = {L'\0'};
522   base::ReplaceChars(*text, kCr, kBlank, text);
523 #elif defined(OS_WIN)
524   // Do nothing
525 #else
526   NOTIMPLEMENTED();
527 #endif
528 }
529
530 }  // namespace
531
532 bool InitializeSDK(void* data) {
533   FPDF_InitLibrary(data);
534
535 #if defined(OS_LINUX)
536   // Font loading doesn't work in the renderer sandbox in Linux.
537   FPDF_SetSystemFontInfo(&g_font_info);
538 #endif
539
540   FSDK_SetOOMHandler(&g_oom_info);
541   FSDK_SetUnSpObjProcessHandler(&g_unsuppored_info);
542
543   return true;
544 }
545
546 void ShutdownSDK() {
547   FPDF_DestroyLibrary();
548 }
549
550 PDFEngine* PDFEngine::Create(PDFEngine::Client* client) {
551   return new PDFiumEngine(client);
552 }
553
554 PDFiumEngine::PDFiumEngine(PDFEngine::Client* client)
555     : client_(client),
556       current_zoom_(1.0),
557       current_rotation_(0),
558       doc_loader_(this),
559       password_tries_remaining_(0),
560       doc_(NULL),
561       form_(NULL),
562       defer_page_unload_(false),
563       selecting_(false),
564       mouse_down_state_(PDFiumPage::NONSELECTABLE_AREA,
565                         PDFiumPage::LinkTarget()),
566       next_page_to_search_(-1),
567       last_page_to_search_(-1),
568       last_character_index_to_search_(-1),
569       current_find_index_(-1),
570       permissions_(0),
571       fpdf_availability_(NULL),
572       next_timer_id_(0),
573       last_page_mouse_down_(-1),
574       first_visible_page_(-1),
575       most_visible_page_(-1),
576       called_do_document_action_(false),
577       render_grayscale_(false),
578       progressive_paint_timeout_(0),
579       getting_password_(false) {
580   find_factory_.Initialize(this);
581   password_factory_.Initialize(this);
582
583   file_access_.m_FileLen = 0;
584   file_access_.m_GetBlock = &GetBlock;
585   file_access_.m_Param = &doc_loader_;
586
587   file_availability_.version = 1;
588   file_availability_.IsDataAvail = &IsDataAvail;
589   file_availability_.loader = &doc_loader_;
590
591   download_hints_.version = 1;
592   download_hints_.AddSegment = &AddSegment;
593   download_hints_.loader = &doc_loader_;
594
595   // Initialize FPDF_FORMFILLINFO member variables.  Deriving from this struct
596   // allows the static callbacks to be able to cast the FPDF_FORMFILLINFO in
597   // callbacks to ourself instead of maintaining a map of them to
598   // PDFiumEngine.
599   FPDF_FORMFILLINFO::version = 1;
600   FPDF_FORMFILLINFO::m_pJsPlatform = this;
601   FPDF_FORMFILLINFO::Release = NULL;
602   FPDF_FORMFILLINFO::FFI_Invalidate = Form_Invalidate;
603   FPDF_FORMFILLINFO::FFI_OutputSelectedRect = Form_OutputSelectedRect;
604   FPDF_FORMFILLINFO::FFI_SetCursor = Form_SetCursor;
605   FPDF_FORMFILLINFO::FFI_SetTimer = Form_SetTimer;
606   FPDF_FORMFILLINFO::FFI_KillTimer = Form_KillTimer;
607   FPDF_FORMFILLINFO::FFI_GetLocalTime = Form_GetLocalTime;
608   FPDF_FORMFILLINFO::FFI_OnChange = Form_OnChange;
609   FPDF_FORMFILLINFO::FFI_GetPage = Form_GetPage;
610   FPDF_FORMFILLINFO::FFI_GetCurrentPage = Form_GetCurrentPage;
611   FPDF_FORMFILLINFO::FFI_GetRotation = Form_GetRotation;
612   FPDF_FORMFILLINFO::FFI_ExecuteNamedAction = Form_ExecuteNamedAction;
613   FPDF_FORMFILLINFO::FFI_SetTextFieldFocus = Form_SetTextFieldFocus;
614   FPDF_FORMFILLINFO::FFI_DoURIAction = Form_DoURIAction;
615   FPDF_FORMFILLINFO::FFI_DoGoToAction = Form_DoGoToAction;
616
617   IPDF_JSPLATFORM::version = 1;
618   IPDF_JSPLATFORM::app_alert = Form_Alert;
619   IPDF_JSPLATFORM::app_beep = Form_Beep;
620   IPDF_JSPLATFORM::app_response = Form_Response;
621   IPDF_JSPLATFORM::Doc_getFilePath = Form_GetFilePath;
622   IPDF_JSPLATFORM::Doc_mail = Form_Mail;
623   IPDF_JSPLATFORM::Doc_print = Form_Print;
624   IPDF_JSPLATFORM::Doc_submitForm = Form_SubmitForm;
625   IPDF_JSPLATFORM::Doc_gotoPage = Form_GotoPage;
626   IPDF_JSPLATFORM::Field_browse = Form_Browse;
627
628   IFSDK_PAUSE::version = 1;
629   IFSDK_PAUSE::user = NULL;
630   IFSDK_PAUSE::NeedToPauseNow = Pause_NeedToPauseNow;
631 }
632
633 PDFiumEngine::~PDFiumEngine() {
634   for (size_t i = 0; i < pages_.size(); ++i)
635     pages_[i]->Unload();
636
637   if (doc_) {
638     if (form_) {
639       FORM_DoDocumentAAction(form_, FPDFDOC_AACTION_WC);
640       FPDFDOC_ExitFormFillEnviroument(form_);
641     }
642     FPDF_CloseDocument(doc_);
643   }
644
645   if (fpdf_availability_)
646     FPDFAvail_Destroy(fpdf_availability_);
647
648   STLDeleteElements(&pages_);
649 }
650
651 int PDFiumEngine::GetBlock(void* param, unsigned long position,
652                            unsigned char* buffer, unsigned long size) {
653   DocumentLoader* loader = static_cast<DocumentLoader*>(param);
654   return loader->GetBlock(position, size, buffer);
655 }
656
657 bool PDFiumEngine::IsDataAvail(FX_FILEAVAIL* param,
658                                size_t offset, size_t size) {
659   PDFiumEngine::FileAvail* file_avail =
660       static_cast<PDFiumEngine::FileAvail*>(param);
661   return file_avail->loader->IsDataAvailable(offset, size);
662 }
663
664 void PDFiumEngine::AddSegment(FX_DOWNLOADHINTS* param,
665                               size_t offset, size_t size) {
666   PDFiumEngine::DownloadHints* download_hints =
667       static_cast<PDFiumEngine::DownloadHints*>(param);
668   return download_hints->loader->RequestData(offset, size);
669 }
670
671 bool PDFiumEngine::New(const char* url) {
672   url_ = url;
673   headers_ = std::string();
674   return true;
675 }
676
677 bool PDFiumEngine::New(const char* url,
678                        const char* headers) {
679   url_ = url;
680   if (!headers)
681     headers_ = std::string();
682   else
683     headers_ = headers;
684   return true;
685 }
686
687 void PDFiumEngine::PageOffsetUpdated(const pp::Point& page_offset) {
688   page_offset_ = page_offset;
689 }
690
691 void PDFiumEngine::PluginSizeUpdated(const pp::Size& size) {
692   CancelPaints();
693
694   plugin_size_ = size;
695   CalculateVisiblePages();
696 }
697
698 void PDFiumEngine::ScrolledToXPosition(int position) {
699   CancelPaints();
700
701   int old_x = position_.x();
702   position_.set_x(position);
703   CalculateVisiblePages();
704   client_->Scroll(pp::Point(old_x - position, 0));
705 }
706
707 void PDFiumEngine::ScrolledToYPosition(int position) {
708   CancelPaints();
709
710   int old_y = position_.y();
711   position_.set_y(position);
712   CalculateVisiblePages();
713   client_->Scroll(pp::Point(0, old_y - position));
714 }
715
716 void PDFiumEngine::PrePaint() {
717   for (size_t i = 0; i < progressive_paints_.size(); ++i)
718     progressive_paints_[i].painted_ = false;
719 }
720
721 void PDFiumEngine::Paint(const pp::Rect& rect,
722                          pp::ImageData* image_data,
723                          std::vector<pp::Rect>* ready,
724                          std::vector<pp::Rect>* pending) {
725   pp::Rect leftover = rect;
726   for (size_t i = 0; i < visible_pages_.size(); ++i) {
727     int index = visible_pages_[i];
728     pp::Rect page_rect = pages_[index]->rect();
729     // Convert the current page's rectangle to screen rectangle.  We do this
730     // instead of the reverse (converting the dirty rectangle from screen to
731     // page coordinates) because then we'd have to convert back to screen
732     // coordinates, and the rounding errors sometime leave pixels dirty or even
733     // move the text up or down a pixel when zoomed.
734     pp::Rect page_rect_in_screen = GetPageScreenRect(index);
735     pp::Rect dirty_in_screen = page_rect_in_screen.Intersect(leftover);
736     if (dirty_in_screen.IsEmpty())
737       continue;
738
739     leftover = leftover.Subtract(dirty_in_screen);
740
741     if (pages_[index]->available()) {
742       int progressive = GetProgressiveIndex(index);
743       if (progressive != -1 &&
744           progressive_paints_[progressive].rect != dirty_in_screen) {
745         // The PDFium code can only handle one progressive paint at a time, so
746         // queue this up. Previously we used to merge the rects when this
747         // happened, but it made scrolling up on complex PDFs very slow since
748         // there would be a damaged rect at the top (from scroll) and at the
749         // bottom (from toolbar).
750         pending->push_back(dirty_in_screen);
751         continue;
752       }
753
754       if (progressive == -1) {
755         progressive = StartPaint(index, dirty_in_screen);
756         progressive_paint_timeout_ = kMaxInitialProgressivePaintTimeMs;
757       } else {
758         progressive_paint_timeout_ = kMaxProgressivePaintTimeMs;
759       }
760
761       progressive_paints_[progressive].painted_ = true;
762       if (ContinuePaint(progressive, image_data)) {
763         FinishPaint(progressive, image_data);
764         ready->push_back(dirty_in_screen);
765       } else {
766         pending->push_back(dirty_in_screen);
767       }
768     } else {
769       PaintUnavailablePage(index, dirty_in_screen, image_data);
770       ready->push_back(dirty_in_screen);
771     }
772   }
773 }
774
775 void PDFiumEngine::PostPaint() {
776   for (size_t i = 0; i < progressive_paints_.size(); ++i) {
777     if (progressive_paints_[i].painted_)
778       continue;
779
780     // This rectangle must have been merged with another one, that's why we
781     // weren't asked to paint it. Remove it or otherwise we'll never finish
782     // painting.
783     FPDF_RenderPage_Close(
784         pages_[progressive_paints_[i].page_index]->GetPage());
785     FPDFBitmap_Destroy(progressive_paints_[i].bitmap);
786     progressive_paints_.erase(progressive_paints_.begin() + i);
787     --i;
788   }
789 }
790
791 bool PDFiumEngine::HandleDocumentLoad(const pp::URLLoader& loader) {
792   password_tries_remaining_ = kMaxPasswordTries;
793   return doc_loader_.Init(loader, url_, headers_);
794 }
795
796 pp::Instance* PDFiumEngine::GetPluginInstance() {
797   return client_->GetPluginInstance();
798 }
799
800 pp::URLLoader PDFiumEngine::CreateURLLoader() {
801   return client_->CreateURLLoader();
802 }
803
804 void PDFiumEngine::AppendPage(PDFEngine* engine, int index) {
805   // Unload and delete the blank page before appending.
806   pages_[index]->Unload();
807   pages_[index]->set_calculated_links(false);
808   pp::Size curr_page_size = GetPageSize(index);
809   FPDFPage_Delete(doc_, index);
810   FPDF_ImportPages(doc_,
811                    static_cast<PDFiumEngine*>(engine)->doc(),
812                    "1",
813                    index);
814   pp::Size new_page_size = GetPageSize(index);
815   if (curr_page_size != new_page_size)
816     LoadPageInfo(true);
817   client_->Invalidate(GetPageScreenRect(index));
818 }
819
820 pp::Point PDFiumEngine::GetScrollPosition() {
821   return position_;
822 }
823
824 void PDFiumEngine::SetScrollPosition(const pp::Point& position) {
825   position_ = position;
826 }
827
828 bool PDFiumEngine::IsProgressiveLoad() {
829   return doc_loader_.is_partial_document();
830 }
831
832 void PDFiumEngine::OnPartialDocumentLoaded() {
833   file_access_.m_FileLen = doc_loader_.document_size();
834   fpdf_availability_ = FPDFAvail_Create(&file_availability_, &file_access_);
835   DCHECK(fpdf_availability_);
836
837   // Currently engine does not deal efficiently with some non-linearized files.
838   // See http://code.google.com/p/chromium/issues/detail?id=59400
839   // To improve user experience we download entire file for non-linearized PDF.
840   if (!FPDFAvail_IsLinearized(fpdf_availability_)) {
841     doc_loader_.RequestData(0, doc_loader_.document_size());
842     return;
843   }
844
845   LoadDocument();
846 }
847
848 void PDFiumEngine::OnPendingRequestComplete() {
849   if (!doc_ || !form_) {
850     LoadDocument();
851     return;
852   }
853
854   // LoadDocument() will result in |pending_pages_| being reset so there's no
855   // need to run the code below in that case.
856   bool update_pages = false;
857   std::vector<int> still_pending;
858   for (size_t i = 0; i < pending_pages_.size(); ++i) {
859     if (CheckPageAvailable(pending_pages_[i], &still_pending)) {
860       update_pages = true;
861       if (IsPageVisible(pending_pages_[i]))
862         client_->Invalidate(GetPageScreenRect(pending_pages_[i]));
863     }
864   }
865   pending_pages_.swap(still_pending);
866   if (update_pages)
867     LoadPageInfo(true);
868 }
869
870 void PDFiumEngine::OnNewDataAvailable() {
871   client_->DocumentLoadProgress(doc_loader_.GetAvailableData(),
872                                 doc_loader_.document_size());
873 }
874
875 void PDFiumEngine::OnDocumentComplete() {
876   if (!doc_ || !form_) {
877     file_access_.m_FileLen = doc_loader_.document_size();
878     LoadDocument();
879     return;
880   }
881
882   bool need_update = false;
883   for (size_t i = 0; i < pages_.size(); ++i) {
884     if (pages_[i]->available())
885       continue;
886
887     pages_[i]->set_available(true);
888     // We still need to call IsPageAvail() even if the whole document is
889     // already downloaded.
890     FPDFAvail_IsPageAvail(fpdf_availability_, i, &download_hints_);
891     need_update = true;
892     if (IsPageVisible(i))
893       client_->Invalidate(GetPageScreenRect(i));
894   }
895   if (need_update)
896     LoadPageInfo(true);
897
898   FinishLoadingDocument();
899 }
900
901 void PDFiumEngine::FinishLoadingDocument() {
902   DCHECK(doc_loader_.IsDocumentComplete() && doc_);
903   if (called_do_document_action_)
904     return;
905   called_do_document_action_ = true;
906
907   // These can only be called now, as the JS might end up needing a page.
908   FORM_DoDocumentJSAction(form_);
909   FORM_DoDocumentOpenAction(form_);
910   if (most_visible_page_ != -1) {
911     FPDF_PAGE new_page = pages_[most_visible_page_]->GetPage();
912     FORM_DoPageAAction(new_page, form_, FPDFPAGE_AACTION_OPEN);
913   }
914
915   if (doc_) // This can only happen if loading |doc_| fails.
916     client_->DocumentLoadComplete(pages_.size());
917 }
918
919 void PDFiumEngine::UnsupportedFeature(int type) {
920   std::string feature;
921   switch (type) {
922     case FPDF_UNSP_DOC_XFAFORM:
923       feature = "XFA";
924       break;
925     case FPDF_UNSP_DOC_PORTABLECOLLECTION:
926       feature = "Portfolios_Packages";
927       break;
928     case FPDF_UNSP_DOC_ATTACHMENT:
929     case FPDF_UNSP_ANNOT_ATTACHMENT:
930       feature = "Attachment";
931       break;
932     case FPDF_UNSP_DOC_SECURITY:
933       feature = "Rights_Management";
934       break;
935     case FPDF_UNSP_DOC_SHAREDREVIEW:
936       feature = "Shared_Review";
937       break;
938     case FPDF_UNSP_DOC_SHAREDFORM_ACROBAT:
939     case FPDF_UNSP_DOC_SHAREDFORM_FILESYSTEM:
940     case FPDF_UNSP_DOC_SHAREDFORM_EMAIL:
941       feature = "Shared_Form";
942       break;
943     case FPDF_UNSP_ANNOT_3DANNOT:
944       feature = "3D";
945       break;
946     case FPDF_UNSP_ANNOT_MOVIE:
947       feature = "Movie";
948       break;
949     case FPDF_UNSP_ANNOT_SOUND:
950       feature = "Sound";
951       break;
952     case FPDF_UNSP_ANNOT_SCREEN_MEDIA:
953     case FPDF_UNSP_ANNOT_SCREEN_RICHMEDIA:
954       feature = "Screen";
955       break;
956     case FPDF_UNSP_ANNOT_SIG:
957       feature = "Digital_Signature";
958       break;
959   }
960   client_->DocumentHasUnsupportedFeature(feature);
961 }
962
963 void PDFiumEngine::ContinueFind(int32_t result) {
964   StartFind(current_find_text_.c_str(), !!result);
965 }
966
967 bool PDFiumEngine::HandleEvent(const pp::InputEvent& event) {
968   DCHECK(!defer_page_unload_);
969   defer_page_unload_ = true;
970   bool rv = false;
971   switch (event.GetType()) {
972     case PP_INPUTEVENT_TYPE_MOUSEDOWN:
973       rv = OnMouseDown(pp::MouseInputEvent(event));
974       break;
975     case PP_INPUTEVENT_TYPE_MOUSEUP:
976       rv = OnMouseUp(pp::MouseInputEvent(event));
977       break;
978     case PP_INPUTEVENT_TYPE_MOUSEMOVE:
979       rv = OnMouseMove(pp::MouseInputEvent(event));
980       break;
981     case PP_INPUTEVENT_TYPE_KEYDOWN:
982       rv = OnKeyDown(pp::KeyboardInputEvent(event));
983       break;
984     case PP_INPUTEVENT_TYPE_KEYUP:
985       rv = OnKeyUp(pp::KeyboardInputEvent(event));
986       break;
987     case PP_INPUTEVENT_TYPE_CHAR:
988       rv = OnChar(pp::KeyboardInputEvent(event));
989       break;
990     default:
991       break;
992   }
993
994   DCHECK(defer_page_unload_);
995   defer_page_unload_ = false;
996   for (size_t i = 0; i < deferred_page_unloads_.size(); ++i)
997     pages_[deferred_page_unloads_[i]]->Unload();
998   deferred_page_unloads_.clear();
999   return rv;
1000 }
1001
1002 uint32_t PDFiumEngine::QuerySupportedPrintOutputFormats() {
1003   if (!HasPermission(PDFEngine::PERMISSION_PRINT_LOW_QUALITY))
1004     return 0;
1005   return PP_PRINTOUTPUTFORMAT_PDF;
1006 }
1007
1008 void PDFiumEngine::PrintBegin() {
1009   FORM_DoDocumentAAction(form_, FPDFDOC_AACTION_WP);
1010 }
1011
1012 pp::Resource PDFiumEngine::PrintPages(
1013     const PP_PrintPageNumberRange_Dev* page_ranges, uint32_t page_range_count,
1014     const PP_PrintSettings_Dev& print_settings) {
1015   if (HasPermission(PDFEngine::PERMISSION_PRINT_HIGH_QUALITY))
1016     return PrintPagesAsPDF(page_ranges, page_range_count, print_settings);
1017   else if (HasPermission(PDFEngine::PERMISSION_PRINT_LOW_QUALITY))
1018     return PrintPagesAsRasterPDF(page_ranges, page_range_count, print_settings);
1019   return pp::Resource();
1020 }
1021
1022 FPDF_DOCUMENT PDFiumEngine::CreateSinglePageRasterPdf(
1023     double source_page_width,
1024     double source_page_height,
1025     const PP_PrintSettings_Dev& print_settings,
1026     PDFiumPage* page_to_print) {
1027   FPDF_DOCUMENT temp_doc = FPDF_CreateNewDocument();
1028   if (!temp_doc)
1029     return temp_doc;
1030
1031   const pp::Size& bitmap_size(page_to_print->rect().size());
1032
1033   FPDF_PAGE temp_page =
1034       FPDFPage_New(temp_doc, 0, source_page_width, source_page_height);
1035
1036   pp::ImageData image = pp::ImageData(client_->GetPluginInstance(),
1037                                       PP_IMAGEDATAFORMAT_BGRA_PREMUL,
1038                                       bitmap_size,
1039                                       false);
1040
1041   FPDF_BITMAP bitmap = FPDFBitmap_CreateEx(bitmap_size.width(),
1042                                            bitmap_size.height(),
1043                                            FPDFBitmap_BGRx,
1044                                            image.data(),
1045                                            image.stride());
1046
1047   // Clear the bitmap
1048   FPDFBitmap_FillRect(
1049       bitmap, 0, 0, bitmap_size.width(), bitmap_size.height(), 0xFFFFFFFF);
1050
1051   pp::Rect page_rect = page_to_print->rect();
1052   FPDF_RenderPageBitmap(bitmap,
1053                         page_to_print->GetPrintPage(),
1054                         page_rect.x(),
1055                         page_rect.y(),
1056                         page_rect.width(),
1057                         page_rect.height(),
1058                         print_settings.orientation,
1059                         FPDF_ANNOT | FPDF_PRINTING | FPDF_NO_CATCH);
1060
1061   double ratio_x = (static_cast<double>(bitmap_size.width()) * kPointsPerInch) /
1062                    print_settings.dpi;
1063   double ratio_y =
1064       (static_cast<double>(bitmap_size.height()) * kPointsPerInch) /
1065       print_settings.dpi;
1066
1067   // Add the bitmap to an image object and add the image object to the output
1068   // page.
1069   FPDF_PAGEOBJECT temp_img = FPDFPageObj_NewImgeObj(temp_doc);
1070   FPDFImageObj_SetBitmap(&temp_page, 1, temp_img, bitmap);
1071   FPDFImageObj_SetMatrix(temp_img, ratio_x, 0, 0, ratio_y, 0, 0);
1072   FPDFPage_InsertObject(temp_page, temp_img);
1073   FPDFPage_GenerateContent(temp_page);
1074   FPDF_ClosePage(temp_page);
1075
1076   page_to_print->ClosePrintPage();
1077   FPDFBitmap_Destroy(bitmap);
1078
1079   return temp_doc;
1080 }
1081
1082 pp::Buffer_Dev PDFiumEngine::PrintPagesAsRasterPDF(
1083     const PP_PrintPageNumberRange_Dev* page_ranges, uint32_t page_range_count,
1084     const PP_PrintSettings_Dev& print_settings) {
1085   if (!page_range_count)
1086     return pp::Buffer_Dev();
1087
1088   // If document is not downloaded yet, disable printing.
1089   if (doc_ && !doc_loader_.IsDocumentComplete())
1090     return pp::Buffer_Dev();
1091
1092   FPDF_DOCUMENT output_doc = FPDF_CreateNewDocument();
1093   if (!output_doc)
1094     return pp::Buffer_Dev();
1095
1096   SaveSelectedFormForPrint();
1097
1098   std::vector<PDFiumPage> pages_to_print;
1099   // width and height of source PDF pages.
1100   std::vector<std::pair<double, double> > source_page_sizes;
1101   // Collect pages to print and sizes of source pages.
1102   std::vector<uint32_t> page_numbers =
1103       GetPageNumbersFromPrintPageNumberRange(page_ranges, page_range_count);
1104   for (size_t i = 0; i < page_numbers.size(); ++i) {
1105     uint32_t page_number = page_numbers[i];
1106     FPDF_PAGE pdf_page = FPDF_LoadPage(doc_, page_number);
1107     double source_page_width = FPDF_GetPageWidth(pdf_page);
1108     double source_page_height = FPDF_GetPageHeight(pdf_page);
1109     source_page_sizes.push_back(std::make_pair(source_page_width,
1110                                                source_page_height));
1111
1112     int width_in_pixels = ConvertUnit(source_page_width,
1113                                       static_cast<int>(kPointsPerInch),
1114                                       print_settings.dpi);
1115     int height_in_pixels = ConvertUnit(source_page_height,
1116                                        static_cast<int>(kPointsPerInch),
1117                                        print_settings.dpi);
1118
1119     pp::Rect rect(width_in_pixels, height_in_pixels);
1120     pages_to_print.push_back(PDFiumPage(this, page_number, rect, true));
1121     FPDF_ClosePage(pdf_page);
1122   }
1123
1124 #if defined(OS_LINUX)
1125   g_last_instance_id = client_->GetPluginInstance()->pp_instance();
1126 #endif
1127
1128   size_t i = 0;
1129   for (; i < pages_to_print.size(); ++i) {
1130     double source_page_width = source_page_sizes[i].first;
1131     double source_page_height = source_page_sizes[i].second;
1132
1133     // Use temp_doc to compress image by saving PDF to buffer.
1134     FPDF_DOCUMENT temp_doc = CreateSinglePageRasterPdf(source_page_width,
1135                                                        source_page_height,
1136                                                        print_settings,
1137                                                        &pages_to_print[i]);
1138
1139     if (!temp_doc)
1140       break;
1141
1142     pp::Buffer_Dev buffer = GetFlattenedPrintData(temp_doc);
1143     FPDF_CloseDocument(temp_doc);
1144
1145     PDFiumMemBufferFileRead file_read(buffer.data(), buffer.size());
1146     temp_doc = FPDF_LoadCustomDocument(&file_read, NULL);
1147
1148     FPDF_BOOL imported = FPDF_ImportPages(output_doc, temp_doc, "1", i);
1149     FPDF_CloseDocument(temp_doc);
1150     if (!imported)
1151       break;
1152   }
1153
1154   pp::Buffer_Dev buffer;
1155   if (i == pages_to_print.size()) {
1156     FPDF_CopyViewerPreferences(output_doc, doc_);
1157     FitContentsToPrintableAreaIfRequired(output_doc, print_settings);
1158     // Now flatten all the output pages.
1159     buffer = GetFlattenedPrintData(output_doc);
1160   }
1161   FPDF_CloseDocument(output_doc);
1162   return buffer;
1163 }
1164
1165 pp::Buffer_Dev PDFiumEngine::GetFlattenedPrintData(const FPDF_DOCUMENT& doc) {
1166   int page_count = FPDF_GetPageCount(doc);
1167   bool flatten_succeeded = true;
1168   for (int i = 0; i < page_count; ++i) {
1169     FPDF_PAGE page = FPDF_LoadPage(doc, i);
1170     DCHECK(page);
1171     if (page) {
1172       int flatten_ret = FPDFPage_Flatten(page, FLAT_PRINT);
1173       FPDF_ClosePage(page);
1174       if (flatten_ret == FLATTEN_FAIL) {
1175         flatten_succeeded = false;
1176         break;
1177       }
1178     } else {
1179       flatten_succeeded = false;
1180       break;
1181     }
1182   }
1183   if (!flatten_succeeded) {
1184     FPDF_CloseDocument(doc);
1185     return pp::Buffer_Dev();
1186   }
1187
1188   pp::Buffer_Dev buffer;
1189   PDFiumMemBufferFileWrite output_file_write;
1190   if (FPDF_SaveAsCopy(doc, &output_file_write, 0)) {
1191     buffer = pp::Buffer_Dev(
1192         client_->GetPluginInstance(), output_file_write.size());
1193     if (!buffer.is_null()) {
1194       memcpy(buffer.data(), output_file_write.buffer().c_str(),
1195              output_file_write.size());
1196     }
1197   }
1198   return buffer;
1199 }
1200
1201 pp::Buffer_Dev PDFiumEngine::PrintPagesAsPDF(
1202     const PP_PrintPageNumberRange_Dev* page_ranges, uint32_t page_range_count,
1203     const PP_PrintSettings_Dev& print_settings) {
1204   if (!page_range_count)
1205     return pp::Buffer_Dev();
1206
1207   DCHECK(doc_);
1208   FPDF_DOCUMENT output_doc = FPDF_CreateNewDocument();
1209   if (!output_doc)
1210     return pp::Buffer_Dev();
1211
1212   SaveSelectedFormForPrint();
1213
1214   std::string page_number_str;
1215   for (uint32_t index = 0; index < page_range_count; ++index) {
1216     if (!page_number_str.empty())
1217       page_number_str.append(",");
1218     page_number_str.append(
1219         base::IntToString(page_ranges[index].first_page_number + 1));
1220     if (page_ranges[index].first_page_number !=
1221             page_ranges[index].last_page_number) {
1222       page_number_str.append("-");
1223       page_number_str.append(
1224           base::IntToString(page_ranges[index].last_page_number + 1));
1225     }
1226   }
1227
1228   std::vector<uint32_t> page_numbers =
1229       GetPageNumbersFromPrintPageNumberRange(page_ranges, page_range_count);
1230   for (size_t i = 0; i < page_numbers.size(); ++i) {
1231     uint32_t page_number = page_numbers[i];
1232     pages_[page_number]->GetPage();
1233     if (!IsPageVisible(page_numbers[i]))
1234       pages_[page_number]->Unload();
1235   }
1236
1237   FPDF_CopyViewerPreferences(output_doc, doc_);
1238   if (!FPDF_ImportPages(output_doc, doc_, page_number_str.c_str(), 0)) {
1239     FPDF_CloseDocument(output_doc);
1240     return pp::Buffer_Dev();
1241   }
1242
1243   FitContentsToPrintableAreaIfRequired(output_doc, print_settings);
1244
1245   // Now flatten all the output pages.
1246   pp::Buffer_Dev buffer = GetFlattenedPrintData(output_doc);
1247   FPDF_CloseDocument(output_doc);
1248   return buffer;
1249 }
1250
1251 void PDFiumEngine::FitContentsToPrintableAreaIfRequired(
1252     const FPDF_DOCUMENT& doc, const PP_PrintSettings_Dev& print_settings) {
1253   // Check to see if we need to fit pdf contents to printer paper size.
1254   if (print_settings.print_scaling_option !=
1255           PP_PRINTSCALINGOPTION_SOURCE_SIZE) {
1256     int num_pages = FPDF_GetPageCount(doc);
1257     // In-place transformation is more efficient than creating a new
1258     // transformed document from the source document. Therefore, transform
1259     // every page to fit the contents in the selected printer paper.
1260     for (int i = 0; i < num_pages; ++i) {
1261       FPDF_PAGE page = FPDF_LoadPage(doc, i);
1262       TransformPDFPageForPrinting(page, print_settings);
1263       FPDF_ClosePage(page);
1264     }
1265   }
1266 }
1267
1268 void PDFiumEngine::SaveSelectedFormForPrint() {
1269   FORM_ForceToKillFocus(form_);
1270   client_->FormTextFieldFocusChange(false);
1271 }
1272
1273 void PDFiumEngine::PrintEnd() {
1274   FORM_DoDocumentAAction(form_, FPDFDOC_AACTION_DP);
1275 }
1276
1277 PDFiumPage::Area PDFiumEngine::GetCharIndex(
1278     const pp::MouseInputEvent& event, int* page_index,
1279     int* char_index, PDFiumPage::LinkTarget* target) {
1280   // First figure out which page this is in.
1281   pp::Point mouse_point = event.GetPosition();
1282   pp::Point point(
1283       static_cast<int>((mouse_point.x() + position_.x()) / current_zoom_),
1284       static_cast<int>((mouse_point.y() + position_.y()) / current_zoom_));
1285   return GetCharIndex(point, page_index, char_index, target);
1286 }
1287
1288 PDFiumPage::Area PDFiumEngine::GetCharIndex(
1289     const pp::Point& point,
1290     int* page_index,
1291     int* char_index,
1292     PDFiumPage::LinkTarget* target) {
1293   int page = -1;
1294   for (size_t i = 0; i < visible_pages_.size(); ++i) {
1295     if (pages_[visible_pages_[i]]->rect().Contains(point)) {
1296       page = visible_pages_[i];
1297       break;
1298     }
1299   }
1300   if (page == -1)
1301     return PDFiumPage::NONSELECTABLE_AREA;
1302
1303   // If the page hasn't finished rendering, calling into the page sometimes
1304   // leads to hangs.
1305   for (size_t i = 0; i < progressive_paints_.size(); ++i) {
1306     if (progressive_paints_[i].page_index == page)
1307       return PDFiumPage::NONSELECTABLE_AREA;
1308   }
1309
1310   *page_index = page;
1311   return pages_[page]->GetCharIndex(point, current_rotation_, char_index,
1312                                     target);
1313 }
1314
1315 bool PDFiumEngine::OnMouseDown(const pp::MouseInputEvent& event) {
1316   if (event.GetButton() != PP_INPUTEVENT_MOUSEBUTTON_LEFT)
1317     return false;
1318
1319   SelectionChangeInvalidator selection_invalidator(this);
1320   selection_.clear();
1321
1322   int page_index = -1;
1323   int char_index = -1;
1324   PDFiumPage::LinkTarget target;
1325   PDFiumPage::Area area = GetCharIndex(event, &page_index,
1326                                        &char_index, &target);
1327   mouse_down_state_.Set(area, target);
1328
1329   // Decide whether to open link or not based on user action in mouse up and
1330   // mouse move events.
1331   if (area == PDFiumPage::WEBLINK_AREA)
1332     return true;
1333
1334   if (area == PDFiumPage::DOCLINK_AREA) {
1335     client_->ScrollToPage(target.page);
1336     client_->FormTextFieldFocusChange(false);
1337     return true;
1338   }
1339
1340   if (page_index != -1) {
1341     last_page_mouse_down_ = page_index;
1342     double page_x, page_y;
1343     pp::Point point = event.GetPosition();
1344     DeviceToPage(page_index, point.x(), point.y(), &page_x, &page_y);
1345
1346     FORM_OnLButtonDown(form_, pages_[page_index]->GetPage(), 0, page_x, page_y);
1347     int control = FPDPage_HasFormFieldAtPoint(
1348         form_, pages_[page_index]->GetPage(), page_x, page_y);
1349     if (control > FPDF_FORMFIELD_UNKNOWN) {  // returns -1 sometimes...
1350       client_->FormTextFieldFocusChange(control == FPDF_FORMFIELD_TEXTFIELD ||
1351           control == FPDF_FORMFIELD_COMBOBOX);
1352       return true;  // Return now before we get into the selection code.
1353     }
1354   }
1355
1356   client_->FormTextFieldFocusChange(false);
1357
1358   if (area != PDFiumPage::TEXT_AREA)
1359     return true;  // Return true so WebKit doesn't do its own highlighting.
1360
1361   if (event.GetClickCount() == 1) {
1362     OnSingleClick(page_index, char_index);
1363   } else if (event.GetClickCount() == 2 ||
1364              event.GetClickCount() == 3) {
1365     OnMultipleClick(event.GetClickCount(), page_index, char_index);
1366   }
1367
1368   return true;
1369 }
1370
1371 void PDFiumEngine::OnSingleClick(int page_index, int char_index) {
1372   selecting_ = true;
1373   selection_.push_back(PDFiumRange(pages_[page_index], char_index, 0));
1374 }
1375
1376 void PDFiumEngine::OnMultipleClick(int click_count,
1377                                    int page_index,
1378                                    int char_index) {
1379   // It would be more efficient if the SDK could support finding a space, but
1380   // now it doesn't.
1381   int start_index = char_index;
1382   do {
1383     base::char16 cur = pages_[page_index]->GetCharAtIndex(start_index);
1384     // For double click, we want to select one word so we look for whitespace
1385     // boundaries.  For triple click, we want the whole line.
1386     if (cur == '\n' || (click_count == 2 && (cur == ' ' || cur == '\t')))
1387       break;
1388   } while (--start_index >= 0);
1389   if (start_index)
1390     start_index++;
1391
1392   int end_index = char_index;
1393   int total = pages_[page_index]->GetCharCount();
1394   while (end_index++ <= total) {
1395     base::char16 cur = pages_[page_index]->GetCharAtIndex(end_index);
1396     if (cur == '\n' || (click_count == 2 && (cur == ' ' || cur == '\t')))
1397       break;
1398   }
1399
1400   selection_.push_back(PDFiumRange(
1401       pages_[page_index], start_index, end_index - start_index));
1402 }
1403
1404 bool PDFiumEngine::OnMouseUp(const pp::MouseInputEvent& event) {
1405   if (event.GetButton() != PP_INPUTEVENT_MOUSEBUTTON_LEFT)
1406     return false;
1407
1408   int page_index = -1;
1409   int char_index = -1;
1410   PDFiumPage::LinkTarget target;
1411   PDFiumPage::Area area =
1412       GetCharIndex(event, &page_index, &char_index, &target);
1413
1414   // Open link on mouse up for same link for which mouse down happened earlier.
1415   if (mouse_down_state_.Matches(area, target)) {
1416     if (area == PDFiumPage::WEBLINK_AREA) {
1417       bool open_in_new_tab = !!(event.GetModifiers() & kDefaultKeyModifier);
1418       client_->NavigateTo(target.url, open_in_new_tab);
1419       client_->FormTextFieldFocusChange(false);
1420       return true;
1421     }
1422   }
1423
1424   if (page_index != -1) {
1425     double page_x, page_y;
1426     pp::Point point = event.GetPosition();
1427     DeviceToPage(page_index, point.x(), point.y(), &page_x, &page_y);
1428     FORM_OnLButtonUp(
1429         form_, pages_[page_index]->GetPage(), 0, page_x, page_y);
1430   }
1431
1432   if (!selecting_)
1433     return false;
1434
1435   selecting_ = false;
1436   return true;
1437 }
1438
1439 bool PDFiumEngine::OnMouseMove(const pp::MouseInputEvent& event) {
1440   int page_index = -1;
1441   int char_index = -1;
1442   PDFiumPage::LinkTarget target;
1443   PDFiumPage::Area area =
1444       GetCharIndex(event, &page_index, &char_index, &target);
1445
1446   // Clear |mouse_down_state_| if mouse moves away from where the mouse down
1447   // happened.
1448   if (!mouse_down_state_.Matches(area, target))
1449     mouse_down_state_.Reset();
1450
1451   if (!selecting_) {
1452     PP_CursorType_Dev cursor;
1453     switch (area) {
1454       case PDFiumPage::TEXT_AREA:
1455         cursor = PP_CURSORTYPE_IBEAM;
1456         break;
1457       case PDFiumPage::WEBLINK_AREA:
1458       case PDFiumPage::DOCLINK_AREA:
1459         cursor = PP_CURSORTYPE_HAND;
1460         break;
1461       case PDFiumPage::NONSELECTABLE_AREA:
1462       default:
1463         cursor = PP_CURSORTYPE_POINTER;
1464         break;
1465     }
1466
1467     if (page_index != -1) {
1468       double page_x, page_y;
1469       pp::Point point = event.GetPosition();
1470       DeviceToPage(page_index, point.x(), point.y(), &page_x, &page_y);
1471
1472       FORM_OnMouseMove(form_, pages_[page_index]->GetPage(), 0, page_x, page_y);
1473       int control = FPDPage_HasFormFieldAtPoint(
1474           form_, pages_[page_index]->GetPage(), page_x, page_y);
1475       switch (control) {
1476         case FPDF_FORMFIELD_PUSHBUTTON:
1477         case FPDF_FORMFIELD_CHECKBOX:
1478         case FPDF_FORMFIELD_RADIOBUTTON:
1479         case FPDF_FORMFIELD_COMBOBOX:
1480         case FPDF_FORMFIELD_LISTBOX:
1481           cursor = PP_CURSORTYPE_HAND;
1482           break;
1483         case FPDF_FORMFIELD_TEXTFIELD:
1484           cursor = PP_CURSORTYPE_IBEAM;
1485           break;
1486         default:
1487           break;
1488       }
1489     }
1490
1491     client_->UpdateCursor(cursor);
1492     pp::Point point = event.GetPosition();
1493     std::string url = GetLinkAtPosition(event.GetPosition());
1494     if (url != link_under_cursor_) {
1495       link_under_cursor_ = url;
1496       pp::PDF::SetLinkUnderCursor(GetPluginInstance(), url.c_str());
1497     }
1498     // No need to swallow the event, since this might interfere with the
1499     // scrollbars if the user is dragging them.
1500     return false;
1501   }
1502
1503   // We're selecting but right now we're not over text, so don't change the
1504   // current selection.
1505   if (area != PDFiumPage::TEXT_AREA && area != PDFiumPage::WEBLINK_AREA &&
1506       area != PDFiumPage::DOCLINK_AREA) {
1507     return false;
1508   }
1509
1510   SelectionChangeInvalidator selection_invalidator(this);
1511
1512   // Check if the user has descreased their selection area and we need to remove
1513   // pages from selection_.
1514   for (size_t i = 0; i < selection_.size(); ++i) {
1515     if (selection_[i].page_index() == page_index) {
1516       // There should be no other pages after this.
1517       selection_.erase(selection_.begin() + i + 1, selection_.end());
1518       break;
1519     }
1520   }
1521
1522   if (selection_.size() == 0)
1523     return false;
1524
1525   int last = selection_.size() - 1;
1526   if (selection_[last].page_index() == page_index) {
1527     // Selecting within a page.
1528     int count;
1529     if (char_index >= selection_[last].char_index()) {
1530       // Selecting forward.
1531       count = char_index - selection_[last].char_index() + 1;
1532     } else {
1533       count = char_index - selection_[last].char_index() - 1;
1534     }
1535     selection_[last].SetCharCount(count);
1536   } else if (selection_[last].page_index() < page_index) {
1537     // Selecting into the next page.
1538
1539     // First make sure that there are no gaps in selection, i.e. if mousedown on
1540     // page one but we only get mousemove over page three, we want page two.
1541     for (int i = selection_[last].page_index() + 1; i < page_index; ++i) {
1542       selection_.push_back(PDFiumRange(pages_[i], 0,
1543                            pages_[i]->GetCharCount()));
1544     }
1545
1546     int count = pages_[selection_[last].page_index()]->GetCharCount();
1547     selection_[last].SetCharCount(count - selection_[last].char_index());
1548     selection_.push_back(PDFiumRange(pages_[page_index], 0, char_index));
1549   } else {
1550     // Selecting into the previous page.
1551     selection_[last].SetCharCount(-selection_[last].char_index());
1552
1553     // First make sure that there are no gaps in selection, i.e. if mousedown on
1554     // page three but we only get mousemove over page one, we want page two.
1555     for (int i = selection_[last].page_index() - 1; i > page_index; --i) {
1556       selection_.push_back(PDFiumRange(pages_[i], 0,
1557                            pages_[i]->GetCharCount()));
1558     }
1559
1560     int count = pages_[page_index]->GetCharCount();
1561     selection_.push_back(
1562         PDFiumRange(pages_[page_index], count, count - char_index));
1563   }
1564
1565   return true;
1566 }
1567
1568 bool PDFiumEngine::OnKeyDown(const pp::KeyboardInputEvent& event) {
1569   if (last_page_mouse_down_ == -1)
1570     return false;
1571
1572   bool rv = !!FORM_OnKeyDown(
1573       form_, pages_[last_page_mouse_down_]->GetPage(),
1574       event.GetKeyCode(), event.GetModifiers());
1575
1576   if (event.GetKeyCode() == ui::VKEY_BACK ||
1577       event.GetKeyCode() == ui::VKEY_ESCAPE) {
1578     // Chrome doesn't send char events for backspace or escape keys, see
1579     // PlatformKeyboardEventBuilder::isCharacterKey() and
1580     // http://chrome-corpsvn.mtv.corp.google.com/viewvc?view=rev&root=chrome&revision=31805
1581     // for more information.  So just fake one since PDFium uses it.
1582     std::string str;
1583     str.push_back(event.GetKeyCode());
1584     pp::KeyboardInputEvent synthesized(pp::KeyboardInputEvent(
1585         client_->GetPluginInstance(),
1586         PP_INPUTEVENT_TYPE_CHAR,
1587         event.GetTimeStamp(),
1588         event.GetModifiers(),
1589         event.GetKeyCode(),
1590         str));
1591     OnChar(synthesized);
1592   }
1593
1594   return rv;
1595 }
1596
1597 bool PDFiumEngine::OnKeyUp(const pp::KeyboardInputEvent& event) {
1598   if (last_page_mouse_down_ == -1)
1599     return false;
1600
1601   return !!FORM_OnKeyUp(
1602       form_, pages_[last_page_mouse_down_]->GetPage(),
1603       event.GetKeyCode(), event.GetModifiers());
1604 }
1605
1606 bool PDFiumEngine::OnChar(const pp::KeyboardInputEvent& event) {
1607   if (last_page_mouse_down_ == -1)
1608     return false;
1609
1610   base::string16 str = base::UTF8ToUTF16(event.GetCharacterText().AsString());
1611   return !!FORM_OnChar(
1612       form_, pages_[last_page_mouse_down_]->GetPage(),
1613       str[0],
1614       event.GetModifiers());
1615 }
1616
1617 void PDFiumEngine::StartFind(const char* text, bool case_sensitive) {
1618   // We can get a call to StartFind before we have any page information (i.e.
1619   // before the first call to LoadDocument has happened). Handle this case.
1620   if (pages_.empty())
1621     return;
1622
1623   bool first_search = false;
1624   int character_to_start_searching_from = 0;
1625   if (current_find_text_ != text) {  // First time we search for this text.
1626     first_search = true;
1627     std::vector<PDFiumRange> old_selection = selection_;
1628     StopFind();
1629     current_find_text_ = text;
1630
1631     if (old_selection.empty()) {
1632       // Start searching from the beginning of the document.
1633       next_page_to_search_ = 0;
1634       last_page_to_search_ = pages_.size() - 1;
1635       last_character_index_to_search_ = -1;
1636     } else {
1637       // There's a current selection, so start from it.
1638       next_page_to_search_ = old_selection[0].page_index();
1639       last_character_index_to_search_ = old_selection[0].char_index();
1640       character_to_start_searching_from = old_selection[0].char_index();
1641       last_page_to_search_ = next_page_to_search_;
1642     }
1643   }
1644
1645   int current_page = next_page_to_search_;
1646
1647   if (pages_[current_page]->available()) {
1648     base::string16 str = base::UTF8ToUTF16(text);
1649     // Don't use PDFium to search for now, since it doesn't support unicode text.
1650     // Leave the code for now to avoid bit-rot, in case it's fixed later.
1651     if (0) {
1652       SearchUsingPDFium(
1653           str, case_sensitive, first_search, character_to_start_searching_from,
1654           current_page);
1655     } else {
1656       SearchUsingICU(
1657           str, case_sensitive, first_search, character_to_start_searching_from,
1658           current_page);
1659     }
1660
1661     if (!IsPageVisible(current_page))
1662       pages_[current_page]->Unload();
1663   }
1664
1665   if (next_page_to_search_ != last_page_to_search_ ||
1666       (first_search && last_character_index_to_search_ != -1)) {
1667     ++next_page_to_search_;
1668   }
1669
1670   if (next_page_to_search_ == static_cast<int>(pages_.size()))
1671     next_page_to_search_ = 0;
1672   // If there's only one page in the document and we start searching midway,
1673   // then we'll want to search the page one more time.
1674   bool end_of_search =
1675       next_page_to_search_ == last_page_to_search_ &&
1676       // Only one page but didn't start midway.
1677       ((pages_.size() == 1 && last_character_index_to_search_ == -1) ||
1678        // Started midway, but only 1 page and we already looped around.
1679        (pages_.size() == 1 && !first_search) ||
1680        // Started midway, and we've just looped around.
1681        (pages_.size() > 1 && current_page == next_page_to_search_));
1682
1683   if (end_of_search) {
1684     // Send the final notification.
1685     client_->NotifyNumberOfFindResultsChanged(find_results_.size(), true);
1686   } else {
1687     pp::CompletionCallback callback =
1688         find_factory_.NewCallback(&PDFiumEngine::ContinueFind);
1689     pp::Module::Get()->core()->CallOnMainThread(
1690         0, callback, case_sensitive ? 1 : 0);
1691   }
1692 }
1693
1694 void PDFiumEngine::SearchUsingPDFium(const base::string16& term,
1695                                      bool case_sensitive,
1696                                      bool first_search,
1697                                      int character_to_start_searching_from,
1698                                      int current_page) {
1699   // Find all the matches in the current page.
1700   unsigned long flags = case_sensitive ? FPDF_MATCHCASE : 0;
1701   FPDF_SCHHANDLE find = FPDFText_FindStart(
1702       pages_[current_page]->GetTextPage(),
1703       reinterpret_cast<const unsigned short*>(term.c_str()),
1704       flags, character_to_start_searching_from);
1705
1706   // Note: since we search one page at a time, we don't find matches across
1707   // page boundaries.  We could do this manually ourself, but it seems low
1708   // priority since Reader itself doesn't do it.
1709   while (FPDFText_FindNext(find)) {
1710     PDFiumRange result(pages_[current_page],
1711                        FPDFText_GetSchResultIndex(find),
1712                        FPDFText_GetSchCount(find));
1713
1714     if (!first_search &&
1715         last_character_index_to_search_ != -1 &&
1716         result.page_index() == last_page_to_search_ &&
1717         result.char_index() >= last_character_index_to_search_) {
1718       break;
1719     }
1720
1721     AddFindResult(result);
1722   }
1723
1724   FPDFText_FindClose(find);
1725 }
1726
1727 void PDFiumEngine::SearchUsingICU(const base::string16& term,
1728                                   bool case_sensitive,
1729                                   bool first_search,
1730                                   int character_to_start_searching_from,
1731                                   int current_page) {
1732   base::string16 page_text;
1733   int text_length = pages_[current_page]->GetCharCount();
1734   if (character_to_start_searching_from) {
1735     text_length -= character_to_start_searching_from;
1736   } else if (!first_search &&
1737              last_character_index_to_search_ != -1 &&
1738              current_page == last_page_to_search_) {
1739     text_length = last_character_index_to_search_;
1740   }
1741   if (text_length <= 0)
1742     return;
1743   unsigned short* data =
1744       reinterpret_cast<unsigned short*>(WriteInto(&page_text, text_length + 1));
1745   FPDFText_GetText(pages_[current_page]->GetTextPage(),
1746                    character_to_start_searching_from,
1747                    text_length,
1748                    data);
1749   std::vector<PDFEngine::Client::SearchStringResult> results;
1750   client_->SearchString(
1751       page_text.c_str(), term.c_str(), case_sensitive, &results);
1752   for (size_t i = 0; i < results.size(); ++i) {
1753     // Need to map the indexes from the page text, which may have generated
1754     // characters like space etc, to character indices from the page.
1755     int temp_start = results[i].start_index + character_to_start_searching_from;
1756     int start = FPDFText_GetCharIndexFromTextIndex(
1757         pages_[current_page]->GetTextPage(), temp_start);
1758     int end = FPDFText_GetCharIndexFromTextIndex(
1759         pages_[current_page]->GetTextPage(),
1760         temp_start + results[i].length);
1761     AddFindResult(PDFiumRange(pages_[current_page], start, end - start));
1762   }
1763 }
1764
1765 void PDFiumEngine::AddFindResult(const PDFiumRange& result) {
1766   // Figure out where to insert the new location, since we could have
1767   // started searching midway and now we wrapped.
1768   size_t i;
1769   int page_index = result.page_index();
1770   int char_index = result.char_index();
1771   for (i = 0; i < find_results_.size(); ++i) {
1772     if (find_results_[i].page_index() > page_index ||
1773         (find_results_[i].page_index() == page_index &&
1774          find_results_[i].char_index() > char_index)) {
1775       break;
1776     }
1777   }
1778   find_results_.insert(find_results_.begin() + i, result);
1779   UpdateTickMarks();
1780
1781   if (current_find_index_ == -1) {
1782     // Select the first match.
1783     SelectFindResult(true);
1784   } else if (static_cast<int>(i) <= current_find_index_) {
1785     // Update the current match index
1786     current_find_index_++;
1787     client_->NotifySelectedFindResultChanged(current_find_index_);
1788   }
1789   client_->NotifyNumberOfFindResultsChanged(find_results_.size(), false);
1790 }
1791
1792 bool PDFiumEngine::SelectFindResult(bool forward) {
1793   if (find_results_.empty()) {
1794     NOTREACHED();
1795     return false;
1796   }
1797
1798   SelectionChangeInvalidator selection_invalidator(this);
1799
1800   // Move back/forward through the search locations we previously found.
1801   if (forward) {
1802     if (++current_find_index_ == static_cast<int>(find_results_.size()))
1803       current_find_index_ = 0;
1804   } else {
1805     if (--current_find_index_ < 0) {
1806       current_find_index_ = find_results_.size() - 1;
1807     }
1808   }
1809
1810   // Update the selection before telling the client to scroll, since it could
1811   // paint then.
1812   selection_.clear();
1813   selection_.push_back(find_results_[current_find_index_]);
1814
1815   // If the result is not in view, scroll to it.
1816   size_t i;
1817   pp::Rect bounding_rect;
1818   pp::Rect visible_rect = GetVisibleRect();
1819   // Use zoom of 1.0 since visible_rect is without zoom.
1820   std::vector<pp::Rect> rects = find_results_[current_find_index_].
1821       GetScreenRects(pp::Point(), 1.0, current_rotation_);
1822   for (i = 0; i < rects.size(); ++i)
1823     bounding_rect = bounding_rect.Union(rects[i]);
1824   if (!visible_rect.Contains(bounding_rect)) {
1825     pp::Point center = bounding_rect.CenterPoint();
1826     // Make the page centered.
1827     int new_y = static_cast<int>(center.y() * current_zoom_) -
1828                 static_cast<int>(visible_rect.height() * current_zoom_ / 2);
1829     if (new_y < 0)
1830       new_y = 0;
1831     client_->ScrollToY(new_y);
1832
1833     // Only move horizontally if it's not visible.
1834     if (center.x() < visible_rect.x() || center.x() > visible_rect.right()) {
1835       int new_x = static_cast<int>(center.x()  * current_zoom_) -
1836                   static_cast<int>(visible_rect.width() * current_zoom_ / 2);
1837       if (new_x < 0)
1838         new_x = 0;
1839       client_->ScrollToX(new_x);
1840     }
1841   }
1842
1843   client_->NotifySelectedFindResultChanged(current_find_index_);
1844
1845   return true;
1846 }
1847
1848 void PDFiumEngine::StopFind() {
1849   SelectionChangeInvalidator selection_invalidator(this);
1850
1851   selection_.clear();
1852   selecting_ = false;
1853   find_results_.clear();
1854   next_page_to_search_ = -1;
1855   last_page_to_search_ = -1;
1856   last_character_index_to_search_ = -1;
1857   current_find_index_ = -1;
1858   current_find_text_.clear();
1859   UpdateTickMarks();
1860   find_factory_.CancelAll();
1861 }
1862
1863 void PDFiumEngine::UpdateTickMarks() {
1864   std::vector<pp::Rect> tickmarks;
1865   for (size_t i = 0; i < find_results_.size(); ++i) {
1866     pp::Rect rect;
1867     // Always use an origin of 0,0 since scroll positions don't affect tickmark.
1868     std::vector<pp::Rect> rects = find_results_[i].GetScreenRects(
1869       pp::Point(0, 0), current_zoom_, current_rotation_);
1870     for (size_t j = 0; j < rects.size(); ++j)
1871       rect = rect.Union(rects[j]);
1872     tickmarks.push_back(rect);
1873   }
1874
1875   client_->UpdateTickMarks(tickmarks);
1876 }
1877
1878 void PDFiumEngine::ZoomUpdated(double new_zoom_level) {
1879   CancelPaints();
1880
1881   current_zoom_ = new_zoom_level;
1882
1883   CalculateVisiblePages();
1884   UpdateTickMarks();
1885 }
1886
1887 void PDFiumEngine::RotateClockwise() {
1888   current_rotation_ = (current_rotation_ + 1) % 4;
1889   InvalidateAllPages();
1890 }
1891
1892 void PDFiumEngine::RotateCounterclockwise() {
1893   current_rotation_ = (current_rotation_ - 1) % 4;
1894   InvalidateAllPages();
1895 }
1896
1897 void PDFiumEngine::InvalidateAllPages() {
1898   selection_.clear();
1899   find_results_.clear();
1900
1901   CancelPaints();
1902   LoadPageInfo(true);
1903   UpdateTickMarks();
1904   client_->Invalidate(pp::Rect(plugin_size_));
1905 }
1906
1907 std::string PDFiumEngine::GetSelectedText() {
1908   base::string16 result;
1909   for (size_t i = 0; i < selection_.size(); ++i) {
1910     if (i > 0 &&
1911         selection_[i - 1].page_index() > selection_[i].page_index()) {
1912       result = selection_[i].GetText() + result;
1913     } else {
1914       result.append(selection_[i].GetText());
1915     }
1916   }
1917
1918   FormatStringWithHyphens(&result);
1919   FormatStringForOS(&result);
1920   return base::UTF16ToUTF8(result);
1921 }
1922
1923 std::string PDFiumEngine::GetLinkAtPosition(const pp::Point& point) {
1924   int temp;
1925   PDFiumPage::LinkTarget target;
1926   pp::Point point_in_page(
1927       static_cast<int>((point.x() + position_.x()) / current_zoom_),
1928       static_cast<int>((point.y() + position_.y()) / current_zoom_));
1929   PDFiumPage::Area area = GetCharIndex(point_in_page, &temp, &temp, &target);
1930   if (area == PDFiumPage::WEBLINK_AREA)
1931     return target.url;
1932   return std::string();
1933 }
1934
1935 bool PDFiumEngine::IsSelecting() {
1936   return selecting_;
1937 }
1938
1939 bool PDFiumEngine::HasPermission(DocumentPermission permission) const {
1940   switch (permission) {
1941     case PERMISSION_COPY:
1942       return (permissions_ & kPDFPermissionCopyMask) != 0;
1943     case PERMISSION_COPY_ACCESSIBLE:
1944       return (permissions_ & kPDFPermissionCopyAccessibleMask) != 0;
1945     case PERMISSION_PRINT_LOW_QUALITY:
1946       return (permissions_ & kPDFPermissionPrintLowQualityMask) != 0;
1947     case PERMISSION_PRINT_HIGH_QUALITY:
1948       return (permissions_ & kPDFPermissionPrintLowQualityMask) != 0 &&
1949              (permissions_ & kPDFPermissionPrintHighQualityMask) != 0;
1950     default:
1951       return true;
1952   };
1953 }
1954
1955 void PDFiumEngine::SelectAll() {
1956   SelectionChangeInvalidator selection_invalidator(this);
1957
1958   selection_.clear();
1959   for (size_t i = 0; i < pages_.size(); ++i)
1960     if (pages_[i]->available()) {
1961       selection_.push_back(PDFiumRange(pages_[i], 0,
1962                            pages_[i]->GetCharCount()));
1963     }
1964 }
1965
1966 int PDFiumEngine::GetNumberOfPages() {
1967   return pages_.size();
1968 }
1969
1970 int PDFiumEngine::GetNamedDestinationPage(const std::string& destination) {
1971   // Look for the destination.
1972   FPDF_DEST dest = FPDF_GetNamedDestByName(doc_, destination.c_str());
1973   if (!dest) {
1974     // Look for a bookmark with the same name.
1975     base::string16 destination_wide = base::UTF8ToUTF16(destination);
1976     FPDF_WIDESTRING destination_pdf_wide =
1977         reinterpret_cast<FPDF_WIDESTRING>(destination_wide.c_str());
1978     FPDF_BOOKMARK bookmark = FPDFBookmark_Find(doc_, destination_pdf_wide);
1979     if (!bookmark)
1980       return -1;
1981     dest = FPDFBookmark_GetDest(doc_, bookmark);
1982   }
1983   return dest ? FPDFDest_GetPageIndex(doc_, dest) : -1;
1984 }
1985
1986 int PDFiumEngine::GetFirstVisiblePage() {
1987   CalculateVisiblePages();
1988   return first_visible_page_;
1989 }
1990
1991 int PDFiumEngine::GetMostVisiblePage() {
1992   CalculateVisiblePages();
1993   return most_visible_page_;
1994 }
1995
1996 pp::Rect PDFiumEngine::GetPageRect(int index) {
1997   pp::Rect rc(pages_[index]->rect());
1998   rc.Inset(-kPageShadowLeft, -kPageShadowTop,
1999            -kPageShadowRight, -kPageShadowBottom);
2000   return rc;
2001 }
2002
2003 pp::Rect PDFiumEngine::GetPageContentsRect(int index) {
2004   return GetScreenRect(pages_[index]->rect());
2005 }
2006
2007 void PDFiumEngine::PaintThumbnail(pp::ImageData* image_data, int index) {
2008   FPDF_BITMAP bitmap = FPDFBitmap_CreateEx(
2009       image_data->size().width(), image_data->size().height(),
2010       FPDFBitmap_BGRx, image_data->data(), image_data->stride());
2011
2012   if (pages_[index]->available()) {
2013     FPDFBitmap_FillRect(bitmap, 0, 0, image_data->size().width(),
2014                         image_data->size().height(), 0xFFFFFFFF);
2015
2016     FPDF_RenderPageBitmap(
2017         bitmap, pages_[index]->GetPage(), 0, 0, image_data->size().width(),
2018         image_data->size().height(), 0, GetRenderingFlags());
2019   } else {
2020     FPDFBitmap_FillRect(bitmap, 0, 0, image_data->size().width(),
2021                         image_data->size().height(), kPendingPageColor);
2022   }
2023
2024   FPDFBitmap_Destroy(bitmap);
2025 }
2026
2027 void PDFiumEngine::SetGrayscale(bool grayscale) {
2028   render_grayscale_ = grayscale;
2029 }
2030
2031 void PDFiumEngine::OnCallback(int id) {
2032   if (!timers_.count(id))
2033     return;
2034
2035   timers_[id].second(id);
2036   if (timers_.count(id))  // The callback might delete the timer.
2037     client_->ScheduleCallback(id, timers_[id].first);
2038 }
2039
2040 std::string PDFiumEngine::GetPageAsJSON(int index) {
2041   if (!(HasPermission(PERMISSION_COPY) ||
2042         HasPermission(PERMISSION_COPY_ACCESSIBLE))) {
2043     return "{}";
2044   }
2045
2046   if (index < 0 || static_cast<size_t>(index) > pages_.size() - 1)
2047     return "{}";
2048
2049   // TODO(thestig) Remove after debugging http://crbug.com/402035
2050   CHECK(pages_[index]);
2051   scoped_ptr<base::Value> node(
2052       pages_[index]->GetAccessibleContentAsValue(current_rotation_));
2053   std::string page_json;
2054   base::JSONWriter::Write(node.get(), &page_json);
2055   return page_json;
2056 }
2057
2058 bool PDFiumEngine::GetPrintScaling() {
2059   return !!FPDF_VIEWERREF_GetPrintScaling(doc_);
2060 }
2061
2062 void PDFiumEngine::AppendBlankPages(int num_pages) {
2063   DCHECK(num_pages != 0);
2064
2065   if (!doc_)
2066     return;
2067
2068   selection_.clear();
2069   pending_pages_.clear();
2070
2071   // Delete all pages except the first one.
2072   while (pages_.size() > 1) {
2073     delete pages_.back();
2074     pages_.pop_back();
2075     FPDFPage_Delete(doc_, pages_.size());
2076   }
2077
2078   // Calculate document size and all page sizes.
2079   std::vector<pp::Rect> page_rects;
2080   pp::Size page_size = GetPageSize(0);
2081   page_size.Enlarge(kPageShadowLeft + kPageShadowRight,
2082                     kPageShadowTop + kPageShadowBottom);
2083   pp::Size old_document_size = document_size_;
2084   document_size_ = pp::Size(page_size.width(), 0);
2085   for (int i = 0; i < num_pages; ++i) {
2086     if (i != 0) {
2087       // Add space for horizontal separator.
2088       document_size_.Enlarge(0, kPageSeparatorThickness);
2089     }
2090
2091     pp::Rect rect(pp::Point(0, document_size_.height()), page_size);
2092     page_rects.push_back(rect);
2093
2094     document_size_.Enlarge(0, page_size.height());
2095   }
2096
2097   // Create blank pages.
2098   for (int i = 1; i < num_pages; ++i) {
2099     pp::Rect page_rect(page_rects[i]);
2100     page_rect.Inset(kPageShadowLeft, kPageShadowTop,
2101                     kPageShadowRight, kPageShadowBottom);
2102     double width_in_points =
2103         page_rect.width() * kPointsPerInch / kPixelsPerInch;
2104     double height_in_points =
2105         page_rect.height() * kPointsPerInch / kPixelsPerInch;
2106     FPDFPage_New(doc_, i, width_in_points, height_in_points);
2107     pages_.push_back(new PDFiumPage(this, i, page_rect, true));
2108     // TODO(thestig) Remove after debugging http://crbug.com/402035
2109     CHECK(pages_.back());
2110   }
2111
2112   CalculateVisiblePages();
2113   if (document_size_ != old_document_size)
2114     client_->DocumentSizeUpdated(document_size_);
2115 }
2116
2117 void PDFiumEngine::LoadDocument() {
2118   // Check if the document is ready for loading. If it isn't just bail for now,
2119   // we will call LoadDocument() again later.
2120   if (!doc_ && !doc_loader_.IsDocumentComplete() &&
2121       !FPDFAvail_IsDocAvail(fpdf_availability_, &download_hints_)) {
2122     return;
2123   }
2124
2125   // If we're in the middle of getting a password, just return. We will retry
2126   // loading the document after we get the password anyway.
2127   if (getting_password_)
2128     return;
2129
2130   ScopedUnsupportedFeature scoped_unsupported_feature(this);
2131   bool needs_password = false;
2132   if (TryLoadingDoc(false, std::string(), &needs_password)) {
2133     ContinueLoadingDocument(false, std::string());
2134     return;
2135   }
2136   if (needs_password)
2137     GetPasswordAndLoad();
2138   else
2139     client_->DocumentLoadFailed();
2140 }
2141
2142 bool PDFiumEngine::TryLoadingDoc(bool with_password,
2143                                  const std::string& password,
2144                                  bool* needs_password) {
2145   *needs_password = false;
2146   if (doc_)
2147     return true;
2148
2149   const char* password_cstr = NULL;
2150   if (with_password) {
2151     password_cstr = password.c_str();
2152     password_tries_remaining_--;
2153   }
2154   if (doc_loader_.IsDocumentComplete())
2155     doc_ = FPDF_LoadCustomDocument(&file_access_, password_cstr);
2156   else
2157     doc_ = FPDFAvail_GetDocument(fpdf_availability_, password_cstr);
2158
2159   if (!doc_ && FPDF_GetLastError() == FPDF_ERR_PASSWORD)
2160     *needs_password = true;
2161
2162   return doc_ != NULL;
2163 }
2164
2165 void PDFiumEngine::GetPasswordAndLoad() {
2166   getting_password_ = true;
2167   DCHECK(!doc_ && FPDF_GetLastError() == FPDF_ERR_PASSWORD);
2168   client_->GetDocumentPassword(password_factory_.NewCallbackWithOutput(
2169       &PDFiumEngine::OnGetPasswordComplete));
2170 }
2171
2172 void PDFiumEngine::OnGetPasswordComplete(int32_t result,
2173                                          const pp::Var& password) {
2174   getting_password_ = false;
2175
2176   bool password_given = false;
2177   std::string password_text;
2178   if (result == PP_OK && password.is_string()) {
2179     password_text = password.AsString();
2180     if (!password_text.empty())
2181       password_given = true;
2182   }
2183   ContinueLoadingDocument(password_given, password_text);
2184 }
2185
2186 void PDFiumEngine::ContinueLoadingDocument(
2187     bool has_password,
2188     const std::string& password) {
2189   ScopedUnsupportedFeature scoped_unsupported_feature(this);
2190
2191   bool needs_password = false;
2192   bool loaded = TryLoadingDoc(has_password, password, &needs_password);
2193   bool password_incorrect = !loaded && has_password && needs_password;
2194   if (password_incorrect && password_tries_remaining_ > 0) {
2195     GetPasswordAndLoad();
2196     return;
2197   }
2198
2199   if (!doc_) {
2200     client_->DocumentLoadFailed();
2201     return;
2202   }
2203
2204   if (FPDFDoc_GetPageMode(doc_) == PAGEMODE_USEOUTLINES)
2205     client_->DocumentHasUnsupportedFeature("Bookmarks");
2206
2207   permissions_ = FPDF_GetDocPermissions(doc_);
2208
2209   if (!form_) {
2210     // Only returns 0 when data isn't available.  If form data is downloaded, or
2211     // if this isn't a form, returns positive values.
2212     if (!doc_loader_.IsDocumentComplete() &&
2213         !FPDFAvail_IsFormAvail(fpdf_availability_, &download_hints_)) {
2214       return;
2215     }
2216
2217     form_ = FPDFDOC_InitFormFillEnviroument(
2218         doc_, static_cast<FPDF_FORMFILLINFO*>(this));
2219     FPDF_SetFormFieldHighlightColor(form_, 0, kFormHighlightColor);
2220     FPDF_SetFormFieldHighlightAlpha(form_, kFormHighlightAlpha);
2221   }
2222
2223   if (!doc_loader_.IsDocumentComplete()) {
2224     // Check if the first page is available.  In a linearized PDF, that is not
2225     // always page 0.  Doing this gives us the default page size, since when the
2226     // document is available, the first page is available as well.
2227     CheckPageAvailable(FPDFAvail_GetFirstPageNum(doc_), &pending_pages_);
2228   }
2229
2230   LoadPageInfo(false);
2231
2232   if (doc_loader_.IsDocumentComplete())
2233     FinishLoadingDocument();
2234 }
2235
2236 void PDFiumEngine::LoadPageInfo(bool reload) {
2237   pending_pages_.clear();
2238   pp::Size old_document_size = document_size_;
2239   document_size_ = pp::Size();
2240   std::vector<pp::Rect> page_rects;
2241   int page_count = FPDF_GetPageCount(doc_);
2242   bool doc_complete = doc_loader_.IsDocumentComplete();
2243   for (int i = 0; i < page_count; ++i) {
2244     if (i != 0) {
2245       // Add space for horizontal separator.
2246       document_size_.Enlarge(0, kPageSeparatorThickness);
2247     }
2248
2249     // Get page availability. If reload==false, and document is not loaded yet
2250     // (we are using async loading) - mark all pages as unavailable.
2251     // If reload==true (we have document constructed already), get page
2252     // availability flag from already existing PDFiumPage class.
2253     bool page_available = reload ? pages_[i]->available() : doc_complete;
2254
2255     pp::Size size = page_available ? GetPageSize(i) : default_page_size_;
2256     size.Enlarge(kPageShadowLeft + kPageShadowRight,
2257                  kPageShadowTop + kPageShadowBottom);
2258     pp::Rect rect(pp::Point(0, document_size_.height()), size);
2259     page_rects.push_back(rect);
2260
2261     if (size.width() > document_size_.width())
2262       document_size_.set_width(size.width());
2263
2264     document_size_.Enlarge(0, size.height());
2265   }
2266
2267   for (int i = 0; i < page_count; ++i) {
2268     // Center pages relative to the entire document.
2269     page_rects[i].set_x((document_size_.width() - page_rects[i].width()) / 2);
2270     pp::Rect page_rect(page_rects[i]);
2271     page_rect.Inset(kPageShadowLeft, kPageShadowTop,
2272                     kPageShadowRight, kPageShadowBottom);
2273     if (reload) {
2274       pages_[i]->set_rect(page_rect);
2275     } else {
2276       pages_.push_back(new PDFiumPage(this, i, page_rect, doc_complete));
2277       // TODO(thestig) Remove after debugging http://crbug.com/402035
2278       CHECK(pages_.back());
2279     }
2280   }
2281
2282   CalculateVisiblePages();
2283   if (document_size_ != old_document_size)
2284     client_->DocumentSizeUpdated(document_size_);
2285 }
2286
2287 void PDFiumEngine::CalculateVisiblePages() {
2288   // Clear pending requests queue, since it may contain requests to the pages
2289   // that are already invisible (after scrolling for example).
2290   pending_pages_.clear();
2291   doc_loader_.ClearPendingRequests();
2292
2293   visible_pages_.clear();
2294   pp::Rect visible_rect(plugin_size_);
2295   for (size_t i = 0; i < pages_.size(); ++i) {
2296     // Check an entire PageScreenRect, since we might need to repaint side
2297     // borders and shadows even if the page itself is not visible.
2298     // For example, when user use pdf with different page sizes and zoomed in
2299     // outside page area.
2300     if (visible_rect.Intersects(GetPageScreenRect(i))) {
2301       visible_pages_.push_back(i);
2302       CheckPageAvailable(i, &pending_pages_);
2303     } else {
2304       // Need to unload pages when we're not using them, since some PDFs use a
2305       // lot of memory.  See http://crbug.com/48791
2306       if (defer_page_unload_) {
2307         deferred_page_unloads_.push_back(i);
2308       } else {
2309         pages_[i]->Unload();
2310       }
2311
2312       // If the last mouse down was on a page that's no longer visible, reset
2313       // that variable so that we don't send keyboard events to it (the focus
2314       // will be lost when the page is first closed anyways).
2315       if (static_cast<int>(i) == last_page_mouse_down_)
2316         last_page_mouse_down_ = -1;
2317     }
2318   }
2319
2320   // Any pending highlighting of form fields will be invalid since these are in
2321   // screen coordinates.
2322   form_highlights_.clear();
2323
2324   if (visible_pages_.size() == 0)
2325     first_visible_page_ = -1;
2326   else
2327     first_visible_page_ = visible_pages_.front();
2328
2329   int most_visible_page = first_visible_page_;
2330   // Check if the next page is more visible than the first one.
2331   if (most_visible_page != -1 &&
2332       pages_.size() > 0 &&
2333       most_visible_page < static_cast<int>(pages_.size()) - 1) {
2334     pp::Rect rc_first =
2335         visible_rect.Intersect(GetPageScreenRect(most_visible_page));
2336     pp::Rect rc_next =
2337         visible_rect.Intersect(GetPageScreenRect(most_visible_page + 1));
2338     if (rc_next.height() > rc_first.height())
2339       most_visible_page++;
2340   }
2341
2342   SetCurrentPage(most_visible_page);
2343 }
2344
2345 bool PDFiumEngine::IsPageVisible(int index) const {
2346   for (size_t i = 0; i < visible_pages_.size(); ++i) {
2347     if (visible_pages_[i] == index)
2348       return true;
2349   }
2350
2351   return false;
2352 }
2353
2354 bool PDFiumEngine::CheckPageAvailable(int index, std::vector<int>* pending) {
2355   if (!doc_ || !form_)
2356     return false;
2357
2358   if (static_cast<int>(pages_.size()) > index && pages_[index]->available())
2359     return true;
2360
2361   if (!FPDFAvail_IsPageAvail(fpdf_availability_, index, &download_hints_)) {
2362     size_t j;
2363     for (j = 0; j < pending->size(); ++j) {
2364       if ((*pending)[j] == index)
2365         break;
2366     }
2367
2368     if (j == pending->size())
2369       pending->push_back(index);
2370     return false;
2371   }
2372
2373   if (static_cast<int>(pages_.size()) > index)
2374     pages_[index]->set_available(true);
2375   if (!default_page_size_.GetArea())
2376     default_page_size_ = GetPageSize(index);
2377   return true;
2378 }
2379
2380 pp::Size PDFiumEngine::GetPageSize(int index) {
2381   pp::Size size;
2382   double width_in_points = 0;
2383   double height_in_points = 0;
2384   int rv = FPDF_GetPageSizeByIndex(
2385       doc_, index, &width_in_points, &height_in_points);
2386
2387   if (rv) {
2388     int width_in_pixels = static_cast<int>(
2389         width_in_points * kPixelsPerInch / kPointsPerInch);
2390     int height_in_pixels = static_cast<int>(
2391         height_in_points * kPixelsPerInch / kPointsPerInch);
2392     if (current_rotation_ % 2 == 1)
2393       std::swap(width_in_pixels, height_in_pixels);
2394     size = pp::Size(width_in_pixels, height_in_pixels);
2395   }
2396   return size;
2397 }
2398
2399 int PDFiumEngine::StartPaint(int page_index, const pp::Rect& dirty) {
2400   // For the first time we hit paint, do nothing and just record the paint for
2401   // the next callback.  This keeps the UI responsive in case the user is doing
2402   // a lot of scrolling.
2403   ProgressivePaint progressive;
2404   progressive.rect = dirty;
2405   progressive.page_index = page_index;
2406   progressive.bitmap = NULL;
2407   progressive.painted_ = false;
2408   progressive_paints_.push_back(progressive);
2409   return progressive_paints_.size() - 1;
2410 }
2411
2412 bool PDFiumEngine::ContinuePaint(int progressive_index,
2413                                  pp::ImageData* image_data) {
2414 #if defined(OS_LINUX)
2415   g_last_instance_id = client_->GetPluginInstance()->pp_instance();
2416 #endif
2417
2418   int rv;
2419   int page_index = progressive_paints_[progressive_index].page_index;
2420   last_progressive_start_time_ = base::Time::Now();
2421   if (progressive_paints_[progressive_index].bitmap) {
2422     rv = FPDF_RenderPage_Continue(
2423         pages_[page_index]->GetPage(), static_cast<IFSDK_PAUSE*>(this));
2424   } else {
2425     pp::Rect dirty = progressive_paints_[progressive_index].rect;
2426     progressive_paints_[progressive_index].bitmap = CreateBitmap(dirty,
2427                                                                  image_data);
2428     int start_x, start_y, size_x, size_y;
2429     GetPDFiumRect(
2430         page_index, dirty, &start_x, &start_y, &size_x, &size_y);
2431     FPDFBitmap_FillRect(progressive_paints_[progressive_index].bitmap, start_x,
2432                         start_y, size_x, size_y, 0xFFFFFFFF);
2433     rv = FPDF_RenderPageBitmap_Start(
2434         progressive_paints_[progressive_index].bitmap,
2435         pages_[page_index]->GetPage(), start_x, start_y, size_x, size_y,
2436         current_rotation_,
2437         GetRenderingFlags(), static_cast<IFSDK_PAUSE*>(this));
2438   }
2439   return rv != FPDF_RENDER_TOBECOUNTINUED;
2440 }
2441
2442 void PDFiumEngine::FinishPaint(int progressive_index,
2443                                pp::ImageData* image_data) {
2444   int page_index = progressive_paints_[progressive_index].page_index;
2445   pp::Rect dirty_in_screen = progressive_paints_[progressive_index].rect;
2446   FPDF_BITMAP bitmap = progressive_paints_[progressive_index].bitmap;
2447   int start_x, start_y, size_x, size_y;
2448   GetPDFiumRect(
2449       page_index, dirty_in_screen, &start_x, &start_y, &size_x, &size_y);
2450
2451   // Draw the forms.
2452   FPDF_FFLDraw(
2453       form_, bitmap, pages_[page_index]->GetPage(), start_x, start_y, size_x,
2454       size_y, current_rotation_, GetRenderingFlags());
2455
2456   FillPageSides(progressive_index);
2457
2458   // Paint the page shadows.
2459   PaintPageShadow(progressive_index, image_data);
2460
2461   DrawSelections(progressive_index, image_data);
2462
2463   FPDF_RenderPage_Close(pages_[page_index]->GetPage());
2464   FPDFBitmap_Destroy(bitmap);
2465   progressive_paints_.erase(progressive_paints_.begin() + progressive_index);
2466
2467   client_->DocumentPaintOccurred();
2468 }
2469
2470 void PDFiumEngine::CancelPaints() {
2471   for (size_t i = 0; i < progressive_paints_.size(); ++i) {
2472     FPDF_RenderPage_Close(pages_[progressive_paints_[i].page_index]->GetPage());
2473     FPDFBitmap_Destroy(progressive_paints_[i].bitmap);
2474   }
2475   progressive_paints_.clear();
2476 }
2477
2478 void PDFiumEngine::FillPageSides(int progressive_index) {
2479   int page_index = progressive_paints_[progressive_index].page_index;
2480   pp::Rect dirty_in_screen = progressive_paints_[progressive_index].rect;
2481   FPDF_BITMAP bitmap = progressive_paints_[progressive_index].bitmap;
2482
2483   pp::Rect page_rect = pages_[page_index]->rect();
2484   if (page_rect.x() > 0) {
2485     pp::Rect left(0,
2486                   page_rect.y() - kPageShadowTop,
2487                   page_rect.x() - kPageShadowLeft,
2488                   page_rect.height() + kPageShadowTop +
2489                       kPageShadowBottom + kPageSeparatorThickness);
2490     left = GetScreenRect(left).Intersect(dirty_in_screen);
2491
2492     FPDFBitmap_FillRect(bitmap, left.x() - dirty_in_screen.x(),
2493                         left.y() - dirty_in_screen.y(), left.width(),
2494                         left.height(), kBackgroundColor);
2495   }
2496
2497   if (page_rect.right() < document_size_.width()) {
2498     pp::Rect right(page_rect.right() + kPageShadowRight,
2499                    page_rect.y() - kPageShadowTop,
2500                    document_size_.width() - page_rect.right() -
2501                       kPageShadowRight,
2502                    page_rect.height() + kPageShadowTop +
2503                        kPageShadowBottom + kPageSeparatorThickness);
2504     right = GetScreenRect(right).Intersect(dirty_in_screen);
2505
2506     FPDFBitmap_FillRect(bitmap, right.x() - dirty_in_screen.x(),
2507                         right.y() - dirty_in_screen.y(), right.width(),
2508                         right.height(), kBackgroundColor);
2509   }
2510
2511   // Paint separator.
2512   pp::Rect bottom(page_rect.x() - kPageShadowLeft,
2513                   page_rect.bottom() + kPageShadowBottom,
2514                   page_rect.width() + kPageShadowLeft + kPageShadowRight,
2515                   kPageSeparatorThickness);
2516   bottom = GetScreenRect(bottom).Intersect(dirty_in_screen);
2517
2518   FPDFBitmap_FillRect(bitmap, bottom.x() - dirty_in_screen.x(),
2519                       bottom.y() - dirty_in_screen.y(), bottom.width(),
2520                       bottom.height(), kBackgroundColor);
2521 }
2522
2523 void PDFiumEngine::PaintPageShadow(int progressive_index,
2524                                    pp::ImageData* image_data) {
2525   int page_index = progressive_paints_[progressive_index].page_index;
2526   pp::Rect dirty_in_screen = progressive_paints_[progressive_index].rect;
2527   pp::Rect page_rect = pages_[page_index]->rect();
2528   pp::Rect shadow_rect(page_rect);
2529   shadow_rect.Inset(-kPageShadowLeft, -kPageShadowTop,
2530                     -kPageShadowRight, -kPageShadowBottom);
2531
2532   // Due to the rounding errors of the GetScreenRect it is possible to get
2533   // different size shadows on the left and right sides even they are defined
2534   // the same. To fix this issue let's calculate shadow rect and then shrink
2535   // it by the size of the shadows.
2536   shadow_rect = GetScreenRect(shadow_rect);
2537   page_rect = shadow_rect;
2538
2539   page_rect.Inset(static_cast<int>(ceil(kPageShadowLeft * current_zoom_)),
2540                   static_cast<int>(ceil(kPageShadowTop * current_zoom_)),
2541                   static_cast<int>(ceil(kPageShadowRight * current_zoom_)),
2542                   static_cast<int>(ceil(kPageShadowBottom * current_zoom_)));
2543
2544   DrawPageShadow(page_rect, shadow_rect, dirty_in_screen, image_data);
2545 }
2546
2547 void PDFiumEngine::DrawSelections(int progressive_index,
2548                                   pp::ImageData* image_data) {
2549   int page_index = progressive_paints_[progressive_index].page_index;
2550   pp::Rect dirty_in_screen = progressive_paints_[progressive_index].rect;
2551
2552   void* region = NULL;
2553   int stride;
2554   GetRegion(dirty_in_screen.point(), image_data, &region, &stride);
2555
2556   std::vector<pp::Rect> highlighted_rects;
2557   pp::Rect visible_rect = GetVisibleRect();
2558   for (size_t k = 0; k < selection_.size(); ++k) {
2559     if (selection_[k].page_index() != page_index)
2560       continue;
2561     std::vector<pp::Rect> rects = selection_[k].GetScreenRects(
2562         visible_rect.point(), current_zoom_, current_rotation_);
2563     for (size_t j = 0; j < rects.size(); ++j) {
2564       pp::Rect visible_selection = rects[j].Intersect(dirty_in_screen);
2565       if (visible_selection.IsEmpty())
2566         continue;
2567
2568       visible_selection.Offset(
2569           -dirty_in_screen.point().x(), -dirty_in_screen.point().y());
2570       Highlight(region, stride, visible_selection, &highlighted_rects);
2571     }
2572   }
2573
2574   for (size_t k = 0; k < form_highlights_.size(); ++k) {
2575     pp::Rect visible_selection = form_highlights_[k].Intersect(dirty_in_screen);
2576       if (visible_selection.IsEmpty())
2577         continue;
2578
2579     visible_selection.Offset(
2580         -dirty_in_screen.point().x(), -dirty_in_screen.point().y());
2581     Highlight(region, stride, visible_selection, &highlighted_rects);
2582   }
2583   form_highlights_.clear();
2584 }
2585
2586 void PDFiumEngine::PaintUnavailablePage(int page_index,
2587                                         const pp::Rect& dirty,
2588                                         pp::ImageData* image_data) {
2589   int start_x, start_y, size_x, size_y;
2590   GetPDFiumRect(page_index, dirty, &start_x, &start_y, &size_x, &size_y);
2591   FPDF_BITMAP bitmap = CreateBitmap(dirty, image_data);
2592   FPDFBitmap_FillRect(bitmap, start_x, start_y, size_x, size_y,
2593                       kPendingPageColor);
2594
2595   pp::Rect loading_text_in_screen(
2596       pages_[page_index]->rect().width() / 2,
2597       pages_[page_index]->rect().y() + kLoadingTextVerticalOffset, 0, 0);
2598   loading_text_in_screen = GetScreenRect(loading_text_in_screen);
2599   FPDFBitmap_Destroy(bitmap);
2600 }
2601
2602 int PDFiumEngine::GetProgressiveIndex(int page_index) const {
2603   for (size_t i = 0; i < progressive_paints_.size(); ++i) {
2604     if (progressive_paints_[i].page_index == page_index)
2605       return i;
2606   }
2607   return -1;
2608 }
2609
2610 FPDF_BITMAP PDFiumEngine::CreateBitmap(const pp::Rect& rect,
2611                                        pp::ImageData* image_data) const {
2612   void* region;
2613   int stride;
2614   GetRegion(rect.point(), image_data, &region, &stride);
2615   if (!region)
2616     return NULL;
2617   return FPDFBitmap_CreateEx(
2618       rect.width(), rect.height(), FPDFBitmap_BGRx, region, stride);
2619 }
2620
2621 void PDFiumEngine::GetPDFiumRect(
2622     int page_index, const pp::Rect& rect, int* start_x, int* start_y,
2623     int* size_x, int* size_y) const {
2624   pp::Rect page_rect = GetScreenRect(pages_[page_index]->rect());
2625   page_rect.Offset(-rect.x(), -rect.y());
2626
2627   *start_x = page_rect.x();
2628   *start_y = page_rect.y();
2629   *size_x = page_rect.width();
2630   *size_y = page_rect.height();
2631 }
2632
2633 int PDFiumEngine::GetRenderingFlags() const {
2634   int flags = FPDF_LCD_TEXT | FPDF_NO_CATCH;
2635   if (render_grayscale_)
2636     flags |= FPDF_GRAYSCALE;
2637   if (client_->IsPrintPreview())
2638     flags |= FPDF_PRINTING;
2639   return flags;
2640 }
2641
2642 pp::Rect PDFiumEngine::GetVisibleRect() const {
2643   pp::Rect rv;
2644   rv.set_x(static_cast<int>(position_.x() / current_zoom_));
2645   rv.set_y(static_cast<int>(position_.y() / current_zoom_));
2646   rv.set_width(static_cast<int>(ceil(plugin_size_.width() / current_zoom_)));
2647   rv.set_height(static_cast<int>(ceil(plugin_size_.height() / current_zoom_)));
2648   return rv;
2649 }
2650
2651 pp::Rect PDFiumEngine::GetPageScreenRect(int page_index) const {
2652   // Since we use this rect for creating the PDFium bitmap, also include other
2653   // areas around the page that we might need to update such as the page
2654   // separator and the sides if the page is narrower than the document.
2655   return GetScreenRect(pp::Rect(
2656       0,
2657       pages_[page_index]->rect().y() - kPageShadowTop,
2658       document_size_.width(),
2659       pages_[page_index]->rect().height() + kPageShadowTop +
2660           kPageShadowBottom + kPageSeparatorThickness));
2661 }
2662
2663 pp::Rect PDFiumEngine::GetScreenRect(const pp::Rect& rect) const {
2664   pp::Rect rv;
2665   int right =
2666       static_cast<int>(ceil(rect.right() * current_zoom_ - position_.x()));
2667   int bottom =
2668     static_cast<int>(ceil(rect.bottom() * current_zoom_ - position_.y()));
2669
2670   rv.set_x(static_cast<int>(rect.x() * current_zoom_ - position_.x()));
2671   rv.set_y(static_cast<int>(rect.y() * current_zoom_ - position_.y()));
2672   rv.set_width(right - rv.x());
2673   rv.set_height(bottom - rv.y());
2674   return rv;
2675 }
2676
2677 void PDFiumEngine::Highlight(void* buffer,
2678                              int stride,
2679                              const pp::Rect& rect,
2680                              std::vector<pp::Rect>* highlighted_rects) {
2681   if (!buffer)
2682     return;
2683
2684   pp::Rect new_rect = rect;
2685   for (size_t i = 0; i < highlighted_rects->size(); ++i)
2686     new_rect = new_rect.Subtract((*highlighted_rects)[i]);
2687
2688   highlighted_rects->push_back(new_rect);
2689   int l = new_rect.x();
2690   int t = new_rect.y();
2691   int w = new_rect.width();
2692   int h = new_rect.height();
2693
2694   for (int y = t; y < t + h; ++y) {
2695     for (int x = l; x < l + w; ++x) {
2696       uint8* pixel = static_cast<uint8*>(buffer) + y * stride + x * 4;
2697       //  This is our highlight color.
2698       pixel[0] = static_cast<uint8>(
2699           pixel[0] * (kHighlightColorB / 255.0));
2700       pixel[1] = static_cast<uint8>(
2701           pixel[1] * (kHighlightColorG / 255.0));
2702       pixel[2] = static_cast<uint8>(
2703           pixel[2] * (kHighlightColorR / 255.0));
2704     }
2705   }
2706 }
2707
2708 PDFiumEngine::SelectionChangeInvalidator::SelectionChangeInvalidator(
2709     PDFiumEngine* engine) : engine_(engine) {
2710   previous_origin_ = engine_->GetVisibleRect().point();
2711   GetVisibleSelectionsScreenRects(&old_selections_);
2712 }
2713
2714 PDFiumEngine::SelectionChangeInvalidator::~SelectionChangeInvalidator() {
2715   // Offset the old selections if the document scrolled since we recorded them.
2716   pp::Point offset = previous_origin_ - engine_->GetVisibleRect().point();
2717   for (size_t i = 0; i < old_selections_.size(); ++i)
2718     old_selections_[i].Offset(offset);
2719
2720   std::vector<pp::Rect> new_selections;
2721   GetVisibleSelectionsScreenRects(&new_selections);
2722   for (size_t i = 0; i < new_selections.size(); ++i) {
2723     for (size_t j = 0; j < old_selections_.size(); ++j) {
2724       if (new_selections[i] == old_selections_[j]) {
2725         // Rectangle was selected before and after, so no need to invalidate it.
2726         // Mark the rectangles by setting them to empty.
2727         new_selections[i] = old_selections_[j] = pp::Rect();
2728       }
2729     }
2730   }
2731
2732   for (size_t i = 0; i < old_selections_.size(); ++i) {
2733     if (!old_selections_[i].IsEmpty())
2734       engine_->client_->Invalidate(old_selections_[i]);
2735   }
2736   for (size_t i = 0; i < new_selections.size(); ++i) {
2737     if (!new_selections[i].IsEmpty())
2738       engine_->client_->Invalidate(new_selections[i]);
2739   }
2740   engine_->OnSelectionChanged();
2741 }
2742
2743 void
2744 PDFiumEngine::SelectionChangeInvalidator::GetVisibleSelectionsScreenRects(
2745     std::vector<pp::Rect>* rects) {
2746   pp::Rect visible_rect = engine_->GetVisibleRect();
2747   for (size_t i = 0; i < engine_->selection_.size(); ++i) {
2748     int page_index = engine_->selection_[i].page_index();
2749     if (!engine_->IsPageVisible(page_index))
2750       continue;  // This selection is on a page that's not currently visible.
2751
2752     std::vector<pp::Rect> selection_rects =
2753         engine_->selection_[i].GetScreenRects(
2754             visible_rect.point(),
2755             engine_->current_zoom_,
2756             engine_->current_rotation_);
2757     rects->insert(rects->end(), selection_rects.begin(), selection_rects.end());
2758   }
2759 }
2760
2761 PDFiumEngine::MouseDownState::MouseDownState(
2762     const PDFiumPage::Area& area,
2763     const PDFiumPage::LinkTarget& target)
2764     : area_(area), target_(target) {
2765 }
2766
2767 PDFiumEngine::MouseDownState::~MouseDownState() {
2768 }
2769
2770 void PDFiumEngine::MouseDownState::Set(const PDFiumPage::Area& area,
2771                                        const PDFiumPage::LinkTarget& target) {
2772   area_ = area;
2773   target_ = target;
2774 }
2775
2776 void PDFiumEngine::MouseDownState::Reset() {
2777   area_ = PDFiumPage::NONSELECTABLE_AREA;
2778   target_ = PDFiumPage::LinkTarget();
2779 }
2780
2781 bool PDFiumEngine::MouseDownState::Matches(
2782     const PDFiumPage::Area& area,
2783     const PDFiumPage::LinkTarget& target) const {
2784   if (area_ == area) {
2785     if (area == PDFiumPage::WEBLINK_AREA)
2786       return target_.url == target.url;
2787     if (area == PDFiumPage::DOCLINK_AREA)
2788       return target_.page == target.page;
2789     return true;
2790   }
2791   return false;
2792 }
2793
2794 void PDFiumEngine::DeviceToPage(int page_index,
2795                                 float device_x,
2796                                 float device_y,
2797                                 double* page_x,
2798                                 double* page_y) {
2799   *page_x = *page_y = 0;
2800   int temp_x = static_cast<int>((device_x + position_.x())/ current_zoom_ -
2801       pages_[page_index]->rect().x());
2802   int temp_y = static_cast<int>((device_y + position_.y())/ current_zoom_ -
2803       pages_[page_index]->rect().y());
2804   FPDF_DeviceToPage(
2805       pages_[page_index]->GetPage(), 0, 0,
2806       pages_[page_index]->rect().width(),  pages_[page_index]->rect().height(),
2807       current_rotation_, temp_x, temp_y, page_x, page_y);
2808 }
2809
2810 int PDFiumEngine::GetVisiblePageIndex(FPDF_PAGE page) {
2811   for (size_t i = 0; i < visible_pages_.size(); ++i) {
2812     if (pages_[visible_pages_[i]]->GetPage() == page)
2813       return visible_pages_[i];
2814   }
2815   return -1;
2816 }
2817
2818 void PDFiumEngine::SetCurrentPage(int index) {
2819   if (index == most_visible_page_ || !form_)
2820     return;
2821   if (most_visible_page_ != -1 && called_do_document_action_) {
2822     FPDF_PAGE old_page = pages_[most_visible_page_]->GetPage();
2823     FORM_DoPageAAction(old_page, form_, FPDFPAGE_AACTION_CLOSE);
2824   }
2825   most_visible_page_ = index;
2826 #if defined(OS_LINUX)
2827     g_last_instance_id = client_->GetPluginInstance()->pp_instance();
2828 #endif
2829   if (most_visible_page_ != -1 && called_do_document_action_) {
2830     FPDF_PAGE new_page = pages_[most_visible_page_]->GetPage();
2831     FORM_DoPageAAction(new_page, form_, FPDFPAGE_AACTION_OPEN);
2832   }
2833 }
2834
2835 void PDFiumEngine::TransformPDFPageForPrinting(
2836     FPDF_PAGE page,
2837     const PP_PrintSettings_Dev& print_settings) {
2838   // Get the source page width and height in points.
2839   const double src_page_width = FPDF_GetPageWidth(page);
2840   const double src_page_height = FPDF_GetPageHeight(page);
2841
2842   const int src_page_rotation = FPDFPage_GetRotation(page);
2843   const bool fit_to_page = print_settings.print_scaling_option ==
2844       PP_PRINTSCALINGOPTION_FIT_TO_PRINTABLE_AREA;
2845
2846   pp::Size page_size(print_settings.paper_size);
2847   pp::Rect content_rect(print_settings.printable_area);
2848   const bool rotated = (src_page_rotation % 2 == 1);
2849   SetPageSizeAndContentRect(rotated,
2850                             src_page_width > src_page_height,
2851                             &page_size,
2852                             &content_rect);
2853
2854   // Compute the screen page width and height in points.
2855   const int actual_page_width =
2856       rotated ? page_size.height() : page_size.width();
2857   const int actual_page_height =
2858       rotated ? page_size.width() : page_size.height();
2859
2860   const double scale_factor = CalculateScaleFactor(fit_to_page, content_rect,
2861                                                    src_page_width,
2862                                                    src_page_height, rotated);
2863
2864   // Calculate positions for the clip box.
2865   ClipBox source_clip_box;
2866   CalculateClipBoxBoundary(page, scale_factor, rotated, &source_clip_box);
2867
2868   // Calculate the translation offset values.
2869   double offset_x = 0;
2870   double offset_y = 0;
2871   if (fit_to_page) {
2872     CalculateScaledClipBoxOffset(content_rect, source_clip_box, &offset_x,
2873                                  &offset_y);
2874   } else {
2875     CalculateNonScaledClipBoxOffset(content_rect, src_page_rotation,
2876                                     actual_page_width, actual_page_height,
2877                                     source_clip_box, &offset_x, &offset_y);
2878   }
2879
2880   // Reset the media box and crop box. When the page has crop box and media box,
2881   // the plugin will display the crop box contents and not the entire media box.
2882   // If the pages have different crop box values, the plugin will display a
2883   // document of multiple page sizes. To give better user experience, we
2884   // decided to have same crop box and media box values. Hence, the user will
2885   // see a list of uniform pages.
2886   FPDFPage_SetMediaBox(page, 0, 0, page_size.width(), page_size.height());
2887   FPDFPage_SetCropBox(page, 0, 0, page_size.width(), page_size.height());
2888
2889   // Transformation is not required, return. Do this check only after updating
2890   // the media box and crop box. For more detailed information, please refer to
2891   // the comment block right before FPDF_SetMediaBox and FPDF_GetMediaBox calls.
2892   if (scale_factor == 1.0 && offset_x == 0 && offset_y == 0)
2893     return;
2894
2895
2896   // All the positions have been calculated, now manipulate the PDF.
2897   FS_MATRIX matrix = {static_cast<float>(scale_factor),
2898                       0,
2899                       0,
2900                       static_cast<float>(scale_factor),
2901                       static_cast<float>(offset_x),
2902                       static_cast<float>(offset_y)};
2903   FS_RECTF cliprect = {static_cast<float>(source_clip_box.left+offset_x),
2904                        static_cast<float>(source_clip_box.top+offset_y),
2905                        static_cast<float>(source_clip_box.right+offset_x),
2906                        static_cast<float>(source_clip_box.bottom+offset_y)};
2907   FPDFPage_TransFormWithClip(page, &matrix, &cliprect);
2908   FPDFPage_TransformAnnots(page, scale_factor, 0, 0, scale_factor,
2909                            offset_x, offset_y);
2910 }
2911
2912 void PDFiumEngine::DrawPageShadow(const pp::Rect& page_rc,
2913                                   const pp::Rect& shadow_rc,
2914                                   const pp::Rect& clip_rc,
2915                                   pp::ImageData* image_data) {
2916   pp::Rect page_rect(page_rc);
2917   page_rect.Offset(page_offset_);
2918
2919   pp::Rect shadow_rect(shadow_rc);
2920   shadow_rect.Offset(page_offset_);
2921
2922   pp::Rect clip_rect(clip_rc);
2923   clip_rect.Offset(page_offset_);
2924
2925   // Page drop shadow parameters.
2926   const double factor = 0.5;
2927   uint32 depth = std::max(
2928       std::max(page_rect.x() - shadow_rect.x(),
2929                page_rect.y() - shadow_rect.y()),
2930       std::max(shadow_rect.right() - page_rect.right(),
2931                shadow_rect.bottom() - page_rect.bottom()));
2932   depth = static_cast<uint32>(depth * 1.5) + 1;
2933
2934   // We need to check depth only to verify our copy of shadow matrix is correct.
2935   if (!page_shadow_.get() || page_shadow_->depth() != depth)
2936     page_shadow_.reset(new ShadowMatrix(depth, factor, kBackgroundColor));
2937
2938   DCHECK(!image_data->is_null());
2939   DrawShadow(image_data, shadow_rect, page_rect, clip_rect, *page_shadow_);
2940 }
2941
2942 void PDFiumEngine::GetRegion(const pp::Point& location,
2943                              pp::ImageData* image_data,
2944                              void** region,
2945                              int* stride) const {
2946   if (image_data->is_null()) {
2947     DCHECK(plugin_size_.IsEmpty());
2948     *stride = 0;
2949     *region = NULL;
2950     return;
2951   }
2952   char* buffer = static_cast<char*>(image_data->data());
2953   *stride = image_data->stride();
2954
2955   pp::Point offset_location = location + page_offset_;
2956   // TODO: update this when we support BIDI and scrollbars can be on the left.
2957   if (!buffer ||
2958       !pp::Rect(page_offset_, plugin_size_).Contains(offset_location)) {
2959     *region = NULL;
2960     return;
2961   }
2962
2963   buffer += location.y() * (*stride);
2964   buffer += (location.x() + page_offset_.x()) * 4;
2965   *region = buffer;
2966 }
2967
2968 void PDFiumEngine::OnSelectionChanged() {
2969   if (HasPermission(PDFEngine::PERMISSION_COPY))
2970     pp::PDF::SetSelectedText(GetPluginInstance(), GetSelectedText().c_str());
2971 }
2972
2973 void PDFiumEngine::Form_Invalidate(FPDF_FORMFILLINFO* param,
2974                                    FPDF_PAGE page,
2975                                    double left,
2976                                    double top,
2977                                    double right,
2978                                    double bottom) {
2979   PDFiumEngine* engine = static_cast<PDFiumEngine*>(param);
2980   int page_index = engine->GetVisiblePageIndex(page);
2981   if (page_index == -1) {
2982     // This can sometime happen when the page is closed because it went off
2983     // screen, and PDFium invalidates the control as it's being deleted.
2984     return;
2985   }
2986
2987   pp::Rect rect = engine->pages_[page_index]->PageToScreen(
2988       engine->GetVisibleRect().point(), engine->current_zoom_, left, top, right,
2989       bottom, engine->current_rotation_);
2990   engine->client_->Invalidate(rect);
2991 }
2992
2993 void PDFiumEngine::Form_OutputSelectedRect(FPDF_FORMFILLINFO* param,
2994                                            FPDF_PAGE page,
2995                                            double left,
2996                                            double top,
2997                                            double right,
2998                                            double bottom) {
2999   PDFiumEngine* engine = static_cast<PDFiumEngine*>(param);
3000   int page_index = engine->GetVisiblePageIndex(page);
3001   if (page_index == -1) {
3002     NOTREACHED();
3003     return;
3004   }
3005   pp::Rect rect = engine->pages_[page_index]->PageToScreen(
3006       engine->GetVisibleRect().point(), engine->current_zoom_, left, top, right,
3007       bottom, engine->current_rotation_);
3008   engine->form_highlights_.push_back(rect);
3009 }
3010
3011 void PDFiumEngine::Form_SetCursor(FPDF_FORMFILLINFO* param, int cursor_type) {
3012   // We don't need this since it's not enough to change the cursor in all
3013   // scenarios.  Instead, we check which form field we're under in OnMouseMove.
3014 }
3015
3016 int PDFiumEngine::Form_SetTimer(FPDF_FORMFILLINFO* param,
3017                                 int elapse,
3018                                 TimerCallback timer_func) {
3019   PDFiumEngine* engine = static_cast<PDFiumEngine*>(param);
3020   engine->timers_[++engine->next_timer_id_] =
3021       std::pair<int, TimerCallback>(elapse, timer_func);
3022   engine->client_->ScheduleCallback(engine->next_timer_id_, elapse);
3023   return engine->next_timer_id_;
3024 }
3025
3026 void PDFiumEngine::Form_KillTimer(FPDF_FORMFILLINFO* param, int timer_id) {
3027   PDFiumEngine* engine = static_cast<PDFiumEngine*>(param);
3028   engine->timers_.erase(timer_id);
3029 }
3030
3031 FPDF_SYSTEMTIME PDFiumEngine::Form_GetLocalTime(FPDF_FORMFILLINFO* param) {
3032   base::Time time = base::Time::Now();
3033   base::Time::Exploded exploded;
3034   time.LocalExplode(&exploded);
3035
3036   FPDF_SYSTEMTIME rv;
3037   rv.wYear = exploded.year;
3038   rv.wMonth = exploded.month;
3039   rv.wDayOfWeek = exploded.day_of_week;
3040   rv.wDay = exploded.day_of_month;
3041   rv.wHour = exploded.hour;
3042   rv.wMinute = exploded.minute;
3043   rv.wSecond = exploded.second;
3044   rv.wMilliseconds = exploded.millisecond;
3045   return rv;
3046 }
3047
3048 void PDFiumEngine::Form_OnChange(FPDF_FORMFILLINFO* param) {
3049   // Don't care about.
3050 }
3051
3052 FPDF_PAGE PDFiumEngine::Form_GetPage(FPDF_FORMFILLINFO* param,
3053                                      FPDF_DOCUMENT document,
3054                                      int page_index) {
3055   PDFiumEngine* engine = static_cast<PDFiumEngine*>(param);
3056   if (page_index < 0 || page_index >= static_cast<int>(engine->pages_.size()))
3057     return NULL;
3058   return engine->pages_[page_index]->GetPage();
3059 }
3060
3061 FPDF_PAGE PDFiumEngine::Form_GetCurrentPage(FPDF_FORMFILLINFO* param,
3062                                             FPDF_DOCUMENT document) {
3063   // TODO(jam): find out what this is used for.
3064   PDFiumEngine* engine = static_cast<PDFiumEngine*>(param);
3065   int index = engine->last_page_mouse_down_;
3066   if (index == -1) {
3067     index = engine->GetMostVisiblePage();
3068     if (index == -1) {
3069       NOTREACHED();
3070       return NULL;
3071     }
3072   }
3073
3074   return engine->pages_[index]->GetPage();
3075 }
3076
3077 int PDFiumEngine::Form_GetRotation(FPDF_FORMFILLINFO* param, FPDF_PAGE page) {
3078   return 0;
3079 }
3080
3081 void PDFiumEngine::Form_ExecuteNamedAction(FPDF_FORMFILLINFO* param,
3082                                            FPDF_BYTESTRING named_action) {
3083   PDFiumEngine* engine = static_cast<PDFiumEngine*>(param);
3084   std::string action(named_action);
3085   if (action == "Print") {
3086     engine->client_->Print();
3087     return;
3088   }
3089
3090   int index = engine->last_page_mouse_down_;
3091   /* Don't try to calculate the most visible page if we don't have a left click
3092      before this event (this code originally copied Form_GetCurrentPage which of
3093      course needs to do that and which doesn't have recursion). This can end up
3094      causing infinite recursion. See http://crbug.com/240413 for more
3095      information. Either way, it's not necessary for the spec'd list of named
3096      actions.
3097   if (index == -1)
3098     index = engine->GetMostVisiblePage();
3099   */
3100   if (index == -1)
3101     return;
3102
3103   // This is the only list of named actions per the spec (see 12.6.4.11). Adobe
3104   // Reader supports more, like FitWidth, but since they're not part of the spec
3105   // and we haven't got bugs about them, no need to now.
3106   if (action == "NextPage") {
3107     engine->client_->ScrollToPage(index + 1);
3108   } else if (action == "PrevPage") {
3109     engine->client_->ScrollToPage(index - 1);
3110   } else if (action == "FirstPage") {
3111     engine->client_->ScrollToPage(0);
3112   } else if (action == "LastPage") {
3113     engine->client_->ScrollToPage(engine->pages_.size() - 1);
3114   }
3115 }
3116
3117 void PDFiumEngine::Form_SetTextFieldFocus(FPDF_FORMFILLINFO* param,
3118                                           FPDF_WIDESTRING value,
3119                                           FPDF_DWORD valueLen,
3120                                           FPDF_BOOL is_focus) {
3121   // Do nothing for now.
3122   // TODO(gene): use this signal to trigger OSK.
3123 }
3124
3125 void PDFiumEngine::Form_DoURIAction(FPDF_FORMFILLINFO* param,
3126                                     FPDF_BYTESTRING uri) {
3127   PDFiumEngine* engine = static_cast<PDFiumEngine*>(param);
3128   engine->client_->NavigateTo(std::string(uri), false);
3129 }
3130
3131 void PDFiumEngine::Form_DoGoToAction(FPDF_FORMFILLINFO* param,
3132                                      int page_index,
3133                                      int zoom_mode,
3134                                      float* position_array,
3135                                      int size_of_array) {
3136   PDFiumEngine* engine = static_cast<PDFiumEngine*>(param);
3137   engine->client_->ScrollToPage(page_index);
3138 }
3139
3140 int PDFiumEngine::Form_Alert(IPDF_JSPLATFORM* param,
3141                              FPDF_WIDESTRING message,
3142                              FPDF_WIDESTRING title,
3143                              int type,
3144                              int icon) {
3145   // See fpdfformfill.h for these values.
3146   enum AlertType {
3147     ALERT_TYPE_OK = 0,
3148     ALERT_TYPE_OK_CANCEL,
3149     ALERT_TYPE_YES_ON,
3150     ALERT_TYPE_YES_NO_CANCEL
3151   };
3152
3153   enum AlertResult {
3154     ALERT_RESULT_OK = 1,
3155     ALERT_RESULT_CANCEL,
3156     ALERT_RESULT_NO,
3157     ALERT_RESULT_YES
3158   };
3159
3160   PDFiumEngine* engine = static_cast<PDFiumEngine*>(param);
3161   std::string message_str =
3162       base::UTF16ToUTF8(reinterpret_cast<const base::char16*>(message));
3163   if (type == ALERT_TYPE_OK) {
3164     engine->client_->Alert(message_str);
3165     return ALERT_RESULT_OK;
3166   }
3167
3168   bool rv = engine->client_->Confirm(message_str);
3169   if (type == ALERT_TYPE_OK_CANCEL)
3170     return rv ? ALERT_RESULT_OK : ALERT_RESULT_CANCEL;
3171   return rv ? ALERT_RESULT_YES : ALERT_RESULT_NO;
3172 }
3173
3174 void PDFiumEngine::Form_Beep(IPDF_JSPLATFORM* param, int type) {
3175   // Beeps are annoying, and not possible using javascript, so ignore for now.
3176 }
3177
3178 int PDFiumEngine::Form_Response(IPDF_JSPLATFORM* param,
3179                                 FPDF_WIDESTRING question,
3180                                 FPDF_WIDESTRING title,
3181                                 FPDF_WIDESTRING default_response,
3182                                 FPDF_WIDESTRING label,
3183                                 FPDF_BOOL password,
3184                                 void* response,
3185                                 int length) {
3186   std::string question_str = base::UTF16ToUTF8(
3187       reinterpret_cast<const base::char16*>(question));
3188   std::string default_str = base::UTF16ToUTF8(
3189       reinterpret_cast<const base::char16*>(default_response));
3190
3191   PDFiumEngine* engine = static_cast<PDFiumEngine*>(param);
3192   std::string rv = engine->client_->Prompt(question_str, default_str);
3193   base::string16 rv_16 = base::UTF8ToUTF16(rv);
3194   int rv_bytes = rv_16.size() * sizeof(base::char16);
3195   if (response) {
3196     int bytes_to_copy = rv_bytes < length ? rv_bytes : length;
3197     memcpy(response, rv_16.c_str(), bytes_to_copy);
3198   }
3199   return rv_bytes;
3200 }
3201
3202 int PDFiumEngine::Form_GetFilePath(IPDF_JSPLATFORM* param,
3203                                    void* file_path,
3204                                    int length) {
3205   PDFiumEngine* engine = static_cast<PDFiumEngine*>(param);
3206   std::string rv = engine->client_->GetURL();
3207   if (file_path && rv.size() <= static_cast<size_t>(length))
3208     memcpy(file_path, rv.c_str(), rv.size());
3209   return rv.size();
3210 }
3211
3212 void PDFiumEngine::Form_Mail(IPDF_JSPLATFORM* param,
3213                              void* mail_data,
3214                              int length,
3215                              FPDF_BOOL ui,
3216                              FPDF_WIDESTRING to,
3217                              FPDF_WIDESTRING subject,
3218                              FPDF_WIDESTRING cc,
3219                              FPDF_WIDESTRING bcc,
3220                              FPDF_WIDESTRING message) {
3221   DCHECK(length == 0);  // Don't handle attachments; no way with mailto.
3222   std::string to_str =
3223       base::UTF16ToUTF8(reinterpret_cast<const base::char16*>(to));
3224   std::string cc_str =
3225       base::UTF16ToUTF8(reinterpret_cast<const base::char16*>(cc));
3226   std::string bcc_str =
3227       base::UTF16ToUTF8(reinterpret_cast<const base::char16*>(bcc));
3228   std::string subject_str =
3229       base::UTF16ToUTF8(reinterpret_cast<const base::char16*>(subject));
3230   std::string message_str =
3231       base::UTF16ToUTF8(reinterpret_cast<const base::char16*>(message));
3232
3233   PDFiumEngine* engine = static_cast<PDFiumEngine*>(param);
3234   engine->client_->Email(to_str, cc_str, bcc_str, subject_str, message_str);
3235 }
3236
3237 void PDFiumEngine::Form_Print(IPDF_JSPLATFORM* param,
3238                               FPDF_BOOL ui,
3239                               int start,
3240                               int end,
3241                               FPDF_BOOL silent,
3242                               FPDF_BOOL shrink_to_fit,
3243                               FPDF_BOOL print_as_image,
3244                               FPDF_BOOL reverse,
3245                               FPDF_BOOL annotations) {
3246   // No way to pass the extra information to the print dialog using JavaScript.
3247   // Just opening it is fine for now.
3248   PDFiumEngine* engine = static_cast<PDFiumEngine*>(param);
3249   engine->client_->Print();
3250 }
3251
3252 void PDFiumEngine::Form_SubmitForm(IPDF_JSPLATFORM* param,
3253                                    void* form_data,
3254                                    int length,
3255                                    FPDF_WIDESTRING url) {
3256   std::string url_str =
3257       base::UTF16ToUTF8(reinterpret_cast<const base::char16*>(url));
3258   PDFiumEngine* engine = static_cast<PDFiumEngine*>(param);
3259   engine->client_->SubmitForm(url_str, form_data, length);
3260 }
3261
3262 void PDFiumEngine::Form_GotoPage(IPDF_JSPLATFORM* param,
3263                                  int page_number) {
3264   PDFiumEngine* engine = static_cast<PDFiumEngine*>(param);
3265   engine->client_->ScrollToPage(page_number);
3266 }
3267
3268 int PDFiumEngine::Form_Browse(IPDF_JSPLATFORM* param,
3269                               void* file_path,
3270                               int length) {
3271   PDFiumEngine* engine = static_cast<PDFiumEngine*>(param);
3272   std::string path = engine->client_->ShowFileSelectionDialog();
3273   if (path.size() + 1 <= static_cast<size_t>(length))
3274     memcpy(file_path, &path[0], path.size() + 1);
3275   return path.size() + 1;
3276 }
3277
3278 FPDF_BOOL PDFiumEngine::Pause_NeedToPauseNow(IFSDK_PAUSE* param) {
3279   PDFiumEngine* engine = static_cast<PDFiumEngine*>(param);
3280   return (base::Time::Now() - engine->last_progressive_start_time_).
3281       InMilliseconds() > engine->progressive_paint_timeout_;
3282 }
3283
3284 ScopedUnsupportedFeature::ScopedUnsupportedFeature(PDFiumEngine* engine)
3285     : engine_(engine), old_engine_(g_engine_for_unsupported) {
3286   g_engine_for_unsupported = engine_;
3287 }
3288
3289 ScopedUnsupportedFeature::~ScopedUnsupportedFeature() {
3290   g_engine_for_unsupported = old_engine_;
3291 }
3292
3293 PDFEngineExports* PDFEngineExports::Create() {
3294   return new PDFiumEngineExports;
3295 }
3296
3297 namespace {
3298
3299 int CalculatePosition(FPDF_PAGE page,
3300                       const PDFiumEngineExports::RenderingSettings& settings,
3301                       pp::Rect* dest) {
3302   int page_width = static_cast<int>(
3303       FPDF_GetPageWidth(page) * settings.dpi_x / kPointsPerInch);
3304   int page_height = static_cast<int>(
3305       FPDF_GetPageHeight(page) * settings.dpi_y / kPointsPerInch);
3306
3307   // Start by assuming that we will draw exactly to the bounds rect
3308   // specified.
3309   *dest = settings.bounds;
3310
3311   int rotate = 0;  // normal orientation.
3312
3313   // Auto-rotate landscape pages to print correctly.
3314   if (settings.autorotate &&
3315       (dest->width() > dest->height()) != (page_width > page_height)) {
3316     rotate = 3;  // 90 degrees counter-clockwise.
3317     std::swap(page_width, page_height);
3318   }
3319
3320   // See if we need to scale the output
3321   bool scale_to_bounds = false;
3322   if (settings.fit_to_bounds &&
3323       ((page_width > dest->width()) || (page_height > dest->height()))) {
3324     scale_to_bounds = true;
3325   } else if (settings.stretch_to_bounds &&
3326              ((page_width < dest->width()) || (page_height < dest->height()))) {
3327     scale_to_bounds = true;
3328   }
3329
3330   if (scale_to_bounds) {
3331     // If we need to maintain aspect ratio, calculate the actual width and
3332     // height.
3333     if (settings.keep_aspect_ratio) {
3334       double scale_factor_x = page_width;
3335       scale_factor_x /= dest->width();
3336       double scale_factor_y = page_height;
3337       scale_factor_y /= dest->height();
3338       if (scale_factor_x > scale_factor_y) {
3339         dest->set_height(page_height / scale_factor_x);
3340       } else {
3341         dest->set_width(page_width / scale_factor_y);
3342       }
3343     }
3344   } else {
3345     // We are not scaling to bounds. Draw in the actual page size. If the
3346     // actual page size is larger than the bounds, the output will be
3347     // clipped.
3348     dest->set_width(page_width);
3349     dest->set_height(page_height);
3350   }
3351
3352   if (settings.center_in_bounds) {
3353     pp::Point offset((settings.bounds.width() - dest->width()) / 2,
3354                      (settings.bounds.height() - dest->height()) / 2);
3355     dest->Offset(offset);
3356   }
3357   return rotate;
3358 }
3359
3360 }  // namespace
3361
3362 #if defined(OS_WIN)
3363 bool PDFiumEngineExports::RenderPDFPageToDC(const void* pdf_buffer,
3364                                             int buffer_size,
3365                                             int page_number,
3366                                             const RenderingSettings& settings,
3367                                             HDC dc) {
3368   FPDF_DOCUMENT doc = FPDF_LoadMemDocument(pdf_buffer, buffer_size, NULL);
3369   if (!doc)
3370     return false;
3371   FPDF_PAGE page = FPDF_LoadPage(doc, page_number);
3372   if (!page) {
3373     FPDF_CloseDocument(doc);
3374     return false;
3375   }
3376   RenderingSettings new_settings = settings;
3377   // calculate the page size
3378   if (new_settings.dpi_x == -1)
3379     new_settings.dpi_x = GetDeviceCaps(dc, LOGPIXELSX);
3380   if (new_settings.dpi_y == -1)
3381     new_settings.dpi_y = GetDeviceCaps(dc, LOGPIXELSY);
3382
3383   pp::Rect dest;
3384   int rotate = CalculatePosition(page, new_settings, &dest);
3385
3386   int save_state = SaveDC(dc);
3387   // The caller wanted all drawing to happen within the bounds specified.
3388   // Based on scale calculations, our destination rect might be larger
3389   // than the bounds. Set the clip rect to the bounds.
3390   IntersectClipRect(dc, settings.bounds.x(), settings.bounds.y(),
3391                     settings.bounds.x() + settings.bounds.width(),
3392                     settings.bounds.y() + settings.bounds.height());
3393
3394   // A temporary hack. PDFs generated by Cairo (used by Chrome OS to generate
3395   // a PDF output from a webpage) result in very large metafiles and the
3396   // rendering using FPDF_RenderPage is incorrect. In this case, render as a
3397   // bitmap. Note that this code does not kick in for PDFs printed from Chrome
3398   // because in that case we create a temp PDF first before printing and this
3399   // temp PDF does not have a creator string that starts with "cairo".
3400   base::string16 creator;
3401   size_t buffer_bytes = FPDF_GetMetaText(doc, "Creator", NULL, 0);
3402   if (buffer_bytes > 1) {
3403     FPDF_GetMetaText(doc, "Creator", WriteInto(&creator, buffer_bytes),
3404                      buffer_bytes);
3405   }
3406   bool use_bitmap = false;
3407   if (StartsWith(creator, L"cairo", false))
3408     use_bitmap = true;
3409
3410   // Another temporary hack. Some PDFs seems to render very slowly if
3411   // FPDF_RenderPage is directly used on a printer DC. I suspect it is
3412   // because of the code to talk Postscript directly to the printer if
3413   // the printer supports this. Need to discuss this with PDFium. For now,
3414   // render to a bitmap and then blit the bitmap to the DC if we have been
3415   // supplied a printer DC.
3416   int device_type = GetDeviceCaps(dc, TECHNOLOGY);
3417   if (use_bitmap ||
3418       (device_type == DT_RASPRINTER) || (device_type == DT_PLOTTER)) {
3419     FPDF_BITMAP bitmap = FPDFBitmap_Create(dest.width(), dest.height(),
3420                                            FPDFBitmap_BGRx);
3421       // Clear the bitmap
3422     FPDFBitmap_FillRect(bitmap, 0, 0, dest.width(), dest.height(), 0xFFFFFFFF);
3423     FPDF_RenderPageBitmap(
3424         bitmap, page, 0, 0, dest.width(), dest.height(), rotate,
3425         FPDF_ANNOT | FPDF_PRINTING | FPDF_NO_CATCH);
3426     int stride = FPDFBitmap_GetStride(bitmap);
3427     BITMAPINFO bmi;
3428     memset(&bmi, 0, sizeof(bmi));
3429     bmi.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
3430     bmi.bmiHeader.biWidth = dest.width();
3431     bmi.bmiHeader.biHeight = -dest.height();  // top-down image
3432     bmi.bmiHeader.biPlanes = 1;
3433     bmi.bmiHeader.biBitCount = 32;
3434     bmi.bmiHeader.biCompression = BI_RGB;
3435     bmi.bmiHeader.biSizeImage = stride * dest.height();
3436     StretchDIBits(dc, dest.x(), dest.y(), dest.width(), dest.height(),
3437                   0, 0, dest.width(), dest.height(),
3438                   FPDFBitmap_GetBuffer(bitmap), &bmi, DIB_RGB_COLORS, SRCCOPY);
3439     FPDFBitmap_Destroy(bitmap);
3440   } else {
3441     FPDF_RenderPage(dc, page, dest.x(), dest.y(), dest.width(), dest.height(),
3442                     rotate, FPDF_ANNOT | FPDF_PRINTING | FPDF_NO_CATCH);
3443   }
3444   RestoreDC(dc, save_state);
3445   FPDF_ClosePage(page);
3446   FPDF_CloseDocument(doc);
3447   return true;
3448 }
3449 #endif  // OS_WIN
3450
3451 bool PDFiumEngineExports::RenderPDFPageToBitmap(
3452     const void* pdf_buffer,
3453     int pdf_buffer_size,
3454     int page_number,
3455     const RenderingSettings& settings,
3456     void* bitmap_buffer) {
3457   FPDF_DOCUMENT doc = FPDF_LoadMemDocument(pdf_buffer, pdf_buffer_size, NULL);
3458   if (!doc)
3459     return false;
3460   FPDF_PAGE page = FPDF_LoadPage(doc, page_number);
3461   if (!page) {
3462     FPDF_CloseDocument(doc);
3463     return false;
3464   }
3465
3466   pp::Rect dest;
3467   int rotate = CalculatePosition(page, settings, &dest);
3468
3469   FPDF_BITMAP bitmap =
3470       FPDFBitmap_CreateEx(settings.bounds.width(), settings.bounds.height(),
3471                           FPDFBitmap_BGRA, bitmap_buffer,
3472                           settings.bounds.width() * 4);
3473   // Clear the bitmap
3474   FPDFBitmap_FillRect(bitmap, 0, 0, settings.bounds.width(),
3475                       settings.bounds.height(), 0xFFFFFFFF);
3476   // Shift top-left corner of bounds to (0, 0) if it's not there.
3477   dest.set_point(dest.point() - settings.bounds.point());
3478   FPDF_RenderPageBitmap(
3479       bitmap, page, dest.x(), dest.y(), dest.width(), dest.height(), rotate,
3480       FPDF_ANNOT | FPDF_PRINTING | FPDF_NO_CATCH);
3481   FPDFBitmap_Destroy(bitmap);
3482   FPDF_ClosePage(page);
3483   FPDF_CloseDocument(doc);
3484   return true;
3485 }
3486
3487 bool PDFiumEngineExports::GetPDFDocInfo(const void* pdf_buffer,
3488                                         int buffer_size,
3489                                         int* page_count,
3490                                         double* max_page_width) {
3491   FPDF_DOCUMENT doc = FPDF_LoadMemDocument(pdf_buffer, buffer_size, NULL);
3492   if (!doc)
3493     return false;
3494   int page_count_local = FPDF_GetPageCount(doc);
3495   if (page_count) {
3496     *page_count = page_count_local;
3497   }
3498   if (max_page_width) {
3499     *max_page_width = 0;
3500     for (int page_number = 0; page_number < page_count_local; page_number++) {
3501       double page_width = 0;
3502       double page_height = 0;
3503       FPDF_GetPageSizeByIndex(doc, page_number, &page_width, &page_height);
3504       if (page_width > *max_page_width) {
3505         *max_page_width = page_width;
3506       }
3507     }
3508   }
3509   FPDF_CloseDocument(doc);
3510   return true;
3511 }
3512
3513 bool PDFiumEngineExports::GetPDFPageSizeByIndex(
3514     const void* pdf_buffer,
3515     int pdf_buffer_size,
3516     int page_number,
3517     double* width,
3518     double* height) {
3519   FPDF_DOCUMENT doc = FPDF_LoadMemDocument(pdf_buffer, pdf_buffer_size, NULL);
3520   if (!doc)
3521     return false;
3522   bool success = FPDF_GetPageSizeByIndex(doc, page_number, width, height) != 0;
3523   FPDF_CloseDocument(doc);
3524   return success;
3525 }
3526
3527 }  // namespace chrome_pdf