Coverage Report

Created: 2018-09-25 14:53

/work/obj-fuzz/dist/include/mozilla/gfx/PrintTarget.h
Line
Count
Source (jump to first uncovered line)
1
/* -*- Mode: C++; tab-width: 20; indent-tabs-mode: nil; c-basic-offset: 4 -*-
2
 * This Source Code Form is subject to the terms of the Mozilla Public
3
 * License, v. 2.0. If a copy of the MPL was not distributed with this
4
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
5
6
#ifndef MOZILLA_GFX_PRINTTARGET_H
7
#define MOZILLA_GFX_PRINTTARGET_H
8
9
#include <functional>
10
11
#include "mozilla/RefPtr.h"
12
#include "mozilla/gfx/2D.h"
13
#include "nsISupportsImpl.h"
14
#include "nsStringFwd.h"
15
16
namespace mozilla {
17
namespace gfx {
18
19
class DrawEventRecorder;
20
21
/**
22
 * A class that is used to draw output that is to be sent to a printer or print
23
 * preview.
24
 *
25
 * This class wraps a cairo_surface_t* and provides access to it via a
26
 * DrawTarget.  The various checkpointing methods manage the state of the
27
 * platform specific cairo_surface_t*.
28
 */
29
class PrintTarget {
30
public:
31
  typedef std::function<void(nsresult)> PageDoneCallback;
32
33
  NS_INLINE_DECL_REFCOUNTING(PrintTarget);
34
35
  /// Must be matched 1:1 by an EndPrinting/AbortPrinting call.
36
  virtual nsresult BeginPrinting(const nsAString& aTitle,
37
                                 const nsAString& aPrintToFileName,
38
                                 int32_t aStartPage,
39
                                 int32_t aEndPage) {
40
    return NS_OK;
41
  }
42
  virtual nsresult EndPrinting() {
43
    return NS_OK;
44
  }
45
  virtual nsresult AbortPrinting() {
46
#ifdef DEBUG
47
    mHasActivePage = false;
48
#endif
49
    return NS_OK;
50
  }
51
  virtual nsresult BeginPage() {
52
#ifdef DEBUG
53
    MOZ_ASSERT(!mHasActivePage, "Missing EndPage() call");
54
    mHasActivePage = true;
55
#endif
56
    return NS_OK;
57
  }
58
  virtual nsresult EndPage() {
59
#ifdef DEBUG
60
    mHasActivePage = false;
61
#endif
62
    return NS_OK;
63
  }
64
65
  /**
66
   * Releases the resources used by this PrintTarget.  Typically this should be
67
   * called after calling EndPrinting().  Calling this more than once is
68
   * allowed, but subsequent calls are a no-op.
69
   *
70
   * Note that any DrawTarget obtained from this PrintTarget will no longer be
71
   * useful after this method has been called.
72
   */
73
  virtual void Finish();
74
75
  /**
76
   * Returns true if to print landscape our consumers must apply a 90 degrees
77
   * rotation to our DrawTarget.
78
   */
79
  virtual bool RotateNeededForLandscape() const {
80
    return false;
81
  }
82
83
0
  const IntSize& GetSize() const {
84
0
    return mSize;
85
0
  }
86
87
  /**
88
   * Makes a DrawTarget to draw the printer output to, or returns null on
89
   * failure.
90
   *
91
   * If aRecorder is passed a recording DrawTarget will be created instead of
92
   * the type of DrawTarget that would normally be returned for a particular
93
   * subclass of this class.  This argument is only intended to be used in
94
   * the e10s content process if printing output can't otherwise be transfered
95
   * over to the parent process using the normal DrawTarget type.
96
   *
97
   * NOTE: this should only be called between BeginPage()/EndPage() calls, and
98
   * the returned DrawTarget should not be drawn to after EndPage() has been
99
   * called.
100
   *
101
   * XXX For consistency with the old code this takes a size parameter even
102
   * though we already have the size passed to our subclass's CreateOrNull
103
   * factory methods.  The size passed to the factory method comes from
104
   * nsIDeviceContextSpec::MakePrintTarget overrides, whereas the size
105
   * passed to us comes from nsDeviceContext::CreateRenderingContext.  In at
106
   * least one case (nsDeviceContextSpecAndroid::MakePrintTarget) these are
107
   * different.  At some point we should align the two sources and get rid of
108
   * this method's size parameter.
109
   *
110
   * XXX For consistency with the old code this returns a new DrawTarget for
111
   * each call.  Perhaps we can create and cache a DrawTarget in our subclass's
112
   * CreateOrNull factory methods and return that on each call?  Currently that
113
   * seems to cause Mochitest failures on Windows though, which coincidentally
114
   * is the only platform where we get passed an aRecorder.  Probably the
115
   * issue is that we get called more than once with a different aRecorder, so
116
   * storing one recording DrawTarget for our lifetime doesn't currently work.
117
   *
118
   * XXX Could we pass aRecorder to our subclass's CreateOrNull factory methods?
119
   * We'd need to check that our consumers always pass the same aRecorder for
120
   * our entire lifetime.
121
   *
122
   * XXX Once PrintTargetThebes is removed this can become non-virtual.
123
   *
124
   * XXX In the long run, this class and its sub-classes should be converted to
125
   * use STL classes and mozilla::RefCounted<> so the can be moved to Moz2D.
126
   *
127
   * TODO: Consider adding a SetDPI method that calls
128
   * cairo_surface_set_fallback_resolution.
129
   */
130
  virtual already_AddRefed<DrawTarget>
131
  MakeDrawTarget(const IntSize& aSize,
132
                 DrawEventRecorder* aRecorder = nullptr);
133
134
  /**
135
   * Returns a reference DrawTarget. Unlike MakeDrawTarget, this method is not
136
   * restricted to being called between BeginPage()/EndPage() calls, and the
137
   * returned DrawTarget is still valid to use after EndPage() has been called.
138
   */
139
  virtual already_AddRefed<DrawTarget> GetReferenceDrawTarget();
140
141
  /**
142
   * If IsSyncPagePrinting returns true, then a user can assume the content of
143
   * a page was already printed after EndPage().
144
   * If IsSyncPagePrinting returns false, then a user should register a
145
   * callback function using RegisterPageDoneCallback to receive page print
146
   * done notifications.
147
   */
148
  virtual bool IsSyncPagePrinting() const { return true; }
149
  void RegisterPageDoneCallback(PageDoneCallback&& aCallback);
150
  void UnregisterPageDoneCallback();
151
152
  static void AdjustPrintJobNameForIPP(const nsAString& aJobName,
153
                                       nsCString& aAdjustedJobName);
154
  static void AdjustPrintJobNameForIPP(const nsAString& aJobName,
155
                                       nsString& aAdjustedJobName);
156
157
protected:
158
159
  // Only created via subclass's constructors
160
  explicit PrintTarget(cairo_surface_t* aCairoSurface, const IntSize& aSize);
161
162
  // Protected because we're refcounted
163
  virtual ~PrintTarget();
164
165
  static already_AddRefed<DrawTarget>
166
  CreateWrapAndRecordDrawTarget(DrawEventRecorder* aRecorder,
167
                                DrawTarget* aDrawTarget);
168
169
  cairo_surface_t* mCairoSurface;
170
  RefPtr<DrawTarget> mRefDT; // reference DT
171
172
  IntSize mSize;
173
  bool mIsFinished;
174
#ifdef DEBUG
175
  bool mHasActivePage;
176
#endif
177
178
  PageDoneCallback mPageDoneCallback;
179
};
180
181
} // namespace gfx
182
} // namespace mozilla
183
184
#endif /* MOZILLA_GFX_PRINTTARGET_H */