OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 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" | |
13 #include "printing/native_metafile_win.h" | |
14 | 12 |
15 class FilePath; | 13 class FilePath; |
16 | 14 |
17 namespace gfx { | 15 namespace gfx { |
18 class Rect; | 16 class Rect; |
19 } | 17 } |
20 | 18 |
21 namespace printing { | 19 namespace printing { |
22 | 20 |
23 // Simple wrapper class that manage an EMF data stream and its virtual HDC. | 21 // Simple wrapper class that manage an EMF data stream and its virtual HDC. |
24 class Emf : public NativeMetafile { | 22 class Emf { |
25 public: | 23 public: |
26 class Record; | 24 class Record; |
27 class Enumerator; | 25 class Enumerator; |
28 struct EnumerationContext; | 26 struct EnumerationContext; |
29 | 27 |
30 virtual ~Emf(); | 28 Emf(); |
| 29 ~Emf(); |
31 | 30 |
32 // Initializes the Emf with the data in |src_buffer|. Returns true on success. | 31 // Initializes the Emf with the data in |src_buffer|. Returns true on success. |
33 virtual bool Init(const void* src_buffer, uint32 src_buffer_size); | 32 bool Init(const void* src_buffer, uint32 src_buffer_size); |
34 | 33 |
35 // Generates a virtual HDC that will record every GDI commands and compile it | 34 // Generates a virtual HDC that will record every GDI commands and compile it |
36 // in a EMF data stream. | 35 // in a EMF data stream. |
37 // hdc is used to setup the default DPI and color settings. hdc is optional. | 36 // 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 | 37 // rect specifies the dimensions (in .01-millimeter units) of the EMF. rect is |
39 // optional. | 38 // optional. |
40 virtual bool CreateDc(HDC sibling, const RECT* rect); | 39 bool CreateDc(HDC sibling, const RECT* rect); |
41 | 40 |
42 // Similar to the above method but the metafile is backed by a file. | 41 // Similar to the above method but the metafile is backed by a file. |
43 virtual bool CreateFileBackedDc(HDC sibling, | 42 bool CreateFileBackedDc(HDC sibling, const RECT* rect, const FilePath& path); |
44 const RECT* rect, | |
45 const FilePath& path); | |
46 | 43 |
47 // Load an EMF file. | 44 // Load an EMF file. |
48 virtual bool CreateFromFile(const FilePath& metafile_path); | 45 bool CreateFromFile(const FilePath& metafile_path); |
49 | 46 |
50 // TODO(maruel): CreateFromFile(). If ever used. Maybe users would like to | 47 // 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 | 48 // have the ability to save web pages to an EMF file? Afterward, it is easy to |
52 // convert to PDF or PS. | 49 // convert to PDF or PS. |
53 | 50 |
54 // Closes the HDC created by CreateDc() and generates the compiled EMF | 51 // Closes the HDC created by CreateDc() and generates the compiled EMF |
55 // data. | 52 // data. |
56 virtual bool CloseDc(); | 53 bool CloseDc(); |
57 | 54 |
58 // Closes the EMF data handle when it is not needed anymore. | 55 // Closes the EMF data handle when it is not needed anymore. |
59 virtual void CloseEmf(); | 56 void CloseEmf(); |
60 | 57 |
61 // "Plays" the EMF buffer in a HDC. It is the same effect as calling the | 58 // "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 | 59 // 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 | 60 // "logical units" and is optional. If |rect| is NULL, the natural EMF bounds |
64 // are used. | 61 // are used. |
65 // Note: Windows has been known to have stack buffer overflow in its GDI | 62 // Note: Windows has been known to have stack buffer overflow in its GDI |
66 // functions, whether used directly or indirectly through precompiled EMF | 63 // 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, | 64 // data. We have to accept the risk here. Since it is used only for printing, |
68 // it requires user intervention. | 65 // it requires user intervention. |
69 virtual bool Playback(HDC hdc, const RECT* rect) const; | 66 bool Playback(HDC hdc, const RECT* rect) const; |
70 | 67 |
71 // The slow version of Playback(). It enumerates all the records and play them | 68 // 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 | 69 // 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 | 70 // issue with some printers. See Emf::Record::SafePlayback implementation for |
74 // details. | 71 // details. |
75 virtual bool SafePlayback(HDC hdc) const; | 72 bool SafePlayback(HDC hdc) const; |
76 | 73 |
77 // Retrieves the bounds of the painted area by this EMF buffer. This value | 74 // 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. | 75 // should be passed to Playback to keep the exact same size. |
79 virtual gfx::Rect GetBounds() const; | 76 gfx::Rect GetBounds() const; |
80 | 77 |
81 // Retrieves the EMF stream size. | 78 // Retrieves the EMF stream size. |
82 virtual uint32 GetDataSize() const; | 79 uint32 GetDataSize() const; |
83 | 80 |
84 // Retrieves the EMF stream. | 81 // Retrieves the EMF stream. |
85 virtual bool GetData(void* buffer, uint32 size) const; | 82 bool GetData(void* buffer, uint32 size) const; |
86 | 83 |
87 // Retrieves the EMF stream. It is an helper function. | 84 // Retrieves the EMF stream. It is an helper function. |
88 virtual bool GetData(std::vector<uint8>* buffer) const; | 85 bool GetData(std::vector<uint8>* buffer) const; |
89 | 86 |
90 virtual HENHMETAFILE emf() const { | 87 HENHMETAFILE emf() const { |
91 return emf_; | 88 return emf_; |
92 } | 89 } |
93 | 90 |
94 virtual HDC hdc() const { | 91 HDC hdc() const { |
95 return hdc_; | 92 return hdc_; |
96 } | 93 } |
97 | 94 |
98 // Inserts a custom GDICOMMENT records indicating StartPage/EndPage calls | 95 // Inserts a custom GDICOMMENT records indicating StartPage/EndPage calls |
99 // (since StartPage and EndPage do not work in a metafile DC). Only valid | 96 // (since StartPage and EndPage do not work in a metafile DC). Only valid |
100 // when hdc_ is non-NULL. | 97 // when hdc_ is non-NULL. |
101 virtual bool StartPage(); | 98 bool StartPage(); |
102 virtual bool EndPage(); | 99 bool EndPage(); |
103 | 100 |
104 // Saves the EMF data to a file as-is. It is recommended to use the .emf file | 101 // 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 | 102 // extension but it is not enforced. This function synchronously writes to the |
106 // file. For testing only. | 103 // file. For testing only. |
107 virtual bool SaveTo(const std::wstring& filename) const; | 104 bool SaveTo(const std::wstring& filename) const; |
108 | |
109 protected: | |
110 Emf(); | |
111 | 105 |
112 private: | 106 private: |
113 friend class NativeMetafileFactory; | |
114 FRIEND_TEST_ALL_PREFIXES(EmfTest, DC); | |
115 FRIEND_TEST_ALL_PREFIXES(EmfTest, FileBackedDC); | |
116 FRIEND_TEST_ALL_PREFIXES(EmfPrintingTest, Enumerate); | |
117 FRIEND_TEST_ALL_PREFIXES(EmfPrintingTest, PageBreak); | |
118 | |
119 // Playbacks safely one EMF record. | 107 // Playbacks safely one EMF record. |
120 static int CALLBACK SafePlaybackProc(HDC hdc, | 108 static int CALLBACK SafePlaybackProc(HDC hdc, |
121 HANDLETABLE* handle_table, | 109 HANDLETABLE* handle_table, |
122 const ENHMETARECORD* record, | 110 const ENHMETARECORD* record, |
123 int objects_count, | 111 int objects_count, |
124 LPARAM param); | 112 LPARAM param); |
125 | 113 |
126 // Compiled EMF data handle. | 114 // Compiled EMF data handle. |
127 HENHMETAFILE emf_; | 115 HENHMETAFILE emf_; |
128 | 116 |
(...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
196 std::vector<Record> items_; | 184 std::vector<Record> items_; |
197 | 185 |
198 EnumerationContext context_; | 186 EnumerationContext context_; |
199 | 187 |
200 DISALLOW_COPY_AND_ASSIGN(Enumerator); | 188 DISALLOW_COPY_AND_ASSIGN(Enumerator); |
201 }; | 189 }; |
202 | 190 |
203 } // namespace printing | 191 } // namespace printing |
204 | 192 |
205 #endif // PRINTING_EMF_WIN_H_ | 193 #endif // PRINTING_EMF_WIN_H_ |
OLD | NEW |