1 // Copyright (c) 2011 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_
12 #include "base/basictypes.h"
13 #include "base/compiler_specific.h"
14 #include "base/gtest_prod_util.h"
15 #include "printing/metafile.h"
26 // Simple wrapper class that manage an EMF data stream and its virtual HDC.
27 class PRINTING_EXPORT Emf
: public Metafile
{
31 struct EnumerationContext
;
33 // Generates a virtual HDC that will record every GDI commands and compile
34 // it in a EMF data stream.
38 // Generates a new metafile that will record every GDI command, and will
39 // be saved to |metafile_path|.
40 virtual bool InitToFile(const FilePath
& metafile_path
);
42 // Initializes the Emf with the data in |metafile_path|.
43 virtual bool InitFromFile(const FilePath
& metafile_path
);
46 virtual bool Init() OVERRIDE
;
47 virtual bool InitFromData(const void* src_buffer
,
48 uint32 src_buffer_size
) OVERRIDE
;
50 virtual SkDevice
* StartPageForVectorCanvas(
51 const gfx::Size
& page_size
, const gfx::Rect
& content_area
,
52 const float& scale_factor
) OVERRIDE
;
53 // Inserts a custom GDICOMMENT records indicating StartPage/EndPage calls
54 // (since StartPage and EndPage do not work in a metafile DC). Only valid
55 // when hdc_ is non-NULL. |page_size|, |content_area|, and |scale_factor| are
57 virtual bool StartPage(const gfx::Size
& page_size
,
58 const gfx::Rect
& content_area
,
59 const float& scale_factor
) OVERRIDE
;
60 virtual bool FinishPage() OVERRIDE
;
61 virtual bool FinishDocument() OVERRIDE
;
63 virtual uint32
GetDataSize() const OVERRIDE
;
64 virtual bool GetData(void* buffer
, uint32 size
) const OVERRIDE
;
66 // Saves the EMF data to a file as-is. It is recommended to use the .emf file
67 // extension but it is not enforced. This function synchronously writes to the
68 // file. For testing only.
69 virtual bool SaveTo(const FilePath
& file_path
) const OVERRIDE
;
71 // Should be passed to Playback to keep the exact same size.
72 virtual gfx::Rect
GetPageBounds(unsigned int page_number
) const OVERRIDE
;
74 virtual unsigned int GetPageCount() const OVERRIDE
{
78 virtual HDC
context() const OVERRIDE
{
82 virtual bool Playback(HDC hdc
, const RECT
* rect
) const OVERRIDE
;
83 virtual bool SafePlayback(HDC hdc
) const OVERRIDE
;
85 virtual HENHMETAFILE
emf() const OVERRIDE
{
90 FRIEND_TEST_ALL_PREFIXES(EmfTest
, DC
);
91 FRIEND_TEST_ALL_PREFIXES(EmfPrintingTest
, PageBreak
);
92 FRIEND_TEST_ALL_PREFIXES(EmfTest
, FileBackedEmf
);
94 // Retrieves the underlying data stream. It is a helper function.
95 bool GetDataAsVector(std::vector
<uint8
>* buffer
) const;
97 // Playbacks safely one EMF record.
98 static int CALLBACK
SafePlaybackProc(HDC hdc
,
99 HANDLETABLE
* handle_table
,
100 const ENHMETARECORD
* record
,
104 // Compiled EMF data handle.
107 // Valid when generating EMF data through a virtual HDC.
112 DISALLOW_COPY_AND_ASSIGN(Emf
);
115 struct Emf::EnumerationContext
{
116 HANDLETABLE
* handle_table
;
121 // One EMF record. It keeps pointers to the EMF buffer held by Emf::emf_.
122 // The entries become invalid once Emf::CloseEmf() is called.
123 class PRINTING_EXPORT
Emf::Record
{
128 // Plays the record working around quirks with SetLayout,
129 // SetWorldTransform and ModifyWorldTransform. See implementation for details.
130 bool SafePlayback(const XFORM
* base_matrix
) const;
132 // Access the underlying EMF record.
133 const ENHMETARECORD
* record() const { return record_
; }
136 Record(const EnumerationContext
* context
,
137 const ENHMETARECORD
* record
);
141 friend class Enumerator
;
142 const ENHMETARECORD
* record_
;
143 const EnumerationContext
* context_
;
146 // Retrieves individual records out of a Emf buffer. The main use is to skip
147 // over records that are unsupported on a specific printer or to play back
148 // only a part of an EMF buffer.
149 class PRINTING_EXPORT
Emf::Enumerator
{
151 // Iterator type used for iterating the records.
152 typedef std::vector
<Record
>::const_iterator const_iterator
;
154 // Enumerates the records at construction time. |hdc| and |rect| are
155 // both optional at the same time or must both be valid.
156 // Warning: |emf| must be kept valid for the time this object is alive.
157 Enumerator(const Emf
& emf
, HDC hdc
, const RECT
* rect
);
159 // Retrieves the first Record.
160 const_iterator
begin() const;
162 // Retrieves the end of the array.
163 const_iterator
end() const;
166 // Processes one EMF record and saves it in the items_ array.
167 static int CALLBACK
EnhMetaFileProc(HDC hdc
,
168 HANDLETABLE
* handle_table
,
169 const ENHMETARECORD
* record
,
173 // The collection of every EMF records in the currently loaded EMF buffer.
174 // Initialized by Enumerate(). It keeps pointers to the EMF buffer held by
175 // Emf::emf_. The entries become invalid once Emf::CloseEmf() is called.
176 std::vector
<Record
> items_
;
178 EnumerationContext context_
;
180 DISALLOW_COPY_AND_ASSIGN(Enumerator
);
183 } // namespace printing
185 #endif // PRINTING_EMF_WIN_H_