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