Chromium Code Reviews| Index: printing/native_metafile.h |
| diff --git a/printing/native_metafile.h b/printing/native_metafile.h |
| index 7e1d25bf6623c0e7cc0fa17e10d4eae524fcf1b2..91c7dc143977ad269518384579bf351cea016361 100644 |
| --- a/printing/native_metafile.h |
| +++ b/printing/native_metafile.h |
| @@ -6,14 +6,180 @@ |
| #define PRINTING_NATIVE_METAFILE_H_ |
| #include "base/basictypes.h" |
| -#include "build/build_config.h" |
| #if defined(OS_WIN) |
| -#include "printing/native_metafile_win.h" |
| +#include <windows.h> |
| +#include <vector> |
| #elif defined(OS_MACOSX) |
| -#include "printing/native_metafile_mac.h" |
| +#include <ApplicationServices/ApplicationServices.h> |
| +#include <CoreFoundation/CoreFoundation.h> |
| +#include "base/mac/scoped_cftyperef.h" |
| +#endif |
| + |
| +#if defined(OS_WIN) |
| +namespace gfx { |
| +class Rect; |
| +} |
| +#elif defined(OS_MACOSX) |
| +namespace gfx { |
| +class Rect; |
| +class Size; |
| +class Point; |
| +} |
| +#elif defined(OS_POSIX) |
| +typedef struct _cairo_surface cairo_surface_t; |
| +typedef struct _cairo cairo_t; |
| +#endif |
| + |
| +class FilePath; |
| + |
| +namespace printing { |
| + |
| +// This class creates a graphics context that renders into a PDF data stream. |
| +class NativeMetafile { |
| + public: |
| + virtual ~NativeMetafile() {}; |
|
Lei Zhang
2011/03/04 12:02:36
nit: no ; needed.
dpapad
2011/03/04 19:19:43
Done.
|
| + |
| + // Initializes a fresh new metafile, and returns a drawing context for |
| + // rendering into the PDF. Returns false on failure. |
|
Lei Zhang
2011/03/04 12:02:36
Comment is wrong. You don't return a context here.
dpapad
2011/03/04 19:19:43
Done.
|
| + // Note: It should only be called from within the renderer process to allocate |
| + // rendering resources. |
| + virtual bool Init() = 0; |
| + |
| + // Initializes the metafile with the data in |src_buffer|. Returns true |
| + // on success. |
| + // Note: It should only be called from within the browser process. |
|
Lei Zhang
2011/03/04 12:02:36
nit: 2 spaces after 'be'.
dpapad
2011/03/04 19:19:43
Done.
|
| + virtual bool Init(const void* src_buffer, uint32 src_buffer_size) = 0; |
| + |
| + // Returns the size of the underlying PDF data. Only valid after Close() has |
| + // been called. |
| + virtual uint32 GetDataSize() const = 0; |
| + |
| + // Copies the first |dst_buffer_size| bytes of the PDF steam into |
| + // |dst_buffer|. This function should ONLY be called after Close() is invoked. |
|
Lei Zhang
2011/03/04 12:02:36
s/steam/stream/
dpapad
2011/03/04 19:19:43
Done.
|
| + // Returns true if the copy succeeds. |
| + virtual bool GetData(void* dst_buffer, uint32 dst_buffer_size) const = 0; |
| + |
| + // Closes the current page and destroys the context used in rendering that |
| + // page. The results of current page will be appended into the underlying PDF |
| + // data. Returns true on success. |
| + virtual bool FinishPage() = 0; |
| + |
| + // Closes the PDF file. No further rendering is allowed. |
| + virtual void Close() = 0; |
| + |
| + // Saves the raw PDF data to the given file. This function should ONLY be |
| + // called after PDF file is closed. For testing only. |
| + // Returns true if writing succeeded. |
| + virtual bool SaveTo(const FilePath& file_path) const = 0; |
| + |
| +#if defined(OS_WIN)||defined(OS_MACOSX) |
| + // Returns the bounds of the given page. Pages use a 1-based index. |
| + virtual gfx::Rect GetPageBounds(unsigned int page_number) const = 0; |
|
Lei Zhang
2011/03/04 12:02:36
Not sure if this makes sense when the Windows impl
dpapad
2011/03/04 19:19:43
I had the same concern. It will make more sense la
|
| + virtual unsigned int GetPageCount() const = 0; |
| +#endif |
| + |
| +#if defined(OS_WIN) |
| + |
|
Lei Zhang
2011/03/04 12:02:36
nit: remove blank line
dpapad
2011/03/04 19:19:43
Done.
|
| + // Generates a virtual HDC that will record every GDI commands and compile it |
| + // in a EMF data stream. |
| + // hdc is used to setup the default DPI and color settings. hdc is optional. |
| + // rect specifies the dimensions (in .01-millimeter units) of the EMF. rect is |
| + // optional. |
| + virtual bool CreateDc(HDC sibling, const RECT* rect) = 0; |
| + |
| + // Similar to the above method but the metafile is backed by a file. |
| + virtual bool CreateFileBackedDc(HDC sibling, |
| + const RECT* rect, |
| + const FilePath& path) = 0; |
| + |
| + // TODO(maruel): CreateFromFile(). If ever used. Maybe users would like to |
| + // have the ability to save web pages to an EMF file? Afterward, it is easy to |
| + // convert to PDF or PS. |
| + // Load an EMF file. |
| + virtual bool CreateFromFile(const FilePath& metafile_path) = 0; |
| + |
| + // Closes the HDC created by CreateDc() and generates the compiled EMF |
| + // data. |
| + virtual bool CloseDc() = 0; |
| + |
| + // "Plays" the EMF buffer in a HDC. It is the same effect as calling the |
| + // original GDI function that were called when recording the EMF. |rect| is in |
| + // "logical units" and is optional. If |rect| is NULL, the natural EMF bounds |
| + // are used. |
| + // Note: Windows has been known to have stack buffer overflow in its GDI |
| + // functions, whether used directly or indirectly through precompiled EMF |
| + // data. We have to accept the risk here. Since it is used only for printing, |
| + // it requires user intervention. |
| + virtual bool Playback(HDC hdc, const RECT* rect) const = 0; |
| + |
| + // The slow version of Playback(). It enumerates all the records and play them |
| + // back in the HDC. The trick is that it skip over the records known to have |
| + // issue with some printers. See Emf::Record::SafePlayback implementation for |
| + // details. |
| + virtual bool SafePlayback(HDC hdc) const = 0; |
| + |
| + // Retrieves the EMF stream. It is a helper function. |
| + virtual bool GetData(std::vector<uint8>* buffer) const = 0; |
| + |
| + // Inserts a custom GDICOMMENT records indicating StartPage/EndPage calls |
| + // (since StartPage and EndPage do not work in a metafile DC). Only valid |
| + // when hdc_ is non-NULL. |
| + virtual bool StartPage() = 0; |
| + |
| + virtual HENHMETAFILE emf() const = 0; |
| + virtual HDC hdc() const = 0; |
| +#elif defined(OS_MACOSX) |
| + // Prepares a new pdf page at specified |content_origin| with the given |
| + // |page_size| and a |scale_factor| to use for the drawing. |
| + virtual CGContextRef StartPage(const gfx::Size& page_size, |
| + const gfx::Point& content_origin, |
| + const float& scale_factor) = 0; |
| + |
| + // Renders the given page into |rect| in the given context. |
| + // Pages use a 1-based index. The rendering uses the following arguments |
| + // to determine scaling and translation factors. |
| + // |shrink_to_fit| specifies whether the output should be shrunk to fit the |
| + // supplied |rect| if the page size is larger than |rect| in any dimension. |
| + // If this is false, parts of the PDF page that lie outside the bounds will be |
| + // clipped. |
| + // |stretch_to_fit| specifies whether the output should be stretched to fit |
| + // the supplied bounds if the page size is smaller than |rect| in all |
| + // dimensions. |
| + // |center_horizontally| specifies whether the final image (after any scaling |
| + // is done) should be centered horizontally within the given |rect|. |
| + // |center_vertically| specifies whether the final image (after any scaling |
| + // is done) should be centered vertically within the given |rect|. |
| + // Note that all scaling preserves the original aspect ratio of the page. |
| + virtual bool RenderPage(unsigned int page_number, |
| + CGContextRef context, |
| + const CGRect rect, |
| + bool shrink_to_fit, |
| + bool stretch_to_fit, |
| + bool center_horizontally, |
| + bool center_vertically) const = 0; |
| + |
| + // Get the context for rendering to the PDF. |
| + virtual CGContextRef context() = 0; |
| #elif defined(OS_POSIX) |
| -#include "printing/native_metafile_linux.h" |
| + // Sets raw PDF data for the document. This is used when a cairo drawing |
| + // surface has already been created. This method will cause all subsequent |
| + // drawing on the surface to be discarded (in Close()). If Init() has not yet |
| + // been called this method simply calls the second version of Init. |
| + virtual bool SetRawData(const void* src_buffer, uint32 src_buffer_size) = 0; |
| + |
| + // Prepares a new cairo surface/context for rendering a new page. |
| + // The unit is in point (=1/72 in). |
| + // Returns NULL when failed. |
| + virtual cairo_t* StartPage(double width_in_points, |
| + double height_in_points, |
| + double margin_top_in_points, |
| + double margin_right_in_points, |
| + double margin_bottom_in_points, |
| + double margin_left_in_points) = 0; |
| #endif |
| +}; |
| + |
| +} // namespace printing |
| #endif // PRINTING_NATIVE_METAFILE_H_ |