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