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

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: Fixed include ordering, some comments and style 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"
10 9
11 #if defined(OS_WIN) 10 #if defined(OS_WIN)
12 #include "printing/native_metafile_win.h" 11 #include <windows.h>
12 #include <vector>
13 #elif defined(OS_MACOSX) 13 #elif defined(OS_MACOSX)
14 #include "printing/native_metafile_mac.h" 14 #include <ApplicationServices/ApplicationServices.h>
15 #elif defined(OS_POSIX) 15 #include <CoreFoundation/CoreFoundation.h>
16 #include "printing/native_metafile_linux.h" 16 #include "base/mac/scoped_cftyperef.h"
17 #endif 17 #endif
18 18
19 #if defined(OS_WIN)
20 namespace gfx {
21 class Rect;
22 }
23 #elif defined(OS_MACOSX)
24 namespace gfx {
25 class Rect;
26 class Size;
27 class Point;
28 }
29 #elif defined(OS_POSIX)
30 typedef struct _cairo_surface cairo_surface_t;
31 typedef struct _cairo cairo_t;
32 #endif
33
34 class FilePath;
35
36 namespace printing {
37
38 // This class creates a graphics context that renders into a PDF data stream.
39 class NativeMetafile {
40 public:
41 virtual ~NativeMetafile() {};
Lei Zhang 2011/03/04 12:02:36 nit: no ; needed.
dpapad 2011/03/04 19:19:43 Done.
42
43 // Initializes a fresh new metafile, and returns a drawing context for
44 // rendering into the PDF. Returns false on failure.
Lei Zhang 2011/03/04 12:02:36 Comment is wrong. You don't return a context here.
dpapad 2011/03/04 19:19:43 Done.
45 // Note: It should only be called from within the renderer process to allocate
46 // rendering resources.
47 virtual bool Init() = 0;
48
49 // Initializes the metafile with the data in |src_buffer|. Returns true
50 // on success.
51 // Note: It should only be called from within the browser process.
Lei Zhang 2011/03/04 12:02:36 nit: 2 spaces after 'be'.
dpapad 2011/03/04 19:19:43 Done.
52 virtual bool Init(const void* src_buffer, uint32 src_buffer_size) = 0;
53
54 // Returns the size of the underlying PDF data. Only valid after Close() has
55 // been called.
56 virtual uint32 GetDataSize() const = 0;
57
58 // Copies the first |dst_buffer_size| bytes of the PDF steam into
59 // |dst_buffer|. This function should ONLY be called after Close() is invoked.
Lei Zhang 2011/03/04 12:02:36 s/steam/stream/
dpapad 2011/03/04 19:19:43 Done.
60 // Returns true if the copy succeeds.
61 virtual bool GetData(void* dst_buffer, uint32 dst_buffer_size) const = 0;
62
63 // Closes the current page and destroys the context used in rendering that
64 // page. The results of current page will be appended into the underlying PDF
65 // data. Returns true on success.
66 virtual bool FinishPage() = 0;
67
68 // Closes the PDF file. No further rendering is allowed.
69 virtual void Close() = 0;
70
71 // Saves the raw PDF data to the given file. This function should ONLY be
72 // called after PDF file is closed. For testing only.
73 // Returns true if writing succeeded.
74 virtual bool SaveTo(const FilePath& file_path) const = 0;
75
76 #if defined(OS_WIN)||defined(OS_MACOSX)
77 // Returns the bounds of the given page. Pages use a 1-based index.
78 virtual gfx::Rect GetPageBounds(unsigned int page_number) const = 0;
Lei Zhang 2011/03/04 12:02:36 Not sure if this makes sense when the Windows impl
dpapad 2011/03/04 19:19:43 I had the same concern. It will make more sense la
79 virtual unsigned int GetPageCount() const = 0;
80 #endif
81
82 #if defined(OS_WIN)
83
Lei Zhang 2011/03/04 12:02:36 nit: remove blank line
dpapad 2011/03/04 19:19:43 Done.
84 // Generates a virtual HDC that will record every GDI commands and compile it
85 // in a EMF data stream.
86 // hdc is used to setup the default DPI and color settings. hdc is optional.
87 // rect specifies the dimensions (in .01-millimeter units) of the EMF. rect is
88 // optional.
89 virtual bool CreateDc(HDC sibling, const RECT* rect) = 0;
90
91 // Similar to the above method but the metafile is backed by a file.
92 virtual bool CreateFileBackedDc(HDC sibling,
93 const RECT* rect,
94 const FilePath& path) = 0;
95
96 // TODO(maruel): CreateFromFile(). If ever used. Maybe users would like to
97 // have the ability to save web pages to an EMF file? Afterward, it is easy to
98 // convert to PDF or PS.
99 // Load an EMF file.
100 virtual bool CreateFromFile(const FilePath& metafile_path) = 0;
101
102 // Closes the HDC created by CreateDc() and generates the compiled EMF
103 // data.
104 virtual bool CloseDc() = 0;
105
106 // "Plays" the EMF buffer in a HDC. It is the same effect as calling the
107 // original GDI function that were called when recording the EMF. |rect| is in
108 // "logical units" and is optional. If |rect| is NULL, the natural EMF bounds
109 // are used.
110 // Note: Windows has been known to have stack buffer overflow in its GDI
111 // functions, whether used directly or indirectly through precompiled EMF
112 // data. We have to accept the risk here. Since it is used only for printing,
113 // it requires user intervention.
114 virtual bool Playback(HDC hdc, const RECT* rect) const = 0;
115
116 // The slow version of Playback(). It enumerates all the records and play them
117 // back in the HDC. The trick is that it skip over the records known to have
118 // issue with some printers. See Emf::Record::SafePlayback implementation for
119 // details.
120 virtual bool SafePlayback(HDC hdc) const = 0;
121
122 // Retrieves the EMF stream. It is a helper function.
123 virtual bool GetData(std::vector<uint8>* buffer) const = 0;
124
125 // Inserts a custom GDICOMMENT records indicating StartPage/EndPage calls
126 // (since StartPage and EndPage do not work in a metafile DC). Only valid
127 // when hdc_ is non-NULL.
128 virtual bool StartPage() = 0;
129
130 virtual HENHMETAFILE emf() const = 0;
131 virtual HDC hdc() const = 0;
132 #elif defined(OS_MACOSX)
133 // Prepares a new pdf page at specified |content_origin| with the given
134 // |page_size| and a |scale_factor| to use for the drawing.
135 virtual CGContextRef StartPage(const gfx::Size& page_size,
136 const gfx::Point& content_origin,
137 const float& scale_factor) = 0;
138
139 // Renders the given page into |rect| in the given context.
140 // Pages use a 1-based index. The rendering uses the following arguments
141 // to determine scaling and translation factors.
142 // |shrink_to_fit| specifies whether the output should be shrunk to fit the
143 // supplied |rect| if the page size is larger than |rect| in any dimension.
144 // If this is false, parts of the PDF page that lie outside the bounds will be
145 // clipped.
146 // |stretch_to_fit| specifies whether the output should be stretched to fit
147 // the supplied bounds if the page size is smaller than |rect| in all
148 // dimensions.
149 // |center_horizontally| specifies whether the final image (after any scaling
150 // is done) should be centered horizontally within the given |rect|.
151 // |center_vertically| specifies whether the final image (after any scaling
152 // is done) should be centered vertically within the given |rect|.
153 // Note that all scaling preserves the original aspect ratio of the page.
154 virtual bool RenderPage(unsigned int page_number,
155 CGContextRef context,
156 const CGRect rect,
157 bool shrink_to_fit,
158 bool stretch_to_fit,
159 bool center_horizontally,
160 bool center_vertically) const = 0;
161
162 // Get the context for rendering to the PDF.
163 virtual CGContextRef context() = 0;
164 #elif defined(OS_POSIX)
165 // Sets raw PDF data for the document. This is used when a cairo drawing
166 // surface has already been created. This method will cause all subsequent
167 // drawing on the surface to be discarded (in Close()). If Init() has not yet
168 // been called this method simply calls the second version of Init.
169 virtual bool SetRawData(const void* src_buffer, uint32 src_buffer_size) = 0;
170
171 // Prepares a new cairo surface/context for rendering a new page.
172 // The unit is in point (=1/72 in).
173 // Returns NULL when failed.
174 virtual cairo_t* StartPage(double width_in_points,
175 double height_in_points,
176 double margin_top_in_points,
177 double margin_right_in_points,
178 double margin_bottom_in_points,
179 double margin_left_in_points) = 0;
180 #endif
181 };
182
183 } // namespace printing
184
19 #endif // PRINTING_NATIVE_METAFILE_H_ 185 #endif // PRINTING_NATIVE_METAFILE_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698