Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(134)

Side by Side Diff: printing/native_metafile.h

Issue 6611032: Unifying NativeMetafile class interface (as much as possible) for Linux, Mac, Win (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Addressing reviewer's comments Created 9 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #ifndef PRINTING_NATIVE_METAFILE_H_ 5 #ifndef PRINTING_NATIVE_METAFILE_H_
6 #define PRINTING_NATIVE_METAFILE_H_ 6 #define PRINTING_NATIVE_METAFILE_H_
7 7
8 #include "base/basictypes.h" 8 #include "base/basictypes.h"
9 #include "build/build_config.h" 9 #include "build/build_config.h"
10 #include "ui/gfx/native_widget_types.h"
vandebo (ex-Chrome) 2011/03/14 20:24:25 For this code, let's use PlatformSurface from plat
dpapad 2011/03/14 22:15:12 There is some problem in the includes of SkTypes.h
10 11
11 #if defined(OS_WIN) 12 #if defined(OS_WIN)
12 #include "printing/native_metafile_win.h" 13 #include <windows.h>
14 #include <vector>
13 #elif defined(OS_MACOSX) 15 #elif defined(OS_MACOSX)
14 #include "printing/native_metafile_mac.h" 16 #include <ApplicationServices/ApplicationServices.h>
15 #elif defined(OS_POSIX) 17 #include <CoreFoundation/CoreFoundation.h>
16 #include "printing/native_metafile_linux.h" 18 #include "base/mac/scoped_cftyperef.h"
17 #endif 19 #endif
18 20
21 namespace gfx {
22 class Rect;
23 class Size;
24 class Point;
25 }
26
27 #if defined(OS_CHROMEOS)
28 namespace base {
29 class FileDescriptor;
30 }
31 #endif
32
33 class FilePath;
34
35 namespace printing {
36
37 // This class creates a graphics context that renders into a data stream.
vandebo (ex-Chrome) 2011/03/14 20:24:25 nit: ...into a data stream (usually PDF or EMF).
dpapad 2011/03/14 22:15:12 Done.
38 class NativeMetafile {
39 public:
40 virtual ~NativeMetafile() {}
41
42 // Initializes a fresh new metafile for rendering. Returns false on failure.
43 // Note: It should only be called from within the renderer process to allocate
44 // rendering resources.
45 virtual bool Init() = 0;
46
47 // Initializes the metafile with the data in |src_buffer|. Returns true
48 // on success.
49 // Note: It should only be called from within the browser process.
50 virtual bool Init(const void* src_buffer, uint32 src_buffer_size) = 0;
51
52 #if defined(OS_WIN)
53 // Inserts a custom GDICOMMENT records indicating StartPage/EndPage calls
54 // (since StartPage and EndPage do not work in a metafile DC). Only valid
55 // when hdc_ is non-NULL.
56 virtual bool StartPage() = 0;
57 #elif defined(OS_MACOSX)
58 // Prepares a new pdf page at specified |content_origin| with the given
59 // |page_size| and a |scale_factor| to use for the drawing.
60 virtual gfx::NativeDrawingContext StartPage(const gfx::Size& page_size,
61 const gfx::Point& content_origin,
62 const float& scale_factor) = 0;
63 #elif defined(OS_POSIX)
64 // Prepares a new cairo surface/context for rendering a new page.
65 // The unit is in point (=1/72 in).
66 // Returns NULL when failed.
67 virtual gfx::NativeDrawingContext StartPage(const gfx::Size& page_sixe,
68 double margin_top_in_points,
69 double margin_right_in_points,
70 double margin_bottom_in_points,
dpapad 2011/03/14 22:15:12 Removed this parameters. The caller already includ
71 double margin_left_in_points) = 0;
72 #endif
73
74
75 // Closes the current page and destroys the context used in rendering that
76 // page. The results of current page will be appended into the underlying
77 // data stream. Returns true on success.
78 virtual bool FinishPage() = 0;
79
80 // Closes the metafile. No further rendering is allowed (the current page
81 // is implicitly closed).
82 virtual bool Close() = 0;
83
84 // Returns the size of the underlying data stream. Only valid after Close()
85 // has been called.
86 virtual uint32 GetDataSize() const = 0;
87
88 // Copies the first |dst_buffer_size| bytes of the underlying data stream into
89 // |dst_buffer|. This function should ONLY be called after Close() is invoked.
90 // Returns true if the copy succeeds.
91 virtual bool GetData(void* dst_buffer, uint32 dst_buffer_size) const = 0;
92
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(const FilePath& file_path) const = 0;
96
97 // Returns the bounds of the given page. Pages use a 1-based index.
98 virtual gfx::Rect GetPageBounds(unsigned int page_number) const = 0;
99 virtual unsigned int GetPageCount() const = 0;
100
101 // Get the context for rendering to the PDF.
102 virtual gfx::NativeDrawingContext context() const = 0;
103
104 #if defined(OS_WIN)
105 // Generates a virtual HDC that will record every GDI commands and compile it
106 // in a EMF data stream.
107 // hdc is used to setup the default DPI and color settings. hdc is optional.
108 // rect specifies the dimensions (in .01-millimeter units) of the EMF. rect is
109 // optional.
110 virtual bool CreateDc(gfx::NativeDrawingContext sibling,
111 const RECT* rect) = 0;
112
113 // Similar to the above method but the metafile is backed by a file.
114 virtual bool CreateFileBackedDc(gfx::NativeDrawingContext sibling,
115 const RECT* rect,
116 const FilePath& path) = 0;
117
118 // TODO(maruel): CreateFromFile(). If ever used. Maybe users would like to
119 // have the ability to save web pages to an EMF file? Afterward, it is easy to
120 // convert to PDF or PS.
121 // Load a metafile fromdisk.
122 virtual bool CreateFromFile(const FilePath& metafile_path) = 0;
123
124 // Closes the HDC created by CreateDc() and generates the compiled EMF
125 // data.
126 virtual void CloseEmf() = 0;
127
128 // "Plays" the EMF buffer in a HDC. It is the same effect as calling the
129 // original GDI function that were called when recording the EMF. |rect| is in
130 // "logical units" and is optional. If |rect| is NULL, the natural EMF bounds
131 // are used.
132 // Note: Windows has been known to have stack buffer overflow in its GDI
133 // functions, whether used directly or indirectly through precompiled EMF
134 // data. We have to accept the risk here. Since it is used only for printing,
135 // it requires user intervention.
136 virtual bool Playback(gfx::NativeDrawingContext hdc,
137 const RECT* rect) const = 0;
138
139 // The slow version of Playback(). It enumerates all the records and play them
140 // back in the HDC. The trick is that it skip over the records known to have
141 // issue with some printers. See Emf::Record::SafePlayback implementation for
142 // details.
143 virtual bool SafePlayback(gfx::NativeDrawingContext hdc) const = 0;
144
145 // Retrieves the underlying data stream. It is a helper function.
146 virtual bool GetData(std::vector<uint8>* buffer) const = 0;
147
148 virtual HENHMETAFILE emf() const = 0;
149 #elif defined(OS_MACOSX)
150 // Renders the given page into |rect| in the given context.
151 // Pages use a 1-based index. The rendering uses the following arguments
152 // to determine scaling and translation factors.
153 // |shrink_to_fit| specifies whether the output should be shrunk to fit the
154 // supplied |rect| if the page size is larger than |rect| in any dimension.
155 // If this is false, parts of the PDF page that lie outside the bounds will be
156 // clipped.
157 // |stretch_to_fit| specifies whether the output should be stretched to fit
158 // the supplied bounds if the page size is smaller than |rect| in all
159 // dimensions.
160 // |center_horizontally| specifies whether the final image (after any scaling
161 // is done) should be centered horizontally within the given |rect|.
162 // |center_vertically| specifies whether the final image (after any scaling
163 // is done) should be centered vertically within the given |rect|.
164 // Note that all scaling preserves the original aspect ratio of the page.
165 virtual bool RenderPage(unsigned int page_number,
166 gfx::NativeDrawingContext context,
167 const CGRect rect,
168 bool shrink_to_fit,
169 bool stretch_to_fit,
170 bool center_horizontally,
171 bool center_vertically) const = 0;
172 #elif defined(OS_POSIX)
173 // Sets raw PDF data for the document. This is used when a cairo drawing
174 // surface has already been created. This method will cause all subsequent
175 // drawing on the surface to be discarded (in Close()). If Init() has not yet
176 // been called this method simply calls the second version of Init.
177 virtual bool SetRawData(const void* src_buffer, uint32 src_buffer_size) = 0;
178 #if defined(OS_CHROMEOS)
179 // Saves the underlying data to the file associated with fd. This function
180 // should ONLY be called after the metafile is closed.
181 // Returns true if writing succeeded.
182 virtual bool SaveToFD(const base::FileDescriptor& fd) const = 0;
183 #endif // if defined(OS_CHROMEOS)
184
185 #endif
186 };
187
188 } // namespace printing
189
19 #endif // PRINTING_NATIVE_METAFILE_H_ 190 #endif // PRINTING_NATIVE_METAFILE_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698