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