Upload upstream chromium 76.0.3809.146
[platform/framework/web/chromium-efl.git] / pdf / pdf.cc
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.
4
5 #include "pdf/pdf.h"
6
7 #include <stdint.h>
8
9 #include <utility>
10
11 #include "base/logging.h"
12 #include "base/macros.h"
13 #include "pdf/out_of_process_instance.h"
14 #include "pdf/pdf_ppapi.h"
15
16 namespace chrome_pdf {
17
18 namespace {
19
20 class ScopedSdkInitializer {
21  public:
22   ScopedSdkInitializer() {}
23   ~ScopedSdkInitializer() {
24 #if DCHECK_IS_ON()
25     DCHECK(initialized_);
26 #endif
27     if (!IsSDKInitializedViaPepper())
28       ShutdownSDK();
29   }
30
31   // Must be called.
32   bool Init() {
33 #if DCHECK_IS_ON()
34     initialized_ = true;
35 #endif
36     return IsSDKInitializedViaPepper() || InitializeSDK();
37   }
38
39  private:
40 #if DCHECK_IS_ON()
41   bool initialized_ = false;
42 #endif
43
44   DISALLOW_COPY_AND_ASSIGN(ScopedSdkInitializer);
45 };
46
47 }  // namespace
48
49 #if defined(OS_WIN)
50 bool RenderPDFPageToDC(base::span<const uint8_t> pdf_buffer,
51                        int page_number,
52                        HDC dc,
53                        int dpi_x,
54                        int dpi_y,
55                        int bounds_origin_x,
56                        int bounds_origin_y,
57                        int bounds_width,
58                        int bounds_height,
59                        bool fit_to_bounds,
60                        bool stretch_to_bounds,
61                        bool keep_aspect_ratio,
62                        bool center_in_bounds,
63                        bool autorotate,
64                        bool use_color) {
65   ScopedSdkInitializer scoped_sdk_initializer;
66   if (!scoped_sdk_initializer.Init())
67     return false;
68
69   PDFEngineExports* engine_exports = PDFEngineExports::Get();
70   PDFEngineExports::RenderingSettings settings(
71       dpi_x, dpi_y,
72       pp::Rect(bounds_origin_x, bounds_origin_y, bounds_width, bounds_height),
73       fit_to_bounds, stretch_to_bounds, keep_aspect_ratio, center_in_bounds,
74       autorotate, use_color);
75   return engine_exports->RenderPDFPageToDC(pdf_buffer, page_number, settings,
76                                            dc);
77 }
78
79 void SetPDFEnsureTypefaceCharactersAccessible(
80     PDFEnsureTypefaceCharactersAccessible func) {
81   PDFEngineExports::Get()->SetPDFEnsureTypefaceCharactersAccessible(func);
82 }
83
84 void SetPDFUseGDIPrinting(bool enable) {
85   PDFEngineExports::Get()->SetPDFUseGDIPrinting(enable);
86 }
87
88 void SetPDFUsePrintMode(int mode) {
89   PDFEngineExports::Get()->SetPDFUsePrintMode(mode);
90 }
91 #endif  // defined(OS_WIN)
92
93 bool GetPDFDocInfo(base::span<const uint8_t> pdf_buffer,
94                    int* page_count,
95                    double* max_page_width) {
96   ScopedSdkInitializer scoped_sdk_initializer;
97   if (!scoped_sdk_initializer.Init())
98     return false;
99
100   PDFEngineExports* engine_exports = PDFEngineExports::Get();
101   return engine_exports->GetPDFDocInfo(pdf_buffer, page_count, max_page_width);
102 }
103
104 bool GetPDFPageSizeByIndex(base::span<const uint8_t> pdf_buffer,
105                            int page_number,
106                            double* width,
107                            double* height) {
108   ScopedSdkInitializer scoped_sdk_initializer;
109   if (!scoped_sdk_initializer.Init())
110     return false;
111
112   chrome_pdf::PDFEngineExports* engine_exports =
113       chrome_pdf::PDFEngineExports::Get();
114   return engine_exports->GetPDFPageSizeByIndex(pdf_buffer, page_number, width,
115                                                height);
116 }
117
118 bool RenderPDFPageToBitmap(base::span<const uint8_t> pdf_buffer,
119                            int page_number,
120                            void* bitmap_buffer,
121                            int bitmap_width,
122                            int bitmap_height,
123                            int dpi_x,
124                            int dpi_y,
125                            bool autorotate,
126                            bool use_color) {
127   ScopedSdkInitializer scoped_sdk_initializer;
128   if (!scoped_sdk_initializer.Init())
129     return false;
130
131   PDFEngineExports* engine_exports = PDFEngineExports::Get();
132   PDFEngineExports::RenderingSettings settings(
133       dpi_x, dpi_y, pp::Rect(bitmap_width, bitmap_height), true, false, true,
134       true, autorotate, use_color);
135   return engine_exports->RenderPDFPageToBitmap(pdf_buffer, page_number,
136                                                settings, bitmap_buffer);
137 }
138
139 std::vector<uint8_t> ConvertPdfPagesToNupPdf(
140     std::vector<base::span<const uint8_t>> input_buffers,
141     size_t pages_per_sheet,
142     const gfx::Size& page_size,
143     const gfx::Rect& printable_area) {
144   ScopedSdkInitializer scoped_sdk_initializer;
145   if (!scoped_sdk_initializer.Init())
146     return std::vector<uint8_t>();
147
148   PDFEngineExports* engine_exports = PDFEngineExports::Get();
149   return engine_exports->ConvertPdfPagesToNupPdf(
150       std::move(input_buffers), pages_per_sheet, page_size, printable_area);
151 }
152
153 std::vector<uint8_t> ConvertPdfDocumentToNupPdf(
154     base::span<const uint8_t> input_buffer,
155     size_t pages_per_sheet,
156     const gfx::Size& page_size,
157     const gfx::Rect& printable_area) {
158   ScopedSdkInitializer scoped_sdk_initializer;
159   if (!scoped_sdk_initializer.Init())
160     return std::vector<uint8_t>();
161
162   PDFEngineExports* engine_exports = PDFEngineExports::Get();
163   return engine_exports->ConvertPdfDocumentToNupPdf(
164       input_buffer, pages_per_sheet, page_size, printable_area);
165 }
166
167 }  // namespace chrome_pdf