Upload upstream chromium 73.0.3683.0
[platform/framework/web/chromium-efl.git] / printing / metafile.h
1 // Copyright (c) 2012 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 #ifndef PRINTING_METAFILE_H_
6 #define PRINTING_METAFILE_H_
7
8 #include <stdint.h>
9
10 #include <vector>
11
12 #include "base/macros.h"
13 #include "build/build_config.h"
14 #include "printing/native_drawing_context.h"
15 #include "printing/printing_export.h"
16
17 #if defined(OS_WIN)
18 #include <windows.h>
19 #elif defined(OS_MACOSX)
20 #include <ApplicationServices/ApplicationServices.h>
21 #include <CoreFoundation/CoreFoundation.h>
22 #include "base/mac/scoped_cftyperef.h"
23 #endif
24
25 namespace base {
26 class File;
27 }
28
29 namespace gfx {
30 class Rect;
31 class Size;
32 }
33
34 namespace printing {
35
36 // This class plays metafiles from data stream (usually PDF or EMF).
37 class PRINTING_EXPORT MetafilePlayer {
38  public:
39 #if defined(OS_MACOSX)
40   // |shrink_to_fit| specifies whether the output should be shrunk to fit a
41   // destination page if the source PDF is bigger than the destination page in
42   // any dimension. If this is false, parts of the source PDF page that lie
43   // outside the bounds will be clipped.
44   // |stretch_to_fit| specifies whether the output should be stretched to fit
45   // the destination page if the source page size is smaller in all dimensions.
46   // |center_horizontally| specifies whether the output (after any scaling is
47   // done) should be centered horizontally within the destination page.
48   // |center_vertically| specifies whether the output (after any scaling is
49   // done) should be centered vertically within the destination page.
50   // Note that all scaling preserves the original aspect ratio of the page.
51   // |autorotate| specifies whether the source PDF should be autorotated to fit
52   // on the destination page.
53   struct MacRenderPageParams {
54     MacRenderPageParams()
55         : shrink_to_fit(false),
56           stretch_to_fit(false),
57           center_horizontally(false),
58           center_vertically(false),
59           autorotate(false) {
60     }
61
62     bool shrink_to_fit;
63     bool stretch_to_fit;
64     bool center_horizontally;
65     bool center_vertically;
66     bool autorotate;
67   };
68 #endif  // defined(OS_MACOSX)
69   MetafilePlayer();
70   virtual ~MetafilePlayer();
71
72 #if defined(OS_WIN)
73   // The slow version of Playback(). It enumerates all the records and play them
74   // back in the HDC. The trick is that it skip over the records known to have
75   // issue with some printers. See Emf::Record::SafePlayback implementation for
76   // details.
77   virtual bool SafePlayback(printing::NativeDrawingContext hdc) const = 0;
78
79 #elif defined(OS_MACOSX)
80   // Renders the given page into |rect| in the given context.
81   // Pages use a 1-based index. The rendering uses the arguments in
82   // |params| to determine scaling, translation, and rotation.
83   virtual bool RenderPage(unsigned int page_number,
84                           printing::NativeDrawingContext context,
85                           const CGRect rect,
86                           const MacRenderPageParams& params) const = 0;
87 #endif  // if defined(OS_WIN)
88
89   // Populates the buffer with the underlying data. This function should ONLY be
90   // called after the metafile is closed. Returns true if writing succeeded.
91   virtual bool GetDataAsVector(std::vector<char>* buffer) const = 0;
92
93   // Saves the underlying data to the given file. This function should ONLY be
94   // called after the metafile is closed. Returns true if writing succeeded.
95   virtual bool SaveTo(base::File* file) const = 0;
96
97  private:
98   DISALLOW_COPY_AND_ASSIGN(MetafilePlayer);
99 };
100
101 // This class creates a graphics context that renders into a data stream
102 // (usually PDF or EMF).
103 class PRINTING_EXPORT Metafile : public MetafilePlayer {
104  public:
105   Metafile();
106   ~Metafile() override;
107
108   // Initializes a fresh new metafile for rendering. Returns false on failure.
109   // Note: It should only be called from within the renderer process to allocate
110   // rendering resources.
111   virtual bool Init() = 0;
112
113   // Initializes the metafile with the data in |src_buffer|. Returns true
114   // on success.
115   // Note: It should only be called from within the browser process.
116   virtual bool InitFromData(const void* src_buffer,
117                             size_t src_buffer_size) = 0;
118
119   // Prepares a context for rendering a new page with the given |page_size|,
120   // |content_area| and a |scale_factor| to use for the drawing. The units are
121   // in points (=1/72 in).
122   virtual void StartPage(const gfx::Size& page_size,
123                          const gfx::Rect& content_area,
124                          const float& scale_factor) = 0;
125
126   // Closes the current page and destroys the context used in rendering that
127   // page. The results of current page will be appended into the underlying
128   // data stream. Returns true on success.
129   virtual bool FinishPage() = 0;
130
131   // Closes the metafile. No further rendering is allowed (the current page
132   // is implicitly closed).
133   virtual bool FinishDocument() = 0;
134
135   // Returns the size of the underlying data stream. Only valid after Close()
136   // has been called.
137   virtual uint32_t GetDataSize() const = 0;
138
139   // Copies the first |dst_buffer_size| bytes of the underlying data stream into
140   // |dst_buffer|. This function should ONLY be called after Close() is invoked.
141   // Returns true if the copy succeeds.
142   virtual bool GetData(void* dst_buffer, uint32_t dst_buffer_size) const = 0;
143
144   virtual gfx::Rect GetPageBounds(unsigned int page_number) const = 0;
145   virtual unsigned int GetPageCount() const = 0;
146
147   virtual printing::NativeDrawingContext context() const = 0;
148
149 #if defined(OS_WIN)
150   // "Plays" the EMF buffer in a HDC. It is the same effect as calling the
151   // original GDI function that were called when recording the EMF. |rect| is in
152   // "logical units" and is optional. If |rect| is NULL, the natural EMF bounds
153   // are used.
154   // Note: Windows has been known to have stack buffer overflow in its GDI
155   // functions, whether used directly or indirectly through precompiled EMF
156   // data. We have to accept the risk here. Since it is used only for printing,
157   // it requires user intervention.
158   virtual bool Playback(printing::NativeDrawingContext hdc,
159                         const RECT* rect) const = 0;
160 #endif  // OS_WIN
161
162   // MetfilePlayer
163   bool GetDataAsVector(std::vector<char>* buffer) const override;
164   bool SaveTo(base::File* file) const override;
165
166  private:
167   DISALLOW_COPY_AND_ASSIGN(Metafile);
168 };
169
170 }  // namespace printing
171
172 #endif  // PRINTING_METAFILE_H_