blob: 01be9abdc714f0d31a80e5726e4616ed7b38eceb [file] [log] [blame]
[email protected]b5cf844c2012-06-18 21:49:201// Copyright (c) 2012 The Chromium Authors. All rights reserved.
[email protected]0e0fca32009-07-06 15:25:502// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
[email protected]7d7489902011-04-11 21:54:065#ifndef PRINTING_METAFILE_H_
6#define PRINTING_METAFILE_H_
[email protected]0e0fca32009-07-06 15:25:507
avi126e93c2015-12-21 21:48:168#include <stdint.h>
9
vitalybuka5d1290582014-09-12 09:19:5910#include <vector>
11
avi126e93c2015-12-21 21:48:1612#include "base/macros.h"
[email protected]3cfa15ac2009-08-25 17:36:3713#include "build/build_config.h"
Thiago Farina45e319532017-03-23 02:44:4914#include "printing/native_drawing_context.h"
[email protected]69f5b1e62011-09-01 06:34:0415#include "printing/printing_export.h"
[email protected]3cfa15ac2009-08-25 17:36:3716
thestig3109df12017-04-26 21:57:2517#if defined(OS_WIN)
18#include <windows.h>
thestig99fc2132017-04-27 03:37:5419#elif defined(OS_MACOSX)
20#include <ApplicationServices/ApplicationServices.h>
21#include <CoreFoundation/CoreFoundation.h>
22#include "base/mac/scoped_cftyperef.h"
thestig3109df12017-04-26 21:57:2523#endif
24
[email protected]a3ef4832013-02-02 05:12:3325namespace base {
vitalybuka5d1290582014-09-12 09:19:5926class File;
[email protected]a3ef4832013-02-02 05:12:3327}
[email protected]8f17cd3e2011-03-16 01:39:4228
29namespace gfx {
30class Rect;
31class Size;
[email protected]8f17cd3e2011-03-16 01:39:4232}
33
[email protected]8f17cd3e2011-03-16 01:39:4234namespace printing {
35
vitalybuka5d1290582014-09-12 09:19:5936// This class plays metafiles from data stream (usually PDF or EMF).
37class PRINTING_EXPORT MetafilePlayer {
[email protected]8f17cd3e2011-03-16 01:39:4238 public:
thestig99fc2132017-04-27 03:37:5439#if defined(OS_MACOSX)
40 // |shrink_to_fit| specifies whether the output should be shrunk to fit a
41 // destination page if the source PDF is bigger than the destination page in
42 // any dimension. If this is false, parts of the source PDF page that lie
43 // outside the bounds will be clipped.
44 // |stretch_to_fit| specifies whether the output should be stretched to fit
45 // the destination page if the source page size is smaller in all dimensions.
46 // |center_horizontally| specifies whether the output (after any scaling is
47 // done) should be centered horizontally within the destination page.
48 // |center_vertically| specifies whether the output (after any scaling is
49 // done) should be centered vertically within the destination page.
50 // Note that all scaling preserves the original aspect ratio of the page.
51 // |autorotate| specifies whether the source PDF should be autorotated to fit
52 // on the destination page.
53 struct MacRenderPageParams {
54 MacRenderPageParams()
55 : shrink_to_fit(false),
56 stretch_to_fit(false),
57 center_horizontally(false),
58 center_vertically(false),
59 autorotate(false) {
60 }
61
62 bool shrink_to_fit;
63 bool stretch_to_fit;
64 bool center_horizontally;
65 bool center_vertically;
66 bool autorotate;
67 };
68#endif // defined(OS_MACOSX)
vitalybuka5d1290582014-09-12 09:19:5969 MetafilePlayer();
70 virtual ~MetafilePlayer();
[email protected]b5cf844c2012-06-18 21:49:2071
vitalybuka5d1290582014-09-12 09:19:5972#if defined(OS_WIN)
73 // The slow version of Playback(). It enumerates all the records and play them
74 // back in the HDC. The trick is that it skip over the records known to have
75 // issue with some printers. See Emf::Record::SafePlayback implementation for
76 // details.
Nico Weber8e559562017-10-03 01:25:2677 virtual bool SafePlayback(printing::NativeDrawingContext hdc) const = 0;
thestig99fc2132017-04-27 03:37:5478
79#elif defined(OS_MACOSX)
80 // Renders the given page into |rect| in the given context.
81 // Pages use a 1-based index. The rendering uses the arguments in
82 // |params| to determine scaling, translation, and rotation.
83 virtual bool RenderPage(unsigned int page_number,
Nico Weber8e559562017-10-03 01:25:2684 printing::NativeDrawingContext context,
thestig99fc2132017-04-27 03:37:5485 const CGRect rect,
86 const MacRenderPageParams& params) const = 0;
vitalybuka5d1290582014-09-12 09:19:5987#endif // if defined(OS_WIN)
88
skaub7931952016-07-27 18:04:5189 // Populates the buffer with the underlying data. This function should ONLY be
90 // called after the metafile is closed. Returns true if writing succeeded.
91 virtual bool GetDataAsVector(std::vector<char>* buffer) const = 0;
92
vitalybuka5d1290582014-09-12 09:19:5993 // Saves the underlying data to the given file. This function should ONLY be
94 // called after the metafile is closed. Returns true if writing succeeded.
95 virtual bool SaveTo(base::File* file) const = 0;
96
97 private:
98 DISALLOW_COPY_AND_ASSIGN(MetafilePlayer);
99};
100
101// This class creates a graphics context that renders into a data stream
102// (usually PDF or EMF).
103class PRINTING_EXPORT Metafile : public MetafilePlayer {
104 public:
105 Metafile();
dchengd5a0f182014-10-22 00:04:34106 ~Metafile() override;
[email protected]8f17cd3e2011-03-16 01:39:42107
108 // Initializes a fresh new metafile for rendering. Returns false on failure.
109 // Note: It should only be called from within the renderer process to allocate
110 // rendering resources.
111 virtual bool Init() = 0;
112
113 // Initializes the metafile with the data in |src_buffer|. Returns true
114 // on success.
115 // Note: It should only be called from within the browser process.
thestig707a24b22015-09-14 18:16:33116 virtual bool InitFromData(const void* src_buffer,
thestig6b4461f2016-10-28 19:34:30117 size_t src_buffer_size) = 0;
[email protected]8f17cd3e2011-03-16 01:39:42118
[email protected]39892b92011-04-30 02:24:44119 // Prepares a context for rendering a new page with the given |page_size|,
thestig192677ec2016-06-09 07:43:17120 // |content_area| and a |scale_factor| to use for the drawing. The units are
121 // in points (=1/72 in).
122 virtual void StartPage(const gfx::Size& page_size,
[email protected]39892b92011-04-30 02:24:44123 const gfx::Rect& content_area,
[email protected]edc531f92011-03-18 17:52:23124 const float& scale_factor) = 0;
[email protected]8f17cd3e2011-03-16 01:39:42125
126 // Closes the current page and destroys the context used in rendering that
127 // page. The results of current page will be appended into the underlying
128 // data stream. Returns true on success.
129 virtual bool FinishPage() = 0;
130
131 // Closes the metafile. No further rendering is allowed (the current page
132 // is implicitly closed).
[email protected]cdd19f52011-03-19 01:04:57133 virtual bool FinishDocument() = 0;
[email protected]8f17cd3e2011-03-16 01:39:42134
135 // Returns the size of the underlying data stream. Only valid after Close()
136 // has been called.
thestig707a24b22015-09-14 18:16:33137 virtual uint32_t GetDataSize() const = 0;
[email protected]8f17cd3e2011-03-16 01:39:42138
139 // Copies the first |dst_buffer_size| bytes of the underlying data stream into
140 // |dst_buffer|. This function should ONLY be called after Close() is invoked.
141 // Returns true if the copy succeeds.
thestig707a24b22015-09-14 18:16:33142 virtual bool GetData(void* dst_buffer, uint32_t dst_buffer_size) const = 0;
[email protected]8f17cd3e2011-03-16 01:39:42143
[email protected]8f17cd3e2011-03-16 01:39:42144 virtual gfx::Rect GetPageBounds(unsigned int page_number) const = 0;
145 virtual unsigned int GetPageCount() const = 0;
146
Nico Weber8e559562017-10-03 01:25:26147 virtual printing::NativeDrawingContext context() const = 0;
[email protected]8f17cd3e2011-03-16 01:39:42148
thestig3109df12017-04-26 21:57:25149#if defined(OS_WIN)
150 // "Plays" the EMF buffer in a HDC. It is the same effect as calling the
151 // original GDI function that were called when recording the EMF. |rect| is in
152 // "logical units" and is optional. If |rect| is NULL, the natural EMF bounds
153 // are used.
154 // Note: Windows has been known to have stack buffer overflow in its GDI
155 // functions, whether used directly or indirectly through precompiled EMF
156 // data. We have to accept the risk here. Since it is used only for printing,
157 // it requires user intervention.
Nico Weber8e559562017-10-03 01:25:26158 virtual bool Playback(printing::NativeDrawingContext hdc,
thestig3109df12017-04-26 21:57:25159 const RECT* rect) const = 0;
160#endif // OS_WIN
161
skaub7931952016-07-27 18:04:51162 // MetfilePlayer
163 bool GetDataAsVector(std::vector<char>* buffer) const override;
dchengd5a0f182014-10-22 00:04:34164 bool SaveTo(base::File* file) const override;
vitalybuka5d1290582014-09-12 09:19:59165
166 private:
167 DISALLOW_COPY_AND_ASSIGN(Metafile);
[email protected]8f17cd3e2011-03-16 01:39:42168};
169
170} // namespace printing
171
[email protected]7d7489902011-04-11 21:54:06172#endif // PRINTING_METAFILE_H_