summaryrefslogtreecommitdiffstats
path: root/printing/pdf_ps_metafile_cairo.h
blob: 6cfe5fffb2bdcd3283599de74681a4461c4209e4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
// Copyright (c) 2011 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef PRINTING_PDF_PS_METAFILE_CAIRO_H_
#define PRINTING_PDF_PS_METAFILE_CAIRO_H_

#include <string>

#include "base/basictypes.h"
#include "base/gtest_prod_util.h"
#include "printing/native_metafile.h"

typedef struct _cairo_surface cairo_surface_t;

namespace printing {

// This class uses Cairo graphics library to generate PDF stream and stores
// rendering results in a string buffer.
class PdfPsMetafile : public NativeMetafile {
 public:
  virtual ~PdfPsMetafile();

  // NativeMetafile methods.
  virtual bool Init();
  virtual bool Init(const void* src_buffer, uint32 src_buffer_size);

  virtual cairo_t* StartPage(const gfx::Size& page_size,
                             double margin_top_in_points,
                             double margin_left_in_points);
  virtual bool FinishPage();
  virtual bool Close();

  virtual uint32 GetDataSize() const;
  virtual bool GetData(void* dst_buffer, uint32 dst_buffer_size) const;

  virtual bool SaveTo(const FilePath& file_path) const;

  virtual gfx::Rect GetPageBounds(unsigned int page_number) const;
  virtual unsigned int GetPageCount() const;

  virtual cairo_t* context() const;

  virtual bool SetRawData(const void* src_buffer, uint32 src_buffer_size);

#if defined(OS_CHROMEOS)
  virtual bool SaveToFD(const base::FileDescriptor& fd) const;
#endif  // if defined(OS_CHROMEOS)

  // Returns the PdfPsMetafile object that owns the given context. Returns NULL
  // if the context was not created by a PdfPsMetafile object.
  static PdfPsMetafile* FromCairoContext(cairo_t* context);

 protected:
  PdfPsMetafile();

 private:
  friend class NativeMetafileFactory;
  FRIEND_TEST_ALL_PREFIXES(PdfPsTest, Pdf);

  // Cleans up all resources.
  void CleanUpAll();

  // Cairo surface and context for entire PDF file.
  cairo_surface_t* surface_;
  cairo_t* context_;

  // Buffer stores PDF contents for entire PDF file.
  std::string data_;
  // Buffer stores raw PDF contents set by SetRawPageData.
  std::string raw_override_data_;

  DISALLOW_COPY_AND_ASSIGN(PdfPsMetafile);
};

}  // namespace printing

#endif  // PRINTING_PDF_PS_METAFILE_CAIRO_H_