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