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.
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"
20 namespace chrome_pdf {
24 absl::optional<bool> g_use_skia_renderer_enabled_by_policy;
26 class ScopedSdkInitializer {
28 explicit ScopedSdkInitializer(bool enable_v8) {
29 if (!IsSDKInitializedViaPlugin()) {
32 g_use_skia_renderer_enabled_by_policy.value_or(
33 base::FeatureList::IsEnabled(features::kPdfUseSkiaRenderer)),
34 FontMappingMode::kNoMapping);
38 ScopedSdkInitializer(const ScopedSdkInitializer&) = delete;
39 ScopedSdkInitializer& operator=(const ScopedSdkInitializer&) = delete;
41 ~ScopedSdkInitializer() {
42 if (!IsSDKInitializedViaPlugin())
49 void SetUseSkiaRendererPolicy(bool use_skia) {
50 g_use_skia_renderer_enabled_by_policy = use_skia;
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);
59 #endif // BUILDFLAG(IS_CHROMEOS)
62 bool RenderPDFPageToDC(base::span<const uint8_t> pdf_buffer,
72 bool stretch_to_bounds,
73 bool keep_aspect_ratio,
74 bool center_in_bounds,
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,
88 void SetPDFUsePrintMode(int mode) {
89 PDFEngineExports::Get()->SetPDFUsePrintMode(mode);
91 #endif // BUILDFLAG(IS_WIN)
93 bool GetPDFDocInfo(base::span<const uint8_t> pdf_buffer,
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);
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);
107 base::Value GetPDFStructTreeForPage(base::span<const uint8_t> pdf_buffer,
109 ScopedSdkInitializer scoped_sdk_initializer(/*enable_v8=*/true);
110 PDFEngineExports* engine_exports = PDFEngineExports::Get();
111 return engine_exports->GetPDFStructTreeForPage(pdf_buffer, page_index);
114 absl::optional<gfx::SizeF> GetPDFPageSizeByIndex(
115 base::span<const uint8_t> pdf_buffer,
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);
123 bool RenderPDFPageToBitmap(base::span<const uint8_t> pdf_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,
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);
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);
163 } // namespace chrome_pdf