Supervised users: Re-check ManagementPolicy when ProfileIsSupervised changes.
[chromium-blink-merge.git] / printing / emf_win.h
blob1da9364c0a12566b3db244fefbc0796df0c07351
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 virtual ~Emf();
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 virtual bool Init() override;
58 virtual 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 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;
71 virtual uint32 GetDataSize() const override;
72 virtual bool GetData(void* buffer, uint32 size) const override;
74 // Should be passed to Playback to keep the exact same size.
75 virtual gfx::Rect GetPageBounds(unsigned int page_number) const override;
77 virtual unsigned int GetPageCount() const override { return 1; }
79 virtual HDC context() const override {
80 return hdc_;
83 virtual bool Playback(HDC hdc, const RECT* rect) const override;
84 virtual bool SafePlayback(HDC hdc) const override;
86 HENHMETAFILE emf() const { return emf_; }
88 // Returns true if metafile contains alpha blend.
89 bool IsAlphaBlendUsed() const;
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;
95 // Returns new metafile where AlphaBlend replaced by bitmaps. Returns NULL
96 // if fails.
97 scoped_ptr<Emf> RasterizeAlphaBlend() const;
99 private:
100 FRIEND_TEST_ALL_PREFIXES(EmfTest, DC);
101 FRIEND_TEST_ALL_PREFIXES(EmfPrintingTest, PageBreak);
102 FRIEND_TEST_ALL_PREFIXES(EmfTest, FileBackedEmf);
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);
111 // Compiled EMF data handle.
112 HENHMETAFILE emf_;
114 // Valid when generating EMF data through a virtual HDC.
115 HDC hdc_;
117 DISALLOW_COPY_AND_ASSIGN(Emf);
120 struct Emf::EnumerationContext {
121 EnumerationContext();
123 HANDLETABLE* handle_table;
124 int objects_count;
125 HDC hdc;
126 const XFORM* base_matrix;
127 int dc_on_page_start;
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;
137 // Plays the record working around quirks with SetLayout,
138 // SetWorldTransform and ModifyWorldTransform. See implementation for details.
139 bool SafePlayback(EnumerationContext* context) const;
141 // Access the underlying EMF record.
142 const ENHMETARECORD* record() const { return record_; }
144 protected:
145 explicit Record(const ENHMETARECORD* record);
147 private:
148 friend class Emf;
149 friend class Enumerator;
150 const ENHMETARECORD* record_;
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;
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);
166 // Retrieves the first Record.
167 const_iterator begin() const;
169 // Retrieves the end of the array.
170 const_iterator end() const;
172 private:
173 FRIEND_TEST_ALL_PREFIXES(EmfPrintingTest, Enumerate);
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);
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_;
187 EnumerationContext context_;
189 DISALLOW_COPY_AND_ASSIGN(Enumerator);
192 } // namespace printing
194 #endif // PRINTING_EMF_WIN_H_