Add ICU message format support
[chromium-blink-merge.git] / pdf / pdf.cc
blob655a13359eefb7420310213430ceed8ab4713730
1 // Copyright (c) 2010 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "pdf/pdf.h"
7 #if defined(OS_WIN)
8 #include <windows.h>
9 #endif
11 #include "base/command_line.h"
12 #include "base/logging.h"
13 #include "pdf/out_of_process_instance.h"
14 #include "ppapi/c/ppp.h"
15 #include "ppapi/cpp/private/internal_module.h"
16 #include "ppapi/cpp/private/pdf.h"
17 #include "v8/include/v8.h"
19 bool g_sdk_initialized_via_pepper = false;
21 namespace chrome_pdf {
23 PDFModule::PDFModule() {
26 PDFModule::~PDFModule() {
27 if (g_sdk_initialized_via_pepper) {
28 chrome_pdf::ShutdownSDK();
29 g_sdk_initialized_via_pepper = false;
33 bool PDFModule::Init() {
34 return true;
37 pp::Instance* PDFModule::CreateInstance(PP_Instance instance) {
38 if (!g_sdk_initialized_via_pepper) {
39 v8::StartupData natives;
40 v8::StartupData snapshot;
41 pp::PDF::GetV8ExternalSnapshotData(pp::InstanceHandle(instance),
42 &natives.data, &natives.raw_size,
43 &snapshot.data, &snapshot.raw_size);
44 if (natives.data) {
45 v8::V8::SetNativesDataBlob(&natives);
46 v8::V8::SetSnapshotDataBlob(&snapshot);
48 if (!chrome_pdf::InitializeSDK())
49 return NULL;
50 g_sdk_initialized_via_pepper = true;
53 return new OutOfProcessInstance(instance);
57 // Implementation of Global PPP functions ---------------------------------
58 int32_t PPP_InitializeModule(PP_Module module_id,
59 PPB_GetInterface get_browser_interface) {
60 PDFModule* module = new PDFModule();
61 if (!module->InternalInit(module_id, get_browser_interface)) {
62 delete module;
63 return PP_ERROR_FAILED;
66 pp::InternalSetModuleSingleton(module);
67 return PP_OK;
70 void PPP_ShutdownModule() {
71 delete pp::Module::Get();
72 pp::InternalSetModuleSingleton(NULL);
75 const void* PPP_GetInterface(const char* interface_name) {
76 if (!pp::Module::Get())
77 return NULL;
78 return pp::Module::Get()->GetPluginInterface(interface_name);
81 #if defined(OS_WIN)
82 bool RenderPDFPageToDC(const void* pdf_buffer,
83 int buffer_size,
84 int page_number,
85 HDC dc,
86 int dpi,
87 int bounds_origin_x,
88 int bounds_origin_y,
89 int bounds_width,
90 int bounds_height,
91 bool fit_to_bounds,
92 bool stretch_to_bounds,
93 bool keep_aspect_ratio,
94 bool center_in_bounds,
95 bool autorotate) {
96 if (!g_sdk_initialized_via_pepper) {
97 if (!chrome_pdf::InitializeSDK()) {
98 return false;
101 scoped_ptr<chrome_pdf::PDFEngineExports> engine_exports(
102 chrome_pdf::PDFEngineExports::Create());
103 chrome_pdf::PDFEngineExports::RenderingSettings settings(
104 dpi, dpi, pp::Rect(bounds_origin_x, bounds_origin_y, bounds_width,
105 bounds_height),
106 fit_to_bounds, stretch_to_bounds, keep_aspect_ratio, center_in_bounds,
107 autorotate);
108 bool ret = engine_exports->RenderPDFPageToDC(pdf_buffer, buffer_size,
109 page_number, settings, dc);
110 if (!g_sdk_initialized_via_pepper) {
111 chrome_pdf::ShutdownSDK();
113 return ret;
116 #endif // OS_WIN
118 bool GetPDFDocInfo(const void* pdf_buffer,
119 int buffer_size, int* page_count,
120 double* max_page_width) {
121 if (!g_sdk_initialized_via_pepper) {
122 if (!chrome_pdf::InitializeSDK())
123 return false;
125 scoped_ptr<chrome_pdf::PDFEngineExports> engine_exports(
126 chrome_pdf::PDFEngineExports::Create());
127 bool ret = engine_exports->GetPDFDocInfo(
128 pdf_buffer, buffer_size, page_count, max_page_width);
129 if (!g_sdk_initialized_via_pepper) {
130 chrome_pdf::ShutdownSDK();
132 return ret;
135 bool GetPDFPageSizeByIndex(const void* pdf_buffer,
136 int pdf_buffer_size, int page_number,
137 double* width, double* height) {
138 if (!g_sdk_initialized_via_pepper) {
139 if (!chrome_pdf::InitializeSDK())
140 return false;
142 scoped_ptr<chrome_pdf::PDFEngineExports> engine_exports(
143 chrome_pdf::PDFEngineExports::Create());
144 bool ret = engine_exports->GetPDFPageSizeByIndex(
145 pdf_buffer, pdf_buffer_size, page_number, width, height);
146 if (!g_sdk_initialized_via_pepper)
147 chrome_pdf::ShutdownSDK();
148 return ret;
151 bool RenderPDFPageToBitmap(const void* pdf_buffer,
152 int pdf_buffer_size,
153 int page_number,
154 void* bitmap_buffer,
155 int bitmap_width,
156 int bitmap_height,
157 int dpi,
158 bool autorotate) {
159 if (!g_sdk_initialized_via_pepper) {
160 if (!chrome_pdf::InitializeSDK())
161 return false;
163 scoped_ptr<chrome_pdf::PDFEngineExports> engine_exports(
164 chrome_pdf::PDFEngineExports::Create());
165 chrome_pdf::PDFEngineExports::RenderingSettings settings(
166 dpi, dpi, pp::Rect(bitmap_width, bitmap_height), true, false, true, true,
167 autorotate);
168 bool ret = engine_exports->RenderPDFPageToBitmap(
169 pdf_buffer, pdf_buffer_size, page_number, settings, bitmap_buffer);
170 if (!g_sdk_initialized_via_pepper) {
171 chrome_pdf::ShutdownSDK();
173 return ret;
176 } // namespace chrome_pdf