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. 4 5#ifndef PRINTING_METAFILE_H_ 6#define PRINTING_METAFILE_H_ 7 8#include <vector> 9 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" 14 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 22 23namespace base { 24class File; 25} 26 27namespace gfx { 28class Rect; 29class Size; 30} 31 32class SkBaseDevice; 33 34namespace printing { 35 36// This class plays metafiles from data stream (usually PDF or EMF). 37class PRINTING_EXPORT MetafilePlayer { 38 public: 39#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) 69 MetafilePlayer(); 70 virtual ~MetafilePlayer(); 71 72#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. 77 virtual bool SafePlayback(gfx::NativeDrawingContext hdc) const = 0; 78 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, 84 gfx::NativeDrawingContext context, 85 const CGRect rect, 86 const MacRenderPageParams& params) const = 0; 87#endif // if defined(OS_WIN) 88 89 // Saves the underlying data to the given file. This function should ONLY be 90 // called after the metafile is closed. Returns true if writing succeeded. 91 virtual bool SaveTo(base::File* file) const = 0; 92 93 private: 94 DISALLOW_COPY_AND_ASSIGN(MetafilePlayer); 95}; 96 97// This class creates a graphics context that renders into a data stream 98// (usually PDF or EMF). 99class PRINTING_EXPORT Metafile : public MetafilePlayer { 100 public: 101 Metafile(); 102 virtual ~Metafile(); 103 104 // Initializes a fresh new metafile for rendering. Returns false on failure. 105 // Note: It should only be called from within the renderer process to allocate 106 // rendering resources. 107 virtual bool Init() = 0; 108 109 // Initializes the metafile with the data in |src_buffer|. Returns true 110 // on success. 111 // Note: It should only be called from within the browser process. 112 virtual bool InitFromData(const void* src_buffer, uint32 src_buffer_size) = 0; 113 114 // This method calls StartPage and then returns an appropriate 115 // VectorPlatformDevice implementation bound to the context created by 116 // StartPage or NULL on error. 117 virtual SkBaseDevice* StartPageForVectorCanvas(const gfx::Size& page_size, 118 const gfx::Rect& content_area, 119 const float& scale_factor) = 0; 120 121 // Prepares a context for rendering a new page with the given |page_size|, 122 // |content_area| and a |scale_factor| to use for the drawing. The units are 123 // in points (=1/72 in). Returns true on success. 124 virtual bool StartPage(const gfx::Size& page_size, 125 const gfx::Rect& content_area, 126 const float& scale_factor) = 0; 127 128 // Closes the current page and destroys the context used in rendering that 129 // page. The results of current page will be appended into the underlying 130 // data stream. Returns true on success. 131 virtual bool FinishPage() = 0; 132 133 // Closes the metafile. No further rendering is allowed (the current page 134 // is implicitly closed). 135 virtual bool FinishDocument() = 0; 136 137 // Returns the size of the underlying data stream. Only valid after Close() 138 // has been called. 139 virtual uint32 GetDataSize() const = 0; 140 141 // Copies the first |dst_buffer_size| bytes of the underlying data stream into 142 // |dst_buffer|. This function should ONLY be called after Close() is invoked. 143 // Returns true if the copy succeeds. 144 virtual bool GetData(void* dst_buffer, uint32 dst_buffer_size) const = 0; 145 146 virtual gfx::Rect GetPageBounds(unsigned int page_number) const = 0; 147 virtual unsigned int GetPageCount() const = 0; 148 149 virtual gfx::NativeDrawingContext context() const = 0; 150 151#if defined(OS_WIN) 152 // "Plays" the EMF buffer in a HDC. It is the same effect as calling the 153 // original GDI function that were called when recording the EMF. |rect| is in 154 // "logical units" and is optional. If |rect| is NULL, the natural EMF bounds 155 // are used. 156 // Note: Windows has been known to have stack buffer overflow in its GDI 157 // functions, whether used directly or indirectly through precompiled EMF 158 // data. We have to accept the risk here. Since it is used only for printing, 159 // it requires user intervention. 160 virtual bool Playback(gfx::NativeDrawingContext hdc, 161 const RECT* rect) const = 0; 162#endif // OS_WIN 163 164 bool GetDataAsVector(std::vector<char>* buffer) const; 165 166 virtual bool SaveTo(base::File* file) const OVERRIDE; 167 168 private: 169 DISALLOW_COPY_AND_ASSIGN(Metafile); 170}; 171 172} // namespace printing 173 174#endif // PRINTING_METAFILE_H_ 175