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