OLD | NEW |
| (Empty) |
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. | |
4 | |
5 #ifndef PRINTING_NATIVE_METAFILE_H_ | |
6 #define PRINTING_NATIVE_METAFILE_H_ | |
7 | |
8 #include "base/basictypes.h" | |
9 #include "build/build_config.h" | |
10 #include "ui/gfx/native_widget_types.h" | |
11 | |
12 #if defined(OS_WIN) | |
13 #include <windows.h> | |
14 #include <vector> | |
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 class FilePath; | |
22 | |
23 namespace gfx { | |
24 class Point; | |
25 class Rect; | |
26 class Size; | |
27 } | |
28 | |
29 namespace skia { | |
30 class PlatformDevice; | |
31 } | |
32 | |
33 #if defined(OS_CHROMEOS) | |
34 namespace base { | |
35 class FileDescriptor; | |
36 } | |
37 #endif | |
38 | |
39 namespace printing { | |
40 | |
41 // This class creates a graphics context that renders into a data stream | |
42 // (usually PDF or EMF). | |
43 class NativeMetafile { | |
44 public: | |
45 virtual ~NativeMetafile() {} | |
46 | |
47 // Initializes a fresh new metafile for rendering. Returns false on failure. | |
48 // Note: It should only be called from within the renderer process to allocate | |
49 // rendering resources. | |
50 virtual bool Init() = 0; | |
51 | |
52 // Initializes the metafile with the data in |src_buffer|. Returns true | |
53 // on success. | |
54 // Note: It should only be called from within the browser process. | |
55 virtual bool InitFromData(const void* src_buffer, uint32 src_buffer_size) = 0; | |
56 | |
57 // This method calls StartPage and then returns an appropriate | |
58 // VectorPlatformDevice implementation bound to the context created by | |
59 // StartPage or NULL on error. | |
60 virtual skia::PlatformDevice* StartPageForVectorCanvas( | |
61 const gfx::Size& page_size, const gfx::Point& content_origin, | |
62 const float& scale_factor) = 0; | |
63 | |
64 // Prepares a context for rendering a new page at the specified | |
65 // |content_origin| with the given |page_size| and a |scale_factor| to use for | |
66 // the drawing. The units are in points (=1/72 in). Returns true on success. | |
67 virtual bool StartPage(const gfx::Size& page_size, | |
68 const gfx::Point& content_origin, | |
69 const float& scale_factor) = 0; | |
70 | |
71 // Closes the current page and destroys the context used in rendering that | |
72 // page. The results of current page will be appended into the underlying | |
73 // data stream. Returns true on success. | |
74 virtual bool FinishPage() = 0; | |
75 | |
76 // Closes the metafile. No further rendering is allowed (the current page | |
77 // is implicitly closed). | |
78 virtual bool FinishDocument() = 0; | |
79 | |
80 // Returns the size of the underlying data stream. Only valid after Close() | |
81 // has been called. | |
82 virtual uint32 GetDataSize() const = 0; | |
83 | |
84 // Copies the first |dst_buffer_size| bytes of the underlying data stream into | |
85 // |dst_buffer|. This function should ONLY be called after Close() is invoked. | |
86 // Returns true if the copy succeeds. | |
87 virtual bool GetData(void* dst_buffer, uint32 dst_buffer_size) const = 0; | |
88 | |
89 // Saves the underlying data to the given file. This function should ONLY be | |
90 // called after the metafile is closed. Returns true if writing succeeded. | |
91 virtual bool SaveTo(const FilePath& file_path) const = 0; | |
92 | |
93 // Returns the bounds of the given page. Pages use a 1-based index. | |
94 virtual gfx::Rect GetPageBounds(unsigned int page_number) const = 0; | |
95 virtual unsigned int GetPageCount() const = 0; | |
96 | |
97 // Get the context for rendering to the PDF. | |
98 virtual gfx::NativeDrawingContext context() const = 0; | |
99 | |
100 #if defined(OS_WIN) | |
101 // "Plays" the EMF buffer in a HDC. It is the same effect as calling the | |
102 // original GDI function that were called when recording the EMF. |rect| is in | |
103 // "logical units" and is optional. If |rect| is NULL, the natural EMF bounds | |
104 // are used. | |
105 // Note: Windows has been known to have stack buffer overflow in its GDI | |
106 // functions, whether used directly or indirectly through precompiled EMF | |
107 // data. We have to accept the risk here. Since it is used only for printing, | |
108 // it requires user intervention. | |
109 virtual bool Playback(gfx::NativeDrawingContext hdc, | |
110 const RECT* rect) const = 0; | |
111 | |
112 // The slow version of Playback(). It enumerates all the records and play them | |
113 // back in the HDC. The trick is that it skip over the records known to have | |
114 // issue with some printers. See Emf::Record::SafePlayback implementation for | |
115 // details. | |
116 virtual bool SafePlayback(gfx::NativeDrawingContext hdc) const = 0; | |
117 | |
118 virtual HENHMETAFILE emf() const = 0; | |
119 #elif defined(OS_MACOSX) | |
120 // Renders the given page into |rect| in the given context. | |
121 // Pages use a 1-based index. The rendering uses the following arguments | |
122 // to determine scaling and translation factors. | |
123 // |shrink_to_fit| specifies whether the output should be shrunk to fit the | |
124 // supplied |rect| if the page size is larger than |rect| in any dimension. | |
125 // If this is false, parts of the PDF page that lie outside the bounds will be | |
126 // clipped. | |
127 // |stretch_to_fit| specifies whether the output should be stretched to fit | |
128 // the supplied bounds if the page size is smaller than |rect| in all | |
129 // dimensions. | |
130 // |center_horizontally| specifies whether the final image (after any scaling | |
131 // is done) should be centered horizontally within the given |rect|. | |
132 // |center_vertically| specifies whether the final image (after any scaling | |
133 // is done) should be centered vertically within the given |rect|. | |
134 // Note that all scaling preserves the original aspect ratio of the page. | |
135 virtual bool RenderPage(unsigned int page_number, | |
136 gfx::NativeDrawingContext context, | |
137 const CGRect rect, | |
138 bool shrink_to_fit, | |
139 bool stretch_to_fit, | |
140 bool center_horizontally, | |
141 bool center_vertically) const = 0; | |
142 #elif defined(OS_CHROMEOS) | |
143 // Saves the underlying data to the file associated with fd. This function | |
144 // should ONLY be called after the metafile is closed. | |
145 // Returns true if writing succeeded. | |
146 virtual bool SaveToFD(const base::FileDescriptor& fd) const = 0; | |
147 #endif // if defined(OS_CHROMEOS) | |
148 }; | |
149 | |
150 } // namespace printing | |
151 | |
152 #endif // PRINTING_NATIVE_METAFILE_H_ | |
OLD | NEW |