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_
15 #include "base/compiler_specific.h"
16 #include "base/gtest_prod_util.h"
17 #include "base/macros.h"
18 #include "printing/metafile.h"
31 // Simple wrapper class that manage an EMF data stream and its virtual HDC.
32 class PRINTING_EXPORT Emf : public Metafile {
36 struct EnumerationContext;
38 // Generates a virtual HDC that will record every GDI commands and compile
39 // it in a EMF data stream.
46 // Generates a new metafile that will record every GDI command, and will
47 // be saved to |metafile_path|.
48 bool InitToFile(const base::FilePath& metafile_path);
50 // Initializes the Emf with the data in |metafile_path|.
51 bool InitFromFile(const base::FilePath& metafile_path);
55 bool InitFromData(const void* src_buffer, size_t src_buffer_size) override;
57 // Inserts a custom GDICOMMENT records indicating StartPage/EndPage calls
58 // (since StartPage and EndPage do not work in a metafile DC). Only valid
59 // when hdc_ is non-NULL. |page_size|, |content_area|, and |scale_factor| are
61 void StartPage(const gfx::Size& page_size,
62 const gfx::Rect& content_area,
63 const float& scale_factor) override;
64 bool FinishPage() override;
65 bool FinishDocument() override;
67 uint32_t GetDataSize() const override;
68 bool GetData(void* buffer, uint32_t size) const override;
70 // Should be passed to Playback to keep the exact same size.
71 gfx::Rect GetPageBounds(unsigned int page_number) const override;
73 unsigned int GetPageCount() const override;
74 HDC context() const override;
75 bool Playback(HDC hdc, const RECT* rect) const override;
76 bool SafePlayback(HDC hdc) const override;
78 HENHMETAFILE emf() const { return emf_; }
81 FRIEND_TEST_ALL_PREFIXES(EmfTest, DC);
82 FRIEND_TEST_ALL_PREFIXES(EmfPrintingTest, PageBreak);
83 FRIEND_TEST_ALL_PREFIXES(EmfTest, FileBackedEmf);
85 // Playbacks safely one EMF record.
86 static int CALLBACK SafePlaybackProc(HDC hdc,
87 HANDLETABLE* handle_table,
88 const ENHMETARECORD* record,
92 // Compiled EMF data handle.
95 // Valid when generating EMF data through a virtual HDC.
98 DISALLOW_COPY_AND_ASSIGN(Emf);
101 struct Emf::EnumerationContext {
102 EnumerationContext();
104 HANDLETABLE* handle_table;
107 const XFORM* base_matrix;
108 int dc_on_page_start;
111 // One EMF record. It keeps pointers to the EMF buffer held by Emf::emf_.
112 // The entries become invalid once Emf::CloseEmf() is called.
113 class PRINTING_EXPORT Emf::Record {
116 bool Play(EnumerationContext* context) const;
118 // Plays the record working around quirks with SetLayout,
119 // SetWorldTransform and ModifyWorldTransform. See implementation for details.
120 bool SafePlayback(EnumerationContext* context) const;
122 // Access the underlying EMF record.
123 const ENHMETARECORD* record() const { return record_; }
126 explicit Record(const ENHMETARECORD* record);
130 friend class Enumerator;
131 const ENHMETARECORD* record_;
134 // Retrieves individual records out of a Emf buffer. The main use is to skip
135 // over records that are unsupported on a specific printer or to play back
136 // only a part of an EMF buffer.
137 class PRINTING_EXPORT Emf::Enumerator {
139 // Iterator type used for iterating the records.
140 typedef std::vector<Record>::const_iterator const_iterator;
142 // Enumerates the records at construction time. |hdc| and |rect| are
143 // both optional at the same time or must both be valid.
144 // Warning: |emf| must be kept valid for the time this object is alive.
145 Enumerator(const Emf& emf, HDC hdc, const RECT* rect);
149 // Retrieves the first Record.
150 const_iterator begin() const;
152 // Retrieves the end of the array.
153 const_iterator end() const;
156 FRIEND_TEST_ALL_PREFIXES(EmfPrintingTest, Enumerate);
158 // Processes one EMF record and saves it in the items_ array.
159 static int CALLBACK EnhMetaFileProc(HDC hdc,
160 HANDLETABLE* handle_table,
161 const ENHMETARECORD* record,
165 // The collection of every EMF records in the currently loaded EMF buffer.
166 // Initialized by Enumerate(). It keeps pointers to the EMF buffer held by
167 // Emf::emf_. The entries become invalid once Emf::CloseEmf() is called.
168 std::vector<Record> items_;
170 EnumerationContext context_;
172 DISALLOW_COPY_AND_ASSIGN(Enumerator);
175 } // namespace printing
177 #endif // PRINTING_EMF_WIN_H_