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