Add ICU message format support
[chromium-blink-merge.git] / printing / metafile.h
blob33ed54d21f28940c12db1a3b566bfeb540250e79
1 // Copyright (c) 2012 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_METAFILE_H_
6 #define PRINTING_METAFILE_H_
8 #include <vector>
10 #include "base/basictypes.h"
11 #include "build/build_config.h"
12 #include "printing/printing_export.h"
13 #include "ui/gfx/native_widget_types.h"
15 #if defined(OS_WIN)
16 #include <windows.h>
17 #elif defined(OS_MACOSX)
18 #include <ApplicationServices/ApplicationServices.h>
19 #include <CoreFoundation/CoreFoundation.h>
20 #include "base/mac/scoped_cftyperef.h"
21 #endif
23 namespace base {
24 class File;
27 namespace gfx {
28 class Rect;
29 class Size;
32 namespace printing {
34 // This class plays metafiles from data stream (usually PDF or EMF).
35 class PRINTING_EXPORT MetafilePlayer {
36 public:
37 #if defined(OS_MACOSX)
38 // |shrink_to_fit| specifies whether the output should be shrunk to fit a
39 // destination page if the source PDF is bigger than the destination page in
40 // any dimension. If this is false, parts of the source PDF page that lie
41 // outside the bounds will be clipped.
42 // |stretch_to_fit| specifies whether the output should be stretched to fit
43 // the destination page if the source page size is smaller in all dimensions.
44 // |center_horizontally| specifies whether the output (after any scaling is
45 // done) should be centered horizontally within the destination page.
46 // |center_vertically| specifies whether the output (after any scaling is
47 // done) should be centered vertically within the destination page.
48 // Note that all scaling preserves the original aspect ratio of the page.
49 // |autorotate| specifies whether the source PDF should be autorotated to fit
50 // on the destination page.
51 struct MacRenderPageParams {
52 MacRenderPageParams()
53 : shrink_to_fit(false),
54 stretch_to_fit(false),
55 center_horizontally(false),
56 center_vertically(false),
57 autorotate(false) {
60 bool shrink_to_fit;
61 bool stretch_to_fit;
62 bool center_horizontally;
63 bool center_vertically;
64 bool autorotate;
66 #endif // defined(OS_MACOSX)
67 MetafilePlayer();
68 virtual ~MetafilePlayer();
70 #if defined(OS_WIN)
71 // The slow version of Playback(). It enumerates all the records and play them
72 // back in the HDC. The trick is that it skip over the records known to have
73 // issue with some printers. See Emf::Record::SafePlayback implementation for
74 // details.
75 virtual bool SafePlayback(gfx::NativeDrawingContext hdc) const = 0;
77 #elif defined(OS_MACOSX)
78 // Renders the given page into |rect| in the given context.
79 // Pages use a 1-based index. The rendering uses the arguments in
80 // |params| to determine scaling, translation, and rotation.
81 virtual bool RenderPage(unsigned int page_number,
82 gfx::NativeDrawingContext context,
83 const CGRect rect,
84 const MacRenderPageParams& params) const = 0;
85 #endif // if defined(OS_WIN)
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(base::File* file) const = 0;
91 private:
92 DISALLOW_COPY_AND_ASSIGN(MetafilePlayer);
95 // This class creates a graphics context that renders into a data stream
96 // (usually PDF or EMF).
97 class PRINTING_EXPORT Metafile : public MetafilePlayer {
98 public:
99 Metafile();
100 ~Metafile() override;
102 // Initializes a fresh new metafile for rendering. Returns false on failure.
103 // Note: It should only be called from within the renderer process to allocate
104 // rendering resources.
105 virtual bool Init() = 0;
107 // Initializes the metafile with the data in |src_buffer|. Returns true
108 // on success.
109 // Note: It should only be called from within the browser process.
110 virtual bool InitFromData(const void* src_buffer, uint32 src_buffer_size) = 0;
112 // Prepares a context for rendering a new page with the given |page_size|,
113 // |content_area| and a |scale_factor| to use for the drawing. The units are
114 // in points (=1/72 in). Returns true on success.
115 virtual bool StartPage(const gfx::Size& page_size,
116 const gfx::Rect& content_area,
117 const float& scale_factor) = 0;
119 // Closes the current page and destroys the context used in rendering that
120 // page. The results of current page will be appended into the underlying
121 // data stream. Returns true on success.
122 virtual bool FinishPage() = 0;
124 // Closes the metafile. No further rendering is allowed (the current page
125 // is implicitly closed).
126 virtual bool FinishDocument() = 0;
128 // Returns the size of the underlying data stream. Only valid after Close()
129 // has been called.
130 virtual uint32 GetDataSize() const = 0;
132 // Copies the first |dst_buffer_size| bytes of the underlying data stream into
133 // |dst_buffer|. This function should ONLY be called after Close() is invoked.
134 // Returns true if the copy succeeds.
135 virtual bool GetData(void* dst_buffer, uint32 dst_buffer_size) const = 0;
137 virtual gfx::Rect GetPageBounds(unsigned int page_number) const = 0;
138 virtual unsigned int GetPageCount() const = 0;
140 virtual gfx::NativeDrawingContext context() const = 0;
142 #if defined(OS_WIN)
143 // "Plays" the EMF buffer in a HDC. It is the same effect as calling the
144 // original GDI function that were called when recording the EMF. |rect| is in
145 // "logical units" and is optional. If |rect| is NULL, the natural EMF bounds
146 // are used.
147 // Note: Windows has been known to have stack buffer overflow in its GDI
148 // functions, whether used directly or indirectly through precompiled EMF
149 // data. We have to accept the risk here. Since it is used only for printing,
150 // it requires user intervention.
151 virtual bool Playback(gfx::NativeDrawingContext hdc,
152 const RECT* rect) const = 0;
153 #endif // OS_WIN
155 bool GetDataAsVector(std::vector<char>* buffer) const;
157 bool SaveTo(base::File* file) const override;
159 private:
160 DISALLOW_COPY_AND_ASSIGN(Metafile);
163 } // namespace printing
165 #endif // PRINTING_METAFILE_H_