Remove selection expansion finch finch flag.
[chromium-blink-merge.git] / printing / emf_win.h
blobdcdfd8c99a46fac5e8a55d5ceacbca9884779649
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.
5 #ifndef PRINTING_EMF_WIN_H_
6 #define PRINTING_EMF_WIN_H_
8 #include <windows.h>
10 #include <vector>
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"
18 namespace base {
19 class FilePath;
22 namespace gfx {
23 class Rect;
24 class Size;
27 namespace printing {
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;
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;
41 // Generates a virtual HDC that will record every GDI commands and compile
42 // it in a EMF data stream.
43 Emf();
44 ~Emf() override;
46 // Closes metafile.
47 void Close();
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);
53 // Initializes the Emf with the data in |metafile_path|.
54 bool InitFromFile(const base::FilePath& metafile_path);
56 // Metafile methods.
57 bool Init() override;
58 bool InitFromData(const void* src_buffer,
59 uint32 src_buffer_size) override;
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 bool StartPage(const gfx::Size& page_size,
66 const gfx::Rect& content_area,
67 const float& scale_factor) override;
68 bool FinishPage() override;
69 bool FinishDocument() override;
71 uint32 GetDataSize() const override;
72 bool GetData(void* buffer, uint32 size) const override;
74 // Should be passed to Playback to keep the exact same size.
75 gfx::Rect GetPageBounds(unsigned int page_number) const override;
77 unsigned int GetPageCount() const override;
78 HDC context() const override;
79 bool Playback(HDC hdc, const RECT* rect) const override;
80 bool SafePlayback(HDC hdc) const override;
82 HENHMETAFILE emf() const { return emf_; }
84 // Returns true if metafile contains alpha blend.
85 bool IsAlphaBlendUsed() const;
87 // Returns new metafile with only bitmap created by playback of the current
88 // metafile. Returns NULL if fails.
89 scoped_ptr<Emf> RasterizeMetafile(int raster_area_in_pixels) const;
91 // Returns new metafile where AlphaBlend replaced by bitmaps. Returns NULL
92 // if fails.
93 scoped_ptr<Emf> RasterizeAlphaBlend() const;
95 private:
96 FRIEND_TEST_ALL_PREFIXES(EmfTest, DC);
97 FRIEND_TEST_ALL_PREFIXES(EmfPrintingTest, PageBreak);
98 FRIEND_TEST_ALL_PREFIXES(EmfTest, FileBackedEmf);
100 // Playbacks safely one EMF record.
101 static int CALLBACK SafePlaybackProc(HDC hdc,
102 HANDLETABLE* handle_table,
103 const ENHMETARECORD* record,
104 int objects_count,
105 LPARAM param);
107 // Compiled EMF data handle.
108 HENHMETAFILE emf_;
110 // Valid when generating EMF data through a virtual HDC.
111 HDC hdc_;
113 DISALLOW_COPY_AND_ASSIGN(Emf);
116 struct Emf::EnumerationContext {
117 EnumerationContext();
119 HANDLETABLE* handle_table;
120 int objects_count;
121 HDC hdc;
122 const XFORM* base_matrix;
123 int dc_on_page_start;
126 // One EMF record. It keeps pointers to the EMF buffer held by Emf::emf_.
127 // The entries become invalid once Emf::CloseEmf() is called.
128 class PRINTING_EXPORT Emf::Record {
129 public:
130 // Plays the record.
131 bool Play(EnumerationContext* context) const;
133 // Plays the record working around quirks with SetLayout,
134 // SetWorldTransform and ModifyWorldTransform. See implementation for details.
135 bool SafePlayback(EnumerationContext* context) const;
137 // Access the underlying EMF record.
138 const ENHMETARECORD* record() const { return record_; }
140 protected:
141 explicit Record(const ENHMETARECORD* record);
143 private:
144 friend class Emf;
145 friend class Enumerator;
146 const ENHMETARECORD* record_;
149 // Retrieves individual records out of a Emf buffer. The main use is to skip
150 // over records that are unsupported on a specific printer or to play back
151 // only a part of an EMF buffer.
152 class PRINTING_EXPORT Emf::Enumerator {
153 public:
154 // Iterator type used for iterating the records.
155 typedef std::vector<Record>::const_iterator const_iterator;
157 // Enumerates the records at construction time. |hdc| and |rect| are
158 // both optional at the same time or must both be valid.
159 // Warning: |emf| must be kept valid for the time this object is alive.
160 Enumerator(const Emf& emf, HDC hdc, const RECT* rect);
162 ~Enumerator();
164 // Retrieves the first Record.
165 const_iterator begin() const;
167 // Retrieves the end of the array.
168 const_iterator end() const;
170 private:
171 FRIEND_TEST_ALL_PREFIXES(EmfPrintingTest, Enumerate);
173 // Processes one EMF record and saves it in the items_ array.
174 static int CALLBACK EnhMetaFileProc(HDC hdc,
175 HANDLETABLE* handle_table,
176 const ENHMETARECORD* record,
177 int objects_count,
178 LPARAM param);
180 // The collection of every EMF records in the currently loaded EMF buffer.
181 // Initialized by Enumerate(). It keeps pointers to the EMF buffer held by
182 // Emf::emf_. The entries become invalid once Emf::CloseEmf() is called.
183 std::vector<Record> items_;
185 EnumerationContext context_;
187 DISALLOW_COPY_AND_ASSIGN(Enumerator);
190 } // namespace printing
192 #endif // PRINTING_EMF_WIN_H_