[email protected] | e244c6a5 | 2010-08-31 15:36:13 | [diff] [blame] | 1 | // Copyright (c) 2010 The Chromium Authors. All rights reserved. |
license.bot | bf09a50 | 2008-08-24 00:55:55 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 4 | |
[email protected] | 8ff1d42 | 2009-07-07 21:31:39 | [diff] [blame] | 5 | #ifndef PRINTING_EMF_WIN_H_ |
| 6 | #define PRINTING_EMF_WIN_H_ |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 7 | |
| 8 | #include <windows.h> |
| 9 | #include <vector> |
| 10 | |
| 11 | #include "base/basictypes.h" |
| 12 | |
[email protected] | e6cddc57 | 2010-09-29 21:39:45 | [diff] [blame] | 13 | class FilePath; |
| 14 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 15 | namespace gfx { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 16 | class Rect; |
[email protected] | 0e0fca3 | 2009-07-06 15:25:50 | [diff] [blame] | 17 | } |
| 18 | |
| 19 | namespace printing { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 20 | |
| 21 | // Simple wrapper class that manage an EMF data stream and its virtual HDC. |
| 22 | class Emf { |
| 23 | public: |
| 24 | class Record; |
| 25 | class Enumerator; |
| 26 | struct EnumerationContext; |
| 27 | |
| 28 | Emf(); |
| 29 | ~Emf(); |
| 30 | |
[email protected] | e8980a5 | 2010-10-07 20:11:24 | [diff] [blame] | 31 | // Initializes the Emf with the data in |src_buffer|. Returns true on success. |
| 32 | bool Init(const void* src_buffer, uint32 src_buffer_size); |
| 33 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 34 | // Generates a virtual HDC that will record every GDI commands and compile it |
| 35 | // in a EMF data stream. |
| 36 | // hdc is used to setup the default DPI and color settings. hdc is optional. |
| 37 | // rect specifies the dimensions (in .01-millimeter units) of the EMF. rect is |
| 38 | // optional. |
| 39 | bool CreateDc(HDC sibling, const RECT* rect); |
| 40 | |
[email protected] | e6cddc57 | 2010-09-29 21:39:45 | [diff] [blame] | 41 | // Similar to the above method but the metafile is backed by a file. |
| 42 | bool CreateFileBackedDc(HDC sibling, const RECT* rect, const FilePath& path); |
| 43 | |
[email protected] | e6cddc57 | 2010-09-29 21:39:45 | [diff] [blame] | 44 | // Load an EMF file. |
| 45 | bool CreateFromFile(const FilePath& metafile_path); |
| 46 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 47 | // TODO(maruel): CreateFromFile(). If ever used. Maybe users would like to |
| 48 | // have the ability to save web pages to an EMF file? Afterward, it is easy to |
| 49 | // convert to PDF or PS. |
| 50 | |
| 51 | // Closes the HDC created by CreateDc() and generates the compiled EMF |
| 52 | // data. |
| 53 | bool CloseDc(); |
| 54 | |
| 55 | // Closes the EMF data handle when it is not needed anymore. |
| 56 | void CloseEmf(); |
| 57 | |
| 58 | // "Plays" the EMF buffer in a HDC. It is the same effect as calling the |
| 59 | // original GDI function that were called when recording the EMF. |rect| is in |
| 60 | // "logical units" and is optional. If |rect| is NULL, the natural EMF bounds |
| 61 | // are used. |
| 62 | // Note: Windows has been known to have stack buffer overflow in its GDI |
| 63 | // functions, whether used directly or indirectly through precompiled EMF |
| 64 | // data. We have to accept the risk here. Since it is used only for printing, |
| 65 | // it requires user intervention. |
| 66 | bool Playback(HDC hdc, const RECT* rect) const; |
| 67 | |
| 68 | // The slow version of Playback(). It enumerates all the records and play them |
| 69 | // back in the HDC. The trick is that it skip over the records known to have |
| 70 | // issue with some printers. See Emf::Record::SafePlayback implementation for |
| 71 | // details. |
| 72 | bool SafePlayback(HDC hdc) const; |
| 73 | |
| 74 | // Retrieves the bounds of the painted area by this EMF buffer. This value |
| 75 | // should be passed to Playback to keep the exact same size. |
| 76 | gfx::Rect GetBounds() const; |
| 77 | |
| 78 | // Retrieves the EMF stream size. |
[email protected] | b5ab398 | 2010-02-16 23:58:27 | [diff] [blame] | 79 | uint32 GetDataSize() const; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 80 | |
| 81 | // Retrieves the EMF stream. |
[email protected] | b5ab398 | 2010-02-16 23:58:27 | [diff] [blame] | 82 | bool GetData(void* buffer, uint32 size) const; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 83 | |
| 84 | // Retrieves the EMF stream. It is an helper function. |
| 85 | bool GetData(std::vector<uint8>* buffer) const; |
| 86 | |
| 87 | HENHMETAFILE emf() const { |
| 88 | return emf_; |
| 89 | } |
| 90 | |
| 91 | HDC hdc() const { |
| 92 | return hdc_; |
| 93 | } |
| 94 | |
[email protected] | 2aa8e18 | 2010-07-12 16:25:04 | [diff] [blame] | 95 | // Inserts a custom GDICOMMENT records indicating StartPage/EndPage calls |
| 96 | // (since StartPage and EndPage do not work in a metafile DC). Only valid |
| 97 | // when hdc_ is non-NULL. |
| 98 | bool StartPage(); |
| 99 | bool EndPage(); |
| 100 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 101 | // Saves the EMF data to a file as-is. It is recommended to use the .emf file |
| 102 | // extension but it is not enforced. This function synchronously writes to the |
| 103 | // file. For testing only. |
| 104 | bool SaveTo(const std::wstring& filename) const; |
| 105 | |
| 106 | private: |
| 107 | // Playbacks safely one EMF record. |
| 108 | static int CALLBACK SafePlaybackProc(HDC hdc, |
| 109 | HANDLETABLE* handle_table, |
| 110 | const ENHMETARECORD* record, |
| 111 | int objects_count, |
| 112 | LPARAM param); |
| 113 | |
| 114 | // Compiled EMF data handle. |
| 115 | HENHMETAFILE emf_; |
| 116 | |
| 117 | // Valid when generating EMF data through a virtual HDC. |
| 118 | HDC hdc_; |
| 119 | |
[email protected] | 5930cb6 | 2009-12-08 02:04:22 | [diff] [blame] | 120 | DISALLOW_COPY_AND_ASSIGN(Emf); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 121 | }; |
| 122 | |
| 123 | struct Emf::EnumerationContext { |
| 124 | HANDLETABLE* handle_table; |
| 125 | int objects_count; |
| 126 | HDC hdc; |
| 127 | }; |
| 128 | |
| 129 | // One EMF record. It keeps pointers to the EMF buffer held by Emf::emf_. |
| 130 | // The entries become invalid once Emf::CloseEmf() is called. |
| 131 | class Emf::Record { |
| 132 | public: |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 133 | // Plays the record. |
| 134 | bool Play() const; |
| 135 | |
| 136 | // Plays the record working around quirks with SetLayout, |
| 137 | // SetWorldTransform and ModifyWorldTransform. See implementation for details. |
| 138 | bool SafePlayback(const XFORM* base_matrix) const; |
| 139 | |
| 140 | // Access the underlying EMF record. |
| 141 | const ENHMETARECORD* record() const { return record_; } |
| 142 | |
| 143 | protected: |
| 144 | Record(const EnumerationContext* context, |
| 145 | const ENHMETARECORD* record); |
| 146 | |
| 147 | private: |
| 148 | friend class Emf; |
| 149 | friend class Enumerator; |
| 150 | const ENHMETARECORD* record_; |
| 151 | const EnumerationContext* context_; |
| 152 | }; |
| 153 | |
| 154 | // Retrieves individual records out of a Emf buffer. The main use is to skip |
| 155 | // over records that are unsupported on a specific printer or to play back |
| 156 | // only a part of an EMF buffer. |
| 157 | class Emf::Enumerator { |
| 158 | public: |
| 159 | // Iterator type used for iterating the records. |
| 160 | typedef std::vector<Record>::const_iterator const_iterator; |
| 161 | |
| 162 | // Enumerates the records at construction time. |hdc| and |rect| are |
| 163 | // both optional at the same time or must both be valid. |
| 164 | // Warning: |emf| must be kept valid for the time this object is alive. |
| 165 | Enumerator(const Emf& emf, HDC hdc, const RECT* rect); |
| 166 | |
| 167 | // Retrieves the first Record. |
| 168 | const_iterator begin() const; |
| 169 | |
| 170 | // Retrieves the end of the array. |
| 171 | const_iterator end() const; |
| 172 | |
| 173 | private: |
| 174 | // Processes one EMF record and saves it in the items_ array. |
| 175 | static int CALLBACK EnhMetaFileProc(HDC hdc, |
| 176 | HANDLETABLE* handle_table, |
| 177 | const ENHMETARECORD* record, |
| 178 | int objects_count, |
| 179 | LPARAM param); |
| 180 | |
| 181 | // The collection of every EMF records in the currently loaded EMF buffer. |
| 182 | // Initialized by Enumerate(). It keeps pointers to the EMF buffer held by |
| 183 | // Emf::emf_. The entries become invalid once Emf::CloseEmf() is called. |
| 184 | std::vector<Record> items_; |
| 185 | |
| 186 | EnumerationContext context_; |
| 187 | |
[email protected] | 5930cb6 | 2009-12-08 02:04:22 | [diff] [blame] | 188 | DISALLOW_COPY_AND_ASSIGN(Enumerator); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 189 | }; |
| 190 | |
[email protected] | 0e0fca3 | 2009-07-06 15:25:50 | [diff] [blame] | 191 | } // namespace printing |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 192 | |
[email protected] | 8ff1d42 | 2009-07-07 21:31:39 | [diff] [blame] | 193 | #endif // PRINTING_EMF_WIN_H_ |