blob: f8a05586bf6bcfd855181ee64a58b873045635c5 [file] [log] [blame]
[email protected]e244c6a52010-08-31 15:36:131// Copyright (c) 2010 The Chromium Authors. All rights reserved.
license.botbf09a502008-08-24 00:55:552// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
initial.commit09911bf2008-07-26 23:55:294
[email protected]8ff1d422009-07-07 21:31:395#ifndef PRINTING_EMF_WIN_H_
6#define PRINTING_EMF_WIN_H_
initial.commit09911bf2008-07-26 23:55:297
8#include <windows.h>
9#include <vector>
10
11#include "base/basictypes.h"
12
[email protected]e6cddc572010-09-29 21:39:4513class FilePath;
14
initial.commit09911bf2008-07-26 23:55:2915namespace gfx {
initial.commit09911bf2008-07-26 23:55:2916class Rect;
[email protected]0e0fca32009-07-06 15:25:5017}
18
19namespace printing {
initial.commit09911bf2008-07-26 23:55:2920
21// Simple wrapper class that manage an EMF data stream and its virtual HDC.
22class Emf {
23 public:
24 class Record;
25 class Enumerator;
26 struct EnumerationContext;
27
28 Emf();
29 ~Emf();
30
[email protected]e8980a52010-10-07 20:11:2431 // Initializes the Emf with the data in |src_buffer|. Returns true on success.
32 bool Init(const void* src_buffer, uint32 src_buffer_size);
33
initial.commit09911bf2008-07-26 23:55:2934 // Generates a virtual HDC that will record every GDI commands and compile it
35 // in a EMF data stream.
36 // hdc is used to setup the default DPI and color settings. hdc is optional.
37 // rect specifies the dimensions (in .01-millimeter units) of the EMF. rect is
38 // optional.
39 bool CreateDc(HDC sibling, const RECT* rect);
40
[email protected]e6cddc572010-09-29 21:39:4541 // Similar to the above method but the metafile is backed by a file.
42 bool CreateFileBackedDc(HDC sibling, const RECT* rect, const FilePath& path);
43
[email protected]e6cddc572010-09-29 21:39:4544 // Load an EMF file.
45 bool CreateFromFile(const FilePath& metafile_path);
46
initial.commit09911bf2008-07-26 23:55:2947 // TODO(maruel): CreateFromFile(). If ever used. Maybe users would like to
48 // have the ability to save web pages to an EMF file? Afterward, it is easy to
49 // convert to PDF or PS.
50
51 // Closes the HDC created by CreateDc() and generates the compiled EMF
52 // data.
53 bool CloseDc();
54
55 // Closes the EMF data handle when it is not needed anymore.
56 void CloseEmf();
57
58 // "Plays" the EMF buffer in a HDC. It is the same effect as calling the
59 // original GDI function that were called when recording the EMF. |rect| is in
60 // "logical units" and is optional. If |rect| is NULL, the natural EMF bounds
61 // are used.
62 // Note: Windows has been known to have stack buffer overflow in its GDI
63 // functions, whether used directly or indirectly through precompiled EMF
64 // data. We have to accept the risk here. Since it is used only for printing,
65 // it requires user intervention.
66 bool Playback(HDC hdc, const RECT* rect) const;
67
68 // The slow version of Playback(). It enumerates all the records and play them
69 // back in the HDC. The trick is that it skip over the records known to have
70 // issue with some printers. See Emf::Record::SafePlayback implementation for
71 // details.
72 bool SafePlayback(HDC hdc) const;
73
74 // Retrieves the bounds of the painted area by this EMF buffer. This value
75 // should be passed to Playback to keep the exact same size.
76 gfx::Rect GetBounds() const;
77
78 // Retrieves the EMF stream size.
[email protected]b5ab3982010-02-16 23:58:2779 uint32 GetDataSize() const;
initial.commit09911bf2008-07-26 23:55:2980
81 // Retrieves the EMF stream.
[email protected]b5ab3982010-02-16 23:58:2782 bool GetData(void* buffer, uint32 size) const;
initial.commit09911bf2008-07-26 23:55:2983
84 // Retrieves the EMF stream. It is an helper function.
85 bool GetData(std::vector<uint8>* buffer) const;
86
87 HENHMETAFILE emf() const {
88 return emf_;
89 }
90
91 HDC hdc() const {
92 return hdc_;
93 }
94
[email protected]2aa8e182010-07-12 16:25:0495 // Inserts a custom GDICOMMENT records indicating StartPage/EndPage calls
96 // (since StartPage and EndPage do not work in a metafile DC). Only valid
97 // when hdc_ is non-NULL.
98 bool StartPage();
99 bool EndPage();
100
initial.commit09911bf2008-07-26 23:55:29101 // Saves the EMF data to a file as-is. It is recommended to use the .emf file
102 // extension but it is not enforced. This function synchronously writes to the
103 // file. For testing only.
104 bool SaveTo(const std::wstring& filename) const;
105
106 private:
107 // Playbacks safely one EMF record.
108 static int CALLBACK SafePlaybackProc(HDC hdc,
109 HANDLETABLE* handle_table,
110 const ENHMETARECORD* record,
111 int objects_count,
112 LPARAM param);
113
114 // Compiled EMF data handle.
115 HENHMETAFILE emf_;
116
117 // Valid when generating EMF data through a virtual HDC.
118 HDC hdc_;
119
[email protected]5930cb62009-12-08 02:04:22120 DISALLOW_COPY_AND_ASSIGN(Emf);
initial.commit09911bf2008-07-26 23:55:29121};
122
123struct Emf::EnumerationContext {
124 HANDLETABLE* handle_table;
125 int objects_count;
126 HDC hdc;
127};
128
129// One EMF record. It keeps pointers to the EMF buffer held by Emf::emf_.
130// The entries become invalid once Emf::CloseEmf() is called.
131class Emf::Record {
132 public:
initial.commit09911bf2008-07-26 23:55:29133 // Plays the record.
134 bool Play() const;
135
136 // Plays the record working around quirks with SetLayout,
137 // SetWorldTransform and ModifyWorldTransform. See implementation for details.
138 bool SafePlayback(const XFORM* base_matrix) const;
139
140 // Access the underlying EMF record.
141 const ENHMETARECORD* record() const { return record_; }
142
143 protected:
144 Record(const EnumerationContext* context,
145 const ENHMETARECORD* record);
146
147 private:
148 friend class Emf;
149 friend class Enumerator;
150 const ENHMETARECORD* record_;
151 const EnumerationContext* context_;
152};
153
154// Retrieves individual records out of a Emf buffer. The main use is to skip
155// over records that are unsupported on a specific printer or to play back
156// only a part of an EMF buffer.
157class Emf::Enumerator {
158 public:
159 // Iterator type used for iterating the records.
160 typedef std::vector<Record>::const_iterator const_iterator;
161
162 // Enumerates the records at construction time. |hdc| and |rect| are
163 // both optional at the same time or must both be valid.
164 // Warning: |emf| must be kept valid for the time this object is alive.
165 Enumerator(const Emf& emf, HDC hdc, const RECT* rect);
166
167 // Retrieves the first Record.
168 const_iterator begin() const;
169
170 // Retrieves the end of the array.
171 const_iterator end() const;
172
173 private:
174 // Processes one EMF record and saves it in the items_ array.
175 static int CALLBACK EnhMetaFileProc(HDC hdc,
176 HANDLETABLE* handle_table,
177 const ENHMETARECORD* record,
178 int objects_count,
179 LPARAM param);
180
181 // The collection of every EMF records in the currently loaded EMF buffer.
182 // Initialized by Enumerate(). It keeps pointers to the EMF buffer held by
183 // Emf::emf_. The entries become invalid once Emf::CloseEmf() is called.
184 std::vector<Record> items_;
185
186 EnumerationContext context_;
187
[email protected]5930cb62009-12-08 02:04:22188 DISALLOW_COPY_AND_ASSIGN(Enumerator);
initial.commit09911bf2008-07-26 23:55:29189};
190
[email protected]0e0fca32009-07-06 15:25:50191} // namespace printing
initial.commit09911bf2008-07-26 23:55:29192
[email protected]8ff1d422009-07-07 21:31:39193#endif // PRINTING_EMF_WIN_H_