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