Another take on menu's. This uses the hosting menu scroll view container as a menuba...
[chromium-blink-merge.git] / printing / pdf_metafile_mac.h
blob264e970cedc28728fb4d350de02a09a2a4941add
1 // Copyright (c) 2009 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_PDF_METAFILE_MAC_H_
6 #define PRINTING_PDF_METAFILE_MAC_H_
8 #include <ApplicationServices/ApplicationServices.h>
9 #include <CoreFoundation/CoreFoundation.h>
11 #include "base/basictypes.h"
12 #include "base/scoped_cftyperef.h"
14 namespace gfx {
15 class Rect;
17 class FilePath;
19 namespace printing {
21 // This class creates a graphics context that renders into a PDF data stream.
22 class PdfMetafile {
23 public:
24 // To create PDF data, callers should also call Init() to set up the
25 // rendering context.
26 // To create a metafile from existing Data, callers should also call
27 // Init(const void*, uint32).
28 PdfMetafile();
30 ~PdfMetafile() {}
32 // Initializes a new metafile, and returns a drawing context for rendering
33 // into the PDF. Returns NULL on failure.
34 // Note: The returned context *must not be retained* past Close(). If it is,
35 // the data returned from GetData will not be valid PDF data.
36 CGContextRef Init();
38 // Initializes a copy of metafile from PDF data. Returns true on success.
39 bool Init(const void* src_buffer, uint32 src_buffer_size);
40 // Alias for Init, for compatibility with Emf-based code.
41 bool CreateFromData(const void* src_buffer, uint32 src_buffer_size);
43 // Prepares a new pdf page with the given width and height and a scale
44 // factor to use for the drawing.
45 void StartPage(double width, double height, double scale_factor);
47 // Closes the current page.
48 void FinishPage();
50 // Closes the PDF file; no further rendering is allowed.
51 void Close();
53 // Renders the given page into |rect| in the given context.
54 // Pages use a 1-based index. The rendering uses the following arguments
55 // to determine scaling and translation factors.
56 // |shrink_to_fit| specifies whether the output should be shrunk to fit the
57 // supplied |rect| if the page size is larger than |rect| in any dimension.
58 // If this is false, parts of the PDF page that lie outside the bounds will be
59 // clipped.
60 // |stretch_to_fit| specifies whether the output should be stretched to fit
61 // the supplied bounds if the page size is smaller than |rect| in all
62 // dimensions.
63 // |center_horizontally| specifies whether the final image (after any scaling
64 // is done) should be centered horizontally within the given |rect|.
65 // |center_vertically| specifies whether the final image (after any scaling
66 // is done) should be centered vertically within the given |rect|.
67 // Note that all scaling preserves the original aspect ratio of the page.
68 bool RenderPage(unsigned int page_number, CGContextRef context,
69 const CGRect rect, bool shrink_to_fit, bool stretch_to_fit,
70 bool center_horizontally, bool center_vertically) const;
71 unsigned int GetPageCount() const;
73 // Returns the bounds of the given page.
74 // Pages use a 1-based index.
75 gfx::Rect GetPageBounds(unsigned int page_number) const;
77 // Returns the size of the underlying PDF data. Only valid after Close() has
78 // been called.
79 uint32 GetDataSize() const;
81 // Copies the first |dst_buffer_size| bytes of the PDF data into |dst_buffer|.
82 // Only valid after Close() has been called.
83 // Returns true if the copy succeeds.
84 bool GetData(void* dst_buffer, uint32 dst_buffer_size) const;
86 // Saves the raw PDF data to the given file. For testing only.
87 // Returns true if writing succeeded.
88 bool SaveTo(const FilePath& file_path) const;
90 private:
91 // Returns a CGPDFDocumentRef version of pdf_data_.
92 CGPDFDocumentRef GetPDFDocument() const;
94 // Context for rendering to the pdf.
95 scoped_cftyperef<CGContextRef> context_;
97 // PDF backing store.
98 scoped_cftyperef<CFMutableDataRef> pdf_data_;
100 // Lazily-created CGPDFDocument representation of pdf_data_.
101 mutable scoped_cftyperef<CGPDFDocumentRef> pdf_doc_;
103 // Whether or not a page is currently open.
104 bool page_is_open_;
106 DISALLOW_COPY_AND_ASSIGN(PdfMetafile);
109 } // namespace printing
111 #endif // PRINTING_PDF_METAFILE_MAC_H_