1 // Copyright (c) 2011 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"
30 #if defined(OS_CHROMEOS)
32 struct FileDescriptor
;
38 // This class creates a graphics context that renders into a data stream
39 // (usually PDF or EMF).
40 class PRINTING_EXPORT Metafile
{
42 virtual ~Metafile() {}
44 // Initializes a fresh new metafile for rendering. Returns false on failure.
45 // Note: It should only be called from within the renderer process to allocate
46 // rendering resources.
47 virtual bool Init() = 0;
49 // Initializes the metafile with the data in |src_buffer|. Returns true
51 // Note: It should only be called from within the browser process.
52 virtual bool InitFromData(const void* src_buffer
, uint32 src_buffer_size
) = 0;
54 // This method calls StartPage and then returns an appropriate
55 // VectorPlatformDevice implementation bound to the context created by
56 // StartPage or NULL on error.
57 virtual SkDevice
* StartPageForVectorCanvas(
58 const gfx::Size
& page_size
,
59 const gfx::Rect
& content_area
,
60 const float& scale_factor
) = 0;
62 // Prepares a context for rendering a new page with the given |page_size|,
63 // |content_area| and a |scale_factor| to use for the drawing. The units are
64 // in points (=1/72 in). Returns true on success.
65 virtual bool StartPage(const gfx::Size
& page_size
,
66 const gfx::Rect
& content_area
,
67 const float& scale_factor
) = 0;
69 // Closes the current page and destroys the context used in rendering that
70 // page. The results of current page will be appended into the underlying
71 // data stream. Returns true on success.
72 virtual bool FinishPage() = 0;
74 // Closes the metafile. No further rendering is allowed (the current page
75 // is implicitly closed).
76 virtual bool FinishDocument() = 0;
78 // Returns the size of the underlying data stream. Only valid after Close()
80 virtual uint32
GetDataSize() const = 0;
82 // Copies the first |dst_buffer_size| bytes of the underlying data stream into
83 // |dst_buffer|. This function should ONLY be called after Close() is invoked.
84 // Returns true if the copy succeeds.
85 virtual bool GetData(void* dst_buffer
, uint32 dst_buffer_size
) const = 0;
87 // Saves the underlying data to the given file. This function should ONLY be
88 // called after the metafile is closed. Returns true if writing succeeded.
89 virtual bool SaveTo(const FilePath
& file_path
) const = 0;
91 // Returns the bounds of the given page. Pages use a 1-based index.
92 virtual gfx::Rect
GetPageBounds(unsigned int page_number
) const = 0;
93 virtual unsigned int GetPageCount() const = 0;
95 // Get the context for rendering to the PDF.
96 virtual gfx::NativeDrawingContext
context() const = 0;
99 // "Plays" the EMF buffer in a HDC. It is the same effect as calling the
100 // original GDI function that were called when recording the EMF. |rect| is in
101 // "logical units" and is optional. If |rect| is NULL, the natural EMF bounds
103 // Note: Windows has been known to have stack buffer overflow in its GDI
104 // functions, whether used directly or indirectly through precompiled EMF
105 // data. We have to accept the risk here. Since it is used only for printing,
106 // it requires user intervention.
107 virtual bool Playback(gfx::NativeDrawingContext hdc
,
108 const RECT
* rect
) const = 0;
110 // The slow version of Playback(). It enumerates all the records and play them
111 // back in the HDC. The trick is that it skip over the records known to have
112 // issue with some printers. See Emf::Record::SafePlayback implementation for
114 virtual bool SafePlayback(gfx::NativeDrawingContext hdc
) const = 0;
116 virtual HENHMETAFILE
emf() const = 0;
117 #elif defined(OS_MACOSX)
118 // Renders the given page into |rect| in the given context.
119 // Pages use a 1-based index. The rendering uses the following arguments
120 // to determine scaling and translation factors.
121 // |shrink_to_fit| specifies whether the output should be shrunk to fit the
122 // supplied |rect| if the page size is larger than |rect| in any dimension.
123 // If this is false, parts of the PDF page that lie outside the bounds will be
125 // |stretch_to_fit| specifies whether the output should be stretched to fit
126 // the supplied bounds if the page size is smaller than |rect| in all
128 // |center_horizontally| specifies whether the final image (after any scaling
129 // is done) should be centered horizontally within the given |rect|.
130 // |center_vertically| specifies whether the final image (after any scaling
131 // is done) should be centered vertically within the given |rect|.
132 // Note that all scaling preserves the original aspect ratio of the page.
133 virtual bool RenderPage(unsigned int page_number
,
134 gfx::NativeDrawingContext context
,
138 bool center_horizontally
,
139 bool center_vertically
) const = 0;
140 #elif defined(OS_CHROMEOS)
141 // Saves the underlying data to the file associated with fd. This function
142 // should ONLY be called after the metafile is closed.
143 // Returns true if writing succeeded.
144 virtual bool SaveToFD(const base::FileDescriptor
& fd
) const = 0;
145 #endif // if defined(OS_CHROMEOS)
148 } // namespace printing
150 #endif // PRINTING_METAFILE_H_