blob: 84c0556c52999e0a2c8b907e7952a25c95b2b004 [file] [log] [blame]
[email protected]5ad76172011-03-02 19:25:171// Copyright (c) 2011 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
[email protected]5ad76172011-03-02 19:25:178#include "base/basictypes.h"
[email protected]3cfa15ac2009-08-25 17:36:379#include "build/build_config.h"
[email protected]8f17cd3e2011-03-16 01:39:4210#include "ui/gfx/native_widget_types.h"
[email protected]3cfa15ac2009-08-25 17:36:3711
[email protected]0e0fca32009-07-06 15:25:5012#if defined(OS_WIN)
[email protected]8f17cd3e2011-03-16 01:39:4213#include <windows.h>
[email protected]0e0fca32009-07-06 15:25:5014#elif defined(OS_MACOSX)
[email protected]8f17cd3e2011-03-16 01:39:4215#include <ApplicationServices/ApplicationServices.h>
16#include <CoreFoundation/CoreFoundation.h>
17#include "base/mac/scoped_cftyperef.h"
[email protected]0e0fca32009-07-06 15:25:5018#endif
19
[email protected]8f17cd3e2011-03-16 01:39:4220class FilePath;
21
22namespace gfx {
[email protected]edc531f92011-03-18 17:52:2323class Point;
[email protected]8f17cd3e2011-03-16 01:39:4224class Rect;
25class Size;
[email protected]8f17cd3e2011-03-16 01:39:4226}
27
[email protected]62f2e802011-05-26 14:28:3528class SkDevice;
[email protected]d2fdcf02011-03-21 22:16:4329
[email protected]8f17cd3e2011-03-16 01:39:4230#if defined(OS_CHROMEOS)
31namespace base {
[email protected]fb4cb372011-05-08 17:20:1132struct FileDescriptor;
[email protected]8f17cd3e2011-03-16 01:39:4233}
34#endif
35
36namespace printing {
37
38// This class creates a graphics context that renders into a data stream
39// (usually PDF or EMF).
[email protected]7d7489902011-04-11 21:54:0640class Metafile {
[email protected]8f17cd3e2011-03-16 01:39:4241 public:
[email protected]7d7489902011-04-11 21:54:0642 virtual ~Metafile() {}
[email protected]8f17cd3e2011-03-16 01:39:4243
44 // Initializes a fresh new metafile for rendering. Returns false on failure.
45 // Note: It should only be called from within the renderer process to allocate
46 // rendering resources.
47 virtual bool Init() = 0;
48
49 // Initializes the metafile with the data in |src_buffer|. Returns true
50 // on success.
51 // Note: It should only be called from within the browser process.
[email protected]89eff96a2011-03-17 23:22:0652 virtual bool InitFromData(const void* src_buffer, uint32 src_buffer_size) = 0;
[email protected]8f17cd3e2011-03-16 01:39:4253
[email protected]d2fdcf02011-03-21 22:16:4354 // This method calls StartPage and then returns an appropriate
55 // VectorPlatformDevice implementation bound to the context created by
[email protected]534c4fb2011-08-02 16:44:2056 // StartPage or NULL on error.
[email protected]62f2e802011-05-26 14:28:3557 virtual SkDevice* StartPageForVectorCanvas(
[email protected]39892b92011-04-30 02:24:4458 const gfx::Size& page_size,
59 const gfx::Rect& content_area,
[email protected]d2fdcf02011-03-21 22:16:4360 const float& scale_factor) = 0;
61
[email protected]39892b92011-04-30 02:24:4462 // Prepares a context for rendering a new page with the given |page_size|,
63 // |content_area| and a |scale_factor| to use for the drawing. The units are
64 // in points (=1/72 in). Returns true on success.
[email protected]edc531f92011-03-18 17:52:2365 virtual bool StartPage(const gfx::Size& page_size,
[email protected]39892b92011-04-30 02:24:4466 const gfx::Rect& content_area,
[email protected]edc531f92011-03-18 17:52:2367 const float& scale_factor) = 0;
[email protected]8f17cd3e2011-03-16 01:39:4268
69 // Closes the current page and destroys the context used in rendering that
70 // page. The results of current page will be appended into the underlying
71 // data stream. Returns true on success.
72 virtual bool FinishPage() = 0;
73
74 // Closes the metafile. No further rendering is allowed (the current page
75 // is implicitly closed).
[email protected]cdd19f52011-03-19 01:04:5776 virtual bool FinishDocument() = 0;
[email protected]8f17cd3e2011-03-16 01:39:4277
78 // Returns the size of the underlying data stream. Only valid after Close()
79 // has been called.
80 virtual uint32 GetDataSize() const = 0;
81
82 // Copies the first |dst_buffer_size| bytes of the underlying data stream into
83 // |dst_buffer|. This function should ONLY be called after Close() is invoked.
84 // Returns true if the copy succeeds.
85 virtual bool GetData(void* dst_buffer, uint32 dst_buffer_size) const = 0;
86
87 // Saves the underlying data to the given file. This function should ONLY be
88 // called after the metafile is closed. Returns true if writing succeeded.
89 virtual bool SaveTo(const FilePath& file_path) const = 0;
90
91 // Returns the bounds of the given page. Pages use a 1-based index.
92 virtual gfx::Rect GetPageBounds(unsigned int page_number) const = 0;
93 virtual unsigned int GetPageCount() const = 0;
94
95 // Get the context for rendering to the PDF.
96 virtual gfx::NativeDrawingContext context() const = 0;
97
98#if defined(OS_WIN)
[email protected]8f17cd3e2011-03-16 01:39:4299 // "Plays" the EMF buffer in a HDC. It is the same effect as calling the
100 // original GDI function that were called when recording the EMF. |rect| is in
101 // "logical units" and is optional. If |rect| is NULL, the natural EMF bounds
102 // are used.
103 // Note: Windows has been known to have stack buffer overflow in its GDI
104 // functions, whether used directly or indirectly through precompiled EMF
105 // data. We have to accept the risk here. Since it is used only for printing,
106 // it requires user intervention.
107 virtual bool Playback(gfx::NativeDrawingContext hdc,
108 const RECT* rect) const = 0;
109
110 // The slow version of Playback(). It enumerates all the records and play them
111 // back in the HDC. The trick is that it skip over the records known to have
112 // issue with some printers. See Emf::Record::SafePlayback implementation for
113 // details.
114 virtual bool SafePlayback(gfx::NativeDrawingContext hdc) const = 0;
115
[email protected]8f17cd3e2011-03-16 01:39:42116 virtual HENHMETAFILE emf() const = 0;
117#elif defined(OS_MACOSX)
118 // Renders the given page into |rect| in the given context.
119 // Pages use a 1-based index. The rendering uses the following arguments
120 // to determine scaling and translation factors.
121 // |shrink_to_fit| specifies whether the output should be shrunk to fit the
122 // supplied |rect| if the page size is larger than |rect| in any dimension.
123 // If this is false, parts of the PDF page that lie outside the bounds will be
124 // clipped.
125 // |stretch_to_fit| specifies whether the output should be stretched to fit
126 // the supplied bounds if the page size is smaller than |rect| in all
127 // dimensions.
128 // |center_horizontally| specifies whether the final image (after any scaling
129 // is done) should be centered horizontally within the given |rect|.
130 // |center_vertically| specifies whether the final image (after any scaling
131 // is done) should be centered vertically within the given |rect|.
132 // Note that all scaling preserves the original aspect ratio of the page.
133 virtual bool RenderPage(unsigned int page_number,
134 gfx::NativeDrawingContext context,
135 const CGRect rect,
136 bool shrink_to_fit,
137 bool stretch_to_fit,
138 bool center_horizontally,
139 bool center_vertically) const = 0;
[email protected]09ab8912011-03-23 15:33:24140#elif defined(OS_CHROMEOS)
[email protected]8f17cd3e2011-03-16 01:39:42141 // Saves the underlying data to the file associated with fd. This function
142 // should ONLY be called after the metafile is closed.
143 // Returns true if writing succeeded.
144 virtual bool SaveToFD(const base::FileDescriptor& fd) const = 0;
145#endif // if defined(OS_CHROMEOS)
[email protected]8f17cd3e2011-03-16 01:39:42146};
147
148} // namespace printing
149
[email protected]7d7489902011-04-11 21:54:06150#endif // PRINTING_METAFILE_H_