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