blob: 99eb1853df78c677aadc7db985702bd3f2856879 [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]69f5b1e62011-09-01 06:34:0410#include "printing/printing_export.h"
[email protected]8f17cd3e2011-03-16 01:39:4211#include "ui/gfx/native_widget_types.h"
[email protected]3cfa15ac2009-08-25 17:36:3712
[email protected]0e0fca32009-07-06 15:25:5013#if defined(OS_WIN)
[email protected]8f17cd3e2011-03-16 01:39:4214#include <windows.h>
[email protected]0e0fca32009-07-06 15:25:5015#elif defined(OS_MACOSX)
[email protected]8f17cd3e2011-03-16 01:39:4216#include <ApplicationServices/ApplicationServices.h>
17#include <CoreFoundation/CoreFoundation.h>
18#include "base/mac/scoped_cftyperef.h"
[email protected]0e0fca32009-07-06 15:25:5019#endif
20
[email protected]8f17cd3e2011-03-16 01:39:4221class FilePath;
22
23namespace gfx {
[email protected]edc531f92011-03-18 17:52:2324class Point;
[email protected]8f17cd3e2011-03-16 01:39:4225class Rect;
26class Size;
[email protected]8f17cd3e2011-03-16 01:39:4227}
28
[email protected]62f2e802011-05-26 14:28:3529class SkDevice;
[email protected]d2fdcf02011-03-21 22:16:4330
[email protected]8f17cd3e2011-03-16 01:39:4231#if defined(OS_CHROMEOS)
32namespace base {
[email protected]fb4cb372011-05-08 17:20:1133struct FileDescriptor;
[email protected]8f17cd3e2011-03-16 01:39:4234}
35#endif
36
37namespace printing {
38
39// This class creates a graphics context that renders into a data stream
40// (usually PDF or EMF).
[email protected]69f5b1e62011-09-01 06:34:0441class PRINTING_EXPORT Metafile {
[email protected]8f17cd3e2011-03-16 01:39:4242 public:
[email protected]7d7489902011-04-11 21:54:0643 virtual ~Metafile() {}
[email protected]8f17cd3e2011-03-16 01:39:4244
45 // Initializes a fresh new metafile for rendering. Returns false on failure.
46 // Note: It should only be called from within the renderer process to allocate
47 // rendering resources.
48 virtual bool Init() = 0;
49
50 // Initializes the metafile with the data in |src_buffer|. Returns true
51 // on success.
52 // Note: It should only be called from within the browser process.
[email protected]89eff96a2011-03-17 23:22:0653 virtual bool InitFromData(const void* src_buffer, uint32 src_buffer_size) = 0;
[email protected]8f17cd3e2011-03-16 01:39:4254
[email protected]d2fdcf02011-03-21 22:16:4355 // This method calls StartPage and then returns an appropriate
56 // VectorPlatformDevice implementation bound to the context created by
[email protected]534c4fb2011-08-02 16:44:2057 // StartPage or NULL on error.
[email protected]62f2e802011-05-26 14:28:3558 virtual SkDevice* StartPageForVectorCanvas(
[email protected]39892b92011-04-30 02:24:4459 const gfx::Size& page_size,
60 const gfx::Rect& content_area,
[email protected]d2fdcf02011-03-21 22:16:4361 const float& scale_factor) = 0;
62
[email protected]39892b92011-04-30 02:24:4463 // Prepares a context for rendering a new page with the given |page_size|,
64 // |content_area| and a |scale_factor| to use for the drawing. The units are
65 // in points (=1/72 in). Returns true on success.
[email protected]edc531f92011-03-18 17:52:2366 virtual bool StartPage(const gfx::Size& page_size,
[email protected]39892b92011-04-30 02:24:4467 const gfx::Rect& content_area,
[email protected]edc531f92011-03-18 17:52:2368 const float& scale_factor) = 0;
[email protected]8f17cd3e2011-03-16 01:39:4269
70 // Closes the current page and destroys the context used in rendering that
71 // page. The results of current page will be appended into the underlying
72 // data stream. Returns true on success.
73 virtual bool FinishPage() = 0;
74
75 // Closes the metafile. No further rendering is allowed (the current page
76 // is implicitly closed).
[email protected]cdd19f52011-03-19 01:04:5777 virtual bool FinishDocument() = 0;
[email protected]8f17cd3e2011-03-16 01:39:4278
79 // Returns the size of the underlying data stream. Only valid after Close()
80 // has been called.
81 virtual uint32 GetDataSize() const = 0;
82
83 // Copies the first |dst_buffer_size| bytes of the underlying data stream into
84 // |dst_buffer|. This function should ONLY be called after Close() is invoked.
85 // Returns true if the copy succeeds.
86 virtual bool GetData(void* dst_buffer, uint32 dst_buffer_size) const = 0;
87
88 // Saves the underlying data to the given file. This function should ONLY be
89 // called after the metafile is closed. Returns true if writing succeeded.
90 virtual bool SaveTo(const FilePath& file_path) const = 0;
91
92 // Returns the bounds of the given page. Pages use a 1-based index.
93 virtual gfx::Rect GetPageBounds(unsigned int page_number) const = 0;
94 virtual unsigned int GetPageCount() const = 0;
95
96 // Get the context for rendering to the PDF.
97 virtual gfx::NativeDrawingContext context() const = 0;
98
99#if defined(OS_WIN)
[email protected]8f17cd3e2011-03-16 01:39:42100 // "Plays" the EMF buffer in a HDC. It is the same effect as calling the
101 // original GDI function that were called when recording the EMF. |rect| is in
102 // "logical units" and is optional. If |rect| is NULL, the natural EMF bounds
103 // are used.
104 // Note: Windows has been known to have stack buffer overflow in its GDI
105 // functions, whether used directly or indirectly through precompiled EMF
106 // data. We have to accept the risk here. Since it is used only for printing,
107 // it requires user intervention.
108 virtual bool Playback(gfx::NativeDrawingContext hdc,
109 const RECT* rect) const = 0;
110
111 // The slow version of Playback(). It enumerates all the records and play them
112 // back in the HDC. The trick is that it skip over the records known to have
113 // issue with some printers. See Emf::Record::SafePlayback implementation for
114 // details.
115 virtual bool SafePlayback(gfx::NativeDrawingContext hdc) const = 0;
116
[email protected]8f17cd3e2011-03-16 01:39:42117 virtual HENHMETAFILE emf() const = 0;
118#elif defined(OS_MACOSX)
119 // Renders the given page into |rect| in the given context.
120 // Pages use a 1-based index. The rendering uses the following arguments
121 // to determine scaling and translation factors.
122 // |shrink_to_fit| specifies whether the output should be shrunk to fit the
123 // supplied |rect| if the page size is larger than |rect| in any dimension.
124 // If this is false, parts of the PDF page that lie outside the bounds will be
125 // clipped.
126 // |stretch_to_fit| specifies whether the output should be stretched to fit
127 // the supplied bounds if the page size is smaller than |rect| in all
128 // dimensions.
129 // |center_horizontally| specifies whether the final image (after any scaling
130 // is done) should be centered horizontally within the given |rect|.
131 // |center_vertically| specifies whether the final image (after any scaling
132 // is done) should be centered vertically within the given |rect|.
133 // Note that all scaling preserves the original aspect ratio of the page.
134 virtual bool RenderPage(unsigned int page_number,
135 gfx::NativeDrawingContext context,
136 const CGRect rect,
137 bool shrink_to_fit,
138 bool stretch_to_fit,
139 bool center_horizontally,
140 bool center_vertically) const = 0;
[email protected]09ab8912011-03-23 15:33:24141#elif defined(OS_CHROMEOS)
[email protected]8f17cd3e2011-03-16 01:39:42142 // Saves the underlying data to the file associated with fd. This function
143 // should ONLY be called after the metafile is closed.
144 // Returns true if writing succeeded.
145 virtual bool SaveToFD(const base::FileDescriptor& fd) const = 0;
146#endif // if defined(OS_CHROMEOS)
[email protected]8f17cd3e2011-03-16 01:39:42147};
148
149} // namespace printing
150
[email protected]7d7489902011-04-11 21:54:06151#endif // PRINTING_METAFILE_H_