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 "base/basictypes.h"
9 #include "build/build_config.h"
10 #include "printing/printing_export.h"
11 #include "ui/gfx/native_widget_types.h"
15 #elif defined(OS_MACOSX)
16 #include <ApplicationServices/ApplicationServices.h>
17 #include <CoreFoundation/CoreFoundation.h>
18 #include "base/mac/scoped_cftyperef.h"
32 #if defined(OS_CHROMEOS)
34 struct FileDescriptor
;
40 // This class creates a graphics context that renders into a data stream
41 // (usually PDF or EMF).
42 class PRINTING_EXPORT Metafile
{
44 #if defined(OS_MACOSX)
45 // |shrink_to_fit| specifies whether the output should be shrunk to fit a
46 // destination page if the source PDF is bigger than the destination page in
47 // any dimension. If this is false, parts of the source PDF page that lie
48 // outside the bounds will be clipped.
49 // |stretch_to_fit| specifies whether the output should be stretched to fit
50 // the destination page if the source page size is smaller in all dimensions.
51 // |center_horizontally| specifies whether the output (after any scaling is
52 // done) should be centered horizontally within the destination page.
53 // |center_vertically| specifies whether the output (after any scaling is
54 // done) should be centered vertically within the destination page.
55 // Note that all scaling preserves the original aspect ratio of the page.
56 // |autorotate| specifies whether the source PDF should be autorotated to fit
57 // on the destination page.
58 struct MacRenderPageParams
{
60 : shrink_to_fit(false),
61 stretch_to_fit(false),
62 center_horizontally(false),
63 center_vertically(false),
69 bool center_horizontally
;
70 bool center_vertically
;
73 #endif // defined(OS_MACOSX)
75 virtual ~Metafile() {}
77 // Initializes a fresh new metafile for rendering. Returns false on failure.
78 // Note: It should only be called from within the renderer process to allocate
79 // rendering resources.
80 virtual bool Init() = 0;
82 // Initializes the metafile with the data in |src_buffer|. Returns true
84 // Note: It should only be called from within the browser process.
85 virtual bool InitFromData(const void* src_buffer
, uint32 src_buffer_size
) = 0;
87 // This method calls StartPage and then returns an appropriate
88 // VectorPlatformDevice implementation bound to the context created by
89 // StartPage or NULL on error.
90 virtual SkDevice
* StartPageForVectorCanvas(
91 const gfx::Size
& page_size
,
92 const gfx::Rect
& content_area
,
93 const float& scale_factor
) = 0;
95 // Prepares a context for rendering a new page with the given |page_size|,
96 // |content_area| and a |scale_factor| to use for the drawing. The units are
97 // in points (=1/72 in). Returns true on success.
98 virtual bool StartPage(const gfx::Size
& page_size
,
99 const gfx::Rect
& content_area
,
100 const float& scale_factor
) = 0;
102 // Closes the current page and destroys the context used in rendering that
103 // page. The results of current page will be appended into the underlying
104 // data stream. Returns true on success.
105 virtual bool FinishPage() = 0;
107 // Closes the metafile. No further rendering is allowed (the current page
108 // is implicitly closed).
109 virtual bool FinishDocument() = 0;
111 // Returns the size of the underlying data stream. Only valid after Close()
113 virtual uint32
GetDataSize() const = 0;
115 // Copies the first |dst_buffer_size| bytes of the underlying data stream into
116 // |dst_buffer|. This function should ONLY be called after Close() is invoked.
117 // Returns true if the copy succeeds.
118 virtual bool GetData(void* dst_buffer
, uint32 dst_buffer_size
) const = 0;
120 // Saves the underlying data to the given file. This function should ONLY be
121 // called after the metafile is closed. Returns true if writing succeeded.
122 virtual bool SaveTo(const base::FilePath
& file_path
) const = 0;
124 // Returns the bounds of the given page. Pages use a 1-based index.
125 virtual gfx::Rect
GetPageBounds(unsigned int page_number
) const = 0;
126 virtual unsigned int GetPageCount() const = 0;
128 // Get the context for rendering to the PDF.
129 virtual gfx::NativeDrawingContext
context() const = 0;
132 // "Plays" the EMF buffer in a HDC. It is the same effect as calling the
133 // original GDI function that were called when recording the EMF. |rect| is in
134 // "logical units" and is optional. If |rect| is NULL, the natural EMF bounds
136 // Note: Windows has been known to have stack buffer overflow in its GDI
137 // functions, whether used directly or indirectly through precompiled EMF
138 // data. We have to accept the risk here. Since it is used only for printing,
139 // it requires user intervention.
140 virtual bool Playback(gfx::NativeDrawingContext hdc
,
141 const RECT
* rect
) const = 0;
143 // The slow version of Playback(). It enumerates all the records and play them
144 // back in the HDC. The trick is that it skip over the records known to have
145 // issue with some printers. See Emf::Record::SafePlayback implementation for
147 virtual bool SafePlayback(gfx::NativeDrawingContext hdc
) const = 0;
149 virtual HENHMETAFILE
emf() const = 0;
150 #elif defined(OS_MACOSX)
151 // Renders the given page into |rect| in the given context.
152 // Pages use a 1-based index. The rendering uses the arguments in
153 // |params| to determine scaling, translation, and rotation.
154 virtual bool RenderPage(unsigned int page_number
,
155 gfx::NativeDrawingContext context
,
157 const MacRenderPageParams
& params
) const = 0;
158 #elif defined(OS_CHROMEOS)
159 // Saves the underlying data to the file associated with fd. This function
160 // should ONLY be called after the metafile is closed.
161 // Returns true if writing succeeded.
162 virtual bool SaveToFD(const base::FileDescriptor
& fd
) const = 0;
163 #endif // if defined(OS_CHROMEOS)
166 } // namespace printing
168 #endif // PRINTING_METAFILE_H_