Roll src/third_party/skia d39c191:cd523eb
[chromium-blink-merge.git] / printing / metafile.h
blobaf0b237250843f43684aec0b9d9bd0503d37038d
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.
5 #ifndef PRINTING_METAFILE_H_
6 #define PRINTING_METAFILE_H_
8 #include <vector>
10 #include "base/basictypes.h"
11 #include "build/build_config.h"
12 #include "printing/printing_export.h"
13 #include "ui/gfx/native_widget_types.h"
15 #if defined(OS_WIN)
16 #include <windows.h>
17 #elif defined(OS_MACOSX)
18 #include <ApplicationServices/ApplicationServices.h>
19 #include <CoreFoundation/CoreFoundation.h>
20 #include "base/mac/scoped_cftyperef.h"
21 #endif
23 namespace base {
24 class File;
27 namespace gfx {
28 class Rect;
29 class Size;
32 class SkBaseDevice;
34 namespace printing {
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) {
62 bool shrink_to_fit;
63 bool stretch_to_fit;
64 bool center_horizontally;
65 bool center_vertically;
66 bool autorotate;
68 #endif // defined(OS_MACOSX)
69 MetafilePlayer();
70 virtual ~MetafilePlayer();
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(gfx::NativeDrawingContext hdc) const = 0;
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 gfx::NativeDrawingContext context,
85 const CGRect rect,
86 const MacRenderPageParams& params) const = 0;
87 #endif // if defined(OS_WIN)
89 // Saves the underlying data to the given file. This function should ONLY be
90 // called after the metafile is closed. Returns true if writing succeeded.
91 virtual bool SaveTo(base::File* file) const = 0;
93 private:
94 DISALLOW_COPY_AND_ASSIGN(MetafilePlayer);
97 // This class creates a graphics context that renders into a data stream
98 // (usually PDF or EMF).
99 class PRINTING_EXPORT Metafile : public MetafilePlayer {
100 public:
101 Metafile();
102 virtual ~Metafile();
104 // Initializes a fresh new metafile for rendering. Returns false on failure.
105 // Note: It should only be called from within the renderer process to allocate
106 // rendering resources.
107 virtual bool Init() = 0;
109 // Initializes the metafile with the data in |src_buffer|. Returns true
110 // on success.
111 // Note: It should only be called from within the browser process.
112 virtual bool InitFromData(const void* src_buffer, uint32 src_buffer_size) = 0;
114 // This method calls StartPage and then returns an appropriate
115 // VectorPlatformDevice implementation bound to the context created by
116 // StartPage or NULL on error.
117 virtual SkBaseDevice* StartPageForVectorCanvas(const gfx::Size& page_size,
118 const gfx::Rect& content_area,
119 const float& scale_factor) = 0;
121 // Prepares a context for rendering a new page with the given |page_size|,
122 // |content_area| and a |scale_factor| to use for the drawing. The units are
123 // in points (=1/72 in). Returns true on success.
124 virtual bool StartPage(const gfx::Size& page_size,
125 const gfx::Rect& content_area,
126 const float& scale_factor) = 0;
128 // Closes the current page and destroys the context used in rendering that
129 // page. The results of current page will be appended into the underlying
130 // data stream. Returns true on success.
131 virtual bool FinishPage() = 0;
133 // Closes the metafile. No further rendering is allowed (the current page
134 // is implicitly closed).
135 virtual bool FinishDocument() = 0;
137 // Returns the size of the underlying data stream. Only valid after Close()
138 // has been called.
139 virtual uint32 GetDataSize() const = 0;
141 // Copies the first |dst_buffer_size| bytes of the underlying data stream into
142 // |dst_buffer|. This function should ONLY be called after Close() is invoked.
143 // Returns true if the copy succeeds.
144 virtual bool GetData(void* dst_buffer, uint32 dst_buffer_size) const = 0;
146 virtual gfx::Rect GetPageBounds(unsigned int page_number) const = 0;
147 virtual unsigned int GetPageCount() const = 0;
149 virtual gfx::NativeDrawingContext context() const = 0;
151 #if defined(OS_WIN)
152 // "Plays" the EMF buffer in a HDC. It is the same effect as calling the
153 // original GDI function that were called when recording the EMF. |rect| is in
154 // "logical units" and is optional. If |rect| is NULL, the natural EMF bounds
155 // are used.
156 // Note: Windows has been known to have stack buffer overflow in its GDI
157 // functions, whether used directly or indirectly through precompiled EMF
158 // data. We have to accept the risk here. Since it is used only for printing,
159 // it requires user intervention.
160 virtual bool Playback(gfx::NativeDrawingContext hdc,
161 const RECT* rect) const = 0;
162 #endif // OS_WIN
164 bool GetDataAsVector(std::vector<char>* buffer) const;
166 virtual bool SaveTo(base::File* file) const OVERRIDE;
168 private:
169 DISALLOW_COPY_AND_ASSIGN(Metafile);
172 } // namespace printing
174 #endif // PRINTING_METAFILE_H_