Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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_METAFILE_H_ | 5 #ifndef PRINTING_METAFILE_H_ |
| 6 #define PRINTING_METAFILE_H_ | 6 #define PRINTING_METAFILE_H_ |
| 7 | 7 |
| 8 #include <vector> | |
| 9 | |
| 8 #include "base/basictypes.h" | 10 #include "base/basictypes.h" |
| 9 #include "build/build_config.h" | 11 #include "build/build_config.h" |
| 10 #include "printing/printing_export.h" | 12 #include "printing/printing_export.h" |
| 11 #include "ui/gfx/native_widget_types.h" | 13 #include "ui/gfx/native_widget_types.h" |
| 12 | 14 |
| 13 #if defined(OS_WIN) | 15 #if defined(OS_WIN) |
| 14 #include <windows.h> | 16 #include <windows.h> |
| 15 #elif defined(OS_MACOSX) | 17 #elif defined(OS_MACOSX) |
| 16 #include <ApplicationServices/ApplicationServices.h> | 18 #include <ApplicationServices/ApplicationServices.h> |
| 17 #include <CoreFoundation/CoreFoundation.h> | 19 #include <CoreFoundation/CoreFoundation.h> |
| 18 #include "base/mac/scoped_cftyperef.h" | 20 #include "base/mac/scoped_cftyperef.h" |
| 19 #endif | 21 #endif |
| 20 | 22 |
| 21 namespace base { | 23 namespace base { |
| 22 class FilePath; | 24 class File; |
| 23 } | 25 } |
| 24 | 26 |
| 25 namespace gfx { | 27 namespace gfx { |
| 26 class Rect; | 28 class Rect; |
| 27 class Size; | 29 class Size; |
| 28 } | 30 } |
| 29 | 31 |
| 30 class SkBaseDevice; | 32 class SkBaseDevice; |
| 31 | 33 |
| 32 #if defined(OS_CHROMEOS) || defined(OS_ANDROID) | |
| 33 namespace base { | |
| 34 struct FileDescriptor; | |
| 35 } | |
| 36 #endif | |
| 37 | |
| 38 namespace printing { | 34 namespace printing { |
| 39 | 35 |
| 40 // This class creates a graphics context that renders into a data stream | 36 // This class plays metafiles from data stream (usually PDF or EMF). |
| 41 // (usually PDF or EMF). | 37 class PRINTING_EXPORT MetafilePlayer { |
| 42 class PRINTING_EXPORT Metafile { | |
| 43 public: | 38 public: |
| 44 #if defined(OS_MACOSX) | 39 #if defined(OS_MACOSX) |
| 45 // |shrink_to_fit| specifies whether the output should be shrunk to fit a | 40 // |shrink_to_fit| specifies whether the output should be shrunk to fit a |
| 46 // destination page if the source PDF is bigger than the destination page in | 41 // destination page if the source PDF is bigger than the destination page in |
| 47 // any dimension. If this is false, parts of the source PDF page that lie | 42 // any dimension. If this is false, parts of the source PDF page that lie |
| 48 // outside the bounds will be clipped. | 43 // outside the bounds will be clipped. |
| 49 // |stretch_to_fit| specifies whether the output should be stretched to fit | 44 // |stretch_to_fit| specifies whether the output should be stretched to fit |
| 50 // the destination page if the source page size is smaller in all dimensions. | 45 // the destination page if the source page size is smaller in all dimensions. |
| 51 // |center_horizontally| specifies whether the output (after any scaling is | 46 // |center_horizontally| specifies whether the output (after any scaling is |
| 52 // done) should be centered horizontally within the destination page. | 47 // done) should be centered horizontally within the destination page. |
| (...skipping 11 matching lines...) Expand all Loading... | |
| 64 autorotate(false) { | 59 autorotate(false) { |
| 65 } | 60 } |
| 66 | 61 |
| 67 bool shrink_to_fit; | 62 bool shrink_to_fit; |
| 68 bool stretch_to_fit; | 63 bool stretch_to_fit; |
| 69 bool center_horizontally; | 64 bool center_horizontally; |
| 70 bool center_vertically; | 65 bool center_vertically; |
| 71 bool autorotate; | 66 bool autorotate; |
| 72 }; | 67 }; |
| 73 #endif // defined(OS_MACOSX) | 68 #endif // defined(OS_MACOSX) |
| 69 MetafilePlayer() {} | |
|
Lei Zhang
2014/09/11 23:31:10
nit: Move impl to .cc file
http://www.chromium.or
Vitaly Buka (NO REVIEWS)
2014/09/12 01:54:00
Done.
| |
| 70 virtual ~MetafilePlayer() {} | |
| 74 | 71 |
| 72 #if defined(OS_WIN) | |
| 73 // The slow version of Playback(). It enumerates all the records and play them | |
| 74 // back in the HDC. The trick is that it skip over the records known to have | |
| 75 // issue with some printers. See Emf::Record::SafePlayback implementation for | |
| 76 // details. | |
| 77 virtual bool SafePlayback(gfx::NativeDrawingContext hdc) const = 0; | |
| 78 | |
| 79 #elif defined(OS_MACOSX) | |
| 80 // Renders the given page into |rect| in the given context. | |
| 81 // Pages use a 1-based index. The rendering uses the arguments in | |
| 82 // |params| to determine scaling, translation, and rotation. | |
| 83 virtual bool RenderPage(unsigned int page_number, | |
| 84 gfx::NativeDrawingContext context, | |
| 85 const CGRect rect, | |
| 86 const MacRenderPageParams& params) const = 0; | |
| 87 #endif // if defined(OS_WIN) | |
| 88 | |
| 89 // Saves the underlying data to the given file. This function should ONLY be | |
| 90 // called after the metafile is closed. Returns true if writing succeeded. | |
| 91 virtual bool SaveTo(base::File* file) const = 0; | |
| 92 | |
| 93 private: | |
| 94 DISALLOW_COPY_AND_ASSIGN(MetafilePlayer); | |
| 95 }; | |
| 96 | |
| 97 // This class creates a graphics context that renders into a data stream | |
| 98 // (usually PDF or EMF). | |
| 99 class PRINTING_EXPORT Metafile : public MetafilePlayer { | |
| 100 public: | |
| 101 Metafile() {} | |
| 75 virtual ~Metafile() {} | 102 virtual ~Metafile() {} |
| 76 | 103 |
| 77 // Initializes a fresh new metafile for rendering. Returns false on failure. | 104 // Initializes a fresh new metafile for rendering. Returns false on failure. |
| 78 // Note: It should only be called from within the renderer process to allocate | 105 // Note: It should only be called from within the renderer process to allocate |
| 79 // rendering resources. | 106 // rendering resources. |
| 80 virtual bool Init() = 0; | 107 virtual bool Init() = 0; |
| 81 | 108 |
| 82 // Initializes the metafile with the data in |src_buffer|. Returns true | 109 // Initializes the metafile with the data in |src_buffer|. Returns true |
| 83 // on success. | 110 // on success. |
| 84 // Note: It should only be called from within the browser process. | 111 // Note: It should only be called from within the browser process. |
| 85 virtual bool InitFromData(const void* src_buffer, uint32 src_buffer_size) = 0; | 112 virtual bool InitFromData(const void* src_buffer, uint32 src_buffer_size) = 0; |
| 86 | 113 |
| 87 // This method calls StartPage and then returns an appropriate | 114 // This method calls StartPage and then returns an appropriate |
| 88 // VectorPlatformDevice implementation bound to the context created by | 115 // VectorPlatformDevice implementation bound to the context created by |
| 89 // StartPage or NULL on error. | 116 // StartPage or NULL on error. |
| 90 virtual SkBaseDevice* StartPageForVectorCanvas( | 117 virtual SkBaseDevice* StartPageForVectorCanvas(const gfx::Size& page_size, |
| 91 const gfx::Size& page_size, | 118 const gfx::Rect& content_area, |
| 92 const gfx::Rect& content_area, | 119 const float& scale_factor) = 0; |
| 93 const float& scale_factor) = 0; | |
| 94 | 120 |
| 95 // Prepares a context for rendering a new page with the given |page_size|, | 121 // Prepares a context for rendering a new page with the given |page_size|, |
| 96 // |content_area| and a |scale_factor| to use for the drawing. The units are | 122 // |content_area| and a |scale_factor| to use for the drawing. The units are |
| 97 // in points (=1/72 in). Returns true on success. | 123 // in points (=1/72 in). Returns true on success. |
| 98 virtual bool StartPage(const gfx::Size& page_size, | 124 virtual bool StartPage(const gfx::Size& page_size, |
| 99 const gfx::Rect& content_area, | 125 const gfx::Rect& content_area, |
| 100 const float& scale_factor) = 0; | 126 const float& scale_factor) = 0; |
| 101 | 127 |
| 102 // Closes the current page and destroys the context used in rendering that | 128 // Closes the current page and destroys the context used in rendering that |
| 103 // page. The results of current page will be appended into the underlying | 129 // page. The results of current page will be appended into the underlying |
| 104 // data stream. Returns true on success. | 130 // data stream. Returns true on success. |
| 105 virtual bool FinishPage() = 0; | 131 virtual bool FinishPage() = 0; |
| 106 | 132 |
| 107 // Closes the metafile. No further rendering is allowed (the current page | 133 // Closes the metafile. No further rendering is allowed (the current page |
| 108 // is implicitly closed). | 134 // is implicitly closed). |
| 109 virtual bool FinishDocument() = 0; | 135 virtual bool FinishDocument() = 0; |
| 110 | 136 |
| 111 // Returns the size of the underlying data stream. Only valid after Close() | 137 // Returns the size of the underlying data stream. Only valid after Close() |
| 112 // has been called. | 138 // has been called. |
| 113 virtual uint32 GetDataSize() const = 0; | 139 virtual uint32 GetDataSize() const = 0; |
| 114 | 140 |
| 115 // Copies the first |dst_buffer_size| bytes of the underlying data stream into | 141 // Copies the first |dst_buffer_size| bytes of the underlying data stream into |
| 116 // |dst_buffer|. This function should ONLY be called after Close() is invoked. | 142 // |dst_buffer|. This function should ONLY be called after Close() is invoked. |
| 117 // Returns true if the copy succeeds. | 143 // Returns true if the copy succeeds. |
| 118 virtual bool GetData(void* dst_buffer, uint32 dst_buffer_size) const = 0; | 144 virtual bool GetData(void* dst_buffer, uint32 dst_buffer_size) const = 0; |
| 119 | 145 |
| 120 // Saves the underlying data to the given file. This function should ONLY be | |
| 121 // called after the metafile is closed. Returns true if writing succeeded. | |
| 122 virtual bool SaveTo(const base::FilePath& file_path) const = 0; | |
| 123 | |
| 124 // Returns the bounds of the given page. Pages use a 1-based index. | |
| 125 virtual gfx::Rect GetPageBounds(unsigned int page_number) const = 0; | 146 virtual gfx::Rect GetPageBounds(unsigned int page_number) const = 0; |
| 126 virtual unsigned int GetPageCount() const = 0; | 147 virtual unsigned int GetPageCount() const = 0; |
| 127 | 148 |
| 128 // Get the context for rendering to the PDF. | |
| 129 virtual gfx::NativeDrawingContext context() const = 0; | 149 virtual gfx::NativeDrawingContext context() const = 0; |
| 130 | 150 |
| 131 #if defined(OS_WIN) | 151 #if defined(OS_WIN) |
| 132 // "Plays" the EMF buffer in a HDC. It is the same effect as calling the | 152 // "Plays" the EMF buffer in a HDC. It is the same effect as calling the |
| 133 // original GDI function that were called when recording the EMF. |rect| is in | 153 // original GDI function that were called when recording the EMF. |rect| is in |
| 134 // "logical units" and is optional. If |rect| is NULL, the natural EMF bounds | 154 // "logical units" and is optional. If |rect| is NULL, the natural EMF bounds |
| 135 // are used. | 155 // are used. |
| 136 // Note: Windows has been known to have stack buffer overflow in its GDI | 156 // Note: Windows has been known to have stack buffer overflow in its GDI |
| 137 // functions, whether used directly or indirectly through precompiled EMF | 157 // functions, whether used directly or indirectly through precompiled EMF |
| 138 // data. We have to accept the risk here. Since it is used only for printing, | 158 // data. We have to accept the risk here. Since it is used only for printing, |
| 139 // it requires user intervention. | 159 // it requires user intervention. |
| 140 virtual bool Playback(gfx::NativeDrawingContext hdc, | 160 virtual bool Playback(gfx::NativeDrawingContext hdc, |
| 141 const RECT* rect) const = 0; | 161 const RECT* rect) const = 0; |
| 162 #endif // OS_WIN | |
| 142 | 163 |
| 143 // The slow version of Playback(). It enumerates all the records and play them | 164 bool GetDataAsVector(std::vector<char>* buffer) const; |
| 144 // back in the HDC. The trick is that it skip over the records known to have | |
| 145 // issue with some printers. See Emf::Record::SafePlayback implementation for | |
| 146 // details. | |
| 147 virtual bool SafePlayback(gfx::NativeDrawingContext hdc) const = 0; | |
| 148 | 165 |
| 149 virtual HENHMETAFILE emf() const = 0; | 166 virtual bool SaveTo(base::File* file) const OVERRIDE; |
| 150 #elif defined(OS_MACOSX) | 167 |
| 151 // Renders the given page into |rect| in the given context. | 168 private: |
| 152 // Pages use a 1-based index. The rendering uses the arguments in | 169 DISALLOW_COPY_AND_ASSIGN(Metafile); |
| 153 // |params| to determine scaling, translation, and rotation. | |
| 154 virtual bool RenderPage(unsigned int page_number, | |
| 155 gfx::NativeDrawingContext context, | |
| 156 const CGRect rect, | |
| 157 const MacRenderPageParams& params) const = 0; | |
| 158 #elif defined(OS_CHROMEOS) || defined(OS_ANDROID) | |
| 159 // Saves the underlying data to the file associated with fd. This function | |
| 160 // should ONLY be called after the metafile is closed. | |
| 161 // Returns true if writing succeeded. | |
| 162 virtual bool SaveToFD(const base::FileDescriptor& fd) const = 0; | |
| 163 #endif // if defined(OS_CHROMEOS) || defined(OS_ANDROID) | |
| 164 }; | 170 }; |
| 165 | 171 |
| 166 } // namespace printing | 172 } // namespace printing |
| 167 | 173 |
| 168 #endif // PRINTING_METAFILE_H_ | 174 #endif // PRINTING_METAFILE_H_ |
| OLD | NEW |