[email protected] | 5ad7617 | 2011-03-02 19:25:17 | [diff] [blame] | 1 | // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
[email protected] | 0e0fca3 | 2009-07-06 15:25:50 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
[email protected] | 8ff1d42 | 2009-07-07 21:31:39 | [diff] [blame] | 5 | #ifndef PRINTING_NATIVE_METAFILE_H_ |
| 6 | #define PRINTING_NATIVE_METAFILE_H_ |
[email protected] | 0e0fca3 | 2009-07-06 15:25:50 | [diff] [blame] | 7 | |
[email protected] | 5ad7617 | 2011-03-02 19:25:17 | [diff] [blame] | 8 | #include "base/basictypes.h" |
[email protected] | 3cfa15ac | 2009-08-25 17:36:37 | [diff] [blame] | 9 | #include "build/build_config.h" |
[email protected] | 8f17cd3e | 2011-03-16 01:39:42 | [diff] [blame] | 10 | #include "ui/gfx/native_widget_types.h" |
[email protected] | 3cfa15ac | 2009-08-25 17:36:37 | [diff] [blame] | 11 | |
[email protected] | 0e0fca3 | 2009-07-06 15:25:50 | [diff] [blame] | 12 | #if defined(OS_WIN) |
[email protected] | 8f17cd3e | 2011-03-16 01:39:42 | [diff] [blame] | 13 | #include <windows.h> |
| 14 | #include <vector> |
[email protected] | 0e0fca3 | 2009-07-06 15:25:50 | [diff] [blame] | 15 | #elif defined(OS_MACOSX) |
[email protected] | 8f17cd3e | 2011-03-16 01:39:42 | [diff] [blame] | 16 | #include <ApplicationServices/ApplicationServices.h> |
| 17 | #include <CoreFoundation/CoreFoundation.h> |
| 18 | #include "base/mac/scoped_cftyperef.h" |
[email protected] | 0e0fca3 | 2009-07-06 15:25:50 | [diff] [blame] | 19 | #endif |
| 20 | |
[email protected] | 8f17cd3e | 2011-03-16 01:39:42 | [diff] [blame] | 21 | class FilePath; |
| 22 | |
| 23 | namespace gfx { |
[email protected] | edc531f9 | 2011-03-18 17:52:23 | [diff] [blame] | 24 | class Point; |
[email protected] | 8f17cd3e | 2011-03-16 01:39:42 | [diff] [blame] | 25 | class Rect; |
| 26 | class Size; |
[email protected] | 8f17cd3e | 2011-03-16 01:39:42 | [diff] [blame] | 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. |
[email protected] | 89eff96a | 2011-03-17 23:22:06 | [diff] [blame] | 51 | virtual bool InitFromData(const void* src_buffer, uint32 src_buffer_size) = 0; |
[email protected] | 8f17cd3e | 2011-03-16 01:39:42 | [diff] [blame] | 52 | |
[email protected] | edc531f9 | 2011-03-18 17:52:23 | [diff] [blame] | 53 | // Prepares a context for rendering a new page at the specified |
| 54 | // |content_origin| with the given |page_size| and a |scale_factor| to use for |
| 55 | // the drawing. The units are in points (=1/72 in). Returns true on success. |
| 56 | virtual bool StartPage(const gfx::Size& page_size, |
| 57 | const gfx::Point& content_origin, |
| 58 | const float& scale_factor) = 0; |
[email protected] | 8f17cd3e | 2011-03-16 01:39:42 | [diff] [blame] | 59 | |
| 60 | // Closes the current page and destroys the context used in rendering that |
| 61 | // page. The results of current page will be appended into the underlying |
| 62 | // data stream. Returns true on success. |
| 63 | virtual bool FinishPage() = 0; |
| 64 | |
| 65 | // Closes the metafile. No further rendering is allowed (the current page |
| 66 | // is implicitly closed). |
[email protected] | cdd19f5 | 2011-03-19 01:04:57 | [diff] [blame^] | 67 | virtual bool FinishDocument() = 0; |
[email protected] | 8f17cd3e | 2011-03-16 01:39:42 | [diff] [blame] | 68 | |
| 69 | // Returns the size of the underlying data stream. Only valid after Close() |
| 70 | // has been called. |
| 71 | virtual uint32 GetDataSize() const = 0; |
| 72 | |
| 73 | // Copies the first |dst_buffer_size| bytes of the underlying data stream into |
| 74 | // |dst_buffer|. This function should ONLY be called after Close() is invoked. |
| 75 | // Returns true if the copy succeeds. |
| 76 | virtual bool GetData(void* dst_buffer, uint32 dst_buffer_size) const = 0; |
| 77 | |
| 78 | // Saves the underlying data to the given file. This function should ONLY be |
| 79 | // called after the metafile is closed. Returns true if writing succeeded. |
| 80 | virtual bool SaveTo(const FilePath& file_path) const = 0; |
| 81 | |
| 82 | // Returns the bounds of the given page. Pages use a 1-based index. |
| 83 | virtual gfx::Rect GetPageBounds(unsigned int page_number) const = 0; |
| 84 | virtual unsigned int GetPageCount() const = 0; |
| 85 | |
| 86 | // Get the context for rendering to the PDF. |
| 87 | virtual gfx::NativeDrawingContext context() const = 0; |
| 88 | |
| 89 | #if defined(OS_WIN) |
[email protected] | f2da4b0 | 2011-03-18 05:45:51 | [diff] [blame] | 90 | // Generates a virtual HDC that will record every GDI commands and compile it |
| 91 | // in a EMF data stream. |
| 92 | // hdc is used to setup the default DPI and color settings. hdc is optional. |
| 93 | // rect specifies the dimensions (in .01-millimeter units) of the EMF. rect is |
| 94 | // optional. |
| 95 | virtual bool CreateDc(gfx::NativeDrawingContext sibling, |
| 96 | const RECT* rect) = 0; |
| 97 | |
| 98 | // Similar to the above method but the metafile is backed by a file. |
| 99 | virtual bool CreateFileBackedDc(gfx::NativeDrawingContext sibling, |
| 100 | const RECT* rect, |
| 101 | const FilePath& path) = 0; |
| 102 | |
| 103 | // TODO(maruel): CreateFromFile(). If ever used. Maybe users would like to |
| 104 | // have the ability to save web pages to an EMF file? Afterward, it is easy to |
| 105 | // convert to PDF or PS. |
| 106 | // Load a metafile fromdisk. |
| 107 | virtual bool CreateFromFile(const FilePath& metafile_path) = 0; |
| 108 | |
| 109 | // Closes the HDC created by CreateDc() and generates the compiled EMF |
| 110 | // data. |
| 111 | virtual void CloseEmf() = 0; |
| 112 | |
[email protected] | 8f17cd3e | 2011-03-16 01:39:42 | [diff] [blame] | 113 | // "Plays" the EMF buffer in a HDC. It is the same effect as calling the |
| 114 | // original GDI function that were called when recording the EMF. |rect| is in |
| 115 | // "logical units" and is optional. If |rect| is NULL, the natural EMF bounds |
| 116 | // are used. |
| 117 | // Note: Windows has been known to have stack buffer overflow in its GDI |
| 118 | // functions, whether used directly or indirectly through precompiled EMF |
| 119 | // data. We have to accept the risk here. Since it is used only for printing, |
| 120 | // it requires user intervention. |
| 121 | virtual bool Playback(gfx::NativeDrawingContext hdc, |
| 122 | const RECT* rect) const = 0; |
| 123 | |
| 124 | // The slow version of Playback(). It enumerates all the records and play them |
| 125 | // back in the HDC. The trick is that it skip over the records known to have |
| 126 | // issue with some printers. See Emf::Record::SafePlayback implementation for |
| 127 | // details. |
| 128 | virtual bool SafePlayback(gfx::NativeDrawingContext hdc) const = 0; |
| 129 | |
| 130 | // Retrieves the underlying data stream. It is a helper function. |
| 131 | virtual bool GetData(std::vector<uint8>* buffer) const = 0; |
| 132 | |
| 133 | virtual HENHMETAFILE emf() const = 0; |
| 134 | #elif defined(OS_MACOSX) |
| 135 | // Renders the given page into |rect| in the given context. |
| 136 | // Pages use a 1-based index. The rendering uses the following arguments |
| 137 | // to determine scaling and translation factors. |
| 138 | // |shrink_to_fit| specifies whether the output should be shrunk to fit the |
| 139 | // supplied |rect| if the page size is larger than |rect| in any dimension. |
| 140 | // If this is false, parts of the PDF page that lie outside the bounds will be |
| 141 | // clipped. |
| 142 | // |stretch_to_fit| specifies whether the output should be stretched to fit |
| 143 | // the supplied bounds if the page size is smaller than |rect| in all |
| 144 | // dimensions. |
| 145 | // |center_horizontally| specifies whether the final image (after any scaling |
| 146 | // is done) should be centered horizontally within the given |rect|. |
| 147 | // |center_vertically| specifies whether the final image (after any scaling |
| 148 | // is done) should be centered vertically within the given |rect|. |
| 149 | // Note that all scaling preserves the original aspect ratio of the page. |
| 150 | virtual bool RenderPage(unsigned int page_number, |
| 151 | gfx::NativeDrawingContext context, |
| 152 | const CGRect rect, |
| 153 | bool shrink_to_fit, |
| 154 | bool stretch_to_fit, |
| 155 | bool center_horizontally, |
| 156 | bool center_vertically) const = 0; |
| 157 | #elif defined(OS_POSIX) |
| 158 | // Sets raw PDF data for the document. This is used when a cairo drawing |
| 159 | // surface has already been created. This method will cause all subsequent |
| 160 | // drawing on the surface to be discarded (in Close()). If Init() has not yet |
| 161 | // been called this method simply calls the second version of Init. |
| 162 | virtual bool SetRawData(const void* src_buffer, uint32 src_buffer_size) = 0; |
| 163 | #if defined(OS_CHROMEOS) |
| 164 | // Saves the underlying data to the file associated with fd. This function |
| 165 | // should ONLY be called after the metafile is closed. |
| 166 | // Returns true if writing succeeded. |
| 167 | virtual bool SaveToFD(const base::FileDescriptor& fd) const = 0; |
| 168 | #endif // if defined(OS_CHROMEOS) |
| 169 | |
| 170 | #endif |
| 171 | }; |
| 172 | |
| 173 | } // namespace printing |
| 174 | |
[email protected] | 8ff1d42 | 2009-07-07 21:31:39 | [diff] [blame] | 175 | #endif // PRINTING_NATIVE_METAFILE_H_ |