Update To 11.40.268.0
[platform/framework/web/crosswalk.git] / src / printing / emf_win.h
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #ifndef PRINTING_EMF_WIN_H_
6 #define PRINTING_EMF_WIN_H_
7
8 #include <windows.h>
9
10 #include <vector>
11
12 #include "base/basictypes.h"
13 #include "base/compiler_specific.h"
14 #include "base/gtest_prod_util.h"
15 #include "base/memory/scoped_ptr.h"
16 #include "printing/metafile.h"
17
18 namespace base {
19 class FilePath;
20 }
21
22 namespace gfx {
23 class Rect;
24 class Size;
25 }
26
27 namespace printing {
28
29 // http://msdn2.microsoft.com/en-us/library/ms535522.aspx
30 // Windows 2000/XP: When a page in a spooled file exceeds approximately 350
31 // MB, it can fail to print and not send an error message.
32 const size_t kMetafileMaxSize = 350*1024*1024;
33
34 // Simple wrapper class that manage an EMF data stream and its virtual HDC.
35 class PRINTING_EXPORT Emf : public Metafile {
36  public:
37   class Record;
38   class Enumerator;
39   struct EnumerationContext;
40
41   // Generates a virtual HDC that will record every GDI commands and compile
42   // it in a EMF data stream.
43   Emf();
44   virtual ~Emf();
45
46   // Closes metafile.
47   void Close();
48
49   // Generates a new metafile that will record every GDI command, and will
50   // be saved to |metafile_path|.
51   bool InitToFile(const base::FilePath& metafile_path);
52
53   // Initializes the Emf with the data in |metafile_path|.
54   bool InitFromFile(const base::FilePath& metafile_path);
55
56   // Metafile methods.
57   virtual bool Init() override;
58   virtual bool InitFromData(const void* src_buffer,
59                             uint32 src_buffer_size) override;
60
61   // Inserts a custom GDICOMMENT records indicating StartPage/EndPage calls
62   // (since StartPage and EndPage do not work in a metafile DC). Only valid
63   // when hdc_ is non-NULL. |page_size|, |content_area|, and |scale_factor| are
64   // ignored.
65   virtual bool StartPage(const gfx::Size& page_size,
66                          const gfx::Rect& content_area,
67                          const float& scale_factor) override;
68   virtual bool FinishPage() override;
69   virtual bool FinishDocument() override;
70
71   virtual uint32 GetDataSize() const override;
72   virtual bool GetData(void* buffer, uint32 size) const override;
73
74   // Should be passed to Playback to keep the exact same size.
75   virtual gfx::Rect GetPageBounds(unsigned int page_number) const override;
76
77   virtual unsigned int GetPageCount() const override { return 1; }
78
79   virtual HDC context() const override {
80     return hdc_;
81   }
82
83   virtual bool Playback(HDC hdc, const RECT* rect) const override;
84   virtual bool SafePlayback(HDC hdc) const override;
85
86   HENHMETAFILE emf() const { return emf_; }
87
88   // Returns true if metafile contains alpha blend.
89   bool IsAlphaBlendUsed() const;
90
91   // Returns new metafile with only bitmap created by playback of the current
92   // metafile. Returns NULL if fails.
93   scoped_ptr<Emf> RasterizeMetafile(int raster_area_in_pixels) const;
94
95   // Returns new metafile where AlphaBlend replaced by bitmaps. Returns NULL
96   // if fails.
97   scoped_ptr<Emf> RasterizeAlphaBlend() const;
98
99  private:
100   FRIEND_TEST_ALL_PREFIXES(EmfTest, DC);
101   FRIEND_TEST_ALL_PREFIXES(EmfPrintingTest, PageBreak);
102   FRIEND_TEST_ALL_PREFIXES(EmfTest, FileBackedEmf);
103
104   // Playbacks safely one EMF record.
105   static int CALLBACK SafePlaybackProc(HDC hdc,
106                                        HANDLETABLE* handle_table,
107                                        const ENHMETARECORD* record,
108                                        int objects_count,
109                                        LPARAM param);
110
111   // Compiled EMF data handle.
112   HENHMETAFILE emf_;
113
114   // Valid when generating EMF data through a virtual HDC.
115   HDC hdc_;
116
117   DISALLOW_COPY_AND_ASSIGN(Emf);
118 };
119
120 struct Emf::EnumerationContext {
121   EnumerationContext();
122
123   HANDLETABLE* handle_table;
124   int objects_count;
125   HDC hdc;
126   const XFORM* base_matrix;
127   int dc_on_page_start;
128 };
129
130 // One EMF record. It keeps pointers to the EMF buffer held by Emf::emf_.
131 // The entries become invalid once Emf::CloseEmf() is called.
132 class PRINTING_EXPORT Emf::Record {
133  public:
134   // Plays the record.
135   bool Play(EnumerationContext* context) const;
136
137   // Plays the record working around quirks with SetLayout,
138   // SetWorldTransform and ModifyWorldTransform. See implementation for details.
139   bool SafePlayback(EnumerationContext* context) const;
140
141   // Access the underlying EMF record.
142   const ENHMETARECORD* record() const { return record_; }
143
144  protected:
145   explicit Record(const ENHMETARECORD* record);
146
147  private:
148   friend class Emf;
149   friend class Enumerator;
150   const ENHMETARECORD* record_;
151 };
152
153 // Retrieves individual records out of a Emf buffer. The main use is to skip
154 // over records that are unsupported on a specific printer or to play back
155 // only a part of an EMF buffer.
156 class PRINTING_EXPORT Emf::Enumerator {
157  public:
158   // Iterator type used for iterating the records.
159   typedef std::vector<Record>::const_iterator const_iterator;
160
161   // Enumerates the records at construction time. |hdc| and |rect| are
162   // both optional at the same time or must both be valid.
163   // Warning: |emf| must be kept valid for the time this object is alive.
164   Enumerator(const Emf& emf, HDC hdc, const RECT* rect);
165
166   // Retrieves the first Record.
167   const_iterator begin() const;
168
169   // Retrieves the end of the array.
170   const_iterator end() const;
171
172  private:
173   FRIEND_TEST_ALL_PREFIXES(EmfPrintingTest, Enumerate);
174
175   // Processes one EMF record and saves it in the items_ array.
176   static int CALLBACK EnhMetaFileProc(HDC hdc,
177                                       HANDLETABLE* handle_table,
178                                       const ENHMETARECORD* record,
179                                       int objects_count,
180                                       LPARAM param);
181
182   // The collection of every EMF records in the currently loaded EMF buffer.
183   // Initialized by Enumerate(). It keeps pointers to the EMF buffer held by
184   // Emf::emf_. The entries become invalid once Emf::CloseEmf() is called.
185   std::vector<Record> items_;
186
187   EnumerationContext context_;
188
189   DISALLOW_COPY_AND_ASSIGN(Enumerator);
190 };
191
192 }  // namespace printing
193
194 #endif  // PRINTING_EMF_WIN_H_