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