Fix FullScreen crash in Webapp
[platform/framework/web/chromium-efl.git] / pdf / pdf.cc
1 // Copyright 2010 The Chromium Authors
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/feature_list.h"
12 #include "build/build_config.h"
13 #include "pdf/pdf_engine.h"
14 #include "pdf/pdf_features.h"
15 #include "pdf/pdf_init.h"
16 #include "third_party/abseil-cpp/absl/types/optional.h"
17 #include "ui/gfx/geometry/rect.h"
18 #include "ui/gfx/geometry/size_f.h"
19
20 namespace chrome_pdf {
21
22 namespace {
23
24 absl::optional<bool> g_use_skia_renderer_enabled_by_policy;
25
26 class ScopedSdkInitializer {
27  public:
28   explicit ScopedSdkInitializer(bool enable_v8) {
29     if (!IsSDKInitializedViaPlugin()) {
30       InitializeSDK(
31           enable_v8,
32           g_use_skia_renderer_enabled_by_policy.value_or(
33               base::FeatureList::IsEnabled(features::kPdfUseSkiaRenderer)),
34           FontMappingMode::kNoMapping);
35     }
36   }
37
38   ScopedSdkInitializer(const ScopedSdkInitializer&) = delete;
39   ScopedSdkInitializer& operator=(const ScopedSdkInitializer&) = delete;
40
41   ~ScopedSdkInitializer() {
42     if (!IsSDKInitializedViaPlugin())
43       ShutdownSDK();
44   }
45 };
46
47 }  // namespace
48
49 void SetUseSkiaRendererPolicy(bool use_skia) {
50   g_use_skia_renderer_enabled_by_policy = use_skia;
51 }
52
53 #if BUILDFLAG(IS_CHROMEOS)
54 std::vector<uint8_t> CreateFlattenedPdf(
55     base::span<const uint8_t> input_buffer) {
56   ScopedSdkInitializer scoped_sdk_initializer(/*enable_v8=*/false);
57   return PDFEngineExports::Get()->CreateFlattenedPdf(input_buffer);
58 }
59 #endif  // BUILDFLAG(IS_CHROMEOS)
60
61 #if BUILDFLAG(IS_WIN)
62 bool RenderPDFPageToDC(base::span<const uint8_t> pdf_buffer,
63                        int page_index,
64                        HDC dc,
65                        int dpi_x,
66                        int dpi_y,
67                        int bounds_origin_x,
68                        int bounds_origin_y,
69                        int bounds_width,
70                        int bounds_height,
71                        bool fit_to_bounds,
72                        bool stretch_to_bounds,
73                        bool keep_aspect_ratio,
74                        bool center_in_bounds,
75                        bool autorotate,
76                        bool use_color) {
77   ScopedSdkInitializer scoped_sdk_initializer(/*enable_v8=*/true);
78   PDFEngineExports* engine_exports = PDFEngineExports::Get();
79   PDFEngineExports::RenderingSettings settings(
80       gfx::Size(dpi_x, dpi_y),
81       gfx::Rect(bounds_origin_x, bounds_origin_y, bounds_width, bounds_height),
82       fit_to_bounds, stretch_to_bounds, keep_aspect_ratio, center_in_bounds,
83       autorotate, use_color, /*render_for_printing=*/true);
84   return engine_exports->RenderPDFPageToDC(pdf_buffer, page_index, settings,
85                                            dc);
86 }
87
88 void SetPDFUsePrintMode(int mode) {
89   PDFEngineExports::Get()->SetPDFUsePrintMode(mode);
90 }
91 #endif  // BUILDFLAG(IS_WIN)
92
93 bool GetPDFDocInfo(base::span<const uint8_t> pdf_buffer,
94                    int* page_count,
95                    float* max_page_width) {
96   ScopedSdkInitializer scoped_sdk_initializer(/*enable_v8=*/true);
97   PDFEngineExports* engine_exports = PDFEngineExports::Get();
98   return engine_exports->GetPDFDocInfo(pdf_buffer, page_count, max_page_width);
99 }
100
101 absl::optional<bool> IsPDFDocTagged(base::span<const uint8_t> pdf_buffer) {
102   ScopedSdkInitializer scoped_sdk_initializer(/*enable_v8=*/true);
103   PDFEngineExports* engine_exports = PDFEngineExports::Get();
104   return engine_exports->IsPDFDocTagged(pdf_buffer);
105 }
106
107 base::Value GetPDFStructTreeForPage(base::span<const uint8_t> pdf_buffer,
108                                     int page_index) {
109   ScopedSdkInitializer scoped_sdk_initializer(/*enable_v8=*/true);
110   PDFEngineExports* engine_exports = PDFEngineExports::Get();
111   return engine_exports->GetPDFStructTreeForPage(pdf_buffer, page_index);
112 }
113
114 absl::optional<gfx::SizeF> GetPDFPageSizeByIndex(
115     base::span<const uint8_t> pdf_buffer,
116     int page_index) {
117   ScopedSdkInitializer scoped_sdk_initializer(/*enable_v8=*/true);
118   chrome_pdf::PDFEngineExports* engine_exports =
119       chrome_pdf::PDFEngineExports::Get();
120   return engine_exports->GetPDFPageSizeByIndex(pdf_buffer, page_index);
121 }
122
123 bool RenderPDFPageToBitmap(base::span<const uint8_t> pdf_buffer,
124                            int page_index,
125                            void* bitmap_buffer,
126                            const gfx::Size& bitmap_size,
127                            const gfx::Size& dpi,
128                            const RenderOptions& options) {
129   ScopedSdkInitializer scoped_sdk_initializer(/*enable_v8=*/true);
130   PDFEngineExports* engine_exports = PDFEngineExports::Get();
131   PDFEngineExports::RenderingSettings settings(
132       dpi, gfx::Rect(bitmap_size),
133       /*fit_to_bounds=*/true, options.stretch_to_bounds,
134       options.keep_aspect_ratio,
135       /*center_in_bounds=*/true, options.autorotate, options.use_color,
136       options.render_device_type == RenderDeviceType::kPrinter);
137   return engine_exports->RenderPDFPageToBitmap(pdf_buffer, page_index, settings,
138                                                bitmap_buffer);
139 }
140
141 std::vector<uint8_t> ConvertPdfPagesToNupPdf(
142     std::vector<base::span<const uint8_t>> input_buffers,
143     size_t pages_per_sheet,
144     const gfx::Size& page_size,
145     const gfx::Rect& printable_area) {
146   ScopedSdkInitializer scoped_sdk_initializer(/*enable_v8=*/false);
147   PDFEngineExports* engine_exports = PDFEngineExports::Get();
148   return engine_exports->ConvertPdfPagesToNupPdf(
149       std::move(input_buffers), pages_per_sheet, page_size, printable_area);
150 }
151
152 std::vector<uint8_t> ConvertPdfDocumentToNupPdf(
153     base::span<const uint8_t> input_buffer,
154     size_t pages_per_sheet,
155     const gfx::Size& page_size,
156     const gfx::Rect& printable_area) {
157   ScopedSdkInitializer scoped_sdk_initializer(/*enable_v8=*/false);
158   PDFEngineExports* engine_exports = PDFEngineExports::Get();
159   return engine_exports->ConvertPdfDocumentToNupPdf(
160       input_buffer, pages_per_sheet, page_size, printable_area);
161 }
162
163 }  // namespace chrome_pdf