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_
12 #include "base/macros.h"
13 #include "build/build_config.h"
14 #include "printing/native_drawing_context.h"
15 #include "printing/printing_export.h"
19 #elif defined(OS_MACOSX)
20 #include <ApplicationServices/ApplicationServices.h>
21 #include <CoreFoundation/CoreFoundation.h>
22 #include "base/mac/scoped_cftyperef.h"
36 // This class plays metafiles from data stream (usually PDF or EMF).
37 class PRINTING_EXPORT MetafilePlayer {
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 {
55 : shrink_to_fit(false),
56 stretch_to_fit(false),
57 center_horizontally(false),
58 center_vertically(false),
64 bool center_horizontally;
65 bool center_vertically;
68 #endif // defined(OS_MACOSX)
70 virtual ~MetafilePlayer();
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
77 virtual bool SafePlayback(printing::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 printing::NativeDrawingContext context,
86 const MacRenderPageParams& params) const = 0;
87 #endif // if defined(OS_WIN)
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;
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;
98 DISALLOW_COPY_AND_ASSIGN(MetafilePlayer);
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 {
106 ~Metafile() override;
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;
113 // Initializes the metafile with the data in |src_buffer|. Returns true
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;
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;
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;
131 // Closes the metafile. No further rendering is allowed (the current page
132 // is implicitly closed).
133 virtual bool FinishDocument() = 0;
135 // Returns the size of the underlying data stream. Only valid after Close()
137 virtual uint32_t GetDataSize() const = 0;
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;
144 virtual gfx::Rect GetPageBounds(unsigned int page_number) const = 0;
145 virtual unsigned int GetPageCount() const = 0;
147 virtual printing::NativeDrawingContext context() const = 0;
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
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;
163 bool GetDataAsVector(std::vector<char>* buffer) const override;
164 bool SaveTo(base::File* file) const override;
167 DISALLOW_COPY_AND_ASSIGN(Metafile);
170 } // namespace printing
172 #endif // PRINTING_METAFILE_H_