2018-11-30 22:52:05 +03:00
|
|
|
/* -*- Mode: C++; tab-width: 20; indent-tabs-mode: nil; c-basic-offset: 2 -*-
|
2016-06-03 13:27:31 +03:00
|
|
|
* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
|
|
|
|
|
|
|
#ifndef MOZILLA_GFX_PRINTTARGET_H
|
|
|
|
#define MOZILLA_GFX_PRINTTARGET_H
|
|
|
|
|
2017-11-04 23:36:37 +03:00
|
|
|
#include <functional>
|
|
|
|
|
2016-06-03 13:27:31 +03:00
|
|
|
#include "mozilla/RefPtr.h"
|
|
|
|
#include "mozilla/gfx/2D.h"
|
|
|
|
#include "nsISupportsImpl.h"
|
2017-08-17 02:48:52 +03:00
|
|
|
#include "nsStringFwd.h"
|
2016-06-03 13:27:31 +03:00
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
namespace gfx {
|
|
|
|
|
|
|
|
class DrawEventRecorder;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* A class that is used to draw output that is to be sent to a printer or print
|
|
|
|
* preview.
|
|
|
|
*
|
|
|
|
* This class wraps a cairo_surface_t* and provides access to it via a
|
|
|
|
* DrawTarget. The various checkpointing methods manage the state of the
|
|
|
|
* platform specific cairo_surface_t*.
|
|
|
|
*/
|
|
|
|
class PrintTarget {
|
|
|
|
public:
|
2017-11-04 23:36:37 +03:00
|
|
|
typedef std::function<void(nsresult)> PageDoneCallback;
|
2016-06-03 13:27:31 +03:00
|
|
|
|
|
|
|
NS_INLINE_DECL_REFCOUNTING(PrintTarget);
|
|
|
|
|
|
|
|
/// Must be matched 1:1 by an EndPrinting/AbortPrinting call.
|
|
|
|
virtual nsresult BeginPrinting(const nsAString& aTitle,
|
2016-11-29 10:54:30 +03:00
|
|
|
const nsAString& aPrintToFileName,
|
|
|
|
int32_t aStartPage, int32_t aEndPage) {
|
2016-06-03 13:27:31 +03:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
virtual nsresult EndPrinting() { return NS_OK; }
|
|
|
|
virtual nsresult AbortPrinting() {
|
2016-10-27 21:25:02 +03:00
|
|
|
#ifdef DEBUG
|
|
|
|
mHasActivePage = false;
|
|
|
|
#endif
|
2016-06-03 13:27:31 +03:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
virtual nsresult BeginPage() {
|
2016-10-27 21:25:02 +03:00
|
|
|
#ifdef DEBUG
|
|
|
|
MOZ_ASSERT(!mHasActivePage, "Missing EndPage() call");
|
|
|
|
mHasActivePage = true;
|
|
|
|
#endif
|
2016-06-03 13:27:31 +03:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
virtual nsresult EndPage() {
|
2016-10-27 21:25:02 +03:00
|
|
|
#ifdef DEBUG
|
|
|
|
mHasActivePage = false;
|
|
|
|
#endif
|
2016-06-03 13:27:31 +03:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Releases the resources used by this PrintTarget. Typically this should be
|
|
|
|
* called after calling EndPrinting(). Calling this more than once is
|
|
|
|
* allowed, but subsequent calls are a no-op.
|
|
|
|
*
|
|
|
|
* Note that any DrawTarget obtained from this PrintTarget will no longer be
|
|
|
|
* useful after this method has been called.
|
|
|
|
*/
|
|
|
|
virtual void Finish();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns true if to print landscape our consumers must apply a 90 degrees
|
|
|
|
* rotation to our DrawTarget.
|
|
|
|
*/
|
|
|
|
virtual bool RotateNeededForLandscape() const { return false; }
|
|
|
|
|
|
|
|
const IntSize& GetSize() const { return mSize; }
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Makes a DrawTarget to draw the printer output to, or returns null on
|
|
|
|
* failure.
|
|
|
|
*
|
|
|
|
* If aRecorder is passed a recording DrawTarget will be created instead of
|
|
|
|
* the type of DrawTarget that would normally be returned for a particular
|
|
|
|
* subclass of this class. This argument is only intended to be used in
|
|
|
|
* the e10s content process if printing output can't otherwise be transfered
|
|
|
|
* over to the parent process using the normal DrawTarget type.
|
|
|
|
*
|
2016-10-27 21:25:02 +03:00
|
|
|
* NOTE: this should only be called between BeginPage()/EndPage() calls, and
|
|
|
|
* the returned DrawTarget should not be drawn to after EndPage() has been
|
|
|
|
* called.
|
|
|
|
*
|
2016-06-03 13:27:31 +03:00
|
|
|
* XXX For consistency with the old code this takes a size parameter even
|
|
|
|
* though we already have the size passed to our subclass's CreateOrNull
|
|
|
|
* factory methods. The size passed to the factory method comes from
|
|
|
|
* nsIDeviceContextSpec::MakePrintTarget overrides, whereas the size
|
|
|
|
* passed to us comes from nsDeviceContext::CreateRenderingContext. In at
|
|
|
|
* least one case (nsDeviceContextSpecAndroid::MakePrintTarget) these are
|
|
|
|
* different. At some point we should align the two sources and get rid of
|
|
|
|
* this method's size parameter.
|
|
|
|
*
|
|
|
|
* XXX For consistency with the old code this returns a new DrawTarget for
|
|
|
|
* each call. Perhaps we can create and cache a DrawTarget in our subclass's
|
|
|
|
* CreateOrNull factory methods and return that on each call? Currently that
|
|
|
|
* seems to cause Mochitest failures on Windows though, which coincidentally
|
|
|
|
* is the only platform where we get passed an aRecorder. Probably the
|
|
|
|
* issue is that we get called more than once with a different aRecorder, so
|
|
|
|
* storing one recording DrawTarget for our lifetime doesn't currently work.
|
|
|
|
*
|
|
|
|
* XXX Could we pass aRecorder to our subclass's CreateOrNull factory methods?
|
|
|
|
* We'd need to check that our consumers always pass the same aRecorder for
|
|
|
|
* our entire lifetime.
|
|
|
|
*
|
2016-06-16 19:28:50 +03:00
|
|
|
* XXX Once PrintTargetThebes is removed this can become non-virtual.
|
|
|
|
*
|
2016-06-03 13:27:31 +03:00
|
|
|
* XXX In the long run, this class and its sub-classes should be converted to
|
|
|
|
* use STL classes and mozilla::RefCounted<> so the can be moved to Moz2D.
|
|
|
|
*
|
|
|
|
* TODO: Consider adding a SetDPI method that calls
|
|
|
|
* cairo_surface_set_fallback_resolution.
|
|
|
|
*/
|
|
|
|
virtual already_AddRefed<DrawTarget> MakeDrawTarget(
|
2016-06-06 18:10:28 +03:00
|
|
|
const IntSize& aSize, DrawEventRecorder* aRecorder = nullptr);
|
2016-06-03 13:27:31 +03:00
|
|
|
|
2016-10-27 21:24:12 +03:00
|
|
|
/**
|
|
|
|
* Returns a reference DrawTarget. Unlike MakeDrawTarget, this method is not
|
|
|
|
* restricted to being called between BeginPage()/EndPage() calls, and the
|
2018-01-18 15:45:18 +03:00
|
|
|
* returned DrawTarget is still valid to use after EndPage() has been called.
|
2016-10-27 21:24:12 +03:00
|
|
|
*/
|
2018-01-18 15:45:18 +03:00
|
|
|
virtual already_AddRefed<DrawTarget> GetReferenceDrawTarget();
|
2016-10-27 21:24:12 +03:00
|
|
|
|
2017-11-04 23:36:37 +03:00
|
|
|
/**
|
|
|
|
* If IsSyncPagePrinting returns true, then a user can assume the content of
|
|
|
|
* a page was already printed after EndPage().
|
|
|
|
* If IsSyncPagePrinting returns false, then a user should register a
|
|
|
|
* callback function using RegisterPageDoneCallback to receive page print
|
|
|
|
* done notifications.
|
|
|
|
*/
|
|
|
|
virtual bool IsSyncPagePrinting() const { return true; }
|
|
|
|
void RegisterPageDoneCallback(PageDoneCallback&& aCallback);
|
|
|
|
void UnregisterPageDoneCallback();
|
|
|
|
|
2017-10-13 09:47:33 +03:00
|
|
|
static void AdjustPrintJobNameForIPP(const nsAString& aJobName,
|
|
|
|
nsCString& aAdjustedJobName);
|
2017-10-13 09:48:39 +03:00
|
|
|
static void AdjustPrintJobNameForIPP(const nsAString& aJobName,
|
|
|
|
nsString& aAdjustedJobName);
|
2017-10-13 09:47:33 +03:00
|
|
|
|
2016-06-03 13:27:31 +03:00
|
|
|
protected:
|
|
|
|
// Only created via subclass's constructors
|
2016-06-06 18:10:28 +03:00
|
|
|
explicit PrintTarget(cairo_surface_t* aCairoSurface, const IntSize& aSize);
|
2016-06-03 13:27:31 +03:00
|
|
|
|
|
|
|
// Protected because we're refcounted
|
|
|
|
virtual ~PrintTarget();
|
|
|
|
|
2017-03-22 22:10:42 +03:00
|
|
|
static already_AddRefed<DrawTarget> CreateWrapAndRecordDrawTarget(
|
2017-06-12 23:52:29 +03:00
|
|
|
DrawEventRecorder* aRecorder, DrawTarget* aDrawTarget);
|
2016-06-16 19:28:50 +03:00
|
|
|
|
2016-06-06 18:10:28 +03:00
|
|
|
cairo_surface_t* mCairoSurface;
|
2016-10-27 21:24:12 +03:00
|
|
|
RefPtr<DrawTarget> mRefDT; // reference DT
|
2017-03-21 12:36:08 +03:00
|
|
|
|
2016-06-03 13:27:31 +03:00
|
|
|
IntSize mSize;
|
|
|
|
bool mIsFinished;
|
2016-10-27 21:25:02 +03:00
|
|
|
#ifdef DEBUG
|
|
|
|
bool mHasActivePage;
|
|
|
|
#endif
|
2017-11-04 23:36:37 +03:00
|
|
|
|
|
|
|
PageDoneCallback mPageDoneCallback;
|
2016-06-03 13:27:31 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace gfx
|
|
|
|
} // namespace mozilla
|
|
|
|
|
|
|
|
#endif /* MOZILLA_GFX_PRINTTARGET_H */
|