From fdc00a7042d912aafaabddae4d9c84199921ef23 Mon Sep 17 00:00:00 2001 From: Bo Xu Date: Tue, 28 Oct 2014 23:03:33 -0700 Subject: Merge XFA to PDFium master at 4dc95e7 on 10/28/2014 --- .../pdf/pdfium/pdfium_assert_matching_enums.cc | 207 ++ xfa_test/pdf/pdfium/pdfium_engine.cc | 3884 ++++++++++++++++++++ xfa_test/pdf/pdfium/pdfium_engine.h | 687 ++++ xfa_test/pdf/pdfium/pdfium_mem_buffer_file_read.cc | 34 + xfa_test/pdf/pdfium/pdfium_mem_buffer_file_read.h | 30 + .../pdf/pdfium/pdfium_mem_buffer_file_write.cc | 33 + xfa_test/pdf/pdfium/pdfium_mem_buffer_file_write.h | 34 + xfa_test/pdf/pdfium/pdfium_page.cc | 477 +++ xfa_test/pdf/pdfium/pdfium_page.h | 136 + xfa_test/pdf/pdfium/pdfium_range.cc | 79 + xfa_test/pdf/pdfium/pdfium_range.h | 54 + 11 files changed, 5655 insertions(+) create mode 100644 xfa_test/pdf/pdfium/pdfium_assert_matching_enums.cc create mode 100644 xfa_test/pdf/pdfium/pdfium_engine.cc create mode 100644 xfa_test/pdf/pdfium/pdfium_engine.h create mode 100644 xfa_test/pdf/pdfium/pdfium_mem_buffer_file_read.cc create mode 100644 xfa_test/pdf/pdfium/pdfium_mem_buffer_file_read.h create mode 100644 xfa_test/pdf/pdfium/pdfium_mem_buffer_file_write.cc create mode 100644 xfa_test/pdf/pdfium/pdfium_mem_buffer_file_write.h create mode 100644 xfa_test/pdf/pdfium/pdfium_page.cc create mode 100644 xfa_test/pdf/pdfium/pdfium_page.h create mode 100644 xfa_test/pdf/pdfium/pdfium_range.cc create mode 100644 xfa_test/pdf/pdfium/pdfium_range.h (limited to 'xfa_test/pdf/pdfium') diff --git a/xfa_test/pdf/pdfium/pdfium_assert_matching_enums.cc b/xfa_test/pdf/pdfium/pdfium_assert_matching_enums.cc new file mode 100644 index 0000000000..ef140cf7c9 --- /dev/null +++ b/xfa_test/pdf/pdfium/pdfium_assert_matching_enums.cc @@ -0,0 +1,207 @@ +// Copyright (c) 2010 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#include "base/basictypes.h" +#include "ppapi/c/pp_input_event.h" +#include "ppapi/c/private/ppb_pdf.h" +#include "third_party/pdfium/fpdfsdk/include/fpdf_fwlevent.h" +#include "third_party/pdfium/fpdfsdk/include/fpdf_sysfontinfo.h" +#include "ui/events/keycodes/keyboard_codes.h" + +#define COMPILE_ASSERT_MATCH(np_name, pdfium_name) \ + COMPILE_ASSERT(int(np_name) == int(pdfium_name), mismatching_enums) + +COMPILE_ASSERT_MATCH(PP_INPUTEVENT_MODIFIER_SHIFTKEY, FWL_EVENTFLAG_ShiftKey); +COMPILE_ASSERT_MATCH(PP_INPUTEVENT_MODIFIER_CONTROLKEY, + FWL_EVENTFLAG_ControlKey); +COMPILE_ASSERT_MATCH(PP_INPUTEVENT_MODIFIER_ALTKEY, FWL_EVENTFLAG_AltKey); +COMPILE_ASSERT_MATCH(PP_INPUTEVENT_MODIFIER_METAKEY, FWL_EVENTFLAG_MetaKey); +COMPILE_ASSERT_MATCH(PP_INPUTEVENT_MODIFIER_ISKEYPAD, FWL_EVENTFLAG_KeyPad); +COMPILE_ASSERT_MATCH(PP_INPUTEVENT_MODIFIER_ISAUTOREPEAT, + FWL_EVENTFLAG_AutoRepeat); +COMPILE_ASSERT_MATCH(PP_INPUTEVENT_MODIFIER_LEFTBUTTONDOWN, + FWL_EVENTFLAG_LeftButtonDown); +COMPILE_ASSERT_MATCH(PP_INPUTEVENT_MODIFIER_MIDDLEBUTTONDOWN, + FWL_EVENTFLAG_MiddleButtonDown); +COMPILE_ASSERT_MATCH(PP_INPUTEVENT_MODIFIER_RIGHTBUTTONDOWN, + FWL_EVENTFLAG_RightButtonDown); + +COMPILE_ASSERT_MATCH(ui::VKEY_BACK, FWL_VKEY_Back); +COMPILE_ASSERT_MATCH(ui::VKEY_TAB, FWL_VKEY_Tab); +COMPILE_ASSERT_MATCH(ui::VKEY_CLEAR, FWL_VKEY_Clear); +COMPILE_ASSERT_MATCH(ui::VKEY_RETURN, FWL_VKEY_Return); +COMPILE_ASSERT_MATCH(ui::VKEY_SHIFT, FWL_VKEY_Shift); +COMPILE_ASSERT_MATCH(ui::VKEY_CONTROL, FWL_VKEY_Control); +COMPILE_ASSERT_MATCH(ui::VKEY_MENU, FWL_VKEY_Menu); +COMPILE_ASSERT_MATCH(ui::VKEY_PAUSE, FWL_VKEY_Pause); +COMPILE_ASSERT_MATCH(ui::VKEY_CAPITAL, FWL_VKEY_Capital); +COMPILE_ASSERT_MATCH(ui::VKEY_KANA, FWL_VKEY_Kana); +COMPILE_ASSERT_MATCH(ui::VKEY_HANGUL, FWL_VKEY_Hangul); +COMPILE_ASSERT_MATCH(ui::VKEY_JUNJA, FWL_VKEY_Junja); +COMPILE_ASSERT_MATCH(ui::VKEY_FINAL, FWL_VKEY_Final); +COMPILE_ASSERT_MATCH(ui::VKEY_HANJA, FWL_VKEY_Hanja); +COMPILE_ASSERT_MATCH(ui::VKEY_KANJI, FWL_VKEY_Kanji); +COMPILE_ASSERT_MATCH(ui::VKEY_ESCAPE, FWL_VKEY_Escape); +COMPILE_ASSERT_MATCH(ui::VKEY_CONVERT, FWL_VKEY_Convert); +COMPILE_ASSERT_MATCH(ui::VKEY_NONCONVERT, FWL_VKEY_NonConvert); +COMPILE_ASSERT_MATCH(ui::VKEY_ACCEPT, FWL_VKEY_Accept); +COMPILE_ASSERT_MATCH(ui::VKEY_MODECHANGE, FWL_VKEY_ModeChange); +COMPILE_ASSERT_MATCH(ui::VKEY_SPACE, FWL_VKEY_Space); +COMPILE_ASSERT_MATCH(ui::VKEY_PRIOR, FWL_VKEY_Prior); +COMPILE_ASSERT_MATCH(ui::VKEY_NEXT, FWL_VKEY_Next); +COMPILE_ASSERT_MATCH(ui::VKEY_END, FWL_VKEY_End); +COMPILE_ASSERT_MATCH(ui::VKEY_HOME, FWL_VKEY_Home); +COMPILE_ASSERT_MATCH(ui::VKEY_LEFT, FWL_VKEY_Left); +COMPILE_ASSERT_MATCH(ui::VKEY_UP, FWL_VKEY_Up); +COMPILE_ASSERT_MATCH(ui::VKEY_RIGHT, FWL_VKEY_Right); +COMPILE_ASSERT_MATCH(ui::VKEY_DOWN, FWL_VKEY_Down); +COMPILE_ASSERT_MATCH(ui::VKEY_SELECT, FWL_VKEY_Select); +COMPILE_ASSERT_MATCH(ui::VKEY_PRINT, FWL_VKEY_Print); +COMPILE_ASSERT_MATCH(ui::VKEY_EXECUTE, FWL_VKEY_Execute); +COMPILE_ASSERT_MATCH(ui::VKEY_SNAPSHOT, FWL_VKEY_Snapshot); +COMPILE_ASSERT_MATCH(ui::VKEY_INSERT, FWL_VKEY_Insert); +COMPILE_ASSERT_MATCH(ui::VKEY_DELETE, FWL_VKEY_Delete); +COMPILE_ASSERT_MATCH(ui::VKEY_HELP, FWL_VKEY_Help); +COMPILE_ASSERT_MATCH(ui::VKEY_0, FWL_VKEY_0); +COMPILE_ASSERT_MATCH(ui::VKEY_1, FWL_VKEY_1); +COMPILE_ASSERT_MATCH(ui::VKEY_2, FWL_VKEY_2); +COMPILE_ASSERT_MATCH(ui::VKEY_3, FWL_VKEY_3); +COMPILE_ASSERT_MATCH(ui::VKEY_4, FWL_VKEY_4); +COMPILE_ASSERT_MATCH(ui::VKEY_5, FWL_VKEY_5); +COMPILE_ASSERT_MATCH(ui::VKEY_6, FWL_VKEY_6); +COMPILE_ASSERT_MATCH(ui::VKEY_7, FWL_VKEY_7); +COMPILE_ASSERT_MATCH(ui::VKEY_8, FWL_VKEY_8); +COMPILE_ASSERT_MATCH(ui::VKEY_9, FWL_VKEY_9); +COMPILE_ASSERT_MATCH(ui::VKEY_A, FWL_VKEY_A); +COMPILE_ASSERT_MATCH(ui::VKEY_B, FWL_VKEY_B); +COMPILE_ASSERT_MATCH(ui::VKEY_C, FWL_VKEY_C); +COMPILE_ASSERT_MATCH(ui::VKEY_D, FWL_VKEY_D); +COMPILE_ASSERT_MATCH(ui::VKEY_E, FWL_VKEY_E); +COMPILE_ASSERT_MATCH(ui::VKEY_F, FWL_VKEY_F); +COMPILE_ASSERT_MATCH(ui::VKEY_G, FWL_VKEY_G); +COMPILE_ASSERT_MATCH(ui::VKEY_H, FWL_VKEY_H); +COMPILE_ASSERT_MATCH(ui::VKEY_I, FWL_VKEY_I); +COMPILE_ASSERT_MATCH(ui::VKEY_J, FWL_VKEY_J); +COMPILE_ASSERT_MATCH(ui::VKEY_K, FWL_VKEY_K); +COMPILE_ASSERT_MATCH(ui::VKEY_L, FWL_VKEY_L); +COMPILE_ASSERT_MATCH(ui::VKEY_M, FWL_VKEY_M); +COMPILE_ASSERT_MATCH(ui::VKEY_N, FWL_VKEY_N); +COMPILE_ASSERT_MATCH(ui::VKEY_O, FWL_VKEY_O); +COMPILE_ASSERT_MATCH(ui::VKEY_P, FWL_VKEY_P); +COMPILE_ASSERT_MATCH(ui::VKEY_Q, FWL_VKEY_Q); +COMPILE_ASSERT_MATCH(ui::VKEY_R, FWL_VKEY_R); +COMPILE_ASSERT_MATCH(ui::VKEY_S, FWL_VKEY_S); +COMPILE_ASSERT_MATCH(ui::VKEY_T, FWL_VKEY_T); +COMPILE_ASSERT_MATCH(ui::VKEY_U, FWL_VKEY_U); +COMPILE_ASSERT_MATCH(ui::VKEY_V, FWL_VKEY_V); +COMPILE_ASSERT_MATCH(ui::VKEY_W, FWL_VKEY_W); +COMPILE_ASSERT_MATCH(ui::VKEY_X, FWL_VKEY_X); +COMPILE_ASSERT_MATCH(ui::VKEY_Y, FWL_VKEY_Y); +COMPILE_ASSERT_MATCH(ui::VKEY_Z, FWL_VKEY_Z); +COMPILE_ASSERT_MATCH(ui::VKEY_LWIN, FWL_VKEY_LWin); +COMPILE_ASSERT_MATCH(ui::VKEY_COMMAND, FWL_VKEY_Command); +COMPILE_ASSERT_MATCH(ui::VKEY_RWIN, FWL_VKEY_RWin); +COMPILE_ASSERT_MATCH(ui::VKEY_APPS, FWL_VKEY_Apps); +COMPILE_ASSERT_MATCH(ui::VKEY_SLEEP, FWL_VKEY_Sleep); +COMPILE_ASSERT_MATCH(ui::VKEY_NUMPAD0, FWL_VKEY_NumPad0); +COMPILE_ASSERT_MATCH(ui::VKEY_NUMPAD1, FWL_VKEY_NumPad1); +COMPILE_ASSERT_MATCH(ui::VKEY_NUMPAD2, FWL_VKEY_NumPad2); +COMPILE_ASSERT_MATCH(ui::VKEY_NUMPAD3, FWL_VKEY_NumPad3); +COMPILE_ASSERT_MATCH(ui::VKEY_NUMPAD4, FWL_VKEY_NumPad4); +COMPILE_ASSERT_MATCH(ui::VKEY_NUMPAD5, FWL_VKEY_NumPad5); +COMPILE_ASSERT_MATCH(ui::VKEY_NUMPAD6, FWL_VKEY_NumPad6); +COMPILE_ASSERT_MATCH(ui::VKEY_NUMPAD7, FWL_VKEY_NumPad7); +COMPILE_ASSERT_MATCH(ui::VKEY_NUMPAD8, FWL_VKEY_NumPad8); +COMPILE_ASSERT_MATCH(ui::VKEY_NUMPAD9, FWL_VKEY_NumPad9); +COMPILE_ASSERT_MATCH(ui::VKEY_MULTIPLY, FWL_VKEY_Multiply); +COMPILE_ASSERT_MATCH(ui::VKEY_ADD, FWL_VKEY_Add); +COMPILE_ASSERT_MATCH(ui::VKEY_SEPARATOR, FWL_VKEY_Separator); +COMPILE_ASSERT_MATCH(ui::VKEY_SUBTRACT, FWL_VKEY_Subtract); +COMPILE_ASSERT_MATCH(ui::VKEY_DECIMAL, FWL_VKEY_Decimal); +COMPILE_ASSERT_MATCH(ui::VKEY_DIVIDE, FWL_VKEY_Divide); +COMPILE_ASSERT_MATCH(ui::VKEY_F1, FWL_VKEY_F1); +COMPILE_ASSERT_MATCH(ui::VKEY_F2, FWL_VKEY_F2); +COMPILE_ASSERT_MATCH(ui::VKEY_F3, FWL_VKEY_F3); +COMPILE_ASSERT_MATCH(ui::VKEY_F4, FWL_VKEY_F4); +COMPILE_ASSERT_MATCH(ui::VKEY_F5, FWL_VKEY_F5); +COMPILE_ASSERT_MATCH(ui::VKEY_F6, FWL_VKEY_F6); +COMPILE_ASSERT_MATCH(ui::VKEY_F7, FWL_VKEY_F7); +COMPILE_ASSERT_MATCH(ui::VKEY_F8, FWL_VKEY_F8); +COMPILE_ASSERT_MATCH(ui::VKEY_F9, FWL_VKEY_F9); +COMPILE_ASSERT_MATCH(ui::VKEY_F10, FWL_VKEY_F10); +COMPILE_ASSERT_MATCH(ui::VKEY_F11, FWL_VKEY_F11); +COMPILE_ASSERT_MATCH(ui::VKEY_F12, FWL_VKEY_F12); +COMPILE_ASSERT_MATCH(ui::VKEY_F13, FWL_VKEY_F13); +COMPILE_ASSERT_MATCH(ui::VKEY_F14, FWL_VKEY_F14); +COMPILE_ASSERT_MATCH(ui::VKEY_F15, FWL_VKEY_F15); +COMPILE_ASSERT_MATCH(ui::VKEY_F16, FWL_VKEY_F16); +COMPILE_ASSERT_MATCH(ui::VKEY_F17, FWL_VKEY_F17); +COMPILE_ASSERT_MATCH(ui::VKEY_F18, FWL_VKEY_F18); +COMPILE_ASSERT_MATCH(ui::VKEY_F19, FWL_VKEY_F19); +COMPILE_ASSERT_MATCH(ui::VKEY_F20, FWL_VKEY_F20); +COMPILE_ASSERT_MATCH(ui::VKEY_F21, FWL_VKEY_F21); +COMPILE_ASSERT_MATCH(ui::VKEY_F22, FWL_VKEY_F22); +COMPILE_ASSERT_MATCH(ui::VKEY_F23, FWL_VKEY_F23); +COMPILE_ASSERT_MATCH(ui::VKEY_F24, FWL_VKEY_F24); +COMPILE_ASSERT_MATCH(ui::VKEY_NUMLOCK, FWL_VKEY_NunLock); +COMPILE_ASSERT_MATCH(ui::VKEY_SCROLL, FWL_VKEY_Scroll); +COMPILE_ASSERT_MATCH(ui::VKEY_LSHIFT, FWL_VKEY_LShift); +COMPILE_ASSERT_MATCH(ui::VKEY_RSHIFT, FWL_VKEY_RShift); +COMPILE_ASSERT_MATCH(ui::VKEY_LCONTROL, FWL_VKEY_LControl); +COMPILE_ASSERT_MATCH(ui::VKEY_RCONTROL, FWL_VKEY_RControl); +COMPILE_ASSERT_MATCH(ui::VKEY_LMENU, FWL_VKEY_LMenu); +COMPILE_ASSERT_MATCH(ui::VKEY_RMENU, FWL_VKEY_RMenu); +COMPILE_ASSERT_MATCH(ui::VKEY_BROWSER_BACK, FWL_VKEY_BROWSER_Back); +COMPILE_ASSERT_MATCH(ui::VKEY_BROWSER_FORWARD, FWL_VKEY_BROWSER_Forward); +COMPILE_ASSERT_MATCH(ui::VKEY_BROWSER_REFRESH, FWL_VKEY_BROWSER_Refresh); +COMPILE_ASSERT_MATCH(ui::VKEY_BROWSER_STOP, FWL_VKEY_BROWSER_Stop); +COMPILE_ASSERT_MATCH(ui::VKEY_BROWSER_SEARCH, FWL_VKEY_BROWSER_Search); +COMPILE_ASSERT_MATCH(ui::VKEY_BROWSER_FAVORITES, FWL_VKEY_BROWSER_Favorites); +COMPILE_ASSERT_MATCH(ui::VKEY_BROWSER_HOME, FWL_VKEY_BROWSER_Home); +COMPILE_ASSERT_MATCH(ui::VKEY_VOLUME_MUTE, FWL_VKEY_VOLUME_Mute); +COMPILE_ASSERT_MATCH(ui::VKEY_VOLUME_DOWN, FWL_VKEY_VOLUME_Down); +COMPILE_ASSERT_MATCH(ui::VKEY_VOLUME_UP, FWL_VKEY_VOLUME_Up); +COMPILE_ASSERT_MATCH(ui::VKEY_MEDIA_NEXT_TRACK, FWL_VKEY_MEDIA_NEXT_Track); +COMPILE_ASSERT_MATCH(ui::VKEY_MEDIA_PREV_TRACK, FWL_VKEY_MEDIA_PREV_Track); +COMPILE_ASSERT_MATCH(ui::VKEY_MEDIA_STOP, FWL_VKEY_MEDIA_Stop); +COMPILE_ASSERT_MATCH(ui::VKEY_MEDIA_PLAY_PAUSE, FWL_VKEY_MEDIA_PLAY_Pause); +COMPILE_ASSERT_MATCH(ui::VKEY_MEDIA_LAUNCH_MAIL, FWL_VKEY_MEDIA_LAUNCH_Mail); +COMPILE_ASSERT_MATCH(ui::VKEY_MEDIA_LAUNCH_MEDIA_SELECT, + FWL_VKEY_MEDIA_LAUNCH_MEDIA_Select); +COMPILE_ASSERT_MATCH(ui::VKEY_MEDIA_LAUNCH_APP1, FWL_VKEY_MEDIA_LAUNCH_APP1); +COMPILE_ASSERT_MATCH(ui::VKEY_MEDIA_LAUNCH_APP2, FWL_VKEY_MEDIA_LAUNCH_APP2); +COMPILE_ASSERT_MATCH(ui::VKEY_OEM_1, FWL_VKEY_OEM_1); +COMPILE_ASSERT_MATCH(ui::VKEY_OEM_PLUS, FWL_VKEY_OEM_Plus); +COMPILE_ASSERT_MATCH(ui::VKEY_OEM_COMMA, FWL_VKEY_OEM_Comma); +COMPILE_ASSERT_MATCH(ui::VKEY_OEM_MINUS, FWL_VKEY_OEM_Minus); +COMPILE_ASSERT_MATCH(ui::VKEY_OEM_PERIOD, FWL_VKEY_OEM_Period); +COMPILE_ASSERT_MATCH(ui::VKEY_OEM_2, FWL_VKEY_OEM_2); +COMPILE_ASSERT_MATCH(ui::VKEY_OEM_3, FWL_VKEY_OEM_3); +COMPILE_ASSERT_MATCH(ui::VKEY_OEM_4, FWL_VKEY_OEM_4); +COMPILE_ASSERT_MATCH(ui::VKEY_OEM_5, FWL_VKEY_OEM_5); +COMPILE_ASSERT_MATCH(ui::VKEY_OEM_6, FWL_VKEY_OEM_6); +COMPILE_ASSERT_MATCH(ui::VKEY_OEM_7, FWL_VKEY_OEM_7); +COMPILE_ASSERT_MATCH(ui::VKEY_OEM_8, FWL_VKEY_OEM_8); +COMPILE_ASSERT_MATCH(ui::VKEY_OEM_102, FWL_VKEY_OEM_102); +COMPILE_ASSERT_MATCH(ui::VKEY_PROCESSKEY, FWL_VKEY_ProcessKey); +COMPILE_ASSERT_MATCH(ui::VKEY_PACKET, FWL_VKEY_Packet); +COMPILE_ASSERT_MATCH(ui::VKEY_ATTN, FWL_VKEY_Attn); +COMPILE_ASSERT_MATCH(ui::VKEY_CRSEL, FWL_VKEY_Crsel); +COMPILE_ASSERT_MATCH(ui::VKEY_EXSEL, FWL_VKEY_Exsel); +COMPILE_ASSERT_MATCH(ui::VKEY_EREOF, FWL_VKEY_Ereof); +COMPILE_ASSERT_MATCH(ui::VKEY_PLAY, FWL_VKEY_Play); +COMPILE_ASSERT_MATCH(ui::VKEY_ZOOM, FWL_VKEY_Zoom); +COMPILE_ASSERT_MATCH(ui::VKEY_NONAME, FWL_VKEY_NoName); +COMPILE_ASSERT_MATCH(ui::VKEY_PA1, FWL_VKEY_PA1); +COMPILE_ASSERT_MATCH(ui::VKEY_OEM_CLEAR, FWL_VKEY_OEM_Clear); +COMPILE_ASSERT_MATCH(ui::VKEY_UNKNOWN, FWL_VKEY_Unknown); + +COMPILE_ASSERT_MATCH(PP_PRIVATEFONTCHARSET_ANSI, FXFONT_ANSI_CHARSET); +COMPILE_ASSERT_MATCH(PP_PRIVATEFONTCHARSET_DEFAULT, FXFONT_DEFAULT_CHARSET); +COMPILE_ASSERT_MATCH(PP_PRIVATEFONTCHARSET_SYMBOL, FXFONT_SYMBOL_CHARSET); +COMPILE_ASSERT_MATCH(PP_PRIVATEFONTCHARSET_SHIFTJIS, FXFONT_SHIFTJIS_CHARSET); +COMPILE_ASSERT_MATCH(PP_PRIVATEFONTCHARSET_HANGUL, FXFONT_HANGEUL_CHARSET); +COMPILE_ASSERT_MATCH(PP_PRIVATEFONTCHARSET_GB2312, FXFONT_GB2312_CHARSET); +COMPILE_ASSERT_MATCH(PP_PRIVATEFONTCHARSET_CHINESEBIG5, + FXFONT_CHINESEBIG5_CHARSET); diff --git a/xfa_test/pdf/pdfium/pdfium_engine.cc b/xfa_test/pdf/pdfium/pdfium_engine.cc new file mode 100644 index 0000000000..44e7052bf3 --- /dev/null +++ b/xfa_test/pdf/pdfium/pdfium_engine.cc @@ -0,0 +1,3884 @@ +// Copyright (c) 2012 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#include "pdf/pdfium/pdfium_engine.h" + +#include + +#include "base/json/json_writer.h" +#include "base/logging.h" +#include "base/memory/scoped_ptr.h" +#include "base/stl_util.h" +#include "base/strings/string_number_conversions.h" +#include "base/strings/string_piece.h" +#include "base/strings/string_util.h" +#include "base/strings/utf_string_conversions.h" +#include "base/values.h" +#include "pdf/draw_utils.h" +#include "pdf/pdfium/pdfium_mem_buffer_file_read.h" +#include "pdf/pdfium/pdfium_mem_buffer_file_write.h" +#include "ppapi/c/pp_errors.h" +#include "ppapi/c/pp_input_event.h" +#include "ppapi/c/ppb_core.h" +#include "ppapi/c/private/ppb_pdf.h" +#include "ppapi/cpp/dev/memory_dev.h" +#include "ppapi/cpp/input_event.h" +#include "ppapi/cpp/instance.h" +#include "ppapi/cpp/module.h" +#include "ppapi/cpp/private/pdf.h" +#include "ppapi/cpp/trusted/browser_font_trusted.h" +#include "ppapi/cpp/url_response_info.h" +#include "ppapi/cpp/var.h" +#include "third_party/pdfium/fpdfsdk/include/fpdf_ext.h" +#include "third_party/pdfium/fpdfsdk/include/fpdf_flatten.h" +#include "third_party/pdfium/fpdfsdk/include/fpdf_searchex.h" +#include "third_party/pdfium/fpdfsdk/include/fpdf_sysfontinfo.h" +#include "third_party/pdfium/fpdfsdk/include/fpdf_transformpage.h" +#include "third_party/pdfium/fpdfsdk/include/fpdfedit.h" +#include "third_party/pdfium/fpdfsdk/include/fpdfoom.h" +#include "third_party/pdfium/fpdfsdk/include/fpdfppo.h" +#include "third_party/pdfium/fpdfsdk/include/fpdfsave.h" +#include "third_party/pdfium/fpdfsdk/include/pdfwindow/PDFWindow.h" +#include "third_party/pdfium/fpdfsdk/include/pdfwindow/PWL_FontMap.h" +#include "ui/events/keycodes/keyboard_codes.h" + +namespace chrome_pdf { + +namespace { + +#define kPageShadowTop 3 +#define kPageShadowBottom 7 +#define kPageShadowLeft 5 +#define kPageShadowRight 5 + +#define kPageSeparatorThickness 4 +#define kHighlightColorR 153 +#define kHighlightColorG 193 +#define kHighlightColorB 218 + +const uint32 kPendingPageColor = 0xFFEEEEEE; + +#define kFormHighlightColor 0xFFE4DD +#define kFormHighlightAlpha 100 + +#define kMaxPasswordTries 3 + +// See Table 3.20 in +// http://www.adobe.com/devnet/acrobat/pdfs/pdf_reference_1-7.pdf +#define kPDFPermissionPrintLowQualityMask 1 << 2 +#define kPDFPermissionPrintHighQualityMask 1 << 11 +#define kPDFPermissionCopyMask 1 << 4 +#define kPDFPermissionCopyAccessibleMask 1 << 9 + +#define kLoadingTextVerticalOffset 50 + +// The maximum amount of time we'll spend doing a paint before we give back +// control of the thread. +#define kMaxProgressivePaintTimeMs 50 + +// The maximum amount of time we'll spend doing the first paint. This is less +// than the above to keep things smooth if the user is scrolling quickly. We +// try painting a little because with accelerated compositing, we get flushes +// only every 16 ms. If we were to wait until the next flush to paint the rest +// of the pdf, we would never get to draw the pdf and would only draw the +// scrollbars. This value is picked to give enough time for gpu related code to +// do its thing and still fit within the timelimit for 60Hz. For the +// non-composited case, this doesn't make things worse since we're still +// painting the scrollbars > 60 Hz. +#define kMaxInitialProgressivePaintTimeMs 10 + +// Copied from printing/units.cc because we don't want to depend on printing +// since it brings in libpng which causes duplicate symbols with PDFium. +const int kPointsPerInch = 72; +const int kPixelsPerInch = 96; + +struct ClipBox { + float left; + float right; + float top; + float bottom; +}; + +int ConvertUnit(int value, int old_unit, int new_unit) { + // With integer arithmetic, to divide a value with correct rounding, you need + // to add half of the divisor value to the dividend value. You need to do the + // reverse with negative number. + if (value >= 0) { + return ((value * new_unit) + (old_unit / 2)) / old_unit; + } else { + return ((value * new_unit) - (old_unit / 2)) / old_unit; + } +} + +std::vector GetPageNumbersFromPrintPageNumberRange( + const PP_PrintPageNumberRange_Dev* page_ranges, + uint32_t page_range_count) { + std::vector page_numbers; + for (uint32_t index = 0; index < page_range_count; ++index) { + for (uint32_t page_number = page_ranges[index].first_page_number; + page_number <= page_ranges[index].last_page_number; ++page_number) { + page_numbers.push_back(page_number); + } + } + return page_numbers; +} + +#if defined(OS_LINUX) + +PP_Instance g_last_instance_id; + +struct PDFFontSubstitution { + const char* pdf_name; + const char* face; + bool bold; + bool italic; +}; + +PP_BrowserFont_Trusted_Weight WeightToBrowserFontTrustedWeight(int weight) { + COMPILE_ASSERT(PP_BROWSERFONT_TRUSTED_WEIGHT_100 == 0, + PP_BrowserFont_Trusted_Weight_Min); + COMPILE_ASSERT(PP_BROWSERFONT_TRUSTED_WEIGHT_900 == 8, + PP_BrowserFont_Trusted_Weight_Max); + const int kMinimumWeight = 100; + const int kMaximumWeight = 900; + int normalized_weight = + std::min(std::max(weight, kMinimumWeight), kMaximumWeight); + normalized_weight = (normalized_weight / 100) - 1; + return static_cast(normalized_weight); +} + +// This list is for CPWL_FontMap::GetDefaultFontByCharset(). +// We pretend to have these font natively and let the browser (or underlying +// fontconfig) to pick the proper font on the system. +void EnumFonts(struct _FPDF_SYSFONTINFO* sysfontinfo, void* mapper) { + FPDF_AddInstalledFont(mapper, "Arial", FXFONT_DEFAULT_CHARSET); + + int i = 0; + while (CPWL_FontMap::defaultTTFMap[i].charset != -1) { + FPDF_AddInstalledFont(mapper, + CPWL_FontMap::defaultTTFMap[i].fontname, + CPWL_FontMap::defaultTTFMap[i].charset); + ++i; + } +} + +const PDFFontSubstitution PDFFontSubstitutions[] = { + {"Courier", "Courier New", false, false}, + {"Courier-Bold", "Courier New", true, false}, + {"Courier-BoldOblique", "Courier New", true, true}, + {"Courier-Oblique", "Courier New", false, true}, + {"Helvetica", "Arial", false, false}, + {"Helvetica-Bold", "Arial", true, false}, + {"Helvetica-BoldOblique", "Arial", true, true}, + {"Helvetica-Oblique", "Arial", false, true}, + {"Times-Roman", "Times New Roman", false, false}, + {"Times-Bold", "Times New Roman", true, false}, + {"Times-BoldItalic", "Times New Roman", true, true}, + {"Times-Italic", "Times New Roman", false, true}, + + // MS P?(Mincho|Gothic) are the most notable fonts in Japanese PDF files + // without embedding the glyphs. Sometimes the font names are encoded + // in Japanese Windows's locale (CP932/Shift_JIS) without space. + // Most Linux systems don't have the exact font, but for outsourcing + // fontconfig to find substitutable font in the system, we pass ASCII + // font names to it. + {"MS-PGothic", "MS PGothic", false, false}, + {"MS-Gothic", "MS Gothic", false, false}, + {"MS-PMincho", "MS PMincho", false, false}, + {"MS-Mincho", "MS Mincho", false, false}, + // MS PGothic in Shift_JIS encoding. + {"\x82\x6C\x82\x72\x82\x6F\x83\x53\x83\x56\x83\x62\x83\x4E", + "MS PGothic", false, false}, + // MS Gothic in Shift_JIS encoding. + {"\x82\x6C\x82\x72\x83\x53\x83\x56\x83\x62\x83\x4E", + "MS Gothic", false, false}, + // MS PMincho in Shift_JIS encoding. + {"\x82\x6C\x82\x72\x82\x6F\x96\xBE\x92\xA9", + "MS PMincho", false, false}, + // MS Mincho in Shift_JIS encoding. + {"\x82\x6C\x82\x72\x96\xBE\x92\xA9", + "MS Mincho", false, false}, +}; + +void* MapFont(struct _FPDF_SYSFONTINFO*, int weight, int italic, + int charset, int pitch_family, const char* face, int* exact) { + // Do not attempt to map fonts if pepper is not initialized (for privet local + // printing). + // TODO(noamsml): Real font substitution (http://crbug.com/391978) + if (!pp::Module::Get()) + return NULL; + + pp::BrowserFontDescription description; + + // Pretend the system does not have the Symbol font to force a fallback to + // the built in Symbol font in CFX_FontMapper::FindSubstFont(). + if (strcmp(face, "Symbol") == 0) + return NULL; + + if (pitch_family & FXFONT_FF_FIXEDPITCH) { + description.set_family(PP_BROWSERFONT_TRUSTED_FAMILY_MONOSPACE); + } else if (pitch_family & FXFONT_FF_ROMAN) { + description.set_family(PP_BROWSERFONT_TRUSTED_FAMILY_SERIF); + } + + // Map from the standard PDF fonts to TrueType font names. + size_t i; + for (i = 0; i < arraysize(PDFFontSubstitutions); ++i) { + if (strcmp(face, PDFFontSubstitutions[i].pdf_name) == 0) { + description.set_face(PDFFontSubstitutions[i].face); + if (PDFFontSubstitutions[i].bold) + description.set_weight(PP_BROWSERFONT_TRUSTED_WEIGHT_BOLD); + if (PDFFontSubstitutions[i].italic) + description.set_italic(true); + break; + } + } + + if (i == arraysize(PDFFontSubstitutions)) { + // TODO(kochi): Pass the face in UTF-8. If face is not encoded in UTF-8, + // convert to UTF-8 before passing. + description.set_face(face); + + description.set_weight(WeightToBrowserFontTrustedWeight(weight)); + description.set_italic(italic > 0); + } + + if (!pp::PDF::IsAvailable()) { + NOTREACHED(); + return NULL; + } + + PP_Resource font_resource = pp::PDF::GetFontFileWithFallback( + pp::InstanceHandle(g_last_instance_id), + &description.pp_font_description(), + static_cast(charset)); + long res_id = font_resource; + return reinterpret_cast(res_id); +} + +unsigned long GetFontData(struct _FPDF_SYSFONTINFO*, void* font_id, + unsigned int table, unsigned char* buffer, + unsigned long buf_size) { + if (!pp::PDF::IsAvailable()) { + NOTREACHED(); + return 0; + } + + uint32_t size = buf_size; + long res_id = reinterpret_cast(font_id); + if (!pp::PDF::GetFontTableForPrivateFontFile(res_id, table, buffer, &size)) + return 0; + return size; +} + +void DeleteFont(struct _FPDF_SYSFONTINFO*, void* font_id) { + long res_id = reinterpret_cast(font_id); + pp::Module::Get()->core()->ReleaseResource(res_id); +} + +FPDF_SYSFONTINFO g_font_info = { + 1, + 0, + EnumFonts, + MapFont, + 0, + GetFontData, + 0, + 0, + DeleteFont +}; +#endif // defined(OS_LINUX) + +void OOM_Handler(_OOM_INFO*) { + // Kill the process. This is important for security, since the code doesn't + // NULL-check many memory allocations. If a malloc fails, returns NULL, and + // the buffer is then used, it provides a handy mapping of memory starting at + // address 0 for an attacker to utilize. + abort(); +} + +OOM_INFO g_oom_info = { + 1, + OOM_Handler +}; + +PDFiumEngine* g_engine_for_unsupported; + +void Unsupported_Handler(UNSUPPORT_INFO*, int type) { + if (!g_engine_for_unsupported) { + NOTREACHED(); + return; + } + + g_engine_for_unsupported->UnsupportedFeature(type); +} + +UNSUPPORT_INFO g_unsuppored_info = { + 1, + Unsupported_Handler +}; + +// Set the destination page size and content area in points based on source +// page rotation and orientation. +// +// |rotated| True if source page is rotated 90 degree or 270 degree. +// |is_src_page_landscape| is true if the source page orientation is landscape. +// |page_size| has the actual destination page size in points. +// |content_rect| has the actual destination page printable area values in +// points. +void SetPageSizeAndContentRect(bool rotated, + bool is_src_page_landscape, + pp::Size* page_size, + pp::Rect* content_rect) { + bool is_dst_page_landscape = page_size->width() > page_size->height(); + bool page_orientation_mismatched = is_src_page_landscape != + is_dst_page_landscape; + bool rotate_dst_page = rotated ^ page_orientation_mismatched; + if (rotate_dst_page) { + page_size->SetSize(page_size->height(), page_size->width()); + content_rect->SetRect(content_rect->y(), content_rect->x(), + content_rect->height(), content_rect->width()); + } +} + +// Calculate the scale factor between |content_rect| and a page of size +// |src_width| x |src_height|. +// +// |scale_to_fit| is true, if we need to calculate the scale factor. +// |content_rect| specifies the printable area of the destination page, with +// origin at left-bottom. Values are in points. +// |src_width| specifies the source page width in points. +// |src_height| specifies the source page height in points. +// |rotated| True if source page is rotated 90 degree or 270 degree. +double CalculateScaleFactor(bool scale_to_fit, + const pp::Rect& content_rect, + double src_width, double src_height, bool rotated) { + if (!scale_to_fit || src_width == 0 || src_height == 0) + return 1.0; + + double actual_source_page_width = rotated ? src_height : src_width; + double actual_source_page_height = rotated ? src_width : src_height; + double ratio_x = static_cast(content_rect.width()) / + actual_source_page_width; + double ratio_y = static_cast(content_rect.height()) / + actual_source_page_height; + return std::min(ratio_x, ratio_y); +} + +// Compute source clip box boundaries based on the crop box / media box of +// source page and scale factor. +// +// |page| Handle to the source page. Returned by FPDF_LoadPage function. +// |scale_factor| specifies the scale factor that should be applied to source +// clip box boundaries. +// |rotated| True if source page is rotated 90 degree or 270 degree. +// |clip_box| out param to hold the computed source clip box values. +void CalculateClipBoxBoundary(FPDF_PAGE page, double scale_factor, bool rotated, + ClipBox* clip_box) { + if (!FPDFPage_GetCropBox(page, &clip_box->left, &clip_box->bottom, + &clip_box->right, &clip_box->top)) { + if (!FPDFPage_GetMediaBox(page, &clip_box->left, &clip_box->bottom, + &clip_box->right, &clip_box->top)) { + // Make the default size to be letter size (8.5" X 11"). We are just + // following the PDFium way of handling these corner cases. PDFium always + // consider US-Letter as the default page size. + float paper_width = 612; + float paper_height = 792; + clip_box->left = 0; + clip_box->bottom = 0; + clip_box->right = rotated ? paper_height : paper_width; + clip_box->top = rotated ? paper_width : paper_height; + } + } + clip_box->left *= scale_factor; + clip_box->right *= scale_factor; + clip_box->bottom *= scale_factor; + clip_box->top *= scale_factor; +} + +// Calculate the clip box translation offset for a page that does need to be +// scaled. All parameters are in points. +// +// |content_rect| specifies the printable area of the destination page, with +// origin at left-bottom. +// |source_clip_box| specifies the source clip box positions, relative to +// origin at left-bottom. +// |offset_x| and |offset_y| will contain the final translation offsets for the +// source clip box, relative to origin at left-bottom. +void CalculateScaledClipBoxOffset(const pp::Rect& content_rect, + const ClipBox& source_clip_box, + double* offset_x, double* offset_y) { + const float clip_box_width = source_clip_box.right - source_clip_box.left; + const float clip_box_height = source_clip_box.top - source_clip_box.bottom; + + // Center the intended clip region to real clip region. + *offset_x = (content_rect.width() - clip_box_width) / 2 + content_rect.x() - + source_clip_box.left; + *offset_y = (content_rect.height() - clip_box_height) / 2 + content_rect.y() - + source_clip_box.bottom; +} + +// Calculate the clip box offset for a page that does not need to be scaled. +// All parameters are in points. +// +// |content_rect| specifies the printable area of the destination page, with +// origin at left-bottom. +// |rotation| specifies the source page rotation values which are N / 90 +// degrees. +// |page_width| specifies the screen destination page width. +// |page_height| specifies the screen destination page height. +// |source_clip_box| specifies the source clip box positions, relative to origin +// at left-bottom. +// |offset_x| and |offset_y| will contain the final translation offsets for the +// source clip box, relative to origin at left-bottom. +void CalculateNonScaledClipBoxOffset(const pp::Rect& content_rect, int rotation, + int page_width, int page_height, + const ClipBox& source_clip_box, + double* offset_x, double* offset_y) { + // Align the intended clip region to left-top corner of real clip region. + switch (rotation) { + case 0: + *offset_x = -1 * source_clip_box.left; + *offset_y = page_height - source_clip_box.top; + break; + case 1: + *offset_x = 0; + *offset_y = -1 * source_clip_box.bottom; + break; + case 2: + *offset_x = page_width - source_clip_box.right; + *offset_y = 0; + break; + case 3: + *offset_x = page_height - source_clip_box.right; + *offset_y = page_width - source_clip_box.top; + break; + default: + NOTREACHED(); + break; + } +} + +// This formats a string with special 0xfffe end-of-line hyphens the same way +// as Adobe Reader. When a hyphen is encountered, the next non-CR/LF whitespace +// becomes CR+LF and the hyphen is erased. If there is no whitespace between +// two hyphens, the latter hyphen is erased and ignored. +void FormatStringWithHyphens(base::string16* text) { + // First pass marks all the hyphen positions. + struct HyphenPosition { + HyphenPosition() : position(0), next_whitespace_position(0) {} + size_t position; + size_t next_whitespace_position; // 0 for none + }; + std::vector hyphen_positions; + HyphenPosition current_hyphen_position; + bool current_hyphen_position_is_valid = false; + const base::char16 kPdfiumHyphenEOL = 0xfffe; + + for (size_t i = 0; i < text->size(); ++i) { + const base::char16& current_char = (*text)[i]; + if (current_char == kPdfiumHyphenEOL) { + if (current_hyphen_position_is_valid) + hyphen_positions.push_back(current_hyphen_position); + current_hyphen_position = HyphenPosition(); + current_hyphen_position.position = i; + current_hyphen_position_is_valid = true; + } else if (IsWhitespace(current_char)) { + if (current_hyphen_position_is_valid) { + if (current_char != L'\r' && current_char != L'\n') + current_hyphen_position.next_whitespace_position = i; + hyphen_positions.push_back(current_hyphen_position); + current_hyphen_position_is_valid = false; + } + } + } + if (current_hyphen_position_is_valid) + hyphen_positions.push_back(current_hyphen_position); + + // With all the hyphen positions, do the search and replace. + while (!hyphen_positions.empty()) { + static const base::char16 kCr[] = {L'\r', L'\0'}; + const HyphenPosition& position = hyphen_positions.back(); + if (position.next_whitespace_position != 0) { + (*text)[position.next_whitespace_position] = L'\n'; + text->insert(position.next_whitespace_position, kCr); + } + text->erase(position.position, 1); + hyphen_positions.pop_back(); + } + + // Adobe Reader also get rid of trailing spaces right before a CRLF. + static const base::char16 kSpaceCrCn[] = {L' ', L'\r', L'\n', L'\0'}; + static const base::char16 kCrCn[] = {L'\r', L'\n', L'\0'}; + ReplaceSubstringsAfterOffset(text, 0, kSpaceCrCn, kCrCn); +} + +// Replace CR/LF with just LF on POSIX. +void FormatStringForOS(base::string16* text) { +#if defined(OS_POSIX) + static const base::char16 kCr[] = {L'\r', L'\0'}; + static const base::char16 kBlank[] = {L'\0'}; + base::ReplaceChars(*text, kCr, kBlank, text); +#elif defined(OS_WIN) + // Do nothing +#else + NOTIMPLEMENTED(); +#endif +} + +} // namespace + +bool InitializeSDK(void* data) { + FPDF_InitLibrary(data); + +#if defined(OS_LINUX) + // Font loading doesn't work in the renderer sandbox in Linux. + FPDF_SetSystemFontInfo(&g_font_info); +#endif + + FSDK_SetOOMHandler(&g_oom_info); + FSDK_SetUnSpObjProcessHandler(&g_unsuppored_info); + + return true; +} + +void ShutdownSDK() { + FPDF_DestroyLibrary(); +} + +PDFEngine* PDFEngine::Create(PDFEngine::Client* client) { + return new PDFiumEngine(client); +} + +PDFiumEngine::PDFiumEngine(PDFEngine::Client* client) + : client_(client), + current_zoom_(1.0), + current_rotation_(0), + doc_loader_(this), + password_tries_remaining_(0), + doc_(NULL), + form_(NULL), + defer_page_unload_(false), + selecting_(false), + mouse_down_state_(PDFiumPage::NONSELECTABLE_AREA, + PDFiumPage::LinkTarget()), + next_page_to_search_(-1), + last_page_to_search_(-1), + last_character_index_to_search_(-1), + current_find_index_(-1), + resume_find_index_(-1), + permissions_(0), + fpdf_availability_(NULL), + next_timer_id_(0), + last_page_mouse_down_(-1), + first_visible_page_(-1), + most_visible_page_(-1), + called_do_document_action_(false), + render_grayscale_(false), + progressive_paint_timeout_(0), + getting_password_(false) { + find_factory_.Initialize(this); + password_factory_.Initialize(this); + + file_access_.m_FileLen = 0; + file_access_.m_GetBlock = &GetBlock; + file_access_.m_Param = &doc_loader_; + + file_availability_.version = 1; + file_availability_.IsDataAvail = &IsDataAvail; + file_availability_.loader = &doc_loader_; + + download_hints_.version = 1; + download_hints_.AddSegment = &AddSegment; + download_hints_.loader = &doc_loader_; + + // Initialize FPDF_FORMFILLINFO member variables. Deriving from this struct + // allows the static callbacks to be able to cast the FPDF_FORMFILLINFO in + // callbacks to ourself instead of maintaining a map of them to + // PDFiumEngine. + FPDF_FORMFILLINFO::version = 1; + FPDF_FORMFILLINFO::m_pJsPlatform = this; + FPDF_FORMFILLINFO::Release = NULL; + FPDF_FORMFILLINFO::FFI_Invalidate = Form_Invalidate; + FPDF_FORMFILLINFO::FFI_OutputSelectedRect = Form_OutputSelectedRect; + FPDF_FORMFILLINFO::FFI_SetCursor = Form_SetCursor; + FPDF_FORMFILLINFO::FFI_SetTimer = Form_SetTimer; + FPDF_FORMFILLINFO::FFI_KillTimer = Form_KillTimer; + FPDF_FORMFILLINFO::FFI_GetLocalTime = Form_GetLocalTime; + FPDF_FORMFILLINFO::FFI_OnChange = Form_OnChange; + FPDF_FORMFILLINFO::FFI_GetPage = Form_GetPage; + FPDF_FORMFILLINFO::FFI_GetCurrentPage = Form_GetCurrentPage; + FPDF_FORMFILLINFO::FFI_GetRotation = Form_GetRotation; + FPDF_FORMFILLINFO::FFI_ExecuteNamedAction = Form_ExecuteNamedAction; + FPDF_FORMFILLINFO::FFI_SetTextFieldFocus = Form_SetTextFieldFocus; + FPDF_FORMFILLINFO::FFI_DoURIAction = Form_DoURIAction; + FPDF_FORMFILLINFO::FFI_DoGoToAction = Form_DoGoToAction; +#ifdef _TEST_XFA + FPDF_FORMFILLINFO::FFI_EmailTo = Form_EmailTo; + FPDF_FORMFILLINFO::FFI_DisplayCaret = Form_DisplayCaret; + //FPDF_FORMFILLINFO::FFI_GetCurDocumentIndex = Form_GetCurDocumentIndex; + //FPDF_FORMFILLINFO::FFI_GetDocumentCount = Form_GetDocumentCount; + FPDF_FORMFILLINFO::FFI_SetCurrentPage = Form_SetCurrentPage; + FPDF_FORMFILLINFO::FFI_GetCurrentPageIndex = Form_GetCurrentPageIndex; + FPDF_FORMFILLINFO::FFI_GetPageViewRect = Form_GetPageViewRect; + FPDF_FORMFILLINFO::FFI_GetPlatform = Form_GetPlatform; + FPDF_FORMFILLINFO::FFI_PopupMenu = Form_PopupMenu; + FPDF_FORMFILLINFO::FFI_PostRequestURL = Form_PostRequestURL; + FPDF_FORMFILLINFO::FFI_PutRequestURL = Form_PutRequestURL; +// FPDF_FORMFILLINFO::FFI_ShowFileDialog = Form_ShowFileDialog; + FPDF_FORMFILLINFO::FFI_UploadTo = Form_UploadTo; + FPDF_FORMFILLINFO::FFI_DownloadFromURL = Form_DownloadFromURL; + // FPDF_FORMFILLINFO::FFI_GetFilePath = MyForm_GetFilePath; + FPDF_FORMFILLINFO::FFI_OpenFile = Form_OpenFile; + FPDF_FORMFILLINFO::FFI_GotoURL = Form_GotoURL; + FPDF_FORMFILLINFO::FFI_GetLanguage = Form_GetLanguage; +#endif // _TEST_XFA + IPDF_JSPLATFORM::version = 1; + IPDF_JSPLATFORM::app_alert = Form_Alert; + IPDF_JSPLATFORM::app_beep = Form_Beep; + IPDF_JSPLATFORM::app_response = Form_Response; + IPDF_JSPLATFORM::Doc_getFilePath = Form_GetFilePath; + IPDF_JSPLATFORM::Doc_mail = Form_Mail; + IPDF_JSPLATFORM::Doc_print = Form_Print; + IPDF_JSPLATFORM::Doc_submitForm = Form_SubmitForm; + IPDF_JSPLATFORM::Doc_gotoPage = Form_GotoPage; + IPDF_JSPLATFORM::Field_browse = Form_Browse; + + IFSDK_PAUSE::version = 1; + IFSDK_PAUSE::user = NULL; + IFSDK_PAUSE::NeedToPauseNow = Pause_NeedToPauseNow; +} + +PDFiumEngine::~PDFiumEngine() { + for (size_t i = 0; i < pages_.size(); ++i) + pages_[i]->Unload(); + + if (doc_) { + if (form_) { + FORM_DoDocumentAAction(form_, FPDFDOC_AACTION_WC); + } + FPDF_CloseDocument(doc_); + if (form_) { + FPDFDOC_ExitFormFillEnviroument(form_); + } + } + + if (fpdf_availability_) + FPDFAvail_Destroy(fpdf_availability_); + + STLDeleteElements(&pages_); +} + +#ifdef _TEST_XFA + +typedef struct _FPDF_FILE { + FPDF_FILEHANDLER fileHandler; + FILE* file; +}FPDF_FILE; + + +void Sample_Release(FPDF_LPVOID clientData) { + if (!clientData) return; + + fclose(((FPDF_FILE*)clientData)->file); + delete ((FPDF_FILE*)clientData); +} + +FPDF_DWORD Sample_GetSize(FPDF_LPVOID clientData) { + if (!clientData) return 0; + + long curPos = ftell(((FPDF_FILE*)clientData)->file); + fseek(((FPDF_FILE*)clientData)->file, 0, SEEK_END); + long size = ftell(((FPDF_FILE*)clientData)->file); + fseek(((FPDF_FILE*)clientData)->file, curPos, SEEK_SET); + + return (FPDF_DWORD)size; +} + +FPDF_RESULT Sample_ReadBlock(FPDF_LPVOID clientData, FPDF_DWORD offset, FPDF_LPVOID buffer, FPDF_DWORD size) { + if (!clientData) return -1; + + fseek(((FPDF_FILE*)clientData)->file, (long)offset, SEEK_SET); + size_t readSize = fread(buffer, 1, size, ((FPDF_FILE*)clientData)->file); + return readSize == size ? 0 : -1; +} + +FPDF_RESULT Sample_WriteBlock(FPDF_LPVOID clientData, FPDF_DWORD offset, FPDF_LPCVOID buffer, FPDF_DWORD size) { + if (!clientData) return -1; + + fseek(((FPDF_FILE*)clientData)->file, (long)offset, SEEK_SET); + //Write data + size_t writeSize = fwrite(buffer, 1, size, ((FPDF_FILE*)clientData)->file); + return writeSize == size ? 0 : -1; +} + +FPDF_RESULT Sample_Flush(FPDF_LPVOID clientData) { + if (!clientData) return -1; + + //Flush file + fflush(((FPDF_FILE*)clientData)->file); + + return 0; +} + +FPDF_RESULT Sample_Truncate(FPDF_LPVOID clientData, FPDF_DWORD size) { + return 0; +} + + +void PDFiumEngine::Form_EmailTo(FPDF_FORMFILLINFO* pThis, + FPDF_FILEHANDLER* fileHandler, + FPDF_WIDESTRING to, + FPDF_WIDESTRING subject, + FPDF_WIDESTRING cc, + FPDF_WIDESTRING bcc, + FPDF_WIDESTRING message) { + std::string to_str = + base::UTF16ToUTF8(reinterpret_cast(to)); + std::string subject_str = + base::UTF16ToUTF8(reinterpret_cast(subject)); + std::string cc_str = + base::UTF16ToUTF8(reinterpret_cast(cc)); + std::string bcc_str = + base::UTF16ToUTF8(reinterpret_cast(bcc)); + std::string message_str = + base::UTF16ToUTF8(reinterpret_cast(message)); + + PDFiumEngine* engine = static_cast(pThis); + engine->client_->Email(to_str, cc_str, bcc_str, subject_str, message_str); +} + +void PDFiumEngine::Form_DisplayCaret(FPDF_FORMFILLINFO* pThis, + FPDF_PAGE page, FPDF_BOOL bVisible, + double left, double top, double right, double bottom) { + PDFiumEngine* engine = static_cast(pThis); + engine->client_->UpdateCursor(PP_CURSORTYPE_IBEAM); + std::vector tickmarks; + pp::Rect rect(left, top, right, bottom); + tickmarks.push_back(rect); + engine->client_->UpdateTickMarks(tickmarks); +} + +//int PDFiumEngine::Form_GetCurDocumentIndex(FPDF_FORMFILLINFO* pThis) { +// PDFiumEngine* engine = static_cast(pThis); +// engine->client_->RunJSCmd("alert(\"GetCurDocumentIndex\")"); +// return 0; +//} +// +//int PDFiumEngine::Form_GetDocumentCount(FPDF_FORMFILLINFO* pThis) { +// PDFiumEngine* engine = static_cast(pThis); +// engine->client_->RunJSCmd("alert(\"GetCurDocumentCount\")"); +// return 0; +//} + +void PDFiumEngine::Form_SetCurrentPage(FPDF_FORMFILLINFO* pThis, FPDF_DOCUMENT document, int iCurPage) { + PDFiumEngine* engine = static_cast(pThis); + pp::Rect pageViewRect = engine->GetPageContentsRect(iCurPage); + engine->ScrolledToYPosition(pageViewRect.height()); + pp::Point pos(1, pageViewRect.height()); + engine->SetScrollPosition(pos); +} + +int PDFiumEngine::Form_GetCurrentPageIndex(FPDF_FORMFILLINFO* pThis, FPDF_DOCUMENT document) { + //int pageCount = FPDF_GetPageCount(document); + int pageIndex = -1; + PDFiumEngine* engine = static_cast(pThis); + pageIndex = engine->GetMostVisiblePage(); + return pageIndex; +} + +void PDFiumEngine::Form_GetPageViewRect(FPDF_FORMFILLINFO* pThis, + FPDF_PAGE page, + double* left, double* top, double* right, double* bottom) { + PDFiumEngine* engine = static_cast(pThis); + int page_index = engine->GetMostVisiblePage(); + pp::Rect pageViewRect = engine->GetPageContentsRect(page_index); + + *left = pageViewRect.x(); + *right = pageViewRect.width() + pageViewRect.x(); + *top = pageViewRect.y(); + *bottom = pageViewRect.height(); + + std::string javascript = "alert(\"PageViewRect:" + + base::DoubleToString(*left) + "," + + base::DoubleToString(*right) + "," + + base::DoubleToString(*top) + "," + + base::DoubleToString(*bottom) + "," + + "\")"; + +} + +int PDFiumEngine::Form_GetPlatform(FPDF_FORMFILLINFO* pThis, + void* platform, + int length) { + int platform_flag = -1; +#if defined(WIN32) + platform_flag = 0; +#elif defined(__linux__) + platform_flag = 1; +#else + platform_flag = 2; +#endif + std::string javascript = "alert(\"Platform:" + + base::DoubleToString(platform_flag) + + "\")"; + //platform = new char[3]; + //char tem[10] = "WIN"; + //strncpy((char*)platform, tem, 3); + + return 3; +} + +FPDF_BOOL PDFiumEngine::Form_PopupMenu(FPDF_FORMFILLINFO* pThis, + FPDF_PAGE page, + FPDF_WIDGET hWidget, + int menuFlag, float x, float y) { + return false; +} + +FPDF_BOOL PDFiumEngine::Form_PostRequestURL(FPDF_FORMFILLINFO* pThis, FPDF_WIDESTRING wsURL, + FPDF_WIDESTRING wsData, FPDF_WIDESTRING wsContentType, + FPDF_WIDESTRING wsEncode, FPDF_WIDESTRING wsHeader, FPDF_BSTR* respone) { + std::string url_str = + base::UTF16ToUTF8(reinterpret_cast(wsURL)); + std::string data_str = + base::UTF16ToUTF8(reinterpret_cast(wsData)); + std::string content_type_str = + base::UTF16ToUTF8(reinterpret_cast(wsContentType)); + std::string encode_str = + base::UTF16ToUTF8(reinterpret_cast(wsEncode)); + std::string header_str = + base::UTF16ToUTF8(reinterpret_cast(wsHeader)); + + std::string javascript = "alert(\"Post:" + + url_str + "," + data_str + "," + content_type_str + "," + + encode_str + "," + header_str + + "\")"; + return true; +} + +FPDF_BOOL PDFiumEngine::Form_PutRequestURL(FPDF_FORMFILLINFO* pThis, + FPDF_WIDESTRING wsURL, FPDF_WIDESTRING wsData, FPDF_WIDESTRING wsEncode) { + std::string url_str = + base::UTF16ToUTF8(reinterpret_cast(wsURL)); + std::string data_str = + base::UTF16ToUTF8(reinterpret_cast(wsData)); + std::string encode_str = + base::UTF16ToUTF8(reinterpret_cast(wsEncode)); + + std::string javascript = "alert(\"Put:" + + url_str + "," + data_str + "," + encode_str + + "\")"; + + return true; +} + +//FPDF_BOOL PDFiumEngine::Form_ShowFileDialog(FPDF_FORMFILLINFO* pThis, FPDF_WIDESTRING wsTitle, FPDF_WIDESTRING wsFilter, FPDF_BOOL isOpen, FPDF_STRINGHANDLE pathArr) { +// int tem = 1; +// tem++; +// return false; +//} + +void PDFiumEngine::Form_UploadTo(FPDF_FORMFILLINFO* pThis, FPDF_FILEHANDLER* fileHandler, int fileFlag, FPDF_WIDESTRING uploadTo) { + std::string to_str = + base::UTF16ToUTF8(reinterpret_cast(uploadTo)); + +} + +FPDF_LPFILEHANDLER PDFiumEngine::Form_DownloadFromURL(FPDF_FORMFILLINFO* pThis, FPDF_WIDESTRING URL) { + std::string url_str = + base::UTF16ToUTF8(reinterpret_cast(URL)); + + + // Get URL data... + FILE* file = fopen(FXQA_TESTFILE("downloadtest.tem"), "w"); + FPDF_FILE* pFileHander = new FPDF_FILE; + pFileHander->file = file; + pFileHander->fileHandler.clientData = pFileHander; + pFileHander->fileHandler.Flush = Sample_Flush; + pFileHander->fileHandler.GetSize = Sample_GetSize; + pFileHander->fileHandler.ReadBlock = Sample_ReadBlock; + pFileHander->fileHandler.Release = Sample_Release; + pFileHander->fileHandler.Truncate = Sample_Truncate; + pFileHander->fileHandler.WriteBlock = Sample_WriteBlock; + + return &pFileHander->fileHandler; +} + +//FPDF_BOOL PDFiumEngine::MyForm_GetFilePath(FPDF_FORMFILLINFO* pThis, FPDF_FILEHANDLER* pFileHandler, void* filePath, int length) { +// //std::string filePath = engine->client_->FileOpen(fileFlag); +// if (filePath == NULL) { +// std::string javascript = "alert(\"GetFilePath:NULL\")"; +// PDFiumEngine* engine = static_cast(pThis); +// engine->client_->RunJSCmd(javascript); +// return false; +// } +// std::string filePath_str = (char*)filePath; +// +// std::string javascript = "alert(\"GetFilePath:" + filePath_str + "\")"; +// PDFiumEngine* engine = static_cast(pThis); +// engine->client_->RunJSCmd(javascript); +// +// FILE* file = fopen(FXQA_TESTFILE("tem.xdp"), "w"); +// FPDF_FILE* pFileHander = new FPDF_FILE; +// pFileHander->file = file; +// pFileHander->fileHandler.clientData = pFileHander; +// pFileHander->fileHandler.Flush = Sample_Flush; +// pFileHander->fileHandler.GetSize = Sample_GetSize; +// pFileHander->fileHandler.ReadBlock = Sample_ReadBlock; +// pFileHander->fileHandler.Release = Sample_Release; +// pFileHander->fileHandler.Truncate = Sample_Truncate; +// pFileHander->fileHandler.WriteBlock = Sample_WriteBlock; +// +// pFileHandler = &pFileHander->fileHandler; +// +// return true; +//} + +FPDF_FILEHANDLER* PDFiumEngine::Form_OpenFile(FPDF_FORMFILLINFO* pThis, int fileFlag, FPDF_WIDESTRING wsURL, const char* mode) { + std::string url_str = "NULL"; + if (wsURL == NULL) { + url_str = "NULL"; + } + else { + url_str = + base::UTF16ToUTF8(reinterpret_cast(wsURL)); + } + if (strncmp(mode, "wb", 2) == 0) { + + FILE* file = fopen(FXQA_TESTFILE("tem.txt"), mode); + FPDF_FILE* pFileHander = new FPDF_FILE; + pFileHander->file = file; + pFileHander->fileHandler.clientData = pFileHander; + pFileHander->fileHandler.Flush = Sample_Flush; + pFileHander->fileHandler.GetSize = Sample_GetSize; + pFileHander->fileHandler.ReadBlock = Sample_ReadBlock; + pFileHander->fileHandler.Release = Sample_Release; + pFileHander->fileHandler.Truncate = Sample_Truncate; + pFileHander->fileHandler.WriteBlock = Sample_WriteBlock; + return &pFileHander->fileHandler; + } + else { + url_str = base::UTF16ToUTF8(reinterpret_cast(wsURL)); + } + return NULL; +} + + +void PDFiumEngine::Form_GotoURL(FPDF_FORMFILLINFO* pThis, FPDF_DOCUMENT document, FPDF_WIDESTRING wsURL) { + std::string url_str = + base::UTF16ToUTF8(reinterpret_cast(wsURL)); + int pageCount = FPDF_GetPageCount(document); +} + +int PDFiumEngine::Form_GetLanguage(FPDF_FORMFILLINFO* pThis, void* language, int length) { + return 0; +} + +#endif // _TEST_XFA + +int PDFiumEngine::GetBlock(void* param, unsigned long position, + unsigned char* buffer, unsigned long size) { + DocumentLoader* loader = static_cast(param); + return loader->GetBlock(position, size, buffer); +} + +bool PDFiumEngine::IsDataAvail(FX_FILEAVAIL* param, + size_t offset, size_t size) { + PDFiumEngine::FileAvail* file_avail = + static_cast(param); + return file_avail->loader->IsDataAvailable(offset, size); +} + +void PDFiumEngine::AddSegment(FX_DOWNLOADHINTS* param, + size_t offset, size_t size) { + PDFiumEngine::DownloadHints* download_hints = + static_cast(param); + return download_hints->loader->RequestData(offset, size); +} + +bool PDFiumEngine::New(const char* url) { + url_ = url; + headers_ = std::string(); + return true; +} + +bool PDFiumEngine::New(const char* url, + const char* headers) { + url_ = url; + if (!headers) + headers_ = std::string(); + else + headers_ = headers; + return true; +} + +void PDFiumEngine::PageOffsetUpdated(const pp::Point& page_offset) { + page_offset_ = page_offset; +} + +void PDFiumEngine::PluginSizeUpdated(const pp::Size& size) { + CancelPaints(); + + plugin_size_ = size; + CalculateVisiblePages(); +} + +void PDFiumEngine::ScrolledToXPosition(int position) { + CancelPaints(); + + int old_x = position_.x(); + position_.set_x(position); + CalculateVisiblePages(); + client_->Scroll(pp::Point(old_x - position, 0)); +} + +void PDFiumEngine::ScrolledToYPosition(int position) { + CancelPaints(); + + int old_y = position_.y(); + position_.set_y(position); + CalculateVisiblePages(); + client_->Scroll(pp::Point(0, old_y - position)); +} + +void PDFiumEngine::PrePaint() { + for (size_t i = 0; i < progressive_paints_.size(); ++i) + progressive_paints_[i].painted_ = false; +} + +void PDFiumEngine::Paint(const pp::Rect& rect, + pp::ImageData* image_data, + std::vector* ready, + std::vector* pending) { + pp::Rect leftover = rect; + for (size_t i = 0; i < visible_pages_.size(); ++i) { + int index = visible_pages_[i]; + pp::Rect page_rect = pages_[index]->rect(); + // Convert the current page's rectangle to screen rectangle. We do this + // instead of the reverse (converting the dirty rectangle from screen to + // page coordinates) because then we'd have to convert back to screen + // coordinates, and the rounding errors sometime leave pixels dirty or even + // move the text up or down a pixel when zoomed. + pp::Rect page_rect_in_screen = GetPageScreenRect(index); + pp::Rect dirty_in_screen = page_rect_in_screen.Intersect(leftover); + if (dirty_in_screen.IsEmpty()) + continue; + + leftover = leftover.Subtract(dirty_in_screen); + + if (pages_[index]->available()) { + int progressive = GetProgressiveIndex(index); + if (progressive != -1 && + progressive_paints_[progressive].rect != dirty_in_screen) { + // The PDFium code can only handle one progressive paint at a time, so + // queue this up. Previously we used to merge the rects when this + // happened, but it made scrolling up on complex PDFs very slow since + // there would be a damaged rect at the top (from scroll) and at the + // bottom (from toolbar). + pending->push_back(dirty_in_screen); + continue; + } + + if (progressive == -1) { + progressive = StartPaint(index, dirty_in_screen); + progressive_paint_timeout_ = kMaxInitialProgressivePaintTimeMs; + } else { + progressive_paint_timeout_ = kMaxProgressivePaintTimeMs; + } + + progressive_paints_[progressive].painted_ = true; + if (ContinuePaint(progressive, image_data)) { + FinishPaint(progressive, image_data); + ready->push_back(dirty_in_screen); + } else { + pending->push_back(dirty_in_screen); + } + } else { + PaintUnavailablePage(index, dirty_in_screen, image_data); + ready->push_back(dirty_in_screen); + } + } +} + +void PDFiumEngine::PostPaint() { + for (size_t i = 0; i < progressive_paints_.size(); ++i) { + if (progressive_paints_[i].painted_) + continue; + + // This rectangle must have been merged with another one, that's why we + // weren't asked to paint it. Remove it or otherwise we'll never finish + // painting. + FPDF_RenderPage_Close( + pages_[progressive_paints_[i].page_index]->GetPage()); + FPDFBitmap_Destroy(progressive_paints_[i].bitmap); + progressive_paints_.erase(progressive_paints_.begin() + i); + --i; + } +} + +bool PDFiumEngine::HandleDocumentLoad(const pp::URLLoader& loader) { + password_tries_remaining_ = kMaxPasswordTries; + return doc_loader_.Init(loader, url_, headers_); +} + +pp::Instance* PDFiumEngine::GetPluginInstance() { + return client_->GetPluginInstance(); +} + +pp::URLLoader PDFiumEngine::CreateURLLoader() { + return client_->CreateURLLoader(); +} + +void PDFiumEngine::AppendPage(PDFEngine* engine, int index) { + // Unload and delete the blank page before appending. + pages_[index]->Unload(); + pages_[index]->set_calculated_links(false); + pp::Size curr_page_size = GetPageSize(index); + FPDFPage_Delete(doc_, index); + FPDF_ImportPages(doc_, + static_cast(engine)->doc(), + "1", + index); + pp::Size new_page_size = GetPageSize(index); + if (curr_page_size != new_page_size) + LoadPageInfo(true); + client_->Invalidate(GetPageScreenRect(index)); +} + +pp::Point PDFiumEngine::GetScrollPosition() { + return position_; +} + +void PDFiumEngine::SetScrollPosition(const pp::Point& position) { + position_ = position; +} + +bool PDFiumEngine::IsProgressiveLoad() { + return doc_loader_.is_partial_document(); +} + +void PDFiumEngine::OnPartialDocumentLoaded() { + file_access_.m_FileLen = doc_loader_.document_size(); + fpdf_availability_ = FPDFAvail_Create(&file_availability_, &file_access_); + DCHECK(fpdf_availability_); + + // Currently engine does not deal efficiently with some non-linearized files. + // See http://code.google.com/p/chromium/issues/detail?id=59400 + // To improve user experience we download entire file for non-linearized PDF. + if (!FPDFAvail_IsLinearized(fpdf_availability_)) { + doc_loader_.RequestData(0, doc_loader_.document_size()); + return; + } + + LoadDocument(); +} + +void PDFiumEngine::OnPendingRequestComplete() { + if (!doc_ || !form_) { + LoadDocument(); + return; + } + + // LoadDocument() will result in |pending_pages_| being reset so there's no + // need to run the code below in that case. + bool update_pages = false; + std::vector still_pending; + for (size_t i = 0; i < pending_pages_.size(); ++i) { + if (CheckPageAvailable(pending_pages_[i], &still_pending)) { + update_pages = true; + if (IsPageVisible(pending_pages_[i])) + client_->Invalidate(GetPageScreenRect(pending_pages_[i])); + } + } + pending_pages_.swap(still_pending); + if (update_pages) + LoadPageInfo(true); +} + +void PDFiumEngine::OnNewDataAvailable() { + client_->DocumentLoadProgress(doc_loader_.GetAvailableData(), + doc_loader_.document_size()); +} + +void PDFiumEngine::OnDocumentComplete() { + if (!doc_ || !form_) { + file_access_.m_FileLen = doc_loader_.document_size(); + LoadDocument(); + return; + } + + bool need_update = false; + for (size_t i = 0; i < pages_.size(); ++i) { + if (pages_[i]->available()) + continue; + + pages_[i]->set_available(true); + // We still need to call IsPageAvail() even if the whole document is + // already downloaded. + FPDFAvail_IsPageAvail(fpdf_availability_, i, &download_hints_); + need_update = true; + if (IsPageVisible(i)) + client_->Invalidate(GetPageScreenRect(i)); + } + if (need_update) + LoadPageInfo(true); + + FinishLoadingDocument(); +} + +void PDFiumEngine::FinishLoadingDocument() { + DCHECK(doc_loader_.IsDocumentComplete() && doc_); + if (called_do_document_action_) + return; + called_do_document_action_ = true; + + // These can only be called now, as the JS might end up needing a page. + FORM_DoDocumentJSAction(form_); + FORM_DoDocumentOpenAction(form_); + if (most_visible_page_ != -1) { + FPDF_PAGE new_page = pages_[most_visible_page_]->GetPage(); + FORM_DoPageAAction(new_page, form_, FPDFPAGE_AACTION_OPEN); + } + + if (doc_) // This can only happen if loading |doc_| fails. + client_->DocumentLoadComplete(pages_.size()); +} + +void PDFiumEngine::UnsupportedFeature(int type) { + std::string feature; + switch (type) { + case FPDF_UNSP_DOC_XFAFORM: + //feature = "XFA"; + break; + case FPDF_UNSP_DOC_PORTABLECOLLECTION: + feature = "Portfolios_Packages"; + break; + case FPDF_UNSP_DOC_ATTACHMENT: + case FPDF_UNSP_ANNOT_ATTACHMENT: + feature = "Attachment"; + break; + case FPDF_UNSP_DOC_SECURITY: + feature = "Rights_Management"; + break; + case FPDF_UNSP_DOC_SHAREDREVIEW: + feature = "Shared_Review"; + break; + case FPDF_UNSP_DOC_SHAREDFORM_ACROBAT: + case FPDF_UNSP_DOC_SHAREDFORM_FILESYSTEM: + case FPDF_UNSP_DOC_SHAREDFORM_EMAIL: + feature = "Shared_Form"; + break; + case FPDF_UNSP_ANNOT_3DANNOT: + feature = "3D"; + break; + case FPDF_UNSP_ANNOT_MOVIE: + feature = "Movie"; + break; + case FPDF_UNSP_ANNOT_SOUND: + feature = "Sound"; + break; + case FPDF_UNSP_ANNOT_SCREEN_MEDIA: + case FPDF_UNSP_ANNOT_SCREEN_RICHMEDIA: + feature = "Screen"; + break; + case FPDF_UNSP_ANNOT_SIG: + feature = "Digital_Signature"; + break; + } + client_->DocumentHasUnsupportedFeature(feature); +} + +void PDFiumEngine::ContinueFind(int32_t result) { + StartFind(current_find_text_.c_str(), !!result); +} + +bool PDFiumEngine::HandleEvent(const pp::InputEvent& event) { + DCHECK(!defer_page_unload_); + defer_page_unload_ = true; + bool rv = false; + switch (event.GetType()) { + case PP_INPUTEVENT_TYPE_MOUSEDOWN: + rv = OnMouseDown(pp::MouseInputEvent(event)); + break; + case PP_INPUTEVENT_TYPE_MOUSEUP: + rv = OnMouseUp(pp::MouseInputEvent(event)); + break; + case PP_INPUTEVENT_TYPE_MOUSEMOVE: + rv = OnMouseMove(pp::MouseInputEvent(event)); + break; + case PP_INPUTEVENT_TYPE_KEYDOWN: + rv = OnKeyDown(pp::KeyboardInputEvent(event)); + break; + case PP_INPUTEVENT_TYPE_KEYUP: + rv = OnKeyUp(pp::KeyboardInputEvent(event)); + break; + case PP_INPUTEVENT_TYPE_CHAR: + rv = OnChar(pp::KeyboardInputEvent(event)); + break; + default: + break; + } + + DCHECK(defer_page_unload_); + defer_page_unload_ = false; + for (size_t i = 0; i < deferred_page_unloads_.size(); ++i) + pages_[deferred_page_unloads_[i]]->Unload(); + deferred_page_unloads_.clear(); + return rv; +} + +uint32_t PDFiumEngine::QuerySupportedPrintOutputFormats() { + if (!HasPermission(PDFEngine::PERMISSION_PRINT_LOW_QUALITY)) + return 0; + return PP_PRINTOUTPUTFORMAT_PDF; +} + +void PDFiumEngine::PrintBegin() { + FORM_DoDocumentAAction(form_, FPDFDOC_AACTION_WP); +} + +pp::Resource PDFiumEngine::PrintPages( + const PP_PrintPageNumberRange_Dev* page_ranges, uint32_t page_range_count, + const PP_PrintSettings_Dev& print_settings) { + if (HasPermission(PDFEngine::PERMISSION_PRINT_HIGH_QUALITY)) + return PrintPagesAsPDF(page_ranges, page_range_count, print_settings); + else if (HasPermission(PDFEngine::PERMISSION_PRINT_LOW_QUALITY)) + return PrintPagesAsRasterPDF(page_ranges, page_range_count, print_settings); + return pp::Resource(); +} + +FPDF_DOCUMENT PDFiumEngine::CreateSinglePageRasterPdf( + double source_page_width, + double source_page_height, + const PP_PrintSettings_Dev& print_settings, + PDFiumPage* page_to_print) { + FPDF_DOCUMENT temp_doc = FPDF_CreateNewDocument(); + if (!temp_doc) + return temp_doc; + + const pp::Size& bitmap_size(page_to_print->rect().size()); + + FPDF_PAGE temp_page = + FPDFPage_New(temp_doc, 0, source_page_width, source_page_height); + + pp::ImageData image = pp::ImageData(client_->GetPluginInstance(), + PP_IMAGEDATAFORMAT_BGRA_PREMUL, + bitmap_size, + false); + + FPDF_BITMAP bitmap = FPDFBitmap_CreateEx(bitmap_size.width(), + bitmap_size.height(), + FPDFBitmap_BGRx, + image.data(), + image.stride()); + + // Clear the bitmap + FPDFBitmap_FillRect( + bitmap, 0, 0, bitmap_size.width(), bitmap_size.height(), 0xFFFFFFFF); + + pp::Rect page_rect = page_to_print->rect(); + FPDF_RenderPageBitmap(bitmap, + page_to_print->GetPrintPage(), + page_rect.x(), + page_rect.y(), + page_rect.width(), + page_rect.height(), + print_settings.orientation, + FPDF_ANNOT | FPDF_PRINTING | FPDF_NO_CATCH); + + double ratio_x = (static_cast(bitmap_size.width()) * kPointsPerInch) / + print_settings.dpi; + double ratio_y = + (static_cast(bitmap_size.height()) * kPointsPerInch) / + print_settings.dpi; + + // Add the bitmap to an image object and add the image object to the output + // page. + FPDF_PAGEOBJECT temp_img = FPDFPageObj_NewImgeObj(temp_doc); + FPDFImageObj_SetBitmap(&temp_page, 1, temp_img, bitmap); + FPDFImageObj_SetMatrix(temp_img, ratio_x, 0, 0, ratio_y, 0, 0); + FPDFPage_InsertObject(temp_page, temp_img); + FPDFPage_GenerateContent(temp_page); + FPDF_ClosePage(temp_page); + + page_to_print->ClosePrintPage(); + FPDFBitmap_Destroy(bitmap); + + return temp_doc; +} + +pp::Buffer_Dev PDFiumEngine::PrintPagesAsRasterPDF( + const PP_PrintPageNumberRange_Dev* page_ranges, uint32_t page_range_count, + const PP_PrintSettings_Dev& print_settings) { + if (!page_range_count) + return pp::Buffer_Dev(); + + // If document is not downloaded yet, disable printing. + if (doc_ && !doc_loader_.IsDocumentComplete()) + return pp::Buffer_Dev(); + + FPDF_DOCUMENT output_doc = FPDF_CreateNewDocument(); + if (!output_doc) + return pp::Buffer_Dev(); + + SaveSelectedFormForPrint(); + + std::vector pages_to_print; + // width and height of source PDF pages. + std::vector > source_page_sizes; + // Collect pages to print and sizes of source pages. + std::vector page_numbers = + GetPageNumbersFromPrintPageNumberRange(page_ranges, page_range_count); + for (size_t i = 0; i < page_numbers.size(); ++i) { + uint32_t page_number = page_numbers[i]; + FPDF_PAGE pdf_page = FPDF_LoadPage(doc_, page_number); + double source_page_width = FPDF_GetPageWidth(pdf_page); + double source_page_height = FPDF_GetPageHeight(pdf_page); + source_page_sizes.push_back(std::make_pair(source_page_width, + source_page_height)); + + int width_in_pixels = ConvertUnit(source_page_width, + static_cast(kPointsPerInch), + print_settings.dpi); + int height_in_pixels = ConvertUnit(source_page_height, + static_cast(kPointsPerInch), + print_settings.dpi); + + pp::Rect rect(width_in_pixels, height_in_pixels); + pages_to_print.push_back(PDFiumPage(this, page_number, rect, true)); + FPDF_ClosePage(pdf_page); + } + +#if defined(OS_LINUX) + g_last_instance_id = client_->GetPluginInstance()->pp_instance(); +#endif + + size_t i = 0; + for (; i < pages_to_print.size(); ++i) { + double source_page_width = source_page_sizes[i].first; + double source_page_height = source_page_sizes[i].second; + + // Use temp_doc to compress image by saving PDF to buffer. + FPDF_DOCUMENT temp_doc = CreateSinglePageRasterPdf(source_page_width, + source_page_height, + print_settings, + &pages_to_print[i]); + + if (!temp_doc) + break; + + pp::Buffer_Dev buffer = GetFlattenedPrintData(temp_doc); + FPDF_CloseDocument(temp_doc); + + PDFiumMemBufferFileRead file_read(buffer.data(), buffer.size()); + temp_doc = FPDF_LoadCustomDocument(&file_read, NULL); + + FPDF_BOOL imported = FPDF_ImportPages(output_doc, temp_doc, "1", i); + FPDF_CloseDocument(temp_doc); + if (!imported) + break; + } + + pp::Buffer_Dev buffer; + if (i == pages_to_print.size()) { + FPDF_CopyViewerPreferences(output_doc, doc_); + FitContentsToPrintableAreaIfRequired(output_doc, print_settings); + // Now flatten all the output pages. + buffer = GetFlattenedPrintData(output_doc); + } + FPDF_CloseDocument(output_doc); + return buffer; +} + +pp::Buffer_Dev PDFiumEngine::GetFlattenedPrintData(const FPDF_DOCUMENT& doc) { + int page_count = FPDF_GetPageCount(doc); + bool flatten_succeeded = true; + for (int i = 0; i < page_count; ++i) { + FPDF_PAGE page = FPDF_LoadPage(doc, i); + DCHECK(page); + if (page) { + int flatten_ret = FPDFPage_Flatten(page, FLAT_PRINT); + FPDF_ClosePage(page); + if (flatten_ret == FLATTEN_FAIL) { + flatten_succeeded = false; + break; + } + } else { + flatten_succeeded = false; + break; + } + } + if (!flatten_succeeded) { + FPDF_CloseDocument(doc); + return pp::Buffer_Dev(); + } + + pp::Buffer_Dev buffer; + PDFiumMemBufferFileWrite output_file_write; + if (FPDF_SaveAsCopy(doc, &output_file_write, 0)) { + buffer = pp::Buffer_Dev( + client_->GetPluginInstance(), output_file_write.size()); + if (!buffer.is_null()) { + memcpy(buffer.data(), output_file_write.buffer().c_str(), + output_file_write.size()); + } + } + return buffer; +} + +pp::Buffer_Dev PDFiumEngine::PrintPagesAsPDF( + const PP_PrintPageNumberRange_Dev* page_ranges, uint32_t page_range_count, + const PP_PrintSettings_Dev& print_settings) { + if (!page_range_count) + return pp::Buffer_Dev(); + + DCHECK(doc_); + FPDF_DOCUMENT output_doc = FPDF_CreateNewDocument(); + if (!output_doc) + return pp::Buffer_Dev(); + + SaveSelectedFormForPrint(); + + std::string page_number_str; + for (uint32_t index = 0; index < page_range_count; ++index) { + if (!page_number_str.empty()) + page_number_str.append(","); + page_number_str.append( + base::IntToString(page_ranges[index].first_page_number + 1)); + if (page_ranges[index].first_page_number != + page_ranges[index].last_page_number) { + page_number_str.append("-"); + page_number_str.append( + base::IntToString(page_ranges[index].last_page_number + 1)); + } + } + + std::vector page_numbers = + GetPageNumbersFromPrintPageNumberRange(page_ranges, page_range_count); + for (size_t i = 0; i < page_numbers.size(); ++i) { + uint32_t page_number = page_numbers[i]; + pages_[page_number]->GetPage(); + if (!IsPageVisible(page_numbers[i])) + pages_[page_number]->Unload(); + } + + FPDF_CopyViewerPreferences(output_doc, doc_); + if (!FPDF_ImportPages(output_doc, doc_, page_number_str.c_str(), 0)) { + FPDF_CloseDocument(output_doc); + return pp::Buffer_Dev(); + } + + FitContentsToPrintableAreaIfRequired(output_doc, print_settings); + + // Now flatten all the output pages. + pp::Buffer_Dev buffer = GetFlattenedPrintData(output_doc); + FPDF_CloseDocument(output_doc); + return buffer; +} + +void PDFiumEngine::FitContentsToPrintableAreaIfRequired( + const FPDF_DOCUMENT& doc, const PP_PrintSettings_Dev& print_settings) { + // Check to see if we need to fit pdf contents to printer paper size. + if (print_settings.print_scaling_option != + PP_PRINTSCALINGOPTION_SOURCE_SIZE) { + int num_pages = FPDF_GetPageCount(doc); + // In-place transformation is more efficient than creating a new + // transformed document from the source document. Therefore, transform + // every page to fit the contents in the selected printer paper. + for (int i = 0; i < num_pages; ++i) { + FPDF_PAGE page = FPDF_LoadPage(doc, i); + TransformPDFPageForPrinting(page, print_settings); + FPDF_ClosePage(page); + } + } +} + +void PDFiumEngine::SaveSelectedFormForPrint() { + FORM_ForceToKillFocus(form_); + client_->FormTextFieldFocusChange(false); +} + +void PDFiumEngine::PrintEnd() { + FORM_DoDocumentAAction(form_, FPDFDOC_AACTION_DP); +} + +PDFiumPage::Area PDFiumEngine::GetCharIndex( + const pp::MouseInputEvent& event, int* page_index, + int* char_index, PDFiumPage::LinkTarget* target) { + // First figure out which page this is in. + pp::Point mouse_point = event.GetPosition(); + pp::Point point( + static_cast((mouse_point.x() + position_.x()) / current_zoom_), + static_cast((mouse_point.y() + position_.y()) / current_zoom_)); + return GetCharIndex(point, page_index, char_index, target); +} + +PDFiumPage::Area PDFiumEngine::GetCharIndex( + const pp::Point& point, + int* page_index, + int* char_index, + PDFiumPage::LinkTarget* target) { + int page = -1; + for (size_t i = 0; i < visible_pages_.size(); ++i) { + if (pages_[visible_pages_[i]]->rect().Contains(point)) { + page = visible_pages_[i]; + break; + } + } + if (page == -1) + return PDFiumPage::NONSELECTABLE_AREA; + + // If the page hasn't finished rendering, calling into the page sometimes + // leads to hangs. + for (size_t i = 0; i < progressive_paints_.size(); ++i) { + if (progressive_paints_[i].page_index == page) + return PDFiumPage::NONSELECTABLE_AREA; + } + + *page_index = page; + return pages_[page]->GetCharIndex(point, current_rotation_, char_index, + target); +} + +bool PDFiumEngine::OnMouseDown(const pp::MouseInputEvent& event) { + if (event.GetButton() != PP_INPUTEVENT_MOUSEBUTTON_LEFT) + return false; + + SelectionChangeInvalidator selection_invalidator(this); + selection_.clear(); + + int page_index = -1; + int char_index = -1; + PDFiumPage::LinkTarget target; + PDFiumPage::Area area = GetCharIndex(event, &page_index, + &char_index, &target); + mouse_down_state_.Set(area, target); + + // Decide whether to open link or not based on user action in mouse up and + // mouse move events. + if (area == PDFiumPage::WEBLINK_AREA) + return true; + + if (area == PDFiumPage::DOCLINK_AREA) { + client_->ScrollToPage(target.page); + client_->FormTextFieldFocusChange(false); + return true; + } + + if (page_index != -1) { + last_page_mouse_down_ = page_index; + double page_x, page_y; + pp::Point point = event.GetPosition(); + DeviceToPage(page_index, point.x(), point.y(), &page_x, &page_y); + + FORM_OnLButtonDown(form_, pages_[page_index]->GetPage(), 0, page_x, page_y); + int control = FPDPage_HasFormFieldAtPoint( + form_, pages_[page_index]->GetPage(), page_x, page_y); + if (control > FPDF_FORMFIELD_UNKNOWN) { // returns -1 sometimes... +#ifdef _TEST_XFA + client_->FormTextFieldFocusChange(control == FPDF_FORMFIELD_TEXTFIELD || + control == FPDF_FORMFIELD_COMBOBOX || + control == FPDF_FORMFIELD_XFA); +#else + client_->FormTextFieldFocusChange(control == FPDF_FORMFIELD_TEXTFIELD || + control == FPDF_FORMFIELD_COMBOBOX); +#endif + return true; // Return now before we get into the selection code. + } + } + + client_->FormTextFieldFocusChange(false); + + if (area != PDFiumPage::TEXT_AREA) + return true; // Return true so WebKit doesn't do its own highlighting. + + if (event.GetClickCount() == 1) { + OnSingleClick(page_index, char_index); + } else if (event.GetClickCount() == 2 || + event.GetClickCount() == 3) { + OnMultipleClick(event.GetClickCount(), page_index, char_index); + } + + return true; +} + +void PDFiumEngine::OnSingleClick(int page_index, int char_index) { + selecting_ = true; + selection_.push_back(PDFiumRange(pages_[page_index], char_index, 0)); +} + +void PDFiumEngine::OnMultipleClick(int click_count, + int page_index, + int char_index) { + // It would be more efficient if the SDK could support finding a space, but + // now it doesn't. + int start_index = char_index; + do { + base::char16 cur = pages_[page_index]->GetCharAtIndex(start_index); + // For double click, we want to select one word so we look for whitespace + // boundaries. For triple click, we want the whole line. + if (cur == '\n' || (click_count == 2 && (cur == ' ' || cur == '\t'))) + break; + } while (--start_index >= 0); + if (start_index) + start_index++; + + int end_index = char_index; + int total = pages_[page_index]->GetCharCount(); + while (end_index++ <= total) { + base::char16 cur = pages_[page_index]->GetCharAtIndex(end_index); + if (cur == '\n' || (click_count == 2 && (cur == ' ' || cur == '\t'))) + break; + } + + selection_.push_back(PDFiumRange( + pages_[page_index], start_index, end_index - start_index)); +} + +bool PDFiumEngine::OnMouseUp(const pp::MouseInputEvent& event) { + if (event.GetButton() != PP_INPUTEVENT_MOUSEBUTTON_LEFT) + return false; + + int page_index = -1; + int char_index = -1; + PDFiumPage::LinkTarget target; + PDFiumPage::Area area = + GetCharIndex(event, &page_index, &char_index, &target); + + // Open link on mouse up for same link for which mouse down happened earlier. + if (mouse_down_state_.Matches(area, target)) { + if (area == PDFiumPage::WEBLINK_AREA) { + bool open_in_new_tab = !!(event.GetModifiers() & kDefaultKeyModifier); + client_->NavigateTo(target.url, open_in_new_tab); + client_->FormTextFieldFocusChange(false); + return true; + } + } + + if (page_index != -1) { + double page_x, page_y; + pp::Point point = event.GetPosition(); + DeviceToPage(page_index, point.x(), point.y(), &page_x, &page_y); + FORM_OnLButtonUp( + form_, pages_[page_index]->GetPage(), 0, page_x, page_y); + } + + if (!selecting_) + return false; + + selecting_ = false; + return true; +} + +bool PDFiumEngine::OnMouseMove(const pp::MouseInputEvent& event) { + int page_index = -1; + int char_index = -1; + PDFiumPage::LinkTarget target; + PDFiumPage::Area area = + GetCharIndex(event, &page_index, &char_index, &target); + + // Clear |mouse_down_state_| if mouse moves away from where the mouse down + // happened. + if (!mouse_down_state_.Matches(area, target)) + mouse_down_state_.Reset(); + + if (!selecting_) { + PP_CursorType_Dev cursor; + switch (area) { + case PDFiumPage::TEXT_AREA: + cursor = PP_CURSORTYPE_IBEAM; + break; + case PDFiumPage::WEBLINK_AREA: + case PDFiumPage::DOCLINK_AREA: + cursor = PP_CURSORTYPE_HAND; + break; + case PDFiumPage::NONSELECTABLE_AREA: + default: + cursor = PP_CURSORTYPE_POINTER; + break; + } + + if (page_index != -1) { + double page_x, page_y; + pp::Point point = event.GetPosition(); + DeviceToPage(page_index, point.x(), point.y(), &page_x, &page_y); + + FORM_OnMouseMove(form_, pages_[page_index]->GetPage(), 0, page_x, page_y); + int control = FPDPage_HasFormFieldAtPoint( + form_, pages_[page_index]->GetPage(), page_x, page_y); + switch (control) { + case FPDF_FORMFIELD_PUSHBUTTON: + case FPDF_FORMFIELD_CHECKBOX: + case FPDF_FORMFIELD_RADIOBUTTON: + case FPDF_FORMFIELD_COMBOBOX: + case FPDF_FORMFIELD_LISTBOX: + cursor = PP_CURSORTYPE_HAND; + break; + case FPDF_FORMFIELD_TEXTFIELD: + cursor = PP_CURSORTYPE_IBEAM; + break; + default: + break; + } + } + + client_->UpdateCursor(cursor); + pp::Point point = event.GetPosition(); + std::string url = GetLinkAtPosition(event.GetPosition()); + if (url != link_under_cursor_) { + link_under_cursor_ = url; + pp::PDF::SetLinkUnderCursor(GetPluginInstance(), url.c_str()); + } + // No need to swallow the event, since this might interfere with the + // scrollbars if the user is dragging them. + return false; + } + + // We're selecting but right now we're not over text, so don't change the + // current selection. + if (area != PDFiumPage::TEXT_AREA && area != PDFiumPage::WEBLINK_AREA && + area != PDFiumPage::DOCLINK_AREA) { + return false; + } + + SelectionChangeInvalidator selection_invalidator(this); + + // Check if the user has descreased their selection area and we need to remove + // pages from selection_. + for (size_t i = 0; i < selection_.size(); ++i) { + if (selection_[i].page_index() == page_index) { + // There should be no other pages after this. + selection_.erase(selection_.begin() + i + 1, selection_.end()); + break; + } + } + + if (selection_.size() == 0) + return false; + + int last = selection_.size() - 1; + if (selection_[last].page_index() == page_index) { + // Selecting within a page. + int count; + if (char_index >= selection_[last].char_index()) { + // Selecting forward. + count = char_index - selection_[last].char_index() + 1; + } else { + count = char_index - selection_[last].char_index() - 1; + } + selection_[last].SetCharCount(count); + } else if (selection_[last].page_index() < page_index) { + // Selecting into the next page. + + // First make sure that there are no gaps in selection, i.e. if mousedown on + // page one but we only get mousemove over page three, we want page two. + for (int i = selection_[last].page_index() + 1; i < page_index; ++i) { + selection_.push_back(PDFiumRange(pages_[i], 0, + pages_[i]->GetCharCount())); + } + + int count = pages_[selection_[last].page_index()]->GetCharCount(); + selection_[last].SetCharCount(count - selection_[last].char_index()); + selection_.push_back(PDFiumRange(pages_[page_index], 0, char_index)); + } else { + // Selecting into the previous page. + selection_[last].SetCharCount(-selection_[last].char_index()); + + // First make sure that there are no gaps in selection, i.e. if mousedown on + // page three but we only get mousemove over page one, we want page two. + for (int i = selection_[last].page_index() - 1; i > page_index; --i) { + selection_.push_back(PDFiumRange(pages_[i], 0, + pages_[i]->GetCharCount())); + } + + int count = pages_[page_index]->GetCharCount(); + selection_.push_back( + PDFiumRange(pages_[page_index], count, count - char_index)); + } + + return true; +} + +bool PDFiumEngine::OnKeyDown(const pp::KeyboardInputEvent& event) { + if (last_page_mouse_down_ == -1) + return false; + + bool rv = !!FORM_OnKeyDown( + form_, pages_[last_page_mouse_down_]->GetPage(), + event.GetKeyCode(), event.GetModifiers()); + + if (event.GetKeyCode() == ui::VKEY_BACK || + event.GetKeyCode() == ui::VKEY_ESCAPE) { + // Chrome doesn't send char events for backspace or escape keys, see + // PlatformKeyboardEventBuilder::isCharacterKey() and + // http://chrome-corpsvn.mtv.corp.google.com/viewvc?view=rev&root=chrome&revision=31805 + // for more information. So just fake one since PDFium uses it. + std::string str; + str.push_back(event.GetKeyCode()); + pp::KeyboardInputEvent synthesized(pp::KeyboardInputEvent( + client_->GetPluginInstance(), + PP_INPUTEVENT_TYPE_CHAR, + event.GetTimeStamp(), + event.GetModifiers(), + event.GetKeyCode(), + str)); + OnChar(synthesized); + } + + return rv; +} + +bool PDFiumEngine::OnKeyUp(const pp::KeyboardInputEvent& event) { + if (last_page_mouse_down_ == -1) + return false; + + return !!FORM_OnKeyUp( + form_, pages_[last_page_mouse_down_]->GetPage(), + event.GetKeyCode(), event.GetModifiers()); +} + +bool PDFiumEngine::OnChar(const pp::KeyboardInputEvent& event) { + if (last_page_mouse_down_ == -1) + return false; + + base::string16 str = base::UTF8ToUTF16(event.GetCharacterText().AsString()); + return !!FORM_OnChar( + form_, pages_[last_page_mouse_down_]->GetPage(), + str[0], + event.GetModifiers()); +} + +void PDFiumEngine::StartFind(const char* text, bool case_sensitive) { + // We can get a call to StartFind before we have any page information (i.e. + // before the first call to LoadDocument has happened). Handle this case. + if (pages_.empty()) + return; + + bool first_search = false; + int character_to_start_searching_from = 0; + if (current_find_text_ != text) { // First time we search for this text. + first_search = true; + std::vector old_selection = selection_; + StopFind(); + current_find_text_ = text; + + if (old_selection.empty()) { + // Start searching from the beginning of the document. + next_page_to_search_ = 0; + last_page_to_search_ = pages_.size() - 1; + last_character_index_to_search_ = -1; + } else { + // There's a current selection, so start from it. + next_page_to_search_ = old_selection[0].page_index(); + last_character_index_to_search_ = old_selection[0].char_index(); + character_to_start_searching_from = old_selection[0].char_index(); + last_page_to_search_ = next_page_to_search_; + } + } + + int current_page = next_page_to_search_; + + if (pages_[current_page]->available()) { + base::string16 str = base::UTF8ToUTF16(text); + // Don't use PDFium to search for now, since it doesn't support unicode text. + // Leave the code for now to avoid bit-rot, in case it's fixed later. + if (0) { + SearchUsingPDFium( + str, case_sensitive, first_search, character_to_start_searching_from, + current_page); + } else { + SearchUsingICU( + str, case_sensitive, first_search, character_to_start_searching_from, + current_page); + } + + if (!IsPageVisible(current_page)) + pages_[current_page]->Unload(); + } + + if (next_page_to_search_ != last_page_to_search_ || + (first_search && last_character_index_to_search_ != -1)) { + ++next_page_to_search_; + } + + if (next_page_to_search_ == static_cast(pages_.size())) + next_page_to_search_ = 0; + // If there's only one page in the document and we start searching midway, + // then we'll want to search the page one more time. + bool end_of_search = + next_page_to_search_ == last_page_to_search_ && + // Only one page but didn't start midway. + ((pages_.size() == 1 && last_character_index_to_search_ == -1) || + // Started midway, but only 1 page and we already looped around. + (pages_.size() == 1 && !first_search) || + // Started midway, and we've just looped around. + (pages_.size() > 1 && current_page == next_page_to_search_)); + + if (end_of_search) { + // Send the final notification. + client_->NotifyNumberOfFindResultsChanged(find_results_.size(), true); + + // When searching is complete, resume finding at a particular index. + if (resume_find_index_ != -1) { + current_find_index_ = resume_find_index_; + resume_find_index_ = -1; + } + } else { + pp::CompletionCallback callback = + find_factory_.NewCallback(&PDFiumEngine::ContinueFind); + pp::Module::Get()->core()->CallOnMainThread( + 0, callback, case_sensitive ? 1 : 0); + } +} + +void PDFiumEngine::SearchUsingPDFium(const base::string16& term, + bool case_sensitive, + bool first_search, + int character_to_start_searching_from, + int current_page) { + // Find all the matches in the current page. + unsigned long flags = case_sensitive ? FPDF_MATCHCASE : 0; + FPDF_SCHHANDLE find = FPDFText_FindStart( + pages_[current_page]->GetTextPage(), + reinterpret_cast(term.c_str()), + flags, character_to_start_searching_from); + + // Note: since we search one page at a time, we don't find matches across + // page boundaries. We could do this manually ourself, but it seems low + // priority since Reader itself doesn't do it. + while (FPDFText_FindNext(find)) { + PDFiumRange result(pages_[current_page], + FPDFText_GetSchResultIndex(find), + FPDFText_GetSchCount(find)); + + if (!first_search && + last_character_index_to_search_ != -1 && + result.page_index() == last_page_to_search_ && + result.char_index() >= last_character_index_to_search_) { + break; + } + + AddFindResult(result); + } + + FPDFText_FindClose(find); +} + +void PDFiumEngine::SearchUsingICU(const base::string16& term, + bool case_sensitive, + bool first_search, + int character_to_start_searching_from, + int current_page) { + base::string16 page_text; + int text_length = pages_[current_page]->GetCharCount(); + if (character_to_start_searching_from) { + text_length -= character_to_start_searching_from; + } else if (!first_search && + last_character_index_to_search_ != -1 && + current_page == last_page_to_search_) { + text_length = last_character_index_to_search_; + } + if (text_length <= 0) + return; + unsigned short* data = + reinterpret_cast(WriteInto(&page_text, text_length + 1)); + FPDFText_GetText(pages_[current_page]->GetTextPage(), + character_to_start_searching_from, + text_length, + data); + std::vector results; + client_->SearchString( + page_text.c_str(), term.c_str(), case_sensitive, &results); + for (size_t i = 0; i < results.size(); ++i) { + // Need to map the indexes from the page text, which may have generated + // characters like space etc, to character indices from the page. + int temp_start = results[i].start_index + character_to_start_searching_from; + int start = FPDFText_GetCharIndexFromTextIndex( + pages_[current_page]->GetTextPage(), temp_start); + int end = FPDFText_GetCharIndexFromTextIndex( + pages_[current_page]->GetTextPage(), + temp_start + results[i].length); + AddFindResult(PDFiumRange(pages_[current_page], start, end - start)); + } +} + +void PDFiumEngine::AddFindResult(const PDFiumRange& result) { + // Figure out where to insert the new location, since we could have + // started searching midway and now we wrapped. + size_t i; + int page_index = result.page_index(); + int char_index = result.char_index(); + for (i = 0; i < find_results_.size(); ++i) { + if (find_results_[i].page_index() > page_index || + (find_results_[i].page_index() == page_index && + find_results_[i].char_index() > char_index)) { + break; + } + } + find_results_.insert(find_results_.begin() + i, result); + UpdateTickMarks(); + + if (current_find_index_ == -1 && resume_find_index_ == -1) { + // Select the first match. + SelectFindResult(true); + } else if (static_cast(i) <= current_find_index_) { + // Update the current match index + current_find_index_++; + client_->NotifySelectedFindResultChanged(current_find_index_); + } + client_->NotifyNumberOfFindResultsChanged(find_results_.size(), false); +} + +bool PDFiumEngine::SelectFindResult(bool forward) { + if (find_results_.empty()) { + NOTREACHED(); + return false; + } + + SelectionChangeInvalidator selection_invalidator(this); + + // Move back/forward through the search locations we previously found. + if (forward) { + if (++current_find_index_ == static_cast(find_results_.size())) + current_find_index_ = 0; + } else { + if (--current_find_index_ < 0) { + current_find_index_ = find_results_.size() - 1; + } + } + + // Update the selection before telling the client to scroll, since it could + // paint then. + selection_.clear(); + selection_.push_back(find_results_[current_find_index_]); + + // If the result is not in view, scroll to it. + size_t i; + pp::Rect bounding_rect; + pp::Rect visible_rect = GetVisibleRect(); + // Use zoom of 1.0 since visible_rect is without zoom. + std::vector rects = find_results_[current_find_index_]. + GetScreenRects(pp::Point(), 1.0, current_rotation_); + for (i = 0; i < rects.size(); ++i) + bounding_rect = bounding_rect.Union(rects[i]); + if (!visible_rect.Contains(bounding_rect)) { + pp::Point center = bounding_rect.CenterPoint(); + // Make the page centered. + int new_y = static_cast(center.y() * current_zoom_) - + static_cast(visible_rect.height() * current_zoom_ / 2); + if (new_y < 0) + new_y = 0; + client_->ScrollToY(new_y); + + // Only move horizontally if it's not visible. + if (center.x() < visible_rect.x() || center.x() > visible_rect.right()) { + int new_x = static_cast(center.x() * current_zoom_) - + static_cast(visible_rect.width() * current_zoom_ / 2); + if (new_x < 0) + new_x = 0; + client_->ScrollToX(new_x); + } + } + + client_->NotifySelectedFindResultChanged(current_find_index_); + + return true; +} + +void PDFiumEngine::StopFind() { + SelectionChangeInvalidator selection_invalidator(this); + + selection_.clear(); + selecting_ = false; + find_results_.clear(); + next_page_to_search_ = -1; + last_page_to_search_ = -1; + last_character_index_to_search_ = -1; + current_find_index_ = -1; + current_find_text_.clear(); + UpdateTickMarks(); + find_factory_.CancelAll(); +} + +void PDFiumEngine::UpdateTickMarks() { + std::vector tickmarks; + for (size_t i = 0; i < find_results_.size(); ++i) { + pp::Rect rect; + // Always use an origin of 0,0 since scroll positions don't affect tickmark. + std::vector rects = find_results_[i].GetScreenRects( + pp::Point(0, 0), current_zoom_, current_rotation_); + for (size_t j = 0; j < rects.size(); ++j) + rect = rect.Union(rects[j]); + tickmarks.push_back(rect); + } + + client_->UpdateTickMarks(tickmarks); +} + +void PDFiumEngine::ZoomUpdated(double new_zoom_level) { + CancelPaints(); + + current_zoom_ = new_zoom_level; + + CalculateVisiblePages(); + UpdateTickMarks(); +} + +void PDFiumEngine::RotateClockwise() { + current_rotation_ = (current_rotation_ + 1) % 4; + + // Store the current find index so that we can resume finding at that + // particular index after we have recomputed the find results. + std::string current_find_text = current_find_text_; + resume_find_index_ = current_find_index_; + + InvalidateAllPages(); + + if (!current_find_text.empty()) + StartFind(current_find_text.c_str(), false); +} + +void PDFiumEngine::RotateCounterclockwise() { + current_rotation_ = (current_rotation_ - 1) % 4; + + // Store the current find index so that we can resume finding at that + // particular index after we have recomputed the find results. + std::string current_find_text = current_find_text_; + resume_find_index_ = current_find_index_; + + InvalidateAllPages(); + + if (!current_find_text.empty()) + StartFind(current_find_text.c_str(), false); +} + +void PDFiumEngine::InvalidateAllPages() { + CancelPaints(); + StopFind(); + LoadPageInfo(true); + client_->Invalidate(pp::Rect(plugin_size_)); +} + +std::string PDFiumEngine::GetSelectedText() { + base::string16 result; + for (size_t i = 0; i < selection_.size(); ++i) { + if (i > 0 && + selection_[i - 1].page_index() > selection_[i].page_index()) { + result = selection_[i].GetText() + result; + } else { + result.append(selection_[i].GetText()); + } + } + + FormatStringWithHyphens(&result); + FormatStringForOS(&result); + return base::UTF16ToUTF8(result); +} + +std::string PDFiumEngine::GetLinkAtPosition(const pp::Point& point) { + int temp; + PDFiumPage::LinkTarget target; + pp::Point point_in_page( + static_cast((point.x() + position_.x()) / current_zoom_), + static_cast((point.y() + position_.y()) / current_zoom_)); + PDFiumPage::Area area = GetCharIndex(point_in_page, &temp, &temp, &target); + if (area == PDFiumPage::WEBLINK_AREA) + return target.url; + return std::string(); +} + +bool PDFiumEngine::IsSelecting() { + return selecting_; +} + +bool PDFiumEngine::HasPermission(DocumentPermission permission) const { + switch (permission) { + case PERMISSION_COPY: + return (permissions_ & kPDFPermissionCopyMask) != 0; + case PERMISSION_COPY_ACCESSIBLE: + return (permissions_ & kPDFPermissionCopyAccessibleMask) != 0; + case PERMISSION_PRINT_LOW_QUALITY: + return (permissions_ & kPDFPermissionPrintLowQualityMask) != 0; + case PERMISSION_PRINT_HIGH_QUALITY: + return (permissions_ & kPDFPermissionPrintLowQualityMask) != 0 && + (permissions_ & kPDFPermissionPrintHighQualityMask) != 0; + default: + return true; + }; +} + +void PDFiumEngine::SelectAll() { + SelectionChangeInvalidator selection_invalidator(this); + + selection_.clear(); + for (size_t i = 0; i < pages_.size(); ++i) + if (pages_[i]->available()) { + selection_.push_back(PDFiumRange(pages_[i], 0, + pages_[i]->GetCharCount())); + } +} + +int PDFiumEngine::GetNumberOfPages() { + return pages_.size(); +} + +int PDFiumEngine::GetNamedDestinationPage(const std::string& destination) { + // Look for the destination. + FPDF_DEST dest = FPDF_GetNamedDestByName(doc_, destination.c_str()); + if (!dest) { + // Look for a bookmark with the same name. + base::string16 destination_wide = base::UTF8ToUTF16(destination); + FPDF_WIDESTRING destination_pdf_wide = + reinterpret_cast(destination_wide.c_str()); + FPDF_BOOKMARK bookmark = FPDFBookmark_Find(doc_, destination_pdf_wide); + if (!bookmark) + return -1; + dest = FPDFBookmark_GetDest(doc_, bookmark); + } + return dest ? FPDFDest_GetPageIndex(doc_, dest) : -1; +} + +int PDFiumEngine::GetFirstVisiblePage() { + CalculateVisiblePages(); + return first_visible_page_; +} + +int PDFiumEngine::GetMostVisiblePage() { + CalculateVisiblePages(); + return most_visible_page_; +} + +pp::Rect PDFiumEngine::GetPageRect(int index) { + pp::Rect rc(pages_[index]->rect()); + rc.Inset(-kPageShadowLeft, -kPageShadowTop, + -kPageShadowRight, -kPageShadowBottom); + return rc; +} + +pp::Rect PDFiumEngine::GetPageContentsRect(int index) { + return GetScreenRect(pages_[index]->rect()); +} + +void PDFiumEngine::PaintThumbnail(pp::ImageData* image_data, int index) { + FPDF_BITMAP bitmap = FPDFBitmap_CreateEx( + image_data->size().width(), image_data->size().height(), + FPDFBitmap_BGRx, image_data->data(), image_data->stride()); + + if (pages_[index]->available()) { + FPDFBitmap_FillRect(bitmap, 0, 0, image_data->size().width(), + image_data->size().height(), 0xFFFFFFFF); + + FPDF_RenderPageBitmap( + bitmap, pages_[index]->GetPage(), 0, 0, image_data->size().width(), + image_data->size().height(), 0, GetRenderingFlags()); + } else { + FPDFBitmap_FillRect(bitmap, 0, 0, image_data->size().width(), + image_data->size().height(), kPendingPageColor); + } + + FPDFBitmap_Destroy(bitmap); +} + +void PDFiumEngine::SetGrayscale(bool grayscale) { + render_grayscale_ = grayscale; +} + +void PDFiumEngine::OnCallback(int id) { + if (!timers_.count(id)) + return; + + timers_[id].second(id); + if (timers_.count(id)) // The callback might delete the timer. + client_->ScheduleCallback(id, timers_[id].first); +} + +std::string PDFiumEngine::GetPageAsJSON(int index) { + if (!(HasPermission(PERMISSION_COPY) || + HasPermission(PERMISSION_COPY_ACCESSIBLE))) { + return "{}"; + } + + if (index < 0 || static_cast(index) > pages_.size() - 1) + return "{}"; + + scoped_ptr node( + pages_[index]->GetAccessibleContentAsValue(current_rotation_)); + std::string page_json; + base::JSONWriter::Write(node.get(), &page_json); + return page_json; +} + +bool PDFiumEngine::GetPrintScaling() { + return !!FPDF_VIEWERREF_GetPrintScaling(doc_); +} + +void PDFiumEngine::AppendBlankPages(int num_pages) { + DCHECK(num_pages != 0); + + if (!doc_) + return; + + selection_.clear(); + pending_pages_.clear(); + + // Delete all pages except the first one. + while (pages_.size() > 1) { + delete pages_.back(); + pages_.pop_back(); + FPDFPage_Delete(doc_, pages_.size()); + } + + // Calculate document size and all page sizes. + std::vector page_rects; + pp::Size page_size = GetPageSize(0); + page_size.Enlarge(kPageShadowLeft + kPageShadowRight, + kPageShadowTop + kPageShadowBottom); + pp::Size old_document_size = document_size_; + document_size_ = pp::Size(page_size.width(), 0); + for (int i = 0; i < num_pages; ++i) { + if (i != 0) { + // Add space for horizontal separator. + document_size_.Enlarge(0, kPageSeparatorThickness); + } + + pp::Rect rect(pp::Point(0, document_size_.height()), page_size); + page_rects.push_back(rect); + + document_size_.Enlarge(0, page_size.height()); + } + + // Create blank pages. + for (int i = 1; i < num_pages; ++i) { + pp::Rect page_rect(page_rects[i]); + page_rect.Inset(kPageShadowLeft, kPageShadowTop, + kPageShadowRight, kPageShadowBottom); + double width_in_points = + page_rect.width() * kPointsPerInch / kPixelsPerInch; + double height_in_points = + page_rect.height() * kPointsPerInch / kPixelsPerInch; + FPDFPage_New(doc_, i, width_in_points, height_in_points); + pages_.push_back(new PDFiumPage(this, i, page_rect, true)); + } + + CalculateVisiblePages(); + if (document_size_ != old_document_size) + client_->DocumentSizeUpdated(document_size_); +} + +void PDFiumEngine::LoadDocument() { + // Check if the document is ready for loading. If it isn't just bail for now, + // we will call LoadDocument() again later. + if (!doc_ && !doc_loader_.IsDocumentComplete() && + !FPDFAvail_IsDocAvail(fpdf_availability_, &download_hints_)) { + return; + } + + // If we're in the middle of getting a password, just return. We will retry + // loading the document after we get the password anyway. + if (getting_password_) + return; + + ScopedUnsupportedFeature scoped_unsupported_feature(this); + bool needs_password = false; + if (TryLoadingDoc(false, std::string(), &needs_password)) { + ContinueLoadingDocument(false, std::string()); + return; + } + if (needs_password) + GetPasswordAndLoad(); + else + client_->DocumentLoadFailed(); +} + +bool PDFiumEngine::TryLoadingDoc(bool with_password, + const std::string& password, + bool* needs_password) { + *needs_password = false; + if (doc_) + return true; + + const char* password_cstr = NULL; + if (with_password) { + password_cstr = password.c_str(); + password_tries_remaining_--; + } + if (doc_loader_.IsDocumentComplete()) + doc_ = FPDF_LoadCustomDocument(&file_access_, password_cstr); + else + doc_ = FPDFAvail_GetDocument(fpdf_availability_, password_cstr); + + if (!doc_ && FPDF_GetLastError() == FPDF_ERR_PASSWORD) + *needs_password = true; + + return doc_ != NULL; +} + +void PDFiumEngine::GetPasswordAndLoad() { + getting_password_ = true; + DCHECK(!doc_ && FPDF_GetLastError() == FPDF_ERR_PASSWORD); + client_->GetDocumentPassword(password_factory_.NewCallbackWithOutput( + &PDFiumEngine::OnGetPasswordComplete)); +} + +void PDFiumEngine::OnGetPasswordComplete(int32_t result, + const pp::Var& password) { + getting_password_ = false; + + bool password_given = false; + std::string password_text; + if (result == PP_OK && password.is_string()) { + password_text = password.AsString(); + if (!password_text.empty()) + password_given = true; + } + ContinueLoadingDocument(password_given, password_text); +} + +void PDFiumEngine::ContinueLoadingDocument( + bool has_password, + const std::string& password) { + ScopedUnsupportedFeature scoped_unsupported_feature(this); + + bool needs_password = false; + bool loaded = TryLoadingDoc(has_password, password, &needs_password); + bool password_incorrect = !loaded && has_password && needs_password; + if (password_incorrect && password_tries_remaining_ > 0) { + GetPasswordAndLoad(); + return; + } + + if (!doc_) { + client_->DocumentLoadFailed(); + return; + } + + if (FPDFDoc_GetPageMode(doc_) == PAGEMODE_USEOUTLINES) + client_->DocumentHasUnsupportedFeature("Bookmarks"); + + permissions_ = FPDF_GetDocPermissions(doc_); + + if (!form_) { + // Only returns 0 when data isn't available. If form data is downloaded, or + // if this isn't a form, returns positive values. + if (!doc_loader_.IsDocumentComplete() && + !FPDFAvail_IsFormAvail(fpdf_availability_, &download_hints_)) { + return; + } + + form_ = FPDFDOC_InitFormFillEnviroument( + doc_, static_cast(this)); +#ifdef _TEST_XFA + FPDF_LoadXFA(doc_); +#endif + + FPDF_SetFormFieldHighlightColor(form_, 0, kFormHighlightColor); + FPDF_SetFormFieldHighlightAlpha(form_, kFormHighlightAlpha); + } + + if (!doc_loader_.IsDocumentComplete()) { + // Check if the first page is available. In a linearized PDF, that is not + // always page 0. Doing this gives us the default page size, since when the + // document is available, the first page is available as well. + CheckPageAvailable(FPDFAvail_GetFirstPageNum(doc_), &pending_pages_); + } + + LoadPageInfo(false); + + if (doc_loader_.IsDocumentComplete()) + FinishLoadingDocument(); +} + +void PDFiumEngine::LoadPageInfo(bool reload) { + pending_pages_.clear(); + pp::Size old_document_size = document_size_; + document_size_ = pp::Size(); + std::vector page_rects; + int page_count = FPDF_GetPageCount(doc_); + bool doc_complete = doc_loader_.IsDocumentComplete(); + for (int i = 0; i < page_count; ++i) { + if (i != 0) { + // Add space for horizontal separator. + document_size_.Enlarge(0, kPageSeparatorThickness); + } + + // Get page availability. If reload==false, and document is not loaded yet + // (we are using async loading) - mark all pages as unavailable. + // If reload==true (we have document constructed already), get page + // availability flag from already existing PDFiumPage class. + bool page_available = reload ? pages_[i]->available() : doc_complete; + + pp::Size size = page_available ? GetPageSize(i) : default_page_size_; + size.Enlarge(kPageShadowLeft + kPageShadowRight, + kPageShadowTop + kPageShadowBottom); + pp::Rect rect(pp::Point(0, document_size_.height()), size); + page_rects.push_back(rect); + + if (size.width() > document_size_.width()) + document_size_.set_width(size.width()); + + document_size_.Enlarge(0, size.height()); + } + + for (int i = 0; i < page_count; ++i) { + // Center pages relative to the entire document. + page_rects[i].set_x((document_size_.width() - page_rects[i].width()) / 2); + pp::Rect page_rect(page_rects[i]); + page_rect.Inset(kPageShadowLeft, kPageShadowTop, + kPageShadowRight, kPageShadowBottom); + if (reload) { + pages_[i]->set_rect(page_rect); + } else { + pages_.push_back(new PDFiumPage(this, i, page_rect, doc_complete)); + } + } + + CalculateVisiblePages(); + if (document_size_ != old_document_size) + client_->DocumentSizeUpdated(document_size_); +} + +void PDFiumEngine::CalculateVisiblePages() { + // Clear pending requests queue, since it may contain requests to the pages + // that are already invisible (after scrolling for example). + pending_pages_.clear(); + doc_loader_.ClearPendingRequests(); + + visible_pages_.clear(); + pp::Rect visible_rect(plugin_size_); + for (size_t i = 0; i < pages_.size(); ++i) { + // Check an entire PageScreenRect, since we might need to repaint side + // borders and shadows even if the page itself is not visible. + // For example, when user use pdf with different page sizes and zoomed in + // outside page area. + if (visible_rect.Intersects(GetPageScreenRect(i))) { + visible_pages_.push_back(i); + CheckPageAvailable(i, &pending_pages_); + } else { + // Need to unload pages when we're not using them, since some PDFs use a + // lot of memory. See http://crbug.com/48791 + if (defer_page_unload_) { + deferred_page_unloads_.push_back(i); + } else { + pages_[i]->Unload(); + } + + // If the last mouse down was on a page that's no longer visible, reset + // that variable so that we don't send keyboard events to it (the focus + // will be lost when the page is first closed anyways). + if (static_cast(i) == last_page_mouse_down_) + last_page_mouse_down_ = -1; + } + } + + // Any pending highlighting of form fields will be invalid since these are in + // screen coordinates. + form_highlights_.clear(); + + if (visible_pages_.size() == 0) + first_visible_page_ = -1; + else + first_visible_page_ = visible_pages_.front(); + + int most_visible_page = first_visible_page_; + // Check if the next page is more visible than the first one. + if (most_visible_page != -1 && + pages_.size() > 0 && + most_visible_page < static_cast(pages_.size()) - 1) { + pp::Rect rc_first = + visible_rect.Intersect(GetPageScreenRect(most_visible_page)); + pp::Rect rc_next = + visible_rect.Intersect(GetPageScreenRect(most_visible_page + 1)); + if (rc_next.height() > rc_first.height()) + most_visible_page++; + } + + SetCurrentPage(most_visible_page); +} + +bool PDFiumEngine::IsPageVisible(int index) const { + for (size_t i = 0; i < visible_pages_.size(); ++i) { + if (visible_pages_[i] == index) + return true; + } + + return false; +} + +bool PDFiumEngine::CheckPageAvailable(int index, std::vector* pending) { + if (!doc_ || !form_) + return false; + + if (static_cast(pages_.size()) > index && pages_[index]->available()) + return true; + + if (!FPDFAvail_IsPageAvail(fpdf_availability_, index, &download_hints_)) { + size_t j; + for (j = 0; j < pending->size(); ++j) { + if ((*pending)[j] == index) + break; + } + + if (j == pending->size()) + pending->push_back(index); + return false; + } + + if (static_cast(pages_.size()) > index) + pages_[index]->set_available(true); + if (!default_page_size_.GetArea()) + default_page_size_ = GetPageSize(index); + return true; +} + +pp::Size PDFiumEngine::GetPageSize(int index) { + pp::Size size; + double width_in_points = 0; + double height_in_points = 0; + int rv = FPDF_GetPageSizeByIndex( + doc_, index, &width_in_points, &height_in_points); + + if (rv) { + int width_in_pixels = static_cast( + width_in_points * kPixelsPerInch / kPointsPerInch); + int height_in_pixels = static_cast( + height_in_points * kPixelsPerInch / kPointsPerInch); + if (current_rotation_ % 2 == 1) + std::swap(width_in_pixels, height_in_pixels); + size = pp::Size(width_in_pixels, height_in_pixels); + } + return size; +} + +int PDFiumEngine::StartPaint(int page_index, const pp::Rect& dirty) { + // For the first time we hit paint, do nothing and just record the paint for + // the next callback. This keeps the UI responsive in case the user is doing + // a lot of scrolling. + ProgressivePaint progressive; + progressive.rect = dirty; + progressive.page_index = page_index; + progressive.bitmap = NULL; + progressive.painted_ = false; + progressive_paints_.push_back(progressive); + return progressive_paints_.size() - 1; +} + +bool PDFiumEngine::ContinuePaint(int progressive_index, + pp::ImageData* image_data) { +#if defined(OS_LINUX) + g_last_instance_id = client_->GetPluginInstance()->pp_instance(); +#endif + + int rv; + int page_index = progressive_paints_[progressive_index].page_index; + last_progressive_start_time_ = base::Time::Now(); + if (progressive_paints_[progressive_index].bitmap) { + rv = FPDF_RenderPage_Continue( + pages_[page_index]->GetPage(), static_cast(this)); + } else { + pp::Rect dirty = progressive_paints_[progressive_index].rect; + progressive_paints_[progressive_index].bitmap = CreateBitmap(dirty, + image_data); + int start_x, start_y, size_x, size_y; + GetPDFiumRect( + page_index, dirty, &start_x, &start_y, &size_x, &size_y); + FPDFBitmap_FillRect(progressive_paints_[progressive_index].bitmap, start_x, + start_y, size_x, size_y, 0xFFFFFFFF); + rv = FPDF_RenderPageBitmap_Start( + progressive_paints_[progressive_index].bitmap, + pages_[page_index]->GetPage(), start_x, start_y, size_x, size_y, + current_rotation_, + GetRenderingFlags(), static_cast(this)); + } + return rv != FPDF_RENDER_TOBECOUNTINUED; +} + +void PDFiumEngine::FinishPaint(int progressive_index, + pp::ImageData* image_data) { + int page_index = progressive_paints_[progressive_index].page_index; + pp::Rect dirty_in_screen = progressive_paints_[progressive_index].rect; + FPDF_BITMAP bitmap = progressive_paints_[progressive_index].bitmap; + int start_x, start_y, size_x, size_y; + GetPDFiumRect( + page_index, dirty_in_screen, &start_x, &start_y, &size_x, &size_y); + + // Draw the forms. + FPDF_FFLDraw( + form_, bitmap, pages_[page_index]->GetPage(), start_x, start_y, size_x, + size_y, current_rotation_, GetRenderingFlags()); + + FillPageSides(progressive_index); + + // Paint the page shadows. + PaintPageShadow(progressive_index, image_data); + + DrawSelections(progressive_index, image_data); + + FPDF_RenderPage_Close(pages_[page_index]->GetPage()); + FPDFBitmap_Destroy(bitmap); + progressive_paints_.erase(progressive_paints_.begin() + progressive_index); + + client_->DocumentPaintOccurred(); +} + +void PDFiumEngine::CancelPaints() { + for (size_t i = 0; i < progressive_paints_.size(); ++i) { + FPDF_RenderPage_Close(pages_[progressive_paints_[i].page_index]->GetPage()); + FPDFBitmap_Destroy(progressive_paints_[i].bitmap); + } + progressive_paints_.clear(); +} + +void PDFiumEngine::FillPageSides(int progressive_index) { + int page_index = progressive_paints_[progressive_index].page_index; + pp::Rect dirty_in_screen = progressive_paints_[progressive_index].rect; + FPDF_BITMAP bitmap = progressive_paints_[progressive_index].bitmap; + + pp::Rect page_rect = pages_[page_index]->rect(); + if (page_rect.x() > 0) { + pp::Rect left(0, + page_rect.y() - kPageShadowTop, + page_rect.x() - kPageShadowLeft, + page_rect.height() + kPageShadowTop + + kPageShadowBottom + kPageSeparatorThickness); + left = GetScreenRect(left).Intersect(dirty_in_screen); + + FPDFBitmap_FillRect(bitmap, left.x() - dirty_in_screen.x(), + left.y() - dirty_in_screen.y(), left.width(), + left.height(), kBackgroundColor); + } + + if (page_rect.right() < document_size_.width()) { + pp::Rect right(page_rect.right() + kPageShadowRight, + page_rect.y() - kPageShadowTop, + document_size_.width() - page_rect.right() - + kPageShadowRight, + page_rect.height() + kPageShadowTop + + kPageShadowBottom + kPageSeparatorThickness); + right = GetScreenRect(right).Intersect(dirty_in_screen); + + FPDFBitmap_FillRect(bitmap, right.x() - dirty_in_screen.x(), + right.y() - dirty_in_screen.y(), right.width(), + right.height(), kBackgroundColor); + } + + // Paint separator. + pp::Rect bottom(page_rect.x() - kPageShadowLeft, + page_rect.bottom() + kPageShadowBottom, + page_rect.width() + kPageShadowLeft + kPageShadowRight, + kPageSeparatorThickness); + bottom = GetScreenRect(bottom).Intersect(dirty_in_screen); + + FPDFBitmap_FillRect(bitmap, bottom.x() - dirty_in_screen.x(), + bottom.y() - dirty_in_screen.y(), bottom.width(), + bottom.height(), kBackgroundColor); +} + +void PDFiumEngine::PaintPageShadow(int progressive_index, + pp::ImageData* image_data) { + int page_index = progressive_paints_[progressive_index].page_index; + pp::Rect dirty_in_screen = progressive_paints_[progressive_index].rect; + pp::Rect page_rect = pages_[page_index]->rect(); + pp::Rect shadow_rect(page_rect); + shadow_rect.Inset(-kPageShadowLeft, -kPageShadowTop, + -kPageShadowRight, -kPageShadowBottom); + + // Due to the rounding errors of the GetScreenRect it is possible to get + // different size shadows on the left and right sides even they are defined + // the same. To fix this issue let's calculate shadow rect and then shrink + // it by the size of the shadows. + shadow_rect = GetScreenRect(shadow_rect); + page_rect = shadow_rect; + + page_rect.Inset(static_cast(ceil(kPageShadowLeft * current_zoom_)), + static_cast(ceil(kPageShadowTop * current_zoom_)), + static_cast(ceil(kPageShadowRight * current_zoom_)), + static_cast(ceil(kPageShadowBottom * current_zoom_))); + + DrawPageShadow(page_rect, shadow_rect, dirty_in_screen, image_data); +} + +void PDFiumEngine::DrawSelections(int progressive_index, + pp::ImageData* image_data) { + int page_index = progressive_paints_[progressive_index].page_index; + pp::Rect dirty_in_screen = progressive_paints_[progressive_index].rect; + + void* region = NULL; + int stride; + GetRegion(dirty_in_screen.point(), image_data, ®ion, &stride); + + std::vector highlighted_rects; + pp::Rect visible_rect = GetVisibleRect(); + for (size_t k = 0; k < selection_.size(); ++k) { + if (selection_[k].page_index() != page_index) + continue; + std::vector rects = selection_[k].GetScreenRects( + visible_rect.point(), current_zoom_, current_rotation_); + for (size_t j = 0; j < rects.size(); ++j) { + pp::Rect visible_selection = rects[j].Intersect(dirty_in_screen); + if (visible_selection.IsEmpty()) + continue; + + visible_selection.Offset( + -dirty_in_screen.point().x(), -dirty_in_screen.point().y()); + Highlight(region, stride, visible_selection, &highlighted_rects); + } + } + + for (size_t k = 0; k < form_highlights_.size(); ++k) { + pp::Rect visible_selection = form_highlights_[k].Intersect(dirty_in_screen); + if (visible_selection.IsEmpty()) + continue; + + visible_selection.Offset( + -dirty_in_screen.point().x(), -dirty_in_screen.point().y()); + Highlight(region, stride, visible_selection, &highlighted_rects); + } + form_highlights_.clear(); +} + +void PDFiumEngine::PaintUnavailablePage(int page_index, + const pp::Rect& dirty, + pp::ImageData* image_data) { + int start_x, start_y, size_x, size_y; + GetPDFiumRect(page_index, dirty, &start_x, &start_y, &size_x, &size_y); + FPDF_BITMAP bitmap = CreateBitmap(dirty, image_data); + FPDFBitmap_FillRect(bitmap, start_x, start_y, size_x, size_y, + kPendingPageColor); + + pp::Rect loading_text_in_screen( + pages_[page_index]->rect().width() / 2, + pages_[page_index]->rect().y() + kLoadingTextVerticalOffset, 0, 0); + loading_text_in_screen = GetScreenRect(loading_text_in_screen); + FPDFBitmap_Destroy(bitmap); +} + +int PDFiumEngine::GetProgressiveIndex(int page_index) const { + for (size_t i = 0; i < progressive_paints_.size(); ++i) { + if (progressive_paints_[i].page_index == page_index) + return i; + } + return -1; +} + +FPDF_BITMAP PDFiumEngine::CreateBitmap(const pp::Rect& rect, + pp::ImageData* image_data) const { + void* region; + int stride; + GetRegion(rect.point(), image_data, ®ion, &stride); + if (!region) + return NULL; + return FPDFBitmap_CreateEx( + rect.width(), rect.height(), FPDFBitmap_BGRx, region, stride); +} + +void PDFiumEngine::GetPDFiumRect( + int page_index, const pp::Rect& rect, int* start_x, int* start_y, + int* size_x, int* size_y) const { + pp::Rect page_rect = GetScreenRect(pages_[page_index]->rect()); + page_rect.Offset(-rect.x(), -rect.y()); + + *start_x = page_rect.x(); + *start_y = page_rect.y(); + *size_x = page_rect.width(); + *size_y = page_rect.height(); +} + +int PDFiumEngine::GetRenderingFlags() const { + int flags = FPDF_LCD_TEXT | FPDF_NO_CATCH; + if (render_grayscale_) + flags |= FPDF_GRAYSCALE; + if (client_->IsPrintPreview()) + flags |= FPDF_PRINTING; + return flags; +} + +pp::Rect PDFiumEngine::GetVisibleRect() const { + pp::Rect rv; + rv.set_x(static_cast(position_.x() / current_zoom_)); + rv.set_y(static_cast(position_.y() / current_zoom_)); + rv.set_width(static_cast(ceil(plugin_size_.width() / current_zoom_))); + rv.set_height(static_cast(ceil(plugin_size_.height() / current_zoom_))); + return rv; +} + +pp::Rect PDFiumEngine::GetPageScreenRect(int page_index) const { + // Since we use this rect for creating the PDFium bitmap, also include other + // areas around the page that we might need to update such as the page + // separator and the sides if the page is narrower than the document. + return GetScreenRect(pp::Rect( + 0, + pages_[page_index]->rect().y() - kPageShadowTop, + document_size_.width(), + pages_[page_index]->rect().height() + kPageShadowTop + + kPageShadowBottom + kPageSeparatorThickness)); +} + +pp::Rect PDFiumEngine::GetScreenRect(const pp::Rect& rect) const { + pp::Rect rv; + int right = + static_cast(ceil(rect.right() * current_zoom_ - position_.x())); + int bottom = + static_cast(ceil(rect.bottom() * current_zoom_ - position_.y())); + + rv.set_x(static_cast(rect.x() * current_zoom_ - position_.x())); + rv.set_y(static_cast(rect.y() * current_zoom_ - position_.y())); + rv.set_width(right - rv.x()); + rv.set_height(bottom - rv.y()); + return rv; +} + +void PDFiumEngine::Highlight(void* buffer, + int stride, + const pp::Rect& rect, + std::vector* highlighted_rects) { + if (!buffer) + return; + + pp::Rect new_rect = rect; + for (size_t i = 0; i < highlighted_rects->size(); ++i) + new_rect = new_rect.Subtract((*highlighted_rects)[i]); + + highlighted_rects->push_back(new_rect); + int l = new_rect.x(); + int t = new_rect.y(); + int w = new_rect.width(); + int h = new_rect.height(); + + for (int y = t; y < t + h; ++y) { + for (int x = l; x < l + w; ++x) { + uint8* pixel = static_cast(buffer) + y * stride + x * 4; + // This is our highlight color. + pixel[0] = static_cast( + pixel[0] * (kHighlightColorB / 255.0)); + pixel[1] = static_cast( + pixel[1] * (kHighlightColorG / 255.0)); + pixel[2] = static_cast( + pixel[2] * (kHighlightColorR / 255.0)); + } + } +} + +PDFiumEngine::SelectionChangeInvalidator::SelectionChangeInvalidator( + PDFiumEngine* engine) : engine_(engine) { + previous_origin_ = engine_->GetVisibleRect().point(); + GetVisibleSelectionsScreenRects(&old_selections_); +} + +PDFiumEngine::SelectionChangeInvalidator::~SelectionChangeInvalidator() { + // Offset the old selections if the document scrolled since we recorded them. + pp::Point offset = previous_origin_ - engine_->GetVisibleRect().point(); + for (size_t i = 0; i < old_selections_.size(); ++i) + old_selections_[i].Offset(offset); + + std::vector new_selections; + GetVisibleSelectionsScreenRects(&new_selections); + for (size_t i = 0; i < new_selections.size(); ++i) { + for (size_t j = 0; j < old_selections_.size(); ++j) { + if (!old_selections_[j].IsEmpty() && + new_selections[i] == old_selections_[j]) { + // Rectangle was selected before and after, so no need to invalidate it. + // Mark the rectangles by setting them to empty. + new_selections[i] = old_selections_[j] = pp::Rect(); + break; + } + } + } + + for (size_t i = 0; i < old_selections_.size(); ++i) { + if (!old_selections_[i].IsEmpty()) + engine_->client_->Invalidate(old_selections_[i]); + } + for (size_t i = 0; i < new_selections.size(); ++i) { + if (!new_selections[i].IsEmpty()) + engine_->client_->Invalidate(new_selections[i]); + } + engine_->OnSelectionChanged(); +} + +void +PDFiumEngine::SelectionChangeInvalidator::GetVisibleSelectionsScreenRects( + std::vector* rects) { + pp::Rect visible_rect = engine_->GetVisibleRect(); + for (size_t i = 0; i < engine_->selection_.size(); ++i) { + int page_index = engine_->selection_[i].page_index(); + if (!engine_->IsPageVisible(page_index)) + continue; // This selection is on a page that's not currently visible. + + std::vector selection_rects = + engine_->selection_[i].GetScreenRects( + visible_rect.point(), + engine_->current_zoom_, + engine_->current_rotation_); + rects->insert(rects->end(), selection_rects.begin(), selection_rects.end()); + } +} + +PDFiumEngine::MouseDownState::MouseDownState( + const PDFiumPage::Area& area, + const PDFiumPage::LinkTarget& target) + : area_(area), target_(target) { +} + +PDFiumEngine::MouseDownState::~MouseDownState() { +} + +void PDFiumEngine::MouseDownState::Set(const PDFiumPage::Area& area, + const PDFiumPage::LinkTarget& target) { + area_ = area; + target_ = target; +} + +void PDFiumEngine::MouseDownState::Reset() { + area_ = PDFiumPage::NONSELECTABLE_AREA; + target_ = PDFiumPage::LinkTarget(); +} + +bool PDFiumEngine::MouseDownState::Matches( + const PDFiumPage::Area& area, + const PDFiumPage::LinkTarget& target) const { + if (area_ == area) { + if (area == PDFiumPage::WEBLINK_AREA) + return target_.url == target.url; + if (area == PDFiumPage::DOCLINK_AREA) + return target_.page == target.page; + return true; + } + return false; +} + +void PDFiumEngine::DeviceToPage(int page_index, + float device_x, + float device_y, + double* page_x, + double* page_y) { + *page_x = *page_y = 0; + int temp_x = static_cast((device_x + position_.x())/ current_zoom_ - + pages_[page_index]->rect().x()); + int temp_y = static_cast((device_y + position_.y())/ current_zoom_ - + pages_[page_index]->rect().y()); + FPDF_DeviceToPage( + pages_[page_index]->GetPage(), 0, 0, + pages_[page_index]->rect().width(), pages_[page_index]->rect().height(), + current_rotation_, temp_x, temp_y, page_x, page_y); +} + +int PDFiumEngine::GetVisiblePageIndex(FPDF_PAGE page) { + for (size_t i = 0; i < visible_pages_.size(); ++i) { + if (pages_[visible_pages_[i]]->GetPage() == page) + return visible_pages_[i]; + } + return -1; +} + +void PDFiumEngine::SetCurrentPage(int index) { + if (index == most_visible_page_ || !form_) + return; + if (most_visible_page_ != -1 && called_do_document_action_) { + FPDF_PAGE old_page = pages_[most_visible_page_]->GetPage(); + FORM_DoPageAAction(old_page, form_, FPDFPAGE_AACTION_CLOSE); + } + most_visible_page_ = index; +#if defined(OS_LINUX) + g_last_instance_id = client_->GetPluginInstance()->pp_instance(); +#endif + if (most_visible_page_ != -1 && called_do_document_action_) { + FPDF_PAGE new_page = pages_[most_visible_page_]->GetPage(); + FORM_DoPageAAction(new_page, form_, FPDFPAGE_AACTION_OPEN); + } +} + +void PDFiumEngine::TransformPDFPageForPrinting( + FPDF_PAGE page, + const PP_PrintSettings_Dev& print_settings) { + // Get the source page width and height in points. + const double src_page_width = FPDF_GetPageWidth(page); + const double src_page_height = FPDF_GetPageHeight(page); + + const int src_page_rotation = FPDFPage_GetRotation(page); + const bool fit_to_page = print_settings.print_scaling_option == + PP_PRINTSCALINGOPTION_FIT_TO_PRINTABLE_AREA; + + pp::Size page_size(print_settings.paper_size); + pp::Rect content_rect(print_settings.printable_area); + const bool rotated = (src_page_rotation % 2 == 1); + SetPageSizeAndContentRect(rotated, + src_page_width > src_page_height, + &page_size, + &content_rect); + + // Compute the screen page width and height in points. + const int actual_page_width = + rotated ? page_size.height() : page_size.width(); + const int actual_page_height = + rotated ? page_size.width() : page_size.height(); + + const double scale_factor = CalculateScaleFactor(fit_to_page, content_rect, + src_page_width, + src_page_height, rotated); + + // Calculate positions for the clip box. + ClipBox source_clip_box; + CalculateClipBoxBoundary(page, scale_factor, rotated, &source_clip_box); + + // Calculate the translation offset values. + double offset_x = 0; + double offset_y = 0; + if (fit_to_page) { + CalculateScaledClipBoxOffset(content_rect, source_clip_box, &offset_x, + &offset_y); + } else { + CalculateNonScaledClipBoxOffset(content_rect, src_page_rotation, + actual_page_width, actual_page_height, + source_clip_box, &offset_x, &offset_y); + } + + // Reset the media box and crop box. When the page has crop box and media box, + // the plugin will display the crop box contents and not the entire media box. + // If the pages have different crop box values, the plugin will display a + // document of multiple page sizes. To give better user experience, we + // decided to have same crop box and media box values. Hence, the user will + // see a list of uniform pages. + FPDFPage_SetMediaBox(page, 0, 0, page_size.width(), page_size.height()); + FPDFPage_SetCropBox(page, 0, 0, page_size.width(), page_size.height()); + + // Transformation is not required, return. Do this check only after updating + // the media box and crop box. For more detailed information, please refer to + // the comment block right before FPDF_SetMediaBox and FPDF_GetMediaBox calls. + if (scale_factor == 1.0 && offset_x == 0 && offset_y == 0) + return; + + + // All the positions have been calculated, now manipulate the PDF. + FS_MATRIX matrix = {static_cast(scale_factor), + 0, + 0, + static_cast(scale_factor), + static_cast(offset_x), + static_cast(offset_y)}; + FS_RECTF cliprect = {static_cast(source_clip_box.left+offset_x), + static_cast(source_clip_box.top+offset_y), + static_cast(source_clip_box.right+offset_x), + static_cast(source_clip_box.bottom+offset_y)}; + FPDFPage_TransFormWithClip(page, &matrix, &cliprect); + FPDFPage_TransformAnnots(page, scale_factor, 0, 0, scale_factor, + offset_x, offset_y); +} + +void PDFiumEngine::DrawPageShadow(const pp::Rect& page_rc, + const pp::Rect& shadow_rc, + const pp::Rect& clip_rc, + pp::ImageData* image_data) { + pp::Rect page_rect(page_rc); + page_rect.Offset(page_offset_); + + pp::Rect shadow_rect(shadow_rc); + shadow_rect.Offset(page_offset_); + + pp::Rect clip_rect(clip_rc); + clip_rect.Offset(page_offset_); + + // Page drop shadow parameters. + const double factor = 0.5; + uint32 depth = std::max( + std::max(page_rect.x() - shadow_rect.x(), + page_rect.y() - shadow_rect.y()), + std::max(shadow_rect.right() - page_rect.right(), + shadow_rect.bottom() - page_rect.bottom())); + depth = static_cast(depth * 1.5) + 1; + + // We need to check depth only to verify our copy of shadow matrix is correct. + if (!page_shadow_.get() || page_shadow_->depth() != depth) + page_shadow_.reset(new ShadowMatrix(depth, factor, kBackgroundColor)); + + DCHECK(!image_data->is_null()); + DrawShadow(image_data, shadow_rect, page_rect, clip_rect, *page_shadow_); +} + +void PDFiumEngine::GetRegion(const pp::Point& location, + pp::ImageData* image_data, + void** region, + int* stride) const { + if (image_data->is_null()) { + DCHECK(plugin_size_.IsEmpty()); + *stride = 0; + *region = NULL; + return; + } + char* buffer = static_cast(image_data->data()); + *stride = image_data->stride(); + + pp::Point offset_location = location + page_offset_; + // TODO: update this when we support BIDI and scrollbars can be on the left. + if (!buffer || + !pp::Rect(page_offset_, plugin_size_).Contains(offset_location)) { + *region = NULL; + return; + } + + buffer += location.y() * (*stride); + buffer += (location.x() + page_offset_.x()) * 4; + *region = buffer; +} + +void PDFiumEngine::OnSelectionChanged() { + if (HasPermission(PDFEngine::PERMISSION_COPY)) + pp::PDF::SetSelectedText(GetPluginInstance(), GetSelectedText().c_str()); +} + +void PDFiumEngine::Form_Invalidate(FPDF_FORMFILLINFO* param, + FPDF_PAGE page, + double left, + double top, + double right, + double bottom) { + PDFiumEngine* engine = static_cast(param); + int page_index = engine->GetVisiblePageIndex(page); + if (page_index == -1) { + // This can sometime happen when the page is closed because it went off + // screen, and PDFium invalidates the control as it's being deleted. + return; + } + + pp::Rect rect = engine->pages_[page_index]->PageToScreen( + engine->GetVisibleRect().point(), engine->current_zoom_, left, top, right, + bottom, engine->current_rotation_); + engine->client_->Invalidate(rect); +} + +void PDFiumEngine::Form_OutputSelectedRect(FPDF_FORMFILLINFO* param, + FPDF_PAGE page, + double left, + double top, + double right, + double bottom) { + PDFiumEngine* engine = static_cast(param); + int page_index = engine->GetVisiblePageIndex(page); + if (page_index == -1) { + NOTREACHED(); + return; + } + pp::Rect rect = engine->pages_[page_index]->PageToScreen( + engine->GetVisibleRect().point(), engine->current_zoom_, left, top, right, + bottom, engine->current_rotation_); + engine->form_highlights_.push_back(rect); +} + +void PDFiumEngine::Form_SetCursor(FPDF_FORMFILLINFO* param, int cursor_type) { + // We don't need this since it's not enough to change the cursor in all + // scenarios. Instead, we check which form field we're under in OnMouseMove. +} + +int PDFiumEngine::Form_SetTimer(FPDF_FORMFILLINFO* param, + int elapse, + TimerCallback timer_func) { + PDFiumEngine* engine = static_cast(param); + engine->timers_[++engine->next_timer_id_] = + std::pair(elapse, timer_func); + engine->client_->ScheduleCallback(engine->next_timer_id_, elapse); + return engine->next_timer_id_; +} + +void PDFiumEngine::Form_KillTimer(FPDF_FORMFILLINFO* param, int timer_id) { + PDFiumEngine* engine = static_cast(param); + engine->timers_.erase(timer_id); +} + +FPDF_SYSTEMTIME PDFiumEngine::Form_GetLocalTime(FPDF_FORMFILLINFO* param) { + base::Time time = base::Time::Now(); + base::Time::Exploded exploded; + time.LocalExplode(&exploded); + + FPDF_SYSTEMTIME rv; + rv.wYear = exploded.year; + rv.wMonth = exploded.month; + rv.wDayOfWeek = exploded.day_of_week; + rv.wDay = exploded.day_of_month; + rv.wHour = exploded.hour; + rv.wMinute = exploded.minute; + rv.wSecond = exploded.second; + rv.wMilliseconds = exploded.millisecond; + return rv; +} + +void PDFiumEngine::Form_OnChange(FPDF_FORMFILLINFO* param) { + // Don't care about. +} + +FPDF_PAGE PDFiumEngine::Form_GetPage(FPDF_FORMFILLINFO* param, + FPDF_DOCUMENT document, + int page_index) { + PDFiumEngine* engine = static_cast(param); + if (page_index < 0 || page_index >= static_cast(engine->pages_.size())) + return NULL; + return engine->pages_[page_index]->GetPage(); +} + +FPDF_PAGE PDFiumEngine::Form_GetCurrentPage(FPDF_FORMFILLINFO* param, + FPDF_DOCUMENT document) { + // TODO(jam): find out what this is used for. + PDFiumEngine* engine = static_cast(param); + int index = engine->last_page_mouse_down_; + if (index == -1) { + index = engine->GetMostVisiblePage(); + if (index == -1) { + NOTREACHED(); + return NULL; + } + } + + return engine->pages_[index]->GetPage(); +} + +int PDFiumEngine::Form_GetRotation(FPDF_FORMFILLINFO* param, FPDF_PAGE page) { + return 0; +} + +void PDFiumEngine::Form_ExecuteNamedAction(FPDF_FORMFILLINFO* param, + FPDF_BYTESTRING named_action) { + PDFiumEngine* engine = static_cast(param); + std::string action(named_action); + if (action == "Print") { + engine->client_->Print(); + return; + } + + int index = engine->last_page_mouse_down_; + /* Don't try to calculate the most visible page if we don't have a left click + before this event (this code originally copied Form_GetCurrentPage which of + course needs to do that and which doesn't have recursion). This can end up + causing infinite recursion. See http://crbug.com/240413 for more + information. Either way, it's not necessary for the spec'd list of named + actions. + if (index == -1) + index = engine->GetMostVisiblePage(); + */ + if (index == -1) + return; + + // This is the only list of named actions per the spec (see 12.6.4.11). Adobe + // Reader supports more, like FitWidth, but since they're not part of the spec + // and we haven't got bugs about them, no need to now. + if (action == "NextPage") { + engine->client_->ScrollToPage(index + 1); + } else if (action == "PrevPage") { + engine->client_->ScrollToPage(index - 1); + } else if (action == "FirstPage") { + engine->client_->ScrollToPage(0); + } else if (action == "LastPage") { + engine->client_->ScrollToPage(engine->pages_.size() - 1); + } +} + +void PDFiumEngine::Form_SetTextFieldFocus(FPDF_FORMFILLINFO* param, + FPDF_WIDESTRING value, + FPDF_DWORD valueLen, + FPDF_BOOL is_focus) { + // Do nothing for now. + // TODO(gene): use this signal to trigger OSK. +} + +void PDFiumEngine::Form_DoURIAction(FPDF_FORMFILLINFO* param, + FPDF_BYTESTRING uri) { + PDFiumEngine* engine = static_cast(param); + engine->client_->NavigateTo(std::string(uri), false); +} + +void PDFiumEngine::Form_DoGoToAction(FPDF_FORMFILLINFO* param, + int page_index, + int zoom_mode, + float* position_array, + int size_of_array) { + PDFiumEngine* engine = static_cast(param); + engine->client_->ScrollToPage(page_index); +} + +int PDFiumEngine::Form_Alert(IPDF_JSPLATFORM* param, + FPDF_WIDESTRING message, + FPDF_WIDESTRING title, + int type, + int icon) { + // See fpdfformfill.h for these values. + enum AlertType { + ALERT_TYPE_OK = 0, + ALERT_TYPE_OK_CANCEL, + ALERT_TYPE_YES_ON, + ALERT_TYPE_YES_NO_CANCEL + }; + + enum AlertResult { + ALERT_RESULT_OK = 1, + ALERT_RESULT_CANCEL, + ALERT_RESULT_NO, + ALERT_RESULT_YES + }; + + PDFiumEngine* engine = static_cast(param); + std::string message_str = + base::UTF16ToUTF8(reinterpret_cast(message)); + if (type == ALERT_TYPE_OK) { + engine->client_->Alert(message_str); + return ALERT_RESULT_OK; + } + + bool rv = engine->client_->Confirm(message_str); + if (type == ALERT_TYPE_OK_CANCEL) + return rv ? ALERT_RESULT_OK : ALERT_RESULT_CANCEL; + return rv ? ALERT_RESULT_YES : ALERT_RESULT_NO; +} + +void PDFiumEngine::Form_Beep(IPDF_JSPLATFORM* param, int type) { + // Beeps are annoying, and not possible using javascript, so ignore for now. +} + +int PDFiumEngine::Form_Response(IPDF_JSPLATFORM* param, + FPDF_WIDESTRING question, + FPDF_WIDESTRING title, + FPDF_WIDESTRING default_response, + FPDF_WIDESTRING label, + FPDF_BOOL password, + void* response, + int length) { + std::string question_str = base::UTF16ToUTF8( + reinterpret_cast(question)); + std::string default_str = base::UTF16ToUTF8( + reinterpret_cast(default_response)); + + PDFiumEngine* engine = static_cast(param); + std::string rv = engine->client_->Prompt(question_str, default_str); + base::string16 rv_16 = base::UTF8ToUTF16(rv); + int rv_bytes = rv_16.size() * sizeof(base::char16); + if (response) { + int bytes_to_copy = rv_bytes < length ? rv_bytes : length; + memcpy(response, rv_16.c_str(), bytes_to_copy); + } + return rv_bytes; +} + +int PDFiumEngine::Form_GetFilePath(IPDF_JSPLATFORM* param, + void* file_path, + int length) { + PDFiumEngine* engine = static_cast(param); + std::string rv = engine->client_->GetURL(); + if (file_path && rv.size() <= static_cast(length)) + memcpy(file_path, rv.c_str(), rv.size()); + return rv.size(); +} + +void PDFiumEngine::Form_Mail(IPDF_JSPLATFORM* param, + void* mail_data, + int length, + FPDF_BOOL ui, + FPDF_WIDESTRING to, + FPDF_WIDESTRING subject, + FPDF_WIDESTRING cc, + FPDF_WIDESTRING bcc, + FPDF_WIDESTRING message) { + DCHECK(length == 0); // Don't handle attachments; no way with mailto. + std::string to_str = + base::UTF16ToUTF8(reinterpret_cast(to)); + std::string cc_str = + base::UTF16ToUTF8(reinterpret_cast(cc)); + std::string bcc_str = + base::UTF16ToUTF8(reinterpret_cast(bcc)); + std::string subject_str = + base::UTF16ToUTF8(reinterpret_cast(subject)); + std::string message_str = + base::UTF16ToUTF8(reinterpret_cast(message)); + + PDFiumEngine* engine = static_cast(param); + engine->client_->Email(to_str, cc_str, bcc_str, subject_str, message_str); +} + +void PDFiumEngine::Form_Print(IPDF_JSPLATFORM* param, + FPDF_BOOL ui, + int start, + int end, + FPDF_BOOL silent, + FPDF_BOOL shrink_to_fit, + FPDF_BOOL print_as_image, + FPDF_BOOL reverse, + FPDF_BOOL annotations) { + // No way to pass the extra information to the print dialog using JavaScript. + // Just opening it is fine for now. + PDFiumEngine* engine = static_cast(param); + engine->client_->Print(); +} + +void PDFiumEngine::Form_SubmitForm(IPDF_JSPLATFORM* param, + void* form_data, + int length, + FPDF_WIDESTRING url) { + std::string url_str = + base::UTF16ToUTF8(reinterpret_cast(url)); + PDFiumEngine* engine = static_cast(param); + engine->client_->SubmitForm(url_str, form_data, length); +} + +void PDFiumEngine::Form_GotoPage(IPDF_JSPLATFORM* param, + int page_number) { + PDFiumEngine* engine = static_cast(param); + engine->client_->ScrollToPage(page_number); +} + +int PDFiumEngine::Form_Browse(IPDF_JSPLATFORM* param, + void* file_path, + int length) { + PDFiumEngine* engine = static_cast(param); + std::string path = engine->client_->ShowFileSelectionDialog(); + if (path.size() + 1 <= static_cast(length)) + memcpy(file_path, &path[0], path.size() + 1); + return path.size() + 1; +} + +FPDF_BOOL PDFiumEngine::Pause_NeedToPauseNow(IFSDK_PAUSE* param) { + PDFiumEngine* engine = static_cast(param); + return (base::Time::Now() - engine->last_progressive_start_time_). + InMilliseconds() > engine->progressive_paint_timeout_; +} + +ScopedUnsupportedFeature::ScopedUnsupportedFeature(PDFiumEngine* engine) + : engine_(engine), old_engine_(g_engine_for_unsupported) { + g_engine_for_unsupported = engine_; +} + +ScopedUnsupportedFeature::~ScopedUnsupportedFeature() { + g_engine_for_unsupported = old_engine_; +} + +PDFEngineExports* PDFEngineExports::Create() { + return new PDFiumEngineExports; +} + +namespace { + +int CalculatePosition(FPDF_PAGE page, + const PDFiumEngineExports::RenderingSettings& settings, + pp::Rect* dest) { + int page_width = static_cast( + FPDF_GetPageWidth(page) * settings.dpi_x / kPointsPerInch); + int page_height = static_cast( + FPDF_GetPageHeight(page) * settings.dpi_y / kPointsPerInch); + + // Start by assuming that we will draw exactly to the bounds rect + // specified. + *dest = settings.bounds; + + int rotate = 0; // normal orientation. + + // Auto-rotate landscape pages to print correctly. + if (settings.autorotate && + (dest->width() > dest->height()) != (page_width > page_height)) { + rotate = 3; // 90 degrees counter-clockwise. + std::swap(page_width, page_height); + } + + // See if we need to scale the output + bool scale_to_bounds = false; + if (settings.fit_to_bounds && + ((page_width > dest->width()) || (page_height > dest->height()))) { + scale_to_bounds = true; + } else if (settings.stretch_to_bounds && + ((page_width < dest->width()) || (page_height < dest->height()))) { + scale_to_bounds = true; + } + + if (scale_to_bounds) { + // If we need to maintain aspect ratio, calculate the actual width and + // height. + if (settings.keep_aspect_ratio) { + double scale_factor_x = page_width; + scale_factor_x /= dest->width(); + double scale_factor_y = page_height; + scale_factor_y /= dest->height(); + if (scale_factor_x > scale_factor_y) { + dest->set_height(page_height / scale_factor_x); + } else { + dest->set_width(page_width / scale_factor_y); + } + } + } else { + // We are not scaling to bounds. Draw in the actual page size. If the + // actual page size is larger than the bounds, the output will be + // clipped. + dest->set_width(page_width); + dest->set_height(page_height); + } + + if (settings.center_in_bounds) { + pp::Point offset((settings.bounds.width() - dest->width()) / 2, + (settings.bounds.height() - dest->height()) / 2); + dest->Offset(offset); + } + return rotate; +} + +} // namespace + +#if defined(OS_WIN) +bool PDFiumEngineExports::RenderPDFPageToDC(const void* pdf_buffer, + int buffer_size, + int page_number, + const RenderingSettings& settings, + HDC dc) { + FPDF_DOCUMENT doc = FPDF_LoadMemDocument(pdf_buffer, buffer_size, NULL); + if (!doc) + return false; + FPDF_PAGE page = FPDF_LoadPage(doc, page_number); + if (!page) { + FPDF_CloseDocument(doc); + return false; + } + RenderingSettings new_settings = settings; + // calculate the page size + if (new_settings.dpi_x == -1) + new_settings.dpi_x = GetDeviceCaps(dc, LOGPIXELSX); + if (new_settings.dpi_y == -1) + new_settings.dpi_y = GetDeviceCaps(dc, LOGPIXELSY); + + pp::Rect dest; + int rotate = CalculatePosition(page, new_settings, &dest); + + int save_state = SaveDC(dc); + // The caller wanted all drawing to happen within the bounds specified. + // Based on scale calculations, our destination rect might be larger + // than the bounds. Set the clip rect to the bounds. + IntersectClipRect(dc, settings.bounds.x(), settings.bounds.y(), + settings.bounds.x() + settings.bounds.width(), + settings.bounds.y() + settings.bounds.height()); + + // A temporary hack. PDFs generated by Cairo (used by Chrome OS to generate + // a PDF output from a webpage) result in very large metafiles and the + // rendering using FPDF_RenderPage is incorrect. In this case, render as a + // bitmap. Note that this code does not kick in for PDFs printed from Chrome + // because in that case we create a temp PDF first before printing and this + // temp PDF does not have a creator string that starts with "cairo". + base::string16 creator; + size_t buffer_bytes = FPDF_GetMetaText(doc, "Creator", NULL, 0); + if (buffer_bytes > 1) { + FPDF_GetMetaText( + doc, "Creator", WriteInto(&creator, buffer_bytes + 1), buffer_bytes); + } + bool use_bitmap = false; + if (StartsWith(creator, L"cairo", false)) + use_bitmap = true; + + // Another temporary hack. Some PDFs seems to render very slowly if + // FPDF_RenderPage is directly used on a printer DC. I suspect it is + // because of the code to talk Postscript directly to the printer if + // the printer supports this. Need to discuss this with PDFium. For now, + // render to a bitmap and then blit the bitmap to the DC if we have been + // supplied a printer DC. + int device_type = GetDeviceCaps(dc, TECHNOLOGY); + if (use_bitmap || + (device_type == DT_RASPRINTER) || (device_type == DT_PLOTTER)) { + FPDF_BITMAP bitmap = FPDFBitmap_Create(dest.width(), dest.height(), + FPDFBitmap_BGRx); + // Clear the bitmap + FPDFBitmap_FillRect(bitmap, 0, 0, dest.width(), dest.height(), 0xFFFFFFFF); + FPDF_RenderPageBitmap( + bitmap, page, 0, 0, dest.width(), dest.height(), rotate, + FPDF_ANNOT | FPDF_PRINTING | FPDF_NO_CATCH); + int stride = FPDFBitmap_GetStride(bitmap); + BITMAPINFO bmi; + memset(&bmi, 0, sizeof(bmi)); + bmi.bmiHeader.biSize = sizeof(BITMAPINFOHEADER); + bmi.bmiHeader.biWidth = dest.width(); + bmi.bmiHeader.biHeight = -dest.height(); // top-down image + bmi.bmiHeader.biPlanes = 1; + bmi.bmiHeader.biBitCount = 32; + bmi.bmiHeader.biCompression = BI_RGB; + bmi.bmiHeader.biSizeImage = stride * dest.height(); + StretchDIBits(dc, dest.x(), dest.y(), dest.width(), dest.height(), + 0, 0, dest.width(), dest.height(), + FPDFBitmap_GetBuffer(bitmap), &bmi, DIB_RGB_COLORS, SRCCOPY); + FPDFBitmap_Destroy(bitmap); + } else { + FPDF_RenderPage(dc, page, dest.x(), dest.y(), dest.width(), dest.height(), + rotate, FPDF_ANNOT | FPDF_PRINTING | FPDF_NO_CATCH); + } + RestoreDC(dc, save_state); + FPDF_ClosePage(page); + FPDF_CloseDocument(doc); + return true; +} +#endif // OS_WIN + +bool PDFiumEngineExports::RenderPDFPageToBitmap( + const void* pdf_buffer, + int pdf_buffer_size, + int page_number, + const RenderingSettings& settings, + void* bitmap_buffer) { + FPDF_DOCUMENT doc = FPDF_LoadMemDocument(pdf_buffer, pdf_buffer_size, NULL); + if (!doc) + return false; + FPDF_PAGE page = FPDF_LoadPage(doc, page_number); + if (!page) { + FPDF_CloseDocument(doc); + return false; + } + + pp::Rect dest; + int rotate = CalculatePosition(page, settings, &dest); + + FPDF_BITMAP bitmap = + FPDFBitmap_CreateEx(settings.bounds.width(), settings.bounds.height(), + FPDFBitmap_BGRA, bitmap_buffer, + settings.bounds.width() * 4); + // Clear the bitmap + FPDFBitmap_FillRect(bitmap, 0, 0, settings.bounds.width(), + settings.bounds.height(), 0xFFFFFFFF); + // Shift top-left corner of bounds to (0, 0) if it's not there. + dest.set_point(dest.point() - settings.bounds.point()); + FPDF_RenderPageBitmap( + bitmap, page, dest.x(), dest.y(), dest.width(), dest.height(), rotate, + FPDF_ANNOT | FPDF_PRINTING | FPDF_NO_CATCH); + FPDFBitmap_Destroy(bitmap); + FPDF_ClosePage(page); + FPDF_CloseDocument(doc); + return true; +} + +bool PDFiumEngineExports::GetPDFDocInfo(const void* pdf_buffer, + int buffer_size, + int* page_count, + double* max_page_width) { + FPDF_DOCUMENT doc = FPDF_LoadMemDocument(pdf_buffer, buffer_size, NULL); + if (!doc) + return false; + int page_count_local = FPDF_GetPageCount(doc); + if (page_count) { + *page_count = page_count_local; + } + if (max_page_width) { + *max_page_width = 0; + for (int page_number = 0; page_number < page_count_local; page_number++) { + double page_width = 0; + double page_height = 0; + FPDF_GetPageSizeByIndex(doc, page_number, &page_width, &page_height); + if (page_width > *max_page_width) { + *max_page_width = page_width; + } + } + } + FPDF_CloseDocument(doc); + return true; +} + +bool PDFiumEngineExports::GetPDFPageSizeByIndex( + const void* pdf_buffer, + int pdf_buffer_size, + int page_number, + double* width, + double* height) { + FPDF_DOCUMENT doc = FPDF_LoadMemDocument(pdf_buffer, pdf_buffer_size, NULL); + if (!doc) + return false; + bool success = FPDF_GetPageSizeByIndex(doc, page_number, width, height) != 0; + FPDF_CloseDocument(doc); + return success; +} + +} // namespace chrome_pdf diff --git a/xfa_test/pdf/pdfium/pdfium_engine.h b/xfa_test/pdf/pdfium/pdfium_engine.h new file mode 100644 index 0000000000..242c33df27 --- /dev/null +++ b/xfa_test/pdf/pdfium/pdfium_engine.h @@ -0,0 +1,687 @@ +// Copyright (c) 2012 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#ifndef PDF_PDFIUM_PDFIUM_ENGINE_H_ +#define PDF_PDFIUM_PDFIUM_ENGINE_H_ + +#include +#include +#include +#include + +#include "base/memory/scoped_ptr.h" +#include "base/time/time.h" +#include "pdf/document_loader.h" +#include "pdf/pdf_engine.h" +#include "pdf/pdfium/pdfium_page.h" +#include "pdf/pdfium/pdfium_range.h" +#include "ppapi/cpp/completion_callback.h" +#include "ppapi/cpp/dev/buffer_dev.h" +#include "ppapi/cpp/image_data.h" +#include "ppapi/cpp/point.h" +#include "third_party/pdfium/fpdfsdk/include/fpdf_dataavail.h" +#include "third_party/pdfium/fpdfsdk/include/fpdf_progressive.h" +#include "third_party/pdfium/fpdfsdk/include/fpdfformfill.h" +#include "third_party/pdfium/fpdfsdk/include/fpdfview.h" + +#define _TEST_XFA +#ifdef _TEST_XFA +# if defined(WIN32) +# define FXQA_TESTFILE(filename) "E:/"#filename +# else +# define FXQA_TESTFILE(filename) "/home/"#filename +# endif +#endif + +namespace pp { +class KeyboardInputEvent; +class MouseInputEvent; +} + +namespace chrome_pdf { + +class ShadowMatrix; + +class PDFiumEngine : public PDFEngine, + public DocumentLoader::Client, + public FPDF_FORMFILLINFO, + public IPDF_JSPLATFORM, + public IFSDK_PAUSE { + public: + explicit PDFiumEngine(PDFEngine::Client* client); + virtual ~PDFiumEngine(); + + // PDFEngine implementation. + virtual bool New(const char* url); + virtual bool New(const char* url, + const char* headers); + virtual void PageOffsetUpdated(const pp::Point& page_offset); + virtual void PluginSizeUpdated(const pp::Size& size); + virtual void ScrolledToXPosition(int position); + virtual void ScrolledToYPosition(int position); + virtual void PrePaint(); + virtual void Paint(const pp::Rect& rect, + pp::ImageData* image_data, + std::vector* ready, + std::vector* pending); + virtual void PostPaint(); + virtual bool HandleDocumentLoad(const pp::URLLoader& loader); + virtual bool HandleEvent(const pp::InputEvent& event); + virtual uint32_t QuerySupportedPrintOutputFormats(); + virtual void PrintBegin(); + virtual pp::Resource PrintPages( + const PP_PrintPageNumberRange_Dev* page_ranges, + uint32_t page_range_count, + const PP_PrintSettings_Dev& print_settings); + virtual void PrintEnd(); + virtual void StartFind(const char* text, bool case_sensitive); + virtual bool SelectFindResult(bool forward); + virtual void StopFind(); + virtual void ZoomUpdated(double new_zoom_level); + virtual void RotateClockwise(); + virtual void RotateCounterclockwise(); + virtual std::string GetSelectedText(); + virtual std::string GetLinkAtPosition(const pp::Point& point); + virtual bool IsSelecting(); + virtual bool HasPermission(DocumentPermission permission) const; + virtual void SelectAll(); + virtual int GetNumberOfPages(); + virtual int GetNamedDestinationPage(const std::string& destination); + virtual int GetFirstVisiblePage(); + virtual int GetMostVisiblePage(); + virtual pp::Rect GetPageRect(int index); + virtual pp::Rect GetPageContentsRect(int index); + virtual int GetVerticalScrollbarYPosition() { return position_.y(); } + virtual void PaintThumbnail(pp::ImageData* image_data, int index); + virtual void SetGrayscale(bool grayscale); + virtual void OnCallback(int id); + virtual std::string GetPageAsJSON(int index); + virtual bool GetPrintScaling(); + virtual void AppendBlankPages(int num_pages); + virtual void AppendPage(PDFEngine* engine, int index); + virtual pp::Point GetScrollPosition(); + virtual void SetScrollPosition(const pp::Point& position); + virtual bool IsProgressiveLoad(); + + // DocumentLoader::Client implementation. + virtual pp::Instance* GetPluginInstance(); + virtual pp::URLLoader CreateURLLoader(); + virtual void OnPartialDocumentLoaded(); + virtual void OnPendingRequestComplete(); + virtual void OnNewDataAvailable(); + virtual void OnDocumentComplete(); + + void UnsupportedFeature(int type); + + std::string current_find_text() const { return current_find_text_; } + + FPDF_DOCUMENT doc() { return doc_; } + FPDF_FORMHANDLE form() { return form_; } + + private: + // This helper class is used to detect the difference in selection between + // construction and destruction. At destruction, it invalidates all the + // parts that are newly selected, along with all the parts that used to be + // selected but are not anymore. + class SelectionChangeInvalidator { + public: + explicit SelectionChangeInvalidator(PDFiumEngine* engine); + ~SelectionChangeInvalidator(); + private: + // Sets the given container to the all the currently visible selection + // rectangles, in screen coordinates. + void GetVisibleSelectionsScreenRects(std::vector* rects); + + PDFiumEngine* engine_; + // Screen rectangles that were selected on construction. + std::vector old_selections_; + // The origin at the time this object was constructed. + pp::Point previous_origin_; + }; + + // Used to store mouse down state to handle it in other mouse event handlers. + class MouseDownState { + public: + MouseDownState(const PDFiumPage::Area& area, + const PDFiumPage::LinkTarget& target); + ~MouseDownState(); + + void Set(const PDFiumPage::Area& area, + const PDFiumPage::LinkTarget& target); + void Reset(); + bool Matches(const PDFiumPage::Area& area, + const PDFiumPage::LinkTarget& target) const; + + private: + PDFiumPage::Area area_; + PDFiumPage::LinkTarget target_; + + DISALLOW_COPY_AND_ASSIGN(MouseDownState); + }; + + friend class SelectionChangeInvalidator; + + struct FileAvail : public FX_FILEAVAIL { + DocumentLoader* loader; + }; + + struct DownloadHints : public FX_DOWNLOADHINTS { + DocumentLoader* loader; + }; + + // PDFium interface to get block of data. + static int GetBlock(void* param, unsigned long position, + unsigned char* buffer, unsigned long size); + + // PDFium interface to check is block of data is available. + static bool IsDataAvail(FX_FILEAVAIL* param, + size_t offset, size_t size); + + // PDFium interface to request download of the block of data. + static void AddSegment(FX_DOWNLOADHINTS* param, + size_t offset, size_t size); + + // We finished getting the pdf file, so load it. This will complete + // asynchronously (due to password fetching) and may be run multiple times. + void LoadDocument(); + + // Try loading the document. Returns true if the document is successfully + // loaded or is already loaded otherwise it will return false. If + // |with_password| is set to true, the document will be loaded with + // |password|. If the document could not be loaded and needs a password, + // |needs_password| will be set to true. + bool TryLoadingDoc(bool with_password, + const std::string& password, + bool* needs_password); + + // Ask the user for the document password and then continue loading the + // document. + void GetPasswordAndLoad(); + + // Called when the password has been retrieved. + void OnGetPasswordComplete(int32_t result, + const pp::Var& password); + + // Continues loading the document when the password has been retrieved, or if + // there is no password. + void ContinueLoadingDocument(bool has_password, + const std::string& password); + + // Finish loading the document and notify the client that the document has + // been loaded. This should only be run after |doc_| has been loaded and the + // document is fully downloaded. If this has been run once, it will result in + // a no-op. + void FinishLoadingDocument(); + + // Loads information about the pages in the document and calculate the + // document size. + void LoadPageInfo(bool reload); + + // Calculate which pages should be displayed right now. + void CalculateVisiblePages(); + + // Returns true iff the given page index is visible. CalculateVisiblePages + // must have been called first. + bool IsPageVisible(int index) const; + + // Checks if a page is now available, and if so marks it as such and returns + // true. Otherwise, it will return false and will add the index to the given + // array if it's not already there. + bool CheckPageAvailable(int index, std::vector* pending); + + // Helper function to get a given page's size in pixels. This is not part of + // PDFiumPage because we might not have that structure when we need this. + pp::Size GetPageSize(int index); + + void UpdateTickMarks(); + + // Called to continue searching so we don't block the main thread. + void ContinueFind(int32_t result); + + // Inserts a find result into find_results_, which is sorted. + void AddFindResult(const PDFiumRange& result); + + // Search a page using PDFium's methods. Doesn't work with unicode. This + // function is just kept arount in case PDFium code is fixed. + void SearchUsingPDFium(const base::string16& term, + bool case_sensitive, + bool first_search, + int character_to_start_searching_from, + int current_page); + + // Search a page ourself using ICU. + void SearchUsingICU(const base::string16& term, + bool case_sensitive, + bool first_search, + int character_to_start_searching_from, + int current_page); + + // Input event handlers. + bool OnMouseDown(const pp::MouseInputEvent& event); + bool OnMouseUp(const pp::MouseInputEvent& event); + bool OnMouseMove(const pp::MouseInputEvent& event); + bool OnKeyDown(const pp::KeyboardInputEvent& event); + bool OnKeyUp(const pp::KeyboardInputEvent& event); + bool OnChar(const pp::KeyboardInputEvent& event); + + FPDF_DOCUMENT CreateSinglePageRasterPdf( + double source_page_width, + double source_page_height, + const PP_PrintSettings_Dev& print_settings, + PDFiumPage* page_to_print); + + pp::Buffer_Dev PrintPagesAsRasterPDF( + const PP_PrintPageNumberRange_Dev* page_ranges, + uint32_t page_range_count, + const PP_PrintSettings_Dev& print_settings); + + pp::Buffer_Dev PrintPagesAsPDF(const PP_PrintPageNumberRange_Dev* page_ranges, + uint32_t page_range_count, + const PP_PrintSettings_Dev& print_settings); + + pp::Buffer_Dev GetFlattenedPrintData(const FPDF_DOCUMENT& doc); + void FitContentsToPrintableAreaIfRequired( + const FPDF_DOCUMENT& doc, + const PP_PrintSettings_Dev& print_settings); + void SaveSelectedFormForPrint(); + + // Given a mouse event, returns which page and character location it's closest + // to. + PDFiumPage::Area GetCharIndex(const pp::MouseInputEvent& event, + int* page_index, + int* char_index, + PDFiumPage::LinkTarget* target); + PDFiumPage::Area GetCharIndex(const pp::Point& point, + int* page_index, + int* char_index, + PDFiumPage::LinkTarget* target); + + void OnSingleClick(int page_index, int char_index); + void OnMultipleClick(int click_count, int page_index, int char_index); + + // Starts a progressive paint operation given a rectangle in screen + // coordinates. Returns the index in progressive_rects_. + int StartPaint(int page_index, const pp::Rect& dirty); + + // Continues a paint operation that was started earlier. Returns true if the + // paint is done, or false if it needs to be continued. + bool ContinuePaint(int progressive_index, pp::ImageData* image_data); + + // Called once PDFium is finished rendering a page so that we draw our + // borders, highlighting etc. + void FinishPaint(int progressive_index, pp::ImageData* image_data); + + // Stops any paints that are in progress. + void CancelPaints(); + + // Invalidates all pages. Use this when some global parameter, such as page + // orientation, has changed. + void InvalidateAllPages(); + + // If the page is narrower than the document size, paint the extra space + // with the page background. + void FillPageSides(int progressive_index); + + void PaintPageShadow(int progressive_index, pp::ImageData* image_data); + + // Highlight visible find results and selections. + void DrawSelections(int progressive_index, pp::ImageData* image_data); + + // Paints an page that hasn't finished downloading. + void PaintUnavailablePage(int page_index, + const pp::Rect& dirty, + pp::ImageData* image_data); + + // Given a page index, returns the corresponding index in progressive_rects_, + // or -1 if it doesn't exist. + int GetProgressiveIndex(int page_index) const; + + // Creates a FPDF_BITMAP from a rectangle in screen coordinates. + FPDF_BITMAP CreateBitmap(const pp::Rect& rect, + pp::ImageData* image_data) const; + + // Given a rectangle in screen coordinates, returns the coordinates in the + // units that PDFium rendering functions expect. + void GetPDFiumRect(int page_index, const pp::Rect& rect, int* start_x, + int* start_y, int* size_x, int* size_y) const; + + // Returns the rendering flags to pass to PDFium. + int GetRenderingFlags() const; + + // Returns the currently visible rectangle in document coordinates. + pp::Rect GetVisibleRect() const; + + // Returns a page's rect in screen coordinates, as well as its surrounding + // border areas and bottom separator. + pp::Rect GetPageScreenRect(int page_index) const; + + // Given a rectangle in document coordinates, returns the rectange into screen + // coordinates (i.e. 0,0 is top left corner of plugin area). If it's not + // visible, an empty rectangle is returned. + pp::Rect GetScreenRect(const pp::Rect& rect) const; + + // Highlights the given rectangle. + void Highlight(void* buffer, + int stride, + const pp::Rect& rect, + std::vector* highlighted_rects); + + // Helper function to convert a device to page coordinates. If the page is + // not yet loaded, page_x and page_y will be set to 0. + void DeviceToPage(int page_index, + float device_x, + float device_y, + double* page_x, + double* page_y); + + // Helper function to get the index of a given FPDF_PAGE. Returns -1 if not + // found. + int GetVisiblePageIndex(FPDF_PAGE page); + + // Helper function to change the current page, running page open/close + // triggers as necessary. + void SetCurrentPage(int index); + + // Transform |page| contents to fit in the selected printer paper size. + void TransformPDFPageForPrinting(FPDF_PAGE page, + const PP_PrintSettings_Dev& print_settings); + + void DrawPageShadow(const pp::Rect& page_rect, + const pp::Rect& shadow_rect, + const pp::Rect& clip_rect, + pp::ImageData* image_data); + + void GetRegion(const pp::Point& location, + pp::ImageData* image_data, + void** region, + int* stride) const; + + // Called when the selection changes. + void OnSelectionChanged(); + + // FPDF_FORMFILLINFO callbacks. + static void Form_Invalidate(FPDF_FORMFILLINFO* param, + FPDF_PAGE page, + double left, + double top, + double right, + double bottom); + static void Form_OutputSelectedRect(FPDF_FORMFILLINFO* param, + FPDF_PAGE page, + double left, + double top, + double right, + double bottom); + static void Form_SetCursor(FPDF_FORMFILLINFO* param, int cursor_type); + static int Form_SetTimer(FPDF_FORMFILLINFO* param, + int elapse, + TimerCallback timer_func); + static void Form_KillTimer(FPDF_FORMFILLINFO* param, int timer_id); + static FPDF_SYSTEMTIME Form_GetLocalTime(FPDF_FORMFILLINFO* param); + static void Form_OnChange(FPDF_FORMFILLINFO* param); + static FPDF_PAGE Form_GetPage(FPDF_FORMFILLINFO* param, + FPDF_DOCUMENT document, + int page_index); + static FPDF_PAGE Form_GetCurrentPage(FPDF_FORMFILLINFO* param, + FPDF_DOCUMENT document); + static int Form_GetRotation(FPDF_FORMFILLINFO* param, FPDF_PAGE page); + static void Form_ExecuteNamedAction(FPDF_FORMFILLINFO* param, + FPDF_BYTESTRING named_action); + static void Form_SetTextFieldFocus(FPDF_FORMFILLINFO* param, + FPDF_WIDESTRING value, + FPDF_DWORD valueLen, + FPDF_BOOL is_focus); + static void Form_DoURIAction(FPDF_FORMFILLINFO* param, FPDF_BYTESTRING uri); + static void Form_DoGoToAction(FPDF_FORMFILLINFO* param, + int page_index, + int zoom_mode, + float* position_array, + int size_of_array); + + // IPDF_JSPLATFORM callbacks. + static int Form_Alert(IPDF_JSPLATFORM* param, + FPDF_WIDESTRING message, + FPDF_WIDESTRING title, + int type, + int icon); + static void Form_Beep(IPDF_JSPLATFORM* param, int type); + static int Form_Response(IPDF_JSPLATFORM* param, + FPDF_WIDESTRING question, + FPDF_WIDESTRING title, + FPDF_WIDESTRING default_response, + FPDF_WIDESTRING label, + FPDF_BOOL password, + void* response, + int length); + static int Form_GetFilePath(IPDF_JSPLATFORM* param, + void* file_path, + int length); + static void Form_Mail(IPDF_JSPLATFORM* param, + void* mail_data, + int length, + FPDF_BOOL ui, + FPDF_WIDESTRING to, + FPDF_WIDESTRING subject, + FPDF_WIDESTRING cc, + FPDF_WIDESTRING bcc, + FPDF_WIDESTRING message); + static void Form_Print(IPDF_JSPLATFORM* param, + FPDF_BOOL ui, + int start, + int end, + FPDF_BOOL silent, + FPDF_BOOL shrink_to_fit, + FPDF_BOOL print_as_image, + FPDF_BOOL reverse, + FPDF_BOOL annotations); + static void Form_SubmitForm(IPDF_JSPLATFORM* param, + void* form_data, + int length, + FPDF_WIDESTRING url); + static void Form_GotoPage(IPDF_JSPLATFORM* param, int page_number); + static int Form_Browse(IPDF_JSPLATFORM* param, void* file_path, int length); +#ifdef _TEST_XFA + static void Form_EmailTo(FPDF_FORMFILLINFO* pThis, FPDF_FILEHANDLER* fileHandler, + FPDF_WIDESTRING to, FPDF_WIDESTRING subject, FPDF_WIDESTRING cc, FPDF_WIDESTRING bcc, FPDF_WIDESTRING message); + static void Form_DisplayCaret(FPDF_FORMFILLINFO* pThis, FPDF_PAGE page, FPDF_BOOL bVisible, double left, double top, double right, double bottom); + //static int Form_GetCurDocumentIndex(FPDF_FORMFILLINFO* pThis); + //static int Form_GetDocumentCount(FPDF_FORMFILLINFO* pThis); + static void Form_SetCurrentPage(FPDF_FORMFILLINFO* pThis, FPDF_DOCUMENT document, int iCurPage); + static int Form_GetCurrentPageIndex(FPDF_FORMFILLINFO* pThis, FPDF_DOCUMENT document); + static void Form_GetPageViewRect(FPDF_FORMFILLINFO* pThis, FPDF_PAGE page, double* left, double* top, double* right, double* bottom); + static int Form_GetPlatform(FPDF_FORMFILLINFO* pThis, void* platform, int length); + static FPDF_BOOL Form_PopupMenu(FPDF_FORMFILLINFO* pThis, FPDF_PAGE page, FPDF_WIDGET hWidget, int menuFlag, float x, float y); + static FPDF_BOOL Form_PostRequestURL(FPDF_FORMFILLINFO* pThis, FPDF_WIDESTRING wsURL, FPDF_WIDESTRING wsData, FPDF_WIDESTRING wsContentType, FPDF_WIDESTRING wsEncode, FPDF_WIDESTRING wsHeader, FPDF_BSTR* respone); + static FPDF_BOOL Form_PutRequestURL(FPDF_FORMFILLINFO* pThis, FPDF_WIDESTRING wsURL, FPDF_WIDESTRING wsData, FPDF_WIDESTRING wsEncode); + //static FPDF_BOOL Form_ShowFileDialog(FPDF_FORMFILLINFO* pThis, FPDF_WIDESTRING wsTitle, FPDF_WIDESTRING wsFilter, FPDF_BOOL isOpen, FPDF_STRINGHANDLE pathArr); + static void Form_UploadTo(FPDF_FORMFILLINFO* pThis, FPDF_FILEHANDLER* fileHandler, int fileFlag, FPDF_WIDESTRING uploadTo); + static FPDF_LPFILEHANDLER Form_DownloadFromURL(FPDF_FORMFILLINFO* pThis, FPDF_WIDESTRING URL); + //static FPDF_BOOL MyForm_GetFilePath(FPDF_FORMFILLINFO* pThis, FPDF_FILEHANDLER* pFileHandler, void* filePath, int length); + static FPDF_FILEHANDLER* Form_OpenFile(FPDF_FORMFILLINFO* pThis, int fileFlag, FPDF_WIDESTRING wsURL, const char* mode); + static void Form_GotoURL(FPDF_FORMFILLINFO* pThis, FPDF_DOCUMENT document, FPDF_WIDESTRING wsURL); + static int Form_GetLanguage(FPDF_FORMFILLINFO* pThis, void* language, int length); +#endif // _TEST_XFA + // IFSDK_PAUSE callbacks + static FPDF_BOOL Pause_NeedToPauseNow(IFSDK_PAUSE* param); + + PDFEngine::Client* client_; + pp::Size document_size_; // Size of document in pixels. + + // The scroll position in screen coordinates. + pp::Point position_; + // The offset of the page into the viewport. + pp::Point page_offset_; + // The plugin size in screen coordinates. + pp::Size plugin_size_; + double current_zoom_; + unsigned int current_rotation_; + + DocumentLoader doc_loader_; // Main document's loader. + std::string url_; + std::string headers_; + pp::CompletionCallbackFactory find_factory_; + + pp::CompletionCallbackFactory password_factory_; + int32_t password_tries_remaining_; + + // The current text used for searching. + std::string current_find_text_; + + // The PDFium wrapper object for the document. + FPDF_DOCUMENT doc_; + + // The PDFium wrapper for form data. Used even if there are no form controls + // on the page. + FPDF_FORMHANDLE form_; + + // The page(s) of the document. Store a vector of pointers so that when the + // vector is resized we don't close the pages that are used in pending + // paints. + std::vector pages_; + + // The indexes of the pages currently visible. + std::vector visible_pages_; + + // The indexes of the pages pending download. + std::vector pending_pages_; + + // During handling of input events we don't want to unload any pages in + // callbacks to us from PDFium, since the current page can change while PDFium + // code still has a pointer to it. + bool defer_page_unload_; + std::vector deferred_page_unloads_; + + // Used for selection. There could be more than one range if selection spans + // more than one page. + std::vector selection_; + // True if we're in the middle of selection. + bool selecting_; + + MouseDownState mouse_down_state_; + + // Used for searching. + typedef std::vector FindResults; + FindResults find_results_; + // Which page to search next. + int next_page_to_search_; + // Where to stop searching. + int last_page_to_search_; + int last_character_index_to_search_; // -1 if search until end of page. + // Which result the user has currently selected. + int current_find_index_; + // Where to resume searching. + int resume_find_index_; + + // Permissions bitfield. + unsigned long permissions_; + + // Interface structure to provide access to document stream. + FPDF_FILEACCESS file_access_; + // Interface structure to check data availability in the document stream. + FileAvail file_availability_; + // Interface structure to request data chunks from the document stream. + DownloadHints download_hints_; + // Pointer to the document availability interface. + FPDF_AVAIL fpdf_availability_; + + pp::Size default_page_size_; + + // Used to manage timers that form fill API needs. The pair holds the timer + // period, in ms, and the callback function. + std::map > timers_; + int next_timer_id_; + + // Holds the page index of the last page that the mouse clicked on. + int last_page_mouse_down_; + + // Holds the page index of the first visible page; refreshed by calling + // CalculateVisiblePages() + int first_visible_page_; + + // Holds the page index of the most visible page; refreshed by calling + // CalculateVisiblePages() + int most_visible_page_; + + // Set to true after FORM_DoDocumentJSAction/FORM_DoDocumentOpenAction have + // been called. Only after that can we call FORM_DoPageAAction. + bool called_do_document_action_; + + // Records parts of form fields that need to be highlighted at next paint, in + // screen coordinates. + std::vector form_highlights_; + + // Whether to render in grayscale or in color. + bool render_grayscale_; + + // The link currently under the cursor. + std::string link_under_cursor_; + + // Pending progressive paints. + struct ProgressivePaint { + pp::Rect rect; // In screen coordinates. + FPDF_BITMAP bitmap; + int page_index; + // Temporary used to figure out if in a series of Paint() calls whether this + // pending paint was updated or not. + int painted_; + }; + std::vector progressive_paints_; + + // Keeps track of when we started the last progressive paint, so that in our + // callback we can determine if we need to pause. + base::Time last_progressive_start_time_; + + // The timeout to use for the current progressive paint. + int progressive_paint_timeout_; + + // Shadow matrix for generating the page shadow bitmap. + scoped_ptr page_shadow_; + + // Set to true if the user is being prompted for their password. Will be set + // to false after the user finishes getting their password. + bool getting_password_; +}; + +// Create a local variable of this when calling PDFium functions which can call +// our global callback when an unsupported feature is reached. +class ScopedUnsupportedFeature { + public: + explicit ScopedUnsupportedFeature(PDFiumEngine* engine); + ~ScopedUnsupportedFeature(); + private: + PDFiumEngine* engine_; + PDFiumEngine* old_engine_; +}; + +class PDFiumEngineExports : public PDFEngineExports { + public: + PDFiumEngineExports() {} +#if defined(OS_WIN) + // See the definition of RenderPDFPageToDC in pdf.cc for details. + virtual bool RenderPDFPageToDC(const void* pdf_buffer, + int buffer_size, + int page_number, + const RenderingSettings& settings, + HDC dc); +#endif // OS_WIN + virtual bool RenderPDFPageToBitmap(const void* pdf_buffer, + int pdf_buffer_size, + int page_number, + const RenderingSettings& settings, + void* bitmap_buffer); + + virtual bool GetPDFDocInfo(const void* pdf_buffer, + int buffer_size, + int* page_count, + double* max_page_width); + + // See the definition of GetPDFPageSizeByIndex in pdf.cc for details. + virtual bool GetPDFPageSizeByIndex(const void* pdf_buffer, + int pdf_buffer_size, int page_number, + double* width, double* height); +}; + +} // namespace chrome_pdf + +#endif // PDF_PDFIUM_PDFIUM_ENGINE_H_ diff --git a/xfa_test/pdf/pdfium/pdfium_mem_buffer_file_read.cc b/xfa_test/pdf/pdfium/pdfium_mem_buffer_file_read.cc new file mode 100644 index 0000000000..b2371b42ff --- /dev/null +++ b/xfa_test/pdf/pdfium/pdfium_mem_buffer_file_read.cc @@ -0,0 +1,34 @@ +// Copyright (c) 2012 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#include "pdf/pdfium/pdfium_mem_buffer_file_read.h" + +#include + +namespace chrome_pdf { + +PDFiumMemBufferFileRead::PDFiumMemBufferFileRead(const void* data, + size_t size) { + m_FileLen = size; + m_Param = this; + m_GetBlock = &GetBlock; + data_ = reinterpret_cast(data); +} + +PDFiumMemBufferFileRead::~PDFiumMemBufferFileRead() { +} + +int PDFiumMemBufferFileRead::GetBlock(void* param, + unsigned long position, + unsigned char* buf, + unsigned long size) { + const PDFiumMemBufferFileRead* data = + reinterpret_cast(param); + if (!data || position + size > data->m_FileLen) + return 0; + memcpy(buf, data->data_ + position, size); + return 1; +} + +} // namespace chrome_pdf diff --git a/xfa_test/pdf/pdfium/pdfium_mem_buffer_file_read.h b/xfa_test/pdf/pdfium/pdfium_mem_buffer_file_read.h new file mode 100644 index 0000000000..88ec410a6c --- /dev/null +++ b/xfa_test/pdf/pdfium/pdfium_mem_buffer_file_read.h @@ -0,0 +1,30 @@ +// Copyright (c) 2012 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#ifndef PDF_PDFIUM_PDFIUM_MEM_BUFFER_FILE_READ_H_ +#define PDF_PDFIUM_PDFIUM_MEM_BUFFER_FILE_READ_H_ + +#include + +#include "third_party/pdfium/fpdfsdk/include/fpdfview.h" + +namespace chrome_pdf { + +// Implementation of FPDF_FILEACCESS from a memory buffer. +class PDFiumMemBufferFileRead : public FPDF_FILEACCESS { + public: + PDFiumMemBufferFileRead(const void* data, size_t size); + ~PDFiumMemBufferFileRead(); + + private: + static int GetBlock(void* param, + unsigned long position, + unsigned char* buf, + unsigned long size); + const unsigned char* data_; +}; + +} // namespace chrome_pdf + +#endif // PDF_PDFIUM_PDFIUM_MEM_BUFFER_FILE_READ_H_ diff --git a/xfa_test/pdf/pdfium/pdfium_mem_buffer_file_write.cc b/xfa_test/pdf/pdfium/pdfium_mem_buffer_file_write.cc new file mode 100644 index 0000000000..45e564b67b --- /dev/null +++ b/xfa_test/pdf/pdfium/pdfium_mem_buffer_file_write.cc @@ -0,0 +1,33 @@ +// Copyright (c) 2010 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#include "pdf/pdfium/pdfium_mem_buffer_file_write.h" + +namespace chrome_pdf { + +PDFiumMemBufferFileWrite::PDFiumMemBufferFileWrite() { + version = 1; + WriteBlock = &WriteBlockImpl; +} + +PDFiumMemBufferFileWrite::~PDFiumMemBufferFileWrite() { +} + +int PDFiumMemBufferFileWrite::WriteBlockImpl(FPDF_FILEWRITE* this_file_write, + const void* data, + unsigned long size) { + PDFiumMemBufferFileWrite* mem_buffer_file_write = + static_cast(this_file_write); + return mem_buffer_file_write->DoWriteBlock(data, size); +} + +int PDFiumMemBufferFileWrite::DoWriteBlock(const void* data, + unsigned long size) { + buffer_.append(static_cast(data), size); + return 1; +} + + +} // namespace chrome_pdf + diff --git a/xfa_test/pdf/pdfium/pdfium_mem_buffer_file_write.h b/xfa_test/pdf/pdfium/pdfium_mem_buffer_file_write.h new file mode 100644 index 0000000000..1795c83e89 --- /dev/null +++ b/xfa_test/pdf/pdfium/pdfium_mem_buffer_file_write.h @@ -0,0 +1,34 @@ +// Copyright (c) 2010 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#ifndef PDF_PDFIUM_PDFIUM_MEM_BUFFER_FILE_WRITE_ +#define PDF_PDFIUM_PDFIUM_MEM_BUFFER_FILE_WRITE_ + +#include + +#include "third_party/pdfium/fpdfsdk/include/fpdfsave.h" + +namespace chrome_pdf { + +// Implementation of FPDF_FILEWRITE into a memory buffer. +class PDFiumMemBufferFileWrite : public FPDF_FILEWRITE { + public: + PDFiumMemBufferFileWrite(); + ~PDFiumMemBufferFileWrite(); + + const std::basic_string& buffer() { return buffer_; } + size_t size() { return buffer_.size(); } + + private: + int DoWriteBlock(const void* data, unsigned long size); + static int WriteBlockImpl(FPDF_FILEWRITE* this_file_write, const void* data, + unsigned long size); + + std::basic_string buffer_; +}; + +} // namespace chrome_pdf + +#endif // PDF_PDFIUM_PDFIUM_MEM_BUFFER_FILE_WRITE_ + diff --git a/xfa_test/pdf/pdfium/pdfium_page.cc b/xfa_test/pdf/pdfium/pdfium_page.cc new file mode 100644 index 0000000000..d8a5dce5a2 --- /dev/null +++ b/xfa_test/pdf/pdfium/pdfium_page.cc @@ -0,0 +1,477 @@ +// Copyright (c) 2010 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#include "pdf/pdfium/pdfium_page.h" + +#include + +#include "base/logging.h" +#include "base/strings/string_number_conversions.h" +#include "base/strings/string_util.h" +#include "base/strings/utf_string_conversions.h" +#include "base/values.h" +#include "pdf/pdfium/pdfium_engine.h" + +// Used when doing hit detection. +#define kTolerance 20.0 + +// Dictionary Value key names for returning the accessible page content as JSON. +const char kPageWidth[] = "width"; +const char kPageHeight[] = "height"; +const char kPageTextBox[] = "textBox"; +const char kTextBoxLeft[] = "left"; +const char kTextBoxTop[] = "top"; +const char kTextBoxWidth[] = "width"; +const char kTextBoxHeight[] = "height"; +const char kTextBoxFontSize[] = "fontSize"; +const char kTextBoxNodes[] = "textNodes"; +const char kTextNodeType[] = "type"; +const char kTextNodeText[] = "text"; +const char kTextNodeURL[] = "url"; +const char kTextNodeTypeText[] = "text"; +const char kTextNodeTypeURL[] = "url"; +const char kDocLinkURLPrefix[] = "#page"; + +namespace chrome_pdf { + +PDFiumPage::PDFiumPage(PDFiumEngine* engine, + int i, + const pp::Rect& r, + bool available) + : engine_(engine), + page_(NULL), + text_page_(NULL), + index_(i), + rect_(r), + calculated_links_(false), + available_(available) { +} + +PDFiumPage::~PDFiumPage() { +} + +void PDFiumPage::Unload() { + if (text_page_) { + FPDFText_ClosePage(text_page_); + text_page_ = NULL; + } + + if (page_) { + if (engine_->form()) { + FORM_OnBeforeClosePage(page_, engine_->form()); + } + FPDF_ClosePage(page_); + page_ = NULL; + } +} + +FPDF_PAGE PDFiumPage::GetPage() { + ScopedUnsupportedFeature scoped_unsupported_feature(engine_); + if (!available_) + return NULL; + if (!page_) { + page_ = FPDF_LoadPage(engine_->doc(), index_); + if (page_ && engine_->form()) { + FORM_OnAfterLoadPage(page_, engine_->form()); + } + } + return page_; +} + +FPDF_PAGE PDFiumPage::GetPrintPage() { + ScopedUnsupportedFeature scoped_unsupported_feature(engine_); + if (!available_) + return NULL; + if (!page_) + page_ = FPDF_LoadPage(engine_->doc(), index_); + return page_; +} + +void PDFiumPage::ClosePrintPage() { + if (page_) { + FPDF_ClosePage(page_); + page_ = NULL; + } +} + +FPDF_TEXTPAGE PDFiumPage::GetTextPage() { + if (!available_) + return NULL; + if (!text_page_) + text_page_ = FPDFText_LoadPage(GetPage()); + return text_page_; +} + +base::Value* PDFiumPage::GetAccessibleContentAsValue(int rotation) { + base::DictionaryValue* node = new base::DictionaryValue(); + + if (!available_) + return node; + + double width = FPDF_GetPageWidth(GetPage()); + double height = FPDF_GetPageHeight(GetPage()); + + base::ListValue* text = new base::ListValue(); + int box_count = FPDFText_CountRects(GetTextPage(), 0, GetCharCount()); + for (int i = 0; i < box_count; i++) { + double left, top, right, bottom; + FPDFText_GetRect(GetTextPage(), i, &left, &top, &right, &bottom); + text->Append( + GetTextBoxAsValue(height, left, top, right, bottom, rotation)); + } + + node->SetDouble(kPageWidth, width); + node->SetDouble(kPageHeight, height); + node->Set(kPageTextBox, text); // Takes ownership of |text| + + return node; +} + +base::Value* PDFiumPage::GetTextBoxAsValue(double page_height, + double left, double top, + double right, double bottom, + int rotation) { + base::string16 text_utf16; + int char_count = + FPDFText_GetBoundedText(GetTextPage(), left, top, right, bottom, NULL, 0); + if (char_count > 0) { + unsigned short* data = reinterpret_cast( + WriteInto(&text_utf16, char_count + 1)); + FPDFText_GetBoundedText(GetTextPage(), + left, top, right, bottom, + data, char_count); + } + std::string text_utf8 = base::UTF16ToUTF8(text_utf16); + + FPDF_LINK link = FPDFLink_GetLinkAtPoint(GetPage(), left, top); + Area area; + std::vector targets; + if (link) { + targets.push_back(LinkTarget()); + area = GetLinkTarget(link, &targets[0]); + } else { + pp::Rect rect( + PageToScreen(pp::Point(), 1.0, left, top, right, bottom, rotation)); + GetLinks(rect, &targets); + area = targets.size() == 0 ? TEXT_AREA : WEBLINK_AREA; + } + + int char_index = FPDFText_GetCharIndexAtPos(GetTextPage(), left, top, + kTolerance, kTolerance); + double font_size = FPDFText_GetFontSize(GetTextPage(), char_index); + + base::DictionaryValue* node = new base::DictionaryValue(); + node->SetDouble(kTextBoxLeft, left); + node->SetDouble(kTextBoxTop, page_height - top); + node->SetDouble(kTextBoxWidth, right - left); + node->SetDouble(kTextBoxHeight, top - bottom); + node->SetDouble(kTextBoxFontSize, font_size); + + base::ListValue* text_nodes = new base::ListValue(); + + if (area == DOCLINK_AREA) { + std::string url = kDocLinkURLPrefix + base::IntToString(targets[0].page); + text_nodes->Append(CreateURLNode(text_utf8, url)); + } else if (area == WEBLINK_AREA && link) { + text_nodes->Append(CreateURLNode(text_utf8, targets[0].url)); + } else if (area == WEBLINK_AREA && !link) { + size_t start = 0; + for (size_t i = 0; i < targets.size(); ++i) { + // Remove the extra NULL character at end. + // Otherwise, find() will not return any matches. + if (targets[i].url.size() > 0 && + targets[i].url[targets[i].url.size() - 1] == '\0') { + targets[i].url.resize(targets[i].url.size() - 1); + } + // There should only ever be one NULL character + DCHECK(targets[i].url[targets[i].url.size() - 1] != '\0'); + + // PDFium may change the case of generated links. + std::string lowerCaseURL = base::StringToLowerASCII(targets[i].url); + std::string lowerCaseText = base::StringToLowerASCII(text_utf8); + size_t pos = lowerCaseText.find(lowerCaseURL, start); + size_t length = targets[i].url.size(); + if (pos == std::string::npos) { + // Check if the link is a "mailto:" URL + if (lowerCaseURL.compare(0, 7, "mailto:") == 0) { + pos = lowerCaseText.find(lowerCaseURL.substr(7), start); + length -= 7; + } + + if (pos == std::string::npos) { + // No match has been found. This should never happen. + continue; + } + } + + std::string before_text = text_utf8.substr(start, pos - start); + if (before_text.size() > 0) + text_nodes->Append(CreateTextNode(before_text)); + std::string link_text = text_utf8.substr(pos, length); + text_nodes->Append(CreateURLNode(link_text, targets[i].url)); + + start = pos + length; + } + std::string before_text = text_utf8.substr(start); + if (before_text.size() > 0) + text_nodes->Append(CreateTextNode(before_text)); + } else { + text_nodes->Append(CreateTextNode(text_utf8)); + } + + node->Set(kTextBoxNodes, text_nodes); // Takes ownership of |text_nodes|. + return node; +} + +base::Value* PDFiumPage::CreateTextNode(std::string text) { + base::DictionaryValue* node = new base::DictionaryValue(); + node->SetString(kTextNodeType, kTextNodeTypeText); + node->SetString(kTextNodeText, text); + return node; +} + +base::Value* PDFiumPage::CreateURLNode(std::string text, std::string url) { + base::DictionaryValue* node = new base::DictionaryValue(); + node->SetString(kTextNodeType, kTextNodeTypeURL); + node->SetString(kTextNodeText, text); + node->SetString(kTextNodeURL, url); + return node; +} + +PDFiumPage::Area PDFiumPage::GetCharIndex(const pp::Point& point, + int rotation, + int* char_index, + LinkTarget* target) { + if (!available_) + return NONSELECTABLE_AREA; + pp::Point point2 = point - rect_.point(); + double new_x, new_y; + FPDF_DeviceToPage(GetPage(), 0, 0, rect_.width(), rect_.height(), + rotation, point2.x(), point2.y(), &new_x, &new_y); + + int rv = FPDFText_GetCharIndexAtPos( + GetTextPage(), new_x, new_y, kTolerance, kTolerance); + *char_index = rv; + + FPDF_LINK link = FPDFLink_GetLinkAtPoint(GetPage(), new_x, new_y); + if (link) { + // We don't handle all possible link types of the PDF. For example, + // launch actions, cross-document links, etc. + // In that case, GetLinkTarget() will return NONSELECTABLE_AREA + // and we should proceed with area detection. + PDFiumPage::Area area = GetLinkTarget(link, target); + if (area != PDFiumPage::NONSELECTABLE_AREA) + return area; + } + + if (rv < 0) + return NONSELECTABLE_AREA; + + return GetLink(*char_index, target) != -1 ? WEBLINK_AREA : TEXT_AREA; +} + +base::char16 PDFiumPage::GetCharAtIndex(int index) { + if (!available_) + return L'\0'; + return static_cast(FPDFText_GetUnicode(GetTextPage(), index)); +} + +int PDFiumPage::GetCharCount() { + if (!available_) + return 0; + return FPDFText_CountChars(GetTextPage()); +} + +PDFiumPage::Area PDFiumPage::GetLinkTarget( + FPDF_LINK link, PDFiumPage::LinkTarget* target) { + FPDF_DEST dest = FPDFLink_GetDest(engine_->doc(), link); + if (dest != NULL) + return GetDestinationTarget(dest, target); + + FPDF_ACTION action = FPDFLink_GetAction(link); + if (action) { + switch (FPDFAction_GetType(action)) { + case PDFACTION_GOTO: { + FPDF_DEST dest = FPDFAction_GetDest(engine_->doc(), action); + if (dest) + return GetDestinationTarget(dest, target); + // TODO(gene): We don't fully support all types of the in-document + // links. Need to implement that. There is a bug to track that: + // http://code.google.com/p/chromium/issues/detail?id=55776 + } break; + case PDFACTION_URI: { + if (target) { + size_t buffer_size = + FPDFAction_GetURIPath(engine_->doc(), action, NULL, 0); + if (buffer_size > 1) { + void* data = WriteInto(&target->url, buffer_size + 1); + FPDFAction_GetURIPath(engine_->doc(), action, data, buffer_size); + } + } + return WEBLINK_AREA; + } break; + // TODO(gene): We don't support PDFACTION_REMOTEGOTO and PDFACTION_LAUNCH + // at the moment. + } + } + + return NONSELECTABLE_AREA; +} + +PDFiumPage::Area PDFiumPage::GetDestinationTarget( + FPDF_DEST destination, PDFiumPage::LinkTarget* target) { + int page_index = FPDFDest_GetPageIndex(engine_->doc(), destination); + if (target) { + target->page = page_index; + } + return DOCLINK_AREA; +} + +int PDFiumPage::GetLink(int char_index, PDFiumPage::LinkTarget* target) { + if (!available_) + return -1; + + CalculateLinks(); + + // Get the bounding box of the rect again, since it might have moved because + // of the tolerance above. + double left, right, bottom, top; + FPDFText_GetCharBox(GetTextPage(), char_index, &left, &right, &bottom, &top); + + pp::Point origin( + PageToScreen(pp::Point(), 1.0, left, top, right, bottom, 0).point()); + for (size_t i = 0; i < links_.size(); ++i) { + for (size_t j = 0; j < links_[i].rects.size(); ++j) { + if (links_[i].rects[j].Contains(origin)) { + if (target) + target->url = links_[i].url; + return i; + } + } + } + return -1; +} + +std::vector PDFiumPage::GetLinks(pp::Rect text_area, + std::vector* targets) { + if (!available_) + return std::vector(); + + CalculateLinks(); + + std::vector links; + + for (size_t i = 0; i < links_.size(); ++i) { + for (size_t j = 0; j < links_[i].rects.size(); ++j) { + if (links_[i].rects[j].Intersects(text_area)) { + if (targets) { + LinkTarget target; + target.url = links_[i].url; + targets->push_back(target); + } + links.push_back(i); + } + } + } + return links; +} + +void PDFiumPage::CalculateLinks() { + if (calculated_links_) + return; + + calculated_links_ = true; + FPDF_PAGELINK links = FPDFLink_LoadWebLinks(GetTextPage()); + int count = FPDFLink_CountWebLinks(links); + for (int i = 0; i < count; ++i) { + base::string16 url; + int url_length = FPDFLink_GetURL(links, i, NULL, 0); + if (url_length > 1) { // WriteInto needs at least 2 characters. + unsigned short* data = + reinterpret_cast(WriteInto(&url, url_length + 1)); + FPDFLink_GetURL(links, i, data, url_length); + } + Link link; + link.url = base::UTF16ToUTF8(url); + + // If the link cannot be converted to a pp::Var, then it is not possible to + // pass it to JS. In this case, ignore the link like other PDF viewers. + // See http://crbug.com/312882 for an example. + pp::Var link_var(link.url); + if (!link_var.is_string()) + continue; + + // Make sure all the characters in the URL are valid per RFC 1738. + // http://crbug.com/340326 has a sample bad PDF. + // GURL does not work correctly, e.g. it just strips \t \r \n. + bool is_invalid_url = false; + for (size_t j = 0; j < link.url.length(); ++j) { + // Control characters are not allowed. + // 0x7F is also a control character. + // 0x80 and above are not in US-ASCII. + if (link.url[j] < ' ' || link.url[j] >= '\x7F') { + is_invalid_url = true; + break; + } + } + if (is_invalid_url) + continue; + + int rect_count = FPDFLink_CountRects(links, i); + for (int j = 0; j < rect_count; ++j) { + double left, top, right, bottom; + FPDFLink_GetRect(links, i, j, &left, &top, &right, &bottom); + link.rects.push_back( + PageToScreen(pp::Point(), 1.0, left, top, right, bottom, 0)); + } + links_.push_back(link); + } + FPDFLink_CloseWebLinks(links); +} + +pp::Rect PDFiumPage::PageToScreen(const pp::Point& offset, + double zoom, + double left, + double top, + double right, + double bottom, + int rotation) { + if (!available_) + return pp::Rect(); + + int new_left, new_top, new_right, new_bottom; + FPDF_PageToDevice( + page_, + static_cast((rect_.x() - offset.x()) * zoom), + static_cast((rect_.y() - offset.y()) * zoom), + static_cast(ceil(rect_.width() * zoom)), + static_cast(ceil(rect_.height() * zoom)), + rotation, left, top, &new_left, &new_top); + FPDF_PageToDevice( + page_, + static_cast((rect_.x() - offset.x()) * zoom), + static_cast((rect_.y() - offset.y()) * zoom), + static_cast(ceil(rect_.width() * zoom)), + static_cast(ceil(rect_.height() * zoom)), + rotation, right, bottom, &new_right, &new_bottom); + + // If the PDF is rotated, the horizontal/vertical coordinates could be + // flipped. See + // http://www.netl.doe.gov/publications/proceedings/03/ubc/presentations/Goeckner-pres.pdf + if (new_right < new_left) + std::swap(new_right, new_left); + if (new_bottom < new_top) + std::swap(new_bottom, new_top); + + return pp::Rect( + new_left, new_top, new_right - new_left + 1, new_bottom - new_top + 1); +} + +PDFiumPage::Link::Link() { +} + +PDFiumPage::Link::~Link() { +} + +} // namespace chrome_pdf diff --git a/xfa_test/pdf/pdfium/pdfium_page.h b/xfa_test/pdf/pdfium/pdfium_page.h new file mode 100644 index 0000000000..22ea142988 --- /dev/null +++ b/xfa_test/pdf/pdfium/pdfium_page.h @@ -0,0 +1,136 @@ +// Copyright (c) 2010 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#ifndef PDF_PDFIUM_PDFIUM_PAGE_H_ +#define PDF_PDFIUM_PDFIUM_PAGE_H_ + +#include +#include + +#include "base/strings/string16.h" +#include "ppapi/cpp/rect.h" +#include "third_party/pdfium/fpdfsdk/include/fpdfdoc.h" +#include "third_party/pdfium/fpdfsdk/include/fpdfformfill.h" +#include "third_party/pdfium/fpdfsdk/include/fpdftext.h" + +namespace base { +class Value; +} + +namespace chrome_pdf { + +class PDFiumEngine; + +// Wrapper around a page from the document. +class PDFiumPage { + public: + PDFiumPage(PDFiumEngine* engine, + int i, + const pp::Rect& r, + bool available); + ~PDFiumPage(); + // Unloads the PDFium data for this page from memory. + void Unload(); + // Gets the FPDF_PAGE for this page, loading and parsing it if necessary. + FPDF_PAGE GetPage(); + //Get the FPDF_PAGE for printing. + FPDF_PAGE GetPrintPage(); + //Close the printing page. + void ClosePrintPage(); + + // Returns FPDF_TEXTPAGE for the page, loading and parsing it if necessary. + FPDF_TEXTPAGE GetTextPage(); + + // Returns a DictionaryValue version of the page. + base::Value* GetAccessibleContentAsValue(int rotation); + + enum Area { + NONSELECTABLE_AREA, + TEXT_AREA, + WEBLINK_AREA, // Area is a hyperlink. + DOCLINK_AREA, // Area is a link to a different part of the same document. + }; + + struct LinkTarget { + // We are using std::string here which have a copy contructor. + // That prevents us from using union here. + std::string url; // Valid for WEBLINK_AREA only. + int page; // Valid for DOCLINK_AREA only. + }; + + // Given a point in the document that's in this page, returns its character + // index if it's near a character, and also the type of text. + // Target is optional. It will be filled in for WEBLINK_AREA or + // DOCLINK_AREA only. + Area GetCharIndex(const pp::Point& point, int rotation, int* char_index, + LinkTarget* target); + + // Gets the character at the given index. + base::char16 GetCharAtIndex(int index); + + // Gets the number of characters in the page. + int GetCharCount(); + + // Converts from page coordinates to screen coordinates. + pp::Rect PageToScreen(const pp::Point& offset, + double zoom, + double left, + double top, + double right, + double bottom, + int rotation); + + int index() const { return index_; } + pp::Rect rect() const { return rect_; } + void set_rect(const pp::Rect& r) { rect_ = r; } + bool available() const { return available_; } + void set_available(bool available) { available_ = available; } + void set_calculated_links(bool calculated_links) { + calculated_links_ = calculated_links; + } + + private: + // Returns a link index if the given character index is over a link, or -1 + // otherwise. + int GetLink(int char_index, LinkTarget* target); + // Returns the link indices if the given rect intersects a link rect, or an + // empty vector otherwise. + std::vector GetLinks(pp::Rect text_area, + std::vector* targets); + // Calculate the locations of any links on the page. + void CalculateLinks(); + // Returns link type and target associated with a link. Returns + // NONSELECTABLE_AREA if link detection failed. + Area GetLinkTarget(FPDF_LINK link, LinkTarget* target); + // Returns target associated with a destination. + Area GetDestinationTarget(FPDF_DEST destination, LinkTarget* target); + // Returns the text in the supplied box as a Value Node + base::Value* GetTextBoxAsValue(double page_height, double left, double top, + double right, double bottom, int rotation); + // Helper functions for JSON generation + base::Value* CreateTextNode(std::string text); + base::Value* CreateURLNode(std::string text, std::string url); + + struct Link { + Link(); + ~Link(); + + std::string url; + // Bounding rectangles of characters. + std::vector rects; + }; + + PDFiumEngine* engine_; + FPDF_PAGE page_; + FPDF_TEXTPAGE text_page_; + int index_; + pp::Rect rect_; + bool calculated_links_; + std::vector links_; + bool available_; +}; + +} // namespace chrome_pdf + +#endif // PDF_PDFIUM_PDFIUM_PAGE_H_ diff --git a/xfa_test/pdf/pdfium/pdfium_range.cc b/xfa_test/pdf/pdfium/pdfium_range.cc new file mode 100644 index 0000000000..c77d5906dd --- /dev/null +++ b/xfa_test/pdf/pdfium/pdfium_range.cc @@ -0,0 +1,79 @@ +// Copyright (c) 2010 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#include "pdf/pdfium/pdfium_range.h" + +#include "base/logging.h" +#include "base/strings/string_util.h" + +namespace chrome_pdf { + +PDFiumRange::PDFiumRange(PDFiumPage* page, int char_index, int char_count) + : page_(page), + char_index_(char_index), + char_count_(char_count), + cached_screen_rects_zoom_(0) { +} + +PDFiumRange::~PDFiumRange() { +} + +void PDFiumRange::SetCharCount(int char_count) { + char_count_ = char_count; + + cached_screen_rects_offset_ = pp::Point(); + cached_screen_rects_zoom_ = 0; +} + +std::vector PDFiumRange::GetScreenRects(const pp::Point& offset, + double zoom, + int rotation) { + if (offset == cached_screen_rects_offset_ && + zoom == cached_screen_rects_zoom_) { + return cached_screen_rects_; + } + + cached_screen_rects_.clear(); + cached_screen_rects_offset_ = offset; + cached_screen_rects_zoom_ = zoom; + + int char_index = char_index_; + int char_count = char_count_; + if (char_count < 0) { + char_count *= -1; + char_index -= char_count - 1; + } + + int count = FPDFText_CountRects(page_->GetTextPage(), char_index, char_count); + for (int i = 0; i < count; ++i) { + double left, top, right, bottom; + FPDFText_GetRect(page_->GetTextPage(), i, &left, &top, &right, &bottom); + cached_screen_rects_.push_back( + page_->PageToScreen(offset, zoom, left, top, right, bottom, rotation)); + } + + return cached_screen_rects_; +} + +base::string16 PDFiumRange::GetText() { + int index = char_index_; + int count = char_count_; + if (!count) + return base::string16(); + if (count < 0) { + count *= -1; + index -= count - 1; + } + + base::string16 rv; + unsigned short* data = + reinterpret_cast(WriteInto(&rv, count + 1)); + if (data) { + int written = FPDFText_GetText(page_->GetTextPage(), index, count, data); + rv.reserve(written); + } + return rv; +} + +} // namespace chrome_pdf diff --git a/xfa_test/pdf/pdfium/pdfium_range.h b/xfa_test/pdf/pdfium/pdfium_range.h new file mode 100644 index 0000000000..ed8daf65bb --- /dev/null +++ b/xfa_test/pdf/pdfium/pdfium_range.h @@ -0,0 +1,54 @@ +// Copyright (c) 2010 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#ifndef PDF_PDFIUM_PDFIUM_RANGE_H_ +#define PDF_PDFIUM_PDFIUM_RANGE_H_ + +#include +#include + +#include "base/strings/string16.h" +#include "pdf/pdfium/pdfium_page.h" +#include "ppapi/cpp/rect.h" + +namespace chrome_pdf { + +// Describes location of a string of characters. +class PDFiumRange { + public: + PDFiumRange(PDFiumPage* page, int char_index, int char_count); + ~PDFiumRange(); + + // Update how many characters are in the selection. Could be negative if + // backwards. + void SetCharCount(int char_count); + + int page_index() const { return page_->index(); } + int char_index() const { return char_index_; } + int char_count() const { return char_count_; } + + // Gets bounding rectangles of range in screen coordinates. + std::vector GetScreenRects(const pp::Point& offset, + double zoom, + int rotation); + + // Gets the string of characters in this range. + base::string16 GetText(); + + private: + PDFiumPage* page_; + // Index of first character. + int char_index_; + // How many characters are part of this range (negative if backwards). + int char_count_; + + // Cache of ScreenRect, and the associated variables used when caching it. + std::vector cached_screen_rects_; + pp::Point cached_screen_rects_offset_; + double cached_screen_rects_zoom_; +}; + +} // namespace chrome_pdf + +#endif // PDF_PDFIUM_PDFIUM_RANGE_H_ -- cgit v1.2.3