[email protected] | b5cf844c | 2012-06-18 21:49:20 | [diff] [blame] | 1 | // Copyright (c) 2012 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 | |
avi | 126e93c | 2015-12-21 21:48:16 | [diff] [blame] | 8 | #include <stdint.h> |
| 9 | |
vitalybuka | 5d129058 | 2014-09-12 09:19:59 | [diff] [blame] | 10 | #include <vector> |
| 11 | |
avi | 126e93c | 2015-12-21 21:48:16 | [diff] [blame] | 12 | #include "base/macros.h" |
[email protected] | 3cfa15ac | 2009-08-25 17:36:37 | [diff] [blame] | 13 | #include "build/build_config.h" |
Thiago Farina | 45e31953 | 2017-03-23 02:44:49 | [diff] [blame] | 14 | #include "printing/native_drawing_context.h" |
[email protected] | 69f5b1e6 | 2011-09-01 06:34:04 | [diff] [blame] | 15 | #include "printing/printing_export.h" |
[email protected] | 3cfa15ac | 2009-08-25 17:36:37 | [diff] [blame] | 16 | |
thestig | 3109df1 | 2017-04-26 21:57:25 | [diff] [blame] | 17 | #if defined(OS_WIN) |
| 18 | #include <windows.h> |
thestig | 99fc213 | 2017-04-27 03:37:54 | [diff] [blame] | 19 | #elif defined(OS_MACOSX) |
| 20 | #include <ApplicationServices/ApplicationServices.h> |
| 21 | #include <CoreFoundation/CoreFoundation.h> |
| 22 | #include "base/mac/scoped_cftyperef.h" |
thestig | 3109df1 | 2017-04-26 21:57:25 | [diff] [blame] | 23 | #endif |
| 24 | |
[email protected] | a3ef483 | 2013-02-02 05:12:33 | [diff] [blame] | 25 | namespace base { |
vitalybuka | 5d129058 | 2014-09-12 09:19:59 | [diff] [blame] | 26 | class File; |
[email protected] | a3ef483 | 2013-02-02 05:12:33 | [diff] [blame] | 27 | } |
[email protected] | 8f17cd3e | 2011-03-16 01:39:42 | [diff] [blame] | 28 | |
| 29 | namespace gfx { |
| 30 | class Rect; |
| 31 | class Size; |
[email protected] | 8f17cd3e | 2011-03-16 01:39:42 | [diff] [blame] | 32 | } |
| 33 | |
[email protected] | 8f17cd3e | 2011-03-16 01:39:42 | [diff] [blame] | 34 | namespace printing { |
| 35 | |
vitalybuka | 5d129058 | 2014-09-12 09:19:59 | [diff] [blame] | 36 | // This class plays metafiles from data stream (usually PDF or EMF). |
| 37 | class PRINTING_EXPORT MetafilePlayer { |
[email protected] | 8f17cd3e | 2011-03-16 01:39:42 | [diff] [blame] | 38 | public: |
thestig | 99fc213 | 2017-04-27 03:37:54 | [diff] [blame] | 39 | #if defined(OS_MACOSX) |
| 40 | // |shrink_to_fit| specifies whether the output should be shrunk to fit a |
| 41 | // destination page if the source PDF is bigger than the destination page in |
| 42 | // any dimension. If this is false, parts of the source PDF page that lie |
| 43 | // outside the bounds will be clipped. |
| 44 | // |stretch_to_fit| specifies whether the output should be stretched to fit |
| 45 | // the destination page if the source page size is smaller in all dimensions. |
| 46 | // |center_horizontally| specifies whether the output (after any scaling is |
| 47 | // done) should be centered horizontally within the destination page. |
| 48 | // |center_vertically| specifies whether the output (after any scaling is |
| 49 | // done) should be centered vertically within the destination page. |
| 50 | // Note that all scaling preserves the original aspect ratio of the page. |
| 51 | // |autorotate| specifies whether the source PDF should be autorotated to fit |
| 52 | // on the destination page. |
| 53 | struct MacRenderPageParams { |
| 54 | MacRenderPageParams() |
| 55 | : shrink_to_fit(false), |
| 56 | stretch_to_fit(false), |
| 57 | center_horizontally(false), |
| 58 | center_vertically(false), |
| 59 | autorotate(false) { |
| 60 | } |
| 61 | |
| 62 | bool shrink_to_fit; |
| 63 | bool stretch_to_fit; |
| 64 | bool center_horizontally; |
| 65 | bool center_vertically; |
| 66 | bool autorotate; |
| 67 | }; |
| 68 | #endif // defined(OS_MACOSX) |
vitalybuka | 5d129058 | 2014-09-12 09:19:59 | [diff] [blame] | 69 | MetafilePlayer(); |
| 70 | virtual ~MetafilePlayer(); |
[email protected] | b5cf844c | 2012-06-18 21:49:20 | [diff] [blame] | 71 | |
vitalybuka | 5d129058 | 2014-09-12 09:19:59 | [diff] [blame] | 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. |
Nico Weber | 8e55956 | 2017-10-03 01:25:26 | [diff] [blame] | 77 | virtual bool SafePlayback(printing::NativeDrawingContext hdc) const = 0; |
thestig | 99fc213 | 2017-04-27 03:37:54 | [diff] [blame] | 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, |
Nico Weber | 8e55956 | 2017-10-03 01:25:26 | [diff] [blame] | 84 | printing::NativeDrawingContext context, |
thestig | 99fc213 | 2017-04-27 03:37:54 | [diff] [blame] | 85 | const CGRect rect, |
| 86 | const MacRenderPageParams& params) const = 0; |
vitalybuka | 5d129058 | 2014-09-12 09:19:59 | [diff] [blame] | 87 | #endif // if defined(OS_WIN) |
| 88 | |
skau | b793195 | 2016-07-27 18:04:51 | [diff] [blame] | 89 | // Populates the buffer with the underlying data. This function should ONLY be |
| 90 | // called after the metafile is closed. Returns true if writing succeeded. |
| 91 | virtual bool GetDataAsVector(std::vector<char>* buffer) const = 0; |
| 92 | |
vitalybuka | 5d129058 | 2014-09-12 09:19:59 | [diff] [blame] | 93 | // Saves the underlying data to the given file. This function should ONLY be |
| 94 | // called after the metafile is closed. Returns true if writing succeeded. |
| 95 | virtual bool SaveTo(base::File* file) const = 0; |
| 96 | |
| 97 | private: |
| 98 | DISALLOW_COPY_AND_ASSIGN(MetafilePlayer); |
| 99 | }; |
| 100 | |
| 101 | // This class creates a graphics context that renders into a data stream |
| 102 | // (usually PDF or EMF). |
| 103 | class PRINTING_EXPORT Metafile : public MetafilePlayer { |
| 104 | public: |
| 105 | Metafile(); |
dcheng | d5a0f18 | 2014-10-22 00:04:34 | [diff] [blame] | 106 | ~Metafile() override; |
[email protected] | 8f17cd3e | 2011-03-16 01:39:42 | [diff] [blame] | 107 | |
| 108 | // Initializes a fresh new metafile for rendering. Returns false on failure. |
| 109 | // Note: It should only be called from within the renderer process to allocate |
| 110 | // rendering resources. |
| 111 | virtual bool Init() = 0; |
| 112 | |
| 113 | // Initializes the metafile with the data in |src_buffer|. Returns true |
| 114 | // on success. |
| 115 | // Note: It should only be called from within the browser process. |
thestig | 707a24b2 | 2015-09-14 18:16:33 | [diff] [blame] | 116 | virtual bool InitFromData(const void* src_buffer, |
thestig | 6b4461f | 2016-10-28 19:34:30 | [diff] [blame] | 117 | size_t src_buffer_size) = 0; |
[email protected] | 8f17cd3e | 2011-03-16 01:39:42 | [diff] [blame] | 118 | |
[email protected] | 39892b9 | 2011-04-30 02:24:44 | [diff] [blame] | 119 | // Prepares a context for rendering a new page with the given |page_size|, |
thestig | 192677ec | 2016-06-09 07:43:17 | [diff] [blame] | 120 | // |content_area| and a |scale_factor| to use for the drawing. The units are |
| 121 | // in points (=1/72 in). |
| 122 | virtual void StartPage(const gfx::Size& page_size, |
[email protected] | 39892b9 | 2011-04-30 02:24:44 | [diff] [blame] | 123 | const gfx::Rect& content_area, |
[email protected] | edc531f9 | 2011-03-18 17:52:23 | [diff] [blame] | 124 | const float& scale_factor) = 0; |
[email protected] | 8f17cd3e | 2011-03-16 01:39:42 | [diff] [blame] | 125 | |
| 126 | // Closes the current page and destroys the context used in rendering that |
| 127 | // page. The results of current page will be appended into the underlying |
| 128 | // data stream. Returns true on success. |
| 129 | virtual bool FinishPage() = 0; |
| 130 | |
| 131 | // Closes the metafile. No further rendering is allowed (the current page |
| 132 | // is implicitly closed). |
[email protected] | cdd19f5 | 2011-03-19 01:04:57 | [diff] [blame] | 133 | virtual bool FinishDocument() = 0; |
[email protected] | 8f17cd3e | 2011-03-16 01:39:42 | [diff] [blame] | 134 | |
| 135 | // Returns the size of the underlying data stream. Only valid after Close() |
| 136 | // has been called. |
thestig | 707a24b2 | 2015-09-14 18:16:33 | [diff] [blame] | 137 | virtual uint32_t GetDataSize() const = 0; |
[email protected] | 8f17cd3e | 2011-03-16 01:39:42 | [diff] [blame] | 138 | |
| 139 | // Copies the first |dst_buffer_size| bytes of the underlying data stream into |
| 140 | // |dst_buffer|. This function should ONLY be called after Close() is invoked. |
| 141 | // Returns true if the copy succeeds. |
thestig | 707a24b2 | 2015-09-14 18:16:33 | [diff] [blame] | 142 | virtual bool GetData(void* dst_buffer, uint32_t dst_buffer_size) const = 0; |
[email protected] | 8f17cd3e | 2011-03-16 01:39:42 | [diff] [blame] | 143 | |
[email protected] | 8f17cd3e | 2011-03-16 01:39:42 | [diff] [blame] | 144 | virtual gfx::Rect GetPageBounds(unsigned int page_number) const = 0; |
| 145 | virtual unsigned int GetPageCount() const = 0; |
| 146 | |
Nico Weber | 8e55956 | 2017-10-03 01:25:26 | [diff] [blame] | 147 | virtual printing::NativeDrawingContext context() const = 0; |
[email protected] | 8f17cd3e | 2011-03-16 01:39:42 | [diff] [blame] | 148 | |
thestig | 3109df1 | 2017-04-26 21:57:25 | [diff] [blame] | 149 | #if defined(OS_WIN) |
| 150 | // "Plays" the EMF buffer in a HDC. It is the same effect as calling the |
| 151 | // original GDI function that were called when recording the EMF. |rect| is in |
| 152 | // "logical units" and is optional. If |rect| is NULL, the natural EMF bounds |
| 153 | // are used. |
| 154 | // Note: Windows has been known to have stack buffer overflow in its GDI |
| 155 | // functions, whether used directly or indirectly through precompiled EMF |
| 156 | // data. We have to accept the risk here. Since it is used only for printing, |
| 157 | // it requires user intervention. |
Nico Weber | 8e55956 | 2017-10-03 01:25:26 | [diff] [blame] | 158 | virtual bool Playback(printing::NativeDrawingContext hdc, |
thestig | 3109df1 | 2017-04-26 21:57:25 | [diff] [blame] | 159 | const RECT* rect) const = 0; |
| 160 | #endif // OS_WIN |
| 161 | |
skau | b793195 | 2016-07-27 18:04:51 | [diff] [blame] | 162 | // MetfilePlayer |
| 163 | bool GetDataAsVector(std::vector<char>* buffer) const override; |
dcheng | d5a0f18 | 2014-10-22 00:04:34 | [diff] [blame] | 164 | bool SaveTo(base::File* file) const override; |
vitalybuka | 5d129058 | 2014-09-12 09:19:59 | [diff] [blame] | 165 | |
| 166 | private: |
| 167 | DISALLOW_COPY_AND_ASSIGN(Metafile); |
[email protected] | 8f17cd3e | 2011-03-16 01:39:42 | [diff] [blame] | 168 | }; |
| 169 | |
| 170 | } // namespace printing |
| 171 | |
[email protected] | 7d748990 | 2011-04-11 21:54:06 | [diff] [blame] | 172 | #endif // PRINTING_METAFILE_H_ |