• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "base/basictypes.h"
9 #include "build/build_config.h"
10 #include "printing/printing_export.h"
11 #include "ui/gfx/native_widget_types.h"
12 
13 #if defined(OS_WIN)
14 #include <windows.h>
15 #elif defined(OS_MACOSX)
16 #include <ApplicationServices/ApplicationServices.h>
17 #include <CoreFoundation/CoreFoundation.h>
18 #include "base/mac/scoped_cftyperef.h"
19 #endif
20 
21 namespace base {
22 class FilePath;
23 }
24 
25 namespace gfx {
26 class Rect;
27 class Size;
28 }
29 
30 class SkBaseDevice;
31 
32 #if defined(OS_CHROMEOS) || defined(OS_ANDROID)
33 namespace base {
34 struct FileDescriptor;
35 }
36 #endif
37 
38 namespace printing {
39 
40 // This class creates a graphics context that renders into a data stream
41 // (usually PDF or EMF).
42 class PRINTING_EXPORT Metafile {
43  public:
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 {
MacRenderPageParamsMacRenderPageParams59     MacRenderPageParams()
60         : shrink_to_fit(false),
61           stretch_to_fit(false),
62           center_horizontally(false),
63           center_vertically(false),
64           autorotate(false) {
65     }
66 
67     bool shrink_to_fit;
68     bool stretch_to_fit;
69     bool center_horizontally;
70     bool center_vertically;
71     bool autorotate;
72   };
73 #endif  // defined(OS_MACOSX)
74 
~Metafile()75   virtual ~Metafile() {}
76 
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;
81 
82   // Initializes the metafile with the data in |src_buffer|. Returns true
83   // on success.
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;
86 
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 SkBaseDevice* StartPageForVectorCanvas(
91       const gfx::Size& page_size,
92       const gfx::Rect& content_area,
93       const float& scale_factor) = 0;
94 
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;
101 
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;
106 
107   // Closes the metafile. No further rendering is allowed (the current page
108   // is implicitly closed).
109   virtual bool FinishDocument() = 0;
110 
111   // Returns the size of the underlying data stream. Only valid after Close()
112   // has been called.
113   virtual uint32 GetDataSize() const = 0;
114 
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;
119 
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;
123 
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;
127 
128   // Get the context for rendering to the PDF.
129   virtual gfx::NativeDrawingContext context() const = 0;
130 
131 #if defined(OS_WIN)
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
135   // are used.
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;
142 
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
146   // details.
147   virtual bool SafePlayback(gfx::NativeDrawingContext hdc) const = 0;
148 
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,
156                           const CGRect rect,
157                           const MacRenderPageParams& params) const = 0;
158 #elif defined(OS_CHROMEOS) || defined(OS_ANDROID)
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) || defined(OS_ANDROID)
164 };
165 
166 }  // namespace printing
167 
168 #endif  // PRINTING_METAFILE_H_
169