[M108 Migration][HBBTV] Implement ewk_context_register_jsplugin_mime_types API
[platform/framework/web/chromium-efl.git] / pdf / pdf.h
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 #ifndef PDF_PDF_H_
6 #define PDF_PDF_H_
7
8 #include <vector>
9
10 #include "base/containers/span.h"
11 #include "base/values.h"
12 #include "build/build_config.h"
13 #include "third_party/abseil-cpp/absl/types/optional.h"
14
15 #if BUILDFLAG(IS_WIN)
16 #include <windows.h>
17 #endif
18
19 namespace gfx {
20 class Rect;
21 class Size;
22 class SizeF;
23 }  // namespace gfx
24
25 namespace chrome_pdf {
26
27 #if BUILDFLAG(IS_CHROMEOS)
28 // Create a flattened PDF document from an existing PDF document.
29 // `input_buffer` is the buffer that contains the entire PDF document to be
30 // flattened.
31 std::vector<uint8_t> CreateFlattenedPdf(base::span<const uint8_t> input_buffer);
32 #endif  // BUILDFLAG(IS_CHROMEOS)
33
34 #if BUILDFLAG(IS_WIN)
35 // Printing modes - type to convert PDF to for printing. See PDFium's
36 // FPDF_SetPrintMode() for details.
37 enum PrintingMode {
38   kEmf = 0,
39   kTextOnly = 1,
40   kPostScript2 = 2,
41   kPostScript3 = 3,
42   // Values 4 and 5 are similar to `kPostScript2` and `kPostScript3`, but are
43   // not intended for use in sandboxed environments like Chromium's.
44   kEmfWithReducedRasterization = 6,
45   kPostScript3WithType42Fonts = 7,
46   // Value 8 is similar to `kPostScript3WithType42Fonts`, but is not intended
47   // for use in sandboxed environments like Chromium's.
48 };
49
50 // `pdf_buffer` is the buffer that contains the entire PDF document to be
51 //     rendered.
52 // `page_index` is the 0-based index of the page to be rendered.
53 // `dc` is the device context to render into.
54 // `dpi_x` and `dpi_y` is the resolution.
55 // `bounds_origin_x`, `bounds_origin_y`, `bounds_width` and `bounds_height`
56 //     specify a bounds rectangle within the DC in which to render the PDF
57 //     page.
58 // `fit_to_bounds` specifies whether the output should be shrunk to fit the
59 //     supplied bounds if the page size is larger than the bounds in any
60 //     dimension. If this is false, parts of the PDF page that lie outside
61 //     the bounds will be clipped.
62 // `stretch_to_bounds` specifies whether the output should be stretched to fit
63 //     the supplied bounds if the page size is smaller than the bounds in any
64 //     dimension.
65 // If both `fit_to_bounds` and `stretch_to_bounds` are true, then
66 //     `fit_to_bounds` is honored first.
67 // `keep_aspect_ratio` If any scaling is to be done is true, this flag
68 //     specifies whether the original aspect ratio of the page should be
69 //     preserved while scaling.
70 // `center_in_bounds` specifies whether the final image (after any scaling is
71 //     done) should be centered within the given bounds.
72 // `autorotate` specifies whether the final image should be rotated to match
73 //     the output bound.
74 // `use_color` specifies color or grayscale.
75 // Returns false if the document or the page number are not valid.
76 bool RenderPDFPageToDC(base::span<const uint8_t> pdf_buffer,
77                        int page_index,
78                        HDC dc,
79                        int dpi_x,
80                        int dpi_y,
81                        int bounds_origin_x,
82                        int bounds_origin_y,
83                        int bounds_width,
84                        int bounds_height,
85                        bool fit_to_bounds,
86                        bool stretch_to_bounds,
87                        bool keep_aspect_ratio,
88                        bool center_in_bounds,
89                        bool autorotate,
90                        bool use_color);
91
92 void SetPDFUsePrintMode(int mode);
93 #endif  // BUILDFLAG(IS_WIN)
94
95 // `page_count` and `max_page_width` are optional and can be NULL.
96 // Returns false if the document is not valid.
97 bool GetPDFDocInfo(base::span<const uint8_t> pdf_buffer,
98                    int* page_count,
99                    float* max_page_width);
100
101 // Whether the PDF is Tagged (see 10.7 "Tagged PDF" in PDF Reference 1.7).
102 // Returns true if it's a tagged (accessible) PDF, false if it's a valid
103 // PDF but untagged, and nullopt if the PDF can't be parsed.
104 absl::optional<bool> IsPDFDocTagged(base::span<const uint8_t> pdf_buffer);
105
106 // Given a tagged PDF (see IsPDFDocTagged, above), return the portion of
107 // the structure tree for a given page as a hierarchical tree of base::Values.
108 base::Value GetPDFStructTreeForPage(base::span<const uint8_t> pdf_buffer,
109                                     int page_index);
110
111 // Gets the dimensions of a specific page in a document.
112 // `pdf_buffer` is the buffer that contains the entire PDF document to be
113 //     rendered.
114 // `page_index` is the page number that the function will get the dimensions of.
115 // Returns the size of the page in points, or nullopt if the document or the
116 // page number are not valid.
117 absl::optional<gfx::SizeF> GetPDFPageSizeByIndex(
118     base::span<const uint8_t> pdf_buffer,
119     int page_index);
120
121 enum class RenderDeviceType {
122   kDisplay,
123   kPrinter,
124 };
125
126 struct RenderOptions {
127   // Whether the output should be stretched to fit the supplied bitmap.
128   bool stretch_to_bounds;
129   // If any scaling is needed, whether the original aspect ratio of the page is
130   // preserved while scaling.
131   bool keep_aspect_ratio;
132   // Whether the final image should be rotated to match the output bound.
133   bool autorotate;
134   // Specifies color or grayscale.
135   bool use_color;
136   // What type of device to render for.
137   RenderDeviceType render_device_type;
138 };
139
140 // Renders PDF page into 4-byte per pixel BGRA color bitmap.
141 // `pdf_buffer` is the buffer that contains the entire PDF document to be
142 //     rendered.
143 // `page_index` is the 0-based index of the page to be rendered.
144 // `bitmap_buffer` is the output buffer for bitmap.
145 // `bitmap_size` is the size of the output bitmap.
146 // `dpi` is the 2D resolution.
147 // `options` is the options to render with.
148 // Returns false if the document or the page number are not valid.
149 bool RenderPDFPageToBitmap(base::span<const uint8_t> pdf_buffer,
150                            int page_index,
151                            void* bitmap_buffer,
152                            const gfx::Size& bitmap_size,
153                            const gfx::Size& dpi,
154                            const RenderOptions& options);
155
156 // Convert multiple PDF pages into a N-up PDF.
157 // `input_buffers` is the vector of buffers with each buffer contains a PDF.
158 //     If any of the PDFs contains multiple pages, only the first page of the
159 //     document is used.
160 // `pages_per_sheet` is the number of pages to put on one sheet.
161 // `page_size` is the output page size, measured in PDF "user space" units.
162 // `printable_area` is the output page printable area, measured in PDF
163 //     "user space" units.  Should be smaller than `page_size`.
164 //
165 // `page_size` is the print media size.  The page size of the output N-up PDF is
166 // determined by the `pages_per_sheet`, the orientation of the PDF pages
167 // contained in the `input_buffers`, and the media page size `page_size`. For
168 // example, when `page_size` = 512x792, `pages_per_sheet` = 2, and the
169 // orientation of `input_buffers` = portrait, the output N-up PDF will be
170 // 792x512.
171 // See printing::NupParameters for more details on how the output page
172 // orientation is determined, to understand why `page_size` may be swapped in
173 // some cases.
174 std::vector<uint8_t> ConvertPdfPagesToNupPdf(
175     std::vector<base::span<const uint8_t>> input_buffers,
176     size_t pages_per_sheet,
177     const gfx::Size& page_size,
178     const gfx::Rect& printable_area);
179
180 // Convert a PDF document to a N-up PDF document.
181 // `input_buffer` is the buffer that contains the entire PDF document to be
182 //     converted to a N-up PDF document.
183 // `pages_per_sheet` is the number of pages to put on one sheet.
184 // `page_size` is the output page size, measured in PDF "user space" units.
185 // `printable_area` is the output page printable area, measured in PDF
186 //     "user space" units.  Should be smaller than `page_size`.
187 //
188 // Refer to the description of ConvertPdfPagesToNupPdf to understand how the
189 // output page size will be calculated.
190 // The algorithm used to determine the output page size is the same.
191 std::vector<uint8_t> ConvertPdfDocumentToNupPdf(
192     base::span<const uint8_t> input_buffer,
193     size_t pages_per_sheet,
194     const gfx::Size& page_size,
195     const gfx::Rect& printable_area);
196
197 }  // namespace chrome_pdf
198
199 #endif  // PDF_PDF_H_